@n-dream/builders-cli 0.2.3 → 0.2.5
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/dist/builders-cli.cjs +97 -9
- package/package.json +1 -1
package/dist/builders-cli.cjs
CHANGED
|
@@ -31590,13 +31590,18 @@ var FORWARDED = /* @__PURE__ */ new Set(["x-forwarded-host", "x-forwarded-proto"
|
|
|
31590
31590
|
function defaultBackoff(attempt) {
|
|
31591
31591
|
return Math.min(3e4, 1e3 * 2 ** Math.min(attempt, 5)) * (0.75 + Math.random() * 0.5);
|
|
31592
31592
|
}
|
|
31593
|
+
var RESTART_JITTER_MS = 250;
|
|
31593
31594
|
var TunnelClient = class {
|
|
31594
31595
|
constructor(o) {
|
|
31595
31596
|
this.o = o;
|
|
31596
31597
|
}
|
|
31597
31598
|
o;
|
|
31598
31599
|
stopped = false;
|
|
31600
|
+
// The link that serves new streams.
|
|
31599
31601
|
link = null;
|
|
31602
|
+
// Links that the service drains (BYE "restart"): they serve their open
|
|
31603
|
+
// streams until the service closes them.
|
|
31604
|
+
draining = /* @__PURE__ */ new Set();
|
|
31600
31605
|
wake = null;
|
|
31601
31606
|
lastState = "";
|
|
31602
31607
|
say(line) {
|
|
@@ -31607,8 +31612,17 @@ var TunnelClient = class {
|
|
|
31607
31612
|
stop() {
|
|
31608
31613
|
this.stopped = true;
|
|
31609
31614
|
this.link?.close("stopped");
|
|
31615
|
+
for (const l of [...this.draining]) l.close("stopped");
|
|
31610
31616
|
this.wake?.();
|
|
31611
31617
|
}
|
|
31618
|
+
// Connects a new link now (the preview command found that the instance
|
|
31619
|
+
// that serves viewers does not have this link). Closes the current link,
|
|
31620
|
+
// or ends the wait before the next try.
|
|
31621
|
+
reconnect() {
|
|
31622
|
+
if (this.stopped) return;
|
|
31623
|
+
if (this.link) this.link.close("A new link was asked for.");
|
|
31624
|
+
else this.wake?.();
|
|
31625
|
+
}
|
|
31612
31626
|
// Connects and reconnects until stop(), a refusal or a replacement.
|
|
31613
31627
|
async run() {
|
|
31614
31628
|
let attempt = 0;
|
|
@@ -31648,6 +31662,11 @@ var TunnelClient = class {
|
|
|
31648
31662
|
this.say(`stopped: ${end.reason}`);
|
|
31649
31663
|
return { reason: end.reason, code: 1 };
|
|
31650
31664
|
}
|
|
31665
|
+
if (end.restart) {
|
|
31666
|
+
this.say("the tunnel service asked for a new link, reconnecting");
|
|
31667
|
+
await this.sleep(Math.floor(Math.random() * RESTART_JITTER_MS));
|
|
31668
|
+
continue;
|
|
31669
|
+
}
|
|
31651
31670
|
this.say(`disconnected (${end.reason}), reconnecting`);
|
|
31652
31671
|
await this.sleep((this.o.backoffMs ?? defaultBackoff)(attempt++));
|
|
31653
31672
|
}
|
|
@@ -31671,7 +31690,9 @@ var TunnelClient = class {
|
|
|
31671
31690
|
};
|
|
31672
31691
|
});
|
|
31673
31692
|
}
|
|
31674
|
-
// Serves one link until it closes
|
|
31693
|
+
// Serves one link until it closes, or until the service drains it (BYE
|
|
31694
|
+
// "restart", restart: true): then the link serves its open streams in
|
|
31695
|
+
// the background until it closes.
|
|
31675
31696
|
serve(link, onReady) {
|
|
31676
31697
|
this.link = link;
|
|
31677
31698
|
const session = new LinkSession(link, this.o.port, this.o.localHost ?? "127.0.0.1");
|
|
@@ -31684,6 +31705,11 @@ var TunnelClient = class {
|
|
|
31684
31705
|
};
|
|
31685
31706
|
session.onBye = (reason) => {
|
|
31686
31707
|
final = reason === "replaced" || reason === "revoked";
|
|
31708
|
+
if (reason === "restart" && this.link === link) {
|
|
31709
|
+
this.link = null;
|
|
31710
|
+
this.draining.add(link);
|
|
31711
|
+
resolve2({ reason, final: false, restart: true });
|
|
31712
|
+
}
|
|
31687
31713
|
};
|
|
31688
31714
|
const liveness = this.o.livenessMs ?? LIVENESS_MS;
|
|
31689
31715
|
const watch = setInterval(() => {
|
|
@@ -31693,7 +31719,8 @@ var TunnelClient = class {
|
|
|
31693
31719
|
link.onClose = (reason) => {
|
|
31694
31720
|
clearInterval(watch);
|
|
31695
31721
|
session.closeAll();
|
|
31696
|
-
this.link
|
|
31722
|
+
this.draining.delete(link);
|
|
31723
|
+
if (this.link === link) this.link = null;
|
|
31697
31724
|
resolve2({ reason: session.byeReason ?? reason, final });
|
|
31698
31725
|
};
|
|
31699
31726
|
if (link.closed) link.onClose("closed");
|
|
@@ -31761,6 +31788,7 @@ var LinkSession = class {
|
|
|
31761
31788
|
const reason = String(json(f).reason ?? "closed");
|
|
31762
31789
|
this.byeReason = reason;
|
|
31763
31790
|
this.onBye(reason);
|
|
31791
|
+
if (reason === "restart") return;
|
|
31764
31792
|
return this.link.close(reason);
|
|
31765
31793
|
}
|
|
31766
31794
|
case 16 /* REQ */: {
|
|
@@ -32081,6 +32109,7 @@ function emulatorEnv(ports, o) {
|
|
|
32081
32109
|
STORAGE_EMULATOR_HOST: `http://127.0.0.1:${ports.storage}`,
|
|
32082
32110
|
GOOGLE_CLOUD_PROJECT: EMULATOR_PROJECT,
|
|
32083
32111
|
GCLOUD_PROJECT: EMULATOR_PROJECT,
|
|
32112
|
+
METADATA_SERVER_DETECTION: "none",
|
|
32084
32113
|
...o.storage ? { STORAGE_BUCKET: EMULATOR_BUCKET } : {}
|
|
32085
32114
|
};
|
|
32086
32115
|
}
|
|
@@ -32952,7 +32981,7 @@ var import_node_fs4 = require("node:fs");
|
|
|
32952
32981
|
var import_node_os3 = require("node:os");
|
|
32953
32982
|
var import_node_path4 = require("node:path");
|
|
32954
32983
|
var TOKEN_MAX_AGE_MS = 12 * 60 * 60 * 1e3;
|
|
32955
|
-
var CLI_VERSION = true ? "0.2.
|
|
32984
|
+
var CLI_VERSION = true ? "0.2.5" : "dev";
|
|
32956
32985
|
function olderVersion(a, b) {
|
|
32957
32986
|
const parse = (v) => /^\d+\.\d+\.\d+$/.test(v) ? v.split(".").map(Number) : null;
|
|
32958
32987
|
const x = parse(a);
|
|
@@ -33047,17 +33076,50 @@ function runningPreview(r) {
|
|
|
33047
33076
|
} catch {
|
|
33048
33077
|
return null;
|
|
33049
33078
|
}
|
|
33079
|
+
let cmd;
|
|
33050
33080
|
try {
|
|
33051
|
-
|
|
33052
|
-
return cmd.includes("tunnel") && cmd.includes("--dev") ? { pid, version } : null;
|
|
33081
|
+
cmd = (0, import_node_fs4.readFileSync)(`/proc/${pid}/cmdline`, "utf8");
|
|
33053
33082
|
} catch {
|
|
33054
|
-
|
|
33083
|
+
if ((0, import_node_fs4.existsSync)("/proc")) return null;
|
|
33084
|
+
try {
|
|
33085
|
+
cmd = (0, import_node_child_process3.execFileSync)("ps", ["-p", String(pid), "-o", "command="], { encoding: "utf8", timeout: 5e3 });
|
|
33086
|
+
} catch {
|
|
33087
|
+
return null;
|
|
33088
|
+
}
|
|
33055
33089
|
}
|
|
33090
|
+
return cmd.includes("tunnel") && cmd.includes("--dev") ? { pid, version } : null;
|
|
33056
33091
|
}
|
|
33092
|
+
async function linkStatus(r, f = proxiedFetch(r.tunnelUrl)) {
|
|
33093
|
+
let token;
|
|
33094
|
+
try {
|
|
33095
|
+
token = (0, import_node_fs4.readFileSync)(r.tokenFile, "utf8").split(/\r?\n/, 1)[0].trim();
|
|
33096
|
+
} catch {
|
|
33097
|
+
return "unknown";
|
|
33098
|
+
}
|
|
33099
|
+
if (!token) return "unknown";
|
|
33100
|
+
try {
|
|
33101
|
+
const res = await f(`${r.tunnelUrl}/status`, { headers: { authorization: `Bearer ${token}` }, signal: AbortSignal.timeout(STATUS_TIMEOUT_MS) });
|
|
33102
|
+
if (!res.ok) {
|
|
33103
|
+
await res.body?.cancel().catch(() => void 0);
|
|
33104
|
+
return "unknown";
|
|
33105
|
+
}
|
|
33106
|
+
const b = await res.json();
|
|
33107
|
+
return b.connected === true ? "connected" : b.connected === false ? "disconnected" : "unknown";
|
|
33108
|
+
} catch {
|
|
33109
|
+
return "unknown";
|
|
33110
|
+
}
|
|
33111
|
+
}
|
|
33112
|
+
var STATUS_TIMEOUT_MS = 1e4;
|
|
33113
|
+
var STARTING_MS = 15e3;
|
|
33114
|
+
var RECONNECT_WAIT_MS = 2e4;
|
|
33115
|
+
var RECONNECT_VERSION = "0.2.5";
|
|
33057
33116
|
async function startPreview(r, o = {}) {
|
|
33058
33117
|
const running = runningPreview(r);
|
|
33059
33118
|
const pid = running?.pid ?? null;
|
|
33060
|
-
if (running && !o.restart && !olderVersion(running.version, o.version ?? CLI_VERSION))
|
|
33119
|
+
if (running && !o.restart && !olderVersion(running.version, o.version ?? CLI_VERSION)) {
|
|
33120
|
+
const reconnected = o.status ? await reconnectIfLost(r, running.pid, running.version, o) : "running";
|
|
33121
|
+
if (reconnected !== "failed") return reconnected;
|
|
33122
|
+
}
|
|
33061
33123
|
if (pid) {
|
|
33062
33124
|
const group = (signal) => {
|
|
33063
33125
|
try {
|
|
@@ -33090,13 +33152,38 @@ async function startPreview(r, o = {}) {
|
|
|
33090
33152
|
`, { mode: 384 });
|
|
33091
33153
|
return "started";
|
|
33092
33154
|
}
|
|
33155
|
+
async function reconnectIfLost(r, pid, version, o) {
|
|
33156
|
+
let age = Infinity;
|
|
33157
|
+
try {
|
|
33158
|
+
age = Date.now() - (0, import_node_fs4.statSync)(pidFile2(r)).mtimeMs;
|
|
33159
|
+
} catch {
|
|
33160
|
+
}
|
|
33161
|
+
if (age < (o.startingMs ?? STARTING_MS)) return "running";
|
|
33162
|
+
if (await o.status() !== "disconnected") return "running";
|
|
33163
|
+
if (olderVersion(version, RECONNECT_VERSION)) return "failed";
|
|
33164
|
+
try {
|
|
33165
|
+
process.kill(pid, "SIGUSR2");
|
|
33166
|
+
} catch {
|
|
33167
|
+
return "failed";
|
|
33168
|
+
}
|
|
33169
|
+
const end = Date.now() + (o.reconnectWaitMs ?? RECONNECT_WAIT_MS);
|
|
33170
|
+
let last = "disconnected";
|
|
33171
|
+
while (Date.now() < end) {
|
|
33172
|
+
await new Promise((res) => setTimeout(res, o.pollMs ?? 1e3));
|
|
33173
|
+
last = await o.status();
|
|
33174
|
+
if (last === "connected") return "reconnected";
|
|
33175
|
+
}
|
|
33176
|
+
return last === "unknown" ? "running" : "failed";
|
|
33177
|
+
}
|
|
33093
33178
|
async function previewCommand(rest, cwd = process.cwd()) {
|
|
33094
33179
|
const { record, restart } = parsePreviewArgs(rest, cwd);
|
|
33095
33180
|
(0, import_node_fs4.mkdirSync)(buildersDir(), { recursive: true, mode: 448 });
|
|
33096
33181
|
(0, import_node_fs4.writeFileSync)(recordFile(record), `${JSON.stringify(record)}
|
|
33097
33182
|
`, { mode: 384 });
|
|
33098
|
-
const did = await startPreview(record, { restart });
|
|
33099
|
-
console.error(
|
|
33183
|
+
const did = await startPreview(record, { restart, status: () => linkStatus(record) });
|
|
33184
|
+
console.error(
|
|
33185
|
+
did === "running" ? "The dev preview runs." : did === "reconnected" ? "The dev preview runs. Its link was connected again." : `The dev preview starts. Log: ${logFile(record)}`
|
|
33186
|
+
);
|
|
33100
33187
|
console.error(javaProblem() ?? "The test data is in the Firebase emulators (.builders-dev/emulators/), saved every 30 s. The first start takes about 30 s.");
|
|
33101
33188
|
return 0;
|
|
33102
33189
|
}
|
|
@@ -33253,6 +33340,7 @@ async function main(argv = process.argv.slice(2), env = process.env) {
|
|
|
33253
33340
|
};
|
|
33254
33341
|
process.on("SIGINT", stop);
|
|
33255
33342
|
process.on("SIGTERM", stop);
|
|
33343
|
+
process.on("SIGUSR2", () => client.reconnect());
|
|
33256
33344
|
const { code } = await client.run();
|
|
33257
33345
|
server?.stop();
|
|
33258
33346
|
await emulators?.stop();
|
package/package.json
CHANGED