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/dist/readers.js CHANGED
@@ -1,36 +1,20 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.ScanImageReader = exports.ScanDataReader = void 0;
13
- const _1 = require(".");
1
+ import { SANEFrame, SANEStatus } from "./index.js";
14
2
  class EventBaseClass {
15
- constructor() {
16
- this._firing = false;
17
- this._listeners = {};
18
- }
3
+ _firing = false;
4
+ _listeners = {};
19
5
  /**
20
6
  * Add event listener.
21
7
  */
22
8
  on(type, listener) {
23
- var _a;
24
9
  if (!this._listeners[type]) {
25
10
  this._listeners[type] = [];
26
11
  }
27
- (_a = this._listeners[type]) === null || _a === void 0 ? void 0 : _a.push(listener);
12
+ this._listeners[type]?.push(listener);
28
13
  }
29
14
  /**
30
15
  * Fire event.
31
16
  */
32
17
  fire(type, ...args) {
33
- var _a;
34
18
  // if already firing an event, queue for later
35
19
  if (this._firing) {
36
20
  return Promise.resolve().then(() => this.fire(type, ...args));
@@ -38,7 +22,7 @@ class EventBaseClass {
38
22
  // fire now
39
23
  this._firing = true;
40
24
  try {
41
- (_a = this._listeners[type]) === null || _a === void 0 ? void 0 : _a.forEach(fn => fn.apply(this, args));
25
+ this._listeners[type]?.forEach(fn => fn.apply(this, args));
42
26
  }
43
27
  finally {
44
28
  this._firing = false;
@@ -61,121 +45,102 @@ class EventBaseClass {
61
45
  *
62
46
  * {@link https://sane-project.gitlab.io/standard/1.06/api.html#code-flow}
63
47
  */
64
- class ScanDataReader extends EventBaseClass {
48
+ export class ScanDataReader extends EventBaseClass {
49
+ _lib;
50
+ _used = false;
51
+ _killed = false;
65
52
  constructor(lib) {
66
53
  super();
67
- this._used = false;
68
- this._killed = false;
69
54
  this._lib = lib;
70
55
  }
71
- _readPromise(parameters) {
72
- return new Promise((resolve, reject) => {
73
- const read = () => __awaiter(this, void 0, void 0, function* () {
74
- try {
75
- if (this._killed) {
76
- yield this._lib.sane_cancel(); // ignore status
77
- }
78
- // reads are non-blocking because of how emscripten works
79
- // this causes sane_read() to immediately return with
80
- // 0 bytes of data, this goes against the SANE spec,
81
- // blocking I/O by default
82
- // the only option right now is to poll the read using
83
- // setTimeout(), this is not ideal
84
- // currently even emscripten's asyncify does not help here,
85
- // but in the future it might help support blocking I/O
86
- // XXX: are we in the future now? can we improve this
87
- // https://github.com/emscripten-core/emscripten/issues/13214
88
- const { status, data } = yield this._lib.sane_read(); // non-blocking
89
- if (status === _1.SANEStatus.GOOD) {
90
- if (parameters && data.length) {
91
- this.fire('data', parameters, data);
92
- }
93
- }
94
- else if (status === _1.SANEStatus.CANCELLED ||
95
- // special case for EOF after sane_cancel, after a successful
96
- // page read (EOF) and subsequent call to sane_cancel we expected a
97
- // CANCELLED status but the test backend never changes from EOF,
98
- // this is different on the pixma backend (it changes to CANCELLED),
99
- // it's unknown what is the correct option, or what happens on other
100
- // backends, handle special case as cancelled for now
101
- // XXX: more research
102
- // https://sane-project.gitlab.io/standard/1.06/api.html#code-flow
103
- (status === _1.SANEStatus.EOF && this._killed)) {
104
- if (this._killed instanceof Error) {
105
- reject(this._killed);
106
- return;
107
- }
108
- resolve();
109
- return;
110
- }
111
- else if (status === _1.SANEStatus.EOF) {
112
- // image finished
113
- this._killed = true;
114
- }
115
- else {
116
- // TODO: handle document feeder / multi-page scans
117
- this._killed = new Error(`Status ${_1.SANEStatus[status]} during sane_read().`);
56
+ async _readPromise(parameters) {
57
+ while (true) {
58
+ try {
59
+ if (this._killed) {
60
+ await this._lib.sane_cancel(); // ignore status
61
+ }
62
+ const { status, data } = await this._lib.sane_read();
63
+ if (status === SANEStatus.GOOD) {
64
+ if (parameters && data.length) {
65
+ this.fire('data', parameters, data);
118
66
  }
119
- setTimeout(read, data && data.length > 0 ? 10 : 200);
120
67
  }
121
- catch (e) {
122
- const ee = e instanceof Error ? e : new Error("Unknown error while scanning.");
123
- if (this._killed) {
124
- // already killed, but sane_read is still going? die
125
- reject(ee);
126
- return;
68
+ else if (status === SANEStatus.CANCELLED ||
69
+ // special case for EOF after sane_cancel, after a successful
70
+ // page read (EOF) and subsequent call to sane_cancel we expected a
71
+ // CANCELLED status but the test backend never changes from EOF,
72
+ // this is different on the pixma backend (it changes to CANCELLED),
73
+ // it's unknown what is the correct option, or what happens on other
74
+ // backends, handle special case as cancelled for now
75
+ // XXX: more research
76
+ // https://sane-project.gitlab.io/standard/1.06/api.html#code-flow
77
+ (status === SANEStatus.EOF && this._killed)) {
78
+ if (this._killed instanceof Error) {
79
+ throw this._killed;
127
80
  }
128
- this._killed = ee;
129
- setTimeout(read, 200);
81
+ return;
82
+ }
83
+ else if (status === SANEStatus.EOF) {
84
+ // image finished
85
+ this._killed = true;
130
86
  }
131
- });
132
- setTimeout(read, 200);
133
- });
87
+ else {
88
+ // TODO: handle document feeder / multi-page scans
89
+ this._killed = new Error(`Status ${SANEStatus[status]} during sane_read().`);
90
+ }
91
+ }
92
+ catch (e) {
93
+ const ee = e instanceof Error ? e : new Error("Unknown error while scanning.");
94
+ if (this._killed) {
95
+ // already killed, but sane_read is still going? die
96
+ throw ee;
97
+ }
98
+ this._killed = ee;
99
+ }
100
+ }
134
101
  }
135
102
  /**
136
103
  * Start scanning operation.
137
104
  */
138
- start() {
139
- return __awaiter(this, void 0, void 0, function* () {
140
- if (this._used) {
141
- // readers are single use
142
- throw new Error("Scan readers cannot be reused.");
143
- }
144
- else {
145
- const { status } = yield this._lib.sane_start();
146
- if (status !== _1.SANEStatus.GOOD) {
147
- return {
148
- status,
149
- parameters: null,
150
- promise: Promise.reject(new Error(`Status ${_1.SANEStatus[status]} during sane_start().`)),
151
- };
152
- }
153
- this._used = true;
154
- }
155
- // after successful start, sane_cancel needs to be called and we need
156
- // to wait for sane_read, so subsequent fails need to use this._killed
157
- // and run the read loop
158
- const { status, parameters } = yield this._lib.sane_get_parameters();
159
- if (status !== _1.SANEStatus.GOOD) {
160
- this._killed = new Error(`Status ${_1.SANEStatus[status]} during sane_get_parameters().`);
105
+ async start() {
106
+ if (this._used) {
107
+ // readers are single use
108
+ throw new Error("Scan readers cannot be reused.");
109
+ }
110
+ else {
111
+ const { status } = await this._lib.sane_start();
112
+ if (status !== SANEStatus.GOOD) {
161
113
  return {
162
- status, parameters,
163
- promise: this._readPromise(parameters),
114
+ status,
115
+ parameters: null,
116
+ promise: Promise.reject(new Error(`Status ${SANEStatus[status]} during sane_start().`)),
164
117
  };
165
118
  }
166
- try {
167
- this.fire('start', parameters);
168
- }
169
- catch (e) {
170
- this._killed = e instanceof Error ? e : new Error("Unknown error while starting the scan.");
171
- }
119
+ this._used = true;
120
+ }
121
+ // after successful start, sane_cancel needs to be called and we need
122
+ // to wait for sane_read, so subsequent fails need to use this._killed
123
+ // and run the read loop
124
+ const { status, parameters } = await this._lib.sane_get_parameters();
125
+ if (status !== SANEStatus.GOOD) {
126
+ this._killed = new Error(`Status ${SANEStatus[status]} during sane_get_parameters().`);
172
127
  return {
173
128
  status, parameters,
174
- promise: this._readPromise(parameters).finally(() => {
175
- this.fire('stop', parameters, this._killed instanceof Error ? this._killed : null);
176
- }),
129
+ promise: this._readPromise(parameters),
177
130
  };
178
- });
131
+ }
132
+ try {
133
+ this.fire('start', parameters);
134
+ }
135
+ catch (e) {
136
+ this._killed = e instanceof Error ? e : new Error("Unknown error while starting the scan.");
137
+ }
138
+ return {
139
+ status, parameters,
140
+ promise: this._readPromise(parameters).finally(() => {
141
+ this.fire('stop', parameters, this._killed instanceof Error ? this._killed : null);
142
+ }),
143
+ };
179
144
  }
180
145
  /**
181
146
  * Cancel scanning operation.
@@ -186,7 +151,6 @@ class ScanDataReader extends EventBaseClass {
186
151
  }
187
152
  }
188
153
  }
189
- exports.ScanDataReader = ScanDataReader;
190
154
  /**
191
155
  * Image reader that automatically generates RGBA data while reading from
192
156
  * SANE's API using {@link ScanDataReader}. It supports the most common scan
@@ -204,18 +168,18 @@ exports.ScanDataReader = ScanDataReader;
204
168
  *
205
169
  * {@link https://sane-project.gitlab.io/standard/1.06/api.html#code-flow}
206
170
  */
207
- class ScanImageReader extends ScanDataReader {
171
+ export class ScanImageReader extends ScanDataReader {
172
+ _line = 0;
173
+ _allData = new Uint8ClampedArray();
174
+ _remainder = new Uint8ClampedArray();
208
175
  constructor(lib) {
209
176
  super(lib);
210
- this._line = 0;
211
- this._allData = new Uint8ClampedArray();
212
- this._remainder = new Uint8ClampedArray();
213
177
  this.on('start', this._onStart);
214
178
  this.on('data', this._onData);
215
179
  this.on('stop', this._onStop);
216
180
  }
217
181
  _onStart(parameters) {
218
- if (parameters.format !== _1.SANEFrame.GRAY && parameters.format !== _1.SANEFrame.RGB) {
182
+ if (parameters.format !== SANEFrame.GRAY && parameters.format !== SANEFrame.RGB) {
219
183
  throw new Error(`Invalid format (${JSON.stringify(parameters)}).`);
220
184
  }
221
185
  if (parameters.depth !== 1 && parameters.depth !== 8) {
@@ -230,7 +194,7 @@ class ScanImageReader extends ScanDataReader {
230
194
  if (parameters.bytes_per_line <= 0 || parameters.pixels_per_line <= 0 || parameters.lines <= 0) {
231
195
  throw new Error(`Unexpected image size (${JSON.stringify(parameters)}).`);
232
196
  }
233
- const channels = parameters.format === _1.SANEFrame.GRAY ? 1 : 3;
197
+ const channels = parameters.format === SANEFrame.GRAY ? 1 : 3;
234
198
  const bits = parameters.pixels_per_line * parameters.depth * channels;
235
199
  if (parameters.bytes_per_line * 8 < bits) {
236
200
  throw new Error(`Unexpected byte count (${JSON.stringify(parameters)}).`);
@@ -254,7 +218,7 @@ class ScanImageReader extends ScanDataReader {
254
218
  const dataOut = new Uint8ClampedArray(lc * parameters.pixels_per_line * 4); // RGBA (8-bit)
255
219
  let ik = 0; // consumed data on dataIn
256
220
  if (parameters.depth === 8) {
257
- if (parameters.format === _1.SANEFrame.GRAY) {
221
+ if (parameters.format === SANEFrame.GRAY) {
258
222
  // 8-bit / 1 channel
259
223
  for (let l = 0, o = 0; l < lc; l++, ik += parameters.bytes_per_line) { // for lines
260
224
  for (let p = 0, i = ik; p < parameters.pixels_per_line; p++, i++, o += 4) { // for pixels
@@ -274,7 +238,7 @@ class ScanImageReader extends ScanDataReader {
274
238
  }
275
239
  }
276
240
  else { // parameters.depth == 1
277
- if (parameters.format === _1.SANEFrame.GRAY) {
241
+ if (parameters.format === SANEFrame.GRAY) {
278
242
  // 1-bit / 1 channel
279
243
  for (let l = 0, o = 0; l < lc; l++, ik += parameters.bytes_per_line) { // for lines
280
244
  for (let p = 0, i = ik; p < parameters.pixels_per_line; i++) { // for in bytes
@@ -315,4 +279,4 @@ class ScanImageReader extends ScanDataReader {
315
279
  }
316
280
  }
317
281
  }
318
- exports.ScanImageReader = ScanImageReader;
282
+ //# sourceMappingURL=readers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"readers.js","sourceRoot":"","sources":["../src/readers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,SAAS,EAAkB,UAAU,EAAE,MAAM,YAAY,CAAC;AAE5E,MAAe,cAAc;IAEjB,OAAO,GAAG,KAAK,CAAC;IAChB,UAAU,GAEd,EAAE,CAAC;IAEP;;OAEG;IACH,EAAE,CAAoB,IAAO,EAAE,QAAiC;QAC5D,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;YACzB,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;QAC/B,CAAC;QACD,IA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CAAC,IAAI,CAAC;4BAC5C,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;4BACpD,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;4BACpD,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,QAAQ;wBACnC,CAAC;oBACL,CAAC;gBACL,CAAC;YACL,CAAC;QACL,CAAC;QACD,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;QACtC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;QACnD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,KAAK,GAAG,UAAU,CAAC,eAAe,GAAG,CAAC,CAAC,CAAC;QACxE,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;IACrB,CAAC;IAEO,OAAO,CAAC,UAA0B,EAAE,KAAmB;QAC3D,IAAI,CAAC,KAAK,EAAE,CAAC;YACT,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,UAAU,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;QAClD,CAAC;IACL,CAAC;CAEJ"}
package/package.json CHANGED
@@ -1,26 +1,27 @@
1
1
  {
2
2
  "name": "sane-wasm",
3
- "version": "0.5.0",
3
+ "version": "0.6.1",
4
4
  "description": "A project to bring the SANE API to the web.",
5
+ "type": "module",
5
6
  "main": "dist/index.js",
6
7
  "types": "dist/index.d.ts",
7
8
  "scripts": {
8
- "test": "jest",
9
- "clean": "npm run clean:ts && npm run clean:sane",
9
+ "test": "vitest run",
10
+ "test:watch": "vitest",
11
+ "clean": "npm run clean:sane && npm run clean:ts",
10
12
  "clean:ts": "rm -rf dist/ docs/",
11
13
  "clean:sane": "./build.sh --no-build --clean",
12
14
  "prebuild": "rm -rf dist/ docs/ build/",
13
- "build": "npm run build:ts && npm run build:sane",
15
+ "build": "npm run build:sane && npm run build:ts",
14
16
  "build:ts": "tsc && typedoc",
15
17
  "postbuild:ts": "rm -rf dist/docs-plugin.*",
16
18
  "build:sane": "./build.sh --with-docker --clean",
17
- "debug:sane": "./build.sh --with-docker --debug --emrun"
19
+ "debug": "./build.sh --with-docker --debug --emrun",
20
+ "emrun": "./build.sh --with-docker --no-build --emrun"
18
21
  },
19
22
  "files": [
20
23
  "dist/",
21
- "lib/",
22
24
  "build/",
23
- "libsane.d.ts",
24
25
  "LICENSE-LGPL.txt",
25
26
  "LICENSE.txt"
26
27
  ],
@@ -34,11 +35,6 @@
34
35
  "url": "https://github.com/goncalomb/sane-wasm/issues"
35
36
  },
36
37
  "homepage": "https://github.com/goncalomb/sane-wasm#readme",
37
- "jest": {
38
- "setupFilesAfterEnv": [
39
- "jest-extended/all"
40
- ]
41
- },
42
38
  "peerDependencies": {
43
39
  "usb": "^2.10.0"
44
40
  },
@@ -48,9 +44,12 @@
48
44
  }
49
45
  },
50
46
  "devDependencies": {
51
- "jest": "^29.6.4",
52
- "jest-extended": "^4.0.1",
53
- "typedoc": "^0.25.0",
54
- "typescript": "^5.2.2"
47
+ "@types/emscripten": "^1.41.6",
48
+ "@types/node": "^26.6.4",
49
+ "jest-extended": "^7.0.0",
50
+ "typedoc": "^0.28.20",
51
+ "typescript": "^6.0.3",
52
+ "usb": "^2.10.0",
53
+ "vitest": "^5.0.3"
55
54
  }
56
55
  }
@@ -1,195 +0,0 @@
1
- /**
2
- * @license
3
- * Copyright 2015 The Emscripten Authors
4
- * SPDX-License-Identifier: MIT
5
- */
6
-
7
- // Pthread Web Worker startup routine:
8
- // This is the entry point file that is loaded first by each Web Worker
9
- // that executes pthreads on the Emscripten application.
10
-
11
- 'use strict';
12
-
13
- var Module = {};
14
-
15
- // Node.js support
16
- var ENVIRONMENT_IS_NODE = typeof process == 'object' && typeof process.versions == 'object' && typeof process.versions.node == 'string';
17
- if (ENVIRONMENT_IS_NODE) {
18
- // Create as web-worker-like an environment as we can.
19
-
20
- var nodeWorkerThreads = require('worker_threads');
21
-
22
- var parentPort = nodeWorkerThreads.parentPort;
23
-
24
- parentPort.on('message', (data) => onmessage({ data: data }));
25
-
26
- var fs = require('fs');
27
-
28
- Object.assign(global, {
29
- self: global,
30
- require,
31
- Module,
32
- location: {
33
- href: __filename
34
- },
35
- Worker: nodeWorkerThreads.Worker,
36
- importScripts: (f) => (0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f),
37
- postMessage: (msg) => parentPort.postMessage(msg),
38
- performance: global.performance || { now: Date.now },
39
- });
40
- }
41
-
42
- // Thread-local guard variable for one-time init of the JS state
43
- var initializedJS = false;
44
-
45
- function assert(condition, text) {
46
- if (!condition) abort('Assertion failed: ' + text);
47
- }
48
-
49
- function threadPrintErr() {
50
- var text = Array.prototype.slice.call(arguments).join(' ');
51
- // See https://github.com/emscripten-core/emscripten/issues/14804
52
- if (ENVIRONMENT_IS_NODE) {
53
- fs.writeSync(2, text + '\n');
54
- return;
55
- }
56
- console.error(text);
57
- }
58
- function threadAlert() {
59
- var text = Array.prototype.slice.call(arguments).join(' ');
60
- postMessage({cmd: 'alert', text, threadId: Module['_pthread_self']()});
61
- }
62
- // We don't need out() for now, but may need to add it if we want to use it
63
- // here. Or, if this code all moves into the main JS, that problem will go
64
- // away. (For now, adding it here increases code size for no benefit.)
65
- var out = () => { throw 'out() is not defined in worker.js.'; }
66
- var err = threadPrintErr;
67
- self.alert = threadAlert;
68
- var dbg = threadPrintErr;
69
-
70
- Module['instantiateWasm'] = (info, receiveInstance) => {
71
- // Instantiate from the module posted from the main thread.
72
- // We can just use sync instantiation in the worker.
73
- var module = Module['wasmModule'];
74
- // We don't need the module anymore; new threads will be spawned from the main thread.
75
- Module['wasmModule'] = null;
76
- var instance = new WebAssembly.Instance(module, info);
77
- // TODO: Due to Closure regression https://github.com/google/closure-compiler/issues/3193,
78
- // the above line no longer optimizes out down to the following line.
79
- // When the regression is fixed, we can remove this if/else.
80
- return receiveInstance(instance);
81
- }
82
-
83
- // Turn unhandled rejected promises into errors so that the main thread will be
84
- // notified about them.
85
- self.onunhandledrejection = (e) => {
86
- throw e.reason || e;
87
- };
88
-
89
- function handleMessage(e) {
90
- try {
91
- if (e.data.cmd === 'load') { // Preload command that is called once per worker to parse and load the Emscripten code.
92
-
93
- // Until we initialize the runtime, queue up any further incoming messages.
94
- let messageQueue = [];
95
- self.onmessage = (e) => messageQueue.push(e);
96
-
97
- // And add a callback for when the runtime is initialized.
98
- self.startWorker = (instance) => {
99
- Module = instance;
100
- // Notify the main thread that this thread has loaded.
101
- postMessage({ 'cmd': 'loaded' });
102
- // Process any messages that were queued before the thread was ready.
103
- for (let msg of messageQueue) {
104
- handleMessage(msg);
105
- }
106
- // Restore the real message handler.
107
- self.onmessage = handleMessage;
108
- };
109
-
110
- // Module and memory were sent from main thread
111
- Module['wasmModule'] = e.data.wasmModule;
112
-
113
- // Use `const` here to ensure that the variable is scoped only to
114
- // that iteration, allowing safe reference from a closure.
115
- for (const handler of e.data.handlers) {
116
- Module[handler] = (...args) => {
117
- postMessage({ cmd: 'callHandler', handler, args: args });
118
- }
119
- }
120
-
121
- Module['wasmMemory'] = e.data.wasmMemory;
122
-
123
- Module['buffer'] = Module['wasmMemory'].buffer;
124
-
125
- Module['workerID'] = e.data.workerID;
126
-
127
- Module['ENVIRONMENT_IS_PTHREAD'] = true;
128
-
129
- if (typeof e.data.urlOrBlob == 'string') {
130
- importScripts(e.data.urlOrBlob);
131
- } else {
132
- var objectUrl = URL.createObjectURL(e.data.urlOrBlob);
133
- importScripts(objectUrl);
134
- URL.revokeObjectURL(objectUrl);
135
- }
136
- LibSANE(Module);
137
- } else if (e.data.cmd === 'run') {
138
- // Pass the thread address to wasm to store it for fast access.
139
- Module['__emscripten_thread_init'](e.data.pthread_ptr, /*is_main=*/0, /*is_runtime=*/0, /*can_block=*/1);
140
-
141
- // Await mailbox notifications with `Atomics.waitAsync` so we can start
142
- // using the fast `Atomics.notify` notification path.
143
- Module['__emscripten_thread_mailbox_await'](e.data.pthread_ptr);
144
-
145
- assert(e.data.pthread_ptr);
146
- // Also call inside JS module to set up the stack frame for this pthread in JS module scope
147
- Module['establishStackSpace']();
148
- Module['PThread'].receiveObjectTransfer(e.data);
149
- Module['PThread'].threadInitTLS();
150
-
151
- if (!initializedJS) {
152
- // Embind must initialize itself on all threads, as it generates support JS.
153
- // We only do this once per worker since they get reused
154
- Module['__embind_initialize_bindings']();
155
- initializedJS = true;
156
- }
157
-
158
- try {
159
- Module['invokeEntryPoint'](e.data.start_routine, e.data.arg);
160
- } catch(ex) {
161
- if (ex != 'unwind') {
162
- // The pthread "crashed". Do not call `_emscripten_thread_exit` (which
163
- // would make this thread joinable). Instead, re-throw the exception
164
- // and let the top level handler propagate it back to the main thread.
165
- throw ex;
166
- }
167
- }
168
- } else if (e.data.cmd === 'cancel') { // Main thread is asking for a pthread_cancel() on this thread.
169
- if (Module['_pthread_self']()) {
170
- Module['__emscripten_thread_exit'](-1);
171
- }
172
- } else if (e.data.target === 'setimmediate') {
173
- // no-op
174
- } else if (e.data.cmd === 'checkMailbox') {
175
- if (initializedJS) {
176
- Module['checkMailbox']();
177
- }
178
- } else if (e.data.cmd) {
179
- // The received message looks like something that should be handled by this message
180
- // handler, (since there is a e.data.cmd field present), but is not one of the
181
- // recognized commands:
182
- err(`worker.js received unknown command ${e.data.cmd}`);
183
- err(e.data);
184
- }
185
- } catch(ex) {
186
- err(`worker.js onmessage() captured an uncaught exception: ${ex}`);
187
- if (ex && ex.stack) err(ex.stack);
188
- if (Module['__emscripten_thread_crashed']) {
189
- Module['__emscripten_thread_crashed']();
190
- }
191
- throw ex;
192
- }
193
- };
194
-
195
- self.onmessage = handleMessage;
package/lib/index.js DELETED
@@ -1,7 +0,0 @@
1
- if (globalThis.window && globalThis.window.document) {
2
- // browser environment (e.g. webpack)
3
- module.exports = require('./loader.js');
4
- } else {
5
- // local environment (e.g. node)
6
- module.exports = eval('require')('../build/libsane.js');
7
- }