@atproto/lex-data 0.0.14 → 0.1.0-next.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 (68) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/blob.d.ts +118 -39
  3. package/dist/blob.d.ts.map +1 -1
  4. package/dist/blob.js +68 -22
  5. package/dist/blob.js.map +1 -1
  6. package/dist/cid.d.ts.map +1 -1
  7. package/dist/cid.js +75 -79
  8. package/dist/cid.js.map +1 -1
  9. package/dist/index.js +8 -11
  10. package/dist/index.js.map +1 -1
  11. package/dist/lex-equals.js +9 -12
  12. package/dist/lex-equals.js.map +1 -1
  13. package/dist/lex-error.js +2 -7
  14. package/dist/lex-error.js.map +1 -1
  15. package/dist/lex.js +10 -17
  16. package/dist/lex.js.map +1 -1
  17. package/dist/lib/nodejs-buffer.d.ts +4 -0
  18. package/dist/lib/nodejs-buffer.d.ts.map +1 -1
  19. package/dist/lib/nodejs-buffer.js +1 -4
  20. package/dist/lib/nodejs-buffer.js.map +1 -1
  21. package/dist/lib/util.js +2 -6
  22. package/dist/lib/util.js.map +1 -1
  23. package/dist/object.js +3 -8
  24. package/dist/object.js.map +1 -1
  25. package/dist/uint8array-base64.js +1 -2
  26. package/dist/uint8array-concat.d.ts +2 -2
  27. package/dist/uint8array-concat.d.ts.map +1 -1
  28. package/dist/uint8array-concat.js +4 -8
  29. package/dist/uint8array-concat.js.map +1 -1
  30. package/dist/uint8array-from-base64.js +7 -11
  31. package/dist/uint8array-from-base64.js.map +1 -1
  32. package/dist/uint8array-to-base64.js +7 -11
  33. package/dist/uint8array-to-base64.js.map +1 -1
  34. package/dist/uint8array.d.ts +1 -1
  35. package/dist/uint8array.d.ts.map +1 -1
  36. package/dist/uint8array.js +17 -23
  37. package/dist/uint8array.js.map +1 -1
  38. package/dist/utf8-from-base64.js +6 -10
  39. package/dist/utf8-from-base64.js.map +1 -1
  40. package/dist/utf8-from-bytes.d.ts +3 -0
  41. package/dist/utf8-from-bytes.d.ts.map +1 -0
  42. package/dist/utf8-from-bytes.js +15 -0
  43. package/dist/utf8-from-bytes.js.map +1 -0
  44. package/dist/utf8-grapheme-len.js +4 -8
  45. package/dist/utf8-grapheme-len.js.map +1 -1
  46. package/dist/utf8-len.js +4 -8
  47. package/dist/utf8-len.js.map +1 -1
  48. package/dist/utf8-to-base64.js +8 -12
  49. package/dist/utf8-to-base64.js.map +1 -1
  50. package/dist/utf8.d.ts +18 -0
  51. package/dist/utf8.d.ts.map +1 -1
  52. package/dist/utf8.js +32 -16
  53. package/dist/utf8.js.map +1 -1
  54. package/package.json +7 -8
  55. package/src/blob.test.ts +38 -25
  56. package/src/blob.ts +190 -52
  57. package/src/cid-implementation.test.ts +3 -3
  58. package/src/cid.ts +1 -0
  59. package/src/core-js.d.ts +2 -0
  60. package/src/lib/nodejs-buffer.ts +10 -0
  61. package/src/uint8array-concat.ts +7 -3
  62. package/src/uint8array-from-base64.test.ts +2 -2
  63. package/src/uint8array-to-base64.test.ts +2 -2
  64. package/src/uint8array.test.ts +2 -2
  65. package/src/utf8-from-bytes.test.ts +43 -0
  66. package/src/utf8-from-bytes.ts +21 -0
  67. package/src/utf8.ts +20 -0
  68. package/tsconfig.tests.json +1 -1
package/dist/cid.js CHANGED
@@ -1,32 +1,9 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CID = exports.SHA512_HASH_CODE = exports.SHA256_HASH_CODE = exports.RAW_DATA_CODEC = exports.CBOR_DATA_CODEC = void 0;
4
- exports.multihashEquals = multihashEquals;
5
- exports.asMultiformatsCID = asMultiformatsCID;
6
- exports.isRawCid = isRawCid;
7
- exports.isDaslCid = isDaslCid;
8
- exports.isCborCid = isCborCid;
9
- exports.checkCid = checkCid;
10
- exports.isCid = isCid;
11
- exports.ifCid = ifCid;
12
- exports.asCid = asCid;
13
- exports.decodeCid = decodeCid;
14
- exports.parseCid = parseCid;
15
- exports.validateCidString = validateCidString;
16
- exports.parseCidSafe = parseCidSafe;
17
- exports.ensureValidCidString = ensureValidCidString;
18
- exports.isCidForBytes = isCidForBytes;
19
- exports.createCid = createCid;
20
- exports.cidForCbor = cidForCbor;
21
- exports.cidForRawBytes = cidForRawBytes;
22
- exports.cidForRawHash = cidForRawHash;
23
- const cid_1 = require("multiformats/cid");
24
- Object.defineProperty(exports, "CID", { enumerable: true, get: function () { return cid_1.CID; } });
25
- const digest_1 = require("multiformats/hashes/digest");
26
- const sha2_1 = require("multiformats/hashes/sha2");
27
- const util_js_1 = require("./lib/util.js");
28
- const object_js_1 = require("./object.js");
29
- const uint8array_js_1 = require("./uint8array.js");
1
+ import { CID } from 'multiformats/cid';
2
+ import { create as createDigest } from 'multiformats/hashes/digest';
3
+ import { sha256, sha512 } from 'multiformats/hashes/sha2';
4
+ import { isUint8, toHexString } from './lib/util.js';
5
+ import { isObject } from './object.js';
6
+ import { ui8Equals } from './uint8array.js';
30
7
  /**
31
8
  * Codec code that indicates the CID references a CBOR-encoded data structure.
32
9
  *
@@ -34,7 +11,7 @@ const uint8array_js_1 = require("./uint8array.js");
34
11
  *
35
12
  * @see {@link https://dasl.ing/cid.html Content IDs (DASL)}
36
13
  */
37
- exports.CBOR_DATA_CODEC = 0x71;
14
+ export const CBOR_DATA_CODEC = 0x71;
38
15
  /**
39
16
  * Codec code that indicates the CID references raw binary data (like media blobs).
40
17
  *
@@ -42,15 +19,15 @@ exports.CBOR_DATA_CODEC = 0x71;
42
19
  *
43
20
  * @see {@link https://dasl.ing/cid.html Content IDs (DASL)}
44
21
  */
45
- exports.RAW_DATA_CODEC = 0x55;
22
+ export const RAW_DATA_CODEC = 0x55;
46
23
  /**
47
24
  * Hash code that indicates that a CID uses SHA-256.
48
25
  */
49
- exports.SHA256_HASH_CODE = sha2_1.sha256.code;
26
+ export const SHA256_HASH_CODE = sha256.code;
50
27
  /**
51
28
  * Hash code that indicates that a CID uses SHA-512.
52
29
  */
53
- exports.SHA512_HASH_CODE = sha2_1.sha512.code;
30
+ export const SHA512_HASH_CODE = sha512.code;
54
31
  /**
55
32
  * Compares two {@link Multihash} for equality.
56
33
  *
@@ -58,11 +35,30 @@ exports.SHA512_HASH_CODE = sha2_1.sha512.code;
58
35
  * @param b - Second {@link Multihash}
59
36
  * @returns `true` if both multihashes have the same code and digest
60
37
  */
61
- function multihashEquals(a, b) {
38
+ export function multihashEquals(a, b) {
62
39
  if (a === b)
63
40
  return true;
64
- return a.code === b.code && (0, uint8array_js_1.ui8Equals)(a.digest, b.digest);
41
+ return a.code === b.code && ui8Equals(a.digest, b.digest);
65
42
  }
43
+ // multiformats' CID class is not very portable because:
44
+ //
45
+ // - In dependent packages that use "moduleResolution" set to "node16",
46
+ // "nodenext" or "bundler", TypeScript fails to properly resolve the
47
+ // multiformats package when importing CID from @atproto/lex-data. This causes
48
+ // type errors in those packages. This is caused by the fact that the
49
+ // multiformats version <10 (which is the last version that supports CommonJS)
50
+ // uses "exports" field in package.json, which do not contain "types"
51
+ // entrypoints.
52
+ // https://www.npmjs.com/package/multiformats/v/9.9.0?activeTab=code
53
+ // - By defining our own interface and helper functions, we can have more
54
+ // control over the public API exposed by this package.
55
+ // - It allow us to have a stable interface in case we need to swap out, or
56
+ // eventually update multiformats (should we choose to drop CommonJS support)
57
+ // in the future.
58
+ // @NOTE Even though it is not portable, we still re-export CID here so that
59
+ // dependent packages where it can be used, have access to it (instead of
60
+ // importing directly from "multiformats" or "multiformats/cid").
61
+ export { /** @deprecated */ CID };
66
62
  /**
67
63
  * Converts a {@link Cid} to a multiformats {@link CID} instance.
68
64
  *
@@ -71,12 +67,12 @@ function multihashEquals(a, b) {
71
67
  * implementation directly. This is to avoid compatibility issues, and in order
72
68
  * to allow better portability, compatibility and future updates.
73
69
  */
74
- function asMultiformatsCID(input) {
70
+ export function asMultiformatsCID(input) {
75
71
  const cid =
76
72
  // Already a multiformats CID instance
77
- cid_1.CID.asCID(input) ??
73
+ CID.asCID(input) ??
78
74
  // Create a new multiformats CID instance
79
- cid_1.CID.create(input.version, input.code, (0, digest_1.create)(input.multihash.code, input.multihash.digest));
75
+ CID.create(input.version, input.code, createDigest(input.multihash.code, input.multihash.digest));
80
76
  // @NOTE: the "satisfies" operator is used here to ensure that the Cid
81
77
  // interface is indeed compatible with multiformats' CID implementation, which
82
78
  // allows us to safely rely on multiformats' CID implementation where Cid are
@@ -89,8 +85,8 @@ function asMultiformatsCID(input) {
89
85
  * @param cid - The CID to check
90
86
  * @returns `true` if the CID is a version 1 CID with raw multicodec
91
87
  */
92
- function isRawCid(cid) {
93
- return cid.version === 1 && cid.code === exports.RAW_DATA_CODEC;
88
+ export function isRawCid(cid) {
89
+ return cid.version === 1 && cid.code === RAW_DATA_CODEC;
94
90
  }
95
91
  /**
96
92
  * Type guard to check if a CID is DASL compliant.
@@ -98,10 +94,10 @@ function isRawCid(cid) {
98
94
  * @param cid - The CID to check
99
95
  * @returns `true` if the CID is DASL compliant (v1, raw/dag-cbor, sha256)
100
96
  */
101
- function isDaslCid(cid) {
97
+ export function isDaslCid(cid) {
102
98
  return (cid.version === 1 &&
103
- (cid.code === exports.RAW_DATA_CODEC || cid.code === exports.CBOR_DATA_CODEC) &&
104
- cid.multihash.code === exports.SHA256_HASH_CODE &&
99
+ (cid.code === RAW_DATA_CODEC || cid.code === CBOR_DATA_CODEC) &&
100
+ cid.multihash.code === SHA256_HASH_CODE &&
105
101
  cid.multihash.digest.byteLength === 0x20 // Should always be 32 bytes (256 bits) for SHA-256, but double-checking anyways
106
102
  );
107
103
  }
@@ -111,10 +107,10 @@ function isDaslCid(cid) {
111
107
  * @param cid - The CID to check
112
108
  * @returns `true` if the CID is a DAG-CBOR CID (v1, dag-cbor, sha256)
113
109
  */
114
- function isCborCid(cid) {
115
- return cid.code === exports.CBOR_DATA_CODEC && isDaslCid(cid);
110
+ export function isCborCid(cid) {
111
+ return cid.code === CBOR_DATA_CODEC && isDaslCid(cid);
116
112
  }
117
- function checkCid(cid, options) {
113
+ export function checkCid(cid, options) {
118
114
  switch (options?.flavor) {
119
115
  case undefined:
120
116
  return true;
@@ -128,25 +124,25 @@ function checkCid(cid, options) {
128
124
  throw new TypeError(`Unknown CID flavor: ${options?.flavor}`);
129
125
  }
130
126
  }
131
- function isCid(value, options) {
127
+ export function isCid(value, options) {
132
128
  return isCidImplementation(value) && checkCid(value, options);
133
129
  }
134
- function ifCid(value, options) {
130
+ export function ifCid(value, options) {
135
131
  if (isCid(value, options))
136
132
  return value;
137
133
  return null;
138
134
  }
139
- function asCid(value, options) {
135
+ export function asCid(value, options) {
140
136
  if (isCid(value, options))
141
137
  return value;
142
138
  throw new Error(`Invalid ${options?.flavor ? `${options.flavor} CID` : 'CID'} "${value}"`);
143
139
  }
144
- function decodeCid(cidBytes, options) {
145
- const cid = cid_1.CID.decode(cidBytes);
140
+ export function decodeCid(cidBytes, options) {
141
+ const cid = CID.decode(cidBytes);
146
142
  return asCid(cid, options);
147
143
  }
148
- function parseCid(input, options) {
149
- const cid = cid_1.CID.parse(input);
144
+ export function parseCid(input, options) {
145
+ const cid = CID.parse(input);
150
146
  return asCid(cid, options);
151
147
  }
152
148
  /**
@@ -159,10 +155,10 @@ function parseCid(input, options) {
159
155
  * @param options - Optional flavor constraints
160
156
  * @returns `true` if the string is a valid CID
161
157
  */
162
- function validateCidString(input, options) {
158
+ export function validateCidString(input, options) {
163
159
  return parseCidSafe(input, options)?.toString() === input;
164
160
  }
165
- function parseCidSafe(input, options) {
161
+ export function parseCidSafe(input, options) {
166
162
  try {
167
163
  return parseCid(input, options);
168
164
  }
@@ -177,7 +173,7 @@ function parseCidSafe(input, options) {
177
173
  * @param options - Optional flavor constraints
178
174
  * @throws If the string is not a valid CID
179
175
  */
180
- function ensureValidCidString(input, options) {
176
+ export function ensureValidCidString(input, options) {
181
177
  if (!validateCidString(input, options)) {
182
178
  throw new Error(`Invalid CID string "${input}"`);
183
179
  }
@@ -188,17 +184,17 @@ function ensureValidCidString(input, options) {
188
184
  * @params bytes The bytes to verify.
189
185
  * @returns true if the CID matches the bytes, false otherwise.
190
186
  */
191
- async function isCidForBytes(cid, bytes) {
192
- if (cid.multihash.code === sha2_1.sha256.code) {
193
- const multihash = await sha2_1.sha256.digest(bytes);
187
+ export async function isCidForBytes(cid, bytes) {
188
+ if (cid.multihash.code === sha256.code) {
189
+ const multihash = await sha256.digest(bytes);
194
190
  return multihashEquals(multihash, cid.multihash);
195
191
  }
196
- if (cid.multihash.code === sha2_1.sha512.code) {
197
- const multihash = await sha2_1.sha512.digest(bytes);
192
+ if (cid.multihash.code === sha512.code) {
193
+ const multihash = await sha512.digest(bytes);
198
194
  return multihashEquals(multihash, cid.multihash);
199
195
  }
200
196
  // Don't know how to verify other multihash codes
201
- throw new Error(`Unsupported CID multihash code: ${(0, util_js_1.toHexString)(cid.multihash.code)}`);
197
+ throw new Error(`Unsupported CID multihash code: ${toHexString(cid.multihash.code)}`);
202
198
  }
203
199
  /**
204
200
  * Creates a CID from a multicodec, multihash code, and digest.
@@ -215,8 +211,8 @@ async function isCidForBytes(cid, bytes) {
215
211
  * const cid = createCid(RAW_DATA_CODEC, SHA256_HASH_CODE, hashDigest)
216
212
  * ```
217
213
  */
218
- function createCid(code, multihashCode, digest) {
219
- const cid = cid_1.CID.createV1(code, (0, digest_1.create)(multihashCode, digest));
214
+ export function createCid(code, multihashCode, digest) {
215
+ const cid = CID.createV1(code, createDigest(multihashCode, digest));
220
216
  return cid;
221
217
  }
222
218
  /**
@@ -227,9 +223,9 @@ function createCid(code, multihashCode, digest) {
227
223
  * @param bytes - The CBOR-encoded bytes to hash
228
224
  * @returns A promise that resolves to the CborCid
229
225
  */
230
- async function cidForCbor(bytes) {
231
- const multihash = await sha2_1.sha256.digest(bytes);
232
- return cid_1.CID.createV1(exports.CBOR_DATA_CODEC, multihash);
226
+ export async function cidForCbor(bytes) {
227
+ const multihash = await sha256.digest(bytes);
228
+ return CID.createV1(CBOR_DATA_CODEC, multihash);
233
229
  }
234
230
  /**
235
231
  * Creates a raw CID for the given binary bytes.
@@ -239,9 +235,9 @@ async function cidForCbor(bytes) {
239
235
  * @param bytes - The raw binary bytes to hash
240
236
  * @returns A promise that resolves to the RawCid
241
237
  */
242
- async function cidForRawBytes(bytes) {
243
- const multihash = await sha2_1.sha256.digest(bytes);
244
- return cid_1.CID.createV1(exports.RAW_DATA_CODEC, multihash);
238
+ export async function cidForRawBytes(bytes) {
239
+ const multihash = await sha256.digest(bytes);
240
+ return CID.createV1(RAW_DATA_CODEC, multihash);
245
241
  }
246
242
  /**
247
243
  * Creates a raw CID from an existing SHA-256 hash digest.
@@ -250,15 +246,15 @@ async function cidForRawBytes(bytes) {
250
246
  * @returns A RawCid with the given digest
251
247
  * @throws If the digest is not a valid SHA-256 hash (not 32 bytes)
252
248
  */
253
- function cidForRawHash(digest) {
249
+ export function cidForRawHash(digest) {
254
250
  // Fool-proofing
255
251
  if (digest.length !== 0x20) {
256
- throw new Error(`Invalid SHA-256 hash length: ${(0, util_js_1.toHexString)(digest.length)}`);
252
+ throw new Error(`Invalid SHA-256 hash length: ${toHexString(digest.length)}`);
257
253
  }
258
- return createCid(exports.RAW_DATA_CODEC, sha2_1.sha256.code, digest);
254
+ return createCid(RAW_DATA_CODEC, sha256.code, digest);
259
255
  }
260
256
  function isCidImplementation(value) {
261
- if (cid_1.CID.asCID(value)) {
257
+ if (CID.asCID(value)) {
262
258
  // CIDs created using older multiformats versions did not have a "bytes"
263
259
  // property.
264
260
  return value.bytes != null;
@@ -266,17 +262,17 @@ function isCidImplementation(value) {
266
262
  else {
267
263
  // Unknown implementation, do a structural check
268
264
  try {
269
- if (!(0, object_js_1.isObject)(value))
265
+ if (!isObject(value))
270
266
  return false;
271
267
  const val = value;
272
268
  if (val.version !== 0 && val.version !== 1)
273
269
  return false;
274
- if (!(0, util_js_1.isUint8)(val.code))
270
+ if (!isUint8(val.code))
275
271
  return false;
276
- if (!(0, object_js_1.isObject)(val.multihash))
272
+ if (!isObject(val.multihash))
277
273
  return false;
278
274
  const mh = val.multihash;
279
- if (!(0, util_js_1.isUint8)(mh.code))
275
+ if (!isUint8(mh.code))
280
276
  return false;
281
277
  if (!(mh.digest instanceof Uint8Array))
282
278
  return false;
@@ -293,7 +289,7 @@ function isCidImplementation(value) {
293
289
  return false;
294
290
  if (val.bytes.length !== 4 + mh.digest.length)
295
291
  return false;
296
- if (!(0, uint8array_js_1.ui8Equals)(val.bytes.subarray(4), mh.digest))
292
+ if (!ui8Equals(val.bytes.subarray(4), mh.digest))
297
293
  return false;
298
294
  if (typeof val.equals !== 'function')
299
295
  return false;
package/dist/cid.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"cid.js","sourceRoot":"","sources":["../src/cid.ts"],"names":[],"mappings":";;;AAgEA,0CAGC;AAsDD,8CAoBC;AAkFD,4BAEC;AAkBD,8BAOC;AAiBD,8BAEC;AAoCD,4BAaC;AAWD,sBAEC;AAaD,sBAGC;AAeD,sBAKC;AAaD,8BAMC;AAYD,4BAGC;AAYD,8CAKC;AA2BD,oCASC;AASD,oDAOC;AAQD,sCAkBC;AAiBD,8BAOC;AAUD,gCAGC;AAUD,wCAGC;AASD,sCAQC;AAnjBD,0CAAsC;AA+GV,oFA/GnB,SAAG,OA+GmB;AA9G/B,uDAAmE;AACnE,mDAAyD;AACzD,2CAAoD;AACpD,2CAAsC;AACtC,mDAA2C;AAE3C;;;;;;GAMG;AACU,QAAA,eAAe,GAAG,IAAI,CAAA;AAGnC;;;;;;GAMG;AACU,QAAA,cAAc,GAAG,IAAI,CAAA;AAGlC;;GAEG;AACU,QAAA,gBAAgB,GAAG,aAAM,CAAC,IAAI,CAAA;AAG3C;;GAEG;AACU,QAAA,gBAAgB,GAAG,aAAM,CAAC,IAAI,CAAA;AAqB3C;;;;;;GAMG;AACH,SAAgB,eAAe,CAAC,CAAY,EAAE,CAAY;IACxD,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,CAAA;IACxB,OAAO,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,IAAI,IAAI,IA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AAI,CAAC,IAAA,yBAAS,EAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC;gBAAE,OAAO,KAAK,CAAA;YAE9D,IAAI,OAAO,GAAG,CAAC,MAAM,KAAK,UAAU;gBAAE,OAAO,KAAK,CAAA;YAClD,IAAI,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,IAAI;gBAAE,OAAO,KAAK,CAAA;YAE1C,OAAO,IAAI,CAAA;QACb,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAA;QACd,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["import { CID } from 'multiformats/cid'\nimport { create as createDigest } from 'multiformats/hashes/digest'\nimport { sha256, sha512 } from 'multiformats/hashes/sha2'\nimport { isUint8, toHexString } from './lib/util.js'\nimport { isObject } from './object.js'\nimport { ui8Equals } from './uint8array.js'\n\n/**\n * Codec code that indicates the CID references a CBOR-encoded data structure.\n *\n * Used when encoding structured data in AT Protocol repositories.\n *\n * @see {@link https://dasl.ing/cid.html Content IDs (DASL)}\n */\nexport const CBOR_DATA_CODEC = 0x71\nexport type CBOR_DATA_CODEC = typeof CBOR_DATA_CODEC\n\n/**\n * Codec code that indicates the CID references raw binary data (like media blobs).\n *\n * Used in DASL CIDs for binary blobs like images and media.\n *\n * @see {@link https://dasl.ing/cid.html Content IDs (DASL)}\n */\nexport const RAW_DATA_CODEC = 0x55\nexport type RAW_DATA_CODEC = typeof RAW_DATA_CODEC\n\n/**\n * Hash code that indicates that a CID uses SHA-256.\n */\nexport const SHA256_HASH_CODE = sha256.code\nexport type SHA256_HASH_CODE = typeof SHA256_HASH_CODE\n\n/**\n * Hash code that indicates that a CID uses SHA-512.\n */\nexport const SHA512_HASH_CODE = sha512.code\nexport type SHA512_HASH_CODE = typeof SHA512_HASH_CODE\n\n/**\n * Represent the hash part of a CID, which includes the hash algorithm code and\n * the raw digest bytes.\n *\n * @see {@link https://dasl.ing/cid.html Content IDs (DASL)}\n */\nexport interface Multihash<THashCode extends number = number> {\n /**\n * Code of the hash algorithm (e.g., SHA256_HASH_CODE).\n */\n code: THashCode\n\n /**\n * Raw digest bytes.\n */\n digest: Uint8Array\n}\n\n/**\n * Compares two {@link Multihash} for equality.\n *\n * @param a - First {@link Multihash}\n * @param b - Second {@link Multihash}\n * @returns `true` if both multihashes have the same code and digest\n */\nexport function multihashEquals(a: Multihash, b: Multihash): boolean {\n if (a === b) return true\n return a.code === b.code && ui8Equals(a.digest, b.digest)\n}\n\ndeclare module 'multiformats/cid' {\n /**\n * @deprecated use the {@link Cid} interface from `@atproto/lex-data`, and\n * related helpers ({@link isCid}, {@link ifCid}, {@link asCid},\n * {@link parseCid}, {@link decodeCid}), instead.\n *\n * This is marked as deprecated because we want to discourage direct usage of\n * `multiformats/cid` in dependent packages, and instead have them rely on the\n * {@link Cid} interface from `@atproto/lex-data`. The {@link CID} class from\n * `multiformats` version <10 has compatibility issues with certain TypeScript\n * configuration, which can lead to type errors in dependent packages.\n *\n * We are stuck with version 9 because `@atproto` packages did not drop\n * CommonJS support yet, and multiformats version 10 only supports ES modules.\n *\n * In order to avoid compatibility issues, while preparing for future breaking\n * changes (CID in multiformats v10+ has a slightly different interface), as\n * we update or swap out `multiformats`, `@atproto/lex-data` provides its own\n * stable {@link Cid} interface.\n */\n interface CID {}\n}\n\n// multiformats' CID class is not very portable because:\n//\n// - In dependent packages that use \"moduleResolution\" set to \"node16\",\n// \"nodenext\" or \"bundler\", TypeScript fails to properly resolve the\n// multiformats package when importing CID from @atproto/lex-data. This causes\n// type errors in those packages. This is caused by the fact that the\n// multiformats version <10 (which is the last version that supports CommonJS)\n// uses \"exports\" field in package.json, which do not contain \"types\"\n// entrypoints.\n// https://www.npmjs.com/package/multiformats/v/9.9.0?activeTab=code\n// - By defining our own interface and helper functions, we can have more\n// control over the public API exposed by this package.\n// - It allow us to have a stable interface in case we need to swap out, or\n// eventually update multiformats (should we choose to drop CommonJS support)\n// in the future.\n\n// @NOTE Even though it is not portable, we still re-export CID here so that\n// dependent packages where it can be used, have access to it (instead of\n// importing directly from \"multiformats\" or \"multiformats/cid\").\nexport { /** @deprecated */ CID }\n\n/**\n * Converts a {@link Cid} to a multiformats {@link CID} instance.\n *\n * @deprecated Packages depending on `@atproto/lex-data` should use the\n * {@link Cid} interface instead of relying on `multiformats`'s {@link CID}\n * implementation directly. This is to avoid compatibility issues, and in order\n * to allow better portability, compatibility and future updates.\n */\nexport function asMultiformatsCID<\n TVersion extends 0 | 1 = 0 | 1,\n TCodec extends number = number,\n THashCode extends number = number,\n>(input: Cid<TVersion, TCodec, THashCode>) {\n const cid =\n // Already a multiformats CID instance\n CID.asCID(input) ??\n // Create a new multiformats CID instance\n CID.create(\n input.version,\n input.code,\n createDigest(input.multihash.code, input.multihash.digest),\n )\n\n // @NOTE: the \"satisfies\" operator is used here to ensure that the Cid\n // interface is indeed compatible with multiformats' CID implementation, which\n // allows us to safely rely on multiformats' CID implementation where Cid are\n // needed.\n return cid satisfies Cid as CID & Cid<TVersion, TCodec, THashCode>\n}\n\n/**\n * Content Identifier (CID) for addressing content by its hash.\n *\n * CIDs are self-describing content addresses used throughout AT Protocol for\n * linking to data by its cryptographic hash. This interface provides a\n * stable API that is compatible with the `multiformats` library but avoids\n * direct dependency issues.\n *\n * @typeParam TVersion - CID version (0 or 1)\n * @typeParam TCodec - Multicodec content type code\n * @typeParam THashCode - Multihash algorithm code\n *\n * @example\n * ```typescript\n * import type { Cid } from '@atproto/lex-data'\n * import { parseCid, isCid } from '@atproto/lex-data'\n *\n * // Parse a CID from a string\n * const cid: Cid = parseCid('bafyreib...')\n *\n * // Check if a value is a CID\n * if (isCid(value)) {\n * console.log(cid.toString())\n * }\n * ```\n *\n * @see {@link isCid} to check if a value is a valid CID\n * @see {@link parseCid} to parse a CID from a string\n * @see {@link decodeCid} to decode a CID from bytes\n * @see {@link https://dasl.ing/cid.html Content IDs (DASL)}\n */\nexport interface Cid<\n TVersion extends 0 | 1 = 0 | 1,\n TCodec extends number = number,\n THashCode extends number = number,\n> {\n // @NOTE This interface is compatible with multiformats' CID implementation\n // which we are using under the hood.\n\n /** CID version (0 or 1). AT Protocol uses CIDv1. */\n readonly version: TVersion\n /** Coded (e.g., {@link CBOR_DATA_CODEC}, {@link RAW_DATA_CODEC}). */\n readonly code: TCodec\n /** The multihash containing the hash algorithm and digest. */\n readonly multihash: Multihash<THashCode>\n\n /**\n * Binary representation of the whole CID.\n */\n readonly bytes: Uint8Array\n\n /**\n * Compares this CID with another for equality.\n *\n * @param other - The CID to compare with\n * @returns `true` if the CIDs are equal\n */\n equals(other: Cid): boolean\n\n /**\n * Returns the string representation of this CID (base32 for v1, base58btc for v0).\n */\n toString(): string\n}\n\n/**\n * Represents the CID of raw binary data (like media blobs).\n *\n * The use of {@link SHA256_HASH_CODE} is recommended but not required for raw CIDs.\n *\n * @see {@link https://atproto.com/specs/data-model#link-and-cid-formats AT Protocol Data Model - Link and CID Formats}\n */\nexport type RawCid = Cid<1, RAW_DATA_CODEC>\n\n/**\n * Type guard to check if a CID is a raw binary CID.\n *\n * @param cid - The CID to check\n * @returns `true` if the CID is a version 1 CID with raw multicodec\n */\nexport function isRawCid(cid: Cid): cid is RawCid {\n return cid.version === 1 && cid.code === RAW_DATA_CODEC\n}\n\n/**\n * Represents a DASL compliant CID.\n *\n * DASL CIDs are version 1 CIDs using either raw or DAG-CBOR multicodec\n * with SHA-256 multihash.\n *\n * @see {@link https://dasl.ing/cid.html Content IDs (DASL)}\n */\nexport type DaslCid = Cid<1, RAW_DATA_CODEC | CBOR_DATA_CODEC, SHA256_HASH_CODE>\n\n/**\n * Type guard to check if a CID is DASL compliant.\n *\n * @param cid - The CID to check\n * @returns `true` if the CID is DASL compliant (v1, raw/dag-cbor, sha256)\n */\nexport function isDaslCid(cid: Cid): cid is DaslCid {\n return (\n cid.version === 1 &&\n (cid.code === RAW_DATA_CODEC || cid.code === CBOR_DATA_CODEC) &&\n cid.multihash.code === SHA256_HASH_CODE &&\n cid.multihash.digest.byteLength === 0x20 // Should always be 32 bytes (256 bits) for SHA-256, but double-checking anyways\n )\n}\n\n/**\n * Represents the CID of AT Protocol DAG-CBOR data (like repository MST nodes).\n *\n * CBOR CIDs are version 1 CIDs using DAG-CBOR multicodec with SHA-256 multihash.\n *\n * @see {@link https://atproto.com/specs/data-model#link-and-cid-formats AT Protocol Data Model - Link and CID Formats}\n */\nexport type CborCid = Cid<1, CBOR_DATA_CODEC, SHA256_HASH_CODE>\n\n/**\n * Type guard to check if a CID is a DAG-CBOR CID.\n *\n * @param cid - The CID to check\n * @returns `true` if the CID is a DAG-CBOR CID (v1, dag-cbor, sha256)\n */\nexport function isCborCid(cid: Cid): cid is CborCid {\n return cid.code === CBOR_DATA_CODEC && isDaslCid(cid)\n}\n\n/**\n * Options for checking CID flavor constraints.\n */\nexport type CheckCidOptions = {\n /**\n * The CID flavor to check for.\n * - `'raw'` - Raw binary CID ({@link RawCid})\n * - `'cbor'` - DAG-CBOR CID ({@link CborCid})\n * - `'dasl'` - DASL compliant CID ({@link DaslCid})\n */\n flavor?: 'raw' | 'cbor' | 'dasl'\n}\n\n/**\n * Infers the CID type based on check options.\n *\n * @typeParam TOptions - The options used for checking\n */\nexport type InferCheckedCid<TOptions extends CheckCidOptions> =\n TOptions extends { flavor: 'raw' }\n ? RawCid\n : TOptions extends { flavor: 'cbor' }\n ? CborCid\n : Cid\n\n/**\n * Type guard to check whether a {@link Cid} instance meets specific flavor\n * constraints.\n */\nexport function checkCid<TOptions extends CheckCidOptions>(\n cid: Cid,\n options: TOptions,\n): cid is InferCheckedCid<TOptions>\nexport function checkCid(cid: Cid, options?: CheckCidOptions): boolean\nexport function checkCid(cid: Cid, options?: CheckCidOptions): boolean {\n switch (options?.flavor) {\n case undefined:\n return true\n case 'cbor':\n return isCborCid(cid)\n case 'dasl':\n return isDaslCid(cid)\n case 'raw':\n return isRawCid(cid)\n default:\n throw new TypeError(`Unknown CID flavor: ${options?.flavor}`)\n }\n}\n\n/**\n * Type guard to check whether a value is a valid {@link Cid} instance,\n * optionally checking for specific flavor constraints.\n */\nexport function isCid<TOptions extends CheckCidOptions>(\n value: unknown,\n options: TOptions,\n): value is InferCheckedCid<TOptions>\nexport function isCid(value: unknown, options?: CheckCidOptions): value is Cid\nexport function isCid(value: unknown, options?: CheckCidOptions): value is Cid {\n return isCidImplementation(value) && checkCid(value, options)\n}\n\n/**\n * Returns the input value as a {@link Cid} if it is valid, or `null` otherwise.\n */\nexport function ifCid<TValue, TOptions extends CheckCidOptions>(\n value: unknown,\n options: TOptions,\n): (TValue & InferCheckedCid<TOptions>) | null\nexport function ifCid<TValue>(\n value: TValue,\n options?: CheckCidOptions,\n): (TValue & Cid) | null\nexport function ifCid(value: unknown, options?: CheckCidOptions): Cid | null {\n if (isCid(value, options)) return value\n return null\n}\n\n/**\n * Returns the input value as a {@link Cid} if it is valid.\n *\n * @throws if the input is not a valid {@link Cid}.\n */\nexport function asCid<TValue, TOptions extends CheckCidOptions>(\n value: TValue,\n options: TOptions,\n): TValue & InferCheckedCid<TOptions>\nexport function asCid<TValue>(\n value: TValue,\n options?: CheckCidOptions,\n): TValue & Cid\nexport function asCid(value: unknown, options?: CheckCidOptions): Cid {\n if (isCid(value, options)) return value\n throw new Error(\n `Invalid ${options?.flavor ? `${options.flavor} CID` : 'CID'} \"${value}\"`,\n )\n}\n\n/**\n * Decodes a CID from its binary representation.\n *\n * @see {@link https://dasl.ing/cid.html DASL-CIDs}\n * @throws if the input do not represent a valid DASL {@link Cid}\n */\nexport function decodeCid<TOptions extends CheckCidOptions>(\n cidBytes: Uint8Array,\n options: TOptions,\n): InferCheckedCid<TOptions>\nexport function decodeCid(cidBytes: Uint8Array, options?: CheckCidOptions): Cid\nexport function decodeCid(\n cidBytes: Uint8Array,\n options?: CheckCidOptions,\n): Cid {\n const cid = CID.decode(cidBytes)\n return asCid(cid, options)\n}\n\n/**\n * Parses a CID string into a Cid object.\n *\n * @throws if the input is not a valid CID string.\n */\nexport function parseCid<TOptions extends CheckCidOptions>(\n input: string,\n options: TOptions,\n): InferCheckedCid<TOptions>\nexport function parseCid(input: string, options?: CheckCidOptions): Cid\nexport function parseCid(input: string, options?: CheckCidOptions): Cid {\n const cid = CID.parse(input)\n return asCid(cid, options)\n}\n\n/**\n * Validates that a string is a valid CID representation.\n *\n * Unlike {@link parseCid}, this function returns a boolean instead of throwing.\n * It also verifies that the string is the canonical representation of the CID.\n *\n * @param input - The string to validate\n * @param options - Optional flavor constraints\n * @returns `true` if the string is a valid CID\n */\nexport function validateCidString(\n input: string,\n options?: CheckCidOptions,\n): boolean {\n return parseCidSafe(input, options)?.toString() === input\n}\n\n/**\n * Safely parses a CID string, returning `null` on failure instead of throwing.\n *\n * @param input - The string to parse\n * @param options - Optional flavor constraints\n * @returns The parsed CID, or `null` if parsing fails\n *\n * @example\n * ```typescript\n * import { parseCidSafe } from '@atproto/lex-data'\n *\n * const cid = parseCidSafe('bafyreib...')\n * if (cid) {\n * console.log(cid.toString())\n * }\n * ```\n */\nexport function parseCidSafe<TOptions extends CheckCidOptions>(\n input: string,\n options: TOptions,\n): InferCheckedCid<TOptions> | null\nexport function parseCidSafe(\n input: string,\n options?: CheckCidOptions,\n): Cid | null\nexport function parseCidSafe(\n input: string,\n options?: CheckCidOptions,\n): Cid | null {\n try {\n return parseCid(input, options)\n } catch {\n return null\n }\n}\n\n/**\n * Ensures that a string is a valid CID representation.\n *\n * @param input - The string to validate\n * @param options - Optional flavor constraints\n * @throws If the string is not a valid CID\n */\nexport function ensureValidCidString(\n input: string,\n options?: CheckCidOptions,\n): void {\n if (!validateCidString(input, options)) {\n throw new Error(`Invalid CID string \"${input}\"`)\n }\n}\n\n/**\n * Verifies whether the multihash of a given {@link cid} matches the hash of the provided {@link bytes}.\n * @params cid The CID to match against the bytes.\n * @params bytes The bytes to verify.\n * @returns true if the CID matches the bytes, false otherwise.\n */\nexport async function isCidForBytes(\n cid: Cid,\n bytes: Uint8Array,\n): Promise<boolean> {\n if (cid.multihash.code === sha256.code) {\n const multihash = await sha256.digest(bytes)\n return multihashEquals(multihash, cid.multihash)\n }\n\n if (cid.multihash.code === sha512.code) {\n const multihash = await sha512.digest(bytes)\n return multihashEquals(multihash, cid.multihash)\n }\n\n // Don't know how to verify other multihash codes\n throw new Error(\n `Unsupported CID multihash code: ${toHexString(cid.multihash.code)}`,\n )\n}\n\n/**\n * Creates a CID from a multicodec, multihash code, and digest.\n *\n * @param code - The multicodec content type code\n * @param multihashCode - The multihash algorithm code\n * @param digest - The raw hash digest bytes\n * @returns A new CIDv1 instance\n *\n * @example\n * ```typescript\n * import { createCid, RAW_DATA_CODEC, SHA256_HASH_CODE } from '@atproto/lex-data'\n *\n * const cid = createCid(RAW_DATA_CODEC, SHA256_HASH_CODE, hashDigest)\n * ```\n */\nexport function createCid<TCodec extends number, THashCode extends number>(\n code: TCodec,\n multihashCode: THashCode,\n digest: Uint8Array,\n) {\n const cid: Cid = CID.createV1(code, createDigest(multihashCode, digest))\n return cid as Cid<1, TCodec, THashCode>\n}\n\n/**\n * Creates a DAG-CBOR CID for the given CBOR bytes.\n *\n * Computes the SHA-256 hash of the bytes and creates a CIDv1 with DAG-CBOR multicodec.\n *\n * @param bytes - The CBOR-encoded bytes to hash\n * @returns A promise that resolves to the CborCid\n */\nexport async function cidForCbor(bytes: Uint8Array): Promise<CborCid> {\n const multihash = await sha256.digest(bytes)\n return CID.createV1(CBOR_DATA_CODEC, multihash) as CborCid\n}\n\n/**\n * Creates a raw CID for the given binary bytes.\n *\n * Computes the SHA-256 hash of the bytes and creates a CIDv1 with raw multicodec.\n *\n * @param bytes - The raw binary bytes to hash\n * @returns A promise that resolves to the RawCid\n */\nexport async function cidForRawBytes(bytes: Uint8Array): Promise<RawCid> {\n const multihash = await sha256.digest(bytes)\n return CID.createV1(RAW_DATA_CODEC, multihash) as RawCid\n}\n\n/**\n * Creates a raw CID from an existing SHA-256 hash digest.\n *\n * @param digest - The SHA-256 hash digest (must be 32 bytes)\n * @returns A RawCid with the given digest\n * @throws If the digest is not a valid SHA-256 hash (not 32 bytes)\n */\nexport function cidForRawHash(digest: Uint8Array): RawCid {\n // Fool-proofing\n if (digest.length !== 0x20) {\n throw new Error(\n `Invalid SHA-256 hash length: ${toHexString(digest.length)}`,\n )\n }\n return createCid(RAW_DATA_CODEC, sha256.code, digest)\n}\n\nfunction isCidImplementation(value: unknown): value is Cid {\n if (CID.asCID(value)) {\n // CIDs created using older multiformats versions did not have a \"bytes\"\n // property.\n return (value as { bytes?: Uint8Array }).bytes != null\n } else {\n // Unknown implementation, do a structural check\n try {\n if (!isObject(value)) return false\n\n const val = value as Record<string, unknown>\n if (val.version !== 0 && val.version !== 1) return false\n if (!isUint8(val.code)) return false\n\n if (!isObject(val.multihash)) return false\n const mh = val.multihash as Record<string, unknown>\n if (!isUint8(mh.code)) return false\n if (!(mh.digest instanceof Uint8Array)) return false\n\n // Ensure that the bytes array is consistent with other properties\n if (!(val.bytes instanceof Uint8Array)) return false\n if (val.bytes[0] !== val.version) return false\n if (val.bytes[1] !== val.code) return false\n if (val.bytes[2] !== mh.code) return false\n if (val.bytes[3] !== mh.digest.length) return false\n if (val.bytes.length !== 4 + mh.digest.length) return false\n if (!ui8Equals(val.bytes.subarray(4), mh.digest)) return false\n\n if (typeof val.equals !== 'function') return false\n if (val.equals(val) !== true) return false\n\n return true\n } catch {\n return false\n }\n }\n}\n"]}
1
+ {"version":3,"file":"cid.js","sourceRoot":"","sources":["../src/cid.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,MAAM,IAAI,YAAY,EAAE,MAAM,4BAA4B,CAAA;AACnE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AACzD,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACt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d,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["import { CID } from 'multiformats/cid'\nimport { create as createDigest } from 'multiformats/hashes/digest'\nimport { sha256, sha512 } from 'multiformats/hashes/sha2'\nimport { isUint8, toHexString } from './lib/util.js'\nimport { isObject } from './object.js'\nimport { ui8Equals } from './uint8array.js'\n\n/**\n * Codec code that indicates the CID references a CBOR-encoded data structure.\n *\n * Used when encoding structured data in AT Protocol repositories.\n *\n * @see {@link https://dasl.ing/cid.html Content IDs (DASL)}\n */\nexport const CBOR_DATA_CODEC = 0x71\nexport type CBOR_DATA_CODEC = typeof CBOR_DATA_CODEC\n\n/**\n * Codec code that indicates the CID references raw binary data (like media blobs).\n *\n * Used in DASL CIDs for binary blobs like images and media.\n *\n * @see {@link https://dasl.ing/cid.html Content IDs (DASL)}\n */\nexport const RAW_DATA_CODEC = 0x55\nexport type RAW_DATA_CODEC = typeof RAW_DATA_CODEC\n\n/**\n * Hash code that indicates that a CID uses SHA-256.\n */\nexport const SHA256_HASH_CODE = sha256.code\nexport type SHA256_HASH_CODE = typeof SHA256_HASH_CODE\n\n/**\n * Hash code that indicates that a CID uses SHA-512.\n */\nexport const SHA512_HASH_CODE = sha512.code\nexport type SHA512_HASH_CODE = typeof SHA512_HASH_CODE\n\n/**\n * Represent the hash part of a CID, which includes the hash algorithm code and\n * the raw digest bytes.\n *\n * @see {@link https://dasl.ing/cid.html Content IDs (DASL)}\n */\nexport interface Multihash<THashCode extends number = number> {\n /**\n * Code of the hash algorithm (e.g., SHA256_HASH_CODE).\n */\n code: THashCode\n\n /**\n * Raw digest bytes.\n */\n digest: Uint8Array\n}\n\n/**\n * Compares two {@link Multihash} for equality.\n *\n * @param a - First {@link Multihash}\n * @param b - Second {@link Multihash}\n * @returns `true` if both multihashes have the same code and digest\n */\nexport function multihashEquals(a: Multihash, b: Multihash): boolean {\n if (a === b) return true\n return a.code === b.code && ui8Equals(a.digest, b.digest)\n}\n\ndeclare module 'multiformats/cid' {\n /**\n * @deprecated use the {@link Cid} interface from `@atproto/lex-data`, and\n * related helpers ({@link isCid}, {@link ifCid}, {@link asCid},\n * {@link parseCid}, {@link decodeCid}), instead.\n *\n * This is marked as deprecated because we want to discourage direct usage of\n * `multiformats/cid` in dependent packages, and instead have them rely on the\n * {@link Cid} interface from `@atproto/lex-data`. The {@link CID} class from\n * `multiformats` version <10 has compatibility issues with certain TypeScript\n * configuration, which can lead to type errors in dependent packages.\n *\n * We are stuck with version 9 because `@atproto` packages did not drop\n * CommonJS support yet, and multiformats version 10 only supports ES modules.\n *\n * In order to avoid compatibility issues, while preparing for future breaking\n * changes (CID in multiformats v10+ has a slightly different interface), as\n * we update or swap out `multiformats`, `@atproto/lex-data` provides its own\n * stable {@link Cid} interface.\n */\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n interface CID {}\n}\n\n// multiformats' CID class is not very portable because:\n//\n// - In dependent packages that use \"moduleResolution\" set to \"node16\",\n// \"nodenext\" or \"bundler\", TypeScript fails to properly resolve the\n// multiformats package when importing CID from @atproto/lex-data. This causes\n// type errors in those packages. This is caused by the fact that the\n// multiformats version <10 (which is the last version that supports CommonJS)\n// uses \"exports\" field in package.json, which do not contain \"types\"\n// entrypoints.\n// https://www.npmjs.com/package/multiformats/v/9.9.0?activeTab=code\n// - By defining our own interface and helper functions, we can have more\n// control over the public API exposed by this package.\n// - It allow us to have a stable interface in case we need to swap out, or\n// eventually update multiformats (should we choose to drop CommonJS support)\n// in the future.\n\n// @NOTE Even though it is not portable, we still re-export CID here so that\n// dependent packages where it can be used, have access to it (instead of\n// importing directly from \"multiformats\" or \"multiformats/cid\").\nexport { /** @deprecated */ CID }\n\n/**\n * Converts a {@link Cid} to a multiformats {@link CID} instance.\n *\n * @deprecated Packages depending on `@atproto/lex-data` should use the\n * {@link Cid} interface instead of relying on `multiformats`'s {@link CID}\n * implementation directly. This is to avoid compatibility issues, and in order\n * to allow better portability, compatibility and future updates.\n */\nexport function asMultiformatsCID<\n TVersion extends 0 | 1 = 0 | 1,\n TCodec extends number = number,\n THashCode extends number = number,\n>(input: Cid<TVersion, TCodec, THashCode>) {\n const cid =\n // Already a multiformats CID instance\n CID.asCID(input) ??\n // Create a new multiformats CID instance\n CID.create(\n input.version,\n input.code,\n createDigest(input.multihash.code, input.multihash.digest),\n )\n\n // @NOTE: the \"satisfies\" operator is used here to ensure that the Cid\n // interface is indeed compatible with multiformats' CID implementation, which\n // allows us to safely rely on multiformats' CID implementation where Cid are\n // needed.\n return cid satisfies Cid as CID & Cid<TVersion, TCodec, THashCode>\n}\n\n/**\n * Content Identifier (CID) for addressing content by its hash.\n *\n * CIDs are self-describing content addresses used throughout AT Protocol for\n * linking to data by its cryptographic hash. This interface provides a\n * stable API that is compatible with the `multiformats` library but avoids\n * direct dependency issues.\n *\n * @typeParam TVersion - CID version (0 or 1)\n * @typeParam TCodec - Multicodec content type code\n * @typeParam THashCode - Multihash algorithm code\n *\n * @example\n * ```typescript\n * import type { Cid } from '@atproto/lex-data'\n * import { parseCid, isCid } from '@atproto/lex-data'\n *\n * // Parse a CID from a string\n * const cid: Cid = parseCid('bafyreib...')\n *\n * // Check if a value is a CID\n * if (isCid(value)) {\n * console.log(cid.toString())\n * }\n * ```\n *\n * @see {@link isCid} to check if a value is a valid CID\n * @see {@link parseCid} to parse a CID from a string\n * @see {@link decodeCid} to decode a CID from bytes\n * @see {@link https://dasl.ing/cid.html Content IDs (DASL)}\n */\nexport interface Cid<\n TVersion extends 0 | 1 = 0 | 1,\n TCodec extends number = number,\n THashCode extends number = number,\n> {\n // @NOTE This interface is compatible with multiformats' CID implementation\n // which we are using under the hood.\n\n /** CID version (0 or 1). AT Protocol uses CIDv1. */\n readonly version: TVersion\n /** Coded (e.g., {@link CBOR_DATA_CODEC}, {@link RAW_DATA_CODEC}). */\n readonly code: TCodec\n /** The multihash containing the hash algorithm and digest. */\n readonly multihash: Multihash<THashCode>\n\n /**\n * Binary representation of the whole CID.\n */\n readonly bytes: Uint8Array\n\n /**\n * Compares this CID with another for equality.\n *\n * @param other - The CID to compare with\n * @returns `true` if the CIDs are equal\n */\n equals(other: Cid): boolean\n\n /**\n * Returns the string representation of this CID (base32 for v1, base58btc for v0).\n */\n toString(): string\n}\n\n/**\n * Represents the CID of raw binary data (like media blobs).\n *\n * The use of {@link SHA256_HASH_CODE} is recommended but not required for raw CIDs.\n *\n * @see {@link https://atproto.com/specs/data-model#link-and-cid-formats AT Protocol Data Model - Link and CID Formats}\n */\nexport type RawCid = Cid<1, RAW_DATA_CODEC>\n\n/**\n * Type guard to check if a CID is a raw binary CID.\n *\n * @param cid - The CID to check\n * @returns `true` if the CID is a version 1 CID with raw multicodec\n */\nexport function isRawCid(cid: Cid): cid is RawCid {\n return cid.version === 1 && cid.code === RAW_DATA_CODEC\n}\n\n/**\n * Represents a DASL compliant CID.\n *\n * DASL CIDs are version 1 CIDs using either raw or DAG-CBOR multicodec\n * with SHA-256 multihash.\n *\n * @see {@link https://dasl.ing/cid.html Content IDs (DASL)}\n */\nexport type DaslCid = Cid<1, RAW_DATA_CODEC | CBOR_DATA_CODEC, SHA256_HASH_CODE>\n\n/**\n * Type guard to check if a CID is DASL compliant.\n *\n * @param cid - The CID to check\n * @returns `true` if the CID is DASL compliant (v1, raw/dag-cbor, sha256)\n */\nexport function isDaslCid(cid: Cid): cid is DaslCid {\n return (\n cid.version === 1 &&\n (cid.code === RAW_DATA_CODEC || cid.code === CBOR_DATA_CODEC) &&\n cid.multihash.code === SHA256_HASH_CODE &&\n cid.multihash.digest.byteLength === 0x20 // Should always be 32 bytes (256 bits) for SHA-256, but double-checking anyways\n )\n}\n\n/**\n * Represents the CID of AT Protocol DAG-CBOR data (like repository MST nodes).\n *\n * CBOR CIDs are version 1 CIDs using DAG-CBOR multicodec with SHA-256 multihash.\n *\n * @see {@link https://atproto.com/specs/data-model#link-and-cid-formats AT Protocol Data Model - Link and CID Formats}\n */\nexport type CborCid = Cid<1, CBOR_DATA_CODEC, SHA256_HASH_CODE>\n\n/**\n * Type guard to check if a CID is a DAG-CBOR CID.\n *\n * @param cid - The CID to check\n * @returns `true` if the CID is a DAG-CBOR CID (v1, dag-cbor, sha256)\n */\nexport function isCborCid(cid: Cid): cid is CborCid {\n return cid.code === CBOR_DATA_CODEC && isDaslCid(cid)\n}\n\n/**\n * Options for checking CID flavor constraints.\n */\nexport type CheckCidOptions = {\n /**\n * The CID flavor to check for.\n * - `'raw'` - Raw binary CID ({@link RawCid})\n * - `'cbor'` - DAG-CBOR CID ({@link CborCid})\n * - `'dasl'` - DASL compliant CID ({@link DaslCid})\n */\n flavor?: 'raw' | 'cbor' | 'dasl'\n}\n\n/**\n * Infers the CID type based on check options.\n *\n * @typeParam TOptions - The options used for checking\n */\nexport type InferCheckedCid<TOptions extends CheckCidOptions> =\n TOptions extends { flavor: 'raw' }\n ? RawCid\n : TOptions extends { flavor: 'cbor' }\n ? CborCid\n : Cid\n\n/**\n * Type guard to check whether a {@link Cid} instance meets specific flavor\n * constraints.\n */\nexport function checkCid<TOptions extends CheckCidOptions>(\n cid: Cid,\n options: TOptions,\n): cid is InferCheckedCid<TOptions>\nexport function checkCid(cid: Cid, options?: CheckCidOptions): boolean\nexport function checkCid(cid: Cid, options?: CheckCidOptions): boolean {\n switch (options?.flavor) {\n case undefined:\n return true\n case 'cbor':\n return isCborCid(cid)\n case 'dasl':\n return isDaslCid(cid)\n case 'raw':\n return isRawCid(cid)\n default:\n throw new TypeError(`Unknown CID flavor: ${options?.flavor}`)\n }\n}\n\n/**\n * Type guard to check whether a value is a valid {@link Cid} instance,\n * optionally checking for specific flavor constraints.\n */\nexport function isCid<TOptions extends CheckCidOptions>(\n value: unknown,\n options: TOptions,\n): value is InferCheckedCid<TOptions>\nexport function isCid(value: unknown, options?: CheckCidOptions): value is Cid\nexport function isCid(value: unknown, options?: CheckCidOptions): value is Cid {\n return isCidImplementation(value) && checkCid(value, options)\n}\n\n/**\n * Returns the input value as a {@link Cid} if it is valid, or `null` otherwise.\n */\nexport function ifCid<TValue, TOptions extends CheckCidOptions>(\n value: unknown,\n options: TOptions,\n): (TValue & InferCheckedCid<TOptions>) | null\nexport function ifCid<TValue>(\n value: TValue,\n options?: CheckCidOptions,\n): (TValue & Cid) | null\nexport function ifCid(value: unknown, options?: CheckCidOptions): Cid | null {\n if (isCid(value, options)) return value\n return null\n}\n\n/**\n * Returns the input value as a {@link Cid} if it is valid.\n *\n * @throws if the input is not a valid {@link Cid}.\n */\nexport function asCid<TValue, TOptions extends CheckCidOptions>(\n value: TValue,\n options: TOptions,\n): TValue & InferCheckedCid<TOptions>\nexport function asCid<TValue>(\n value: TValue,\n options?: CheckCidOptions,\n): TValue & Cid\nexport function asCid(value: unknown, options?: CheckCidOptions): Cid {\n if (isCid(value, options)) return value\n throw new Error(\n `Invalid ${options?.flavor ? `${options.flavor} CID` : 'CID'} \"${value}\"`,\n )\n}\n\n/**\n * Decodes a CID from its binary representation.\n *\n * @see {@link https://dasl.ing/cid.html DASL-CIDs}\n * @throws if the input do not represent a valid DASL {@link Cid}\n */\nexport function decodeCid<TOptions extends CheckCidOptions>(\n cidBytes: Uint8Array,\n options: TOptions,\n): InferCheckedCid<TOptions>\nexport function decodeCid(cidBytes: Uint8Array, options?: CheckCidOptions): Cid\nexport function decodeCid(\n cidBytes: Uint8Array,\n options?: CheckCidOptions,\n): Cid {\n const cid = CID.decode(cidBytes)\n return asCid(cid, options)\n}\n\n/**\n * Parses a CID string into a Cid object.\n *\n * @throws if the input is not a valid CID string.\n */\nexport function parseCid<TOptions extends CheckCidOptions>(\n input: string,\n options: TOptions,\n): InferCheckedCid<TOptions>\nexport function parseCid(input: string, options?: CheckCidOptions): Cid\nexport function parseCid(input: string, options?: CheckCidOptions): Cid {\n const cid = CID.parse(input)\n return asCid(cid, options)\n}\n\n/**\n * Validates that a string is a valid CID representation.\n *\n * Unlike {@link parseCid}, this function returns a boolean instead of throwing.\n * It also verifies that the string is the canonical representation of the CID.\n *\n * @param input - The string to validate\n * @param options - Optional flavor constraints\n * @returns `true` if the string is a valid CID\n */\nexport function validateCidString(\n input: string,\n options?: CheckCidOptions,\n): boolean {\n return parseCidSafe(input, options)?.toString() === input\n}\n\n/**\n * Safely parses a CID string, returning `null` on failure instead of throwing.\n *\n * @param input - The string to parse\n * @param options - Optional flavor constraints\n * @returns The parsed CID, or `null` if parsing fails\n *\n * @example\n * ```typescript\n * import { parseCidSafe } from '@atproto/lex-data'\n *\n * const cid = parseCidSafe('bafyreib...')\n * if (cid) {\n * console.log(cid.toString())\n * }\n * ```\n */\nexport function parseCidSafe<TOptions extends CheckCidOptions>(\n input: string,\n options: TOptions,\n): InferCheckedCid<TOptions> | null\nexport function parseCidSafe(\n input: string,\n options?: CheckCidOptions,\n): Cid | null\nexport function parseCidSafe(\n input: string,\n options?: CheckCidOptions,\n): Cid | null {\n try {\n return parseCid(input, options)\n } catch {\n return null\n }\n}\n\n/**\n * Ensures that a string is a valid CID representation.\n *\n * @param input - The string to validate\n * @param options - Optional flavor constraints\n * @throws If the string is not a valid CID\n */\nexport function ensureValidCidString(\n input: string,\n options?: CheckCidOptions,\n): void {\n if (!validateCidString(input, options)) {\n throw new Error(`Invalid CID string \"${input}\"`)\n }\n}\n\n/**\n * Verifies whether the multihash of a given {@link cid} matches the hash of the provided {@link bytes}.\n * @params cid The CID to match against the bytes.\n * @params bytes The bytes to verify.\n * @returns true if the CID matches the bytes, false otherwise.\n */\nexport async function isCidForBytes(\n cid: Cid,\n bytes: Uint8Array,\n): Promise<boolean> {\n if (cid.multihash.code === sha256.code) {\n const multihash = await sha256.digest(bytes)\n return multihashEquals(multihash, cid.multihash)\n }\n\n if (cid.multihash.code === sha512.code) {\n const multihash = await sha512.digest(bytes)\n return multihashEquals(multihash, cid.multihash)\n }\n\n // Don't know how to verify other multihash codes\n throw new Error(\n `Unsupported CID multihash code: ${toHexString(cid.multihash.code)}`,\n )\n}\n\n/**\n * Creates a CID from a multicodec, multihash code, and digest.\n *\n * @param code - The multicodec content type code\n * @param multihashCode - The multihash algorithm code\n * @param digest - The raw hash digest bytes\n * @returns A new CIDv1 instance\n *\n * @example\n * ```typescript\n * import { createCid, RAW_DATA_CODEC, SHA256_HASH_CODE } from '@atproto/lex-data'\n *\n * const cid = createCid(RAW_DATA_CODEC, SHA256_HASH_CODE, hashDigest)\n * ```\n */\nexport function createCid<TCodec extends number, THashCode extends number>(\n code: TCodec,\n multihashCode: THashCode,\n digest: Uint8Array,\n) {\n const cid: Cid = CID.createV1(code, createDigest(multihashCode, digest))\n return cid as Cid<1, TCodec, THashCode>\n}\n\n/**\n * Creates a DAG-CBOR CID for the given CBOR bytes.\n *\n * Computes the SHA-256 hash of the bytes and creates a CIDv1 with DAG-CBOR multicodec.\n *\n * @param bytes - The CBOR-encoded bytes to hash\n * @returns A promise that resolves to the CborCid\n */\nexport async function cidForCbor(bytes: Uint8Array): Promise<CborCid> {\n const multihash = await sha256.digest(bytes)\n return CID.createV1(CBOR_DATA_CODEC, multihash) as CborCid\n}\n\n/**\n * Creates a raw CID for the given binary bytes.\n *\n * Computes the SHA-256 hash of the bytes and creates a CIDv1 with raw multicodec.\n *\n * @param bytes - The raw binary bytes to hash\n * @returns A promise that resolves to the RawCid\n */\nexport async function cidForRawBytes(bytes: Uint8Array): Promise<RawCid> {\n const multihash = await sha256.digest(bytes)\n return CID.createV1(RAW_DATA_CODEC, multihash) as RawCid\n}\n\n/**\n * Creates a raw CID from an existing SHA-256 hash digest.\n *\n * @param digest - The SHA-256 hash digest (must be 32 bytes)\n * @returns A RawCid with the given digest\n * @throws If the digest is not a valid SHA-256 hash (not 32 bytes)\n */\nexport function cidForRawHash(digest: Uint8Array): RawCid {\n // Fool-proofing\n if (digest.length !== 0x20) {\n throw new Error(\n `Invalid SHA-256 hash length: ${toHexString(digest.length)}`,\n )\n }\n return createCid(RAW_DATA_CODEC, sha256.code, digest)\n}\n\nfunction isCidImplementation(value: unknown): value is Cid {\n if (CID.asCID(value)) {\n // CIDs created using older multiformats versions did not have a \"bytes\"\n // property.\n return (value as { bytes?: Uint8Array }).bytes != null\n } else {\n // Unknown implementation, do a structural check\n try {\n if (!isObject(value)) return false\n\n const val = value as Record<string, unknown>\n if (val.version !== 0 && val.version !== 1) return false\n if (!isUint8(val.code)) return false\n\n if (!isObject(val.multihash)) return false\n const mh = val.multihash as Record<string, unknown>\n if (!isUint8(mh.code)) return false\n if (!(mh.digest instanceof Uint8Array)) return false\n\n // Ensure that the bytes array is consistent with other properties\n if (!(val.bytes instanceof Uint8Array)) return false\n if (val.bytes[0] !== val.version) return false\n if (val.bytes[1] !== val.code) return false\n if (val.bytes[2] !== mh.code) return false\n if (val.bytes[3] !== mh.digest.length) return false\n if (val.bytes.length !== 4 + mh.digest.length) return false\n if (!ui8Equals(val.bytes.subarray(4), mh.digest)) return false\n\n if (typeof val.equals !== 'function') return false\n if (val.equals(val) !== true) return false\n\n return true\n } catch {\n return false\n }\n }\n}\n"]}
package/dist/index.js CHANGED
@@ -1,12 +1,9 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./blob.js"), exports);
5
- tslib_1.__exportStar(require("./cid.js"), exports);
6
- tslib_1.__exportStar(require("./lex-equals.js"), exports);
7
- tslib_1.__exportStar(require("./lex-error.js"), exports);
8
- tslib_1.__exportStar(require("./lex.js"), exports);
9
- tslib_1.__exportStar(require("./object.js"), exports);
10
- tslib_1.__exportStar(require("./uint8array.js"), exports);
11
- tslib_1.__exportStar(require("./utf8.js"), exports);
1
+ export * from './blob.js';
2
+ export * from './cid.js';
3
+ export * from './lex-equals.js';
4
+ export * from './lex-error.js';
5
+ export * from './lex.js';
6
+ export * from './object.js';
7
+ export * from './uint8array.js';
8
+ export * from './utf8.js';
12
9
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,oDAAyB;AACzB,mDAAwB;AACxB,0DAA+B;AAC/B,yDAA8B;AAC9B,mDAAwB;AACxB,sDAA2B;AAC3B,0DAA+B;AAC/B,oDAAyB","sourcesContent":["export * from './blob.js'\nexport * from './cid.js'\nexport * from './lex-equals.js'\nexport * from './lex-error.js'\nexport * from './lex.js'\nexport * from './object.js'\nexport * from './uint8array.js'\nexport * from './utf8.js'\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,UAAU,CAAA;AACxB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,aAAa,CAAA;AAC3B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,WAAW,CAAA","sourcesContent":["export * from './blob.js'\nexport * from './cid.js'\nexport * from './lex-equals.js'\nexport * from './lex-error.js'\nexport * from './lex.js'\nexport * from './object.js'\nexport * from './uint8array.js'\nexport * from './utf8.js'\n"]}
@@ -1,9 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.lexEquals = lexEquals;
4
- const cid_js_1 = require("./cid.js");
5
- const object_js_1 = require("./object.js");
6
- const uint8array_js_1 = require("./uint8array.js");
1
+ import { ifCid, isCid } from './cid.js';
2
+ import { isPlainObject } from './object.js';
3
+ import { ui8Equals } from './uint8array.js';
7
4
  /**
8
5
  * Performs deep equality comparison between two {@link LexValue}s.
9
6
  *
@@ -42,7 +39,7 @@ const uint8array_js_1 = require("./uint8array.js");
42
39
  * lexEquals(new Uint8Array([1, 2]), new Uint8Array([1, 2])) // true
43
40
  * ```
44
41
  */
45
- function lexEquals(a, b) {
42
+ export function lexEquals(a, b) {
46
43
  if (Object.is(a, b)) {
47
44
  return true;
48
45
  }
@@ -72,20 +69,20 @@ function lexEquals(a, b) {
72
69
  if (ArrayBuffer.isView(a)) {
73
70
  if (!ArrayBuffer.isView(b))
74
71
  return false;
75
- return (0, uint8array_js_1.ui8Equals)(a, b);
72
+ return ui8Equals(a, b);
76
73
  }
77
74
  else if (ArrayBuffer.isView(b)) {
78
75
  return false;
79
76
  }
80
- if ((0, cid_js_1.isCid)(a)) {
77
+ if (isCid(a)) {
81
78
  // @NOTE CID.equals returns its argument when it is falsy (e.g. null or
82
79
  // undefined) so we need to explicitly check that the output is "true".
83
- return (0, cid_js_1.ifCid)(b)?.equals(a) === true;
80
+ return ifCid(b)?.equals(a) === true;
84
81
  }
85
- else if ((0, cid_js_1.isCid)(b)) {
82
+ else if (isCid(b)) {
86
83
  return false;
87
84
  }
88
- if (!(0, object_js_1.isPlainObject)(a) || !(0, object_js_1.isPlainObject)(b)) {
85
+ if (!isPlainObject(a) || !isPlainObject(b)) {
89
86
  // Foolproof (should never happen)
90
87
  throw new TypeError('Invalid LexValue (expected CID, Uint8Array, or LexMap)');
91
88
  }
@@ -1 +1 @@
1
- {"version":3,"file":"lex-equals.js","sourceRoot":"","sources":["../src/lex-equals.ts"],"names":[],"mappings":";;AA2CA,8BA+EC;AA1HD,qCAAuC;AAEvC,2CAA2C;AAC3C,mDAA2C;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,SAAgB,SAAS,CAAC,CAAW,EAAE,CAAW;IAChD,IAAI,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;QACpB,OAAO,IAAI,CAAA;IACb,CAAC;IAED,IACE,CAAC,IAAI,IAAI;QACT,CAAC,IAAI,IAAI;QACT,OAAO,CAAC,KAAK,QAAQ;QACrB,OAAO,CAAC,KAAK,QAAQ,EACrB,CAAC;QACD,OAAO,KAAK,CAAA;IACd,CAAC;IAED,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;QACrB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;YACtB,OAAO,KAAK,CAAA;QACd,CAAC;QACD,IAAI,CAAC,CAAC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AAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IAC5B,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IAE5B,IAAI,KAAK,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM,EAAE,CAAC;QAClC,OAAO,KAAK,CAAA;IACd,CAAC;IAED,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE,CAAC;QACxB,MAAM,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC,CAAA;QACnB,MAAM,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC,CAAA;QAEnB,wEAAwE;QACxE,4DAA4D;QAC5D,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACvB,IAAI,IAAI,KAAK,SAAS,IAAI,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC;gBAAE,SAAQ;YACvD,OAAO,KAAK,CAAA;QACd,CAAC;aAAM,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YAC9B,OAAO,KAAK,CAAA;QACd,CAAC;QAED,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,CAAC;YAC3B,OAAO,KAAK,CAAA;QACd,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAA;AACb,CAAC","sourcesContent":["import { ifCid, isCid } from './cid.js'\nimport { LexValue } from './lex.js'\nimport { isPlainObject } from './object.js'\nimport { ui8Equals } from './uint8array.js'\n\n/**\n * Performs deep equality comparison between two {@link LexValue}s.\n *\n * This function correctly handles all Lexicon data types including:\n * - Primitives (string, number, boolean, null)\n * - Arrays (recursive element comparison)\n * - Objects/LexMaps (recursive key-value comparison)\n * - Uint8Arrays (byte-by-byte comparison)\n * - CIDs (using CID equality)\n *\n * @param a - First LexValue to compare\n * @param b - Second LexValue to compare\n * @returns `true` if the values are deeply equal\n * @throws {TypeError} If either value is not a valid LexValue (e.g., contains unsupported types)\n *\n * @example\n * ```typescript\n * import { lexEquals } from '@atproto/lex-data'\n *\n * // Primitives\n * lexEquals('hello', 'hello') // true\n * lexEquals(42, 42) // true\n *\n * // Arrays\n * lexEquals([1, 2, 3], [1, 2, 3]) // true\n * lexEquals([1, 2], [1, 2, 3]) // false\n *\n * // Objects\n * lexEquals({ a: 1, b: 2 }, { a: 1, b: 2 }) // true\n * lexEquals({ a: 1 }, { a: 1, b: 2 }) // false\n *\n * // CIDs\n * lexEquals(cid1, cid2) // true if CIDs are equal\n *\n * // Uint8Arrays\n * lexEquals(new Uint8Array([1, 2]), new Uint8Array([1, 2])) // true\n * ```\n */\nexport function lexEquals(a: LexValue, b: LexValue): boolean {\n if (Object.is(a, b)) {\n return true\n }\n\n if (\n a == null ||\n b == null ||\n typeof a !== 'object' ||\n typeof b !== 'object'\n ) {\n return false\n }\n\n if (Array.isArray(a)) {\n if (!Array.isArray(b)) {\n return false\n }\n if (a.length !== b.length) {\n return false\n }\n for (let i = 0; i < a.length; i++) {\n if (!lexEquals(a[i], b[i])) {\n return false\n }\n }\n return true\n } else if (Array.isArray(b)) {\n return false\n }\n\n if (ArrayBuffer.isView(a)) {\n if (!ArrayBuffer.isView(b)) return false\n return ui8Equals(a, b)\n } else if (ArrayBuffer.isView(b)) {\n return false\n }\n\n if (isCid(a)) {\n // @NOTE CID.equals returns its argument when it is falsy (e.g. null or\n // undefined) so we need to explicitly check that the output is \"true\".\n return ifCid(b)?.equals(a) === true\n } else if (isCid(b)) {\n return false\n }\n\n if (!isPlainObject(a) || !isPlainObject(b)) {\n // Foolproof (should never happen)\n throw new TypeError(\n 'Invalid LexValue (expected CID, Uint8Array, or LexMap)',\n )\n }\n\n const aKeys = Object.keys(a)\n const bKeys = Object.keys(b)\n\n if (aKeys.length !== bKeys.length) {\n return false\n }\n\n for (const key of aKeys) {\n const aVal = a[key]\n const bVal = b[key]\n\n // Needed because of the optional index signature in the Lex object type\n // though, in practice, aVal should never be undefined here.\n if (aVal === undefined) {\n if (bVal === undefined && bKeys.includes(key)) continue\n return false\n } else if (bVal === undefined) {\n return false\n }\n\n if (!lexEquals(aVal, bVal)) {\n return false\n }\n }\n\n return true\n}\n"]}
1
+ {"version":3,"file":"lex-equals.js","sourceRoot":"","sources":["../src/lex-equals.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,UAAU,CAAA;AAEvC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,MAAM,UAAU,SAAS,CAAC,CAAW,EAAE,CAAW;IAChD,IAAI,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;QACpB,OAAO,IAAI,CAAA;IACb,CAAC;IAED,IACE,CAAC,IAAI,IAAI;QACT,CAAC,IAAI,IAAI;QACT,OAAO,CAAC,KAAK,QAAQ;QACrB,OAAO,CAAC,KAAK,QAAQ,EACrB,CAAC;QACD,OAAO,KAAK,CAAA;IACd,CAAC;IAED,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;QACrB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;YACtB,OAAO,KAAK,CAAA;QACd,CAAC;QACD,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC;YAC1B,OAAO,KAAK,CAAA;QACd,CAAC;QACD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAClC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;gBAC3B,OAAO,KAAK,CAAA;YACd,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAA;IACb,CAAC;SAAM,IAAI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IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACvB,IAAI,IAAI,KAAK,SAAS,IAAI,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC;gBAAE,SAAQ;YACvD,OAAO,KAAK,CAAA;QACd,CAAC;aAAM,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YAC9B,OAAO,KAAK,CAAA;QACd,CAAC;QAED,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,CAAC;YAC3B,OAAO,KAAK,CAAA;QACd,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAA;AACb,CAAC","sourcesContent":["import { ifCid, isCid } from './cid.js'\nimport { LexValue } from './lex.js'\nimport { isPlainObject } from './object.js'\nimport { ui8Equals } from './uint8array.js'\n\n/**\n * Performs deep equality comparison between two {@link LexValue}s.\n *\n * This function correctly handles all Lexicon data types including:\n * - Primitives (string, number, boolean, null)\n * - Arrays (recursive element comparison)\n * - Objects/LexMaps (recursive key-value comparison)\n * - Uint8Arrays (byte-by-byte comparison)\n * - CIDs (using CID equality)\n *\n * @param a - First LexValue to compare\n * @param b - Second LexValue to compare\n * @returns `true` if the values are deeply equal\n * @throws {TypeError} If either value is not a valid LexValue (e.g., contains unsupported types)\n *\n * @example\n * ```typescript\n * import { lexEquals } from '@atproto/lex-data'\n *\n * // Primitives\n * lexEquals('hello', 'hello') // true\n * lexEquals(42, 42) // true\n *\n * // Arrays\n * lexEquals([1, 2, 3], [1, 2, 3]) // true\n * lexEquals([1, 2], [1, 2, 3]) // false\n *\n * // Objects\n * lexEquals({ a: 1, b: 2 }, { a: 1, b: 2 }) // true\n * lexEquals({ a: 1 }, { a: 1, b: 2 }) // false\n *\n * // CIDs\n * lexEquals(cid1, cid2) // true if CIDs are equal\n *\n * // Uint8Arrays\n * lexEquals(new Uint8Array([1, 2]), new Uint8Array([1, 2])) // true\n * ```\n */\nexport function lexEquals(a: LexValue, b: LexValue): boolean {\n if (Object.is(a, b)) {\n return true\n }\n\n if (\n a == null ||\n b == null ||\n typeof a !== 'object' ||\n typeof b !== 'object'\n ) {\n return false\n }\n\n if (Array.isArray(a)) {\n if (!Array.isArray(b)) {\n return false\n }\n if (a.length !== b.length) {\n return false\n }\n for (let i = 0; i < a.length; i++) {\n if (!lexEquals(a[i], b[i])) {\n return false\n }\n }\n return true\n } else if (Array.isArray(b)) {\n return false\n }\n\n if (ArrayBuffer.isView(a)) {\n if (!ArrayBuffer.isView(b)) return false\n return ui8Equals(a, b)\n } else if (ArrayBuffer.isView(b)) {\n return false\n }\n\n if (isCid(a)) {\n // @NOTE CID.equals returns its argument when it is falsy (e.g. null or\n // undefined) so we need to explicitly check that the output is \"true\".\n return ifCid(b)?.equals(a) === true\n } else if (isCid(b)) {\n return false\n }\n\n if (!isPlainObject(a) || !isPlainObject(b)) {\n // Foolproof (should never happen)\n throw new TypeError(\n 'Invalid LexValue (expected CID, Uint8Array, or LexMap)',\n )\n }\n\n const aKeys = Object.keys(a)\n const bKeys = Object.keys(b)\n\n if (aKeys.length !== bKeys.length) {\n return false\n }\n\n for (const key of aKeys) {\n const aVal = a[key]\n const bVal = b[key]\n\n // Needed because of the optional index signature in the Lex object type\n // though, in practice, aVal should never be undefined here.\n if (aVal === undefined) {\n if (bVal === undefined && bKeys.includes(key)) continue\n return false\n } else if (bVal === undefined) {\n return false\n }\n\n if (!lexEquals(aVal, bVal)) {\n return false\n }\n }\n\n return true\n}\n"]}
package/dist/lex-error.js CHANGED
@@ -1,6 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LexError = void 0;
4
1
  /**
5
2
  * Error class for Lexicon-related errors.
6
3
  *
@@ -10,9 +7,7 @@ exports.LexError = void 0;
10
7
  *
11
8
  * @typeParam N - The specific error code type
12
9
  */
13
- class LexError extends Error {
14
- error;
15
- name = 'LexError';
10
+ export class LexError extends Error {
16
11
  /**
17
12
  * @param error - The error code identifying the type of error, typically used in XRPC error payloads
18
13
  * @param message - Optional human-readable error message
@@ -22,6 +17,7 @@ class LexError extends Error {
22
17
  options) {
23
18
  super(message, options);
24
19
  this.error = error;
20
+ this.name = 'LexError';
25
21
  }
26
22
  /**
27
23
  * Returns a string representation of this error.
@@ -42,5 +38,4 @@ class LexError extends Error {
42
38
  return { error, message: message || undefined };
43
39
  }
44
40
  }
45
- exports.LexError = LexError;
46
41
  //# sourceMappingURL=lex-error.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"lex-error.js","sourceRoot":"","sources":["../src/lex-error.ts"],"names":[],"mappings":";;;AAsCA;;;;;;;;GAQG;AACH,MAAa,QAAgD,SAAQ,KAAK;IAS7D;IARX,IAAI,GAAG,UAAU,CAAA;IAEjB;;;;OAIG;IACH,YACW,KAAQ,EACjB,OAAgB,EAAE,gDAAgD;IAClE,OAAsB;QAEtB,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;QAJd,UAAK,GAAL,KAAK,CAAG;IAKnB,CAAC;IAED;;;;OAIG;IACH,QAAQ;QACN,OAAO,GAAG,IAAI,CAAC,IAAI,MAAM,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC,OAAO,EAAE,CAAA;IACxD,CAAC;IAED;;;;;OAKG;IACH,MAAM;QACJ,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,IAAI,CAAA;QAC/B,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,CAAA;IACjD,CAAC;CACF;AAnCD,4BAmCC","sourcesContent":["/**\n * Error code type for Lexicon errors.\n *\n * Error codes identify the type of error that occurred (e.g., 'InvalidRequest').\n *\n * @example\n * ```typescript\n * import type { LexErrorCode } from '@atproto/lex-data'\n *\n * const errorCode: LexErrorCode = 'InvalidRequest'\n * ```\n */\nexport type LexErrorCode = string & NonNullable<unknown>\n\n/**\n * JSON-serializable error data structure.\n *\n * This is the standard format for error responses in the AT Protocol XRPC protocol.\n *\n * @typeParam N - The specific error code type\n *\n * @example\n * ```typescript\n * import type { LexErrorData } from '@atproto/lex-data'\n *\n * const errorData: LexErrorData = {\n * error: 'InvalidRequest',\n * message: 'Missing required field: handle'\n * }\n * ```\n */\nexport type LexErrorData<N extends LexErrorCode = LexErrorCode> = {\n /** The error code identifying the type of error. */\n error: N\n /** Optional human-readable error message. */\n message?: string\n}\n\n/**\n * Error class for Lexicon-related errors.\n *\n * LexError extends the standard JavaScript {@link Error} with AT\n * Protocol-specific functionality including an `error` code property and\n * methods for representation as (XRPC) error responses payloads.\n *\n * @typeParam N - The specific error code type\n */\nexport class LexError<N extends LexErrorCode = LexErrorCode> extends Error {\n name = 'LexError'\n\n /**\n * @param error - The error code identifying the type of error, typically used in XRPC error payloads\n * @param message - Optional human-readable error message\n * @param options - Standard Error options (e.g., cause)\n */\n constructor(\n readonly error: N,\n message?: string, // Defaults to empty string in Error constructor\n options?: ErrorOptions,\n ) {\n super(message, options)\n }\n\n /**\n * Returns a string representation of this error.\n *\n * @returns A formatted string: \"LexErrorClass: [MyErrorCode] My message\"\n */\n toString(): string {\n return `${this.name}: [${this.error}] ${this.message}`\n }\n\n /**\n * Converts this error to a JSON-serializable object.\n *\n * @returns The error data suitable for JSON serialization\n * @note The `error` generic is *not* constrained to {@link N} to allow subclasses to override the error code type.\n */\n toJSON(): LexErrorData<LexErrorCode> {\n const { error, message } = this\n return { error, message: message || undefined }\n }\n}\n"]}
1
+ {"version":3,"file":"lex-error.js","sourceRoot":"","sources":["../src/lex-error.ts"],"names":[],"mappings":"AAsCA;;;;;;;;GAQG;AACH,MAAM,OAAO,QAAgD,SAAQ,KAAK;IAGxE;;;;OAIG;IACH,YACW,KAAQ,EACjB,OAAgB,EAAE,gDAAgD;IAClE,OAAsB;QAEtB,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;QAJd,UAAK,GAAL,KAAK,CAAG;QARnB,SAAI,GAAG,UAAU,CAAA;IAajB,CAAC;IAED;;;;OAIG;IACH,QAAQ;QACN,OAAO,GAAG,IAAI,CAAC,IAAI,MAAM,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC,OAAO,EAAE,CAAA;IACxD,CAAC;IAED;;;;;OAKG;IACH,MAAM;QACJ,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,IAAI,CAAA;QAC/B,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,CAAA;IACjD,CAAC;CACF","sourcesContent":["/**\n * Error code type for Lexicon errors.\n *\n * Error codes identify the type of error that occurred (e.g., 'InvalidRequest').\n *\n * @example\n * ```typescript\n * import type { LexErrorCode } from '@atproto/lex-data'\n *\n * const errorCode: LexErrorCode = 'InvalidRequest'\n * ```\n */\nexport type LexErrorCode = string & NonNullable<unknown>\n\n/**\n * JSON-serializable error data structure.\n *\n * This is the standard format for error responses in the AT Protocol XRPC protocol.\n *\n * @typeParam N - The specific error code type\n *\n * @example\n * ```typescript\n * import type { LexErrorData } from '@atproto/lex-data'\n *\n * const errorData: LexErrorData = {\n * error: 'InvalidRequest',\n * message: 'Missing required field: handle'\n * }\n * ```\n */\nexport type LexErrorData<N extends LexErrorCode = LexErrorCode> = {\n /** The error code identifying the type of error. */\n error: N\n /** Optional human-readable error message. */\n message?: string\n}\n\n/**\n * Error class for Lexicon-related errors.\n *\n * LexError extends the standard JavaScript {@link Error} with AT\n * Protocol-specific functionality including an `error` code property and\n * methods for representation as (XRPC) error responses payloads.\n *\n * @typeParam N - The specific error code type\n */\nexport class LexError<N extends LexErrorCode = LexErrorCode> extends Error {\n name = 'LexError'\n\n /**\n * @param error - The error code identifying the type of error, typically used in XRPC error payloads\n * @param message - Optional human-readable error message\n * @param options - Standard Error options (e.g., cause)\n */\n constructor(\n readonly error: N,\n message?: string, // Defaults to empty string in Error constructor\n options?: ErrorOptions,\n ) {\n super(message, options)\n }\n\n /**\n * Returns a string representation of this error.\n *\n * @returns A formatted string: \"LexErrorClass: [MyErrorCode] My message\"\n */\n toString(): string {\n return `${this.name}: [${this.error}] ${this.message}`\n }\n\n /**\n * Converts this error to a JSON-serializable object.\n *\n * @returns The error data suitable for JSON serialization\n * @note The `error` generic is *not* constrained to {@link N} to allow subclasses to override the error code type.\n */\n toJSON(): LexErrorData<LexErrorCode> {\n const { error, message } = this\n return { error, message: message || undefined }\n }\n}\n"]}