@atproto/common 0.6.3 → 0.6.5
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 +30 -0
- package/dist/logger.d.ts +2 -2
- package/dist/logger.d.ts.map +1 -1
- package/dist/logger.js +16 -20
- package/dist/logger.js.map +1 -1
- package/package.json +17 -12
- package/jest.config.cjs +0 -21
- package/src/buffers.ts +0 -14
- package/src/dates.ts +0 -20
- package/src/env.ts +0 -25
- package/src/fs.ts +0 -54
- package/src/index.ts +0 -10
- package/src/ipld-multi.ts +0 -9
- package/src/ipld.ts +0 -138
- package/src/logger.ts +0 -36
- package/src/obfuscate.ts +0 -85
- package/src/streams.ts +0 -239
- package/tests/ipld-multi.test.ts +0 -34
- package/tests/ipld-vectors.ts +0 -277
- package/tests/ipld.test.ts +0 -29
- package/tests/streams.test.ts +0 -263
- package/tsconfig.build.json +0 -8
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.json +0 -7
- package/tsconfig.tests.json +0 -7
package/tests/streams.test.ts
DELETED
|
@@ -1,263 +0,0 @@
|
|
|
1
|
-
import events from 'node:events'
|
|
2
|
-
import { PassThrough, Readable } from 'node:stream'
|
|
3
|
-
import * as streams from '../src/streams.js'
|
|
4
|
-
|
|
5
|
-
describe('streams', () => {
|
|
6
|
-
describe('forwardStreamErrors', () => {
|
|
7
|
-
it('forwards errors through a set of streams', () => {
|
|
8
|
-
const streamA = new PassThrough()
|
|
9
|
-
const streamB = new PassThrough()
|
|
10
|
-
let streamBError: Error | null = null
|
|
11
|
-
const err = new Error('foo')
|
|
12
|
-
|
|
13
|
-
streamB.on('error', (err) => {
|
|
14
|
-
streamBError = err
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
streams.forwardStreamErrors(streamA, streamB)
|
|
18
|
-
|
|
19
|
-
streamA.emit('error', err)
|
|
20
|
-
|
|
21
|
-
expect(streamBError).toBe(err)
|
|
22
|
-
})
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
describe('cloneStream', () => {
|
|
26
|
-
it('should clone stream', () => {
|
|
27
|
-
const stream = new PassThrough()
|
|
28
|
-
let clonedError: Error | undefined
|
|
29
|
-
let clonedData: string | undefined
|
|
30
|
-
|
|
31
|
-
const cloned = streams.cloneStream(stream)
|
|
32
|
-
|
|
33
|
-
cloned.on('data', (data) => {
|
|
34
|
-
clonedData = String(data)
|
|
35
|
-
})
|
|
36
|
-
cloned.on('error', (err) => {
|
|
37
|
-
clonedError = err
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
stream.emit('data', 'foo')
|
|
41
|
-
stream.emit('error', new Error('foo error'))
|
|
42
|
-
|
|
43
|
-
expect(clonedData).toEqual('foo')
|
|
44
|
-
expect(clonedError?.message).toEqual('foo error')
|
|
45
|
-
})
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
describe('streamSize', () => {
|
|
49
|
-
it('reads entire stream and computes size', async () => {
|
|
50
|
-
const stream = Readable.from(['f', 'o', 'o'])
|
|
51
|
-
|
|
52
|
-
const size = await streams.streamSize(stream)
|
|
53
|
-
|
|
54
|
-
expect(size).toBe(3)
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
it('returns 0 for empty streams', async () => {
|
|
58
|
-
const stream = Readable.from([])
|
|
59
|
-
const size = await streams.streamSize(stream)
|
|
60
|
-
|
|
61
|
-
expect(size).toBe(0)
|
|
62
|
-
})
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
describe('streamToNodeBuffer', () => {
|
|
66
|
-
it('converts stream to byte array', async () => {
|
|
67
|
-
const stream = Readable.from(Buffer.from('foo'))
|
|
68
|
-
const bytes = await streams.streamToNodeBuffer(stream)
|
|
69
|
-
|
|
70
|
-
expect(bytes[0]).toBe('f'.charCodeAt(0))
|
|
71
|
-
expect(bytes[1]).toBe('o'.charCodeAt(0))
|
|
72
|
-
expect(bytes[2]).toBe('o'.charCodeAt(0))
|
|
73
|
-
expect(bytes.length).toBe(3)
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
it('converts async iterable to byte array', async () => {
|
|
77
|
-
const iterable = (async function* () {
|
|
78
|
-
yield Buffer.from('b')
|
|
79
|
-
yield Buffer.from('a')
|
|
80
|
-
yield new Uint8Array(['r'.charCodeAt(0)])
|
|
81
|
-
})()
|
|
82
|
-
const bytes = await streams.streamToNodeBuffer(iterable)
|
|
83
|
-
|
|
84
|
-
expect(bytes[0]).toBe('b'.charCodeAt(0))
|
|
85
|
-
expect(bytes[1]).toBe('a'.charCodeAt(0))
|
|
86
|
-
expect(bytes[2]).toBe('r'.charCodeAt(0))
|
|
87
|
-
expect(bytes.length).toBe(3)
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
it('throws error for non Uint8Array chunks', async () => {
|
|
91
|
-
const iterable: AsyncIterable<any> = (async function* () {
|
|
92
|
-
yield Buffer.from('b')
|
|
93
|
-
yield Buffer.from('a')
|
|
94
|
-
yield 'r'
|
|
95
|
-
})()
|
|
96
|
-
|
|
97
|
-
await expect(streams.streamToNodeBuffer(iterable)).rejects.toThrow(
|
|
98
|
-
'expected Uint8Array',
|
|
99
|
-
)
|
|
100
|
-
})
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
describe('byteIterableToStream', () => {
|
|
104
|
-
it('converts byte iterable to stream', async () => {
|
|
105
|
-
const iterable: AsyncIterable<Uint8Array> = {
|
|
106
|
-
async *[Symbol.asyncIterator]() {
|
|
107
|
-
yield new Uint8Array([0xa, 0xb])
|
|
108
|
-
},
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const stream = streams.byteIterableToStream(iterable)
|
|
112
|
-
|
|
113
|
-
for await (const chunk of stream) {
|
|
114
|
-
expect(chunk[0]).toBe(0xa)
|
|
115
|
-
expect(chunk[1]).toBe(0xb)
|
|
116
|
-
}
|
|
117
|
-
})
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
describe('coalesceByteStream', () => {
|
|
121
|
-
it('coalesces chunks without changing bytes', async () => {
|
|
122
|
-
const stream = streams.coalesceByteStream(
|
|
123
|
-
Readable.from([
|
|
124
|
-
new Uint8Array([0x1]),
|
|
125
|
-
new Uint8Array([0x2, 0x3]),
|
|
126
|
-
new Uint8Array([0x4, 0x5, 0x6]),
|
|
127
|
-
new Uint8Array([0x7]),
|
|
128
|
-
new Uint8Array([0x8]),
|
|
129
|
-
new Uint8Array([0x9]),
|
|
130
|
-
]),
|
|
131
|
-
4,
|
|
132
|
-
)
|
|
133
|
-
|
|
134
|
-
const chunks = await stream.toArray()
|
|
135
|
-
|
|
136
|
-
expect(Buffer.concat(chunks)).toEqual(
|
|
137
|
-
Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9]),
|
|
138
|
-
)
|
|
139
|
-
|
|
140
|
-
for (let i = 0; i < chunks.length; i++) {
|
|
141
|
-
expect(chunks[i]).toBeInstanceOf(Uint8Array)
|
|
142
|
-
if (i < chunks.length - 1) {
|
|
143
|
-
expect(chunks[i].length).toBeGreaterThanOrEqual(4)
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
})
|
|
147
|
-
|
|
148
|
-
it('yields chunks as they come when target size is 1', async () => {
|
|
149
|
-
const stream = streams.coalesceByteStream(
|
|
150
|
-
Readable.from([
|
|
151
|
-
new Uint8Array([0x1]),
|
|
152
|
-
new Uint8Array([0x2, 0x3]),
|
|
153
|
-
new Uint8Array([0x4, 0x5, 0x6]),
|
|
154
|
-
]),
|
|
155
|
-
1,
|
|
156
|
-
)
|
|
157
|
-
|
|
158
|
-
const chunks = await stream.toArray()
|
|
159
|
-
|
|
160
|
-
expect(chunks).toEqual([
|
|
161
|
-
new Uint8Array([0x1]),
|
|
162
|
-
new Uint8Array([0x2, 0x3]),
|
|
163
|
-
new Uint8Array([0x4, 0x5, 0x6]),
|
|
164
|
-
])
|
|
165
|
-
})
|
|
166
|
-
|
|
167
|
-
it('coalesces into a single chunk when target size exceeds total', async () => {
|
|
168
|
-
const stream = streams.coalesceByteStream(
|
|
169
|
-
Readable.from([
|
|
170
|
-
new Uint8Array([0x1]),
|
|
171
|
-
new Uint8Array([0x2, 0x3]),
|
|
172
|
-
new Uint8Array([0x4, 0x5, 0x6]),
|
|
173
|
-
]),
|
|
174
|
-
1000,
|
|
175
|
-
)
|
|
176
|
-
|
|
177
|
-
const chunks = await stream.toArray()
|
|
178
|
-
|
|
179
|
-
expect(chunks.length).toBe(1)
|
|
180
|
-
expect(Buffer.concat(chunks)).toEqual(
|
|
181
|
-
Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6]),
|
|
182
|
-
)
|
|
183
|
-
})
|
|
184
|
-
|
|
185
|
-
it('forwards source stream errors', async () => {
|
|
186
|
-
const source = new PassThrough()
|
|
187
|
-
const stream = streams.coalesceByteStream(source, 4)
|
|
188
|
-
const err = new Error('source failed')
|
|
189
|
-
|
|
190
|
-
const gotError = events.once(stream, 'error')
|
|
191
|
-
source.emit('error', err)
|
|
192
|
-
|
|
193
|
-
expect(await gotError).toEqual([err])
|
|
194
|
-
})
|
|
195
|
-
|
|
196
|
-
it('destroying the coalesced stream destroys the source', async () => {
|
|
197
|
-
let finalized = false
|
|
198
|
-
async function* gen() {
|
|
199
|
-
try {
|
|
200
|
-
while (true) {
|
|
201
|
-
yield new Uint8Array(1024)
|
|
202
|
-
}
|
|
203
|
-
} finally {
|
|
204
|
-
finalized = true
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
const source = Readable.from(gen(), { objectMode: false })
|
|
208
|
-
const stream = streams.coalesceByteStream(source, 4096)
|
|
209
|
-
|
|
210
|
-
await events.once(stream, 'data')
|
|
211
|
-
stream.destroy()
|
|
212
|
-
await new Promise((resolve) => source.once('close', resolve))
|
|
213
|
-
|
|
214
|
-
expect(source.destroyed).toBe(true)
|
|
215
|
-
expect(finalized).toBe(true)
|
|
216
|
-
})
|
|
217
|
-
})
|
|
218
|
-
|
|
219
|
-
describe('bytesToStream', () => {
|
|
220
|
-
it('converts byte array to readable stream', async () => {
|
|
221
|
-
const bytes = new Uint8Array([0xa, 0xb])
|
|
222
|
-
const stream = streams.bytesToStream(bytes)
|
|
223
|
-
|
|
224
|
-
for await (const chunk of stream) {
|
|
225
|
-
expect(chunk[0]).toBe(0xa)
|
|
226
|
-
expect(chunk[1]).toBe(0xb)
|
|
227
|
-
}
|
|
228
|
-
})
|
|
229
|
-
})
|
|
230
|
-
|
|
231
|
-
describe('MaxSizeChecker', () => {
|
|
232
|
-
it('destroys once max size is met', async () => {
|
|
233
|
-
const stream = new Readable()
|
|
234
|
-
const err = new Error('foo')
|
|
235
|
-
const checker = new streams.MaxSizeChecker(1, () => err)
|
|
236
|
-
let lastError: Error | undefined
|
|
237
|
-
|
|
238
|
-
const outStream = stream.pipe(checker)
|
|
239
|
-
|
|
240
|
-
outStream.on('error', (err) => {
|
|
241
|
-
lastError = err
|
|
242
|
-
})
|
|
243
|
-
|
|
244
|
-
const waitForStream = new Promise<void>((resolve) => {
|
|
245
|
-
stream.on('end', () => {
|
|
246
|
-
resolve()
|
|
247
|
-
})
|
|
248
|
-
})
|
|
249
|
-
|
|
250
|
-
expect(checker.totalSize).toBe(0)
|
|
251
|
-
|
|
252
|
-
stream.push(new Uint8Array([0xa]))
|
|
253
|
-
stream.push(new Uint8Array([0xb]))
|
|
254
|
-
stream.push(null)
|
|
255
|
-
|
|
256
|
-
await waitForStream
|
|
257
|
-
|
|
258
|
-
expect(checker.totalSize).toBe(2)
|
|
259
|
-
expect(checker.destroyed).toBe(true)
|
|
260
|
-
expect(lastError).toBe(err)
|
|
261
|
-
})
|
|
262
|
-
})
|
|
263
|
-
})
|
package/tsconfig.build.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":"7.0.0-dev.20260614.1","root":["./src/buffers.ts","./src/dates.ts","./src/env.ts","./src/fs.ts","./src/index.ts","./src/ipld-multi.ts","./src/ipld.ts","./src/logger.ts","./src/obfuscate.ts","./src/streams.ts"]}
|
package/tsconfig.json
DELETED