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: 1 addition & 0 deletions packages/nuxi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"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: 3 additions & 2 deletions packages/nuxi/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ 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, provider } from 'std-env'
import { isBun, isTest } from 'std-env'

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

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

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

const urls = await devProxy.listener.getURLs()
// run initially in in no-fork mode
Expand Down
41 changes: 7 additions & 34 deletions packages/nuxi/src/dev/socket.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
import type { RequestListener } from 'node:http'
import type { AddressInfo } from 'node:net'
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'

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`)
}
import { cleanSocket, getSocketAddress } from 'get-port-please'

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

export async function createSocketListener(handler: RequestListener, proxyAddress?: AddressInfo) {
const socketPath = generateSocketPath('nuxt-dev')
const socketPath = getSocketAddress({
name: 'nuxt-dev',
random: true,
})
const server = new Server(handler)

if (process.platform !== 'win32' && existsSync(socketPath)) {
try {
unlinkSync(socketPath)
}
catch {
// suppress errors if the socket file cannot be removed
}
}
await cleanSocket(socketPath)
await new Promise<void>(resolve => server.listen({ path: socketPath }, resolve))
const url = formatSocketURL(socketPath)
return {
Expand All @@ -67,15 +48,7 @@ export async function createSocketListener(handler: RequestListener, proxyAddres
await new Promise<void>((resolve, reject) => server.close(err => err ? reject(err) : resolve()))
}
finally {
// Clean up socket file on Unix systems
if (process.platform !== 'win32') {
try {
unlinkSync(socketPath)
}
catch {
// suppress errors
}
}
await cleanSocket(socketPath)
}
},
getURLs: async () => [{ url, type: 'network' as const }],
Expand Down
8 changes: 6 additions & 2 deletions packages/nuxi/src/dev/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { loadKit } from '../utils/kit'

import { loadNuxtManifest, resolveNuxtManifest, writeNuxtManifest } from '../utils/nuxt'
import { renderError } from './error'
import { formatSocketURL } from './socket'
import { formatSocketURL, isSocketURL } from './socket'

export type NuxtParentIPCMessage
= | { type: 'nuxt:internal:dev:context', context: NuxtDevContext, socket?: boolean }
Expand Down Expand Up @@ -351,7 +351,11 @@ export function resolveDevServerDefaults(listenOptions: Partial<Pick<ListenOptio
const defaultConfig: Partial<NuxtConfig> = {}

if (urls) {
defaultConfig.vite = { server: { allowedHosts: urls.map(u => new URL(u).hostname) } }
defaultConfig.vite = {
server: {
allowedHosts: urls.filter(u => !isSocketURL(u)).map(u => new URL(u).hostname),
},
}
}

// defined hostname
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,6 +41,7 @@
"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,7 +64,6 @@
"@nuxt/kit": "^4.0.0",
"@nuxt/schema": "^4.0.0",
"@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: 6 additions & 3 deletions pnpm-lock.yaml

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

Loading