-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathHttpResponseStream.js
33 lines (25 loc) · 985 Bytes
/
HttpResponseStream.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
/**
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* HttpResponseStream is NOT used by the runtime.
* It is only exposed in the `awslambda` variable for customers to use.
*/
'use strict';
const METADATA_PRELUDE_CONTENT_TYPE =
'application/vnd.awslambda.http-integration-response';
const DELIMITER_LEN = 8;
// Implements the application/vnd.awslambda.http-integration-response content type.
class HttpResponseStream {
static from(underlyingStream, prelude) {
underlyingStream.setContentType(METADATA_PRELUDE_CONTENT_TYPE);
// JSON.stringify is required. NULL byte is not allowed in metadataPrelude.
const metadataPrelude = JSON.stringify(prelude);
underlyingStream._onBeforeFirstWrite = (write) => {
write(metadataPrelude);
// Write 8 null bytes after the JSON prelude.
write(new Uint8Array(DELIMITER_LEN));
};
return underlyingStream;
}
}
module.exports.HttpResponseStream = HttpResponseStream;