@webiny/handler 6.0.0-beta.0 → 6.0.0-rc.0

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 (86) hide show
  1. package/Context.d.ts +4 -3
  2. package/Context.js +6 -14
  3. package/Context.js.map +1 -1
  4. package/PreHandler/IPreHandler.d.ts +9 -0
  5. package/PreHandler/IPreHandler.js +7 -0
  6. package/PreHandler/IPreHandler.js.map +1 -0
  7. package/PreHandler/IfNotOptionsRequest.d.ts +9 -0
  8. package/PreHandler/IfNotOptionsRequest.js +21 -0
  9. package/PreHandler/IfNotOptionsRequest.js.map +1 -0
  10. package/PreHandler/IfOptionsRequest.d.ts +9 -0
  11. package/PreHandler/IfOptionsRequest.js +21 -0
  12. package/PreHandler/IfOptionsRequest.js.map +1 -0
  13. package/PreHandler/PreHandler.d.ts +9 -0
  14. package/PreHandler/PreHandler.js +17 -0
  15. package/PreHandler/PreHandler.js.map +1 -0
  16. package/PreHandler/ProcessBeforeHandlerPlugins.d.ts +10 -0
  17. package/PreHandler/ProcessBeforeHandlerPlugins.js +24 -0
  18. package/PreHandler/ProcessBeforeHandlerPlugins.js.map +1 -0
  19. package/PreHandler/ProcessContextPlugins.d.ts +10 -0
  20. package/PreHandler/ProcessContextPlugins.js +24 -0
  21. package/PreHandler/ProcessContextPlugins.js.map +1 -0
  22. package/PreHandler/ProcessHandlerOnRequestPlugins.d.ts +10 -0
  23. package/PreHandler/ProcessHandlerOnRequestPlugins.js +26 -0
  24. package/PreHandler/ProcessHandlerOnRequestPlugins.js.map +1 -0
  25. package/PreHandler/SendEarlyOptionsResponse.d.ts +9 -0
  26. package/PreHandler/SendEarlyOptionsResponse.js +31 -0
  27. package/PreHandler/SendEarlyOptionsResponse.js.map +1 -0
  28. package/PreHandler/SetDefaultHeaders.d.ts +9 -0
  29. package/PreHandler/SetDefaultHeaders.js +57 -0
  30. package/PreHandler/SetDefaultHeaders.js.map +1 -0
  31. package/README.md +10 -14
  32. package/ResponseHeaders.d.ts +6 -7
  33. package/ResponseHeaders.js +1 -8
  34. package/ResponseHeaders.js.map +1 -1
  35. package/abstractions/Reply.d.ts +5 -0
  36. package/abstractions/Reply.js +4 -0
  37. package/abstractions/Reply.js.map +1 -0
  38. package/abstractions/Request.d.ts +5 -0
  39. package/abstractions/Request.js +4 -0
  40. package/abstractions/Request.js.map +1 -0
  41. package/fastify.d.ts +6 -4
  42. package/fastify.js +170 -194
  43. package/fastify.js.map +1 -1
  44. package/index.d.ts +17 -12
  45. package/index.js +18 -126
  46. package/index.js.map +1 -1
  47. package/package.json +16 -24
  48. package/plugins/BeforeHandlerPlugin.d.ts +1 -1
  49. package/plugins/BeforeHandlerPlugin.js +3 -11
  50. package/plugins/BeforeHandlerPlugin.js.map +1 -1
  51. package/plugins/EventPlugin.d.ts +3 -3
  52. package/plugins/EventPlugin.js +3 -12
  53. package/plugins/EventPlugin.js.map +1 -1
  54. package/plugins/HandlerErrorPlugin.d.ts +1 -1
  55. package/plugins/HandlerErrorPlugin.js +3 -11
  56. package/plugins/HandlerErrorPlugin.js.map +1 -1
  57. package/plugins/HandlerOnRequestPlugin.d.ts +9 -8
  58. package/plugins/HandlerOnRequestPlugin.js +5 -12
  59. package/plugins/HandlerOnRequestPlugin.js.map +1 -1
  60. package/plugins/HandlerResultPlugin.d.ts +1 -1
  61. package/plugins/HandlerResultPlugin.js +3 -11
  62. package/plugins/HandlerResultPlugin.js.map +1 -1
  63. package/plugins/ModifyFastifyPlugin.d.ts +2 -2
  64. package/plugins/ModifyFastifyPlugin.js +3 -11
  65. package/plugins/ModifyFastifyPlugin.js.map +1 -1
  66. package/plugins/ModifyResponseHeadersPlugin.d.ts +3 -3
  67. package/plugins/ModifyResponseHeadersPlugin.js +3 -11
  68. package/plugins/ModifyResponseHeadersPlugin.js.map +1 -1
  69. package/plugins/OnRequestResponseSendPlugin.d.ts +26 -0
  70. package/plugins/OnRequestResponseSendPlugin.js +31 -0
  71. package/plugins/OnRequestResponseSendPlugin.js.map +1 -0
  72. package/plugins/OnRequestTimeoutPlugin.d.ts +12 -0
  73. package/plugins/OnRequestTimeoutPlugin.js +16 -0
  74. package/plugins/OnRequestTimeoutPlugin.js.map +1 -0
  75. package/plugins/RoutePlugin.d.ts +2 -2
  76. package/plugins/RoutePlugin.js +3 -11
  77. package/plugins/RoutePlugin.js.map +1 -1
  78. package/stringifyError.d.ts +5 -0
  79. package/stringifyError.js +20 -0
  80. package/stringifyError.js.map +1 -0
  81. package/suppressPunycodeWarnings.d.ts +1 -0
  82. package/suppressPunycodeWarnings.js +10 -0
  83. package/suppressPunycodeWarnings.js.map +1 -0
  84. package/types.d.ts +9 -7
  85. package/types.js +1 -19
  86. package/types.js.map +1 -1
package/fastify.d.ts CHANGED
@@ -1,9 +1,11 @@
1
- /// <reference types="node" />
2
- import { PluginCollection, PluginsContainer } from "@webiny/plugins/types";
3
- import { FastifyInstance, FastifyServerOptions as ServerOptions } from "fastify";
1
+ import type { PluginCollection } from "@webiny/plugins/types.js";
2
+ import { PluginsContainer } from "@webiny/plugins/types.js";
3
+ import type { FastifyInstance, FastifyServerOptions as ServerOptions } from "fastify";
4
4
  export interface CreateHandlerParams {
5
5
  plugins: PluginCollection | PluginsContainer;
6
6
  options?: ServerOptions;
7
7
  debug?: boolean;
8
8
  }
9
- export declare const createHandler: (params: CreateHandlerParams) => FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault> & PromiseLike<FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>>;
9
+ export declare const createHandler: (params: CreateHandlerParams) => FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault> & PromiseLike<FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>> & {
10
+ __linterBrands: "SafePromiseLike";
11
+ };
package/fastify.js CHANGED
@@ -1,91 +1,48 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.createHandler = void 0;
8
- var _types = require("@webiny/plugins/types");
9
- var _fastify = _interopRequireDefault(require("fastify"));
10
- var _utils = require("@webiny/utils");
11
- var _Context = require("./Context");
12
- var _error = _interopRequireDefault(require("@webiny/error"));
13
- var _RoutePlugin = require("./plugins/RoutePlugin");
14
- var _handlerClient = require("@webiny/handler-client");
15
- var _cookie = _interopRequireDefault(require("@fastify/cookie"));
16
- var _compress = _interopRequireDefault(require("@fastify/compress"));
17
- var _api = require("@webiny/api");
18
- var _BeforeHandlerPlugin = require("./plugins/BeforeHandlerPlugin");
19
- var _HandlerResultPlugin = require("./plugins/HandlerResultPlugin");
20
- var _HandlerErrorPlugin = require("./plugins/HandlerErrorPlugin");
21
- var _ModifyFastifyPlugin = require("./plugins/ModifyFastifyPlugin");
22
- var _HandlerOnRequestPlugin = require("./plugins/HandlerOnRequestPlugin");
23
- var _ResponseHeaders = require("./ResponseHeaders");
24
- var _ModifyResponseHeadersPlugin = require("./plugins/ModifyResponseHeadersPlugin");
25
- function createDefaultHeaders() {
26
- return _ResponseHeaders.ResponseHeaders.create({
27
- "content-type": "application/json; charset=utf-8",
28
- "cache-control": "no-store",
29
- "access-control-allow-origin": "*",
30
- "access-control-allow-headers": "*",
31
- "access-control-allow-methods": "OPTIONS,POST,GET,DELETE,PUT,PATCH",
32
- ...(0, _utils.getWebinyVersionHeaders)()
33
- });
34
- }
35
- const getDefaultOptionsHeaders = () => {
36
- return _ResponseHeaders.ResponseHeaders.create({
37
- "access-control-max-age": "86400",
38
- "cache-control": "public, max-age=86400"
39
- });
40
- };
41
- const getDefaultHeaders = routes => {
42
- const headers = createDefaultHeaders();
43
-
44
- /**
45
- * If we are accepting all headers, just output that one.
46
- */
47
- const keys = Object.keys(routes);
48
- const all = keys.every(key => routes[key].length > 0);
49
- if (all) {
50
- headers.set("access-control-allow-methods", "*");
51
- } else {
52
- const allowedMethods = keys.filter(type => {
53
- if (!routes[type] || !Array.isArray(routes[type])) {
54
- return false;
55
- }
56
- return routes[type].length > 0;
57
- }).sort().join(",");
58
- headers.set("access-control-allow-methods", allowedMethods);
59
- }
60
- return headers;
61
- };
62
- const stringifyError = error => {
63
- const {
64
- name,
65
- message,
66
- code,
67
- stack,
68
- data
69
- } = error;
70
- return JSON.stringify({
71
- ...error,
72
- constructorName: error.constructor?.name || "UnknownError",
73
- name: name || "No error name",
74
- message: message || "No error message",
75
- code: code || "NO_CODE",
76
- data,
77
- stack: process.env.DEBUG === "true" ? stack : "Turn on the debug flag to see the stack."
78
- });
79
- };
1
+ import { PluginsContainer } from "@webiny/plugins/types.js";
2
+ import fastify from "fastify";
3
+ import { middleware } from "@webiny/utils";
4
+ import { Context } from "./Context.js";
5
+ import WebinyError from "@webiny/error";
6
+ import { RoutePlugin } from "./plugins/RoutePlugin.js";
7
+ import { createHandlerClient } from "@webiny/handler-client";
8
+ import fastifyCookie from "@fastify/cookie";
9
+ import fastifyCompress from "@fastify/compress";
10
+ import { ContextPlugin } from "@webiny/api";
11
+ import { BeforeHandlerPlugin } from "./plugins/BeforeHandlerPlugin.js";
12
+ import { HandlerResultPlugin } from "./plugins/HandlerResultPlugin.js";
13
+ import { HandlerErrorPlugin } from "./plugins/HandlerErrorPlugin.js";
14
+ import { ModifyFastifyPlugin } from "./plugins/ModifyFastifyPlugin.js";
15
+ import { HandlerOnRequestPlugin } from "./plugins/HandlerOnRequestPlugin.js";
16
+ import { ResponseHeaders } from "./ResponseHeaders.js";
17
+ import { ModifyResponseHeadersPlugin } from "./plugins/ModifyResponseHeadersPlugin.js";
18
+ import { SetDefaultHeaders } from "./PreHandler/SetDefaultHeaders.js";
19
+ import { PreHandler } from "./PreHandler/PreHandler.js";
20
+ import { stringifyError } from "./stringifyError.js";
21
+ import { ProcessHandlerOnRequestPlugins } from "./PreHandler/ProcessHandlerOnRequestPlugins.js";
22
+ import { ProcessContextPlugins } from "./PreHandler/ProcessContextPlugins.js";
23
+ import { IfNotOptionsRequest } from "./PreHandler/IfNotOptionsRequest.js";
24
+ import { ProcessBeforeHandlerPlugins } from "./PreHandler/ProcessBeforeHandlerPlugins.js";
25
+ import { IfOptionsRequest } from "./PreHandler/IfOptionsRequest.js";
26
+ import { SendEarlyOptionsResponse } from "./PreHandler/SendEarlyOptionsResponse.js";
27
+ import { OnRequestTimeoutPlugin } from "./plugins/OnRequestTimeoutPlugin.js";
28
+ import { OnRequestResponseSendPlugin } from "./plugins/OnRequestResponseSendPlugin.js";
29
+ import { Request } from "./abstractions/Request.js";
30
+ import { Reply } from "./abstractions/Reply.js";
80
31
  const modifyResponseHeaders = (app, request, reply) => {
81
- const modifyHeaders = app.webiny.plugins.byType(_ModifyResponseHeadersPlugin.ModifyResponseHeadersPlugin.type);
82
- const headers = _ResponseHeaders.ResponseHeaders.create(reply.getHeaders());
32
+ const modifyHeaders = app.webiny.plugins.byType(ModifyResponseHeadersPlugin.type);
33
+ const replyHeaders = reply.getHeaders();
34
+ const headers = ResponseHeaders.create(replyHeaders);
83
35
  modifyHeaders.forEach(plugin => {
84
36
  plugin.modify(request, headers);
85
37
  });
86
- reply.headers(headers.getHeaders());
38
+
39
+ // Exclude 'set-cookie' header to avoid duplication.
40
+ // Cookies are managed by @fastify/cookie and calling reply.headers() with 'set-cookie' duplicates them.
41
+ const headersToSet = headers.getHeaders();
42
+ delete headersToSet["set-cookie"];
43
+ reply.headers(headersToSet);
87
44
  };
88
- const createHandler = params => {
45
+ export const createHandler = params => {
89
46
  const definedRoutes = {
90
47
  POST: [],
91
48
  GET: [],
@@ -102,11 +59,14 @@ const createHandler = params => {
102
59
  PROPPATCH: [],
103
60
  SEARCH: [],
104
61
  TRACE: [],
105
- UNLOCK: []
62
+ UNLOCK: [],
63
+ REPORT: [],
64
+ MKCALENDAR: []
106
65
  };
107
66
  const throwOnDefinedRoute = (type, path, options) => {
108
67
  if (type === "ALL") {
109
- const all = Object.keys(definedRoutes).find(key => {
68
+ const all = Object.keys(definedRoutes).find(k => {
69
+ const key = k.toUpperCase();
110
70
  const routes = definedRoutes[key];
111
71
  return routes.includes(path);
112
72
  });
@@ -115,7 +75,7 @@ const createHandler = params => {
115
75
  }
116
76
  console.error(`Error while registering onAll route. One of the routes is already defined.`);
117
77
  console.error(JSON.stringify(all));
118
- throw new _error.default(`You cannot override a route with onAll() method, please remove unnecessary route from the system.`, "OVERRIDE_ROUTE_ERROR", {
78
+ throw new WebinyError(`You cannot override a route with onAll() method, please remove unnecessary route from the system.`, "OVERRIDE_ROUTE_ERROR", {
119
79
  type,
120
80
  path
121
81
  });
@@ -125,12 +85,13 @@ const createHandler = params => {
125
85
  return;
126
86
  }
127
87
  console.error(`Error while trying to override route: [${type}] ${path}`);
128
- throw new _error.default(`When you are trying to override existing route, you must send "override" parameter when adding that route.`, "OVERRIDE_ROUTE_ERROR", {
88
+ throw new WebinyError(`When you are trying to override existing route, you must send "override" parameter when adding that route.`, "OVERRIDE_ROUTE_ERROR", {
129
89
  type,
130
90
  path
131
91
  });
132
92
  };
133
- const addDefinedRoute = (type, path) => {
93
+ const addDefinedRoute = (input, path) => {
94
+ const type = input.toUpperCase();
134
95
  if (!definedRoutes[type]) {
135
96
  return;
136
97
  } else if (definedRoutes[type].includes(path)) {
@@ -142,17 +103,16 @@ const createHandler = params => {
142
103
  /**
143
104
  * We must attach the server to our internal context if we want to have it accessible.
144
105
  */
145
- const app = (0, _fastify.default)({
106
+ const app = fastify({
146
107
  bodyLimit: 536870912,
147
108
  // 512MB
148
-
149
- // TODO: in the near future, pass own Pino logger instance.
150
- logger: false,
109
+ disableRequestLogging: true,
110
+ allowErrorHandlerOverride: true,
151
111
  ...(params.options || {})
152
112
  });
153
113
 
154
114
  /**
155
- * We need to register routes in our system so we can output headers later on and dissallow overriding routes.
115
+ * We need to register routes in our system to output headers later on, and disallow route overriding.
156
116
  */
157
117
  app.addHook("onRoute", route => {
158
118
  const method = route.method;
@@ -173,7 +133,7 @@ const createHandler = params => {
173
133
  *
174
134
  * https://github.com/fastify/fastify-cookie
175
135
  */
176
- app.register(_cookie.default, {
136
+ app.register(fastifyCookie, {
177
137
  parseOptions: {} // options for parsing cookies
178
138
  });
179
139
  /**
@@ -181,7 +141,7 @@ const createHandler = params => {
181
141
  *
182
142
  * https://github.com/fastify/fastify-compress
183
143
  */
184
- app.register(_compress.default, {
144
+ app.register(fastifyCompress, {
185
145
  global: true,
186
146
  threshold: 1024,
187
147
  onUnsupportedEncoding: (encoding, _, reply) => {
@@ -229,15 +189,15 @@ const createHandler = params => {
229
189
  }
230
190
  };
231
191
  let context;
232
- const plugins = new _types.PluginsContainer([
192
+ const plugins = new PluginsContainer([
233
193
  /**
234
194
  * We must have handlerClient by default.
235
195
  * And it must be one of the first context plugins applied.
236
196
  */
237
- (0, _handlerClient.createHandlerClient)()]);
197
+ createHandlerClient()]);
238
198
  plugins.merge(params.plugins || []);
239
199
  try {
240
- context = new _Context.Context({
200
+ context = new Context({
241
201
  plugins,
242
202
  /**
243
203
  * Inserted via webpack at build time.
@@ -257,95 +217,68 @@ const createHandler = params => {
257
217
  app.decorate("webiny", context);
258
218
 
259
219
  /**
260
- * On every request we add default headers, which can be changed later.
261
- * Also, if it is an options request, we skip everything after this hook and output options headers.
220
+ * To prevent Unsupported Media Type errors on OPTIONS requests with a body,
221
+ * we need to have a custom parser
262
222
  */
263
- app.addHook("onRequest", async (request, reply) => {
264
- const isOptionsRequest = request.method === "OPTIONS";
265
- /**
266
- * Our default headers are always set. Users can override them.
267
- */
268
- const defaultHeaders = getDefaultHeaders(definedRoutes);
269
- const initialHeaders = isOptionsRequest ? defaultHeaders.merge(getDefaultOptionsHeaders()) : defaultHeaders;
270
- reply.headers(initialHeaders.getHeaders());
271
- /**
272
- * Users can define their own custom handlers for the onRequest event - so let's run them first.
273
- */
274
- const plugins = app.webiny.plugins.byType(_HandlerOnRequestPlugin.HandlerOnRequestPlugin.type);
275
- let name;
276
- try {
277
- for (const plugin of plugins) {
278
- name = plugin.name;
279
- const result = await plugin.exec(request, reply);
280
- if (result === false) {
281
- return;
282
- }
283
- }
284
- } catch (ex) {
285
- console.error(`Error while running the "HandlerOnRequestPlugin" ${name ? `(${name})` : ""} plugin in the onRequest hook.`);
286
- console.error(stringifyError(ex));
287
- throw ex;
288
- }
289
- /**
290
- * When we receive the OPTIONS request, we end it before it goes any further as there is no need for anything to run after this - at least for our use cases.
291
- *
292
- * Users can prevent this by creating their own HandlerOnRequestPlugin and returning false as the result of the callable.
293
- */
294
- if (!isOptionsRequest) {
295
- return;
296
- }
297
- if (reply.sent) {
298
- /**
299
- * At this point throwing an exception will not do anything with the response. So just log it.
300
- */
301
- console.error(JSON.stringify({
302
- message: `Output was already sent. Please check custom plugins of type "HandlerOnRequestPlugin".`,
303
- explanation: "This error can happen if the user plugin ended the reply, but did not return false as response."
304
- }));
223
+ app.addContentTypeParser("application/json", {
224
+ parseAs: "string",
225
+ bodyLimit: 1024 * 1024
226
+ }, (req, body, done) => {
227
+ if (req.method === "OPTIONS") {
228
+ done(null, undefined);
305
229
  return;
306
230
  }
307
- modifyResponseHeaders(app, request, reply);
308
- reply.code(204).send("").hijack();
309
- });
310
- app.addHook("preParsing", async (request, reply) => {
311
- app.webiny.request = request;
312
- app.webiny.reply = reply;
313
- const plugins = app.webiny.plugins.byType(_api.ContextPlugin.type);
314
- let name;
315
231
  try {
316
- for (const plugin of plugins) {
317
- name = plugin.name;
318
- await plugin.apply(app.webiny);
319
- }
320
- } catch (ex) {
321
- console.error(`Error while running the "ContextPlugin" ${name ? `(${name})` : ""} plugin in the preParsing hook.`);
322
- console.error(stringifyError(ex));
323
- throw ex;
232
+ const json = typeof body === "string" ? body : body.toString("utf8");
233
+ done(null, JSON.parse(json));
234
+ } catch (err) {
235
+ done(err);
324
236
  }
325
237
  });
238
+
326
239
  /**
240
+ * With this we ensure that an undefined request body is not parsed on OPTIONS requests,
241
+ * in case there's a `content-type` header set for whatever reason.
327
242
  *
243
+ * @see https://fastify.dev/docs/latest/Reference/ContentTypeParser/#content-type-parser
328
244
  */
329
- app.addHook("preHandler", async () => {
330
- const plugins = app.webiny.plugins.byType(_BeforeHandlerPlugin.BeforeHandlerPlugin.type);
331
- let name;
332
- try {
333
- for (const plugin of plugins) {
334
- name = plugin.name;
335
- await plugin.apply(app.webiny);
336
- }
337
- } catch (ex) {
338
- console.error(`Error while running the "BeforeHandlerPlugin" ${name ? `(${name})` : ""} plugin in the preHandler hook.`);
339
- console.error(stringifyError(ex));
340
- throw ex;
245
+ app.addHook("onRequest", async request => {
246
+ if (request.method === "OPTIONS" && request.body === undefined) {
247
+ request.headers["content-type"] = undefined;
341
248
  }
342
249
  });
343
250
 
344
251
  /**
345
- *
252
+ * At this point, request body is properly parsed, and we can execute Webiny business logic.
253
+ * - set default headers
254
+ * - process `HandlerOnRequestPlugin`
255
+ * - if OPTIONS request, exit early
256
+ * - process `ContextPlugin`
257
+ * - process `BeforeHandlerPlugin`
346
258
  */
347
- const preSerialization = async (_, __, payload) => {
348
- const plugins = app.webiny.plugins.byType(_HandlerResultPlugin.HandlerResultPlugin.type);
259
+ app.addHook("preHandler", async (request, reply) => {
260
+ app.webiny.request = request;
261
+ app.webiny.reply = reply;
262
+
263
+ // Bind request and reply to DI container for runtime access
264
+ if (app.webiny.container) {
265
+ app.webiny.container.registerInstance(Request, request);
266
+ app.webiny.container.registerInstance(Reply, reply);
267
+ }
268
+ /**
269
+ * Default code to 200 - so we do not need to set it again.
270
+ * Usually we set errors manually when we use reply.send.
271
+ */
272
+ reply.code(200);
273
+ const handlerOnRequestPlugins = app.webiny.plugins.byType(HandlerOnRequestPlugin.type);
274
+ const contextPlugins = app.webiny.plugins.byType(ContextPlugin.type);
275
+ const beforeHandlerPlugins = app.webiny.plugins.byType(BeforeHandlerPlugin.type);
276
+ const modifyHeadersPlugins = app.webiny.plugins.byType(ModifyResponseHeadersPlugin.type);
277
+ const preHandler = new PreHandler([new SetDefaultHeaders(definedRoutes), new ProcessHandlerOnRequestPlugins(handlerOnRequestPlugins), new IfNotOptionsRequest([new ProcessContextPlugins(app.webiny, contextPlugins), new ProcessBeforeHandlerPlugins(app.webiny, beforeHandlerPlugins)]), new IfOptionsRequest([new SendEarlyOptionsResponse(modifyHeadersPlugins)])]);
278
+ await preHandler.execute(request, reply, app.webiny);
279
+ });
280
+ app.addHook("preSerialization", async (_, __, payload) => {
281
+ const plugins = app.webiny.plugins.byType(HandlerResultPlugin.type);
349
282
  let name;
350
283
  try {
351
284
  for (const plugin of plugins) {
@@ -358,9 +291,31 @@ const createHandler = params => {
358
291
  throw ex;
359
292
  }
360
293
  return payload;
361
- };
362
- app.addHook("preSerialization", preSerialization);
363
- app.setErrorHandler(async (error, request, reply) => {
294
+ });
295
+ app.setErrorHandler(async (error, _, reply) => {
296
+ /**
297
+ * IMPORTANT! Do not send anything if reply was already sent.
298
+ */
299
+ if (reply.sent) {
300
+ console.warn("Reply already sent, cannot send the result (handler:setErrorHandler).");
301
+ return reply;
302
+ }
303
+ if (error.code?.startsWith("Authentication/")) {
304
+ return reply.status(401).headers({
305
+ "Cache-Control": "no-store"
306
+ }).send(JSON.stringify({
307
+ message: error.message,
308
+ code: error.code
309
+ }));
310
+ }
311
+ if (error.code === "Tenancy/TenantDisabled") {
312
+ return reply.status(503).headers({
313
+ "Cache-Control": "no-store"
314
+ }).send(JSON.stringify({
315
+ message: error.message,
316
+ code: error.code
317
+ }));
318
+ }
364
319
  return reply.status(500).headers({
365
320
  "Cache-Control": "no-store"
366
321
  }).send(
@@ -374,24 +329,37 @@ const createHandler = params => {
374
329
  }));
375
330
  });
376
331
  app.addHook("onError", async (_, reply, error) => {
377
- const plugins = app.webiny.plugins.byType(_HandlerErrorPlugin.HandlerErrorPlugin.type);
332
+ const plugins = app.webiny.plugins.byType(HandlerErrorPlugin.type);
378
333
  /**
379
334
  * Log error to cloud, as these can be extremely annoying to debug!
380
335
  */
381
- console.error("@webiny/handler");
382
- console.error(stringifyError(error));
383
- reply.status(500).headers({
384
- "Cache-Control": "no-store"
385
- }).send(
336
+ console.error("Logging error in @webiny/handler");
337
+ try {
338
+ console.error(stringifyError(error));
339
+ } catch (ex) {
340
+ console.warn("Could not stringify error:");
341
+ console.log(error);
342
+ console.error("Stringify error:", ex);
343
+ }
386
344
  /**
387
- * When we are sending the error in the response, we cannot send the whole error object, as it might contain some sensitive data.
345
+ * IMPORTANT! Do not send anything if reply was already sent.
388
346
  */
389
- JSON.stringify({
390
- message: error.message,
391
- code: error.code,
392
- data: error.data
393
- }));
394
- const handler = (0, _utils.middleware)(plugins.map(pl => {
347
+ if (!reply.sent) {
348
+ reply.status(500).headers({
349
+ "Cache-Control": "no-store"
350
+ }).send(
351
+ /**
352
+ * When we are sending the error in the response, we cannot send the whole error object, as it might contain some sensitive data.
353
+ */
354
+ JSON.stringify({
355
+ message: error.message,
356
+ code: error.code,
357
+ data: error.data
358
+ }));
359
+ } else {
360
+ console.warn("Reply already sent, cannot send the result (handler:addHook:onError).");
361
+ }
362
+ const handler = middleware(plugins.map(pl => {
395
363
  return (context, error, next) => {
396
364
  return pl.handle(context, error, next);
397
365
  };
@@ -403,8 +371,13 @@ const createHandler = params => {
403
371
  /**
404
372
  * Apply response headers modifier plugins.
405
373
  */
406
- app.addHook("onSend", async (request, reply, payload) => {
374
+ app.addHook("onSend", async (request, reply, input) => {
407
375
  modifyResponseHeaders(app, request, reply);
376
+ const plugins = app.webiny.plugins.byType(OnRequestResponseSendPlugin.type);
377
+ let payload = input;
378
+ for (const plugin of plugins) {
379
+ payload = await plugin.exec(request, reply, payload);
380
+ }
408
381
  return payload;
409
382
  });
410
383
 
@@ -414,14 +387,18 @@ const createHandler = params => {
414
387
  app.addHook("onResponse", async () => {
415
388
  await context.benchmark.output();
416
389
  });
417
- app.addHook("onTimeout", async () => {
390
+ app.addHook("onTimeout", async (request, reply) => {
391
+ const plugins = app.webiny.plugins.byType(OnRequestTimeoutPlugin.type);
392
+ for (const plugin of plugins) {
393
+ await plugin.exec(request, reply);
394
+ }
418
395
  await context.benchmark.output();
419
396
  });
420
397
 
421
398
  /**
422
399
  * With these plugins we give users possibility to do anything they want on our fastify instance.
423
400
  */
424
- const modifyPlugins = app.webiny.plugins.byType(_ModifyFastifyPlugin.ModifyFastifyPlugin.type);
401
+ const modifyPlugins = app.webiny.plugins.byType(ModifyFastifyPlugin.type);
425
402
  let modifyFastifyPluginName;
426
403
  try {
427
404
  for (const plugin of modifyPlugins) {
@@ -441,7 +418,7 @@ const createHandler = params => {
441
418
  *
442
419
  * Routes are registered in fastify but events must be handled in package which implements cloud specific methods.
443
420
  */
444
- const routePlugins = app.webiny.plugins.byType(_RoutePlugin.RoutePlugin.type);
421
+ const routePlugins = app.webiny.plugins.byType(RoutePlugin.type);
445
422
 
446
423
  /**
447
424
  * Add routes to the system.
@@ -462,6 +439,5 @@ const createHandler = params => {
462
439
  }
463
440
  return app;
464
441
  };
465
- exports.createHandler = createHandler;
466
442
 
467
443
  //# sourceMappingURL=fastify.js.map