@blamejs/blamejs-shop 0.1.28 → 0.1.30

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.
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+ /**
3
+ * @module b.base32
4
+ * @nav Data
5
+ * @title Base32
6
+ *
7
+ * @intro
8
+ * Encode and decode <a href="https://www.rfc-editor.org/rfc/rfc4648">RFC
9
+ * 4648</a> Base32 — the case-insensitive, digit-light alphabet used for
10
+ * TOTP / 2FA secrets, DNSSEC NSEC3 hashes, and human-transcribable
11
+ * identifiers. Both RFC 4648 variants are supported: the standard
12
+ * alphabet (<code>variant: "rfc4648"</code>, default) and the
13
+ * extended-hex alphabet (<code>variant: "rfc4648-hex"</code>, which sorts
14
+ * in the same order as the underlying bytes).
15
+ *
16
+ * <code>encode</code> pads to an 8-character boundary with
17
+ * <code>=</code> by default (pass <code>padding: false</code> for the
18
+ * bare form TOTP key URIs use). <code>decode</code> is strict by default
19
+ * — it rejects any character outside the alphabet — but
20
+ * <code>loose: true</code> accepts the real-world shapes humans produce:
21
+ * lower-case input, embedded spaces and dashes, and missing padding.
22
+ *
23
+ * @card
24
+ * RFC 4648 Base32 encode / decode (standard + extended-hex alphabets,
25
+ * padded or bare, strict or lenient) — the codec behind TOTP secrets and
26
+ * transcribable identifiers.
27
+ */
28
+
29
+ var { defineClass } = require("./framework-error");
30
+
31
+ var Base32Error = defineClass("Base32Error", { alwaysPermanent: true });
32
+
33
+ var ALPHABETS = {
34
+ "rfc4648": "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
35
+ "rfc4648-hex": "0123456789ABCDEFGHIJKLMNOPQRSTUV",
36
+ };
37
+ // Reverse lookups per variant: char-code → 5-bit value.
38
+ var LOOKUPS = {};
39
+ Object.keys(ALPHABETS).forEach(function (v) {
40
+ var map = {};
41
+ for (var i = 0; i < ALPHABETS[v].length; i++) map[ALPHABETS[v].charAt(i)] = i;
42
+ LOOKUPS[v] = map;
43
+ });
44
+
45
+ var GROUP = 8; // allow:raw-byte-literal — Base32 emits 8 chars per 5 input bytes (RFC 4648 §6)
46
+ var BITS = 5; // 5 bits per Base32 symbol
47
+
48
+ function _alphabet(variant) {
49
+ var a = ALPHABETS[variant || "rfc4648"];
50
+ if (!a) throw new Base32Error("base32/bad-variant", "base32: variant must be 'rfc4648' or 'rfc4648-hex'");
51
+ return a;
52
+ }
53
+
54
+ /**
55
+ * @primitive b.base32.encode
56
+ * @signature b.base32.encode(input, opts?)
57
+ * @since 0.12.65
58
+ * @status stable
59
+ * @related b.base32.decode
60
+ *
61
+ * Encode a Buffer (or Uint8Array) to an RFC 4648 Base32 string. Output is
62
+ * padded to an 8-character boundary with <code>=</code> unless
63
+ * <code>padding: false</code>. The empty input encodes to the empty string.
64
+ *
65
+ * @opts
66
+ * variant: "rfc4648" | "rfc4648-hex", // default: "rfc4648"
67
+ * padding: boolean, // default: true
68
+ *
69
+ * @example
70
+ * b.base32.encode(Buffer.from("foobar"));
71
+ * // → "MFRGGZDFMZTWQ===="
72
+ */
73
+ function encode(input, opts) {
74
+ opts = opts || {};
75
+ var buf;
76
+ if (Buffer.isBuffer(input)) buf = input;
77
+ else if (input instanceof Uint8Array) buf = Buffer.from(input);
78
+ else throw new Base32Error("base32/bad-input", "base32.encode: input must be a Buffer or Uint8Array");
79
+ var alphabet = _alphabet(opts.variant);
80
+ var pad = opts.padding !== false;
81
+
82
+ var out = "";
83
+ var value = 0, bits = 0;
84
+ for (var i = 0; i < buf.length; i++) {
85
+ value = (value << 8) | buf[i]; // allow:raw-byte-literal — shift in one input byte
86
+ bits += 8; // allow:raw-byte-literal — eight bits per input byte
87
+ while (bits >= BITS) {
88
+ out += alphabet.charAt((value >>> (bits - BITS)) & 31); // allow:raw-byte-literal — low 5 bits mask (2^5 - 1)
89
+ bits -= BITS;
90
+ }
91
+ }
92
+ if (bits > 0) out += alphabet.charAt((value << (BITS - bits)) & 31); // allow:raw-byte-literal — final partial group, low 5 bits
93
+ if (pad) while (out.length % GROUP !== 0) out += "=";
94
+ return out;
95
+ }
96
+
97
+ /**
98
+ * @primitive b.base32.decode
99
+ * @signature b.base32.decode(str, opts?)
100
+ * @since 0.12.65
101
+ * @status stable
102
+ * @related b.base32.encode
103
+ *
104
+ * Decode an RFC 4648 Base32 string to a Buffer. Strict by default: any
105
+ * character outside the variant's alphabet (other than trailing
106
+ * <code>=</code> padding) throws <code>Base32Error</code>. With
107
+ * <code>loose: true</code> the decoder up-cases the input and ignores
108
+ * embedded spaces and dashes (and missing padding) — the shapes TOTP keys
109
+ * and hand-typed codes take.
110
+ *
111
+ * @opts
112
+ * variant: "rfc4648" | "rfc4648-hex", // default: "rfc4648"
113
+ * loose: boolean, // default: false
114
+ *
115
+ * @example
116
+ * b.base32.decode("MFRGGZDFMZTWQ====").toString();
117
+ * // → "foobar"
118
+ */
119
+ function decode(str, opts) {
120
+ opts = opts || {};
121
+ if (typeof str !== "string") throw new Base32Error("base32/bad-input", "base32.decode: input must be a string");
122
+ _alphabet(opts.variant);
123
+ var lookup = LOOKUPS[opts.variant || "rfc4648"];
124
+ var loose = opts.loose === true;
125
+
126
+ var bytes = [];
127
+ var value = 0, bits = 0;
128
+ var inPad = false; // once "=" padding starts, only more "=" may follow
129
+ for (var i = 0; i < str.length; i++) {
130
+ var ch = str.charAt(i);
131
+ if (ch === "=") { inPad = true; continue; } // trailing padding
132
+ if (loose && (ch === " " || ch === "-")) continue; // ignore separators
133
+ // A data character after padding is malformed in either mode — the "="
134
+ // run must be trailing (rejects "M=Y======" / "MZXW=6YTB").
135
+ if (inPad) throw new Base32Error("base32/bad-char", "base32.decode: data character '" + ch + "' after padding at index " + i);
136
+ if (loose) ch = ch.toUpperCase();
137
+ var idx = lookup[ch];
138
+ if (idx === undefined) throw new Base32Error("base32/bad-char", "base32.decode: invalid Base32 character '" + str.charAt(i) + "' at index " + i);
139
+ value = (value << BITS) | idx;
140
+ bits += BITS;
141
+ if (bits >= 8) { // allow:raw-byte-literal — emit a full output byte
142
+ bytes.push((value >>> (bits - 8)) & 0xff); // allow:raw-byte-literal — eight-bit output byte mask
143
+ bits -= 8; // allow:raw-byte-literal — consumed eight bits
144
+ }
145
+ }
146
+ return Buffer.from(bytes);
147
+ }
148
+
149
+ module.exports = {
150
+ encode: encode,
151
+ decode: decode,
152
+ ALPHABETS: ALPHABETS,
153
+ Base32Error: Base32Error,
154
+ };