@atproto/lex-data 0.0.0

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 (82) hide show
  1. package/dist/blob.d.ts +16 -0
  2. package/dist/blob.d.ts.map +1 -0
  3. package/dist/blob.js +73 -0
  4. package/dist/blob.js.map +1 -0
  5. package/dist/cid.d.ts +12 -0
  6. package/dist/cid.d.ts.map +1 -0
  7. package/dist/cid.js +47 -0
  8. package/dist/cid.js.map +1 -0
  9. package/dist/index.d.ts +9 -0
  10. package/dist/index.d.ts.map +1 -0
  11. package/dist/index.js +12 -0
  12. package/dist/index.js.map +1 -0
  13. package/dist/language.d.ts +18 -0
  14. package/dist/language.d.ts.map +1 -0
  15. package/dist/language.js +30 -0
  16. package/dist/language.js.map +1 -0
  17. package/dist/lex-equals.d.ts +3 -0
  18. package/dist/lex-equals.d.ts.map +1 -0
  19. package/dist/lex-equals.js +78 -0
  20. package/dist/lex-equals.js.map +1 -0
  21. package/dist/lex.d.ts +18 -0
  22. package/dist/lex.d.ts.map +1 -0
  23. package/dist/lex.js +83 -0
  24. package/dist/lex.js.map +1 -0
  25. package/dist/lib/nodejs-buffer.d.ts +15 -0
  26. package/dist/lib/nodejs-buffer.d.ts.map +1 -0
  27. package/dist/lib/nodejs-buffer.js +12 -0
  28. package/dist/lib/nodejs-buffer.js.map +1 -0
  29. package/dist/object.d.ts +3 -0
  30. package/dist/object.d.ts.map +1 -0
  31. package/dist/object.js +22 -0
  32. package/dist/object.js.map +1 -0
  33. package/dist/uint8array-from-base64.d.ts +16 -0
  34. package/dist/uint8array-from-base64.d.ts.map +1 -0
  35. package/dist/uint8array-from-base64.js +60 -0
  36. package/dist/uint8array-from-base64.js.map +1 -0
  37. package/dist/uint8array-to-base64.d.ts +16 -0
  38. package/dist/uint8array-to-base64.d.ts.map +1 -0
  39. package/dist/uint8array-to-base64.js +30 -0
  40. package/dist/uint8array-to-base64.js.map +1 -0
  41. package/dist/uint8array.d.ts +21 -0
  42. package/dist/uint8array.d.ts.map +1 -0
  43. package/dist/uint8array.js +57 -0
  44. package/dist/uint8array.js.map +1 -0
  45. package/dist/utf8-grapheme-len.d.ts +3 -0
  46. package/dist/utf8-grapheme-len.d.ts.map +1 -0
  47. package/dist/utf8-grapheme-len.js +23 -0
  48. package/dist/utf8-grapheme-len.js.map +1 -0
  49. package/dist/utf8-len.d.ts +3 -0
  50. package/dist/utf8-len.d.ts.map +1 -0
  51. package/dist/utf8-len.js +50 -0
  52. package/dist/utf8-len.js.map +1 -0
  53. package/dist/utf8.d.ts +3 -0
  54. package/dist/utf8.d.ts.map +1 -0
  55. package/dist/utf8.js +12 -0
  56. package/dist/utf8.js.map +1 -0
  57. package/package.json +51 -0
  58. package/src/blob.test.ts +186 -0
  59. package/src/blob.ts +99 -0
  60. package/src/cid.ts +50 -0
  61. package/src/index.ts +8 -0
  62. package/src/language.test.ts +87 -0
  63. package/src/language.ts +39 -0
  64. package/src/lex-equals.test.ts +153 -0
  65. package/src/lex-equals.ts +85 -0
  66. package/src/lex.test.ts +124 -0
  67. package/src/lex.ts +78 -0
  68. package/src/lib/nodejs-buffer.ts +27 -0
  69. package/src/object.test.ts +78 -0
  70. package/src/object.ts +21 -0
  71. package/src/uint8array-from-base64.test.ts +113 -0
  72. package/src/uint8array-from-base64.ts +85 -0
  73. package/src/uint8array-to-base64.ts +45 -0
  74. package/src/uint8array.ts +78 -0
  75. package/src/utf8-grapheme-len.test.ts +37 -0
  76. package/src/utf8-grapheme-len.ts +21 -0
  77. package/src/utf8-len.test.ts +31 -0
  78. package/src/utf8-len.ts +51 -0
  79. package/src/utf8.ts +14 -0
  80. package/tsconfig.build.json +12 -0
  81. package/tsconfig.json +7 -0
  82. package/tsconfig.tests.json +9 -0
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fromBase64Node = exports.fromBase64Native = void 0;
4
+ exports.fromBase64Ponyfill = fromBase64Ponyfill;
5
+ const from_string_1 = require("uint8arrays/from-string");
6
+ const nodejs_buffer_js_1 = require("./lib/nodejs-buffer.js");
7
+ const Buffer = nodejs_buffer_js_1.NodeJSBuffer;
8
+ exports.fromBase64Native = typeof Uint8Array.fromBase64 === 'function'
9
+ ? function fromBase64Native(b64) {
10
+ return Uint8Array.fromBase64(b64, { lastChunkHandling: 'loose' });
11
+ }
12
+ : null;
13
+ exports.fromBase64Node = Buffer
14
+ ? function fromBase64Node(b64) {
15
+ const bytes = Buffer.from(b64, 'base64');
16
+ verifyBase64ForBytes(b64, bytes);
17
+ // Convert to Uint8Array because even though Buffer is a sub class of
18
+ // Uint8Array, it serializes differently to Uint8Array (e.g. in JSON) and
19
+ // results in unexpected behavior downstream (e.g. in tests)
20
+ return new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
21
+ }
22
+ : null;
23
+ function fromBase64Ponyfill(b64) {
24
+ const bytes = (0, from_string_1.fromString)(b64, 'base64');
25
+ verifyBase64ForBytes(b64, bytes);
26
+ return bytes;
27
+ }
28
+ // @NOTE NodeJS will silently stop decoding at the first invalid character,
29
+ // while "uint8arrays/from-string" will not validate that the padding is
30
+ // correct. The following function performs basic validation to ensure that the
31
+ // input was a valid base64 string. The availability of the "bytes" allows
32
+ // to perform checks with O[1] complexity.
33
+ function verifyBase64ForBytes(b64, bytes) {
34
+ const paddingCount = b64.endsWith('==') ? 2 : b64.endsWith('=') ? 1 : 0;
35
+ const trimmedLength = b64.length - paddingCount;
36
+ const expectedByteLength = Math.floor((trimmedLength * 3) / 4);
37
+ if (bytes.length !== expectedByteLength) {
38
+ throw new Error('Invalid base64 string');
39
+ }
40
+ const expectedB64Length = (bytes.length / 3) * 4;
41
+ const expectedPaddingCount = expectedB64Length % 4 === 0 ? 0 : 4 - (expectedB64Length % 4);
42
+ const expectedFullB64Length = expectedB64Length + expectedPaddingCount;
43
+ if (b64.length > expectedFullB64Length) {
44
+ throw new Error('Invalid base64 string');
45
+ }
46
+ // The previous might still allow false positive if only the last few
47
+ // chars are invalid.
48
+ for (let i = Math.ceil(expectedB64Length); i < b64.length - paddingCount; i++) {
49
+ const code = b64.charCodeAt(i);
50
+ if (!(code >= 65 && code <= 90) && // A-Z
51
+ !(code >= 97 && code <= 122) && // a-z
52
+ !(code >= 48 && code <= 57) && // 0-9
53
+ code !== 43 && // +
54
+ code !== 47 // /
55
+ ) {
56
+ throw new Error('Invalid base64 string');
57
+ }
58
+ }
59
+ }
60
+ //# sourceMappingURL=uint8array-from-base64.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uint8array-from-base64.js","sourceRoot":"","sources":["../src/uint8array-from-base64.ts"],"names":[],"mappings":";;;AAuCA,gDAIC;AA3CD,yDAAoD;AACpD,6DAAqD;AAErD,MAAM,MAAM,GAAG,+BAAY,CAAA;AAkBd,QAAA,gBAAgB,GAC3B,OAAO,UAAU,CAAC,UAAU,KAAK,UAAU;IACzC,CAAC,CAAC,SAAS,gBAAgB,CAAC,GAAW;QACnC,OAAO,UAAU,CAAC,UAAW,CAAC,GAAG,EAAE,EAAE,iBAAiB,EAAE,OAAO,EAAE,CAAC,CAAA;IACpE,CAAC;IACH,CAAC,CAAC,IAAI,CAAA;AAEG,QAAA,cAAc,GAAG,MAAM;IAClC,CAAC,CAAC,SAAS,cAAc,CAAC,GAAW;QACjC,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAA;QACxC,oBAAoB,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;QAChC,qEAAqE;QACrE,yEAAyE;QACzE,4DAA4D;QAC5D,OAAO,IAAI,UAAU,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,UAAU,CAAC,CAAA;IACzE,CAAC;IACH,CAAC,CAAC,IAAI,CAAA;AAER,SAAgB,kBAAkB,CAAC,GAAW;IAC5C,MAAM,KAAK,GAAG,IAAA,wBAAU,EAAC,GAAG,EAAE,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AAG,GAAG,CAAC,MAAM,GAAG,YAAY,EAC7B,CAAC,EAAE,EACH,CAAC;QACD,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;QAC9B,IACE,CAAC,CAAC,IAAI,IAAI,EAAE,IAAI,IAAI,IAAI,EAAE,CAAC,IAAI,MAAM;YACrC,CAAC,CAAC,IAAI,IAAI,EAAE,IAAI,IAAI,IAAI,GAAG,CAAC,IAAI,MAAM;YACtC,CAAC,CAAC,IAAI,IAAI,EAAE,IAAI,IAAI,IAAI,EAAE,CAAC,IAAI,MAAM;YACrC,IAAI,KAAK,EAAE,IAAI,IAAI;YACnB,IAAI,KAAK,EAAE,CAAC,IAAI;UAChB,CAAC;YACD,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAA;QAC1C,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["import { fromString } from 'uint8arrays/from-string'\nimport { NodeJSBuffer } from './lib/nodejs-buffer.js'\n\nconst Buffer = NodeJSBuffer\n\ndeclare global {\n interface Uint8ArrayConstructor {\n /**\n * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/fromBase64 Uint8Array.fromBase64()}\n */\n fromBase64?: (\n b64: string,\n options?: {\n /** @default 'base64' */\n alphabet?: 'base64' | 'base64url'\n lastChunkHandling?: 'loose' | 'strict' | 'stop-before-partial'\n },\n ) => Uint8Array\n }\n}\n\nexport const fromBase64Native =\n typeof Uint8Array.fromBase64 === 'function'\n ? function fromBase64Native(b64: string): Uint8Array {\n return Uint8Array.fromBase64!(b64, { lastChunkHandling: 'loose' })\n }\n : null\n\nexport const fromBase64Node = Buffer\n ? function fromBase64Node(b64: string): Uint8Array {\n const bytes = Buffer.from(b64, 'base64')\n verifyBase64ForBytes(b64, bytes)\n // Convert to Uint8Array because even though Buffer is a sub class of\n // Uint8Array, it serializes differently to Uint8Array (e.g. in JSON) and\n // results in unexpected behavior downstream (e.g. in tests)\n return new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength)\n }\n : null\n\nexport function fromBase64Ponyfill(b64: string): Uint8Array {\n const bytes = fromString(b64, 'base64')\n verifyBase64ForBytes(b64, bytes)\n return bytes\n}\n\n// @NOTE NodeJS will silently stop decoding at the first invalid character,\n// while \"uint8arrays/from-string\" will not validate that the padding is\n// correct. The following function performs basic validation to ensure that the\n// input was a valid base64 string. The availability of the \"bytes\" allows\n// to perform checks with O[1] complexity.\nfunction verifyBase64ForBytes(b64: string, bytes: Uint8Array): void {\n const paddingCount = b64.endsWith('==') ? 2 : b64.endsWith('=') ? 1 : 0\n const trimmedLength = b64.length - paddingCount\n const expectedByteLength = Math.floor((trimmedLength * 3) / 4)\n if (bytes.length !== expectedByteLength) {\n throw new Error('Invalid base64 string')\n }\n\n const expectedB64Length = (bytes.length / 3) * 4\n const expectedPaddingCount =\n expectedB64Length % 4 === 0 ? 0 : 4 - (expectedB64Length % 4)\n const expectedFullB64Length = expectedB64Length + expectedPaddingCount\n if (b64.length > expectedFullB64Length) {\n throw new Error('Invalid base64 string')\n }\n\n // The previous might still allow false positive if only the last few\n // chars are invalid.\n for (\n let i = Math.ceil(expectedB64Length);\n i < b64.length - paddingCount;\n i++\n ) {\n const code = b64.charCodeAt(i)\n if (\n !(code >= 65 && code <= 90) && // A-Z\n !(code >= 97 && code <= 122) && // a-z\n !(code >= 48 && code <= 57) && // 0-9\n code !== 43 && // +\n code !== 47 // /\n ) {\n throw new Error('Invalid base64 string')\n }\n }\n}\n"]}
@@ -0,0 +1,16 @@
1
+ declare global {
2
+ interface Uint8Array {
3
+ /**
4
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/toBase64 Uint8Array.prototype.toBase64()}
5
+ */
6
+ toBase64?: (options?: {
7
+ /** @default 'base64' */
8
+ alphabet?: 'base64' | 'base64url';
9
+ omitPadding?: boolean;
10
+ }) => string;
11
+ }
12
+ }
13
+ export declare const toBase64Native: ((bytes: Uint8Array) => string) | null;
14
+ export declare const toBase64Node: ((bytes: Uint8Array) => string) | null;
15
+ export declare function toBase64Ponyfill(bytes: Uint8Array): string;
16
+ //# sourceMappingURL=uint8array-to-base64.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uint8array-to-base64.d.ts","sourceRoot":"","sources":["../src/uint8array-to-base64.ts"],"names":[],"mappings":"AAKA,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,UAAU;QAClB;;WAEG;QACH,QAAQ,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE;YACpB,wBAAwB;YACxB,QAAQ,CAAC,EAAE,QAAQ,GAAG,WAAW,CAAA;YACjC,WAAW,CAAC,EAAE,OAAO,CAAA;SACtB,KAAK,MAAM,CAAA;KACb;CACF;AAED,eAAO,MAAM,cAAc,WAEU,UAAU,KAAG,MAAM,QAG9C,CAAA;AAEV,eAAO,MAAM,YAAY,WACQ,UAAU,KAAG,MAAM,QAc5C,CAAA;AAER,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAE1D"}
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toBase64Node = exports.toBase64Native = void 0;
4
+ exports.toBase64Ponyfill = toBase64Ponyfill;
5
+ const to_string_1 = require("uint8arrays/to-string");
6
+ const nodejs_buffer_js_1 = require("./lib/nodejs-buffer.js");
7
+ const Buffer = nodejs_buffer_js_1.NodeJSBuffer;
8
+ exports.toBase64Native = typeof Uint8Array.prototype.toBase64 === 'function'
9
+ ? function toBase64Native(bytes) {
10
+ return bytes.toBase64({ omitPadding: true });
11
+ }
12
+ : null;
13
+ exports.toBase64Node = Buffer
14
+ ? function toBase64Node(bytes) {
15
+ const b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes)).toString('base64');
16
+ // @NOTE We strip padding for strict compatibility with
17
+ // uint8arrays.toString behavior. Tests failing because of the presence of
18
+ // padding are not really synonymous with an actual error and we might
19
+ // (should?) actually want to keep the padding at some point.
20
+ return b64.charCodeAt(b64.length - 1) === /* '=' */ 0x3d
21
+ ? b64.charCodeAt(b64.length - 2) === /* '=' */ 0x3d
22
+ ? b64.slice(0, -2) // '=='
23
+ : b64.slice(0, -1) // '='
24
+ : b64;
25
+ }
26
+ : null;
27
+ function toBase64Ponyfill(bytes) {
28
+ return (0, to_string_1.toString)(bytes, 'base64');
29
+ }
30
+ //# sourceMappingURL=uint8array-to-base64.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uint8array-to-base64.js","sourceRoot":"","sources":["../src/uint8array-to-base64.ts"],"names":[],"mappings":";;;AA0CA,4CAEC;AA5CD,qDAAgD;AAChD,6DAAqD;AAErD,MAAM,MAAM,GAAG,+BAAY,CAAA;AAed,QAAA,cAAc,GACzB,OAAO,UAAU,CAAC,SAAS,CAAC,QAAQ,KAAK,UAAU;IACjD,CAAC,CAAC,SAAS,cAAc,CAAC,KAAiB;QACvC,OAAO,KAAK,CAAC,QAAS,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,CAAA;IAC/C,CAAC;IACH,CAAC,CAAC,IAAI,CAAA;AAEG,QAAA,YAAY,GAAG,MAAM;IAChC,CAAC,CAAC,SAAS,YAAY,CAAC,KAAiB;QACrC,MAAM,GAAG,GAAG,CACV,KAAK,YAAY,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CACrD,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAA;QACpB,uDAAuD;QACvD,0EAA0E;QAC1E,sEAAsE;QACtE,6DAA6D;QAC7D,OAAO,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,SAAS,CAAC,IAAI;YACtD,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,SAAS,CAAC,IAAI;gBACjD,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO;gBAC1B,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM;YAC3B,CAAC,CAAC,GAAG,CAAA;IACT,CAAC;IACH,CAAC,CAAC,IAAI,CAAA;AAER,SAAgB,gBAAgB,CAAC,KAAiB;IAChD,OAAO,IAAA,oBAAQ,EAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;AAClC,CAAC","sourcesContent":["import { toString } from 'uint8arrays/to-string'\nimport { NodeJSBuffer } from './lib/nodejs-buffer.js'\n\nconst Buffer = NodeJSBuffer\n\ndeclare global {\n interface Uint8Array {\n /**\n * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/toBase64 Uint8Array.prototype.toBase64()}\n */\n toBase64?: (options?: {\n /** @default 'base64' */\n alphabet?: 'base64' | 'base64url'\n omitPadding?: boolean\n }) => string\n }\n}\n\nexport const toBase64Native =\n typeof Uint8Array.prototype.toBase64 === 'function'\n ? function toBase64Native(bytes: Uint8Array): string {\n return bytes.toBase64!({ omitPadding: true })\n }\n : null\n\nexport const toBase64Node = Buffer\n ? function toBase64Node(bytes: Uint8Array): string {\n const b64 = (\n bytes instanceof Buffer ? bytes : Buffer.from(bytes)\n ).toString('base64')\n // @NOTE We strip padding for strict compatibility with\n // uint8arrays.toString behavior. Tests failing because of the presence of\n // padding are not really synonymous with an actual error and we might\n // (should?) actually want to keep the padding at some point.\n return b64.charCodeAt(b64.length - 1) === /* '=' */ 0x3d\n ? b64.charCodeAt(b64.length - 2) === /* '=' */ 0x3d\n ? b64.slice(0, -2) // '=='\n : b64.slice(0, -1) // '='\n : b64\n }\n : null\n\nexport function toBase64Ponyfill(bytes: Uint8Array): string {\n return toString(bytes, 'base64')\n}\n"]}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Encodes a Uint8Array into a base64 string.
3
+ *
4
+ * @returns The base64 encoded string
5
+ */
6
+ export declare const toBase64: (bytes: Uint8Array) => string;
7
+ /**
8
+ * Decodes a base64 string into a Uint8Array.
9
+ *
10
+ * @returns The decoded {@link Uint8Array}
11
+ * @throws If the input is not a valid base64 string
12
+ */
13
+ export declare const fromBase64: (b64: string) => Uint8Array;
14
+ /**
15
+ * Coerces various binary data representations into a Uint8Array.
16
+ *
17
+ * @return `undefined` if the input could not be coerced into a {@link Uint8Array}.
18
+ */
19
+ export declare function asUint8Array(input: unknown): Uint8Array | undefined;
20
+ export declare function ui8Equals(a: Uint8Array, b: Uint8Array): boolean;
21
+ //# sourceMappingURL=uint8array.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uint8array.d.ts","sourceRoot":"","sources":["../src/uint8array.ts"],"names":[],"mappings":"AAgBA;;;;GAIG;AACH,eAAO,MAAM,QAAQ,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,MACM,CAAA;AAEpD;;;;;GAKG;AACH,eAAO,MAAM,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,UACgB,CAAA;AAS1D;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS,CAkBnE;AAED,wBAAgB,SAAS,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,OAAO,CAY/D"}
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fromBase64 = exports.toBase64 = void 0;
4
+ exports.asUint8Array = asUint8Array;
5
+ exports.ui8Equals = ui8Equals;
6
+ const uint8array_from_base64_js_1 = require("./uint8array-from-base64.js");
7
+ const uint8array_to_base64_js_1 = require("./uint8array-to-base64.js");
8
+ // @TODO drop dependency on uint8arrays package once Uint8Array.fromBase64 /
9
+ // Uint8Array.prototype.toBase64 is widely supported, and mark fromBase64 /
10
+ // toBase64 as deprecated. We can also drop NodeJS specific implementations
11
+ // once NodeJS <24 is no longer supported.
12
+ /**
13
+ * Encodes a Uint8Array into a base64 string.
14
+ *
15
+ * @returns The base64 encoded string
16
+ */
17
+ exports.toBase64 = uint8array_to_base64_js_1.toBase64Native ?? uint8array_to_base64_js_1.toBase64Node ?? uint8array_to_base64_js_1.toBase64Ponyfill;
18
+ /**
19
+ * Decodes a base64 string into a Uint8Array.
20
+ *
21
+ * @returns The decoded {@link Uint8Array}
22
+ * @throws If the input is not a valid base64 string
23
+ */
24
+ exports.fromBase64 = uint8array_from_base64_js_1.fromBase64Native ?? uint8array_from_base64_js_1.fromBase64Node ?? uint8array_from_base64_js_1.fromBase64Ponyfill;
25
+ if (exports.toBase64 === uint8array_to_base64_js_1.toBase64Ponyfill || exports.fromBase64 === uint8array_from_base64_js_1.fromBase64Ponyfill) {
26
+ /*#__PURE__*/
27
+ console.warn('[@atproto/lex-data]: Uint8Array.fromBase64 / Uint8Array.prototype.toBase64 not available in this environment. Falling back to ponyfill implementation.');
28
+ }
29
+ /**
30
+ * Coerces various binary data representations into a Uint8Array.
31
+ *
32
+ * @return `undefined` if the input could not be coerced into a {@link Uint8Array}.
33
+ */
34
+ function asUint8Array(input) {
35
+ if (input instanceof Uint8Array) {
36
+ return input;
37
+ }
38
+ if (ArrayBuffer.isView(input)) {
39
+ return new Uint8Array(input.buffer, input.byteOffset, input.byteLength / Uint8Array.BYTES_PER_ELEMENT);
40
+ }
41
+ if (input instanceof ArrayBuffer) {
42
+ return new Uint8Array(input);
43
+ }
44
+ return undefined;
45
+ }
46
+ function ui8Equals(a, b) {
47
+ if (a.byteLength !== b.byteLength) {
48
+ return false;
49
+ }
50
+ for (let i = 0; i < a.byteLength; i++) {
51
+ if (a[i] !== b[i]) {
52
+ return false;
53
+ }
54
+ }
55
+ return true;
56
+ }
57
+ //# sourceMappingURL=uint8array.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uint8array.js","sourceRoot":"","sources":["../src/uint8array.ts"],"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C;YAClB,OAAO,KAAK,CAAA;QACd,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAA;AACb,CAAC","sourcesContent":["import {\n fromBase64Native,\n fromBase64Node,\n fromBase64Ponyfill,\n} from './uint8array-from-base64.js'\nimport {\n toBase64Native,\n toBase64Node,\n toBase64Ponyfill,\n} from './uint8array-to-base64.js'\n\n// @TODO drop dependency on uint8arrays package once Uint8Array.fromBase64 /\n// Uint8Array.prototype.toBase64 is widely supported, and mark fromBase64 /\n// toBase64 as deprecated. We can also drop NodeJS specific implementations\n// once NodeJS <24 is no longer supported.\n\n/**\n * Encodes a Uint8Array into a base64 string.\n *\n * @returns The base64 encoded string\n */\nexport const toBase64: (bytes: Uint8Array) => string =\n toBase64Native ?? toBase64Node ?? toBase64Ponyfill\n\n/**\n * Decodes a base64 string into a Uint8Array.\n *\n * @returns The decoded {@link Uint8Array}\n * @throws If the input is not a valid base64 string\n */\nexport const fromBase64: (b64: string) => Uint8Array =\n fromBase64Native ?? fromBase64Node ?? fromBase64Ponyfill\n\nif (toBase64 === toBase64Ponyfill || fromBase64 === fromBase64Ponyfill) {\n /*#__PURE__*/\n console.warn(\n '[@atproto/lex-data]: Uint8Array.fromBase64 / Uint8Array.prototype.toBase64 not available in this environment. Falling back to ponyfill implementation.',\n )\n}\n\n/**\n * Coerces various binary data representations into a Uint8Array.\n *\n * @return `undefined` if the input could not be coerced into a {@link Uint8Array}.\n */\nexport function asUint8Array(input: unknown): Uint8Array | undefined {\n if (input instanceof Uint8Array) {\n return input\n }\n\n if (ArrayBuffer.isView(input)) {\n return new Uint8Array(\n input.buffer,\n input.byteOffset,\n input.byteLength / Uint8Array.BYTES_PER_ELEMENT,\n )\n }\n\n if (input instanceof ArrayBuffer) {\n return new Uint8Array(input)\n }\n\n return undefined\n}\n\nexport function ui8Equals(a: Uint8Array, b: Uint8Array): boolean {\n if (a.byteLength !== b.byteLength) {\n return false\n }\n\n for (let i = 0; i < a.byteLength; i++) {\n if (a[i] !== b[i]) {\n return false\n }\n }\n\n return true\n}\n"]}
@@ -0,0 +1,3 @@
1
+ export declare const graphemeLenNative: ((str: string) => number) | null;
2
+ export declare function graphemeLenPonyfill(str: string): number;
3
+ //# sourceMappingURL=utf8-grapheme-len.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utf8-grapheme-len.d.ts","sourceRoot":"","sources":["../src/utf8-grapheme-len.ts"],"names":[],"mappings":"AAUA,eAAO,MAAM,iBAAiB,SACM,MAAM,KAAG,MAAM,QAK3C,CAAA;AAER,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEvD"}
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.graphemeLenNative = void 0;
4
+ exports.graphemeLenPonyfill = graphemeLenPonyfill;
5
+ const grapheme_1 = require("unicode-segmenter/grapheme");
6
+ // @TODO: Drop usage of "unicode-segmenter" package when Intl.Segmenter is
7
+ // widely supported.
8
+ // https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter
9
+ const segmenter = 'Segmenter' in Intl && typeof Intl.Segmenter === 'function'
10
+ ? /*#__PURE__*/ new Intl.Segmenter()
11
+ : null;
12
+ exports.graphemeLenNative = segmenter
13
+ ? function graphemeLenNative(str) {
14
+ let length = 0;
15
+ for (const _ of segmenter.segment(str))
16
+ length++;
17
+ return length;
18
+ }
19
+ : null;
20
+ function graphemeLenPonyfill(str) {
21
+ return (0, grapheme_1.countGraphemes)(str);
22
+ }
23
+ //# sourceMappingURL=utf8-grapheme-len.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utf8-grapheme-len.js","sourceRoot":"","sources":["../src/utf8-grapheme-len.ts"],"names":[],"mappings":";;;AAkBA,kDAEC;AApBD,yDAA2D;AAE3D,0EAA0E;AAC1E,oBAAoB;AACpB,+FAA+F;AAC/F,MAAM,SAAS,GACb,WAAW,IAAI,IAAI,IAAI,OAAO,IAAI,CAAC,SAAS,KAAK,UAAU;IACzD,CAAC,CAAC,aAAa,CAAC,IAAI,IAAI,CAAC,SAAS,EAAE;IACpC,CAAC,CAAC,IAAI,CAAA;AAEG,QAAA,iBAAiB,GAAG,SAAS;IACxC,CAAC,CAAC,SAAS,iBAAiB,CAAC,GAAW;QACpC,IAAI,MAAM,GAAG,CAAC,CAAA;QACd,KAAK,MAAM,CAAC,IAAI,SAAS,CAAC,OAAO,CAAC,GAAG,CAAC;YAAE,MAAM,EAAE,CAAA;QAChD,OAAO,MAAM,CAAA;IACf,CAAC;IACH,CAAC,CAAC,IAAI,CAAA;AAER,SAAgB,mBAAmB,CAAC,GAAW;IAC7C,OAAO,IAAA,yBAAc,EAAC,GAAG,CAAC,CAAA;AAC5B,CAAC","sourcesContent":["import { countGraphemes } from 'unicode-segmenter/grapheme'\n\n// @TODO: Drop usage of \"unicode-segmenter\" package when Intl.Segmenter is\n// widely supported.\n// https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter\nconst segmenter =\n 'Segmenter' in Intl && typeof Intl.Segmenter === 'function'\n ? /*#__PURE__*/ new Intl.Segmenter()\n : null\n\nexport const graphemeLenNative = segmenter\n ? function graphemeLenNative(str: string): number {\n let length = 0\n for (const _ of segmenter.segment(str)) length++\n return length\n }\n : null\n\nexport function graphemeLenPonyfill(str: string): number {\n return countGraphemes(str)\n}\n"]}
@@ -0,0 +1,3 @@
1
+ export declare const utf8LenNode: ((string: string) => number) | null;
2
+ export declare function utf8LenCompute(string: string): number;
3
+ //# sourceMappingURL=utf8-len.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utf8-len.d.ts","sourceRoot":"","sources":["../src/utf8-len.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,WAAW,YACS,MAAM,KAAG,MAAM,QAGxC,CAAA;AAER,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAsCrD"}
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.utf8LenNode = void 0;
4
+ exports.utf8LenCompute = utf8LenCompute;
5
+ const nodejs_buffer_js_1 = require("./lib/nodejs-buffer.js");
6
+ // @NOTE This file is not meant to be exported directly. Instead, we re-export
7
+ // public functions from ./utf8.ts. The reason for this separation is that this
8
+ // file allows to test both the NodeJS-optimized and ponyfill implementations.
9
+ exports.utf8LenNode = nodejs_buffer_js_1.NodeJSBuffer
10
+ ? function utf8LenNode(string) {
11
+ return nodejs_buffer_js_1.NodeJSBuffer.byteLength(string, 'utf8');
12
+ }
13
+ : null;
14
+ function utf8LenCompute(string) {
15
+ // The code below is similar to TextEncoder's implementation of UTF-8
16
+ // encoding. However, using TextEncoder to get the byte length is slower
17
+ // as it requires allocating a new Uint8Array and copying data:
18
+ // return new TextEncoder().encode(string).byteLength
19
+ // The base length is the string length (all ASCII)
20
+ let len = string.length;
21
+ let code;
22
+ // The loop calculates the number of additional bytes needed for
23
+ // non-ASCII characters
24
+ for (let i = 0; i < string.length; i += 1) {
25
+ code = string.charCodeAt(i);
26
+ if (code <= 0x7f) {
27
+ // ASCII, 1 byte
28
+ }
29
+ else if (code <= 0x7ff) {
30
+ // 2 bytes char
31
+ len += 1;
32
+ }
33
+ else {
34
+ // 3 bytes char
35
+ len += 2;
36
+ // If the current char is a high surrogate, and the next char is a low
37
+ // surrogate, skip the next char as the total is a 4 bytes char
38
+ // (represented as a surrogate pair in UTF-16) and was already accounted
39
+ // for.
40
+ if (code >= 0xd800 && code <= 0xdbff) {
41
+ code = string.charCodeAt(i + 1);
42
+ if (code >= 0xdc00 && code <= 0xdfff) {
43
+ i++;
44
+ }
45
+ }
46
+ }
47
+ }
48
+ return len;
49
+ }
50
+ //# sourceMappingURL=utf8-len.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utf8-len.js","sourceRoot":"","sources":["../src/utf8-len.ts"],"names":[],"mappings":";;;AAYA,wCAsCC;AAlDD,6DA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.js'\n\n// @NOTE This file is not meant to be exported directly. Instead, we re-export\n// public functions from ./utf8.ts. The reason for this separation is that this\n// file allows to test both the NodeJS-optimized and ponyfill implementations.\n\nexport const utf8LenNode = NodeJSBuffer\n ? function utf8LenNode(string: string): number {\n return NodeJSBuffer!.byteLength(string, 'utf8')\n }\n : null\n\nexport function utf8LenCompute(string: string): number {\n // The code below is similar to TextEncoder's implementation of UTF-8\n // encoding. However, using TextEncoder to get the byte length is slower\n // as it requires allocating a new Uint8Array and copying data:\n\n // return new TextEncoder().encode(string).byteLength\n\n // The base length is the string length (all ASCII)\n let len = string.length\n let code: number\n\n // The loop calculates the number of additional bytes needed for\n // non-ASCII characters\n for (let i = 0; i < string.length; i += 1) {\n code = string.charCodeAt(i)\n\n if (code <= 0x7f) {\n // ASCII, 1 byte\n } else if (code <= 0x7ff) {\n // 2 bytes char\n len += 1\n } else {\n // 3 bytes char\n len += 2\n // If the current char is a high surrogate, and the next char is a low\n // surrogate, skip the next char as the total is a 4 bytes char\n // (represented as a surrogate pair in UTF-16) and was already accounted\n // for.\n if (code >= 0xd800 && code <= 0xdbff) {\n code = string.charCodeAt(i + 1)\n if (code >= 0xdc00 && code <= 0xdfff) {\n i++\n }\n }\n }\n }\n\n return len\n}\n"]}
package/dist/utf8.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export declare const graphemeLen: (str: string) => number;
2
+ export declare const utf8Len: (string: string) => number;
3
+ //# sourceMappingURL=utf8.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utf8.d.ts","sourceRoot":"","sources":["../src/utf8.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,WAAW,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MACD,CAAA;AAS1C,eAAO,MAAM,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAAsC,CAAA"}
package/dist/utf8.js ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.utf8Len = exports.graphemeLen = void 0;
4
+ const utf8_grapheme_len_js_1 = require("./utf8-grapheme-len.js");
5
+ const utf8_len_js_1 = require("./utf8-len.js");
6
+ exports.graphemeLen = utf8_grapheme_len_js_1.graphemeLenNative ?? utf8_grapheme_len_js_1.graphemeLenPonyfill;
7
+ if (exports.graphemeLen === utf8_grapheme_len_js_1.graphemeLenPonyfill) {
8
+ /*#__PURE__*/
9
+ console.warn('[@atproto/lex-data]: Intl.Segmenter is not available in this environment. Falling back to ponyfill implementation.');
10
+ }
11
+ exports.utf8Len = utf8_len_js_1.utf8LenNode ?? utf8_len_js_1.utf8LenCompute;
12
+ //# sourceMappingURL=utf8.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utf8.js","sourceRoot":"","sources":["../src/utf8.ts"],"names":[],"mappings":";;;AAAA,iEAA+E;AAC/E,+CAA2D;AAE9C,QAAA,WAAW,GACtB,wCAAiB,IAAI,0CAAmB,CAAA;AAE1C,IAAI,mBAAW,KAAK,0CAAmB,EAAE,CAAC;IACxC,aAAa;IACb,OAAO,CAAC,IAAI,CACV,oHAAoH,CACrH,CAAA;AACH,CAAC;AAEY,QAAA,OAAO,GAA+B,yBAAW,IAAI,4BAAc,CAAA","sourcesContent":["import { graphemeLenNative, graphemeLenPonyfill } from './utf8-grapheme-len.js'\nimport { utf8LenCompute, utf8LenNode } from './utf8-len.js'\n\nexport const graphemeLen: (str: string) => number =\n graphemeLenNative ?? graphemeLenPonyfill\n\nif (graphemeLen === graphemeLenPonyfill) {\n /*#__PURE__*/\n console.warn(\n '[@atproto/lex-data]: Intl.Segmenter is not available in this environment. Falling back to ponyfill implementation.',\n )\n}\n\nexport const utf8Len: (string: string) => number = utf8LenNode ?? utf8LenCompute\n"]}
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@atproto/lex-data",
3
+ "version": "0.0.0",
4
+ "license": "MIT",
5
+ "description": "Core utilities for AT Lexicons",
6
+ "keywords": [
7
+ "atproto",
8
+ "lexicon",
9
+ "utilities"
10
+ ],
11
+ "homepage": "https://atproto.com",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/bluesky-social/atproto",
15
+ "directory": "packages/lex/lex-data"
16
+ },
17
+ "files": [
18
+ "./src",
19
+ "./tsconfig.build.json",
20
+ "./tsconfig.tests.json",
21
+ "./tsconfig.json",
22
+ "./dist"
23
+ ],
24
+ "sideEffects": false,
25
+ "type": "commonjs",
26
+ "main": "./dist/index.js",
27
+ "types": "./dist/index.d.ts",
28
+ "exports": {
29
+ ".": {
30
+ "browser": "./dist/index.js",
31
+ "import": "./dist/index.js",
32
+ "require": "./dist/index.js",
33
+ "types": "./dist/index.d.ts"
34
+ }
35
+ },
36
+ "dependencies": {
37
+ "@atproto/syntax": "workspace:*",
38
+ "multiformats": "^9.9.0",
39
+ "tslib": "^2.8.1",
40
+ "uint8arrays": "3.0.0",
41
+ "unicode-segmenter": "^0.14.0"
42
+ },
43
+ "devDependencies": {
44
+ "core-js": "^3",
45
+ "jest": "^28.1.2"
46
+ },
47
+ "scripts": {
48
+ "build": "tsc --build tsconfig.build.json",
49
+ "test": "jest"
50
+ }
51
+ }
@@ -0,0 +1,186 @@
1
+ import { isBlobRef, isLegacyBlobRef } from './blob.js'
2
+ import { CID } from './cid.js'
3
+
4
+ // await cidForRawBytes(Buffer.from('Hello, World!'))
5
+ const blobCid = CID.parse(
6
+ 'bafkreig77vqcdozl2wyk6z3cscaj5q5fggi53aoh64fewkdiri3cdauyn4',
7
+ )
8
+ // await cidForLex(Buffer.from('Hello, World!'))
9
+ const lexCid = CID.parse(
10
+ 'bafyreic52vzks7wdklat4evp3vimohl55i2unzqpshz2ytka5omzr7exdy',
11
+ )
12
+
13
+ describe('isBlobRef', () => {
14
+ it('tests valid blobCid and lexCid', () => {
15
+ expect(blobCid.code).toBe(0x55) // raw
16
+ expect(blobCid.multihash.code).toBe(0x12) // sha2-256
17
+ expect(lexCid.code).toBe(0x71) // dag-cbor
18
+ expect(lexCid.multihash.code).toBe(0x12) // sha2-256
19
+ })
20
+
21
+ it('parses valid blob', () => {
22
+ expect(
23
+ isBlobRef({
24
+ $type: 'blob',
25
+ ref: blobCid,
26
+ mimeType: 'image/jpeg',
27
+ size: 10000,
28
+ }),
29
+ ).toBe(true)
30
+
31
+ expect(
32
+ isBlobRef(
33
+ {
34
+ $type: 'blob',
35
+ ref: lexCid,
36
+ mimeType: 'image/jpeg',
37
+ size: 10000,
38
+ },
39
+ // In non-strict mode, any CID should be accepted
40
+ { strict: false },
41
+ ),
42
+ ).toBe(true)
43
+ })
44
+
45
+ it('rejects invalid inputs', () => {
46
+ expect(
47
+ isBlobRef({
48
+ $type: 'blob',
49
+ ref: { $link: blobCid.toString() },
50
+ mimeType: 'image/jpeg',
51
+ size: '10000',
52
+ }),
53
+ ).toBe(false)
54
+ expect(
55
+ isBlobRef(
56
+ {
57
+ $type: 'blob',
58
+ ref: { $link: blobCid.toString() },
59
+ mimeType: 'image/jpeg',
60
+ size: '10000',
61
+ },
62
+ { strict: true },
63
+ ),
64
+ ).toBe(false)
65
+
66
+ expect(
67
+ isBlobRef({
68
+ $type: 'blob',
69
+ mimeType: 'image/jpeg',
70
+ size: 10000,
71
+ }),
72
+ ).toBe(false)
73
+
74
+ expect(
75
+ isBlobRef(
76
+ {
77
+ $type: 'blob',
78
+ mimeType: 'image/jpeg',
79
+ size: 10000,
80
+ },
81
+ { strict: true },
82
+ ),
83
+ ).toBe(false)
84
+ })
85
+
86
+ it('rejects invalid CID/multihash code', () => {
87
+ expect(
88
+ isBlobRef(
89
+ {
90
+ $type: 'blob',
91
+ ref: blobCid,
92
+ mimeType: 'image/jpeg',
93
+ size: 10000,
94
+ },
95
+ { strict: true },
96
+ ),
97
+ ).toBe(true)
98
+
99
+ expect(
100
+ isBlobRef(
101
+ {
102
+ $type: 'blob',
103
+ ref: lexCid,
104
+ mimeType: 'image/jpeg',
105
+ size: 10000,
106
+ },
107
+ { strict: true },
108
+ ),
109
+ ).toBe(false)
110
+ })
111
+
112
+ it('rejects extra keys', () => {
113
+ expect(
114
+ isBlobRef({
115
+ $type: 'blob',
116
+ ref: blobCid,
117
+ mimeType: 'image/jpeg',
118
+ size: 10000,
119
+ extra: 'not allowed',
120
+ }),
121
+ ).toBe(false)
122
+
123
+ expect(
124
+ isBlobRef(
125
+ {
126
+ $type: 'blob',
127
+ ref: blobCid,
128
+ mimeType: 'image/jpeg',
129
+ size: 10000,
130
+ extra: 'not allowed',
131
+ },
132
+ { strict: true },
133
+ ),
134
+ ).toBe(false)
135
+ })
136
+ })
137
+
138
+ describe('isLegacyBlobRef', () => {
139
+ it('parses valid legacy blob', () => {
140
+ expect(
141
+ isLegacyBlobRef({
142
+ cid: blobCid.toString(),
143
+ mimeType: 'image/jpeg',
144
+ }),
145
+ ).toBe(true)
146
+
147
+ expect(
148
+ isLegacyBlobRef({
149
+ cid: lexCid.toString(),
150
+ mimeType: 'image/jpeg',
151
+ }),
152
+ ).toBe(true)
153
+ })
154
+
155
+ it('rejects invalid inputs', () => {
156
+ expect(
157
+ isLegacyBlobRef({
158
+ cid: 'babbaaa',
159
+ mimeType: 'image/jpeg',
160
+ }),
161
+ ).toBe(false)
162
+
163
+ expect(
164
+ isLegacyBlobRef({
165
+ cid: 12345,
166
+ mimeType: 'image/jpeg',
167
+ }),
168
+ ).toBe(false)
169
+
170
+ expect(
171
+ isLegacyBlobRef({
172
+ mimeType: 'image/jpeg',
173
+ }),
174
+ ).toBe(false)
175
+ })
176
+
177
+ it('rejects extra keys', () => {
178
+ expect(
179
+ isLegacyBlobRef({
180
+ cid: blobCid.toString(),
181
+ mimeType: 'image/jpeg',
182
+ extra: 'not allowed',
183
+ }),
184
+ ).toBe(false)
185
+ })
186
+ })