@n-dream/builders-cli 0.2.1 → 0.2.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/README.md +14 -0
- package/dist/builders-cli.cjs +235 -30
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -41,4 +41,18 @@ direct. The connection to the local dev server never uses the proxy.
|
|
|
41
41
|
It connects again by itself when the connection breaks. It stops when the
|
|
42
42
|
token is not valid, or when a newer tunnel for the same work takes over.
|
|
43
43
|
|
|
44
|
+
## Keep the session awake
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
npx -y @n-dream/builders-cli awake --token-file <file> [--again] [--idle-minutes 15] [--max-minutes 25]
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
A cloud session of a coding agent pauses after a few minutes without
|
|
51
|
+
activity, and the dev preview stops with it. The agent runs this command
|
|
52
|
+
in the background at the end of each answer (`open_app` gives it as
|
|
53
|
+
`keepAwake`). It waits until nobody used the preview or wrote for 15
|
|
54
|
+
minutes, and then ends with `idle`. After 25 minutes it ends with `in use`,
|
|
55
|
+
and the agent starts it again with `--again` (not a new message). Only one
|
|
56
|
+
runs per preview.
|
|
57
|
+
|
|
44
58
|
Node.js 20.18.1 or later. No runtime dependencies (all are bundled).
|
package/dist/builders-cli.cjs
CHANGED
|
@@ -18840,7 +18840,7 @@ var require_snapshot_recorder = __commonJS({
|
|
|
18840
18840
|
"../../node_modules/.pnpm/undici@7.30.1/node_modules/undici/lib/mock/snapshot-recorder.js"(exports2, module2) {
|
|
18841
18841
|
"use strict";
|
|
18842
18842
|
var { writeFile: writeFile2, readFile: readFile2, mkdir: mkdir2 } = require("node:fs/promises");
|
|
18843
|
-
var { dirname:
|
|
18843
|
+
var { dirname: dirname4, resolve: resolve2 } = require("node:path");
|
|
18844
18844
|
var { setTimeout: setTimeout2, clearTimeout: clearTimeout2 } = require("node:timers");
|
|
18845
18845
|
var { InvalidArgumentError, UndiciError } = require_errors();
|
|
18846
18846
|
var { hashId, isUrlExcludedFactory, normalizeHeaders, createHeaderFilters } = require_snapshot_utils();
|
|
@@ -19071,7 +19071,7 @@ var require_snapshot_recorder = __commonJS({
|
|
|
19071
19071
|
throw new InvalidArgumentError("Snapshot path is required");
|
|
19072
19072
|
}
|
|
19073
19073
|
const resolvedPath = resolve2(path);
|
|
19074
|
-
await mkdir2(
|
|
19074
|
+
await mkdir2(dirname4(resolvedPath), { recursive: true });
|
|
19075
19075
|
const data = Array.from(this.#snapshots.entries()).map(([hash, snapshot]) => ({
|
|
19076
19076
|
hash,
|
|
19077
19077
|
snapshot
|
|
@@ -31222,7 +31222,7 @@ ${captureLines}` : capture.stack;
|
|
|
31222
31222
|
|
|
31223
31223
|
// src/main.ts
|
|
31224
31224
|
var import_node_crypto2 = require("node:crypto");
|
|
31225
|
-
var
|
|
31225
|
+
var import_node_fs5 = require("node:fs");
|
|
31226
31226
|
|
|
31227
31227
|
// src/client.ts
|
|
31228
31228
|
var import_node_http = __toESM(require("node:http"), 1);
|
|
@@ -31715,6 +31715,7 @@ var TunnelClient = class {
|
|
|
31715
31715
|
serve(link, onReady) {
|
|
31716
31716
|
this.link = link;
|
|
31717
31717
|
const session = new LinkSession(link, this.o.port, this.o.localHost ?? "127.0.0.1", this.o.filesPort);
|
|
31718
|
+
if (this.o.onRequest) session.onRequest = this.o.onRequest;
|
|
31718
31719
|
return new Promise((resolve2) => {
|
|
31719
31720
|
let final = false;
|
|
31720
31721
|
session.onReady = (r) => {
|
|
@@ -31758,6 +31759,8 @@ var LinkSession = class {
|
|
|
31758
31759
|
};
|
|
31759
31760
|
onBye = () => {
|
|
31760
31761
|
};
|
|
31762
|
+
onRequest = () => {
|
|
31763
|
+
};
|
|
31761
31764
|
byeReason = null;
|
|
31762
31765
|
// When the last frame came (liveness).
|
|
31763
31766
|
lastFrame = Date.now();
|
|
@@ -31804,6 +31807,10 @@ var LinkSession = class {
|
|
|
31804
31807
|
}
|
|
31805
31808
|
case 16 /* REQ */: {
|
|
31806
31809
|
const head = json(f);
|
|
31810
|
+
try {
|
|
31811
|
+
this.onRequest(String(head.method), Array.isArray(head.headers) ? head.headers : []);
|
|
31812
|
+
} catch {
|
|
31813
|
+
}
|
|
31807
31814
|
try {
|
|
31808
31815
|
return this.request(f.stream, head);
|
|
31809
31816
|
} catch {
|
|
@@ -32147,13 +32154,189 @@ async function startDevFiles(o) {
|
|
|
32147
32154
|
return { port: addr.port, close: () => server.close() };
|
|
32148
32155
|
}
|
|
32149
32156
|
|
|
32150
|
-
// src/
|
|
32151
|
-
var import_node_child_process2 = require("node:child_process");
|
|
32157
|
+
// src/awake.ts
|
|
32152
32158
|
var import_node_fs3 = require("node:fs");
|
|
32153
32159
|
var import_node_os = require("node:os");
|
|
32154
32160
|
var import_node_path3 = require("node:path");
|
|
32161
|
+
var DEFAULT_IDLE_MIN = 15;
|
|
32162
|
+
var DEFAULT_MAX_MIN = 25;
|
|
32163
|
+
var MAX_MAX_MIN = 28;
|
|
32164
|
+
var QUIET_MS = 2 * 60 * 60 * 1e3;
|
|
32165
|
+
var CHECK_MS = 3e4;
|
|
32166
|
+
var TOUCH_EVERY_MS = 3e4;
|
|
32167
|
+
var activeFile = (tokenFile) => tokenFile.replace(/\.tunnel$/, ".active");
|
|
32168
|
+
var pidFile = (tokenFile) => tokenFile.replace(/\.tunnel$/, ".awake.pid");
|
|
32169
|
+
var promptFile = (tokenFile) => tokenFile.replace(/\.tunnel$/, ".prompt");
|
|
32170
|
+
var idleFile = (tokenFile) => tokenFile.replace(/\.tunnel$/, ".awake-idle");
|
|
32171
|
+
var AFTER_IDLE_MS = 6e4;
|
|
32172
|
+
function touchFile(file, t) {
|
|
32173
|
+
const d = new Date(t);
|
|
32174
|
+
if (!(0, import_node_fs3.existsSync)(file)) (0, import_node_fs3.closeSync)((0, import_node_fs3.openSync)(file, "w", 384));
|
|
32175
|
+
(0, import_node_fs3.utimesSync)(file, d, d);
|
|
32176
|
+
}
|
|
32177
|
+
function isUse(method, headers) {
|
|
32178
|
+
const h = (name) => headers.find(([k]) => k.toLowerCase() === name)?.[1];
|
|
32179
|
+
if (method !== "GET" && method !== "HEAD" && method !== "OPTIONS") return true;
|
|
32180
|
+
if (h("sec-fetch-mode") === "navigate") return true;
|
|
32181
|
+
return h("rsc") === "1" && h("next-router-prefetch") === void 0;
|
|
32182
|
+
}
|
|
32183
|
+
function activityMarker(tokenFile, now = Date.now) {
|
|
32184
|
+
const file = activeFile(tokenFile);
|
|
32185
|
+
let last = 0;
|
|
32186
|
+
return () => {
|
|
32187
|
+
const t = now();
|
|
32188
|
+
if (t - last < TOUCH_EVERY_MS) return;
|
|
32189
|
+
last = t;
|
|
32190
|
+
try {
|
|
32191
|
+
touchFile(file, t);
|
|
32192
|
+
} catch {
|
|
32193
|
+
}
|
|
32194
|
+
};
|
|
32195
|
+
}
|
|
32196
|
+
function mtime(file) {
|
|
32197
|
+
try {
|
|
32198
|
+
return (0, import_node_fs3.statSync)(file).mtimeMs;
|
|
32199
|
+
} catch {
|
|
32200
|
+
return 0;
|
|
32201
|
+
}
|
|
32202
|
+
}
|
|
32203
|
+
function waiterRuns(file, tokenFile) {
|
|
32204
|
+
let pid;
|
|
32205
|
+
try {
|
|
32206
|
+
pid = Number((0, import_node_fs3.readFileSync)(file, "utf8").trim());
|
|
32207
|
+
} catch {
|
|
32208
|
+
return false;
|
|
32209
|
+
}
|
|
32210
|
+
if (!Number.isInteger(pid) || pid <= 1 || pid === process.pid) return false;
|
|
32211
|
+
try {
|
|
32212
|
+
process.kill(pid, 0);
|
|
32213
|
+
} catch {
|
|
32214
|
+
return false;
|
|
32215
|
+
}
|
|
32216
|
+
try {
|
|
32217
|
+
const cmd = (0, import_node_fs3.readFileSync)(`/proc/${pid}/cmdline`, "utf8");
|
|
32218
|
+
return cmd.includes("awake") && cmd.includes(tokenFile);
|
|
32219
|
+
} catch {
|
|
32220
|
+
return !(0, import_node_fs3.existsSync)("/proc");
|
|
32221
|
+
}
|
|
32222
|
+
}
|
|
32223
|
+
function parseAwakeArgs(rest) {
|
|
32224
|
+
const o = {};
|
|
32225
|
+
let again = false;
|
|
32226
|
+
for (let i = 0; i < rest.length; i++) {
|
|
32227
|
+
if (rest[i] === "--again") {
|
|
32228
|
+
again = true;
|
|
32229
|
+
continue;
|
|
32230
|
+
}
|
|
32231
|
+
const m = /^--(token-file|idle-minutes|max-minutes)$/.exec(rest[i]);
|
|
32232
|
+
const v = rest[i + 1];
|
|
32233
|
+
if (!m || v === void 0) throw new Error(`Usage: builders-cli awake --token-file <file> [--again] [--idle-minutes ${DEFAULT_IDLE_MIN}] [--max-minutes ${DEFAULT_MAX_MIN}]`);
|
|
32234
|
+
o[m[1]] = v;
|
|
32235
|
+
i++;
|
|
32236
|
+
}
|
|
32237
|
+
const tokenFile = o["token-file"];
|
|
32238
|
+
if (!tokenFile || !tokenFile.endsWith(".tunnel") || (0, import_node_path3.dirname)(tokenFile) !== (0, import_node_path3.join)((0, import_node_os.homedir)(), ".builders")) throw new Error("Give --token-file: the .tunnel file in ~/.builders from open_app.");
|
|
32239
|
+
const minutes = (k, d, max) => {
|
|
32240
|
+
const n = o[k] === void 0 ? d : Number(o[k]);
|
|
32241
|
+
if (!Number.isFinite(n) || n <= 0 || n > max) throw new Error(`--${k} must be more than 0 and at most ${max}.`);
|
|
32242
|
+
return n * 6e4;
|
|
32243
|
+
};
|
|
32244
|
+
return { tokenFile, again, idleMs: minutes("idle-minutes", DEFAULT_IDLE_MIN, 120), maxMs: minutes("max-minutes", DEFAULT_MAX_MIN, MAX_MAX_MIN) };
|
|
32245
|
+
}
|
|
32246
|
+
async function awake(o, say = (l) => console.log(l)) {
|
|
32247
|
+
const now = o.now ?? Date.now;
|
|
32248
|
+
const sleep = o.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
32249
|
+
if (!o.again && now() - mtime(idleFile(o.tokenFile)) < AFTER_IDLE_MS) {
|
|
32250
|
+
say("idle: the preview is not in use. Do nothing and say nothing, and do not run this command again until the user writes.");
|
|
32251
|
+
return "idle";
|
|
32252
|
+
}
|
|
32253
|
+
if (!o.again) {
|
|
32254
|
+
try {
|
|
32255
|
+
touchFile(promptFile(o.tokenFile), now());
|
|
32256
|
+
} catch {
|
|
32257
|
+
}
|
|
32258
|
+
}
|
|
32259
|
+
const pids = pidFile(o.tokenFile);
|
|
32260
|
+
if (!takeLock(pids, o.tokenFile)) {
|
|
32261
|
+
say("already waiting: another waiter keeps this session awake. Do nothing.");
|
|
32262
|
+
return "already waiting";
|
|
32263
|
+
}
|
|
32264
|
+
const start = now();
|
|
32265
|
+
try {
|
|
32266
|
+
for (; ; ) {
|
|
32267
|
+
const prompt = mtime(promptFile(o.tokenFile)) || start;
|
|
32268
|
+
const preview = now() - prompt < QUIET_MS ? mtime(activeFile(o.tokenFile)) : 0;
|
|
32269
|
+
const last = Math.max(prompt, preview);
|
|
32270
|
+
if (now() - last >= o.idleMs) {
|
|
32271
|
+
try {
|
|
32272
|
+
touchFile(idleFile(o.tokenFile), now());
|
|
32273
|
+
} catch {
|
|
32274
|
+
}
|
|
32275
|
+
say(`idle: nobody used the preview or the session for ${Math.round(o.idleMs / 6e4)} minutes. Do nothing and say nothing.`);
|
|
32276
|
+
return "idle";
|
|
32277
|
+
}
|
|
32278
|
+
if (now() - start >= o.maxMs) {
|
|
32279
|
+
say("in use: the preview or the session is still in use. Start this command again in the background with --again, and say nothing.");
|
|
32280
|
+
return "in use";
|
|
32281
|
+
}
|
|
32282
|
+
await sleep(Math.min(CHECK_MS, Math.max(1, last + o.idleMs - now())));
|
|
32283
|
+
}
|
|
32284
|
+
} finally {
|
|
32285
|
+
try {
|
|
32286
|
+
if ((0, import_node_fs3.readFileSync)(pids, "utf8").trim() === String(process.pid)) (0, import_node_fs3.unlinkSync)(pids);
|
|
32287
|
+
} catch {
|
|
32288
|
+
}
|
|
32289
|
+
}
|
|
32290
|
+
}
|
|
32291
|
+
function takeLock(pids, tokenFile) {
|
|
32292
|
+
const tmp = `${pids}.${process.pid}`;
|
|
32293
|
+
(0, import_node_fs3.writeFileSync)(tmp, `${process.pid}
|
|
32294
|
+
`, { mode: 384 });
|
|
32295
|
+
try {
|
|
32296
|
+
for (let tries = 0; tries < 5; tries++) {
|
|
32297
|
+
try {
|
|
32298
|
+
(0, import_node_fs3.linkSync)(tmp, pids);
|
|
32299
|
+
return true;
|
|
32300
|
+
} catch (e) {
|
|
32301
|
+
if (e.code !== "EEXIST") throw e;
|
|
32302
|
+
}
|
|
32303
|
+
if (waiterRuns(pids, tokenFile)) return false;
|
|
32304
|
+
try {
|
|
32305
|
+
(0, import_node_fs3.renameSync)(pids, `${pids}.gone.${process.pid}`);
|
|
32306
|
+
(0, import_node_fs3.unlinkSync)(`${pids}.gone.${process.pid}`);
|
|
32307
|
+
} catch {
|
|
32308
|
+
}
|
|
32309
|
+
}
|
|
32310
|
+
return false;
|
|
32311
|
+
} finally {
|
|
32312
|
+
try {
|
|
32313
|
+
(0, import_node_fs3.unlinkSync)(tmp);
|
|
32314
|
+
} catch {
|
|
32315
|
+
}
|
|
32316
|
+
}
|
|
32317
|
+
}
|
|
32318
|
+
async function awakeCommand(rest) {
|
|
32319
|
+
await awake(parseAwakeArgs(rest));
|
|
32320
|
+
return 0;
|
|
32321
|
+
}
|
|
32322
|
+
|
|
32323
|
+
// src/preview.ts
|
|
32324
|
+
var import_node_child_process2 = require("node:child_process");
|
|
32325
|
+
var import_node_fs4 = require("node:fs");
|
|
32326
|
+
var import_node_os2 = require("node:os");
|
|
32327
|
+
var import_node_path4 = require("node:path");
|
|
32155
32328
|
var TOKEN_MAX_AGE_MS = 12 * 60 * 60 * 1e3;
|
|
32156
|
-
var
|
|
32329
|
+
var CLI_VERSION = true ? "0.2.2" : "dev";
|
|
32330
|
+
function olderVersion(a, b) {
|
|
32331
|
+
const parse = (v) => /^\d+\.\d+\.\d+$/.test(v) ? v.split(".").map(Number) : null;
|
|
32332
|
+
const x = parse(a);
|
|
32333
|
+
const y = parse(b);
|
|
32334
|
+
if (!y) return false;
|
|
32335
|
+
if (!x) return true;
|
|
32336
|
+
for (let i = 0; i < 3; i++) if (x[i] !== y[i]) return x[i] < y[i];
|
|
32337
|
+
return false;
|
|
32338
|
+
}
|
|
32339
|
+
var buildersDir = () => (0, import_node_path4.join)((0, import_node_os2.homedir)(), ".builders");
|
|
32157
32340
|
function value(rest, i, name) {
|
|
32158
32341
|
const v = rest[i + 1];
|
|
32159
32342
|
if (v === void 0) throw new Error(`--${name} needs a value.`);
|
|
@@ -32174,11 +32357,11 @@ function parsePreviewArgs(rest, cwd) {
|
|
|
32174
32357
|
}
|
|
32175
32358
|
for (const k of ["port", "url", "preview-url", "token-file"]) if (!o[k]) throw new Error(`Give --${k}.`);
|
|
32176
32359
|
const record = {
|
|
32177
|
-
dir: (0,
|
|
32360
|
+
dir: (0, import_node_path4.resolve)(cwd),
|
|
32178
32361
|
port: Number(o.port),
|
|
32179
32362
|
tunnelUrl: o.url,
|
|
32180
32363
|
previewUrl: o["preview-url"],
|
|
32181
|
-
tokenFile: (0,
|
|
32364
|
+
tokenFile: (0, import_node_path4.resolve)(cwd, o["token-file"]),
|
|
32182
32365
|
...o["dev-url"] ? { devUrl: o["dev-url"] } : {},
|
|
32183
32366
|
...o["add-ons"] !== void 0 ? { addOns: o["add-ons"] } : {}
|
|
32184
32367
|
};
|
|
@@ -32188,7 +32371,7 @@ function parsePreviewArgs(rest, cwd) {
|
|
|
32188
32371
|
}
|
|
32189
32372
|
function checkRecord(r, host, now = Date.now()) {
|
|
32190
32373
|
if (!Number.isInteger(r.port) || r.port < 1024 || r.port > 65535) return "The port is not valid.";
|
|
32191
|
-
if (typeof r.dir !== "string" || !(0,
|
|
32374
|
+
if (typeof r.dir !== "string" || !(0, import_node_path4.isAbsolute)(r.dir) || !(0, import_node_fs4.existsSync)((0, import_node_path4.join)(r.dir, ".git"))) return "The app folder is not a git folder.";
|
|
32192
32375
|
const url = (u) => {
|
|
32193
32376
|
try {
|
|
32194
32377
|
return typeof u === "string" ? new URL(u) : null;
|
|
@@ -32202,10 +32385,10 @@ function checkRecord(r, host, now = Date.now()) {
|
|
|
32202
32385
|
if (!tunnel || tunnel.protocol !== "https:" || tunnel.host !== host || tunnel.pathname !== "/_tunnel") return "The tunnel URL is not of this Builders host.";
|
|
32203
32386
|
if (r.devUrl !== void 0 && (!dev || dev.protocol !== "https:" || dev.host !== host)) return "The dev URL is not of this Builders host.";
|
|
32204
32387
|
if (!preview || preview.protocol !== "https:" || !preview.host.startsWith("d--") || !preview.host.endsWith(`.${host}`)) return "The preview URL is not of this Builders host.";
|
|
32205
|
-
if (typeof r.tokenFile !== "string" || (0,
|
|
32388
|
+
if (typeof r.tokenFile !== "string" || (0, import_node_path4.dirname)(r.tokenFile) !== buildersDir() || !r.tokenFile.endsWith(".tunnel")) return "The token file is not in ~/.builders.";
|
|
32206
32389
|
let age;
|
|
32207
32390
|
try {
|
|
32208
|
-
age = now - (0,
|
|
32391
|
+
age = now - (0, import_node_fs4.statSync)(r.tokenFile).mtimeMs;
|
|
32209
32392
|
} catch {
|
|
32210
32393
|
return "The token file is missing.";
|
|
32211
32394
|
}
|
|
@@ -32213,16 +32396,25 @@ function checkRecord(r, host, now = Date.now()) {
|
|
|
32213
32396
|
if (r.addOns !== void 0 && !/^[a-z,-]*$/.test(r.addOns)) return "The add-ons are not valid.";
|
|
32214
32397
|
return null;
|
|
32215
32398
|
}
|
|
32216
|
-
var logFile = (r) => (0,
|
|
32399
|
+
var logFile = (r) => (0, import_node_path4.join)((0, import_node_os2.tmpdir)(), `builders-${(0, import_node_path4.basename)(r.dir)}-preview.log`);
|
|
32217
32400
|
var recordFile = (r) => r.tokenFile.replace(/\.tunnel$/, ".preview.json");
|
|
32218
|
-
var
|
|
32401
|
+
var pidFile2 = (r) => r.tokenFile.replace(/\.tunnel$/, ".pid");
|
|
32219
32402
|
function runningPid(r) {
|
|
32403
|
+
return runningPreview(r)?.pid ?? null;
|
|
32404
|
+
}
|
|
32405
|
+
var versionFile = (r) => r.tokenFile.replace(/\.tunnel$/, ".preview-version");
|
|
32406
|
+
function runningPreview(r) {
|
|
32220
32407
|
let pid;
|
|
32221
32408
|
try {
|
|
32222
|
-
pid = Number((0,
|
|
32409
|
+
pid = Number((0, import_node_fs4.readFileSync)(pidFile2(r), "utf8").trim());
|
|
32223
32410
|
} catch {
|
|
32224
32411
|
return null;
|
|
32225
32412
|
}
|
|
32413
|
+
let version = "";
|
|
32414
|
+
try {
|
|
32415
|
+
version = (0, import_node_fs4.readFileSync)(versionFile(r), "utf8").trim();
|
|
32416
|
+
} catch {
|
|
32417
|
+
}
|
|
32226
32418
|
if (!Number.isInteger(pid) || pid <= 1) return null;
|
|
32227
32419
|
try {
|
|
32228
32420
|
process.kill(pid, 0);
|
|
@@ -32230,15 +32422,16 @@ function runningPid(r) {
|
|
|
32230
32422
|
return null;
|
|
32231
32423
|
}
|
|
32232
32424
|
try {
|
|
32233
|
-
const cmd = (0,
|
|
32234
|
-
return cmd.includes("tunnel") && cmd.includes("--dev") ? pid : null;
|
|
32425
|
+
const cmd = (0, import_node_fs4.readFileSync)(`/proc/${pid}/cmdline`, "utf8");
|
|
32426
|
+
return cmd.includes("tunnel") && cmd.includes("--dev") ? { pid, version } : null;
|
|
32235
32427
|
} catch {
|
|
32236
|
-
return (0,
|
|
32428
|
+
return (0, import_node_fs4.existsSync)("/proc") ? null : { pid, version };
|
|
32237
32429
|
}
|
|
32238
32430
|
}
|
|
32239
32431
|
async function startPreview(r, o = {}) {
|
|
32240
|
-
const
|
|
32241
|
-
|
|
32432
|
+
const running = runningPreview(r);
|
|
32433
|
+
const pid = running?.pid ?? null;
|
|
32434
|
+
if (running && !o.restart && !olderVersion(running.version, o.version ?? CLI_VERSION)) return "running";
|
|
32242
32435
|
if (pid) {
|
|
32243
32436
|
const group = (signal) => {
|
|
32244
32437
|
try {
|
|
@@ -32250,7 +32443,7 @@ async function startPreview(r, o = {}) {
|
|
|
32250
32443
|
for (let i = 0; i < 50 && runningPid(r); i++) await new Promise((res) => setTimeout(res, 100));
|
|
32251
32444
|
group("SIGKILL");
|
|
32252
32445
|
}
|
|
32253
|
-
const log = (0,
|
|
32446
|
+
const log = (0, import_node_fs4.openSync)(logFile(r), "a");
|
|
32254
32447
|
const child = (0, import_node_child_process2.spawn)(process.execPath, [o.script ?? process.argv[1], "tunnel", "--dev", "--port", String(r.port)], {
|
|
32255
32448
|
cwd: r.dir,
|
|
32256
32449
|
detached: true,
|
|
@@ -32265,14 +32458,16 @@ async function startPreview(r, o = {}) {
|
|
|
32265
32458
|
}
|
|
32266
32459
|
});
|
|
32267
32460
|
child.unref();
|
|
32268
|
-
(0,
|
|
32461
|
+
(0, import_node_fs4.writeFileSync)(pidFile2(r), `${child.pid}
|
|
32462
|
+
`, { mode: 384 });
|
|
32463
|
+
(0, import_node_fs4.writeFileSync)(versionFile(r), `${o.version ?? CLI_VERSION}
|
|
32269
32464
|
`, { mode: 384 });
|
|
32270
32465
|
return "started";
|
|
32271
32466
|
}
|
|
32272
32467
|
async function previewCommand(rest, cwd = process.cwd()) {
|
|
32273
32468
|
const { record, restart } = parsePreviewArgs(rest, cwd);
|
|
32274
|
-
(0,
|
|
32275
|
-
(0,
|
|
32469
|
+
(0, import_node_fs4.mkdirSync)(buildersDir(), { recursive: true, mode: 448 });
|
|
32470
|
+
(0, import_node_fs4.writeFileSync)(recordFile(record), `${JSON.stringify(record)}
|
|
32276
32471
|
`, { mode: 384 });
|
|
32277
32472
|
const did = await startPreview(record, { restart });
|
|
32278
32473
|
console.error(did === "running" ? "The dev preview runs." : `The dev preview starts. Log: ${logFile(record)}`);
|
|
@@ -32284,18 +32479,18 @@ async function resumeCommand(rest) {
|
|
|
32284
32479
|
if (!host || !/^[a-z0-9.-]+$/.test(host)) throw new Error("Give --host <tenant host>.");
|
|
32285
32480
|
let names;
|
|
32286
32481
|
try {
|
|
32287
|
-
names = (0,
|
|
32482
|
+
names = (0, import_node_fs4.readdirSync)(buildersDir()).filter((n) => n.endsWith(".preview.json"));
|
|
32288
32483
|
} catch {
|
|
32289
32484
|
return 0;
|
|
32290
32485
|
}
|
|
32291
32486
|
for (const n of names) {
|
|
32292
32487
|
let r;
|
|
32293
32488
|
try {
|
|
32294
|
-
r = JSON.parse((0,
|
|
32489
|
+
r = JSON.parse((0, import_node_fs4.readFileSync)((0, import_node_path4.join)(buildersDir(), n), "utf8"));
|
|
32295
32490
|
} catch {
|
|
32296
32491
|
continue;
|
|
32297
32492
|
}
|
|
32298
|
-
if (checkRecord(r, host) || recordFile(r) !== (0,
|
|
32493
|
+
if (checkRecord(r, host) || recordFile(r) !== (0, import_node_path4.join)(buildersDir(), n)) continue;
|
|
32299
32494
|
await startPreview(r);
|
|
32300
32495
|
}
|
|
32301
32496
|
return 0;
|
|
@@ -32305,6 +32500,7 @@ async function resumeCommand(rest) {
|
|
|
32305
32500
|
var USAGE = `Usage: builders-cli tunnel --port <port> [--dev] [--url <tunnel url>] [--token-file <file>] [--transport auto|ws|poll]
|
|
32306
32501
|
builders-cli preview --port <port> --url <tunnel url> --preview-url <url> --token-file <file> [--dev-url <url>] [--add-ons <list>] [--restart]
|
|
32307
32502
|
builders-cli resume --host <tenant host>
|
|
32503
|
+
builders-cli awake --token-file <file> [--again] [--idle-minutes 15] [--max-minutes 25]
|
|
32308
32504
|
|
|
32309
32505
|
--dev also run the app's dev server (pnpm dev --port <port>) in this folder, and start it again when it stops
|
|
32310
32506
|
|
|
@@ -32312,7 +32508,7 @@ Environment:
|
|
|
32312
32508
|
BUILDERS_TUNNEL_TOKEN_FILE a file with the tunnel token (or --token-file)
|
|
32313
32509
|
BUILDERS_TUNNEL_TOKEN the tunnel token, if there is no token file
|
|
32314
32510
|
BUILDERS_TUNNEL_URL the tunnel URL, if --url is not given`;
|
|
32315
|
-
function readTokenFile(file, read = (f) => (0,
|
|
32511
|
+
function readTokenFile(file, read = (f) => (0, import_node_fs5.readFileSync)(f, "utf8")) {
|
|
32316
32512
|
let text;
|
|
32317
32513
|
try {
|
|
32318
32514
|
text = read(file);
|
|
@@ -32372,10 +32568,14 @@ ${USAGE}`);
|
|
|
32372
32568
|
${USAGE}`);
|
|
32373
32569
|
return { port, token, url: url.toString().replace(/\/$/, ""), transport, ...tokenFile ? { tokenFile } : {}, ...dev ? { dev } : {} };
|
|
32374
32570
|
}
|
|
32571
|
+
var useMarker = (mark) => (method, headers) => {
|
|
32572
|
+
if (isUse(method, headers)) mark();
|
|
32573
|
+
};
|
|
32375
32574
|
async function main(argv = process.argv.slice(2), env = process.env) {
|
|
32376
|
-
if (argv[0] === "preview" || argv[0] === "resume") {
|
|
32575
|
+
if (argv[0] === "preview" || argv[0] === "resume" || argv[0] === "awake") {
|
|
32377
32576
|
try {
|
|
32378
|
-
|
|
32577
|
+
const rest = argv.slice(1);
|
|
32578
|
+
return await (argv[0] === "preview" ? previewCommand(rest) : argv[0] === "resume" ? resumeCommand(rest) : awakeCommand(rest));
|
|
32379
32579
|
} catch (e) {
|
|
32380
32580
|
console.error(e instanceof Error ? e.message : String(e));
|
|
32381
32581
|
return 2;
|
|
@@ -32404,7 +32604,12 @@ async function main(argv = process.argv.slice(2), env = process.env) {
|
|
|
32404
32604
|
files = await startDevFiles({ dataDir, secret });
|
|
32405
32605
|
devEnv = { [DEV_DATA_DIR_ENV]: dataDir, [DEV_FILES_SECRET_ENV]: secret };
|
|
32406
32606
|
}
|
|
32407
|
-
const client = new TunnelClient({
|
|
32607
|
+
const client = new TunnelClient({
|
|
32608
|
+
...o,
|
|
32609
|
+
readToken,
|
|
32610
|
+
...files ? { filesPort: files.port } : {},
|
|
32611
|
+
...o.dev && file?.endsWith(".tunnel") ? { onRequest: useMarker(activityMarker(file)) } : {}
|
|
32612
|
+
});
|
|
32408
32613
|
const server = o.dev ? new DevServer({ port: o.port, env: devEnv }) : null;
|
|
32409
32614
|
const running = server?.run();
|
|
32410
32615
|
const stop = () => {
|
package/package.json
CHANGED