@gahojin-inc/aws-lambda-mock-context 2025.3.0 → 2025.4.1
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/index.cjs +125 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +21 -0
- package/dist/index.d.mts +21 -0
- package/dist/index.mjs +94 -70
- package/dist/index.mjs.map +1 -1
- package/package.json +19 -17
- package/dist/cjs/index.cjs +0 -80
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/index.d.cts +0 -22
- package/dist/index.d.ts +0 -22
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
//#region rolldown:runtime
|
|
4
|
+
var __create = Object.create;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
12
|
+
key = keys[i];
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
21
|
+
value: mod,
|
|
22
|
+
enumerable: true
|
|
23
|
+
}) : target, mod));
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
const node_crypto = __toESM(require("node:crypto"));
|
|
27
|
+
|
|
28
|
+
//#region src/index.ts
|
|
29
|
+
const defer = () => {
|
|
30
|
+
const deferred = {};
|
|
31
|
+
const promise = new Promise((resolve, reject) => {
|
|
32
|
+
deferred.resolve = resolve;
|
|
33
|
+
deferred.reject = reject;
|
|
34
|
+
});
|
|
35
|
+
return {
|
|
36
|
+
promise,
|
|
37
|
+
resolve: deferred.resolve,
|
|
38
|
+
reject: deferred.reject
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
const lambdaTimeout = (context, timeout) => {
|
|
42
|
+
return setTimeout(() => {
|
|
43
|
+
if (Math.round(context.getRemainingTimeInMillis() / 1e3) === 0) context.fail(new Error(`Task timed out after ${timeout}.00 seconds`));
|
|
44
|
+
}, timeout * 1e3);
|
|
45
|
+
};
|
|
46
|
+
let globalOptions = {};
|
|
47
|
+
const setGlobalOptions = (options) => {
|
|
48
|
+
Object.assign(globalOptions, options);
|
|
49
|
+
return globalOptions;
|
|
50
|
+
};
|
|
51
|
+
const resetGlobalOptions = () => {
|
|
52
|
+
globalOptions = {};
|
|
53
|
+
};
|
|
54
|
+
const getStrictKeys = (object) => Object.keys(object);
|
|
55
|
+
const removeUndefinedValues = (options) => {
|
|
56
|
+
if (!options) return {};
|
|
57
|
+
for (const key of getStrictKeys(options)) options[key] === void 0 && delete options[key];
|
|
58
|
+
return options;
|
|
59
|
+
};
|
|
60
|
+
const mockContext = (options, overrides) => {
|
|
61
|
+
const requestId = (0, node_crypto.randomUUID)();
|
|
62
|
+
const logStream = requestId.replace(/-/g, "");
|
|
63
|
+
const opts = Object.assign({
|
|
64
|
+
region: "us-west-1",
|
|
65
|
+
account: "123456789012",
|
|
66
|
+
functionName: "aws-lambda-mock-context",
|
|
67
|
+
functionVersion: "$LATEST",
|
|
68
|
+
memoryLimitInMB: "128",
|
|
69
|
+
timeout: 3,
|
|
70
|
+
handleTimeout: false
|
|
71
|
+
}, removeUndefinedValues(globalOptions), removeUndefinedValues(options));
|
|
72
|
+
const deferred = defer();
|
|
73
|
+
const d = new Date();
|
|
74
|
+
const logDate = [
|
|
75
|
+
d.getFullYear(),
|
|
76
|
+
`0${d.getMonth() + 1}`.slice(-2),
|
|
77
|
+
`0${d.getDate()}`.slice(-2)
|
|
78
|
+
].join("/");
|
|
79
|
+
const start = d.getTime();
|
|
80
|
+
let end = null;
|
|
81
|
+
let timeout = null;
|
|
82
|
+
const context = {
|
|
83
|
+
callbackWaitsForEmptyEventLoop: true,
|
|
84
|
+
functionName: opts.functionName,
|
|
85
|
+
functionVersion: opts.functionVersion,
|
|
86
|
+
invokedFunctionArn: `arn:aws:lambda:${opts.region}:${opts.account}:function:${opts.functionName}:${opts.alias || opts.functionVersion}`,
|
|
87
|
+
memoryLimitInMB: opts.memoryLimitInMB,
|
|
88
|
+
awsRequestId: requestId,
|
|
89
|
+
logGroupName: `/aws/lambda/${opts.functionName}`,
|
|
90
|
+
logStreamName: `${logDate}/[${opts.functionVersion}]/${logStream}`,
|
|
91
|
+
identity: opts.identity,
|
|
92
|
+
clientContext: opts.clientContext,
|
|
93
|
+
getRemainingTimeInMillis: () => {
|
|
94
|
+
const endTime = end || Date.now();
|
|
95
|
+
const remainingTime = opts.timeout * 1e3 - (endTime - start);
|
|
96
|
+
return Math.max(0, remainingTime);
|
|
97
|
+
},
|
|
98
|
+
succeed: (messageOrObject) => {
|
|
99
|
+
end = Date.now();
|
|
100
|
+
deferred.resolve(messageOrObject);
|
|
101
|
+
},
|
|
102
|
+
fail: (error) => {
|
|
103
|
+
end = Date.now();
|
|
104
|
+
deferred.reject(typeof error === "string" ? new Error(error) : error);
|
|
105
|
+
},
|
|
106
|
+
done: (error, result) => {
|
|
107
|
+
if (timeout) clearTimeout(timeout);
|
|
108
|
+
if (error) {
|
|
109
|
+
context.fail(error);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
context.succeed(result);
|
|
113
|
+
},
|
|
114
|
+
Promise: deferred.promise
|
|
115
|
+
};
|
|
116
|
+
if (opts.handleTimeout && opts.timeout > 0) timeout = lambdaTimeout(context, opts.timeout);
|
|
117
|
+
return Object.assign(context, overrides);
|
|
118
|
+
};
|
|
119
|
+
var src_default = mockContext;
|
|
120
|
+
|
|
121
|
+
//#endregion
|
|
122
|
+
exports.default = src_default
|
|
123
|
+
exports.resetGlobalOptions = resetGlobalOptions
|
|
124
|
+
exports.setGlobalOptions = setGlobalOptions
|
|
125
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["deferred: Partial<Deferred>","context: MockContext","timeout: number","globalOptions: Options","options: Options","object: T","options?: Options","overrides?: E","end: number | null","timeout: NodeJS.Timeout | null"],"sources":["../src/index.ts"],"sourcesContent":["import { randomUUID } from 'node:crypto'\nimport type { ClientContext, CognitoIdentity, Context } from 'aws-lambda'\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 handleTimeout?: boolean\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}\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\nlet globalOptions: Options = {}\n\nconst setGlobalOptions = (options: Options): Options => {\n Object.assign(globalOptions, options)\n return globalOptions\n}\n\nconst resetGlobalOptions = (): void => {\n globalOptions = {}\n}\n\nconst getStrictKeys = <T extends Record<string, any>>(object: T): (keyof T)[] => Object.keys(object)\n\nconst removeUndefinedValues = (options?: Options): Options => {\n if (!options) {\n return {}\n }\n for (const key of getStrictKeys(options)) {\n options[key] === undefined && delete options[key]\n }\n return options\n}\n\nconst mockContext = <E = Record<string, any>>(options?: Options, overrides?: E): MockContext & E => {\n const requestId = randomUUID()\n const logStream = requestId.replace(/-/g, '')\n\n const opts = Object.assign(\n {\n region: 'us-west-1',\n account: '123456789012',\n functionName: 'aws-lambda-mock-context',\n functionVersion: '$LATEST',\n memoryLimitInMB: '128',\n timeout: 3,\n handleTimeout: false,\n },\n removeUndefinedValues(globalOptions),\n removeUndefinedValues(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: requestId,\n logGroupName: `/aws/lambda/${opts.functionName}`,\n logStreamName: `${logDate}/[${opts.functionVersion}]/${logStream}`,\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.handleTimeout && opts.timeout > 0) {\n timeout = lambdaTimeout(context, opts.timeout)\n }\n\n return Object.assign(context, overrides)\n}\n\nexport { setGlobalOptions, resetGlobalOptions }\nexport default mockContext\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,MAAM,QAAQ,MAAgB;CAC5B,MAAMA,WAA8B,CAAE;CACtC,MAAM,UAAU,IAAI,QAAQ,CAAC,SAAS,WAAW;AAC/C,WAAS,UAAU;AACnB,WAAS,SAAS;CACnB;AACD,QAAO;EACL;EAEA,SAAS,SAAS;EAElB,QAAQ,SAAS;CAClB;AACF;AAED,MAAM,gBAAgB,CAACC,SAAsBC,YAAoC;AAC/E,QAAO,WAAW,MAAM;AACtB,MAAI,KAAK,MAAM,QAAQ,0BAA0B,GAAG,IAAK,KAAK,EAC5D,SAAQ,KAAK,IAAI,OAAO,uBAAuB,QAAQ,cAAc;CAExE,GAAE,UAAU,IAAK;AACnB;AAED,IAAIC,gBAAyB,CAAE;AAE/B,MAAM,mBAAmB,CAACC,YAA8B;AACtD,QAAO,OAAO,eAAe,QAAQ;AACrC,QAAO;AACR;AAED,MAAM,qBAAqB,MAAY;AACrC,iBAAgB,CAAE;AACnB;AAED,MAAM,gBAAgB,CAAgCC,WAA2B,OAAO,KAAK,OAAO;AAEpG,MAAM,wBAAwB,CAACC,YAA+B;AAC5D,MAAK,QACH,QAAO,CAAE;AAEX,MAAK,MAAM,OAAO,cAAc,QAAQ,CACtC,SAAQ,0BAA6B,QAAQ;AAE/C,QAAO;AACR;AAED,MAAM,cAAc,CAA0BA,SAAmBC,cAAmC;CAClG,MAAM,YAAY,6BAAY;CAC9B,MAAM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AAGvB,OAAI,OAAO;AACT,YAAQ,KAAK,MAAM;AACnB;GACD;AAED,WAAQ,QAAQ,OAAO;EACxB;EACD,SAAS,SAAS;CACnB;AAGD,KAAI,KAAK,iBAAiB,KAAK,UAAU,EACvC,WAAU,cAAc,SAAS,KAAK,QAAQ;AAGhD,QAAO,OAAO,OAAO,SAAS,UAAU;AACzC;AAGD,kBAAe"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ClientContext, CognitoIdentity, Context } from "aws-lambda";
|
|
2
|
+
type Options = {
|
|
3
|
+
region?: string
|
|
4
|
+
account?: string
|
|
5
|
+
alias?: string
|
|
6
|
+
functionName?: string
|
|
7
|
+
functionVersion?: string
|
|
8
|
+
memoryLimitInMB?: string
|
|
9
|
+
identity?: CognitoIdentity
|
|
10
|
+
clientContext?: ClientContext
|
|
11
|
+
timeout?: number
|
|
12
|
+
handleTimeout?: boolean
|
|
13
|
+
};
|
|
14
|
+
interface MockContext extends Context {
|
|
15
|
+
Promise: Promise<any>;
|
|
16
|
+
}
|
|
17
|
+
declare const setGlobalOptions: (options: Options) => Options;
|
|
18
|
+
declare const resetGlobalOptions: () => void;
|
|
19
|
+
declare const mockContext: <E = Record<string, any>>(options?: Options, overrides?: E) => MockContext & E;
|
|
20
|
+
export { setGlobalOptions, resetGlobalOptions };
|
|
21
|
+
export default mockContext;
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ClientContext, CognitoIdentity, Context } from "aws-lambda";
|
|
2
|
+
type Options = {
|
|
3
|
+
region?: string
|
|
4
|
+
account?: string
|
|
5
|
+
alias?: string
|
|
6
|
+
functionName?: string
|
|
7
|
+
functionVersion?: string
|
|
8
|
+
memoryLimitInMB?: string
|
|
9
|
+
identity?: CognitoIdentity
|
|
10
|
+
clientContext?: ClientContext
|
|
11
|
+
timeout?: number
|
|
12
|
+
handleTimeout?: boolean
|
|
13
|
+
};
|
|
14
|
+
interface MockContext extends Context {
|
|
15
|
+
Promise: Promise<any>;
|
|
16
|
+
}
|
|
17
|
+
declare const setGlobalOptions: (options: Options) => Options;
|
|
18
|
+
declare const resetGlobalOptions: () => void;
|
|
19
|
+
declare const mockContext: <E = Record<string, any>>(options?: Options, overrides?: E) => MockContext & E;
|
|
20
|
+
export { setGlobalOptions, resetGlobalOptions };
|
|
21
|
+
export default mockContext;
|
package/dist/index.mjs
CHANGED
|
@@ -1,74 +1,98 @@
|
|
|
1
|
-
import { randomUUID } from
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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), globalOptions = {}, setGlobalOptions = (options) => (Object.assign(globalOptions, options), globalOptions), resetGlobalOptions = () => {
|
|
18
|
-
globalOptions = {};
|
|
19
|
-
}, getStrictKeys = (object) => Object.keys(object), removeUndefinedValues = (options) => {
|
|
20
|
-
if (!options)
|
|
21
|
-
return {};
|
|
22
|
-
for (let key of getStrictKeys(options))
|
|
23
|
-
options[key] === void 0 && delete options[key];
|
|
24
|
-
return options;
|
|
25
|
-
}, mockContext = (options, overrides) => {
|
|
26
|
-
let requestId = randomUUID(), logStream = requestId.replace(/-/g, ""), opts = Object.assign(
|
|
27
|
-
{
|
|
28
|
-
region: "us-west-1",
|
|
29
|
-
account: "123456789012",
|
|
30
|
-
functionName: "aws-lambda-mock-context",
|
|
31
|
-
functionVersion: "$LATEST",
|
|
32
|
-
memoryLimitInMB: "128",
|
|
33
|
-
timeout: 3,
|
|
34
|
-
handleTimeout: false
|
|
35
|
-
},
|
|
36
|
-
removeUndefinedValues(globalOptions),
|
|
37
|
-
removeUndefinedValues(options)
|
|
38
|
-
), 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 = {
|
|
39
|
-
callbackWaitsForEmptyEventLoop: true,
|
|
40
|
-
functionName: opts.functionName,
|
|
41
|
-
functionVersion: opts.functionVersion,
|
|
42
|
-
invokedFunctionArn: `arn:aws:lambda:${opts.region}:${opts.account}:function:${opts.functionName}:${opts.alias || opts.functionVersion}`,
|
|
43
|
-
memoryLimitInMB: opts.memoryLimitInMB,
|
|
44
|
-
awsRequestId: requestId,
|
|
45
|
-
logGroupName: `/aws/lambda/${opts.functionName}`,
|
|
46
|
-
logStreamName: `${logDate}/[${opts.functionVersion}]/${logStream}`,
|
|
47
|
-
identity: opts.identity,
|
|
48
|
-
clientContext: opts.clientContext,
|
|
49
|
-
getRemainingTimeInMillis: () => {
|
|
50
|
-
let endTime = end || Date.now(), remainingTime = opts.timeout * 1e3 - (endTime - start);
|
|
51
|
-
return Math.max(0, remainingTime);
|
|
52
|
-
},
|
|
53
|
-
succeed: (messageOrObject) => {
|
|
54
|
-
end = Date.now(), deferred.resolve(messageOrObject);
|
|
55
|
-
},
|
|
56
|
-
fail: (error) => {
|
|
57
|
-
end = Date.now(), deferred.reject(typeof error == "string" ? new Error(error) : error);
|
|
58
|
-
},
|
|
59
|
-
done: (error, result) => {
|
|
60
|
-
if (timeout && clearTimeout(timeout), error) {
|
|
61
|
-
context.fail(error);
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
context.succeed(result);
|
|
65
|
-
},
|
|
66
|
-
Promise: deferred.promise
|
|
67
|
-
};
|
|
68
|
-
return opts.handleTimeout && opts.timeout > 0 && (timeout = lambdaTimeout(context, opts.timeout)), Object.assign(context, overrides);
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
const defer = () => {
|
|
5
|
+
const deferred = {};
|
|
6
|
+
const promise = new Promise((resolve, reject) => {
|
|
7
|
+
deferred.resolve = resolve;
|
|
8
|
+
deferred.reject = reject;
|
|
9
|
+
});
|
|
10
|
+
return {
|
|
11
|
+
promise,
|
|
12
|
+
resolve: deferred.resolve,
|
|
13
|
+
reject: deferred.reject
|
|
14
|
+
};
|
|
69
15
|
};
|
|
70
|
-
|
|
16
|
+
const lambdaTimeout = (context, timeout) => {
|
|
17
|
+
return setTimeout(() => {
|
|
18
|
+
if (Math.round(context.getRemainingTimeInMillis() / 1e3) === 0) context.fail(new Error(`Task timed out after ${timeout}.00 seconds`));
|
|
19
|
+
}, timeout * 1e3);
|
|
20
|
+
};
|
|
21
|
+
let globalOptions = {};
|
|
22
|
+
const setGlobalOptions = (options) => {
|
|
23
|
+
Object.assign(globalOptions, options);
|
|
24
|
+
return globalOptions;
|
|
25
|
+
};
|
|
26
|
+
const resetGlobalOptions = () => {
|
|
27
|
+
globalOptions = {};
|
|
28
|
+
};
|
|
29
|
+
const getStrictKeys = (object) => Object.keys(object);
|
|
30
|
+
const removeUndefinedValues = (options) => {
|
|
31
|
+
if (!options) return {};
|
|
32
|
+
for (const key of getStrictKeys(options)) options[key] === void 0 && delete options[key];
|
|
33
|
+
return options;
|
|
34
|
+
};
|
|
35
|
+
const mockContext = (options, overrides) => {
|
|
36
|
+
const requestId = randomUUID();
|
|
37
|
+
const logStream = requestId.replace(/-/g, "");
|
|
38
|
+
const opts = Object.assign({
|
|
39
|
+
region: "us-west-1",
|
|
40
|
+
account: "123456789012",
|
|
41
|
+
functionName: "aws-lambda-mock-context",
|
|
42
|
+
functionVersion: "$LATEST",
|
|
43
|
+
memoryLimitInMB: "128",
|
|
44
|
+
timeout: 3,
|
|
45
|
+
handleTimeout: false
|
|
46
|
+
}, removeUndefinedValues(globalOptions), removeUndefinedValues(options));
|
|
47
|
+
const deferred = defer();
|
|
48
|
+
const d = new Date();
|
|
49
|
+
const logDate = [
|
|
50
|
+
d.getFullYear(),
|
|
51
|
+
`0${d.getMonth() + 1}`.slice(-2),
|
|
52
|
+
`0${d.getDate()}`.slice(-2)
|
|
53
|
+
].join("/");
|
|
54
|
+
const start = d.getTime();
|
|
55
|
+
let end = null;
|
|
56
|
+
let timeout = null;
|
|
57
|
+
const context = {
|
|
58
|
+
callbackWaitsForEmptyEventLoop: true,
|
|
59
|
+
functionName: opts.functionName,
|
|
60
|
+
functionVersion: opts.functionVersion,
|
|
61
|
+
invokedFunctionArn: `arn:aws:lambda:${opts.region}:${opts.account}:function:${opts.functionName}:${opts.alias || opts.functionVersion}`,
|
|
62
|
+
memoryLimitInMB: opts.memoryLimitInMB,
|
|
63
|
+
awsRequestId: requestId,
|
|
64
|
+
logGroupName: `/aws/lambda/${opts.functionName}`,
|
|
65
|
+
logStreamName: `${logDate}/[${opts.functionVersion}]/${logStream}`,
|
|
66
|
+
identity: opts.identity,
|
|
67
|
+
clientContext: opts.clientContext,
|
|
68
|
+
getRemainingTimeInMillis: () => {
|
|
69
|
+
const endTime = end || Date.now();
|
|
70
|
+
const remainingTime = opts.timeout * 1e3 - (endTime - start);
|
|
71
|
+
return Math.max(0, remainingTime);
|
|
72
|
+
},
|
|
73
|
+
succeed: (messageOrObject) => {
|
|
74
|
+
end = Date.now();
|
|
75
|
+
deferred.resolve(messageOrObject);
|
|
76
|
+
},
|
|
77
|
+
fail: (error) => {
|
|
78
|
+
end = Date.now();
|
|
79
|
+
deferred.reject(typeof error === "string" ? new Error(error) : error);
|
|
80
|
+
},
|
|
81
|
+
done: (error, result) => {
|
|
82
|
+
if (timeout) clearTimeout(timeout);
|
|
83
|
+
if (error) {
|
|
84
|
+
context.fail(error);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
context.succeed(result);
|
|
88
|
+
},
|
|
89
|
+
Promise: deferred.promise
|
|
90
|
+
};
|
|
91
|
+
if (opts.handleTimeout && opts.timeout > 0) timeout = lambdaTimeout(context, opts.timeout);
|
|
92
|
+
return Object.assign(context, overrides);
|
|
93
|
+
};
|
|
94
|
+
var src_default = mockContext;
|
|
71
95
|
|
|
72
|
-
|
|
73
|
-
|
|
96
|
+
//#endregion
|
|
97
|
+
export { src_default as default, resetGlobalOptions, setGlobalOptions };
|
|
74
98
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["deferred: Partial<Deferred>","context: MockContext","timeout: number","globalOptions: Options","options: Options","object: T","options?: Options","overrides?: E","end: number | null","timeout: NodeJS.Timeout | null"],"sources":["../src/index.ts"],"sourcesContent":["import { randomUUID } from 'node:crypto'\nimport type { ClientContext, CognitoIdentity, Context } from 'aws-lambda'\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 handleTimeout?: boolean\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}\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\nlet globalOptions: Options = {}\n\nconst setGlobalOptions = (options: Options): Options => {\n Object.assign(globalOptions, options)\n return globalOptions\n}\n\nconst resetGlobalOptions = (): void => {\n globalOptions = {}\n}\n\nconst getStrictKeys = <T extends Record<string, any>>(object: T): (keyof T)[] => Object.keys(object)\n\nconst removeUndefinedValues = (options?: Options): Options => {\n if (!options) {\n return {}\n }\n for (const key of getStrictKeys(options)) {\n options[key] === undefined && delete options[key]\n }\n return options\n}\n\nconst mockContext = <E = Record<string, any>>(options?: Options, overrides?: E): MockContext & E => {\n const requestId = randomUUID()\n const logStream = requestId.replace(/-/g, '')\n\n const opts = Object.assign(\n {\n region: 'us-west-1',\n account: '123456789012',\n functionName: 'aws-lambda-mock-context',\n functionVersion: '$LATEST',\n memoryLimitInMB: '128',\n timeout: 3,\n handleTimeout: false,\n },\n removeUndefinedValues(globalOptions),\n removeUndefinedValues(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: requestId,\n logGroupName: `/aws/lambda/${opts.functionName}`,\n logStreamName: `${logDate}/[${opts.functionVersion}]/${logStream}`,\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.handleTimeout && opts.timeout > 0) {\n timeout = lambdaTimeout(context, opts.timeout)\n }\n\n return Object.assign(context, overrides)\n}\n\nexport { setGlobalOptions, resetGlobalOptions }\nexport default mockContext\n"],"mappings":";;;AA0BA,MAAM,QAAQ,MAAgB;CAC5B,MAAMA,WAA8B,CAAE;CACtC,MAAM,UAAU,IAAI,QAAQ,CAAC,SAAS,WAAW;AAC/C,WAAS,UAAU;AACnB,WAAS,SAAS;CACnB;AACD,QAAO;EACL;EAEA,SAAS,SAAS;EAElB,QAAQ,SAAS;CAClB;AACF;AAED,MAAM,gBAAgB,CAACC,SAAsBC,YAAoC;AAC/E,QAAO,WAAW,MAAM;AACtB,MAAI,KAAK,MAAM,QAAQ,0BAA0B,GAAG,IAAK,KAAK,EAC5D,SAAQ,KAAK,IAAI,OAAO,uBAAuB,QAAQ,cAAc;CAExE,GAAE,UAAU,IAAK;AACnB;AAED,IAAIC,gBAAyB,CAAE;AAE/B,MAAM,mBAAmB,CAACC,YAA8B;AACtD,QAAO,OAAO,eAAe,QAAQ;AACrC,QAAO;AACR;AAED,MAAM,qBAAqB,MAAY;AACrC,iBAAgB,CAAE;AACnB;AAED,MAAM,gBAAgB,CAAgCC,WAA2B,OAAO,KAAK,OAAO;AAEpG,MAAM,wBAAwB,CAACC,YAA+B;AAC5D,MAAK,QACH,QAAO,CAAE;AAEX,MAAK,MAAM,OAAO,cAAc,QAAQ,CACtC,SAAQ,0BAA6B,QAAQ;AAE/C,QAAO;AACR;AAED,MAAM,cAAc,CAA0BA,SAAmBC,cAAmC;CAClG,MAAM,YAAY,YAAY;CAC9B,MAAM,YAAY,UAAU,QAAQ,MAAM,GAAG;CAE7C,MAAM,OAAO,OAAO,OAClB;EACE,QAAQ;EACR,SAAS;EACT,cAAc;EACd,iBAAiB;EACjB,iBAAiB;EACjB,SAAS;EACT,eAAe;CAChB,GACD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,cAAc,SAAS,KAAK,QAAQ;AAGhD,QAAO,OAAO,OAAO,SAAS,UAAU;AACzC;AAGD,kBAAe"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gahojin-inc/aws-lambda-mock-context",
|
|
3
|
-
"version": "2025.
|
|
3
|
+
"version": "2025.4.1",
|
|
4
4
|
"description": "AWS Lambda mock context object",
|
|
5
5
|
"author": "GAHOJIN, Inc.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -22,38 +22,39 @@
|
|
|
22
22
|
"access": "public",
|
|
23
23
|
"provenance": true
|
|
24
24
|
},
|
|
25
|
-
"main": "dist/
|
|
25
|
+
"main": "dist/index.cjs",
|
|
26
26
|
"module": "dist/index.mjs",
|
|
27
|
+
"types": "dist/index.d.cts",
|
|
27
28
|
"exports": {
|
|
28
29
|
"./package.json": "./package.json",
|
|
29
30
|
".": {
|
|
30
31
|
"import": {
|
|
31
|
-
"types": "./dist/index.d.
|
|
32
|
+
"types": "./dist/index.d.mts",
|
|
32
33
|
"default": "./dist/index.mjs"
|
|
33
34
|
},
|
|
34
35
|
"require": {
|
|
35
|
-
"types": "./dist/
|
|
36
|
-
"default": "./dist/
|
|
36
|
+
"types": "./dist/index.d.cts",
|
|
37
|
+
"default": "./dist/index.cjs"
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
42
|
"@arethetypeswrong/cli": "0.17.4",
|
|
42
43
|
"@biomejs/biome": "1.9.4",
|
|
43
|
-
"@commitlint/cli": "19.
|
|
44
|
-
"@commitlint/config-conventional": "19.
|
|
45
|
-
"@types/aws-lambda": "8.10.
|
|
46
|
-
"@types/node": "20.17.
|
|
47
|
-
"@vitest/coverage-v8": "3.
|
|
48
|
-
"esbuild": "0.25.0",
|
|
49
|
-
"husky": "9.1.7",
|
|
44
|
+
"@commitlint/cli": "19.8.0",
|
|
45
|
+
"@commitlint/config-conventional": "19.8.0",
|
|
46
|
+
"@types/aws-lambda": "8.10.149",
|
|
47
|
+
"@types/node": "20.17.32",
|
|
48
|
+
"@vitest/coverage-v8": "3.1.2",
|
|
50
49
|
"jest-extended": "4.0.2",
|
|
51
|
-
"
|
|
50
|
+
"lefthook": "1.11.12",
|
|
51
|
+
"npm-check-updates": "18.0.1",
|
|
52
52
|
"rimraf": "6.0.1",
|
|
53
|
-
"
|
|
54
|
-
"typescript": "5.8.
|
|
53
|
+
"rolldown": "1.0.0-beta.8",
|
|
54
|
+
"typescript": "5.8.3",
|
|
55
|
+
"unplugin-isolated-decl": "0.13.10",
|
|
55
56
|
"vite-tsconfig-paths": "5.1.4",
|
|
56
|
-
"vitest": "3.
|
|
57
|
+
"vitest": "3.1.2"
|
|
57
58
|
},
|
|
58
59
|
"engines": {
|
|
59
60
|
"node": ">=18.x",
|
|
@@ -64,7 +65,8 @@
|
|
|
64
65
|
"scripts": {
|
|
65
66
|
"preinstall": "pnpm dlx only-allow pnpm",
|
|
66
67
|
"clean": "rimraf dist",
|
|
67
|
-
"
|
|
68
|
+
"prebuild": "rimraf dist",
|
|
69
|
+
"build": "rolldown -c",
|
|
68
70
|
"prepublish": "pnpm run build",
|
|
69
71
|
"lint": "biome check --write .",
|
|
70
72
|
"check": "tsc",
|
package/dist/cjs/index.cjs
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var crypto = require('crypto');
|
|
6
|
-
|
|
7
|
-
// src/index.ts
|
|
8
|
-
var defer = () => {
|
|
9
|
-
let deferred = {};
|
|
10
|
-
return {
|
|
11
|
-
promise: new Promise((resolve, reject) => {
|
|
12
|
-
deferred.resolve = resolve, deferred.reject = reject;
|
|
13
|
-
}),
|
|
14
|
-
// biome-ignore lint/style/noNonNullAssertion: <explanation>
|
|
15
|
-
resolve: deferred.resolve,
|
|
16
|
-
// biome-ignore lint/style/noNonNullAssertion: <explanation>
|
|
17
|
-
reject: deferred.reject
|
|
18
|
-
};
|
|
19
|
-
}, lambdaTimeout = (context, timeout) => setTimeout(() => {
|
|
20
|
-
Math.round(context.getRemainingTimeInMillis() / 1e3) === 0 && context.fail(new Error(`Task timed out after ${timeout}.00 seconds`));
|
|
21
|
-
}, timeout * 1e3), globalOptions = {}, setGlobalOptions = (options) => (Object.assign(globalOptions, options), globalOptions), resetGlobalOptions = () => {
|
|
22
|
-
globalOptions = {};
|
|
23
|
-
}, getStrictKeys = (object) => Object.keys(object), removeUndefinedValues = (options) => {
|
|
24
|
-
if (!options)
|
|
25
|
-
return {};
|
|
26
|
-
for (let key of getStrictKeys(options))
|
|
27
|
-
options[key] === void 0 && delete options[key];
|
|
28
|
-
return options;
|
|
29
|
-
}, mockContext = (options, overrides) => {
|
|
30
|
-
let requestId = crypto.randomUUID(), logStream = requestId.replace(/-/g, ""), opts = Object.assign(
|
|
31
|
-
{
|
|
32
|
-
region: "us-west-1",
|
|
33
|
-
account: "123456789012",
|
|
34
|
-
functionName: "aws-lambda-mock-context",
|
|
35
|
-
functionVersion: "$LATEST",
|
|
36
|
-
memoryLimitInMB: "128",
|
|
37
|
-
timeout: 3,
|
|
38
|
-
handleTimeout: false
|
|
39
|
-
},
|
|
40
|
-
removeUndefinedValues(globalOptions),
|
|
41
|
-
removeUndefinedValues(options)
|
|
42
|
-
), 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 = {
|
|
43
|
-
callbackWaitsForEmptyEventLoop: true,
|
|
44
|
-
functionName: opts.functionName,
|
|
45
|
-
functionVersion: opts.functionVersion,
|
|
46
|
-
invokedFunctionArn: `arn:aws:lambda:${opts.region}:${opts.account}:function:${opts.functionName}:${opts.alias || opts.functionVersion}`,
|
|
47
|
-
memoryLimitInMB: opts.memoryLimitInMB,
|
|
48
|
-
awsRequestId: requestId,
|
|
49
|
-
logGroupName: `/aws/lambda/${opts.functionName}`,
|
|
50
|
-
logStreamName: `${logDate}/[${opts.functionVersion}]/${logStream}`,
|
|
51
|
-
identity: opts.identity,
|
|
52
|
-
clientContext: opts.clientContext,
|
|
53
|
-
getRemainingTimeInMillis: () => {
|
|
54
|
-
let endTime = end || Date.now(), remainingTime = opts.timeout * 1e3 - (endTime - start);
|
|
55
|
-
return Math.max(0, remainingTime);
|
|
56
|
-
},
|
|
57
|
-
succeed: (messageOrObject) => {
|
|
58
|
-
end = Date.now(), deferred.resolve(messageOrObject);
|
|
59
|
-
},
|
|
60
|
-
fail: (error) => {
|
|
61
|
-
end = Date.now(), deferred.reject(typeof error == "string" ? new Error(error) : error);
|
|
62
|
-
},
|
|
63
|
-
done: (error, result) => {
|
|
64
|
-
if (timeout && clearTimeout(timeout), error) {
|
|
65
|
-
context.fail(error);
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
context.succeed(result);
|
|
69
|
-
},
|
|
70
|
-
Promise: deferred.promise
|
|
71
|
-
};
|
|
72
|
-
return opts.handleTimeout && opts.timeout > 0 && (timeout = lambdaTimeout(context, opts.timeout)), Object.assign(context, overrides);
|
|
73
|
-
};
|
|
74
|
-
var index_default = mockContext;
|
|
75
|
-
|
|
76
|
-
exports.default = index_default;
|
|
77
|
-
exports.resetGlobalOptions = resetGlobalOptions;
|
|
78
|
-
exports.setGlobalOptions = setGlobalOptions;
|
|
79
|
-
//# sourceMappingURL=index.cjs.map
|
|
80
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/cjs/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"names":["randomUUID"],"mappings":";;;;;;;AA0BA,IAAM,QAAQ,MAAgB;AAC5B,EAAA,IAAM,WAA8B,EAAC;AAKrC,EAAO,OAAA;AAAA,IACL,OALc,EAAA,IAAI,OAAQ,CAAA,CAA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'\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 handleTimeout?: boolean\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}\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\nlet globalOptions: Options = {}\n\nconst setGlobalOptions = (options: Options): Options => {\n Object.assign(globalOptions, options)\n return globalOptions\n}\n\nconst resetGlobalOptions = () => {\n globalOptions = {}\n}\n\nconst getStrictKeys = <T extends Record<string, any>>(object: T): (keyof T)[] => Object.keys(object)\n\nconst removeUndefinedValues = (options?: Options): Options => {\n if (!options) {\n return {}\n }\n for (const key of getStrictKeys(options)) {\n options[key] === undefined && delete options[key]\n }\n return options\n}\n\nconst mockContext = <E = Record<string, any>>(options?: Options, overrides?: E): MockContext & E => {\n const requestId = randomUUID()\n const logStream = requestId.replace(/-/g, '')\n\n const opts = Object.assign(\n {\n region: 'us-west-1',\n account: '123456789012',\n functionName: 'aws-lambda-mock-context',\n functionVersion: '$LATEST',\n memoryLimitInMB: '128',\n timeout: 3,\n handleTimeout: false,\n },\n removeUndefinedValues(globalOptions),\n removeUndefinedValues(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: requestId,\n logGroupName: `/aws/lambda/${opts.functionName}`,\n logStreamName: `${logDate}/[${opts.functionVersion}]/${logStream}`,\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.handleTimeout && opts.timeout > 0) {\n timeout = lambdaTimeout(context, opts.timeout)\n }\n\n return Object.assign(context, overrides)\n}\n\nexport { setGlobalOptions, resetGlobalOptions }\nexport default mockContext\n"]}
|
package/dist/cjs/index.d.cts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
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
|
-
handleTimeout?: boolean;
|
|
14
|
-
};
|
|
15
|
-
interface MockContext extends Context {
|
|
16
|
-
Promise: Promise<any>;
|
|
17
|
-
}
|
|
18
|
-
declare const setGlobalOptions: (options: Options) => Options;
|
|
19
|
-
declare const resetGlobalOptions: () => void;
|
|
20
|
-
declare const mockContext: <E = Record<string, any>>(options?: Options, overrides?: E) => MockContext & E;
|
|
21
|
-
|
|
22
|
-
export { mockContext as default, resetGlobalOptions, setGlobalOptions };
|
package/dist/index.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
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
|
-
handleTimeout?: boolean;
|
|
14
|
-
};
|
|
15
|
-
interface MockContext extends Context {
|
|
16
|
-
Promise: Promise<any>;
|
|
17
|
-
}
|
|
18
|
-
declare const setGlobalOptions: (options: Options) => Options;
|
|
19
|
-
declare const resetGlobalOptions: () => void;
|
|
20
|
-
declare const mockContext: <E = Record<string, any>>(options?: Options, overrides?: E) => MockContext & E;
|
|
21
|
-
|
|
22
|
-
export { mockContext as default, resetGlobalOptions, setGlobalOptions };
|