fix(nitro): use random port for stackblitz (#1666)

This commit is contained in:
pooya parsa 2021-11-03 16:06:42 +01:00 committed by GitHub
parent c8b07a6a1c
commit 3cd4494770
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 17 deletions

View File

@ -34,7 +34,7 @@
"rc9": "^1.2.0",
"scule": "^0.2.1",
"semver": "^7.3.5",
"std-env": "3.0.0-0",
"std-env": "^3.0.0",
"ufo": "^0.7.9",
"unctx": "^1.0.2",
"untyped": "^0.2.11"

View File

@ -63,7 +63,7 @@
"rollup-plugin-visualizer": "^5.5.2",
"serve-placeholder": "^1.2.4",
"serve-static": "^1.14.1",
"std-env": "3.0.0-0",
"std-env": "^3.0.0",
"table": "^6.7.2",
"ufo": "^0.7.9",
"unenv": "^0.4.0",

View File

@ -4,14 +4,18 @@ import { tmpdir } from 'os'
import { join } from 'path'
import { mkdirSync } from 'fs'
import { threadId, parentPort } from 'worker_threads'
import { isWindows, provider } from 'std-env'
import { handle } from '../server'
const server = new Server(handle)
function createSocket () {
const isWin = process.platform === 'win32'
function getAddress () {
// https://github.com/nuxt/framework/issues/1636
if (provider === 'stackblitz' || process.env.NITRO_NO_UNIX_SOCKET) {
return '0'
}
const socketName = `worker-${process.pid}-${threadId}.sock`
if (isWin) {
if (isWindows) {
return join('\\\\.\\pipe\\nitro', socketName)
} else {
const socketDir = join(tmpdir(), 'nitro')
@ -20,7 +24,13 @@ function createSocket () {
}
}
const socketPath = createSocket()
server.listen(socketPath, () => {
parentPort.postMessage({ event: 'listen', address: { socketPath } })
const listenAddress = getAddress()
server.listen(listenAddress, () => {
const _address = server.address()
parentPort.postMessage({
event: 'listen',
address: typeof _address === 'string'
? { socketPath: _address }
: `http://localhost:${_address.port}`
})
})

View File

@ -2893,7 +2893,7 @@ __metadata:
rc9: ^1.2.0
scule: ^0.2.1
semver: ^7.3.5
std-env: 3.0.0-0
std-env: ^3.0.0
ufo: ^0.7.9
unbuild: latest
unctx: ^1.0.2
@ -2972,7 +2972,7 @@ __metadata:
rollup-plugin-visualizer: ^5.5.2
serve-placeholder: ^1.2.4
serve-static: ^1.14.1
std-env: 3.0.0-0
std-env: ^3.0.0
table: ^6.7.2
ufo: ^0.7.9
unbuild: latest
@ -18931,13 +18931,6 @@ __metadata:
languageName: node
linkType: hard
"std-env@npm:3.0.0-0":
version: 3.0.0-0
resolution: "std-env@npm:3.0.0-0"
checksum: 0a0b5daaafeadc570ebaa7b36603f3a04f43a6eb893feeeaaae3caa3d0a2fa6f3a5c0f9376bd8245c73d289b2c3b2176de0daa4f75de0f2c6c125e32a95880d8
languageName: node
linkType: hard
"std-env@npm:^2.2.1, std-env@npm:^2.3.0":
version: 2.3.1
resolution: "std-env@npm:2.3.1"
@ -18947,6 +18940,13 @@ __metadata:
languageName: node
linkType: hard
"std-env@npm:^3.0.0":
version: 3.0.0
resolution: "std-env@npm:3.0.0"
checksum: 5a9b91e853e869780c3af90c3d8e4325497da839084daa2edcc5b6ad3585bfd866ecb54d16143a5abbbd0e02ddf05fdd6bcc6e5068638c820652178eb435315c
languageName: node
linkType: hard
"stream-browserify@npm:^2.0.1":
version: 2.0.2
resolution: "stream-browserify@npm:2.0.2"