qr-payload-parser 1.0.0 → 1.0.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/LICENSE +21 -21
- package/README.md +104 -98
- package/index.d.ts +120 -120
- package/index.js +479 -479
- package/package.json +5 -4
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,98 +1,104 @@
|
|
|
1
|
-
# qr-payload-parser
|
|
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
|
-
|
|
1
|
+
# qr-payload-parser
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/qr-payload-parser)
|
|
4
|
+
[](https://github.com/sunasty-labs/qr-payload/actions/workflows/ci.yml)
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+
|
|
8
|
+
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.
|
|
9
|
+
|
|
10
|
+
- **Documentation**: [Sunasty Labs Docs](https://sunasty-docs.readthedocs.io/en/latest/packages/javascript-qr-payload/)
|
|
11
|
+
- **Live Browser Tool**: [Sunasty QR Scanner](https://sunasty.com/qr-scan/) · [Sunasty QR Generator](https://sunasty.com/qr/)
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- **Structured QR Payload Parser**: `parseQrPayload` normalizes raw QR strings into a strongly typed discriminated union (`wifi`, `totp`, `vcard`, `email`, `sms`, `phone`, `url`, `text`).
|
|
16
|
+
- **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.
|
|
17
|
+
- **URL Security Inspection**: `analyzeUrlSecurity` detects unencrypted `http://` links and raw IPv4/IPv6 hostnames before navigation.
|
|
18
|
+
- **Low-Contrast Image Preprocessors**: `applyHighContrastThreshold` and `normalizeAndBinarizeLowContrast` prepare RGBA buffers for QR matrix detection.
|
|
19
|
+
- **Batch History Export**: `serializeScanHistoryToCsv` (RFC 4180 compliant) and `serializeScanHistoryToJson`.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
Via npm or JSR:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install qr-payload-parser
|
|
27
|
+
# or via JSR
|
|
28
|
+
npx jsr add @sunasty/qr-payload
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Or import directly in browsers and Deno via ESM CDN (jsDelivr):
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
import {
|
|
35
|
+
parseQrPayload,
|
|
36
|
+
generateTotpCode,
|
|
37
|
+
analyzeUrlSecurity,
|
|
38
|
+
} from 'https://cdn.jsdelivr.net/npm/qr-payload-parser@1.0.0/+esm';
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
### Parsing Structured QR Payloads
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
import { parseQrPayload } from 'qr-payload-parser';
|
|
47
|
+
|
|
48
|
+
const wifi = parseQrPayload('WIFI:S:ProdNetwork;T:WPA2;P:SecretKey2026;H:false;;');
|
|
49
|
+
// { kind: 'wifi', ssid: 'ProdNetwork', encryption: 'WPA2', password: 'SecretKey2026', hidden: false }
|
|
50
|
+
|
|
51
|
+
const vcard = parseQrPayload(
|
|
52
|
+
'BEGIN:VCARD\nVERSION:3.0\nFN:Alex Martin\nORG:Sunasty\nTEL:+33123456789\nEMAIL:alex@sunasty.com\nEND:VCARD'
|
|
53
|
+
);
|
|
54
|
+
// { kind: 'vcard', fullName: 'Alex Martin', org: 'Sunasty', phone: '+33123456789', email: 'alex@sunasty.com', ... }
|
|
55
|
+
|
|
56
|
+
const urlInfo = parseQrPayload('http://192.168.1.1/login');
|
|
57
|
+
// { kind: 'url', domain: '192.168.1.1', isInsecureHttp: true, isRawIp: true, hasSecurityWarning: true, ... }
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Generating RFC 6238 2FA TOTP Codes
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
import { parseQrPayload, generateTotpCode } from 'qr-payload-parser';
|
|
64
|
+
|
|
65
|
+
const parsed = parseQrPayload(
|
|
66
|
+
'otpauth://totp/Sunasty:ops@sunasty.com?secret=JBSWY3DPEHPK3PXP&issuer=Sunasty'
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
if (parsed.kind === 'totp') {
|
|
70
|
+
const { code, remainingSeconds } = await generateTotpCode(
|
|
71
|
+
parsed.secret,
|
|
72
|
+
Date.now(),
|
|
73
|
+
parsed.period,
|
|
74
|
+
parsed.digits
|
|
75
|
+
);
|
|
76
|
+
console.log(`Current 2FA code: ${code} (valid for ${remainingSeconds}s)`);
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Exporting Scan History to RFC 4180 CSV
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
import { serializeScanHistoryToCsv } from 'qr-payload-parser';
|
|
84
|
+
|
|
85
|
+
const csv = serializeScanHistoryToCsv([
|
|
86
|
+
{
|
|
87
|
+
timestamp: '2026-10-22T12:00:00.000Z',
|
|
88
|
+
source: 'camera',
|
|
89
|
+
type: 'url',
|
|
90
|
+
isUrl: true,
|
|
91
|
+
data: 'https://sunasty.com/qr-scan/',
|
|
92
|
+
},
|
|
93
|
+
]);
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Web Utilities
|
|
97
|
+
|
|
98
|
+
- Client-side QR code scanner from image or webcam: [https://sunasty.com/qr-scan/](https://sunasty.com/qr-scan/)
|
|
99
|
+
- Custom SVG and PNG QR code generator: [https://sunasty.com/qr-code/](https://sunasty.com/qr-code/)
|
|
100
|
+
- Chrome/Chromium MV3 extension repository: [https://github.com/sunasty-labs/qr-scanner](https://github.com/sunasty-labs/qr-scanner)
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
MIT
|
package/index.d.ts
CHANGED
|
@@ -1,120 +1,120 @@
|
|
|
1
|
-
export declare const SCAN_ACCEPTED_MIME: string[];
|
|
2
|
-
export declare const SCAN_MAX_BYTES: number;
|
|
3
|
-
|
|
4
|
-
export interface UrlSecurityAnalysis {
|
|
5
|
-
valid: boolean;
|
|
6
|
-
href: string;
|
|
7
|
-
protocol: string;
|
|
8
|
-
domain: string;
|
|
9
|
-
pathAndQuery: string;
|
|
10
|
-
isInsecureHttp: boolean;
|
|
11
|
-
isRawIp: boolean;
|
|
12
|
-
hasSecurityWarning: boolean;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface WifiPayload {
|
|
16
|
-
kind: 'wifi';
|
|
17
|
-
ssid: string;
|
|
18
|
-
encryption: string;
|
|
19
|
-
password: string;
|
|
20
|
-
hidden: boolean;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface VcardPayload {
|
|
24
|
-
kind: 'vcard';
|
|
25
|
-
fullName: string;
|
|
26
|
-
phone: string;
|
|
27
|
-
email: string;
|
|
28
|
-
org: string;
|
|
29
|
-
rawVcard: string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface EmailPayload {
|
|
33
|
-
kind: 'email';
|
|
34
|
-
to: string;
|
|
35
|
-
subject: string;
|
|
36
|
-
body: string;
|
|
37
|
-
mailtoHref: string;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface SmsPayload {
|
|
41
|
-
kind: 'sms';
|
|
42
|
-
number: string;
|
|
43
|
-
message: string;
|
|
44
|
-
smsHref: string;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export interface PhonePayload {
|
|
48
|
-
kind: 'phone';
|
|
49
|
-
number: string;
|
|
50
|
-
telHref: string;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export interface TotpPayload {
|
|
54
|
-
kind: 'totp';
|
|
55
|
-
label: string;
|
|
56
|
-
account: string;
|
|
57
|
-
issuer: string;
|
|
58
|
-
secret: string;
|
|
59
|
-
digits: number;
|
|
60
|
-
period: number;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export interface UrlPayload extends UrlSecurityAnalysis {
|
|
64
|
-
kind: 'url';
|
|
65
|
-
text: string;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export interface TextPayload {
|
|
69
|
-
kind: 'text';
|
|
70
|
-
text: string;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export type ParsedQrPayload =
|
|
74
|
-
| WifiPayload
|
|
75
|
-
| VcardPayload
|
|
76
|
-
| EmailPayload
|
|
77
|
-
| SmsPayload
|
|
78
|
-
| PhonePayload
|
|
79
|
-
| TotpPayload
|
|
80
|
-
| UrlPayload
|
|
81
|
-
| TextPayload;
|
|
82
|
-
|
|
83
|
-
export interface TotpResult {
|
|
84
|
-
code: string;
|
|
85
|
-
remainingSeconds: number;
|
|
86
|
-
period: number;
|
|
87
|
-
counter: number;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export interface ScanRecord {
|
|
91
|
-
timestamp?: string;
|
|
92
|
-
source?: string;
|
|
93
|
-
type?: string;
|
|
94
|
-
isUrl?: boolean;
|
|
95
|
-
data: string;
|
|
96
|
-
[key: string]: unknown;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export declare function isAcceptedScanMime(mime: string): boolean;
|
|
100
|
-
export declare function looksLikeUrl(data: string): boolean;
|
|
101
|
-
export declare function analyzeUrlSecurity(rawUrl: string): UrlSecurityAnalysis;
|
|
102
|
-
export declare function parseQrPayload(data: string): ParsedQrPayload;
|
|
103
|
-
export declare function decodeBase32(base32: string): Uint8Array;
|
|
104
|
-
export declare function generateTotpCode(
|
|
105
|
-
secretBase32: string,
|
|
106
|
-
timestampMs?: number,
|
|
107
|
-
period?: number,
|
|
108
|
-
digits?: number
|
|
109
|
-
): Promise<TotpResult>;
|
|
110
|
-
export declare function applyHighContrastThreshold(
|
|
111
|
-
data: Uint8ClampedArray,
|
|
112
|
-
threshold?: number,
|
|
113
|
-
invert?: boolean
|
|
114
|
-
): Uint8ClampedArray;
|
|
115
|
-
export declare function normalizeAndBinarizeLowContrast(
|
|
116
|
-
data: Uint8ClampedArray
|
|
117
|
-
): Uint8ClampedArray;
|
|
118
|
-
export declare function serializeScanHistoryToCsv(results: ScanRecord[]): string;
|
|
119
|
-
export declare function serializeScanHistoryToJson(results: Array<Record<string, unknown>>): string;
|
|
120
|
-
export declare function truncateForDisplay(data: string, maxLen?: number): string;
|
|
1
|
+
export declare const SCAN_ACCEPTED_MIME: string[];
|
|
2
|
+
export declare const SCAN_MAX_BYTES: number;
|
|
3
|
+
|
|
4
|
+
export interface UrlSecurityAnalysis {
|
|
5
|
+
valid: boolean;
|
|
6
|
+
href: string;
|
|
7
|
+
protocol: string;
|
|
8
|
+
domain: string;
|
|
9
|
+
pathAndQuery: string;
|
|
10
|
+
isInsecureHttp: boolean;
|
|
11
|
+
isRawIp: boolean;
|
|
12
|
+
hasSecurityWarning: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface WifiPayload {
|
|
16
|
+
kind: 'wifi';
|
|
17
|
+
ssid: string;
|
|
18
|
+
encryption: string;
|
|
19
|
+
password: string;
|
|
20
|
+
hidden: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface VcardPayload {
|
|
24
|
+
kind: 'vcard';
|
|
25
|
+
fullName: string;
|
|
26
|
+
phone: string;
|
|
27
|
+
email: string;
|
|
28
|
+
org: string;
|
|
29
|
+
rawVcard: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface EmailPayload {
|
|
33
|
+
kind: 'email';
|
|
34
|
+
to: string;
|
|
35
|
+
subject: string;
|
|
36
|
+
body: string;
|
|
37
|
+
mailtoHref: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface SmsPayload {
|
|
41
|
+
kind: 'sms';
|
|
42
|
+
number: string;
|
|
43
|
+
message: string;
|
|
44
|
+
smsHref: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface PhonePayload {
|
|
48
|
+
kind: 'phone';
|
|
49
|
+
number: string;
|
|
50
|
+
telHref: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface TotpPayload {
|
|
54
|
+
kind: 'totp';
|
|
55
|
+
label: string;
|
|
56
|
+
account: string;
|
|
57
|
+
issuer: string;
|
|
58
|
+
secret: string;
|
|
59
|
+
digits: number;
|
|
60
|
+
period: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface UrlPayload extends UrlSecurityAnalysis {
|
|
64
|
+
kind: 'url';
|
|
65
|
+
text: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface TextPayload {
|
|
69
|
+
kind: 'text';
|
|
70
|
+
text: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export type ParsedQrPayload =
|
|
74
|
+
| WifiPayload
|
|
75
|
+
| VcardPayload
|
|
76
|
+
| EmailPayload
|
|
77
|
+
| SmsPayload
|
|
78
|
+
| PhonePayload
|
|
79
|
+
| TotpPayload
|
|
80
|
+
| UrlPayload
|
|
81
|
+
| TextPayload;
|
|
82
|
+
|
|
83
|
+
export interface TotpResult {
|
|
84
|
+
code: string;
|
|
85
|
+
remainingSeconds: number;
|
|
86
|
+
period: number;
|
|
87
|
+
counter: number;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface ScanRecord {
|
|
91
|
+
timestamp?: string;
|
|
92
|
+
source?: string;
|
|
93
|
+
type?: string;
|
|
94
|
+
isUrl?: boolean;
|
|
95
|
+
data: string;
|
|
96
|
+
[key: string]: unknown;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export declare function isAcceptedScanMime(mime: string): boolean;
|
|
100
|
+
export declare function looksLikeUrl(data: string): boolean;
|
|
101
|
+
export declare function analyzeUrlSecurity(rawUrl: string): UrlSecurityAnalysis;
|
|
102
|
+
export declare function parseQrPayload(data: string): ParsedQrPayload;
|
|
103
|
+
export declare function decodeBase32(base32: string): Uint8Array;
|
|
104
|
+
export declare function generateTotpCode(
|
|
105
|
+
secretBase32: string,
|
|
106
|
+
timestampMs?: number,
|
|
107
|
+
period?: number,
|
|
108
|
+
digits?: number
|
|
109
|
+
): Promise<TotpResult>;
|
|
110
|
+
export declare function applyHighContrastThreshold(
|
|
111
|
+
data: Uint8ClampedArray,
|
|
112
|
+
threshold?: number,
|
|
113
|
+
invert?: boolean
|
|
114
|
+
): Uint8ClampedArray;
|
|
115
|
+
export declare function normalizeAndBinarizeLowContrast(
|
|
116
|
+
data: Uint8ClampedArray
|
|
117
|
+
): Uint8ClampedArray;
|
|
118
|
+
export declare function serializeScanHistoryToCsv(results: ScanRecord[]): string;
|
|
119
|
+
export declare function serializeScanHistoryToJson(results: Array<Record<string, unknown>>): string;
|
|
120
|
+
export declare function truncateForDisplay(data: string, maxLen?: number): string;
|