@aws/lambda-invoke-store
Advanced tools
| 'use strict'; | ||
| var async_hooks = require('async_hooks'); | ||
| const noGlobalAwsLambda = process.env["AWS_LAMBDA_NODEJS_NO_GLOBAL_AWSLAMBDA"] === "1" || | ||
| process.env["AWS_LAMBDA_NODEJS_NO_GLOBAL_AWSLAMBDA"] === "true"; | ||
| if (!noGlobalAwsLambda) { | ||
| globalThis.awslambda = globalThis.awslambda || {}; | ||
| } | ||
| const PROTECTED_KEYS = { | ||
| REQUEST_ID: Symbol("_AWS_LAMBDA_REQUEST_ID"), | ||
| X_RAY_TRACE_ID: Symbol("_AWS_LAMBDA_X_RAY_TRACE_ID"), | ||
| TENANT_ID: Symbol("_AWS_LAMBDA_TENANT_ID"), | ||
| }; | ||
| class InvokeStoreImpl { | ||
| static storage = new async_hooks.AsyncLocalStorage(); | ||
| static PROTECTED_KEYS = PROTECTED_KEYS; | ||
| static run(context, fn) { | ||
| return this.storage.run({ ...context }, fn); | ||
| } | ||
| static getContext() { | ||
| return this.storage.getStore(); | ||
| } | ||
| static get(key) { | ||
| const context = this.storage.getStore(); | ||
| return context?.[key]; | ||
| } | ||
| static set(key, value) { | ||
| if (this.isProtectedKey(key)) { | ||
| throw new Error(`Cannot modify protected Lambda context field`); | ||
| } | ||
| const context = this.storage.getStore(); | ||
| if (context) { | ||
| context[key] = value; | ||
| } | ||
| } | ||
| static getRequestId() { | ||
| return this.get(this.PROTECTED_KEYS.REQUEST_ID) ?? "-"; | ||
| } | ||
| static getXRayTraceId() { | ||
| return this.get(this.PROTECTED_KEYS.X_RAY_TRACE_ID); | ||
| } | ||
| static getTenantId() { | ||
| return this.get(this.PROTECTED_KEYS.TENANT_ID); | ||
| } | ||
| static hasContext() { | ||
| return this.storage.getStore() !== undefined; | ||
| } | ||
| static isProtectedKey(key) { | ||
| return (key === this.PROTECTED_KEYS.REQUEST_ID || | ||
| key === this.PROTECTED_KEYS.X_RAY_TRACE_ID); | ||
| } | ||
| } | ||
| let instance; | ||
| if (!noGlobalAwsLambda && globalThis.awslambda?.InvokeStore) { | ||
| instance = globalThis.awslambda.InvokeStore; | ||
| } | ||
| else { | ||
| instance = InvokeStoreImpl; | ||
| if (!noGlobalAwsLambda && globalThis.awslambda) { | ||
| globalThis.awslambda.InvokeStore = instance; | ||
| } | ||
| } | ||
| const InvokeStore = instance; | ||
| exports.InvokeStore = InvokeStore; |
| import { AsyncLocalStorage } from 'async_hooks'; | ||
| const noGlobalAwsLambda = process.env["AWS_LAMBDA_NODEJS_NO_GLOBAL_AWSLAMBDA"] === "1" || | ||
| process.env["AWS_LAMBDA_NODEJS_NO_GLOBAL_AWSLAMBDA"] === "true"; | ||
| if (!noGlobalAwsLambda) { | ||
| globalThis.awslambda = globalThis.awslambda || {}; | ||
| } | ||
| const PROTECTED_KEYS = { | ||
| REQUEST_ID: Symbol("_AWS_LAMBDA_REQUEST_ID"), | ||
| X_RAY_TRACE_ID: Symbol("_AWS_LAMBDA_X_RAY_TRACE_ID"), | ||
| TENANT_ID: Symbol("_AWS_LAMBDA_TENANT_ID"), | ||
| }; | ||
| class InvokeStoreImpl { | ||
| static storage = new AsyncLocalStorage(); | ||
| static PROTECTED_KEYS = PROTECTED_KEYS; | ||
| static run(context, fn) { | ||
| return this.storage.run({ ...context }, fn); | ||
| } | ||
| static getContext() { | ||
| return this.storage.getStore(); | ||
| } | ||
| static get(key) { | ||
| const context = this.storage.getStore(); | ||
| return context?.[key]; | ||
| } | ||
| static set(key, value) { | ||
| if (this.isProtectedKey(key)) { | ||
| throw new Error(`Cannot modify protected Lambda context field`); | ||
| } | ||
| const context = this.storage.getStore(); | ||
| if (context) { | ||
| context[key] = value; | ||
| } | ||
| } | ||
| static getRequestId() { | ||
| return this.get(this.PROTECTED_KEYS.REQUEST_ID) ?? "-"; | ||
| } | ||
| static getXRayTraceId() { | ||
| return this.get(this.PROTECTED_KEYS.X_RAY_TRACE_ID); | ||
| } | ||
| static getTenantId() { | ||
| return this.get(this.PROTECTED_KEYS.TENANT_ID); | ||
| } | ||
| static hasContext() { | ||
| return this.storage.getStore() !== undefined; | ||
| } | ||
| static isProtectedKey(key) { | ||
| return (key === this.PROTECTED_KEYS.REQUEST_ID || | ||
| key === this.PROTECTED_KEYS.X_RAY_TRACE_ID); | ||
| } | ||
| } | ||
| let instance; | ||
| if (!noGlobalAwsLambda && globalThis.awslambda?.InvokeStore) { | ||
| instance = globalThis.awslambda.InvokeStore; | ||
| } | ||
| else { | ||
| instance = InvokeStoreImpl; | ||
| if (!noGlobalAwsLambda && globalThis.awslambda) { | ||
| globalThis.awslambda.InvokeStore = instance; | ||
| } | ||
| } | ||
| const InvokeStore = instance; | ||
| export { InvokeStore }; |
| /** | ||
| * Generic store context that uses protected keys for Lambda fields | ||
| * and allows custom user properties | ||
| */ | ||
| export interface InvokeStoreContext { | ||
| [key: string | symbol]: unknown; | ||
| } | ||
| /** | ||
| * InvokeStore implementation class | ||
| */ | ||
| declare class InvokeStoreImpl { | ||
| private static storage; | ||
| static readonly PROTECTED_KEYS: { | ||
| readonly REQUEST_ID: symbol; | ||
| readonly X_RAY_TRACE_ID: symbol; | ||
| readonly TENANT_ID: symbol; | ||
| }; | ||
| /** | ||
| * Initialize and run code within an invoke context | ||
| */ | ||
| static run<T>(context: InvokeStoreContext, fn: () => T | Promise<T>): T | Promise<T>; | ||
| /** | ||
| * Get the complete current context | ||
| */ | ||
| static getContext(): InvokeStoreContext | undefined; | ||
| /** | ||
| * Get a specific value from the context by key | ||
| */ | ||
| static get<T = unknown>(key: string | symbol): T | undefined; | ||
| /** | ||
| * Set a custom value in the current context | ||
| * Protected Lambda context fields cannot be overwritten | ||
| */ | ||
| static set(key: string | symbol, value: unknown): void; | ||
| /** | ||
| * Get the current request ID | ||
| */ | ||
| static getRequestId(): string; | ||
| /** | ||
| * Get the current X-ray trace ID | ||
| */ | ||
| static getXRayTraceId(): string | undefined; | ||
| /** | ||
| * Get the current tenant ID | ||
| */ | ||
| static getTenantId(): string | undefined; | ||
| /** | ||
| * Check if we're currently within an invoke context | ||
| */ | ||
| static hasContext(): boolean; | ||
| /** | ||
| * Check if a key is protected (readonly Lambda context field) | ||
| */ | ||
| private static isProtectedKey; | ||
| } | ||
| export declare const InvokeStore: typeof InvokeStoreImpl; | ||
| export {}; |
+24
-8
| { | ||
| "name": "@aws/lambda-invoke-store", | ||
| "version": "0.1.0", | ||
| "version": "0.1.1", | ||
| "description": "Invoke scoped data storage for AWS Lambda Node.js Runtime Environment", | ||
| "homepage": "https://github.com/awslabs/aws-lambda-invoke-store", | ||
| "main": "./dist/invoke-store.js", | ||
| "types": "./dist/invoke-store.d.ts", | ||
| "main": "./dist-cjs/invoke-store.js", | ||
| "module": "./dist-es/invoke-store.js", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist-types/invoke-store.d.ts", | ||
| "module": "./dist-es/invoke-store.js", | ||
| "node": "./dist-cjs/invoke-store.js", | ||
| "import": "./dist-es/invoke-store.js", | ||
| "require": "./dist-cjs/invoke-store.js" | ||
| } | ||
| }, | ||
| "files": [ | ||
| "dist" | ||
| "dist-es", | ||
| "dist-cjs", | ||
| "dist-types" | ||
| ], | ||
@@ -21,4 +32,5 @@ "repository": { | ||
| "scripts": { | ||
| "build": "tsc", | ||
| "clean": "rm -rf dist", | ||
| "build": "yarn clean && yarn build:types && node ./scripts/build-rollup.js", | ||
| "build:types": "tsc -p tsconfig.types.json", | ||
| "clean": "rm -rf dist-types dist-cjs dist-es", | ||
| "test": "vitest run", | ||
@@ -30,5 +42,9 @@ "test:watch": "vitest watch", | ||
| "@changesets/cli": "^2.29.6", | ||
| "@rollup/plugin-node-resolve": "^16.0.3", | ||
| "@rollup/plugin-typescript": "^12.3.0", | ||
| "@tsconfig/node18": "^18.2.4", | ||
| "@types/node": "^18.19.112", | ||
| "typescript": "~5.4.5", | ||
| "@types/node": "^18.19.130", | ||
| "rollup": "^4.52.5", | ||
| "tslib": "^2.8.1", | ||
| "typescript": "^5.9.3", | ||
| "vitest": "^3.1.1" | ||
@@ -35,0 +51,0 @@ }, |
| /** | ||
| * Generic store context that uses protected keys for Lambda fields | ||
| * and allows custom user properties | ||
| */ | ||
| export interface InvokeStoreContext { | ||
| [key: string | symbol]: unknown; | ||
| } | ||
| /** | ||
| * InvokeStore implementation class | ||
| */ | ||
| declare class InvokeStoreImpl { | ||
| private static storage; | ||
| static readonly PROTECTED_KEYS: { | ||
| readonly REQUEST_ID: symbol; | ||
| readonly X_RAY_TRACE_ID: symbol; | ||
| readonly TENANT_ID: symbol; | ||
| }; | ||
| /** | ||
| * Initialize and run code within an invoke context | ||
| */ | ||
| static run<T>(context: InvokeStoreContext, fn: () => T | Promise<T>): T | Promise<T>; | ||
| /** | ||
| * Get the complete current context | ||
| */ | ||
| static getContext(): InvokeStoreContext | undefined; | ||
| /** | ||
| * Get a specific value from the context by key | ||
| */ | ||
| static get<T = unknown>(key: string | symbol): T | undefined; | ||
| /** | ||
| * Set a custom value in the current context | ||
| * Protected Lambda context fields cannot be overwritten | ||
| */ | ||
| static set(key: string | symbol, value: unknown): void; | ||
| /** | ||
| * Get the current request ID | ||
| */ | ||
| static getRequestId(): string; | ||
| /** | ||
| * Get the current X-ray trace ID | ||
| */ | ||
| static getXRayTraceId(): string | undefined; | ||
| /** | ||
| * Get the current tenant ID | ||
| */ | ||
| static getTenantId(): string | undefined; | ||
| /** | ||
| * Check if we're currently within an invoke context | ||
| */ | ||
| static hasContext(): boolean; | ||
| /** | ||
| * Check if a key is protected (readonly Lambda context field) | ||
| */ | ||
| private static isProtectedKey; | ||
| } | ||
| export declare const InvokeStore: typeof InvokeStoreImpl; | ||
| export {}; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.InvokeStore = void 0; | ||
| const async_hooks_1 = require("async_hooks"); | ||
| // AWS_LAMBDA_NODEJS_NO_GLOBAL_AWSLAMBDA provides an escape hatch since we're modifying the global object which may not be expected to a customer's handler. | ||
| const noGlobalAwsLambda = process.env["AWS_LAMBDA_NODEJS_NO_GLOBAL_AWSLAMBDA"] === "1" || | ||
| process.env["AWS_LAMBDA_NODEJS_NO_GLOBAL_AWSLAMBDA"] === "true"; | ||
| if (!noGlobalAwsLambda) { | ||
| globalThis.awslambda = globalThis.awslambda || {}; | ||
| } | ||
| const PROTECTED_KEYS = { | ||
| REQUEST_ID: Symbol("_AWS_LAMBDA_REQUEST_ID"), | ||
| X_RAY_TRACE_ID: Symbol("_AWS_LAMBDA_X_RAY_TRACE_ID"), | ||
| TENANT_ID: Symbol("_AWS_LAMBDA_TENANT_ID"), | ||
| }; | ||
| /** | ||
| * InvokeStore implementation class | ||
| */ | ||
| class InvokeStoreImpl { | ||
| static storage = new async_hooks_1.AsyncLocalStorage(); | ||
| // Protected keys for Lambda context fields | ||
| static PROTECTED_KEYS = PROTECTED_KEYS; | ||
| /** | ||
| * Initialize and run code within an invoke context | ||
| */ | ||
| static run(context, fn) { | ||
| return this.storage.run({ ...context }, fn); | ||
| } | ||
| /** | ||
| * Get the complete current context | ||
| */ | ||
| static getContext() { | ||
| return this.storage.getStore(); | ||
| } | ||
| /** | ||
| * Get a specific value from the context by key | ||
| */ | ||
| static get(key) { | ||
| const context = this.storage.getStore(); | ||
| return context?.[key]; | ||
| } | ||
| /** | ||
| * Set a custom value in the current context | ||
| * Protected Lambda context fields cannot be overwritten | ||
| */ | ||
| static set(key, value) { | ||
| if (this.isProtectedKey(key)) { | ||
| throw new Error(`Cannot modify protected Lambda context field`); | ||
| } | ||
| const context = this.storage.getStore(); | ||
| if (context) { | ||
| context[key] = value; | ||
| } | ||
| } | ||
| /** | ||
| * Get the current request ID | ||
| */ | ||
| static getRequestId() { | ||
| return this.get(this.PROTECTED_KEYS.REQUEST_ID) ?? "-"; | ||
| } | ||
| /** | ||
| * Get the current X-ray trace ID | ||
| */ | ||
| static getXRayTraceId() { | ||
| return this.get(this.PROTECTED_KEYS.X_RAY_TRACE_ID); | ||
| } | ||
| /** | ||
| * Get the current tenant ID | ||
| */ | ||
| static getTenantId() { | ||
| return this.get(this.PROTECTED_KEYS.TENANT_ID); | ||
| } | ||
| /** | ||
| * Check if we're currently within an invoke context | ||
| */ | ||
| static hasContext() { | ||
| return this.storage.getStore() !== undefined; | ||
| } | ||
| /** | ||
| * Check if a key is protected (readonly Lambda context field) | ||
| */ | ||
| static isProtectedKey(key) { | ||
| return (key === this.PROTECTED_KEYS.REQUEST_ID || | ||
| key === this.PROTECTED_KEYS.X_RAY_TRACE_ID); | ||
| } | ||
| } | ||
| let instance; | ||
| if (!noGlobalAwsLambda && globalThis.awslambda?.InvokeStore) { | ||
| instance = globalThis.awslambda.InvokeStore; | ||
| } | ||
| else { | ||
| instance = InvokeStoreImpl; | ||
| if (!noGlobalAwsLambda && globalThis.awslambda) { | ||
| globalThis.awslambda.InvokeStore = instance; | ||
| } | ||
| } | ||
| exports.InvokeStore = instance; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
23630
7.44%6
20%182
18.18%9
80%6
100%1
Infinity%