@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.
- package/CHANGELOG.md +8 -0
- package/dist/blob.d.ts +2 -2
- package/dist/blob.d.ts.map +1 -1
- package/dist/blob.js +1 -1
- package/dist/blob.js.map +1 -1
- package/dist/lex-equals.d.ts +1 -1
- package/dist/lex-equals.d.ts.map +1 -1
- package/dist/lex-equals.js.map +1 -1
- package/dist/lex.d.ts +1 -1
- package/dist/lex.d.ts.map +1 -1
- package/dist/lex.js.map +1 -1
- package/dist/lib/nodejs-buffer.js.map +1 -1
- package/dist/uint8array-from-base64.d.ts +1 -1
- package/dist/uint8array-from-base64.d.ts.map +1 -1
- package/dist/uint8array-from-base64.js.map +1 -1
- package/dist/uint8array-to-base64.d.ts +1 -1
- package/dist/uint8array-to-base64.d.ts.map +1 -1
- package/dist/uint8array-to-base64.js.map +1 -1
- package/dist/uint8array.d.ts +1 -1
- package/dist/uint8array.d.ts.map +1 -1
- package/dist/uint8array.js.map +1 -1
- package/dist/utf8-from-base64.d.ts +1 -1
- package/dist/utf8-from-base64.d.ts.map +1 -1
- package/dist/utf8-from-base64.js.map +1 -1
- package/dist/utf8-to-base64.d.ts +1 -1
- package/dist/utf8-to-base64.d.ts.map +1 -1
- package/dist/utf8-to-base64.js.map +1 -1
- package/dist/utf8.d.ts +1 -1
- package/dist/utf8.d.ts.map +1 -1
- package/dist/utf8.js.map +1 -1
- package/package.json +4 -8
- package/src/blob.test.ts +0 -405
- package/src/blob.ts +0 -478
- package/src/cid-implementation.test.ts +0 -129
- package/src/cid.test.ts +0 -350
- package/src/cid.ts +0 -603
- package/src/core-js.d.ts +0 -2
- package/src/index.ts +0 -8
- package/src/lex-equals.test.ts +0 -183
- package/src/lex-equals.ts +0 -123
- package/src/lex-error.test.ts +0 -54
- package/src/lex-error.ts +0 -83
- package/src/lex.test.ts +0 -279
- package/src/lex.ts +0 -253
- package/src/lib/nodejs-buffer.ts +0 -46
- package/src/lib/util.test.ts +0 -49
- package/src/lib/util.ts +0 -7
- package/src/object.test.ts +0 -80
- package/src/object.ts +0 -83
- package/src/uint8array-base64.ts +0 -2
- package/src/uint8array-concat.test.ts +0 -197
- package/src/uint8array-concat.ts +0 -25
- package/src/uint8array-from-base64.test.ts +0 -130
- package/src/uint8array-from-base64.ts +0 -98
- package/src/uint8array-to-base64.test.ts +0 -170
- package/src/uint8array-to-base64.ts +0 -55
- package/src/uint8array.test.ts +0 -503
- package/src/uint8array.ts +0 -197
- package/src/utf8-from-base64.test.ts +0 -39
- package/src/utf8-from-base64.ts +0 -23
- package/src/utf8-from-bytes.test.ts +0 -43
- package/src/utf8-from-bytes.ts +0 -21
- package/src/utf8-grapheme-len.test.ts +0 -38
- package/src/utf8-grapheme-len.ts +0 -21
- package/src/utf8-len.test.ts +0 -21
- package/src/utf8-len.ts +0 -51
- package/src/utf8-to-base64.test.ts +0 -35
- package/src/utf8-to-base64.ts +0 -22
- package/src/utf8.ts +0 -128
- package/tsconfig.build.json +0 -12
- package/tsconfig.json +0 -7
- package/tsconfig.tests.json +0 -8
package/src/blob.test.ts
DELETED
|
@@ -1,405 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest'
|
|
2
|
-
import {
|
|
3
|
-
BlobRef,
|
|
4
|
-
LegacyBlobRef,
|
|
5
|
-
enumBlobRefs,
|
|
6
|
-
isLegacyBlobRef,
|
|
7
|
-
isTypedBlobRef,
|
|
8
|
-
} from './blob.js'
|
|
9
|
-
import { RawCid, parseCid } from './cid.js'
|
|
10
|
-
import { LexArray, LexMap, LexValue } from './lex.js'
|
|
11
|
-
|
|
12
|
-
// await cidForRawBytes(Buffer.from('Hello, World!'))
|
|
13
|
-
const validBlobCid = parseCid(
|
|
14
|
-
'bafkreig77vqcdozl2wyk6z3cscaj5q5fggi53aoh64fewkdiri3cdauyn4',
|
|
15
|
-
{ flavor: 'raw' },
|
|
16
|
-
)
|
|
17
|
-
|
|
18
|
-
// await cidForLex(Buffer.from('Hello, World!'))
|
|
19
|
-
const invalidBlobCid = parseCid(
|
|
20
|
-
'bafyreic52vzks7wdklat4evp3vimohl55i2unzqpshz2ytka5omzr7exdy',
|
|
21
|
-
{ flavor: 'cbor' },
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
describe(isTypedBlobRef, () => {
|
|
25
|
-
it('tests valid blobCid and lexCid', () => {
|
|
26
|
-
expect(validBlobCid.code).toBe(0x55) // raw
|
|
27
|
-
expect(validBlobCid.multihash.code).toBe(0x12) // sha2-256
|
|
28
|
-
expect(invalidBlobCid.code).toBe(0x71) // dag-cbor
|
|
29
|
-
expect(invalidBlobCid.multihash.code).toBe(0x12) // sha2-256
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
it('parses valid blob', () => {
|
|
33
|
-
expect(
|
|
34
|
-
isTypedBlobRef({
|
|
35
|
-
$type: 'blob',
|
|
36
|
-
ref: validBlobCid,
|
|
37
|
-
mimeType: 'image/jpeg',
|
|
38
|
-
size: 10000,
|
|
39
|
-
}),
|
|
40
|
-
).toBe(true)
|
|
41
|
-
|
|
42
|
-
expect(
|
|
43
|
-
isTypedBlobRef(
|
|
44
|
-
{
|
|
45
|
-
$type: 'blob',
|
|
46
|
-
ref: invalidBlobCid,
|
|
47
|
-
mimeType: 'image/jpeg',
|
|
48
|
-
size: 10000,
|
|
49
|
-
},
|
|
50
|
-
// In non-strict mode, any CID should be accepted
|
|
51
|
-
{ strict: false },
|
|
52
|
-
),
|
|
53
|
-
).toBe(true)
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
it('performs strict validation by default', () => {
|
|
57
|
-
expect(
|
|
58
|
-
isTypedBlobRef({
|
|
59
|
-
$type: 'blob',
|
|
60
|
-
ref: invalidBlobCid,
|
|
61
|
-
mimeType: 'image/jpeg',
|
|
62
|
-
size: 10000,
|
|
63
|
-
}),
|
|
64
|
-
).toBe(false)
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
it('rejects invalid inputs', () => {
|
|
68
|
-
expect(
|
|
69
|
-
isTypedBlobRef({
|
|
70
|
-
$type: 'blob',
|
|
71
|
-
ref: { $link: validBlobCid.toString() },
|
|
72
|
-
mimeType: 'image/jpeg',
|
|
73
|
-
size: '10000',
|
|
74
|
-
}),
|
|
75
|
-
).toBe(false)
|
|
76
|
-
|
|
77
|
-
expect(
|
|
78
|
-
isTypedBlobRef({
|
|
79
|
-
// $type: 'blob',
|
|
80
|
-
ref: validBlobCid,
|
|
81
|
-
mimeType: 'image/jpeg',
|
|
82
|
-
size: 10000,
|
|
83
|
-
}),
|
|
84
|
-
).toBe(false)
|
|
85
|
-
|
|
86
|
-
expect(
|
|
87
|
-
isTypedBlobRef({
|
|
88
|
-
$type: 'blob',
|
|
89
|
-
ref: validBlobCid,
|
|
90
|
-
mimeType: { toString: () => 'image/jpeg' },
|
|
91
|
-
size: 10000,
|
|
92
|
-
}),
|
|
93
|
-
).toBe(false)
|
|
94
|
-
|
|
95
|
-
expect(
|
|
96
|
-
isTypedBlobRef(
|
|
97
|
-
{
|
|
98
|
-
$type: 'blob',
|
|
99
|
-
ref: { $link: validBlobCid.toString() },
|
|
100
|
-
mimeType: 'image/jpeg',
|
|
101
|
-
size: '10000',
|
|
102
|
-
},
|
|
103
|
-
{ strict: true },
|
|
104
|
-
),
|
|
105
|
-
).toBe(false)
|
|
106
|
-
|
|
107
|
-
expect(
|
|
108
|
-
isTypedBlobRef({
|
|
109
|
-
$type: 'blob',
|
|
110
|
-
mimeType: 'image/jpeg',
|
|
111
|
-
size: 10000,
|
|
112
|
-
}),
|
|
113
|
-
).toBe(false)
|
|
114
|
-
|
|
115
|
-
expect(
|
|
116
|
-
isTypedBlobRef(
|
|
117
|
-
{
|
|
118
|
-
$type: 'blob',
|
|
119
|
-
mimeType: 'image/jpeg',
|
|
120
|
-
size: 10000,
|
|
121
|
-
},
|
|
122
|
-
{ strict: true },
|
|
123
|
-
),
|
|
124
|
-
).toBe(false)
|
|
125
|
-
|
|
126
|
-
expect(isTypedBlobRef('not an object')).toBe(false)
|
|
127
|
-
expect(isTypedBlobRef([])).toBe(false)
|
|
128
|
-
expect(isTypedBlobRef(new Date())).toBe(false)
|
|
129
|
-
expect(isTypedBlobRef(new Map())).toBe(false)
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
it('rejects non-integer size', () => {
|
|
133
|
-
expect(
|
|
134
|
-
isTypedBlobRef({
|
|
135
|
-
$type: 'blob',
|
|
136
|
-
ref: validBlobCid,
|
|
137
|
-
mimeType: 'image/jpeg',
|
|
138
|
-
size: 10000.5,
|
|
139
|
-
}),
|
|
140
|
-
).toBe(false)
|
|
141
|
-
})
|
|
142
|
-
|
|
143
|
-
it('rejects invalid CID/multihash code', () => {
|
|
144
|
-
expect(
|
|
145
|
-
isTypedBlobRef(
|
|
146
|
-
{
|
|
147
|
-
$type: 'blob',
|
|
148
|
-
ref: validBlobCid,
|
|
149
|
-
mimeType: 'image/jpeg',
|
|
150
|
-
size: 10000,
|
|
151
|
-
},
|
|
152
|
-
{ strict: true },
|
|
153
|
-
),
|
|
154
|
-
).toBe(true)
|
|
155
|
-
|
|
156
|
-
expect(
|
|
157
|
-
isTypedBlobRef(
|
|
158
|
-
{
|
|
159
|
-
$type: 'blob',
|
|
160
|
-
ref: invalidBlobCid,
|
|
161
|
-
mimeType: 'image/jpeg',
|
|
162
|
-
size: 10000,
|
|
163
|
-
},
|
|
164
|
-
{ strict: true },
|
|
165
|
-
),
|
|
166
|
-
).toBe(false)
|
|
167
|
-
})
|
|
168
|
-
|
|
169
|
-
it('rejects extra keys', () => {
|
|
170
|
-
expect(
|
|
171
|
-
isTypedBlobRef({
|
|
172
|
-
$type: 'blob',
|
|
173
|
-
ref: validBlobCid,
|
|
174
|
-
mimeType: 'image/jpeg',
|
|
175
|
-
size: 10000,
|
|
176
|
-
extra: 'not allowed',
|
|
177
|
-
}),
|
|
178
|
-
).toBe(false)
|
|
179
|
-
|
|
180
|
-
expect(
|
|
181
|
-
isTypedBlobRef(
|
|
182
|
-
{
|
|
183
|
-
$type: 'blob',
|
|
184
|
-
ref: validBlobCid,
|
|
185
|
-
mimeType: 'image/jpeg',
|
|
186
|
-
size: 10000,
|
|
187
|
-
extra: 'not allowed',
|
|
188
|
-
},
|
|
189
|
-
{ strict: true },
|
|
190
|
-
),
|
|
191
|
-
).toBe(false)
|
|
192
|
-
})
|
|
193
|
-
|
|
194
|
-
describe('strict mode', () => {
|
|
195
|
-
it('rejects invalid CID version', () => {
|
|
196
|
-
const cidV0 = parseCid(
|
|
197
|
-
'QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG', // CID v0
|
|
198
|
-
)
|
|
199
|
-
expect(
|
|
200
|
-
isTypedBlobRef(
|
|
201
|
-
{
|
|
202
|
-
$type: 'blob',
|
|
203
|
-
ref: cidV0,
|
|
204
|
-
mimeType: 'image/jpeg',
|
|
205
|
-
size: 10000,
|
|
206
|
-
},
|
|
207
|
-
{ strict: true },
|
|
208
|
-
),
|
|
209
|
-
).toBe(false)
|
|
210
|
-
})
|
|
211
|
-
})
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
describe(isLegacyBlobRef, () => {
|
|
215
|
-
it('parses valid legacy blob', () => {
|
|
216
|
-
expect(
|
|
217
|
-
isLegacyBlobRef({
|
|
218
|
-
cid: validBlobCid.toString(),
|
|
219
|
-
mimeType: 'image/jpeg',
|
|
220
|
-
}),
|
|
221
|
-
).toBe(true)
|
|
222
|
-
|
|
223
|
-
expect(
|
|
224
|
-
isLegacyBlobRef(
|
|
225
|
-
{
|
|
226
|
-
cid: invalidBlobCid.toString(),
|
|
227
|
-
mimeType: 'image/jpeg',
|
|
228
|
-
},
|
|
229
|
-
{ strict: false },
|
|
230
|
-
),
|
|
231
|
-
).toBe(true)
|
|
232
|
-
})
|
|
233
|
-
|
|
234
|
-
it('rejects invalid inputs', () => {
|
|
235
|
-
expect(
|
|
236
|
-
isLegacyBlobRef(
|
|
237
|
-
{
|
|
238
|
-
cid: invalidBlobCid.toString(),
|
|
239
|
-
mimeType: 'image/jpeg',
|
|
240
|
-
},
|
|
241
|
-
{ strict: true },
|
|
242
|
-
),
|
|
243
|
-
).toBe(false)
|
|
244
|
-
|
|
245
|
-
expect(
|
|
246
|
-
isLegacyBlobRef({
|
|
247
|
-
cid: 'babbaaa',
|
|
248
|
-
mimeType: 'image/jpeg',
|
|
249
|
-
}),
|
|
250
|
-
).toBe(false)
|
|
251
|
-
|
|
252
|
-
expect(
|
|
253
|
-
isLegacyBlobRef({
|
|
254
|
-
cid: 12345,
|
|
255
|
-
mimeType: 'image/jpeg',
|
|
256
|
-
}),
|
|
257
|
-
).toBe(false)
|
|
258
|
-
|
|
259
|
-
expect(
|
|
260
|
-
isLegacyBlobRef({
|
|
261
|
-
mimeType: 'image/jpeg',
|
|
262
|
-
}),
|
|
263
|
-
).toBe(false)
|
|
264
|
-
|
|
265
|
-
expect(
|
|
266
|
-
isLegacyBlobRef({
|
|
267
|
-
cid: invalidBlobCid.toString(),
|
|
268
|
-
mimeType: { toString: () => 'image/jpeg' },
|
|
269
|
-
}),
|
|
270
|
-
).toBe(false)
|
|
271
|
-
|
|
272
|
-
expect(
|
|
273
|
-
isLegacyBlobRef({
|
|
274
|
-
cid: invalidBlobCid.toString(),
|
|
275
|
-
mimeType: 3,
|
|
276
|
-
}),
|
|
277
|
-
).toBe(false)
|
|
278
|
-
|
|
279
|
-
expect(
|
|
280
|
-
isLegacyBlobRef({
|
|
281
|
-
cid: invalidBlobCid.toString(),
|
|
282
|
-
mimeType: '',
|
|
283
|
-
}),
|
|
284
|
-
).toBe(false)
|
|
285
|
-
|
|
286
|
-
expect(isLegacyBlobRef([])).toBe(false)
|
|
287
|
-
expect(isLegacyBlobRef('not an object')).toBe(false)
|
|
288
|
-
expect(isLegacyBlobRef(new Date())).toBe(false)
|
|
289
|
-
expect(isLegacyBlobRef(new Map())).toBe(false)
|
|
290
|
-
})
|
|
291
|
-
|
|
292
|
-
it('rejects extra keys', () => {
|
|
293
|
-
expect(
|
|
294
|
-
isLegacyBlobRef({
|
|
295
|
-
cid: validBlobCid.toString(),
|
|
296
|
-
mimeType: 'image/jpeg',
|
|
297
|
-
extra: 'not allowed',
|
|
298
|
-
}),
|
|
299
|
-
).toBe(false)
|
|
300
|
-
})
|
|
301
|
-
})
|
|
302
|
-
|
|
303
|
-
describe(enumBlobRefs, () => {
|
|
304
|
-
const valid1: BlobRef<RawCid> = {
|
|
305
|
-
$type: 'blob',
|
|
306
|
-
ref: validBlobCid,
|
|
307
|
-
mimeType: 'image/png',
|
|
308
|
-
size: 2048,
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
const valid2: BlobRef<RawCid> = {
|
|
312
|
-
$type: 'blob',
|
|
313
|
-
ref: validBlobCid,
|
|
314
|
-
mimeType: 'image/jpeg',
|
|
315
|
-
size: 1024,
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
const invalid: BlobRef = {
|
|
319
|
-
$type: 'blob',
|
|
320
|
-
ref: invalidBlobCid,
|
|
321
|
-
mimeType: 'image/jpeg',
|
|
322
|
-
size: 1024,
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
const legacy: LegacyBlobRef = {
|
|
326
|
-
cid: validBlobCid.toString(),
|
|
327
|
-
mimeType: 'image/gif',
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
const data: LexValue = {
|
|
331
|
-
name: 'example',
|
|
332
|
-
file: { deeply: { nested: { in: { object: { valid1 } } } } },
|
|
333
|
-
attachments: [valid2, invalid, legacy, { description: 'not a blob' }],
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
it('enumerates valid BlobRefs by default', () => {
|
|
337
|
-
const refs = Array.from(enumBlobRefs(data))
|
|
338
|
-
expect(refs).toHaveLength(2)
|
|
339
|
-
expect(refs.includes(valid1)).toBe(true)
|
|
340
|
-
expect(refs.includes(valid2)).toBe(true)
|
|
341
|
-
})
|
|
342
|
-
|
|
343
|
-
describe('strict support', () => {
|
|
344
|
-
it('enumerates valid BlobRefs in strict mode', () => {
|
|
345
|
-
const refs = Array.from(enumBlobRefs(data, { strict: true }))
|
|
346
|
-
expect(refs).toHaveLength(2)
|
|
347
|
-
expect(refs.includes(valid1)).toBe(true)
|
|
348
|
-
expect(refs.includes(valid2)).toBe(true)
|
|
349
|
-
})
|
|
350
|
-
|
|
351
|
-
it('enumerates all BlobRefs in non-strict mode', () => {
|
|
352
|
-
const refs = Array.from(enumBlobRefs(data, { strict: false }))
|
|
353
|
-
expect(refs).toHaveLength(3)
|
|
354
|
-
expect(refs.includes(valid1)).toBe(true)
|
|
355
|
-
expect(refs.includes(valid2)).toBe(true)
|
|
356
|
-
expect(refs.includes(invalid)).toBe(true)
|
|
357
|
-
})
|
|
358
|
-
})
|
|
359
|
-
|
|
360
|
-
describe('legacy support', () => {
|
|
361
|
-
it('returns LegacyBlobRefs when legacy option is enabled', () => {
|
|
362
|
-
const refs = Array.from(enumBlobRefs(data, { allowLegacy: true }))
|
|
363
|
-
expect(refs).toHaveLength(3)
|
|
364
|
-
expect(refs.includes(valid1)).toBe(true)
|
|
365
|
-
expect(refs.includes(valid2)).toBe(true)
|
|
366
|
-
expect(refs.includes(legacy)).toBe(true)
|
|
367
|
-
})
|
|
368
|
-
})
|
|
369
|
-
|
|
370
|
-
describe('safety', () => {
|
|
371
|
-
it('handles cyclic structures without infinite loops', () => {
|
|
372
|
-
const cyclicArray: LexArray = [valid2]
|
|
373
|
-
const cyclicObject: LexMap = {
|
|
374
|
-
name: 'cyclic',
|
|
375
|
-
blob: valid1,
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
// Creating a cycle
|
|
379
|
-
cyclicArray.push(cyclicArray)
|
|
380
|
-
cyclicObject.self = cyclicObject
|
|
381
|
-
|
|
382
|
-
const refs = Array.from(
|
|
383
|
-
enumBlobRefs({
|
|
384
|
-
cyclicObject,
|
|
385
|
-
cyclicArray,
|
|
386
|
-
}),
|
|
387
|
-
)
|
|
388
|
-
expect(refs).toHaveLength(2)
|
|
389
|
-
expect(refs.includes(valid1)).toBe(true)
|
|
390
|
-
expect(refs.includes(valid2)).toBe(true)
|
|
391
|
-
})
|
|
392
|
-
|
|
393
|
-
it('handles deep structures without exceeding call stack', () => {
|
|
394
|
-
// Creating a deep nested structure
|
|
395
|
-
let deepData: LexMap = { blob: valid1 }
|
|
396
|
-
for (let i = 0; i < 100_000; i++) {
|
|
397
|
-
deepData = { nested: deepData }
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
const refs = Array.from(enumBlobRefs(deepData))
|
|
401
|
-
expect(refs).toHaveLength(1)
|
|
402
|
-
expect(refs[0]).toBe(valid1)
|
|
403
|
-
})
|
|
404
|
-
})
|
|
405
|
-
})
|