@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 +22 -0
- package/dist/blob.d.ts +20 -12
- package/dist/blob.d.ts.map +1 -1
- package/dist/blob.js +5 -16
- package/dist/blob.js.map +1 -1
- package/dist/cid.d.ts +124 -30
- package/dist/cid.d.ts.map +1 -1
- package/dist/cid.js +191 -39
- package/dist/cid.js.map +1 -1
- package/dist/lex-equals.js +1 -1
- package/dist/lex-equals.js.map +1 -1
- package/package.json +1 -1
- package/src/blob.test.ts +6 -3
- package/src/blob.ts +40 -33
- package/src/cid-implementation.test.ts +137 -0
- package/src/cid.test.ts +172 -53
- package/src/cid.ts +349 -55
- package/src/lex-equals.ts +2 -2
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
|
-
|
|
6
|
-
|
|
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
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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(
|
|
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('
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
|
58
|
-
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
|
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(
|
|
174
|
+
expect(decodedCid.toString()).toBe(cborCidStr)
|
|
83
175
|
})
|
|
84
176
|
})
|
|
85
177
|
|
|
86
178
|
describe(parseCid, () => {
|
|
87
179
|
it('parses valid CIDs', () => {
|
|
88
|
-
|
|
89
|
-
|
|
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(
|
|
189
|
+
describe(parseCidSafe, () => {
|
|
100
190
|
it('parses valid CIDs', () => {
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
+
})
|