@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/middlewares/http.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
const uuid = require('uuid');
|
|
2
|
-
const url = require('url');
|
|
3
|
-
const LambdaPool = require('../classes/LambdaPool');
|
|
4
|
-
const RequestEvent = require('../classes/RequestEvent');
|
|
5
|
-
const CommunicationRegistry = require('../registry/communication');
|
|
6
|
-
|
|
7
|
-
let overallLimit = 3000;
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @typedef Communication
|
|
11
|
-
* @property {string} type
|
|
12
|
-
* @property {string} [path]
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @typedef Options
|
|
17
|
-
* @property {string} lambdaPath
|
|
18
|
-
* @property {string} handlerKey
|
|
19
|
-
* @property {function} [logger]
|
|
20
|
-
* @property {number} [limit]
|
|
21
|
-
* @property {Communication} [communication]
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* @param {Options} options
|
|
26
|
-
* @returns {Function}
|
|
27
|
-
*/
|
|
28
|
-
function createHttpMiddleware(options) {
|
|
29
|
-
const {
|
|
30
|
-
lambdaPath,
|
|
31
|
-
handlerKey = 'handler',
|
|
32
|
-
logger = () => {},
|
|
33
|
-
limit = overallLimit,
|
|
34
|
-
communication = {},
|
|
35
|
-
} = options;
|
|
36
|
-
const currentCommunication = {
|
|
37
|
-
...(!communication.type ? { type: 'ipc' } : communication),
|
|
38
|
-
};
|
|
39
|
-
const communicationConfig = {
|
|
40
|
-
...(CommunicationRegistry[currentCommunication.type] && CommunicationRegistry[currentCommunication.type].js),
|
|
41
|
-
...(!CommunicationRegistry[currentCommunication.type] && currentCommunication),
|
|
42
|
-
};
|
|
43
|
-
// TODO: tmp folders
|
|
44
|
-
if (!communicationConfig.path || typeof communicationConfig.path !== "string") return (req, res, next) => { next() };
|
|
45
|
-
|
|
46
|
-
if (limit) {
|
|
47
|
-
overallLimit = limit;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const StorageDriver = require(communicationConfig.path);
|
|
51
|
-
if (StorageDriver.start) StorageDriver.start();
|
|
52
|
-
const lambdaPool = new LambdaPool({ logger, communication: currentCommunication });
|
|
53
|
-
return (request, response) => {
|
|
54
|
-
const {
|
|
55
|
-
query: queryStringParameters,
|
|
56
|
-
pathname: path
|
|
57
|
-
} = url.parse(request.url, true);
|
|
58
|
-
|
|
59
|
-
/** @var {RequestEvent} event */
|
|
60
|
-
const requestEvent = new RequestEvent;
|
|
61
|
-
requestEvent.httpMethod = request.method.toUpperCase();
|
|
62
|
-
requestEvent.path = path;
|
|
63
|
-
requestEvent.queryStringParameters = queryStringParameters;
|
|
64
|
-
requestEvent.headers = request.headers;
|
|
65
|
-
|
|
66
|
-
const requestId = uuid.v4();
|
|
67
|
-
let storage;
|
|
68
|
-
|
|
69
|
-
let lambdaInstance;
|
|
70
|
-
|
|
71
|
-
logger('Invoking lambda', `${lambdaPath}#${handlerKey}`);
|
|
72
|
-
|
|
73
|
-
const closeListener = () => {
|
|
74
|
-
if (!response.finished) response.end();
|
|
75
|
-
if (storage) storage.destroy();
|
|
76
|
-
};
|
|
77
|
-
Promise.resolve()
|
|
78
|
-
.then(() => lambdaPool.getLambda(lambdaPath, handlerKey))
|
|
79
|
-
.then(instance => {
|
|
80
|
-
instance.addEventListenerOnce('close', closeListener);
|
|
81
|
-
|
|
82
|
-
lambdaInstance = instance;
|
|
83
|
-
storage = new StorageDriver(requestId, instance);
|
|
84
|
-
return Promise.resolve(instance);
|
|
85
|
-
})
|
|
86
|
-
.then(() => new Promise(res => lambdaInstance.invoke(requestId, requestEvent, res)))
|
|
87
|
-
/** @var {ResponseEvent} responseEvent */
|
|
88
|
-
.then(responseEvent => {
|
|
89
|
-
if (responseEvent.statusCode) {
|
|
90
|
-
response.writeHead(responseEvent.statusCode, responseEvent.headers);
|
|
91
|
-
if (responseEvent.body) {
|
|
92
|
-
const bufferEncoding = responseEvent.isBase64Encoded ? 'base64' : 'utf8';
|
|
93
|
-
response.end(Buffer.from(responseEvent.body, bufferEncoding));
|
|
94
|
-
} else {
|
|
95
|
-
response.end();
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
lambdaInstance.removeEventListener('close', closeListener);
|
|
100
|
-
return storage ? storage.destroy() : Promise.resolve();
|
|
101
|
-
})
|
|
102
|
-
.catch(err => {
|
|
103
|
-
logger(err);
|
|
104
|
-
response.writeHead(500);
|
|
105
|
-
response.write('Something went wrong.');
|
|
106
|
-
response.end();
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
module.exports = createHttpMiddleware;
|
package/middlewares/invoke.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
const ResponseEvent = require('../classes/ResponseEvent');
|
|
2
|
-
const {
|
|
3
|
-
EVENT_STARTED,
|
|
4
|
-
EVENT_REQUEST,
|
|
5
|
-
EVENT_RESPONSE,
|
|
6
|
-
} = require('../constants');
|
|
7
|
-
const CommunicationRegistry = require('../registry/communication');
|
|
8
|
-
|
|
9
|
-
const {
|
|
10
|
-
LAMBDA = './testLambda.js',
|
|
11
|
-
HANDLER = 'handler',
|
|
12
|
-
COMMUNICATION = '{}',
|
|
13
|
-
} = process.env;
|
|
14
|
-
|
|
15
|
-
const lambdaHandler = require(LAMBDA)[HANDLER];
|
|
16
|
-
const Communication = JSON.parse(COMMUNICATION);
|
|
17
|
-
const Storage = require(CommunicationRegistry[Communication.type].js.path);
|
|
18
|
-
|
|
19
|
-
setTimeout(() => {
|
|
20
|
-
process.exit(0);
|
|
21
|
-
}, 15 * 60 * 1000); // setting to default 15 minutes AWS timeout 15 * 60 * 1000
|
|
22
|
-
|
|
23
|
-
function messageListener(event) {
|
|
24
|
-
if (event.type === EVENT_REQUEST) {
|
|
25
|
-
const storage = new Storage(event.id, process);
|
|
26
|
-
|
|
27
|
-
Promise.resolve()
|
|
28
|
-
.then(() => storage.getRequest())
|
|
29
|
-
.then(requestEvent => new Promise(resolve => {
|
|
30
|
-
lambdaHandler(requestEvent, {}, (error, response = {}) => {
|
|
31
|
-
const responseEvent = new ResponseEvent;
|
|
32
|
-
|
|
33
|
-
if (error) {
|
|
34
|
-
responseEvent.statusCode = 500;
|
|
35
|
-
responseEvent.body = error.body || error + '';
|
|
36
|
-
} else {
|
|
37
|
-
Object.assign(responseEvent, response);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
resolve(responseEvent);
|
|
41
|
-
});
|
|
42
|
-
}))
|
|
43
|
-
.then(responseEvent => storage.setResponse(responseEvent))
|
|
44
|
-
.then(() => process.send({ type: EVENT_RESPONSE, id: event.id }));
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
process.on('message', messageListener);
|
|
49
|
-
|
|
50
|
-
process.send({ type: EVENT_STARTED });
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
const moment = require('moment');
|
|
2
|
-
|
|
3
|
-
const getDate = exports = () => moment().format('YYYY-MM-DD HH:mm:ss.SS');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
*
|
|
7
|
-
* @param {Lambda} lambdaInstance
|
|
8
|
-
* @param {function} [logger]
|
|
9
|
-
*/
|
|
10
|
-
module.exports = (lambdaInstance, logger = () => {}) => {
|
|
11
|
-
const messageListener = data => {
|
|
12
|
-
logger(`[${getDate()}] ${data.toString().trim()}`);
|
|
13
|
-
};
|
|
14
|
-
if (lambdaInstance.stdout) {
|
|
15
|
-
lambdaInstance.stdout.off('data', messageListener);
|
|
16
|
-
lambdaInstance.stdout.on('data', messageListener);
|
|
17
|
-
}
|
|
18
|
-
if (lambdaInstance.stderr) {
|
|
19
|
-
lambdaInstance.stderr.off('data', messageListener);
|
|
20
|
-
lambdaInstance.stderr.on('data', messageListener);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const closeListener = code => {
|
|
24
|
-
if (code) logger(`[${getDate()}] child process exited with code ${code}`);
|
|
25
|
-
};
|
|
26
|
-
lambdaInstance.addEventListenerOnce('close', closeListener);
|
|
27
|
-
};
|
package/scripts/version.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
const { execSync } = require('child_process');
|
|
2
|
-
|
|
3
|
-
const { TRAVIS_TAG, TRAVIS_BUILD_NUMBER } = process.env;
|
|
4
|
-
|
|
5
|
-
const major = Math.floor(TRAVIS_BUILD_NUMBER / 100);
|
|
6
|
-
const minor = TRAVIS_BUILD_NUMBER - major;
|
|
7
|
-
const version = `1.${major}.${minor}-${TRAVIS_TAG || 'release'}`;
|
|
8
|
-
|
|
9
|
-
execSync(`yarn version --no-git-tag-version --new-version "v${version}"`, { shell: true });
|
|
10
|
-
console.log('version set', version);
|