@wirunrom/hqr-generate 0.2.0 → 0.2.2

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,16 +1,114 @@
1
- # hqr-generate
1
+ # @wirunrom/hqr-generate
2
2
 
3
- Stable black/white QR Code generator returning PNG data URL (Rust + WASM).
3
+ A **stable black-and-white QR Code generator** that returns a **PNG Data URL or PNG bytes**, powered by **Rust + WebAssembly (WASM)**.
4
4
 
5
- ## Install
5
+ This library is designed with a **scan-reliability-first** mindset and a **frontend-first API**, making it easy to use in modern web applications without additional configuration.
6
6
 
7
- npm i hqr-generate
7
+ ---
8
8
 
9
- ## Usage (React / Next.js)
9
+ ## Features
10
+
11
+ - High-contrast **black & white only** output (maximum scan compatibility)
12
+ - Optimized for both **old and new mobile devices**
13
+ - Deterministic and consistent QR output
14
+ - Lightweight and fast (**Rust + WASM**)
15
+ - Supports both:
16
+ - **PNG Data URL** (simple usage)
17
+ - **PNG raw bytes** (best performance)
18
+ - Works out of the box with:
19
+ - Plain HTML
20
+ - React
21
+ - Next.js (Pages Router & App Router)
22
+ - Modern bundlers (Vite, Webpack, etc.)
23
+
24
+ ---
25
+
26
+ ## Installation
27
+
28
+ ```bash
29
+ npm install @wirunrom/hqr-generate
30
+ ```
31
+
32
+ ## Basic Usage (Browser / React / Next.js)
33
+
34
+ Generate PNG Data URL (simple & compatible)
10
35
 
11
36
  ```ts
12
- import { qr_png_data_url } from "hqr-generate";
37
+ import { qr_png_data_url } from "@wirunrom/hqr-generate";
13
38
 
14
39
  const src = await qr_png_data_url("hello", 320, 4, "Q");
15
- // <img src={src} />
40
+ <img src={src} alt="QR Code" />
41
+ ```
42
+
43
+ Generate PNG Bytes
44
+ Using raw bytes avoids Base64 overhead and is more memory-efficient.
45
+
46
+ ```ts
47
+ import { qr_png_bytes } from "@wirunrom/hqr-generate";
48
+
49
+ const bytes = await qr_png_bytes("hello", 320, 4, "Q");
50
+ const url = URL.createObjectURL(new Blob([bytes], { type: "image/png" }));
51
+
52
+ <img src={url} alt="QR Code" />
16
53
  ```
54
+
55
+ ## React Hook Helper
56
+
57
+ **useQrPngDataUrl**
58
+ A React hook that generates a PNG Data URL and updates automatically when inputs change.
59
+
60
+ ```ts
61
+ import { useQrPngDataUrl } from "@wirunrom/hqr-generate/react";
62
+
63
+ function QR() {
64
+ const src = useQrPngDataUrl("hello world", {
65
+ size: 320,
66
+ margin: 4,
67
+ ecc: "Q",
68
+ });
69
+
70
+ if (!src) return null;
71
+ return <img src={src} alt="QR Code" />;
72
+ }
73
+ ```
74
+
75
+ **useQrPngBlobUrl**
76
+ A React hook that generates a Blob URL and updates automatically when inputs change.
77
+
78
+ ```ts
79
+ import { useQrPngBlobUrl } from "@wirunrom/hqr-generate/react";
80
+
81
+ function QR() {
82
+ const src = useQrPngBlobUrl("hello world", {
83
+ size: 320,
84
+ margin: 4,
85
+ ecc: "Q",
86
+ });
87
+
88
+ if (!src) return null;
89
+ return <img src={src} alt="QR Code" />;
90
+ }
91
+ ```
92
+
93
+ ## API Reference
94
+
95
+ **qr_png_data_url(text, size?, margin?, ecc?)**
96
+ Generate a QR code and return a PNG Data URL.
97
+
98
+ | Name | Type | Default | Description |
99
+ | ------ | -------------------------- | ------- | ---------------------------- |
100
+ | text | `string` | — | Text to encode |
101
+ | size | `number` | `320` | Image size in pixels |
102
+ | margin | `number` | `4` | Quiet zone (recommended ≥ 4) |
103
+ | ecc | `"L" \| "M" \| "Q" \| "H"` | `"Q"` | Error correction level |
104
+
105
+ **qr_png_bytes(text, size?, margin?, ecc?)**
106
+ Generate a QR code and return PNG raw bytes (Uint8Array).
107
+ This is the fastest and most memory-efficient option.
108
+
109
+ | Name | Type | Default | Description |
110
+ | ------ | -------------------------- | ------- | ---------------------------- |
111
+ | text | `string` | — | Text to encode |
112
+ | size | `number` | `320` | Image size in pixels |
113
+ | margin | `number` | `4` | Quiet zone (recommended ≥ 4) |
114
+ | ecc | `"L" \| "M" \| "Q" \| "H"` | `"Q"` | Error correction level |
@@ -0,0 +1,23 @@
1
+ export type QrEcc = "L" | "M" | "Q" | "H";
2
+
3
+ export interface UseQrPngOptions {
4
+ size?: number;
5
+ margin?: number;
6
+ ecc?: QrEcc;
7
+ }
8
+
9
+ /**
10
+ * React hook that returns PNG Data URL string (base64).
11
+ */
12
+ export declare function useQrPngDataUrl(
13
+ text: string,
14
+ opts?: UseQrPngOptions
15
+ ): string;
16
+
17
+ /**
18
+ * React hook that returns Blob URL string.
19
+ */
20
+ export declare function useQrPngBlobUrl(
21
+ text: string,
22
+ opts?: UseQrPngOptions
23
+ ): string;
@@ -0,0 +1,73 @@
1
+ import { useEffect, useState } from "react";
2
+ import { qr_png_data_url, qr_png_bytes } from "../index.bundler.js";
3
+
4
+ export function useQrPngDataUrl(text, opts) {
5
+ const size = opts?.size ?? 320;
6
+ const margin = opts?.margin ?? 4;
7
+ const ecc = opts?.ecc ?? "Q";
8
+
9
+ const [src, setSrc] = useState("");
10
+
11
+ useEffect(() => {
12
+ let alive = true;
13
+
14
+ if (!text) {
15
+ setSrc("");
16
+ return () => {
17
+ alive = false;
18
+ };
19
+ }
20
+
21
+ qr_png_data_url(text, size, margin, ecc)
22
+ .then((res) => alive && setSrc(res))
23
+ .catch(() => alive && setSrc(""));
24
+
25
+ return () => {
26
+ alive = false;
27
+ };
28
+ }, [text, size, margin, ecc]);
29
+
30
+ return src;
31
+ }
32
+
33
+ /**
34
+ * Faster than base64 data url: uses Blob URL + revokes on cleanup
35
+ */
36
+ export function useQrPngBlobUrl(text, opts) {
37
+ const size = opts?.size ?? 320;
38
+ const margin = opts?.margin ?? 4;
39
+ const ecc = opts?.ecc ?? "Q";
40
+
41
+ const [src, setSrc] = useState("");
42
+
43
+ useEffect(() => {
44
+ let alive = true;
45
+ let objectUrl = "";
46
+
47
+ if (!text) {
48
+ setSrc("");
49
+ return () => {
50
+ alive = false;
51
+ if (objectUrl) URL.revokeObjectURL(objectUrl);
52
+ };
53
+ }
54
+
55
+ qr_png_bytes(text, size, margin, ecc)
56
+ .then((bytes) => {
57
+ if (!alive) return;
58
+ if (objectUrl) URL.revokeObjectURL(objectUrl);
59
+ objectUrl = URL.createObjectURL(
60
+ new Blob([bytes], { type: "image/png" }),
61
+ );
62
+ setSrc(objectUrl);
63
+ })
64
+ .catch(() => alive && setSrc(""));
65
+
66
+ return () => {
67
+ alive = false;
68
+ if (objectUrl) URL.revokeObjectURL(objectUrl);
69
+ };
70
+ }, [text, size, margin, ecc]);
71
+
72
+ return src;
73
+ }
@@ -0,0 +1,21 @@
1
+ import init, {
2
+ qr_png_data_url as _qr_png_data_url,
3
+ qr_png_bytes as _qr_png_bytes,
4
+ } from "./pkg/bundler/hqr_generate.js";
5
+
6
+ let _initPromise;
7
+
8
+ async function ensureInit() {
9
+ if (!_initPromise) _initPromise = init();
10
+ await _initPromise;
11
+ }
12
+
13
+ export async function qr_png_data_url(text, size = 320, margin = 4, ecc = "Q") {
14
+ await ensureInit();
15
+ return _qr_png_data_url(text, size, margin, ecc);
16
+ }
17
+
18
+ export async function qr_png_bytes(text, size = 320, margin = 4, ecc = "Q") {
19
+ await ensureInit();
20
+ return _qr_png_bytes(text, size, margin, ecc);
21
+ }
package/index.d.ts CHANGED
@@ -1,8 +1,22 @@
1
1
  export type QrEcc = "L" | "M" | "Q" | "H";
2
2
 
3
- export function qr_png_data_url(
3
+ /**
4
+ * Generate QR code PNG as Data URL (base64).
5
+ */
6
+ export declare function qr_png_data_url(
4
7
  text: string,
5
8
  size?: number,
6
9
  margin?: number,
7
10
  ecc?: QrEcc
8
11
  ): Promise<string>;
12
+
13
+ /**
14
+ * Generate QR code PNG as raw bytes (Uint8Array).
15
+ * Recommended for best performance (Blob URL, upload, caching).
16
+ */
17
+ export declare function qr_png_bytes(
18
+ text: string,
19
+ size?: number,
20
+ margin?: number,
21
+ ecc?: QrEcc
22
+ ): Promise<Uint8Array>;
package/index.web.js ADDED
@@ -0,0 +1,29 @@
1
+ import init, {
2
+ qr_png_data_url as _qr_png_data_url,
3
+ qr_png_bytes as _qr_png_bytes,
4
+ } from "./pkg/web/hqr_generate.js";
5
+
6
+ let _initPromise;
7
+
8
+ /** @returns {Promise<void>} */
9
+ async function ensureInit() {
10
+ if (!_initPromise) _initPromise = init();
11
+ await _initPromise;
12
+ }
13
+
14
+ /**
15
+ * @param {string} text
16
+ * @param {number} [size=320]
17
+ * @param {number} [margin=4]
18
+ * @param {"L"|"M"|"Q"|"H"} [ecc="Q"]
19
+ * @returns {Promise<string>}
20
+ */
21
+ export async function qr_png_data_url(text, size = 320, margin = 4, ecc = "Q") {
22
+ await ensureInit();
23
+ return _qr_png_data_url(text, size, margin, ecc);
24
+ }
25
+
26
+ export async function qr_png_bytes(text, size = 320, margin = 4, ecc = "Q") {
27
+ await ensureInit();
28
+ return _qr_png_bytes(text, size, margin, ecc);
29
+ }
package/package.json CHANGED
@@ -1,30 +1,49 @@
1
1
  {
2
2
  "name": "@wirunrom/hqr-generate",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Stable black/white QR code generator (PNG data URL) powered by Rust + WASM",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "pkg/**",
8
- "index.browser.js",
9
- "index.node.js",
8
+ "index.web.js",
9
+ "index.bundler.js",
10
10
  "index.d.ts",
11
+ "react/**",
12
+ "bundler/**",
11
13
  "README.md",
12
14
  "LICENSE"
13
15
  ],
14
- "main": "./index.node.js",
15
- "module": "./index.browser.js",
16
+ "main": "./index.web.js",
17
+ "module": "./index.web.js",
16
18
  "types": "./index.d.ts",
17
19
  "exports": {
18
20
  ".": {
19
21
  "types": "./index.d.ts",
20
- "browser": "./index.browser.js",
21
- "default": "./index.node.js"
22
+ "default": "./index.web.js"
23
+ },
24
+ "./bundler": {
25
+ "types": "./index.d.ts",
26
+ "default": "./index.bundler.js"
27
+ },
28
+ "./react": {
29
+ "types": "./react/index.d.ts",
30
+ "default": "./react/index.js"
31
+ },
32
+ "./bundler/react": {
33
+ "types": "./bundler/react.d.ts",
34
+ "default": "./bundler/react.js"
22
35
  }
23
36
  },
37
+ "peerDependencies": {
38
+ "react": ">=17",
39
+ "react-dom": ">=17"
40
+ },
24
41
  "scripts": {
25
- "build": "wasm-pack build --target bundler",
26
42
  "clean": "rm -rf pkg",
27
- "prepack": "npm run clean && npm run build && rm -f pkg/.gitignore",
43
+ "build:web": "wasm-pack build --target web --out-dir pkg/web",
44
+ "build:bundler": "wasm-pack build --target bundler --out-dir pkg/bundler",
45
+ "build": "npm run build:web && npm run build:bundler",
46
+ "prepack": "npm run clean && npm run build && rm -f pkg/web/.gitignore pkg/bundler/.gitignore",
28
47
  "publish:npm": "npm publish --registry=https://registry.npmjs.org --access public",
29
48
  "publish:github": "npm publish --registry=https://npm.pkg.github.com --userconfig .npmrc.github --access public"
30
49
  },
@@ -0,0 +1,114 @@
1
+ # @wirunrom/hqr-generate
2
+
3
+ A **stable black-and-white QR Code generator** that returns a **PNG Data URL or PNG bytes**, powered by **Rust + WebAssembly (WASM)**.
4
+
5
+ This library is designed with a **scan-reliability-first** mindset and a **frontend-first API**, making it easy to use in modern web applications without additional configuration.
6
+
7
+ ---
8
+
9
+ ## Features
10
+
11
+ - High-contrast **black & white only** output (maximum scan compatibility)
12
+ - Optimized for both **old and new mobile devices**
13
+ - Deterministic and consistent QR output
14
+ - Lightweight and fast (**Rust + WASM**)
15
+ - Supports both:
16
+ - **PNG Data URL** (simple usage)
17
+ - **PNG raw bytes** (best performance)
18
+ - Works out of the box with:
19
+ - Plain HTML
20
+ - React
21
+ - Next.js (Pages Router & App Router)
22
+ - Modern bundlers (Vite, Webpack, etc.)
23
+
24
+ ---
25
+
26
+ ## Installation
27
+
28
+ ```bash
29
+ npm install @wirunrom/hqr-generate
30
+ ```
31
+
32
+ ## Basic Usage (Browser / React / Next.js)
33
+
34
+ Generate PNG Data URL (simple & compatible)
35
+
36
+ ```ts
37
+ import { qr_png_data_url } from "@wirunrom/hqr-generate";
38
+
39
+ const src = await qr_png_data_url("hello", 320, 4, "Q");
40
+ <img src={src} alt="QR Code" />
41
+ ```
42
+
43
+ Generate PNG Bytes
44
+ Using raw bytes avoids Base64 overhead and is more memory-efficient.
45
+
46
+ ```ts
47
+ import { qr_png_bytes } from "@wirunrom/hqr-generate";
48
+
49
+ const bytes = await qr_png_bytes("hello", 320, 4, "Q");
50
+ const url = URL.createObjectURL(new Blob([bytes], { type: "image/png" }));
51
+
52
+ <img src={url} alt="QR Code" />
53
+ ```
54
+
55
+ ## React Hook Helper
56
+
57
+ **useQrPngDataUrl**
58
+ A React hook that generates a PNG Data URL and updates automatically when inputs change.
59
+
60
+ ```ts
61
+ import { useQrPngDataUrl } from "@wirunrom/hqr-generate/react";
62
+
63
+ function QR() {
64
+ const src = useQrPngDataUrl("hello world", {
65
+ size: 320,
66
+ margin: 4,
67
+ ecc: "Q",
68
+ });
69
+
70
+ if (!src) return null;
71
+ return <img src={src} alt="QR Code" />;
72
+ }
73
+ ```
74
+
75
+ **useQrPngBlobUrl**
76
+ A React hook that generates a Blob URL and updates automatically when inputs change.
77
+
78
+ ```ts
79
+ import { useQrPngBlobUrl } from "@wirunrom/hqr-generate/react";
80
+
81
+ function QR() {
82
+ const src = useQrPngBlobUrl("hello world", {
83
+ size: 320,
84
+ margin: 4,
85
+ ecc: "Q",
86
+ });
87
+
88
+ if (!src) return null;
89
+ return <img src={src} alt="QR Code" />;
90
+ }
91
+ ```
92
+
93
+ ## API Reference
94
+
95
+ **qr_png_data_url(text, size?, margin?, ecc?)**
96
+ Generate a QR code and return a PNG Data URL.
97
+
98
+ | Name | Type | Default | Description |
99
+ | ------ | -------------------------- | ------- | ---------------------------- |
100
+ | text | `string` | — | Text to encode |
101
+ | size | `number` | `320` | Image size in pixels |
102
+ | margin | `number` | `4` | Quiet zone (recommended ≥ 4) |
103
+ | ecc | `"L" \| "M" \| "Q" \| "H"` | `"Q"` | Error correction level |
104
+
105
+ **qr_png_bytes(text, size?, margin?, ecc?)**
106
+ Generate a QR code and return PNG raw bytes (Uint8Array).
107
+ This is the fastest and most memory-efficient option.
108
+
109
+ | Name | Type | Default | Description |
110
+ | ------ | -------------------------- | ------- | ---------------------------- |
111
+ | text | `string` | — | Text to encode |
112
+ | size | `number` | `320` | Image size in pixels |
113
+ | margin | `number` | `4` | Quiet zone (recommended ≥ 4) |
114
+ | ecc | `"L" \| "M" \| "Q" \| "H"` | `"Q"` | Error correction level |
@@ -0,0 +1,6 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export function qr_png_bytes(text: string, size: number, margin: number, ecc: number): Uint8Array;
5
+
6
+ export function qr_png_data_url(text: string, size: number, margin: number, ecc: number): string;
@@ -5,5 +5,5 @@ import { __wbg_set_wasm } from "./hqr_generate_bg.js";
5
5
  __wbg_set_wasm(wasm);
6
6
  wasm.__wbindgen_start();
7
7
  export {
8
- qr_png_data_url
8
+ qr_png_bytes, qr_png_data_url
9
9
  } from "./hqr_generate_bg.js";
@@ -2,31 +2,53 @@
2
2
  * @param {string} text
3
3
  * @param {number} size
4
4
  * @param {number} margin
5
- * @param {string} ecc
5
+ * @param {number} ecc
6
+ * @returns {Uint8Array}
7
+ */
8
+ export function qr_png_bytes(text, size, margin, ecc) {
9
+ const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
10
+ const len0 = WASM_VECTOR_LEN;
11
+ const ret = wasm.qr_png_bytes(ptr0, len0, size, margin, ecc);
12
+ if (ret[2]) {
13
+ throw takeFromExternrefTable0(ret[1]);
14
+ }
15
+ return takeFromExternrefTable0(ret[0]);
16
+ }
17
+
18
+ /**
19
+ * @param {string} text
20
+ * @param {number} size
21
+ * @param {number} margin
22
+ * @param {number} ecc
6
23
  * @returns {string}
7
24
  */
8
25
  export function qr_png_data_url(text, size, margin, ecc) {
9
- let deferred4_0;
10
- let deferred4_1;
26
+ let deferred3_0;
27
+ let deferred3_1;
11
28
  try {
12
29
  const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
13
30
  const len0 = WASM_VECTOR_LEN;
14
- const ptr1 = passStringToWasm0(ecc, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
15
- const len1 = WASM_VECTOR_LEN;
16
- const ret = wasm.qr_png_data_url(ptr0, len0, size, margin, ptr1, len1);
17
- var ptr3 = ret[0];
18
- var len3 = ret[1];
31
+ const ret = wasm.qr_png_data_url(ptr0, len0, size, margin, ecc);
32
+ var ptr2 = ret[0];
33
+ var len2 = ret[1];
19
34
  if (ret[3]) {
20
- ptr3 = 0; len3 = 0;
35
+ ptr2 = 0; len2 = 0;
21
36
  throw takeFromExternrefTable0(ret[2]);
22
37
  }
23
- deferred4_0 = ptr3;
24
- deferred4_1 = len3;
25
- return getStringFromWasm0(ptr3, len3);
38
+ deferred3_0 = ptr2;
39
+ deferred3_1 = len2;
40
+ return getStringFromWasm0(ptr2, len2);
26
41
  } finally {
27
- wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
42
+ wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
28
43
  }
29
44
  }
45
+ export function __wbg___wbindgen_throw_be289d5034ed271b(arg0, arg1) {
46
+ throw new Error(getStringFromWasm0(arg0, arg1));
47
+ }
48
+ export function __wbg_new_from_slice_a3d2629dc1826784(arg0, arg1) {
49
+ const ret = new Uint8Array(getArrayU8FromWasm0(arg0, arg1));
50
+ return ret;
51
+ }
30
52
  export function __wbindgen_cast_0000000000000001(arg0, arg1) {
31
53
  // Cast intrinsic for `Ref(String) -> Externref`.
32
54
  const ret = getStringFromWasm0(arg0, arg1);
@@ -41,6 +63,11 @@ export function __wbindgen_init_externref_table() {
41
63
  table.set(offset + 2, true);
42
64
  table.set(offset + 3, false);
43
65
  }
66
+ function getArrayU8FromWasm0(ptr, len) {
67
+ ptr = ptr >>> 0;
68
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
69
+ }
70
+
44
71
  function getStringFromWasm0(ptr, len) {
45
72
  ptr = ptr >>> 0;
46
73
  return decodeText(ptr, len);
Binary file
@@ -1,7 +1,8 @@
1
1
  /* tslint:disable */
2
2
  /* eslint-disable */
3
3
  export const memory: WebAssembly.Memory;
4
- export const qr_png_data_url: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number];
4
+ export const qr_png_bytes: (a: number, b: number, c: number, d: number, e: number) => [number, number, number];
5
+ export const qr_png_data_url: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number];
5
6
  export const __wbindgen_externrefs: WebAssembly.Table;
6
7
  export const __wbindgen_malloc: (a: number, b: number) => number;
7
8
  export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
@@ -0,0 +1,114 @@
1
+ # @wirunrom/hqr-generate
2
+
3
+ A **stable black-and-white QR Code generator** that returns a **PNG Data URL or PNG bytes**, powered by **Rust + WebAssembly (WASM)**.
4
+
5
+ This library is designed with a **scan-reliability-first** mindset and a **frontend-first API**, making it easy to use in modern web applications without additional configuration.
6
+
7
+ ---
8
+
9
+ ## Features
10
+
11
+ - High-contrast **black & white only** output (maximum scan compatibility)
12
+ - Optimized for both **old and new mobile devices**
13
+ - Deterministic and consistent QR output
14
+ - Lightweight and fast (**Rust + WASM**)
15
+ - Supports both:
16
+ - **PNG Data URL** (simple usage)
17
+ - **PNG raw bytes** (best performance)
18
+ - Works out of the box with:
19
+ - Plain HTML
20
+ - React
21
+ - Next.js (Pages Router & App Router)
22
+ - Modern bundlers (Vite, Webpack, etc.)
23
+
24
+ ---
25
+
26
+ ## Installation
27
+
28
+ ```bash
29
+ npm install @wirunrom/hqr-generate
30
+ ```
31
+
32
+ ## Basic Usage (Browser / React / Next.js)
33
+
34
+ Generate PNG Data URL (simple & compatible)
35
+
36
+ ```ts
37
+ import { qr_png_data_url } from "@wirunrom/hqr-generate";
38
+
39
+ const src = await qr_png_data_url("hello", 320, 4, "Q");
40
+ <img src={src} alt="QR Code" />
41
+ ```
42
+
43
+ Generate PNG Bytes
44
+ Using raw bytes avoids Base64 overhead and is more memory-efficient.
45
+
46
+ ```ts
47
+ import { qr_png_bytes } from "@wirunrom/hqr-generate";
48
+
49
+ const bytes = await qr_png_bytes("hello", 320, 4, "Q");
50
+ const url = URL.createObjectURL(new Blob([bytes], { type: "image/png" }));
51
+
52
+ <img src={url} alt="QR Code" />
53
+ ```
54
+
55
+ ## React Hook Helper
56
+
57
+ **useQrPngDataUrl**
58
+ A React hook that generates a PNG Data URL and updates automatically when inputs change.
59
+
60
+ ```ts
61
+ import { useQrPngDataUrl } from "@wirunrom/hqr-generate/react";
62
+
63
+ function QR() {
64
+ const src = useQrPngDataUrl("hello world", {
65
+ size: 320,
66
+ margin: 4,
67
+ ecc: "Q",
68
+ });
69
+
70
+ if (!src) return null;
71
+ return <img src={src} alt="QR Code" />;
72
+ }
73
+ ```
74
+
75
+ **useQrPngBlobUrl**
76
+ A React hook that generates a Blob URL and updates automatically when inputs change.
77
+
78
+ ```ts
79
+ import { useQrPngBlobUrl } from "@wirunrom/hqr-generate/react";
80
+
81
+ function QR() {
82
+ const src = useQrPngBlobUrl("hello world", {
83
+ size: 320,
84
+ margin: 4,
85
+ ecc: "Q",
86
+ });
87
+
88
+ if (!src) return null;
89
+ return <img src={src} alt="QR Code" />;
90
+ }
91
+ ```
92
+
93
+ ## API Reference
94
+
95
+ **qr_png_data_url(text, size?, margin?, ecc?)**
96
+ Generate a QR code and return a PNG Data URL.
97
+
98
+ | Name | Type | Default | Description |
99
+ | ------ | -------------------------- | ------- | ---------------------------- |
100
+ | text | `string` | — | Text to encode |
101
+ | size | `number` | `320` | Image size in pixels |
102
+ | margin | `number` | `4` | Quiet zone (recommended ≥ 4) |
103
+ | ecc | `"L" \| "M" \| "Q" \| "H"` | `"Q"` | Error correction level |
104
+
105
+ **qr_png_bytes(text, size?, margin?, ecc?)**
106
+ Generate a QR code and return PNG raw bytes (Uint8Array).
107
+ This is the fastest and most memory-efficient option.
108
+
109
+ | Name | Type | Default | Description |
110
+ | ------ | -------------------------- | ------- | ---------------------------- |
111
+ | text | `string` | — | Text to encode |
112
+ | size | `number` | `320` | Image size in pixels |
113
+ | margin | `number` | `4` | Quiet zone (recommended ≥ 4) |
114
+ | ecc | `"L" \| "M" \| "Q" \| "H"` | `"Q"` | Error correction level |
@@ -0,0 +1,42 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export function qr_png_bytes(text: string, size: number, margin: number, ecc: number): Uint8Array;
5
+
6
+ export function qr_png_data_url(text: string, size: number, margin: number, ecc: number): string;
7
+
8
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
9
+
10
+ export interface InitOutput {
11
+ readonly memory: WebAssembly.Memory;
12
+ readonly qr_png_bytes: (a: number, b: number, c: number, d: number, e: number) => [number, number, number];
13
+ readonly qr_png_data_url: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number];
14
+ readonly __wbindgen_externrefs: WebAssembly.Table;
15
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
16
+ readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
17
+ readonly __externref_table_dealloc: (a: number) => void;
18
+ readonly __wbindgen_free: (a: number, b: number, c: number) => void;
19
+ readonly __wbindgen_start: () => void;
20
+ }
21
+
22
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
23
+
24
+ /**
25
+ * Instantiates the given `module`, which can either be bytes or
26
+ * a precompiled `WebAssembly.Module`.
27
+ *
28
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
29
+ *
30
+ * @returns {InitOutput}
31
+ */
32
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
33
+
34
+ /**
35
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
36
+ * for everything else, calls `WebAssembly.instantiate` directly.
37
+ *
38
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
39
+ *
40
+ * @returns {Promise<InitOutput>}
41
+ */
42
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
@@ -0,0 +1,259 @@
1
+ /* @ts-self-types="./hqr_generate.d.ts" */
2
+
3
+ /**
4
+ * @param {string} text
5
+ * @param {number} size
6
+ * @param {number} margin
7
+ * @param {number} ecc
8
+ * @returns {Uint8Array}
9
+ */
10
+ export function qr_png_bytes(text, size, margin, ecc) {
11
+ const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
12
+ const len0 = WASM_VECTOR_LEN;
13
+ const ret = wasm.qr_png_bytes(ptr0, len0, size, margin, ecc);
14
+ if (ret[2]) {
15
+ throw takeFromExternrefTable0(ret[1]);
16
+ }
17
+ return takeFromExternrefTable0(ret[0]);
18
+ }
19
+
20
+ /**
21
+ * @param {string} text
22
+ * @param {number} size
23
+ * @param {number} margin
24
+ * @param {number} ecc
25
+ * @returns {string}
26
+ */
27
+ export function qr_png_data_url(text, size, margin, ecc) {
28
+ let deferred3_0;
29
+ let deferred3_1;
30
+ try {
31
+ const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
32
+ const len0 = WASM_VECTOR_LEN;
33
+ const ret = wasm.qr_png_data_url(ptr0, len0, size, margin, ecc);
34
+ var ptr2 = ret[0];
35
+ var len2 = ret[1];
36
+ if (ret[3]) {
37
+ ptr2 = 0; len2 = 0;
38
+ throw takeFromExternrefTable0(ret[2]);
39
+ }
40
+ deferred3_0 = ptr2;
41
+ deferred3_1 = len2;
42
+ return getStringFromWasm0(ptr2, len2);
43
+ } finally {
44
+ wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
45
+ }
46
+ }
47
+
48
+ function __wbg_get_imports() {
49
+ const import0 = {
50
+ __proto__: null,
51
+ __wbg___wbindgen_throw_be289d5034ed271b: function(arg0, arg1) {
52
+ throw new Error(getStringFromWasm0(arg0, arg1));
53
+ },
54
+ __wbg_new_from_slice_a3d2629dc1826784: function(arg0, arg1) {
55
+ const ret = new Uint8Array(getArrayU8FromWasm0(arg0, arg1));
56
+ return ret;
57
+ },
58
+ __wbindgen_cast_0000000000000001: function(arg0, arg1) {
59
+ // Cast intrinsic for `Ref(String) -> Externref`.
60
+ const ret = getStringFromWasm0(arg0, arg1);
61
+ return ret;
62
+ },
63
+ __wbindgen_init_externref_table: function() {
64
+ const table = wasm.__wbindgen_externrefs;
65
+ const offset = table.grow(4);
66
+ table.set(0, undefined);
67
+ table.set(offset + 0, undefined);
68
+ table.set(offset + 1, null);
69
+ table.set(offset + 2, true);
70
+ table.set(offset + 3, false);
71
+ },
72
+ };
73
+ return {
74
+ __proto__: null,
75
+ "./hqr_generate_bg.js": import0,
76
+ };
77
+ }
78
+
79
+ function getArrayU8FromWasm0(ptr, len) {
80
+ ptr = ptr >>> 0;
81
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
82
+ }
83
+
84
+ function getStringFromWasm0(ptr, len) {
85
+ ptr = ptr >>> 0;
86
+ return decodeText(ptr, len);
87
+ }
88
+
89
+ let cachedUint8ArrayMemory0 = null;
90
+ function getUint8ArrayMemory0() {
91
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
92
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
93
+ }
94
+ return cachedUint8ArrayMemory0;
95
+ }
96
+
97
+ function passStringToWasm0(arg, malloc, realloc) {
98
+ if (realloc === undefined) {
99
+ const buf = cachedTextEncoder.encode(arg);
100
+ const ptr = malloc(buf.length, 1) >>> 0;
101
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
102
+ WASM_VECTOR_LEN = buf.length;
103
+ return ptr;
104
+ }
105
+
106
+ let len = arg.length;
107
+ let ptr = malloc(len, 1) >>> 0;
108
+
109
+ const mem = getUint8ArrayMemory0();
110
+
111
+ let offset = 0;
112
+
113
+ for (; offset < len; offset++) {
114
+ const code = arg.charCodeAt(offset);
115
+ if (code > 0x7F) break;
116
+ mem[ptr + offset] = code;
117
+ }
118
+ if (offset !== len) {
119
+ if (offset !== 0) {
120
+ arg = arg.slice(offset);
121
+ }
122
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
123
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
124
+ const ret = cachedTextEncoder.encodeInto(arg, view);
125
+
126
+ offset += ret.written;
127
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
128
+ }
129
+
130
+ WASM_VECTOR_LEN = offset;
131
+ return ptr;
132
+ }
133
+
134
+ function takeFromExternrefTable0(idx) {
135
+ const value = wasm.__wbindgen_externrefs.get(idx);
136
+ wasm.__externref_table_dealloc(idx);
137
+ return value;
138
+ }
139
+
140
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
141
+ cachedTextDecoder.decode();
142
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
143
+ let numBytesDecoded = 0;
144
+ function decodeText(ptr, len) {
145
+ numBytesDecoded += len;
146
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
147
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
148
+ cachedTextDecoder.decode();
149
+ numBytesDecoded = len;
150
+ }
151
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
152
+ }
153
+
154
+ const cachedTextEncoder = new TextEncoder();
155
+
156
+ if (!('encodeInto' in cachedTextEncoder)) {
157
+ cachedTextEncoder.encodeInto = function (arg, view) {
158
+ const buf = cachedTextEncoder.encode(arg);
159
+ view.set(buf);
160
+ return {
161
+ read: arg.length,
162
+ written: buf.length
163
+ };
164
+ };
165
+ }
166
+
167
+ let WASM_VECTOR_LEN = 0;
168
+
169
+ let wasmModule, wasm;
170
+ function __wbg_finalize_init(instance, module) {
171
+ wasm = instance.exports;
172
+ wasmModule = module;
173
+ cachedUint8ArrayMemory0 = null;
174
+ wasm.__wbindgen_start();
175
+ return wasm;
176
+ }
177
+
178
+ async function __wbg_load(module, imports) {
179
+ if (typeof Response === 'function' && module instanceof Response) {
180
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
181
+ try {
182
+ return await WebAssembly.instantiateStreaming(module, imports);
183
+ } catch (e) {
184
+ const validResponse = module.ok && expectedResponseType(module.type);
185
+
186
+ if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
187
+ console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
188
+
189
+ } else { throw e; }
190
+ }
191
+ }
192
+
193
+ const bytes = await module.arrayBuffer();
194
+ return await WebAssembly.instantiate(bytes, imports);
195
+ } else {
196
+ const instance = await WebAssembly.instantiate(module, imports);
197
+
198
+ if (instance instanceof WebAssembly.Instance) {
199
+ return { instance, module };
200
+ } else {
201
+ return instance;
202
+ }
203
+ }
204
+
205
+ function expectedResponseType(type) {
206
+ switch (type) {
207
+ case 'basic': case 'cors': case 'default': return true;
208
+ }
209
+ return false;
210
+ }
211
+ }
212
+
213
+ function initSync(module) {
214
+ if (wasm !== undefined) return wasm;
215
+
216
+
217
+ if (module !== undefined) {
218
+ if (Object.getPrototypeOf(module) === Object.prototype) {
219
+ ({module} = module)
220
+ } else {
221
+ console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
222
+ }
223
+ }
224
+
225
+ const imports = __wbg_get_imports();
226
+ if (!(module instanceof WebAssembly.Module)) {
227
+ module = new WebAssembly.Module(module);
228
+ }
229
+ const instance = new WebAssembly.Instance(module, imports);
230
+ return __wbg_finalize_init(instance, module);
231
+ }
232
+
233
+ async function __wbg_init(module_or_path) {
234
+ if (wasm !== undefined) return wasm;
235
+
236
+
237
+ if (module_or_path !== undefined) {
238
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
239
+ ({module_or_path} = module_or_path)
240
+ } else {
241
+ console.warn('using deprecated parameters for the initialization function; pass a single object instead')
242
+ }
243
+ }
244
+
245
+ if (module_or_path === undefined) {
246
+ module_or_path = new URL('hqr_generate_bg.wasm', import.meta.url);
247
+ }
248
+ const imports = __wbg_get_imports();
249
+
250
+ if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
251
+ module_or_path = fetch(module_or_path);
252
+ }
253
+
254
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
255
+
256
+ return __wbg_finalize_init(instance, module);
257
+ }
258
+
259
+ export { initSync, __wbg_init as default };
Binary file
@@ -0,0 +1,11 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ export const memory: WebAssembly.Memory;
4
+ export const qr_png_bytes: (a: number, b: number, c: number, d: number, e: number) => [number, number, number];
5
+ export const qr_png_data_url: (a: number, b: number, c: number, d: number, e: number) => [number, number, number, number];
6
+ export const __wbindgen_externrefs: WebAssembly.Table;
7
+ export const __wbindgen_malloc: (a: number, b: number) => number;
8
+ export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
9
+ export const __externref_table_dealloc: (a: number) => void;
10
+ export const __wbindgen_free: (a: number, b: number, c: number) => void;
11
+ export const __wbindgen_start: () => void;
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "hqr-generate",
3
+ "type": "module",
4
+ "description": "Stable black/white QR code generator (PNG data URL) powered by Rust + WASM",
5
+ "version": "0.1.0",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/heart569522/hqr-generate"
10
+ },
11
+ "files": [
12
+ "hqr_generate_bg.wasm",
13
+ "hqr_generate.js",
14
+ "hqr_generate.d.ts"
15
+ ],
16
+ "main": "hqr_generate.js",
17
+ "types": "hqr_generate.d.ts",
18
+ "sideEffects": [
19
+ "./snippets/*"
20
+ ]
21
+ }
@@ -0,0 +1,17 @@
1
+ export type QrEcc = "L" | "M" | "Q" | "H";
2
+
3
+ export interface UseQrPngOptions {
4
+ size?: number;
5
+ margin?: number;
6
+ ecc?: QrEcc;
7
+ }
8
+
9
+ export declare function useQrPngDataUrl(
10
+ text: string,
11
+ opts?: UseQrPngOptions
12
+ ): string;
13
+
14
+ export declare function useQrPngBlobUrl(
15
+ text: string,
16
+ opts?: UseQrPngOptions
17
+ ): string;
package/react/index.js ADDED
@@ -0,0 +1,91 @@
1
+ import { useEffect, useState } from "react";
2
+ import { qr_png_data_url, qr_png_bytes } from "../index.web.js";
3
+
4
+ /**
5
+ * React hook for QR Code Data URL (browser-only).
6
+ * Backward-compatible, but heavier than Blob URL due to base64.
7
+ * @param {string} text
8
+ * @param {{size?:number, margin?:number, ecc?:"L"|"M"|"Q"|"H"}} [opts]
9
+ */
10
+ export function useQrPngDataUrl(text, opts) {
11
+ const size = opts?.size ?? 320;
12
+ const margin = opts?.margin ?? 4;
13
+ const ecc = opts?.ecc ?? "Q";
14
+
15
+ const [src, setSrc] = useState("");
16
+
17
+ useEffect(() => {
18
+ let alive = true;
19
+
20
+ if (!text) {
21
+ setSrc("");
22
+ return () => {
23
+ alive = false;
24
+ };
25
+ }
26
+
27
+ qr_png_data_url(text, size, margin, ecc)
28
+ .then((res) => {
29
+ if (alive) setSrc(res);
30
+ })
31
+ .catch(() => {
32
+ if (alive) setSrc("");
33
+ });
34
+
35
+ return () => {
36
+ alive = false;
37
+ };
38
+ }, [text, size, margin, ecc]);
39
+
40
+ return src;
41
+ }
42
+
43
+ /**
44
+ * React hook for QR Code as Blob URL (browser-only).
45
+ * Faster + lower memory than base64 data URL for frequent updates / large images.
46
+ * @param {string} text
47
+ * @param {{size?:number, margin?:number, ecc?:"L"|"M"|"Q"|"H"}} [opts]
48
+ */
49
+ export function useQrPngBlobUrl(text, opts) {
50
+ const size = opts?.size ?? 320;
51
+ const margin = opts?.margin ?? 4;
52
+ const ecc = opts?.ecc ?? "Q";
53
+
54
+ const [src, setSrc] = useState("");
55
+
56
+ useEffect(() => {
57
+ let alive = true;
58
+ let objectUrl = "";
59
+
60
+ if (!text) {
61
+ setSrc("");
62
+ return () => {
63
+ alive = false;
64
+ if (objectUrl) URL.revokeObjectURL(objectUrl);
65
+ };
66
+ }
67
+
68
+ qr_png_bytes(text, size, margin, ecc)
69
+ .then((bytes) => {
70
+ if (!alive) return;
71
+
72
+ // revoke old url (if any)
73
+ if (objectUrl) URL.revokeObjectURL(objectUrl);
74
+
75
+ objectUrl = URL.createObjectURL(
76
+ new Blob([bytes], { type: "image/png" }),
77
+ );
78
+ setSrc(objectUrl);
79
+ })
80
+ .catch(() => {
81
+ if (alive) setSrc("");
82
+ });
83
+
84
+ return () => {
85
+ alive = false;
86
+ if (objectUrl) URL.revokeObjectURL(objectUrl);
87
+ };
88
+ }, [text, size, margin, ecc]);
89
+
90
+ return src;
91
+ }
package/index.browser.js DELETED
@@ -1,24 +0,0 @@
1
- let _initPromise;
2
- let _modPromise;
3
-
4
- async function loadWasm() {
5
- if (!_modPromise) {
6
- _modPromise = import("./pkg/hqr_generate.js");
7
- }
8
- const mod = await _modPromise;
9
-
10
- const initFn =
11
- mod.default ?? mod.__wbg_init ?? mod.init ?? mod.__wbindgen_start;
12
-
13
- if (typeof initFn === "function") {
14
- if (!_initPromise) _initPromise = initFn();
15
- await _initPromise;
16
- }
17
-
18
- return mod;
19
- }
20
-
21
- export async function qr_png_data_url(text, size = 320, margin = 4, ecc = "Q") {
22
- const mod = await loadWasm();
23
- return mod.qr_png_data_url(text, size, margin, ecc);
24
- }
package/index.node.js DELETED
@@ -1,5 +0,0 @@
1
- export async function qr_png_data_url() {
2
- throw new Error(
3
- 'hqr-generate is a browser/WASM library. Use it in a Client Component ("use client") only.',
4
- );
5
- }
package/pkg/README.md DELETED
@@ -1,16 +0,0 @@
1
- # hqr-generate
2
-
3
- Stable black/white QR Code generator returning PNG data URL (Rust + WASM).
4
-
5
- ## Install
6
-
7
- npm i hqr-generate
8
-
9
- ## Usage (React / Next.js)
10
-
11
- ```ts
12
- import { qr_png_data_url } from "hqr-generate";
13
-
14
- const src = await qr_png_data_url("hello", 320, 4, "Q");
15
- // <img src={src} />
16
- ```
@@ -1,4 +0,0 @@
1
- /* tslint:disable */
2
- /* eslint-disable */
3
-
4
- export function qr_png_data_url(text: string, size: number, margin: number, ecc: string): string;
Binary file
File without changes