@gahojin-inc/aws-lambda-mock-context 2025.11.0 → 2025.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/index.cjs CHANGED
@@ -1,29 +1,5 @@
1
1
  Object.defineProperty(exports, '__esModule', { value: true });
2
- //#region rolldown:runtime
3
- var __create = Object.create;
4
- var __defProp = Object.defineProperty;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getProtoOf = Object.getPrototypeOf;
8
- var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
- key = keys[i];
12
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
- get: ((k) => from[k]).bind(null, key),
14
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
- });
16
- }
17
- return to;
18
- };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
20
- value: mod,
21
- enumerable: true
22
- }) : target, mod));
23
-
24
- //#endregion
25
2
  let node_crypto = require("node:crypto");
26
- node_crypto = __toESM(node_crypto);
27
3
 
28
4
  //#region src/index.ts
29
5
  const defer = () => {
@@ -92,6 +68,7 @@ const mockContext = (options, overrides) => {
92
68
  logStreamName: `${logDate}/[${opts.functionVersion}]/${logStream}`,
93
69
  identity: opts.identity,
94
70
  clientContext: opts.clientContext,
71
+ tenantId: opts.tenantId,
95
72
  getRemainingTimeInMillis: () => {
96
73
  const endTime = end || Date.now();
97
74
  const remainingTime = opts.timeout * 1e3 - (endTime - start);
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["deferred: Partial<Deferred>","globalOptions: Options","end: number | null","timeout: NodeJS.Timeout | null","context: MockContext"],"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 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 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,cAAwB;CAC5B,MAAMA,WAA8B,EAAE;CACtC,MAAM,UAAU,IAAI,SAAS,SAAS,WAAW;AAC/C,WAAS,UAAU;AACnB,WAAS,SAAS;GAClB;AACF,QAAO,SAAS,QAAQ;AACxB,QAAO,SAAS,OA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B,oBAAoB,kBAAkB,KAAK,OAAO,GAAG,KAAK,QAAQ,YAAY,KAAK,aAAa,GAAG,KAAK,SAAS,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,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;;AAI1C,kBAAe"}
1
+ {"version":3,"file":"index.cjs","names":["deferred: Partial<Deferred>","globalOptions: Options","end: number | null","timeout: NodeJS.Timeout | null","context: MockContext"],"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,MAAMA,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,IAAIC,gBAAyB,EAAE;AAE/B,MAAM,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;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;;AAI1C,kBAAe"}
package/dist/index.d.cts CHANGED
@@ -6,8 +6,9 @@ type Options = {
6
6
  functionName?: string;
7
7
  functionVersion?: string;
8
8
  memoryLimitInMB?: string;
9
- identity?: CognitoIdentity;
10
- clientContext?: ClientContext;
9
+ identity?: CognitoIdentity | undefined;
10
+ clientContext?: ClientContext | undefined;
11
+ tenantId?: string | undefined;
11
12
  timeout?: number;
12
13
  handleTimeout?: boolean;
13
14
  };
package/dist/index.d.mts CHANGED
@@ -6,8 +6,9 @@ type Options = {
6
6
  functionName?: string;
7
7
  functionVersion?: string;
8
8
  memoryLimitInMB?: string;
9
- identity?: CognitoIdentity;
10
- clientContext?: ClientContext;
9
+ identity?: CognitoIdentity | undefined;
10
+ clientContext?: ClientContext | undefined;
11
+ tenantId?: string | undefined;
11
12
  timeout?: number;
12
13
  handleTimeout?: boolean;
13
14
  };
package/dist/index.mjs CHANGED
@@ -67,6 +67,7 @@ const mockContext = (options, overrides) => {
67
67
  logStreamName: `${logDate}/[${opts.functionVersion}]/${logStream}`,
68
68
  identity: opts.identity,
69
69
  clientContext: opts.clientContext,
70
+ tenantId: opts.tenantId,
70
71
  getRemainingTimeInMillis: () => {
71
72
  const endTime = end || Date.now();
72
73
  const remainingTime = opts.timeout * 1e3 - (endTime - start);
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":["deferred: Partial<Deferred>","globalOptions: Options","end: number | null","timeout: NodeJS.Timeout | null","context: MockContext"],"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 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 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,cAAwB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AK,IAAI;CAC1G,MAAM,QAAQ,EAAE,SAAS;CACzB,IAAIC,MAAqB;CACzB,IAAIC,UAAiC;CAGrC,MAAMC,UAAuB;EAC3B,gCAAgC;EAChC,cAAc,KAAK;EACnB,iBAAiB,KAAK;EACtB,oBAAoB,kBAAkB,KAAK,OAAO,GAAG,KAAK,QAAQ,YAAY,KAAK,aAAa,GAAG,KAAK,SAAS,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,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;;AAI1C,kBAAe"}
1
+ {"version":3,"file":"index.mjs","names":["deferred: Partial<Deferred>","globalOptions: Options","end: number | null","timeout: NodeJS.Timeout | null","context: MockContext"],"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,MAAMA,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,MA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AAe,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;;AAI1C,kBAAe"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gahojin-inc/aws-lambda-mock-context",
3
- "version": "2025.11.0",
3
+ "version": "2025.11.2",
4
4
  "description": "AWS Lambda mock context object",
5
5
  "author": "GAHOJIN, Inc.",
6
6
  "license": "Apache-2.0",
@@ -40,29 +40,27 @@
40
40
  },
41
41
  "devDependencies": {
42
42
  "@arethetypeswrong/cli": "0.18.2",
43
- "@biomejs/biome": "2.3.2",
43
+ "@biomejs/biome": "2.3.6",
44
44
  "@commitlint/cli": "20.1.0",
45
45
  "@commitlint/config-conventional": "20.0.0",
46
- "@types/aws-lambda": "8.10.157",
47
- "@types/node": "24.9.2",
48
- "@vitest/coverage-v8": "4.0.5",
49
- "jest-extended": "6.0.0",
50
- "lefthook": "2.0.2",
46
+ "@types/aws-lambda": "8.10.159",
47
+ "@types/node": "24.10.1",
48
+ "@typescript/native-preview": "7.0.0-dev.20251119.1",
49
+ "@vitest/coverage-v8": "4.0.10",
50
+ "jest-extended": "7.0.0",
51
+ "lefthook": "2.0.4",
51
52
  "npm-check-updates": "19.1.2",
52
- "rimraf": "6.0.1",
53
- "rolldown": "1.0.0-beta.45",
53
+ "rolldown": "1.0.0-beta.51",
54
54
  "typescript": "5.9.3",
55
- "unplugin-isolated-decl": "0.15.3",
56
- "vite": "7.1.12",
55
+ "unplugin-isolated-decl": "0.15.6",
56
+ "vite": "7.2.2",
57
57
  "vite-tsconfig-paths": "5.1.4",
58
- "vitest": "4.0.5"
58
+ "vitest": "4.0.10"
59
59
  },
60
60
  "scripts": {
61
- "clean": "rimraf dist",
62
- "prebuild": "rimraf dist",
63
61
  "build": "rolldown -c",
64
62
  "lint": "biome check --write .",
65
- "check": "tsc",
63
+ "check": "tsgo",
66
64
  "check:packagejson": "attw --pack .",
67
65
  "test": "vitest --watch",
68
66
  "test:unit": "vitest --passWithNoTests --run --coverage",