@koeroesi86/node-lambda-invoke 1.0.21-d1fa702 → 26.10.36900076505-master
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.
- package/README.md +4 -4
- package/dist/classes/FileStorage.d.ts +18 -0
- package/dist/classes/FileStorage.js +62 -0
- package/dist/classes/FileStorage.js.map +1 -0
- package/dist/classes/IPCStorage.d.ts +26 -0
- package/dist/classes/IPCStorage.js +208 -0
- package/dist/classes/IPCStorage.js.map +1 -0
- package/dist/classes/Lambda.d.ts +30 -0
- package/dist/classes/Lambda.js +100 -0
- package/dist/classes/Lambda.js.map +1 -0
- package/dist/classes/LambdaPool.d.ts +19 -0
- package/dist/classes/LambdaPool.js +74 -0
- package/dist/classes/LambdaPool.js.map +1 -0
- package/dist/classes/RequestEvent.d.ts +13 -0
- package/dist/classes/RequestEvent.js +6 -0
- package/dist/classes/RequestEvent.js.map +1 -0
- package/dist/classes/ResponseEvent.d.ts +8 -0
- package/dist/classes/ResponseEvent.js +6 -0
- package/dist/classes/ResponseEvent.js.map +1 -0
- package/dist/classes/Worker.d.ts +20 -0
- package/dist/classes/Worker.js +77 -0
- package/dist/classes/Worker.js.map +1 -0
- package/dist/constants/index.d.ts +5 -0
- package/dist/constants/index.js +10 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/middlewares/http.d.ts +5 -0
- package/dist/middlewares/http.js +75 -0
- package/dist/middlewares/http.js.map +1 -0
- package/dist/middlewares/invoke.d.ts +1 -0
- package/dist/middlewares/invoke.js +48 -0
- package/dist/middlewares/invoke.js.map +1 -0
- package/dist/middlewares/stdoutListener.d.ts +4 -0
- package/dist/middlewares/stdoutListener.js +27 -0
- package/dist/middlewares/stdoutListener.js.map +1 -0
- package/dist/registry/communication.json +12 -0
- package/dist/registry/index.d.ts +2 -0
- package/dist/registry/index.js +19 -0
- package/dist/registry/index.js.map +1 -0
- package/dist/types.d.ts +36 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/sendToParent.d.ts +3 -0
- package/dist/utils/sendToParent.js +10 -0
- package/dist/utils/sendToParent.js.map +1 -0
- package/example/example.js +1 -1
- package/package.json +28 -11
- package/.travis.yml +0 -16
- package/classes/FileStorage.js +0 -114
- package/classes/IPCStorage.js +0 -216
- package/classes/Lambda.js +0 -124
- package/classes/LambdaPool.js +0 -78
- package/classes/RequestEvent.js +0 -15
- package/classes/ResponseEvent.js +0 -12
- package/classes/Worker.js +0 -93
- package/constants/index.js +0 -8
- package/index.js +0 -9
- package/middlewares/http.js +0 -111
- package/middlewares/invoke.js +0 -50
- package/middlewares/stdoutListener.js +0 -27
- package/registry/communication.json +0 -12
- package/scripts/version.js +0 -10
package/classes/FileStorage.js
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
const { resolve } = require('path');
|
|
2
|
-
const {
|
|
3
|
-
unlink,
|
|
4
|
-
readFile,
|
|
5
|
-
writeFile,
|
|
6
|
-
existsSync,
|
|
7
|
-
mkdirSync,
|
|
8
|
-
} = require('fs');
|
|
9
|
-
const rimraf = require('rimraf');
|
|
10
|
-
|
|
11
|
-
const serializer = {
|
|
12
|
-
serialize: data => JSON.stringify(data),
|
|
13
|
-
deserialize: data => JSON.parse(data),
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @type {{restore: (function(*=): Promise), save: (function(*=, *=): Promise), destroy: (function(*=): Promise)}}
|
|
18
|
-
*/
|
|
19
|
-
const Driver = {
|
|
20
|
-
save: (path, data) => new Promise((res, rej) =>
|
|
21
|
-
writeFile(path, data, 'utf8', err => err ? rej(err) : res())
|
|
22
|
-
),
|
|
23
|
-
restore: path => new Promise((res, rej) =>
|
|
24
|
-
readFile(path, 'utf8', (err, data) => err ? rej(err) : res(data))
|
|
25
|
-
),
|
|
26
|
-
destroy: path => new Promise((res, rej) =>
|
|
27
|
-
existsSync(path) ? unlink(path, err => err ? rej(err) : res()) : setTimeout(res, 0)
|
|
28
|
-
),
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
class FileStorage {
|
|
33
|
-
/**
|
|
34
|
-
* @param {string} id
|
|
35
|
-
*/
|
|
36
|
-
constructor(id) {
|
|
37
|
-
this.id = id;
|
|
38
|
-
|
|
39
|
-
this.setResponse = this.setResponse.bind(this);
|
|
40
|
-
this.getResponse = this.getResponse.bind(this);
|
|
41
|
-
this.setRequest = this.setRequest.bind(this);
|
|
42
|
-
this.getRequest = this.getRequest.bind(this);
|
|
43
|
-
this.destroy = this.destroy.bind(this);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* @returns {string}
|
|
48
|
-
*/
|
|
49
|
-
get requestPath() {
|
|
50
|
-
return resolve(FileStorage.requestBase, `./${this.id}`);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* @returns {string}
|
|
55
|
-
*/
|
|
56
|
-
get responsePath() {
|
|
57
|
-
return resolve(FileStorage.responseBase, `./${this.id}`);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* @param {ResponseEvent} response
|
|
62
|
-
* @returns {Promise}
|
|
63
|
-
*/
|
|
64
|
-
setResponse(response) {
|
|
65
|
-
return Driver.save(this.responsePath, serializer.serialize(response));
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* @returns {Promise<ResponseEvent>}
|
|
70
|
-
*/
|
|
71
|
-
getResponse() {
|
|
72
|
-
return Driver.restore(this.responsePath)
|
|
73
|
-
.then(data => Promise.resolve(serializer.deserialize(data)));
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* @param {RequestEvent} request
|
|
78
|
-
* @returns {Promise}
|
|
79
|
-
*/
|
|
80
|
-
setRequest(request) {
|
|
81
|
-
return Driver.save(this.requestPath, serializer.serialize(request));
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* @returns {Promise<RequestEvent>}
|
|
86
|
-
*/
|
|
87
|
-
getRequest() {
|
|
88
|
-
return Driver.restore(this.requestPath)
|
|
89
|
-
.then(data => Promise.resolve(serializer.deserialize(data)));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* @returns {Promise}
|
|
94
|
-
*/
|
|
95
|
-
destroy() {
|
|
96
|
-
return Promise.all([
|
|
97
|
-
Driver.destroy(this.responsePath),
|
|
98
|
-
Driver.destroy(this.requestPath)
|
|
99
|
-
]);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
FileStorage.requestBase = resolve(__dirname, `../requests/`);
|
|
104
|
-
FileStorage.responseBase = resolve(__dirname, `../responses/`);
|
|
105
|
-
|
|
106
|
-
FileStorage.start = () => {
|
|
107
|
-
rimraf.sync(resolve(FileStorage.requestBase, './*'));
|
|
108
|
-
rimraf.sync(resolve(FileStorage.responseBase, './*'));
|
|
109
|
-
|
|
110
|
-
if (!existsSync(FileStorage.requestBase)) mkdirSync(FileStorage.requestBase, { recursive: true });
|
|
111
|
-
if (!existsSync(FileStorage.responseBase)) mkdirSync(FileStorage.responseBase, { recursive: true });
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
module.exports = FileStorage;
|
package/classes/IPCStorage.js
DELETED
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
const Lambda = require('./Lambda');
|
|
2
|
-
|
|
3
|
-
let storage = {};
|
|
4
|
-
|
|
5
|
-
const MESSAGES = {
|
|
6
|
-
GET_RESONSE: 'GW_GET_RESONSE',
|
|
7
|
-
SET_RESONSE: 'GW_SET_RESONSE',
|
|
8
|
-
SET_RESONSE_FINISHED: 'GW_SET_RESONSE_FINISHED',
|
|
9
|
-
GET_REQUEST: 'GW_GET_REQUEST',
|
|
10
|
-
SET_REQUEST: 'GW_SET_REQUEST',
|
|
11
|
-
SET_REQUEST_FINISHED: 'GW_SET_REQUEST_FINISHED',
|
|
12
|
-
DESTROY: 'GW_DESTROY',
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const getRequestKey = id => `request-${id}`;
|
|
16
|
-
const getResponseKey = id => `response-${id}`;
|
|
17
|
-
|
|
18
|
-
process.on('message', message => {
|
|
19
|
-
if (message.type === MESSAGES.SET_REQUEST) {
|
|
20
|
-
const id = getRequestKey(message.id);
|
|
21
|
-
storage[id] = message.payload;
|
|
22
|
-
process.send({ type: MESSAGES.SET_REQUEST_FINISHED, id: message.id });
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (message.type === MESSAGES.SET_RESONSE) {
|
|
26
|
-
const id = getResponseKey(message.id);
|
|
27
|
-
storage[id] = message.payload;
|
|
28
|
-
process.send({ type: MESSAGES.SET_RESONSE_FINISHED, id: message.id });
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// if (message.type === MESSAGES.DESTROY) {
|
|
32
|
-
// const requestId = getResponseKey(message.id);
|
|
33
|
-
// delete storage[requestId];
|
|
34
|
-
// const responseId = getResponseKey(message.id);
|
|
35
|
-
// delete storage[responseId];
|
|
36
|
-
// }
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
class IPCStorage {
|
|
40
|
-
/**
|
|
41
|
-
* @param {string} id
|
|
42
|
-
* @param {Process|Lambda} [instance]
|
|
43
|
-
*/
|
|
44
|
-
constructor(id, instance) {
|
|
45
|
-
this.id = id;
|
|
46
|
-
/** @type {Process|Lambda} */
|
|
47
|
-
this.instance = instance;
|
|
48
|
-
|
|
49
|
-
this.setResponse = this.setResponse.bind(this);
|
|
50
|
-
this.getResponse = this.getResponse.bind(this);
|
|
51
|
-
this.setRequest = this.setRequest.bind(this);
|
|
52
|
-
this.getRequest = this.getRequest.bind(this);
|
|
53
|
-
this.destroy = this.destroy.bind(this);
|
|
54
|
-
this.invoker = this.invoker.bind(this);
|
|
55
|
-
this.executor = this.executor.bind(this);
|
|
56
|
-
this.invokerMessageListener = this.invokerMessageListener.bind(this);
|
|
57
|
-
this.executorMessageListener = this.executorMessageListener.bind(this);
|
|
58
|
-
|
|
59
|
-
if (this.instance instanceof Lambda) {
|
|
60
|
-
this.invoker();
|
|
61
|
-
} else {
|
|
62
|
-
this.executor();
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
invoker() {
|
|
67
|
-
this.instance.addEventListener('message', this.invokerMessageListener);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
invokerMessageListener(message) {
|
|
71
|
-
if (message && message.type === MESSAGES.GET_REQUEST) {
|
|
72
|
-
const id = getRequestKey(message.id);
|
|
73
|
-
const item = storage[id];
|
|
74
|
-
if (item) {
|
|
75
|
-
this.instance.postMessage({ type: MESSAGES.SET_REQUEST, id: message.id, payload: item });
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (message && message.type === MESSAGES.SET_RESONSE) {
|
|
80
|
-
const id = getResponseKey(message.id);
|
|
81
|
-
storage[id] = message.payload;
|
|
82
|
-
this.instance.postMessage({ type: MESSAGES.SET_RESONSE_FINISHED, id: message.id });
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
executor() {
|
|
87
|
-
this.instance.on('message', this.executorMessageListener);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
executorMessageListener(message) {
|
|
91
|
-
if (message && message.type === MESSAGES.SET_REQUEST) {
|
|
92
|
-
const id = getRequestKey(message.id);
|
|
93
|
-
storage[id] = message.payload;
|
|
94
|
-
process.send({ type: MESSAGES.SET_REQUEST_FINISHED, id: message.id });
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (message && message.type === MESSAGES.GET_RESONSE) {
|
|
98
|
-
const id = getResponseKey(message.id);
|
|
99
|
-
const item = storage[id];
|
|
100
|
-
if (item) {
|
|
101
|
-
process.send({ type: MESSAGES.SET_RESONSE, id: message.id, payload: item });
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* @returns {string}
|
|
108
|
-
*/
|
|
109
|
-
get requestKey() {
|
|
110
|
-
return getRequestKey(this.id);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* @returns {string}
|
|
115
|
-
*/
|
|
116
|
-
get responseKey() {
|
|
117
|
-
return getResponseKey(this.id);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* @param {ResponseEvent} response
|
|
122
|
-
* @returns {Promise}
|
|
123
|
-
*/
|
|
124
|
-
setResponse(response) {
|
|
125
|
-
storage[this.responseKey] = response;
|
|
126
|
-
return new Promise(resolve => {
|
|
127
|
-
const finishListener = message => {
|
|
128
|
-
if (message && message.type === MESSAGES.SET_RESONSE_FINISHED && message.id === this.id) {
|
|
129
|
-
resolve();
|
|
130
|
-
this.instance.off('message', finishListener);
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
this.instance.on('message', finishListener);
|
|
134
|
-
this.instance.send({ type: MESSAGES.SET_RESONSE, id: this.id, payload: response });
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* @returns {Promise<ResponseEvent>}
|
|
140
|
-
*/
|
|
141
|
-
getResponse() {
|
|
142
|
-
return new Promise(resolve => {
|
|
143
|
-
if (storage[this.responseKey]) return resolve(storage[this.responseKey]);
|
|
144
|
-
const finishListener = message => {
|
|
145
|
-
if (message && message.type === MESSAGES.SET_RESONSE && message.id === this.id) {
|
|
146
|
-
storage[this.responseKey] = message.payload;
|
|
147
|
-
resolve(message.payload);
|
|
148
|
-
this.instance.off('message', finishListener);
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
this.instance.on('message', finishListener);
|
|
152
|
-
this.instance.send({ type: MESSAGES.GET_RESONSE, id: this.id });
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* @param {RequestEvent} request
|
|
158
|
-
* @returns {Promise}
|
|
159
|
-
*/
|
|
160
|
-
setRequest(request) {
|
|
161
|
-
storage[this.requestKey] = request;
|
|
162
|
-
return new Promise(resolve => {
|
|
163
|
-
const finishListener = message => {
|
|
164
|
-
if (message && message.type === MESSAGES.SET_REQUEST_FINISHED && message.id === this.id) {
|
|
165
|
-
resolve();
|
|
166
|
-
this.instance.off('message', finishListener);
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
this.instance.on('message', finishListener);
|
|
170
|
-
this.instance.send({ type: MESSAGES.SET_REQUEST, id: this.id, payload: request });
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* @returns {Promise<RequestEvent>}
|
|
176
|
-
*/
|
|
177
|
-
getRequest() {
|
|
178
|
-
return new Promise(resolve => {
|
|
179
|
-
if (storage[this.requestKey]) return resolve(storage[this.requestKey]);
|
|
180
|
-
const finishListener = message => {
|
|
181
|
-
if (message && message.type === MESSAGES.SET_REQUEST && message.id === this.id) {
|
|
182
|
-
storage[this.requestKey] = message.payload;
|
|
183
|
-
resolve(message.payload);
|
|
184
|
-
this.instance.off('message', finishListener);
|
|
185
|
-
// this.instance.send({ type: MESSAGES.SET_REQUEST_FINISHED, id: message.id })
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
this.instance.on('message', finishListener);
|
|
189
|
-
this.instance.send({ type: MESSAGES.GET_REQUEST, id: this.id });
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* @returns {Promise}
|
|
195
|
-
*/
|
|
196
|
-
destroy() {
|
|
197
|
-
storage[this.requestKey] = null;
|
|
198
|
-
delete storage[this.requestKey];
|
|
199
|
-
storage[this.responseKey] = null;
|
|
200
|
-
delete storage[this.responseKey];
|
|
201
|
-
|
|
202
|
-
if (this.instance instanceof Lambda) {
|
|
203
|
-
this.instance.removeEventListener('message', this.invokerMessageListener);
|
|
204
|
-
} else {
|
|
205
|
-
this.instance.off('message', this.executorMessageListener);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
return Promise.resolve();
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
IPCStorage.start = () => {
|
|
213
|
-
storage = {};
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
module.exports = IPCStorage;
|
package/classes/Lambda.js
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
const { resolve } = require('path');
|
|
2
|
-
const Worker = require('./Worker');
|
|
3
|
-
const {
|
|
4
|
-
EVENT_REQUEST,
|
|
5
|
-
EVENT_RESPONSE,
|
|
6
|
-
} = require('../constants');
|
|
7
|
-
const CommunicationRegistry = require('../registry/communication');
|
|
8
|
-
|
|
9
|
-
class Lambda {
|
|
10
|
-
/**
|
|
11
|
-
* @param {string} path
|
|
12
|
-
* @param {string} handler
|
|
13
|
-
* @param {function} [logger]
|
|
14
|
-
* @param {Object} communication
|
|
15
|
-
*/
|
|
16
|
-
constructor(path, handler, logger = () => {}, communication) {
|
|
17
|
-
this._path = path;
|
|
18
|
-
this._handler = handler;
|
|
19
|
-
this._logger = logger;
|
|
20
|
-
this._storagePath = CommunicationRegistry[communication.type].js.path;
|
|
21
|
-
this._communication = communication;
|
|
22
|
-
this.StorageDriver = require(this._storagePath);
|
|
23
|
-
this.createInstance();
|
|
24
|
-
this.busy = false;
|
|
25
|
-
|
|
26
|
-
const killTimer = setTimeout(() => {
|
|
27
|
-
this._logger('Shutting down lambda.');
|
|
28
|
-
if (this.instance) {
|
|
29
|
-
this.instance.terminate();
|
|
30
|
-
this.instance = null;
|
|
31
|
-
}
|
|
32
|
-
}, 15 * 60 * 1000);
|
|
33
|
-
|
|
34
|
-
this.instance.addEventListenerOnce('close', code => {
|
|
35
|
-
this.instance = null;
|
|
36
|
-
clearTimeout(killTimer);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
this._onFinished = this._onFinished.bind(this);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
get stdout() {
|
|
43
|
-
return this.instance.stdout;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
get stderr() {
|
|
47
|
-
return this.instance.stderr;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
createInstance() {
|
|
51
|
-
this.instance = new Worker(
|
|
52
|
-
resolve(__dirname, '../middlewares/invoke.js'),
|
|
53
|
-
{
|
|
54
|
-
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
|
|
55
|
-
env: {
|
|
56
|
-
LAMBDA: this._path,
|
|
57
|
-
HANDLER: this._handler,
|
|
58
|
-
COMMUNICATION: JSON.stringify(this._communication),
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* @param {string} requestId
|
|
66
|
-
* @param {RequestEvent} requestEvent
|
|
67
|
-
* @param {function} callback
|
|
68
|
-
*/
|
|
69
|
-
invoke(requestId, requestEvent, callback = () => {}) {
|
|
70
|
-
if (!this.instance) this.createInstance();
|
|
71
|
-
this._storage = new this.StorageDriver(requestId, this.instance);
|
|
72
|
-
Promise.resolve()
|
|
73
|
-
.then(() => this._storage.setRequest(requestEvent))
|
|
74
|
-
.then(() => {
|
|
75
|
-
this.busy = true;
|
|
76
|
-
this._requestId = requestId;
|
|
77
|
-
this._callback = callback;
|
|
78
|
-
this.instance.addEventListener('message', this._onFinished);
|
|
79
|
-
this.instance.postMessage({ type: EVENT_REQUEST, id: requestId });
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* @param event
|
|
85
|
-
* @private
|
|
86
|
-
*/
|
|
87
|
-
_onFinished(event) {
|
|
88
|
-
if (event.type === EVENT_RESPONSE && event.id === this._requestId) {
|
|
89
|
-
this.busy = false;
|
|
90
|
-
this.instance.removeEventListener('message', this._onFinished);
|
|
91
|
-
this._storage.getResponse().then(responseEvent => this._callback(responseEvent));
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* @param {string} event
|
|
97
|
-
* @param {function} listener
|
|
98
|
-
*/
|
|
99
|
-
addEventListener(event, listener) {
|
|
100
|
-
this.instance.addEventListener(event, listener);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* @param {string} event
|
|
105
|
-
* @param {function} listener
|
|
106
|
-
*/
|
|
107
|
-
addEventListenerOnce(event, listener) {
|
|
108
|
-
this.instance.addEventListenerOnce(event, listener);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* @param {string} event
|
|
113
|
-
* @param {function} listener
|
|
114
|
-
*/
|
|
115
|
-
removeEventListener(event, listener) {
|
|
116
|
-
this.instance.removeEventListener(event, listener);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
postMessage(message, cb = () => {}) {
|
|
120
|
-
this.instance.postMessage(message, cb);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
module.exports = Lambda;
|
package/classes/LambdaPool.js
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
const uuid = require('uuid');
|
|
2
|
-
const Lambda = require('../classes/Lambda');
|
|
3
|
-
const stdoutListener = require('../middlewares/stdoutListener');
|
|
4
|
-
const { EVENT_STARTED } = require('../constants');
|
|
5
|
-
|
|
6
|
-
const lambdaInstances = {};
|
|
7
|
-
|
|
8
|
-
function getOverallCount() {
|
|
9
|
-
return Object.keys(lambdaInstances).reduce((result, current) => Object.keys(lambdaInstances[current]).length + result, 0);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function getNonBusyId(lambdaToInvoke) {
|
|
13
|
-
const timeLimit = Date.now() - 15 * 60 * 1000 + 5000; // lifespan of lambda, to give enough time to respond before killed
|
|
14
|
-
return Object.keys(lambdaInstances[lambdaToInvoke] || {}).find(id => {
|
|
15
|
-
const instance = lambdaInstances[lambdaToInvoke][id];
|
|
16
|
-
return !instance.busy && instance.createdAt >= timeLimit;
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
class LambdaPool {
|
|
21
|
-
constructor({ overallLimit, logger = () => {}, communication }) {
|
|
22
|
-
this.communication = communication;
|
|
23
|
-
this.overallLimit = overallLimit;
|
|
24
|
-
this.logger = logger;
|
|
25
|
-
|
|
26
|
-
this.getLambda = this.getLambda.bind(this);
|
|
27
|
-
this.createLambda = this.createLambda.bind(this);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
createLambda(lambdaToInvoke, handlerKey) {
|
|
31
|
-
return new Promise(resolve => {
|
|
32
|
-
const currentId = uuid.v4();
|
|
33
|
-
const currentLambdaInstance = new Lambda(lambdaToInvoke, handlerKey, this.logger, this.communication);
|
|
34
|
-
|
|
35
|
-
const lambdaStartListener = message => {
|
|
36
|
-
if (message.type === EVENT_STARTED) {
|
|
37
|
-
this.logger(`[${currentId}] started`);
|
|
38
|
-
currentLambdaInstance.removeEventListener('message', lambdaStartListener);
|
|
39
|
-
resolve({ id: currentId, instance: currentLambdaInstance });
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
currentLambdaInstance.addEventListener('message', lambdaStartListener);
|
|
43
|
-
|
|
44
|
-
currentLambdaInstance.addEventListenerOnce('close', code => {
|
|
45
|
-
if (code) this.logger(`[${currentId}] Lambda exited with code ${code}`);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
stdoutListener(currentLambdaInstance, this.logger);
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
getLambda(lambdaToInvoke, handlerKey) {
|
|
53
|
-
const nonBusyId = getNonBusyId(lambdaToInvoke);
|
|
54
|
-
if (getOverallCount() >= this.overallLimit) {
|
|
55
|
-
return Promise.resolve()
|
|
56
|
-
.then(() => new Promise(r => setTimeout(r, 100)))
|
|
57
|
-
.then(() => this.getLambda(lambdaToInvoke, handlerKey));
|
|
58
|
-
} else if (!lambdaInstances[lambdaToInvoke] || !nonBusyId) {
|
|
59
|
-
return Promise.resolve()
|
|
60
|
-
.then(() => this.createLambda(lambdaToInvoke, handlerKey))
|
|
61
|
-
.then(({ id, instance }) => {
|
|
62
|
-
instance.createdAt = Date.now();
|
|
63
|
-
lambdaInstances[lambdaToInvoke] = {
|
|
64
|
-
...(lambdaInstances[lambdaToInvoke] && lambdaInstances[lambdaToInvoke]),
|
|
65
|
-
[id]: instance,
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
return Promise.resolve(instance);
|
|
69
|
-
});
|
|
70
|
-
} else if(nonBusyId) {
|
|
71
|
-
return Promise.resolve(lambdaInstances[lambdaToInvoke][nonBusyId]);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return Promise.reject();
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
module.exports = LambdaPool;
|
package/classes/RequestEvent.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @class RequestEvent
|
|
3
|
-
* @property {String} path
|
|
4
|
-
* @property {Object.<String, String>} headers
|
|
5
|
-
* @property {Object.<String, String>} pathParameters
|
|
6
|
-
* @property {Object} requestContext
|
|
7
|
-
* @property {String} resource
|
|
8
|
-
* @property {String} httpMethod
|
|
9
|
-
* @property {Object.<String, String>} queryStringParameters
|
|
10
|
-
* @property {Object.<String, String>} stageVariables
|
|
11
|
-
*/
|
|
12
|
-
class RequestEvent {
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
module.exports = RequestEvent;
|
package/classes/ResponseEvent.js
DELETED
package/classes/Worker.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
const { spawn } = require('child_process');
|
|
2
|
-
|
|
3
|
-
// TODO: move this to separate package
|
|
4
|
-
class Worker {
|
|
5
|
-
/**
|
|
6
|
-
* @param {string} workerPath
|
|
7
|
-
* @param {Object} options
|
|
8
|
-
*/
|
|
9
|
-
constructor(workerPath, options = {}) {
|
|
10
|
-
this.workerPath = workerPath;
|
|
11
|
-
this.instance = spawn(
|
|
12
|
-
'node',
|
|
13
|
-
[
|
|
14
|
-
...this.workerPath.split(' ')
|
|
15
|
-
],
|
|
16
|
-
{
|
|
17
|
-
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
|
|
18
|
-
...options,
|
|
19
|
-
}
|
|
20
|
-
);
|
|
21
|
-
if (this.instance.stdout) {
|
|
22
|
-
const messageListener = data => {
|
|
23
|
-
console.info(data.toString().trim());
|
|
24
|
-
};
|
|
25
|
-
this.instance.stdout.on('data', messageListener);
|
|
26
|
-
}
|
|
27
|
-
this.instance.once('close', () => {
|
|
28
|
-
this.instance = null;
|
|
29
|
-
delete this.instance;
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
this.addEventListener = this.addEventListener.bind(this);
|
|
33
|
-
this.removeEventListener = this.removeEventListener.bind(this);
|
|
34
|
-
this.terminate = this.terminate.bind(this);
|
|
35
|
-
this.postMessage = this.postMessage.bind(this);
|
|
36
|
-
this.send = this.send.bind(this);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
set onmessage(onmessage) {
|
|
40
|
-
this.addEventListener('message', onmessage);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
set onerror(onerror) {
|
|
44
|
-
this.addEventListener('error', onerror);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
get stdout() {
|
|
48
|
-
return this.instance.stdout;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
get stderr() {
|
|
52
|
-
return this.instance.stderr;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
addEventListener(event, listener) {
|
|
56
|
-
if (this.instance) this.instance.on(event, listener);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
addEventListenerOnce(event, listener) {
|
|
60
|
-
if (this.instance) this.instance.once(event, listener);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
on(event, listener) {
|
|
64
|
-
this.addEventListener(event, listener);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
off(event, listener) {
|
|
68
|
-
this.removeEventListener(event, listener);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
send(message, cb = () => {}) {
|
|
72
|
-
this.postMessage(message, cb);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
removeEventListener(event, listener) {
|
|
76
|
-
if (this.instance && this.instance.off) this.instance.off(event, listener);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// from EventTarget prototype, if needed
|
|
80
|
-
// dispatchEvent() {
|
|
81
|
-
//
|
|
82
|
-
// }
|
|
83
|
-
|
|
84
|
-
terminate() {
|
|
85
|
-
if (this.instance) this.instance.kill('SIGINT');
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
postMessage(message, cb = () => {}) {
|
|
89
|
-
if (this.instance) this.instance.send(message, cb);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
module.exports = Worker;
|
package/constants/index.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
const EVENT_STARTED = 'LAMBDA_EVENT_STARTED';
|
|
2
|
-
module.exports.EVENT_STARTED = EVENT_STARTED;
|
|
3
|
-
|
|
4
|
-
const EVENT_REQUEST = 'LAMBDA_EVENT_REQUEST';
|
|
5
|
-
module.exports.EVENT_REQUEST = EVENT_REQUEST;
|
|
6
|
-
|
|
7
|
-
const EVENT_RESPONSE = 'LAMBDA_EVENT_RESPONSE';
|
|
8
|
-
module.exports.EVENT_RESPONSE = EVENT_RESPONSE;
|
package/index.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
module.exports.httpMiddleware = require('./middlewares/http');
|
|
2
|
-
|
|
3
|
-
module.exports.Lambda = require('./classes/Lambda');
|
|
4
|
-
|
|
5
|
-
module.exports.FileStorage = require('./classes/FileStorage');
|
|
6
|
-
|
|
7
|
-
module.exports.IPCStorage = require('./classes/IPCStorage');
|
|
8
|
-
|
|
9
|
-
module.exports.Worker = require('./classes/Worker');
|