@rosepetal/barcode-engine-client 0.2.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/LICENSE +202 -0
- package/README.md +209 -0
- package/lib/engine.js +684 -0
- package/lib/format.js +22 -0
- package/lib/framing.js +174 -0
- package/lib/index.js +18 -0
- package/package.json +32 -0
- package/test/fake-engine.js +292 -0
- package/test/fixtures/analyze-pass.json +1 -0
- package/test/fixtures/verify-fail.json +1 -0
- package/test/fixtures/verify-not-detected.json +1 -0
- package/test/fixtures/verify-pass.json +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
# @rosepetal/barcode-engine-client
|
|
2
|
+
|
|
3
|
+
Node client of `rp-barcode serve`, the persistent decode and verify service of
|
|
4
|
+
[rosepetal-barcode-sdk](https://github.com/rosepetal-ai/rosepetal-barcode-sdk) (Go, static binary). It spawns the
|
|
5
|
+
binary once, talks protocol 1 over its stdin/stdout (length-prefixed frames, see below), keeps one request timer per
|
|
6
|
+
request (`decode`, `verify`, `analyze`), bounds what is in flight, restarts a dead engine with a growing wait and
|
|
7
|
+
stops it cleanly. CommonJS, no runtime dependencies, Node >= 18.
|
|
8
|
+
|
|
9
|
+
It exists as a package because `node-red-contrib-barcode-reader` and `node-red-contrib-barcode-verifier` share the same
|
|
10
|
+
binary and must share **one engine process per Node-RED runtime**: both `require` this module and `acquire()` its
|
|
11
|
+
singleton, so the process is started by the first decode or verify and stopped by the last `release()`.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm i @rosepetal/barcode-engine-client
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The binary is not in this package. It comes from one of the platform packages, declared here as optional
|
|
20
|
+
dependencies and installed next to the client when the registry that carries them is configured for the
|
|
21
|
+
`@rosepetal` scope (they are published by the SDK's release workflow, `0.2.x` = the SDK version):
|
|
22
|
+
|
|
23
|
+
- `@rosepetal/barcode-engine-linux-x64`
|
|
24
|
+
- `@rosepetal/barcode-engine-linux-arm64`
|
|
25
|
+
|
|
26
|
+
each with the static binary at `bin/rp-barcode` (musl uses the same one). Without them, `RP_BARCODE_ENGINE` points at
|
|
27
|
+
an `rp-barcode` binary (SDK >= 0.2.0), or `rp-barcode` is looked up in `PATH`. `resolveBinary()` tries in that order:
|
|
28
|
+
|
|
29
|
+
1. `RP_BARCODE_ENGINE` (explicit: when set and unusable the start fails, it never falls back),
|
|
30
|
+
2. the platform package for `<platform>-<arch>` (`${process.platform}-${process.arch}`: `linux-x64`, `linux-arm64`),
|
|
31
|
+
along this module's `node_modules` chain,
|
|
32
|
+
3. `rp-barcode` in `PATH`,
|
|
33
|
+
|
|
34
|
+
and throws `EngineError` `unavailable` listing every place it tried. The binary runs as `<path> serve`.
|
|
35
|
+
|
|
36
|
+
## API
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
const rp = require('@rosepetal/barcode-engine-client');
|
|
40
|
+
|
|
41
|
+
// One shared engine per process (started lazily by the first decode or verify)
|
|
42
|
+
const engine = rp.acquire(); // refs + 1, returns the singleton (rp.getEngine() returns it without counting)
|
|
43
|
+
rp.setEngineOptions({ defaultTimeoutMs: 8000 }); // merges into the singleton's options (see the table below)
|
|
44
|
+
|
|
45
|
+
const image = { width: 640, height: 480, channels: 1, colorSpace: 'GRAY' }; // encoding defaults to "raw"
|
|
46
|
+
const { symbols, image: size, timing } = await engine.decode(image, pixels, { symbologies: ['EAN13'], effort: 'robust' },
|
|
47
|
+
{ timeoutMs: 5000 });
|
|
48
|
+
// symbols: schema-1.0 symbol objects as the SDK writes them ([] when none); size: {width, height}; timing: {totalMs, decodeMs, queuedMs}
|
|
49
|
+
|
|
50
|
+
const { result } = await engine.verify(image, pixels, { region: { x: 120, y: 80, w: 400, h: 200, orientation: 0 },
|
|
51
|
+
options: { spec: { kind: 'gs1_table', table: '1' } },
|
|
52
|
+
acquisition: { milsPerPixel: 3.25 } }, { timeoutMs: 10000 });
|
|
53
|
+
// result: the schema-1.0 VerifyResult as the SDK writes it (status, reasons, symbol, quality, dimensions, gs1, report, …)
|
|
54
|
+
|
|
55
|
+
await rp.release(); // refs - 1; the last release() stops the process
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
After its `release()`, a holder must not call `decode()`/`verify()`/`ping()` on the engine: a request after the last
|
|
59
|
+
release starts a process that no release will stop (it lives until the runtime exits). The reader node guards this
|
|
60
|
+
with its `rpClosed` flag; the verifier needs the same rule.
|
|
61
|
+
|
|
62
|
+
`engine.decode(image, payload, options, { timeoutMs })`: `image` is `{width, height, channels, colorSpace, encoding?}`
|
|
63
|
+
(`encoding` `raw`, payload = packed rows of `width·height·channels` bytes, `colorSpace` `GRAY|RGB|BGR|RGBA|BGRA`; or
|
|
64
|
+
`encoded`, payload = a JPEG/PNG/BMP/TIFF file); `options` is the `decodeOptions` object of the SDK's result schema 1.0
|
|
65
|
+
§12.1 (known keys only, every key optional, the server rejects unknown ones); `timeoutMs` (default
|
|
66
|
+
`defaultTimeoutMs`) is the client's timer and also travels as `deadlineMs` (sent as an integer number of milliseconds,
|
|
67
|
+
rounded up; the timer is capped at 2^31 − 1 ms). A payload larger than the engine's advertised `limits.maxPayload` is
|
|
68
|
+
refused with `invalid_input` before anything is sent.
|
|
69
|
+
|
|
70
|
+
`engine.verify(image, payload, { region, options, acquisition, analyze }, { timeoutMs })` (SDK service 2D): the
|
|
71
|
+
ISO/IEC 15416 verification of the symbol in `region`. `image`/`payload` as `decode`; `region` is
|
|
72
|
+
`{x, y, w, h, orientation}` in image pixels: `w`/`h` are extents along the image axes whatever the orientation,
|
|
73
|
+
`orientation` is the reading direction in clockwise degrees (a multiple of 90, default 0). The client checks and
|
|
74
|
+
rounds the region before sending (`Math.round` on `x`, `y`, `w`, `h`; the orientation never rounded, an integer
|
|
75
|
+
multiple of 90 normalised to 0/90/180/270, so −90 is 270 and 450 is 90; `w` and `h` at least 2 px after rounding; a
|
|
76
|
+
missing or non-finite coordinate, an orientation that is not an integer multiple of 90 (`90.4` included) or an
|
|
77
|
+
unknown key is `invalid_input` without anything sent), so a
|
|
78
|
+
normalised region scaled to pixels never reaches the server as a fraction; the bounds against the image are the
|
|
79
|
+
server's check (`barcode: region {…} outside image WxH`). `options` is the `verifyOptions` object of the schema
|
|
80
|
+
(§12.2, known keys only, every key optional; the server refuses unknown keys and values outside their vocabularies)
|
|
81
|
+
and `acquisition` the `acquisition` object (§12.3, `calibration` §12.4) or `null` (no physical scale, 660 nm, no
|
|
82
|
+
calibration); `analyze: true` sends `analyze` and the result carries `scans`. `timeoutMs` as `decode` (the verifier
|
|
83
|
+
node passes its own 10 s). Resolves `{ result, image, timing }`: `result` is the `VerifyResult` (or
|
|
84
|
+
`AnalysisResult`) exactly as the SDK writes it, `report` included; `status: "not_detected"` is a result, not an
|
|
85
|
+
error; `image` the real size; `timing` is `{totalMs, verifyMs, queuedMs}`. An engine whose hello does not list the op
|
|
86
|
+
(an SDK 0.2.0 built before 2D) rejects with `unsupported` before anything is sent.
|
|
87
|
+
|
|
88
|
+
Other members of an `Engine`: `start()` (resolves with the hello; idempotent, never two processes), `ping()` (a
|
|
89
|
+
liveness probe the server answers without queueing), `stop()` (drain → `shutdown` → SIGTERM → SIGKILL, resolves when
|
|
90
|
+
the child is gone; what was pending rejects with `exited`), `running`, `hello`, `pid`, `refs`, `lastError`,
|
|
91
|
+
`configure(options)`.
|
|
92
|
+
|
|
93
|
+
`new Engine(options)` and `setEngineOptions(options)` take (defaults in `DEFAULTS`):
|
|
94
|
+
|
|
95
|
+
| Option | Default | Meaning |
|
|
96
|
+
|---|---|---|
|
|
97
|
+
| `command`, `args` | `null`, `['serve']` | `null` resolves the binary at every start (`RP_BARCODE_ENGINE` may change) |
|
|
98
|
+
| `env` | `null` (= `process.env`) | environment of the child |
|
|
99
|
+
| `maxQueue` | 128 | requests in flight before `overloaded` |
|
|
100
|
+
| `maxBufferedBytes` | 256 MiB | bytes still unwritten to the engine's stdin before `overloaded` (an idle pipe always takes one frame) |
|
|
101
|
+
| `helloTimeoutMs` | 5000 | wait for the hello after the spawn |
|
|
102
|
+
| `defaultTimeoutMs` | 5000 | `timeoutMs` of `decode`/`verify`/`ping` when not given (the verifier node passes its own 10 s) |
|
|
103
|
+
| `backoff` | `{initialMs: 1000, maxMs: 30000}` | wait before restarting after a failed start or a crash, doubled each time, reset by a good hello |
|
|
104
|
+
| `drainMs`, `exitMs` | 2000, 1000 | `stop()`: wait for the requests in flight; wait after `shutdown`, then after SIGTERM |
|
|
105
|
+
|
|
106
|
+
Every failure is an `EngineError` with a `code`:
|
|
107
|
+
|
|
108
|
+
| `code` | From | When |
|
|
109
|
+
|---|---|---|
|
|
110
|
+
| `unavailable` | client | no binary, no hello in time, the spawn failed, or a start inside the wait after a failure (`retry in N ms`) |
|
|
111
|
+
| `protocol` | client | hello with another `protocol`, a bad frame from the engine, a reply that carries a payload, a `verify` reply without a `result` object, an unprompted `bad_frame` |
|
|
112
|
+
| `exited` | client | the process went away with the request in flight, `stop()`, or a `stop()` that landed while the request waited for a broken child to die (the engine is not relaunched for a holder that let go) |
|
|
113
|
+
| `timeout` | client | the request timer |
|
|
114
|
+
| `overloaded` | client or server | `maxQueue` / `maxBufferedBytes` reached, or the server's queue is full |
|
|
115
|
+
| `invalid_input` | client or server | client: `image` is not an object, `payload` is not a Buffer/Uint8Array, the `region` of a `verify` is not an object, has an unknown key, a missing or non-finite coordinate, a side under 2 px or an orientation that is not a multiple of 90, the payload exceeds the engine's `limits.maxPayload` (all refused before anything is sent) or the header cannot be framed; server: verbatim (a bad `image`, a payload of another length, an unknown `options`/`acquisition` key or a value outside its vocabulary, a region outside the image), the engine keeps running |
|
|
116
|
+
| `unsupported` | client or server | client: the hello does not list `verify`/`analyze` (refused before anything is sent); server: verbatim, the engine keeps running |
|
|
117
|
+
| `deadline`, `internal` | server | verbatim, the engine keeps running |
|
|
118
|
+
|
|
119
|
+
Events of an `Engine` (never `'error'`): `'started'` `({pid, hello, source})` after each good hello (`source` is
|
|
120
|
+
`RP_BARCODE_ENGINE`, the package name, `PATH` or `command`), `'exit'` `({code, signal})` when the child is gone,
|
|
121
|
+
`'stderr'` `(text)` with the engine's log lines.
|
|
122
|
+
|
|
123
|
+
Also exported: `resolveBinary({paths})`, `ENGINE_PACKAGES`, `PROTOCOL` (1), `DEFAULT_TIMEOUT_MS`, `INSTALL_HINT` (the
|
|
124
|
+
sentence a node prints next to an `unavailable`), the frame codec `encodeFrame`, `FrameParser`, `FramingError`, and
|
|
125
|
+
`formatName(symbol)` / `FORMAT_NAMES` / `ADDON_NAMES`: the `format` string the nodes print for a schema-1.0 symbol
|
|
126
|
+
(ZXing's spelling, `EAN-2`/`EAN-5` for the add-ons by their `identifier`), so the reader and the verifier agree.
|
|
127
|
+
|
|
128
|
+
## Protocol 1 in short
|
|
129
|
+
|
|
130
|
+
Specified in the SDK's [`docs/service.md`](https://github.com/rosepetal-ai/rosepetal-barcode-sdk/blob/dev/docs/service.md)
|
|
131
|
+
(a private repository: the link works for the organisation's members); this client implements it as written.
|
|
132
|
+
|
|
133
|
+
- Everything on stdin/stdout is a frame: `u32 BE headerLen | u32 BE payloadLen | header (UTF-8 JSON object) | payload`.
|
|
134
|
+
`headerLen` 2..1 MiB, `payloadLen` <= `maxPayload` (256 MiB by default, `--max-payload`).
|
|
135
|
+
- The server writes a `hello` first (`id` 0): `protocol`, `sdk`, `engineVersion`, `schema`, `ops`, `workers`, `queue`,
|
|
136
|
+
`limits {maxHeader, maxPayload, maxSide, maxPixels}`. The client requires `protocol === 1`.
|
|
137
|
+
- `decode` (`id`, `image`, `options`, `deadlineMs`) with the pixels as payload → `{id, ok: true, image, symbols, timing}`,
|
|
138
|
+
never with a payload. `ping` → `{id, ok: true}`, answered without queueing. `shutdown` → `{id, ok: true}`; what is in
|
|
139
|
+
flight finishes, what is still queued is answered `unsupported`, then exit 0.
|
|
140
|
+
- `verify`/`analyze` (`id`, `image`, `region`, `options`, `acquisition`, `deadlineMs`) with the pixels as payload →
|
|
141
|
+
`{id, ok: true, image, result, timing}`: `result` the schema-1.0 `VerifyResult`/`AnalysisResult` (`report`
|
|
142
|
+
included; `not_detected` is a result), `timing` `{totalMs, verifyMs, queuedMs}`. Since SDK 0.2.0 with 2D: the hello
|
|
143
|
+
lists them in `ops` (a 2A engine answers `unsupported`; the client checks `ops` first).
|
|
144
|
+
- Errors: `{id, ok: false, error: {code, message}}` with `invalid_input`, `unsupported`, `overloaded`, `deadline`,
|
|
145
|
+
`internal` (the server keeps running) or `bad_frame` (`id` 0, then the server exits 3: the stream is no longer
|
|
146
|
+
delimited, the client restarts it).
|
|
147
|
+
- EOF on stdin: the server decodes what is in flight **and what is already queued** (a queued request whose
|
|
148
|
+
`deadlineMs` expired answers `deadline`), then exits 0; a broken stdout is exit 1.
|
|
149
|
+
- Replies come out in the order they finish; `deadlineMs` only rejects a request still queued when it expires.
|
|
150
|
+
|
|
151
|
+
## Testing with the fake engine
|
|
152
|
+
|
|
153
|
+
`FAKE_ENGINE` is the path of `test/fake-engine.js`, a fake `rp-barcode serve` in Node (protocol 1, one fixed EAN-13
|
|
154
|
+
per decode, canned `VerifyResult`s for `verify`/`analyze`) that ships with the package so the tests of the nodes
|
|
155
|
+
never need a real binary:
|
|
156
|
+
|
|
157
|
+
```js
|
|
158
|
+
const rp = require('@rosepetal/barcode-engine-client');
|
|
159
|
+
process.env.RP_BARCODE_ENGINE = rp.FAKE_ENGINE; // the shared engine spawns the fake (executable, #!/usr/bin/env node)
|
|
160
|
+
rp.setEngineOptions({ helloTimeoutMs: 3000, backoff: { initialMs: 50, maxMs: 200 }, drainMs: 500, exitMs: 300 });
|
|
161
|
+
// or, for an engine of your own: new Engine({ command: process.execPath, args: [rp.FAKE_ENGINE, 'serve'], env: { ...process.env, FAKE_DELAY_MS: '300' } })
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Its behaviour is driven by the child's environment: `FAKE_DELAY_MS`, `FAKE_CRASH_AFTER`, `FAKE_PROTOCOL`,
|
|
165
|
+
`FAKE_GARBAGE`, `FAKE_OVERLOAD`, `FAKE_NO_HELLO`, `FAKE_IGNORE_STOP`, `FAKE_MAX_PAYLOAD`, `FAKE_REPLY_PAYLOAD`,
|
|
166
|
+
`FAKE_STALL`, and for the verifier `FAKE_VERIFY_STATUS=pass|fail|not_detected` (the canned result of
|
|
167
|
+
`verify`/`analyze`, default `pass`), `FAKE_DECODE_SYMBOLS=n` (copies of the EAN-13 a `decode` answers, default 1,
|
|
168
|
+
`0` = none) and `FAKE_OPS=a,b,c` (the ops the hello announces: `FAKE_OPS=decode,ping,shutdown` is a 2A engine
|
|
169
|
+
without `verify`), all documented at the top of the file. `verify`/`analyze` answer the canned results of
|
|
170
|
+
`test/fixtures/*.json` (`verify-pass.json`, `verify-fail.json`, `verify-not-detected.json`, `analyze-pass.json`:
|
|
171
|
+
what the CLI printed for synthetic renders, generated by `test/tools/make-verify-fixtures.sh` from the SDK
|
|
172
|
+
checkout, shipped in the package so a consumer's tests can `require('@rosepetal/barcode-engine-client/test/fixtures/verify-pass.json')`),
|
|
173
|
+
with the request's region echoed in `result.symbol.region`; `analyze` under `fail`/`not_detected` is the verify
|
|
174
|
+
fixture plus `scans: []`. Like the real server it takes `encoding` `raw` (`payload.length === width·height·channels`)
|
|
175
|
+
and `encoded` (a non-empty PNG/JPEG/BMP/TIFF file told by its magic bytes; the size answered is the PNG's IHDR,
|
|
176
|
+
else the header's `width`/`height`; with no size at all the bounds check is skipped), rejects unknown keys inside
|
|
177
|
+
`image`, `region`, `options` and `acquisition`, a region under 2 px, off a multiple of 90 or outside the image, and a
|
|
178
|
+
non-integer `deadlineMs` with `invalid_input` and the server's wording (`options: json: unknown field "scans"`);
|
|
179
|
+
unlike it, it matches keys case-sensitively (the binary's `encoding/json` also takes `TryInvert` for `tryInvert`),
|
|
180
|
+
does not check option values against their vocabularies (`edition: "2020"` passes the fake, the server refuses it)
|
|
181
|
+
nor that `options`/`acquisition` (and their `spec`/`calibration`) are objects.
|
|
182
|
+
|
|
183
|
+
## Development
|
|
184
|
+
|
|
185
|
+
```sh
|
|
186
|
+
cd clients/node
|
|
187
|
+
npm test # node --test: framing (10) + engine (32, verify/analyze included) on the fake engine, no child left behind
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
To regenerate the canned results after a change of the SDK's verification (never by `npm test`):
|
|
191
|
+
|
|
192
|
+
```sh
|
|
193
|
+
make build GO=/usr/local/go/bin/go # from the SDK root: bin/rp-barcode
|
|
194
|
+
cd clients/node && RP_BARCODE=../../bin/rp-barcode sh test/tools/make-verify-fixtures.sh
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
No lockfile: the package has zero dependencies, so there is nothing to install and nothing to `npm ci`; the tests need
|
|
198
|
+
only Node's built-ins and the package's own files. The optional platform packages are resolved by each consumer's own
|
|
199
|
+
install (its lock), never through a lock of this package.
|
|
200
|
+
|
|
201
|
+
CI runs `npm test` in the `node-client` job of `.github/workflows/ci.yml` (Node 22, no install step). The Go targets
|
|
202
|
+
of the repository (`make lint`, `make test`) do not touch this directory.
|
|
203
|
+
|
|
204
|
+
The SDK's release workflow (`.github/workflows/publish-engine.yml`, on every `v*` tag) publishes this package to
|
|
205
|
+
npmjs (`npm test`, then `npm publish --access public` with `NPM_TOKEN`; a prerelease tag goes to the dist-tag of its
|
|
206
|
+
preid) in the same run that publishes the platform packages to the private registry, and installs both in clean
|
|
207
|
+
containers afterwards (`scripts/verify-published.sh`). Its version is pinned to the SDK's by `TestNpmClientVersion`.
|
|
208
|
+
Until the first tag (`v0.2.0`, `docs/release-runbook.md`) install it from a checkout
|
|
209
|
+
(`npm i /path/to/rosepetal-barcode-sdk/clients/node`).
|