Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Commit

Permalink
Fix path issues with pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
Maigret Aurélien committed May 9, 2020
1 parent 9dc5c41 commit f42ac88
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
7 changes: 5 additions & 2 deletions server/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class GCEConfigure {
}

const projectPath = this._path(project.path)
if (!await this._checkDirectory(`${slug}.path`, projectPath)) {
if (!await this._checkDirectory(`${slug}.path`, projectPath, true)) {
return
}

Expand Down Expand Up @@ -331,8 +331,11 @@ class GCEConfigure {
return fullpath
}

async _checkDirectory (slug, directory) {
async _checkDirectory (slug, directory, mustBeAbsolute) {
try {
if (mustBeAbsolute && !path.isAbsolute(directory)) {
throw new Error('Must be an absolute directory')
}
const stat = await fs.stat(directory)
if (!stat.isDirectory()) {
throw new Error('Not a directory')
Expand Down
12 changes: 8 additions & 4 deletions server/http.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const logger = require('./logger')
const http = require('http')
const path = require('path')
const fs = require('fs').promises
const fs = require('fs')
const WebSocket = require('ws')

class GCEHttp {
Expand Down Expand Up @@ -68,12 +68,14 @@ class GCEHttp {
async _onRequest (req, res) {
try {
let fullpath = path.normalize(req.url)
if (fullpath === '/') {
if (fullpath === '/' || fullpath === '\\') {
fullpath = '/app.html'
}

try {
const stat = await fs.stat(path.join(__dirname, '../public/', fullpath))
// Don't use fs promises, they are issues with them on pkg: https://github.com/zeit/pkg/issues/455
// const stat = await fs.promises.stat(path.join(__dirname, '../public/', fullpath))
const stat = fs.statSync(path.join(__dirname, '../public/', fullpath))
if (!stat.isFile()) {
throw new Error('Not a file')
}
Expand All @@ -83,7 +85,9 @@ class GCEHttp {
}

logger.debug('GCE HTTP', req.headers.host, fullpath)
const content = await fs.readFile(path.join(__dirname, '../public/', fullpath))
// Don't use fs promises, they are issues with them on pkg: https://github.com/zeit/pkg/issues/455
// const content = await fs.promises.readFile(path.join(__dirname, '../public/', fullpath))
const content = fs.readFileSync(path.join(__dirname, '../public/', fullpath))
const extension = path.parse(fullpath).ext
res.writeHead(200, { 'Content-type': this.mimeTypes[extension] || 'text/plain' })
res.end(content)
Expand Down
3 changes: 2 additions & 1 deletion server/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const utils = require('./utils')
const logger = require('./logger')
const GCEServer = require('./server')
const path = require('path')

module.exports = async function ({ serverPort = 6730, loadBalancerPort = 6731, ssl = null, configDirectories = [] }) {
try {
Expand All @@ -10,7 +11,7 @@ module.exports = async function ({ serverPort = 6730, loadBalancerPort = 6731, s

const configs = []
for (const configDirectory of configDirectories) {
configs.push(require(configDirectory))
configs.push(require(path.join(process.cwd(), configDirectory)))
}

const config = utils.mergeDeep(
Expand Down

0 comments on commit f42ac88

Please sign in to comment.