@gahojin-inc/aws-lambda-mock-context 2026.2.0 → 2026.4.0
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.d.ts +25 -0
- package/dist/{index.mjs → index.js} +2 -3
- package/dist/index.js.map +1 -0
- package/package.json +17 -23
- package/dist/index.cjs +0 -103
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -22
- package/dist/index.d.mts +0 -22
- package/dist/index.mjs.map +0 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ClientContext, CognitoIdentity, Context } from "aws-lambda";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
type Options = {
|
|
5
|
+
region?: string;
|
|
6
|
+
account?: string;
|
|
7
|
+
alias?: string;
|
|
8
|
+
functionName?: string;
|
|
9
|
+
functionVersion?: string;
|
|
10
|
+
memoryLimitInMB?: string;
|
|
11
|
+
identity?: CognitoIdentity | undefined;
|
|
12
|
+
clientContext?: ClientContext | undefined;
|
|
13
|
+
tenantId?: string | undefined;
|
|
14
|
+
timeout?: number;
|
|
15
|
+
handleTimeout?: boolean;
|
|
16
|
+
};
|
|
17
|
+
interface MockContext extends Context {
|
|
18
|
+
Promise: Promise<any>;
|
|
19
|
+
}
|
|
20
|
+
declare const setGlobalOptions: (options: Options) => Options;
|
|
21
|
+
declare const resetGlobalOptions: () => void;
|
|
22
|
+
declare const mockContext: <E = Record<string, any>>(options?: Options, overrides?: E) => MockContext & E;
|
|
23
|
+
//#endregion
|
|
24
|
+
export { mockContext as default, resetGlobalOptions, setGlobalOptions };
|
|
25
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
|
|
3
2
|
//#region src/index.ts
|
|
4
3
|
const defer = () => {
|
|
5
4
|
const deferred = {};
|
|
@@ -94,7 +93,7 @@ const mockContext = (options, overrides) => {
|
|
|
94
93
|
if (opts.handleTimeout && opts.timeout > 0) timeout = lambdaTimeout(context, opts.timeout);
|
|
95
94
|
return Object.assign(context, overrides);
|
|
96
95
|
};
|
|
97
|
-
|
|
98
96
|
//#endregion
|
|
99
97
|
export { mockContext as default, resetGlobalOptions, setGlobalOptions };
|
|
100
|
-
|
|
98
|
+
|
|
99
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"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 | undefined\n clientContext?: ClientContext | undefined\n tenantId?: string | undefined\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 assert(deferred.resolve)\n assert(deferred.reject)\n return {\n promise,\n resolve: deferred.resolve,\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 tenantId: opts.tenantId,\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 { resetGlobalOptions, setGlobalOptions }\nexport default mockContext\n"],"mappings":";;AA2BA,MAAM,cAAwB;CAC5B,MAAM,WAA8B,EAAE;CACtC,MAAM,UAAU,IAAI,SAAS,SAAS,WAAW;AAC/C,WAAS,UAAU;AACnB,WAAS,SAAS;GAClB;AACF,QAAO,SAAS,QAAQ;AACxB,QAAO,SAAS,OAAO;AACvB,QAAO;EACL;EACA,SAAS,SAAS;EAClB,QAAQ,SAAS;EAClB;;AAGH,MAAM,iBAAiB,SAAsB,YAAoC;AAC/E,QAAO,iBAAiB;AACtB,MAAI,KAAK,MAAM,QAAQ,0BAA0B,GAAG,IAAK,KAAK,EAC5D,SAAQ,qBAAK,IAAI,MAAM,wBAAwB,QAAQ,aAAa,CAAC;IAEtE,UAAU,IAAK;;AAGpB,IAAI,gBAAyB,EAAE;AAE/B,MAAM,oBAAoB,YAA8B;AACtD,QAAO,OAAO,eAAe,QAAQ;AACrC,QAAO;;AAGT,MAAM,2BAAiC;AACrC,iBAAgB,EAAE;;AAGpB,MAAM,iBAAgD,WAA2B,OAAO,KAAK,OAAO;AAEpG,MAAM,yBAAyB,YAA+B;AAC5D,KAAI,CAAC,QACH,QAAO,EAAE;AAEX,MAAK,MAAM,OAAO,cAAc,QAAQ,CACtC,SAAQ,SAAS,KAAA,KAAa,OAAO,QAAQ;AAE/C,QAAO;;AAGT,MAAM,eAAwC,SAAmB,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;EAChB,EACD,sBAAsB,cAAc,EACpC,sBAAsB,QAAQ,CAC/B;CAED,MAAM,WAAW,OAAO;CAExB,MAAM,oBAAI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": "@gahojin-inc/aws-lambda-mock-context",
|
|
3
|
-
"version": "2026.
|
|
3
|
+
"version": "2026.4.0",
|
|
4
4
|
"description": "AWS Lambda mock context object",
|
|
5
5
|
"author": "GAHOJIN, Inc.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -22,45 +22,39 @@
|
|
|
22
22
|
"access": "public",
|
|
23
23
|
"provenance": true
|
|
24
24
|
},
|
|
25
|
-
"main": "dist/index.
|
|
26
|
-
"module": "dist/index.
|
|
27
|
-
"types": "dist/index.d.
|
|
25
|
+
"main": "dist/index.js",
|
|
26
|
+
"module": "dist/index.js",
|
|
27
|
+
"types": "dist/index.d.ts",
|
|
28
28
|
"exports": {
|
|
29
29
|
"./package.json": "./package.json",
|
|
30
30
|
".": {
|
|
31
31
|
"import": {
|
|
32
|
-
"types": "./dist/index.d.
|
|
33
|
-
"default": "./dist/index.
|
|
34
|
-
},
|
|
35
|
-
"require": {
|
|
36
|
-
"types": "./dist/index.d.cts",
|
|
37
|
-
"default": "./dist/index.cjs"
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"default": "./dist/index.js"
|
|
38
34
|
}
|
|
39
35
|
}
|
|
40
36
|
},
|
|
41
37
|
"devDependencies": {
|
|
42
38
|
"@arethetypeswrong/cli": "0.18.2",
|
|
43
|
-
"@biomejs/biome": "2.
|
|
44
|
-
"@types/aws-lambda": "8.10.
|
|
45
|
-
"@types/node": "24.
|
|
46
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
47
|
-
"@vitest/coverage-v8": "4.
|
|
39
|
+
"@biomejs/biome": "2.4.9",
|
|
40
|
+
"@types/aws-lambda": "8.10.161",
|
|
41
|
+
"@types/node": "24.12.0",
|
|
42
|
+
"@typescript/native-preview": "7.0.0-dev.20260324.1",
|
|
43
|
+
"@vitest/coverage-v8": "4.1.2",
|
|
48
44
|
"jest-extended": "7.0.0",
|
|
49
|
-
"
|
|
50
|
-
"rolldown": "
|
|
51
|
-
"
|
|
52
|
-
"unplugin-isolated-decl": "0.15.7",
|
|
53
|
-
"vite": "7.3.1",
|
|
45
|
+
"rolldown": "1.0.0-rc.12",
|
|
46
|
+
"rolldown-plugin-dts": "0.23.2",
|
|
47
|
+
"vite": "8.0.3",
|
|
54
48
|
"vite-tsconfig-paths": "6.1.1",
|
|
55
|
-
"vitest": "4.
|
|
49
|
+
"vitest": "4.1.2"
|
|
56
50
|
},
|
|
57
51
|
"scripts": {
|
|
58
52
|
"build": "rolldown -c",
|
|
59
53
|
"lint": "biome check --write .",
|
|
60
54
|
"check": "tsgo",
|
|
61
|
-
"check:packagejson": "attw --pack .",
|
|
55
|
+
"check:packagejson": "attw --pack --profile esm-only .",
|
|
62
56
|
"test": "vitest --watch",
|
|
63
57
|
"test:unit": "vitest --passWithNoTests --run --coverage",
|
|
64
|
-
"ncu": "
|
|
58
|
+
"ncu": "mise run ncu"
|
|
65
59
|
}
|
|
66
60
|
}
|
package/dist/index.cjs
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
2
|
-
let node_crypto = require("node:crypto");
|
|
3
|
-
|
|
4
|
-
//#region src/index.ts
|
|
5
|
-
const defer = () => {
|
|
6
|
-
const deferred = {};
|
|
7
|
-
const promise = new Promise((resolve, reject) => {
|
|
8
|
-
deferred.resolve = resolve;
|
|
9
|
-
deferred.reject = reject;
|
|
10
|
-
});
|
|
11
|
-
assert(deferred.resolve);
|
|
12
|
-
assert(deferred.reject);
|
|
13
|
-
return {
|
|
14
|
-
promise,
|
|
15
|
-
resolve: deferred.resolve,
|
|
16
|
-
reject: deferred.reject
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
const lambdaTimeout = (context, timeout) => {
|
|
20
|
-
return setTimeout(() => {
|
|
21
|
-
if (Math.round(context.getRemainingTimeInMillis() / 1e3) === 0) context.fail(/* @__PURE__ */ new Error(`Task timed out after ${timeout}.00 seconds`));
|
|
22
|
-
}, timeout * 1e3);
|
|
23
|
-
};
|
|
24
|
-
let globalOptions = {};
|
|
25
|
-
const setGlobalOptions = (options) => {
|
|
26
|
-
Object.assign(globalOptions, options);
|
|
27
|
-
return globalOptions;
|
|
28
|
-
};
|
|
29
|
-
const resetGlobalOptions = () => {
|
|
30
|
-
globalOptions = {};
|
|
31
|
-
};
|
|
32
|
-
const getStrictKeys = (object) => Object.keys(object);
|
|
33
|
-
const removeUndefinedValues = (options) => {
|
|
34
|
-
if (!options) return {};
|
|
35
|
-
for (const key of getStrictKeys(options)) options[key] === void 0 && delete options[key];
|
|
36
|
-
return options;
|
|
37
|
-
};
|
|
38
|
-
const mockContext = (options, overrides) => {
|
|
39
|
-
const requestId = (0, node_crypto.randomUUID)();
|
|
40
|
-
const logStream = requestId.replace(/-/g, "");
|
|
41
|
-
const opts = Object.assign({
|
|
42
|
-
region: "us-west-1",
|
|
43
|
-
account: "123456789012",
|
|
44
|
-
functionName: "aws-lambda-mock-context",
|
|
45
|
-
functionVersion: "$LATEST",
|
|
46
|
-
memoryLimitInMB: "128",
|
|
47
|
-
timeout: 3,
|
|
48
|
-
handleTimeout: false
|
|
49
|
-
}, removeUndefinedValues(globalOptions), removeUndefinedValues(options));
|
|
50
|
-
const deferred = defer();
|
|
51
|
-
const d = /* @__PURE__ */ new Date();
|
|
52
|
-
const logDate = [
|
|
53
|
-
d.getFullYear(),
|
|
54
|
-
`0${d.getMonth() + 1}`.slice(-2),
|
|
55
|
-
`0${d.getDate()}`.slice(-2)
|
|
56
|
-
].join("/");
|
|
57
|
-
const start = d.getTime();
|
|
58
|
-
let end = null;
|
|
59
|
-
let timeout = null;
|
|
60
|
-
const context = {
|
|
61
|
-
callbackWaitsForEmptyEventLoop: true,
|
|
62
|
-
functionName: opts.functionName,
|
|
63
|
-
functionVersion: opts.functionVersion,
|
|
64
|
-
invokedFunctionArn: `arn:aws:lambda:${opts.region}:${opts.account}:function:${opts.functionName}:${opts.alias || opts.functionVersion}`,
|
|
65
|
-
memoryLimitInMB: opts.memoryLimitInMB,
|
|
66
|
-
awsRequestId: requestId,
|
|
67
|
-
logGroupName: `/aws/lambda/${opts.functionName}`,
|
|
68
|
-
logStreamName: `${logDate}/[${opts.functionVersion}]/${logStream}`,
|
|
69
|
-
identity: opts.identity,
|
|
70
|
-
clientContext: opts.clientContext,
|
|
71
|
-
tenantId: opts.tenantId,
|
|
72
|
-
getRemainingTimeInMillis: () => {
|
|
73
|
-
const endTime = end || Date.now();
|
|
74
|
-
const remainingTime = opts.timeout * 1e3 - (endTime - start);
|
|
75
|
-
return Math.max(0, remainingTime);
|
|
76
|
-
},
|
|
77
|
-
succeed: (messageOrObject) => {
|
|
78
|
-
end = Date.now();
|
|
79
|
-
deferred.resolve(messageOrObject);
|
|
80
|
-
},
|
|
81
|
-
fail: (error) => {
|
|
82
|
-
end = Date.now();
|
|
83
|
-
deferred.reject(typeof error === "string" ? new Error(error) : error);
|
|
84
|
-
},
|
|
85
|
-
done: (error, result) => {
|
|
86
|
-
if (timeout) clearTimeout(timeout);
|
|
87
|
-
if (error) {
|
|
88
|
-
context.fail(error);
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
context.succeed(result);
|
|
92
|
-
},
|
|
93
|
-
Promise: deferred.promise
|
|
94
|
-
};
|
|
95
|
-
if (opts.handleTimeout && opts.timeout > 0) timeout = lambdaTimeout(context, opts.timeout);
|
|
96
|
-
return Object.assign(context, overrides);
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
//#endregion
|
|
100
|
-
exports.default = mockContext;
|
|
101
|
-
exports.resetGlobalOptions = resetGlobalOptions;
|
|
102
|
-
exports.setGlobalOptions = setGlobalOptions;
|
|
103
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":[],"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 | undefined\n clientContext?: ClientContext | undefined\n tenantId?: string | undefined\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 assert(deferred.resolve)\n assert(deferred.reject)\n return {\n promise,\n resolve: deferred.resolve,\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 tenantId: opts.tenantId,\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":";;;;AA2BA,MAAM,cAAwB;CAC5B,MAAM,WAA8B,EAAE;CACtC,MAAM,UAAU,IAAI,SAAS,SAAS,WAAW;AAC/C,WAAS,UAAU;AACnB,WAAS,SAAS;GAClB;AACF,QAAO,SAAS,QAAQ;AACxB,QAAO,SAAS,OAAO;AACvB,QAAO;EACL;EACA,SAAS,SAAS;EAClB,QAAQ,SAAS;EAClB;;AAGH,MAAM,iBAA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,KAAK;EACtH,iBAAiB,KAAK;EACtB,cAAc;EACd,cAAc,eAAe,KAAK;EAClC,eAAe,GAAG,QAAQ,IAAI,KAAK,gBAAgB,IAAI;EACvD,UAAU,KAAK;EACf,eAAe,KAAK;EACpB,UAAU,KAAK;EACf,gCAAgC;GAC9B,MAAM,UAAU,OAAO,KAAK,KAAK;GACjC,MAAM,gBAAgB,KAAK,UAAU,OAAQ,UAAU;AAEvD,UAAO,KAAK,IAAI,GAAG,cAAc;;EAEnC,UAAU,oBAAoB;AAC5B,SAAM,KAAK,KAAK;AAEhB,YAAS,QAAQ,gBAAgB;;EAEnC,OAAO,UAAU;AACf,SAAM,KAAK,KAAK;AAEhB,YAAS,OAAO,OAAO,UAAU,WAAW,IAAI,MAAM,MAAM,GAAG,MAAM;;EAEvE,OAAO,OAAO,WAAW;AACvB,OAAI,QACF,cAAa,QAAQ;AAGvB,OAAI,OAAO;AACT,YAAQ,KAAK,MAAM;AACnB;;AAGF,WAAQ,QAAQ,OAAO;;EAEzB,SAAS,SAAS;EACnB;AAGD,KAAI,KAAK,iBAAiB,KAAK,UAAU,EACvC,WAAU,cAAc,SAAS,KAAK,QAAQ;AAGhD,QAAO,OAAO,OAAO,SAAS,UAAU"}
|
package/dist/index.d.cts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
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 | undefined;
|
|
10
|
-
clientContext?: ClientContext | undefined;
|
|
11
|
-
tenantId?: string | undefined;
|
|
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
|
-
export { setGlobalOptions, resetGlobalOptions };
|
|
22
|
-
export default mockContext;
|
package/dist/index.d.mts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
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 | undefined;
|
|
10
|
-
clientContext?: ClientContext | undefined;
|
|
11
|
-
tenantId?: string | undefined;
|
|
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
|
-
export { setGlobalOptions, resetGlobalOptions };
|
|
22
|
-
export default mockContext;
|
package/dist/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"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 | undefined\n clientContext?: ClientContext | undefined\n tenantId?: string | undefined\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 assert(deferred.resolve)\n assert(deferred.reject)\n return {\n promise,\n resolve: deferred.resolve,\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 tenantId: opts.tenantId,\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":";;;AA2BA,MAAM,cAAwB;CAC5B,MAAM,WAA8B,EAAE;CACtC,MAAM,UAAU,IAAI,SAAS,SAAS,WAAW;AAC/C,WAAS,UAAU;AACnB,WAAS,SAAS;GAClB;AACF,QAAO,SAAS,QAAQ;AACxB,QAAO,SAAS,OAAO;AACvB,QAAO;EACL;EACA,SAAS,SAAS;EAClB,QAAQ,SAAS;EAClB;;AAGH,MAAM,iBAAiB,SAAsB,YAAoC;AAC/E,QAAO,iBAAiB;AACtB,MAAI,KAAK,MAAM,QAAQ,0BAA0B,GAAG,IAAK,KAAK,EAC5D,SAAQ,qBAAK,IAAI,MAAM,wBAAwB,QAAQ,aAAa,CAAC;IAEtE,UAAU,IAAK;;AAGpB,IAAI,gBAAyB,EAAE;AAE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