@atproto/lex-json 0.1.1 → 0.1.3
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 +1 -1
- package/dist/blob.d.ts.map +1 -1
- package/dist/blob.js +1 -1
- package/dist/blob.js.map +1 -1
- package/dist/bytes.d.ts +1 -1
- package/dist/bytes.d.ts.map +1 -1
- package/dist/bytes.js.map +1 -1
- package/dist/json-bytes-decoder.d.ts +1 -1
- package/dist/json-bytes-decoder.d.ts.map +1 -1
- package/dist/json-bytes-decoder.js.map +1 -1
- package/dist/lex-json.d.ts +2 -2
- package/dist/lex-json.d.ts.map +1 -1
- package/dist/lex-json.js +1 -1
- package/dist/lex-json.js.map +1 -1
- package/dist/link.d.ts +2 -2
- package/dist/link.d.ts.map +1 -1
- package/dist/link.js +1 -1
- package/dist/link.js.map +1 -1
- package/package.json +5 -9
- package/src/blob.ts +0 -70
- package/src/bytes.test.ts +0 -55
- package/src/bytes.ts +0 -73
- package/src/index.ts +0 -4
- package/src/json-bytes-decoder.bench.ts +0 -252
- package/src/json-bytes-decoder.test.ts +0 -889
- package/src/json-bytes-decoder.ts +0 -672
- package/src/json.ts +0 -48
- package/src/lex-json.bench.ts +0 -125
- package/src/lex-json.test.ts +0 -991
- package/src/lex-json.ts +0 -352
- package/src/link.ts +0 -101
- package/tsconfig.build.json +0 -11
- package/tsconfig.json +0 -7
- package/tsconfig.tests.json +0 -8
package/src/bytes.test.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest'
|
|
2
|
-
import { encodeLexBytes, parseLexBytes } from './bytes.js'
|
|
3
|
-
|
|
4
|
-
describe(parseLexBytes, () => {
|
|
5
|
-
it('parses valid $bytes object', () => {
|
|
6
|
-
const bytes = Buffer.from('Hello, world!')
|
|
7
|
-
const input = { $bytes: bytes.toString('base64') }
|
|
8
|
-
const result = parseLexBytes(input)
|
|
9
|
-
expect(result).toBeInstanceOf(Uint8Array)
|
|
10
|
-
expect(new TextDecoder().decode(result!)).toBe('Hello, world!')
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
it('parses valid $bytes object (without padding)', () => {
|
|
14
|
-
const bytes = Buffer.from('Hello, world!')
|
|
15
|
-
const input = { $bytes: bytes.toString('base64').replace(/=*$/, '') }
|
|
16
|
-
const result = parseLexBytes(input)
|
|
17
|
-
expect(result).toBeInstanceOf(Uint8Array)
|
|
18
|
-
expect(new TextDecoder().decode(result!)).toBe('Hello, world!')
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
it('returns undefined for non-$bytes object', () => {
|
|
22
|
-
const input = { foo: 'bar' }
|
|
23
|
-
const result = parseLexBytes(input)
|
|
24
|
-
expect(result).toBeUndefined()
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
it('returns undefined for $bytes with non-string value', () => {
|
|
28
|
-
const input = { $bytes: 12345 }
|
|
29
|
-
const result = parseLexBytes(input)
|
|
30
|
-
expect(result).toBeUndefined()
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
it('returns undefined for $bytes with extra properties', () => {
|
|
34
|
-
const bytes = Buffer.from('Hello, world!')
|
|
35
|
-
const input = { $bytes: bytes.toString('base64'), extra: true }
|
|
36
|
-
const result = parseLexBytes(input)
|
|
37
|
-
expect(result).toBeUndefined()
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
it('returns undefined for invalid base64 string', () => {
|
|
41
|
-
const input = { $bytes: '!!!invalid-base64!!!' }
|
|
42
|
-
const result = parseLexBytes(input)
|
|
43
|
-
expect(result).toBeUndefined()
|
|
44
|
-
})
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
describe(encodeLexBytes, () => {
|
|
48
|
-
it('encodes Uint8Array to $bytes object', () => {
|
|
49
|
-
const bytes = Buffer.from('Hello, world!')
|
|
50
|
-
const result = encodeLexBytes(bytes)
|
|
51
|
-
expect(result).toEqual({
|
|
52
|
-
$bytes: bytes.toString('base64').replace(/=*$/, ''),
|
|
53
|
-
})
|
|
54
|
-
})
|
|
55
|
-
})
|
package/src/bytes.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { fromBase64, toBase64 } from '@atproto/lex-data'
|
|
2
|
-
import { JsonValue } from './json.js'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Parses a `{$bytes: string}` JSON object into a `Uint8Array`.
|
|
6
|
-
*
|
|
7
|
-
* In the AT Protocol data model, binary data is represented in JSON as an object
|
|
8
|
-
* with a single `$bytes` property containing a base64-encoded string. This function
|
|
9
|
-
* decodes that representation back into raw bytes.
|
|
10
|
-
*
|
|
11
|
-
* @param input - An object potentially containing a `$bytes` property
|
|
12
|
-
* @returns The decoded `Uint8Array` if the input is a valid `$bytes` object,
|
|
13
|
-
* or `undefined` if the input is not a valid `$bytes` representation
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* // Parse a $bytes object to Uint8Array
|
|
18
|
-
* const bytes = parseLexBytes({ $bytes: 'SGVsbG8sIHdvcmxkIQ==' })
|
|
19
|
-
* // bytes is Uint8Array containing "Hello, world!"
|
|
20
|
-
*
|
|
21
|
-
* // Returns undefined for non-$bytes objects
|
|
22
|
-
* const result = parseLexBytes({ foo: 'bar' })
|
|
23
|
-
* // result is undefined
|
|
24
|
-
*
|
|
25
|
-
* // Returns undefined for objects with extra properties
|
|
26
|
-
* const invalid = parseLexBytes({ $bytes: 'SGVsbG8=', extra: true })
|
|
27
|
-
* // invalid is undefined
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
export function parseLexBytes(
|
|
31
|
-
input?: Record<string, unknown>,
|
|
32
|
-
): Uint8Array | undefined {
|
|
33
|
-
if (!input || !('$bytes' in input)) {
|
|
34
|
-
return undefined
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
for (const key in input) {
|
|
38
|
-
if (key !== '$bytes') {
|
|
39
|
-
return undefined
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (typeof input.$bytes !== 'string') {
|
|
44
|
-
return undefined
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
try {
|
|
48
|
-
return fromBase64(input.$bytes)
|
|
49
|
-
} catch {
|
|
50
|
-
return undefined
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Encodes a `Uint8Array` into a `{$bytes: string}` JSON representation.
|
|
56
|
-
*
|
|
57
|
-
* In the AT Protocol data model, binary data is represented in JSON as an object
|
|
58
|
-
* with a single `$bytes` property containing a base64-encoded string. This function
|
|
59
|
-
* performs that encoding.
|
|
60
|
-
*
|
|
61
|
-
* @param bytes - The binary data to encode
|
|
62
|
-
* @returns An object with a `$bytes` property containing the base64-encoded data
|
|
63
|
-
*
|
|
64
|
-
* @example
|
|
65
|
-
* ```typescript
|
|
66
|
-
* const bytes = new TextEncoder().encode('Hello, world!')
|
|
67
|
-
* const encoded = encodeLexBytes(bytes)
|
|
68
|
-
* // encoded is { $bytes: 'SGVsbG8sIHdvcmxkIQ==' }
|
|
69
|
-
* ```
|
|
70
|
-
*/
|
|
71
|
-
export function encodeLexBytes(bytes: Uint8Array): JsonValue {
|
|
72
|
-
return { $bytes: toBase64(bytes) }
|
|
73
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
import { bench, describe } from 'vitest'
|
|
2
|
-
import { utf8FromBytes } from '@atproto/lex-data'
|
|
3
|
-
import { JsonBytesDecoder } from './json-bytes-decoder.js'
|
|
4
|
-
import { LexParseOptions, lexParse, lexParseJsonBytes } from './lex-json.js'
|
|
5
|
-
|
|
6
|
-
// This benchmark compares the performance of two implementations of
|
|
7
|
-
// lexParseJsonBytes:
|
|
8
|
-
// 1. lexParseJsonBytesDecoder: An implementation that uses a custom decoder
|
|
9
|
-
// class that operates directly on bytes to parse JSON and handle AT Protocol
|
|
10
|
-
// special types.
|
|
11
|
-
// 2. lexParseJsonBytesNaive: A simpler implementation that first decodes bytes
|
|
12
|
-
// to a UTF-8 string and then uses the existing lexParse function to parse
|
|
13
|
-
// the JSON.
|
|
14
|
-
|
|
15
|
-
describe('small object', () => {
|
|
16
|
-
benchData({
|
|
17
|
-
$type: 'app.bsky.feed.post',
|
|
18
|
-
text: 'Hello world! 👋',
|
|
19
|
-
createdAt: '2024-01-01T00:00:00Z',
|
|
20
|
-
})
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
describe('simple mixed structure', () => {
|
|
24
|
-
benchData({
|
|
25
|
-
cid: {
|
|
26
|
-
$link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
27
|
-
},
|
|
28
|
-
bytes: {
|
|
29
|
-
$bytes: 'nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0',
|
|
30
|
-
},
|
|
31
|
-
blob: {
|
|
32
|
-
$type: 'blob',
|
|
33
|
-
ref: {
|
|
34
|
-
$link: 'bafkreig77vqcdozl2wyk6z3cscaj5q5fggi53aoh64fewkdiri3cdauyn4',
|
|
35
|
-
},
|
|
36
|
-
mimeType: 'image/jpeg',
|
|
37
|
-
size: 10000,
|
|
38
|
-
},
|
|
39
|
-
nested: {
|
|
40
|
-
array: [
|
|
41
|
-
{
|
|
42
|
-
number: 42,
|
|
43
|
-
string: 'hello world',
|
|
44
|
-
bool: true,
|
|
45
|
-
null: null,
|
|
46
|
-
},
|
|
47
|
-
],
|
|
48
|
-
string: 'Hello 世界! 🌍🌎🌏 Ñoño',
|
|
49
|
-
createdAt: '2024-01-01T00:00:00Z',
|
|
50
|
-
},
|
|
51
|
-
})
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
describe('large structure', () => {
|
|
55
|
-
// Similar to the large object benchmark but smaller
|
|
56
|
-
benchData({
|
|
57
|
-
items: Array.from({ length: 25 }, (_, i) => ({
|
|
58
|
-
id: i,
|
|
59
|
-
name: `Item ${i}`,
|
|
60
|
-
longUnicode:
|
|
61
|
-
'Lorem ipsum dolor sit amet, consectetur adipiscing elit 🤩.\n'.repeat(
|
|
62
|
-
3,
|
|
63
|
-
),
|
|
64
|
-
tags: ['tag1', 'tag2', 'tag3'],
|
|
65
|
-
bytes: {
|
|
66
|
-
$bytes: Buffer.from(`This is some byte data for item ${i}`).toString(
|
|
67
|
-
'base64',
|
|
68
|
-
),
|
|
69
|
-
},
|
|
70
|
-
cid: {
|
|
71
|
-
$link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
72
|
-
},
|
|
73
|
-
metadata: {
|
|
74
|
-
created: '2024-01-01T00:00:00Z',
|
|
75
|
-
count: i * 10,
|
|
76
|
-
nested: {
|
|
77
|
-
flag: i % 2 === 0,
|
|
78
|
-
values: [i, i * 2, i * 3],
|
|
79
|
-
},
|
|
80
|
-
items: Array.from({ length: 5 }, (_, j) => ({
|
|
81
|
-
id: `${i}-${j}`,
|
|
82
|
-
value: `Value ${i}-${j}`,
|
|
83
|
-
})),
|
|
84
|
-
},
|
|
85
|
-
})),
|
|
86
|
-
})
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
describe.skip('extensive test suite', () => {
|
|
90
|
-
describe('integer', () => {
|
|
91
|
-
benchData(42)
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
describe('float', () => {
|
|
95
|
-
benchData(42.42, { strict: false })
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
describe('short ascii', () => {
|
|
99
|
-
benchData('hello world')
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
describe('short unicode', () => {
|
|
103
|
-
benchData('Hello 世界! 🌍🌎🌏 Ñoño')
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
describe('Long text with unicode and escaped characters', () => {
|
|
107
|
-
benchData(
|
|
108
|
-
[
|
|
109
|
-
'Lorém ipsum dolor sit amet, consectetur adipiscing elit 🤩.',
|
|
110
|
-
'Sed ço eiusmod tempor 🇧🇪 incididunt ut labore et dolore magna aliqua.',
|
|
111
|
-
'',
|
|
112
|
-
'\tUt enim ° minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.',
|
|
113
|
-
'\tDuis aute @ dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.',
|
|
114
|
-
'',
|
|
115
|
-
'Excepteur sint õ cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
|
|
116
|
-
].join('\n'),
|
|
117
|
-
)
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
describe('$link', () => {
|
|
121
|
-
benchData({
|
|
122
|
-
$link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
123
|
-
})
|
|
124
|
-
})
|
|
125
|
-
|
|
126
|
-
describe('$bytes', () => {
|
|
127
|
-
const alphabet =
|
|
128
|
-
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
|
129
|
-
|
|
130
|
-
for (const length of [32, 128, 256, 512, 1024, 1024 * 1024]) {
|
|
131
|
-
describe(String(length), () => {
|
|
132
|
-
benchData({
|
|
133
|
-
$bytes: Array.from(
|
|
134
|
-
{ length },
|
|
135
|
-
(_, i) => alphabet[i % alphabet.length],
|
|
136
|
-
).join(''),
|
|
137
|
-
})
|
|
138
|
-
})
|
|
139
|
-
}
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
describe('small object', () => {
|
|
143
|
-
benchData({
|
|
144
|
-
string: 'hello world',
|
|
145
|
-
number: 42,
|
|
146
|
-
bool: true,
|
|
147
|
-
null: null,
|
|
148
|
-
})
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
describe('medium object', () => {
|
|
152
|
-
benchData({
|
|
153
|
-
user: {
|
|
154
|
-
id: '12345',
|
|
155
|
-
name: 'John Doe',
|
|
156
|
-
email: 'john@example.com',
|
|
157
|
-
active: true,
|
|
158
|
-
score: 95,
|
|
159
|
-
},
|
|
160
|
-
posts: [
|
|
161
|
-
{ id: 1, title: 'First Post', likes: 10 },
|
|
162
|
-
{ id: 2, title: 'Second Post', likes: 25 },
|
|
163
|
-
{ id: 3, title: 'Third Post', likes: 42 },
|
|
164
|
-
],
|
|
165
|
-
metadata: {
|
|
166
|
-
created: '2024-01-01T00:00:00Z',
|
|
167
|
-
updated: '2024-01-15T12:30:00Z',
|
|
168
|
-
},
|
|
169
|
-
})
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
describe('heavy nesting', () => {
|
|
173
|
-
type NestedObject = { level: number; nested?: NestedObject }
|
|
174
|
-
const nestedObject = { level: 0 }
|
|
175
|
-
let current: NestedObject = nestedObject
|
|
176
|
-
for (let i = 1; i <= 100; i++) {
|
|
177
|
-
current.nested = { level: i }
|
|
178
|
-
current = current.nested
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
benchData(nestedObject)
|
|
182
|
-
})
|
|
183
|
-
|
|
184
|
-
describe('number heavy array', () => {
|
|
185
|
-
benchData([
|
|
186
|
-
0,
|
|
187
|
-
1,
|
|
188
|
-
-1,
|
|
189
|
-
42,
|
|
190
|
-
-100,
|
|
191
|
-
9007199254740991,
|
|
192
|
-
42,
|
|
193
|
-
-1000,
|
|
194
|
-
9007199254740991,
|
|
195
|
-
Date.now(),
|
|
196
|
-
...Array.from({ length: 100 }, (_, i) =>
|
|
197
|
-
Math.floor(i * 1000 * Math.random()),
|
|
198
|
-
),
|
|
199
|
-
])
|
|
200
|
-
})
|
|
201
|
-
|
|
202
|
-
describe('many empty objects', () => {
|
|
203
|
-
benchData(Array.from({ length: 200 }, () => ({})))
|
|
204
|
-
})
|
|
205
|
-
|
|
206
|
-
describe('many small objects', () => {
|
|
207
|
-
benchData(
|
|
208
|
-
Array.from({ length: 200 }, (_, i) => ({
|
|
209
|
-
id: i,
|
|
210
|
-
name: `item${i}`,
|
|
211
|
-
active: true,
|
|
212
|
-
})),
|
|
213
|
-
)
|
|
214
|
-
})
|
|
215
|
-
|
|
216
|
-
describe('many empty arrays', () => {
|
|
217
|
-
benchData(Array.from({ length: 200 }, () => []))
|
|
218
|
-
})
|
|
219
|
-
|
|
220
|
-
describe('many small arrays', () => {
|
|
221
|
-
benchData(Array.from({ length: 200 }, (_, i) => [i, '', true]))
|
|
222
|
-
})
|
|
223
|
-
})
|
|
224
|
-
|
|
225
|
-
function benchData(data: unknown, options?: LexParseOptions) {
|
|
226
|
-
const bytes = Buffer.from(JSON.stringify(data))
|
|
227
|
-
|
|
228
|
-
const lexParseJsonBytesDecoder: typeof lexParseJsonBytes = (
|
|
229
|
-
bytes,
|
|
230
|
-
options,
|
|
231
|
-
) => {
|
|
232
|
-
const decoder = new JsonBytesDecoder(bytes, options?.strict)
|
|
233
|
-
return decoder.decode()
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
const lexParseJsonBytesNaive: typeof lexParseJsonBytes = (bytes, options) => {
|
|
237
|
-
const jsonString = utf8FromBytes(bytes)
|
|
238
|
-
return lexParse(jsonString, options)
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
bench('current', () => {
|
|
242
|
-
lexParseJsonBytes(bytes, options)
|
|
243
|
-
})
|
|
244
|
-
|
|
245
|
-
bench(lexParseJsonBytesDecoder, () => {
|
|
246
|
-
lexParseJsonBytesDecoder(bytes, options)
|
|
247
|
-
})
|
|
248
|
-
|
|
249
|
-
bench(lexParseJsonBytesNaive, () => {
|
|
250
|
-
lexParseJsonBytesNaive(bytes, options)
|
|
251
|
-
})
|
|
252
|
-
}
|