@atproto/lex-data 0.1.3 → 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 +8 -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.test.ts DELETED
@@ -1,350 +0,0 @@
1
- import { CID } from 'multiformats/cid'
2
- import { sha256, sha512 } from 'multiformats/hashes/sha2'
3
- import { describe, expect, it } from 'vitest'
4
- import { BytesCid, createCustomCid } from './cid-implementation.test.js'
5
- import {
6
- CBOR_DATA_CODEC,
7
- Cid,
8
- RAW_DATA_CODEC,
9
- SHA256_HASH_CODE,
10
- asMultiformatsCID,
11
- cidForRawHash,
12
- decodeCid,
13
- ensureValidCidString,
14
- isCid,
15
- isCidForBytes,
16
- parseCid,
17
- parseCidSafe,
18
- } from './cid.js'
19
- import { ui8Equals } from './uint8array.js'
20
-
21
- const invalidCidStr = 'invalidcidstring'
22
-
23
- const cborCidStr = 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a'
24
- const cborCid = parseCid(cborCidStr, { flavor: 'cbor' })
25
-
26
- const rawCidStr = 'bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4'
27
- const rawCid = parseCid(rawCidStr, { flavor: 'raw' })
28
-
29
- const rawCidLike: Cid = createCustomCid(
30
- 1,
31
- RAW_DATA_CODEC,
32
- SHA256_HASH_CODE,
33
- rawCid.multihash.digest,
34
- )
35
- const rawBytesCid = new BytesCid(rawCid.bytes)
36
-
37
- describe(isCid, () => {
38
- describe('non-strict mode', () => {
39
- it('returns true for parsed CIDs', () => {
40
- expect(isCid(cborCid)).toBe(true)
41
- expect(isCid(rawCid)).toBe(true)
42
- })
43
-
44
- it('returns true for custom compatible CID implementations', () => {
45
- expect(isCid(rawCidLike)).toBe(true)
46
- expect(isCid(rawBytesCid)).toBe(true)
47
- })
48
-
49
- it('returns true for CID v0 and v1', async () => {
50
- const digest = await sha256.digest(Buffer.from('hello world'))
51
- const cidV0 = CID.createV0(digest)
52
- const cidV1 = CID.createV1(RAW_DATA_CODEC, digest)
53
- expect(isCid(cidV0)).toBe(true)
54
- expect(isCid(cidV1)).toBe(true)
55
- })
56
-
57
- it('returns false for invalid CIDs', () => {
58
- expect(isCid(new Date())).toBe(false)
59
- expect(isCid({})).toBe(false)
60
- expect(isCid('not a cid')).toBe(false)
61
- })
62
- })
63
-
64
- describe('flavors', () => {
65
- describe('raw', () => {
66
- it('validated "raw" cids', async () => {
67
- const digest = await sha256.digest(Buffer.from('hello world'))
68
- const cid = CID.createV1(RAW_DATA_CODEC, digest)
69
- expect(isCid(cid, { flavor: 'raw' })).toBe(true)
70
- })
71
-
72
- it('allows other hash algorithms', async () => {
73
- const digest = await sha512.digest(Buffer.from('hello world'))
74
- const cid = CID.createV1(RAW_DATA_CODEC, digest)
75
- expect(isCid(cid, { flavor: 'raw' })).toBe(true)
76
- })
77
-
78
- it('rejects CID v0 when strict option is set', async () => {
79
- const digest = await sha256.digest(Buffer.from('hello world'))
80
- const cid = CID.createV0(digest)
81
- expect(isCid(cid, { flavor: 'raw' })).toBe(false)
82
- })
83
-
84
- it('rejects CIDs with invalid code', async () => {
85
- const digest = await sha256.digest(Buffer.from('hello world'))
86
- const cid = CID.createV1(3333, digest)
87
- expect(isCid(cid, { flavor: 'raw' })).toBe(false)
88
- })
89
- })
90
-
91
- describe('cbor', () => {
92
- it('validated "cbor" cids', async () => {
93
- const digest = await sha256.digest(Buffer.from('hello world'))
94
- const cid = CID.createV1(CBOR_DATA_CODEC, digest)
95
- expect(isCid(cid, { flavor: 'cbor' })).toBe(true)
96
- })
97
-
98
- it('rejects CIDs with invalid hash algorithm', async () => {
99
- const digest = await sha512.digest(Buffer.from('hello world'))
100
- const cid = CID.createV1(RAW_DATA_CODEC, digest)
101
- expect(isCid(cid, { flavor: 'cbor' })).toBe(false)
102
- })
103
-
104
- it('rejects CID v0 when strict option is set', async () => {
105
- const digest = await sha256.digest(Buffer.from('hello world'))
106
- const cid = CID.createV0(digest)
107
- expect(isCid(cid, { flavor: 'cbor' })).toBe(false)
108
- })
109
-
110
- it('rejects CIDs with invalid code', async () => {
111
- const digest = await sha256.digest(Buffer.from('hello world'))
112
- const cid = CID.createV1(3333, digest)
113
- expect(isCid(cid, { flavor: 'cbor' })).toBe(false)
114
- })
115
- })
116
- })
117
-
118
- describe('alternative cid implementations', () => {
119
- it('accepts compatible CID implementations', () => {
120
- expect(isCid(rawCidLike)).toBe(true)
121
- })
122
-
123
- it('rejects non-matching version', () => {
124
- expect(isCid({ ...rawCidLike, version: 0 })).toBe(false)
125
- })
126
-
127
- it('rejects non-matching code', () => {
128
- expect(isCid({ ...rawCidLike, code: -1 })).toBe(false)
129
- expect(isCid({ ...rawCidLike, code: 0 })).toBe(false)
130
- expect(isCid({ ...rawCidLike, code: 256 })).toBe(false)
131
- })
132
-
133
- it('rejects invalid bytes property', () => {
134
- expect(isCid({ ...rawCidLike, bytes: undefined })).toBe(false)
135
- expect(isCid({ ...rawCidLike, bytes: 12 })).toBe(false)
136
- expect(isCid({ ...rawCidLike, bytes: {} })).toBe(false)
137
- expect(isCid({ ...rawCidLike, bytes: [] })).toBe(false)
138
-
139
- expect(
140
- isCid({
141
- ...rawCidLike,
142
- bytes: rawCidLike.bytes.subarray(0, rawCidLike.bytes.length - 1),
143
- }),
144
- ).toBe(false)
145
-
146
- const bytes = new Uint8Array(rawCidLike.bytes.length)
147
-
148
- bytes.set(rawCidLike.bytes)
149
- expect(isCid({ ...rawCidLike, bytes })).toBe(true)
150
-
151
- bytes[0] = bytes[0] ^ 0xff
152
- expect(isCid({ ...rawCidLike, bytes })).toBe(false)
153
- bytes.set(rawCidLike.bytes)
154
-
155
- bytes[3] = bytes[3] ^ 0xff
156
- expect(isCid({ ...rawCidLike, bytes })).toBe(false)
157
- bytes.set(rawCidLike.bytes)
158
-
159
- bytes[6] = bytes[6] ^ 0xff
160
- expect(isCid({ ...rawCidLike, bytes })).toBe(false)
161
- bytes.set(rawCidLike.bytes)
162
- })
163
-
164
- describe('multihash property', () => {
165
- it('rejects non-matching object', () => {
166
- expect(isCid({ ...rawCidLike, multihash: undefined })).toBe(false)
167
- expect(isCid({ ...rawCidLike, multihash: 12 })).toBe(false)
168
- expect(isCid({ ...rawCidLike, multihash: {} })).toBe(false)
169
- expect(isCid({ ...rawCidLike, multihash: [] })).toBe(false)
170
- })
171
-
172
- it('rejects non-matching code', () => {
173
- expect(
174
- isCid({
175
- ...rawCidLike,
176
- multihash: { ...rawCidLike.multihash, code: -1 },
177
- }),
178
- ).toBe(false)
179
- expect(
180
- isCid({
181
- ...rawCidLike,
182
- multihash: { ...rawCidLike.multihash, code: 0 },
183
- }),
184
- ).toBe(false)
185
- expect(
186
- isCid({
187
- ...rawCidLike,
188
- multihash: { ...rawCidLike.multihash, code: 256 },
189
- }),
190
- ).toBe(false)
191
- })
192
-
193
- it('rejects non Uint8Array digest', () => {
194
- expect(
195
- isCid({
196
- ...rawCidLike,
197
- multihash: { ...rawCidLike.multihash, digest: new Array(32) },
198
- }),
199
- ).toBe(false)
200
- })
201
-
202
- it('rejects non Uint8Array digest', () => {
203
- expect(
204
- isCid({
205
- ...rawCidLike,
206
- multihash: { ...rawCidLike.multihash, digest: new Array(32) },
207
- }),
208
- ).toBe(false)
209
- })
210
-
211
- it('rejects non-matching digest', () => {
212
- const differentDigest = new Uint8Array(32)
213
- differentDigest[0] = 1
214
- expect(
215
- isCid({
216
- ...rawCidLike,
217
- multihash: { ...rawCidLike.multihash, digest: differentDigest },
218
- }),
219
- ).toBe(false)
220
- })
221
- })
222
-
223
- describe('equals() method', () => {
224
- it('rejects objects without equals method', () => {
225
- expect(isCid({ ...rawCidLike, equals: undefined })).toBe(false)
226
- expect(isCid({ ...rawCidLike, equals: () => false })).toBe(false)
227
- })
228
-
229
- it('rejects object with throwing equals method', () => {
230
- expect(
231
- isCid({
232
- ...rawCidLike,
233
- equals: () => {
234
- throw new Error('fail')
235
- },
236
- }),
237
- ).toBe(false)
238
- })
239
- })
240
- })
241
- })
242
-
243
- describe(decodeCid, () => {
244
- it('decodes CID from bytes', () => {
245
- const cid = parseCid(cborCidStr)
246
- const bytes = cid.bytes
247
- const decodedCid = decodeCid(bytes)
248
- expect(decodedCid.toString()).toBe(cborCidStr)
249
- })
250
- })
251
-
252
- describe(parseCid, () => {
253
- it('parses valid CIDs', () => {
254
- expect(parseCid(cborCidStr).toString()).toBe(cborCidStr)
255
- expect(parseCid(rawCidStr).toString()).toBe(rawCidStr)
256
- })
257
-
258
- it('throws for invalid CIDs', () => {
259
- expect(() => parseCid(invalidCidStr)).toThrow()
260
- })
261
- })
262
-
263
- describe(isCidForBytes, () => {
264
- describe('raw', () => {
265
- it('returns true for valid raw CID bytes', async () => {
266
- for (const hasher of [sha256, sha512]) {
267
- const data = new TextEncoder().encode('hello world')
268
- const digest = await hasher.digest(data)
269
- const cid = CID.createV1(RAW_DATA_CODEC, digest)
270
- expect(await isCidForBytes(cid, data)).toBe(true)
271
-
272
- data[0] = data[0] ^ 0xff
273
- expect(await isCidForBytes(cid, data)).toBe(false)
274
- }
275
- })
276
- })
277
-
278
- describe('cbor', () => {
279
- it('returns true for valid cbor CID bytes', async () => {
280
- for (const hasher of [sha256, sha512]) {
281
- // @NOTE this is not valid CBOR, but sufficient for testing the hash
282
- const data = new TextEncoder().encode('hello world')
283
- const digest = await hasher.digest(data)
284
- const cid = CID.createV1(CBOR_DATA_CODEC, digest)
285
- expect(await isCidForBytes(cid, data)).toBe(true)
286
-
287
- data[0] = data[0] ^ 0xff
288
- expect(await isCidForBytes(cid, data)).toBe(false)
289
- }
290
- })
291
- })
292
- })
293
-
294
- describe(parseCidSafe, () => {
295
- it('parses valid CIDs', () => {
296
- expect(parseCidSafe(cborCidStr)?.toString()).toBe(cborCidStr)
297
- expect(parseCidSafe(rawCidStr)?.toString()).toBe(rawCidStr)
298
- })
299
-
300
- it('returns undefined for invalid CIDs', () => {
301
- expect(parseCidSafe(invalidCidStr)).toBeNull()
302
- })
303
- })
304
-
305
- describe(ensureValidCidString, () => {
306
- it('does not throw for valid CIDs', () => {
307
- expect(() => ensureValidCidString(cborCidStr)).not.toThrow()
308
- })
309
-
310
- it('throws for invalid CIDs', () => {
311
- expect(() => ensureValidCidString(invalidCidStr)).toThrow(
312
- 'Invalid CID string',
313
- )
314
- })
315
- })
316
-
317
- describe(cidForRawHash, () => {
318
- it('creates a RawCid from a SHA-256 hash', () => {
319
- const hash = new Uint8Array(32)
320
- const cid = cidForRawHash(hash)
321
- expect(cid.code).toBe(RAW_DATA_CODEC)
322
- expect(cid.multihash.code).toBe(SHA256_HASH_CODE)
323
- expect(ui8Equals(cid.multihash.digest, hash)).toBe(true)
324
- })
325
-
326
- it('rejects hashes on invalid lengths', () => {
327
- expect(() => cidForRawHash(new Uint8Array(31))).toThrow(
328
- 'Invalid SHA-256 hash length',
329
- )
330
- expect(() => cidForRawHash(new Uint8Array(33))).toThrow(
331
- 'Invalid SHA-256 hash length',
332
- )
333
- })
334
- })
335
-
336
- describe(asMultiformatsCID, () => {
337
- it('converts compatible CID to multiformats CID', () => {
338
- for (const cid of [cborCid, rawCid, rawCidLike, rawBytesCid]) {
339
- expect(asMultiformatsCID(cid)).toBeInstanceOf(CID)
340
- expect(asMultiformatsCID(cid)).toMatchObject({
341
- version: cid.version,
342
- code: cid.code,
343
- multihash: {
344
- code: cid.multihash.code,
345
- digest: cid.multihash.digest,
346
- },
347
- })
348
- }
349
- })
350
- })