@webiny/handler 0.0.0-unstable.2af142b57e → 0.0.0-unstable.39223eb3c1

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 (82) hide show
  1. package/Context.d.ts +18 -10
  2. package/Context.js +19 -13
  3. package/Context.js.map +1 -1
  4. package/PreHandler/IPreHandler.d.ts +9 -0
  5. package/PreHandler/IPreHandler.js +13 -0
  6. package/PreHandler/IPreHandler.js.map +1 -0
  7. package/PreHandler/IfNotOptionsRequest.d.ts +9 -0
  8. package/PreHandler/IfNotOptionsRequest.js +28 -0
  9. package/PreHandler/IfNotOptionsRequest.js.map +1 -0
  10. package/PreHandler/IfOptionsRequest.d.ts +9 -0
  11. package/PreHandler/IfOptionsRequest.js +28 -0
  12. package/PreHandler/IfOptionsRequest.js.map +1 -0
  13. package/PreHandler/PreHandler.d.ts +9 -0
  14. package/PreHandler/PreHandler.js +24 -0
  15. package/PreHandler/PreHandler.js.map +1 -0
  16. package/PreHandler/ProcessBeforeHandlerPlugins.d.ts +10 -0
  17. package/PreHandler/ProcessBeforeHandlerPlugins.js +31 -0
  18. package/PreHandler/ProcessBeforeHandlerPlugins.js.map +1 -0
  19. package/PreHandler/ProcessContextPlugins.d.ts +10 -0
  20. package/PreHandler/ProcessContextPlugins.js +31 -0
  21. package/PreHandler/ProcessContextPlugins.js.map +1 -0
  22. package/PreHandler/ProcessHandlerOnRequestPlugins.d.ts +10 -0
  23. package/PreHandler/ProcessHandlerOnRequestPlugins.js +33 -0
  24. package/PreHandler/ProcessHandlerOnRequestPlugins.js.map +1 -0
  25. package/PreHandler/SendEarlyOptionsResponse.d.ts +9 -0
  26. package/PreHandler/SendEarlyOptionsResponse.js +38 -0
  27. package/PreHandler/SendEarlyOptionsResponse.js.map +1 -0
  28. package/PreHandler/SetDefaultHeaders.d.ts +9 -0
  29. package/PreHandler/SetDefaultHeaders.js +64 -0
  30. package/PreHandler/SetDefaultHeaders.js.map +1 -0
  31. package/ResponseHeaders.d.ts +25 -0
  32. package/ResponseHeaders.js +46 -0
  33. package/ResponseHeaders.js.map +1 -0
  34. package/fastify.d.ts +6 -4
  35. package/fastify.js +173 -190
  36. package/fastify.js.map +1 -1
  37. package/index.d.ts +6 -1
  38. package/index.js +48 -12
  39. package/index.js.map +1 -1
  40. package/package.json +15 -24
  41. package/plugins/BeforeHandlerPlugin.d.ts +1 -1
  42. package/plugins/BeforeHandlerPlugin.js +4 -5
  43. package/plugins/BeforeHandlerPlugin.js.map +1 -1
  44. package/plugins/EventPlugin.d.ts +1 -1
  45. package/plugins/EventPlugin.js +4 -5
  46. package/plugins/EventPlugin.js.map +1 -1
  47. package/plugins/HandlerErrorPlugin.d.ts +6 -3
  48. package/plugins/HandlerErrorPlugin.js +4 -5
  49. package/plugins/HandlerErrorPlugin.js.map +1 -1
  50. package/plugins/HandlerOnRequestPlugin.d.ts +9 -8
  51. package/plugins/HandlerOnRequestPlugin.js +13 -7
  52. package/plugins/HandlerOnRequestPlugin.js.map +1 -1
  53. package/plugins/HandlerResultPlugin.d.ts +1 -1
  54. package/plugins/HandlerResultPlugin.js +4 -5
  55. package/plugins/HandlerResultPlugin.js.map +1 -1
  56. package/plugins/ModifyFastifyPlugin.d.ts +1 -1
  57. package/plugins/ModifyFastifyPlugin.js +4 -5
  58. package/plugins/ModifyFastifyPlugin.js.map +1 -1
  59. package/plugins/ModifyResponseHeadersPlugin.d.ts +14 -0
  60. package/plugins/ModifyResponseHeadersPlugin.js +24 -0
  61. package/plugins/ModifyResponseHeadersPlugin.js.map +1 -0
  62. package/plugins/OnRequestResponseSendPlugin.d.ts +26 -0
  63. package/plugins/OnRequestResponseSendPlugin.js +40 -0
  64. package/plugins/OnRequestResponseSendPlugin.js.map +1 -0
  65. package/plugins/OnRequestTimeoutPlugin.d.ts +12 -0
  66. package/plugins/OnRequestTimeoutPlugin.js +24 -0
  67. package/plugins/OnRequestTimeoutPlugin.js.map +1 -0
  68. package/plugins/RoutePlugin.d.ts +1 -1
  69. package/plugins/RoutePlugin.js +4 -5
  70. package/plugins/RoutePlugin.js.map +1 -1
  71. package/stringifyError.d.ts +5 -0
  72. package/stringifyError.js +27 -0
  73. package/stringifyError.js.map +1 -0
  74. package/suppressPunycodeWarnings.d.ts +4 -0
  75. package/suppressPunycodeWarnings.js +11 -0
  76. package/suppressPunycodeWarnings.js.map +1 -0
  77. package/types.d.ts +10 -13
  78. package/types.js +4 -7
  79. package/types.js.map +1 -1
  80. package/middleware.d.ts +0 -4
  81. package/middleware.js +0 -45
  82. package/middleware.js.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"names":["isFunction","setter","ResponseHeaders","headers","Map","constructor","initialHeaders","Object","keys","forEach","key","set","header","previousValue","get","newValue","merge","create","getHeaders","fromEntries","exports"],"sources":["ResponseHeaders.ts"],"sourcesContent":["import type * as http from \"http\";\n\ntype ExtraHeaders = {\n \"content-type\"?: string | undefined;\n \"x-webiny-version\"?: http.OutgoingHttpHeader | undefined;\n};\n\ntype AllHeaders = http.OutgoingHttpHeaders & ExtraHeaders;\n\nexport type StandardHeaderValue = http.OutgoingHttpHeader | undefined;\n\n// Extract known standard headers, and remove all non-string keys.\nexport type StandardHeaders = {\n [K in keyof AllHeaders as string extends K\n ? never\n : number extends K\n ? never\n : K]: http.OutgoingHttpHeaders[K];\n} & {\n [name: string]: StandardHeaderValue;\n};\n\nfunction isFunction<T>(setter: unknown): setter is (value: T) => T {\n return typeof setter === \"function\";\n}\n\ntype Setter<T> = ((value: T) => T) | T;\n\nexport class ResponseHeaders {\n private readonly headers = new Map<keyof StandardHeaders, StandardHeaderValue>();\n\n private constructor(initialHeaders?: StandardHeaders) {\n if (initialHeaders) {\n (Object.keys(initialHeaders) as Array<keyof StandardHeaders>).forEach(key => {\n this.headers.set(key, initialHeaders[key]);\n });\n }\n }\n\n set<T extends keyof StandardHeaders>(header: T, setter: Setter<StandardHeaders[T]>) {\n if (isFunction<StandardHeaders[T]>(setter)) {\n const previousValue = this.headers.get(header) as StandardHeaders[T];\n const newValue = setter(previousValue);\n this.headers.set(header, newValue);\n return this;\n }\n\n this.headers.set(header, setter);\n\n return this;\n }\n\n merge(headers: ResponseHeaders) {\n return ResponseHeaders.create({ ...this.getHeaders(), ...headers.getHeaders() });\n }\n\n getHeaders() {\n return Object.fromEntries(this.headers);\n }\n\n static create(initialHeaders?: StandardHeaders) {\n return new ResponseHeaders(initialHeaders);\n }\n}\n"],"mappings":";;;;;;AAWA;;AAWA,SAASA,UAAUA,CAAIC,MAAe,EAA6B;EAC/D,OAAO,OAAOA,MAAM,KAAK,UAAU;AACvC;AAIO,MAAMC,eAAe,CAAC;EACRC,OAAO,GAAG,IAAIC,GAAG,CAA6C,CAAC;EAExEC,WAAWA,CAACC,cAAgC,EAAE;IAClD,IAAIA,cAAc,EAAE;MACfC,MAAM,CAACC,IAAI,CAACF,cAAc,CAAC,CAAkCG,OAAO,CAACC,GAAG,IAAI;QACzE,IAAI,CAACP,OAAO,CAACQ,GAAG,CAACD,GAAG,EAAEJ,cAAc,CAACI,GAAG,CAAC,CAAC;MAC9C,CAAC,CAAC;IACN;EACJ;EAEAC,GAAGA,CAAkCC,MAAS,EAAEX,MAAkC,EAAE;IAChF,IAAID,UAAU,CAAqBC,MAAM,CAAC,EAAE;MACxC,MAAMY,aAAa,GAAG,IAAI,CAACV,OAAO,CAACW,GAAG,CAACF,MAAM,CAAuB;MACpE,MAAMG,QAAQ,GAAGd,MAAM,CAACY,aAAa,CAAC;MACtC,IAAI,CAACV,OAAO,CAACQ,GAAG,CAACC,MAAM,EAAEG,QAAQ,CAAC;MAClC,OAAO,IAAI;IACf;IAEA,IAAI,CAACZ,OAAO,CAACQ,GAAG,CAACC,MAAM,EAAEX,MAAM,CAAC;IAEhC,OAAO,IAAI;EACf;EAEAe,KAAKA,CAACb,OAAwB,EAAE;IAC5B,OAAOD,eAAe,CAACe,MAAM,CAAC;MAAE,GAAG,IAAI,CAACC,UAAU,CAAC,CAAC;MAAE,GAAGf,OAAO,CAACe,UAAU,CAAC;IAAE,CAAC,CAAC;EACpF;EAEAA,UAAUA,CAAA,EAAG;IACT,OAAOX,MAAM,CAACY,WAAW,CAAC,IAAI,CAAChB,OAAO,CAAC;EAC3C;EAEA,OAAOc,MAAMA,CAACX,cAAgC,EAAE;IAC5C,OAAO,IAAIJ,eAAe,CAACI,cAAc,CAAC;EAC9C;AACJ;AAACc,OAAA,CAAAlB,eAAA,GAAAA,eAAA","ignoreList":[]}
package/fastify.d.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  /// <reference types="node" />
2
- import { PluginCollection } from "@webiny/plugins/types";
3
- import { FastifyServerOptions as ServerOptions } from "fastify";
2
+ import type { PluginCollection } from "@webiny/plugins/types";
3
+ import { PluginsContainer } from "@webiny/plugins/types";
4
+ import type { FastifyInstance, FastifyServerOptions as ServerOptions } from "fastify";
4
5
  export interface CreateHandlerParams {
5
- plugins: PluginCollection;
6
+ plugins: PluginCollection | PluginsContainer;
6
7
  options?: ServerOptions;
8
+ debug?: boolean;
7
9
  }
8
- export declare const createHandler: (params: CreateHandlerParams) => import("fastify").FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault> & PromiseLike<import("fastify").FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>>;
10
+ 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>>;
package/fastify.js CHANGED
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
7
  exports.createHandler = void 0;
8
- var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
8
+ var _types = require("@webiny/plugins/types");
9
9
  var _fastify = _interopRequireDefault(require("fastify"));
10
10
  var _utils = require("@webiny/utils");
11
11
  var _Context = require("./Context");
@@ -14,61 +14,33 @@ var _RoutePlugin = require("./plugins/RoutePlugin");
14
14
  var _handlerClient = require("@webiny/handler-client");
15
15
  var _cookie = _interopRequireDefault(require("@fastify/cookie"));
16
16
  var _compress = _interopRequireDefault(require("@fastify/compress"));
17
- var _middleware = require("./middleware");
18
17
  var _api = require("@webiny/api");
19
18
  var _BeforeHandlerPlugin = require("./plugins/BeforeHandlerPlugin");
20
19
  var _HandlerResultPlugin = require("./plugins/HandlerResultPlugin");
21
20
  var _HandlerErrorPlugin = require("./plugins/HandlerErrorPlugin");
22
21
  var _ModifyFastifyPlugin = require("./plugins/ModifyFastifyPlugin");
23
22
  var _HandlerOnRequestPlugin = require("./plugins/HandlerOnRequestPlugin");
24
- const DEFAULT_HEADERS = (0, _objectSpread2.default)({
25
- "Cache-Control": "no-store",
26
- "Content-Type": "application/json; charset=utf-8",
27
- "Access-Control-Allow-Origin": "*",
28
- "Access-Control-Allow-Headers": "*",
29
- "Access-Control-Allow-Methods": "OPTIONS,POST,GET,DELETE,PUT,PATCH"
30
- }, (0, _utils.getWebinyVersionHeaders)());
31
- const getDefaultHeaders = routes => {
32
- /**
33
- * If we are accepting all headers, just output that one.
34
- */
35
- const keys = Object.keys(routes);
36
- const all = keys.every(key => routes[key].length > 0);
37
- if (all) {
38
- return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, DEFAULT_HEADERS), {}, {
39
- "Access-Control-Allow-Methods": "*"
40
- });
41
- }
42
- return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, DEFAULT_HEADERS), {}, {
43
- "Access-Control-Allow-Methods": keys.filter(type => {
44
- if (!routes[type] || Array.isArray(routes[type]) === false) {
45
- return false;
46
- }
47
- return routes[type].length > 0;
48
- }).sort().join(",")
23
+ var _ResponseHeaders = require("./ResponseHeaders");
24
+ var _ModifyResponseHeadersPlugin = require("./plugins/ModifyResponseHeadersPlugin");
25
+ var _SetDefaultHeaders = require("./PreHandler/SetDefaultHeaders");
26
+ var _PreHandler = require("./PreHandler/PreHandler");
27
+ var _stringifyError = require("./stringifyError");
28
+ var _ProcessHandlerOnRequestPlugins = require("./PreHandler/ProcessHandlerOnRequestPlugins");
29
+ var _ProcessContextPlugins = require("./PreHandler/ProcessContextPlugins");
30
+ var _IfNotOptionsRequest = require("./PreHandler/IfNotOptionsRequest");
31
+ var _ProcessBeforeHandlerPlugins = require("./PreHandler/ProcessBeforeHandlerPlugins");
32
+ var _IfOptionsRequest = require("./PreHandler/IfOptionsRequest");
33
+ var _SendEarlyOptionsResponse = require("./PreHandler/SendEarlyOptionsResponse");
34
+ var _OnRequestTimeoutPlugin = require("./plugins/OnRequestTimeoutPlugin.js");
35
+ var _OnRequestResponseSendPlugin = require("./plugins/OnRequestResponseSendPlugin.js");
36
+ const modifyResponseHeaders = (app, request, reply) => {
37
+ const modifyHeaders = app.webiny.plugins.byType(_ModifyResponseHeadersPlugin.ModifyResponseHeadersPlugin.type);
38
+ const replyHeaders = reply.getHeaders();
39
+ const headers = _ResponseHeaders.ResponseHeaders.create(replyHeaders);
40
+ modifyHeaders.forEach(plugin => {
41
+ plugin.modify(request, headers);
49
42
  });
50
- };
51
- const stringifyError = error => {
52
- var _error$constructor;
53
- const {
54
- name,
55
- message,
56
- code,
57
- stack,
58
- data
59
- } = error;
60
- return JSON.stringify((0, _objectSpread2.default)((0, _objectSpread2.default)({}, error), {}, {
61
- constructorName: ((_error$constructor = error.constructor) === null || _error$constructor === void 0 ? void 0 : _error$constructor.name) || "UnknownError",
62
- name: name || "No error name",
63
- message: message || "No error message",
64
- code: code || "NO_CODE",
65
- data,
66
- stack: process.env.DEBUG === "true" ? stack : "Turn on the debug flag to see the stack."
67
- }));
68
- };
69
- const OPTIONS_HEADERS = {
70
- "Access-Control-Max-Age": "86400",
71
- "Cache-Control": "public, max-age=86400"
43
+ reply.headers(headers.getHeaders());
72
44
  };
73
45
  const createHandler = params => {
74
46
  const definedRoutes = {
@@ -87,35 +59,39 @@ const createHandler = params => {
87
59
  PROPPATCH: [],
88
60
  SEARCH: [],
89
61
  TRACE: [],
90
- UNLOCK: []
62
+ UNLOCK: [],
63
+ REPORT: [],
64
+ MKCALENDAR: []
91
65
  };
92
66
  const throwOnDefinedRoute = (type, path, options) => {
93
67
  if (type === "ALL") {
94
- const all = Object.keys(definedRoutes).find(key => {
68
+ const all = Object.keys(definedRoutes).find(k => {
69
+ const key = k.toUpperCase();
95
70
  const routes = definedRoutes[key];
96
71
  return routes.includes(path);
97
72
  });
98
73
  if (!all) {
99
74
  return;
100
75
  }
101
- console.log(`Error while registering onAll route. One of the routes is already defined.`);
102
- console.log(JSON.stringify(all));
76
+ console.error(`Error while registering onAll route. One of the routes is already defined.`);
77
+ console.error(JSON.stringify(all));
103
78
  throw new _error.default(`You cannot override a route with onAll() method, please remove unnecessary route from the system.`, "OVERRIDE_ROUTE_ERROR", {
104
79
  type,
105
80
  path
106
81
  });
107
82
  } else if (definedRoutes[type].includes(path) === false) {
108
83
  return;
109
- } else if ((options === null || options === void 0 ? void 0 : options.override) === true) {
84
+ } else if (options?.override === true) {
110
85
  return;
111
86
  }
112
- console.log(`Error while trying to override route: [${type}] ${path}`);
87
+ console.error(`Error while trying to override route: [${type}] ${path}`);
113
88
  throw new _error.default(`When you are trying to override existing route, you must send "override" parameter when adding that route.`, "OVERRIDE_ROUTE_ERROR", {
114
89
  type,
115
90
  path
116
91
  });
117
92
  };
118
- const addDefinedRoute = (type, path) => {
93
+ const addDefinedRoute = (input, path) => {
94
+ const type = input.toUpperCase();
119
95
  if (!definedRoutes[type]) {
120
96
  return;
121
97
  } else if (definedRoutes[type].includes(path)) {
@@ -123,12 +99,19 @@ const createHandler = params => {
123
99
  }
124
100
  definedRoutes[type].push(path);
125
101
  };
102
+
126
103
  /**
127
104
  * We must attach the server to our internal context if we want to have it accessible.
128
105
  */
129
- const app = (0, _fastify.default)((0, _objectSpread2.default)({}, params.options || {}));
106
+ const app = (0, _fastify.default)({
107
+ bodyLimit: 536870912,
108
+ // 512MB
109
+ disableRequestLogging: true,
110
+ ...(params.options || {})
111
+ });
112
+
130
113
  /**
131
- * We need to register routes in our system so we can output headers later on and dissallow overriding routes.
114
+ * We need to register routes in our system to output headers later on, and disallow route overriding.
132
115
  */
133
116
  app.addHook("onRoute", route => {
134
117
  const method = route.method;
@@ -205,24 +188,25 @@ const createHandler = params => {
205
188
  }
206
189
  };
207
190
  let context;
191
+ const plugins = new _types.PluginsContainer([
192
+ /**
193
+ * We must have handlerClient by default.
194
+ * And it must be one of the first context plugins applied.
195
+ */
196
+ (0, _handlerClient.createHandlerClient)()]);
197
+ plugins.merge(params.plugins || []);
208
198
  try {
209
199
  context = new _Context.Context({
210
- plugins: [
211
- /**
212
- * We must have handlerClient by default.
213
- * And it must be one of the first context plugins applied.
214
- */
215
- (0, _handlerClient.createHandlerClient)(), ...(params.plugins || [])],
200
+ plugins,
216
201
  /**
217
- * Inserted via webpack on build time.
202
+ * Inserted via webpack at build time.
218
203
  */
219
204
  WEBINY_VERSION: process.env.WEBINY_VERSION,
220
- server: app,
221
205
  routes
222
206
  });
223
207
  } catch (ex) {
224
- console.log(`Error while constructing the Context.`);
225
- console.log(stringifyError(ex));
208
+ console.error(`Error while constructing the Context.`);
209
+ console.error((0, _stringifyError.stringifyError)(ex));
226
210
  throw ex;
227
211
  }
228
212
 
@@ -232,117 +216,41 @@ const createHandler = params => {
232
216
  app.decorate("webiny", context);
233
217
 
234
218
  /**
235
- * We have few types of triggers:
236
- * * Events - EventPlugin
237
- * * Routes - RoutePlugin
219
+ * With this we ensure that an undefined request body is not parsed on OPTIONS requests,
220
+ * in case there's a `content-type` header set for whatever reason.
238
221
  *
239
- * Routes are registered in fastify but events must be handled in package which implements cloud specific methods.
222
+ * @see https://fastify.dev/docs/latest/Reference/ContentTypeParser/#content-type-parser
240
223
  */
241
- const routePlugins = app.webiny.plugins.byType(_RoutePlugin.RoutePlugin.type);
242
-
243
- /**
244
- * Add routes to the system.
245
- */
246
- let routePluginName;
247
- try {
248
- for (const plugin of routePlugins) {
249
- routePluginName = plugin.name;
250
- plugin.cb((0, _objectSpread2.default)((0, _objectSpread2.default)({}, app.webiny.routes), {}, {
251
- context: app.webiny
252
- }));
224
+ app.addHook("onRequest", async request => {
225
+ if (request.method === "OPTIONS" && request.body === undefined) {
226
+ request.headers["content-type"] = undefined;
253
227
  }
254
- } catch (ex) {
255
- console.log(`Error while running the "RoutePlugin" ${routePluginName ? `(${routePluginName})` : ""} plugin in the beginning of the "createHandler" callable.`);
256
- console.log(stringifyError(ex));
257
- throw ex;
258
- }
228
+ });
259
229
 
260
230
  /**
261
- * On every request we add default headers, which can be changed later.
262
- * Also, if it is an options request, we skip everything after this hook and output options headers.
231
+ * At this point, request body is properly parsed, and we can execute Webiny business logic.
232
+ * - set default headers
233
+ * - process `HandlerOnRequestPlugin`
234
+ * - if OPTIONS request, exit early
235
+ * - process `ContextPlugin`
236
+ * - process `BeforeHandlerPlugin`
263
237
  */
264
- app.addHook("onRequest", async (request, reply) => {
265
- /**
266
- * Our default headers are always set. Users can override them.
267
- */
268
- const defaultHeaders = getDefaultHeaders(definedRoutes);
269
- reply.headers(defaultHeaders);
270
- /**
271
- * Users can define their own custom handlers for the onRequest event - so let's run them first.
272
- */
273
- const plugins = app.webiny.plugins.byType(_HandlerOnRequestPlugin.HandlerOnRequestPlugin.type);
274
- let name;
275
- try {
276
- for (const plugin of plugins) {
277
- name = plugin.name;
278
- const result = await plugin.exec(request, reply);
279
- if (result === false) {
280
- return;
281
- }
282
- }
283
- } catch (ex) {
284
- console.log(`Error while running the "HandlerOnRequestPlugin" ${name ? `(${name})` : ""} plugin in the onRequest hook.`);
285
- console.log(stringifyError(ex));
286
- throw ex;
287
- }
288
- /**
289
- * 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.
290
- *
291
- * Users can prevent this by creating their own HandlerOnRequestPlugin and returning false as the result of the callable.
292
- */
293
- if (request.method !== "OPTIONS") {
294
- return;
295
- }
296
- if (reply.sent) {
297
- /**
298
- * At this point throwing an exception will not do anything with the response. So just log it.
299
- */
300
- console.log(JSON.stringify({
301
- message: `Output was already sent. Please check custom plugins of type "HandlerOnRequestPlugin".`,
302
- explanation: "This error can happen if the user plugin ended the reply, but did not return false as response."
303
- }));
304
- return;
305
- }
306
- reply.headers((0, _objectSpread2.default)((0, _objectSpread2.default)({}, defaultHeaders), OPTIONS_HEADERS)).code(204).send("").hijack();
307
- });
308
- app.addHook("preParsing", async (request, reply) => {
238
+ app.addHook("preHandler", async (request, reply) => {
309
239
  app.webiny.request = request;
310
240
  app.webiny.reply = reply;
311
- const plugins = app.webiny.plugins.byType(_api.ContextPlugin.type);
312
- let name;
313
- try {
314
- for (const plugin of plugins) {
315
- name = plugin.name;
316
- await plugin.apply(app.webiny);
317
- }
318
- } catch (ex) {
319
- console.log(`Error while running the "ContextPlugin" ${name ? `(${name})` : ""} plugin in the preParsing hook.`);
320
- console.log(stringifyError(ex));
321
- throw ex;
322
- }
323
- });
324
- /**
325
- *
326
- */
327
- app.addHook("preHandler", async () => {
328
- const plugins = app.webiny.plugins.byType(_BeforeHandlerPlugin.BeforeHandlerPlugin.type);
329
- let name;
330
- try {
331
- for (const plugin of plugins) {
332
- name = plugin.name;
333
- await plugin.apply(app.webiny);
334
- }
335
- } catch (ex) {
336
- console.log(`Error while running the "BeforeHandlerPlugin" ${name ? `(${name})` : ""} plugin in the preHandler hook.`);
337
- console.log(stringifyError(ex));
338
- throw ex;
339
- }
241
+ /**
242
+ * Default code to 200 - so we do not need to set it again.
243
+ * Usually we set errors manually when we use reply.send.
244
+ */
245
+ reply.code(200);
246
+ const handlerOnRequestPlugins = app.webiny.plugins.byType(_HandlerOnRequestPlugin.HandlerOnRequestPlugin.type);
247
+ const contextPlugins = app.webiny.plugins.byType(_api.ContextPlugin.type);
248
+ const beforeHandlerPlugins = app.webiny.plugins.byType(_BeforeHandlerPlugin.BeforeHandlerPlugin.type);
249
+ const modifyHeadersPlugins = app.webiny.plugins.byType(_ModifyResponseHeadersPlugin.ModifyResponseHeadersPlugin.type);
250
+ const preHandler = new _PreHandler.PreHandler([new _SetDefaultHeaders.SetDefaultHeaders(definedRoutes), new _ProcessHandlerOnRequestPlugins.ProcessHandlerOnRequestPlugins(handlerOnRequestPlugins), new _IfNotOptionsRequest.IfNotOptionsRequest([new _ProcessContextPlugins.ProcessContextPlugins(app.webiny, contextPlugins), new _ProcessBeforeHandlerPlugins.ProcessBeforeHandlerPlugins(app.webiny, beforeHandlerPlugins)]), new _IfOptionsRequest.IfOptionsRequest([new _SendEarlyOptionsResponse.SendEarlyOptionsResponse(modifyHeadersPlugins)])]);
251
+ await preHandler.execute(request, reply, app.webiny);
340
252
  });
341
-
342
- /**
343
- *
344
- */
345
- const preSerialization = async (_, __, payload) => {
253
+ app.addHook("preSerialization", async (_, __, payload) => {
346
254
  const plugins = app.webiny.plugins.byType(_HandlerResultPlugin.HandlerResultPlugin.type);
347
255
  let name;
348
256
  try {
@@ -351,14 +259,20 @@ const createHandler = params => {
351
259
  await plugin.handle(app.webiny, payload);
352
260
  }
353
261
  } catch (ex) {
354
- console.log(`Error while running the "HandlerResultPlugin" ${name ? `(${name})` : ""} plugin in the preSerialization hook.`);
355
- console.log(stringifyError(ex));
262
+ console.error(`Error while running the "HandlerResultPlugin" ${name ? `(${name})` : ""} plugin in the preSerialization hook.`);
263
+ console.error((0, _stringifyError.stringifyError)(ex));
356
264
  throw ex;
357
265
  }
358
266
  return payload;
359
- };
360
- app.addHook("preSerialization", preSerialization);
361
- app.setErrorHandler(async (error, request, reply) => {
267
+ });
268
+ app.setErrorHandler(async (error, _, reply) => {
269
+ /**
270
+ * IMPORTANT! Do not send anything if reply was already sent.
271
+ */
272
+ if (reply.sent) {
273
+ console.warn("Reply already sent, cannot send the result (handler:setErrorHandler).");
274
+ return reply;
275
+ }
362
276
  return reply.status(500).headers({
363
277
  "Cache-Control": "no-store"
364
278
  }).send(
@@ -376,20 +290,33 @@ const createHandler = params => {
376
290
  /**
377
291
  * Log error to cloud, as these can be extremely annoying to debug!
378
292
  */
379
- console.log("@webiny/handler");
380
- console.log(stringifyError(error));
381
- reply.status(500).headers({
382
- "Cache-Control": "no-store"
383
- }).send(
293
+ console.error("Logging error in @webiny/handler");
294
+ try {
295
+ console.error((0, _stringifyError.stringifyError)(error));
296
+ } catch (ex) {
297
+ console.warn("Could not stringify error:");
298
+ console.log(error);
299
+ console.error("Stringify error:", ex);
300
+ }
384
301
  /**
385
- * When we are sending the error in the response, we cannot send the whole error object, as it might contain some sensitive data.
302
+ * IMPORTANT! Do not send anything if reply was already sent.
386
303
  */
387
- JSON.stringify({
388
- message: error.message,
389
- code: error.code,
390
- data: error.data
391
- }));
392
- const handler = (0, _middleware.middleware)(plugins.map(pl => {
304
+ if (!reply.sent) {
305
+ reply.status(500).headers({
306
+ "Cache-Control": "no-store"
307
+ }).send(
308
+ /**
309
+ * When we are sending the error in the response, we cannot send the whole error object, as it might contain some sensitive data.
310
+ */
311
+ JSON.stringify({
312
+ message: error.message,
313
+ code: error.code,
314
+ data: error.data
315
+ }));
316
+ } else {
317
+ console.warn("Reply already sent, cannot send the result (handler:addHook:onError).");
318
+ }
319
+ const handler = (0, _utils.middleware)(plugins.map(pl => {
393
320
  return (context, error, next) => {
394
321
  return pl.handle(context, error, next);
395
322
  };
@@ -398,6 +325,33 @@ const createHandler = params => {
398
325
  return reply;
399
326
  });
400
327
 
328
+ /**
329
+ * Apply response headers modifier plugins.
330
+ */
331
+ app.addHook("onSend", async (request, reply, input) => {
332
+ modifyResponseHeaders(app, request, reply);
333
+ const plugins = app.webiny.plugins.byType(_OnRequestResponseSendPlugin.OnRequestResponseSendPlugin.type);
334
+ let payload = input;
335
+ for (const plugin of plugins) {
336
+ payload = await plugin.exec(request, reply, payload);
337
+ }
338
+ return payload;
339
+ });
340
+
341
+ /**
342
+ * We need to output the benchmark results at the end of the request in both response and timeout cases
343
+ */
344
+ app.addHook("onResponse", async () => {
345
+ await context.benchmark.output();
346
+ });
347
+ app.addHook("onTimeout", async (request, reply) => {
348
+ const plugins = app.webiny.plugins.byType(_OnRequestTimeoutPlugin.OnRequestTimeoutPlugin.type);
349
+ for (const plugin of plugins) {
350
+ await plugin.exec(request, reply);
351
+ }
352
+ await context.benchmark.output();
353
+ });
354
+
401
355
  /**
402
356
  * With these plugins we give users possibility to do anything they want on our fastify instance.
403
357
  */
@@ -409,10 +363,39 @@ const createHandler = params => {
409
363
  plugin.modify(app);
410
364
  }
411
365
  } catch (ex) {
412
- console.log(`Error while running the "ModifyFastifyPlugin" ${modifyFastifyPluginName ? `(${modifyFastifyPluginName})` : ""} plugin in the end of the "createHandler" callable.`);
413
- console.log(stringifyError(ex));
366
+ console.error(`Error while running the "ModifyFastifyPlugin" ${modifyFastifyPluginName ? `(${modifyFastifyPluginName})` : ""} plugin in the end of the "createHandler" callable.`);
367
+ console.error((0, _stringifyError.stringifyError)(ex));
368
+ throw ex;
369
+ }
370
+
371
+ /**
372
+ * We have few types of triggers:
373
+ * * Events - EventPlugin
374
+ * * Routes - RoutePlugin
375
+ *
376
+ * Routes are registered in fastify but events must be handled in package which implements cloud specific methods.
377
+ */
378
+ const routePlugins = app.webiny.plugins.byType(_RoutePlugin.RoutePlugin.type);
379
+
380
+ /**
381
+ * Add routes to the system.
382
+ */
383
+ let routePluginName;
384
+ try {
385
+ for (const plugin of routePlugins) {
386
+ routePluginName = plugin.name;
387
+ plugin.cb({
388
+ ...app.webiny.routes,
389
+ context: app.webiny
390
+ });
391
+ }
392
+ } catch (ex) {
393
+ console.error(`Error while running the "RoutePlugin" ${routePluginName ? `(${routePluginName})` : ""} plugin in the beginning of the "createHandler" callable.`);
394
+ console.error((0, _stringifyError.stringifyError)(ex));
414
395
  throw ex;
415
396
  }
416
397
  return app;
417
398
  };
418
- exports.createHandler = createHandler;
399
+ exports.createHandler = createHandler;
400
+
401
+ //# sourceMappingURL=fastify.js.map