Cache zeroperl.wasm in the ExifTool worker after first fetch.
Avoid repeat multi-megabyte downloads when Tor/Firefox do not HTTP-cache worker WASM responses.
This commit is contained in:
@@ -6,8 +6,31 @@ import type { ExiftoolWorkerJob } from '@/types/workers/exiftoolWorkerJob';
|
||||
import { resolveErrorMessage } from '@/utils/resolveErrorMessage';
|
||||
import { toArrayBuffer } from '@/utils/toArrayBuffer';
|
||||
|
||||
let cachedZeroperlWasm: ArrayBuffer | null = null;
|
||||
|
||||
/**
|
||||
* Load `zeroperl.wasm` once per worker lifetime. Tor/Firefox often skip HTTP cache for
|
||||
* worker fetches and may not store responses above the memory-cache entry size limit.
|
||||
*/
|
||||
const fetchZeroperlWasm = async (): Promise<Response> => {
|
||||
if (!cachedZeroperlWasm) {
|
||||
const response = await fetch(zeroperlWasmUrl);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to load zeroperl.wasm: HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
cachedZeroperlWasm = await response.arrayBuffer();
|
||||
}
|
||||
|
||||
return new Response(cachedZeroperlWasm, {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/wasm' }
|
||||
});
|
||||
};
|
||||
|
||||
/** Custom fetch function for `@uswriting/exiftool` to load `zeroperl.wasm` from the worker. */
|
||||
const exiftoolParseOptions = { fetch: () => fetch(zeroperlWasmUrl) };
|
||||
const exiftoolParseOptions = { fetch: fetchZeroperlWasm };
|
||||
|
||||
const jobQueue: ExiftoolWorkerJob[] = [];
|
||||
let drainPromise: Promise<void> | null = null;
|
||||
@@ -137,11 +160,7 @@ const processJob = async (job: ExiftoolWorkerJob): Promise<void> => {
|
||||
try {
|
||||
await initializeWorker();
|
||||
|
||||
const result = await writeMetadata(
|
||||
job.file,
|
||||
{},
|
||||
{ args: ['-all='], ...exiftoolParseOptions }
|
||||
);
|
||||
const result = await writeMetadata(job.file, {}, { args: ['-all='], ...exiftoolParseOptions });
|
||||
|
||||
if (!result.success) {
|
||||
const message = result.error ?? 'Could not strip image metadata';
|
||||
|
||||
Reference in New Issue
Block a user