@miaskiewicz/turbo-dom 0.1.45 → 0.1.47

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@miaskiewicz/turbo-dom",
3
- "version": "0.1.45",
3
+ "version": "0.1.47",
4
4
  "description": "Faster, more spec-correct DOM for test runners — native html5ever (Rust/WASM) parser + lazy copy-on-write DOM. A drop-in-style alternative to jsdom/happy-dom for vitest & jest.",
5
5
  "license": "MIT",
6
6
  "main": "index.js",
@@ -5,7 +5,9 @@ const NS_SHORT = ['', 'svg', 'math'];
5
5
 
6
6
  // Inflate the packed byte blob into named typed-array views (zero-copy).
7
7
  // Mirrors the column order in lib.rs JsSoa::from. One ArrayBuffer, ~13 views.
8
- function unpack(soa) {
8
+ // Exported so the parse cache can unpack ONCE per HTML (the views are read-only
9
+ // over the shared immutable buffer) instead of re-unpacking per Document.
10
+ export function unpack(soa) {
9
11
  const u8 = soa.packed, ab = u8.buffer, base = u8.byteOffset, n = soa.n, m = soa.m;
10
12
  let off = base;
11
13
  const i32 = () => { const a = new Int32Array(ab, off, n); off += n * 4; return a; };
@@ -9,6 +9,7 @@
9
9
  import { createRequire } from 'node:module';
10
10
  import { Document } from './dom.mjs';
11
11
  import { createWindow } from './window.mjs';
12
+ import { unpack } from './buffer.mjs';
12
13
 
13
14
  const require = createRequire(import.meta.url);
14
15
  const native = require('../../index.js');
@@ -33,7 +34,11 @@ function parseBufferCached(html) {
33
34
  if (html !== __parseCacheMRU) { __parseCache.delete(html); __parseCache.set(html, hit); __parseCacheMRU = html; }
34
35
  return hit;
35
36
  }
36
- const soa = native.parseBuffer(html);
37
+ let soa = native.parseBuffer(html);
38
+ // Unpack the packed blob into typed-array views ONCE here, not per Document in
39
+ // every Buffer ctor — the views are read-only over the shared immutable buffer,
40
+ // so all Documents backed by this cached soa reuse them (no re-unpack per file).
41
+ if (soa.packed) soa = unpack(soa);
37
42
  // Evict the single oldest entry, not the whole cache — a suite with >64 distinct
38
43
  // shells should keep its hot fixtures warm, not thrash every one cold on overflow.
39
44
  if (__parseCache.size >= __PARSE_CACHE_MAX) __parseCache.delete(__parseCache.keys().next().value);
@@ -133,7 +133,8 @@ export function createWindow(document, { url = 'http://localhost/' } = {}) {
133
133
  document,
134
134
  name: '',
135
135
  closed: false,
136
- origin: new URL(url).origin,
136
+ // origin is lazy (per-env, captures url) — window.origin is rarely read, so the
137
+ // eager `new URL(url)` parse per createWindow was usually wasted.
137
138
  // customElements is lazy (SHARED_LAZY) — env-independent + rarely used; building
138
139
  // its two Maps eagerly per createWindow was wasted for the typical test.
139
140
  Image: function Image(w, h) { const img = document.createElement('img'); if (w != null) img.setAttribute('width', w); if (h != null) img.setAttribute('height', h); return img; },
@@ -148,6 +149,7 @@ export function createWindow(document, { url = 'http://localhost/' } = {}) {
148
149
  // Everything env-independent lives in the module-level SHARED_LAZY (built once,
149
150
  // not re-allocated per test file) — see below. None constructed until touched.
150
151
  const lazy = {
152
+ origin: () => new URL(url).origin,
151
153
  // subsystem grouping: history co-materializes with (and shares) location
152
154
  location: () => makeLocation(url),
153
155
  history: () => makeHistory(windowProxy.location),