sane-wasm 0.1.0 → 0.1.2
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/build/libsane.js +50 -13
- package/build/libsane.wasm +0 -0
- package/lib/index.js +7 -0
- package/lib/loader.js +80 -0
- package/package.json +17 -3
package/build/libsane.js
CHANGED
|
@@ -978,7 +978,7 @@ function dbg(text) {
|
|
|
978
978
|
}
|
|
979
979
|
|
|
980
980
|
var ASM_CONSTS = {
|
|
981
|
-
|
|
981
|
+
1061804: ($0, $1, $2) => {
|
|
982
982
|
return stringToUTF16(Emval.toValue($0), $1, $2);
|
|
983
983
|
}
|
|
984
984
|
};
|
|
@@ -987,8 +987,18 @@ function module_set(k, v) {
|
|
|
987
987
|
Module[UTF8ToString(k)] = Emval.toValue(v);
|
|
988
988
|
}
|
|
989
989
|
|
|
990
|
+
function em_libusb_event_target() {
|
|
991
|
+
if (ENVIRONMENT_IS_NODE) {
|
|
992
|
+
if (!Module.libusbEventTarget) {
|
|
993
|
+
Module.libusbEventTarget = new EventTarget();
|
|
994
|
+
}
|
|
995
|
+
return Module.libusbEventTarget;
|
|
996
|
+
}
|
|
997
|
+
return globalThis;
|
|
998
|
+
}
|
|
999
|
+
|
|
990
1000
|
function em_libusb_notify() {
|
|
991
|
-
dispatchEvent(new Event("em-libusb"));
|
|
1001
|
+
em_libusb_event_target().dispatchEvent(new Event("em-libusb"));
|
|
992
1002
|
Object.values(PThread.pthreads).forEach(w => {
|
|
993
1003
|
w.postMessage({
|
|
994
1004
|
target: "setimmediate",
|
|
@@ -1000,24 +1010,40 @@ function em_libusb_notify() {
|
|
|
1000
1010
|
function __asyncjs__em_libusb_wait(timeout) {
|
|
1001
1011
|
return Asyncify.handleAsync(async () => {
|
|
1002
1012
|
let onEvent, onEventWorker, timeoutId, intervalId;
|
|
1013
|
+
let parentPort = ENVIRONMENT_IS_WORKER && ENVIRONMENT_IS_NODE && require("node:worker_threads").parentPort;
|
|
1003
1014
|
try {
|
|
1004
1015
|
intervalId = setInterval(_em_force_yield_main_thread, 50);
|
|
1005
1016
|
return await new Promise(resolve => {
|
|
1006
1017
|
onEvent = () => resolve(0);
|
|
1007
|
-
addEventListener("em-libusb", onEvent);
|
|
1008
|
-
if (
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1018
|
+
em_libusb_event_target().addEventListener("em-libusb", onEvent);
|
|
1019
|
+
if (ENVIRONMENT_IS_WORKER) {
|
|
1020
|
+
if (ENVIRONMENT_IS_NODE) {
|
|
1021
|
+
onEventWorker = data => {
|
|
1022
|
+
if (data.cmd == "em-libusb") {
|
|
1023
|
+
resolve(0);
|
|
1024
|
+
}
|
|
1025
|
+
};
|
|
1026
|
+
parentPort.on("message", onEventWorker);
|
|
1027
|
+
} else {
|
|
1028
|
+
onEventWorker = e => {
|
|
1029
|
+
if (e.data.cmd == "em-libusb") {
|
|
1030
|
+
resolve(0);
|
|
1031
|
+
}
|
|
1032
|
+
};
|
|
1033
|
+
addEventListener("message", onEventWorker);
|
|
1034
|
+
}
|
|
1015
1035
|
}
|
|
1016
1036
|
timeoutId = setTimeout(resolve, timeout, -1);
|
|
1017
1037
|
});
|
|
1018
1038
|
} finally {
|
|
1019
|
-
removeEventListener("em-libusb", onEvent);
|
|
1020
|
-
|
|
1039
|
+
em_libusb_event_target().removeEventListener("em-libusb", onEvent);
|
|
1040
|
+
if (ENVIRONMENT_IS_WORKER) {
|
|
1041
|
+
if (ENVIRONMENT_IS_NODE) {
|
|
1042
|
+
parentPort.off("message", onEventWorker);
|
|
1043
|
+
} else {
|
|
1044
|
+
addEventListener("message", onEventWorker);
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1021
1047
|
clearInterval(intervalId);
|
|
1022
1048
|
clearTimeout(timeoutId);
|
|
1023
1049
|
}
|
|
@@ -8422,7 +8448,7 @@ var ___emscripten_embedded_file_data = Module["___emscripten_embedded_file_data"
|
|
|
8422
8448
|
|
|
8423
8449
|
var ___start_em_js = Module["___start_em_js"] = 1058876;
|
|
8424
8450
|
|
|
8425
|
-
var ___stop_em_js = Module["___stop_em_js"] =
|
|
8451
|
+
var ___stop_em_js = Module["___stop_em_js"] = 1061804;
|
|
8426
8452
|
|
|
8427
8453
|
Module["addRunDependency"] = addRunDependency;
|
|
8428
8454
|
|
|
@@ -8567,6 +8593,17 @@ if (Module["noInitialRun"]) shouldRunNow = false;
|
|
|
8567
8593
|
|
|
8568
8594
|
run();
|
|
8569
8595
|
|
|
8596
|
+
{
|
|
8597
|
+
if (ENVIRONMENT_IS_NODE) {
|
|
8598
|
+
if (!globalThis.navigator) {
|
|
8599
|
+
globalThis.navigator = {};
|
|
8600
|
+
}
|
|
8601
|
+
if (!globalThis.navigator.usb) {
|
|
8602
|
+
globalThis.navigator.usb = require("usb").webusb;
|
|
8603
|
+
}
|
|
8604
|
+
}
|
|
8605
|
+
}
|
|
8606
|
+
|
|
8570
8607
|
|
|
8571
8608
|
return LibSANE.ready
|
|
8572
8609
|
}
|
package/build/libsane.wasm
CHANGED
|
Binary file
|
package/lib/index.js
ADDED
package/lib/loader.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
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
|
+
};
|
package/package.json
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sane-wasm",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "A project to bring the SANE API to the web.",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "lib/index.js",
|
|
6
6
|
"types": "libsane.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
|
+
"test": "jest",
|
|
8
9
|
"clean": "./build.sh --with-docker --no-build --clean",
|
|
9
10
|
"build": "./build.sh --with-docker --clean",
|
|
10
11
|
"build-debug": "./build.sh --with-docker --debug --emrun"
|
|
11
12
|
},
|
|
12
13
|
"files": [
|
|
14
|
+
"lib/",
|
|
13
15
|
"build/",
|
|
14
16
|
"libsane.d.ts",
|
|
15
17
|
"LICENSE-LGPL.txt",
|
|
@@ -24,5 +26,17 @@
|
|
|
24
26
|
"bugs": {
|
|
25
27
|
"url": "https://github.com/goncalomb/sane-wasm/issues"
|
|
26
28
|
},
|
|
27
|
-
"homepage": "https://github.com/goncalomb/sane-wasm#readme"
|
|
29
|
+
"homepage": "https://github.com/goncalomb/sane-wasm#readme",
|
|
30
|
+
"jest": {
|
|
31
|
+
"setupFilesAfterEnv": [
|
|
32
|
+
"jest-extended/all"
|
|
33
|
+
]
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"usb": "^2.10.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"jest": "^29.6.4",
|
|
40
|
+
"jest-extended": "^4.0.1"
|
|
41
|
+
}
|
|
28
42
|
}
|