This commit is contained in:
iqudoo
2026-06-17 20:20:27 +08:00
parent 23b97548cf
commit bee953617d
2 changed files with 47 additions and 9 deletions

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -565,6 +565,51 @@ function getZXingReaderWasmUrl() {
return "./" + ZXING_READER_WASM_PATH;
}
let cachedWasmBytes = null;
let cachedWasmBytesUrl = "";
function fetchWasmArrayBuffer(wasmUrl) {
if (cachedWasmBytes && cachedWasmBytesUrl === wasmUrl) {
return Promise.resolve(cachedWasmBytes);
}
return fetch(wasmUrl, { credentials: "same-origin" }).then((response) => {
if (!response || typeof response.arrayBuffer !== "function") {
throw new Error("wasm fetch response is invalid");
}
if (!response.ok) {
throw new Error(`wasm fetch failed: ${response.status}`);
}
return response.arrayBuffer();
}).then((bytes) => {
cachedWasmBytes = bytes;
cachedWasmBytesUrl = wasmUrl;
return bytes;
});
}
function createZxingWasmOverrides() {
const wasmUrl = getZXingReaderWasmUrl();
return {
locateFile(path) {
if (path && path.indexOf(".wasm") !== -1) {
return wasmUrl;
}
return path;
},
instantiateWasm(imports, receiveInstance) {
fetchWasmArrayBuffer(wasmUrl).then((bytes) => {
return WebAssembly.instantiate(bytes, imports);
}).then((result) => {
receiveInstance(result.instance, result.module);
}).catch((err) => {
printWarn("wasm instantiate failed:", err, "url:", wasmUrl);
throw err;
});
return {};
},
};
}
function prepareBarcodeDetectorWithTimeout(options) {
return promiseWithTimeout(
prepareBarcodeDetector(options),
@@ -582,14 +627,7 @@ function prepareBarcodeDetector(options) {
if (!barcodeDetectorPreparePromise || options.forcePonyfill) {
const preparePromise = prepareZXingModule({
fireImmediately: true,
overrides: {
locateFile: path => {
if (path && path.indexOf(".wasm") !== -1) {
return getZXingReaderWasmUrl();
}
return path;
}
}
overrides: createZxingWasmOverrides(),
}).then(() => BarcodeDetectorPonyfill).catch(err => {
if (!options.forcePonyfill) {
barcodeDetectorPreparePromise = null;