@atproto/lex-data 0.0.6 → 0.0.8

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # @atproto/lex-data
2
2
 
3
+ ## 0.0.8
4
+
5
+ ### Patch Changes
6
+
7
+ - [#4528](https://github.com/bluesky-social/atproto/pull/4528) [`dfd4bee`](https://github.com/bluesky-social/atproto/commit/dfd4bee4abbc1a3e53531bb499a6f3169c13ed9e) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add `asMultiformatsCID` utility for improved compatibility between `Cid` interface and legacy use of multiformat's `CID`
8
+
9
+ - [#4528](https://github.com/bluesky-social/atproto/pull/4528) [`dfd4bee`](https://github.com/bluesky-social/atproto/commit/dfd4bee4abbc1a3e53531bb499a6f3169c13ed9e) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Make the `Cid` interface stricter to allow for better futur proofing.
10
+
11
+ - [#4528](https://github.com/bluesky-social/atproto/pull/4528) [`dfd4bee`](https://github.com/bluesky-social/atproto/commit/dfd4bee4abbc1a3e53531bb499a6f3169c13ed9e) Thanks [@matthieusieben](https://github.com/matthieusieben)! - `isCid`, `ifCid` and `asCid` now always return the input value instead a potentially different instance.
12
+
13
+ - [#4528](https://github.com/bluesky-social/atproto/pull/4528) [`dfd4bee`](https://github.com/bluesky-social/atproto/commit/dfd4bee4abbc1a3e53531bb499a6f3169c13ed9e) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Expose `createCid` utility
14
+
15
+ ## 0.0.7
16
+
17
+ ### Patch Changes
18
+
19
+ - [#4512](https://github.com/bluesky-social/atproto/pull/4512) [`d78484f`](https://github.com/bluesky-social/atproto/commit/d78484f94d8ba1352ec66030115000d515c9dafe) Thanks [@matthieusieben](https://github.com/matthieusieben)! - **breaking**: replace `strict` options with `flavor` checking when parsing/decoding/validating `Cid` values
20
+
21
+ - [#4512](https://github.com/bluesky-social/atproto/pull/4512) [`d78484f`](https://github.com/bluesky-social/atproto/commit/d78484f94d8ba1352ec66030115000d515c9dafe) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Expose CID creation and validation utilities
22
+
23
+ - [#4512](https://github.com/bluesky-social/atproto/pull/4512) [`d78484f`](https://github.com/bluesky-social/atproto/commit/d78484f94d8ba1352ec66030115000d515c9dafe) Thanks [@matthieusieben](https://github.com/matthieusieben)! - **breaking**: `asCid` now throws if the input cannot be cast into a `Cid`
24
+
3
25
  ## 0.0.6
4
26
 
5
27
  ### Patch Changes
package/dist/blob.d.ts CHANGED
@@ -1,15 +1,15 @@
1
- import { Cid } from './cid.js';
1
+ import { Cid, RawCid } from './cid.js';
2
2
  import { LexValue } from './lex.js';
3
3
  /**
4
4
  * @note {@link BlobRef} is just a {@link LexMap} with a specific shape.
5
5
  */
6
- export type BlobRef = {
6
+ export type BlobRef<Ref extends Cid = Cid> = {
7
7
  $type: 'blob';
8
8
  mimeType: string;
9
- ref: Cid;
9
+ ref: Ref;
10
10
  size: number;
11
11
  };
12
- export type BlobRefValidationOptions = {
12
+ export type BlobRefCheckOptions = {
13
13
  /**
14
14
  * If `false`, skips strict CID validation of {@link BlobRef.ref}, allowing
15
15
  * any valid CID. Otherwise, validates that the CID is v1, uses the raw
@@ -19,7 +19,14 @@ export type BlobRefValidationOptions = {
19
19
  */
20
20
  strict?: boolean;
21
21
  };
22
- export declare function isBlobRef(input: unknown, options?: BlobRefValidationOptions): input is BlobRef;
22
+ export type InferCheckedBlobRef<TOptions extends BlobRefCheckOptions> = TOptions extends {
23
+ strict: false;
24
+ } ? BlobRef : {
25
+ strict: boolean;
26
+ } extends TOptions ? BlobRef : BlobRef<RawCid>;
27
+ export declare function isBlobRef(input: unknown): input is BlobRef<RawCid>;
28
+ export declare function isBlobRef<TOptions extends BlobRefCheckOptions>(input: unknown, options: TOptions): input is InferCheckedBlobRef<TOptions>;
29
+ export declare function isBlobRef(input: unknown, options?: BlobRefCheckOptions): input is BlobRef;
23
30
  /**
24
31
  * @note {@link LegacyBlobRef} is just a {@link LexMap} with a specific shape.
25
32
  */
@@ -28,21 +35,22 @@ export type LegacyBlobRef = {
28
35
  mimeType: string;
29
36
  };
30
37
  export declare function isLegacyBlobRef(input: unknown): input is LegacyBlobRef;
31
- export type EnumBlobRefsOptions = BlobRefValidationOptions & {
38
+ export type EnumBlobRefsOptions = BlobRefCheckOptions & {
32
39
  /**
33
40
  * @defaults to `false`
34
41
  */
35
42
  allowLegacy?: boolean;
36
43
  };
44
+ export type InferEnumBlobRefs<TOptions extends EnumBlobRefsOptions> = TOptions extends {
45
+ allowLegacy: true;
46
+ } ? InferCheckedBlobRef<TOptions> | LegacyBlobRef : {
47
+ allowLegacy: boolean;
48
+ } extends TOptions ? InferCheckedBlobRef<TOptions> | LegacyBlobRef : InferCheckedBlobRef<TOptions>;
37
49
  /**
38
50
  * Enumerates all {@link BlobRef}s (and, optionally, {@link LegacyBlobRef}s)
39
51
  * found within a {@link LexValue}.
40
52
  */
41
- export declare function enumBlobRefs(input: LexValue, options: EnumBlobRefsOptions & {
42
- allowLegacy: true;
43
- }): Generator<BlobRef | LegacyBlobRef, void, unknown>;
44
- export declare function enumBlobRefs(input: LexValue, options?: EnumBlobRefsOptions & {
45
- allowLegacy?: false;
46
- }): Generator<BlobRef, void, unknown>;
53
+ export declare function enumBlobRefs(input: LexValue): Generator<BlobRef<RawCid>, void, unknown>;
54
+ export declare function enumBlobRefs<TOptions extends EnumBlobRefsOptions>(input: LexValue, options: TOptions): Generator<InferEnumBlobRefs<TOptions>, void, unknown>;
47
55
  export declare function enumBlobRefs(input: LexValue, options?: EnumBlobRefsOptions): Generator<BlobRef | LegacyBlobRef, void, unknown>;
48
56
  //# sourceMappingURL=blob.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"blob.d.ts","sourceRoot":"","sources":["../src/blob.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EAKJ,MAAM,UAAU,CAAA;AACjB,OAAO,EAAE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GAAG,KAAK,IAAI,aAAa,CAyBtE;AAED,MAAM,MAAM,mBAAmB,GAAG,mBAAmB,GAAG;IACtD;;OAEG;IACH,WAAW,CAAC,EAAE,OAAO,CAAA;CACtB,CAAA;AAED,MAAM,MAAM,iBAAiB,CAAC,QAAQ,SAAS,mBAAmB,IAChE,QAAQ,SAAS;IAAE,WAAW,EAAE,IAAI,CAAA;CAAE,GAClC,mBAAmB,CAAC,QAAQ,CAAC,GAAG,aAAa,GAC7C;IAAE,WAAW,EAAE,OAAO,CAAA;CAAE,SAAS,QAAQ,GACvC,mBAAmB,CAAC,QAAQ,CAAC,GAAG,aAAa,GAC7C,mBAAmB,CAAC,QAAQ,CAAC,CAAA;AAErC;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,QAAQ,GACd,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA;AAC5C,wBAAgB,YAAY,CAAC,QAAQ,SAAS,mBAAmB,EAC/D,KAAK,EAAE,QAAQ,EACf,OAAO,EAAE,QAAQ,GAChB,SAAS,CAAC,iBAAiB,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA;AACxD,wBAAgB,YAAY,CAC1B,KAAK,EAAE,QAAQ,EACf,OAAO,CAAC,EAAE,mBAAmB,GAC5B,SAAS,CAAC,OAAO,GAAG,aAAa,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA"}
package/dist/blob.js CHANGED
@@ -31,21 +31,12 @@ function isBlobRef(input, options) {
31
31
  return false;
32
32
  }
33
33
  }
34
- const cid = (0, cid_js_1.asCid)(ref);
34
+ const cid = (0, cid_js_1.ifCid)(ref,
35
+ // Strict unless explicitly disabled
36
+ options?.strict === false ? undefined : { flavor: 'raw' });
35
37
  if (!cid) {
36
38
  return false;
37
39
  }
38
- if (options?.strict !== false) {
39
- if (cid.version !== 1) {
40
- return false;
41
- }
42
- if (cid.code !== cid_js_1.RAW_BIN_MULTICODEC) {
43
- return false;
44
- }
45
- if (cid.multihash.code !== cid_js_1.SHA2_256_MULTIHASH_CODE) {
46
- return false;
47
- }
48
- }
49
40
  return true;
50
41
  }
51
42
  function isLegacyBlobRef(input) {
@@ -64,15 +55,13 @@ function isLegacyBlobRef(input) {
64
55
  return false;
65
56
  }
66
57
  }
67
- try {
68
- (0, cid_js_1.parseCid)(cid);
69
- }
70
- catch {
58
+ if (!(0, cid_js_1.validateCidString)(cid)) {
71
59
  return false;
72
60
  }
73
61
  return true;
74
62
  }
75
63
  function* enumBlobRefs(input, options) {
64
+ // LegacyBlobRef not included by default
76
65
  const includeLegacy = options?.allowLegacy === true;
77
66
  // Using a stack to avoid recursion depth issues.
78
67
  const stack = [input];
package/dist/blob.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"blob.js","sourceRoot":"","sources":["../src/blob.ts"],"names":[],"mappings":";;AA+BA,8BAuDC;AAUD,0CA2BC;AAyBD,oCAwCC;AA5LD,qCAMiB;AAEjB,2CAAyD;AAuBzD,SAAgB,SAAS,CACvB,KAAc,EACd,OAAkC;IAElC,IAAI,CAAC,IAAA,yBAAa,EAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,OAAO,KAAK,CAAA;IACd,CAAC;IAED,IAAI,KAAK,EAAE,KAAK,KAAK,MAAM,EAAE,CAAC;QAC5B,OAAO,KAAK,CAAA;IACd,CAAC;IAED,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,KAAK,CAAA;IACrC,mCAAmC;IACnC,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QAC5D,OAAO,KAAK,CAAA;IACd,CAAC;IAED,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC;QACxE,OAAO,KAAK,CAAA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AAA;gBACb,CAAC;qBAAM,IAAI,aAAa,IAAI,eAAe,CAAC,KAAK,CAAC,EAAE,CAAC;oBACnD,MAAM,KAAK,CAAA;gBACb,CAAC;qBAAM,CAAC;oBACN,KAAK,MAAM,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;wBACrC,IAAI,CAAC,IAAI,IAAI;4BAAE,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;oBAC9B,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC,QAAQ,KAAK,CAAC,MAAM,GAAG,CAAC,EAAC;IAE1B,+BAA+B;IAC/B,OAAO,CAAC,KAAK,EAAE,CAAA;AACjB,CAAC","sourcesContent":["import {\n Cid,\n RAW_BIN_MULTICODEC,\n SHA2_256_MULTIHASH_CODE,\n asCid,\n parseCid,\n} from './cid.js'\nimport { LexValue } from './lex.js'\nimport { isPlainObject, isPlainProto } from './object.js'\n\n/**\n * @note {@link BlobRef} is just a {@link LexMap} with a specific shape.\n */\nexport type BlobRef = {\n $type: 'blob'\n mimeType: string\n ref: Cid\n size: number\n}\n\nexport type BlobRefValidationOptions = {\n /**\n * If `false`, skips strict CID validation of {@link BlobRef.ref}, allowing\n * any valid CID. Otherwise, validates that the CID is v1, uses the raw\n * multicodec, and has a sha256 multihash.\n *\n * @defaults to `true`\n */\n strict?: boolean\n}\n\nexport function isBlobRef(\n input: unknown,\n options?: BlobRefValidationOptions,\n): input is BlobRef {\n if (!isPlainObject(input)) {\n return false\n }\n\n if (input?.$type !== 'blob') {\n return false\n }\n\n const { mimeType, size, ref } = input\n // @NOTE Very basic mime validation\n if (typeof mimeType !== 'string' || !mimeType.includes('/')) {\n return false\n }\n\n if (typeof size !== 'number' || size < 0 || !Number.isSafeInteger(size)) {\n return false\n }\n\n if (typeof ref !== 'object' || ref === null) {\n return false\n }\n\n for (const key in input) {\n if (\n key !== '$type' &&\n key !== 'mimeType' &&\n key !== 'ref' &&\n key !== 'size'\n ) {\n return false\n }\n }\n\n const cid = asCid(ref)\n if (!cid) {\n return false\n }\n\n if (options?.strict !== false) {\n if (cid.version !== 1) {\n return false\n }\n if (cid.code !== RAW_BIN_MULTICODEC) {\n return false\n }\n if (cid.multihash.code !== SHA2_256_MULTIHASH_CODE) {\n return false\n }\n }\n\n return true\n}\n\n/**\n * @note {@link LegacyBlobRef} is just a {@link LexMap} with a specific shape.\n */\nexport type LegacyBlobRef = {\n cid: string\n mimeType: string\n}\n\nexport function isLegacyBlobRef(input: unknown): input is LegacyBlobRef {\n if (!isPlainObject(input)) {\n return false\n }\n\n const { cid, mimeType } = input\n if (typeof cid !== 'string') {\n return false\n }\n\n if (typeof mimeType !== 'string' || mimeType.length === 0) {\n return false\n }\n\n for (const key in input) {\n if (key !== 'cid' && key !== 'mimeType') {\n return false\n }\n }\n\n try {\n parseCid(cid)\n } catch {\n return false\n }\n\n return true\n}\n\nexport type EnumBlobRefsOptions = BlobRefValidationOptions & {\n /**\n * @defaults to `false`\n */\n allowLegacy?: boolean\n}\n\n/**\n * Enumerates all {@link BlobRef}s (and, optionally, {@link LegacyBlobRef}s)\n * found within a {@link LexValue}.\n */\nexport function enumBlobRefs(\n input: LexValue,\n options: EnumBlobRefsOptions & { allowLegacy: true },\n): Generator<BlobRef | LegacyBlobRef, void, unknown>\nexport function enumBlobRefs(\n input: LexValue,\n options?: EnumBlobRefsOptions & { allowLegacy?: false },\n): Generator<BlobRef, void, unknown>\nexport function enumBlobRefs(\n input: LexValue,\n options?: EnumBlobRefsOptions,\n): Generator<BlobRef | LegacyBlobRef, void, unknown>\nexport function* enumBlobRefs(\n input: LexValue,\n options?: EnumBlobRefsOptions,\n): Generator<BlobRef | LegacyBlobRef, void, unknown> {\n const includeLegacy = options?.allowLegacy === true\n\n // Using a stack to avoid recursion depth issues.\n const stack: LexValue[] = [input]\n\n // Since we are using a stack, we could end-up in an infinite loop with cyclic\n // structures. Cyclic structures are not valid LexValues and should, thus,\n // never occur, but let's be safe.\n const visited = new Set<object>()\n\n do {\n const value = stack.pop()!\n\n if (value != null && typeof value === 'object') {\n if (Array.isArray(value)) {\n if (visited.has(value)) continue\n visited.add(value)\n stack.push(...value)\n } else if (isPlainProto(value)) {\n if (visited.has(value)) continue\n visited.add(value)\n if (isBlobRef(value, options)) {\n yield value\n } else if (includeLegacy && isLegacyBlobRef(value)) {\n yield value\n } else {\n for (const v of Object.values(value)) {\n if (v != null) stack.push(v)\n }\n }\n }\n }\n } while (stack.length > 0)\n\n // Optimization: ease GC's work\n visited.clear()\n}\n"]}
1
+ {"version":3,"file":"blob.js","sourceRoot":"","sources":["../src/blob.ts"],"names":[],"mappings":";;AAyCA,8BA+CC;AAUD,0CAyBC;AA+BD,oCAyCC;AAnMD,qCAAgE;AAEhE,2CAAyD;AAuCzD,SAAgB,SAAS,CACvB,KAAc,EACd,OAA6B;IAE7B,IAAI,CAAC,IAAA,yBAAa,EAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,OAAO,KAAK,CAAA;IACd,CAAC;IAED,IAAI,KAAK,EAAE,KAAK,KAAK,MAAM,EAAE,CAAC;QAC5B,OAAO,KAAK,CAAA;IACd,CAAC;IAED,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,KAAK,CAAA;IACrC,mCAAmC;IACnC,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QAC5D,OAAO,KAAK,CAAA;IACd,CAAC;IAED,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC;QACxE,OAAO,KAAK,CAAA;IACd,CAAC;IAED,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;QAC5C,OAAO,KAAK,CAAA;IACd,CAAC;IAED,KAAK,MAAM,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AG,CAAC,EAAC;IAE1B,+BAA+B;IAC/B,OAAO,CAAC,KAAK,EAAE,CAAA;AACjB,CAAC","sourcesContent":["import { Cid, RawCid, ifCid, validateCidString } from './cid.js'\nimport { LexValue } from './lex.js'\nimport { isPlainObject, isPlainProto } from './object.js'\n\n/**\n * @note {@link BlobRef} is just a {@link LexMap} with a specific shape.\n */\nexport type BlobRef<Ref extends Cid = Cid> = {\n $type: 'blob'\n mimeType: string\n ref: Ref\n size: number\n}\n\nexport type BlobRefCheckOptions = {\n /**\n * If `false`, skips strict CID validation of {@link BlobRef.ref}, allowing\n * any valid CID. Otherwise, validates that the CID is v1, uses the raw\n * multicodec, and has a sha256 multihash.\n *\n * @defaults to `true`\n */\n strict?: boolean\n}\n\nexport type InferCheckedBlobRef<TOptions extends BlobRefCheckOptions> =\n TOptions extends { strict: false }\n ? BlobRef\n : { strict: boolean } extends TOptions\n ? BlobRef\n : BlobRef<RawCid>\n\nexport function isBlobRef(input: unknown): input is BlobRef<RawCid>\nexport function isBlobRef<TOptions extends BlobRefCheckOptions>(\n input: unknown,\n options: TOptions,\n): input is InferCheckedBlobRef<TOptions>\nexport function isBlobRef(\n input: unknown,\n options?: BlobRefCheckOptions,\n): input is BlobRef\nexport function isBlobRef(\n input: unknown,\n options?: BlobRefCheckOptions,\n): input is BlobRef {\n if (!isPlainObject(input)) {\n return false\n }\n\n if (input?.$type !== 'blob') {\n return false\n }\n\n const { mimeType, size, ref } = input\n // @NOTE Very basic mime validation\n if (typeof mimeType !== 'string' || !mimeType.includes('/')) {\n return false\n }\n\n if (typeof size !== 'number' || size < 0 || !Number.isSafeInteger(size)) {\n return false\n }\n\n if (typeof ref !== 'object' || ref === null) {\n return false\n }\n\n for (const key in input) {\n if (\n key !== '$type' &&\n key !== 'mimeType' &&\n key !== 'ref' &&\n key !== 'size'\n ) {\n return false\n }\n }\n\n const cid = ifCid(\n ref,\n // Strict unless explicitly disabled\n options?.strict === false ? undefined : { flavor: 'raw' },\n )\n if (!cid) {\n return false\n }\n\n return true\n}\n\n/**\n * @note {@link LegacyBlobRef} is just a {@link LexMap} with a specific shape.\n */\nexport type LegacyBlobRef = {\n cid: string\n mimeType: string\n}\n\nexport function isLegacyBlobRef(input: unknown): input is LegacyBlobRef {\n if (!isPlainObject(input)) {\n return false\n }\n\n const { cid, mimeType } = input\n if (typeof cid !== 'string') {\n return false\n }\n\n if (typeof mimeType !== 'string' || mimeType.length === 0) {\n return false\n }\n\n for (const key in input) {\n if (key !== 'cid' && key !== 'mimeType') {\n return false\n }\n }\n\n if (!validateCidString(cid)) {\n return false\n }\n\n return true\n}\n\nexport type EnumBlobRefsOptions = BlobRefCheckOptions & {\n /**\n * @defaults to `false`\n */\n allowLegacy?: boolean\n}\n\nexport type InferEnumBlobRefs<TOptions extends EnumBlobRefsOptions> =\n TOptions extends { allowLegacy: true }\n ? InferCheckedBlobRef<TOptions> | LegacyBlobRef\n : { allowLegacy: boolean } extends TOptions\n ? InferCheckedBlobRef<TOptions> | LegacyBlobRef\n : InferCheckedBlobRef<TOptions>\n\n/**\n * Enumerates all {@link BlobRef}s (and, optionally, {@link LegacyBlobRef}s)\n * found within a {@link LexValue}.\n */\nexport function enumBlobRefs(\n input: LexValue,\n): Generator<BlobRef<RawCid>, void, unknown>\nexport function enumBlobRefs<TOptions extends EnumBlobRefsOptions>(\n input: LexValue,\n options: TOptions,\n): Generator<InferEnumBlobRefs<TOptions>, void, unknown>\nexport function enumBlobRefs(\n input: LexValue,\n options?: EnumBlobRefsOptions,\n): Generator<BlobRef | LegacyBlobRef, void, unknown>\nexport function* enumBlobRefs(\n input: LexValue,\n options?: EnumBlobRefsOptions,\n): Generator<BlobRef | LegacyBlobRef, void, unknown> {\n // LegacyBlobRef not included by default\n const includeLegacy = options?.allowLegacy === true\n\n // Using a stack to avoid recursion depth issues.\n const stack: LexValue[] = [input]\n\n // Since we are using a stack, we could end-up in an infinite loop with cyclic\n // structures. Cyclic structures are not valid LexValues and should, thus,\n // never occur, but let's be safe.\n const visited = new Set<object>()\n\n do {\n const value = stack.pop()!\n\n if (value != null && typeof value === 'object') {\n if (Array.isArray(value)) {\n if (visited.has(value)) continue\n visited.add(value)\n stack.push(...value)\n } else if (isPlainProto(value)) {\n if (visited.has(value)) continue\n visited.add(value)\n if (isBlobRef(value, options)) {\n yield value\n } else if (includeLegacy && isLegacyBlobRef(value)) {\n yield value\n } else {\n for (const v of Object.values(value)) {\n if (v != null) stack.push(v)\n }\n }\n }\n }\n } while (stack.length > 0)\n\n // Optimization: ease GC's work\n visited.clear()\n}\n"]}
package/dist/cid.d.ts CHANGED
@@ -1,18 +1,28 @@
1
1
  import { CID } from 'multiformats/cid';
2
2
  export declare const DAG_CBOR_MULTICODEC = 113;
3
- export declare const RAW_BIN_MULTICODEC = 85;
4
- export declare const SHA2_256_MULTIHASH_CODE = 18;
5
- export type MultihashDigest<Code extends number = number> = {
6
- code: Code;
3
+ export type DAG_CBOR_MULTICODEC = typeof DAG_CBOR_MULTICODEC;
4
+ export declare const RAW_MULTICODEC = 85;
5
+ export type RAW_MULTICODEC = typeof RAW_MULTICODEC;
6
+ export declare const SHA256_MULTIHASH: 18;
7
+ export type SHA256_MULTIHASH = typeof SHA256_MULTIHASH;
8
+ export declare const SHA512_MULTIHASH: 19;
9
+ export type SHA512_MULTIHASH = typeof SHA512_MULTIHASH;
10
+ export interface Multihash<TCode extends number = number> {
11
+ /**
12
+ * Code of the multihash
13
+ */
14
+ code: TCode;
15
+ /**
16
+ * Raw digest
17
+ */
7
18
  digest: Uint8Array;
8
- size: number;
9
- bytes: Uint8Array;
10
- };
19
+ }
20
+ export declare function multihashEquals(a: Multihash, b: Multihash): boolean;
11
21
  declare module 'multiformats/cid' {
12
22
  /**
13
23
  * @deprecated use the {@link Cid} interface from `@atproto/lex-data`, and
14
- * related helpers ({@link asCid}, {@link parseCid}, {@link decodeCid},
15
- * {@link createCid}, {@link isCid}), instead.
24
+ * related helpers ({@link isCid}, {@link ifCid}, {@link asCid},
25
+ * {@link parseCid}, {@link decodeCid}), instead.
16
26
  *
17
27
  * This is marked as deprecated because we want to discourage direct usage of
18
28
  * `multiformats/cid` in dependent packages, and instead have them rely on the
@@ -25,33 +35,117 @@ declare module 'multiformats/cid' {
25
35
  *
26
36
  * In order to avoid compatibility issues, while preparing for future breaking
27
37
  * changes (CID in multiformats v10+ has a slightly different interface), as
28
- * we update or swap out `multiformats`, we provide our own stable {@link Cid}
29
- * interface.
38
+ * we update or swap out `multiformats`, `@atproto/lex-data` provides its own
39
+ * stable {@link Cid} interface.
30
40
  */
31
41
  interface CID {
32
42
  }
33
43
  }
34
- export { CID };
44
+ export { /** @deprecated */ CID };
45
+ /**
46
+ * Converts a {@link Cid} to a multiformats {@link CID} instance.
47
+ *
48
+ * @deprecated Packages depending on `@atproto/lex-data` should use the
49
+ * {@link Cid} interface instead of relying on `multiformats`'s {@link CID}
50
+ * implementation directly. This is to avoid compatibility issues, and in order
51
+ * to allow better portability, compatibility and future updates.
52
+ */
53
+ export declare function asMultiformatsCID<TVersion extends 0 | 1 = 0 | 1, TCode extends number = number, TMultihashCode extends number = number>(input: Cid<TVersion, TCode, TMultihashCode>): CID & Cid<TVersion, TCode, TMultihashCode>;
35
54
  /**
36
- * Interface for working with decoded CID string, compatible with
37
- * {@link CID} implementation.
55
+ * Interface for working with CIDs
38
56
  */
39
- export interface Cid {
40
- version: 0 | 1;
41
- code: number;
42
- multihash: MultihashDigest;
43
- bytes: Uint8Array;
44
- equals(other: unknown): boolean;
57
+ export interface Cid<TVersion extends 0 | 1 = 0 | 1, TCode extends number = number, TMultihashCode extends number = number> {
58
+ readonly version: TVersion;
59
+ readonly code: TCode;
60
+ readonly multihash: Multihash<TMultihashCode>;
61
+ /**
62
+ * Binary representation of the whole CID.
63
+ */
64
+ readonly bytes: Uint8Array;
65
+ equals(other: Cid): boolean;
45
66
  toString(): string;
46
67
  }
47
- export declare function asCid(value: unknown): Cid | null;
48
- export declare function parseCid(input: string): Cid;
49
- export declare function decodeCid(bytes: Uint8Array): Cid;
50
- export declare function createCid(code: number, digest: MultihashDigest): Cid;
51
- export declare function isCid(value: unknown, options?: {
52
- strict?: boolean;
53
- }): value is Cid;
54
- export declare function validateCidString(input: string): boolean;
55
- export declare function parseCidString(input: string): Cid | undefined;
56
- export declare function ensureValidCidString(input: string): void;
68
+ /**
69
+ * Represents the cid of raw binary data (like media blobs).
70
+ *
71
+ * The use of {@link SHA256_MULTIHASH} is recommended but not required for raw CIDs.
72
+ *
73
+ * @see {@link https://atproto.com/specs/data-model#link-and-cid-formats ATproto Data Model - Link and CID Formats}
74
+ */
75
+ export type RawCid = Cid<1, RAW_MULTICODEC>;
76
+ export declare function isRawCid(cid: Cid): cid is RawCid;
77
+ /**
78
+ * Represents a DASL compliant CID.
79
+ * @see {@link https://dasl.ing/cid.html DASL-CIDs}
80
+ */
81
+ export type DaslCid = Cid<1, RAW_MULTICODEC | DAG_CBOR_MULTICODEC, SHA256_MULTIHASH>;
82
+ export declare function isDaslCid(cid: Cid): cid is DaslCid;
83
+ /**
84
+ * Represents the cid of ATProto DAG-CBOR data (like repository MST nodes).
85
+ * @see {@link https://atproto.com/specs/data-model#link-and-cid-formats ATproto Data Model - Link and CID Formats}
86
+ */
87
+ export type CborCid = Cid<1, DAG_CBOR_MULTICODEC, SHA256_MULTIHASH>;
88
+ export declare function isCborCid(cid: Cid): cid is CborCid;
89
+ export type CheckCidOptions = {
90
+ flavor?: 'raw' | 'cbor' | 'dasl';
91
+ };
92
+ export type InferCheckedCid<TOptions> = TOptions extends {
93
+ flavor: 'raw';
94
+ } ? RawCid : TOptions extends {
95
+ flavor: 'cbor';
96
+ } ? CborCid : Cid;
97
+ /**
98
+ * Type guard to check whether a {@link Cid} instance meets specific flavor
99
+ * constraints.
100
+ */
101
+ export declare function checkCid<TOptions extends CheckCidOptions>(cid: Cid, options: TOptions): cid is InferCheckedCid<TOptions>;
102
+ export declare function checkCid(cid: Cid, options?: CheckCidOptions): boolean;
103
+ /**
104
+ * Type guard to check whether a value is a valid {@link Cid} instance,
105
+ * optionally checking for specific flavor constraints.
106
+ */
107
+ export declare function isCid<TOptions extends CheckCidOptions>(value: unknown, options: TOptions): value is InferCheckedCid<TOptions>;
108
+ export declare function isCid(value: unknown, options?: CheckCidOptions): value is Cid;
109
+ /**
110
+ * Returns the input value as a {@link Cid} if it is valid, or `null` otherwise.
111
+ */
112
+ export declare function ifCid<TValue, TOptions extends CheckCidOptions>(value: unknown, options: TOptions): (TValue & InferCheckedCid<TOptions>) | null;
113
+ export declare function ifCid<TValue>(value: TValue, options?: CheckCidOptions): (TValue & Cid) | null;
114
+ /**
115
+ * Returns the input value as a {@link Cid} if it is valid.
116
+ *
117
+ * @throws if the input is not a valid {@link Cid}.
118
+ */
119
+ export declare function asCid<TValue, TOptions extends CheckCidOptions>(value: TValue, options: TOptions): TValue & InferCheckedCid<TOptions>;
120
+ export declare function asCid<TValue>(value: TValue, options?: CheckCidOptions): Cid & TValue;
121
+ /**
122
+ * Decodes a CID from its binary representation.
123
+ *
124
+ * @see {@link https://dasl.ing/cid.html DASL-CIDs}
125
+ * @throws if the input do not represent a valid DASL {@link Cid}
126
+ */
127
+ export declare function decodeCid<TOptions extends CheckCidOptions>(cidBytes: Uint8Array, options: TOptions): InferCheckedCid<TOptions>;
128
+ export declare function decodeCid(cidBytes: Uint8Array, options?: CheckCidOptions): Cid;
129
+ /**
130
+ * Parses a CID string into a Cid object.
131
+ *
132
+ * @throws if the input is not a valid CID string.
133
+ */
134
+ export declare function parseCid<TOptions extends CheckCidOptions>(input: string, options: TOptions): InferCheckedCid<TOptions>;
135
+ export declare function parseCid(input: string, options?: CheckCidOptions): Cid;
136
+ export declare function validateCidString(input: string, options?: CheckCidOptions): boolean;
137
+ export declare function parseCidSafe<TOptions extends CheckCidOptions>(input: string, options: TOptions): InferCheckedCid<TOptions> | null;
138
+ export declare function parseCidSafe(input: string, options?: CheckCidOptions): Cid | null;
139
+ export declare function ensureValidCidString(input: string, options?: CheckCidOptions): void;
140
+ /**
141
+ * Verifies whether the multihash of a given {@link cid} matches the hash of the provided {@link bytes}.
142
+ * @params cid The CID to match against the bytes.
143
+ * @params bytes The bytes to verify.
144
+ * @returns true if the CID matches the bytes, false otherwise.
145
+ */
146
+ export declare function isCidForBytes(cid: Cid, bytes: Uint8Array): Promise<boolean>;
147
+ export declare function createCid<TCode extends number, TMultihashCode extends number>(code: TCode, multihashCode: TMultihashCode, digest: Uint8Array): Cid<1, TCode, TMultihashCode>;
148
+ export declare function cidForCbor(bytes: Uint8Array): Promise<CborCid>;
149
+ export declare function cidForRawBytes(bytes: Uint8Array): Promise<RawCid>;
150
+ export declare function cidForRawHash(digest: Uint8Array): RawCid;
57
151
  //# sourceMappingURL=cid.d.ts.map
package/dist/cid.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"cid.d.ts","sourceRoot":"","sources":["../src/cid.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAEtC,eAAO,MAAM,mBAAmB,MAAO,CAAA;AACvC,eAAO,MAAM,kBAAkB,KAAO,CAAA;AAEtC,eAAO,MAAM,uBAAuB,KAAO,CAAA;AAE3C,MAAM,MAAM,eAAe,CAAC,IAAI,SAAS,MAAM,GAAG,MAAM,IAAI;IAC1D,IAAI,EAAE,IAAI,CAAA;IACV,MAAM,EAAE,UAAU,CAAA;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,UAAU,CAAA;CAClB,CAAA;AAED,OAAO,QAAQ,kBAAkB,CAAC;IAChC;;;;;;;;;;;;;;;;;;OAkBG;IACH,UAAU,GAAG;KAAG;CACjB;AAqBD,OAAO,EAAE,GAAG,EAAE,CAAA;AAEd;;;GAGG;AACH,MAAM,WAAW,GAAG;IAClB,OAAO,EAAE,CAAC,GAAG,CAAC,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,EAAE,eAAe,CAAA;IAC1B,KAAK,EAAE,UAAU,CAAA;IAC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BAAgB,YAAY,CAAC,QAAQ,SAAS,eAAe,EAC3D,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,QAAQ,GAChB,eAAe,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAA;AACnC,wBAAgB,YAAY,CAC1B,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,eAAe,GACxB,GAAG,GAAG,IAAI,CAAA;AAYb,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,eAAe,GACxB,IAAI,CAIN;AAED;;;;;GAKG;AACH,wBAAsB,aAAa,CACjC,GAAG,EAAE,GAAG,EACR,KAAK,EAAE,UAAU,GAChB,OAAO,CAAC,OAAO,CAAC,CAalB;AAED,wBAAgB,SAAS,CAAC,KAAK,SAAS,MAAM,EAAE,cAAc,SAAS,MAAM,EAC3E,IAAI,EAAE,KAAK,EACX,aAAa,EAAE,cAAc,EAC7B,MAAM,EAAE,UAAU,GAGJ,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,cAAc,CAAC,CAC5C;AAED,wBAAsB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CAGpE;AAED,wBAAsB,cAAc,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAGvE;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAMxD"}
package/dist/cid.js CHANGED
@@ -1,63 +1,215 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CID = exports.SHA2_256_MULTIHASH_CODE = exports.RAW_BIN_MULTICODEC = exports.DAG_CBOR_MULTICODEC = void 0;
3
+ exports.CID = exports.SHA512_MULTIHASH = exports.SHA256_MULTIHASH = exports.RAW_MULTICODEC = exports.DAG_CBOR_MULTICODEC = 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;
4
12
  exports.asCid = asCid;
5
- exports.parseCid = parseCid;
6
13
  exports.decodeCid = decodeCid;
7
- exports.createCid = createCid;
8
- exports.isCid = isCid;
14
+ exports.parseCid = parseCid;
9
15
  exports.validateCidString = validateCidString;
10
- exports.parseCidString = parseCidString;
16
+ exports.parseCidSafe = parseCidSafe;
11
17
  exports.ensureValidCidString = ensureValidCidString;
18
+ exports.isCidForBytes = isCidForBytes;
19
+ exports.createCid = createCid;
20
+ exports.cidForCbor = cidForCbor;
21
+ exports.cidForRawBytes = cidForRawBytes;
22
+ exports.cidForRawHash = cidForRawHash;
12
23
  const cid_1 = require("multiformats/cid");
13
24
  Object.defineProperty(exports, "CID", { enumerable: true, get: function () { return cid_1.CID; } });
14
- exports.DAG_CBOR_MULTICODEC = 0x71;
15
- exports.RAW_BIN_MULTICODEC = 0x55;
16
- exports.SHA2_256_MULTIHASH_CODE = 0x12;
17
- function asCid(value) {
18
- return cid_1.CID.asCID(value);
25
+ const digest_1 = require("multiformats/hashes/digest");
26
+ const sha2_1 = require("multiformats/hashes/sha2");
27
+ const object_js_1 = require("./object.js");
28
+ const uint8array_js_1 = require("./uint8array.js");
29
+ exports.DAG_CBOR_MULTICODEC = 0x71; // DRISL conformant DAG-CBOR
30
+ exports.RAW_MULTICODEC = 0x55; // raw binary codec used in DASL CIDs
31
+ exports.SHA256_MULTIHASH = sha2_1.sha256.code;
32
+ exports.SHA512_MULTIHASH = sha2_1.sha512.code;
33
+ function multihashEquals(a, b) {
34
+ return a.code === b.code && (0, uint8array_js_1.ui8Equals)(a.digest, b.digest);
19
35
  }
20
- function parseCid(input) {
21
- return cid_1.CID.parse(input);
36
+ /**
37
+ * Converts a {@link Cid} to a multiformats {@link CID} instance.
38
+ *
39
+ * @deprecated Packages depending on `@atproto/lex-data` should use the
40
+ * {@link Cid} interface instead of relying on `multiformats`'s {@link CID}
41
+ * implementation directly. This is to avoid compatibility issues, and in order
42
+ * to allow better portability, compatibility and future updates.
43
+ */
44
+ function asMultiformatsCID(input) {
45
+ const cid =
46
+ // Already a multiformats CID instance
47
+ cid_1.CID.asCID(input) ??
48
+ // Create a new multiformats CID instance
49
+ cid_1.CID.create(input.version, input.code, (0, digest_1.create)(input.multihash.code, input.multihash.digest));
50
+ // @NOTE: the "satisfies" operator is used here to ensure that the Cid
51
+ // interface is indeed compatible with multiformats' CID implementation, which
52
+ // allows us to safely rely on multiformats' CID implementation where Cid are
53
+ // needed.
54
+ return cid;
22
55
  }
23
- function decodeCid(bytes) {
24
- return cid_1.CID.decode(bytes);
56
+ function isRawCid(cid) {
57
+ return cid.version === 1 && cid.code === exports.RAW_MULTICODEC;
25
58
  }
26
- function createCid(code, digest) {
27
- return cid_1.CID.createV1(code, digest);
59
+ function isDaslCid(cid) {
60
+ return (cid.version === 1 &&
61
+ (cid.code === exports.RAW_MULTICODEC || cid.code === exports.DAG_CBOR_MULTICODEC) &&
62
+ cid.multihash.code === exports.SHA256_MULTIHASH &&
63
+ cid.multihash.digest.byteLength === 32 // Should always be 32 bytes (256 bits) for SHA-256, but double-checking anyways
64
+ );
28
65
  }
29
- function isCid(value, options) {
30
- const cid = asCid(value);
31
- if (!cid) {
32
- return false;
33
- }
34
- if (options?.strict) {
35
- if (cid.version !== 1) {
36
- return false;
37
- }
38
- if (cid.code !== exports.RAW_BIN_MULTICODEC && cid.code !== exports.DAG_CBOR_MULTICODEC) {
39
- return false;
40
- }
41
- if (cid.multihash.code !== exports.SHA2_256_MULTIHASH_CODE) {
42
- return false;
43
- }
66
+ function isCborCid(cid) {
67
+ return cid.code === exports.DAG_CBOR_MULTICODEC && isDaslCid(cid);
68
+ }
69
+ function checkCid(cid, options) {
70
+ switch (options?.flavor) {
71
+ case undefined:
72
+ return true;
73
+ case 'cbor':
74
+ return isCborCid(cid);
75
+ case 'dasl':
76
+ return isDaslCid(cid);
77
+ case 'raw':
78
+ return isRawCid(cid);
79
+ default:
80
+ throw new TypeError(`Unknown CID flavor: ${options?.flavor}`);
44
81
  }
45
- return true;
46
82
  }
47
- function validateCidString(input) {
48
- return parseCidString(input)?.toString() === input;
83
+ function isCid(value, options) {
84
+ return isCidImplementation(value) && checkCid(value, options);
85
+ }
86
+ function ifCid(value, options) {
87
+ if (isCidImplementation(value) && checkCid(value, options))
88
+ return value;
89
+ return null;
90
+ }
91
+ function asCid(value, options) {
92
+ if (isCidImplementation(value) && checkCid(value, options))
93
+ return value;
94
+ throw new Error('Not a valid CID');
49
95
  }
50
- function parseCidString(input) {
96
+ function decodeCid(cidBytes, options) {
97
+ const cid = cid_1.CID.decode(cidBytes);
98
+ return asCid(cid, options);
99
+ }
100
+ function parseCid(input, options) {
101
+ const cid = cid_1.CID.parse(input);
102
+ return asCid(cid, options);
103
+ }
104
+ function validateCidString(input, options) {
105
+ return parseCidSafe(input, options)?.toString() === input;
106
+ }
107
+ function parseCidSafe(input, options) {
51
108
  try {
52
- return parseCid(input);
109
+ return parseCid(input, options);
53
110
  }
54
111
  catch {
55
- return undefined;
112
+ return null;
56
113
  }
57
114
  }
58
- function ensureValidCidString(input) {
59
- if (!validateCidString(input)) {
115
+ function ensureValidCidString(input, options) {
116
+ if (!validateCidString(input, options)) {
60
117
  throw new Error(`Invalid CID string`);
61
118
  }
62
119
  }
120
+ /**
121
+ * Verifies whether the multihash of a given {@link cid} matches the hash of the provided {@link bytes}.
122
+ * @params cid The CID to match against the bytes.
123
+ * @params bytes The bytes to verify.
124
+ * @returns true if the CID matches the bytes, false otherwise.
125
+ */
126
+ async function isCidForBytes(cid, bytes) {
127
+ if (cid.multihash.code === sha2_1.sha256.code) {
128
+ const multihash = await sha2_1.sha256.digest(bytes);
129
+ return multihashEquals(multihash, cid.multihash);
130
+ }
131
+ if (cid.multihash.code === sha2_1.sha512.code) {
132
+ const multihash = await sha2_1.sha512.digest(bytes);
133
+ return multihashEquals(multihash, cid.multihash);
134
+ }
135
+ // Don't know how to verify other multihash codes
136
+ throw new Error('Unsupported CID multihash');
137
+ }
138
+ function createCid(code, multihashCode, digest) {
139
+ const cid = cid_1.CID.createV1(code, (0, digest_1.create)(multihashCode, digest));
140
+ return cid;
141
+ }
142
+ async function cidForCbor(bytes) {
143
+ const multihash = await sha2_1.sha256.digest(bytes);
144
+ return cid_1.CID.createV1(exports.DAG_CBOR_MULTICODEC, multihash);
145
+ }
146
+ async function cidForRawBytes(bytes) {
147
+ const multihash = await sha2_1.sha256.digest(bytes);
148
+ return cid_1.CID.createV1(exports.RAW_MULTICODEC, multihash);
149
+ }
150
+ function cidForRawHash(digest) {
151
+ // Fool-proofing
152
+ if (digest.length !== 32) {
153
+ throw new Error(`Invalid SHA-256 hash length: ${digest.length}`);
154
+ }
155
+ return createCid(exports.RAW_MULTICODEC, sha2_1.sha256.code, digest);
156
+ }
157
+ /**
158
+ * @internal
159
+ */
160
+ function isCidImplementation(value) {
161
+ if (cid_1.CID.asCID(value)) {
162
+ // CIDs created using older multiformats versions did not have a "bytes"
163
+ // property.
164
+ return value.bytes != null;
165
+ }
166
+ else {
167
+ // Unknown implementation, do a structural check
168
+ try {
169
+ if (!(0, object_js_1.isObject)(value))
170
+ return false;
171
+ const val = value;
172
+ if (val.version !== 0 && val.version !== 1)
173
+ return false;
174
+ if (!isUint8(val.code))
175
+ return false;
176
+ if (!(0, object_js_1.isObject)(val.multihash))
177
+ return false;
178
+ const mh = val.multihash;
179
+ if (!isUint8(mh.code))
180
+ return false;
181
+ if (!(mh.digest instanceof Uint8Array))
182
+ return false;
183
+ // Ensure that the bytes array is consistent with other properties
184
+ if (!(val.bytes instanceof Uint8Array))
185
+ return false;
186
+ if (val.bytes[0] !== val.version)
187
+ return false;
188
+ if (val.bytes[1] !== val.code)
189
+ return false;
190
+ if (val.bytes[2] !== mh.code)
191
+ return false;
192
+ if (val.bytes[3] !== mh.digest.length)
193
+ return false;
194
+ if (val.bytes.length !== 4 + mh.digest.length)
195
+ return false;
196
+ if (!(0, uint8array_js_1.ui8Equals)(val.bytes.subarray(4), mh.digest))
197
+ return false;
198
+ if (typeof val.equals !== 'function')
199
+ return false;
200
+ if (val.equals(val) !== true)
201
+ return false;
202
+ return true;
203
+ }
204
+ catch {
205
+ return false;
206
+ }
207
+ }
208
+ }
209
+ /**
210
+ * @internal
211
+ */
212
+ function isUint8(val) {
213
+ return Number.isInteger(val) && val >= 0 && val < 256;
214
+ }
63
215
  //# sourceMappingURL=cid.js.map