|
| 1 | +import { tmpdir } from "node:os"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { Server } from "node:net"; |
| 4 | +import { rm } from "node:fs/promises"; |
| 5 | + |
| 6 | +export interface GetSocketOptions { |
| 7 | + /* Human readable prefix for the socket name */ |
| 8 | + name: string; |
| 9 | + |
| 10 | + /** |
| 11 | + * Use process ID in the socket name. |
| 12 | + */ |
| 13 | + pid?: boolean; |
| 14 | + |
| 15 | + /** |
| 16 | + * Use a random number in the socket name. |
| 17 | + * |
| 18 | + */ |
| 19 | + random?: boolean; |
| 20 | +} |
| 21 | + |
| 22 | +let _nodeMajorVersion: number | undefined; |
| 23 | +let _isSocketSupported: boolean | undefined; |
| 24 | + |
| 25 | +/** |
| 26 | + * Generates a socket address based on the provided options. |
| 27 | + */ |
| 28 | +export function getSocketAddress(opts: GetSocketOptions): string { |
| 29 | + const parts = [ |
| 30 | + opts.name, |
| 31 | + opts.pid ? process.pid : undefined, |
| 32 | + opts.random ? Math.round(Math.random() * 10_000) : undefined, |
| 33 | + ].filter(Boolean); |
| 34 | + |
| 35 | + const socketName = `${parts.join("-")}.sock`; |
| 36 | + |
| 37 | + // Windows: pipe |
| 38 | + if (process.platform === "win32") { |
| 39 | + return join(String.raw`\\.\pipe`, socketName); |
| 40 | + } |
| 41 | + |
| 42 | + // Linux: abstract namespace |
| 43 | + // Fallback to a regular file socket on older Node.js versions to avoid issues |
| 44 | + if (process.platform === "linux") { |
| 45 | + if (_nodeMajorVersion === undefined) { |
| 46 | + _nodeMajorVersion = +process.versions.node.split(".")[0]; |
| 47 | + } |
| 48 | + if (_nodeMajorVersion >= 20) { |
| 49 | + return `\0${socketName}`; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // Unix socket |
| 54 | + return join(tmpdir(), socketName); |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Test if the current environment supports sockets. |
| 59 | + */ |
| 60 | +export async function isSocketSupported(): Promise<boolean> { |
| 61 | + if (_isSocketSupported !== undefined) { |
| 62 | + return _isSocketSupported; |
| 63 | + } |
| 64 | + if (globalThis.process?.versions?.webcontainer) { |
| 65 | + // Seems broken: https://stackblitz.com/edit/stackblitz-starters-1y68uhvu |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + const socketAddress = getSocketAddress({ name: "get-port", random: true }); |
| 70 | + const server = new Server(); |
| 71 | + try { |
| 72 | + await new Promise<void>((resolve, reject) => { |
| 73 | + server.on("error", reject); |
| 74 | + server.listen(socketAddress, resolve); |
| 75 | + }); |
| 76 | + _isSocketSupported = true; |
| 77 | + return true; |
| 78 | + } catch { |
| 79 | + _isSocketSupported = false; |
| 80 | + return false; |
| 81 | + } finally { |
| 82 | + if (server.listening) { |
| 83 | + server.close(); |
| 84 | + await cleanSocket(socketAddress); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export async function cleanSocket(path: string): Promise<void> { |
| 90 | + if (!path || path[0] === "\0" || path.startsWith(String.raw`\\.\pipe`)) { |
| 91 | + // Abstract namespace sockets or invalid paths |
| 92 | + return; |
| 93 | + } |
| 94 | + return rm(path, { force: true, recursive: true }).catch(console.error); |
| 95 | +} |
0 commit comments