forked from SamVerschueren/aws-lambda-mock-context
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (63 loc) · 1.75 KB
/
index.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
'use strict';
const uuid = require('uuid');
const moment = require('moment');
const defer = require('pinkie-defer');
const pkg = require('./package.json');
module.exports = options => {
const id = uuid.v1();
const stream = uuid.v4().replace(/-/g, '');
const opts = Object.assign({
region: 'us-west-1',
account: '123456789012',
functionName: pkg.name,
functionVersion: '$LATEST',
memoryLimitInMB: '128',
timeout: 3
}, options);
const deferred = defer();
const start = Date.now();
let end;
const context = {
callbackWaitsForEmptyEventLoop: true,
functionName: opts.functionName,
functionVersion: opts.functionVersion,
invokedFunctionArn: `arn:aws:lambda:${opts.region}:${opts.account}:function:${opts.functionName}:${opts.alias || opts.functionVersion}`,
memoryLimitInMB: opts.memoryLimitInMB,
awsRequestId: id,
invokeid: id,
logGroupName: `/aws/lambda/${opts.functionName}`,
logStreamName: `${moment().format('YYYY/MM/DD')}/[${opts.functionVersion}]/${stream}`,
getRemainingTimeInMillis: () => {
const endTime = end || Date.now();
const remainingTime = (opts.timeout * 1000) - (endTime - start);
return Math.max(0, remainingTime);
},
succeed: result => {
end = Date.now();
deferred.resolve(result);
},
fail: err => {
end = Date.now();
if (typeof err === 'string') {
err = new Error(err);
}
deferred.reject(err);
},
done: (err, result) => {
if (err) {
context.fail(err);
return;
}
context.succeed(result);
},
Promise: new Promise(deferred)
};
if(opts.timeout !== 0){
setTimeout(() => {
if (context.getRemainingTimeInMillis() === 0) {
context.fail(new Error(`Task timed out after ${opts.timeout}.00 seconds`));
}
}, opts.timeout * 1000);
}
return context;
};