@nddeps/barcode-scanner 0.5.1 → 0.6.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 No Deprecated Dependencies
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,113 @@
1
- import { createBarcodeScanner } from './create-barcode-scanner';
2
- export * from './utils';
3
- export { createBarcodeScanner };
4
- export default createBarcodeScanner;
1
+ import { BarcodeDetector } from 'barcode-detector/ponyfill';
2
+ import { BarcodeDetectorOptions } from 'barcode-detector/ponyfill';
3
+ import { BarcodeFormat } from 'barcode-detector/ponyfill';
4
+ import { DetectedBarcode } from 'barcode-detector/ponyfill';
5
+
6
+ declare type Context = {
7
+ state: State;
8
+ };
9
+
10
+ declare function createBarcodeScanner(video: HTMLVideoElement, { debug, formats, getScanArea, handleDecodeFailure, handleDecodeSuccess, lifecycle, scanRate, }?: Options): Promise<{
11
+ decode: (imageData: ImageData) => Promise<DetectedBarcode | null>;
12
+ destroy: () => Promise<void>;
13
+ pause: () => Promise<void>;
14
+ start: ({ facingMode, ...rest }?: {
15
+ facingMode?: "environment" | "user";
16
+ handleDecodeFailure?: DecodeFailureHandler;
17
+ handleDecodeSuccess?: DecodeSuccessHandler;
18
+ }) => Promise<void>;
19
+ startCallbackProcessed: boolean;
20
+ state: State;
21
+ stop: () => Promise<void>;
22
+ }>;
23
+ export { createBarcodeScanner }
24
+ export default createBarcodeScanner;
25
+
26
+ export declare function createWorker({ formats }: {
27
+ formats: BarcodeFormat[];
28
+ }): {
29
+ decode: (imageData: ImageData) => Promise<DetectedBarcode | null>;
30
+ worker: Worker;
31
+ };
32
+
33
+ declare type DecodeFailureHandler = () => Promise<void> | void;
34
+
35
+ declare type DecodeSuccessHandler = (data: string, area: ScanArea) => Promise<void> | void;
36
+
37
+ export declare function getCameraAccess(): Promise<boolean>;
38
+
39
+ export declare function getScanArea(video: HTMLVideoElement): ScanArea;
40
+
41
+ export declare function getVideoRenderOffset(video: HTMLVideoElement, renderSize: RenderSize): RenderOffset;
42
+
43
+ export declare function getVideoRenderSize(video: HTMLVideoElement): RenderSize;
44
+
45
+ export declare function hasCameraAccess(): Promise<boolean>;
46
+
47
+ export declare function isBarcodeDetectorAvailable<T extends object>(value: T): value is {
48
+ BarcodeDetector: {
49
+ new (barcodeDetectorOptions?: BarcodeDetectorOptions): BarcodeDetector;
50
+ } & BarcodeDetector;
51
+ } & T;
52
+
53
+ declare type Lifecycle = {
54
+ onBeforeCreate?: LifecycleHook;
55
+ onBeforeDecode?: LifecycleHook;
56
+ onBeforePause?: LifecycleHook;
57
+ onBeforeStart?: LifecycleHook;
58
+ onBeforeStop?: LifecycleHook;
59
+ onCreate?: LifecycleHook;
60
+ onDecode?: LifecycleHook;
61
+ onPause?: LifecycleHook;
62
+ onStart?: LifecycleHook;
63
+ onStop?: LifecycleHook;
64
+ };
65
+
66
+ declare type LifecycleHook = (ctx: Context) => void;
67
+
68
+ declare type Options = {
69
+ debug?: boolean;
70
+ formats?: BarcodeFormat[];
71
+ getScanArea?: (video: HTMLVideoElement) => ScanArea;
72
+ handleDecodeFailure?: DecodeFailureHandler;
73
+ handleDecodeSuccess?: DecodeSuccessHandler;
74
+ lifecycle?: Lifecycle;
75
+ scanRate?: number;
76
+ };
77
+
78
+ export declare type RenderOffset = {
79
+ x: number;
80
+ y: number;
81
+ };
82
+
83
+ export declare type RenderSize = {
84
+ height: number;
85
+ width: number;
86
+ };
87
+
88
+ export declare type ScanArea = {
89
+ height: number;
90
+ width: number;
91
+ x: number;
92
+ y: number;
93
+ };
94
+
95
+ declare type State = {
96
+ decodeFrameTs: number;
97
+ isDecodeFrameProcessed: boolean;
98
+ isDestroyed: boolean;
99
+ isVideoActive: boolean;
100
+ isVideoPaused: boolean;
101
+ isWorkerLoadFailure: boolean;
102
+ scanArea: ScanArea;
103
+ scanRate: number;
104
+ video: HTMLVideoElement;
105
+ };
106
+
107
+ export declare function translateAreaToVideoRender(video: HTMLVideoElement, area: ScanArea): ScanArea;
108
+
109
+ export declare function translateAreaToVideoSource(video: HTMLVideoElement, area: ScanArea): ScanArea;
110
+
111
+ export declare function wait(ms: number): Promise<unknown>;
112
+
113
+ export { }
package/dist/index.js CHANGED
@@ -1,174 +1,176 @@
1
- const L = "decode-timeout", O = "worker-load-failure";
2
- const R = "worker-load-timeout", V = "" + new URL("zxing-reader.wasm", import.meta.url).href;
1
+ const k = "decode-timeout", R = "worker-load-failure";
2
+ const T = "worker-load-timeout", C = "" + new URL("zxing-reader.wasm", import.meta.url).href;
3
3
  function H({ formats: e }) {
4
- const o = new Worker(new URL(
4
+ const n = new Worker(new URL(
5
5
  /* @vite-ignore */
6
6
  "" + new URL("worker.js", import.meta.url).href,
7
7
  import.meta.url
8
- ), { type: "module" }), a = new Promise((i, s) => {
9
- const r = setTimeout(() => {
10
- o.removeEventListener("message", h), s(new Error(R));
11
- }, 32e3), h = ({ data: { payload: c, type: u } }) => {
12
- u === "init" && (clearTimeout(r), o.removeEventListener("message", h), c.status === "success" ? i(!0) : s(new Error(O)));
8
+ ), { type: "module" }), i = new Promise((a, r) => {
9
+ const o = setTimeout(() => {
10
+ n.removeEventListener("message", f), r(new Error(T));
11
+ }, 32e3), f = ({ data: { payload: c, type: m } }) => {
12
+ m === "init" && (clearTimeout(o), n.removeEventListener("message", f), c.status === "success" ? a(!0) : r(new Error(R)));
13
13
  };
14
- o.addEventListener("message", h), o.postMessage({
14
+ n.addEventListener("message", f), n.postMessage({
15
15
  payload: {
16
16
  formats: e,
17
- wasmUrl: V
17
+ wasmUrl: C
18
18
  },
19
19
  type: "config"
20
20
  });
21
21
  });
22
- async function n(i) {
23
- await a;
24
- const s = `${performance.now()}-${Math.random().toString(36).slice(2)}`;
25
- return new Promise((r, h) => {
22
+ async function s(a) {
23
+ await i;
24
+ const r = `${performance.now()}-${Math.random().toString(36).slice(2)}`;
25
+ return new Promise((o, f) => {
26
26
  const c = setTimeout(() => {
27
- o.removeEventListener("message", u), h(new Error(L));
28
- }, 16e3), u = ({ data: { payload: g, type: p } }) => {
29
- p !== "decode" || g.uuid !== s || (clearTimeout(c), o.removeEventListener("message", u), r(g.data));
27
+ n.removeEventListener("message", m), f(new Error(k));
28
+ }, 16e3), m = ({ data: { payload: g, type: p } }) => {
29
+ p !== "decode" || g.uuid !== r || (clearTimeout(c), n.removeEventListener("message", m), o(g.data));
30
30
  };
31
- o.addEventListener("message", u), o.postMessage({
31
+ n.addEventListener("message", m), n.postMessage({
32
32
  payload: {
33
- data: i,
34
- uuid: s
33
+ data: a,
34
+ uuid: r
35
35
  },
36
36
  type: "decode"
37
37
  });
38
38
  });
39
39
  }
40
40
  return {
41
- decode: n,
42
- worker: o
41
+ decode: s,
42
+ worker: n
43
43
  };
44
44
  }
45
- async function _() {
45
+ async function V() {
46
46
  try {
47
47
  return (await navigator.permissions.query({ name: "camera" })).state === "granted";
48
48
  } catch {
49
- return (await navigator.mediaDevices.enumerateDevices()).filter((a) => a.deviceId && a.kind === "videoinput").length > 0;
49
+ return (await navigator.mediaDevices.enumerateDevices()).filter((i) => i.deviceId && i.kind === "videoinput").length > 0;
50
50
  }
51
51
  }
52
- async function b() {
53
- if (await _())
52
+ async function _() {
53
+ if (await V())
54
54
  return !0;
55
55
  try {
56
- const o = (await navigator.mediaDevices.getUserMedia({ video: !0 })).getTracks();
57
- for (const a of o)
58
- a.stop();
56
+ const n = (await navigator.mediaDevices.getUserMedia({ video: !0 })).getTracks();
57
+ for (const i of n)
58
+ i.stop();
59
59
  return !0;
60
60
  } catch {
61
61
  return !1;
62
62
  }
63
63
  }
64
- function C(e) {
65
- const o = Math.round(0.6666666666666666 * Math.min(e.videoWidth, e.videoHeight));
64
+ function I(e) {
65
+ const n = Math.round(0.6666666666666666 * Math.min(e.videoWidth, e.videoHeight));
66
66
  return {
67
- height: o,
68
- width: o,
69
- x: Math.round((e.videoWidth - o) / 2),
70
- y: Math.round((e.videoHeight - o) / 2)
67
+ height: n,
68
+ width: n,
69
+ x: Math.round((e.videoWidth - n) / 2),
70
+ y: Math.round((e.videoHeight - n) / 2)
71
71
  };
72
72
  }
73
- function W(e, o) {
74
- const a = window.getComputedStyle(e), [n, i] = a.objectPosition.split(" ").map(
75
- (s, r) => s.endsWith("%") ? (r === 0 ? e.offsetWidth - o.width : e.offsetHeight - o.height) * parseFloat(s) / 100 : parseFloat(s)
73
+ function W(e, n) {
74
+ const i = window.getComputedStyle(e), [s, a] = i.objectPosition.split(" ").map(
75
+ (r, o) => r.endsWith("%") ? (o === 0 ? e.offsetWidth - n.width : e.offsetHeight - n.height) * parseFloat(r) / 100 : parseFloat(r)
76
76
  );
77
77
  return {
78
- x: n,
79
- y: i
78
+ x: s,
79
+ y: a
80
80
  };
81
81
  }
82
- function S(e) {
83
- const o = window.getComputedStyle(e), a = { height: e.offsetHeight, width: e.offsetWidth }, n = a.width / a.height, i = { height: e.videoHeight, width: e.videoWidth }, s = i.width / i.height;
84
- switch (o.objectFit) {
82
+ function b(e) {
83
+ const n = window.getComputedStyle(e), i = { height: e.offsetHeight, width: e.offsetWidth }, s = i.width / i.height, a = { height: e.videoHeight, width: e.videoWidth }, r = a.width / a.height;
84
+ switch (n.objectFit) {
85
85
  case "contain":
86
86
  return {
87
- height: s < n ? e.offsetHeight : e.offsetWidth / s,
88
- width: s < n ? e.offsetHeight * s : e.offsetWidth
87
+ height: r < s ? e.offsetHeight : e.offsetWidth / r,
88
+ width: r < s ? e.offsetHeight * r : e.offsetWidth
89
89
  };
90
90
  case "cover":
91
91
  return {
92
- height: s > n ? e.offsetHeight : e.offsetWidth / s,
93
- width: s > n ? e.offsetHeight * s : e.offsetWidth
92
+ height: r > s ? e.offsetHeight : e.offsetWidth / r,
93
+ width: r > s ? e.offsetHeight * r : e.offsetWidth
94
94
  };
95
95
  case "fill":
96
- return a;
97
- case "none":
98
96
  return i;
97
+ case "none":
98
+ return a;
99
99
  case "scale-down":
100
100
  return {
101
101
  height: Math.min(
102
- s < n ? e.offsetHeight : e.offsetWidth / s,
102
+ r < s ? e.offsetHeight : e.offsetWidth / r,
103
103
  e.videoHeight
104
104
  ),
105
105
  width: Math.min(
106
- s < n ? e.offsetHeight * s : e.offsetWidth,
106
+ r < s ? e.offsetHeight * r : e.offsetWidth,
107
107
  e.videoWidth
108
108
  )
109
109
  };
110
110
  default:
111
- return i;
111
+ return a;
112
112
  }
113
113
  }
114
114
  function U(e) {
115
115
  return "BarcodeDetector" in e;
116
116
  }
117
- function I(e, o) {
118
- const a = /scaleX\(-1\)/.test(e.style.transform), n = S(e), i = W(e, n), s = a ? e.videoWidth - o.x - o.width : o.x, r = o.y;
117
+ function B(e, n) {
118
+ const i = /scaleX\(-1\)/.test(e.style.transform), s = b(e), a = W(e, s), r = i ? e.videoWidth - n.x - n.width : n.x, o = n.y;
119
119
  return {
120
- height: o.height / e.videoHeight * n.height,
121
- width: o.width / e.videoWidth * n.width,
122
- x: s / e.videoWidth * n.width + i.x,
123
- y: r / e.videoHeight * n.height + i.y
120
+ height: n.height / e.videoHeight * s.height,
121
+ width: n.width / e.videoWidth * s.width,
122
+ x: r / e.videoWidth * s.width + a.x,
123
+ y: o / e.videoHeight * s.height + a.y
124
124
  };
125
125
  }
126
- function B(e, o) {
127
- const a = /scaleX\(-1\)/.test(e.style.transform), n = S(e), i = W(e, n), s = a ? n.width - (o.x - i.x) - o.width : o.x - i.x, r = o.y - i.y, h = e.videoHeight / n.height, c = e.videoWidth / n.width;
126
+ function j(e, n) {
127
+ const i = /scaleX\(-1\)/.test(e.style.transform), s = b(e), a = W(e, s), r = i ? s.width - (n.x - a.x) - n.width : n.x - a.x, o = n.y - a.y, f = e.videoHeight / s.height, c = e.videoWidth / s.width;
128
128
  return {
129
- height: o.height * h,
130
- width: o.width * c,
131
- x: s * c,
132
- y: r * h
129
+ height: n.height * f,
130
+ width: n.width * c,
131
+ x: r * c,
132
+ y: o * f
133
133
  };
134
134
  }
135
- function j(e) {
136
- return new Promise((o) => setTimeout(o, e));
135
+ function q(e) {
136
+ return new Promise((n) => setTimeout(n, e));
137
137
  }
138
- async function q(e, {
139
- debug: o,
140
- formats: a = ["qr_code"],
141
- getScanArea: n = C,
142
- handleDecodeFailure: i,
143
- handleDecodeSuccess: s,
144
- lifecycle: r = {},
145
- scanRate: h = 24
138
+ async function K(e, {
139
+ debug: n,
140
+ formats: i = ["qr_code"],
141
+ getScanArea: s = I,
142
+ handleDecodeFailure: a,
143
+ handleDecodeSuccess: r,
144
+ lifecycle: o = {},
145
+ scanRate: f = 24
146
146
  } = {}) {
147
147
  if (!(e instanceof HTMLVideoElement))
148
148
  throw new Error("video is not a HTMLVideoElement");
149
- if (!(s instanceof Function))
149
+ if (!(r instanceof Function))
150
150
  throw new Error("handleDecodeSuccess is not a function");
151
- if (!(i instanceof Function))
151
+ if (!(a instanceof Function))
152
152
  throw new Error("handleDecodeFailure is not a function");
153
- const c = document.createElement("canvas"), u = c.getContext("2d", { willReadFrequently: !0 });
154
- if (!u)
153
+ const c = document.createElement("canvas"), m = c.getContext("2d", { willReadFrequently: !0 });
154
+ if (!m)
155
155
  throw new Error("canvas context is not supported");
156
- const { decode: g, worker: p } = H({ formats: a }), t = {
156
+ const { decode: g, worker: p } = H({ formats: i }), t = {
157
157
  decodeFrameTs: performance.now(),
158
158
  isDecodeFrameProcessed: !1,
159
159
  isDestroyed: !1,
160
160
  isVideoActive: !1,
161
161
  isVideoPaused: !1,
162
162
  isWorkerLoadFailure: !1,
163
- scanArea: n(e),
164
- scanRate: h,
163
+ scanArea: s(e),
164
+ scanRate: f,
165
165
  video: e
166
- }, f = { state: t }, v = e.requestVideoFrameCallback?.bind(e) ?? requestAnimationFrame;
167
- t.video.autoplay = !0, t.video.disablePictureInPicture = !0, t.video.hidden = !1, t.video.muted = !0, t.video.playsInline = !0, r.onCreate && r.onCreate(f);
168
- function T(m, l = () => {
166
+ }, w = { state: t }, D = e.requestVideoFrameCallback?.bind(e) ?? requestAnimationFrame;
167
+ t.video.autoplay = !0, t.video.disablePictureInPicture = !0, t.video.hidden = !1, t.video.muted = !0, t.video.playsInline = !0, o.onCreate && o.onCreate(w);
168
+ let E = !1;
169
+ document.addEventListener("barcode-scanner:beforestart", () => E = !0), document.addEventListener("barcode-scanner:start", () => E = !1);
170
+ function S(d, l = () => {
169
171
  }) {
170
- v(w);
171
- async function w() {
172
+ D(u);
173
+ async function u() {
172
174
  if (t.isDestroyed || t.isVideoActive === !1)
173
175
  return;
174
176
  if (
@@ -177,10 +179,10 @@ async function q(e, {
177
179
  t.isDecodeFrameProcessed || // Skip if the video is not ready
178
180
  t.video.readyState <= 1
179
181
  ) {
180
- v(w);
182
+ D(u);
181
183
  return;
182
184
  }
183
- t.isDecodeFrameProcessed = !0, t.scanArea = n(t.video), r.onBeforeDecode && r.onBeforeDecode(f), c.height = t.scanArea.height, c.width = t.scanArea.width, u.clearRect(0, 0, c.width, c.height), u.drawImage(
185
+ t.isDecodeFrameProcessed = !0, t.scanArea = s(t.video), o.onBeforeDecode && o.onBeforeDecode(w), c.height = t.scanArea.height, c.width = t.scanArea.width, m.clearRect(0, 0, c.width, c.height), m.drawImage(
184
186
  t.video,
185
187
  t.scanArea.x,
186
188
  t.scanArea.y,
@@ -191,76 +193,92 @@ async function q(e, {
191
193
  c.width,
192
194
  c.height
193
195
  );
194
- const E = u.getImageData(0, 0, c.width, c.height);
195
- o && window.dispatchEvent(
196
+ const v = m.getImageData(0, 0, c.width, c.height);
197
+ n && window.dispatchEvent(
196
198
  new CustomEvent("barcode-scanner:decode-frame", {
197
199
  detail: {
198
- imageData: E
200
+ imageData: v
199
201
  }
200
202
  })
201
203
  );
202
204
  try {
203
- const d = await g(E);
204
- if (d) {
205
- const D = d.cornerPoints.map((y) => y.x), A = d.cornerPoints.map((y) => y.y), k = {
206
- height: Math.max(...A) - Math.min(...A),
207
- width: Math.max(...D) - Math.min(...D),
208
- x: Math.min(...D) + t.scanArea.x,
209
- y: Math.min(...A) + t.scanArea.y
205
+ const h = await g(v);
206
+ if (h) {
207
+ const A = h.cornerPoints.map((M) => M.x), y = h.cornerPoints.map((M) => M.y), x = {
208
+ height: Math.max(...y) - Math.min(...y),
209
+ width: Math.max(...A) - Math.min(...A),
210
+ x: Math.min(...A) + t.scanArea.x,
211
+ y: Math.min(...y) + t.scanArea.y
210
212
  };
211
- await Promise.resolve(m(d.rawValue, k));
213
+ await Promise.resolve(d(h.rawValue, x));
212
214
  } else
213
215
  await Promise.resolve(l());
214
- } catch (d) {
215
- console.warn("Failed to decode barcode"), d && (console.error(d), d instanceof Error && (d.cause === O || d.cause === R) && (t.isWorkerLoadFailure = !0));
216
+ } catch (h) {
217
+ console.warn("Failed to decode barcode"), h && (console.error(h), h instanceof Error && (h.cause === R || h.cause === T) && (t.isWorkerLoadFailure = !0));
216
218
  } finally {
217
- r.onDecode && r.onDecode(f), t.isWorkerLoadFailure === !1 && (t.isDecodeFrameProcessed = !1, t.decodeFrameTs = performance.now(), v(w));
219
+ o.onDecode && o.onDecode(w), t.isWorkerLoadFailure === !1 && (t.isDecodeFrameProcessed = !1, t.decodeFrameTs = performance.now(), D(u));
218
220
  }
219
221
  }
220
222
  }
221
- function F() {
222
- t.isDestroyed || (M(), p.terminate(), t.isDestroyed = !0);
223
- }
224
223
  async function P() {
225
- t.isVideoActive === !1 || t.isVideoPaused || t.isDestroyed || (r.onBeforePause && r.onBeforePause(f), t.video.srcObject instanceof MediaStream && (t.video.srcObject.getTracks().forEach((m) => m.stop()), t.video.srcObject = null), t.isVideoPaused = !0, r.onPause && r.onPause(f));
224
+ t.isDestroyed || (await O(), p.terminate(), t.isDestroyed = !0);
226
225
  }
227
- async function x({
228
- facingMode: m = "environment",
226
+ async function L() {
227
+ t.isVideoActive === !1 || t.isVideoPaused || t.isDestroyed || (E && await new Promise((d) => {
228
+ const l = setTimeout(() => {
229
+ document.removeEventListener("barcode-scanner:start", u), d(null);
230
+ }, 8e3), u = () => {
231
+ clearTimeout(l), document.removeEventListener("barcode-scanner:start", u), d(null);
232
+ };
233
+ document.addEventListener("barcode-scanner:start", u);
234
+ }), o.onBeforePause && o.onBeforePause(w), t.video.srcObject instanceof MediaStream && (t.video.srcObject.getTracks().forEach((d) => d.stop()), t.video.srcObject = null), t.isVideoPaused = !0, o.onPause && o.onPause(w));
235
+ }
236
+ async function F({
237
+ facingMode: d = "environment",
229
238
  ...l
230
239
  } = {}) {
231
- const w = l.handleDecodeSuccess ?? s, E = l.handleDecodeFailure ?? i;
232
- if (!w)
240
+ const u = l.handleDecodeSuccess ?? r, v = l.handleDecodeFailure ?? a;
241
+ if (!u)
233
242
  throw new Error("handleDecodeSuccess is required");
234
- if (r.onBeforeStart && r.onBeforeStart(f), !await b())
243
+ if (document.dispatchEvent(new CustomEvent("barcode-scanner:beforestart")), o.onBeforeStart && o.onBeforeStart(w), !await _())
235
244
  throw new Error("No camera access");
236
245
  t.video.srcObject instanceof MediaStream || (t.video.srcObject = await navigator.mediaDevices.getUserMedia({
237
246
  video: {
238
- facingMode: m
247
+ facingMode: d
239
248
  }
240
- }), await t.video.play(), t.isVideoActive = !0, t.isVideoPaused = !1, t.scanArea = n(t.video), t.video.style.transform = m === "user" ? "scaleX(-1)" : "none", r.onStart && r.onStart(f), T(w, E));
249
+ }), await t.video.play(), t.isVideoActive = !0, t.isVideoPaused = !1, t.scanArea = s(t.video), t.video.style.transform = d === "user" ? "scaleX(-1)" : "none", document.dispatchEvent(new CustomEvent("barcode-scanner:start")), o.onStart && o.onStart(w), S(u, v));
241
250
  }
242
- async function M() {
243
- t.isVideoActive === !1 || t.isDestroyed || (r.onBeforeStop && r.onBeforeStop(f), t.video.srcObject instanceof MediaStream && (t.video.srcObject.getTracks().forEach((m) => m.stop()), t.video.srcObject = null), t.isVideoActive = !1, t.isVideoPaused = !1, t.video.poster = "", r.onStop && r.onStop(f));
251
+ async function O() {
252
+ t.isDestroyed || (E && await new Promise((d) => {
253
+ const l = setTimeout(() => {
254
+ document.removeEventListener("barcode-scanner:start", u), d(null);
255
+ }, 8e3), u = () => {
256
+ clearTimeout(l), document.removeEventListener("barcode-scanner:start", u), d(null);
257
+ };
258
+ document.addEventListener("barcode-scanner:start", u);
259
+ }), o.onBeforeStop && o.onBeforeStop(w), t.video.srcObject instanceof MediaStream && (t.video.srcObject.getTracks().forEach((d) => d.stop()), t.video.srcObject = null), t.isVideoActive = !1, t.isVideoPaused = !1, t.video.poster = "", o.onStop && o.onStop(w));
244
260
  }
245
261
  return {
246
262
  decode: g,
247
- destroy: F,
248
- pause: P,
249
- start: x,
263
+ destroy: P,
264
+ pause: L,
265
+ start: F,
266
+ startCallbackProcessed: E,
250
267
  state: t,
251
- stop: M
268
+ stop: O
252
269
  };
253
270
  }
254
271
  export {
255
- q as createBarcodeScanner,
256
- q as default,
257
- b as getCameraAccess,
258
- C as getScanArea,
272
+ K as createBarcodeScanner,
273
+ H as createWorker,
274
+ K as default,
275
+ _ as getCameraAccess,
276
+ I as getScanArea,
259
277
  W as getVideoRenderOffset,
260
- S as getVideoRenderSize,
261
- _ as hasCameraAccess,
278
+ b as getVideoRenderSize,
279
+ V as hasCameraAccess,
262
280
  U as isBarcodeDetectorAvailable,
263
- I as translateAreaToVideoRender,
264
- B as translateAreaToVideoSource,
265
- j as wait
281
+ B as translateAreaToVideoRender,
282
+ j as translateAreaToVideoSource,
283
+ q as wait
266
284
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nddeps/barcode-scanner",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "private": false,
5
5
  "keywords": [
6
6
  "barcode",
@@ -16,13 +16,10 @@
16
16
  "author": "Aleksei Saenko",
17
17
  "type": "module",
18
18
  "exports": {
19
- ".": {
20
- "types": "./dist/index.d.ts",
21
- "import": "./dist/index.js"
22
- },
23
- "./worker": {
24
- "import": "./dist/worker.js"
25
- }
19
+ ".": "./dist/index.js",
20
+ "./package.json": "./package.json",
21
+ "./wasm": "./dist/zxing-reader.wasm",
22
+ "./worker": "./dist/worker.js"
26
23
  },
27
24
  "main": "./dist/index.js",
28
25
  "types": "./dist/index.d.ts",
@@ -30,7 +27,7 @@
30
27
  "./dist"
31
28
  ],
32
29
  "scripts": {
33
- "build": "vite build && tsc -b",
30
+ "build": "vite build",
34
31
  "dev": "vite",
35
32
  "preview": "vite preview"
36
33
  },
@@ -60,6 +57,7 @@
60
57
  "stylelint-order": "^7.0.1",
61
58
  "typescript": "~5.9.3",
62
59
  "typescript-eslint": "^8.54.0",
63
- "vite": "^7.3.1"
60
+ "vite": "^7.3.1",
61
+ "vite-plugin-dts": "^4.5.4"
64
62
  }
65
63
  }
@@ -1,7 +0,0 @@
1
- declare const WORKER_DECODE_TIMEOUT: number;
2
- declare const WORKER_DECODE_TIMEOUT_CAUSE = "decode-timeout";
3
- declare const WORKER_DECODE_FAILURE_CAUSE = "decode-failure";
4
- declare const WORKER_LOAD_FAILURE_CAUSE = "worker-load-failure";
5
- declare const WORKER_LOAD_TIMEOUT: number;
6
- declare const WORKER_LOAD_TIMEOUT_CAUSE = "worker-load-timeout";
7
- export { WORKER_DECODE_FAILURE_CAUSE, WORKER_DECODE_TIMEOUT, WORKER_DECODE_TIMEOUT_CAUSE, WORKER_LOAD_FAILURE_CAUSE, WORKER_LOAD_TIMEOUT, WORKER_LOAD_TIMEOUT_CAUSE, };
@@ -1,54 +0,0 @@
1
- import type { BarcodeFormat } from 'barcode-detector/ponyfill';
2
- import { type ScanArea } from './utils';
3
- type Context = {
4
- state: State;
5
- };
6
- type DecodeFailureHandler = () => Promise<void> | void;
7
- type DecodeSuccessHandler = (data: string, area: ScanArea) => Promise<void> | void;
8
- type Lifecycle = {
9
- onBeforeCreate?: LifecycleHook;
10
- onBeforeDecode?: LifecycleHook;
11
- onBeforePause?: LifecycleHook;
12
- onBeforeStart?: LifecycleHook;
13
- onBeforeStop?: LifecycleHook;
14
- onCreate?: LifecycleHook;
15
- onDecode?: LifecycleHook;
16
- onPause?: LifecycleHook;
17
- onStart?: LifecycleHook;
18
- onStop?: LifecycleHook;
19
- };
20
- type LifecycleHook = (ctx: Context) => void;
21
- type Options = {
22
- debug?: boolean;
23
- formats?: BarcodeFormat[];
24
- getScanArea?: (video: HTMLVideoElement) => ScanArea;
25
- handleDecodeFailure?: DecodeFailureHandler;
26
- handleDecodeSuccess?: DecodeSuccessHandler;
27
- lifecycle?: Lifecycle;
28
- scanRate?: number;
29
- };
30
- type State = {
31
- decodeFrameTs: number;
32
- isDecodeFrameProcessed: boolean;
33
- isDestroyed: boolean;
34
- isVideoActive: boolean;
35
- isVideoPaused: boolean;
36
- isWorkerLoadFailure: boolean;
37
- scanArea: ScanArea;
38
- scanRate: number;
39
- video: HTMLVideoElement;
40
- };
41
- declare function createBarcodeScanner(video: HTMLVideoElement, { debug, formats, getScanArea, handleDecodeFailure, handleDecodeSuccess, lifecycle, scanRate, }?: Options): Promise<{
42
- decode: (imageData: ImageData) => Promise<import("barcode-detector/ponyfill").DetectedBarcode | null>;
43
- destroy: () => void;
44
- pause: () => Promise<void>;
45
- start: ({ facingMode, ...rest }?: {
46
- facingMode?: "environment" | "user";
47
- handleDecodeFailure?: DecodeFailureHandler;
48
- handleDecodeSuccess?: DecodeSuccessHandler;
49
- }) => Promise<void>;
50
- state: State;
51
- stop: () => Promise<void>;
52
- }>;
53
- export type { DecodeFailureHandler, DecodeSuccessHandler, LifecycleHook, State };
54
- export { createBarcodeScanner };
@@ -1,8 +0,0 @@
1
- import type { BarcodeFormat, DetectedBarcode } from 'barcode-detector/ponyfill';
2
- declare function createWorker({ formats }: {
3
- formats: BarcodeFormat[];
4
- }): {
5
- decode: (imageData: ImageData) => Promise<DetectedBarcode | null>;
6
- worker: Worker;
7
- };
8
- export { createWorker };
@@ -1,2 +0,0 @@
1
- declare function getCameraAccess(): Promise<boolean>;
2
- export { getCameraAccess };
@@ -1,9 +0,0 @@
1
- type ScanArea = {
2
- height: number;
3
- width: number;
4
- x: number;
5
- y: number;
6
- };
7
- declare function getScanArea(video: HTMLVideoElement): ScanArea;
8
- export type { ScanArea };
9
- export { getScanArea };
@@ -1,8 +0,0 @@
1
- import type { RenderSize } from './get-video-render-size';
2
- type RenderOffset = {
3
- x: number;
4
- y: number;
5
- };
6
- declare function getVideoRenderOffset(video: HTMLVideoElement, renderSize: RenderSize): RenderOffset;
7
- export type { RenderOffset };
8
- export { getVideoRenderOffset };
@@ -1,7 +0,0 @@
1
- type RenderSize = {
2
- height: number;
3
- width: number;
4
- };
5
- declare function getVideoRenderSize(video: HTMLVideoElement): RenderSize;
6
- export type { RenderSize };
7
- export { getVideoRenderSize };
@@ -1,2 +0,0 @@
1
- declare function hasCameraAccess(): Promise<boolean>;
2
- export { hasCameraAccess };
@@ -1,9 +0,0 @@
1
- export * from './get-camera-access';
2
- export * from './get-scan-area';
3
- export * from './get-video-render-offset';
4
- export * from './get-video-render-size';
5
- export * from './has-camera-access';
6
- export * from './is-barcode-detector-available';
7
- export * from './translate-area-to-video-render';
8
- export * from './translate-area-to-video-source';
9
- export * from './wait';
@@ -1,7 +0,0 @@
1
- import type { BarcodeDetector, BarcodeDetectorOptions } from 'barcode-detector/ponyfill';
2
- declare function isBarcodeDetectorAvailable<T extends object>(value: T): value is {
3
- BarcodeDetector: {
4
- new (barcodeDetectorOptions?: BarcodeDetectorOptions): BarcodeDetector;
5
- } & BarcodeDetector;
6
- } & T;
7
- export { isBarcodeDetectorAvailable };
@@ -1,3 +0,0 @@
1
- import type { ScanArea } from './get-scan-area';
2
- declare function translateAreaToVideoRender(video: HTMLVideoElement, area: ScanArea): ScanArea;
3
- export { translateAreaToVideoRender };
@@ -1,3 +0,0 @@
1
- import type { ScanArea } from './get-scan-area';
2
- declare function translateAreaToVideoSource(video: HTMLVideoElement, area: ScanArea): ScanArea;
3
- export { translateAreaToVideoSource };
@@ -1,2 +0,0 @@
1
- declare function wait(ms: number): Promise<unknown>;
2
- export { wait };
@@ -1,29 +0,0 @@
1
- import type { BarcodeFormat, DetectedBarcode } from 'barcode-detector/ponyfill';
2
- type Config = {
3
- payload: {
4
- formats: BarcodeFormat[];
5
- wasmUrl: string;
6
- };
7
- type: 'config';
8
- };
9
- type DecodeRequest = {
10
- payload: {
11
- data: ImageData;
12
- uuid: string;
13
- };
14
- type: 'decode';
15
- };
16
- type DecodeResponse = {
17
- payload: {
18
- data: DetectedBarcode | null;
19
- uuid: string;
20
- };
21
- type: 'decode';
22
- };
23
- type Init = {
24
- payload: {
25
- status: 'failure' | 'success';
26
- };
27
- type: 'init';
28
- };
29
- export type { Config, DecodeRequest, DecodeResponse, Init };