@buildkitgg/dev-domains 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/README.md +727 -2
- package/dist/cli/command.d.ts +3 -0
- package/dist/cli/command.js +17 -0
- package/dist/cli/commands.d.ts +125 -0
- package/dist/cli/commands.js +99 -0
- package/dist/cli/init.d.ts +6 -0
- package/dist/cli/init.js +148 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +13 -0
- package/dist/cli/terminal.d.ts +11 -0
- package/dist/cli/terminal.js +43 -0
- package/dist/config/load.d.ts +36 -0
- package/dist/config/load.js +68 -0
- package/dist/config/read.d.ts +2 -0
- package/dist/config/read.js +19 -0
- package/dist/config/schema.d.ts +73 -0
- package/dist/config/schema.js +71 -0
- package/dist/domains/resolve.d.ts +37 -0
- package/dist/domains/resolve.js +54 -0
- package/dist/node.d.ts +10 -0
- package/dist/node.js +16 -0
- package/dist/playwright.d.ts +13 -0
- package/dist/playwright.js +20 -0
- package/dist/project/detect.d.ts +3 -0
- package/dist/project/detect.js +101 -0
- package/dist/project/discovery.d.ts +6 -0
- package/dist/project/discovery.js +70 -0
- package/dist/project/identity.d.ts +6 -0
- package/dist/project/identity.js +44 -0
- package/dist/project/manifest.d.ts +11 -0
- package/dist/project/manifest.js +22 -0
- package/dist/project/package-manager.d.ts +2 -0
- package/dist/project/package-manager.js +28 -0
- package/dist/runtime/command.d.ts +4 -0
- package/dist/runtime/command.js +77 -0
- package/dist/runtime/port.d.ts +5 -0
- package/dist/runtime/port.js +37 -0
- package/dist/runtime/portless.d.ts +2 -0
- package/dist/runtime/portless.js +65 -0
- package/dist/runtime/process.d.ts +13 -0
- package/dist/runtime/process.js +101 -0
- package/dist/runtime/proxy.d.ts +10 -0
- package/dist/runtime/proxy.js +102 -0
- package/dist/runtime/state.d.ts +3 -0
- package/dist/runtime/state.js +8 -0
- package/dist/webhooks/gateway.d.ts +10 -0
- package/dist/webhooks/gateway.js +99 -0
- package/dist/webhooks/tunnel.d.ts +2 -0
- package/dist/webhooks/tunnel.js +27 -0
- package/package.json +65 -3
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { resolveCommand } from "package-manager-detector/commands";
|
|
3
|
+
import { interpolate } from "../domains/resolve.js";
|
|
4
|
+
import { readManifest } from "../project/manifest.js";
|
|
5
|
+
export async function serverCommand(endpoint, packageManager, cwd, variables, port) {
|
|
6
|
+
const values = { ...variables, port: String(port) };
|
|
7
|
+
let command;
|
|
8
|
+
if (endpoint.script) {
|
|
9
|
+
const manifest = await readManifest(cwd);
|
|
10
|
+
const source = manifest.scripts[endpoint.script];
|
|
11
|
+
if (!source) {
|
|
12
|
+
throw new Error(`Script ${endpoint.script} does not exist in ${cwd}`);
|
|
13
|
+
}
|
|
14
|
+
if (/\b(dev-domains|portless)\b/.test(source)) {
|
|
15
|
+
throw new Error(`Script ${endpoint.script} wraps a domain CLI. Point to the underlying server script instead.`);
|
|
16
|
+
}
|
|
17
|
+
command = [];
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
command = (endpoint.command ?? []).map((arg) => interpolate(arg, values));
|
|
21
|
+
}
|
|
22
|
+
if (endpoint.framework === "next") {
|
|
23
|
+
command.push("--hostname", "127.0.0.1", "--port", String(port));
|
|
24
|
+
}
|
|
25
|
+
if (endpoint.framework === "vite") {
|
|
26
|
+
command.push("--host", "127.0.0.1", "--port", String(port), "--strictPort");
|
|
27
|
+
}
|
|
28
|
+
if (endpoint.framework === "storybook") {
|
|
29
|
+
command.push("--host", "127.0.0.1", "--port", String(port), "--exact-port", "--no-open", "--ci");
|
|
30
|
+
}
|
|
31
|
+
if (endpoint.script) {
|
|
32
|
+
const resolved = resolveCommand(packageManager, "run", [endpoint.script, ...command]);
|
|
33
|
+
if (!resolved) {
|
|
34
|
+
throw new Error(`Cannot run scripts with ${packageManager}`);
|
|
35
|
+
}
|
|
36
|
+
return [resolved.command, ...resolved.args];
|
|
37
|
+
}
|
|
38
|
+
return command;
|
|
39
|
+
}
|
|
40
|
+
export function serverEnvironment(endpoint, port, inherited, hostnames = [endpoint.hostname]) {
|
|
41
|
+
const env = {
|
|
42
|
+
...inherited,
|
|
43
|
+
...Object.fromEntries(Object.entries(endpoint.endpoint.env).map(([key, value]) => [
|
|
44
|
+
key,
|
|
45
|
+
interpolate(value, { ...endpoint.variables, port: String(port) })
|
|
46
|
+
]))
|
|
47
|
+
};
|
|
48
|
+
// Package-local binaries take precedence, followed by workspace ancestors and the current Node runtime.
|
|
49
|
+
const bins = [];
|
|
50
|
+
let directory = endpoint.cwd;
|
|
51
|
+
while (true) {
|
|
52
|
+
bins.push(path.join(directory, "node_modules", ".bin"));
|
|
53
|
+
const parent = path.dirname(directory);
|
|
54
|
+
if (directory === parent) {
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
directory = parent;
|
|
58
|
+
}
|
|
59
|
+
const existingPath = Object.entries(env).find(([key]) => key.toUpperCase() === "PATH")?.[1] || "";
|
|
60
|
+
for (const key of Object.keys(env)) {
|
|
61
|
+
if (key.toUpperCase() === "PATH") {
|
|
62
|
+
delete env[key];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
...env,
|
|
67
|
+
PATH: [...bins, path.dirname(process.execPath), existingPath].join(path.delimiter),
|
|
68
|
+
PORT: String(port),
|
|
69
|
+
HOST: "127.0.0.1",
|
|
70
|
+
PORTLESS_URL: endpoint.origin,
|
|
71
|
+
__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS: hostnames.join(","),
|
|
72
|
+
BUILDKIT_ACTIVE: "1",
|
|
73
|
+
DEV_DOMAINS_ORIGIN: endpoint.origin,
|
|
74
|
+
DEV_DOMAINS_HOSTNAME: endpoint.hostname,
|
|
75
|
+
DEV_DOMAINS_WORKTREE: endpoint.variables.worktree
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { createServer } from "node:net";
|
|
2
|
+
import { routeStore } from "./state.js";
|
|
3
|
+
/** Hold an OS-assigned port until its route is registered, then hand it to the server. */
|
|
4
|
+
export async function reservePort() {
|
|
5
|
+
for (let attempt = 0; attempt < 20; attempt++) {
|
|
6
|
+
const server = createServer();
|
|
7
|
+
await new Promise((resolve, reject) => {
|
|
8
|
+
server.once("error", reject);
|
|
9
|
+
server.listen({ host: "127.0.0.1", port: 0, exclusive: true }, resolve);
|
|
10
|
+
});
|
|
11
|
+
const address = server.address();
|
|
12
|
+
let released;
|
|
13
|
+
const release = () => (released ??= new Promise((resolve, reject) => {
|
|
14
|
+
server.close((error) => (error ? reject(error) : resolve()));
|
|
15
|
+
}));
|
|
16
|
+
if (!address || typeof address === "string") {
|
|
17
|
+
await release();
|
|
18
|
+
throw new Error("Could not reserve a local server port");
|
|
19
|
+
}
|
|
20
|
+
// Another launcher may have registered its route and released its socket,
|
|
21
|
+
// but its framework has not bound the port yet.
|
|
22
|
+
try {
|
|
23
|
+
if (routeStore()
|
|
24
|
+
.loadRoutes()
|
|
25
|
+
.some((route) => route.port === address.port)) {
|
|
26
|
+
await release();
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
await release();
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
return { port: address.port, release };
|
|
35
|
+
}
|
|
36
|
+
throw new Error("Could not reserve an unused local server port. Try again.");
|
|
37
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { RouteConflictError } from "portless";
|
|
2
|
+
import { endpointDirectory } from "../domains/resolve.js";
|
|
3
|
+
import { serverCommand, serverEnvironment } from "./command.js";
|
|
4
|
+
import { reservePort } from "./port.js";
|
|
5
|
+
import { runProcess } from "./process.js";
|
|
6
|
+
import { ensureProxy, proxyEnvironment } from "./proxy.js";
|
|
7
|
+
import { routeStore } from "./state.js";
|
|
8
|
+
export async function runEndpoint(project, endpoint, signal, quiet = false) {
|
|
9
|
+
if (process.env.BUILDKIT_ACTIVE) {
|
|
10
|
+
throw new Error("Recursive dev-domains invocation. Configure the underlying server command.");
|
|
11
|
+
}
|
|
12
|
+
await endpointDirectory(project, endpoint);
|
|
13
|
+
const hostnames = (project.config.proxy.domains ?? [project.config.domain]).map((domain) => `${endpoint.name}.${domain}`);
|
|
14
|
+
await ensureProxy(project, signal);
|
|
15
|
+
const store = routeStore();
|
|
16
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
17
|
+
signal?.throwIfAborted();
|
|
18
|
+
const reservation = await reservePort();
|
|
19
|
+
const claimed = [];
|
|
20
|
+
try {
|
|
21
|
+
const command = await serverCommand(endpoint.endpoint, project.config.packageManager, endpoint.cwd, endpoint.variables, reservation.port);
|
|
22
|
+
for (const hostname of hostnames) {
|
|
23
|
+
try {
|
|
24
|
+
const existing = store.loadRoutes().find((route) => route.hostname === hostname);
|
|
25
|
+
if (existing) {
|
|
26
|
+
throw new RouteConflictError(hostname, existing.pid);
|
|
27
|
+
}
|
|
28
|
+
// addRoute takes Portless's cross-process lock and never replaces a live owner.
|
|
29
|
+
store.addRoute(hostname, reservation.port, process.pid);
|
|
30
|
+
claimed.push(hostname);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
if (error instanceof RouteConflictError) {
|
|
34
|
+
throw new Error(`Domain ${hostname} is already in use. Rename the worktree or configure a different endpoint name. The existing server was left running.`, { cause: error });
|
|
35
|
+
}
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const env = serverEnvironment(endpoint, reservation.port, proxyEnvironment(project), hostnames);
|
|
40
|
+
// Most framework CLIs accept a port, not an inherited listening socket.
|
|
41
|
+
// Keep the registration until the child exits; retry a competing external bind.
|
|
42
|
+
await reservation.release();
|
|
43
|
+
const result = await runProcess(command, { cwd: endpoint.cwd, env, signal, quiet });
|
|
44
|
+
if (signal?.aborted ||
|
|
45
|
+
result.code === 0 ||
|
|
46
|
+
!/EADDRINUSE|address already in use/i.test(result.stderr) ||
|
|
47
|
+
attempt === 2) {
|
|
48
|
+
return result.code;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
finally {
|
|
52
|
+
// Closing an already released reservation is harmless. Cleanup must also
|
|
53
|
+
// cover command validation failures and partially registered alias sets.
|
|
54
|
+
try {
|
|
55
|
+
await reservation.release();
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
for (const hostname of claimed) {
|
|
59
|
+
store.removeRoute(hostname, process.pid);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return 1;
|
|
65
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Own only the process tree we started. Never stop a shared proxy. */
|
|
2
|
+
export declare function runProcess(command: string[], options: {
|
|
3
|
+
cwd: string;
|
|
4
|
+
env?: NodeJS.ProcessEnv;
|
|
5
|
+
signal?: AbortSignal;
|
|
6
|
+
onSpawn?: (pid: number) => void;
|
|
7
|
+
quiet?: boolean;
|
|
8
|
+
}): Promise<{
|
|
9
|
+
code: number;
|
|
10
|
+
stderr: string;
|
|
11
|
+
pid?: number;
|
|
12
|
+
}>;
|
|
13
|
+
export declare function withSignals<T>(operation: (signal: AbortSignal) => Promise<T>): Promise<T>;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
import spawn from "cross-spawn";
|
|
4
|
+
const exec = promisify(execFile);
|
|
5
|
+
/** Own only the process tree we started. Never stop a shared proxy. */
|
|
6
|
+
export async function runProcess(command, options) {
|
|
7
|
+
const [executable, ...args] = command;
|
|
8
|
+
if (!executable) {
|
|
9
|
+
throw new Error("A command is required");
|
|
10
|
+
}
|
|
11
|
+
options.signal?.throwIfAborted();
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
const child = spawn(executable, args, {
|
|
14
|
+
cwd: options.cwd,
|
|
15
|
+
env: options.env,
|
|
16
|
+
stdio: ["inherit", "pipe", "pipe"],
|
|
17
|
+
detached: process.platform !== "win32",
|
|
18
|
+
windowsHide: true
|
|
19
|
+
});
|
|
20
|
+
let stderr = "";
|
|
21
|
+
let timer;
|
|
22
|
+
let isDone = false;
|
|
23
|
+
const kill = async (force) => {
|
|
24
|
+
if (!child.pid || isDone) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
if (process.platform === "win32") {
|
|
29
|
+
await exec("taskkill", ["/PID", String(child.pid), "/T", "/F"], { windowsHide: true, timeout: 5000 });
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
process.kill(-child.pid, force ? "SIGKILL" : "SIGTERM");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
if (!isDone &&
|
|
37
|
+
child.exitCode === null &&
|
|
38
|
+
!(error instanceof Error && "code" in error && error.code === "ESRCH")) {
|
|
39
|
+
child.kill("SIGKILL");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
const abort = () => {
|
|
44
|
+
void kill(false);
|
|
45
|
+
timer ??= setTimeout(() => {
|
|
46
|
+
void kill(true);
|
|
47
|
+
}, 5000);
|
|
48
|
+
timer.unref();
|
|
49
|
+
};
|
|
50
|
+
const finish = () => {
|
|
51
|
+
isDone = true;
|
|
52
|
+
if (timer) {
|
|
53
|
+
clearTimeout(timer);
|
|
54
|
+
}
|
|
55
|
+
options.signal?.removeEventListener("abort", abort);
|
|
56
|
+
};
|
|
57
|
+
child.stdout?.on("data", (chunk) => {
|
|
58
|
+
if (!options.quiet) {
|
|
59
|
+
process.stdout.write(chunk);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
child.stderr?.on("data", (chunk) => {
|
|
63
|
+
stderr = (stderr + chunk.toString()).slice(-8192);
|
|
64
|
+
if (!options.quiet) {
|
|
65
|
+
process.stderr.write(chunk);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
child.once("spawn", () => {
|
|
69
|
+
if (child.pid) {
|
|
70
|
+
options.onSpawn?.(child.pid);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
child.once("error", (error) => {
|
|
74
|
+
finish();
|
|
75
|
+
reject(new Error(`Cannot start ${executable}: ${error.message}`, { cause: error }));
|
|
76
|
+
});
|
|
77
|
+
child.once("close", (code, signal) => {
|
|
78
|
+
finish();
|
|
79
|
+
resolve({ code: code ?? (signal === "SIGINT" ? 130 : 143), stderr, pid: child.pid });
|
|
80
|
+
});
|
|
81
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
82
|
+
if (options.signal?.aborted) {
|
|
83
|
+
abort();
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
export async function withSignals(operation) {
|
|
88
|
+
const controller = new AbortController();
|
|
89
|
+
const abort = () => controller.abort();
|
|
90
|
+
process.on("SIGINT", abort);
|
|
91
|
+
process.on("SIGTERM", abort);
|
|
92
|
+
process.on("SIGHUP", abort);
|
|
93
|
+
try {
|
|
94
|
+
return await operation(controller.signal);
|
|
95
|
+
}
|
|
96
|
+
finally {
|
|
97
|
+
process.off("SIGINT", abort);
|
|
98
|
+
process.off("SIGTERM", abort);
|
|
99
|
+
process.off("SIGHUP", abort);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Project } from "../domains/resolve.js";
|
|
2
|
+
export declare function proxyEnvironment(project: Project): NodeJS.ProcessEnv;
|
|
3
|
+
/** Suffixes do not limit Portless's exact routes or its per-hostname TLS certificates. */
|
|
4
|
+
export declare function hasCompatibleProxy(project: Project, signal?: AbortSignal): Promise<boolean>;
|
|
5
|
+
export declare function proxyCommand(project: Project, action: "setup" | "status" | "doctor", signal?: AbortSignal): Promise<{
|
|
6
|
+
code: number;
|
|
7
|
+
stderr: string;
|
|
8
|
+
pid?: number;
|
|
9
|
+
}>;
|
|
10
|
+
export declare function ensureProxy(project: Project, signal?: AbortSignal): Promise<void>;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import http from "node:http";
|
|
4
|
+
import https from "node:https";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { isErrnoException, isProcessAlive, PORTLESS_HEADER } from "portless";
|
|
8
|
+
import { runProcess } from "./process.js";
|
|
9
|
+
import { stateDirectory } from "./state.js";
|
|
10
|
+
const cli = fileURLToPath(new URL("./cli.js", import.meta.resolve("portless")));
|
|
11
|
+
export function proxyEnvironment(project) {
|
|
12
|
+
const proxy = project.config.proxy;
|
|
13
|
+
const caPath = path.join(stateDirectory(), "ca.pem");
|
|
14
|
+
return {
|
|
15
|
+
...process.env,
|
|
16
|
+
npm_command: undefined,
|
|
17
|
+
PNPM_SCRIPT_SRC_DIR: undefined,
|
|
18
|
+
NODE_EXTRA_CA_CERTS: process.env.NODE_EXTRA_CA_CERTS || (proxy.https && existsSync(caPath) ? caPath : undefined),
|
|
19
|
+
PORTLESS: "1",
|
|
20
|
+
PORTLESS_TLD: (proxy.domains ?? [project.config.domain]).join(","),
|
|
21
|
+
PORTLESS_PORT: String(proxy.port),
|
|
22
|
+
PORTLESS_HTTPS: proxy.https ? "1" : "0",
|
|
23
|
+
PORTLESS_LAN: "0",
|
|
24
|
+
PORTLESS_SYNC_HOSTS: proxy.syncHosts ? "1" : "0",
|
|
25
|
+
PORTLESS_NGROK: "0",
|
|
26
|
+
PORTLESS_TAILSCALE: "0",
|
|
27
|
+
PORTLESS_FUNNEL: "0",
|
|
28
|
+
PORTLESS_APP_PORT: ""
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
async function readState(name) {
|
|
32
|
+
try {
|
|
33
|
+
return (await readFile(path.join(stateDirectory(), name), "utf8")).trim();
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
if (isErrnoException(error) && error.code === "ENOENT") {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** Suffixes do not limit Portless's exact routes or its per-hostname TLS certificates. */
|
|
43
|
+
export async function hasCompatibleProxy(project, signal) {
|
|
44
|
+
const pid = Number(await readState("proxy.pid"));
|
|
45
|
+
if (!Number.isSafeInteger(pid) || pid <= 0 || !isProcessAlive(pid)) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
const proxy = project.config.proxy;
|
|
49
|
+
const port = Number(await readState("proxy.port"));
|
|
50
|
+
const isHttps = existsSync(path.join(stateDirectory(), "proxy.tls"));
|
|
51
|
+
if (port !== proxy.port || isHttps !== proxy.https) {
|
|
52
|
+
throw new Error("The shared Portless proxy has different port or TLS settings. Coordinate those settings before starting this endpoint. The proxy was left running.");
|
|
53
|
+
}
|
|
54
|
+
const ca = isHttps ? await readFile(path.join(stateDirectory(), "ca.pem")) : undefined;
|
|
55
|
+
const request = isHttps ? https.request : http.request;
|
|
56
|
+
const hostname = `proxy.${project.config.domain}`;
|
|
57
|
+
await new Promise((resolve, reject) => {
|
|
58
|
+
const req = request({
|
|
59
|
+
host: "127.0.0.1",
|
|
60
|
+
port,
|
|
61
|
+
method: "HEAD",
|
|
62
|
+
path: "/",
|
|
63
|
+
servername: hostname,
|
|
64
|
+
ca,
|
|
65
|
+
headers: { host: hostname },
|
|
66
|
+
signal: AbortSignal.any([AbortSignal.timeout(10000), ...(signal ? [signal] : [])])
|
|
67
|
+
}, (response) => {
|
|
68
|
+
response.resume();
|
|
69
|
+
if (response.headers[PORTLESS_HEADER.toLowerCase()] !== "1") {
|
|
70
|
+
reject(new Error("The configured port is not responding as Portless. The existing process was left running."));
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
resolve();
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
req.once("error", reject);
|
|
77
|
+
req.end();
|
|
78
|
+
});
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
export async function proxyCommand(project, action, signal) {
|
|
82
|
+
if (action === "setup" && (await hasCompatibleProxy(project, signal))) {
|
|
83
|
+
return { code: 0, stderr: "" };
|
|
84
|
+
}
|
|
85
|
+
const args = action === "setup"
|
|
86
|
+
? [
|
|
87
|
+
"proxy",
|
|
88
|
+
"start",
|
|
89
|
+
...(project.config.proxy.domains ?? [project.config.domain]).flatMap((domain) => ["--tld", domain]),
|
|
90
|
+
"--port",
|
|
91
|
+
String(project.config.proxy.port),
|
|
92
|
+
...(project.config.proxy.https ? [] : ["--no-tls"])
|
|
93
|
+
]
|
|
94
|
+
: [action === "status" ? "list" : "doctor"];
|
|
95
|
+
return runProcess([process.execPath, cli, ...args], { cwd: project.root, env: proxyEnvironment(project), signal });
|
|
96
|
+
}
|
|
97
|
+
export async function ensureProxy(project, signal) {
|
|
98
|
+
const result = await proxyCommand(project, "setup", signal);
|
|
99
|
+
if (result.code !== 0 || !(await hasCompatibleProxy(project, signal))) {
|
|
100
|
+
throw new Error("Could not start the Portless proxy. Run dev-domains setup and resolve the reported issue.");
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { RouteStore, resolveUserHome } from "portless";
|
|
3
|
+
export function stateDirectory() {
|
|
4
|
+
return process.env.PORTLESS_STATE_DIR || path.join(resolveUserHome(), ".portless");
|
|
5
|
+
}
|
|
6
|
+
export function routeStore() {
|
|
7
|
+
return new RouteStore(stateDirectory());
|
|
8
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Loopback-only, POST-only gateway. Bodies are opaque and never logged or persisted. */
|
|
2
|
+
export declare function startGateway(options: {
|
|
3
|
+
paths: string[];
|
|
4
|
+
hostname: string;
|
|
5
|
+
target: () => number | undefined;
|
|
6
|
+
maxBytes?: number;
|
|
7
|
+
}): Promise<{
|
|
8
|
+
url: string;
|
|
9
|
+
close: () => Promise<void>;
|
|
10
|
+
}>;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { createServer, request } from "node:http";
|
|
2
|
+
import { pipeline } from "node:stream";
|
|
3
|
+
function headersForUpstream(headers, host) {
|
|
4
|
+
const clean = { ...headers, host };
|
|
5
|
+
const excluded = [
|
|
6
|
+
"connection",
|
|
7
|
+
"keep-alive",
|
|
8
|
+
"proxy-authenticate",
|
|
9
|
+
"proxy-authorization",
|
|
10
|
+
"te",
|
|
11
|
+
"trailer",
|
|
12
|
+
"transfer-encoding",
|
|
13
|
+
"upgrade",
|
|
14
|
+
"forwarded",
|
|
15
|
+
"x-forwarded-for",
|
|
16
|
+
"x-forwarded-host",
|
|
17
|
+
...(headers.connection?.split(",").map((name) => name.trim().toLowerCase()) ?? [])
|
|
18
|
+
];
|
|
19
|
+
for (const header of excluded) {
|
|
20
|
+
delete clean[header];
|
|
21
|
+
}
|
|
22
|
+
return clean;
|
|
23
|
+
}
|
|
24
|
+
/** Loopback-only, POST-only gateway. Bodies are opaque and never logged or persisted. */
|
|
25
|
+
export async function startGateway(options) {
|
|
26
|
+
const server = createServer(async (req, res) => {
|
|
27
|
+
const rawPath = req.url?.split("?")[0] || "";
|
|
28
|
+
if (req.method !== "POST" || !options.paths.includes(rawPath)) {
|
|
29
|
+
res.writeHead(404).end();
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const port = options.target();
|
|
33
|
+
if (!port) {
|
|
34
|
+
res.writeHead(503).end("Start the local endpoint first");
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const chunks = [];
|
|
38
|
+
let size = 0;
|
|
39
|
+
try {
|
|
40
|
+
for await (const chunk of req) {
|
|
41
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
42
|
+
size += buffer.length;
|
|
43
|
+
if (size > (options.maxBytes ?? 1048576)) {
|
|
44
|
+
res.writeHead(413).end();
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
chunks.push(buffer);
|
|
48
|
+
}
|
|
49
|
+
const body = Buffer.concat(chunks);
|
|
50
|
+
const upstream = request({
|
|
51
|
+
host: "127.0.0.1",
|
|
52
|
+
port,
|
|
53
|
+
path: req.url,
|
|
54
|
+
method: "POST",
|
|
55
|
+
headers: { ...headersForUpstream(req.headers, options.hostname), "content-length": body.length },
|
|
56
|
+
timeout: 15000
|
|
57
|
+
}, (response) => {
|
|
58
|
+
const headers = headersForUpstream(response.headers, options.hostname);
|
|
59
|
+
delete headers.host;
|
|
60
|
+
res.writeHead(response.statusCode || 502, headers);
|
|
61
|
+
pipeline(response, res, () => {
|
|
62
|
+
upstream.destroy();
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
upstream.on("timeout", () => upstream.destroy(new Error("Webhook target timed out")));
|
|
66
|
+
upstream.on("error", () => {
|
|
67
|
+
if (!res.headersSent) {
|
|
68
|
+
res.writeHead(502);
|
|
69
|
+
}
|
|
70
|
+
res.end();
|
|
71
|
+
});
|
|
72
|
+
res.on("close", () => upstream.destroy());
|
|
73
|
+
upstream.end(body);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
if (!res.headersSent) {
|
|
77
|
+
res.writeHead(400);
|
|
78
|
+
}
|
|
79
|
+
res.end();
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
server.requestTimeout = 15000;
|
|
83
|
+
server.headersTimeout = 10000;
|
|
84
|
+
await new Promise((resolve, reject) => {
|
|
85
|
+
server.once("error", reject);
|
|
86
|
+
server.listen(0, "127.0.0.1", resolve);
|
|
87
|
+
});
|
|
88
|
+
const address = server.address();
|
|
89
|
+
if (!address || typeof address === "string") {
|
|
90
|
+
throw new Error("Cannot resolve webhook gateway address");
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
url: `http://127.0.0.1:${address.port}`,
|
|
94
|
+
close: async () => {
|
|
95
|
+
server.closeAllConnections();
|
|
96
|
+
await new Promise((resolve, reject) => server.close((error) => (error ? reject(error) : resolve())));
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { interpolate } from "../domains/resolve.js";
|
|
2
|
+
import { runProcess } from "../runtime/process.js";
|
|
3
|
+
import { routeStore } from "../runtime/state.js";
|
|
4
|
+
import { startGateway } from "./gateway.js";
|
|
5
|
+
export async function runTunnel(project, endpoint, signal) {
|
|
6
|
+
const tunnel = endpoint.endpoint.tunnel;
|
|
7
|
+
if (!tunnel) {
|
|
8
|
+
throw new Error(`No tunnel configured for ${endpoint.service}. Add an explicit command and allowed paths.`);
|
|
9
|
+
}
|
|
10
|
+
if (!tunnel.command.some((arg) => arg.includes("{url}"))) {
|
|
11
|
+
throw new Error("Tunnel command must use {url} to target the restricted gateway");
|
|
12
|
+
}
|
|
13
|
+
const target = () => routeStore()
|
|
14
|
+
.loadRoutes()
|
|
15
|
+
.find((route) => route.hostname === endpoint.hostname)?.port;
|
|
16
|
+
if (!target()) {
|
|
17
|
+
throw new Error(`Start ${endpoint.service} before opening its tunnel`);
|
|
18
|
+
}
|
|
19
|
+
const gateway = await startGateway({ paths: tunnel.paths, hostname: endpoint.hostname, target });
|
|
20
|
+
try {
|
|
21
|
+
process.stdout.write(`Exposing POST ${tunnel.paths.join(", ")} for ${endpoint.origin}\n${tunnel.publicUrl ? `Public origin: ${tunnel.publicUrl}\n` : "The tunnel provider will print its public URL.\n"}`);
|
|
22
|
+
return (await runProcess(tunnel.command.map((arg) => interpolate(arg, { ...endpoint.variables, url: gateway.url })), { cwd: project.root, signal })).code;
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
await gateway.close();
|
|
26
|
+
}
|
|
27
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@buildkitgg/dev-domains",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"description": "Worktree-aware development domains built on Portless",
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@clack/prompts": "1.8.1",
|
|
11
|
+
"c12": "^3.3.4",
|
|
12
|
+
"citty": "0.2.2",
|
|
13
|
+
"cross-spawn": "^7.0.6",
|
|
14
|
+
"package-manager-detector": "1.9.0",
|
|
15
|
+
"portless": "0.15.8",
|
|
16
|
+
"zod": "4.6.7"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@repo/oxlint-config": "0.0.0",
|
|
20
|
+
"@repo/typescript-config": "0.0.0",
|
|
21
|
+
"@types/cross-spawn": "^6.0.6",
|
|
22
|
+
"@types/node": "24.13.2",
|
|
23
|
+
"@vitest/coverage-v8": "4.1.11",
|
|
24
|
+
"typescript": "7.0.1-rc",
|
|
25
|
+
"vitest": "4.1.11"
|
|
26
|
+
},
|
|
27
|
+
"type": "module",
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=24"
|
|
30
|
+
},
|
|
31
|
+
"bin": {
|
|
32
|
+
"dev-domains": "./dist/cli/main.js"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist/**/*.js",
|
|
36
|
+
"dist/**/*.d.ts",
|
|
37
|
+
"README.md",
|
|
38
|
+
"CHANGELOG.md",
|
|
39
|
+
"LICENSE"
|
|
40
|
+
],
|
|
41
|
+
"exports": {
|
|
42
|
+
".": {
|
|
43
|
+
"types": "./dist/config/schema.d.ts",
|
|
44
|
+
"import": "./dist/config/schema.js"
|
|
45
|
+
},
|
|
46
|
+
"./node": {
|
|
47
|
+
"types": "./dist/node.d.ts",
|
|
48
|
+
"import": "./dist/node.js"
|
|
49
|
+
},
|
|
50
|
+
"./playwright": {
|
|
51
|
+
"types": "./dist/playwright.d.ts",
|
|
52
|
+
"import": "./dist/playwright.js"
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"repository": {
|
|
56
|
+
"type": "git",
|
|
57
|
+
"url": "https://github.com/AndrewCraswell/buildkit.git",
|
|
58
|
+
"directory": "packages/dev-domains"
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "tsc -p tsconfig.build.json",
|
|
62
|
+
"check:types": "pnpm build && tsc --noEmit",
|
|
63
|
+
"lint": "oxlint --max-warnings=0",
|
|
64
|
+
"test": "vitest run",
|
|
65
|
+
"test:coverage": "vitest run --coverage",
|
|
66
|
+
"test:integration": "pnpm build && vitest run --config vitest.integration.config.ts"
|
|
67
|
+
}
|
|
6
68
|
}
|