@wix/himalaya-cli 0.1911.0 → 0.1913.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.
@@ -48,7 +48,11 @@ export interface PageStream {
48
48
  next(data: unknown): void;
49
49
  end(): void;
50
50
  error(e: unknown): void;
51
- /** Called once if the page cancels (`break` out of its iterator) or the instance unmounts. */
51
+ /**
52
+ * Called once when the stream is abandoned: the page cancels (`break` out of its iterator), the
53
+ * instance unmounts, reloads or crashes, the same request id is sent again (a retry), or the
54
+ * handler opened the stream but returned something else. Not called after `end()`/`error()`.
55
+ */
52
56
  onCancel(cb: () => void): void;
53
57
  readonly closed: boolean;
54
58
  }
@@ -22,8 +22,11 @@ export declare function onPageMessage(cb: (msg: unknown, instanceId: string) =>
22
22
  * `himalaya.<namespace>.<method>(…)`) with worker code. A handler gets `(args, ctx)` and returns a value or a promise; a throw answers
23
23
  * `ok: false`. To stream, return `openPageStream(ctx.instanceId, ctx.requestId)`.
24
24
  *
25
- * Throws when `namespace` is `"himalaya"`, already registered, already exposed, or not
26
- * `/^[a-z][a-z0-9_]{0,63}$/`. A method the namespace lacks answers `code: "unknown_method"`.
25
+ * Throws when `namespace` is `"himalaya"`, already registered, already exposed, reserved by the
26
+ * page client (`call`, `on`, `probe`, `version`, `then`, `constructor`, `general`, `site`), or not
27
+ * `/^[a-z][a-z0-9_]{0,63}$/`; and when a method name is not a dotted path of up to four
28
+ * `/^[A-Za-z][A-Za-z0-9_]{0,63}$/` segments, starts with an `on` segment, or has a `then`,
29
+ * `toString` or `valueOf` segment (the page proxy cannot call those). A method the namespace lacks answers `code: "unknown_method"`.
27
30
  */
28
31
  export declare function onPageRequest(namespace: string, handlers: Readonly<Record<string, PageRequestHandler>>): void;
29
32
  /**
@@ -144,7 +144,9 @@ function createPageClient(getBridge) {
144
144
  handlers.delete(entry);
145
145
  };
146
146
  }
147
+ const MANIFEST_TTL_MS = 1e3;
147
148
  let manifest;
149
+ const fresh = (bridge) => manifest?.on === bridge && Date.now() - manifest.at < MANIFEST_TTL_MS;
148
150
  function currentBridge() {
149
151
  try {
150
152
  return workerBridge();
@@ -171,7 +173,7 @@ function createPageClient(getBridge) {
171
173
  done = true;
172
174
  clearInterval(again);
173
175
  if (asking === state) asking = void 0;
174
- if (list) manifest = { on: target, list };
176
+ if (list) manifest = { on: target, list, at: Date.now() };
175
177
  resolve(list);
176
178
  };
177
179
  const ask = () => {
@@ -194,15 +196,15 @@ function createPageClient(getBridge) {
194
196
  async function enabledCapabilities2(timeoutMs = 3e3) {
195
197
  const bridge = currentBridge();
196
198
  if (!bridge) return null;
197
- if (manifest?.on === bridge) return manifest.list;
198
- if (!(timeoutMs > 0)) return null;
199
+ if (fresh(bridge)) return manifest.list;
200
+ if (!(timeoutMs > 0)) return manifest?.on === bridge ? manifest.list : null;
199
201
  const budget = Math.min(timeoutMs, 2147483647);
200
202
  const answer = askShared(bridge, budget);
201
203
  return new Promise((resolve) => {
202
204
  const stop = setTimeout(() => resolve(manifest?.on === bridge ? manifest.list : null), budget);
203
205
  void answer.then((list) => {
204
206
  clearTimeout(stop);
205
- resolve(list);
207
+ resolve(list ?? (manifest?.on === bridge ? manifest.list : null));
206
208
  });
207
209
  });
208
210
  }
@@ -167,6 +167,7 @@ var RESERVED_NAMESPACES = /* @__PURE__ */ new Set(["call", "on", "probe", "versi
167
167
  var UNREACHABLE_SEGMENTS = /* @__PURE__ */ new Set(["then", "toString", "valueOf"]);
168
168
  var NAMESPACE_PATTERN = /^[a-z][a-z0-9_]{0,63}$/;
169
169
  var METHOD_PATTERN = /^[A-Za-z][A-Za-z0-9_]{0,63}(?:\.[A-Za-z][A-Za-z0-9_]{0,63}){0,3}$/;
170
+ var MAX_ABANDONED = 256;
170
171
  var ID_PATTERN = /^[A-Za-z0-9-]{1,96}$/;
171
172
  function pageRequest(value) {
172
173
  if (!value || typeof value !== "object") return null;
@@ -238,6 +239,18 @@ function createPageDispatcher(runtime2) {
238
239
  throw new Error(`page ${kind} "${name}" is reserved: the page's himalaya.${name} is not a namespace`);
239
240
  }
240
241
  }
242
+ function assertMethodShape(namespace, method) {
243
+ if (typeof method !== "string" || !METHOD_PATTERN.test(method)) {
244
+ throw new Error(`page method ${JSON.stringify(`${namespace}.${method}`)} must match ${METHOD_PATTERN}`);
245
+ }
246
+ }
247
+ function assertMethod(namespace, method) {
248
+ assertMethodShape(namespace, method);
249
+ const segments = method.split(".");
250
+ if (segments[0] === "on" || segments.some((segment) => UNREACHABLE_SEGMENTS.has(segment))) {
251
+ throw new Error(`page method ${JSON.stringify(`${namespace}.${method}`)} is reserved: the page proxy cannot call it`);
252
+ }
253
+ }
241
254
  function expose(capabilities) {
242
255
  for (const id of Object.keys(capabilities)) {
243
256
  if (id === PAGE_HANDSHAKE_CAPABILITY) {
@@ -245,6 +258,7 @@ function createPageDispatcher(runtime2) {
245
258
  }
246
259
  assertReachable(id, "capability");
247
260
  assertFree(id);
261
+ for (const method of capabilities[id].methods) assertMethodShape(id, method);
248
262
  }
249
263
  for (const [id, entry] of Object.entries(capabilities)) {
250
264
  exposed.set(id, { module: entry.module, methods: new Set(entry.methods) });
@@ -259,13 +273,7 @@ function createPageDispatcher(runtime2) {
259
273
  assertFree(namespace);
260
274
  const methods = /* @__PURE__ */ new Map();
261
275
  for (const [method, handler] of Object.entries(handlers ?? {})) {
262
- if (!METHOD_PATTERN.test(method)) {
263
- throw new Error(`page method ${JSON.stringify(`${namespace}.${method}`)} must match ${METHOD_PATTERN}`);
264
- }
265
- const segments = method.split(".");
266
- if (segments[0] === "on" || segments.some((segment) => UNREACHABLE_SEGMENTS.has(segment))) {
267
- throw new Error(`page method ${JSON.stringify(`${namespace}.${method}`)} is reserved: the page proxy cannot call it`);
268
- }
276
+ assertMethod(namespace, method);
269
277
  if (typeof handler !== "function") throw new Error(`page handler ${namespace}.${method} is not a function`);
270
278
  methods.set(method, handler);
271
279
  }
@@ -406,20 +414,23 @@ function createPageDispatcher(runtime2) {
406
414
  }
407
415
  function onPage(msg, instanceId) {
408
416
  const sys = msg && typeof msg === "object" && msg.v === 1 ? msg.sys : void 0;
409
- if (sys === "cancel") return cancel(instanceId, msg.sid);
410
- if (sys === "console") return forwardConsole(instanceId, msg);
417
+ if (sys === "cancel" || sys === "console") {
418
+ if (!gone.has(instanceId)) live.add(instanceId);
419
+ if (sys === "cancel") return cancel(instanceId, msg.sid);
420
+ return forwardConsole(instanceId, msg);
421
+ }
411
422
  if (sys === "site") {
412
423
  if (!gone.has(instanceId)) deliverSite(site.page(msg, instanceId));
413
424
  return;
414
425
  }
415
- const dead = gone.has(instanceId);
416
- if (!dead) live.add(instanceId);
426
+ if (gone.has(instanceId)) return;
427
+ live.add(instanceId);
417
428
  const request = exposed.size > 0 || namespaces.size > 0 ? pageRequest(msg) : null;
418
429
  if (!request) {
419
430
  raw?.(msg, instanceId);
420
431
  return;
421
432
  }
422
- if (!dead) route(request, instanceId, msg);
433
+ route(request, instanceId, msg);
423
434
  }
424
435
  function route(request, instanceId, message) {
425
436
  const respond = (response) => post(instanceId, { v: 1, id: request.id, ...response });
@@ -515,7 +526,7 @@ function createPageDispatcher(runtime2) {
515
526
  function begin(k) {
516
527
  const previous = inFlight.get(k);
517
528
  if (previous && !previous.abandoned) {
518
- abandon(k, previous);
529
+ previous.abandoned = true;
519
530
  const stream = ownStream(k, previous);
520
531
  if (stream) discard(k, stream);
521
532
  }
@@ -561,6 +572,7 @@ function createPageDispatcher(runtime2) {
561
572
  if (stream) discard(k, stream);
562
573
  }
563
574
  function forwardConsole(instanceId, msg) {
575
+ if (gone.has(instanceId)) return;
564
576
  const event = {
565
577
  instanceId,
566
578
  level: typeof msg.level === "string" ? msg.level : "log",
@@ -640,6 +652,17 @@ function createPageDispatcher(runtime2) {
640
652
  if (!k.startsWith(prefix)) continue;
641
653
  abandon(k, pending);
642
654
  }
655
+ trimAbandoned();
656
+ }
657
+ function trimAbandoned() {
658
+ let total = 0;
659
+ for (const set of lingering.values()) total += set.size;
660
+ for (const k of [...lingering.keys()]) {
661
+ if (total <= MAX_ABANDONED) break;
662
+ total -= lingering.get(k).size;
663
+ lingering.delete(k);
664
+ if (inFlight.get(k)?.abandoned) inFlight.delete(k);
665
+ }
643
666
  }
644
667
  function unmount(instanceId) {
645
668
  live.delete(instanceId);
@@ -2,7 +2,7 @@
2
2
  "files": [
3
3
  {
4
4
  "name": "sw.js",
5
- "bytes": 2802985
5
+ "bytes": 2803317
6
6
  },
7
7
  {
8
8
  "name": "host.html",
@@ -21,7 +21,7 @@
21
21
  "bytes": 24320
22
22
  },
23
23
  {
24
- "name": "preview-deferred/d7d3e5a20cf0acc0fb9058dba6f24d4c6646f0889e3a8cff510b29c41d3eafd4.bin",
24
+ "name": "preview-deferred/a5d585737ec0f4d39abdf4a8e7c45350a720c45c62afbba5ae54265cd4d8ec70.bin",
25
25
  "bytes": 488215
26
26
  },
27
27
  {
@@ -79,10 +79,10 @@
79
79
  ],
80
80
  "deferred": [
81
81
  {
82
- "key": "assets/pdf-B6sk-tIn.js",
82
+ "key": "assets/pdf-IpAn7-Aa.js",
83
83
  "contentType": "application/javascript; charset=utf-8",
84
- "sha256": "d7d3e5a20cf0acc0fb9058dba6f24d4c6646f0889e3a8cff510b29c41d3eafd4",
85
- "file": "preview-deferred/d7d3e5a20cf0acc0fb9058dba6f24d4c6646f0889e3a8cff510b29c41d3eafd4.bin"
84
+ "sha256": "a5d585737ec0f4d39abdf4a8e7c45350a720c45c62afbba5ae54265cd4d8ec70",
85
+ "file": "preview-deferred/a5d585737ec0f4d39abdf4a8e7c45350a720c45c62afbba5ae54265cd4d8ec70.bin"
86
86
  },
87
87
  {
88
88
  "key": "assets/pdf.worker.min-BGnVRhRG.js",