@smartcrab/identifiers 0.3.2 → 0.4.0-next-20261017141739

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.
Files changed (3) hide show
  1. package/dist/index.js +11 -35
  2. package/package.json +1 -1
  3. package/src/index.ts +11 -34
package/dist/index.js CHANGED
@@ -44,45 +44,21 @@ export const base32UrlDecode = (encoded) => {
44
44
  }
45
45
  return new Uint8Array(out);
46
46
  };
47
- const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
48
47
  /** base64url, no padding. Suitable for secrets and tokens. */
49
- export const base64UrlEncode = (bytes) => {
50
- let out = "";
51
- for (let i = 0; i < bytes.length; i += 3) {
52
- const b0 = bytes[i] ?? 0;
53
- const b1 = bytes[i + 1];
54
- const b2 = bytes[i + 2];
55
- out += BASE64_ALPHABET[b0 >> 2] ?? "";
56
- out += BASE64_ALPHABET[((b0 & 0x03) << 4) | ((b1 ?? 0) >> 4)] ?? "";
57
- if (b1 !== undefined)
58
- out += BASE64_ALPHABET[((b1 & 0x0f) << 2) | ((b2 ?? 0) >> 6)] ?? "";
59
- if (b2 !== undefined)
60
- out += BASE64_ALPHABET[b2 & 0x3f] ?? "";
61
- }
62
- return out;
63
- };
48
+ export const base64UrlEncode = (bytes) => btoa(Array.from(bytes, (byte) => String.fromCharCode(byte)).join(""))
49
+ .replaceAll("+", "-")
50
+ .replaceAll("/", "_")
51
+ .replace(/=+$/, "");
64
52
  export const base64UrlDecode = (encoded) => {
65
- const out = [];
66
- let buffer = 0;
67
- let bits = 0;
68
- for (const char of encoded) {
69
- const index = BASE64_ALPHABET.indexOf(char);
70
- if (index < 0) {
71
- throw new Error(`Invalid base64url character: ${char}`);
72
- }
73
- buffer = (buffer << 6) | index;
74
- bits += 6;
75
- if (bits >= 8) {
76
- bits -= 8;
77
- out.push((buffer >>> bits) & 0xff);
78
- }
79
- }
80
- return new Uint8Array(out);
53
+ const invalid = /[^A-Za-z0-9_-]/u.exec(encoded);
54
+ if (invalid !== null)
55
+ throw new Error(`Invalid base64url character: ${invalid[0]}`);
56
+ const complete = encoded.length % 4 === 1 ? encoded.slice(0, -1) : encoded;
57
+ const standard = complete.replaceAll("-", "+").replaceAll("_", "/");
58
+ return Uint8Array.from(atob(standard), (char) => char.charCodeAt(0));
81
59
  };
82
60
  export const randomBytes = (length) => {
83
- const bytes = new Uint8Array(length);
84
- crypto.getRandomValues(bytes);
85
- return bytes;
61
+ return crypto.getRandomValues(new Uint8Array(length));
86
62
  };
87
63
  /** Resource ID prefixes from design.md §7.5. */
88
64
  export const ID_PREFIXES = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smartcrab/identifiers",
3
- "version": "0.3.2",
3
+ "version": "0.4.0-next-20261017141739",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
package/src/index.ts CHANGED
@@ -48,46 +48,23 @@ export const base32UrlDecode = (encoded: string): Uint8Array => {
48
48
  return new Uint8Array(out);
49
49
  };
50
50
 
51
- const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
52
-
53
51
  /** base64url, no padding. Suitable for secrets and tokens. */
54
- export const base64UrlEncode = (bytes: Uint8Array): string => {
55
- let out = "";
56
- for (let i = 0; i < bytes.length; i += 3) {
57
- const b0 = bytes[i] ?? 0;
58
- const b1 = bytes[i + 1];
59
- const b2 = bytes[i + 2];
60
- out += BASE64_ALPHABET[b0 >> 2] ?? "";
61
- out += BASE64_ALPHABET[((b0 & 0x03) << 4) | ((b1 ?? 0) >> 4)] ?? "";
62
- if (b1 !== undefined) out += BASE64_ALPHABET[((b1 & 0x0f) << 2) | ((b2 ?? 0) >> 6)] ?? "";
63
- if (b2 !== undefined) out += BASE64_ALPHABET[b2 & 0x3f] ?? "";
64
- }
65
- return out;
66
- };
52
+ export const base64UrlEncode = (bytes: Uint8Array): string =>
53
+ btoa(Array.from(bytes, (byte) => String.fromCharCode(byte)).join(""))
54
+ .replaceAll("+", "-")
55
+ .replaceAll("/", "_")
56
+ .replace(/=+$/, "");
67
57
 
68
58
  export const base64UrlDecode = (encoded: string): Uint8Array => {
69
- const out: number[] = [];
70
- let buffer = 0;
71
- let bits = 0;
72
- for (const char of encoded) {
73
- const index = BASE64_ALPHABET.indexOf(char);
74
- if (index < 0) {
75
- throw new Error(`Invalid base64url character: ${char}`);
76
- }
77
- buffer = (buffer << 6) | index;
78
- bits += 6;
79
- if (bits >= 8) {
80
- bits -= 8;
81
- out.push((buffer >>> bits) & 0xff);
82
- }
83
- }
84
- return new Uint8Array(out);
59
+ const invalid = /[^A-Za-z0-9_-]/u.exec(encoded);
60
+ if (invalid !== null) throw new Error(`Invalid base64url character: ${invalid[0]}`);
61
+ const complete = encoded.length % 4 === 1 ? encoded.slice(0, -1) : encoded;
62
+ const standard = complete.replaceAll("-", "+").replaceAll("_", "/");
63
+ return Uint8Array.from(atob(standard), (char) => char.charCodeAt(0));
85
64
  };
86
65
 
87
66
  export const randomBytes = (length: number): Uint8Array => {
88
- const bytes = new Uint8Array(length);
89
- crypto.getRandomValues(bytes);
90
- return bytes;
67
+ return crypto.getRandomValues(new Uint8Array(length));
91
68
  };
92
69
 
93
70
  /** Resource ID prefixes from design.md §7.5. */