-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathstreaming.test.ts
323 lines (262 loc) · 10.7 KB
/
streaming.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { Response } from 'node-fetch';
import { PassThrough } from 'stream';
import assert from 'assert';
import { _iterSSEMessages, _decodeChunks as decodeChunks } from 'openai/streaming';
import { LineDecoder } from 'openai/internal/decoders/line';
describe('line decoder', () => {
test('basic', () => {
// baz is not included because the line hasn't ended yet
expect(decodeChunks(['foo', ' bar\nbaz'])).toEqual(['foo bar']);
});
test('basic with \\r', () => {
expect(decodeChunks(['foo', ' bar\r\nbaz'])).toEqual(['foo bar']);
expect(decodeChunks(['foo', ' bar\r\nbaz'], { flush: true })).toEqual(['foo bar', 'baz']);
});
test('trailing new lines', () => {
expect(decodeChunks(['foo', ' bar', 'baz\n', 'thing\n'])).toEqual(['foo barbaz', 'thing']);
});
test('trailing new lines with \\r', () => {
expect(decodeChunks(['foo', ' bar', 'baz\r\n', 'thing\r\n'])).toEqual(['foo barbaz', 'thing']);
});
test('escaped new lines', () => {
expect(decodeChunks(['foo', ' bar\\nbaz\n'])).toEqual(['foo bar\\nbaz']);
});
test('escaped new lines with \\r', () => {
expect(decodeChunks(['foo', ' bar\\r\\nbaz\n'])).toEqual(['foo bar\\r\\nbaz']);
});
test('\\r & \\n split across multiple chunks', () => {
expect(decodeChunks(['foo\r', '\n', 'bar'], { flush: true })).toEqual(['foo', 'bar']);
});
test('single \\r', () => {
expect(decodeChunks(['foo\r', 'bar'], { flush: true })).toEqual(['foo', 'bar']);
});
test('double \\r', () => {
expect(decodeChunks(['foo\r', 'bar\r'], { flush: true })).toEqual(['foo', 'bar']);
expect(decodeChunks(['foo\r', '\r', 'bar'], { flush: true })).toEqual(['foo', '', 'bar']);
// implementation detail that we don't yield the single \r line until a new \r or \n is encountered
expect(decodeChunks(['foo\r', '\r', 'bar'], { flush: false })).toEqual(['foo']);
});
test('double \\r then \\r\\n', () => {
expect(decodeChunks(['foo\r', '\r', '\r', '\n', 'bar', '\n'])).toEqual(['foo', '', '', 'bar']);
expect(decodeChunks(['foo\n', '\n', '\n', 'bar', '\n'])).toEqual(['foo', '', '', 'bar']);
});
test('double newline', () => {
expect(decodeChunks(['foo\n\nbar'], { flush: true })).toEqual(['foo', '', 'bar']);
expect(decodeChunks(['foo', '\n', '\nbar'], { flush: true })).toEqual(['foo', '', 'bar']);
expect(decodeChunks(['foo\n', '\n', 'bar'], { flush: true })).toEqual(['foo', '', 'bar']);
expect(decodeChunks(['foo', '\n', '\n', 'bar'], { flush: true })).toEqual(['foo', '', 'bar']);
});
test('multi-byte characters across chunks', () => {
const decoder = new LineDecoder();
// bytes taken from the string 'известни' and arbitrarily split
// so that some multi-byte characters span multiple chunks
expect(decoder.decode(new Uint8Array([0xd0]))).toHaveLength(0);
expect(decoder.decode(new Uint8Array([0xb8, 0xd0, 0xb7, 0xd0]))).toHaveLength(0);
expect(
decoder.decode(new Uint8Array([0xb2, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82, 0xd0, 0xbd, 0xd0, 0xb8])),
).toHaveLength(0);
const decoded = decoder.decode(new Uint8Array([0xa]));
expect(decoded).toEqual(['известни']);
});
test('flushing trailing newlines', () => {
expect(decodeChunks(['foo\n', '\nbar'], { flush: true })).toEqual(['foo', '', 'bar']);
});
test('flushing empty buffer', () => {
expect(decodeChunks([], { flush: true })).toEqual([]);
});
});
describe('streaming decoding', () => {
test('basic', async () => {
async function* body(): AsyncGenerator<Buffer> {
yield Buffer.from('event: completion\n');
yield Buffer.from('data: {"foo":true}\n');
yield Buffer.from('\n');
}
const stream = _iterSSEMessages(new Response(await iteratorToStream(body())), new AbortController())[
Symbol.asyncIterator
]();
let event = await stream.next();
assert(event.value);
expect(JSON.parse(event.value.data)).toEqual({ foo: true });
event = await stream.next();
expect(event.done).toBeTruthy();
});
test('data without event', async () => {
async function* body(): AsyncGenerator<Buffer> {
yield Buffer.from('data: {"foo":true}\n');
yield Buffer.from('\n');
}
const stream = _iterSSEMessages(new Response(await iteratorToStream(body())), new AbortController())[
Symbol.asyncIterator
]();
let event = await stream.next();
assert(event.value);
expect(event.value.event).toBeNull();
expect(JSON.parse(event.value.data)).toEqual({ foo: true });
event = await stream.next();
expect(event.done).toBeTruthy();
});
test('event without data', async () => {
async function* body(): AsyncGenerator<Buffer> {
yield Buffer.from('event: foo\n');
yield Buffer.from('\n');
}
const stream = _iterSSEMessages(new Response(await iteratorToStream(body())), new AbortController())[
Symbol.asyncIterator
]();
let event = await stream.next();
assert(event.value);
expect(event.value.event).toEqual('foo');
expect(event.value.data).toEqual('');
event = await stream.next();
expect(event.done).toBeTruthy();
});
test('multiple events', async () => {
async function* body(): AsyncGenerator<Buffer> {
yield Buffer.from('event: foo\n');
yield Buffer.from('\n');
yield Buffer.from('event: ping\n');
yield Buffer.from('\n');
}
const stream = _iterSSEMessages(new Response(await iteratorToStream(body())), new AbortController())[
Symbol.asyncIterator
]();
let event = await stream.next();
assert(event.value);
expect(event.value.event).toEqual('foo');
expect(event.value.data).toEqual('');
event = await stream.next();
assert(event.value);
expect(event.value.event).toEqual('ping');
expect(event.value.data).toEqual('');
event = await stream.next();
expect(event.done).toBeTruthy();
});
test('multiple events with data', async () => {
async function* body(): AsyncGenerator<Buffer> {
yield Buffer.from('event: foo\n');
yield Buffer.from('data: {"foo":true}\n');
yield Buffer.from('\n');
yield Buffer.from('event: ping\n');
yield Buffer.from('data: {"bar":false}\n');
yield Buffer.from('\n');
}
const stream = _iterSSEMessages(new Response(await iteratorToStream(body())), new AbortController())[
Symbol.asyncIterator
]();
let event = await stream.next();
assert(event.value);
expect(event.value.event).toEqual('foo');
expect(JSON.parse(event.value.data)).toEqual({ foo: true });
event = await stream.next();
assert(event.value);
expect(event.value.event).toEqual('ping');
expect(JSON.parse(event.value.data)).toEqual({ bar: false });
event = await stream.next();
expect(event.done).toBeTruthy();
});
test('multiple data lines with empty line', async () => {
async function* body(): AsyncGenerator<Buffer> {
yield Buffer.from('event: ping\n');
yield Buffer.from('data: {\n');
yield Buffer.from('data: "foo":\n');
yield Buffer.from('data: \n');
yield Buffer.from('data:\n');
yield Buffer.from('data: true}\n');
yield Buffer.from('\n\n');
}
const stream = _iterSSEMessages(new Response(await iteratorToStream(body())), new AbortController())[
Symbol.asyncIterator
]();
let event = await stream.next();
assert(event.value);
expect(event.value.event).toEqual('ping');
expect(JSON.parse(event.value.data)).toEqual({ foo: true });
expect(event.value.data).toEqual('{\n"foo":\n\n\ntrue}');
event = await stream.next();
expect(event.done).toBeTruthy();
});
test('data json escaped double new line', async () => {
async function* body(): AsyncGenerator<Buffer> {
yield Buffer.from('event: ping\n');
yield Buffer.from('data: {"foo": "my long\\n\\ncontent"}');
yield Buffer.from('\n\n');
}
const stream = _iterSSEMessages(new Response(await iteratorToStream(body())), new AbortController())[
Symbol.asyncIterator
]();
let event = await stream.next();
assert(event.value);
expect(event.value.event).toEqual('ping');
expect(JSON.parse(event.value.data)).toEqual({ foo: 'my long\n\ncontent' });
event = await stream.next();
expect(event.done).toBeTruthy();
});
test('special new line characters', async () => {
async function* body(): AsyncGenerator<Buffer> {
yield Buffer.from('data: {"content": "culpa "}\n');
yield Buffer.from('\n');
yield Buffer.from('data: {"content": "');
yield Buffer.from([0xe2, 0x80, 0xa8]);
yield Buffer.from('"}\n');
yield Buffer.from('\n');
yield Buffer.from('data: {"content": "foo"}\n');
yield Buffer.from('\n');
}
const stream = _iterSSEMessages(new Response(await iteratorToStream(body())), new AbortController())[
Symbol.asyncIterator
]();
let event = await stream.next();
assert(event.value);
expect(JSON.parse(event.value.data)).toEqual({ content: 'culpa ' });
event = await stream.next();
assert(event.value);
expect(JSON.parse(event.value.data)).toEqual({ content: Buffer.from([0xe2, 0x80, 0xa8]).toString() });
event = await stream.next();
assert(event.value);
expect(JSON.parse(event.value.data)).toEqual({ content: 'foo' });
event = await stream.next();
expect(event.done).toBeTruthy();
});
test('multi-byte characters across chunks', async () => {
async function* body(): AsyncGenerator<Buffer> {
yield Buffer.from('event: completion\n');
yield Buffer.from('data: {"content": "');
// bytes taken from the string 'известни' and arbitrarily split
// so that some multi-byte characters span multiple chunks
yield Buffer.from([0xd0]);
yield Buffer.from([0xb8, 0xd0, 0xb7, 0xd0]);
yield Buffer.from([0xb2, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82, 0xd0, 0xbd, 0xd0, 0xb8]);
yield Buffer.from('"}\n');
yield Buffer.from('\n');
}
const stream = _iterSSEMessages(new Response(await iteratorToStream(body())), new AbortController())[
Symbol.asyncIterator
]();
let event = await stream.next();
assert(event.value);
expect(event.value.event).toEqual('completion');
expect(JSON.parse(event.value.data)).toEqual({ content: 'известни' });
event = await stream.next();
expect(event.done).toBeTruthy();
});
});
async function iteratorToStream(iterator: AsyncGenerator<any>): Promise<PassThrough> {
const parts: unknown[] = [];
for await (const chunk of iterator) {
parts.push(chunk);
}
let index = 0;
const stream = new PassThrough({
read() {
const value = parts[index];
if (value === undefined) {
stream.end();
} else {
index += 1;
stream.write(value);
}
},
});
return stream;
}