@atproto/lex-data 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/blob.d.ts +2 -2
  3. package/dist/blob.d.ts.map +1 -1
  4. package/dist/blob.js +1 -1
  5. package/dist/blob.js.map +1 -1
  6. package/dist/lex-equals.d.ts +1 -1
  7. package/dist/lex-equals.d.ts.map +1 -1
  8. package/dist/lex-equals.js.map +1 -1
  9. package/dist/lex.d.ts +1 -1
  10. package/dist/lex.d.ts.map +1 -1
  11. package/dist/lex.js.map +1 -1
  12. package/dist/lib/nodejs-buffer.js.map +1 -1
  13. package/dist/uint8array-from-base64.d.ts +1 -1
  14. package/dist/uint8array-from-base64.d.ts.map +1 -1
  15. package/dist/uint8array-from-base64.js.map +1 -1
  16. package/dist/uint8array-to-base64.d.ts +1 -1
  17. package/dist/uint8array-to-base64.d.ts.map +1 -1
  18. package/dist/uint8array-to-base64.js.map +1 -1
  19. package/dist/uint8array.d.ts +1 -1
  20. package/dist/uint8array.d.ts.map +1 -1
  21. package/dist/uint8array.js.map +1 -1
  22. package/dist/utf8-from-base64.d.ts +1 -1
  23. package/dist/utf8-from-base64.d.ts.map +1 -1
  24. package/dist/utf8-from-base64.js.map +1 -1
  25. package/dist/utf8-to-base64.d.ts +1 -1
  26. package/dist/utf8-to-base64.d.ts.map +1 -1
  27. package/dist/utf8-to-base64.js.map +1 -1
  28. package/dist/utf8.d.ts +1 -1
  29. package/dist/utf8.d.ts.map +1 -1
  30. package/dist/utf8.js.map +1 -1
  31. package/package.json +4 -8
  32. package/src/blob.test.ts +0 -405
  33. package/src/blob.ts +0 -478
  34. package/src/cid-implementation.test.ts +0 -129
  35. package/src/cid.test.ts +0 -350
  36. package/src/cid.ts +0 -603
  37. package/src/core-js.d.ts +0 -2
  38. package/src/index.ts +0 -8
  39. package/src/lex-equals.test.ts +0 -183
  40. package/src/lex-equals.ts +0 -123
  41. package/src/lex-error.test.ts +0 -54
  42. package/src/lex-error.ts +0 -83
  43. package/src/lex.test.ts +0 -279
  44. package/src/lex.ts +0 -253
  45. package/src/lib/nodejs-buffer.ts +0 -46
  46. package/src/lib/util.test.ts +0 -49
  47. package/src/lib/util.ts +0 -7
  48. package/src/object.test.ts +0 -80
  49. package/src/object.ts +0 -83
  50. package/src/uint8array-base64.ts +0 -2
  51. package/src/uint8array-concat.test.ts +0 -197
  52. package/src/uint8array-concat.ts +0 -25
  53. package/src/uint8array-from-base64.test.ts +0 -130
  54. package/src/uint8array-from-base64.ts +0 -98
  55. package/src/uint8array-to-base64.test.ts +0 -170
  56. package/src/uint8array-to-base64.ts +0 -55
  57. package/src/uint8array.test.ts +0 -503
  58. package/src/uint8array.ts +0 -197
  59. package/src/utf8-from-base64.test.ts +0 -39
  60. package/src/utf8-from-base64.ts +0 -23
  61. package/src/utf8-from-bytes.test.ts +0 -43
  62. package/src/utf8-from-bytes.ts +0 -21
  63. package/src/utf8-grapheme-len.test.ts +0 -38
  64. package/src/utf8-grapheme-len.ts +0 -21
  65. package/src/utf8-len.test.ts +0 -21
  66. package/src/utf8-len.ts +0 -51
  67. package/src/utf8-to-base64.test.ts +0 -35
  68. package/src/utf8-to-base64.ts +0 -22
  69. package/src/utf8.ts +0 -128
  70. package/tsconfig.build.json +0 -12
  71. package/tsconfig.json +0 -7
  72. package/tsconfig.tests.json +0 -8
package/src/cid.ts DELETED
@@ -1,603 +0,0 @@
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'
7
-
8
- /**
9
- * Codec code that indicates the CID references a CBOR-encoded data structure.
10
- *
11
- * Used when encoding structured data in AT Protocol repositories.
12
- *
13
- * @see {@link https://dasl.ing/cid.html Content IDs (DASL)}
14
- */
15
- export const CBOR_DATA_CODEC = 0x71
16
- export type CBOR_DATA_CODEC = typeof CBOR_DATA_CODEC
17
-
18
- /**
19
- * Codec code that indicates the CID references raw binary data (like media blobs).
20
- *
21
- * Used in DASL CIDs for binary blobs like images and media.
22
- *
23
- * @see {@link https://dasl.ing/cid.html Content IDs (DASL)}
24
- */
25
- export const RAW_DATA_CODEC = 0x55
26
- export type RAW_DATA_CODEC = typeof RAW_DATA_CODEC
27
-
28
- /**
29
- * Hash code that indicates that a CID uses SHA-256.
30
- */
31
- export const SHA256_HASH_CODE = sha256.code
32
- export type SHA256_HASH_CODE = typeof SHA256_HASH_CODE
33
-
34
- /**
35
- * Hash code that indicates that a CID uses SHA-512.
36
- */
37
- export const SHA512_HASH_CODE = sha512.code
38
- export type SHA512_HASH_CODE = typeof SHA512_HASH_CODE
39
-
40
- /**
41
- * Represent the hash part of a CID, which includes the hash algorithm code and
42
- * the raw digest bytes.
43
- *
44
- * @see {@link https://dasl.ing/cid.html Content IDs (DASL)}
45
- */
46
- export interface Multihash<THashCode extends number = number> {
47
- /**
48
- * Code of the hash algorithm (e.g., SHA256_HASH_CODE).
49
- */
50
- code: THashCode
51
-
52
- /**
53
- * Raw digest bytes.
54
- */
55
- digest: Uint8Array
56
- }
57
-
58
- /**
59
- * Compares two {@link Multihash} for equality.
60
- *
61
- * @param a - First {@link Multihash}
62
- * @param b - Second {@link Multihash}
63
- * @returns `true` if both multihashes have the same code and digest
64
- */
65
- export function multihashEquals(a: Multihash, b: Multihash): boolean {
66
- if (a === b) return true
67
- return a.code === b.code && ui8Equals(a.digest, b.digest)
68
- }
69
-
70
- declare module 'multiformats/cid' {
71
- /**
72
- * @deprecated use the {@link Cid} interface from `@atproto/lex-data`, and
73
- * related helpers ({@link isCid}, {@link ifCid}, {@link asCid},
74
- * {@link parseCid}, {@link decodeCid}), instead.
75
- *
76
- * This is marked as deprecated because we want to discourage direct usage of
77
- * `multiformats/cid` in dependent packages, and instead have them rely on the
78
- * {@link Cid} interface from `@atproto/lex-data`. The {@link CID} class from
79
- * `multiformats` version <10 has compatibility issues with certain TypeScript
80
- * configuration, which can lead to type errors in dependent packages.
81
- *
82
- * We are stuck with version 9 because `@atproto` packages did not drop
83
- * CommonJS support yet, and multiformats version 10 only supports ES modules.
84
- *
85
- * In order to avoid compatibility issues, while preparing for future breaking
86
- * changes (CID in multiformats v10+ has a slightly different interface), as
87
- * we update or swap out `multiformats`, `@atproto/lex-data` provides its own
88
- * stable {@link Cid} interface.
89
- */
90
- // eslint-disable-next-line @typescript-eslint/no-empty-object-type
91
- interface CID {}
92
- }
93
-
94
- // multiformats' CID class is not very portable because:
95
- //
96
- // - In dependent packages that use "moduleResolution" set to "node16",
97
- // "nodenext" or "bundler", TypeScript fails to properly resolve the
98
- // multiformats package when importing CID from @atproto/lex-data. This causes
99
- // type errors in those packages. This is caused by the fact that the
100
- // multiformats version <10 (which is the last version that supports CommonJS)
101
- // uses "exports" field in package.json, which do not contain "types"
102
- // entrypoints.
103
- // https://www.npmjs.com/package/multiformats/v/9.9.0?activeTab=code
104
- // - By defining our own interface and helper functions, we can have more
105
- // control over the public API exposed by this package.
106
- // - It allow us to have a stable interface in case we need to swap out, or
107
- // eventually update multiformats (should we choose to drop CommonJS support)
108
- // in the future.
109
-
110
- // @NOTE Even though it is not portable, we still re-export CID here so that
111
- // dependent packages where it can be used, have access to it (instead of
112
- // importing directly from "multiformats" or "multiformats/cid").
113
- export { /** @deprecated */ CID }
114
-
115
- /**
116
- * Converts a {@link Cid} to a multiformats {@link CID} instance.
117
- *
118
- * @deprecated Packages depending on `@atproto/lex-data` should use the
119
- * {@link Cid} interface instead of relying on `multiformats`'s {@link CID}
120
- * implementation directly. This is to avoid compatibility issues, and in order
121
- * to allow better portability, compatibility and future updates.
122
- */
123
- export function asMultiformatsCID<
124
- TVersion extends 0 | 1 = 0 | 1,
125
- TCodec extends number = number,
126
- THashCode extends number = number,
127
- >(input: Cid<TVersion, TCodec, THashCode>) {
128
- const cid =
129
- // Already a multiformats CID instance
130
- CID.asCID(input) ??
131
- // Create a new multiformats CID instance
132
- CID.create(
133
- input.version,
134
- input.code,
135
- createDigest(input.multihash.code, input.multihash.digest),
136
- )
137
-
138
- // @NOTE: the "satisfies" operator is used here to ensure that the Cid
139
- // interface is indeed compatible with multiformats' CID implementation, which
140
- // allows us to safely rely on multiformats' CID implementation where Cid are
141
- // needed.
142
- return cid satisfies Cid as CID & Cid<TVersion, TCodec, THashCode>
143
- }
144
-
145
- /**
146
- * Content Identifier (CID) for addressing content by its hash.
147
- *
148
- * CIDs are self-describing content addresses used throughout AT Protocol for
149
- * linking to data by its cryptographic hash. This interface provides a
150
- * stable API that is compatible with the `multiformats` library but avoids
151
- * direct dependency issues.
152
- *
153
- * @typeParam TVersion - CID version (0 or 1)
154
- * @typeParam TCodec - Multicodec content type code
155
- * @typeParam THashCode - Multihash algorithm code
156
- *
157
- * @example
158
- * ```typescript
159
- * import type { Cid } from '@atproto/lex-data'
160
- * import { parseCid, isCid } from '@atproto/lex-data'
161
- *
162
- * // Parse a CID from a string
163
- * const cid: Cid = parseCid('bafyreib...')
164
- *
165
- * // Check if a value is a CID
166
- * if (isCid(value)) {
167
- * console.log(cid.toString())
168
- * }
169
- * ```
170
- *
171
- * @see {@link isCid} to check if a value is a valid CID
172
- * @see {@link parseCid} to parse a CID from a string
173
- * @see {@link decodeCid} to decode a CID from bytes
174
- * @see {@link https://dasl.ing/cid.html Content IDs (DASL)}
175
- */
176
- export interface Cid<
177
- TVersion extends 0 | 1 = 0 | 1,
178
- TCodec extends number = number,
179
- THashCode extends number = number,
180
- > {
181
- // @NOTE This interface is compatible with multiformats' CID implementation
182
- // which we are using under the hood.
183
-
184
- /** CID version (0 or 1). AT Protocol uses CIDv1. */
185
- readonly version: TVersion
186
- /** Coded (e.g., {@link CBOR_DATA_CODEC}, {@link RAW_DATA_CODEC}). */
187
- readonly code: TCodec
188
- /** The multihash containing the hash algorithm and digest. */
189
- readonly multihash: Multihash<THashCode>
190
-
191
- /**
192
- * Binary representation of the whole CID.
193
- */
194
- readonly bytes: Uint8Array
195
-
196
- /**
197
- * Compares this CID with another for equality.
198
- *
199
- * @param other - The CID to compare with
200
- * @returns `true` if the CIDs are equal
201
- */
202
- equals(other: Cid): boolean
203
-
204
- /**
205
- * Returns the string representation of this CID (base32 for v1, base58btc for v0).
206
- */
207
- toString(): string
208
- }
209
-
210
- /**
211
- * Represents the CID of raw binary data (like media blobs).
212
- *
213
- * The use of {@link SHA256_HASH_CODE} is recommended but not required for raw CIDs.
214
- *
215
- * @see {@link https://atproto.com/specs/data-model#link-and-cid-formats AT Protocol Data Model - Link and CID Formats}
216
- */
217
- export type RawCid = Cid<1, RAW_DATA_CODEC>
218
-
219
- /**
220
- * Type guard to check if a CID is a raw binary CID.
221
- *
222
- * @param cid - The CID to check
223
- * @returns `true` if the CID is a version 1 CID with raw multicodec
224
- */
225
- export function isRawCid(cid: Cid): cid is RawCid {
226
- return cid.version === 1 && cid.code === RAW_DATA_CODEC
227
- }
228
-
229
- /**
230
- * Represents a DASL compliant CID.
231
- *
232
- * DASL CIDs are version 1 CIDs using either raw or DAG-CBOR multicodec
233
- * with SHA-256 multihash.
234
- *
235
- * @see {@link https://dasl.ing/cid.html Content IDs (DASL)}
236
- */
237
- export type DaslCid = Cid<1, RAW_DATA_CODEC | CBOR_DATA_CODEC, SHA256_HASH_CODE>
238
-
239
- /**
240
- * Type guard to check if a CID is DASL compliant.
241
- *
242
- * @param cid - The CID to check
243
- * @returns `true` if the CID is DASL compliant (v1, raw/dag-cbor, sha256)
244
- */
245
- export function isDaslCid(cid: Cid): cid is DaslCid {
246
- return (
247
- cid.version === 1 &&
248
- (cid.code === RAW_DATA_CODEC || cid.code === CBOR_DATA_CODEC) &&
249
- cid.multihash.code === SHA256_HASH_CODE &&
250
- cid.multihash.digest.byteLength === 0x20 // Should always be 32 bytes (256 bits) for SHA-256, but double-checking anyways
251
- )
252
- }
253
-
254
- /**
255
- * Represents the CID of AT Protocol DAG-CBOR data (like repository MST nodes).
256
- *
257
- * CBOR CIDs are version 1 CIDs using DAG-CBOR multicodec with SHA-256 multihash.
258
- *
259
- * @see {@link https://atproto.com/specs/data-model#link-and-cid-formats AT Protocol Data Model - Link and CID Formats}
260
- */
261
- export type CborCid = Cid<1, CBOR_DATA_CODEC, SHA256_HASH_CODE>
262
-
263
- /**
264
- * Type guard to check if a CID is a DAG-CBOR CID.
265
- *
266
- * @param cid - The CID to check
267
- * @returns `true` if the CID is a DAG-CBOR CID (v1, dag-cbor, sha256)
268
- */
269
- export function isCborCid(cid: Cid): cid is CborCid {
270
- return cid.code === CBOR_DATA_CODEC && isDaslCid(cid)
271
- }
272
-
273
- /**
274
- * Options for checking CID flavor constraints.
275
- */
276
- export type CheckCidOptions = {
277
- /**
278
- * The CID flavor to check for.
279
- * - `'raw'` - Raw binary CID ({@link RawCid})
280
- * - `'cbor'` - DAG-CBOR CID ({@link CborCid})
281
- * - `'dasl'` - DASL compliant CID ({@link DaslCid})
282
- */
283
- flavor?: 'raw' | 'cbor' | 'dasl'
284
- }
285
-
286
- /**
287
- * Infers the CID type based on check options.
288
- *
289
- * @typeParam TOptions - The options used for checking
290
- */
291
- export type InferCheckedCid<TOptions extends CheckCidOptions> =
292
- TOptions extends { flavor: 'raw' }
293
- ? RawCid
294
- : TOptions extends { flavor: 'cbor' }
295
- ? CborCid
296
- : Cid
297
-
298
- /**
299
- * Type guard to check whether a {@link Cid} instance meets specific flavor
300
- * constraints.
301
- */
302
- export function checkCid<TOptions extends CheckCidOptions>(
303
- cid: Cid,
304
- options: TOptions,
305
- ): cid is InferCheckedCid<TOptions>
306
- export function checkCid(cid: Cid, options?: CheckCidOptions): boolean
307
- export function checkCid(cid: Cid, options?: CheckCidOptions): boolean {
308
- switch (options?.flavor) {
309
- case undefined:
310
- return true
311
- case 'cbor':
312
- return isCborCid(cid)
313
- case 'dasl':
314
- return isDaslCid(cid)
315
- case 'raw':
316
- return isRawCid(cid)
317
- default:
318
- throw new TypeError(`Unknown CID flavor: ${options?.flavor}`)
319
- }
320
- }
321
-
322
- /**
323
- * Type guard to check whether a value is a valid {@link Cid} instance,
324
- * optionally checking for specific flavor constraints.
325
- */
326
- export function isCid<TOptions extends CheckCidOptions>(
327
- value: unknown,
328
- options: TOptions,
329
- ): value is InferCheckedCid<TOptions>
330
- export function isCid(value: unknown, options?: CheckCidOptions): value is Cid
331
- export function isCid(value: unknown, options?: CheckCidOptions): value is Cid {
332
- return isCidImplementation(value) && checkCid(value, options)
333
- }
334
-
335
- /**
336
- * Returns the input value as a {@link Cid} if it is valid, or `null` otherwise.
337
- */
338
- export function ifCid<TValue, TOptions extends CheckCidOptions>(
339
- value: unknown,
340
- options: TOptions,
341
- ): (TValue & InferCheckedCid<TOptions>) | null
342
- export function ifCid<TValue>(
343
- value: TValue,
344
- options?: CheckCidOptions,
345
- ): (TValue & Cid) | null
346
- export function ifCid(value: unknown, options?: CheckCidOptions): Cid | null {
347
- if (isCid(value, options)) return value
348
- return null
349
- }
350
-
351
- /**
352
- * Returns the input value as a {@link Cid} if it is valid.
353
- *
354
- * @throws if the input is not a valid {@link Cid}.
355
- */
356
- export function asCid<TValue, TOptions extends CheckCidOptions>(
357
- value: TValue,
358
- options: TOptions,
359
- ): TValue & InferCheckedCid<TOptions>
360
- export function asCid<TValue>(
361
- value: TValue,
362
- options?: CheckCidOptions,
363
- ): TValue & Cid
364
- export function asCid(value: unknown, options?: CheckCidOptions): Cid {
365
- if (isCid(value, options)) return value
366
- throw new Error(
367
- `Invalid ${options?.flavor ? `${options.flavor} CID` : 'CID'} "${value}"`,
368
- )
369
- }
370
-
371
- /**
372
- * Decodes a CID from its binary representation.
373
- *
374
- * @see {@link https://dasl.ing/cid.html DASL-CIDs}
375
- * @throws if the input do not represent a valid DASL {@link Cid}
376
- */
377
- export function decodeCid<TOptions extends CheckCidOptions>(
378
- cidBytes: Uint8Array,
379
- options: TOptions,
380
- ): InferCheckedCid<TOptions>
381
- export function decodeCid(cidBytes: Uint8Array, options?: CheckCidOptions): Cid
382
- export function decodeCid(
383
- cidBytes: Uint8Array,
384
- options?: CheckCidOptions,
385
- ): Cid {
386
- const cid = CID.decode(cidBytes)
387
- return asCid(cid, options)
388
- }
389
-
390
- /**
391
- * Parses a CID string into a Cid object.
392
- *
393
- * @throws if the input is not a valid CID string.
394
- */
395
- export function parseCid<TOptions extends CheckCidOptions>(
396
- input: string,
397
- options: TOptions,
398
- ): InferCheckedCid<TOptions>
399
- export function parseCid(input: string, options?: CheckCidOptions): Cid
400
- export function parseCid(input: string, options?: CheckCidOptions): Cid {
401
- const cid = CID.parse(input)
402
- return asCid(cid, options)
403
- }
404
-
405
- /**
406
- * Validates that a string is a valid CID representation.
407
- *
408
- * Unlike {@link parseCid}, this function returns a boolean instead of throwing.
409
- * It also verifies that the string is the canonical representation of the CID.
410
- *
411
- * @param input - The string to validate
412
- * @param options - Optional flavor constraints
413
- * @returns `true` if the string is a valid CID
414
- */
415
- export function validateCidString(
416
- input: string,
417
- options?: CheckCidOptions,
418
- ): boolean {
419
- return parseCidSafe(input, options)?.toString() === input
420
- }
421
-
422
- /**
423
- * Safely parses a CID string, returning `null` on failure instead of throwing.
424
- *
425
- * @param input - The string to parse
426
- * @param options - Optional flavor constraints
427
- * @returns The parsed CID, or `null` if parsing fails
428
- *
429
- * @example
430
- * ```typescript
431
- * import { parseCidSafe } from '@atproto/lex-data'
432
- *
433
- * const cid = parseCidSafe('bafyreib...')
434
- * if (cid) {
435
- * console.log(cid.toString())
436
- * }
437
- * ```
438
- */
439
- export function parseCidSafe<TOptions extends CheckCidOptions>(
440
- input: string,
441
- options: TOptions,
442
- ): InferCheckedCid<TOptions> | null
443
- export function parseCidSafe(
444
- input: string,
445
- options?: CheckCidOptions,
446
- ): Cid | null
447
- export function parseCidSafe(
448
- input: string,
449
- options?: CheckCidOptions,
450
- ): Cid | null {
451
- try {
452
- return parseCid(input, options)
453
- } catch {
454
- return null
455
- }
456
- }
457
-
458
- /**
459
- * Ensures that a string is a valid CID representation.
460
- *
461
- * @param input - The string to validate
462
- * @param options - Optional flavor constraints
463
- * @throws If the string is not a valid CID
464
- */
465
- export function ensureValidCidString(
466
- input: string,
467
- options?: CheckCidOptions,
468
- ): void {
469
- if (!validateCidString(input, options)) {
470
- throw new Error(`Invalid CID string "${input}"`)
471
- }
472
- }
473
-
474
- /**
475
- * Verifies whether the multihash of a given {@link cid} matches the hash of the provided {@link bytes}.
476
- * @params cid The CID to match against the bytes.
477
- * @params bytes The bytes to verify.
478
- * @returns true if the CID matches the bytes, false otherwise.
479
- */
480
- export async function isCidForBytes(
481
- cid: Cid,
482
- bytes: Uint8Array,
483
- ): Promise<boolean> {
484
- if (cid.multihash.code === sha256.code) {
485
- const multihash = await sha256.digest(bytes)
486
- return multihashEquals(multihash, cid.multihash)
487
- }
488
-
489
- if (cid.multihash.code === sha512.code) {
490
- const multihash = await sha512.digest(bytes)
491
- return multihashEquals(multihash, cid.multihash)
492
- }
493
-
494
- // Don't know how to verify other multihash codes
495
- throw new Error(
496
- `Unsupported CID multihash code: ${toHexString(cid.multihash.code)}`,
497
- )
498
- }
499
-
500
- /**
501
- * Creates a CID from a multicodec, multihash code, and digest.
502
- *
503
- * @param code - The multicodec content type code
504
- * @param multihashCode - The multihash algorithm code
505
- * @param digest - The raw hash digest bytes
506
- * @returns A new CIDv1 instance
507
- *
508
- * @example
509
- * ```typescript
510
- * import { createCid, RAW_DATA_CODEC, SHA256_HASH_CODE } from '@atproto/lex-data'
511
- *
512
- * const cid = createCid(RAW_DATA_CODEC, SHA256_HASH_CODE, hashDigest)
513
- * ```
514
- */
515
- export function createCid<TCodec extends number, THashCode extends number>(
516
- code: TCodec,
517
- multihashCode: THashCode,
518
- digest: Uint8Array,
519
- ) {
520
- const cid: Cid = CID.createV1(code, createDigest(multihashCode, digest))
521
- return cid as Cid<1, TCodec, THashCode>
522
- }
523
-
524
- /**
525
- * Creates a DAG-CBOR CID for the given CBOR bytes.
526
- *
527
- * Computes the SHA-256 hash of the bytes and creates a CIDv1 with DAG-CBOR multicodec.
528
- *
529
- * @param bytes - The CBOR-encoded bytes to hash
530
- * @returns A promise that resolves to the CborCid
531
- */
532
- export async function cidForCbor(bytes: Uint8Array): Promise<CborCid> {
533
- const multihash = await sha256.digest(bytes)
534
- return CID.createV1(CBOR_DATA_CODEC, multihash) as CborCid
535
- }
536
-
537
- /**
538
- * Creates a raw CID for the given binary bytes.
539
- *
540
- * Computes the SHA-256 hash of the bytes and creates a CIDv1 with raw multicodec.
541
- *
542
- * @param bytes - The raw binary bytes to hash
543
- * @returns A promise that resolves to the RawCid
544
- */
545
- export async function cidForRawBytes(bytes: Uint8Array): Promise<RawCid> {
546
- const multihash = await sha256.digest(bytes)
547
- return CID.createV1(RAW_DATA_CODEC, multihash) as RawCid
548
- }
549
-
550
- /**
551
- * Creates a raw CID from an existing SHA-256 hash digest.
552
- *
553
- * @param digest - The SHA-256 hash digest (must be 32 bytes)
554
- * @returns A RawCid with the given digest
555
- * @throws If the digest is not a valid SHA-256 hash (not 32 bytes)
556
- */
557
- export function cidForRawHash(digest: Uint8Array): RawCid {
558
- // Fool-proofing
559
- if (digest.length !== 0x20) {
560
- throw new Error(
561
- `Invalid SHA-256 hash length: ${toHexString(digest.length)}`,
562
- )
563
- }
564
- return createCid(RAW_DATA_CODEC, sha256.code, digest)
565
- }
566
-
567
- function isCidImplementation(value: unknown): value is Cid {
568
- if (CID.asCID(value)) {
569
- // CIDs created using older multiformats versions did not have a "bytes"
570
- // property.
571
- return (value as { bytes?: Uint8Array }).bytes != null
572
- } else {
573
- // Unknown implementation, do a structural check
574
- try {
575
- if (!isObject(value)) return false
576
-
577
- const val = value as Record<string, unknown>
578
- if (val.version !== 0 && val.version !== 1) return false
579
- if (!isUint8(val.code)) return false
580
-
581
- if (!isObject(val.multihash)) return false
582
- const mh = val.multihash as Record<string, unknown>
583
- if (!isUint8(mh.code)) return false
584
- if (!(mh.digest instanceof Uint8Array)) return false
585
-
586
- // Ensure that the bytes array is consistent with other properties
587
- if (!(val.bytes instanceof Uint8Array)) return false
588
- if (val.bytes[0] !== val.version) return false
589
- if (val.bytes[1] !== val.code) return false
590
- if (val.bytes[2] !== mh.code) return false
591
- if (val.bytes[3] !== mh.digest.length) return false
592
- if (val.bytes.length !== 4 + mh.digest.length) return false
593
- if (!ui8Equals(val.bytes.subarray(4), mh.digest)) return false
594
-
595
- if (typeof val.equals !== 'function') return false
596
- if (val.equals(val) !== true) return false
597
-
598
- return true
599
- } catch {
600
- return false
601
- }
602
- }
603
- }
package/src/core-js.d.ts DELETED
@@ -1,2 +0,0 @@
1
- declare module 'core-js/es/typed-array/from-base64.js'
2
- declare module 'core-js/es/typed-array/to-base64.js'
package/src/index.ts DELETED
@@ -1,8 +0,0 @@
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'