forked from aws/aws-lambda-nodejs-runtime-interface-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRAPIDClient.js
213 lines (200 loc) · 5.75 KB
/
RAPIDClient.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
/**
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* This module defines the RAPID client which is responsible for all HTTP
* interactions with the RAPID layer.
*/
'use strict';
const Errors = require('./Errors');
const XRayError = require('./XRayError');
const ERROR_TYPE_HEADER = 'Lambda-Runtime-Function-Error-Type';
const { createResponseStream } = require('./ResponseStream');
/**
* Objects of this class are responsible for all interactions with the RAPID
* API.
*/
module.exports = class RAPIDClient {
constructor(hostnamePort, httpClient, nativeClient) {
this.http = httpClient || require('http');
this.nativeClient =
nativeClient || require('./NativeModuleLoader.js').load();
this.useAlternativeClient =
process.env['AWS_LAMBDA_NODEJS_USE_ALTERNATIVE_CLIENT_1'] === 'true';
let [hostname, port] = hostnamePort.split(':');
this.hostname = hostname;
this.port = parseInt(port, 10);
this.agent = new this.http.Agent({
keepAlive: true,
maxSockets: 1,
});
}
/**
* Complete and invocation with the provided response.
* @param {Object} response
* An arbitrary object to convert to JSON and send back as as response.
* @param {String} id
* The invocation ID.
* @param {Function} callback
* The callback to run after the POST response ends
*/
postInvocationResponse(response, id, callback) {
let bodyString = _trySerializeResponse(response);
this.nativeClient.done(encodeURIComponent(id), bodyString);
callback();
}
/**
* Stream the invocation response.
* @param {String} id
* The invocation ID.
* @param {Function} callback
* The callback to run after the POST response ends
* @return {object}
* A response stream and a Promise that resolves when the stream is done.
*/
getStreamForInvocationResponse(id, callback, options) {
const ret = createResponseStream({
httpOptions: {
agent: this.agent,
http: this.http,
hostname: this.hostname,
method: 'POST',
port: this.port,
path:
'/2018-06-01/runtime/invocation/' +
encodeURIComponent(id) +
'/response',
highWaterMark: options?.highWaterMark,
},
});
return {
request: ret.request,
responseDone: ret.responseDone.then((_) => {
if (callback) {
callback();
}
}),
};
}
/**
* Post an initialization error to the RAPID API.
* @param {Error} error
* @param {Function} callback
* The callback to run after the POST response ends
*/
postInitError(error, callback) {
let response = Errors.toRapidResponse(error);
this._post(
`/2018-06-01/runtime/init/error`,
response,
{ [ERROR_TYPE_HEADER]: response.errorType },
callback,
);
}
/**
* Post an invocation error to the RAPID API
* @param {Error} error
* @param {String} id
* The invocation ID for the in-progress invocation.
* @param {Function} callback
* The callback to run after the POST response ends
*/
postInvocationError(error, id, callback) {
let response = Errors.toRapidResponse(error);
let bodyString = _trySerializeResponse(response);
let xrayString = XRayError.formatted(error);
this.nativeClient.error(encodeURIComponent(id), bodyString, xrayString);
callback();
}
/**
* Get the next invocation.
* @return {PromiseLike.<Object>}
* A promise which resolves to an invocation object that contains the body
* as json and the header array. e.g. {bodyJson, headers}
*/
async nextInvocation() {
if (this.useAlternativeClient) {
const options = {
hostname: this.hostname,
port: this.port,
path: '/2018-06-01/runtime/invocation/next',
method: 'GET',
agent: this.agent,
};
return new Promise((resolve, reject) => {
let request = this.http.request(options, (response) => {
let data = '';
response
.setEncoding('utf-8')
.on('data', (chunk) => {
data += chunk;
})
.on('end', () => {
resolve({
bodyJson: data,
headers: response.headers,
});
});
});
request
.on('error', (e) => {
reject(e);
})
.end();
});
}
return this.nativeClient.next();
}
/**
* HTTP Post to a path.
* @param {String} path
* @param {Object} body
* The body is serialized into JSON before posting.
* @param {Object} headers
* The http headers
* @param (function()} callback
* The callback to run after the POST response ends
*/
_post(path, body, headers, callback) {
let bodyString = _trySerializeResponse(body);
const options = {
hostname: this.hostname,
port: this.port,
path: path,
method: 'POST',
headers: Object.assign(
{
'Content-Type': 'application/json',
'Content-Length': Buffer.from(bodyString).length,
},
headers || {},
),
agent: this.agent,
};
let request = this.http.request(options, (response) => {
response
.on('end', () => {
callback();
})
.on('error', (e) => {
throw e;
})
.on('data', () => {});
});
request
.on('error', (e) => {
throw e;
})
.end(bodyString, 'utf-8');
}
};
/**
* Attempt to serialize an object as json. Capture the failure if it occurs and
* throw one that's known to be serializable.
*/
function _trySerializeResponse(body) {
try {
return JSON.stringify(body === undefined ? null : body);
} catch (err) {
throw new Error('Unable to stringify response body');
}
}