qr-payload-parser 1.0.0 → 1.0.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 +20 -12
- package/index.d.ts +32 -0
- package/index.js +1 -1
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -1,25 +1,33 @@
|
|
|
1
|
-
# qr-payload-parser
|
|
1
|
+
# qr-payload-parser (`@sunasty/qr-payload`)
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/qr-payload-parser)
|
|
4
|
+
[](https://jsr.io/@sunasty/qr-payload)
|
|
5
|
+
[](https://github.com/sunasty-labs/qr-payload/actions/workflows/ci.yml)
|
|
6
|
+

|
|
7
|
+

|
|
2
8
|
|
|
3
9
|
Zero-dependency JavaScript and TypeScript parser for structured QR code payloads (`WIFI:`, `otpauth://totp/`, `BEGIN:VCARD`, `mailto:`, `MATMSG:`, `smsto:`, `sms:`, `tel:`, and HTTP/HTTPS URLs with security inspection) paired with a Web Crypto `SubtleCrypto` RFC 6238 TOTP generator and RFC 4180 CSV/JSON serializers.
|
|
4
10
|
|
|
5
|
-
|
|
11
|
+
- **Documentation**: [qr-payload-parser Reference](https://sunasty-docs.readthedocs.io/en/latest/packages/javascript-qr-payload/)
|
|
12
|
+
- **Live Browser Tool**: [Sunasty QR Scanner](https://sunasty.com/qr-scan/) · [Sunasty QR Generator](https://sunasty.com/qr-code/)
|
|
6
13
|
|
|
7
14
|
## Features
|
|
8
15
|
|
|
9
16
|
- **Structured QR Payload Parser**: `parseQrPayload` normalizes raw QR strings into a strongly typed discriminated union (`wifi`, `totp`, `vcard`, `email`, `sms`, `phone`, `url`, `text`).
|
|
10
|
-
- **RFC 6238 TOTP Generator**: `generateTotpCode` computes 6-digit (or N-digit) HMAC-SHA1 time-based one-time passwords using the standard Web Crypto `SubtleCrypto` API in Node.js (>= 18), browsers, and Service Workers.
|
|
11
|
-
- **URL Security Inspection**: `analyzeUrlSecurity` detects unencrypted `http://` links and raw IPv4/IPv6 hostnames before navigation.
|
|
12
|
-
- **
|
|
13
|
-
- **Batch History Export**: `serializeScanHistoryToCsv` (RFC 4180 compliant) and `
|
|
17
|
+
- **RFC 6238 TOTP Generator**: `generateTotpCode` computes 6-digit (or N-digit) HMAC-SHA1 time-based one-time passwords using the standard Web Crypto `SubtleCrypto` API in Node.js (>= 18), browsers, and Service Workers (`decodeBase32`).
|
|
18
|
+
- **URL Security Inspection**: `analyzeUrlSecurity` detects unencrypted `http://` links and raw IPv4/IPv6 hostnames before navigation (`looksLikeUrl`).
|
|
19
|
+
- **Scan Validation & Preprocessing**: `SCAN_ACCEPTED_MIME`, `SCAN_MAX_BYTES`, `isAcceptedScanMime`, `applyHighContrastThreshold`, and `normalizeAndBinarizeLowContrast` validate and prepare RGBA buffers.
|
|
20
|
+
- **Batch History Export**: `serializeScanHistoryToCsv` (RFC 4180 compliant), `serializeScanHistoryToJson`, and `truncateForDisplay`.
|
|
14
21
|
|
|
15
22
|
## Installation
|
|
16
23
|
|
|
17
|
-
Via npm or JSR:
|
|
18
|
-
|
|
19
24
|
```bash
|
|
25
|
+
# npm (Node.js / bundlers)
|
|
20
26
|
npm install qr-payload-parser
|
|
21
|
-
|
|
27
|
+
|
|
28
|
+
# JSR (@sunasty/qr-payload for Deno / Bun / Node)
|
|
22
29
|
npx jsr add @sunasty/qr-payload
|
|
30
|
+
deno add jsr:@sunasty/qr-payload
|
|
23
31
|
```
|
|
24
32
|
|
|
25
33
|
Or import directly in browsers and Deno via ESM CDN (jsDelivr):
|
|
@@ -29,7 +37,7 @@ import {
|
|
|
29
37
|
parseQrPayload,
|
|
30
38
|
generateTotpCode,
|
|
31
39
|
analyzeUrlSecurity,
|
|
32
|
-
} from 'https://cdn.jsdelivr.net/npm/qr-payload-parser@1.0.
|
|
40
|
+
} from 'https://cdn.jsdelivr.net/npm/qr-payload-parser@1.0.2/+esm';
|
|
33
41
|
```
|
|
34
42
|
|
|
35
43
|
## Usage
|
|
@@ -37,7 +45,7 @@ import {
|
|
|
37
45
|
### Parsing Structured QR Payloads
|
|
38
46
|
|
|
39
47
|
```js
|
|
40
|
-
import { parseQrPayload } from 'qr-payload-parser';
|
|
48
|
+
import { parseQrPayload } from 'qr-payload-parser'; // or '@sunasty/qr-payload' on JSR
|
|
41
49
|
|
|
42
50
|
const wifi = parseQrPayload('WIFI:S:ProdNetwork;T:WPA2;P:SecretKey2026;H:false;;');
|
|
43
51
|
// { kind: 'wifi', ssid: 'ProdNetwork', encryption: 'WPA2', password: 'SecretKey2026', hidden: false }
|
|
@@ -91,7 +99,7 @@ const csv = serializeScanHistoryToCsv([
|
|
|
91
99
|
|
|
92
100
|
- Client-side QR code scanner from image or webcam: [https://sunasty.com/qr-scan/](https://sunasty.com/qr-scan/)
|
|
93
101
|
- Custom SVG and PNG QR code generator: [https://sunasty.com/qr-code/](https://sunasty.com/qr-code/)
|
|
94
|
-
- Chrome/Chromium MV3 extension repository: [https://github.com/
|
|
102
|
+
- Chrome/Chromium MV3 extension repository: [https://github.com/sunasty-labs/qr-scanner](https://github.com/sunasty-labs/qr-scanner)
|
|
95
103
|
|
|
96
104
|
## License
|
|
97
105
|
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-dependency structured QR payload parser (Wi-Fi, TOTP otpauth://, vCard,
|
|
3
|
+
* mailto, MATMSG, SMS, tel, URL security analyzer) and WebCrypto RFC 6238 TOTP generator.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Accepted MIME types for local QR code image scanning (`SCAN_ACCEPTED_MIME`). */
|
|
1
9
|
export declare const SCAN_ACCEPTED_MIME: string[];
|
|
10
|
+
/** Maximum image byte size accepted for local QR decoding (`SCAN_MAX_BYTES`). */
|
|
2
11
|
export declare const SCAN_MAX_BYTES: number;
|
|
3
12
|
|
|
13
|
+
/** Security inspection summary for HTTP/HTTPS and host-based URLs. */
|
|
4
14
|
export interface UrlSecurityAnalysis {
|
|
5
15
|
valid: boolean;
|
|
6
16
|
href: string;
|
|
@@ -12,6 +22,7 @@ export interface UrlSecurityAnalysis {
|
|
|
12
22
|
hasSecurityWarning: boolean;
|
|
13
23
|
}
|
|
14
24
|
|
|
25
|
+
/** Parsed `WIFI:` network QR code payload. */
|
|
15
26
|
export interface WifiPayload {
|
|
16
27
|
kind: 'wifi';
|
|
17
28
|
ssid: string;
|
|
@@ -20,6 +31,7 @@ export interface WifiPayload {
|
|
|
20
31
|
hidden: boolean;
|
|
21
32
|
}
|
|
22
33
|
|
|
34
|
+
/** Parsed `BEGIN:VCARD` contact card QR payload. */
|
|
23
35
|
export interface VcardPayload {
|
|
24
36
|
kind: 'vcard';
|
|
25
37
|
fullName: string;
|
|
@@ -29,6 +41,7 @@ export interface VcardPayload {
|
|
|
29
41
|
rawVcard: string;
|
|
30
42
|
}
|
|
31
43
|
|
|
44
|
+
/** Parsed `mailto:` or `MATMSG:` email QR payload. */
|
|
32
45
|
export interface EmailPayload {
|
|
33
46
|
kind: 'email';
|
|
34
47
|
to: string;
|
|
@@ -37,6 +50,7 @@ export interface EmailPayload {
|
|
|
37
50
|
mailtoHref: string;
|
|
38
51
|
}
|
|
39
52
|
|
|
53
|
+
/** Parsed `smsto:` or `sms:` message QR payload. */
|
|
40
54
|
export interface SmsPayload {
|
|
41
55
|
kind: 'sms';
|
|
42
56
|
number: string;
|
|
@@ -44,12 +58,14 @@ export interface SmsPayload {
|
|
|
44
58
|
smsHref: string;
|
|
45
59
|
}
|
|
46
60
|
|
|
61
|
+
/** Parsed `tel:` phone call QR payload. */
|
|
47
62
|
export interface PhonePayload {
|
|
48
63
|
kind: 'phone';
|
|
49
64
|
number: string;
|
|
50
65
|
telHref: string;
|
|
51
66
|
}
|
|
52
67
|
|
|
68
|
+
/** Parsed `otpauth://` TOTP/HOTP 2FA provisioning payload. */
|
|
53
69
|
export interface TotpPayload {
|
|
54
70
|
kind: 'totp';
|
|
55
71
|
label: string;
|
|
@@ -60,16 +76,19 @@ export interface TotpPayload {
|
|
|
60
76
|
period: number;
|
|
61
77
|
}
|
|
62
78
|
|
|
79
|
+
/** Parsed HTTP/HTTPS URL payload enriched with {@link UrlSecurityAnalysis}. */
|
|
63
80
|
export interface UrlPayload extends UrlSecurityAnalysis {
|
|
64
81
|
kind: 'url';
|
|
65
82
|
text: string;
|
|
66
83
|
}
|
|
67
84
|
|
|
85
|
+
/** Fallback plain-text QR payload when no structured URI schema matches. */
|
|
68
86
|
export interface TextPayload {
|
|
69
87
|
kind: 'text';
|
|
70
88
|
text: string;
|
|
71
89
|
}
|
|
72
90
|
|
|
91
|
+
/** Discriminated union of all structured QR payload variants returned by {@link parseQrPayload}. */
|
|
73
92
|
export type ParsedQrPayload =
|
|
74
93
|
| WifiPayload
|
|
75
94
|
| VcardPayload
|
|
@@ -80,6 +99,7 @@ export type ParsedQrPayload =
|
|
|
80
99
|
| UrlPayload
|
|
81
100
|
| TextPayload;
|
|
82
101
|
|
|
102
|
+
/** Generated RFC 6238 TOTP token and validity counter window returned by {@link generateTotpCode}. */
|
|
83
103
|
export interface TotpResult {
|
|
84
104
|
code: string;
|
|
85
105
|
remainingSeconds: number;
|
|
@@ -87,6 +107,7 @@ export interface TotpResult {
|
|
|
87
107
|
counter: number;
|
|
88
108
|
}
|
|
89
109
|
|
|
110
|
+
/** Scan history record serialized by {@link serializeScanHistoryToCsv} and {@link serializeScanHistoryToJson}. */
|
|
90
111
|
export interface ScanRecord {
|
|
91
112
|
timestamp?: string;
|
|
92
113
|
source?: string;
|
|
@@ -96,25 +117,36 @@ export interface ScanRecord {
|
|
|
96
117
|
[key: string]: unknown;
|
|
97
118
|
}
|
|
98
119
|
|
|
120
|
+
/** Returns true when `mime` is an accepted local image MIME type for QR scanning. */
|
|
99
121
|
export declare function isAcceptedScanMime(mime: string): boolean;
|
|
122
|
+
/** Returns true when raw decoded text appears to be an HTTP/HTTPS URL. */
|
|
100
123
|
export declare function looksLikeUrl(data: string): boolean;
|
|
124
|
+
/** Inspects a URL string for insecure `http:` protocol or raw IPv4/IPv6 hostnames. */
|
|
101
125
|
export declare function analyzeUrlSecurity(rawUrl: string): UrlSecurityAnalysis;
|
|
126
|
+
/** Parses a raw decoded QR string into a strongly typed {@link ParsedQrPayload} discriminated union. */
|
|
102
127
|
export declare function parseQrPayload(data: string): ParsedQrPayload;
|
|
128
|
+
/** Decodes an RFC 4648 Base32 secret string into raw key bytes. */
|
|
103
129
|
export declare function decodeBase32(base32: string): Uint8Array;
|
|
130
|
+
/** Generates an RFC 6238 HMAC-SHA1 TOTP code asynchronously using Web Crypto `SubtleCrypto`. */
|
|
104
131
|
export declare function generateTotpCode(
|
|
105
132
|
secretBase32: string,
|
|
106
133
|
timestampMs?: number,
|
|
107
134
|
period?: number,
|
|
108
135
|
digits?: number
|
|
109
136
|
): Promise<TotpResult>;
|
|
137
|
+
/** Applies a luminance threshold (with optional inversion) to an RGBA pixel buffer. */
|
|
110
138
|
export declare function applyHighContrastThreshold(
|
|
111
139
|
data: Uint8ClampedArray,
|
|
112
140
|
threshold?: number,
|
|
113
141
|
invert?: boolean
|
|
114
142
|
): Uint8ClampedArray;
|
|
143
|
+
/** Normalizes and binarizes low-contrast RGBA image data prior to QR decoding. */
|
|
115
144
|
export declare function normalizeAndBinarizeLowContrast(
|
|
116
145
|
data: Uint8ClampedArray
|
|
117
146
|
): Uint8ClampedArray;
|
|
147
|
+
/** Exports scan history records as RFC 4180 CSV with spreadsheet formula-injection neutralization. */
|
|
118
148
|
export declare function serializeScanHistoryToCsv(results: ScanRecord[]): string;
|
|
149
|
+
/** Exports scan history records as pretty-printed JSON. */
|
|
119
150
|
export declare function serializeScanHistoryToJson(results: Array<Record<string, unknown>>): string;
|
|
151
|
+
/** Truncates long QR payload text safely for UI display up to `maxLen`. */
|
|
120
152
|
export declare function truncateForDisplay(data: string, maxLen?: number): string;
|
package/index.js
CHANGED
|
@@ -55,7 +55,7 @@ export function analyzeUrlSecurity(rawUrl) {
|
|
|
55
55
|
const isRawIp =
|
|
56
56
|
/^\d{1,3}(?:\.\d{1,3}){3}$/.test(hostname) ||
|
|
57
57
|
hostname.startsWith('[') ||
|
|
58
|
-
/^[0-9a-f:]+$/i.test(hostname);
|
|
58
|
+
(hostname.includes(':') && /^[0-9a-f:]+$/i.test(hostname));
|
|
59
59
|
const pathAndQuery =
|
|
60
60
|
parsed.pathname === '/' && !parsed.search && !parsed.hash
|
|
61
61
|
? ''
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qr-payload-parser",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Zero-dependency structured QR payload parser (Wi-Fi, 2FA TOTP otpauth://, vCard, mailto, MATMSG, SMS, tel, URL security analyzer) and RFC 6238 TOTP generator via Web Crypto SubtleCrypto.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|
|
@@ -30,17 +30,18 @@
|
|
|
30
30
|
"vcard",
|
|
31
31
|
"matmsg",
|
|
32
32
|
"parser",
|
|
33
|
-
"webcrypto"
|
|
33
|
+
"webcrypto",
|
|
34
|
+
"zero-dependencies"
|
|
34
35
|
],
|
|
35
36
|
"author": "Sunasty <https://sunasty.com>",
|
|
36
37
|
"license": "MIT",
|
|
37
38
|
"homepage": "https://sunasty.com/qr-scan/",
|
|
38
39
|
"repository": {
|
|
39
40
|
"type": "git",
|
|
40
|
-
"url": "git+https://github.com/
|
|
41
|
+
"url": "git+https://github.com/sunasty-labs/qr-payload.git"
|
|
41
42
|
},
|
|
42
43
|
"bugs": {
|
|
43
|
-
"url": "https://github.com/
|
|
44
|
+
"url": "https://github.com/sunasty-labs/qr-payload/issues"
|
|
44
45
|
},
|
|
45
46
|
"engines": {
|
|
46
47
|
"node": ">=18.0.0"
|