sane-wasm 0.1.2 → 0.3.0

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/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # SANE WebAssembly (sane-wasm)
2
2
 
3
- A project to bring the [SANE API](http://www.sane-project.org/intro.html) to the web.
3
+ A project to bring the [SANE API](http://www.sane-project.org/intro.html) to Node.js and the Web using WebAssembly.
4
4
 
5
- Currently, it only supports USB scanners and is only tested on a browser environment (WebUSB).
5
+ It supports USB scanners on browser environments through the [WebUSB API](https://developer.mozilla.org/en-US/docs/Web/API/WebUSB_API) and on Node.js using [node-usb](https://github.com/node-usb/node-usb).
6
6
 
7
7
  This works by compiling all SANE backends (and required dependencies) to WebAssembly using Emscripten. The other key piece is @RReverser's [bridge from libusb to WebUSB](https://github.com/libusb/libusb/pull/1057) ([1](https://web.dev/porting-libusb-to-webusb/)/[2](https://web.dev/porting-gphoto2-to-the-web/)) with [some patching](https://github.com/goncalomb/sane-wasm/issues/1) to support multi-threading.
8
8
 
@@ -10,15 +10,67 @@ Right now, it includes [all backends](http://www.sane-project.org/lists/sane-bac
10
10
 
11
11
  ## WebScan
12
12
 
13
- **Check [webscan.goncalomb.com](https://webscan.goncalomb.com/) for a demo of sane-wasm.** This is a React application that uses sane-wasm for document/image scanning directly in the browser. Exposes all scanning options to the user for full control.
13
+ **Check [webscan.goncalomb.com](https://webscan.goncalomb.com/) for a demo of sane-wasm.** This is a React application that uses sane-wasm for document/image scanning directly in the browser. It exposes all scanning options to the user for full control.
14
14
 
15
- If you are interested in seeing the compiled output of sane-wasm, check:
15
+ ## Usage
16
16
 
17
- * [webscan.goncalomb.com/sane-wasm/](https://webscan.goncalomb.com/sane-wasm/): all files
18
- * [webscan.goncalomb.com/sane-wasm/libsane.html](https://webscan.goncalomb.com/sane-wasm/libsane.html): test page
17
+ The pre-compiled WebAssembly package for sane-wasm is published on NPM:
18
+
19
+ https://www.npmjs.com/package/sane-wasm?activeTab=code
20
+
21
+ ### For Web Environments (e.g. Webpack)
22
+
23
+ ```
24
+ npm install -D sane-wasm
25
+ ```
26
+
27
+ The main .js will not be bundled with your application. A loader ([lib/loader.js](https://github.com/goncalomb/sane-wasm/blob/master/lib/loader.js)) is provided to automatically load the .js/.wasm files from a CDN ([jsdelivr.com](https://www.jsdelivr.com/)). You can configure the loader to serve the files from your server if you want.
28
+
29
+ See [examples/webpack/](https://github.com/goncalomb/sane-wasm/tree/master/examples/webpack) for a working example.
30
+
31
+ ### For Node.js
32
+
33
+ ```
34
+ npm install sane-wasm usb
35
+ ```
36
+
37
+ The usb package ([node-usb](https://github.com/node-usb/node-usb)) is required to provide USB functionality.
38
+
39
+ See [examples/node/](https://github.com/goncalomb/sane-wasm/tree/master/examples/node) for a working example.
40
+
41
+ ### As Git Submodule (not recommended) / Custom Build
42
+
43
+ As an alternative, you can add sane-wasm as a [submodule](https://git-scm.com/book/en/v2/Git-Tools-Submodules) to your application and integrate `./build.sh` with your build process. This is not recommended as it relies on some magical/dubious code on `./build.sh` to make the build work.
44
+
45
+ You can also just build sane-wasm independently and use the build artifacts...
46
+
47
+ ```
48
+ <script src="build/libsane.js"></script>
49
+ <script>
50
+ window.LibSANE().then(lib => {
51
+ // your code
52
+ console.log(lib.sane_init());
53
+ });
54
+ </script>
55
+ ```
56
+
57
+ ```
58
+ const { libsane } = require('./sane-wasm');
59
+ libsane().then(lib => {
60
+ console.log(lib.sane_init());
61
+ });
62
+ ```
63
+
64
+ ```
65
+ import { libsane } from './sane-wasm';
66
+ const lib = await libsane();
67
+ console.log(lib.sane_init());
68
+ ```
19
69
 
20
70
  ## Building
21
71
 
72
+ ### SANE Only
73
+
22
74
  Building requires [emscripten](https://github.com/emscripten-core/emscripten) and all the required tools to build the dependencies.
23
75
 
24
76
  The preferred way is just to use the pre-configured Docker image:
@@ -33,7 +85,7 @@ Start the test server with:
33
85
 
34
86
  Check the test page at: http://localhost:6931/libsane.html.
35
87
 
36
- ### build.sh
88
+ #### build.sh
37
89
 
38
90
  The `./build.sh` script has other options for debugging:
39
91
 
@@ -47,26 +99,11 @@ usage: build.sh [options]
47
99
  --shell run debug shell (depends on --with-docker)
48
100
  ```
49
101
 
50
- ## Using
51
-
52
- For now, this project has only been used on a browser environment, usage with Node.js or other environments is untested/unsupported.
53
-
54
- The recommended way to use this project is to add it as a [submodule](https://git-scm.com/book/en/v2/Git-Tools-Submodules) to your application. Integrate `./build.sh` with your build process. And include the main .js file:
55
-
56
- ```
57
- <script src="build/libsane.js"></script>
58
- <script>
59
- window.LibSANE().then(LibSANE => {
60
- window.LibSANE = LibSANE;
61
- // your code
62
- console.log(LibSANE.sane_init());
63
- });
64
- </script>
65
- ```
102
+ ### Full Build (SANE + TypeScript)
66
103
 
67
- This will be improved in the future.
104
+ To do a full build use `npm run build`.
68
105
 
69
- ## Exposed API
106
+ ## API
70
107
 
71
108
  Most of the SANE API is exposed as-is. Check the [SANE Standard](https://sane-project.gitlab.io/standard/index.html).
72
109
 
@@ -76,12 +113,12 @@ Some safeguards are in place to avoid API misuse and prevent memory leaks. It's
76
113
 
77
114
  The most important difference with the underlying SANE API is that **device handles are not exposed**. This means that `sane_open()` does not return a device handle. A single handle is managed by the internal code. This effectively means that **only one device can be accessed at a time**. This was a design decision made to simplify the API and prevent other issues.
78
115
 
79
- > Personally, I believe that this is an acceptable change, especially for WebAssembly where it may be easier to lose track of opened resources and crash the application. The SANE API is also somewhat unforgiving and building more safeguards around it (especially with multiple handles) is not worth the effort. Ultimately I don't see a use that requires more than one device open at a time. @goncalomb
116
+ > Personally, I believe that this is an acceptable change, especially for WebAssembly where it may be easier to lose track of opened resources and crash the application. The SANE API is also somewhat unforgiving and building more safeguards around it (especially with multiple handles) is not worth the effort. Ultimately I don't see a use that requires more than one device open at a time. -goncalomb
80
117
 
81
118
  ### Documentation
82
119
 
83
- For now, check the [libsane.d.ts](libsane.d.ts) for the API declaration.
120
+ Check API documentation at: [goncalomb.github.io/sane-wasm/modules.html](https://goncalomb.github.io/sane-wasm/modules.html).
84
121
 
85
122
  ## License
86
123
 
87
- Because of the weird state of SANE's licensing (GPL + linking exception, on some backends), see [backends/LICENSE](https://gitlab.com/sane-project/backends/-/blob/master/LICENSE). I'm releasing this project with dual licensing [GNU GPLv2](LICENSE.txt) + [GNU LGPLv2.1](LICENSE-LGPL.txt). IANAL, you choose.
124
+ Because of the weird state of SANE's licensing (GPL + linking exception, on some backends), see [backends/LICENSE](https://gitlab.com/sane-project/backends/-/blob/master/LICENSE). I'm releasing this project with dual licensing [GNU GPLv2](https://github.com/goncalomb/sane-wasm/blob/master/LICENSE.txt) + [GNU LGPLv2.1](https://github.com/goncalomb/sane-wasm/blob/master/LICENSE-LGPL.txt). IANAL, you choose.
package/build/libsane.js CHANGED
@@ -80,6 +80,7 @@ Module["ready"] = new Promise(function(resolve, reject) {
80
80
  obj.__proto__ = this.prototype;
81
81
  obj[this.reverseObject] = {};
82
82
  Object.keys(obj).forEach(k => {
83
+ obj[obj[k]] = k;
83
84
  obj[this.reverseObject][obj[k]] = k;
84
85
  });
85
86
  }
Binary file
@@ -0,0 +1,499 @@
1
+ /**
2
+ * Equivalent to the SANE API C enum `SANE_Status`.
3
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#status-type}
4
+ */
5
+ export declare enum SANEStatus {
6
+ GOOD = 0,
7
+ UNSUPPORTED = 1,
8
+ CANCELLED = 2,
9
+ DEVICE_BUSY = 3,
10
+ INVAL = 4,
11
+ EOF = 5,
12
+ JAMMED = 6,
13
+ NO_DOCS = 7,
14
+ COVER_OPEN = 8,
15
+ IO_ERROR = 9,
16
+ NO_MEM = 10,
17
+ ACCESS_DENIED = 11
18
+ }
19
+ /**
20
+ * Equivalent to the SANE API C enum `SANE_Value_Type`.
21
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#option-value-type}
22
+ */
23
+ export declare enum SANEValueType {
24
+ BOOL = 0,
25
+ INT = 1,
26
+ FIXED = 2,
27
+ STRING = 3,
28
+ BUTTON = 4,
29
+ GROUP = 5
30
+ }
31
+ /**
32
+ * Equivalent to the SANE API C enum `SANE_Unit`.
33
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#option-value-unit}
34
+ */
35
+ export declare enum SANEUnit {
36
+ NONE = 0,
37
+ PIXEL = 1,
38
+ BIT = 2,
39
+ MM = 3,
40
+ DPI = 4,
41
+ PERCENT = 5,
42
+ MICROSECOND = 6
43
+ }
44
+ /**
45
+ * Equivalent to the SANE API C enum `SANE_Constraint_Type`.
46
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#option-value-constraints}
47
+ */
48
+ export declare enum SANEConstraintType {
49
+ NONE = 0,
50
+ RANGE = 1,
51
+ WORD_LIST = 2,
52
+ STRING_LIST = 3
53
+ }
54
+ /**
55
+ * Equivalent to the SANE API C enum `SANE_Frame`.
56
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-get-parameters}
57
+ */
58
+ export declare enum SANEFrame {
59
+ GRAY = 0,
60
+ RGB = 1,
61
+ RED = 2,
62
+ GREEN = 3,
63
+ BLUE = 4
64
+ }
65
+ /**
66
+ * Library state. This is provided by sane-wasm, it's not part of SANE API.
67
+ */
68
+ export type SANEState = {
69
+ /**
70
+ * Is the library initialized with {@link LibSANE.sane_init}?
71
+ */
72
+ initialized: boolean;
73
+ /**
74
+ * Version code provided by the last {@link LibSANE.sane_init} call.
75
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#version-control}
76
+ */
77
+ version_code: number;
78
+ /**
79
+ * Version code (decoded) provided by the last {@link LibSANE.sane_init} call.
80
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#version-control}
81
+ */
82
+ version: {
83
+ major: number;
84
+ minor: number;
85
+ build: number;
86
+ };
87
+ /**
88
+ * Is a device open with {@link LibSANE.sane_open}?
89
+ */
90
+ open: boolean;
91
+ };
92
+ /**
93
+ * Equivalent to the SANE API C type `SANE_Device`.
94
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#device-descriptor-type}
95
+ */
96
+ export type SANEDevice = {
97
+ name: string;
98
+ vendor: string;
99
+ model: string;
100
+ type: string;
101
+ };
102
+ /**
103
+ * Equivalent to the SANE API C bit mask `SANE_CAP_*`.
104
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#option-capabilities}
105
+ */
106
+ export type SANECap = {
107
+ SOFT_SELECT: boolean;
108
+ HARD_SELECT: boolean;
109
+ SOFT_DETECT: boolean;
110
+ EMULATED: boolean;
111
+ AUTOMATIC: boolean;
112
+ INACTIVE: boolean;
113
+ ADVANCED: boolean;
114
+ };
115
+ /**
116
+ * Equivalent to the SANE API C type `SANE_Range`.
117
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#option-descriptor-type}
118
+ */
119
+ export type SANERange = {
120
+ min: number;
121
+ max: number;
122
+ quant: number;
123
+ };
124
+ /**
125
+ * Equivalent to the SANE API C type `SANE_Option_Descriptor`.
126
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#option-descriptor-type}
127
+ */
128
+ export type SANEOptionDescriptor = {
129
+ name: string;
130
+ title: string;
131
+ desc: string;
132
+ type: SANEValueType;
133
+ unit: SANEUnit;
134
+ size: number;
135
+ cap: SANECap;
136
+ } & ({
137
+ constraint_type: SANEConstraintType.NONE;
138
+ constraint: null;
139
+ } | {
140
+ constraint_type: SANEConstraintType.RANGE;
141
+ constraint: SANERange;
142
+ } | {
143
+ constraint_type: SANEConstraintType.WORD_LIST;
144
+ constraint: number[];
145
+ } | {
146
+ constraint_type: SANEConstraintType.STRING_LIST;
147
+ constraint: string[];
148
+ });
149
+ /**
150
+ * Equivalent to the SANE API C bit mask `SANE_INFO_*`.
151
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-control-option}
152
+ */
153
+ export type SANEInfo = {
154
+ INEXACT: boolean;
155
+ RELOAD_OPTIONS: boolean;
156
+ RELOAD_PARAMS: boolean;
157
+ };
158
+ /**
159
+ * Equivalent to the SANE API C type `SANE_Parameters`.
160
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-get-parameters}
161
+ */
162
+ export type SANEParameters = {
163
+ format: SANEFrame;
164
+ last_frame: boolean;
165
+ bytes_per_line: number;
166
+ pixels_per_line: number;
167
+ lines: number;
168
+ depth: number;
169
+ };
170
+ /**
171
+ * @private Type for enums provided by the core SANE Emscripten module.
172
+ * @deprecated
173
+ */
174
+ export type SANEEnum<E, TE> = E & {
175
+ /**
176
+ * @deprecated Use reverse map already built into TS enums.
177
+ */
178
+ asString: (value: TE) => string;
179
+ };
180
+ /**
181
+ * The core SANE Emscripten module.
182
+ */
183
+ export interface LibSANE {
184
+ /**
185
+ * Commit hash of sane-wasm.
186
+ */
187
+ SANE_WASM_COMMIT: string;
188
+ /**
189
+ * Version code of sane-wasm.
190
+ */
191
+ SANE_WASM_VERSION: string;
192
+ /**
193
+ * List of SANE API backends built into sane-wasm.
194
+ */
195
+ SANE_WASM_BACKENDS: string;
196
+ /**
197
+ * SANE API version code (major).
198
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#version-control}
199
+ */
200
+ SANE_CURRENT_MAJOR: number;
201
+ /**
202
+ * SANE API version code (minor).
203
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#version-control}
204
+ */
205
+ SANE_CURRENT_MINOR: number;
206
+ /**
207
+ * @deprecated Consider using the SANEStatus enum directly.
208
+ *
209
+ * This object is not going away anytime soon (or ever probably).
210
+ *
211
+ * But you should consider using the equivalent enum because it doesn't
212
+ * require having a reference to the library module. All enums are
213
+ * exported with the package.
214
+ */
215
+ SANE_STATUS: SANEEnum<typeof SANEStatus, SANEStatus>;
216
+ /**
217
+ * @deprecated Consider using the SANEValueType enum directly.
218
+ *
219
+ * This object is not going away anytime soon (or ever probably).
220
+ *
221
+ * But you should consider using the equivalent enum because it doesn't
222
+ * require having a reference to the library module. All enums are
223
+ * exported with the package.
224
+ */
225
+ SANE_TYPE: SANEEnum<typeof SANEValueType, SANEValueType>;
226
+ /**
227
+ * @deprecated Consider using the SANEUnit enum directly.
228
+ *
229
+ * This object is not going away anytime soon (or ever probably).
230
+ *
231
+ * But you should consider using the equivalent enum because it doesn't
232
+ * require having a reference to the library module. All enums are
233
+ * exported with the package.
234
+ */
235
+ SANE_UNIT: SANEEnum<typeof SANEUnit, SANEUnit>;
236
+ /**
237
+ * @deprecated Consider using the SANEConstraintType enum directly.
238
+ *
239
+ * This object is not going away anytime soon (or ever probably).
240
+ *
241
+ * But you should consider using the equivalent enum because it doesn't
242
+ * require having a reference to the library module. All enums are
243
+ * exported with the package.
244
+ */
245
+ SANE_CONSTRAINT: SANEEnum<typeof SANEConstraintType, SANEConstraintType>;
246
+ /**
247
+ * @deprecated Consider using the SANEFrame enum directly.
248
+ *
249
+ * This object is not going away anytime soon (or ever probably).
250
+ *
251
+ * But you should consider using the equivalent enum because it doesn't
252
+ * require having a reference to the library module. All enums are
253
+ * exported with the package.
254
+ */
255
+ SANE_FRAME: SANEEnum<typeof SANEFrame, SANEFrame>;
256
+ /**
257
+ * Get the current state of the library.
258
+ *
259
+ * This is provided by sane-wasm, it's not part of SANE API.
260
+ */
261
+ sane_get_state: () => SANEState;
262
+ /**
263
+ * Equivalent to the SANE API C function `sane_init`.
264
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-init}
265
+ */
266
+ sane_init: () => {
267
+ status: SANEStatus.GOOD;
268
+ version_code: number;
269
+ } | {
270
+ status: Exclude<SANEStatus, SANEStatus.GOOD>;
271
+ version_code: null;
272
+ };
273
+ /**
274
+ * Equivalent to the SANE API C function `sane_exit`.
275
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-exit}
276
+ */
277
+ sane_exit: () => Promise<{
278
+ status: SANEStatus;
279
+ }>;
280
+ /**
281
+ * Equivalent to the SANE API C function `sane_get_devices`.
282
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-get-devices}
283
+ */
284
+ sane_get_devices: () => Promise<{
285
+ status: SANEStatus.GOOD;
286
+ devices: SANEDevice[];
287
+ } | {
288
+ status: Exclude<SANEStatus, SANEStatus.GOOD>;
289
+ devices: null;
290
+ }>;
291
+ /**
292
+ * Equivalent to the SANE API C function `sane_open`.
293
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-open}
294
+ */
295
+ sane_open: (devicename: string) => Promise<{
296
+ status: SANEStatus;
297
+ }>;
298
+ /**
299
+ * Equivalent to the SANE API C function `sane_close`.
300
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-close}
301
+ */
302
+ sane_close: () => Promise<{
303
+ status: SANEStatus;
304
+ }>;
305
+ /**
306
+ * Equivalent to the SANE API C function `sane_get_option_descriptor`.
307
+ *
308
+ * The result `option_descriptor` can be null even with
309
+ * `status = SANEStatus.GOOD`, it signals invalid option index.
310
+ *
311
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-get-option-descriptor}
312
+ */
313
+ sane_get_option_descriptor: (option: number) => {
314
+ status: SANEStatus;
315
+ option_descriptor: SANEOptionDescriptor | null;
316
+ };
317
+ /**
318
+ * Equivalent to the SANE API C function `sane_control_option`.
319
+ *
320
+ * With `a = SANE_ACTION_GET_VALUE` (get option value).
321
+ *
322
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-control-option}
323
+ */
324
+ sane_control_option_get_value: (option: number) => Promise<{
325
+ status: SANEStatus.GOOD;
326
+ value: any;
327
+ } | {
328
+ status: Exclude<SANEStatus, SANEStatus.GOOD>;
329
+ value: null;
330
+ }>;
331
+ /**
332
+ * Equivalent to the SANE API C function `sane_control_option`.
333
+ *
334
+ * With `a = SANE_ACTION_SET_VALUE` (set option value).
335
+ *
336
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-control-option}
337
+ */
338
+ sane_control_option_set_value: (option: number, value: any) => Promise<{
339
+ status: SANEStatus.GOOD;
340
+ info: SANEInfo;
341
+ } | {
342
+ status: Exclude<SANEStatus, SANEStatus.GOOD>;
343
+ info: null;
344
+ }>;
345
+ /**
346
+ * Equivalent to the SANE API C function `sane_control_option`.
347
+ *
348
+ * With `a = SANE_ACTION_SET_AUTO` (set option automatic).
349
+ *
350
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-control-option}
351
+ */
352
+ sane_control_option_set_auto: (option: number) => Promise<{
353
+ status: SANEStatus.GOOD;
354
+ info: SANEInfo;
355
+ } | {
356
+ status: Exclude<SANEStatus, SANEStatus.GOOD>;
357
+ info: null;
358
+ }>;
359
+ /**
360
+ * Equivalent to the SANE API C function `sane_get_parameters`.
361
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-get-parameters}
362
+ */
363
+ sane_get_parameters: () => {
364
+ status: SANEStatus.GOOD;
365
+ parameters: SANEParameters;
366
+ } | {
367
+ status: Exclude<SANEStatus, SANEStatus.GOOD>;
368
+ parameters: null;
369
+ };
370
+ /**
371
+ * Equivalent to the SANE API C function `sane_start`.
372
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-start}
373
+ */
374
+ sane_start: () => {
375
+ status: SANEStatus;
376
+ };
377
+ /**
378
+ * Equivalent to the SANE API C function `sane_read`.
379
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-read}
380
+ */
381
+ sane_read: () => Promise<{
382
+ status: SANEStatus.GOOD;
383
+ data: Uint8Array;
384
+ } | {
385
+ status: Exclude<SANEStatus, SANEStatus.GOOD>;
386
+ data: null;
387
+ }>;
388
+ /**
389
+ * Equivalent to the SANE API C function `sane_cancel`.
390
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-cancel}
391
+ */
392
+ sane_cancel: () => Promise<{
393
+ status: SANEStatus;
394
+ }>;
395
+ /**
396
+ * Equivalent to the SANE API C function `sane_strstatus`.
397
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-strstatus}
398
+ */
399
+ sane_strstatus: (status: SANEStatus) => string;
400
+ }
401
+ /**
402
+ * SANE Emscripten module options (specific to sane-wasm).
403
+ */
404
+ export type LibSANEOptions = {
405
+ /**
406
+ * Loader URL that serves the build artifacts (libsane.js etc.).
407
+ *
408
+ * The loader is only used on web environments.
409
+ *
410
+ * @defaultvalue "sane-wasm @ jsdelivr.net"
411
+ */
412
+ loaderURL?: string;
413
+ /**
414
+ * Should the loader prefetch the resources to a Bob object? This is
415
+ * required when loading from a different origin. Check the loader
416
+ * code for more information.
417
+ *
418
+ * The loader is only used on web environments.
419
+ *
420
+ * @defaultvalue `true`
421
+ */
422
+ loaderPrefetchToBlob?: boolean;
423
+ /**
424
+ * Should the loader remove the global module variable `window.LibSANE`?
425
+ *
426
+ * The loader is only used on web environments.
427
+ *
428
+ * @defaultvalue `true`
429
+ */
430
+ loaderRemoveGlobal?: boolean;
431
+ /**
432
+ * Enables SANE low-level debug messages, this can be quite verbose.
433
+ *
434
+ * @defaultvalue `false`
435
+ */
436
+ debugSANE?: boolean;
437
+ /**
438
+ * Enables libusb low-level debug messages, this can be quite verbose.
439
+ *
440
+ * @defaultvalue `false`
441
+ */
442
+ debugUSB?: boolean;
443
+ /**
444
+ * Enables debug messages for SANE function calls.
445
+ *
446
+ * @defaultvalue `false`
447
+ */
448
+ debugFunctionCalls?: boolean;
449
+ /**
450
+ * Sets the number of test devices for SANE's test backend.
451
+ *
452
+ * @defaultvalue `0`
453
+ */
454
+ debugTestDevices?: number;
455
+ /**
456
+ * Enables sane-wasm "promisify" to normalize the API. See pre.js for more
457
+ * information.
458
+ *
459
+ * @deprecated Setting this to `false` changes the API definition! Some
460
+ * functions may stop being asynchronous in some cases. You want this
461
+ * option enabled! This option may be removed in the future.
462
+ *
463
+ * @defaultvalue `true`
464
+ */
465
+ promisify?: boolean;
466
+ /**
467
+ * Enables sane-wasm "promisify" queue that queues function calls to
468
+ * prevent issues with calling multiple asynchronous functions. See pre.js
469
+ * for more information.
470
+ *
471
+ * @deprecated You want this option enabled! This option may be removed in
472
+ * the future.
473
+ *
474
+ * @defaultvalue `true`
475
+ */
476
+ promisifyQueue?: boolean;
477
+ };
478
+ /**
479
+ * The factory that initializes the core SANE Emscripten module.
480
+ */
481
+ export type LibSANEFactory = (options?: {
482
+ /**
483
+ * Emscripten module options specific to sane-wasm.
484
+ */
485
+ sane?: LibSANEOptions;
486
+ /**
487
+ * Other Emscripten module options.
488
+ */
489
+ [k: string]: any;
490
+ }) => Promise<LibSANE>;
491
+ /**
492
+ * Main LibSANE factory.
493
+ */
494
+ export declare const libsane: LibSANEFactory;
495
+ /**
496
+ * @deprecated The default export may be removed in the future.
497
+ * Use the 'libsane' named export.
498
+ */
499
+ export default libsane;
package/dist/index.js ADDED
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.libsane = exports.SANEFrame = exports.SANEConstraintType = exports.SANEUnit = exports.SANEValueType = exports.SANEStatus = void 0;
4
+ /**
5
+ * Equivalent to the SANE API C enum `SANE_Status`.
6
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#status-type}
7
+ */
8
+ var SANEStatus;
9
+ (function (SANEStatus) {
10
+ SANEStatus[SANEStatus["GOOD"] = 0] = "GOOD";
11
+ SANEStatus[SANEStatus["UNSUPPORTED"] = 1] = "UNSUPPORTED";
12
+ SANEStatus[SANEStatus["CANCELLED"] = 2] = "CANCELLED";
13
+ SANEStatus[SANEStatus["DEVICE_BUSY"] = 3] = "DEVICE_BUSY";
14
+ SANEStatus[SANEStatus["INVAL"] = 4] = "INVAL";
15
+ SANEStatus[SANEStatus["EOF"] = 5] = "EOF";
16
+ SANEStatus[SANEStatus["JAMMED"] = 6] = "JAMMED";
17
+ SANEStatus[SANEStatus["NO_DOCS"] = 7] = "NO_DOCS";
18
+ SANEStatus[SANEStatus["COVER_OPEN"] = 8] = "COVER_OPEN";
19
+ SANEStatus[SANEStatus["IO_ERROR"] = 9] = "IO_ERROR";
20
+ SANEStatus[SANEStatus["NO_MEM"] = 10] = "NO_MEM";
21
+ SANEStatus[SANEStatus["ACCESS_DENIED"] = 11] = "ACCESS_DENIED";
22
+ })(SANEStatus || (exports.SANEStatus = SANEStatus = {}));
23
+ /**
24
+ * Equivalent to the SANE API C enum `SANE_Value_Type`.
25
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#option-value-type}
26
+ */
27
+ var SANEValueType;
28
+ (function (SANEValueType) {
29
+ SANEValueType[SANEValueType["BOOL"] = 0] = "BOOL";
30
+ SANEValueType[SANEValueType["INT"] = 1] = "INT";
31
+ SANEValueType[SANEValueType["FIXED"] = 2] = "FIXED";
32
+ SANEValueType[SANEValueType["STRING"] = 3] = "STRING";
33
+ SANEValueType[SANEValueType["BUTTON"] = 4] = "BUTTON";
34
+ SANEValueType[SANEValueType["GROUP"] = 5] = "GROUP";
35
+ })(SANEValueType || (exports.SANEValueType = SANEValueType = {}));
36
+ /**
37
+ * Equivalent to the SANE API C enum `SANE_Unit`.
38
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#option-value-unit}
39
+ */
40
+ var SANEUnit;
41
+ (function (SANEUnit) {
42
+ SANEUnit[SANEUnit["NONE"] = 0] = "NONE";
43
+ SANEUnit[SANEUnit["PIXEL"] = 1] = "PIXEL";
44
+ SANEUnit[SANEUnit["BIT"] = 2] = "BIT";
45
+ SANEUnit[SANEUnit["MM"] = 3] = "MM";
46
+ SANEUnit[SANEUnit["DPI"] = 4] = "DPI";
47
+ SANEUnit[SANEUnit["PERCENT"] = 5] = "PERCENT";
48
+ SANEUnit[SANEUnit["MICROSECOND"] = 6] = "MICROSECOND";
49
+ })(SANEUnit || (exports.SANEUnit = SANEUnit = {}));
50
+ /**
51
+ * Equivalent to the SANE API C enum `SANE_Constraint_Type`.
52
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#option-value-constraints}
53
+ */
54
+ var SANEConstraintType;
55
+ (function (SANEConstraintType) {
56
+ SANEConstraintType[SANEConstraintType["NONE"] = 0] = "NONE";
57
+ SANEConstraintType[SANEConstraintType["RANGE"] = 1] = "RANGE";
58
+ SANEConstraintType[SANEConstraintType["WORD_LIST"] = 2] = "WORD_LIST";
59
+ SANEConstraintType[SANEConstraintType["STRING_LIST"] = 3] = "STRING_LIST";
60
+ })(SANEConstraintType || (exports.SANEConstraintType = SANEConstraintType = {}));
61
+ /**
62
+ * Equivalent to the SANE API C enum `SANE_Frame`.
63
+ * @see {@link https://sane-project.gitlab.io/standard/1.06/api.html#sane-get-parameters}
64
+ */
65
+ var SANEFrame;
66
+ (function (SANEFrame) {
67
+ SANEFrame[SANEFrame["GRAY"] = 0] = "GRAY";
68
+ SANEFrame[SANEFrame["RGB"] = 1] = "RGB";
69
+ SANEFrame[SANEFrame["RED"] = 2] = "RED";
70
+ SANEFrame[SANEFrame["GREEN"] = 3] = "GREEN";
71
+ SANEFrame[SANEFrame["BLUE"] = 4] = "BLUE";
72
+ })(SANEFrame || (exports.SANEFrame = SANEFrame = {}));
73
+ /**
74
+ * Main LibSANE factory.
75
+ */
76
+ exports.libsane = require('../lib');
77
+ /**
78
+ * @deprecated The default export may be removed in the future.
79
+ * Use the 'libsane' named export.
80
+ */
81
+ exports.default = exports.libsane;
package/package.json CHANGED
@@ -1,16 +1,23 @@
1
1
  {
2
2
  "name": "sane-wasm",
3
- "version": "0.1.2",
3
+ "version": "0.3.0",
4
4
  "description": "A project to bring the SANE API to the web.",
5
- "main": "lib/index.js",
6
- "types": "libsane.d.ts",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
7
  "scripts": {
8
8
  "test": "jest",
9
- "clean": "./build.sh --with-docker --no-build --clean",
10
- "build": "./build.sh --with-docker --clean",
11
- "build-debug": "./build.sh --with-docker --debug --emrun"
9
+ "clean": "npm run clean:ts && npm run clean:sane",
10
+ "clean:ts": "rm -rf dist/ docs/",
11
+ "clean:sane": "./build.sh --no-build --clean",
12
+ "prebuild": "rm -rf dist/ docs/ build/",
13
+ "build": "npm run build:ts && npm run build:sane",
14
+ "build:ts": "tsc && typedoc",
15
+ "postbuild:ts": "rm -rf dist/docs-plugin.*",
16
+ "build:sane": "./build.sh --with-docker --clean",
17
+ "debug:sane": "./build.sh --with-docker --debug --emrun"
12
18
  },
13
19
  "files": [
20
+ "dist/",
14
21
  "lib/",
15
22
  "build/",
16
23
  "libsane.d.ts",
@@ -32,11 +39,18 @@
32
39
  "jest-extended/all"
33
40
  ]
34
41
  },
35
- "dependencies": {
42
+ "peerDependencies": {
36
43
  "usb": "^2.10.0"
37
44
  },
45
+ "peerDependenciesMeta": {
46
+ "usb": {
47
+ "optional": true
48
+ }
49
+ },
38
50
  "devDependencies": {
39
51
  "jest": "^29.6.4",
40
- "jest-extended": "^4.0.1"
52
+ "jest-extended": "^4.0.1",
53
+ "typedoc": "^0.25.0",
54
+ "typescript": "^5.2.2"
41
55
  }
42
56
  }
package/libsane.d.ts DELETED
@@ -1,163 +0,0 @@
1
- export enum SANEStatus {
2
- GOOD = 0,
3
- UNSUPPORTED,
4
- CANCELLED,
5
- DEVICE_BUSY,
6
- INVAL,
7
- EOF,
8
- JAMMED,
9
- NO_DOCS,
10
- COVER_OPEN,
11
- IO_ERROR,
12
- NO_MEM,
13
- ACCESS_DENIED,
14
- }
15
-
16
- export enum SANEValueType {
17
- BOOL = 0,
18
- INT,
19
- FIXED,
20
- STRING,
21
- BUTTON,
22
- GROUP,
23
- }
24
-
25
- export enum SANEUnit {
26
- NONE = 0,
27
- PIXEL,
28
- BIT,
29
- MM,
30
- DPI,
31
- PERCENT,
32
- MICROSECOND,
33
- }
34
-
35
- export enum SANEConstraintType {
36
- NONE = 0,
37
- RANGE,
38
- WORD_LIST,
39
- STRING_LIST,
40
- }
41
-
42
- export enum SANEFrame {
43
- GRAY = 0,
44
- RGB,
45
- RED,
46
- GREEN,
47
- BLUE,
48
- }
49
-
50
- export type SANEState = {
51
- initialized: boolean;
52
- version_code: number;
53
- version: {
54
- major: number;
55
- minor: number;
56
- build: number;
57
- },
58
- open: boolean;
59
- }
60
-
61
- export type SANEDevice = {
62
- name: string;
63
- vendor: string;
64
- model: string;
65
- type: string;
66
- }
67
-
68
- export type SANEOptionDescriptor = {
69
- name: string;
70
- title: string;
71
- desc: string;
72
- type: SANEValueType;
73
- unit: SANEUnit;
74
- size: number;
75
- cap: {
76
- SOFT_SELECT: boolean;
77
- HARD_SELECT: boolean;
78
- SOFT_DETECT: boolean;
79
- EMULATED: boolean;
80
- AUTOMATIC: boolean;
81
- INACTIVE: boolean;
82
- ADVANCED: boolean;
83
- };
84
- } & ({
85
- constraint_type: SANEConstraintType.NONE
86
- constraint: null
87
- } | {
88
- constraint_type: SANEConstraintType.RANGE
89
- constraint: {
90
- min: number;
91
- max: number;
92
- quant: number;
93
- }
94
- } | {
95
- constraint_type: SANEConstraintType.WORD_LIST
96
- constraint: number[]
97
- } | {
98
- constraint_type: SANEConstraintType.STRING_LIST
99
- constraint: string[]
100
- })
101
-
102
- export type SANEInfo = {
103
- INEXACT: boolean;
104
- RELOAD_OPTIONS: boolean;
105
- RELOAD_PARAMS: boolean;
106
- }
107
-
108
- export type SANEParameters = {
109
- format: SANEFrame;
110
- last_frame: boolean;
111
- bytes_per_line: number;
112
- pixels_per_line: number;
113
- lines: number;
114
- depth: number;
115
- }
116
-
117
- type SANEEnum<E, TE> = E & {
118
- asString: (value: TE) => string;
119
- }
120
-
121
- export interface LibSANE {
122
- SANE_WASM_COMMIT: string;
123
- SANE_WASM_VERSION: string;
124
- SANE_WASM_BACKENDS: string;
125
-
126
- SANE_CURRENT_MAJOR: number;
127
- SANE_CURRENT_MINOR: number;
128
-
129
- SANE_STATUS: SANEEnum<typeof SANEStatus, SANEStatus>;
130
- SANE_TYPE: SANEEnum<typeof SANEValueType, SANEValueType>;
131
- SANE_UNIT: SANEEnum<typeof SANEUnit, SANEUnit>;
132
- SANE_CONSTRAINT: SANEEnum<typeof SANEConstraintType, SANEConstraintType>;
133
- SANE_FRAME: SANEEnum<typeof SANEFrame, SANEFrame>;
134
-
135
- sane_get_state: () => SANEState;
136
- sane_init: () => { status: SANEStatus; version_code: number };
137
- sane_exit: () => Promise<{ status: SANEStatus; }>;
138
- sane_get_devices: () => Promise<{ status: SANEStatus; devices: SANEDevice[] }>;
139
- sane_open: (devicename: string) => Promise<{ status: SANEStatus; }>;
140
- sane_close: () => Promise<{ status: SANEStatus; }>;
141
- sane_get_option_descriptor: (option: number) => { status: SANEStatus; option_descriptor: SANEOptionDescriptor | null };
142
- sane_control_option_get_value: (option: number) => Promise<{ status: SANEStatus; value: any }>;
143
- sane_control_option_set_value: (option: number, value: any) => Promise<{ status: SANEStatus; info: SANEInfo }>;
144
- sane_control_option_set_auto: (option: number) => Promise<{ status: SANEStatus; info: SANEInfo }>;
145
- sane_get_parameters: () => { status: SANEStatus; parameters: SANEParameters };
146
- sane_start: () => { status: SANEStatus; };
147
- sane_read: () => Promise<{ status: SANEStatus; data: Uint8Array }>;
148
- sane_cancel: () => Promise<{ status: SANEStatus; }>;
149
- sane_strstatus: (status: SANEStatus) => string;
150
- }
151
-
152
- export type LibSANEFactory = (option?: {
153
- sane?: {
154
- debugSANE?: boolean;
155
- debugUSB?: boolean;
156
- debugFunctionCalls?: boolean;
157
- debugTestDevices?: number;
158
- promisify?: boolean;
159
- promisifyQueue?: boolean;
160
- }
161
- } & {
162
- [k: string]: any
163
- }) => Promise<LibSANE>