Untitled conversation
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
/* ──────────────────────────────────────────────────────────────
INLINE WORKER – “file:// edition”
(gzip → text) ES-MODULE – no DOM dependencies
----------------------------------------------------------------
Added / kept
• global URL REGISTRYistry (all inline-generated blob: URLs)
• single beforeunload gc/release for every stub
• lazy-promise helpers (blob / url)
• explicit dispose()
────────────────────────────────────────────────────────────────*/
const filePath = '$__WORKER_PATH__'; // ↖ used only if you want
// fetch-redirect support
let GZ_B64_SRC = `$__WORKER_B64_GZ__`; // ← injected at build-time
/*─────────────────────────────────────────────────────────────────────
❶ Global REGISTRYistry & single beforeunload hook
─────────────────────────────────────────────────────────────────────*/
const REGISTRY = globalThis[
Symbol.for('__INLINE_WORKER_URL_REGISTRYISTRY__')
] ||= new Map(); // filePath? → blobURL
const BOOT = Symbol.for('__INLINE_WORKER_BOOTSTRAPPED__');
if (!globalThis[BOOT]) {
globalThis[BOOT] = true;
/* optional fetch-redirect (skip if not needed) */
const origFetch = globalThis.fetch?.bind(globalThis);
if (origFetch) {
globalThis.fetch = (req, init) => {
const url =
typeof req === 'string' ? req
: req instanceof Request ? req.url
: req instanceof URL ? req.href
: '';
for (const [p, blobURL] of REGISTRY)
if (url.endsWith(p)) return origFetch(blobURL, init);
return origFetch(req, init);
};
}
/* global release */
addEventListener('beforeunload', () => {
for (const u of REGISTRY.values()) URL.revokeObjectURL(u);
REGISTRY.clear();
});
}
/*─────────────────────────────────────────────────────────────────────
❷ Payload guards / helpers
─────────────────────────────────────────────────────────────────────*/
GZ_B64_SRC = GZ_B64_SRC?.trim();
if (!GZ_B64_SRC || GZ_B64_SRC[0] === '$') {
throw new Error('Inline worker payload missing');
}
const b64ToU8 = b64 => {
const bin = atob(b64);
return Uint8Array.from({ length: bin.length }, (_, i) => bin.charCodeAt(i));
};
const asStream = u8 => new ReadableStream({
start(c) { c.enqueue(u8); c.close(); }
});
/*─────────────────────────────────────────────────────────────────────
❸ Main promise – “workerReady”
─────────────────────────────────────────────────────────────────────*/
export const workerReady = (async () => {
const compressed = b64ToU8(GZ_B64_SRC);
const stream = 'DecompressionStream' in globalThis
? asStream(compressed).pipeThrough(new DecompressionStream('gzip'))
: asStream(
(await import(
'https://cdn.jsdelivr.net/npm/fflate@0.8.0/esm/browser.js'
)).gunzipSync(compressed)
);
const workerCode = await new Response(stream).text();
/* GC-hints */
GZ_B64_SRC = undefined;
compressed.fill(0);
/* ── lazy memoised helpers ─────────────────────────── */
let blobP, urlP;
const workerBlobPromise = () =>
blobP ||= Promise.resolve(
new Blob([workerCode], { type: 'application/javascript' })
);
const workerURLPromise = () =>
urlP ||= (async () => {
const url = URL.createObjectURL(await workerBlobPromise());
if (filePath) REGISTRY.set(filePath, url);
return url;
})();
/* quick one-shot helper: spawn() */
const spawn = async (opts = {}) =>
new Worker(await workerURLPromise(), opts);
const disposeURL = () => {
const url = REGISTRY.get(filePath);
if (url) {
URL.revokeObjectURL(url);
REGISTRY.delete(filePath);
}
}
return { workerCode, workerBlobPromise, workerURLPromise, spawn, disposeURL };
})();
export default workerReady;
the two stub dup many code. help refactor the common things out into another importable module usable by both. goal is to DRY.
To achieve a DRY (Don't Repeat Yourself) approach for the two inline modules (worker_inline.stub.mjs and wasm_inline.stub.mjs), we can extract the common logic into a shared module (inlineHelpers.mjs). This includes:
inlineHelpers.mjs// inlineHelpers.mjs
export function b64ToU8(b64) {
const bin = atob(b64);
return Uint8Array.from({ length: bin.length }, (_, i) => bin.charCodeAt(i));
}
export function asStream(u8) {
return new ReadableStream({
start(c) { c.enqueue(u8); c.close(); }
});
}
export function validatePayload(src, errorMsg) {
src = src?.trim();
if (!src || src[0] === '$') {
throw new Error(errorMsg);
}
return src;
}
export function setupFetchRedirect(REGISTRY) {
const origFetch = globalThis.fetch?.bind(globalThis);
if (origFetch) {
globalThis.fetch = (req, init) => {
const url =
typeof req === 'string' ? req
: req instanceof Request ? req.url
: req instanceof URL ? req.href
: '';
for (const [p, blobURL] of REGISTRY.entries())
if (url.endsWith(p)) return origFetch(blobURL, init);
return origFetch(req, init);
};
}
}
export function setupBeforeUnloadCleanup(REGISTRY) {
addEventListener('beforeunload', () => {
for (const u of REGISTRY.values()) URL.revokeObjectURL(u);
REGISTRY.clear();
});
}
export async function decompressStream(compressed) {
const stream = 'DecompressionStream' in globalThis
? asStream(compressed).pipeThrough(new DecompressionStream('gzip'))
: asStream(
(await import(
'https://cdn.jsdelivr.net/npm/fflate@0.8.0/esm/browser.js'
)).gunzipSync(compressed)
);
return stream;
}
/* ──────────────────────────────────────────────────────────────
INLINE WASM – “file:// edition”
(gzip → bytes) ES-MODULE – no DOM dependencies
----------------------------------------------------------------
Added / kept
• global fetch-redirect registry
• compile-streaming + fallback
• isWasmReady flag, sync/async helpers, releaseWasm()
────────────────────────────────────────────────────────────────*/
const filePath = '$__WASM_PATH__'; // injected at build-time
let GZ_B64_SRC = `$__WASM_B64_GZ__`; // ← injected at build-time
// global registry shared by all inline stubs
const REGISTRY = globalThis[
Symbol.for('__INLINE_WASM_FETCH_REGISTRY__')
] ||= new Map(); // filePath → blob:
// ONE flag → has bootstrap run?
const BOOT = Symbol.for('__INLINE_WASM_BOOTSTRAPPED__');
if (!globalThis[BOOT]) {
globalThis[BOOT] = true;
/* fetch redirect --------------------------------------------------- */
const origFetch = globalThis.fetch.bind(globalThis);
globalThis.fetch = (req, init) => {
const url =
typeof req === 'string' ? req
: req instanceof Request ? req.url
: req instanceof URL ? req.href
: '';
for (const [path, blobURL] of REGISTRY)
if (url.endsWith(path)) return origFetch(blobURL, init);
return origFetch(req, init);
};
/* beforeunload cleanup --------------------------------------------- */
addEventListener('beforeunload', () => {
for (const u of REGISTRY.values()) URL.revokeObjectURL(u);
REGISTRY.clear();
});
}
/*─────────────────────────────────────────────────────────────────────
gzip-base64 payload
─────────────────────────────────────────────────────────────────────*/
GZ_B64_SRC = GZ_B64_SRC?.trim();
if (!GZ_B64_SRC || GZ_B64_SRC[0] === '$') {
throw new Error('Inline WASM payload missing');
}
/* helpers */
const b64ToU8 = b64 => {
const bin = atob(b64);
return Uint8Array.from({ length: bin.length }, (_, i) => bin.charCodeAt(i));
};
const asStream = u8 => new ReadableStream({
start(c) { c.enqueue(u8); c.close(); }
});
/* main promise */
export const wasmReady = (async () => {
const compressed = b64ToU8(GZ_B64_SRC);
const stream = 'DecompressionStream' in globalThis
? asStream(compressed).pipeThrough(new DecompressionStream('gzip'))
: asStream(
(await import(
'https://cdn.jsdelivr.net/npm/fflate@0.8.0/esm/browser.js'
)).gunzipSync(compressed)
);
const wasmBuffer = await new Response(stream).arrayBuffer();
// GC-hints
GZ_B64_SRC = undefined;
compressed.fill(0);
/* lazy helpers (memoised) */
let blobP, urlP, modP;
const wasmBlobPromise = () =>
blobP ||= Promise.resolve(new Blob([wasmBuffer], { type: 'application/wasm' }));
const wasmURLPromise = () =>
urlP ||= (async () => {
const url = URL.createObjectURL(await wasmBlobPromise());
if (filePath) REGISTRY.set(filePath, url);
return url;
})();
const modulePromise = () => modP ||= WebAssembly.compile(wasmBuffer);
const instantiate = async (imports = {}) => {
const module = await modulePromise();
return { module, instance: new WebAssembly.Instance(module, imports) };
};
const disposeURL = () => {
const url = REGISTRY.get(filePath);
if (url) {
URL.revokeObjectURL(url);
REGISTRY.delete(filePath);
}
}
return { wasmBuffer, wasmBlobPromise, wasmURLPromise, modulePromise, instantiate, disposeURL };
})();
export default wasmReady;
worker_inline.stub.mjs// worker_inline.stub.mjs
import {
b64ToU8,
asStream,
validatePayload,
setupFetchRedirect,
setupBeforeUnloadCleanup,
decompressStream
} from './inlineHelpers.mjs';
const filePath = '$__WORKER_PATH__';
let GZ_B64_SRC = `$__WORKER_B64_GZ__`;
const REGISTRY = globalThis[
Symbol.for('__INLINE_WORKER_URL_REGISTRYISTRY__')
] ||= new Map();
const BOOT = Symbol.for('__INLINE_WORKER_BOOTSTRAPPED__');
if (!globalThis[BOOT]) {
globalThis[BOOT] = true;
setupFetchRedirect(REGISTRY);
setupBeforeUnloadCleanup(REGISTRY);
}
GZ_B64_SRC = validatePayload(GZ_B64_SRC, 'Inline worker payload missing');
export const workerReady = (async () => {
const compressed = b64ToU8(GZ_B64_SRC);
const stream = await decompressStream(compressed);
const workerCode = await new Response(stream).text();
GZ_B64_SRC = undefined;
compressed.fill(0);
let blobP, urlP;
const workerBlobPromise = () =>
blobP ||= Promise.resolve(
new Blob([workerCode], { type: 'application/javascript' })
);
const workerURLPromise = () =>
urlP ||= (async () => {
const url = URL.createObjectURL(await workerBlobPromise());
if (filePath) REGISTRY.set(filePath, url);
return url;
})();
const spawn = async (opts = {}) =>
new Worker(await workerURLPromise(), opts);
const disposeURL = () => {
const url = REGISTRY.get(filePath);
if (url) {
URL.revokeObjectURL(url);
REGISTRY.delete(filePath);
}
};
return { workerCode, workerBlobPromise, workerURLPromise, spawn, disposeURL };
})();
export default workerReady;
wasm_inline.stub.mjs// wasm_inline.stub.mjs
import {
b64ToU8,
asStream,
validatePayload,
setupFetchRedirect,
setupBeforeUnloadCleanup,
decompressStream
} from './inlineHelpers.mjs';
const filePath = '$__WASM_PATH__';
let GZ_B64_SRC = `$__WASM_B64_GZ__`;
const REGISTRY = globalThis[
Symbol.for('__INLINE_WASM_FETCH_REGISTRY__')
] ||= new Map();
const BOOT = Symbol.for('__INLINE_WASM_BOOTSTRAPPED__');
if (!globalThis[BOOT]) {
globalThis[BOOT] = true;
setupFetchRedirect(REGISTRY);
setupBeforeUnloadCleanup(REGISTRY);
}
GZ_B64_SRC = validatePayload(GZ_B64_SRC, 'Inline WASM payload missing');
export const wasmReady = (async () => {
const compressed = b64ToU8(GZ_B64_SRC);
const stream = await decompressStream(compressed);
const wasmBuffer = await new Response(stream).arrayBuffer();
GZ_B64_SRC = undefined;
compressed.fill(0);
let blobP, urlP, modP;
const wasmBlobPromise = () =>
blobP ||= Promise.resolve(new Blob([wasmBuffer], { type: 'application/wasm' }));
const wasmURLPromise = () =>
urlP ||= (async () => {
const url = URL.createObjectURL(await wasmBlobPromise());
if (filePath) REGISTRY.set(filePath, url);
return url;
})();
const modulePromise = () => modP ||= WebAssembly.compile(wasmBuffer);
const instantiate = async (imports = {}) => {
const module = await modulePromise();
return { module, instance: new WebAssembly.Instance(module, imports) };
};
const disposeURL = () => {
const url = REGISTRY.get(filePath);
if (url) {
URL.revokeObjectURL(url);
REGISTRY.delete(filePath);
}
};
return { wasmBuffer, wasmBlobPromise, wasmURLPromise, modulePromise, instantiate, disposeURL };
})();
export default wasmReady;
inlineHelpers.mjs.text, json, etc.).This structure ensures that the shared logic is maintained in one place, while the specific implementation details remain in their respective modules.