@twin.org/core 0.10.1-next.3 → 0.10.1-next.4

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.
@@ -4,6 +4,7 @@
4
4
  /* eslint-disable no-mixed-operators */
5
5
  import { GeneralError } from "../errors/generalError.js";
6
6
  import { Guards } from "../utils/guards.js";
7
+ import { NativeModules } from "../utils/nativeModules.js";
7
8
  /**
8
9
  * Class to help with base64 Encoding/Decoding.
9
10
  * Sourced from https://github.com/beatgammit/base64-js.
@@ -13,6 +14,11 @@ export class Base64 {
13
14
  * Runtime name for the class.
14
15
  */
15
16
  static CLASS_NAME = "Base64";
17
+ /**
18
+ * True when a global Buffer is available to back encode/decode natively.
19
+ * @internal
20
+ */
21
+ static _hasBuffer = NativeModules.typeExists("Buffer");
16
22
  /**
17
23
  * Alphabet table for encoding.
18
24
  * @internal
@@ -106,6 +112,10 @@ export class Base64 {
106
112
  */
107
113
  static decode(base64) {
108
114
  Guards.string(Base64.CLASS_NAME, "base64", base64);
115
+ Base64.validateLength(base64);
116
+ if (Base64._hasBuffer) {
117
+ return new Uint8Array(globalThis.Buffer.from(base64, "base64"));
118
+ }
109
119
  let tmp;
110
120
  const lens = Base64.getLengths(base64);
111
121
  const validLen = lens[0];
@@ -148,6 +158,9 @@ export class Base64 {
148
158
  */
149
159
  static encode(bytes) {
150
160
  Guards.uint8Array(Base64.CLASS_NAME, "bytes", bytes);
161
+ if (Base64._hasBuffer) {
162
+ return globalThis.Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString("base64");
163
+ }
151
164
  let tmp;
152
165
  const len = bytes.length;
153
166
  const extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes
@@ -178,6 +191,17 @@ export class Base64 {
178
191
  static calcByteLength(validLen, placeHoldersLen) {
179
192
  return ((validLen + placeHoldersLen) * 3) / 4 - placeHoldersLen;
180
193
  }
194
+ /**
195
+ * Validate that a base64 string has a length that is a multiple of 4.
196
+ * @param base64 The base64 string.
197
+ * @throws GeneralError if the string length is not a multiple of 4.
198
+ * @internal
199
+ */
200
+ static validateLength(base64) {
201
+ if (base64.length % 4 > 0) {
202
+ throw new GeneralError(Base64.CLASS_NAME, "length4Multiple", { value: base64.length });
203
+ }
204
+ }
181
205
  /**
182
206
  * Get the valid and placeholder lengths from a base64 string.
183
207
  * @param base64 The base64 string.
@@ -186,10 +210,8 @@ export class Base64 {
186
210
  * @internal
187
211
  */
188
212
  static getLengths(base64) {
213
+ Base64.validateLength(base64);
189
214
  const len = base64.length;
190
- if (len % 4 > 0) {
191
- throw new GeneralError(Base64.CLASS_NAME, "length4Multiple", { value: len });
192
- }
193
215
  // Trim off extra bytes after placeholder bytes are found
194
216
  // See: https://github.com/beatgammit/base64-js/issues/42
195
217
  let validLen = base64.indexOf("=");
@@ -1 +1 @@
1
- {"version":3,"file":"base64.js","sourceRoot":"","sources":["../../../src/encoding/base64.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,+BAA+B;AAC/B,uCAAuC;AAGvC,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;;GAGG;AACH,MAAM,OAAO,MAAM;IAClB;;OAEG;IACI,MAAM,CAAU,UAAU,YAA4B;IAE7D;;;OAGG;IACK,MAAM,CAAU,OAAO,GAC9B,kEAAkE,CAAC;IAEpE;;;OAGG;IACK,MAAM,CAAU,eAAe,GAA6B;QACnE,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,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O,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;YAClC,MAAM,CAAC,OAAO,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;YAClC,MAAM,CAAC,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC;YACjC,MAAM,CAAC,OAAO,CAAC,GAAG,GAAG,IAAI,CAAC,CAC1B,CAAC;IACH,CAAC;IAED;;;;;;;OAOG;IACK,MAAM,CAAC,WAAW,CAAC,KAAiB,EAAE,KAAa,EAAE,GAAW;QACvE,IAAI,GAAG,CAAC;QACR,MAAM,MAAM,GAAG,EAAE,CAAC;QAClB,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;YACrC,GAAG,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;YAC7F,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC;QAC1C,CAAC;QACD,OAAO,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACxB,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/* eslint-disable no-bitwise */\n/* eslint-disable no-mixed-operators */\n\nimport { nameof } from \"@twin.org/nameof\";\nimport { GeneralError } from \"../errors/generalError.js\";\nimport { Guards } from \"../utils/guards.js\";\n\n/**\n * Class to help with base64 Encoding/Decoding.\n * Sourced from https://github.com/beatgammit/base64-js.\n */\nexport class Base64 {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Base64>();\n\n\t/**\n\t * Alphabet table for encoding.\n\t * @internal\n\t */\n\tprivate static readonly _LOOKUP: string =\n\t\t\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\";\n\n\t/**\n\t * Alphabet table for decoding.\n\t * @internal\n\t */\n\tprivate static readonly _REVERSE_LOOKUP: { [id: number]: number } = {\n\t\t\"43\": 62,\n\t\t\"45\": 62,\n\t\t\"47\": 63,\n\t\t\"48\": 52,\n\t\t\"49\": 53,\n\t\t\"50\": 54,\n\t\t\"51\": 55,\n\t\t\"52\": 56,\n\t\t\"53\": 57,\n\t\t\"54\": 58,\n\t\t\"55\": 59,\n\t\t\"56\": 60,\n\t\t\"57\": 61,\n\t\t\"65\": 0,\n\t\t\"66\": 1,\n\t\t\"67\": 2,\n\t\t\"68\": 3,\n\t\t\"69\": 4,\n\t\t\"70\": 5,\n\t\t\"71\": 6,\n\t\t\"72\": 7,\n\t\t\"73\": 8,\n\t\t\"74\": 9,\n\t\t\"75\": 10,\n\t\t\"76\": 11,\n\t\t\"77\": 12,\n\t\t\"78\": 13,\n\t\t\"79\": 14,\n\t\t\"80\": 15,\n\t\t\"81\": 16,\n\t\t\"82\": 17,\n\t\t\"83\": 18,\n\t\t\"84\": 19,\n\t\t\"85\": 20,\n\t\t\"86\": 21,\n\t\t\"87\": 22,\n\t\t\"88\": 23,\n\t\t\"89\": 24,\n\t\t\"90\": 25,\n\t\t\"95\": 63,\n\t\t\"97\": 26,\n\t\t\"98\": 27,\n\t\t\"99\": 28,\n\t\t\"100\": 29,\n\t\t\"101\": 30,\n\t\t\"102\": 31,\n\t\t\"103\": 32,\n\t\t\"104\": 33,\n\t\t\"105\": 34,\n\t\t\"106\": 35,\n\t\t\"107\": 36,\n\t\t\"108\": 37,\n\t\t\"109\": 38,\n\t\t\"110\": 39,\n\t\t\"111\": 40,\n\t\t\"112\": 41,\n\t\t\"113\": 42,\n\t\t\"114\": 43,\n\t\t\"115\": 44,\n\t\t\"116\": 45,\n\t\t\"117\": 46,\n\t\t\"118\": 47,\n\t\t\"119\": 48,\n\t\t\"120\": 49,\n\t\t\"121\": 50,\n\t\t\"122\": 51\n\t};\n\n\t/**\n\t * Get the byte length of the data.\n\t * @param base64 The base64 string.\n\t * @returns The byte length of the data.\n\t */\n\tpublic static byteLength(base64: string): number {\n\t\tconst lens = Base64.getLengths(base64);\n\t\treturn Base64.calcByteLength(lens[0], lens[1]);\n\t}\n\n\t/**\n\t * Convert the base 64 string to a byte array.\n\t * @param base64 The base64 string to convert.\n\t * @returns The byte array.\n\t */\n\tpublic static decode(base64: string): Uint8Array {\n\t\tGuards.string(Base64.CLASS_NAME, nameof(base64), base64);\n\n\t\tlet tmp;\n\t\tconst lens = Base64.getLengths(base64);\n\t\tconst validLen = lens[0];\n\t\tconst placeHoldersLen = lens[1];\n\n\t\tconst arr = new Uint8Array(Base64.calcByteLength(validLen, placeHoldersLen));\n\n\t\tlet curByte = 0;\n\n\t\t// if there are placeholders, only get up to the last complete 4 chars\n\t\tconst len = placeHoldersLen > 0 ? validLen - 4 : validLen;\n\n\t\tlet i;\n\t\tfor (i = 0; i < len; i += 4) {\n\t\t\ttmp =\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 18) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] << 12) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 2)] << 6) |\n\t\t\t\tBase64._REVERSE_LOOKUP[base64.charCodeAt(i + 3)];\n\t\t\tarr[curByte++] = (tmp >> 16) & 0xff;\n\t\t\tarr[curByte++] = (tmp >> 8) & 0xff;\n\t\t\tarr[curByte++] = tmp & 0xff;\n\t\t}\n\n\t\tif (placeHoldersLen === 2) {\n\t\t\ttmp =\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 2) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] >> 4);\n\t\t\tarr[curByte++] = tmp & 0xff;\n\t\t}\n\n\t\tif (placeHoldersLen === 1) {\n\t\t\ttmp =\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 10) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] << 4) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 2)] >> 2);\n\t\t\tarr[curByte++] = (tmp >> 8) & 0xff;\n\t\t\tarr[curByte] = tmp & 0xff;\n\t\t}\n\n\t\treturn arr;\n\t}\n\n\t/**\n\t * Convert a byte array to base 64.\n\t * @param bytes The byte array to convert.\n\t * @returns The data as base64 string.\n\t */\n\tpublic static encode(bytes: Uint8Array): string {\n\t\tGuards.uint8Array(Base64.CLASS_NAME, nameof(bytes), bytes);\n\n\t\tlet tmp;\n\t\tconst len = bytes.length;\n\t\tconst extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes\n\t\tconst parts = [];\n\t\tconst maxChunkLength = 16383; // must be multiple of 3\n\n\t\t// go through the array every three bytes, we'll deal with trailing stuff later\n\t\tfor (let i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {\n\t\t\tparts.push(Base64.encodeChunk(bytes, i, Math.min(i + maxChunkLength, len2)));\n\t\t}\n\n\t\t// pad the end with zeros, but make sure to not forget the extra bytes\n\t\tif (extraBytes === 1) {\n\t\t\ttmp = bytes[len - 1];\n\t\t\tparts.push(`${Base64._LOOKUP[tmp >> 2] + Base64._LOOKUP[(tmp << 4) & 0x3f]}==`);\n\t\t} else if (extraBytes === 2) {\n\t\t\ttmp = (bytes[len - 2] << 8) + bytes[len - 1];\n\t\t\tparts.push(\n\t\t\t\t`${Base64._LOOKUP[tmp >> 10] + Base64._LOOKUP[(tmp >> 4) & 0x3f] + Base64._LOOKUP[(tmp << 2) & 0x3f]}=`\n\t\t\t);\n\t\t}\n\n\t\treturn parts.join(\"\");\n\t}\n\n\t/**\n\t * Calculate the byte length.\n\t * @param validLen The valid length.\n\t * @param placeHoldersLen The placeholder length.\n\t * @returns The length.\n\t * @internal\n\t */\n\tprivate static calcByteLength(validLen: number, placeHoldersLen: number): number {\n\t\treturn ((validLen + placeHoldersLen) * 3) / 4 - placeHoldersLen;\n\t}\n\n\t/**\n\t * Get the valid and placeholder lengths from a base64 string.\n\t * @param base64 The base64 string.\n\t * @returns The lengths.\n\t * @throws GeneralError if the string length is not a multiple of 4.\n\t * @internal\n\t */\n\tprivate static getLengths(base64: string): number[] {\n\t\tconst len = base64.length;\n\n\t\tif (len % 4 > 0) {\n\t\t\tthrow new GeneralError(Base64.CLASS_NAME, \"length4Multiple\", { value: len });\n\t\t}\n\n\t\t// Trim off extra bytes after placeholder bytes are found\n\t\t// See: https://github.com/beatgammit/base64-js/issues/42\n\t\tlet validLen = base64.indexOf(\"=\");\n\t\tif (validLen === -1) {\n\t\t\tvalidLen = len;\n\t\t}\n\n\t\tconst placeHoldersLen = validLen === len ? 0 : 4 - (validLen % 4);\n\n\t\treturn [validLen, placeHoldersLen];\n\t}\n\n\t/**\n\t * Convert the triplet to base 64.\n\t * @param num The number to convert.\n\t * @returns The base64 encoding.\n\t * @internal\n\t */\n\tprivate static tripletToBase64(num: number): string {\n\t\treturn (\n\t\t\tBase64._LOOKUP[(num >> 18) & 0x3f] +\n\t\t\tBase64._LOOKUP[(num >> 12) & 0x3f] +\n\t\t\tBase64._LOOKUP[(num >> 6) & 0x3f] +\n\t\t\tBase64._LOOKUP[num & 0x3f]\n\t\t);\n\t}\n\n\t/**\n\t * Encode a chunk.\n\t * @param bytes The byte array.\n\t * @param start The start index in the buffer.\n\t * @param end The end index in the buffer.\n\t * @returns The encoded chunk.\n\t * @internal\n\t */\n\tprivate static encodeChunk(bytes: Uint8Array, start: number, end: number): string {\n\t\tlet tmp;\n\t\tconst output = [];\n\t\tfor (let i = start; i < end; i += 3) {\n\t\t\ttmp = ((bytes[i] << 16) & 0xff0000) + ((bytes[i + 1] << 8) & 0xff00) + (bytes[i + 2] & 0xff);\n\t\t\toutput.push(Base64.tripletToBase64(tmp));\n\t\t}\n\t\treturn output.join(\"\");\n\t}\n}\n"]}
1
+ {"version":3,"file":"base64.js","sourceRoot":"","sources":["../../../src/encoding/base64.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,+BAA+B;AAC/B,uCAAuC;AAGvC,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAE1D;;;GAGG;AACH,MAAM,OAAO,MAAM;IAClB;;OAEG;IACI,MAAM,CAAU,UAAU,YAA4B;IAE7D;;;OAGG;IACK,MAAM,CAAU,UAAU,GAAY,aAAa,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;IAEjF;;;OAGG;IACK,MAAM,CAAU,OAAO,GAC9B,kEAAkE,CAAC;IAEpE;;;OAGG;IACK,MAAM,CAAU,eAAe,GAA6B;QACnE,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,CAAC;QACP,IAAI,EAAE,CAAC;QACP,IAAI,EAAE,CAAC;QACP,IAAI,EAAE,CAAC;QACP,IAAI,EAAE,CAAC;QACP,IAAI,EAAE,CAAC;QACP,IAAI,EAAE,CAAC;QACP,IAAI,EAAE,CAAC;QACP,IAAI,EAAE,CAAC;QACP,IAAI,EAAE,CAAC;QACP,IAAI,E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xB,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/* eslint-disable no-bitwise */\n/* eslint-disable no-mixed-operators */\n\nimport { nameof } from \"@twin.org/nameof\";\nimport { GeneralError } from \"../errors/generalError.js\";\nimport { Guards } from \"../utils/guards.js\";\nimport { NativeModules } from \"../utils/nativeModules.js\";\n\n/**\n * Class to help with base64 Encoding/Decoding.\n * Sourced from https://github.com/beatgammit/base64-js.\n */\nexport class Base64 {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Base64>();\n\n\t/**\n\t * True when a global Buffer is available to back encode/decode natively.\n\t * @internal\n\t */\n\tprivate static readonly _hasBuffer: boolean = NativeModules.typeExists(\"Buffer\");\n\n\t/**\n\t * Alphabet table for encoding.\n\t * @internal\n\t */\n\tprivate static readonly _LOOKUP: string =\n\t\t\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\";\n\n\t/**\n\t * Alphabet table for decoding.\n\t * @internal\n\t */\n\tprivate static readonly _REVERSE_LOOKUP: { [id: number]: number } = {\n\t\t\"43\": 62,\n\t\t\"45\": 62,\n\t\t\"47\": 63,\n\t\t\"48\": 52,\n\t\t\"49\": 53,\n\t\t\"50\": 54,\n\t\t\"51\": 55,\n\t\t\"52\": 56,\n\t\t\"53\": 57,\n\t\t\"54\": 58,\n\t\t\"55\": 59,\n\t\t\"56\": 60,\n\t\t\"57\": 61,\n\t\t\"65\": 0,\n\t\t\"66\": 1,\n\t\t\"67\": 2,\n\t\t\"68\": 3,\n\t\t\"69\": 4,\n\t\t\"70\": 5,\n\t\t\"71\": 6,\n\t\t\"72\": 7,\n\t\t\"73\": 8,\n\t\t\"74\": 9,\n\t\t\"75\": 10,\n\t\t\"76\": 11,\n\t\t\"77\": 12,\n\t\t\"78\": 13,\n\t\t\"79\": 14,\n\t\t\"80\": 15,\n\t\t\"81\": 16,\n\t\t\"82\": 17,\n\t\t\"83\": 18,\n\t\t\"84\": 19,\n\t\t\"85\": 20,\n\t\t\"86\": 21,\n\t\t\"87\": 22,\n\t\t\"88\": 23,\n\t\t\"89\": 24,\n\t\t\"90\": 25,\n\t\t\"95\": 63,\n\t\t\"97\": 26,\n\t\t\"98\": 27,\n\t\t\"99\": 28,\n\t\t\"100\": 29,\n\t\t\"101\": 30,\n\t\t\"102\": 31,\n\t\t\"103\": 32,\n\t\t\"104\": 33,\n\t\t\"105\": 34,\n\t\t\"106\": 35,\n\t\t\"107\": 36,\n\t\t\"108\": 37,\n\t\t\"109\": 38,\n\t\t\"110\": 39,\n\t\t\"111\": 40,\n\t\t\"112\": 41,\n\t\t\"113\": 42,\n\t\t\"114\": 43,\n\t\t\"115\": 44,\n\t\t\"116\": 45,\n\t\t\"117\": 46,\n\t\t\"118\": 47,\n\t\t\"119\": 48,\n\t\t\"120\": 49,\n\t\t\"121\": 50,\n\t\t\"122\": 51\n\t};\n\n\t/**\n\t * Get the byte length of the data.\n\t * @param base64 The base64 string.\n\t * @returns The byte length of the data.\n\t */\n\tpublic static byteLength(base64: string): number {\n\t\tconst lens = Base64.getLengths(base64);\n\t\treturn Base64.calcByteLength(lens[0], lens[1]);\n\t}\n\n\t/**\n\t * Convert the base 64 string to a byte array.\n\t * @param base64 The base64 string to convert.\n\t * @returns The byte array.\n\t */\n\tpublic static decode(base64: string): Uint8Array {\n\t\tGuards.string(Base64.CLASS_NAME, nameof(base64), base64);\n\n\t\tBase64.validateLength(base64);\n\n\t\tif (Base64._hasBuffer) {\n\t\t\treturn new Uint8Array(globalThis.Buffer.from(base64, \"base64\"));\n\t\t}\n\n\t\tlet tmp;\n\t\tconst lens = Base64.getLengths(base64);\n\t\tconst validLen = lens[0];\n\t\tconst placeHoldersLen = lens[1];\n\n\t\tconst arr = new Uint8Array(Base64.calcByteLength(validLen, placeHoldersLen));\n\n\t\tlet curByte = 0;\n\n\t\t// if there are placeholders, only get up to the last complete 4 chars\n\t\tconst len = placeHoldersLen > 0 ? validLen - 4 : validLen;\n\n\t\tlet i;\n\t\tfor (i = 0; i < len; i += 4) {\n\t\t\ttmp =\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 18) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] << 12) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 2)] << 6) |\n\t\t\t\tBase64._REVERSE_LOOKUP[base64.charCodeAt(i + 3)];\n\t\t\tarr[curByte++] = (tmp >> 16) & 0xff;\n\t\t\tarr[curByte++] = (tmp >> 8) & 0xff;\n\t\t\tarr[curByte++] = tmp & 0xff;\n\t\t}\n\n\t\tif (placeHoldersLen === 2) {\n\t\t\ttmp =\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 2) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] >> 4);\n\t\t\tarr[curByte++] = tmp & 0xff;\n\t\t}\n\n\t\tif (placeHoldersLen === 1) {\n\t\t\ttmp =\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i)] << 10) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 1)] << 4) |\n\t\t\t\t(Base64._REVERSE_LOOKUP[base64.charCodeAt(i + 2)] >> 2);\n\t\t\tarr[curByte++] = (tmp >> 8) & 0xff;\n\t\t\tarr[curByte] = tmp & 0xff;\n\t\t}\n\n\t\treturn arr;\n\t}\n\n\t/**\n\t * Convert a byte array to base 64.\n\t * @param bytes The byte array to convert.\n\t * @returns The data as base64 string.\n\t */\n\tpublic static encode(bytes: Uint8Array): string {\n\t\tGuards.uint8Array(Base64.CLASS_NAME, nameof(bytes), bytes);\n\n\t\tif (Base64._hasBuffer) {\n\t\t\treturn globalThis.Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString(\n\t\t\t\t\"base64\"\n\t\t\t);\n\t\t}\n\n\t\tlet tmp;\n\t\tconst len = bytes.length;\n\t\tconst extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes\n\t\tconst parts = [];\n\t\tconst maxChunkLength = 16383; // must be multiple of 3\n\n\t\t// go through the array every three bytes, we'll deal with trailing stuff later\n\t\tfor (let i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {\n\t\t\tparts.push(Base64.encodeChunk(bytes, i, Math.min(i + maxChunkLength, len2)));\n\t\t}\n\n\t\t// pad the end with zeros, but make sure to not forget the extra bytes\n\t\tif (extraBytes === 1) {\n\t\t\ttmp = bytes[len - 1];\n\t\t\tparts.push(`${Base64._LOOKUP[tmp >> 2] + Base64._LOOKUP[(tmp << 4) & 0x3f]}==`);\n\t\t} else if (extraBytes === 2) {\n\t\t\ttmp = (bytes[len - 2] << 8) + bytes[len - 1];\n\t\t\tparts.push(\n\t\t\t\t`${Base64._LOOKUP[tmp >> 10] + Base64._LOOKUP[(tmp >> 4) & 0x3f] + Base64._LOOKUP[(tmp << 2) & 0x3f]}=`\n\t\t\t);\n\t\t}\n\n\t\treturn parts.join(\"\");\n\t}\n\n\t/**\n\t * Calculate the byte length.\n\t * @param validLen The valid length.\n\t * @param placeHoldersLen The placeholder length.\n\t * @returns The length.\n\t * @internal\n\t */\n\tprivate static calcByteLength(validLen: number, placeHoldersLen: number): number {\n\t\treturn ((validLen + placeHoldersLen) * 3) / 4 - placeHoldersLen;\n\t}\n\n\t/**\n\t * Validate that a base64 string has a length that is a multiple of 4.\n\t * @param base64 The base64 string.\n\t * @throws GeneralError if the string length is not a multiple of 4.\n\t * @internal\n\t */\n\tprivate static validateLength(base64: string): void {\n\t\tif (base64.length % 4 > 0) {\n\t\t\tthrow new GeneralError(Base64.CLASS_NAME, \"length4Multiple\", { value: base64.length });\n\t\t}\n\t}\n\n\t/**\n\t * Get the valid and placeholder lengths from a base64 string.\n\t * @param base64 The base64 string.\n\t * @returns The lengths.\n\t * @throws GeneralError if the string length is not a multiple of 4.\n\t * @internal\n\t */\n\tprivate static getLengths(base64: string): number[] {\n\t\tBase64.validateLength(base64);\n\n\t\tconst len = base64.length;\n\n\t\t// Trim off extra bytes after placeholder bytes are found\n\t\t// See: https://github.com/beatgammit/base64-js/issues/42\n\t\tlet validLen = base64.indexOf(\"=\");\n\t\tif (validLen === -1) {\n\t\t\tvalidLen = len;\n\t\t}\n\n\t\tconst placeHoldersLen = validLen === len ? 0 : 4 - (validLen % 4);\n\n\t\treturn [validLen, placeHoldersLen];\n\t}\n\n\t/**\n\t * Convert the triplet to base 64.\n\t * @param num The number to convert.\n\t * @returns The base64 encoding.\n\t * @internal\n\t */\n\tprivate static tripletToBase64(num: number): string {\n\t\treturn (\n\t\t\tBase64._LOOKUP[(num >> 18) & 0x3f] +\n\t\t\tBase64._LOOKUP[(num >> 12) & 0x3f] +\n\t\t\tBase64._LOOKUP[(num >> 6) & 0x3f] +\n\t\t\tBase64._LOOKUP[num & 0x3f]\n\t\t);\n\t}\n\n\t/**\n\t * Encode a chunk.\n\t * @param bytes The byte array.\n\t * @param start The start index in the buffer.\n\t * @param end The end index in the buffer.\n\t * @returns The encoded chunk.\n\t * @internal\n\t */\n\tprivate static encodeChunk(bytes: Uint8Array, start: number, end: number): string {\n\t\tlet tmp;\n\t\tconst output = [];\n\t\tfor (let i = start; i < end; i += 3) {\n\t\t\ttmp = ((bytes[i] << 16) & 0xff0000) + ((bytes[i + 1] << 8) & 0xff00) + (bytes[i + 2] & 0xff);\n\t\t\toutput.push(Base64.tripletToBase64(tmp));\n\t\t}\n\t\treturn output.join(\"\");\n\t}\n}\n"]}
@@ -1,5 +1,6 @@
1
1
  import { Base64 } from "./base64.js";
2
2
  import { Guards } from "../utils/guards.js";
3
+ import { NativeModules } from "../utils/nativeModules.js";
3
4
  /**
4
5
  * Class to help with base64 URL Encoding/Decoding.
5
6
  * https://www.rfc-editor.org/rfc/rfc4648#section-5.
@@ -8,7 +9,13 @@ export class Base64Url {
8
9
  /**
9
10
  * Runtime name for the class.
10
11
  */
11
- static CLASS_NAME = "Base64";
12
+ static CLASS_NAME = "Base64Url";
13
+ /**
14
+ * True when a global Buffer with the "base64url" encoding is available, which the
15
+ * browser buffer polyfill lacks.
16
+ * @internal
17
+ */
18
+ static _hasBuffer = NativeModules.typeExists("Buffer") && globalThis.Buffer.isEncoding("base64url");
12
19
  /**
13
20
  * Convert the base 64 string to a byte array.
14
21
  * @param base64Url The base64 url string to convert.
@@ -16,6 +23,9 @@ export class Base64Url {
16
23
  */
17
24
  static decode(base64Url) {
18
25
  Guards.string(Base64Url.CLASS_NAME, "base64Url", base64Url);
26
+ if (Base64Url._hasBuffer) {
27
+ return new Uint8Array(globalThis.Buffer.from(base64Url, "base64url"));
28
+ }
19
29
  let base64 = base64Url;
20
30
  // Base 64 url can have padding removed, so add it back if it is missing.
21
31
  if (base64.length > 0 && !base64.endsWith("=")) {
@@ -34,6 +44,9 @@ export class Base64Url {
34
44
  */
35
45
  static encode(bytes) {
36
46
  Guards.uint8Array(Base64Url.CLASS_NAME, "bytes", bytes);
47
+ if (Base64Url._hasBuffer) {
48
+ return globalThis.Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString("base64url");
49
+ }
37
50
  const base64 = Base64.encode(bytes);
38
51
  // Base 64 url can have padding removed, so remove it.
39
52
  return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
@@ -1 +1 @@
1
- {"version":3,"file":"base64Url.js","sourceRoot":"","sources":["../../../src/encoding/base64Url.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;;GAGG;AACH,MAAM,OAAO,SAAS;IACrB;;OAEG;IACI,MAAM,CAAU,UAAU,YAA4B;IAE7D;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,SAAiB;QACrC,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,UAAU,eAAqB,SAAS,CAAC,CAAC;QAElE,IAAI,MAAM,GAAG,SAAS,CAAC;QAEvB,yEAAyE;QACzE,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;YAChD,MAAM,eAAe,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;YAChD,IAAI,eAAe,GAAG,CAAC,IAAI,eAAe,GAAG,CAAC,EAAE,CAAC;gBAChD,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,eAAe,EAAE,GAAG,CAAC,CAAC;YAC9D,CAAC;QACF,CAAC;QAED,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QACtD,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC9B,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAC,KAAiB;QACrC,MAAM,CAAC,UAAU,CAAC,SAAS,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAE9D,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACpC,sDAAsD;QACtD,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IACzE,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { Base64 } from \"./base64.js\";\nimport { Guards } from \"../utils/guards.js\";\n\n/**\n * Class to help with base64 URL Encoding/Decoding.\n * https://www.rfc-editor.org/rfc/rfc4648#section-5.\n */\nexport class Base64Url {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Base64>();\n\n\t/**\n\t * Convert the base 64 string to a byte array.\n\t * @param base64Url The base64 url string to convert.\n\t * @returns The byte array.\n\t */\n\tpublic static decode(base64Url: string): Uint8Array {\n\t\tGuards.string(Base64Url.CLASS_NAME, nameof(base64Url), base64Url);\n\n\t\tlet base64 = base64Url;\n\n\t\t// Base 64 url can have padding removed, so add it back if it is missing.\n\t\tif (base64.length > 0 && !base64.endsWith(\"=\")) {\n\t\t\tconst placeHoldersLen = 4 - (base64.length % 4);\n\t\t\tif (placeHoldersLen > 0 && placeHoldersLen < 4) {\n\t\t\t\tbase64 = base64.padEnd(base64.length + placeHoldersLen, \"=\");\n\t\t\t}\n\t\t}\n\n\t\tbase64 = base64.replace(/-/g, \"+\").replace(/_/g, \"/\");\n\t\treturn Base64.decode(base64);\n\t}\n\n\t/**\n\t * Convert a byte array to base 64 url.\n\t * @param bytes The byte array to convert.\n\t * @returns The data as base64 url string.\n\t */\n\tpublic static encode(bytes: Uint8Array): string {\n\t\tGuards.uint8Array(Base64Url.CLASS_NAME, nameof(bytes), bytes);\n\n\t\tconst base64 = Base64.encode(bytes);\n\t\t// Base 64 url can have padding removed, so remove it.\n\t\treturn base64.replace(/\\+/g, \"-\").replace(/\\//g, \"_\").replace(/=/g, \"\");\n\t}\n}\n"]}
1
+ {"version":3,"file":"base64Url.js","sourceRoot":"","sources":["../../../src/encoding/base64Url.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAE1D;;;GAGG;AACH,MAAM,OAAO,SAAS;IACrB;;OAEG;IACI,MAAM,CAAU,UAAU,eAA+B;IAEhE;;;;OAIG;IACK,MAAM,CAAU,UAAU,GACjC,aAAa,CAAC,UAAU,CAAC,QAAQ,CAAC,IAAI,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,CAAC,QAAQ,CACvF,WAAW,CACX,CAAC;QACH,CAAC;QAED,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACpC,sDAAsD;QACtD,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IACzE,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { nameof } from \"@twin.org/nameof\";\nimport { Base64 } from \"./base64.js\";\nimport { Guards } from \"../utils/guards.js\";\nimport { NativeModules } from \"../utils/nativeModules.js\";\n\n/**\n * Class to help with base64 URL Encoding/Decoding.\n * https://www.rfc-editor.org/rfc/rfc4648#section-5.\n */\nexport class Base64Url {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Base64Url>();\n\n\t/**\n\t * True when a global Buffer with the \"base64url\" encoding is available, which the\n\t * browser buffer polyfill lacks.\n\t * @internal\n\t */\n\tprivate static readonly _hasBuffer: boolean =\n\t\tNativeModules.typeExists(\"Buffer\") && globalThis.Buffer.isEncoding(\"base64url\");\n\n\t/**\n\t * Convert the base 64 string to a byte array.\n\t * @param base64Url The base64 url string to convert.\n\t * @returns The byte array.\n\t */\n\tpublic static decode(base64Url: string): Uint8Array {\n\t\tGuards.string(Base64Url.CLASS_NAME, nameof(base64Url), base64Url);\n\n\t\tif (Base64Url._hasBuffer) {\n\t\t\treturn new Uint8Array(globalThis.Buffer.from(base64Url, \"base64url\"));\n\t\t}\n\n\t\tlet base64 = base64Url;\n\n\t\t// Base 64 url can have padding removed, so add it back if it is missing.\n\t\tif (base64.length > 0 && !base64.endsWith(\"=\")) {\n\t\t\tconst placeHoldersLen = 4 - (base64.length % 4);\n\t\t\tif (placeHoldersLen > 0 && placeHoldersLen < 4) {\n\t\t\t\tbase64 = base64.padEnd(base64.length + placeHoldersLen, \"=\");\n\t\t\t}\n\t\t}\n\n\t\tbase64 = base64.replace(/-/g, \"+\").replace(/_/g, \"/\");\n\t\treturn Base64.decode(base64);\n\t}\n\n\t/**\n\t * Convert a byte array to base 64 url.\n\t * @param bytes The byte array to convert.\n\t * @returns The data as base64 url string.\n\t */\n\tpublic static encode(bytes: Uint8Array): string {\n\t\tGuards.uint8Array(Base64Url.CLASS_NAME, nameof(bytes), bytes);\n\n\t\tif (Base64Url._hasBuffer) {\n\t\t\treturn globalThis.Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString(\n\t\t\t\t\"base64url\"\n\t\t\t);\n\t\t}\n\n\t\tconst base64 = Base64.encode(bytes);\n\t\t// Base 64 url can have padding removed, so remove it.\n\t\treturn base64.replace(/\\+/g, \"-\").replace(/\\//g, \"_\").replace(/=/g, \"\");\n\t}\n}\n"]}
package/dist/es/index.js CHANGED
@@ -69,6 +69,7 @@ export * from "./utils/guards.js";
69
69
  export * from "./utils/i18n.js";
70
70
  export * from "./utils/is.js";
71
71
  export * from "./utils/mutex.js";
72
+ export * from "./utils/nativeModules.js";
72
73
  export * from "./utils/sharedObjectBuffer.js";
73
74
  export * from "./utils/sharedStore.js";
74
75
  export * from "./utils/validation.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,yBAAyB,CAAC;AACxC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,0BAA0B,CAAC;AACzC,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iCAAiC,CAAC;AAChD,cAAc,+BAA+B,CAAC;AAC9C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,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nexport * from \"./encoding/base32.js\";\nexport * from \"./encoding/base58.js\";\nexport * from \"./encoding/base64.js\";\nexport * from \"./encoding/base64Url.js\";\nexport * from \"./errors/alreadyExistsError.js\";\nexport * from \"./errors/baseError.js\";\nexport * from \"./errors/conflictError.js\";\nexport * from \"./errors/generalError.js\";\nexport * from \"./errors/guardError.js\";\nexport * from \"./errors/notFoundError.js\";\nexport * from \"./errors/notImplementedError.js\";\nexport * from \"./errors/notSupportedError.js\";\nexport * from \"./errors/unauthorizedError.js\";\nexport * from \"./errors/unprocessableError.js\";\nexport * from \"./errors/validationError.js\";\nexport * from \"./factories/componentFactory.js\";\nexport * from \"./factories/facadeFactory.js\";\nexport * from \"./factories/factory.js\";\nexport * from \"./helpers/arrayHelper.js\";\nexport * from \"./helpers/envHelper.js\";\nexport * from \"./helpers/errorHelper.js\";\nexport * from \"./helpers/filenameHelper.js\";\nexport * from \"./helpers/hexHelper.js\";\nexport * from \"./helpers/jsonHelper.js\";\nexport * from \"./helpers/numberHelper.js\";\nexport * from \"./helpers/objectHelper.js\";\nexport * from \"./helpers/randomHelper.js\";\nexport * from \"./helpers/stringHelper.js\";\nexport * from \"./helpers/timeoutHelper.js\";\nexport * from \"./helpers/uint8ArrayHelper.js\";\nexport * from \"./models/coerceType.js\";\nexport * from \"./models/IDuration.js\";\nexport * from \"./models/compressionType.js\";\nexport * from \"./models/IComponent.js\";\nexport * from \"./models/IFacade.js\";\nexport * from \"./models/IError.js\";\nexport * from \"./models/II18nShared.js\";\nexport * from \"./models/IKeyValue.js\";\nexport * from \"./models/ILabelledValue.js\";\nexport * from \"./models/ILocale.js\";\nexport * from \"./models/ILocaleDictionary.js\";\nexport * from \"./models/ILocalesIndex.js\";\nexport * from \"./models/IMutexWaiter.js\";\nexport * from \"./models/IMutexWorkerMessage.js\";\nexport * from \"./models/IPatchOperation.js\";\nexport * from \"./models/ISharedObjectBufferOptions.js\";\nexport * from \"./models/ISharedObjectBufferWorkerMessage.js\";\nexport * from \"./models/IUrlParts.js\";\nexport * from \"./models/IValidationFailure.js\";\nexport * from \"./models/mutexMessageTypes.js\";\nexport * from \"./models/sharedObjectBufferMessageTypes.js\";\nexport * from \"./types/bitString.js\";\nexport * from \"./types/duration.js\";\nexport * from \"./types/durationRegExp.js\";\nexport * from \"./types/objectOrArray.js\";\nexport * from \"./types/singleOccurrenceArray.js\";\nexport * from \"./types/singleOccurrenceArrayDepthHelper.js\";\nexport * from \"./types/url.js\";\nexport * from \"./types/urn.js\";\nexport * from \"./utils/asyncCache.js\";\nexport * from \"./utils/lfuCache.js\";\nexport * from \"./utils/lruCache.js\";\nexport * from \"./utils/coerce.js\";\nexport * from \"./utils/compression.js\";\nexport * from \"./utils/converter.js\";\nexport * from \"./utils/guards.js\";\nexport * from \"./utils/i18n.js\";\nexport * from \"./utils/is.js\";\nexport * from \"./utils/mutex.js\";\nexport * from \"./utils/sharedObjectBuffer.js\";\nexport * from \"./utils/sharedStore.js\";\nexport * from \"./utils/validation.js\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;A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gC,CAAC;AAC/C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,4CAA4C,CAAC;AAC3D,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,0BAA0B,CAAC;AACzC,cAAc,kCAAkC,CAAC;AACjD,cAAc,6CAA6C,CAAC;AAC5D,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,0BAA0B,CAAC;AACzC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nexport * from \"./encoding/base32.js\";\nexport * from \"./encoding/base58.js\";\nexport * from \"./encoding/base64.js\";\nexport * from \"./encoding/base64Url.js\";\nexport * from \"./errors/alreadyExistsError.js\";\nexport * from \"./errors/baseError.js\";\nexport * from \"./errors/conflictError.js\";\nexport * from \"./errors/generalError.js\";\nexport * from \"./errors/guardError.js\";\nexport * from \"./errors/notFoundError.js\";\nexport * from \"./errors/notImplementedError.js\";\nexport * from \"./errors/notSupportedError.js\";\nexport * from \"./errors/unauthorizedError.js\";\nexport * from \"./errors/unprocessableError.js\";\nexport * from \"./errors/validationError.js\";\nexport * from \"./factories/componentFactory.js\";\nexport * from \"./factories/facadeFactory.js\";\nexport * from \"./factories/factory.js\";\nexport * from \"./helpers/arrayHelper.js\";\nexport * from \"./helpers/envHelper.js\";\nexport * from \"./helpers/errorHelper.js\";\nexport * from \"./helpers/filenameHelper.js\";\nexport * from \"./helpers/hexHelper.js\";\nexport * from \"./helpers/jsonHelper.js\";\nexport * from \"./helpers/numberHelper.js\";\nexport * from \"./helpers/objectHelper.js\";\nexport * from \"./helpers/randomHelper.js\";\nexport * from \"./helpers/stringHelper.js\";\nexport * from \"./helpers/timeoutHelper.js\";\nexport * from \"./helpers/uint8ArrayHelper.js\";\nexport * from \"./models/coerceType.js\";\nexport * from \"./models/IDuration.js\";\nexport * from \"./models/compressionType.js\";\nexport * from \"./models/IComponent.js\";\nexport * from \"./models/IFacade.js\";\nexport * from \"./models/IError.js\";\nexport * from \"./models/II18nShared.js\";\nexport * from \"./models/IKeyValue.js\";\nexport * from \"./models/ILabelledValue.js\";\nexport * from \"./models/ILocale.js\";\nexport * from \"./models/ILocaleDictionary.js\";\nexport * from \"./models/ILocalesIndex.js\";\nexport * from \"./models/IMutexWaiter.js\";\nexport * from \"./models/IMutexWorkerMessage.js\";\nexport * from \"./models/IPatchOperation.js\";\nexport * from \"./models/ISharedObjectBufferOptions.js\";\nexport * from \"./models/ISharedObjectBufferWorkerMessage.js\";\nexport * from \"./models/IUrlParts.js\";\nexport * from \"./models/IValidationFailure.js\";\nexport * from \"./models/mutexMessageTypes.js\";\nexport * from \"./models/sharedObjectBufferMessageTypes.js\";\nexport * from \"./types/bitString.js\";\nexport * from \"./types/duration.js\";\nexport * from \"./types/durationRegExp.js\";\nexport * from \"./types/objectOrArray.js\";\nexport * from \"./types/singleOccurrenceArray.js\";\nexport * from \"./types/singleOccurrenceArrayDepthHelper.js\";\nexport * from \"./types/url.js\";\nexport * from \"./types/urn.js\";\nexport * from \"./utils/asyncCache.js\";\nexport * from \"./utils/lfuCache.js\";\nexport * from \"./utils/lruCache.js\";\nexport * from \"./utils/coerce.js\";\nexport * from \"./utils/compression.js\";\nexport * from \"./utils/converter.js\";\nexport * from \"./utils/guards.js\";\nexport * from \"./utils/i18n.js\";\nexport * from \"./utils/is.js\";\nexport * from \"./utils/mutex.js\";\nexport * from \"./utils/nativeModules.js\";\nexport * from \"./utils/sharedObjectBuffer.js\";\nexport * from \"./utils/sharedStore.js\";\nexport * from \"./utils/validation.js\";\n"]}
@@ -0,0 +1,66 @@
1
+ // Copyright 2026 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ import { Is } from "./is.js";
4
+ import { SharedStore } from "./sharedStore.js";
5
+ import { BaseError } from "../errors/baseError.js";
6
+ /**
7
+ * Helper for detecting and resolving native platform modules (Node builtins and globals)
8
+ * so a class can prefer a faster native implementation while keeping a pure JavaScript
9
+ * fallback where the native form does not exist (e.g. a browser). See getModule() and
10
+ * init() below for how a builtin is opted into, and typeExists() for a global.
11
+ */
12
+ export class NativeModules {
13
+ /**
14
+ * SharedStore key for the registry of modules resolved via init().
15
+ * @internal
16
+ */
17
+ static _REGISTRY_KEY = "nativeModulesRegistry";
18
+ /**
19
+ * Check whether a global of the given name exists in the current environment.
20
+ * @param name The name of the global to check for, e.g. "Buffer".
21
+ * @returns True if the global exists, false otherwise (e.g. in a browser).
22
+ */
23
+ static typeExists(name) {
24
+ return !Is.undefined(globalThis[name]);
25
+ }
26
+ /**
27
+ * Get a module previously registered by init(). Never resolves anything itself, even
28
+ * a Node builtin - a specifier init() was never called for returns undefined here.
29
+ * @param name The module specifier, e.g. "node:crypto".
30
+ * @returns The module, or undefined if it was never registered via init().
31
+ */
32
+ static getModule(name) {
33
+ const registry = SharedStore.get(NativeModules._REGISTRY_KEY);
34
+ if (!Is.undefined(registry) && name in registry) {
35
+ return registry[name];
36
+ }
37
+ return undefined;
38
+ }
39
+ /**
40
+ * Get the specifiers of the modules registered via init().
41
+ * @returns The registered specifiers.
42
+ */
43
+ static names() {
44
+ const registry = SharedStore.get(NativeModules._REGISTRY_KEY);
45
+ return Object.keys(registry ?? {});
46
+ }
47
+ /**
48
+ * Resolve and register modules for getModule() to return, including Node builtins.
49
+ * @param modules The module specifiers to import and register.
50
+ * @returns The specifiers that failed, keyed by specifier, with the error raised.
51
+ */
52
+ static async init(modules) {
53
+ const registry = SharedStore.get(NativeModules._REGISTRY_KEY, () => ({}));
54
+ const failures = {};
55
+ await Promise.all(modules.map(async (name) => {
56
+ try {
57
+ registry[name] = await import(name);
58
+ }
59
+ catch (error) {
60
+ failures[name] = BaseError.fromError(error).toJsonObject(false);
61
+ }
62
+ }));
63
+ return failures;
64
+ }
65
+ }
66
+ //# sourceMappingURL=nativeModules.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nativeModules.js","sourceRoot":"","sources":["../../../src/utils/nativeModules.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAGnD;;;;;GAKG;AACH,MAAM,OAAO,aAAa;IACzB;;;OAGG;IACK,MAAM,CAAU,aAAa,GAAG,uBAAuB,CAAC;IAEhE;;;;OAIG;IACI,MAAM,CAAC,UAAU,CAAC,IAAY;QACpC,OAAO,CAAC,EAAE,CAAC,SAAS,CAAE,UAAyC,CAAC,IAAI,CAAC,CAAC,CAAC;IACxE,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,SAAS,CAAc,IAAY;QAChD,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAmC,aAAa,CAAC,aAAa,CAAC,CAAC;QAChG,IAAI,CAAC,EAAE,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,IAAI,IAAI,QAAQ,EAAE,CAAC;YACjD,OAAO,QAAQ,CAAC,IAAI,CAAM,CAAC;QAC5B,CAAC;QACD,OAAO,SAAS,CAAC;IAClB,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,KAAK;QAClB,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAmC,aAAa,CAAC,aAAa,CAAC,CAAC;QAChG,OAAO,MAAM,CAAC,IAAI,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC;IACpC,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,OAAiB;QACzC,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAC/B,aAAa,CAAC,aAAa,EAC3B,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,CACV,CAAC;QACF,MAAM,QAAQ,GAAoC,EAAE,CAAC;QAErD,MAAM,OAAO,CAAC,GAAG,CAChB,OAAO,CAAC,GAAG,CAAC,KAAK,EAAC,IAAI,EAAC,EAAE;YACxB,IAAI,CAAC;gBACJ,QAAQ,CAAC,IAAI,CAAC,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,CAAC;YACrC,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBAChB,QAAQ,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;YACjE,CAAC;QACF,CAAC,CAAC,CACF,CAAC;QAEF,OAAO,QAAQ,CAAC;IACjB,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is } from \"./is.js\";\nimport { SharedStore } from \"./sharedStore.js\";\nimport { BaseError } from \"../errors/baseError.js\";\nimport type { IError } from \"../models/IError.js\";\n\n/**\n * Helper for detecting and resolving native platform modules (Node builtins and globals)\n * so a class can prefer a faster native implementation while keeping a pure JavaScript\n * fallback where the native form does not exist (e.g. a browser). See getModule() and\n * init() below for how a builtin is opted into, and typeExists() for a global.\n */\nexport class NativeModules {\n\t/**\n\t * SharedStore key for the registry of modules resolved via init().\n\t * @internal\n\t */\n\tprivate static readonly _REGISTRY_KEY = \"nativeModulesRegistry\";\n\n\t/**\n\t * Check whether a global of the given name exists in the current environment.\n\t * @param name The name of the global to check for, e.g. \"Buffer\".\n\t * @returns True if the global exists, false otherwise (e.g. in a browser).\n\t */\n\tpublic static typeExists(name: string): boolean {\n\t\treturn !Is.undefined((globalThis as { [key: string]: unknown })[name]);\n\t}\n\n\t/**\n\t * Get a module previously registered by init(). Never resolves anything itself, even\n\t * a Node builtin - a specifier init() was never called for returns undefined here.\n\t * @param name The module specifier, e.g. \"node:crypto\".\n\t * @returns The module, or undefined if it was never registered via init().\n\t */\n\tpublic static getModule<T = unknown>(name: string): T | undefined {\n\t\tconst registry = SharedStore.get<{ [specifier: string]: unknown }>(NativeModules._REGISTRY_KEY);\n\t\tif (!Is.undefined(registry) && name in registry) {\n\t\t\treturn registry[name] as T;\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Get the specifiers of the modules registered via init().\n\t * @returns The registered specifiers.\n\t */\n\tpublic static names(): string[] {\n\t\tconst registry = SharedStore.get<{ [specifier: string]: unknown }>(NativeModules._REGISTRY_KEY);\n\t\treturn Object.keys(registry ?? {});\n\t}\n\n\t/**\n\t * Resolve and register modules for getModule() to return, including Node builtins.\n\t * @param modules The module specifiers to import and register.\n\t * @returns The specifiers that failed, keyed by specifier, with the error raised.\n\t */\n\tpublic static async init(modules: string[]): Promise<{ [specifier: string]: IError }> {\n\t\tconst registry = SharedStore.get<{ [specifier: string]: unknown }>(\n\t\t\tNativeModules._REGISTRY_KEY,\n\t\t\t() => ({})\n\t\t);\n\t\tconst failures: { [specifier: string]: IError } = {};\n\n\t\tawait Promise.all(\n\t\t\tmodules.map(async name => {\n\t\t\t\ttry {\n\t\t\t\t\tregistry[name] = await import(name);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tfailures[name] = BaseError.fromError(error).toJsonObject(false);\n\t\t\t\t}\n\t\t\t})\n\t\t);\n\n\t\treturn failures;\n\t}\n}\n"]}
@@ -67,6 +67,7 @@ export * from "./utils/guards.js";
67
67
  export * from "./utils/i18n.js";
68
68
  export * from "./utils/is.js";
69
69
  export * from "./utils/mutex.js";
70
+ export * from "./utils/nativeModules.js";
70
71
  export * from "./utils/sharedObjectBuffer.js";
71
72
  export * from "./utils/sharedStore.js";
72
73
  export * from "./utils/validation.js";
@@ -0,0 +1,35 @@
1
+ import type { IError } from "../models/IError.js";
2
+ /**
3
+ * Helper for detecting and resolving native platform modules (Node builtins and globals)
4
+ * so a class can prefer a faster native implementation while keeping a pure JavaScript
5
+ * fallback where the native form does not exist (e.g. a browser). See getModule() and
6
+ * init() below for how a builtin is opted into, and typeExists() for a global.
7
+ */
8
+ export declare class NativeModules {
9
+ /**
10
+ * Check whether a global of the given name exists in the current environment.
11
+ * @param name The name of the global to check for, e.g. "Buffer".
12
+ * @returns True if the global exists, false otherwise (e.g. in a browser).
13
+ */
14
+ static typeExists(name: string): boolean;
15
+ /**
16
+ * Get a module previously registered by init(). Never resolves anything itself, even
17
+ * a Node builtin - a specifier init() was never called for returns undefined here.
18
+ * @param name The module specifier, e.g. "node:crypto".
19
+ * @returns The module, or undefined if it was never registered via init().
20
+ */
21
+ static getModule<T = unknown>(name: string): T | undefined;
22
+ /**
23
+ * Get the specifiers of the modules registered via init().
24
+ * @returns The registered specifiers.
25
+ */
26
+ static names(): string[];
27
+ /**
28
+ * Resolve and register modules for getModule() to return, including Node builtins.
29
+ * @param modules The module specifiers to import and register.
30
+ * @returns The specifiers that failed, keyed by specifier, with the error raised.
31
+ */
32
+ static init(modules: string[]): Promise<{
33
+ [specifier: string]: IError;
34
+ }>;
35
+ }
package/docs/changelog.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.10.1-next.4](https://github.com/iotaledger/twin-framework/compare/core-v0.10.1-next.3...core-v0.10.1-next.4) (2026-09-18)
4
+
5
+
6
+ ### Features
7
+
8
+ * back base64, sha256 and chacha20poly1305 with native Node implementations ([#518](https://github.com/iotaledger/twin-framework/issues/518)) ([9c09337](https://github.com/iotaledger/twin-framework/commit/9c09337cb8e4045c2c1784b7c072a88de779234d))
9
+
10
+
11
+ ### Dependencies
12
+
13
+ * The following workspace dependencies were updated
14
+ * dependencies
15
+ * @twin.org/nameof bumped from 0.10.1-next.3 to 0.10.1-next.4
16
+ * devDependencies
17
+ * @twin.org/nameof-transformer bumped from 0.10.1-next.3 to 0.10.1-next.4
18
+ * @twin.org/nameof-vitest-plugin bumped from 0.10.1-next.3 to 0.10.1-next.4
19
+
3
20
  ## [0.10.1-next.3](https://github.com/iotaledger/twin-framework/compare/core-v0.10.1-next.2...core-v0.10.1-next.3) (2026-09-18)
4
21
 
5
22
 
package/docs/examples.md CHANGED
@@ -241,6 +241,20 @@ SharedStore.set('region', 'eu-west-1');
241
241
  SharedStore.get<string>('region'); // 'eu-west-1'
242
242
  ```
243
243
 
244
+ ## NativeModules
245
+
246
+ ```typescript
247
+ import { NativeModules } from '@twin.org/core';
248
+
249
+ NativeModules.typeExists('Buffer'); // true on Node, false in a browser without a polyfill
250
+
251
+ // Node builtins are only used by classes such as Sha256 once a host has registered them,
252
+ // typically once at startup. The returned object lists any specifiers that failed to load.
253
+ const failures = await NativeModules.init(['node:crypto']);
254
+ NativeModules.getModule('node:crypto'); // the module, or undefined if init() was never called for it
255
+ NativeModules.names(); // ['node:crypto']
256
+ ```
257
+
244
258
  ## RandomHelper and NumberHelper
245
259
 
246
260
  ```typescript
@@ -0,0 +1,103 @@
1
+ # Class: NativeModules
2
+
3
+ Helper for detecting and resolving native platform modules (Node builtins and globals)
4
+ so a class can prefer a faster native implementation while keeping a pure JavaScript
5
+ fallback where the native form does not exist (e.g. a browser). See getModule() and
6
+ init() below for how a builtin is opted into, and typeExists() for a global.
7
+
8
+ ## Constructors
9
+
10
+ ### Constructor
11
+
12
+ > **new NativeModules**(): `NativeModules`
13
+
14
+ #### Returns
15
+
16
+ `NativeModules`
17
+
18
+ ## Methods
19
+
20
+ ### typeExists() {#typeexists}
21
+
22
+ > `static` **typeExists**(`name`): `boolean`
23
+
24
+ Check whether a global of the given name exists in the current environment.
25
+
26
+ #### Parameters
27
+
28
+ ##### name
29
+
30
+ `string`
31
+
32
+ The name of the global to check for, e.g. "Buffer".
33
+
34
+ #### Returns
35
+
36
+ `boolean`
37
+
38
+ True if the global exists, false otherwise (e.g. in a browser).
39
+
40
+ ***
41
+
42
+ ### getModule() {#getmodule}
43
+
44
+ > `static` **getModule**\<`T`\>(`name`): `T` \| `undefined`
45
+
46
+ Get a module previously registered by init(). Never resolves anything itself, even
47
+ a Node builtin - a specifier init() was never called for returns undefined here.
48
+
49
+ #### Type Parameters
50
+
51
+ ##### T
52
+
53
+ `T` = `unknown`
54
+
55
+ #### Parameters
56
+
57
+ ##### name
58
+
59
+ `string`
60
+
61
+ The module specifier, e.g. "node:crypto".
62
+
63
+ #### Returns
64
+
65
+ `T` \| `undefined`
66
+
67
+ The module, or undefined if it was never registered via init().
68
+
69
+ ***
70
+
71
+ ### names() {#names}
72
+
73
+ > `static` **names**(): `string`[]
74
+
75
+ Get the specifiers of the modules registered via init().
76
+
77
+ #### Returns
78
+
79
+ `string`[]
80
+
81
+ The registered specifiers.
82
+
83
+ ***
84
+
85
+ ### init() {#init}
86
+
87
+ > `static` **init**(`modules`): `Promise`\<\{\[`specifier`: `string`\]: [`IError`](../interfaces/IError.md); \}\>
88
+
89
+ Resolve and register modules for getModule() to return, including Node builtins.
90
+
91
+ #### Parameters
92
+
93
+ ##### modules
94
+
95
+ `string`[]
96
+
97
+ The module specifiers to import and register.
98
+
99
+ #### Returns
100
+
101
+ `Promise`\<\{\[`specifier`: `string`\]: [`IError`](../interfaces/IError.md); \}\>
102
+
103
+ The specifiers that failed, keyed by specifier, with the error raised.
@@ -132,7 +132,7 @@ Additional options for the validation.
132
132
 
133
133
  ###### format?
134
134
 
135
- `RegExp` \| `"base64"` \| `"base58"` \| `"hex"`
135
+ `RegExp` \| `"base64"` \| `"hex"` \| `"base58"`
136
136
 
137
137
  Specific format to check.
138
138
 
@@ -44,6 +44,7 @@
44
44
  - [LfuCache](classes/LfuCache.md)
45
45
  - [LruCache](classes/LruCache.md)
46
46
  - [Mutex](classes/Mutex.md)
47
+ - [NativeModules](classes/NativeModules.md)
47
48
  - [SharedObjectBuffer](classes/SharedObjectBuffer.md)
48
49
  - [SharedStore](classes/SharedStore.md)
49
50
  - [Validation](classes/Validation.md)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/core",
3
- "version": "0.10.1-next.3",
3
+ "version": "0.10.1-next.4",
4
4
  "description": "Helper methods/classes for data type checking/validation/guarding/error handling",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,7 +14,7 @@
14
14
  "node": ">=24.0.0"
15
15
  },
16
16
  "dependencies": {
17
- "@twin.org/nameof": "0.10.1-next.3",
17
+ "@twin.org/nameof": "0.10.1-next.4",
18
18
  "intl-messageformat": "11.2.13",
19
19
  "rfc6902": "5.3.0"
20
20
  },