@andpipes/cli 0.0.2 → 0.1.1
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/LICENSE +201 -1
- package/README.md +61 -0
- package/dist/bin.cjs +8904 -0
- package/dist/bin.d.cts +1 -0
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +20 -0
- package/dist/chunk-7LSHSW4Q.js +758 -0
- package/dist/chunk-QPRR3KCS.js +8493 -0
- package/dist/dev-supervisor.cjs +249 -0
- package/dist/dev-supervisor.d.cts +2 -0
- package/dist/dev-supervisor.d.ts +2 -0
- package/dist/dev-supervisor.js +104 -0
- package/dist/index.cjs +9144 -0
- package/dist/index.d.cts +1265 -0
- package/dist/index.d.ts +1265 -0
- package/dist/index.js +199 -0
- package/package.json +53 -6
- package/bin/andpipe.js +0 -2
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// src/dev-supervisor.ts
|
|
4
|
+
var import_node_os2 = require("os");
|
|
5
|
+
var import_node_path4 = require("path");
|
|
6
|
+
|
|
7
|
+
// src/lib/dev-artifact.ts
|
|
8
|
+
var import_node_fs2 = require("fs");
|
|
9
|
+
var import_node_os = require("os");
|
|
10
|
+
var import_node_path2 = require("path");
|
|
11
|
+
|
|
12
|
+
// src/lib/local-files.ts
|
|
13
|
+
var import_node_crypto = require("crypto");
|
|
14
|
+
var import_node_fs = require("fs");
|
|
15
|
+
var import_node_path = require("path");
|
|
16
|
+
|
|
17
|
+
// src/lib/dev-artifact.ts
|
|
18
|
+
function removeDevArtifact(plan) {
|
|
19
|
+
let failure;
|
|
20
|
+
try {
|
|
21
|
+
(0, import_node_fs2.unlinkSync)(plan.path);
|
|
22
|
+
} catch (error) {
|
|
23
|
+
if (error.code !== "ENOENT") failure = error;
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
(0, import_node_fs2.rmdirSync)(plan.directory);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if (error.code !== "ENOENT" && failure === void 0)
|
|
29
|
+
failure = error;
|
|
30
|
+
}
|
|
31
|
+
if (failure !== void 0) throw failure;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/lib/dev-tunnel.ts
|
|
35
|
+
var import_node_child_process = require("child_process");
|
|
36
|
+
var import_node_fs3 = require("fs");
|
|
37
|
+
var import_node_path3 = require("path");
|
|
38
|
+
var MAX_QUICK_TUNNEL_OUTPUT_BYTES = 64 * 1024;
|
|
39
|
+
var QUICK_TUNNEL_DISCOVERY_TIMEOUT_MS = 3e4;
|
|
40
|
+
var PROCESS_TERM_TIMEOUT_MS = 5e3;
|
|
41
|
+
var PROCESS_KILL_TIMEOUT_MS = 5e3;
|
|
42
|
+
var QUICK_TUNNEL_HOST = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.trycloudflare\.com$/u;
|
|
43
|
+
function parseQuickTunnelOrigin(output) {
|
|
44
|
+
const candidates = output.match(/https?:\/\/[^\s"'<>]+/giu) ?? [];
|
|
45
|
+
const relevant = candidates.filter(
|
|
46
|
+
(candidate) => candidate.toLowerCase().includes("trycloudflare")
|
|
47
|
+
);
|
|
48
|
+
const origins = /* @__PURE__ */ new Set();
|
|
49
|
+
for (const candidate of relevant) {
|
|
50
|
+
let url;
|
|
51
|
+
try {
|
|
52
|
+
url = new URL(candidate);
|
|
53
|
+
} catch {
|
|
54
|
+
throw new Error("Quick Tunnel URL\u304C\u4E0D\u6B63\u3067\u3059");
|
|
55
|
+
}
|
|
56
|
+
if (url.protocol !== "https:" || !QUICK_TUNNEL_HOST.test(url.hostname) || url.username !== "" || url.password !== "" || url.port !== "" || url.pathname !== "/" || url.search !== "" || url.hash !== "" || url.origin !== candidate) {
|
|
57
|
+
throw new Error("Quick Tunnel URL\u304Cexact origin\u3067\u306F\u3042\u308A\u307E\u305B\u3093");
|
|
58
|
+
}
|
|
59
|
+
origins.add(url.origin);
|
|
60
|
+
}
|
|
61
|
+
if (origins.size > 1) throw new Error("Quick Tunnel URL\u304C\u8907\u6570\u691C\u51FA\u3055\u308C\u307E\u3057\u305F");
|
|
62
|
+
return origins.values().next().value;
|
|
63
|
+
}
|
|
64
|
+
var defaultProcessGroupRuntime = {
|
|
65
|
+
killGroup(pgid, signal) {
|
|
66
|
+
process.kill(-pgid, signal);
|
|
67
|
+
},
|
|
68
|
+
groupExists(pgid) {
|
|
69
|
+
try {
|
|
70
|
+
process.kill(-pgid, 0);
|
|
71
|
+
return true;
|
|
72
|
+
} catch (error) {
|
|
73
|
+
return error.code === "EPERM";
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
sleep: (milliseconds) => new Promise((resolve2) => {
|
|
77
|
+
setTimeout(resolve2, milliseconds).unref();
|
|
78
|
+
})
|
|
79
|
+
};
|
|
80
|
+
var defaultParentTunnelRuntime = {
|
|
81
|
+
...defaultProcessGroupRuntime,
|
|
82
|
+
forkSupervisor(modulePath, args, options) {
|
|
83
|
+
return (0, import_node_child_process.fork)(modulePath, [...args], {
|
|
84
|
+
detached: options.detached,
|
|
85
|
+
execArgv: [...options.execArgv],
|
|
86
|
+
stdio: [...options.stdio]
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
var defaultSupervisorRuntime = {
|
|
91
|
+
spawn(file, args, options) {
|
|
92
|
+
return (0, import_node_child_process.spawn)(file, [...args], {
|
|
93
|
+
shell: options.shell,
|
|
94
|
+
detached: options.detached,
|
|
95
|
+
stdio: [...options.stdio]
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
sleep: defaultProcessGroupRuntime.sleep
|
|
99
|
+
};
|
|
100
|
+
function spawnCloudflared(localOrigin, runtime = defaultSupervisorRuntime) {
|
|
101
|
+
return runtime.spawn("cloudflared", ["tunnel", "--url", localOrigin], {
|
|
102
|
+
shell: false,
|
|
103
|
+
detached: false,
|
|
104
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
async function discoverQuickTunnelOrigin(child, runtime) {
|
|
108
|
+
let output = Buffer.alloc(0);
|
|
109
|
+
let settled = false;
|
|
110
|
+
return new Promise((resolve2, reject) => {
|
|
111
|
+
const finish = (error, origin) => {
|
|
112
|
+
if (settled) return;
|
|
113
|
+
settled = true;
|
|
114
|
+
error ? reject(error) : resolve2(origin);
|
|
115
|
+
};
|
|
116
|
+
const append = (chunk) => {
|
|
117
|
+
if (settled) return;
|
|
118
|
+
const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk), "utf8");
|
|
119
|
+
if (output.byteLength + bytes.byteLength > MAX_QUICK_TUNNEL_OUTPUT_BYTES) {
|
|
120
|
+
finish(new Error("Quick Tunnel output limit exceeded"));
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
output = Buffer.concat([output, bytes]);
|
|
124
|
+
try {
|
|
125
|
+
const origin = parseQuickTunnelOrigin(output.toString("utf8"));
|
|
126
|
+
if (origin) finish(void 0, origin);
|
|
127
|
+
} catch (error) {
|
|
128
|
+
finish(error);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
child.stdout?.on("data", append);
|
|
132
|
+
child.stderr?.on("data", append);
|
|
133
|
+
child.once("error", (error) => finish(error));
|
|
134
|
+
child.once(
|
|
135
|
+
"exit",
|
|
136
|
+
(code, signal) => finish(new Error(`cloudflared exited before ready (${code ?? signal ?? "unknown"})`))
|
|
137
|
+
);
|
|
138
|
+
void runtime.sleep(QUICK_TUNNEL_DISCOVERY_TIMEOUT_MS).then(() => finish(new Error("Quick Tunnel URL discovery timed out")));
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
async function stopTunnelChild(child, exited, runtime) {
|
|
142
|
+
child.kill("SIGTERM");
|
|
143
|
+
const term = await Promise.race([
|
|
144
|
+
exited.then(() => true),
|
|
145
|
+
runtime.sleep(PROCESS_TERM_TIMEOUT_MS).then(() => false)
|
|
146
|
+
]);
|
|
147
|
+
if (term) return;
|
|
148
|
+
child.kill("SIGKILL");
|
|
149
|
+
const killed = await Promise.race([
|
|
150
|
+
exited.then(() => true),
|
|
151
|
+
runtime.sleep(PROCESS_KILL_TIMEOUT_MS).then(() => false)
|
|
152
|
+
]);
|
|
153
|
+
if (!killed) throw new Error("cloudflared did not exit after SIGKILL");
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// src/dev-supervisor.ts
|
|
157
|
+
function monotonicNow() {
|
|
158
|
+
return Number(process.hrtime.bigint() / 1000000n);
|
|
159
|
+
}
|
|
160
|
+
function validateInput(artifactPath, localOrigin, deadlineText) {
|
|
161
|
+
const directory = artifactPath ? (0, import_node_path4.dirname)(artifactPath) : "";
|
|
162
|
+
const directoryName = (0, import_node_path4.basename)(directory);
|
|
163
|
+
let url;
|
|
164
|
+
try {
|
|
165
|
+
url = new URL(localOrigin ?? "");
|
|
166
|
+
} catch {
|
|
167
|
+
throw new Error("invalid local origin");
|
|
168
|
+
}
|
|
169
|
+
const port = Number(url.port);
|
|
170
|
+
const deadline = Number(deadlineText);
|
|
171
|
+
if (!artifactPath || (0, import_node_path4.basename)(artifactPath) !== "session.json" || (0, import_node_path4.dirname)(directory) !== (0, import_node_os2.tmpdir)() || !directoryName.startsWith("andpipe-dev-") || url.protocol !== "http:" || url.hostname !== "127.0.0.1" || !Number.isInteger(port) || port < 1 || port > 65535 || url.pathname !== "/" || url.search !== "" || url.hash !== "" || url.origin !== localOrigin || !Number.isSafeInteger(deadline) || deadline <= monotonicNow()) {
|
|
172
|
+
throw new Error("invalid supervisor input");
|
|
173
|
+
}
|
|
174
|
+
return { artifactPath, localOrigin, deadline };
|
|
175
|
+
}
|
|
176
|
+
async function main() {
|
|
177
|
+
const input = validateInput(process.argv[2], process.argv[3], process.argv[4]);
|
|
178
|
+
let requestStop;
|
|
179
|
+
const stopRequested = new Promise((resolve2) => {
|
|
180
|
+
requestStop = resolve2;
|
|
181
|
+
});
|
|
182
|
+
process.once("disconnect", () => requestStop?.());
|
|
183
|
+
process.on("SIGINT", () => requestStop?.());
|
|
184
|
+
process.on("SIGTERM", () => requestStop?.());
|
|
185
|
+
process.on("message", (message) => {
|
|
186
|
+
if (message !== null && typeof message === "object" && message.type === "stop") {
|
|
187
|
+
requestStop?.();
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
const child = spawnCloudflared(input.localOrigin, defaultSupervisorRuntime);
|
|
191
|
+
let childExited = false;
|
|
192
|
+
const childExit = new Promise(
|
|
193
|
+
(resolve2) => {
|
|
194
|
+
child.once("exit", (code, signal) => {
|
|
195
|
+
childExited = true;
|
|
196
|
+
resolve2({ code, signal });
|
|
197
|
+
});
|
|
198
|
+
child.once("error", () => {
|
|
199
|
+
childExited = true;
|
|
200
|
+
resolve2({ code: null, signal: null });
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
);
|
|
204
|
+
let failure;
|
|
205
|
+
try {
|
|
206
|
+
const deadlineStop = defaultSupervisorRuntime.sleep(
|
|
207
|
+
Math.max(0, input.deadline - monotonicNow())
|
|
208
|
+
);
|
|
209
|
+
const discovery = await Promise.race([
|
|
210
|
+
discoverQuickTunnelOrigin(child, defaultSupervisorRuntime).then((publicOrigin) => ({
|
|
211
|
+
kind: "ready",
|
|
212
|
+
publicOrigin
|
|
213
|
+
})),
|
|
214
|
+
stopRequested.then(() => ({ kind: "stop" })),
|
|
215
|
+
deadlineStop.then(() => ({ kind: "deadline" }))
|
|
216
|
+
]);
|
|
217
|
+
if (discovery.kind !== "ready") {
|
|
218
|
+
requestStop?.();
|
|
219
|
+
} else {
|
|
220
|
+
const { publicOrigin } = discovery;
|
|
221
|
+
if (!process.connected || !process.send) throw new Error("parent disconnected before ready");
|
|
222
|
+
process.send({ type: "ready", publicOrigin });
|
|
223
|
+
const reason = await Promise.race([
|
|
224
|
+
stopRequested.then(() => "stop"),
|
|
225
|
+
childExit.then(() => "child-exit"),
|
|
226
|
+
deadlineStop.then(() => "deadline")
|
|
227
|
+
]);
|
|
228
|
+
if (reason === "child-exit") throw new Error("cloudflared exited unexpectedly");
|
|
229
|
+
}
|
|
230
|
+
} catch (error) {
|
|
231
|
+
failure = error;
|
|
232
|
+
}
|
|
233
|
+
try {
|
|
234
|
+
if (!childExited) await stopTunnelChild(child, childExit, defaultSupervisorRuntime);
|
|
235
|
+
} catch (error) {
|
|
236
|
+
failure ??= error;
|
|
237
|
+
}
|
|
238
|
+
try {
|
|
239
|
+
removeDevArtifact({ directory: (0, import_node_path4.dirname)(input.artifactPath), path: input.artifactPath });
|
|
240
|
+
} catch (error) {
|
|
241
|
+
failure ??= error;
|
|
242
|
+
}
|
|
243
|
+
process.exitCode = failure === void 0 ? 0 : 1;
|
|
244
|
+
if (process.connected) process.disconnect?.();
|
|
245
|
+
}
|
|
246
|
+
void main().catch(() => {
|
|
247
|
+
process.exitCode = 1;
|
|
248
|
+
if (process.connected) process.disconnect?.();
|
|
249
|
+
});
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defaultSupervisorRuntime,
|
|
3
|
+
discoverQuickTunnelOrigin,
|
|
4
|
+
removeDevArtifact,
|
|
5
|
+
spawnCloudflared,
|
|
6
|
+
stopTunnelChild
|
|
7
|
+
} from "./chunk-7LSHSW4Q.js";
|
|
8
|
+
|
|
9
|
+
// src/dev-supervisor.ts
|
|
10
|
+
import { tmpdir } from "os";
|
|
11
|
+
import { basename, dirname } from "path";
|
|
12
|
+
function monotonicNow() {
|
|
13
|
+
return Number(process.hrtime.bigint() / 1000000n);
|
|
14
|
+
}
|
|
15
|
+
function validateInput(artifactPath, localOrigin, deadlineText) {
|
|
16
|
+
const directory = artifactPath ? dirname(artifactPath) : "";
|
|
17
|
+
const directoryName = basename(directory);
|
|
18
|
+
let url;
|
|
19
|
+
try {
|
|
20
|
+
url = new URL(localOrigin ?? "");
|
|
21
|
+
} catch {
|
|
22
|
+
throw new Error("invalid local origin");
|
|
23
|
+
}
|
|
24
|
+
const port = Number(url.port);
|
|
25
|
+
const deadline = Number(deadlineText);
|
|
26
|
+
if (!artifactPath || basename(artifactPath) !== "session.json" || dirname(directory) !== tmpdir() || !directoryName.startsWith("andpipe-dev-") || url.protocol !== "http:" || url.hostname !== "127.0.0.1" || !Number.isInteger(port) || port < 1 || port > 65535 || url.pathname !== "/" || url.search !== "" || url.hash !== "" || url.origin !== localOrigin || !Number.isSafeInteger(deadline) || deadline <= monotonicNow()) {
|
|
27
|
+
throw new Error("invalid supervisor input");
|
|
28
|
+
}
|
|
29
|
+
return { artifactPath, localOrigin, deadline };
|
|
30
|
+
}
|
|
31
|
+
async function main() {
|
|
32
|
+
const input = validateInput(process.argv[2], process.argv[3], process.argv[4]);
|
|
33
|
+
let requestStop;
|
|
34
|
+
const stopRequested = new Promise((resolve) => {
|
|
35
|
+
requestStop = resolve;
|
|
36
|
+
});
|
|
37
|
+
process.once("disconnect", () => requestStop?.());
|
|
38
|
+
process.on("SIGINT", () => requestStop?.());
|
|
39
|
+
process.on("SIGTERM", () => requestStop?.());
|
|
40
|
+
process.on("message", (message) => {
|
|
41
|
+
if (message !== null && typeof message === "object" && message.type === "stop") {
|
|
42
|
+
requestStop?.();
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
const child = spawnCloudflared(input.localOrigin, defaultSupervisorRuntime);
|
|
46
|
+
let childExited = false;
|
|
47
|
+
const childExit = new Promise(
|
|
48
|
+
(resolve) => {
|
|
49
|
+
child.once("exit", (code, signal) => {
|
|
50
|
+
childExited = true;
|
|
51
|
+
resolve({ code, signal });
|
|
52
|
+
});
|
|
53
|
+
child.once("error", () => {
|
|
54
|
+
childExited = true;
|
|
55
|
+
resolve({ code: null, signal: null });
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
let failure;
|
|
60
|
+
try {
|
|
61
|
+
const deadlineStop = defaultSupervisorRuntime.sleep(
|
|
62
|
+
Math.max(0, input.deadline - monotonicNow())
|
|
63
|
+
);
|
|
64
|
+
const discovery = await Promise.race([
|
|
65
|
+
discoverQuickTunnelOrigin(child, defaultSupervisorRuntime).then((publicOrigin) => ({
|
|
66
|
+
kind: "ready",
|
|
67
|
+
publicOrigin
|
|
68
|
+
})),
|
|
69
|
+
stopRequested.then(() => ({ kind: "stop" })),
|
|
70
|
+
deadlineStop.then(() => ({ kind: "deadline" }))
|
|
71
|
+
]);
|
|
72
|
+
if (discovery.kind !== "ready") {
|
|
73
|
+
requestStop?.();
|
|
74
|
+
} else {
|
|
75
|
+
const { publicOrigin } = discovery;
|
|
76
|
+
if (!process.connected || !process.send) throw new Error("parent disconnected before ready");
|
|
77
|
+
process.send({ type: "ready", publicOrigin });
|
|
78
|
+
const reason = await Promise.race([
|
|
79
|
+
stopRequested.then(() => "stop"),
|
|
80
|
+
childExit.then(() => "child-exit"),
|
|
81
|
+
deadlineStop.then(() => "deadline")
|
|
82
|
+
]);
|
|
83
|
+
if (reason === "child-exit") throw new Error("cloudflared exited unexpectedly");
|
|
84
|
+
}
|
|
85
|
+
} catch (error) {
|
|
86
|
+
failure = error;
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
if (!childExited) await stopTunnelChild(child, childExit, defaultSupervisorRuntime);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
failure ??= error;
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
removeDevArtifact({ directory: dirname(input.artifactPath), path: input.artifactPath });
|
|
95
|
+
} catch (error) {
|
|
96
|
+
failure ??= error;
|
|
97
|
+
}
|
|
98
|
+
process.exitCode = failure === void 0 ? 0 : 1;
|
|
99
|
+
if (process.connected) process.disconnect?.();
|
|
100
|
+
}
|
|
101
|
+
void main().catch(() => {
|
|
102
|
+
process.exitCode = 1;
|
|
103
|
+
if (process.connected) process.disconnect?.();
|
|
104
|
+
});
|