@aligent/cdk-prerender-proxy 0.2.4 → 0.2.5-beta

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.
Files changed (150) hide show
  1. package/lib/handlers/cache-control.ts +7 -20
  2. package/lib/handlers/error-response.ts +8 -51
  3. package/lib/handlers/node_modules/.package-lock.json +32 -3
  4. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/index.d.ts +6 -0
  5. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/index.js +14 -0
  6. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/cache-control.d.ts +3 -0
  7. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/cache-control.js +24 -0
  8. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/error-response.d.ts +3 -0
  9. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/error-response.js +50 -0
  10. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/prerender-check.d.ts +3 -0
  11. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/prerender-check.js +28 -0
  12. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/prerender.d.ts +3 -0
  13. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/prerender.js +49 -0
  14. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/redirect.d.ts +3 -0
  15. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/lib/redirect.js +33 -0
  16. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/package.json +27 -0
  17. package/lib/handlers/node_modules/@aligent/cdk-lambda-at-edge-handlers/tsconfig.json +3 -0
  18. package/lib/handlers/node_modules/@middy/core/LICENSE +21 -0
  19. package/lib/handlers/node_modules/@middy/core/README.md +65 -0
  20. package/lib/handlers/node_modules/@middy/core/index.cjs +207 -0
  21. package/lib/handlers/node_modules/@middy/core/index.d.ts +91 -0
  22. package/lib/handlers/node_modules/@middy/core/index.js +199 -0
  23. package/lib/handlers/node_modules/@middy/core/package.json +65 -0
  24. package/lib/handlers/node_modules/@types/node/README.md +4 -4
  25. package/lib/handlers/node_modules/@types/node/assert/strict.d.ts +5 -1
  26. package/lib/handlers/node_modules/@types/node/assert.d.ts +904 -67
  27. package/lib/handlers/node_modules/@types/node/async_hooks.d.ts +371 -96
  28. package/lib/handlers/node_modules/@types/node/buffer.d.ts +2199 -25
  29. package/lib/handlers/node_modules/@types/node/child_process.d.ts +1151 -308
  30. package/lib/handlers/node_modules/@types/node/cluster.d.ts +356 -208
  31. package/lib/handlers/node_modules/@types/node/console.d.ts +324 -45
  32. package/lib/handlers/node_modules/@types/node/constants.d.ts +8 -3
  33. package/lib/handlers/node_modules/@types/node/crypto.d.ts +3230 -846
  34. package/lib/handlers/node_modules/@types/node/dgram.d.ts +478 -74
  35. package/lib/handlers/node_modules/@types/node/diagnostics_channel.d.ts +153 -0
  36. package/lib/handlers/node_modules/@types/node/dns/promises.d.ts +308 -35
  37. package/lib/handlers/node_modules/@types/node/dns.d.ts +444 -107
  38. package/lib/handlers/node_modules/@types/node/dom-events.d.ts +126 -0
  39. package/lib/handlers/node_modules/@types/node/domain.d.ts +162 -16
  40. package/lib/handlers/node_modules/@types/node/events.d.ts +624 -39
  41. package/lib/handlers/node_modules/@types/node/fs/promises.d.ts +911 -365
  42. package/lib/handlers/node_modules/@types/node/fs.d.ts +2611 -978
  43. package/lib/handlers/node_modules/@types/node/globals.d.ts +66 -421
  44. package/lib/handlers/node_modules/@types/node/globals.global.d.ts +1 -1
  45. package/lib/handlers/node_modules/@types/node/http.d.ts +1416 -199
  46. package/lib/handlers/node_modules/@types/node/http2.d.ts +1688 -530
  47. package/lib/handlers/node_modules/@types/node/https.d.ts +532 -26
  48. package/lib/handlers/node_modules/@types/node/index.d.ts +92 -16
  49. package/lib/handlers/node_modules/@types/node/inspector.d.ts +511 -811
  50. package/lib/handlers/node_modules/@types/node/module.d.ts +75 -13
  51. package/lib/handlers/node_modules/@types/node/net.d.ts +710 -167
  52. package/lib/handlers/node_modules/@types/node/os.d.ts +252 -25
  53. package/lib/handlers/node_modules/@types/node/package.json +30 -25
  54. package/lib/handlers/node_modules/@types/node/path.d.ts +59 -31
  55. package/lib/handlers/node_modules/@types/node/perf_hooks.d.ts +420 -105
  56. package/lib/handlers/node_modules/@types/node/process.d.ts +1249 -228
  57. package/lib/handlers/node_modules/@types/node/punycode.d.ts +67 -25
  58. package/lib/handlers/node_modules/@types/node/querystring.d.ts +113 -10
  59. package/lib/handlers/node_modules/@types/node/readline/promises.d.ts +143 -0
  60. package/lib/handlers/node_modules/@types/node/readline.d.ts +570 -109
  61. package/lib/handlers/node_modules/@types/node/repl.d.ts +154 -125
  62. package/lib/handlers/node_modules/@types/node/stream/consumers.d.ts +12 -0
  63. package/lib/handlers/node_modules/@types/node/stream/promises.d.ts +18 -43
  64. package/lib/handlers/node_modules/@types/node/stream/web.d.ts +330 -0
  65. package/lib/handlers/node_modules/@types/node/stream.d.ts +1117 -248
  66. package/lib/handlers/node_modules/@types/node/string_decoder.d.ts +60 -0
  67. package/lib/handlers/node_modules/@types/node/test.d.ts +446 -0
  68. package/lib/handlers/node_modules/@types/node/timers/promises.d.ts +81 -9
  69. package/lib/handlers/node_modules/@types/node/timers.d.ts +84 -17
  70. package/lib/handlers/node_modules/@types/node/tls.d.ts +510 -275
  71. package/lib/handlers/node_modules/@types/node/trace_events.d.ts +120 -10
  72. package/lib/handlers/node_modules/@types/node/ts4.8/assert/strict.d.ts +8 -0
  73. package/lib/handlers/node_modules/@types/node/ts4.8/assert.d.ts +961 -0
  74. package/lib/handlers/node_modules/@types/node/ts4.8/async_hooks.d.ts +501 -0
  75. package/lib/handlers/node_modules/@types/node/ts4.8/buffer.d.ts +2259 -0
  76. package/lib/handlers/node_modules/@types/node/ts4.8/child_process.d.ts +1369 -0
  77. package/lib/handlers/node_modules/@types/node/ts4.8/cluster.d.ts +410 -0
  78. package/lib/handlers/node_modules/@types/node/ts4.8/console.d.ts +412 -0
  79. package/lib/handlers/node_modules/@types/node/ts4.8/constants.d.ts +18 -0
  80. package/lib/handlers/node_modules/@types/node/ts4.8/crypto.d.ts +3964 -0
  81. package/lib/handlers/node_modules/@types/node/ts4.8/dgram.d.ts +545 -0
  82. package/lib/handlers/node_modules/@types/node/ts4.8/diagnostics_channel.d.ts +153 -0
  83. package/lib/handlers/node_modules/@types/node/ts4.8/dns/promises.d.ts +370 -0
  84. package/lib/handlers/node_modules/@types/node/ts4.8/dns.d.ts +659 -0
  85. package/lib/handlers/node_modules/@types/node/ts4.8/dom-events.d.ts +126 -0
  86. package/lib/handlers/node_modules/@types/node/ts4.8/domain.d.ts +170 -0
  87. package/lib/handlers/node_modules/@types/node/ts4.8/events.d.ts +678 -0
  88. package/lib/handlers/node_modules/@types/node/ts4.8/fs/promises.d.ts +1138 -0
  89. package/lib/handlers/node_modules/@types/node/ts4.8/fs.d.ts +3872 -0
  90. package/lib/handlers/node_modules/@types/node/ts4.8/globals.d.ts +294 -0
  91. package/lib/handlers/node_modules/@types/node/ts4.8/globals.global.d.ts +1 -0
  92. package/lib/handlers/node_modules/@types/node/ts4.8/http.d.ts +1651 -0
  93. package/lib/handlers/node_modules/@types/node/ts4.8/http2.d.ts +2134 -0
  94. package/lib/handlers/node_modules/@types/node/ts4.8/https.d.ts +542 -0
  95. package/lib/handlers/node_modules/@types/node/ts4.8/index.d.ts +88 -0
  96. package/lib/handlers/node_modules/@types/node/ts4.8/inspector.d.ts +2741 -0
  97. package/lib/handlers/node_modules/@types/node/ts4.8/module.d.ts +114 -0
  98. package/lib/handlers/node_modules/@types/node/ts4.8/net.d.ts +869 -0
  99. package/lib/handlers/node_modules/@types/node/ts4.8/os.d.ts +466 -0
  100. package/lib/handlers/node_modules/@types/node/ts4.8/path.d.ts +191 -0
  101. package/lib/handlers/node_modules/@types/node/ts4.8/perf_hooks.d.ts +625 -0
  102. package/lib/handlers/node_modules/@types/node/ts4.8/process.d.ts +1482 -0
  103. package/lib/handlers/node_modules/@types/node/ts4.8/punycode.d.ts +117 -0
  104. package/lib/handlers/node_modules/@types/node/ts4.8/querystring.d.ts +131 -0
  105. package/lib/handlers/node_modules/@types/node/ts4.8/readline/promises.d.ts +143 -0
  106. package/lib/handlers/node_modules/@types/node/ts4.8/readline.d.ts +653 -0
  107. package/lib/handlers/node_modules/@types/node/ts4.8/repl.d.ts +424 -0
  108. package/lib/handlers/node_modules/@types/node/ts4.8/stream/consumers.d.ts +12 -0
  109. package/lib/handlers/node_modules/@types/node/ts4.8/stream/promises.d.ts +42 -0
  110. package/lib/handlers/node_modules/@types/node/ts4.8/stream/web.d.ts +330 -0
  111. package/lib/handlers/node_modules/@types/node/ts4.8/stream.d.ts +1340 -0
  112. package/lib/handlers/node_modules/@types/node/ts4.8/string_decoder.d.ts +67 -0
  113. package/lib/handlers/node_modules/@types/node/ts4.8/test.d.ts +446 -0
  114. package/lib/handlers/node_modules/@types/node/ts4.8/timers/promises.d.ts +93 -0
  115. package/lib/handlers/node_modules/@types/node/ts4.8/timers.d.ts +94 -0
  116. package/lib/handlers/node_modules/@types/node/ts4.8/tls.d.ts +1028 -0
  117. package/lib/handlers/node_modules/@types/node/ts4.8/trace_events.d.ts +171 -0
  118. package/lib/handlers/node_modules/@types/node/ts4.8/tty.d.ts +206 -0
  119. package/lib/handlers/node_modules/@types/node/ts4.8/url.d.ts +897 -0
  120. package/lib/handlers/node_modules/@types/node/ts4.8/util.d.ts +1926 -0
  121. package/lib/handlers/node_modules/@types/node/ts4.8/v8.d.ts +396 -0
  122. package/lib/handlers/node_modules/@types/node/ts4.8/vm.d.ts +509 -0
  123. package/lib/handlers/node_modules/@types/node/ts4.8/wasi.d.ts +158 -0
  124. package/lib/handlers/node_modules/@types/node/ts4.8/worker_threads.d.ts +689 -0
  125. package/lib/handlers/node_modules/@types/node/ts4.8/zlib.d.ts +517 -0
  126. package/lib/handlers/node_modules/@types/node/tty.d.ts +163 -23
  127. package/lib/handlers/node_modules/@types/node/url.d.ts +825 -44
  128. package/lib/handlers/node_modules/@types/node/util.d.ts +1842 -72
  129. package/lib/handlers/node_modules/@types/node/v8.d.ts +276 -78
  130. package/lib/handlers/node_modules/@types/node/vm.d.ts +410 -53
  131. package/lib/handlers/node_modules/@types/node/wasi.d.ts +102 -30
  132. package/lib/handlers/node_modules/@types/node/worker_threads.d.ts +559 -152
  133. package/lib/handlers/node_modules/@types/node/zlib.d.ts +234 -78
  134. package/lib/handlers/node_modules/esbuild/README.md +3 -0
  135. package/lib/handlers/node_modules/esbuild/bin/esbuild +0 -0
  136. package/lib/handlers/node_modules/esbuild/install.js +256 -0
  137. package/lib/handlers/node_modules/esbuild/lib/main.d.ts +397 -0
  138. package/lib/handlers/node_modules/esbuild/lib/main.js +1939 -0
  139. package/lib/handlers/node_modules/esbuild/package.json +15 -0
  140. package/lib/handlers/package-lock.json +62 -6
  141. package/lib/handlers/package.json +6 -1
  142. package/lib/handlers/prerender-check.ts +8 -27
  143. package/lib/handlers/prerender.ts +8 -43
  144. package/package.json +1 -1
  145. package/lib/handlers/node_modules/@types/node/base.d.ts +0 -19
  146. package/lib/handlers/node_modules/@types/node/diagnostic_channel.d.ts +0 -34
  147. package/lib/handlers/node_modules/@types/node/ts3.6/assert.d.ts +0 -98
  148. package/lib/handlers/node_modules/@types/node/ts3.6/base.d.ts +0 -68
  149. package/lib/handlers/node_modules/@types/node/ts3.6/index.d.ts +0 -7
  150. package/lib/handlers/node_modules/@types/node/util/types.d.ts +0 -53
@@ -0,0 +1,207 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(module, "exports", {
6
+ enumerable: true,
7
+ get: ()=>_default
8
+ });
9
+ const _events = require("events");
10
+ const defaultLambdaHandler = ()=>{};
11
+ const defaultPlugin = {
12
+ timeoutEarlyInMillis: 5,
13
+ timeoutEarlyResponse: ()=>{
14
+ throw new Error('Timeout');
15
+ }
16
+ };
17
+ const middy = (lambdaHandler = defaultLambdaHandler, plugin = {})=>{
18
+ if (typeof lambdaHandler !== 'function') {
19
+ plugin = lambdaHandler;
20
+ lambdaHandler = defaultLambdaHandler;
21
+ }
22
+ plugin = {
23
+ ...defaultPlugin,
24
+ ...plugin
25
+ };
26
+ plugin.timeoutEarly = plugin.timeoutEarlyInMillis > 0;
27
+ plugin.beforePrefetch?.();
28
+ const beforeMiddlewares = [];
29
+ const afterMiddlewares = [];
30
+ const onErrorMiddlewares = [];
31
+ const middy = (event = {}, context = {})=>{
32
+ plugin.requestStart?.();
33
+ const request = {
34
+ event,
35
+ context,
36
+ response: undefined,
37
+ error: undefined,
38
+ internal: plugin.internal ?? {}
39
+ };
40
+ return runRequest(request, [
41
+ ...beforeMiddlewares
42
+ ], lambdaHandler, [
43
+ ...afterMiddlewares
44
+ ], [
45
+ ...onErrorMiddlewares
46
+ ], plugin);
47
+ };
48
+ middy.use = (middlewares)=>{
49
+ if (!Array.isArray(middlewares)) {
50
+ middlewares = [
51
+ middlewares
52
+ ];
53
+ }
54
+ for (const middleware of middlewares){
55
+ const { before , after , onError } = middleware;
56
+ if (!before && !after && !onError) {
57
+ throw new Error('Middleware must be an object containing at least one key among "before", "after", "onError"');
58
+ }
59
+ if (before) middy.before(before);
60
+ if (after) middy.after(after);
61
+ if (onError) middy.onError(onError);
62
+ }
63
+ return middy;
64
+ };
65
+ middy.before = (beforeMiddleware)=>{
66
+ beforeMiddlewares.push(beforeMiddleware);
67
+ return middy;
68
+ };
69
+ middy.after = (afterMiddleware)=>{
70
+ afterMiddlewares.unshift(afterMiddleware);
71
+ return middy;
72
+ };
73
+ middy.onError = (onErrorMiddleware)=>{
74
+ onErrorMiddlewares.unshift(onErrorMiddleware);
75
+ return middy;
76
+ };
77
+ middy.handler = (replaceLambdaHandler)=>{
78
+ lambdaHandler = replaceLambdaHandler;
79
+ return middy;
80
+ };
81
+ return middy;
82
+ };
83
+ const runRequest = async (request, beforeMiddlewares, lambdaHandler, afterMiddlewares, onErrorMiddlewares, plugin)=>{
84
+ const timeoutEarly = plugin.timeoutEarly && request.context.getRemainingTimeInMillis;
85
+ try {
86
+ await runMiddlewares(request, beforeMiddlewares, plugin);
87
+ if (typeof request.response === 'undefined') {
88
+ plugin.beforeHandler?.();
89
+ const handlerAbort = new AbortController();
90
+ let timeoutAbort;
91
+ if (timeoutEarly) timeoutAbort = new AbortController();
92
+ request.response = await Promise.race([
93
+ lambdaHandler(request.event, request.context, {
94
+ signal: handlerAbort.signal
95
+ }),
96
+ timeoutEarly ? setTimeoutPromise(request.context.getRemainingTimeInMillis() - plugin.timeoutEarlyInMillis, {
97
+ signal: timeoutAbort.signal
98
+ }).then(()=>{
99
+ handlerAbort.abort();
100
+ return plugin.timeoutEarlyResponse();
101
+ }) : Promise.race([])
102
+ ]);
103
+ if (timeoutEarly) timeoutAbort.abort();
104
+ plugin.afterHandler?.();
105
+ await runMiddlewares(request, afterMiddlewares, plugin);
106
+ }
107
+ } catch (e1) {
108
+ request.response = undefined;
109
+ request.error = e1;
110
+ try {
111
+ await runMiddlewares(request, onErrorMiddlewares, plugin);
112
+ } catch (e) {
113
+ e.originalError = request.error;
114
+ request.error = e;
115
+ throw request.error;
116
+ }
117
+ if (typeof request.response === 'undefined') throw request.error;
118
+ } finally{
119
+ await plugin.requestEnd?.(request);
120
+ }
121
+ return request.response;
122
+ };
123
+ const runMiddlewares = async (request, middlewares, plugin)=>{
124
+ for (const nextMiddleware of middlewares){
125
+ plugin.beforeMiddleware?.(nextMiddleware.name);
126
+ const res = await nextMiddleware(request);
127
+ plugin.afterMiddleware?.(nextMiddleware.name);
128
+ if (typeof res !== 'undefined') {
129
+ request.response = res;
130
+ return;
131
+ }
132
+ }
133
+ };
134
+ const polyfillAbortController = ()=>{
135
+ if (process.version < 'v15.0.0') {
136
+ class AbortSignal {
137
+ toString() {
138
+ return '[object AbortSignal]';
139
+ }
140
+ get [Symbol.toStringTag]() {
141
+ return 'AbortSignal';
142
+ }
143
+ removeEventListener(name, handler) {
144
+ this.eventEmitter.removeListener(name, handler);
145
+ }
146
+ addEventListener(name, handler) {
147
+ this.eventEmitter.on(name, handler);
148
+ }
149
+ dispatchEvent(type) {
150
+ const event = {
151
+ type,
152
+ target: this
153
+ };
154
+ const handlerName = `on${type}`;
155
+ if (typeof this[handlerName] === 'function') this[handlerName](event);
156
+ this.eventEmitter.emit(type, event);
157
+ }
158
+ constructor(){
159
+ this.eventEmitter = new _events.EventEmitter();
160
+ this.onabort = null;
161
+ this.aborted = false;
162
+ }
163
+ }
164
+ return class AbortController1 {
165
+ abort() {
166
+ if (this.signal.aborted) return;
167
+ this.signal.aborted = true;
168
+ this.signal.dispatchEvent('abort');
169
+ }
170
+ toString() {
171
+ return '[object AbortController]';
172
+ }
173
+ get [Symbol.toStringTag]() {
174
+ return 'AbortController';
175
+ }
176
+ constructor(){
177
+ this.signal = new AbortSignal();
178
+ }
179
+ };
180
+ } else {
181
+ return AbortController;
182
+ }
183
+ };
184
+ global.AbortController = polyfillAbortController();
185
+ const polyfillSetTimeoutPromise = ()=>{
186
+ return (ms, { signal })=>{
187
+ if (signal.aborted) {
188
+ return Promise.reject(new Error('Aborted', 'AbortError'));
189
+ }
190
+ return new Promise((resolve, reject)=>{
191
+ const abortHandler = ()=>{
192
+ clearTimeout(timeout);
193
+ reject(new Error('Aborted', 'AbortError'));
194
+ };
195
+ const timeout = setTimeout(()=>{
196
+ resolve();
197
+ signal.removeEventListener('abort', abortHandler);
198
+ }, ms);
199
+ signal.addEventListener('abort', abortHandler);
200
+ });
201
+ };
202
+ };
203
+ const setTimeoutPromise = polyfillSetTimeoutPromise();
204
+ const _default = middy;
205
+
206
+
207
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1,91 @@
1
+ import {
2
+ Callback as LambdaCallback,
3
+ Context as LambdaContext,
4
+ Handler as LambdaHandler
5
+ } from 'aws-lambda'
6
+
7
+ declare type PluginHook = () => void
8
+ declare type PluginHookWithMiddlewareName = (middlewareName: string) => void
9
+ declare type PluginHookPromise = (request: Request) => Promise<unknown> | unknown
10
+
11
+ interface PluginObject {
12
+ internal?: any
13
+ beforePrefetch?: PluginHook
14
+ requestStart?: PluginHook
15
+ beforeMiddleware?: PluginHookWithMiddlewareName
16
+ afterMiddleware?: PluginHookWithMiddlewareName
17
+ beforeHandler?: PluginHook
18
+ timeoutEarlyInMillis?: number
19
+ timeoutEarlyResponse?: PluginHook
20
+ afterHandler?: PluginHook
21
+ requestEnd?: PluginHookPromise
22
+ }
23
+
24
+ interface Request<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> {
25
+ event: TEvent
26
+ context: TContext
27
+ response: TResult | null
28
+ error: TErr | null
29
+ internal: {
30
+ [key: string]: any
31
+ }
32
+ }
33
+
34
+ declare type MiddlewareFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> = (request: Request<TEvent, TResult, TErr, TContext>) => any
35
+
36
+ export interface MiddlewareObj<TEvent = unknown, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> {
37
+ before?: MiddlewareFn<TEvent, TResult, TErr, TContext>
38
+ after?: MiddlewareFn<TEvent, TResult, TErr, TContext>
39
+ onError?: MiddlewareFn<TEvent, TResult, TErr, TContext>
40
+ }
41
+
42
+ // The AWS provided Handler type uses void | Promise<TResult> so we have no choice but to follow and suppress the linter warning
43
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
44
+ type MiddyInputHandler<TEvent, TResult, TContext extends LambdaContext = LambdaContext> = (event: TEvent, context: TContext, callback: LambdaCallback<TResult>) => void | Promise<TResult>
45
+ type MiddyInputPromiseHandler<TEvent, TResult, TContext extends LambdaContext = LambdaContext> = (event: TEvent, context: TContext,) => Promise<TResult>
46
+
47
+ export interface MiddyfiedHandler<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> extends MiddyInputHandler<TEvent, TResult, TContext>,
48
+ MiddyInputPromiseHandler<TEvent, TResult, TContext> {
49
+ use: UseFn<TEvent, TResult, TErr, TContext>
50
+ before: AttachMiddlewareFn<TEvent, TResult, TErr, TContext>
51
+ after: AttachMiddlewareFn<TEvent, TResult, TErr, TContext>
52
+ onError: AttachMiddlewareFn<TEvent, TResult, TErr, TContext>
53
+ handler: (handler: MiddlewareHandler<LambdaHandler<TEvent, TResult>, TContext>) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
54
+ }
55
+
56
+ declare type AttachMiddlewareFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> =
57
+ (middleware: MiddlewareFn<TEvent, TResult, TErr, TContext>) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
58
+
59
+ declare type AttachMiddlewareObj<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> =
60
+ (middleware: MiddlewareObj<TEvent, TResult, TErr, TContext>) => MiddyfiedHandler<TEvent, TResult, TErr, TContext>
61
+
62
+ declare type UseFn<TEvent = any, TResult = any, TErr = Error, TContext extends LambdaContext = LambdaContext> =
63
+ <TMiddleware extends MiddlewareObj<any, any, Error, any>>(middlewares: TMiddleware | TMiddleware[]) => TMiddleware extends MiddlewareObj<infer TMiddlewareEvent, any, Error, infer TMiddlewareContext>
64
+ ? MiddyfiedHandler<TMiddlewareEvent & TEvent, TResult, TErr, TMiddlewareContext & TContext> // always true
65
+ : never
66
+
67
+ declare type MiddlewareHandler<THandler extends LambdaHandler<any, any>, TContext extends LambdaContext = LambdaContext> =
68
+ THandler extends LambdaHandler<infer TEvent, infer TResult> // always true
69
+ ? MiddyInputHandler<TEvent, TResult, TContext>
70
+ : never
71
+
72
+ /**
73
+ * Middy factory function. Use it to wrap your existing handler to enable middlewares on it.
74
+ * @param handler your original AWS Lambda function
75
+ * @param plugin wraps around each middleware and handler to add custom lifecycle behaviours (e.g. to profile performance)
76
+ */
77
+ 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>
78
+
79
+ declare namespace middy {
80
+ export {
81
+ Request,
82
+ PluginHook,
83
+ PluginHookWithMiddlewareName,
84
+ PluginObject,
85
+ MiddlewareFn,
86
+ MiddlewareObj,
87
+ MiddyfiedHandler
88
+ }
89
+ }
90
+
91
+ export default middy
@@ -0,0 +1,199 @@
1
+ import { EventEmitter } from 'events';
2
+ const defaultLambdaHandler = ()=>{};
3
+ const defaultPlugin = {
4
+ timeoutEarlyInMillis: 5,
5
+ timeoutEarlyResponse: ()=>{
6
+ throw new Error('Timeout');
7
+ }
8
+ };
9
+ const middy = (lambdaHandler = defaultLambdaHandler, plugin = {})=>{
10
+ if (typeof lambdaHandler !== 'function') {
11
+ plugin = lambdaHandler;
12
+ lambdaHandler = defaultLambdaHandler;
13
+ }
14
+ plugin = {
15
+ ...defaultPlugin,
16
+ ...plugin
17
+ };
18
+ plugin.timeoutEarly = plugin.timeoutEarlyInMillis > 0;
19
+ plugin.beforePrefetch?.();
20
+ const beforeMiddlewares = [];
21
+ const afterMiddlewares = [];
22
+ const onErrorMiddlewares = [];
23
+ const middy = (event = {}, context = {})=>{
24
+ plugin.requestStart?.();
25
+ const request = {
26
+ event,
27
+ context,
28
+ response: undefined,
29
+ error: undefined,
30
+ internal: plugin.internal ?? {}
31
+ };
32
+ return runRequest(request, [
33
+ ...beforeMiddlewares
34
+ ], lambdaHandler, [
35
+ ...afterMiddlewares
36
+ ], [
37
+ ...onErrorMiddlewares
38
+ ], plugin);
39
+ };
40
+ middy.use = (middlewares)=>{
41
+ if (!Array.isArray(middlewares)) {
42
+ middlewares = [
43
+ middlewares
44
+ ];
45
+ }
46
+ for (const middleware of middlewares){
47
+ const { before , after , onError } = middleware;
48
+ if (!before && !after && !onError) {
49
+ throw new Error('Middleware must be an object containing at least one key among "before", "after", "onError"');
50
+ }
51
+ if (before) middy.before(before);
52
+ if (after) middy.after(after);
53
+ if (onError) middy.onError(onError);
54
+ }
55
+ return middy;
56
+ };
57
+ middy.before = (beforeMiddleware)=>{
58
+ beforeMiddlewares.push(beforeMiddleware);
59
+ return middy;
60
+ };
61
+ middy.after = (afterMiddleware)=>{
62
+ afterMiddlewares.unshift(afterMiddleware);
63
+ return middy;
64
+ };
65
+ middy.onError = (onErrorMiddleware)=>{
66
+ onErrorMiddlewares.unshift(onErrorMiddleware);
67
+ return middy;
68
+ };
69
+ middy.handler = (replaceLambdaHandler)=>{
70
+ lambdaHandler = replaceLambdaHandler;
71
+ return middy;
72
+ };
73
+ return middy;
74
+ };
75
+ const runRequest = async (request, beforeMiddlewares, lambdaHandler, afterMiddlewares, onErrorMiddlewares, plugin)=>{
76
+ const timeoutEarly = plugin.timeoutEarly && request.context.getRemainingTimeInMillis;
77
+ try {
78
+ await runMiddlewares(request, beforeMiddlewares, plugin);
79
+ if (typeof request.response === 'undefined') {
80
+ plugin.beforeHandler?.();
81
+ const handlerAbort = new AbortController();
82
+ let timeoutAbort;
83
+ if (timeoutEarly) timeoutAbort = new AbortController();
84
+ request.response = await Promise.race([
85
+ lambdaHandler(request.event, request.context, {
86
+ signal: handlerAbort.signal
87
+ }),
88
+ timeoutEarly ? setTimeoutPromise(request.context.getRemainingTimeInMillis() - plugin.timeoutEarlyInMillis, {
89
+ signal: timeoutAbort.signal
90
+ }).then(()=>{
91
+ handlerAbort.abort();
92
+ return plugin.timeoutEarlyResponse();
93
+ }) : Promise.race([])
94
+ ]);
95
+ if (timeoutEarly) timeoutAbort.abort();
96
+ plugin.afterHandler?.();
97
+ await runMiddlewares(request, afterMiddlewares, plugin);
98
+ }
99
+ } catch (e1) {
100
+ request.response = undefined;
101
+ request.error = e1;
102
+ try {
103
+ await runMiddlewares(request, onErrorMiddlewares, plugin);
104
+ } catch (e) {
105
+ e.originalError = request.error;
106
+ request.error = e;
107
+ throw request.error;
108
+ }
109
+ if (typeof request.response === 'undefined') throw request.error;
110
+ } finally{
111
+ await plugin.requestEnd?.(request);
112
+ }
113
+ return request.response;
114
+ };
115
+ const runMiddlewares = async (request, middlewares, plugin)=>{
116
+ for (const nextMiddleware of middlewares){
117
+ plugin.beforeMiddleware?.(nextMiddleware.name);
118
+ const res = await nextMiddleware(request);
119
+ plugin.afterMiddleware?.(nextMiddleware.name);
120
+ if (typeof res !== 'undefined') {
121
+ request.response = res;
122
+ return;
123
+ }
124
+ }
125
+ };
126
+ const polyfillAbortController = ()=>{
127
+ if (process.version < 'v15.0.0') {
128
+ class AbortSignal {
129
+ toString() {
130
+ return '[object AbortSignal]';
131
+ }
132
+ get [Symbol.toStringTag]() {
133
+ return 'AbortSignal';
134
+ }
135
+ removeEventListener(name, handler) {
136
+ this.eventEmitter.removeListener(name, handler);
137
+ }
138
+ addEventListener(name, handler) {
139
+ this.eventEmitter.on(name, handler);
140
+ }
141
+ dispatchEvent(type) {
142
+ const event = {
143
+ type,
144
+ target: this
145
+ };
146
+ const handlerName = `on${type}`;
147
+ if (typeof this[handlerName] === 'function') this[handlerName](event);
148
+ this.eventEmitter.emit(type, event);
149
+ }
150
+ constructor(){
151
+ this.eventEmitter = new EventEmitter();
152
+ this.onabort = null;
153
+ this.aborted = false;
154
+ }
155
+ }
156
+ return class AbortController1 {
157
+ abort() {
158
+ if (this.signal.aborted) return;
159
+ this.signal.aborted = true;
160
+ this.signal.dispatchEvent('abort');
161
+ }
162
+ toString() {
163
+ return '[object AbortController]';
164
+ }
165
+ get [Symbol.toStringTag]() {
166
+ return 'AbortController';
167
+ }
168
+ constructor(){
169
+ this.signal = new AbortSignal();
170
+ }
171
+ };
172
+ } else {
173
+ return AbortController;
174
+ }
175
+ };
176
+ global.AbortController = polyfillAbortController();
177
+ const polyfillSetTimeoutPromise = ()=>{
178
+ return (ms, { signal })=>{
179
+ if (signal.aborted) {
180
+ return Promise.reject(new Error('Aborted', 'AbortError'));
181
+ }
182
+ return new Promise((resolve, reject)=>{
183
+ const abortHandler = ()=>{
184
+ clearTimeout(timeout);
185
+ reject(new Error('Aborted', 'AbortError'));
186
+ };
187
+ const timeout = setTimeout(()=>{
188
+ resolve();
189
+ signal.removeEventListener('abort', abortHandler);
190
+ }, ms);
191
+ signal.addEventListener('abort', abortHandler);
192
+ });
193
+ };
194
+ };
195
+ const setTimeoutPromise = polyfillSetTimeoutPromise();
196
+ export default middy;
197
+
198
+
199
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@middy/core",
3
+ "version": "3.6.2",
4
+ "description": "🛵 The stylish Node.js middleware engine for AWS Lambda (core package)",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=14"
8
+ },
9
+ "engineStrict": true,
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "main": "./index.cjs",
14
+ "module": "./index.js",
15
+ "exports": {
16
+ ".": {
17
+ "import": {
18
+ "types": "./index.d.ts",
19
+ "default": "./index.js"
20
+ },
21
+ "require": {
22
+ "types": "./index.d.ts",
23
+ "default": "./index.cjs"
24
+ }
25
+ }
26
+ },
27
+ "types": "index.d.ts",
28
+ "files": [
29
+ "index.js",
30
+ "index.cjs",
31
+ "index.d.ts"
32
+ ],
33
+ "scripts": {
34
+ "test": "npm run test:unit",
35
+ "test:unit": "ava",
36
+ "test:benchmark": "node __benchmarks__/index.js"
37
+ },
38
+ "license": "MIT",
39
+ "keywords": [
40
+ "Lambda",
41
+ "Middleware",
42
+ "Serverless",
43
+ "Framework",
44
+ "AWS",
45
+ "AWS Lambda"
46
+ ],
47
+ "author": {
48
+ "name": "Middy contributors",
49
+ "url": "https://github.com/middyjs/middy/graphs/contributors"
50
+ },
51
+ "repository": {
52
+ "type": "git",
53
+ "url": "github:middyjs/middy",
54
+ "directory": "packages/core"
55
+ },
56
+ "bugs": {
57
+ "url": "https://github.com/middyjs/middy/issues"
58
+ },
59
+ "homepage": "https://middy.js.org",
60
+ "devDependencies": {
61
+ "@types/aws-lambda": "^8.10.76",
62
+ "@types/node": "^18.0.0"
63
+ },
64
+ "gitHead": "4c5d64c72dd4bfde0d3b828a0082c99f688c759c"
65
+ }
@@ -2,15 +2,15 @@
2
2
  > `npm install --save @types/node`
3
3
 
4
4
  # Summary
5
- This package contains type definitions for Node.js (http://nodejs.org/).
5
+ This package contains type definitions for Node.js (https://nodejs.org/).
6
6
 
7
7
  # Details
8
8
  Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node.
9
9
 
10
10
  ### Additional Details
11
- * Last updated: Fri, 18 Jun 2021 21:01:11 GMT
11
+ * Last updated: Mon, 26 Dec 2022 17:32:37 GMT
12
12
  * Dependencies: none
13
- * Global values: `AbortController`, `AbortSignal`, `Buffer`, `__dirname`, `__filename`, `clearImmediate`, `clearInterval`, `clearTimeout`, `console`, `exports`, `global`, `module`, `process`, `queueMicrotask`, `require`, `setImmediate`, `setInterval`, `setTimeout`
13
+ * Global values: `AbortController`, `AbortSignal`, `__dirname`, `__filename`, `console`, `exports`, `gc`, `global`, `module`, `process`, `require`, `structuredClone`
14
14
 
15
15
  # Credits
16
- These definitions were written by [Microsoft TypeScript](https://github.com/Microsoft), [DefinitelyTyped](https://github.com/DefinitelyTyped), [Alberto Schiabel](https://github.com/jkomyno), [Alvis HT Tang](https://github.com/alvis), [Andrew Makarov](https://github.com/r3nya), [Benjamin Toueg](https://github.com/btoueg), [Chigozirim C.](https://github.com/smac89), [David Junger](https://github.com/touffy), [Deividas Bakanas](https://github.com/DeividasBakanas), [Eugene Y. Q. Shen](https://github.com/eyqs), [Hannes Magnusson](https://github.com/Hannes-Magnusson-CK), [Hoàng Văn Khải](https://github.com/KSXGitHub), [Huw](https://github.com/hoo29), [Kelvin Jin](https://github.com/kjin), [Klaus Meinhardt](https://github.com/ajafff), [Lishude](https://github.com/islishude), [Mariusz Wiktorczyk](https://github.com/mwiktorczyk), [Mohsen Azimi](https://github.com/mohsen1), [Nicolas Even](https://github.com/n-e), [Nikita Galkin](https://github.com/galkin), [Parambir Singh](https://github.com/parambirs), [Sebastian Silbermann](https://github.com/eps1lon), [Simon Schick](https://github.com/SimonSchick), [Thomas den Hollander](https://github.com/ThomasdenH), [Wilco Bakker](https://github.com/WilcoBakker), [wwwy3y3](https://github.com/wwwy3y3), [Samuel Ainsworth](https://github.com/samuela), [Kyle Uehlein](https://github.com/kuehlein), [Thanik Bhongbhibhat](https://github.com/bhongy), [Marcin Kopacz](https://github.com/chyzwar), [Trivikram Kamat](https://github.com/trivikr), [Minh Son Nguyen](https://github.com/nguymin4), [Junxiao Shi](https://github.com/yoursunny), [Ilia Baryshnikov](https://github.com/qwelias), [ExE Boss](https://github.com/ExE-Boss), [Surasak Chaisurin](https://github.com/Ryan-Willpower), [Piotr Błażejewicz](https://github.com/peterblazejewicz), [Anna Henningsen](https://github.com/addaleax), [Jason Kwok](https://github.com/JasonHK), [Victor Perin](https://github.com/victorperin), and [Yongsheng Zhang](https://github.com/ZYSzys).
16
+ These definitions were written by [Microsoft TypeScript](https://github.com/Microsoft), [DefinitelyTyped](https://github.com/DefinitelyTyped), [Alberto Schiabel](https://github.com/jkomyno), [Alvis HT Tang](https://github.com/alvis), [Andrew Makarov](https://github.com/r3nya), [Benjamin Toueg](https://github.com/btoueg), [Chigozirim C.](https://github.com/smac89), [David Junger](https://github.com/touffy), [Deividas Bakanas](https://github.com/DeividasBakanas), [Eugene Y. Q. Shen](https://github.com/eyqs), [Hannes Magnusson](https://github.com/Hannes-Magnusson-CK), [Huw](https://github.com/hoo29), [Kelvin Jin](https://github.com/kjin), [Klaus Meinhardt](https://github.com/ajafff), [Lishude](https://github.com/islishude), [Mariusz Wiktorczyk](https://github.com/mwiktorczyk), [Mohsen Azimi](https://github.com/mohsen1), [Nicolas Even](https://github.com/n-e), [Nikita Galkin](https://github.com/galkin), [Parambir Singh](https://github.com/parambirs), [Sebastian Silbermann](https://github.com/eps1lon), [Simon Schick](https://github.com/SimonSchick), [Thomas den Hollander](https://github.com/ThomasdenH), [Wilco Bakker](https://github.com/WilcoBakker), [wwwy3y3](https://github.com/wwwy3y3), [Samuel Ainsworth](https://github.com/samuela), [Kyle Uehlein](https://github.com/kuehlein), [Thanik Bhongbhibhat](https://github.com/bhongy), [Marcin Kopacz](https://github.com/chyzwar), [Trivikram Kamat](https://github.com/trivikr), [Junxiao Shi](https://github.com/yoursunny), [Ilia Baryshnikov](https://github.com/qwelias), [ExE Boss](https://github.com/ExE-Boss), [Piotr Błażejewicz](https://github.com/peterblazejewicz), [Anna Henningsen](https://github.com/addaleax), [Victor Perin](https://github.com/victorperin), [Yongsheng Zhang](https://github.com/ZYSzys), [NodeJS Contributors](https://github.com/NodeJS), [Linus Unnebäck](https://github.com/LinusU), [wafuwafu13](https://github.com/wafuwafu13), [Matteo Collina](https://github.com/mcollina), and [Dmitry Semigradsky](https://github.com/Semigradsky).
@@ -1,4 +1,8 @@
1
1
  declare module 'assert/strict' {
2
- import { strict } from 'assert';
2
+ import { strict } from 'node:assert';
3
+ export = strict;
4
+ }
5
+ declare module 'node:assert/strict' {
6
+ import { strict } from 'node:assert';
3
7
  export = strict;
4
8
  }