readingroom 0.2.0 → 0.4.0
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 +35 -7
- package/dist/assets/index-BRMmjGIp.js +190 -0
- package/dist/assets/index-C2oluWMZ.css +2 -0
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/src/git.js +216 -0
- package/src/server.js +521 -12
- package/src/tunnel.js +294 -0
- package/dist/assets/index-87varIK4.js +0 -190
- package/dist/assets/index-jgmgco4r.css +0 -2
package/src/tunnel.js
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import {
|
|
3
|
+
chmodSync,
|
|
4
|
+
existsSync,
|
|
5
|
+
mkdirSync,
|
|
6
|
+
mkdtempSync,
|
|
7
|
+
renameSync,
|
|
8
|
+
rmSync,
|
|
9
|
+
statSync,
|
|
10
|
+
writeFileSync,
|
|
11
|
+
} from "node:fs";
|
|
12
|
+
import { tmpdir } from "node:os";
|
|
13
|
+
import path from "node:path";
|
|
14
|
+
import { gunzipSync } from "node:zlib";
|
|
15
|
+
|
|
16
|
+
// Pinning matters: a downloaded binary's `--version` output must match this
|
|
17
|
+
// string, otherwise the tunnel refuses to start.
|
|
18
|
+
const CLOUDFLARED_VERSION = "2026.8.2";
|
|
19
|
+
const RELEASE_BASE =
|
|
20
|
+
"https://github.com/cloudflare/cloudflared/releases/download";
|
|
21
|
+
|
|
22
|
+
const PLATFORM_ASSETS = {
|
|
23
|
+
"darwin-arm64": { file: "cloudflared-darwin-arm64.tgz", archive: "tgz" },
|
|
24
|
+
"darwin-x64": { file: "cloudflared-darwin-amd64.tgz", archive: "tgz" },
|
|
25
|
+
"linux-x64": { file: "cloudflared-linux-amd64", archive: null },
|
|
26
|
+
"linux-arm64": { file: "cloudflared-linux-arm64", archive: null },
|
|
27
|
+
"win32-x64": { file: "cloudflared-windows-amd64.exe", archive: null },
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const URL_TIMEOUT_MS = 30_000;
|
|
31
|
+
const DOWNLOAD_TIMEOUT_MS = 120_000;
|
|
32
|
+
|
|
33
|
+
export function currentPlatformKey() {
|
|
34
|
+
return `${process.platform}-${process.arch}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function assetInfo(platformKey = currentPlatformKey()) {
|
|
38
|
+
return PLATFORM_ASSETS[platformKey] || null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function downloadUrl(platformKey = currentPlatformKey()) {
|
|
42
|
+
const asset = assetInfo(platformKey);
|
|
43
|
+
if (!asset) return null;
|
|
44
|
+
return `${RELEASE_BASE}/${CLOUDFLARED_VERSION}/${asset.file}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function cacheDir() {
|
|
48
|
+
if (process.platform === "win32") {
|
|
49
|
+
return path.join(
|
|
50
|
+
process.env.LOCALAPPDATA ||
|
|
51
|
+
path.join(process.env.USERPROFILE || tmpdir(), "AppData", "Local"),
|
|
52
|
+
"readingroom",
|
|
53
|
+
"cache",
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
if (process.platform === "darwin") {
|
|
57
|
+
return path.join(
|
|
58
|
+
process.env.HOME || tmpdir(),
|
|
59
|
+
"Library",
|
|
60
|
+
"Caches",
|
|
61
|
+
"readingroom",
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
return path.join(
|
|
65
|
+
process.env.XDG_CACHE_HOME || process.env.HOME || tmpdir(),
|
|
66
|
+
process.env.XDG_CACHE_HOME ? "readingroom" : ".cache/readingroom",
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Extract the first regular file from an uncompressed tar buffer. */
|
|
71
|
+
export function untarSingleFile(tar) {
|
|
72
|
+
let offset = 0;
|
|
73
|
+
while (offset + 512 <= tar.length) {
|
|
74
|
+
const header = tar.subarray(offset, offset + 512);
|
|
75
|
+
if (header.every((b) => b === 0)) break;
|
|
76
|
+
const name = header.subarray(0, 100).toString("utf8").replace(/\0.*$/, "");
|
|
77
|
+
const sizeField = header
|
|
78
|
+
.subarray(124, 136)
|
|
79
|
+
.toString("utf8")
|
|
80
|
+
.replace(/\0.*$/, "")
|
|
81
|
+
.trim();
|
|
82
|
+
const size = parseInt(sizeField, 8) || 0;
|
|
83
|
+
const type = header[156];
|
|
84
|
+
const dataStart = offset + 512;
|
|
85
|
+
// Type '0' or NUL is a regular file; skip directories ('5') and the rest.
|
|
86
|
+
if ((type === 0x30 || type === 0x00) && name) {
|
|
87
|
+
return tar.subarray(dataStart, dataStart + size);
|
|
88
|
+
}
|
|
89
|
+
offset = dataStart + Math.ceil(size / 512) * 512;
|
|
90
|
+
}
|
|
91
|
+
throw new Error("no regular file found in tar archive");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function spawnCapture(bin, args, timeoutMs = 10_000) {
|
|
95
|
+
return new Promise((resolve) => {
|
|
96
|
+
let done = false;
|
|
97
|
+
let out = "";
|
|
98
|
+
const child = spawn(bin, args, { stdio: ["ignore", "pipe", "pipe"] });
|
|
99
|
+
const timer = setTimeout(() => {
|
|
100
|
+
if (!done) {
|
|
101
|
+
done = true;
|
|
102
|
+
child.kill("SIGKILL");
|
|
103
|
+
resolve(null);
|
|
104
|
+
}
|
|
105
|
+
}, timeoutMs);
|
|
106
|
+
child.stdout.on("data", (d) => (out += d));
|
|
107
|
+
child.stderr.on("data", (d) => (out += d));
|
|
108
|
+
child.on("error", () => {
|
|
109
|
+
if (!done) {
|
|
110
|
+
done = true;
|
|
111
|
+
clearTimeout(timer);
|
|
112
|
+
resolve(null);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
child.on("close", () => {
|
|
116
|
+
if (!done) {
|
|
117
|
+
done = true;
|
|
118
|
+
clearTimeout(timer);
|
|
119
|
+
resolve(out);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export async function binaryVersion(binPath) {
|
|
126
|
+
const out = await spawnCapture(binPath, ["--version"]);
|
|
127
|
+
if (!out) return null;
|
|
128
|
+
const m = out.match(/cloudflared version ([0-9][^\s)]*)/i);
|
|
129
|
+
return m ? m[1] : null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Make sure a working cloudflared binary exists in the cache dir, downloading
|
|
134
|
+
* it on first use (one time). Returns its path. `onProgress({ phase,
|
|
135
|
+
* bytes, total })` reports download progress; total is null when the response
|
|
136
|
+
* has no content-length.
|
|
137
|
+
*/
|
|
138
|
+
export async function ensureCloudflared({ onProgress } = {}) {
|
|
139
|
+
const asset = assetInfo();
|
|
140
|
+
if (!asset) {
|
|
141
|
+
throw new Error(`no cloudflared build for ${currentPlatformKey()}`);
|
|
142
|
+
}
|
|
143
|
+
const dir = cacheDir();
|
|
144
|
+
const binPath = path.join(
|
|
145
|
+
dir,
|
|
146
|
+
`cloudflared-${CLOUDFLARED_VERSION}-${currentPlatformKey()}`,
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
if (existsSync(binPath) && statSync(binPath).size > 0) {
|
|
150
|
+
const version = await binaryVersion(binPath);
|
|
151
|
+
if (version === CLOUDFLARED_VERSION) return binPath;
|
|
152
|
+
// Corrupt or wrong-version cache entry: drop it and redownload.
|
|
153
|
+
rmSync(binPath, { force: true });
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
mkdirSync(dir, { recursive: true });
|
|
157
|
+
const res = await fetch(downloadUrl(), {
|
|
158
|
+
redirect: "follow",
|
|
159
|
+
signal: AbortSignal.timeout(DOWNLOAD_TIMEOUT_MS),
|
|
160
|
+
});
|
|
161
|
+
if (!res.ok) {
|
|
162
|
+
throw new Error(`could not download cloudflared (HTTP ${res.status})`);
|
|
163
|
+
}
|
|
164
|
+
const total = Number(res.headers.get("content-length")) || null;
|
|
165
|
+
let received = 0;
|
|
166
|
+
const chunks = [];
|
|
167
|
+
for await (const chunk of res.body) {
|
|
168
|
+
chunks.push(chunk);
|
|
169
|
+
received += chunk.length;
|
|
170
|
+
onProgress?.({ phase: "downloading", bytes: received, total });
|
|
171
|
+
}
|
|
172
|
+
let buffer = Buffer.concat(chunks);
|
|
173
|
+
if (asset.archive === "tgz") {
|
|
174
|
+
buffer = untarSingleFile(gunzipSync(buffer));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Write to a temp dir and rename into place so a killed download never
|
|
178
|
+
// leaves a half-written binary that looks cached.
|
|
179
|
+
const tmp = mkdtempSync(path.join(dir, ".download-"));
|
|
180
|
+
try {
|
|
181
|
+
const tmpPath = path.join(tmp, "cloudflared");
|
|
182
|
+
writeFileSync(tmpPath, buffer);
|
|
183
|
+
if (process.platform !== "win32") chmodSync(tmpPath, 0o755);
|
|
184
|
+
renameSync(tmpPath, binPath);
|
|
185
|
+
} finally {
|
|
186
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const version = await binaryVersion(binPath);
|
|
190
|
+
if (version !== CLOUDFLARED_VERSION) {
|
|
191
|
+
rmSync(binPath, { force: true });
|
|
192
|
+
throw new Error(
|
|
193
|
+
version
|
|
194
|
+
? `downloaded cloudflared reports version ${version}, expected ${CLOUDFLARED_VERSION}`
|
|
195
|
+
: "downloaded cloudflared binary failed to run",
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
return binPath;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const TUNNEL_URL_RE = /https:\/\/[a-z0-9][a-z0-9-]*\.trycloudflare\.com/i;
|
|
202
|
+
|
|
203
|
+
/** Pull the public quick-tunnel URL out of cloudflared's log output. */
|
|
204
|
+
export function parseTunnelUrl(text) {
|
|
205
|
+
const m = String(text).match(TUNNEL_URL_RE);
|
|
206
|
+
return m ? m[0] : null;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Start a Cloudflare quick tunnel (trycloudflare.com, no account needed)
|
|
211
|
+
* pointing at a local HTTP server. Resolves as soon as the public URL is
|
|
212
|
+
* known. Returns { url, host, stop() }.
|
|
213
|
+
*
|
|
214
|
+
* `onProgress` mirrors ensureCloudflared. `onUnexpectedExit(message)` fires if
|
|
215
|
+
* the child dies on its own after a successful start (network drop etc.).
|
|
216
|
+
*/
|
|
217
|
+
export async function startQuickTunnel({
|
|
218
|
+
targetPort,
|
|
219
|
+
onProgress,
|
|
220
|
+
onUnexpectedExit,
|
|
221
|
+
}) {
|
|
222
|
+
const binPath = await ensureCloudflared({ onProgress });
|
|
223
|
+
onProgress?.({ phase: "starting" });
|
|
224
|
+
|
|
225
|
+
const child = spawn(
|
|
226
|
+
binPath,
|
|
227
|
+
["tunnel", "--url", `http://127.0.0.1:${targetPort}`, "--no-autoupdate"],
|
|
228
|
+
{ stdio: ["ignore", "ignore", "pipe"] },
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
let stopped = false;
|
|
232
|
+
let urlFound = false;
|
|
233
|
+
let stderr = "";
|
|
234
|
+
|
|
235
|
+
const stop = () =>
|
|
236
|
+
new Promise((resolve) => {
|
|
237
|
+
if (stopped) return resolve();
|
|
238
|
+
stopped = true;
|
|
239
|
+
const killTimer = setTimeout(() => child.kill("SIGKILL"), 3000);
|
|
240
|
+
child.once("exit", () => {
|
|
241
|
+
clearTimeout(killTimer);
|
|
242
|
+
resolve();
|
|
243
|
+
});
|
|
244
|
+
child.kill("SIGTERM");
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
return await new Promise((resolve, reject) => {
|
|
248
|
+
const urlTimer = setTimeout(() => {
|
|
249
|
+
reject(new Error("tunnel did not come up in time"));
|
|
250
|
+
stop();
|
|
251
|
+
}, URL_TIMEOUT_MS);
|
|
252
|
+
|
|
253
|
+
child.on("error", (err) => {
|
|
254
|
+
clearTimeout(urlTimer);
|
|
255
|
+
reject(new Error(`could not run cloudflared: ${err.message}`));
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
const describeExit = (code, signal) =>
|
|
259
|
+
`${signal || `code ${code}`}${stderr.trim() ? ` — ${stderr.slice(-400).trim()}` : ""}`;
|
|
260
|
+
|
|
261
|
+
child.stderr.on("data", (chunk) => {
|
|
262
|
+
if (stderr.length < 8_000) stderr += chunk.toString("utf8");
|
|
263
|
+
if (urlFound) return;
|
|
264
|
+
const url = parseTunnelUrl(stderr);
|
|
265
|
+
if (url) {
|
|
266
|
+
urlFound = true;
|
|
267
|
+
clearTimeout(urlTimer);
|
|
268
|
+
resolve({
|
|
269
|
+
url,
|
|
270
|
+
host: new URL(url).hostname,
|
|
271
|
+
stop: async () => {
|
|
272
|
+
await stop();
|
|
273
|
+
},
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
child.on("exit", (code, signal) => {
|
|
279
|
+
clearTimeout(urlTimer);
|
|
280
|
+
if (stopped) return;
|
|
281
|
+
if (urlFound) {
|
|
282
|
+
onUnexpectedExit?.(
|
|
283
|
+
`cloudflared stopped (${describeExit(code, signal)})`,
|
|
284
|
+
);
|
|
285
|
+
} else {
|
|
286
|
+
reject(
|
|
287
|
+
new Error(
|
|
288
|
+
`cloudflared exited before the tunnel was ready (${describeExit(code, signal)})`,
|
|
289
|
+
),
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
}
|