@atproto/lex-client 0.0.15 → 0.0.16

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.
@@ -0,0 +1,333 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import {
3
+ buildAtprotoHeaders,
4
+ isAsyncIterable,
5
+ isBlobLike,
6
+ toReadableStream,
7
+ toReadableStreamPonyfill,
8
+ } from './util.js'
9
+
10
+ // ============================================================================
11
+ // isBlobLike
12
+ // ============================================================================
13
+
14
+ describe(isBlobLike, () => {
15
+ it('returns true for native Blob', () => {
16
+ expect(isBlobLike(new Blob(['hello']))).toBe(true)
17
+ })
18
+
19
+ it('returns true for native File', () => {
20
+ expect(isBlobLike(new File(['hello'], 'test.txt'))).toBe(true)
21
+ })
22
+
23
+ it('returns false for null', () => {
24
+ expect(isBlobLike(null)).toBe(false)
25
+ })
26
+
27
+ it('returns false for undefined', () => {
28
+ expect(isBlobLike(undefined)).toBe(false)
29
+ })
30
+
31
+ it('returns false for primitives', () => {
32
+ expect(isBlobLike(42)).toBe(false)
33
+ expect(isBlobLike('string')).toBe(false)
34
+ expect(isBlobLike(true)).toBe(false)
35
+ })
36
+
37
+ it('returns false for plain objects', () => {
38
+ expect(isBlobLike({})).toBe(false)
39
+ expect(isBlobLike({ stream: () => {} })).toBe(false)
40
+ })
41
+
42
+ it('returns true for Blob-like objects with [Symbol.toStringTag] = "Blob"', () => {
43
+ const blobLike = {
44
+ [Symbol.toStringTag]: 'Blob',
45
+ stream: () => new ReadableStream(),
46
+ }
47
+ expect(isBlobLike(blobLike)).toBe(true)
48
+ })
49
+
50
+ it('returns true for File-like objects with [Symbol.toStringTag] = "File"', () => {
51
+ const fileLike = {
52
+ [Symbol.toStringTag]: 'File',
53
+ stream: () => new ReadableStream(),
54
+ }
55
+ expect(isBlobLike(fileLike)).toBe(true)
56
+ })
57
+
58
+ it('returns false for objects with Blob tag but no stream method', () => {
59
+ const notBlob = {
60
+ [Symbol.toStringTag]: 'Blob',
61
+ }
62
+ expect(isBlobLike(notBlob)).toBe(false)
63
+ })
64
+
65
+ it('returns false for objects with Blob tag but non-function stream', () => {
66
+ const notBlob = {
67
+ [Symbol.toStringTag]: 'Blob',
68
+ stream: 'not a function',
69
+ }
70
+ expect(isBlobLike(notBlob)).toBe(false)
71
+ })
72
+ })
73
+
74
+ // ============================================================================
75
+ // isAsyncIterable
76
+ // ============================================================================
77
+
78
+ describe(isAsyncIterable, () => {
79
+ it('returns true for async generators', () => {
80
+ async function* gen() {
81
+ yield 1
82
+ }
83
+ expect(isAsyncIterable(gen())).toBe(true)
84
+ })
85
+
86
+ it('returns true for objects with Symbol.asyncIterator', () => {
87
+ const iterable = {
88
+ [Symbol.asyncIterator]() {
89
+ return { next: async () => ({ done: true, value: undefined }) }
90
+ },
91
+ }
92
+ expect(isAsyncIterable(iterable)).toBe(true)
93
+ })
94
+
95
+ it('returns false for null', () => {
96
+ expect(isAsyncIterable(null)).toBe(false)
97
+ })
98
+
99
+ it('returns false for undefined', () => {
100
+ expect(isAsyncIterable(undefined)).toBe(false)
101
+ })
102
+
103
+ it('returns false for plain objects', () => {
104
+ expect(isAsyncIterable({})).toBe(false)
105
+ })
106
+
107
+ it('returns false for sync iterables', () => {
108
+ expect(isAsyncIterable([1, 2, 3])).toBe(false)
109
+ expect(isAsyncIterable('string')).toBe(false)
110
+ })
111
+ })
112
+
113
+ // ============================================================================
114
+ // buildAtprotoHeaders
115
+ // ============================================================================
116
+
117
+ describe(buildAtprotoHeaders, () => {
118
+ it('returns empty headers when no options are set', () => {
119
+ const headers = buildAtprotoHeaders({})
120
+ expect([...headers.entries()]).toEqual([])
121
+ })
122
+
123
+ it('sets atproto-proxy header from service option', () => {
124
+ const headers = buildAtprotoHeaders({
125
+ service: 'did:plc:1234#atproto_labeler',
126
+ })
127
+ expect(headers.get('atproto-proxy')).toBe('did:plc:1234#atproto_labeler')
128
+ })
129
+
130
+ it('does not override existing atproto-proxy header', () => {
131
+ const headers = buildAtprotoHeaders({
132
+ headers: { 'atproto-proxy': 'did:plc:existing#service' },
133
+ service: 'did:plc:new#service',
134
+ })
135
+ expect(headers.get('atproto-proxy')).toBe('did:plc:existing#service')
136
+ })
137
+
138
+ it('sets atproto-accept-labelers from labelers option', () => {
139
+ const headers = buildAtprotoHeaders({
140
+ labelers: ['did:plc:labeler1', 'did:plc:labeler2'] as const,
141
+ })
142
+ expect(headers.get('atproto-accept-labelers')).toBe(
143
+ 'did:plc:labeler1, did:plc:labeler2',
144
+ )
145
+ })
146
+
147
+ it('appends to existing atproto-accept-labelers header', () => {
148
+ const headers = buildAtprotoHeaders({
149
+ headers: { 'atproto-accept-labelers': 'did:plc:existing' },
150
+ labelers: ['did:plc:new'] as const,
151
+ })
152
+ expect(headers.get('atproto-accept-labelers')).toBe(
153
+ 'did:plc:new, did:plc:existing',
154
+ )
155
+ })
156
+
157
+ it('passes through base headers', () => {
158
+ const headers = buildAtprotoHeaders({
159
+ headers: { Authorization: 'Bearer token123' },
160
+ })
161
+ expect(headers.get('Authorization')).toBe('Bearer token123')
162
+ })
163
+
164
+ it('accepts Headers instance as base headers', () => {
165
+ const base = new Headers({ 'X-Custom': 'value' })
166
+ const headers = buildAtprotoHeaders({ headers: base })
167
+ expect(headers.get('X-Custom')).toBe('value')
168
+ })
169
+
170
+ it('sets empty header for empty labelers iterable', () => {
171
+ const headers = buildAtprotoHeaders({ labelers: [] })
172
+ // An empty array still sets the header (to empty string), distinguishing
173
+ // "no labelers requested" from "labelers option not provided"
174
+ expect(headers.has('atproto-accept-labelers')).toBe(true)
175
+ expect(headers.get('atproto-accept-labelers')).toBe('')
176
+ })
177
+ })
178
+
179
+ // ============================================================================
180
+ // toReadableStream
181
+ // ============================================================================
182
+
183
+ describe(toReadableStream, () => {
184
+ it('converts async iterable to ReadableStream', async () => {
185
+ async function* gen() {
186
+ yield new Uint8Array([1, 2])
187
+ yield new Uint8Array([3, 4])
188
+ }
189
+
190
+ const stream = toReadableStream(gen())
191
+ const reader = stream.getReader()
192
+
193
+ const chunk1 = await reader.read()
194
+ expect(chunk1.done).toBe(false)
195
+ expect(chunk1.value).toEqual(new Uint8Array([1, 2]))
196
+
197
+ const chunk2 = await reader.read()
198
+ expect(chunk2.done).toBe(false)
199
+ expect(chunk2.value).toEqual(new Uint8Array([3, 4]))
200
+
201
+ const end = await reader.read()
202
+ expect(end.done).toBe(true)
203
+ })
204
+
205
+ it('handles empty async iterable', async () => {
206
+ async function* gen() {
207
+ // yields nothing
208
+ }
209
+
210
+ const stream = toReadableStream(gen())
211
+ const reader = stream.getReader()
212
+
213
+ const result = await reader.read()
214
+ expect(result.done).toBe(true)
215
+ })
216
+
217
+ it('can be consumed with Response API', async () => {
218
+ async function* gen() {
219
+ yield new TextEncoder().encode('hello ')
220
+ yield new TextEncoder().encode('world')
221
+ }
222
+
223
+ const stream = toReadableStream(gen())
224
+ const response = new Response(stream)
225
+ const text = await response.text()
226
+ expect(text).toBe('hello world')
227
+ })
228
+
229
+ it('propagates errors from the async iterable', async () => {
230
+ async function* gen() {
231
+ yield new Uint8Array([1])
232
+ throw new Error('stream error')
233
+ }
234
+
235
+ const stream = toReadableStream(gen())
236
+ const reader = stream.getReader()
237
+
238
+ // First chunk succeeds
239
+ await reader.read()
240
+
241
+ // Second read should reject
242
+ await expect(reader.read()).rejects.toThrow('stream error')
243
+ })
244
+ })
245
+
246
+ // ============================================================================
247
+ // toReadableStreamPonyfill
248
+ // ============================================================================
249
+
250
+ describe(toReadableStreamPonyfill, () => {
251
+ it('converts async iterable to ReadableStream', async () => {
252
+ async function* gen() {
253
+ yield new Uint8Array([1, 2])
254
+ yield new Uint8Array([3, 4])
255
+ }
256
+
257
+ const stream = toReadableStreamPonyfill(gen())
258
+ const reader = stream.getReader()
259
+
260
+ const chunk1 = await reader.read()
261
+ expect(chunk1.done).toBe(false)
262
+ expect(chunk1.value).toEqual(new Uint8Array([1, 2]))
263
+
264
+ const chunk2 = await reader.read()
265
+ expect(chunk2.done).toBe(false)
266
+ expect(chunk2.value).toEqual(new Uint8Array([3, 4]))
267
+
268
+ const end = await reader.read()
269
+ expect(end.done).toBe(true)
270
+ })
271
+
272
+ it('handles empty async iterable', async () => {
273
+ async function* gen() {
274
+ // yields nothing
275
+ }
276
+
277
+ const stream = toReadableStreamPonyfill(gen())
278
+ const reader = stream.getReader()
279
+
280
+ const result = await reader.read()
281
+ expect(result.done).toBe(true)
282
+ })
283
+
284
+ it('can be consumed with Response API', async () => {
285
+ async function* gen() {
286
+ yield new TextEncoder().encode('hello ')
287
+ yield new TextEncoder().encode('world')
288
+ }
289
+
290
+ const stream = toReadableStreamPonyfill(gen())
291
+ const response = new Response(stream)
292
+ const text = await response.text()
293
+ expect(text).toBe('hello world')
294
+ })
295
+
296
+ it('propagates errors from the async iterable', async () => {
297
+ async function* gen() {
298
+ yield new Uint8Array([1])
299
+ throw new Error('stream error')
300
+ }
301
+
302
+ const stream = toReadableStreamPonyfill(gen())
303
+ const reader = stream.getReader()
304
+
305
+ await reader.read()
306
+ await expect(reader.read()).rejects.toThrow('stream error')
307
+ })
308
+
309
+ it('calls iterator.return() on cancel', async () => {
310
+ let returned = false
311
+ const iterable: AsyncIterable<Uint8Array> = {
312
+ [Symbol.asyncIterator]() {
313
+ return {
314
+ async next() {
315
+ return { done: false, value: new Uint8Array([1]) }
316
+ },
317
+ async return() {
318
+ returned = true
319
+ return { done: true, value: undefined }
320
+ },
321
+ }
322
+ },
323
+ }
324
+
325
+ const stream = toReadableStreamPonyfill(iterable)
326
+ const reader = stream.getReader()
327
+
328
+ await reader.read()
329
+ await reader.cancel()
330
+
331
+ expect(returned).toBe(true)
332
+ })
333
+ })
package/src/util.ts CHANGED
@@ -104,10 +104,19 @@ export function toReadableStream(
104
104
  data: AsyncIterable<Uint8Array>,
105
105
  ): ReadableStream<Uint8Array> {
106
106
  // Use the native ReadableStream.from() if available.
107
+
108
+ /* v8 ignore next -- @preserve */
107
109
  if ('from' in ReadableStream && typeof ReadableStream.from === 'function') {
108
110
  return ReadableStream.from(data)
109
111
  }
110
112
 
113
+ /* v8 ignore next -- @preserve */
114
+ return toReadableStreamPonyfill(data)
115
+ }
116
+
117
+ export function toReadableStreamPonyfill(
118
+ data: AsyncIterable<Uint8Array>,
119
+ ): ReadableStream<Uint8Array> {
111
120
  let iterator: AsyncIterator<Uint8Array> | undefined
112
121
  return new ReadableStream({
113
122
  async pull(controller) {