sane-wasm 0.5.0 → 0.6.1

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/lib/loader.js DELETED
@@ -1,80 +0,0 @@
1
- const { version } = require('../package.json');
2
-
3
- // we cannot use unpkg as a CDN because they don't set CORP
4
- // Cross-Origin-Resource-Policy: cross-origin
5
- // const cdnURL = `https://unpkg.com/sane-wasm@${version}/build`;
6
-
7
- // use good old jsdelivr
8
- const cdnURL = `https://cdn.jsdelivr.net/npm/sane-wasm@${version}/build`;
9
-
10
- let libPromise = null;
11
-
12
- function loadScript(src) {
13
- return new Promise((resolve, reject) => {
14
- document.head.append(Object.assign(
15
- document.createElement('script'),
16
- { src, onload: resolve, onerror: reject }
17
- ));
18
- });
19
- }
20
-
21
- function fetchFileToBlobURL(src) {
22
- return fetch(src).then(res => res.ok ? res.blob() : new Blob()).then(blob => URL.createObjectURL(blob));
23
- }
24
-
25
- async function prepareLib(options) {
26
- const jsURL = `${options.sane.loaderURL}/libsane.js`;
27
- const jsWorkerURL = `${options.sane.loaderURL}/libsane.worker.js`;
28
- const jsWasmURL = `${options.sane.loaderURL}/libsane.wasm`;
29
-
30
- const filesToPrefetch = options.sane.loaderPrefetchToBlob ? [
31
- // because of security restrictions around the Worker constructor
32
- // we need to prefetch the worker js and serve it from a blob
33
- // see: https://github.com/emscripten-core/emscripten/issues/8338
34
- jsWorkerURL,
35
- // since we are already doing all this work to fetch the files in
36
- // parallel, might as well prefetch the wasm file
37
- jsWasmURL,
38
- ] : [];
39
-
40
- const [lib, prefetchResult] = await Promise.all([
41
- loadScript(jsURL).then(() => {
42
- const lib = window.LibSANE;
43
- if (options.sane.loaderRemoveGlobal) {
44
- window.LibSANE = undefined; // nuke global variable
45
- }
46
- return lib;
47
- }),
48
- Promise.all(filesToPrefetch.map(url => fetchFileToBlobURL(url).then(urlBlob => ({ url, urlBlob })))),
49
- ]);
50
-
51
- const preFetchedFiles = prefetchResult.reduce((obj, { url, urlBlob }) => {
52
- obj[url] = urlBlob;
53
- return obj;
54
- }, {});
55
-
56
- return { lib, preFetchedFiles };
57
- }
58
-
59
- module.exports = async (options) => {
60
- options = options || {};
61
- options.sane = {
62
- loaderURL: cdnURL,
63
- loaderPrefetchToBlob: true,
64
- loaderRemoveGlobal: true,
65
- ...(options.sane || {}),
66
- };
67
-
68
- if (!libPromise) {
69
- libPromise = prepareLib(options);
70
- }
71
-
72
- const { lib, preFetchedFiles } = await libPromise;
73
- return lib({
74
- ...(options || {}),
75
- locateFile: (path, prefix) => {
76
- const fullUrl = `${prefix}${path}`;
77
- return preFetchedFiles[fullUrl] ? preFetchedFiles[fullUrl] : (options.locateFile ? options.locateFile(path, prefix) : fullUrl);
78
- },
79
- });
80
- };