Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/nuxi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"defu": "^6.1.4",
"exsolve": "^1.0.7",
"fuse.js": "^7.1.0",
"get-port-please": "^3.2.0",
"giget": "^2.0.0",
"h3": "^1.15.3",
"httpxy": "^0.1.7",
Expand Down
5 changes: 2 additions & 3 deletions packages/nuxi/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import { fork } from 'node:child_process'
import process from 'node:process'

import { defineCommand } from 'citty'
import { isSocketSupported } from 'get-port-please'
import { createProxyServer } from 'httpxy'
import { listen } from 'listhen'
import { getArgs as getListhenArgs, parseArgs as parseListhenArgs } from 'listhen/cli'
import { resolve } from 'pathe'
import { satisfies } from 'semver'
import { isBun, isTest } from 'std-env'
import { isBun, isTest, provider } from 'std-env'

import { initialize } from '../dev'
import { renderError } from '../dev/error'
Expand Down Expand Up @@ -136,7 +135,7 @@ const command = defineCommand({

const nuxtSocketEnv = process.env.NUXT_SOCKET ? process.env.NUXT_SOCKET === '1' : undefined

const useSocket = nuxtSocketEnv ?? (nuxtOptions._majorVersion === 4 && await isSocketSupported())
const useSocket = nuxtSocketEnv ?? (nuxtOptions._majorVersion === 4 && provider !== 'stackblitz')

const urls = await devProxy.listener.getURLs()
// run initially in in no-fork mode
Expand Down
41 changes: 34 additions & 7 deletions packages/nuxi/src/dev/socket.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import type { RequestListener } from 'node:http'
import { existsSync, unlinkSync } from 'node:fs'
import { Server } from 'node:http'
import os from 'node:os'
import process from 'node:process'
import { join } from 'pathe'

import { cleanSocket, getSocketAddress } from 'get-port-please'
function generateSocketPath(prefix: string): string {
const timestamp = Date.now()
const random = Math.random().toString(36).slice(2, 8)

if (process.platform === 'win32') {
// Windows named pipes
return `\\\\.\\pipe\\${prefix}-${timestamp}-${random}`
}

// Unix domain sockets
return join(os.tmpdir(), `${prefix}-${timestamp}-${random}.sock`)
}

export function formatSocketURL(socketPath: string, ssl = false): string {
const protocol = ssl ? 'https' : 'http'
Expand Down Expand Up @@ -30,12 +44,17 @@ export function parseSocketURL(url: string): { socketPath: string, protocol: 'ht
}

export async function createSocketListener(handler: RequestListener, ssl = false) {
const socketPath = getSocketAddress({
name: 'nuxt-dev',
random: true,
})
const socketPath = generateSocketPath('nuxt-dev')
const server = new Server(handler)
await cleanSocket(socketPath)

if (process.platform !== 'win32' && existsSync(socketPath)) {
try {
unlinkSync(socketPath)
}
catch {
// suppress errors if the socket file cannot be removed
}
}
await new Promise<void>(resolve => server.listen({ path: socketPath }, resolve))
const url = formatSocketURL(socketPath, ssl)
return {
Expand All @@ -51,7 +70,15 @@ export async function createSocketListener(handler: RequestListener, ssl = false
await new Promise<void>((resolve, reject) => server.close(err => err ? reject(err) : resolve()))
}
finally {
await cleanSocket(socketPath)
// Clean up socket file on Unix systems
if (process.platform !== 'win32') {
try {
unlinkSync(socketPath)
}
catch {
// suppress errors
}
}
}
},
getURLs: async () => [{ url, type: 'network' as const }],
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"defu": "^6.1.4",
"exsolve": "^1.0.7",
"fuse.js": "^7.1.0",
"get-port-please": "^3.2.0",
"giget": "^2.0.0",
"h3": "^1.15.3",
"httpxy": "^0.1.7",
Expand All @@ -63,6 +62,7 @@
"devDependencies": {
"@nuxt/schema": "^3.17.7",
"@types/node": "^22.16.3",
"get-port-please": "^3.2.0",
"rollup": "^4.45.0",
"rollup-plugin-visualizer": "^6.0.3",
"typescript": "^5.8.3",
Expand Down
9 changes: 3 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading