Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unix domain sockets and windows named pipe support #104

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ Option can be a function for Node.js `upgrade` handler (`(req, head) => void`) o

When using dev server CLI, you can easily use `--ws` and a named export called `websocket` to define [CrossWS Hooks](https://github.com/unjs/crossws) with HMR support!

### `ipc [name]`

Enables IPC: Unix domain sockets are used on unixoid systems - instead of listening to network interfaces - and named pipes on windows (unix: `$PWD/listhen.sock`; windows: `\\?\pipe\listhen`). It's also possible to pass an absolute socket path and full pipe path.

## License

<!-- automd:contributors license=MIT author="pi0" -->
Expand Down
14 changes: 13 additions & 1 deletion src/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { networkInterfaces } from "node:os";
import { networkInterfaces, platform } from "node:os";
import { relative } from "pathe";
import { colors } from "consola/utils";
import { consola } from "consola";
Expand Down Expand Up @@ -88,6 +88,18 @@
return preferPublic ? "" : "localhost";
}

export function getSocketPath(name: true | string) {
const _name = typeof name === "string" && name.length > 0 ? name : "listhen";
if (platform() === "win32") {
if (_name.startsWith("\\\\?\\pipe\\")) {
return _name;
}
return `\\\\?\\pipe\\${_name}`;
}

Check warning on line 98 in src/_utils.ts

View check run for this annotation

Codecov / codecov/patch

src/_utils.ts#L94-L98

Added lines #L94 - L98 were not covered by tests

return _name === "listhen" ? "listhen.sock" : _name;
}

export function getPublicURL(
listhenOptions: ListenOptions,
baseURL?: string,
Expand Down
6 changes: 6 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@
description: "Open a tunnel using https://github.com/unjs/untun",
required: false,
},
socket: {
description:
"Listen on a Unix Domain Socket/Windows Pipe, optionally with custom name or given absolute path",
required: false,
},

Check warning on line 148 in src/cli.ts

View check run for this annotation

Codecov / codecov/patch

src/cli.ts#L144-L148

Added lines #L144 - L148 were not covered by tests
} as const satisfies ArgsDef;
}

Expand All @@ -158,6 +163,7 @@
publicURL: args.publicURL,
public: args.public,
tunnel: args.tunnel,
socket: args.socket as boolean | string | undefined,

Check warning on line 166 in src/cli.ts

View check run for this annotation

Codecov / codecov/patch

src/cli.ts#L166

Added line #L166 was not covered by tests
https: args.https
? <HTTPSOptions>{
cert: args["https.cert"],
Expand Down
36 changes: 31 additions & 5 deletions src/listen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import { renderUnicodeCompact as renderQRCode } from "uqr";
import type { Tunnel } from "untun";
import type { AdapterOptions as CrossWSOptions } from "crossws";
import { isAbsolute, sep } from "pathe";
import { open } from "./lib/open";
import type {
ListenOptions,
Expand All @@ -27,6 +28,7 @@
isLocalhost,
isAnyhost,
getPublicURL,
getSocketPath,
generateURL,
getDefaultHost,
validateHostname,
Expand Down Expand Up @@ -62,6 +64,7 @@
isTest: _isTest,
isProd: _isProd,
public: _public,
socket: false,
autoClose: true,
});

Expand Down Expand Up @@ -110,22 +113,28 @@

// --- Listen ---
let server: Server | HTTPServer;

const serverOptions = listhenOptions.socket
? { path: getSocketPath(listhenOptions.socket) }
: { port, host: listhenOptions.hostname };

let https: Listener["https"] = false;
const httpsOptions = listhenOptions.https as HTTPSOptions;

let _addr: AddressInfo;
if (httpsOptions) {
https = await resolveCertificate(httpsOptions);
server = createHTTPSServer(https, handle);
addShutdown(server);
// @ts-ignore
await promisify(server.listen.bind(server))(port, listhenOptions.hostname);
await promisify(server.listen.bind(server))(serverOptions);
_addr = server.address() as AddressInfo;
listhenOptions.port = _addr.port;
} else {
server = createServer(handle);
addShutdown(server);
// @ts-ignore
await promisify(server.listen.bind(server))(port, listhenOptions.hostname);
await promisify(server.listen.bind(server))(serverOptions);
_addr = server.address() as AddressInfo;
listhenOptions.port = _addr.port;
}
Expand All @@ -150,7 +159,9 @@

// --- GetURL Utility ---
const getURL = (host = listhenOptions.hostname, baseURL?: string) =>
generateURL(host, listhenOptions, baseURL);
serverOptions.path
? `unix+http${listhenOptions.https ? "s" : ""}://${serverOptions.path}`
: generateURL(host, listhenOptions, baseURL);

// --- Start Tunnel ---
let tunnel: Tunnel | undefined;
Expand Down Expand Up @@ -193,6 +204,17 @@
}
};

if (serverOptions.path) {
let _path = serverOptions.path;
const currentDirPath = `.${sep}`;

if (!(isAbsolute(_path) || _path.startsWith(currentDirPath))) {
_path = currentDirPath + _path;
}
_addURL("local", _path);
return urls;
}

Check warning on line 216 in src/listen.ts

View check run for this annotation

Codecov / codecov/patch

src/listen.ts#L208-L216

Added lines #L208 - L216 were not covered by tests

// Add public URL
const publicURL =
getURLOptions.publicURL ||
Expand Down Expand Up @@ -259,8 +281,12 @@

for (const url of urls) {
const type = typeMap[url.type];
let infix = "";
if (listhenOptions.socket && url.type === "local") {
infix = listhenOptions.https ? " (https)" : " (http)";
}

Check warning on line 287 in src/listen.ts

View check run for this annotation

Codecov / codecov/patch

src/listen.ts#L286-L287

Added lines #L286 - L287 were not covered by tests
const label = getColor(type[1])(
` ➜ ${(type[0] + ":").padEnd(8, " ")}${nameSuffix} `,
` ➜ ${(type[0] + infix + ":").padEnd(8, " ")}${nameSuffix} `,
);
let suffix = "";
if (url === firstLocalUrl && listhenOptions.clipboard) {
Expand All @@ -272,7 +298,7 @@
lines.push(`${label} ${formatURL(url.url)}${suffix}`);
}

if (!firstPublicUrl) {
if (!firstPublicUrl && !listhenOptions.socket) {
lines.push(
colors.gray(` ➜ Network: use ${colors.white("--host")} to expose`),
);
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export interface ListenOptions {
| boolean
| CrossWSOptions
| ((req: IncomingMessage, head: Buffer) => void);
/**
* Listhen on a unix domain socket/windows pipe, optionally with custom name
*/
socket: boolean | string;
}

export type GetURLOptions = Pick<
Expand Down
Loading