@middy/core
Advanced tools
Comparing version 4.2.8 to 4.3.0
121
index.d.ts
@@ -9,3 +9,5 @@ import { | ||
declare type PluginHookWithMiddlewareName = (middlewareName: string) => void | ||
declare type PluginHookPromise = (request: Request) => Promise<unknown> | unknown | ||
declare type PluginHookPromise = ( | ||
request: Request | ||
) => Promise<unknown> | unknown | ||
@@ -23,5 +25,11 @@ interface PluginObject { | ||
requestEnd?: PluginHookPromise | ||
streamifyResponse?: Boolean | ||
} | ||
export interface Request<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> { | ||
export interface Request< | ||
TEvent = any, | ||
TResult = any, | ||
TErr = Error, | ||
TContext extends LambdaContext = LambdaContext | ||
> { | ||
event: TEvent | ||
@@ -36,5 +44,15 @@ context: TContext | ||
declare type MiddlewareFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = (request: Request<TEvent, TResult, TErr, TContext>) => any | ||
declare type MiddlewareFn< | ||
TEvent = any, | ||
TResult = any, | ||
TErr = Error, | ||
TContext extends LambdaContext = LambdaContext | ||
> = (request: Request<TEvent, TResult, TErr, TContext>) => any | ||
export interface MiddlewareObj<TEvent = unknown, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> { | ||
export interface MiddlewareObj< | ||
TEvent = unknown, | ||
TResult = any, | ||
TErr = Error, | ||
TContext extends LambdaContext = LambdaContext | ||
> { | ||
before?: MiddlewareFn<TEvent, TResult, TErr, TContext> | ||
@@ -47,6 +65,24 @@ after?: MiddlewareFn<TEvent, TResult, TErr, TContext> | ||
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type | ||
type MiddyInputHandler<TEvent, TResult, TContext extends LambdaContext = LambdaContext> = (event: TEvent, context: TContext, callback: LambdaCallback<TResult>) => void | Promise<TResult> | ||
type MiddyInputPromiseHandler<TEvent, TResult, TContext extends LambdaContext = LambdaContext> = (event: TEvent, context: TContext,) => Promise<TResult> | ||
type MiddyInputHandler< | ||
TEvent, | ||
TResult, | ||
TContext extends LambdaContext = LambdaContext | ||
> = ( | ||
event: TEvent, | ||
context: TContext, | ||
callback: LambdaCallback<TResult> | ||
) => // eslint-disable-next-line @typescript-eslint/no-invalid-void-type | ||
void | Promise<TResult> | ||
type MiddyInputPromiseHandler< | ||
TEvent, | ||
TResult, | ||
TContext extends LambdaContext = LambdaContext | ||
> = (event: TEvent, context: TContext) => Promise<TResult> | ||
export interface MiddyfiedHandler<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> extends MiddyInputHandler<TEvent, TResult, TContext>, | ||
export interface MiddyfiedHandler< | ||
TEvent = any, | ||
TResult = any, | ||
TErr = Error, | ||
TContext extends LambdaContext = LambdaContext | ||
> extends MiddyInputHandler<TEvent, TResult, TContext>, | ||
MiddyInputPromiseHandler<TEvent, TResult, TContext> { | ||
@@ -57,20 +93,55 @@ use: UseFn<TEvent, TResult, TErr, TContext> | ||
onError: AttachMiddlewareFn<TEvent, TResult, TErr, TContext> | ||
handler: <TAdditional>(handler: MiddlewareHandler<LambdaHandler<TEvent & TAdditional, TResult>, TContext>) => MiddyfiedHandler<TEvent, TResult, TErr, TContext> | ||
handler: <TAdditional>( | ||
handler: MiddlewareHandler< | ||
LambdaHandler<TEvent & TAdditional, TResult>, | ||
TContext | ||
> | ||
) => MiddyfiedHandler<TEvent, TResult, TErr, TContext> | ||
} | ||
declare type AttachMiddlewareFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = | ||
(middleware: MiddlewareFn<TEvent, TResult, TErr, TContext>) => MiddyfiedHandler<TEvent, TResult, TErr, TContext> | ||
declare type AttachMiddlewareFn< | ||
TEvent = any, | ||
TResult = any, | ||
TErr = Error, | ||
TContext extends LambdaContext = LambdaContext | ||
> = ( | ||
middleware: MiddlewareFn<TEvent, TResult, TErr, TContext> | ||
) => MiddyfiedHandler<TEvent, TResult, TErr, TContext> | ||
declare type AttachMiddlewareObj<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = | ||
(middleware: MiddlewareObj<TEvent, TResult, TErr, TContext>) => MiddyfiedHandler<TEvent, TResult, TErr, TContext> | ||
declare type AttachMiddlewareObj< | ||
TEvent = any, | ||
TResult = any, | ||
TErr = Error, | ||
TContext extends LambdaContext = LambdaContext | ||
> = ( | ||
middleware: MiddlewareObj<TEvent, TResult, TErr, TContext> | ||
) => MiddyfiedHandler<TEvent, TResult, TErr, TContext> | ||
declare type UseFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = | ||
<TMiddleware extends MiddlewareObj<any, any, Error, any>>(middlewares: TMiddleware | TMiddleware[]) => TMiddleware extends MiddlewareObj<infer TMiddlewareEvent, any, Error, infer TMiddlewareContext> | ||
? MiddyfiedHandler<TMiddlewareEvent & TEvent, TResult, TErr, TMiddlewareContext & TContext> // always true | ||
: never | ||
declare type UseFn< | ||
TEvent = any, | ||
TResult = any, | ||
TErr = Error, | ||
TContext extends LambdaContext = LambdaContext | ||
> = <TMiddleware extends MiddlewareObj<any, any, Error, any>>( | ||
middlewares: TMiddleware | TMiddleware[] | ||
) => TMiddleware extends MiddlewareObj< | ||
infer TMiddlewareEvent, | ||
any, | ||
Error, | ||
infer TMiddlewareContext | ||
> | ||
? MiddyfiedHandler< | ||
TMiddlewareEvent & TEvent, | ||
TResult, | ||
TErr, | ||
TMiddlewareContext & TContext | ||
> // always true | ||
: never | ||
declare type MiddlewareHandler<THandler extends LambdaHandler<any, any>, TContext extends LambdaContext = LambdaContext> = | ||
THandler extends LambdaHandler<infer TEvent, infer TResult> // always true | ||
? MiddyInputHandler<TEvent, TResult, TContext> | ||
: never | ||
declare type MiddlewareHandler< | ||
THandler extends LambdaHandler<any, any>, | ||
TContext extends LambdaContext = LambdaContext | ||
> = THandler extends LambdaHandler<infer TEvent, infer TResult> // always true | ||
? MiddyInputHandler<TEvent, TResult, TContext> | ||
: never | ||
@@ -82,3 +153,11 @@ /** | ||
*/ | ||
declare function middy<TEvent = unknown, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> (handler?: MiddlewareHandler<LambdaHandler<TEvent, TResult>, TContext>, plugin?: PluginObject): MiddyfiedHandler<TEvent, TResult, TErr, TContext> | ||
declare function middy< | ||
TEvent = unknown, | ||
TResult = any, | ||
TErr = Error, | ||
TContext extends LambdaContext = LambdaContext | ||
> ( | ||
handler?: MiddlewareHandler<LambdaHandler<TEvent, TResult>, TContext>, | ||
plugin?: PluginObject | ||
): MiddyfiedHandler<TEvent, TResult, TErr, TContext> | ||
@@ -85,0 +164,0 @@ declare namespace middy { |
26
index.js
@@ -0,1 +1,3 @@ | ||
import { Readable } from 'node:stream'; | ||
import { pipeline } from 'node:stream/promises'; | ||
import { setTimeout } from 'node:timers/promises'; | ||
@@ -7,3 +9,4 @@ const defaultLambdaHandler = ()=>{}; | ||
throw new Error('Timeout'); | ||
} | ||
}, | ||
streamifyResponse: false | ||
}; | ||
@@ -24,3 +27,3 @@ const middy = (lambdaHandler = defaultLambdaHandler, plugin = {})=>{ | ||
const onErrorMiddlewares = []; | ||
const middy = (event = {}, context = {})=>{ | ||
const middyHandler = (event = {}, context = {})=>{ | ||
plugin.requestStart?.(); | ||
@@ -42,2 +45,21 @@ const request = { | ||
}; | ||
const middy = plugin.streamifyResponse ? awslambda.streamifyResponse(async (event, responseStream, context)=>{ | ||
const response = await middyHandler(event, context); | ||
response.body ??= ''; | ||
let { body } = response; | ||
if (typeof body === 'string') { | ||
function* iterator(input) { | ||
const size = 16 * 1024; | ||
let position = 0; | ||
const length = input.length; | ||
while(position < length){ | ||
yield input.substring(position, position + size); | ||
position += size; | ||
} | ||
} | ||
body = Readable.from(iterator(response.body)); | ||
} | ||
responseStream = awslambda.HttpResponseStream.from(responseStream, response); | ||
await pipeline(body, responseStream); | ||
}) : middyHandler; | ||
middy.use = (middlewares)=>{ | ||
@@ -44,0 +66,0 @@ if (!Array.isArray(middlewares)) { |
{ | ||
"name": "@middy/core", | ||
"version": "4.2.8", | ||
"version": "4.3.0", | ||
"description": "🛵 The stylish Node.js middleware engine for AWS Lambda (core package)", | ||
@@ -65,6 +65,7 @@ "type": "module", | ||
"devDependencies": { | ||
"@datastream/core": "0.0.29", | ||
"@types/aws-lambda": "^8.10.76", | ||
"@types/node": "^18.0.0" | ||
}, | ||
"gitHead": "9e7eec0f50fd1edef232edb7a99c035f9bc1f159" | ||
"gitHead": "6ed88ad93746e077e8870d34d970414537abbef7" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27450
619
3