forked from aws/aws-lambda-nodejs-runtime-interface-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponseStreamTest.js
577 lines (481 loc) · 16.6 KB
/
ResponseStreamTest.js
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/**
* Copyright 2021-2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*/
'use strict';
require('should');
const ServerMock = require('mock-http-server');
const {
createResponseStream,
tryCallFail,
} = require('lambda-runtime/ResponseStream.js');
const { HttpResponseStream } = require('lambda-runtime/HttpResponseStream.js');
const { InvalidStreamingOperation } = require('lambda-runtime/Errors.js');
const { verbose, vverbose, vvverbose } = require('lambda-runtime/VerboseLog').logger(
'TEST',
);
const Throttle = require('throttle');
const pipeline = require('util').promisify(require('stream').pipeline);
const { Readable } = require('stream');
const { Buffer } = require('buffer');
const http = require('http');
/**
* Node.js 17 changed the default DNS resolution ordering from always prioritizing ipv4 to the ordering
* returned by the DNS provider. In some environments, this can result in `localhost` resolving to
* an ipv6 address instead of ipv4 and a consequent failure to connect to the server.
* Hence, using `127.0.0.1` in place of localhost.
* ref: https://github.com/nodejs/node/pull/39987
*/
const HOSTNAME = '127.0.0.1';
const PATH = '/asd/efg';
const createStream = (port, options) => {
const {
request: stream,
headersDone,
responseDone: streamDone,
} = createResponseStream({
httpOptions: {
hostname: HOSTNAME,
port,
path: PATH,
method: options?.method || 'POST',
http: options?.http || require('http'),
agent: new http.Agent({
// keepAlive: true,
// maxSockets: 1,
}),
},
contentType: options?.contentType,
});
return { stream, headersDone, streamDone };
};
const streamSpy = (name, stream) =>
stream
.on('end', () => verbose(name, 'ended'))
.on('close', () => verbose(name, 'closed'))
.on('finish', () => verbose(name, 'finished'))
.on('drain', () => verbose(name, 'drained'))
.on('error', () => verbose(name, 'errored'))
.on('pause', () => verbose(name, 'paused'))
.on('readable', () => verbose(name, 'readabled'))
.on('resume', () => verbose(name, 'resumed'));
const assertStream = async (port, useStream) => {
const { stream, headersDone, streamDone } = createStream(port);
streamSpy('test stream', stream);
vverbose('assertStream 1');
await useStream(stream);
vverbose('assertStream 2');
// auto-close.
stream.end();
vverbose('assertStream 3');
const info = await headersDone;
vverbose('assertStream 4');
const body = await streamDone;
vverbose('assertStream 5');
const response = {
statusCode: info.statusCode,
headers: info.headers,
body,
};
vverbose('assertStream 6');
return response;
};
const destroyConnections = (server) => {
server.connections().forEach((c) => c.destroy());
};
describe('stream - throws uncaughtException', () => {
const server = new ServerMock({ host: HOSTNAME, port: 0 });
let originalListener;
before(function (done) {
server.start(done);
});
after(function () {
process.addListener('uncaughtException', originalListener);
});
it('no end will timeout', function (done) {
this.timeout(1000); //increase timeout.
originalListener = process.listeners('uncaughtException').pop();
process.removeListener('uncaughtException', originalListener);
const { stream, streamDone } = createStream(server.getHttpPort());
streamSpy('ender', stream);
stream.setContentType('moon/dust');
// Write to the stream, do not call .end
stream.write(Buffer.from([1]));
process.prependOnceListener('uncaughtException', (err) => {
should(err).be.not.empty();
err.code.should.be.equal('ECONNRESET');
});
setTimeout(() => {
stream.writableFinished.should.be.false();
stream.end();
streamDone.then(() => {
destroyConnections(server);
server.stop(done);
});
}, 500);
});
});
describe('stream/pipeline', function () {
this.timeout(10000);
const server = new ServerMock({ host: HOSTNAME, port: 0 });
beforeEach(function (done) {
server.start(done);
});
afterEach(function (done) {
server.stop(done);
});
it('can pipeline', async () => {
const { stream, streamDone } = createStream(server.getHttpPort());
stream.setContentType('application/octet-stream');
const input = Readable.from(Buffer.from('moon'));
await pipeline(input, stream);
await streamDone;
const reqs = server.requests();
reqs.length.should.be.equal(1);
reqs[0].body.should.be.equal('moon');
});
it('can pipeline with throttle', async () => {
const { stream, streamDone } = createStream(server.getHttpPort());
stream.setContentType('application/octet-stream');
const input = Readable.from(Buffer.from('moon '.repeat(1000)));
await pipeline(input, new Throttle({ bps: 20000, chunkSize: 2 }), stream);
await streamDone;
const reqs = server.requests();
reqs.length.should.be.equal(1);
reqs[0].body.should.be.equal('moon '.repeat(1000));
});
it('can pipeline with throttle 2', async () => {
const { stream, streamDone } = createStream(server.getHttpPort());
stream.setContentType('application/octet-stream');
const input = Readable.from(Buffer.from('moon'));
await pipeline(input, new Throttle({ bps: 1, chunkSize: 1 }), stream);
await streamDone;
const reqs = server.requests();
reqs.length.should.be.equal(1);
reqs[0].body.should.be.equal('moon');
});
it('can pipeline generator function', async () => {
async function* generateContent() {
// Push 1 MiB of data in 1 KiB chunks
for (let i = 1; i <= 100; i++) {
yield (i % 10).toString().repeat(1024); // push 1 KiB to the stream
}
}
const { stream, streamDone } = createStream(server.getHttpPort());
stream.setContentType('application/octet-stream');
const input = generateContent();
await pipeline(input, stream);
await streamDone;
const reqs = server.requests();
reqs.length.should.be.equal(1);
let expected = '';
for await (let s of generateContent()) {
expected += s;
}
reqs[0].body.should.be.equal(expected);
});
});
describe('stream', () => {
const server = new ServerMock({ host: HOSTNAME, port: 0 });
beforeEach(function (done) {
server.start(done);
});
afterEach(function (done) {
server.stop(done);
});
it('write returns true', async () => {
const { stream, streamDone } = createStream(server.getHttpPort());
stream.setContentType('application/octet-stream');
const result = stream.write('moon');
result.should.be.true();
await new Promise((r) => stream.end(r));
await streamDone;
const reqs = server.requests();
reqs.length.should.be.equal(1);
reqs[0].body.should.be.equal('moon');
});
it('default content type', (done) => {
const { stream, streamDone } = createStream(server.getHttpPort());
stream.end(() => {
streamDone.then(() => {
const reqs = server.requests();
reqs.length.should.be.equal(1);
reqs[0].headers['content-type'].should.be.equal(
'application/octet-stream',
);
reqs[0].headers[
'lambda-runtime-function-response-mode'
].should.be.equal('streaming');
done();
});
});
});
it('expect error trailer', async () => {
const { stream, streamDone } = createStream(server.getHttpPort());
await new Promise((r) => stream.end(r));
await streamDone;
const reqs = server.requests();
reqs.length.should.be.equal(1);
reqs[0].headers['trailer'].should.be.equal(
'Lambda-Runtime-Function-Error-Type, Lambda-Runtime-Function-Error-Body',
);
reqs[0].headers['lambda-runtime-function-response-mode'].should.be.equal(
'streaming',
);
});
it('chunked encoding', async () => {
const { stream, streamDone } = createStream(server.getHttpPort());
await new Promise((r) => stream.end(r));
await streamDone;
const reqs = server.requests();
reqs.length.should.be.equal(1);
reqs[0].headers['transfer-encoding'].should.be.equal('chunked');
reqs[0].headers['lambda-runtime-function-response-mode'].should.be.equal(
'streaming',
);
});
it("setContentType doesn't throw when constructed", async () => {
const { stream, streamDone } = createStream(server.getHttpPort());
stream.setContentType('moon/dust');
await new Promise((r) => stream.end(r));
await streamDone;
const reqs = server.requests();
reqs.length.should.be.equal(1);
reqs[0].headers['content-type'].should.be.equal('moon/dust');
reqs[0].headers['lambda-runtime-function-response-mode'].should.be.equal(
'streaming',
);
});
it('content type from options', async () => {
const { stream, streamDone } = createStream(server.getHttpPort(), {
contentType: 'moon/dust',
});
await new Promise((r) => stream.end(r));
await streamDone;
const reqs = server.requests();
reqs.length.should.be.equal(1);
reqs[0].headers['content-type'].should.be.equal('moon/dust');
reqs[0].headers['lambda-runtime-function-response-mode'].should.be.equal(
'streaming',
);
});
it('override content type from options', async () => {
const { stream, streamDone } = createStream(server.getHttpPort(), {
contentType: 'moon/flake',
});
stream.setContentType('moon/dust');
await new Promise((r) => stream.end(r));
await streamDone;
const reqs = server.requests();
reqs.length.should.be.equal(1);
reqs[0].headers['content-type'].should.be.equal('moon/dust');
reqs[0].headers['lambda-runtime-function-response-mode'].should.be.equal(
'streaming',
);
});
it('override content type, metadata prelude', async () => {
let { stream, streamDone } = createStream(server.getHttpPort(), {
contentType: 'moon/flake',
});
stream = HttpResponseStream.from(stream, {
loc: 'mare\x00tranquillitatis',
});
stream.write('ABC');
await new Promise((r) => stream.end(r));
await streamDone;
const reqs = server.requests();
reqs.length.should.be.equal(1);
reqs[0].headers['content-type'].should.be.equal(
'application/vnd.awslambda.http-integration-response',
);
reqs[0].headers['lambda-runtime-function-response-mode'].should.be.equal(
'streaming',
);
// Null in the object value is escaped, prelude added followed by a null byte.
reqs[0].body.should.be.equal(
'{"loc":"mare\\u0000tranquillitatis"}\u0000\u0000\u0000\u0000\u0000\u0000\x00\u0000ABC',
);
});
it('metadata prelude, do not use return value', async () => {
let { stream, streamDone } = createStream(server.getHttpPort());
// Ignore the return value.
HttpResponseStream.from(stream, {
loc: 'mare\x00tranquillitatis',
});
stream.write('ABC');
await new Promise((r) => stream.end(r));
await streamDone;
const reqs = server.requests();
reqs.length.should.be.equal(1);
reqs[0].headers['content-type'].should.be.equal(
'application/vnd.awslambda.http-integration-response',
);
reqs[0].headers['lambda-runtime-function-response-mode'].should.be.equal(
'streaming',
);
// Null in the object value is escaped, prelude added followed by a null byte.
reqs[0].body.should.be.equal(
'{"loc":"mare\\u0000tranquillitatis"}\x00\x00\x00\x00\x00\x00\x00\x00ABC',
);
});
it('setContentType throws after first write', (done) => {
const { stream, streamDone } = createStream(server.getHttpPort());
stream.write(Buffer.from(JSON.stringify({})));
should(() => stream.setContentType('moon/trust')).throw({
name: 'Runtime.InvalidStreamingOperation',
message: 'Cannot set content-type, too late.',
});
stream.end(() => streamDone.then(() => done()));
});
it('setContentType throws after first write, non-default', (done) => {
const { stream, streamDone } = createStream(server.getHttpPort());
stream.setContentType('moon/dust');
stream.write(Buffer.from([1, 2, 3, 2, 1]));
should(() => stream.setContentType('moon/trust')).throw({
name: 'Runtime.InvalidStreamingOperation',
message: 'Cannot set content-type, too late.',
});
stream.end(() => streamDone.then(() => done()));
});
it('send error in trailer', (done) => {
const { stream, streamDone } = createStream(server.getHttpPort(), {});
tryCallFail(stream, 42);
stream.end(() => {
streamDone.then(() => {
const reqs = server.requests();
reqs.length.should.be.equal(1);
reqs[0].trailers['lambda-runtime-function-error-type'].should.be.equal(
'number',
);
const body = JSON.parse(
Buffer.from(
reqs[0].trailers['lambda-runtime-function-error-body'],
'base64',
).toString(),
);
body.should.be.eql({
errorType: 'number',
errorMessage: '42',
trace: [],
});
done();
});
});
});
it('send InvalidStreamingOperation in trailer', (done) => {
const { stream, streamDone } = createStream(server.getHttpPort(), {});
tryCallFail(
stream,
new InvalidStreamingOperation('Cannot set content-type, too late.'),
);
stream.end(() => {
streamDone.then(() => {
const reqs = server.requests();
reqs.length.should.be.equal(1);
reqs[0].trailers['lambda-runtime-function-error-type'].should.be.equal(
'Runtime.InvalidStreamingOperation',
);
const body = JSON.parse(
Buffer.from(
reqs[0].trailers['lambda-runtime-function-error-body'],
'base64',
).toString(),
);
body.errorType.should.be.equal('Runtime.InvalidStreamingOperation');
body.errorMessage.should.be.equal('Cannot set content-type, too late.');
body.trace.should.be.not.empty();
done();
});
});
});
it('send error in trailer, callback from fail', (done) => {
const { stream, streamDone } = createStream(server.getHttpPort(), {});
tryCallFail(stream, 42, () => {
streamDone.then(() => {
const reqs = server.requests();
reqs.length.should.be.equal(1);
reqs[0].trailers['lambda-runtime-function-error-type'].should.be.equal(
'number',
);
const body = JSON.parse(
Buffer.from(
reqs[0].trailers['lambda-runtime-function-error-body'],
'base64',
).toString(),
);
body.should.be.eql({
errorType: 'number',
errorMessage: '42',
trace: [],
});
done();
});
});
});
const TypeErrorMsg =
'The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Object';
[0, 1, 2, 100, 1000].forEach((repeat) => {
[
{
name: 'object, should fail',
value: { moon: 'dust' },
error: { code: 'ERR_INVALID_ARG_TYPE', message: TypeErrorMsg },
},
{
name: 'string',
value: JSON.stringify({ moon: 'dust' }),
expected: '{"moon":"dust"}',
},
{
name: 'buffer',
value: Buffer.from(JSON.stringify({ moon: 'dust' })),
expected: '{"moon":"dust"}',
},
{
name: 'Uint8Array',
value: new TextEncoder().encode(JSON.stringify({ moon: 'dust' })),
expected: '{"moon":"dust"}',
},
].forEach((v) => {
it(`write ${repeat} ${v.name}`, async () => {
server.on({
method: 'POST',
path: PATH,
reply: {
status: 200,
headers: { 'content-type': 'application/octet-stream' },
body: (req) => {
return req.body;
},
},
});
const response = await assertStream(server.getHttpPort(), (stream) => {
stream.setContentType('moon/sparkle');
for (let i = 1; i <= repeat; i++) {
try {
stream.write(v.value);
} catch (err) {
if (!v.error) {
throw err;
}
if (v.error.code) {
err.code.should.be.equal(v.error.code);
}
if (v.error.message) {
err.message.should.be.equal(v.error.message);
}
}
}
stream.end();
});
vvverbose('response', response);
response.statusCode.should.be.equal(200);
if (v.error) {
return;
}
const expected = v.expected.repeat(repeat);
const body = response.body ? response.body.toString('utf-8') : '';
body.should.be.equal(expected);
});
});
});
});