qrcode-reader-vue3 3.1.3 → 4.1.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.
Files changed (71) hide show
  1. package/.browserslistrc +3 -3
  2. package/.editorconfig +18 -18
  3. package/.github/ISSUE_TEMPLATE/bug_report.md +28 -28
  4. package/.github/copilot-instructions.md +209 -0
  5. package/.github/workflows/npm-publish.yml +15 -15
  6. package/.releaserc.yml +4 -4
  7. package/.travis.yml +26 -26
  8. package/CONTRIBUTING.md +23 -23
  9. package/LICENSE +21 -21
  10. package/README.md +194 -194
  11. package/babel.config.js +3 -3
  12. package/dist/QrcodeReaderVue3.common.js +15988 -13327
  13. package/dist/QrcodeReaderVue3.common.js.map +1 -1
  14. package/dist/QrcodeReaderVue3.umd.js +16152 -13482
  15. package/dist/QrcodeReaderVue3.umd.js.map +1 -1
  16. package/dist/QrcodeReaderVue3.umd.min.js +3 -1
  17. package/dist/QrcodeReaderVue3.umd.min.js.map +1 -1
  18. package/dist/demo.html +1 -8
  19. package/docs/.vuepress/components/DemoWrapper.vue +54 -54
  20. package/docs/.vuepress/components/demos/CustomTracking.vue +3 -2
  21. package/docs/.vuepress/components/demos/DecodeAll.vue +58 -58
  22. package/docs/.vuepress/components/demos/DragDrop.vue +80 -80
  23. package/docs/.vuepress/components/demos/Fullscreen.vue +115 -115
  24. package/docs/.vuepress/components/demos/LoadingIndicator.vue +61 -61
  25. package/docs/.vuepress/components/demos/ScanSameQrcodeMoreThanOnce.vue +78 -78
  26. package/docs/.vuepress/components/demos/SwitchCamera.vue +82 -82
  27. package/docs/.vuepress/components/demos/Torch.vue +64 -64
  28. package/docs/.vuepress/components/demos/Upload.vue +28 -28
  29. package/docs/.vuepress/components/demos/Validate.vue +118 -118
  30. package/docs/.vuepress/config.js +41 -41
  31. package/docs/.vuepress/public/checkmark.svg +1 -1
  32. package/docs/.vuepress/public/debug-memory-leak.html +102 -102
  33. package/docs/README.md +7 -7
  34. package/docs/api/QrcodeCapture.md +95 -95
  35. package/docs/api/QrcodeDropZone.md +117 -117
  36. package/docs/api/QrcodeStream.md +298 -298
  37. package/docs/demos/CustomTracking.md +13 -13
  38. package/docs/demos/DecodeAll.md +12 -12
  39. package/docs/demos/DragDrop.md +13 -13
  40. package/docs/demos/Fullscreen.md +13 -13
  41. package/docs/demos/LoadingIndicator.md +14 -14
  42. package/docs/demos/ScanSameQrcodeMoreThanOnce.md +17 -17
  43. package/docs/demos/Simple.md +14 -14
  44. package/docs/demos/SwitchCamera.md +12 -12
  45. package/docs/demos/Torch.md +15 -15
  46. package/docs/demos/Upload.md +16 -16
  47. package/docs/demos/Validate.md +13 -13
  48. package/eslint.config.js +49 -0
  49. package/package.json +33 -23
  50. package/src/components/QrcodeCapture.vue +31 -30
  51. package/src/components/QrcodeDropZone.vue +42 -42
  52. package/src/components/QrcodeStream.vue +28 -31
  53. package/src/index.ts +44 -0
  54. package/src/misc/{camera.js → camera.ts} +79 -21
  55. package/src/misc/{errors.js → errors.ts} +25 -25
  56. package/src/misc/scanner.ts +149 -0
  57. package/src/misc/{shimGetUserMedia.js → shimGetUserMedia.ts} +28 -28
  58. package/src/misc/util.ts +35 -0
  59. package/src/mixins/CommonAPI.vue +9 -4
  60. package/src/shims-vue.d.ts +9 -0
  61. package/src/types/index.d.ts +43 -0
  62. package/src/types/vue-qrcode-reader.d.ts +32 -4
  63. package/src/types/webrtc-adapter.d.ts +25 -0
  64. package/tests/misc/util.test.ts +72 -0
  65. package/tsconfig.json +35 -0
  66. package/vitest.config.ts +10 -0
  67. package/vue.config.js +30 -7
  68. package/.eslintrc.js +0 -25
  69. package/src/index.js +0 -32
  70. package/src/misc/scanner.js +0 -112
  71. package/src/misc/util.js +0 -15
@@ -0,0 +1,43 @@
1
+ import type { App, Plugin, DefineComponent } from 'vue';
2
+ import type { DetectedBarcode } from 'barcode-detector';
3
+
4
+ // QR Code result types
5
+ export interface QRCodeLocation {
6
+ topLeftCorner: { x: number; y: number };
7
+ topRightCorner: { x: number; y: number };
8
+ bottomRightCorner: { x: number; y: number };
9
+ bottomLeftCorner: { x: number; y: number };
10
+ topLeftFinderPattern: Record<string, never>;
11
+ topRightFinderPattern: Record<string, never>;
12
+ bottomLeftFinderPattern: Record<string, never>;
13
+ }
14
+
15
+ export interface QRCodeResult {
16
+ content: string | null;
17
+ location: QRCodeLocation | null;
18
+ imageData: ImageData | null;
19
+ binaryData: Uint8ClampedArray | null;
20
+ }
21
+
22
+ // Component prop types
23
+ export interface QrcodeStreamProps {
24
+ camera?: 'auto' | 'rear' | 'front' | 'off';
25
+ torch?: boolean;
26
+ track?: (detectedCodes: DetectedBarcode[], ctx: CanvasRenderingContext2D | null) => void;
27
+ }
28
+
29
+ export interface QrcodeCaptureProps {}
30
+
31
+ export interface QrcodeDropZoneProps {}
32
+
33
+ // Component types
34
+ export const QrcodeStream: DefineComponent<QrcodeStreamProps>;
35
+ export const QrcodeCapture: DefineComponent<QrcodeCaptureProps>;
36
+ export const QrcodeDropZone: DefineComponent<QrcodeDropZoneProps>;
37
+
38
+ // Plugin install function
39
+ export function install(app: App): void;
40
+
41
+ // Default export
42
+ declare const plugin: Plugin;
43
+ export default plugin;
@@ -1,4 +1,32 @@
1
- declare module "qrcode-reader-vue3";
2
- declare module 'QrcodeCapture';
3
- declare module 'QrcodeDropZone';
4
- declare module 'QrcodeStream';
1
+ // Type definitions for qrcode-reader-vue3
2
+ // Project: https://github.com/scholtz/qrcode-reader-vue3
3
+ // Definitions: https://github.com/scholtz/qrcode-reader-vue3
4
+
5
+ import type { App, Plugin, DefineComponent } from 'vue';
6
+ import type { DetectedBarcode } from 'barcode-detector';
7
+
8
+ export * from './index';
9
+
10
+ declare module 'qrcode-reader-vue3' {
11
+ import { QrcodeStream, QrcodeCapture, QrcodeDropZone, install } from './index';
12
+ export { QrcodeStream, QrcodeCapture, QrcodeDropZone, install };
13
+ export * from './index';
14
+
15
+ const plugin: Plugin;
16
+ export default plugin;
17
+ }
18
+
19
+ declare module 'QrcodeCapture' {
20
+ import { QrcodeCapture } from './index';
21
+ export default QrcodeCapture;
22
+ }
23
+
24
+ declare module 'QrcodeDropZone' {
25
+ import { QrcodeDropZone } from './index';
26
+ export default QrcodeDropZone;
27
+ }
28
+
29
+ declare module 'QrcodeStream' {
30
+ import { QrcodeStream } from './index';
31
+ export default QrcodeStream;
32
+ }
@@ -0,0 +1,25 @@
1
+ interface BrowserDetails {
2
+ browser: string;
3
+ version?: number;
4
+ }
5
+
6
+ declare module "webrtc-adapter/src/js/chrome/getusermedia" {
7
+ export function shimGetUserMedia(
8
+ window: Window,
9
+ browserDetails: BrowserDetails
10
+ ): void;
11
+ }
12
+
13
+ declare module "webrtc-adapter/src/js/firefox/getusermedia" {
14
+ export function shimGetUserMedia(
15
+ window: Window,
16
+ browserDetails: BrowserDetails
17
+ ): void;
18
+ }
19
+
20
+ declare module "webrtc-adapter/src/js/safari/safari_shim" {
21
+ export function shimGetUserMedia(
22
+ window: Window,
23
+ browserDetails: BrowserDetails
24
+ ): void;
25
+ }
@@ -0,0 +1,72 @@
1
+ import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
2
+ import { indempotent, eventOn, timeout } from "../../src/misc/util";
3
+
4
+ describe("indempotent", () => {
5
+ it("reuses the result of the first invocation", () => {
6
+ const action = vi.fn((value: number) => value * 2);
7
+ const memoized = indempotent(
8
+ action as unknown as (...args: unknown[]) => unknown
9
+ ) as (value: number) => number;
10
+
11
+ const first = memoized(3);
12
+ const second = memoized(20);
13
+
14
+ expect(first).toBe(6);
15
+ expect(second).toBe(6);
16
+ expect(action).toHaveBeenCalledTimes(1);
17
+ });
18
+
19
+ it("shares the same promise for concurrent calls", async () => {
20
+ const action = vi.fn(async (value: number) => value * 3);
21
+ const memoized = indempotent(
22
+ action as unknown as (...args: unknown[]) => unknown
23
+ ) as (value: number) => Promise<number>;
24
+
25
+ const [first, second] = await Promise.all([
26
+ memoized(2),
27
+ memoized(5),
28
+ ]);
29
+
30
+ expect(first).toBe(6);
31
+ expect(second).toBe(6);
32
+ expect(action).toHaveBeenCalledTimes(1);
33
+ });
34
+ });
35
+
36
+ describe("eventOn", () => {
37
+ it("resolves with the dispatched event", async () => {
38
+ const target = new EventTarget();
39
+ const promise = eventOn(target, "ready");
40
+ const payload = new Event("ready");
41
+
42
+ target.dispatchEvent(payload);
43
+
44
+ await expect(promise).resolves.toBe(payload);
45
+ });
46
+ });
47
+
48
+ describe("timeout", () => {
49
+ beforeEach(() => {
50
+ vi.useFakeTimers();
51
+ });
52
+
53
+ afterEach(() => {
54
+ vi.useRealTimers();
55
+ });
56
+
57
+ it("waits for the specified delay", async () => {
58
+ const promise = timeout(1000);
59
+
60
+ vi.advanceTimersByTime(999);
61
+ let resolved = false;
62
+ promise.then(() => {
63
+ resolved = true;
64
+ });
65
+
66
+ expect(resolved).toBe(false);
67
+
68
+ vi.advanceTimersByTime(1);
69
+ await expect(promise).resolves.toBeUndefined();
70
+ expect(resolved).toBe(true);
71
+ });
72
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "ESNext",
5
+ "lib": ["ES2020", "DOM", "DOM.Iterable"],
6
+ "jsx": "preserve",
7
+ "moduleResolution": "node",
8
+ "strict": true,
9
+ "esModuleInterop": true,
10
+ "skipLibCheck": true,
11
+ "forceConsistentCasingInFileNames": true,
12
+ "resolveJsonModule": true,
13
+ "isolatedModules": true,
14
+ "declaration": true,
15
+ "declarationDir": "./dist/types",
16
+ "outDir": "./dist",
17
+ "baseUrl": ".",
18
+ "paths": {
19
+ "@/*": ["src/*"]
20
+ },
21
+ "types": ["node"],
22
+ "noImplicitThis": false
23
+ },
24
+ "include": [
25
+ "src/**/*.ts",
26
+ "src/**/*.tsx",
27
+ "src/**/*.vue",
28
+ "src/**/*.d.ts"
29
+ ],
30
+ "exclude": [
31
+ "node_modules",
32
+ "dist",
33
+ "docs"
34
+ ]
35
+ }
@@ -0,0 +1,10 @@
1
+ import { defineConfig } from "vitest/config";
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ environment: "jsdom",
6
+ coverage: {
7
+ reporter: ["text", "lcov"],
8
+ },
9
+ },
10
+ });
package/vue.config.js CHANGED
@@ -1,7 +1,30 @@
1
- module.exports = {
2
- css: { extract: false },
3
-
4
- lintOnSave: false,
5
-
6
- transpileDependencies: ["webrtc-adapter"],
7
- };
1
+ const { defineConfig } = require("@vue/cli-service");
2
+
3
+ module.exports = defineConfig({
4
+ css: { extract: false },
5
+
6
+ lintOnSave: false,
7
+
8
+ transpileDependencies: ["webrtc-adapter"],
9
+
10
+ chainWebpack: (config) => {
11
+ // Disable eslint during build
12
+ config.module.rules.delete('eslint');
13
+
14
+ // Enable TypeScript declaration generation
15
+ if (process.env.NODE_ENV === "production") {
16
+ config.module
17
+ .rule("ts")
18
+ .use("ts-loader")
19
+ .tap((options) => {
20
+ return {
21
+ ...options,
22
+ compilerOptions: {
23
+ declaration: true,
24
+ declarationDir: "./dist/types",
25
+ },
26
+ };
27
+ });
28
+ }
29
+ },
30
+ });
package/.eslintrc.js DELETED
@@ -1,25 +0,0 @@
1
- module.exports = {
2
- root: true,
3
- env: {
4
- node: true,
5
- },
6
- extends: ["plugin:vue/essential", "eslint:recommended", "@vue/prettier"],
7
- parserOptions: {
8
- parser: "babel-eslint",
9
- },
10
- rules: {
11
- "no-console": process.env.NODE_ENV === "production" ? "warn" : "off",
12
- "no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off",
13
- },
14
- overrides: [
15
- {
16
- files: [
17
- "**/__tests__/*.{j,t}s?(x)",
18
- "**/tests/unit/**/*.spec.{j,t}s?(x)",
19
- ],
20
- env: {
21
- mocha: true,
22
- },
23
- },
24
- ],
25
- };
package/src/index.js DELETED
@@ -1,32 +0,0 @@
1
- import QrcodeStream from "./components/QrcodeStream.vue";
2
- import QrcodeCapture from "./components/QrcodeCapture.vue";
3
- import QrcodeDropZone from "./components/QrcodeDropZone.vue";
4
-
5
- // Install the components
6
- export function install(Vue) {
7
- Vue.component("qrcode-stream", QrcodeStream);
8
- Vue.component("qrcode-capture", QrcodeCapture);
9
- Vue.component("qrcode-drop-zone", QrcodeDropZone);
10
- }
11
-
12
- // Expose the components
13
- export { QrcodeStream, QrcodeCapture, QrcodeDropZone };
14
-
15
- /* -- Plugin definition & Auto-install -- */
16
- /* You shouldn't have to modify the code below */
17
-
18
- // Plugin
19
- const plugin = { install };
20
-
21
- export default plugin;
22
-
23
- // Auto-install
24
- let GlobalVue = null;
25
- if (typeof window !== "undefined") {
26
- GlobalVue = window.Vue;
27
- } else if (typeof global !== "undefined") {
28
- GlobalVue = global.Vue;
29
- }
30
- if (GlobalVue) {
31
- GlobalVue.use(plugin);
32
- }
@@ -1,112 +0,0 @@
1
- import { DropImageFetchError } from "./errors.js";
2
- import BarcodeDetector from "barcode-detector";
3
- import { eventOn } from "callforth";
4
-
5
- const adaptOldFormat = (detectedCodes) => {
6
- if (detectedCodes.length > 0) {
7
- const [firstCode] = detectedCodes;
8
-
9
- const [topLeftCorner, topRightCorner, bottomRightCorner, bottomLeftCorner] =
10
- firstCode.cornerPoints;
11
-
12
- return {
13
- content: firstCode.rawValue,
14
- location: {
15
- topLeftCorner,
16
- topRightCorner,
17
- bottomRightCorner,
18
- bottomLeftCorner,
19
-
20
- // not supported by native API:
21
- topLeftFinderPattern: {},
22
- topRightFinderPattern: {},
23
- bottomLeftFinderPattern: {},
24
- },
25
- imageData: null,
26
- binaryData: null,
27
- };
28
- } else {
29
- return {
30
- content: null,
31
- location: null,
32
- imageData: null,
33
- binaryData: null,
34
- };
35
- }
36
- };
37
-
38
- /**
39
- * Continuously extracts frames from camera stream and tries to read
40
- * potentially pictured QR codes.
41
- */
42
- export const keepScanning = (videoElement, options) => {
43
- const barcodeDetector = new BarcodeDetector({ formats: ["qr_code"] });
44
-
45
- const { detectHandler, locateHandler, minDelay } = options;
46
-
47
- const processFrame = (state) => async (timeNow) => {
48
- if (videoElement.readyState > 1) {
49
- const { lastScanned, contentBefore, locationBefore } = state;
50
-
51
- if (timeNow - lastScanned >= minDelay) {
52
- const detectedCodes = await barcodeDetector.detect(videoElement);
53
- const { content, location, imageData, binaryData } =
54
- adaptOldFormat(detectedCodes);
55
-
56
- if (content !== null && content !== contentBefore) {
57
- detectHandler({ content, location, imageData, binaryData });
58
- }
59
-
60
- if (location !== null || locationBefore !== null) {
61
- locateHandler(detectedCodes);
62
- }
63
-
64
- window.requestAnimationFrame(
65
- processFrame({
66
- lastScanned: timeNow,
67
- contentBefore: content ?? contentBefore,
68
- locationBefore: location,
69
- binaryData: binaryData,
70
- })
71
- );
72
- } else {
73
- window.requestAnimationFrame(processFrame(state));
74
- }
75
- }
76
- };
77
-
78
- processFrame({
79
- contentBefore: null,
80
- locationBefore: null,
81
- lastScanned: performance.now(),
82
- binaryData: null,
83
- })();
84
- };
85
-
86
- const imageElementFromUrl = async (url) => {
87
- if (url.startsWith("http") && url.includes(location.host) === false) {
88
- throw new DropImageFetchError();
89
- }
90
-
91
- const image = document.createElement("img");
92
- image.src = url;
93
-
94
- await eventOn(image, "load");
95
-
96
- return image;
97
- };
98
-
99
- export const processFile = async (file) => {
100
- const barcodeDetector = new BarcodeDetector({ formats: ["qr_code"] });
101
- const detectedCodes = await barcodeDetector.detect(file);
102
-
103
- return adaptOldFormat(detectedCodes);
104
- };
105
-
106
- export const processUrl = async (url) => {
107
- const barcodeDetector = new BarcodeDetector({ formats: ["qr_code"] });
108
- const image = await imageElementFromUrl(url);
109
- const detectedCodes = await barcodeDetector.detect(image);
110
-
111
- return adaptOldFormat(detectedCodes);
112
- };
package/src/misc/util.js DELETED
@@ -1,15 +0,0 @@
1
- export const indempotent = (action) => {
2
- let called = false;
3
- let result = undefined;
4
-
5
- return (...args) => {
6
- if (called) {
7
- return result;
8
- } else {
9
- result = action(...args);
10
- called = true;
11
-
12
- return result;
13
- }
14
- };
15
- };