@volter/tabnode 0.5.29 → 0.5.31

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/__sw__.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Service Worker for Mini WebContainers
3
3
  * Intercepts fetch requests and routes them to virtual servers
4
- * Version: 16 - a host per page: each page's channel, servers and preview are its own
4
+ * Version: 17 - a host's document prelude: its frames' documents start with it
5
5
  */
6
6
 
7
7
  const DEBUG = false;
@@ -158,6 +158,9 @@ self.addEventListener('message', (event) => {
158
158
  if (hosts.get(host.id) === host && host.port === initializedPort) handleMainMessage(host, message);
159
159
  };
160
160
  if (data && Array.isArray(data.ownDocuments)) host.ownDocuments = new Set(data.ownDocuments.map((path) => ownPath(String(path))));
161
+ // Markup a frame's document served from this host's servers starts with
162
+ // (after its doctype): the page's first script, before the document's own.
163
+ host.documentPrelude = data && typeof data.documentPrelude === 'string' && data.documentPrelude ? data.documentPrelude : null;
161
164
  DEBUG && console.log('[SW] Initialized communication channel with transferred port');
162
165
  // Re-claim clients so that pages opened after SW activation get controlled.
163
166
  // Without this, controllerchange never fires for late-arriving pages.
@@ -664,7 +667,7 @@ self.addEventListener('fetch', (event) => {
664
667
  const targetPath = url.pathname + url.search;
665
668
  if (context.prefix === null) {
666
669
  DEBUG && console.log('[SW] Answering a root request from the virtual server:', url.pathname);
667
- return handleRootedRequest(event, context.port, targetPath);
670
+ return withDocumentPrelude(event, await handleRootedRequest(event, context.port, targetPath));
668
671
  }
669
672
  if (event.request.mode === 'navigate') {
670
673
  DEBUG && console.log('[SW] Redirecting navigation from virtual context:', url.pathname);
@@ -683,9 +686,54 @@ self.addEventListener('fetch', (event) => {
683
686
  const port = parseInt(match[1], 10);
684
687
  const path = match[2] || '/';
685
688
 
686
- event.respondWith(handleVirtualRequest(event.request, port, path + url.search));
689
+ event.respondWith(handleVirtualRequest(event.request, port, path + url.search).then((response) => withDocumentPrelude(event, response)));
687
690
  });
688
691
 
692
+ /**
693
+ * A frame's HTML document from a host's servers, with the host's prelude
694
+ * first: after a leading doctype (so the document keeps its mode), else at
695
+ * the very start, where the parser puts a script into the head it makes.
696
+ */
697
+ async function withDocumentPrelude(event, response) {
698
+ const request = event.request;
699
+ if (request.mode !== 'navigate' || (request.destination !== 'iframe' && request.destination !== 'frame')) return response;
700
+ if (!response.body || !/text\/html/i.test(response.headers.get('content-type') || '')) return response;
701
+ let host = null;
702
+ try { host = await hostForEvent(event); } catch (e) { /* no host */ }
703
+ const prelude = host && host.documentPrelude;
704
+ if (!prelude) return response;
705
+ const encoder = new TextEncoder();
706
+ const inserted = encoder.encode(prelude);
707
+ let pending = new Uint8Array(0);
708
+ let done = false;
709
+ const release = (controller, cut) => {
710
+ controller.enqueue(pending.subarray(0, cut));
711
+ controller.enqueue(inserted);
712
+ controller.enqueue(pending.subarray(cut));
713
+ pending = new Uint8Array(0);
714
+ done = true;
715
+ };
716
+ const body = response.body.pipeThrough(new TransformStream({
717
+ transform(chunk, controller) {
718
+ if (done) { controller.enqueue(chunk); return; }
719
+ const joined = new Uint8Array(pending.length + chunk.length);
720
+ joined.set(pending); joined.set(chunk, pending.length);
721
+ pending = joined;
722
+ const head = new TextDecoder().decode(pending.subarray(0, 2048));
723
+ const doctype = /^\uFEFF?\s*<!doctype[^>]*>/i.exec(head);
724
+ if (doctype) { release(controller, encoder.encode(doctype[0]).length); return; }
725
+ // A doctype still arriving waits for its end; anything else is no doctype.
726
+ const start = head.replace(/^\uFEFF?\s*/, '');
727
+ if (pending.length < 2048 && ('<!doctype'.startsWith(start.toLowerCase()) || /^<!doctype/i.test(start))) return;
728
+ release(controller, 0);
729
+ },
730
+ flush(controller) { if (!done) release(controller, 0); },
731
+ }));
732
+ const headers = new Headers(response.headers);
733
+ headers.delete('content-length');
734
+ return new Response(body, { status: response.status, statusText: response.statusText, headers });
735
+ }
736
+
689
737
  /**
690
738
  * Handle a request to a virtual server
691
739
  */
@@ -17671,11 +17671,9 @@ class Zlib {
17671
17671
  __privateMethod(this, _Zlib_instances, run_fn).call(this, flush2, inBuf, inOff, inLen, outBuf, outOff, outLen);
17672
17672
  (_a4 = __privateGet(this, _callback)) == null ? void 0 : _a4.call(this);
17673
17673
  });
17674
- return this;
17675
17674
  }
17676
17675
  writeSync(flush2, inBuf, inOff, inLen, outBuf, outOff, outLen) {
17677
17676
  __privateMethod(this, _Zlib_instances, run_fn).call(this, flush2, inBuf, inOff, inLen, outBuf, outOff, outLen);
17678
- return this;
17679
17677
  }
17680
17678
  /**
17681
17679
  * `deflateParams` mid-stream. pako's deflate does not carry it, so the
@@ -17844,11 +17842,9 @@ class BrotliDecoderHandle {
17844
17842
  __privateMethod(this, _BrotliDecoderHandle_instances, run_fn2).call(this, flush2, inBuf, inOff, inLen, outBuf, outOff, outLen);
17845
17843
  (_a4 = __privateGet(this, _callback2)) == null ? void 0 : _a4.call(this);
17846
17844
  });
17847
- return this;
17848
17845
  }
17849
17846
  writeSync(flush2, inBuf, inOff, inLen, outBuf, outOff, outLen) {
17850
17847
  __privateMethod(this, _BrotliDecoderHandle_instances, run_fn2).call(this, flush2, inBuf, inOff, inLen, outBuf, outOff, outLen);
17851
- return this;
17852
17848
  }
17853
17849
  params() {
17854
17850
  }
@@ -21140,17 +21136,10 @@ var implementation$1 = function bind2(that) {
21140
21136
  var implementation = implementation$1;
21141
21137
  var functionBind = Function.prototype.bind || implementation;
21142
21138
  var functionCall = Function.prototype.call;
21143
- var functionApply;
21144
- var hasRequiredFunctionApply;
21145
- function requireFunctionApply() {
21146
- if (hasRequiredFunctionApply) return functionApply;
21147
- hasRequiredFunctionApply = 1;
21148
- functionApply = Function.prototype.apply;
21149
- return functionApply;
21150
- }
21139
+ var functionApply = Function.prototype.apply;
21151
21140
  var reflectApply = typeof Reflect !== "undefined" && Reflect && Reflect.apply;
21152
21141
  var bind$2 = functionBind;
21153
- var $apply$1 = requireFunctionApply();
21142
+ var $apply$1 = functionApply;
21154
21143
  var $call$2 = functionCall;
21155
21144
  var $reflectApply = reflectApply;
21156
21145
  var actualApply = $reflectApply || bind$2.call($call$2, $apply$1);
@@ -21270,7 +21259,7 @@ var hasSymbols = requireHasSymbols()();
21270
21259
  var getProto = requireGetProto();
21271
21260
  var $ObjectGPO = requireObject_getPrototypeOf();
21272
21261
  var $ReflectGPO = requireReflect_getPrototypeOf();
21273
- var $apply = requireFunctionApply();
21262
+ var $apply = functionApply;
21274
21263
  var $call = functionCall;
21275
21264
  var needsEval = {};
21276
21265
  var TypedArray = typeof Uint8Array === "undefined" || !getProto ? undefined$1 : getProto(Uint8Array);
@@ -21911,7 +21900,7 @@ function requireApplyBind() {
21911
21900
  if (hasRequiredApplyBind) return applyBind;
21912
21901
  hasRequiredApplyBind = 1;
21913
21902
  var bind3 = functionBind;
21914
- var $apply2 = requireFunctionApply();
21903
+ var $apply2 = functionApply;
21915
21904
  var actualApply$1 = actualApply;
21916
21905
  applyBind = function applyBind2() {
21917
21906
  return actualApply$1(bind3, $apply2, arguments);
package/dist/index.mjs CHANGED
@@ -17799,11 +17799,9 @@ class Zlib {
17799
17799
  __privateMethod(this, _Zlib_instances, run_fn).call(this, flush2, inBuf, inOff, inLen, outBuf, outOff, outLen);
17800
17800
  (_a3 = __privateGet(this, _callback)) == null ? void 0 : _a3.call(this);
17801
17801
  });
17802
- return this;
17803
17802
  }
17804
17803
  writeSync(flush2, inBuf, inOff, inLen, outBuf, outOff, outLen) {
17805
17804
  __privateMethod(this, _Zlib_instances, run_fn).call(this, flush2, inBuf, inOff, inLen, outBuf, outOff, outLen);
17806
- return this;
17807
17805
  }
17808
17806
  /**
17809
17807
  * `deflateParams` mid-stream. pako's deflate does not carry it, so the
@@ -17972,11 +17970,9 @@ class BrotliDecoderHandle {
17972
17970
  __privateMethod(this, _BrotliDecoderHandle_instances, run_fn2).call(this, flush2, inBuf, inOff, inLen, outBuf, outOff, outLen);
17973
17971
  (_a3 = __privateGet(this, _callback2)) == null ? void 0 : _a3.call(this);
17974
17972
  });
17975
- return this;
17976
17973
  }
17977
17974
  writeSync(flush2, inBuf, inOff, inLen, outBuf, outOff, outLen) {
17978
17975
  __privateMethod(this, _BrotliDecoderHandle_instances, run_fn2).call(this, flush2, inBuf, inOff, inLen, outBuf, outOff, outLen);
17979
- return this;
17980
17976
  }
17981
17977
  params() {
17982
17978
  }
@@ -21288,17 +21284,10 @@ var implementation$1 = function bind2(that) {
21288
21284
  var implementation = implementation$1;
21289
21285
  var functionBind = Function.prototype.bind || implementation;
21290
21286
  var functionCall = Function.prototype.call;
21291
- var functionApply;
21292
- var hasRequiredFunctionApply;
21293
- function requireFunctionApply() {
21294
- if (hasRequiredFunctionApply) return functionApply;
21295
- hasRequiredFunctionApply = 1;
21296
- functionApply = Function.prototype.apply;
21297
- return functionApply;
21298
- }
21287
+ var functionApply = Function.prototype.apply;
21299
21288
  var reflectApply = typeof Reflect !== "undefined" && Reflect && Reflect.apply;
21300
21289
  var bind$2 = functionBind;
21301
- var $apply$1 = requireFunctionApply();
21290
+ var $apply$1 = functionApply;
21302
21291
  var $call$2 = functionCall;
21303
21292
  var $reflectApply = reflectApply;
21304
21293
  var actualApply = $reflectApply || bind$2.call($call$2, $apply$1);
@@ -21418,7 +21407,7 @@ var hasSymbols = requireHasSymbols()();
21418
21407
  var getProto = requireGetProto();
21419
21408
  var $ObjectGPO = requireObject_getPrototypeOf();
21420
21409
  var $ReflectGPO = requireReflect_getPrototypeOf();
21421
- var $apply = requireFunctionApply();
21410
+ var $apply = functionApply;
21422
21411
  var $call = functionCall;
21423
21412
  var needsEval = {};
21424
21413
  var TypedArray = typeof Uint8Array === "undefined" || !getProto ? undefined$1 : getProto(Uint8Array);
@@ -22059,7 +22048,7 @@ function requireApplyBind() {
22059
22048
  if (hasRequiredApplyBind) return applyBind;
22060
22049
  hasRequiredApplyBind = 1;
22061
22050
  var bind3 = functionBind;
22062
- var $apply2 = requireFunctionApply();
22051
+ var $apply2 = functionApply;
22063
22052
  var actualApply$1 = actualApply;
22064
22053
  applyBind = function applyBind2() {
22065
22054
  return actualApply$1(bind3, $apply2, arguments);
@@ -40229,7 +40218,7 @@ class WorkerRuntime {
40229
40218
  this.worker = new Worker(
40230
40219
  new URL(
40231
40220
  /* @vite-ignore */
40232
- "/assets/runtime-worker-Dv_vTkXr.js",
40221
+ "/assets/runtime-worker-CvLlu5oq.js",
40233
40222
  import.meta.url
40234
40223
  ),
40235
40224
  { type: "module" }
@@ -11,8 +11,8 @@ export declare class Zlib {
11
11
  onerror: ((message: string, errno: number, code?: string) => void) | null;
12
12
  constructor(mode: number);
13
13
  init(windowBits: number, level: number, memLevel: number, strategy: number, writeState: Uint32Array, processCallback: ProcessCallback, dictionary?: Uint8Array): void;
14
- write(flush: number, inBuf: Uint8Array | null, inOff: number, inLen: number, outBuf: Uint8Array, outOff: number, outLen: number): this;
15
- writeSync(flush: number, inBuf: Uint8Array | null, inOff: number, inLen: number, outBuf: Uint8Array, outOff: number, outLen: number): this;
14
+ write(flush: number, inBuf: Uint8Array | null, inOff: number, inLen: number, outBuf: Uint8Array, outOff: number, outLen: number): void;
15
+ writeSync(flush: number, inBuf: Uint8Array | null, inOff: number, inLen: number, outBuf: Uint8Array, outOff: number, outLen: number): void;
16
16
  /**
17
17
  * `deflateParams` mid-stream. pako's deflate does not carry it, so the
18
18
  * level and strategy are taken by starting the stream again -- which is
@@ -31,8 +31,8 @@ declare class BrotliDecoderHandle {
31
31
  onerror: ((message: string, errno: number, code?: string) => void) | null;
32
32
  constructor(mode: number);
33
33
  init(_params: Uint32Array, writeState: Uint32Array, processCallback: ProcessCallback): boolean;
34
- write(flush: number, inBuf: Uint8Array | null, inOff: number, inLen: number, outBuf: Uint8Array, outOff: number, outLen: number): this;
35
- writeSync(flush: number, inBuf: Uint8Array | null, inOff: number, inLen: number, outBuf: Uint8Array, outOff: number, outLen: number): this;
34
+ write(flush: number, inBuf: Uint8Array | null, inOff: number, inLen: number, outBuf: Uint8Array, outOff: number, outLen: number): void;
35
+ writeSync(flush: number, inBuf: Uint8Array | null, inOff: number, inLen: number, outBuf: Uint8Array, outOff: number, outLen: number): void;
36
36
  params(): void;
37
37
  reset(): void;
38
38
  close(): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volter/tabnode",
3
- "version": "0.5.29",
3
+ "version": "0.5.31",
4
4
  "description": "Node's own library, in a browser tab.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -163,7 +163,7 @@ export class Zlib {
163
163
  write(
164
164
  flush: number, inBuf: Uint8Array | null, inOff: number, inLen: number,
165
165
  outBuf: Uint8Array, outOff: number, outLen: number,
166
- ): this {
166
+ ): void {
167
167
  // Node's own binding does this work on the thread pool and calls back;
168
168
  // a tab has one thread, so the work is done here and the callback is the
169
169
  // next tick, which is the difference a program can observe.
@@ -173,15 +173,15 @@ export class Zlib {
173
173
  this.#run(flush, inBuf, inOff, inLen, outBuf, outOff, outLen);
174
174
  this.#callback?.();
175
175
  });
176
- return this;
177
176
  }
178
177
 
179
178
  writeSync(
180
179
  flush: number, inBuf: Uint8Array | null, inOff: number, inLen: number,
181
180
  outBuf: Uint8Array, outOff: number, outLen: number,
182
- ): this {
181
+ ): void {
182
+ // Node's binding returns nothing: the answer is the shared pair, and
183
+ // pngjs's `res || this._writeState` reads a returned value in its place.
183
184
  this.#run(flush, inBuf, inOff, inLen, outBuf, outOff, outLen);
184
- return this;
185
185
  }
186
186
 
187
187
  /**
@@ -292,19 +292,17 @@ class BrotliDecoderHandle {
292
292
  }
293
293
  }
294
294
 
295
- write(flush: number, inBuf: Uint8Array | null, inOff: number, inLen: number, outBuf: Uint8Array, outOff: number, outLen: number): this {
295
+ write(flush: number, inBuf: Uint8Array | null, inOff: number, inLen: number, outBuf: Uint8Array, outOff: number, outLen: number): void {
296
296
  const tick = (globalThis as { process?: { nextTick?: (fn: () => void) => void } }).process?.nextTick
297
297
  ?? ((fn: () => void) => { queueMicrotask(fn); });
298
298
  tick(() => {
299
299
  this.#run(flush, inBuf, inOff, inLen, outBuf, outOff, outLen);
300
300
  this.#callback?.();
301
301
  });
302
- return this;
303
302
  }
304
303
 
305
- writeSync(flush: number, inBuf: Uint8Array | null, inOff: number, inLen: number, outBuf: Uint8Array, outOff: number, outLen: number): this {
304
+ writeSync(flush: number, inBuf: Uint8Array | null, inOff: number, inLen: number, outBuf: Uint8Array, outOff: number, outLen: number): void {
306
305
  this.#run(flush, inBuf, inOff, inLen, outBuf, outOff, outLen);
307
- return this;
308
306
  }
309
307
 
310
308
  params(): void {}