Patch zeroperl-ts so worker WASM load honors custom fetch.

Postinstall updates @6over3/zeroperl-ts and wires the worker to load zeroperl.wasm via Vite ?url.
This commit is contained in:
2026-10-15 18:57:43 +02:00
parent 42f9a3eafc
commit e10341d44b
4 changed files with 84 additions and 4 deletions
@@ -0,0 +1,60 @@
/**
* Patch @6over3/zeroperl-ts@1.0.10 after install.
*
* Problem: WASM loader treats only `window` + `document` as browser. Module workers have
* neither, so it dynamic-imports `node:fs/promises` and fails under Vite.
* Custom `fetch` from @uswriting/exiftool is ignored on that branch.
*
* Fix: use the fetch path when a custom fetch is provided (`Ee() || e`).
*
* Upstream: https://github.com/6over3/zeroperl-ts
*
* Re-verify when bumping @6over3/zeroperl-ts: search dist for the ORIGINAL snippets below.
*/
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const patches = [
{
relativePath: 'esm/index.js',
original: 'if(Ee())a=await(await(e??fetch)(q)).arrayBuffer();',
patched: 'if(Ee()||e)a=await(await(e??fetch)(q)).arrayBuffer();'
},
{
relativePath: 'cjs/index.cjs',
original: 'if(pe())a=await(await(e??fetch)(w)).arrayBuffer();',
patched: 'if(pe()||e)a=await(await(e??fetch)(w)).arrayBuffer();'
}
];
const packageRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../node_modules/@6over3/zeroperl-ts/dist'
);
const patchFile = ({ relativePath, original, patched }) => {
const filePath = path.join(packageRoot, relativePath);
if (!fs.existsSync(filePath)) {
throw new Error(`patch-zeroperl-worker-fetch: missing ${filePath} (is @6over3/zeroperl-ts installed?)`);
}
const content = fs.readFileSync(filePath, 'utf8');
if (content.includes(patched)) {
return;
}
if (!content.includes(original)) {
throw new Error(
`patch-zeroperl-worker-fetch: ${relativePath} does not match @6over3/zeroperl-ts@1.0.10; update or remove this script`
);
}
fs.writeFileSync(filePath, content.replaceAll(original, patched));
};
for (const entry of patches) {
patchFile(entry);
}