@middy/util
Advanced tools
+20
-19
@@ -15,3 +15,3 @@ // Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors. | ||
| export interface Options<Client, ClientOptions> { | ||
| AwsClient?: new (...[config]: [any] | any) => Client; | ||
| AwsClient?: new (config: ClientOptions) => Client; | ||
| awsClientOptions?: Partial<ClientOptions>; | ||
@@ -24,2 +24,3 @@ awsClientAssumeRole?: string; | ||
| cacheExpiry?: number; | ||
| cacheKeyExpiry?: Record<string, number>; | ||
| setToContext?: boolean; | ||
@@ -32,4 +33,4 @@ } | ||
| expose: boolean; | ||
| [key: string]: any; | ||
| [key: number]: any; | ||
| [key: string]: unknown; | ||
| [key: number]: unknown; | ||
| } | ||
@@ -124,7 +125,7 @@ | ||
| options: Options<Client, ClientOptions>, | ||
| fetch: (request: middy.Request, cachedValues: any) => any, | ||
| fetch: (request: middy.Request, cachedValues: unknown) => unknown, | ||
| request?: middy.Request, | ||
| ): { value: any; expiry: number }; | ||
| ): { value: unknown; expiry: number }; | ||
| declare function getCache(keys: string): any; | ||
| declare function getCache(keys: string): unknown; | ||
@@ -135,9 +136,9 @@ declare function clearCache(keys?: string | string[] | null): void; | ||
| string: string, | ||
| reviver?: (key: string, value: any) => any, | ||
| ): any; | ||
| reviver?: (key: string, value: unknown) => unknown, | ||
| ): unknown; | ||
| declare function normalizeHttpResponse( | ||
| request: any, | ||
| fallbackResponse?: any, | ||
| ): any; | ||
| request: middy.Request, | ||
| fallbackResponse?: Record<string, unknown>, | ||
| ): Record<string, unknown>; | ||
@@ -147,6 +148,6 @@ declare function createError( | ||
| message: string, | ||
| properties?: Record<string, any>, | ||
| properties?: Record<string, unknown>, | ||
| ): HttpError; | ||
| declare function modifyCache(cacheKey: string, value: any): void; | ||
| declare function modifyCache(cacheKey: string, value: unknown): void; | ||
@@ -157,9 +158,9 @@ declare function catchInvalidSignatureException<Client, Command>( | ||
| command: Command, | ||
| ): Promise<any>; | ||
| ): Promise<unknown>; | ||
| declare function jsonSafeStringify( | ||
| value: any, | ||
| replacer?: (key: string, value: any) => any, | ||
| value: unknown, | ||
| replacer?: (key: string, value: unknown) => unknown, | ||
| space?: string | number, | ||
| ): string | any; | ||
| ): string | unknown; | ||
@@ -181,3 +182,3 @@ declare function decodeBody(event: { | ||
| context: LambdaContext, | ||
| ): any; | ||
| ): unknown; | ||
@@ -188,4 +189,4 @@ declare function lambdaContext( | ||
| context: LambdaContext, | ||
| ): any; | ||
| ): unknown; | ||
| declare const httpErrorCodes: Record<number, string>; |
+50
-21
@@ -64,2 +64,32 @@ // Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors. | ||
| } | ||
| // Fast synchronous path: when all internal values are already resolved | ||
| // (warm/cached invocations), skip all Promise machinery entirely | ||
| let allSync = true; | ||
| const syncResults = new Array(values.length); | ||
| for (let i = 0; i < values.length; i++) { | ||
| const internalKey = values[i]; | ||
| const dotIndex = internalKey.indexOf("."); | ||
| const rootKey = | ||
| dotIndex === -1 ? internalKey : internalKey.substring(0, dotIndex); | ||
| let value = request.internal[rootKey]; | ||
| if (isPromise(value)) { | ||
| allSync = false; | ||
| break; | ||
| } | ||
| if (dotIndex !== -1) { | ||
| for (const part of internalKey.substring(dotIndex + 1).split(".")) { | ||
| value = value?.[part]; | ||
| } | ||
| } | ||
| syncResults[i] = value; | ||
| } | ||
| if (allSync) { | ||
| const obj = {}; | ||
| for (let i = 0; i < keys.length; i++) { | ||
| obj[sanitizeKey(keys[i])] = syncResults[i]; | ||
| } | ||
| return obj; | ||
| } | ||
| // Async fallback: for cold/first invocations with pending promises | ||
| const promises = []; | ||
@@ -83,6 +113,13 @@ for (const internalKey of values) { | ||
| values = await Promise.allSettled(promises); | ||
| const errors = values | ||
| .filter((res) => res.status === "rejected") | ||
| .map((res) => res.reason); | ||
| if (errors.length) { | ||
| const obj = {}; | ||
| let errors; | ||
| for (let i = 0; i < keys.length; i++) { | ||
| if (values[i].status === "rejected") { | ||
| errors ??= []; | ||
| errors.push(values[i].reason); | ||
| } else { | ||
| obj[sanitizeKey(keys[i])] = values[i].value; | ||
| } | ||
| } | ||
| if (errors) { | ||
| throw new Error("Failed to resolve internal values", { | ||
@@ -92,6 +129,2 @@ cause: { package: "@middy/util", data: errors }, | ||
| } | ||
| const obj = {}; | ||
| for (let i = keys.length; i--; ) { | ||
| obj[sanitizeKey(keys[i])] = values[i].value; | ||
| } | ||
| return obj; | ||
@@ -131,3 +164,4 @@ }; | ||
| } | ||
| return { ...cached, cache: true }; | ||
| cached.cache = true; | ||
| return cached; | ||
| } | ||
@@ -168,4 +202,5 @@ } | ||
| if (!cache[cacheKey]) return; | ||
| clearTimeout(cache[cacheKey]?.refresh); | ||
| cache[cacheKey] = { ...cache[cacheKey], value, modified: true }; | ||
| clearTimeout(cache[cacheKey].refresh); | ||
| cache[cacheKey].value = value; | ||
| cache[cacheKey].modified = true; | ||
| }; | ||
@@ -200,6 +235,3 @@ | ||
| export const executionContextKeys = [ | ||
| //'requestId', | ||
| "tenantId", | ||
| ]; | ||
| export const executionContextKeys = ["tenantId"]; | ||
@@ -209,6 +241,3 @@ export const isExecutionModeDurable = (context) => { | ||
| // but would require an extra dependency | ||
| if (context.constructor.name === "DurableContextImpl") { | ||
| return true; | ||
| } | ||
| return false; | ||
| return context.constructor.name === "DurableContextImpl"; | ||
| }; | ||
@@ -251,3 +280,3 @@ | ||
| const { body, isBase64Encoded } = event; | ||
| if (body === undefined || body === null) return body; | ||
| if (typeof body === "undefined" || body === null) return body; | ||
| return isBase64Encoded ? Buffer.from(body, "base64").toString() : body; | ||
@@ -297,3 +326,3 @@ }; | ||
| const httpErrorCodes = { | ||
| export const httpErrorCodes = { | ||
| 100: "Continue", | ||
@@ -300,0 +329,0 @@ 101: "Switching Protocols", |
+3
-4
| { | ||
| "name": "@middy/util", | ||
| "version": "7.1.2", | ||
| "version": "7.1.3", | ||
| "description": "🛵 The stylish Node.js middleware engine for AWS Lambda (util package)", | ||
@@ -63,3 +63,3 @@ "type": "module", | ||
| "@aws-sdk/client-ssm": "^3.0.0", | ||
| "@middy/core": "7.1.2", | ||
| "@middy/core": "7.1.3", | ||
| "@types/aws-lambda": "^8.0.0", | ||
@@ -73,4 +73,3 @@ "@types/node": "^22.0.0", | ||
| "url": "https://github.com/sponsors/willfarrell" | ||
| }, | ||
| "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431" | ||
| } | ||
| } |
+17
-0
@@ -33,2 +33,19 @@ <div align="center"> | ||
| ## Install | ||
| ```bash | ||
| npm install --save @middy/util | ||
| ``` | ||
| ## Documentation and examples | ||
| For documentation and examples, refer to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org/docs/intro/utilities). | ||
| ## Contributing | ||
| Everyone is very welcome to contribute to this repository. Feel free to [raise issues](https://github.com/middyjs/middy/issues) or to [submit Pull Requests](https://github.com/middyjs/middy/pulls). | ||
| ## License | ||
@@ -35,0 +52,0 @@ |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
23986
6.57%587
5.2%57
42.5%