@gahojin-inc/aws-lambda-mock-context 2024.11.0 → 2024.11.2
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/dist/cjs/index.cjs +20 -52
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +19 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +18 -52
- package/dist/index.mjs.map +1 -1
- package/package.json +16 -10
package/dist/cjs/index.cjs
CHANGED
|
@@ -2,39 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
var uuid = require('uuid');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
* @gahojin-inc/aws-lambda-mock-context
|
|
7
|
-
* AWS Lambda mock context object
|
|
8
|
-
* @version: 2024.11.0
|
|
9
|
-
* @author: GAHOJIN, Inc.
|
|
10
|
-
* @license: Apache-2.0
|
|
11
|
-
**/
|
|
12
|
-
|
|
5
|
+
// src/index.ts
|
|
13
6
|
var defer = () => {
|
|
14
|
-
|
|
15
|
-
const promise = new Promise((resolve, reject) => {
|
|
16
|
-
deferred.resolve = resolve;
|
|
17
|
-
deferred.reject = reject;
|
|
18
|
-
});
|
|
7
|
+
let deferred = {};
|
|
19
8
|
return {
|
|
20
|
-
promise,
|
|
9
|
+
promise: new Promise((resolve, reject) => {
|
|
10
|
+
deferred.resolve = resolve, deferred.reject = reject;
|
|
11
|
+
}),
|
|
21
12
|
// biome-ignore lint/style/noNonNullAssertion: <explanation>
|
|
22
13
|
resolve: deferred.resolve,
|
|
23
14
|
// biome-ignore lint/style/noNonNullAssertion: <explanation>
|
|
24
15
|
reject: deferred.reject
|
|
25
16
|
};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
context2.fail(new Error(`Task timed out after ${timeout}.00 seconds`));
|
|
31
|
-
}
|
|
32
|
-
}, timeout * 1e3);
|
|
33
|
-
};
|
|
34
|
-
var context = (options, overrides) => {
|
|
35
|
-
const id = uuid.v1();
|
|
36
|
-
const stream = uuid.v4().replace(/-/g, "");
|
|
37
|
-
const opts = {
|
|
17
|
+
}, lambdaTimeout = (context, timeout) => setTimeout(() => {
|
|
18
|
+
Math.round(context.getRemainingTimeInMillis() / 1e3) === 0 && context.fail(new Error(`Task timed out after ${timeout}.00 seconds`));
|
|
19
|
+
}, timeout * 1e3), src_default = (options, overrides) => {
|
|
20
|
+
let id = uuid.v1(), stream = uuid.v4().replace(/-/g, ""), opts = {
|
|
38
21
|
region: "us-west-1",
|
|
39
22
|
account: "123456789012",
|
|
40
23
|
functionName: "aws-lambda-mock-context",
|
|
@@ -42,15 +25,8 @@ var context = (options, overrides) => {
|
|
|
42
25
|
memoryLimitInMB: "128",
|
|
43
26
|
timeout: 3,
|
|
44
27
|
...options
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const d = /* @__PURE__ */ new Date();
|
|
48
|
-
const logDate = [d.getFullYear(), `0${d.getMonth() + 1}`.slice(-2), `0${d.getDate()}`.slice(-2)].join("/");
|
|
49
|
-
const start = d.getTime();
|
|
50
|
-
let end = null;
|
|
51
|
-
let timeout = null;
|
|
52
|
-
const context2 = {
|
|
53
|
-
callbackWaitsForEmptyEventLoop: true,
|
|
28
|
+
}, deferred = defer(), d = /* @__PURE__ */ new Date(), logDate = [d.getFullYear(), `0${d.getMonth() + 1}`.slice(-2), `0${d.getDate()}`.slice(-2)].join("/"), start = d.getTime(), end = null, timeout = null, context = {
|
|
29
|
+
callbackWaitsForEmptyEventLoop: !0,
|
|
54
30
|
functionName: opts.functionName,
|
|
55
31
|
functionVersion: opts.functionVersion,
|
|
56
32
|
invokedFunctionArn: `arn:aws:lambda:${opts.region}:${opts.account}:function:${opts.functionName}:${opts.alias || opts.functionVersion}`,
|
|
@@ -61,37 +37,29 @@ var context = (options, overrides) => {
|
|
|
61
37
|
identity: opts.identity,
|
|
62
38
|
clientContext: opts.clientContext,
|
|
63
39
|
getRemainingTimeInMillis: () => {
|
|
64
|
-
|
|
65
|
-
const remainingTime = opts.timeout * 1e3 - (endTime - start);
|
|
40
|
+
let endTime = end || Date.now(), remainingTime = opts.timeout * 1e3 - (endTime - start);
|
|
66
41
|
return Math.max(0, remainingTime);
|
|
67
42
|
},
|
|
68
43
|
succeed: (messageOrObject) => {
|
|
69
|
-
end = Date.now();
|
|
70
|
-
deferred.resolve(messageOrObject);
|
|
44
|
+
end = Date.now(), deferred.resolve(messageOrObject);
|
|
71
45
|
},
|
|
72
46
|
fail: (error) => {
|
|
73
|
-
end = Date.now();
|
|
74
|
-
deferred.reject(typeof error === "string" ? new Error(error) : error);
|
|
47
|
+
end = Date.now(), deferred.reject(typeof error == "string" ? new Error(error) : error);
|
|
75
48
|
},
|
|
76
49
|
done: (error, result) => {
|
|
77
|
-
if (timeout) {
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
if (error) {
|
|
81
|
-
context2.fail(error);
|
|
50
|
+
if (timeout && clearTimeout(timeout), error) {
|
|
51
|
+
context.fail(error);
|
|
82
52
|
return;
|
|
83
53
|
}
|
|
84
|
-
|
|
54
|
+
context.succeed(result);
|
|
85
55
|
},
|
|
86
56
|
Promise: deferred.promise
|
|
87
57
|
};
|
|
88
|
-
|
|
89
|
-
timeout = lambdaTimeout(context2, opts.timeout);
|
|
90
|
-
}
|
|
91
|
-
return Object.assign(context2, overrides);
|
|
58
|
+
return opts.timeout > 0 && (timeout = lambdaTimeout(context, opts.timeout)), Object.assign(context, overrides);
|
|
92
59
|
};
|
|
93
|
-
var src_default = context;
|
|
94
60
|
|
|
95
61
|
module.exports = src_default;
|
|
96
62
|
//# sourceMappingURL=index.cjs.map
|
|
63
|
+
|
|
64
|
+
module.exports = exports.default;
|
|
97
65
|
//# sourceMappingURL=index.cjs.map
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"names":["
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"names":["uuidv1","uu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AA,GAAA,GAAM,IAAK,CAAA,GAAA,EAEX,EAAA,QAAA,CAAS,MAAO,CAAA,OAAO,KAAU,IAAA,QAAA,GAAW,IAAI,KAAA,CAAM,KAAK,CAAA,GAAI,KAAK,CAAA;AAAA,KACtE;AAAA,IACA,IAAA,EAAM,CAAC,KAAA,EAAO,MAAW,KAAA;AAKvB,MAAA,IAJI,OACF,IAAA,YAAA,CAAa,OAAO,CAAA,EAGlB,KAAO,EAAA;AACT,QAAA,OAAA,CAAQ,KAAK,KAAK,CAAA;AAClB,QAAA;AAAA;AAGF,MAAA,OAAA,CAAQ,QAAQ,MAAM,CAAA;AAAA,KACxB;AAAA,IACA,SAAS,QAAS,CAAA;AAAA,GACpB;AAGA,EAAA,OAAI,IAAK,CAAA,OAAA,GAAU,CACjB,KAAA,OAAA,GAAU,aAAc,CAAA,OAAA,EAAS,IAAK,CAAA,OAAO,CAGxC,CAAA,EAAA,MAAA,CAAO,MAAO,CAAA,OAAA,EAAS,SAAS,CAAA;AACzC","file":"index.cjs","sourcesContent":["import type { ClientContext, CognitoIdentity, Context } from 'aws-lambda'\nimport { v1 as uuidv1, v4 as uuidv4 } from 'uuid'\n\ntype Options = {\n region?: string\n account?: string\n alias?: string\n functionName?: string\n functionVersion?: string\n memoryLimitInMB?: string\n identity?: CognitoIdentity\n clientContext?: ClientContext\n timeout?: number\n}\n\ninterface MockContext extends Context {\n Promise: Promise<any>\n}\n\ntype Deferred = {\n promise: Promise<unknown>\n resolve: (value: unknown) => void\n reject: (reason?: any) => void\n}\nconst defer = (): Deferred => {\n const deferred: Partial<Deferred> = {}\n const promise = new Promise((resolve, reject) => {\n deferred.resolve = resolve\n deferred.reject = reject\n })\n return {\n promise,\n // biome-ignore lint/style/noNonNullAssertion: <explanation>\n resolve: deferred.resolve!,\n // biome-ignore lint/style/noNonNullAssertion: <explanation>\n reject: deferred.reject!,\n }\n}\n\nconst lambdaTimeout = (context: MockContext, timeout: number): NodeJS.Timeout => {\n return setTimeout(() => {\n if (Math.round(context.getRemainingTimeInMillis() / 1000) === 0) {\n context.fail(new Error(`Task timed out after ${timeout}.00 seconds`))\n }\n }, timeout * 1000)\n}\n\nexport default <E = Record<string, any>>(options?: Options, overrides?: E): MockContext & E => {\n const id = uuidv1()\n const stream = uuidv4().replace(/-/g, '')\n\n const opts = {\n region: 'us-west-1',\n account: '123456789012',\n functionName: 'aws-lambda-mock-context',\n functionVersion: '$LATEST',\n memoryLimitInMB: '128',\n timeout: 3,\n ...options,\n }\n\n const deferred = defer()\n\n const d = new Date()\n const logDate = [d.getFullYear(), `0${d.getMonth() + 1}`.slice(-2), `0${d.getDate()}`.slice(-2)].join('/')\n const start = d.getTime()\n let end: number | null = null\n let timeout: NodeJS.Timeout | null = null\n\n // https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/nodejs-context.html\n const context: MockContext = {\n callbackWaitsForEmptyEventLoop: true,\n functionName: opts.functionName,\n functionVersion: opts.functionVersion,\n invokedFunctionArn: `arn:aws:lambda:${opts.region}:${opts.account}:function:${opts.functionName}:${opts.alias || opts.functionVersion}`,\n memoryLimitInMB: opts.memoryLimitInMB,\n awsRequestId: id,\n logGroupName: `/aws/lambda/${opts.functionName}`,\n logStreamName: `${logDate}/[${opts.functionVersion}]/${stream}`,\n identity: opts.identity,\n clientContext: opts.clientContext,\n getRemainingTimeInMillis: () => {\n const endTime = end || Date.now()\n const remainingTime = opts.timeout * 1000 - (endTime - start)\n\n return Math.max(0, remainingTime)\n },\n succeed: (messageOrObject) => {\n end = Date.now()\n\n deferred.resolve(messageOrObject)\n },\n fail: (error) => {\n end = Date.now()\n\n deferred.reject(typeof error === 'string' ? new Error(error) : error)\n },\n done: (error, result) => {\n if (timeout) {\n clearTimeout(timeout)\n }\n\n if (error) {\n context.fail(error)\n return\n }\n\n context.succeed(result)\n },\n Promise: deferred.promise,\n }\n\n // Lambda Timeout\n if (opts.timeout > 0) {\n timeout = lambdaTimeout(context, opts.timeout)\n }\n\n return Object.assign(context, overrides)\n}\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CognitoIdentity, ClientContext, Context } from 'aws-lambda';
|
|
2
|
+
|
|
3
|
+
type Options = {
|
|
4
|
+
region?: string;
|
|
5
|
+
account?: string;
|
|
6
|
+
alias?: string;
|
|
7
|
+
functionName?: string;
|
|
8
|
+
functionVersion?: string;
|
|
9
|
+
memoryLimitInMB?: string;
|
|
10
|
+
identity?: CognitoIdentity;
|
|
11
|
+
clientContext?: ClientContext;
|
|
12
|
+
timeout?: number;
|
|
13
|
+
};
|
|
14
|
+
interface MockContext extends Context {
|
|
15
|
+
Promise: Promise<any>;
|
|
16
|
+
}
|
|
17
|
+
declare const _default: <E = Record<string, any>>(options?: Options, overrides?: E) => MockContext & E;
|
|
18
|
+
|
|
19
|
+
export = _default;
|
package/dist/index.d.ts
CHANGED
|
@@ -14,6 +14,6 @@ type Options = {
|
|
|
14
14
|
interface MockContext extends Context {
|
|
15
15
|
Promise: Promise<any>;
|
|
16
16
|
}
|
|
17
|
-
declare const
|
|
17
|
+
declare const _default: <E = Record<string, any>>(options?: Options, overrides?: E) => MockContext & E;
|
|
18
18
|
|
|
19
|
-
export {
|
|
19
|
+
export { _default as default };
|
package/dist/index.mjs
CHANGED
|
@@ -1,38 +1,21 @@
|
|
|
1
1
|
import { v1, v4 } from 'uuid';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
* @gahojin-inc/aws-lambda-mock-context
|
|
5
|
-
* AWS Lambda mock context object
|
|
6
|
-
* @version: 2024.11.0
|
|
7
|
-
* @author: GAHOJIN, Inc.
|
|
8
|
-
* @license: Apache-2.0
|
|
9
|
-
**/
|
|
10
|
-
|
|
3
|
+
// src/index.ts
|
|
11
4
|
var defer = () => {
|
|
12
|
-
|
|
13
|
-
const promise = new Promise((resolve, reject) => {
|
|
14
|
-
deferred.resolve = resolve;
|
|
15
|
-
deferred.reject = reject;
|
|
16
|
-
});
|
|
5
|
+
let deferred = {};
|
|
17
6
|
return {
|
|
18
|
-
promise,
|
|
7
|
+
promise: new Promise((resolve, reject) => {
|
|
8
|
+
deferred.resolve = resolve, deferred.reject = reject;
|
|
9
|
+
}),
|
|
19
10
|
// biome-ignore lint/style/noNonNullAssertion: <explanation>
|
|
20
11
|
resolve: deferred.resolve,
|
|
21
12
|
// biome-ignore lint/style/noNonNullAssertion: <explanation>
|
|
22
13
|
reject: deferred.reject
|
|
23
14
|
};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
context2.fail(new Error(`Task timed out after ${timeout}.00 seconds`));
|
|
29
|
-
}
|
|
30
|
-
}, timeout * 1e3);
|
|
31
|
-
};
|
|
32
|
-
var context = (options, overrides) => {
|
|
33
|
-
const id = v1();
|
|
34
|
-
const stream = v4().replace(/-/g, "");
|
|
35
|
-
const opts = {
|
|
15
|
+
}, lambdaTimeout = (context, timeout) => setTimeout(() => {
|
|
16
|
+
Math.round(context.getRemainingTimeInMillis() / 1e3) === 0 && context.fail(new Error(`Task timed out after ${timeout}.00 seconds`));
|
|
17
|
+
}, timeout * 1e3), src_default = (options, overrides) => {
|
|
18
|
+
let id = v1(), stream = v4().replace(/-/g, ""), opts = {
|
|
36
19
|
region: "us-west-1",
|
|
37
20
|
account: "123456789012",
|
|
38
21
|
functionName: "aws-lambda-mock-context",
|
|
@@ -40,15 +23,8 @@ var context = (options, overrides) => {
|
|
|
40
23
|
memoryLimitInMB: "128",
|
|
41
24
|
timeout: 3,
|
|
42
25
|
...options
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const d = /* @__PURE__ */ new Date();
|
|
46
|
-
const logDate = [d.getFullYear(), `0${d.getMonth() + 1}`.slice(-2), `0${d.getDate()}`.slice(-2)].join("/");
|
|
47
|
-
const start = d.getTime();
|
|
48
|
-
let end = null;
|
|
49
|
-
let timeout = null;
|
|
50
|
-
const context2 = {
|
|
51
|
-
callbackWaitsForEmptyEventLoop: true,
|
|
26
|
+
}, deferred = defer(), d = /* @__PURE__ */ new Date(), logDate = [d.getFullYear(), `0${d.getMonth() + 1}`.slice(-2), `0${d.getDate()}`.slice(-2)].join("/"), start = d.getTime(), end = null, timeout = null, context = {
|
|
27
|
+
callbackWaitsForEmptyEventLoop: !0,
|
|
52
28
|
functionName: opts.functionName,
|
|
53
29
|
functionVersion: opts.functionVersion,
|
|
54
30
|
invokedFunctionArn: `arn:aws:lambda:${opts.region}:${opts.account}:function:${opts.functionName}:${opts.alias || opts.functionVersion}`,
|
|
@@ -59,36 +35,26 @@ var context = (options, overrides) => {
|
|
|
59
35
|
identity: opts.identity,
|
|
60
36
|
clientContext: opts.clientContext,
|
|
61
37
|
getRemainingTimeInMillis: () => {
|
|
62
|
-
|
|
63
|
-
const remainingTime = opts.timeout * 1e3 - (endTime - start);
|
|
38
|
+
let endTime = end || Date.now(), remainingTime = opts.timeout * 1e3 - (endTime - start);
|
|
64
39
|
return Math.max(0, remainingTime);
|
|
65
40
|
},
|
|
66
41
|
succeed: (messageOrObject) => {
|
|
67
|
-
end = Date.now();
|
|
68
|
-
deferred.resolve(messageOrObject);
|
|
42
|
+
end = Date.now(), deferred.resolve(messageOrObject);
|
|
69
43
|
},
|
|
70
44
|
fail: (error) => {
|
|
71
|
-
end = Date.now();
|
|
72
|
-
deferred.reject(typeof error === "string" ? new Error(error) : error);
|
|
45
|
+
end = Date.now(), deferred.reject(typeof error == "string" ? new Error(error) : error);
|
|
73
46
|
},
|
|
74
47
|
done: (error, result) => {
|
|
75
|
-
if (timeout) {
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
if (error) {
|
|
79
|
-
context2.fail(error);
|
|
48
|
+
if (timeout && clearTimeout(timeout), error) {
|
|
49
|
+
context.fail(error);
|
|
80
50
|
return;
|
|
81
51
|
}
|
|
82
|
-
|
|
52
|
+
context.succeed(result);
|
|
83
53
|
},
|
|
84
54
|
Promise: deferred.promise
|
|
85
55
|
};
|
|
86
|
-
|
|
87
|
-
timeout = lambdaTimeout(context2, opts.timeout);
|
|
88
|
-
}
|
|
89
|
-
return Object.assign(context2, overrides);
|
|
56
|
+
return opts.timeout > 0 && (timeout = lambdaTimeout(context, opts.timeout)), Object.assign(context, overrides);
|
|
90
57
|
};
|
|
91
|
-
var src_default = context;
|
|
92
58
|
|
|
93
59
|
export { src_default as default };
|
|
94
60
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":["
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["uuidv1","uuidv4"],"mappings":";;;AAwBA,IAAM,QAAQ,MAAgB;AAC5B,EAAA,IAAM,WAA8B,EAAC;AAKrC,EAAO,OAAA;AAAA,IACL,OALc,EAAA,IAAI,OAAQ,CAAA,CAAC,SAAS,MAAW,KAAA;AAC/C,MAAS,QAAA,CAAA,OAAA,GAAU,OACnB,EAAA,QAAA,CAAS,MAAS,GAAA,MAAA;AAAA,KACnB,CAAA;AAAA;AAAA,IAIC,SAAS,QAAS,CAAA,OAAA;AAAA;AAAA,IAElB,QAAQ,QAAS,CAAA;AAAA,GACnB;AACF,CAAA,CAAA,CAEM,aAAgB,GAAA,CAAC,OAAsB,EAAA,OAAA,KACpC,WAAW,MAAM;AACtB,EAAI,IAAK,CAAA,KAAA,CAAM,OAAQ,CAAA,wBAAA,KAA6B,GAAI,CAAA,KAAM,CAC5D,IAAA,OAAA,CAAQ,KAAK,IAAI,KAAA,CAAM,CAAwB,qBAAA,EAAA,OAAO,aAAa,CAAC,CAAA;AAExE,CAAA,EAAG,UAAU,GAAI,CAAA,CAAA,CAGZ,WAAQ,GAAA,CAA0B,SAAmB,SAAmC,KAAA;AAC7F,EAAM,IAAA,EAAA,GAAKA,EAAO,EAAA,EACZ,MAAS,GAAAC,EAAA,GAAS,OAAQ,CAAA,IAAA,EAAM,EAAE,CAAA,EAElC,IAAO,GAAA;AAAA,IACX,MAAQ,EAAA,WAAA;AAAA,IACR,OAAS,EAAA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A,IACtB,aAAgB,GAAA,IAAA,CAAK,OAAU,GAAA,GAAA,IAAQ,OAAU,GAAA,KAAA,CAAA;AAEvD,MAAO,OAAA,IAAA,CAAK,GAAI,CAAA,CAAA,EAAG,aAAa,CAAA;AAAA,KAClC;AAAA,IACA,OAAA,EAAS,CAAC,eAAoB,KAAA;AAC5B,MAAA,GAAA,GAAM,IAAK,CAAA,GAAA,EAEX,EAAA,QAAA,CAAS,QAAQ,eAAe,CAAA;AAAA,KAClC;AAAA,IACA,IAAA,EAAM,CAAC,KAAU,KAAA;AACf,MAAA,GAAA,GAAM,IAAK,CAAA,GAAA,EAEX,EAAA,QAAA,CAAS,MAAO,CAAA,OAAO,KAAU,IAAA,QAAA,GAAW,IAAI,KAAA,CAAM,KAAK,CAAA,GAAI,KAAK,CAAA;AAAA,KACtE;AAAA,IACA,IAAA,EAAM,CAAC,KAAA,EAAO,MAAW,KAAA;AAKvB,MAAA,IAJI,OACF,IAAA,YAAA,CAAa,OAAO,CAAA,EAGlB,KAAO,EAAA;AACT,QAAA,OAAA,CAAQ,KAAK,KAAK,CAAA;AAClB,QAAA;AAAA;AAGF,MAAA,OAAA,CAAQ,QAAQ,MAAM,CAAA;AAAA,KACxB;AAAA,IACA,SAAS,QAAS,CAAA;AAAA,GACpB;AAGA,EAAA,OAAI,IAAK,CAAA,OAAA,GAAU,CACjB,KAAA,OAAA,GAAU,aAAc,CAAA,OAAA,EAAS,IAAK,CAAA,OAAO,CAGxC,CAAA,EAAA,MAAA,CAAO,MAAO,CAAA,OAAA,EAAS,SAAS,CAAA;AACzC","file":"index.mjs","sourcesContent":["import type { ClientContext, CognitoIdentity, Context } from 'aws-lambda'\nimport { v1 as uuidv1, v4 as uuidv4 } from 'uuid'\n\ntype Options = {\n region?: string\n account?: string\n alias?: string\n functionName?: string\n functionVersion?: string\n memoryLimitInMB?: string\n identity?: CognitoIdentity\n clientContext?: ClientContext\n timeout?: number\n}\n\ninterface MockContext extends Context {\n Promise: Promise<any>\n}\n\ntype Deferred = {\n promise: Promise<unknown>\n resolve: (value: unknown) => void\n reject: (reason?: any) => void\n}\nconst defer = (): Deferred => {\n const deferred: Partial<Deferred> = {}\n const promise = new Promise((resolve, reject) => {\n deferred.resolve = resolve\n deferred.reject = reject\n })\n return {\n promise,\n // biome-ignore lint/style/noNonNullAssertion: <explanation>\n resolve: deferred.resolve!,\n // biome-ignore lint/style/noNonNullAssertion: <explanation>\n reject: deferred.reject!,\n }\n}\n\nconst lambdaTimeout = (context: MockContext, timeout: number): NodeJS.Timeout => {\n return setTimeout(() => {\n if (Math.round(context.getRemainingTimeInMillis() / 1000) === 0) {\n context.fail(new Error(`Task timed out after ${timeout}.00 seconds`))\n }\n }, timeout * 1000)\n}\n\nexport default <E = Record<string, any>>(options?: Options, overrides?: E): MockContext & E => {\n const id = uuidv1()\n const stream = uuidv4().replace(/-/g, '')\n\n const opts = {\n region: 'us-west-1',\n account: '123456789012',\n functionName: 'aws-lambda-mock-context',\n functionVersion: '$LATEST',\n memoryLimitInMB: '128',\n timeout: 3,\n ...options,\n }\n\n const deferred = defer()\n\n const d = new Date()\n const logDate = [d.getFullYear(), `0${d.getMonth() + 1}`.slice(-2), `0${d.getDate()}`.slice(-2)].join('/')\n const start = d.getTime()\n let end: number | null = null\n let timeout: NodeJS.Timeout | null = null\n\n // https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/nodejs-context.html\n const context: MockContext = {\n callbackWaitsForEmptyEventLoop: true,\n functionName: opts.functionName,\n functionVersion: opts.functionVersion,\n invokedFunctionArn: `arn:aws:lambda:${opts.region}:${opts.account}:function:${opts.functionName}:${opts.alias || opts.functionVersion}`,\n memoryLimitInMB: opts.memoryLimitInMB,\n awsRequestId: id,\n logGroupName: `/aws/lambda/${opts.functionName}`,\n logStreamName: `${logDate}/[${opts.functionVersion}]/${stream}`,\n identity: opts.identity,\n clientContext: opts.clientContext,\n getRemainingTimeInMillis: () => {\n const endTime = end || Date.now()\n const remainingTime = opts.timeout * 1000 - (endTime - start)\n\n return Math.max(0, remainingTime)\n },\n succeed: (messageOrObject) => {\n end = Date.now()\n\n deferred.resolve(messageOrObject)\n },\n fail: (error) => {\n end = Date.now()\n\n deferred.reject(typeof error === 'string' ? new Error(error) : error)\n },\n done: (error, result) => {\n if (timeout) {\n clearTimeout(timeout)\n }\n\n if (error) {\n context.fail(error)\n return\n }\n\n context.succeed(result)\n },\n Promise: deferred.promise,\n }\n\n // Lambda Timeout\n if (opts.timeout > 0) {\n timeout = lambdaTimeout(context, opts.timeout)\n }\n\n return Object.assign(context, overrides)\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gahojin-inc/aws-lambda-mock-context",
|
|
3
|
-
"version": "2024.11.
|
|
3
|
+
"version": "2024.11.2",
|
|
4
4
|
"description": "AWS Lambda mock context object",
|
|
5
5
|
"author": "GAHOJIN, Inc.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -24,24 +24,29 @@
|
|
|
24
24
|
},
|
|
25
25
|
"main": "dist/cjs/index.cjs",
|
|
26
26
|
"module": "dist/index.mjs",
|
|
27
|
-
"types": "dist/index.d.ts",
|
|
28
27
|
"exports": {
|
|
29
28
|
"./package.json": "./package.json",
|
|
30
29
|
".": {
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
"import": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"default": "./dist/index.mjs"
|
|
33
|
+
},
|
|
34
|
+
"require": {
|
|
35
|
+
"types": "./dist/cjs/index.d.cts",
|
|
36
|
+
"default": "./dist/cjs/index.cjs"
|
|
37
|
+
}
|
|
34
38
|
}
|
|
35
39
|
},
|
|
36
40
|
"dependencies": {
|
|
37
41
|
"uuid": "11.0.3"
|
|
38
42
|
},
|
|
39
43
|
"devDependencies": {
|
|
44
|
+
"@arethetypeswrong/cli": "0.17.0",
|
|
40
45
|
"@biomejs/biome": "1.9.4",
|
|
41
|
-
"@commitlint/cli": "19.
|
|
42
|
-
"@commitlint/config-conventional": "19.
|
|
46
|
+
"@commitlint/cli": "19.6.0",
|
|
47
|
+
"@commitlint/config-conventional": "19.6.0",
|
|
43
48
|
"@types/aws-lambda": "8.10.145",
|
|
44
|
-
"@types/node": "20.17.
|
|
49
|
+
"@types/node": "20.17.7",
|
|
45
50
|
"@types/uuid": "10.0.0",
|
|
46
51
|
"@vitest/coverage-v8": "2.1.5",
|
|
47
52
|
"husky": "9.1.7",
|
|
@@ -49,8 +54,8 @@
|
|
|
49
54
|
"npm-check-updates": "17.1.11",
|
|
50
55
|
"rimraf": "6.0.1",
|
|
51
56
|
"tsup": "8.3.5",
|
|
52
|
-
"typescript": "5.
|
|
53
|
-
"vite-tsconfig-paths": "5.1.
|
|
57
|
+
"typescript": "5.7.2",
|
|
58
|
+
"vite-tsconfig-paths": "5.1.3",
|
|
54
59
|
"vitest": "2.1.5"
|
|
55
60
|
},
|
|
56
61
|
"peerDependencies": {
|
|
@@ -69,6 +74,7 @@
|
|
|
69
74
|
"prepublish": "pnpm run build",
|
|
70
75
|
"lint": "biome check --write .",
|
|
71
76
|
"check": "tsc",
|
|
77
|
+
"check:packagejson": "attw --pack .",
|
|
72
78
|
"test": "vitest --watch",
|
|
73
79
|
"test:unit": "vitest --passWithNoTests --run --coverage",
|
|
74
80
|
"ncu": "npm-check-updates -u"
|