qr-payload-parser 1.0.1 → 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/LICENSE +21 -21
- package/README.md +106 -104
- package/index.d.ts +152 -120
- package/index.js +479 -479
- package/package.json +1 -1
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Sunasty (https://sunasty.com)
|
|
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.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sunasty (https://sunasty.com)
|
|
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/README.md
CHANGED
|
@@ -1,104 +1,106 @@
|
|
|
1
|
-
# qr-payload-parser
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/qr-payload-parser)
|
|
4
|
-
[](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
|
+

|
|
8
|
+
|
|
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.
|
|
10
|
+
|
|
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/)
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- **Structured QR Payload Parser**: `parseQrPayload` normalizes raw QR strings into a strongly typed discriminated union (`wifi`, `totp`, `vcard`, `email`, `sms`, `phone`, `url`, `text`).
|
|
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`.
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
# npm (Node.js / bundlers)
|
|
26
|
+
npm install qr-payload-parser
|
|
27
|
+
|
|
28
|
+
# JSR (@sunasty/qr-payload for Deno / Bun / Node)
|
|
29
|
+
npx jsr add @sunasty/qr-payload
|
|
30
|
+
deno add jsr:@sunasty/qr-payload
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Or import directly in browsers and Deno via ESM CDN (jsDelivr):
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
import {
|
|
37
|
+
parseQrPayload,
|
|
38
|
+
generateTotpCode,
|
|
39
|
+
analyzeUrlSecurity,
|
|
40
|
+
} from 'https://cdn.jsdelivr.net/npm/qr-payload-parser@1.0.2/+esm';
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Usage
|
|
44
|
+
|
|
45
|
+
### Parsing Structured QR Payloads
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
import { parseQrPayload } from 'qr-payload-parser'; // or '@sunasty/qr-payload' on JSR
|
|
49
|
+
|
|
50
|
+
const wifi = parseQrPayload('WIFI:S:ProdNetwork;T:WPA2;P:SecretKey2026;H:false;;');
|
|
51
|
+
// { kind: 'wifi', ssid: 'ProdNetwork', encryption: 'WPA2', password: 'SecretKey2026', hidden: false }
|
|
52
|
+
|
|
53
|
+
const vcard = parseQrPayload(
|
|
54
|
+
'BEGIN:VCARD\nVERSION:3.0\nFN:Alex Martin\nORG:Sunasty\nTEL:+33123456789\nEMAIL:alex@sunasty.com\nEND:VCARD'
|
|
55
|
+
);
|
|
56
|
+
// { kind: 'vcard', fullName: 'Alex Martin', org: 'Sunasty', phone: '+33123456789', email: 'alex@sunasty.com', ... }
|
|
57
|
+
|
|
58
|
+
const urlInfo = parseQrPayload('http://192.168.1.1/login');
|
|
59
|
+
// { kind: 'url', domain: '192.168.1.1', isInsecureHttp: true, isRawIp: true, hasSecurityWarning: true, ... }
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Generating RFC 6238 2FA TOTP Codes
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
import { parseQrPayload, generateTotpCode } from 'qr-payload-parser';
|
|
66
|
+
|
|
67
|
+
const parsed = parseQrPayload(
|
|
68
|
+
'otpauth://totp/Sunasty:ops@sunasty.com?secret=JBSWY3DPEHPK3PXP&issuer=Sunasty'
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
if (parsed.kind === 'totp') {
|
|
72
|
+
const { code, remainingSeconds } = await generateTotpCode(
|
|
73
|
+
parsed.secret,
|
|
74
|
+
Date.now(),
|
|
75
|
+
parsed.period,
|
|
76
|
+
parsed.digits
|
|
77
|
+
);
|
|
78
|
+
console.log(`Current 2FA code: ${code} (valid for ${remainingSeconds}s)`);
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Exporting Scan History to RFC 4180 CSV
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
import { serializeScanHistoryToCsv } from 'qr-payload-parser';
|
|
86
|
+
|
|
87
|
+
const csv = serializeScanHistoryToCsv([
|
|
88
|
+
{
|
|
89
|
+
timestamp: '2026-10-22T12:00:00.000Z',
|
|
90
|
+
source: 'camera',
|
|
91
|
+
type: 'url',
|
|
92
|
+
isUrl: true,
|
|
93
|
+
data: 'https://sunasty.com/qr-scan/',
|
|
94
|
+
},
|
|
95
|
+
]);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Web Utilities
|
|
99
|
+
|
|
100
|
+
- Client-side QR code scanner from image or webcam: [https://sunasty.com/qr-scan/](https://sunasty.com/qr-scan/)
|
|
101
|
+
- Custom SVG and PNG QR code generator: [https://sunasty.com/qr-code/](https://sunasty.com/qr-code/)
|
|
102
|
+
- Chrome/Chromium MV3 extension repository: [https://github.com/sunasty-labs/qr-scanner](https://github.com/sunasty-labs/qr-scanner)
|
|
103
|
+
|
|
104
|
+
## License
|
|
105
|
+
|
|
106
|
+
MIT
|
package/index.d.ts
CHANGED
|
@@ -1,120 +1,152 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
export
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
data:
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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`). */
|
|
9
|
+
export declare const SCAN_ACCEPTED_MIME: string[];
|
|
10
|
+
/** Maximum image byte size accepted for local QR decoding (`SCAN_MAX_BYTES`). */
|
|
11
|
+
export declare const SCAN_MAX_BYTES: number;
|
|
12
|
+
|
|
13
|
+
/** Security inspection summary for HTTP/HTTPS and host-based URLs. */
|
|
14
|
+
export interface UrlSecurityAnalysis {
|
|
15
|
+
valid: boolean;
|
|
16
|
+
href: string;
|
|
17
|
+
protocol: string;
|
|
18
|
+
domain: string;
|
|
19
|
+
pathAndQuery: string;
|
|
20
|
+
isInsecureHttp: boolean;
|
|
21
|
+
isRawIp: boolean;
|
|
22
|
+
hasSecurityWarning: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Parsed `WIFI:` network QR code payload. */
|
|
26
|
+
export interface WifiPayload {
|
|
27
|
+
kind: 'wifi';
|
|
28
|
+
ssid: string;
|
|
29
|
+
encryption: string;
|
|
30
|
+
password: string;
|
|
31
|
+
hidden: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Parsed `BEGIN:VCARD` contact card QR payload. */
|
|
35
|
+
export interface VcardPayload {
|
|
36
|
+
kind: 'vcard';
|
|
37
|
+
fullName: string;
|
|
38
|
+
phone: string;
|
|
39
|
+
email: string;
|
|
40
|
+
org: string;
|
|
41
|
+
rawVcard: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Parsed `mailto:` or `MATMSG:` email QR payload. */
|
|
45
|
+
export interface EmailPayload {
|
|
46
|
+
kind: 'email';
|
|
47
|
+
to: string;
|
|
48
|
+
subject: string;
|
|
49
|
+
body: string;
|
|
50
|
+
mailtoHref: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Parsed `smsto:` or `sms:` message QR payload. */
|
|
54
|
+
export interface SmsPayload {
|
|
55
|
+
kind: 'sms';
|
|
56
|
+
number: string;
|
|
57
|
+
message: string;
|
|
58
|
+
smsHref: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Parsed `tel:` phone call QR payload. */
|
|
62
|
+
export interface PhonePayload {
|
|
63
|
+
kind: 'phone';
|
|
64
|
+
number: string;
|
|
65
|
+
telHref: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Parsed `otpauth://` TOTP/HOTP 2FA provisioning payload. */
|
|
69
|
+
export interface TotpPayload {
|
|
70
|
+
kind: 'totp';
|
|
71
|
+
label: string;
|
|
72
|
+
account: string;
|
|
73
|
+
issuer: string;
|
|
74
|
+
secret: string;
|
|
75
|
+
digits: number;
|
|
76
|
+
period: number;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Parsed HTTP/HTTPS URL payload enriched with {@link UrlSecurityAnalysis}. */
|
|
80
|
+
export interface UrlPayload extends UrlSecurityAnalysis {
|
|
81
|
+
kind: 'url';
|
|
82
|
+
text: string;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Fallback plain-text QR payload when no structured URI schema matches. */
|
|
86
|
+
export interface TextPayload {
|
|
87
|
+
kind: 'text';
|
|
88
|
+
text: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Discriminated union of all structured QR payload variants returned by {@link parseQrPayload}. */
|
|
92
|
+
export type ParsedQrPayload =
|
|
93
|
+
| WifiPayload
|
|
94
|
+
| VcardPayload
|
|
95
|
+
| EmailPayload
|
|
96
|
+
| SmsPayload
|
|
97
|
+
| PhonePayload
|
|
98
|
+
| TotpPayload
|
|
99
|
+
| UrlPayload
|
|
100
|
+
| TextPayload;
|
|
101
|
+
|
|
102
|
+
/** Generated RFC 6238 TOTP token and validity counter window returned by {@link generateTotpCode}. */
|
|
103
|
+
export interface TotpResult {
|
|
104
|
+
code: string;
|
|
105
|
+
remainingSeconds: number;
|
|
106
|
+
period: number;
|
|
107
|
+
counter: number;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Scan history record serialized by {@link serializeScanHistoryToCsv} and {@link serializeScanHistoryToJson}. */
|
|
111
|
+
export interface ScanRecord {
|
|
112
|
+
timestamp?: string;
|
|
113
|
+
source?: string;
|
|
114
|
+
type?: string;
|
|
115
|
+
isUrl?: boolean;
|
|
116
|
+
data: string;
|
|
117
|
+
[key: string]: unknown;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Returns true when `mime` is an accepted local image MIME type for QR scanning. */
|
|
121
|
+
export declare function isAcceptedScanMime(mime: string): boolean;
|
|
122
|
+
/** Returns true when raw decoded text appears to be an HTTP/HTTPS URL. */
|
|
123
|
+
export declare function looksLikeUrl(data: string): boolean;
|
|
124
|
+
/** Inspects a URL string for insecure `http:` protocol or raw IPv4/IPv6 hostnames. */
|
|
125
|
+
export declare function analyzeUrlSecurity(rawUrl: string): UrlSecurityAnalysis;
|
|
126
|
+
/** Parses a raw decoded QR string into a strongly typed {@link ParsedQrPayload} discriminated union. */
|
|
127
|
+
export declare function parseQrPayload(data: string): ParsedQrPayload;
|
|
128
|
+
/** Decodes an RFC 4648 Base32 secret string into raw key bytes. */
|
|
129
|
+
export declare function decodeBase32(base32: string): Uint8Array;
|
|
130
|
+
/** Generates an RFC 6238 HMAC-SHA1 TOTP code asynchronously using Web Crypto `SubtleCrypto`. */
|
|
131
|
+
export declare function generateTotpCode(
|
|
132
|
+
secretBase32: string,
|
|
133
|
+
timestampMs?: number,
|
|
134
|
+
period?: number,
|
|
135
|
+
digits?: number
|
|
136
|
+
): Promise<TotpResult>;
|
|
137
|
+
/** Applies a luminance threshold (with optional inversion) to an RGBA pixel buffer. */
|
|
138
|
+
export declare function applyHighContrastThreshold(
|
|
139
|
+
data: Uint8ClampedArray,
|
|
140
|
+
threshold?: number,
|
|
141
|
+
invert?: boolean
|
|
142
|
+
): Uint8ClampedArray;
|
|
143
|
+
/** Normalizes and binarizes low-contrast RGBA image data prior to QR decoding. */
|
|
144
|
+
export declare function normalizeAndBinarizeLowContrast(
|
|
145
|
+
data: Uint8ClampedArray
|
|
146
|
+
): Uint8ClampedArray;
|
|
147
|
+
/** Exports scan history records as RFC 4180 CSV with spreadsheet formula-injection neutralization. */
|
|
148
|
+
export declare function serializeScanHistoryToCsv(results: ScanRecord[]): string;
|
|
149
|
+
/** Exports scan history records as pretty-printed JSON. */
|
|
150
|
+
export declare function serializeScanHistoryToJson(results: Array<Record<string, unknown>>): string;
|
|
151
|
+
/** Truncates long QR payload text safely for UI display up to `maxLen`. */
|
|
152
|
+
export declare function truncateForDisplay(data: string, maxLen?: number): string;
|