prismalens 0.5.6 → 0.5.7

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 CHANGED
@@ -42,7 +42,8 @@ irm https://prismalens.io/install.ps1 | iex
42
42
  ```
43
43
 
44
44
  Both installers bring their own Node, check the download against the
45
- release's `SHA256SUMS`, and put `pl` on your PATH.
45
+ release's `SHA256SUMS`, and put `pl` on your PATH. With a display, they add
46
+ the desktop app and its launcher too; `--no-desktop` (`-NoDesktop`) skips it.
46
47
 
47
48
  ### Homebrew, and Scoop (preview)
48
49
 
@@ -55,6 +56,9 @@ scoop bucket add prismalens https://github.com/prismalens/scoop-bucket
55
56
  scoop install prismalens
56
57
  ```
57
58
 
59
+ The desktop app is a separate package: `brew install --cask
60
+ prismalens/tap/prismalens-app` or `scoop install prismalens/prismalens-app`.
61
+
58
62
  ## Quick start
59
63
 
60
64
  Check the machine, then start the app:
@@ -27,6 +27,12 @@ export const TARGETS = [
27
27
  const STOP_MESSAGE = "prismalens:stop";
28
28
  export const DEFAULT_BASE =
29
29
  "https://github.com/prismalens/prismalens/releases/download";
30
+ /** Tried first; it keeps only the newest releases, so GitHub is the fallback (#1096). */
31
+ export const MIRROR_BASE = "https://downloads.prismalens.io";
32
+ export const DEFAULT_BASES = [MIRROR_BASE, DEFAULT_BASE];
33
+ /** A base with a successor is dropped after this long without a byte, and may use this share of the budget. */
34
+ const MIRROR_STALL_MS = 30_000;
35
+ const MIRROR_BUDGET_SHARE = 0.6;
30
36
  const HOUR = 3_600_000;
31
37
 
32
38
  export class LauncherError extends Error {}
@@ -67,10 +73,10 @@ export function cacheRoot(env = process.env, platform = process.platform) {
67
73
  // --- the release server ------------------------------------------------------------------
68
74
 
69
75
  /** https: or file: only; credentials leave the URL for an Authorization header. */
70
- export function parseBase(raw) {
76
+ export function parseBase(raw, fallback = DEFAULT_BASE) {
71
77
  let url;
72
78
  try {
73
- url = new URL(raw || DEFAULT_BASE);
79
+ url = new URL(raw || fallback);
74
80
  } catch {
75
81
  throw new LauncherError(
76
82
  "PRISMALENS_RELEASE_BASE_URL is not a valid URL (its value is not shown: it may hold a password).",
@@ -125,11 +131,24 @@ async function fetchOnce(url, net) {
125
131
  const headers = {};
126
132
  if (net.auth && current.origin === net.origin)
127
133
  headers.authorization = net.auth;
128
- const res = await fetch(current, {
129
- headers,
130
- redirect: "manual",
131
- signal: AbortSignal.timeout(Math.max(1, net.deadline - Date.now())),
132
- });
134
+ // A server that never answers is dropped after stallMs, not after the shared deadline.
135
+ const silent = new AbortController();
136
+ const timer = net.stallMs
137
+ ? setTimeout(() => silent.abort(), net.stallMs)
138
+ : null;
139
+ let res;
140
+ try {
141
+ res = await fetch(current, {
142
+ headers,
143
+ redirect: "manual",
144
+ signal: AbortSignal.any([
145
+ AbortSignal.timeout(Math.max(1, net.deadline - Date.now())),
146
+ silent.signal,
147
+ ]),
148
+ });
149
+ } finally {
150
+ clearTimeout(timer);
151
+ }
133
152
  if (res.status >= 300 && res.status < 400 && res.headers.get("location")) {
134
153
  await res.body?.cancel();
135
154
  current = new URL(res.headers.get("location"), current);
@@ -141,6 +160,7 @@ async function fetchOnce(url, net) {
141
160
  }
142
161
 
143
162
  export async function get(url, net) {
163
+ const attempts = net.attempts ?? 6;
144
164
  if (url.startsWith("file:")) {
145
165
  const file = fileURLToPath(url);
146
166
  if (!fs.existsSync(file))
@@ -158,7 +178,7 @@ export async function get(url, net) {
158
178
  await res.body?.cancel();
159
179
  if (res.status === 404)
160
180
  throw new NotFound(`${redact(url, net.secrets)} does not exist`);
161
- if (!transient(res.status) || attempt === 6)
181
+ if (!transient(res.status) || attempt >= attempts)
162
182
  throw new LauncherError(
163
183
  `GET ${redact(url, net.secrets)} returned HTTP ${res.status}`,
164
184
  );
@@ -171,7 +191,7 @@ export async function get(url, net) {
171
191
  } catch (error) {
172
192
  if (error instanceof LauncherError) throw error;
173
193
  lastError = error;
174
- if (attempt === 6 || Date.now() >= net.deadline)
194
+ if (attempt >= attempts || Date.now() >= net.deadline)
175
195
  throw unreachable(url, error, net);
176
196
  wait = 500 * 2 ** (attempt - 1);
177
197
  }
@@ -192,7 +212,9 @@ function unreachable(url, error, net) {
192
212
  code || (error?.cause?.message ?? error?.message),
193
213
  net.secrets,
194
214
  );
195
- const where = net.isDefault ? "github.com" : redact(net.base, net.secrets);
215
+ const where = net.isDefault
216
+ ? new URL(net.base).host
217
+ : redact(net.base, net.secrets);
196
218
  const tls = /CERT|SELF_SIGNED|UNABLE_TO_(VERIFY|GET_ISSUER)/.test(code)
197
219
  ? " Its certificate is not trusted here: behind a TLS-inspecting proxy, set NODE_EXTRA_CA_CERTS to your organisation's CA file (npm's cafile is used when it is unset)."
198
220
  : "";
@@ -338,19 +360,35 @@ export function makeNet({
338
360
  env = process.env,
339
361
  log = () => {},
340
362
  deadlineMs,
363
+ stallMs = MIRROR_STALL_MS,
364
+ defaultBases = DEFAULT_BASES,
341
365
  } = {}) {
342
- const parsed = parseBase(env.PRISMALENS_RELEASE_BASE_URL);
366
+ const raw = env.PRISMALENS_RELEASE_BASE_URL;
367
+ const parsedAll = raw
368
+ ? [parseBase(raw)]
369
+ : defaultBases.map((b) => parseBase(undefined, b));
370
+ const parsed = parsedAll[0];
343
371
  const ms =
344
372
  deadlineMs ??
345
373
  (Number(env.PRISMALENS_LAUNCHER_TIMEOUT_SECONDS) || 900) * 1000;
346
374
  let applied = null;
347
- return {
348
- ...parsed,
349
- origin: parsed.base.startsWith("https:")
350
- ? new URL(parsed.base).origin
351
- : null,
375
+ const shared = {
376
+ secrets: parsedAll.flatMap((p) => p.secrets),
352
377
  deadline: Date.now() + ms,
353
378
  log,
379
+ };
380
+ // A base that has a successor is tried briefly: its failure moves on to the next one.
381
+ const bases = parsedAll.map((p, i) => ({
382
+ ...shared,
383
+ ...p,
384
+ origin: p.base.startsWith("https:") ? new URL(p.base).origin : null,
385
+ attempts: i < parsedAll.length - 1 ? 2 : 6,
386
+ stallMs: i < parsedAll.length - 1 ? stallMs : 0,
387
+ }));
388
+ return {
389
+ ...bases[0],
390
+ stallMs: 0,
391
+ bases,
354
392
  /** The proxy/CA settings in force, applied once before the first request. */
355
393
  ready() {
356
394
  if (!applied && !parsed.base.startsWith("file:")) {
@@ -728,6 +766,25 @@ export function releaseLock(file, token) {
728
766
  return removeEntry(file, token);
729
767
  }
730
768
 
769
+ /** One body read; a broken or silent stream is a LauncherError, so the next base is tried. */
770
+ async function readChunk(reader, net, label) {
771
+ let timer;
772
+ const silence = new Error();
773
+ const stalled = new Promise((_, reject) => {
774
+ if (net.stallMs) timer = setTimeout(() => reject(silence), net.stallMs);
775
+ });
776
+ try {
777
+ return await Promise.race([reader.read(), stalled]);
778
+ } catch (error) {
779
+ reader.cancel().catch(() => {});
780
+ throw new LauncherError(
781
+ `the download of ${label} ${error === silence ? `stalled for ${net.stallMs} ms` : `broke (${error.message})`}`,
782
+ );
783
+ } finally {
784
+ clearTimeout(timer);
785
+ }
786
+ }
787
+
731
788
  async function download(url, file, net, label) {
732
789
  const res = await get(url, net);
733
790
  const total = Number(res.headers.get("content-length")) || 0;
@@ -736,8 +793,11 @@ async function download(url, file, net, label) {
736
793
  const tty = process.stderr.isTTY;
737
794
  let received = 0;
738
795
  let shown = -1;
796
+ const reader = res.body.getReader();
739
797
  try {
740
- for await (const chunk of res.body) {
798
+ for (;;) {
799
+ const { done, value: chunk } = await readChunk(reader, net, label);
800
+ if (done) break;
741
801
  hash.update(chunk);
742
802
  fs.writeSync(out, chunk);
743
803
  received += chunk.byteLength;
@@ -797,12 +857,25 @@ export async function ensureRuntime(root, want, net) {
797
857
 
798
858
  /**
799
859
  * The digest the archive must have: the one npm's launcher manifest pins for its own stable
800
- * release, else the release's SHA256SUMS line, which must match a digest this copy verified before.
860
+ * release, else the release's SHA256SUMS line from the last base (GitHub, or the one a set
861
+ * PRISMALENS_RELEASE_BASE_URL names), which must match a digest this copy verified before.
862
+ * A mirror's SHA256SUMS is never read: a mirror could alter an archive and its sums together.
801
863
  */
802
- async function expectedDigest(want, net, at, archive) {
864
+ async function expectedDigest(want, trusted, archive) {
803
865
  if (want.pinned)
804
866
  return { hex: want.pinned, source: "the digest npm's launcher pins" };
805
- const sums = await (await get(`${at}/SHA256SUMS`, net)).text();
867
+ const host = new URL(trusted.base).host || "disk";
868
+ const url = `${trusted.base}/${want.tag}/SHA256SUMS`;
869
+ let sums;
870
+ try {
871
+ sums = await (await get(url, trusted)).text();
872
+ } catch (error) {
873
+ if (!(error instanceof LauncherError)) throw error;
874
+ const refused = `${want.tag}'s SHA256SUMS is not on ${host}, the only source of checksums; nothing was installed (${error.message})`;
875
+ throw error instanceof NotFound
876
+ ? new NotFound(refused)
877
+ : new LauncherError(refused);
878
+ }
806
879
  const line = sums
807
880
  .split(/\r?\n/)
808
881
  .map((l) => /^([0-9a-f]{64})\s+\*?(.+)$/i.exec(l.trim()))
@@ -817,21 +890,61 @@ async function expectedDigest(want, net, at, archive) {
817
890
  return { hex, source: "SHA256SUMS" };
818
891
  }
819
892
 
893
+ /**
894
+ * The archive in `file`, hashed and checked against the digest from the last base's SHA256SUMS.
895
+ * The archive may come from any base; when one fails (a missing file, a broken or silent body,
896
+ * a wrong hash) the next is asked. A base with a successor gets part of the budget, so the
897
+ * last one always keeps time. Resolves to the sha256.
898
+ */
899
+ async function fetchVerified(want, net, archive, file) {
900
+ const bases = net.bases ?? [net];
901
+ const hostOf = (b) => new URL(b.base).host || "disk";
902
+ const expected = await expectedDigest(want, bases.at(-1), archive);
903
+ const failures = [];
904
+ for (const [i, b] of bases.entries()) {
905
+ const last = i === bases.length - 1;
906
+ const now = Date.now();
907
+ const from = last
908
+ ? b
909
+ : {
910
+ ...b,
911
+ deadline: now + Math.floor((b.deadline - now) * MIRROR_BUDGET_SHARE),
912
+ };
913
+ try {
914
+ const actual = await download(
915
+ `${b.base}/${want.tag}/${archive}`,
916
+ file,
917
+ from,
918
+ `${want.version} from ${hostOf(b)}`,
919
+ );
920
+ if (actual !== expected.hex)
921
+ throw new LauncherError(
922
+ `checksum mismatch for ${archive} (${expected.source} says ${expected.hex}, the download is ${actual}); nothing was installed`,
923
+ );
924
+ return actual;
925
+ } catch (error) {
926
+ if (!(error instanceof LauncherError) || bases.length === 1) throw error;
927
+ failures.push(error.message);
928
+ if (!last)
929
+ net.log(
930
+ `${error.message.split("\n")[0]}; trying ${hostOf(bases[i + 1])}`,
931
+ );
932
+ }
933
+ }
934
+ throw new LauncherError(
935
+ `could not download ${archive} from any of ${bases.map(hostOf).join(", ")}:\n${failures.join("\n")}`,
936
+ );
937
+ }
938
+
820
939
  async function install(root, want, net) {
821
940
  const { version, target, tag } = want;
822
941
  net.ready();
823
942
  const archive = archiveName(version, target);
824
- const at = `${net.base}/${tag}`;
825
- const expected = await expectedDigest(want, net, at, archive);
826
943
  const staging = path.join(root, `.staging-${randomBytes(6).toString("hex")}`);
827
944
  fs.mkdirSync(staging);
828
945
  try {
829
946
  const file = path.join(staging, archive);
830
- const actual = await download(`${at}/${archive}`, file, net, version);
831
- if (actual !== expected.hex)
832
- throw new LauncherError(
833
- `checksum mismatch for ${archive} (${expected.source} says ${expected.hex}, the download is ${actual}); nothing was installed`,
834
- );
947
+ const actual = await fetchVerified(want, net, archive, file);
835
948
  // Windows' own bsdtar reads zip; a Git-for-Windows tar on PATH cannot.
836
949
  const tar = target.startsWith("win32")
837
950
  ? path.join(
@@ -1070,7 +1183,8 @@ export function help(release, root, target) {
1070
1183
  return `PrismaLens launcher ${release.version}
1071
1184
 
1072
1185
  pl from npm is a launcher. On first run it downloads PrismaLens ${release.version} for
1073
- ${target} from its GitHub release, checks it against the release's SHA256SUMS,
1186
+ ${target} from ${new URL(MIRROR_BASE).host}, or from its GitHub release when that
1187
+ has no copy, checks it against the SHA256SUMS of its GitHub release,
1074
1188
  keeps it in ${root}, and runs it. Later runs start from that copy offline.
1075
1189
 
1076
1190
  npm carries stable releases. Inside the app, pl upgrade --channel nightly moves this
@@ -1085,8 +1199,9 @@ Commands
1085
1199
 
1086
1200
  Environment
1087
1201
  PRISMALENS_RELEASE_BASE_URL Where releases come from, as <base>/v<version>/<file>;
1088
- https: or file: only. Default:
1089
- ${DEFAULT_BASE}
1202
+ https: or file: only. When it is
1203
+ set, only it is used. Default: archives from ${MIRROR_BASE},
1204
+ then ${DEFAULT_BASE}; SHA256SUMS always from the latter.
1090
1205
  https://user:token@host/... sends an Authorization header;
1091
1206
  the credentials are never printed.
1092
1207
  PRISMALENS_LAUNCHER_DIR Where runtimes are kept. Default: ${root}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prismalens",
3
- "version": "0.5.6",
3
+ "version": "0.5.7",
4
4
  "description": "Self-hosted incident investigation: an alert arrives, the coding agent you already use investigates a snapshot of the service's code, and the run, evidence and cause stay on the incident.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -38,16 +38,16 @@
38
38
  "prismalens": {
39
39
  "release": {
40
40
  "channel": "stable",
41
- "tag": "v0.5.6",
42
- "version": "0.5.6",
41
+ "tag": "v0.5.7",
42
+ "version": "0.5.7",
43
43
  "sha256": {
44
- "darwin-arm64": "76919001307784f1ce78d18b20200b6f111f04b50a7d1db0095678a0c7305dc8",
45
- "darwin-x64": "9f07cfcd0e28fe082505e2a0aaaa277f654f226362072c9a32b7764fa974a737",
46
- "linux-arm64": "26069d2b028c6ee45d5714eb0204852fd8c57131ed843777788ecd4fc646f371",
47
- "linux-x64": "bb987bd2f698796581d2702a8f520eb4d65c1ea42958507bbd2c60d311e9c55a",
48
- "win32-x64": "08d39084bf4e6692ca57d46bf4e8ed353b032747c5c40e9b3951867b3f2b6296"
44
+ "darwin-arm64": "b3a29b92e0467038ae3d7ab311f69952b7f3f041c454aecac37aff80ab88e898",
45
+ "darwin-x64": "7eb800c7d22b448ac441ee880078659c88fbd4aec4a3a4d7918b9147479fe89c",
46
+ "linux-arm64": "6875f4cf64f5e7238e2c3c1222705f36af7bde2f235b32bbadd648c7a6e14f47",
47
+ "linux-x64": "da3a146e800b4666b2e49463e81d6e820717cc9a8c509d8a7dc8d59b87090c12",
48
+ "win32-x64": "2b68197fd5e0c4142aa94f344793f666cbca7649fe7480eaf3c440eb68b0dc02"
49
49
  }
50
50
  }
51
51
  },
52
- "build": "c331126"
52
+ "build": "b427378"
53
53
  }