@wirunrom/hqr-generate 0.1.0 → 0.2.1
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 +23 -7
- package/bundler/react.d.ts +6 -0
- package/bundler/react.js +35 -0
- package/index.bundler.js +15 -0
- package/index.d.ts +1 -1
- package/index.web.js +23 -0
- package/package.json +28 -9
- package/pkg/bundler/README.md +32 -0
- package/pkg/web/README.md +32 -0
- package/pkg/web/hqr_generate.d.ts +39 -0
- package/pkg/web/hqr_generate.js +232 -0
- package/pkg/web/hqr_generate_bg.wasm +0 -0
- package/pkg/web/hqr_generate_bg.wasm.d.ts +10 -0
- package/pkg/web/package.json +21 -0
- package/react/index.d.ts +6 -0
- package/react/index.js +40 -0
- package/index.browser.js +0 -24
- package/index.node.js +0 -5
- package/pkg/README.md +0 -16
- /package/pkg/{hqr_generate.d.ts → bundler/hqr_generate.d.ts} +0 -0
- /package/pkg/{hqr_generate.js → bundler/hqr_generate.js} +0 -0
- /package/pkg/{hqr_generate_bg.js → bundler/hqr_generate_bg.js} +0 -0
- /package/pkg/{hqr_generate_bg.wasm → bundler/hqr_generate_bg.wasm} +0 -0
- /package/pkg/{hqr_generate_bg.wasm.d.ts → bundler/hqr_generate_bg.wasm.d.ts} +0 -0
- /package/pkg/{package.json → bundler/package.json} +0 -0
package/README.md
CHANGED
|
@@ -1,16 +1,32 @@
|
|
|
1
|
-
# hqr-generate
|
|
1
|
+
# @wirunrom/hqr-generate
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A **stable black-and-white QR Code generator** that returns a **PNG Data URL**, powered by **Rust + WebAssembly (WASM)**.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
- High contrast (black & white) for maximum scan reliability
|
|
6
|
+
- Designed to work across old and new mobile devices
|
|
7
|
+
- Simple API for frontend usage
|
|
8
|
+
- Compatible with React, Next.js, and modern bundlers
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @wirunrom/hqr-generate
|
|
16
|
+
```
|
|
8
17
|
|
|
9
18
|
## Usage (React / Next.js)
|
|
10
19
|
|
|
11
20
|
```ts
|
|
12
|
-
import { qr_png_data_url } from "hqr-generate";
|
|
21
|
+
import { qr_png_data_url } from "@wirunrom/hqr-generate";
|
|
22
|
+
|
|
23
|
+
const src = await qr_png_data_url(
|
|
24
|
+
"hello world",
|
|
25
|
+
320, // image size (px)
|
|
26
|
+
4, // margin (quiet zone)
|
|
27
|
+
"Q", // error correction level: L | M | Q | H
|
|
28
|
+
);
|
|
13
29
|
|
|
14
|
-
|
|
15
|
-
// <img src={src} />
|
|
30
|
+
// Example:
|
|
31
|
+
// <img src={src} alt="QR Code" />
|
|
16
32
|
```
|
package/bundler/react.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
import { qr_png_data_url } from "../index.bundler.js";
|
|
3
|
+
|
|
4
|
+
export function useQrCode(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) => {
|
|
23
|
+
if (alive) setSrc(res);
|
|
24
|
+
})
|
|
25
|
+
.catch(() => {
|
|
26
|
+
if (alive) setSrc("");
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
return () => {
|
|
30
|
+
alive = false;
|
|
31
|
+
};
|
|
32
|
+
}, [text, size, margin, ecc]);
|
|
33
|
+
|
|
34
|
+
return src;
|
|
35
|
+
}
|
package/index.bundler.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import init, {
|
|
2
|
+
qr_png_data_url as _qr_png_data_url,
|
|
3
|
+
} from "./pkg/bundler/hqr_generate.js";
|
|
4
|
+
|
|
5
|
+
let _initPromise;
|
|
6
|
+
|
|
7
|
+
async function ensureInit() {
|
|
8
|
+
if (!_initPromise) _initPromise = init();
|
|
9
|
+
await _initPromise;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function qr_png_data_url(text, size = 320, margin = 4, ecc = "Q") {
|
|
13
|
+
await ensureInit();
|
|
14
|
+
return _qr_png_data_url(text, size, margin, ecc);
|
|
15
|
+
}
|
package/index.d.ts
CHANGED
package/index.web.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import init, {
|
|
2
|
+
qr_png_data_url as _qr_png_data_url,
|
|
3
|
+
} from "./pkg/web/hqr_generate.js";
|
|
4
|
+
|
|
5
|
+
let _initPromise;
|
|
6
|
+
|
|
7
|
+
/** @returns {Promise<void>} */
|
|
8
|
+
async function ensureInit() {
|
|
9
|
+
if (!_initPromise) _initPromise = init();
|
|
10
|
+
await _initPromise;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {string} text
|
|
15
|
+
* @param {number} [size=320]
|
|
16
|
+
* @param {number} [margin=4]
|
|
17
|
+
* @param {"L"|"M"|"Q"|"H"} [ecc="Q"]
|
|
18
|
+
* @returns {Promise<string>}
|
|
19
|
+
*/
|
|
20
|
+
export async function qr_png_data_url(text, size = 320, margin = 4, ecc = "Q") {
|
|
21
|
+
await ensureInit();
|
|
22
|
+
return _qr_png_data_url(text, size, margin, ecc);
|
|
23
|
+
}
|
package/package.json
CHANGED
|
@@ -1,30 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wirunrom/hqr-generate",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
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.
|
|
9
|
-
"index.
|
|
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.
|
|
15
|
-
"module": "./index.
|
|
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
|
-
"
|
|
21
|
-
|
|
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
|
-
"
|
|
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,32 @@
|
|
|
1
|
+
# @wirunrom/hqr-generate
|
|
2
|
+
|
|
3
|
+
A **stable black-and-white QR Code generator** that returns a **PNG Data URL**, powered by **Rust + WebAssembly (WASM)**.
|
|
4
|
+
|
|
5
|
+
- High contrast (black & white) for maximum scan reliability
|
|
6
|
+
- Designed to work across old and new mobile devices
|
|
7
|
+
- Simple API for frontend usage
|
|
8
|
+
- Compatible with React, Next.js, and modern bundlers
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @wirunrom/hqr-generate
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage (React / Next.js)
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { qr_png_data_url } from "@wirunrom/hqr-generate";
|
|
22
|
+
|
|
23
|
+
const src = await qr_png_data_url(
|
|
24
|
+
"hello world",
|
|
25
|
+
320, // image size (px)
|
|
26
|
+
4, // margin (quiet zone)
|
|
27
|
+
"Q", // error correction level: L | M | Q | H
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
// Example:
|
|
31
|
+
// <img src={src} alt="QR Code" />
|
|
32
|
+
```
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# @wirunrom/hqr-generate
|
|
2
|
+
|
|
3
|
+
A **stable black-and-white QR Code generator** that returns a **PNG Data URL**, powered by **Rust + WebAssembly (WASM)**.
|
|
4
|
+
|
|
5
|
+
- High contrast (black & white) for maximum scan reliability
|
|
6
|
+
- Designed to work across old and new mobile devices
|
|
7
|
+
- Simple API for frontend usage
|
|
8
|
+
- Compatible with React, Next.js, and modern bundlers
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @wirunrom/hqr-generate
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage (React / Next.js)
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { qr_png_data_url } from "@wirunrom/hqr-generate";
|
|
22
|
+
|
|
23
|
+
const src = await qr_png_data_url(
|
|
24
|
+
"hello world",
|
|
25
|
+
320, // image size (px)
|
|
26
|
+
4, // margin (quiet zone)
|
|
27
|
+
"Q", // error correction level: L | M | Q | H
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
// Example:
|
|
31
|
+
// <img src={src} alt="QR Code" />
|
|
32
|
+
```
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
export function qr_png_data_url(text: string, size: number, margin: number, ecc: string): string;
|
|
5
|
+
|
|
6
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
7
|
+
|
|
8
|
+
export interface InitOutput {
|
|
9
|
+
readonly memory: WebAssembly.Memory;
|
|
10
|
+
readonly qr_png_data_url: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number, number];
|
|
11
|
+
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
12
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
13
|
+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
14
|
+
readonly __externref_table_dealloc: (a: number) => void;
|
|
15
|
+
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
16
|
+
readonly __wbindgen_start: () => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
23
|
+
* a precompiled `WebAssembly.Module`.
|
|
24
|
+
*
|
|
25
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
26
|
+
*
|
|
27
|
+
* @returns {InitOutput}
|
|
28
|
+
*/
|
|
29
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
33
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
34
|
+
*
|
|
35
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
36
|
+
*
|
|
37
|
+
* @returns {Promise<InitOutput>}
|
|
38
|
+
*/
|
|
39
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/* @ts-self-types="./hqr_generate.d.ts" */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {string} text
|
|
5
|
+
* @param {number} size
|
|
6
|
+
* @param {number} margin
|
|
7
|
+
* @param {string} ecc
|
|
8
|
+
* @returns {string}
|
|
9
|
+
*/
|
|
10
|
+
export function qr_png_data_url(text, size, margin, ecc) {
|
|
11
|
+
let deferred4_0;
|
|
12
|
+
let deferred4_1;
|
|
13
|
+
try {
|
|
14
|
+
const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
15
|
+
const len0 = WASM_VECTOR_LEN;
|
|
16
|
+
const ptr1 = passStringToWasm0(ecc, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
17
|
+
const len1 = WASM_VECTOR_LEN;
|
|
18
|
+
const ret = wasm.qr_png_data_url(ptr0, len0, size, margin, ptr1, len1);
|
|
19
|
+
var ptr3 = ret[0];
|
|
20
|
+
var len3 = ret[1];
|
|
21
|
+
if (ret[3]) {
|
|
22
|
+
ptr3 = 0; len3 = 0;
|
|
23
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
24
|
+
}
|
|
25
|
+
deferred4_0 = ptr3;
|
|
26
|
+
deferred4_1 = len3;
|
|
27
|
+
return getStringFromWasm0(ptr3, len3);
|
|
28
|
+
} finally {
|
|
29
|
+
wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function __wbg_get_imports() {
|
|
34
|
+
const import0 = {
|
|
35
|
+
__proto__: null,
|
|
36
|
+
__wbindgen_cast_0000000000000001: function(arg0, arg1) {
|
|
37
|
+
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
38
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
39
|
+
return ret;
|
|
40
|
+
},
|
|
41
|
+
__wbindgen_init_externref_table: function() {
|
|
42
|
+
const table = wasm.__wbindgen_externrefs;
|
|
43
|
+
const offset = table.grow(4);
|
|
44
|
+
table.set(0, undefined);
|
|
45
|
+
table.set(offset + 0, undefined);
|
|
46
|
+
table.set(offset + 1, null);
|
|
47
|
+
table.set(offset + 2, true);
|
|
48
|
+
table.set(offset + 3, false);
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
return {
|
|
52
|
+
__proto__: null,
|
|
53
|
+
"./hqr_generate_bg.js": import0,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function getStringFromWasm0(ptr, len) {
|
|
58
|
+
ptr = ptr >>> 0;
|
|
59
|
+
return decodeText(ptr, len);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let cachedUint8ArrayMemory0 = null;
|
|
63
|
+
function getUint8ArrayMemory0() {
|
|
64
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
65
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
66
|
+
}
|
|
67
|
+
return cachedUint8ArrayMemory0;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
71
|
+
if (realloc === undefined) {
|
|
72
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
73
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
74
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
75
|
+
WASM_VECTOR_LEN = buf.length;
|
|
76
|
+
return ptr;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let len = arg.length;
|
|
80
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
81
|
+
|
|
82
|
+
const mem = getUint8ArrayMemory0();
|
|
83
|
+
|
|
84
|
+
let offset = 0;
|
|
85
|
+
|
|
86
|
+
for (; offset < len; offset++) {
|
|
87
|
+
const code = arg.charCodeAt(offset);
|
|
88
|
+
if (code > 0x7F) break;
|
|
89
|
+
mem[ptr + offset] = code;
|
|
90
|
+
}
|
|
91
|
+
if (offset !== len) {
|
|
92
|
+
if (offset !== 0) {
|
|
93
|
+
arg = arg.slice(offset);
|
|
94
|
+
}
|
|
95
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
96
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
97
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
98
|
+
|
|
99
|
+
offset += ret.written;
|
|
100
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
WASM_VECTOR_LEN = offset;
|
|
104
|
+
return ptr;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function takeFromExternrefTable0(idx) {
|
|
108
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
109
|
+
wasm.__externref_table_dealloc(idx);
|
|
110
|
+
return value;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
114
|
+
cachedTextDecoder.decode();
|
|
115
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
116
|
+
let numBytesDecoded = 0;
|
|
117
|
+
function decodeText(ptr, len) {
|
|
118
|
+
numBytesDecoded += len;
|
|
119
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
120
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
121
|
+
cachedTextDecoder.decode();
|
|
122
|
+
numBytesDecoded = len;
|
|
123
|
+
}
|
|
124
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const cachedTextEncoder = new TextEncoder();
|
|
128
|
+
|
|
129
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
130
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
131
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
132
|
+
view.set(buf);
|
|
133
|
+
return {
|
|
134
|
+
read: arg.length,
|
|
135
|
+
written: buf.length
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
let WASM_VECTOR_LEN = 0;
|
|
141
|
+
|
|
142
|
+
let wasmModule, wasm;
|
|
143
|
+
function __wbg_finalize_init(instance, module) {
|
|
144
|
+
wasm = instance.exports;
|
|
145
|
+
wasmModule = module;
|
|
146
|
+
cachedUint8ArrayMemory0 = null;
|
|
147
|
+
wasm.__wbindgen_start();
|
|
148
|
+
return wasm;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function __wbg_load(module, imports) {
|
|
152
|
+
if (typeof Response === 'function' && module instanceof Response) {
|
|
153
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
154
|
+
try {
|
|
155
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
156
|
+
} catch (e) {
|
|
157
|
+
const validResponse = module.ok && expectedResponseType(module.type);
|
|
158
|
+
|
|
159
|
+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
|
160
|
+
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);
|
|
161
|
+
|
|
162
|
+
} else { throw e; }
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const bytes = await module.arrayBuffer();
|
|
167
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
168
|
+
} else {
|
|
169
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
170
|
+
|
|
171
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
172
|
+
return { instance, module };
|
|
173
|
+
} else {
|
|
174
|
+
return instance;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function expectedResponseType(type) {
|
|
179
|
+
switch (type) {
|
|
180
|
+
case 'basic': case 'cors': case 'default': return true;
|
|
181
|
+
}
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function initSync(module) {
|
|
187
|
+
if (wasm !== undefined) return wasm;
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
if (module !== undefined) {
|
|
191
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
192
|
+
({module} = module)
|
|
193
|
+
} else {
|
|
194
|
+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const imports = __wbg_get_imports();
|
|
199
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
200
|
+
module = new WebAssembly.Module(module);
|
|
201
|
+
}
|
|
202
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
203
|
+
return __wbg_finalize_init(instance, module);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async function __wbg_init(module_or_path) {
|
|
207
|
+
if (wasm !== undefined) return wasm;
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
if (module_or_path !== undefined) {
|
|
211
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
212
|
+
({module_or_path} = module_or_path)
|
|
213
|
+
} else {
|
|
214
|
+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (module_or_path === undefined) {
|
|
219
|
+
module_or_path = new URL('hqr_generate_bg.wasm', import.meta.url);
|
|
220
|
+
}
|
|
221
|
+
const imports = __wbg_get_imports();
|
|
222
|
+
|
|
223
|
+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
|
224
|
+
module_or_path = fetch(module_or_path);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
228
|
+
|
|
229
|
+
return __wbg_finalize_init(instance, module);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export { initSync, __wbg_init as default };
|
|
Binary file
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
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];
|
|
5
|
+
export const __wbindgen_externrefs: WebAssembly.Table;
|
|
6
|
+
export const __wbindgen_malloc: (a: number, b: number) => number;
|
|
7
|
+
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
8
|
+
export const __externref_table_dealloc: (a: number) => void;
|
|
9
|
+
export const __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
10
|
+
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
|
+
}
|
package/react/index.d.ts
ADDED
package/react/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
import { qr_png_data_url } from "../index.web.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* React hook for QR Code Data URL (browser-only).
|
|
6
|
+
* @param {string} text
|
|
7
|
+
* @param {{size?:number, margin?:number, ecc?:"L"|"M"|"Q"|"H"}} [opts]
|
|
8
|
+
*/
|
|
9
|
+
export function useQrCode(text, opts) {
|
|
10
|
+
const size = opts?.size ?? 320;
|
|
11
|
+
const margin = opts?.margin ?? 4;
|
|
12
|
+
const ecc = opts?.ecc ?? "Q";
|
|
13
|
+
|
|
14
|
+
const [src, setSrc] = useState("");
|
|
15
|
+
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
let alive = true;
|
|
18
|
+
|
|
19
|
+
if (!text) {
|
|
20
|
+
setSrc("");
|
|
21
|
+
return () => {
|
|
22
|
+
alive = false;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
qr_png_data_url(text, size, margin, ecc)
|
|
27
|
+
.then((res) => {
|
|
28
|
+
if (alive) setSrc(res);
|
|
29
|
+
})
|
|
30
|
+
.catch(() => {
|
|
31
|
+
if (alive) setSrc("");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return () => {
|
|
35
|
+
alive = false;
|
|
36
|
+
};
|
|
37
|
+
}, [text, size, margin, ecc]);
|
|
38
|
+
|
|
39
|
+
return src;
|
|
40
|
+
}
|
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
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
|
-
```
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|