@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/src/cid.test.ts CHANGED
@@ -1,29 +1,56 @@
1
+ /* eslint-disable import/no-deprecated */
2
+
1
3
  import { CID } from 'multiformats/cid'
2
4
  import { sha256, sha512 } from 'multiformats/hashes/sha2'
3
5
  import { describe, expect, it } from 'vitest'
6
+ import { BytesCid, createCustomCid } from './cid-implementation.test.js'
4
7
  import {
5
- RAW_BIN_MULTICODEC,
6
- createCid,
8
+ Cid,
9
+ DAG_CBOR_MULTICODEC,
10
+ RAW_MULTICODEC,
11
+ SHA256_MULTIHASH,
12
+ asMultiformatsCID,
13
+ cidForRawHash,
7
14
  decodeCid,
8
15
  ensureValidCidString,
9
16
  isCid,
10
17
  parseCid,
11
- parseCidString,
18
+ parseCidSafe,
12
19
  } from './cid.js'
20
+ import { ui8Equals } from './uint8array.js'
21
+
22
+ const invalidCidStr = 'invalidcidstring'
23
+
24
+ const cborCidStr = 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a'
25
+ const cborCid = parseCid(cborCidStr, { flavor: 'cbor' })
26
+
27
+ const rawCidStr = 'bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4'
28
+ const rawCid = parseCid(rawCidStr, { flavor: 'raw' })
29
+
30
+ const rawCidCustom: Cid = createCustomCid(
31
+ 1,
32
+ RAW_MULTICODEC,
33
+ SHA256_MULTIHASH,
34
+ rawCid.multihash.digest,
35
+ )
36
+ const rawCidCustomBytes = new BytesCid(rawCid.bytes)
13
37
 
14
38
  describe(isCid, () => {
15
39
  describe('non-strict mode', () => {
16
40
  it('returns true for parsed CIDs', () => {
17
- const cid = parseCid(
18
- 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
19
- )
20
- expect(isCid(cid)).toBe(true)
41
+ expect(isCid(cborCid)).toBe(true)
42
+ expect(isCid(rawCid)).toBe(true)
43
+ })
44
+
45
+ it('returns true for custom compatible CID implementations', () => {
46
+ expect(isCid(rawCidCustom)).toBe(true)
47
+ expect(isCid(rawCidCustomBytes)).toBe(true)
21
48
  })
22
49
 
23
50
  it('returns true for CID v0 and v1', async () => {
24
51
  const digest = await sha256.digest(Buffer.from('hello world'))
25
52
  const cidV0 = CID.createV0(digest)
26
- const cidV1 = CID.createV1(RAW_BIN_MULTICODEC, digest)
53
+ const cidV1 = CID.createV1(RAW_MULTICODEC, digest)
27
54
  expect(isCid(cidV0)).toBe(true)
28
55
  expect(isCid(cidV1)).toBe(true)
29
56
  })
@@ -35,92 +62,184 @@ describe(isCid, () => {
35
62
  })
36
63
  })
37
64
 
38
- describe('strict mode', () => {
39
- it('returns true for valid CIDs in strict mode', async () => {
40
- const digest = await sha256.digest(Buffer.from('hello world'))
41
- const cid = CID.createV1(RAW_BIN_MULTICODEC, digest)
42
- expect(isCid(cid, { strict: true })).toBe(true)
65
+ describe('flavors', () => {
66
+ describe('raw', () => {
67
+ it('validated "raw" cids', async () => {
68
+ const digest = await sha256.digest(Buffer.from('hello world'))
69
+ const cid = CID.createV1(RAW_MULTICODEC, digest)
70
+ expect(isCid(cid, { flavor: 'raw' })).toBe(true)
71
+ })
72
+
73
+ it('allows other hash algorithms', async () => {
74
+ const digest = await sha512.digest(Buffer.from('hello world'))
75
+ const cid = CID.createV1(RAW_MULTICODEC, digest)
76
+ expect(isCid(cid, { flavor: 'raw' })).toBe(true)
77
+ })
78
+
79
+ it('rejects CID v0 when strict option is set', async () => {
80
+ const digest = await sha256.digest(Buffer.from('hello world'))
81
+ const cid = CID.createV0(digest)
82
+ expect(isCid(cid, { flavor: 'raw' })).toBe(false)
83
+ })
84
+
85
+ it('rejects CIDs with invalid code', async () => {
86
+ const digest = await sha256.digest(Buffer.from('hello world'))
87
+ const cid = CID.createV1(3333, digest)
88
+ expect(isCid(cid, { flavor: 'raw' })).toBe(false)
89
+ })
43
90
  })
44
91
 
45
- it('rejects CID v0 when strict option is set', async () => {
46
- const digest = await sha256.digest(Buffer.from('hello world'))
47
- const cid = CID.createV0(digest)
48
- expect(isCid(cid, { strict: true })).toBe(false)
92
+ describe('cbor', () => {
93
+ it('validated "cbor" cids', async () => {
94
+ const digest = await sha256.digest(Buffer.from('hello world'))
95
+ const cid = CID.createV1(DAG_CBOR_MULTICODEC, digest)
96
+ expect(isCid(cid, { flavor: 'cbor' })).toBe(true)
97
+ })
98
+
99
+ it('rejects CIDs with invalid hash algorithm', async () => {
100
+ const digest = await sha512.digest(Buffer.from('hello world'))
101
+ const cid = CID.createV1(RAW_MULTICODEC, digest)
102
+ expect(isCid(cid, { flavor: 'cbor' })).toBe(false)
103
+ })
104
+
105
+ it('rejects CID v0 when strict option is set', async () => {
106
+ const digest = await sha256.digest(Buffer.from('hello world'))
107
+ const cid = CID.createV0(digest)
108
+ expect(isCid(cid, { flavor: 'cbor' })).toBe(false)
109
+ })
110
+
111
+ it('rejects CIDs with invalid code', async () => {
112
+ const digest = await sha256.digest(Buffer.from('hello world'))
113
+ const cid = CID.createV1(3333, digest)
114
+ expect(isCid(cid, { flavor: 'cbor' })).toBe(false)
115
+ })
49
116
  })
117
+ })
50
118
 
51
- it('rejects CIDs with invalid hash algorithm', async () => {
52
- const digest = await sha512.digest(Buffer.from('hello world'))
53
- const cid = CID.createV1(RAW_BIN_MULTICODEC, digest)
54
- expect(isCid(cid, { strict: true })).toBe(false)
119
+ describe('alternative cid implementations', () => {
120
+ it('accepts compatible CID implementations', () => {
121
+ expect(isCid(rawCidCustom)).toBe(true)
55
122
  })
56
123
 
57
- it('rejects CIDs with invalid code', async () => {
58
- const digest = await sha256.digest(Buffer.from('hello world'))
59
- const cid = CID.createV1(3333, digest)
60
- expect(isCid(cid, { strict: true })).toBe(false)
124
+ it('rejects non-matching version', () => {
125
+ expect(isCid({ ...rawCidCustom, version: 0 })).toBe(false)
61
126
  })
62
- })
63
- })
64
127
 
65
- describe(createCid, () => {
66
- it('creates a valid CID v1', async () => {
67
- const digest = await sha256.digest(Buffer.from('hello world'))
68
- const cid = createCid(RAW_BIN_MULTICODEC, digest)
69
- expect(cid.version).toBe(1)
70
- expect(cid.code).toBe(RAW_BIN_MULTICODEC)
71
- expect(cid.multihash.code).toBe(sha256.code)
72
- expect(cid.multihash.digest).toEqual(digest.digest)
128
+ it('rejects non-matching code', () => {
129
+ expect(isCid({ ...rawCidCustom, code: 0 })).toBe(false)
130
+ })
131
+
132
+ it('rejects non-matching multihash code', () => {
133
+ expect(
134
+ isCid({
135
+ ...rawCidCustom,
136
+ multihash: { ...rawCidCustom.multihash, code: 0 },
137
+ }),
138
+ ).toBe(false)
139
+ })
140
+
141
+ it('rejects non-matching multihash digest', () => {
142
+ const differentDigest = new Uint8Array(32)
143
+ differentDigest[0] = 1
144
+ expect(
145
+ isCid({
146
+ ...rawCidCustom,
147
+ multihash: { ...rawCidCustom.multihash, digest: differentDigest },
148
+ }),
149
+ ).toBe(false)
150
+ })
151
+
152
+ it('rejects objects without equals method', () => {
153
+ expect(isCid({ ...rawCidCustom, equals: undefined })).toBe(false)
154
+ })
155
+
156
+ it('rejects object with throwing equals method', () => {
157
+ expect(
158
+ isCid({
159
+ ...rawCidCustom,
160
+ equals: () => {
161
+ throw new Error('fail')
162
+ },
163
+ }),
164
+ ).toBe(false)
165
+ })
73
166
  })
74
167
  })
75
168
 
76
169
  describe(decodeCid, () => {
77
170
  it('decodes CID from bytes', () => {
78
- const cidStr = 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a'
79
- const cid = parseCid(cidStr)
171
+ const cid = parseCid(cborCidStr)
80
172
  const bytes = cid.bytes
81
173
  const decodedCid = decodeCid(bytes)
82
- expect(decodedCid.toString()).toBe(cidStr)
174
+ expect(decodedCid.toString()).toBe(cborCidStr)
83
175
  })
84
176
  })
85
177
 
86
178
  describe(parseCid, () => {
87
179
  it('parses valid CIDs', () => {
88
- const cidStr = 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a'
89
- const cid = parseCid(cidStr)
90
- expect(cid.toString()).toBe(cidStr)
180
+ expect(parseCid(cborCidStr).toString()).toBe(cborCidStr)
181
+ expect(parseCid(rawCidStr).toString()).toBe(rawCidStr)
91
182
  })
92
183
 
93
184
  it('throws for invalid CIDs', () => {
94
- const invalidCidStr = 'invalidcidstring'
95
185
  expect(() => parseCid(invalidCidStr)).toThrow()
96
186
  })
97
187
  })
98
188
 
99
- describe(parseCidString, () => {
189
+ describe(parseCidSafe, () => {
100
190
  it('parses valid CIDs', () => {
101
- const cidStr = 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a'
102
- const cid = parseCidString(cidStr)
103
- expect(cid).toBeDefined()
104
- expect(cid!.toString()).toBe(cidStr)
191
+ expect(parseCidSafe(cborCidStr)?.toString()).toBe(cborCidStr)
192
+ expect(parseCidSafe(rawCidStr)?.toString()).toBe(rawCidStr)
105
193
  })
106
194
 
107
195
  it('returns undefined for invalid CIDs', () => {
108
- const invalidCidStr = 'invalidcidstring'
109
- const cid = parseCidString(invalidCidStr)
110
- expect(cid).toBeUndefined()
196
+ expect(parseCidSafe(invalidCidStr)).toBeNull()
111
197
  })
112
198
  })
113
199
 
114
200
  describe(ensureValidCidString, () => {
115
201
  it('does not throw for valid CIDs', () => {
116
- const cidStr = 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a'
117
- expect(() => ensureValidCidString(cidStr)).not.toThrow()
202
+ expect(() => ensureValidCidString(cborCidStr)).not.toThrow()
118
203
  })
119
204
 
120
205
  it('throws for invalid CIDs', () => {
121
- const invalidCidStr = 'invalidcidstring'
122
206
  expect(() => ensureValidCidString(invalidCidStr)).toThrow(
123
207
  'Invalid CID string',
124
208
  )
125
209
  })
126
210
  })
211
+
212
+ describe(cidForRawHash, () => {
213
+ it('creates a RawCid from a SHA-256 hash', () => {
214
+ const hash = new Uint8Array(32)
215
+ const cid = cidForRawHash(hash)
216
+ expect(cid.code).toBe(RAW_MULTICODEC)
217
+ expect(cid.multihash.code).toBe(SHA256_MULTIHASH)
218
+ expect(ui8Equals(cid.multihash.digest, hash)).toBe(true)
219
+ })
220
+
221
+ it('rejects hashes on invalid lengths', () => {
222
+ expect(() => cidForRawHash(new Uint8Array(31))).toThrow(
223
+ 'Invalid SHA-256 hash length',
224
+ )
225
+ expect(() => cidForRawHash(new Uint8Array(33))).toThrow(
226
+ 'Invalid SHA-256 hash length',
227
+ )
228
+ })
229
+ })
230
+
231
+ describe(asMultiformatsCID, () => {
232
+ it('converts compatible CID to multiformats CID', () => {
233
+ for (const cid of [cborCid, rawCid, rawCidCustom, rawCidCustomBytes]) {
234
+ expect(asMultiformatsCID(cid)).toBeInstanceOf(CID)
235
+ expect(asMultiformatsCID(cid)).toMatchObject({
236
+ version: cid.version,
237
+ code: cid.code,
238
+ multihash: {
239
+ code: cid.multihash.code,
240
+ digest: cid.multihash.digest,
241
+ },
242
+ })
243
+ }
244
+ })
245
+ })