@webiny/handler 0.0.0-unstable.6f45466a1d → 0.0.0-unstable.7be00a75a9

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 (70) hide show
  1. package/Context.js +10 -23
  2. package/Context.js.map +1 -1
  3. package/PreHandler/IPreHandler.js +5 -4
  4. package/PreHandler/IPreHandler.js.map +1 -1
  5. package/PreHandler/IfNotOptionsRequest.js +12 -15
  6. package/PreHandler/IfNotOptionsRequest.js.map +1 -1
  7. package/PreHandler/IfOptionsRequest.js +12 -15
  8. package/PreHandler/IfOptionsRequest.js.map +1 -1
  9. package/PreHandler/PreHandler.js +11 -12
  10. package/PreHandler/PreHandler.js.map +1 -1
  11. package/PreHandler/ProcessBeforeHandlerPlugins.js +19 -18
  12. package/PreHandler/ProcessBeforeHandlerPlugins.js.map +1 -1
  13. package/PreHandler/ProcessContextPlugins.js +19 -18
  14. package/PreHandler/ProcessContextPlugins.js.map +1 -1
  15. package/PreHandler/ProcessHandlerOnRequestPlugins.js +18 -19
  16. package/PreHandler/ProcessHandlerOnRequestPlugins.js.map +1 -1
  17. package/PreHandler/RegisterExtensions.d.ts +10 -0
  18. package/PreHandler/RegisterExtensions.js +13 -0
  19. package/PreHandler/RegisterExtensions.js.map +1 -0
  20. package/PreHandler/SendEarlyOptionsResponse.js +20 -25
  21. package/PreHandler/SendEarlyOptionsResponse.js.map +1 -1
  22. package/PreHandler/SetDefaultHeaders.js +37 -48
  23. package/PreHandler/SetDefaultHeaders.js.map +1 -1
  24. package/ResponseHeaders.js +29 -32
  25. package/ResponseHeaders.js.map +1 -1
  26. package/abstractions/Reply.js +2 -1
  27. package/abstractions/Reply.js.map +1 -1
  28. package/abstractions/Request.js +2 -1
  29. package/abstractions/Request.js.map +1 -1
  30. package/abstractions/Route.d.ts +25 -0
  31. package/abstractions/Route.js +30 -0
  32. package/abstractions/Route.js.map +1 -0
  33. package/exports/api.d.ts +1 -0
  34. package/exports/api.js +1 -0
  35. package/fastify.js +279 -403
  36. package/fastify.js.map +1 -1
  37. package/index.d.ts +2 -0
  38. package/index.js +2 -3
  39. package/package.json +20 -16
  40. package/plugins/BeforeHandlerPlugin.js +13 -14
  41. package/plugins/BeforeHandlerPlugin.js.map +1 -1
  42. package/plugins/EventPlugin.js +10 -17
  43. package/plugins/EventPlugin.js.map +1 -1
  44. package/plugins/HandlerErrorPlugin.js +13 -12
  45. package/plugins/HandlerErrorPlugin.js.map +1 -1
  46. package/plugins/HandlerOnRequestPlugin.js +13 -20
  47. package/plugins/HandlerOnRequestPlugin.js.map +1 -1
  48. package/plugins/HandlerResultPlugin.js +13 -12
  49. package/plugins/HandlerResultPlugin.js.map +1 -1
  50. package/plugins/ModifyFastifyPlugin.js +13 -12
  51. package/plugins/ModifyFastifyPlugin.js.map +1 -1
  52. package/plugins/ModifyResponseHeadersPlugin.js +14 -11
  53. package/plugins/ModifyResponseHeadersPlugin.js.map +1 -1
  54. package/plugins/OnRequestResponseSendPlugin.js +13 -27
  55. package/plugins/OnRequestResponseSendPlugin.js.map +1 -1
  56. package/plugins/OnRequestTimeoutPlugin.js +13 -12
  57. package/plugins/OnRequestTimeoutPlugin.js.map +1 -1
  58. package/plugins/RegisterExtensionPlugin.d.ts +12 -0
  59. package/plugins/RegisterExtensionPlugin.js +16 -0
  60. package/plugins/RegisterExtensionPlugin.js.map +1 -0
  61. package/plugins/RoutePlugin.js +10 -9
  62. package/plugins/RoutePlugin.js.map +1 -1
  63. package/stringifyError.js +12 -17
  64. package/stringifyError.js.map +1 -1
  65. package/suppressPunycodeWarnings.js +3 -6
  66. package/suppressPunycodeWarnings.js.map +1 -1
  67. package/types.d.ts +2 -2
  68. package/types.js +0 -2
  69. package/index.js.map +0 -1
  70. package/types.js.map +0 -1
package/fastify.js CHANGED
@@ -2,11 +2,10 @@ import { PluginsContainer } from "@webiny/plugins/types.js";
2
2
  import fastify from "fastify";
3
3
  import { middleware } from "@webiny/utils";
4
4
  import { Context } from "./Context.js";
5
- import WebinyError from "@webiny/error";
5
+ import _webiny_error from "@webiny/error";
6
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";
7
+ import cookie from "@fastify/cookie";
8
+ import compress from "@fastify/compress";
10
9
  import { ContextPlugin } from "@webiny/api";
11
10
  import { BeforeHandlerPlugin } from "./plugins/BeforeHandlerPlugin.js";
12
11
  import { HandlerResultPlugin } from "./plugins/HandlerResultPlugin.js";
@@ -28,416 +27,293 @@ import { OnRequestTimeoutPlugin } from "./plugins/OnRequestTimeoutPlugin.js";
28
27
  import { OnRequestResponseSendPlugin } from "./plugins/OnRequestResponseSendPlugin.js";
29
28
  import { Request } from "./abstractions/Request.js";
30
29
  import { Reply } from "./abstractions/Reply.js";
31
- const modifyResponseHeaders = (app, request, reply) => {
32
- const modifyHeaders = app.webiny.plugins.byType(ModifyResponseHeadersPlugin.type);
33
- const replyHeaders = reply.getHeaders();
34
- const headers = ResponseHeaders.create(replyHeaders);
35
- modifyHeaders.forEach(plugin => {
36
- plugin.modify(request, headers);
37
- });
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);
30
+ import { RegisterExtensionPlugin } from "./plugins/RegisterExtensionPlugin.js";
31
+ import { RegisterExtensions } from "./PreHandler/RegisterExtensions.js";
32
+ const modifyResponseHeaders = (app, request, reply)=>{
33
+ const modifyHeaders = app.webiny.plugins.byType(ModifyResponseHeadersPlugin.type);
34
+ const replyHeaders = reply.getHeaders();
35
+ const headers = ResponseHeaders.create(replyHeaders);
36
+ modifyHeaders.forEach((plugin)=>{
37
+ plugin.modify(request, headers);
38
+ });
39
+ const headersToSet = headers.getHeaders();
40
+ delete headersToSet["set-cookie"];
41
+ reply.headers(headersToSet);
44
42
  };
45
- export const createHandler = params => {
46
- const definedRoutes = {
47
- POST: [],
48
- GET: [],
49
- OPTIONS: [],
50
- DELETE: [],
51
- PATCH: [],
52
- PUT: [],
53
- HEAD: [],
54
- COPY: [],
55
- LOCK: [],
56
- MKCOL: [],
57
- MOVE: [],
58
- PROPFIND: [],
59
- PROPPATCH: [],
60
- SEARCH: [],
61
- TRACE: [],
62
- UNLOCK: [],
63
- REPORT: [],
64
- MKCALENDAR: []
65
- };
66
- const throwOnDefinedRoute = (type, path, options) => {
67
- if (type === "ALL") {
68
- const all = Object.keys(definedRoutes).find(k => {
69
- const key = k.toUpperCase();
70
- const routes = definedRoutes[key];
71
- return routes.includes(path);
72
- });
73
- if (!all) {
74
- return;
75
- }
76
- console.error(`Error while registering onAll route. One of the routes is already defined.`);
77
- console.error(JSON.stringify(all));
78
- throw new WebinyError(`You cannot override a route with onAll() method, please remove unnecessary route from the system.`, "OVERRIDE_ROUTE_ERROR", {
79
- type,
80
- path
81
- });
82
- } else if (definedRoutes[type].includes(path) === false) {
83
- return;
84
- } else if (options?.override === true) {
85
- return;
86
- }
87
- console.error(`Error while trying to override route: [${type}] ${path}`);
88
- throw new WebinyError(`When you are trying to override existing route, you must send "override" parameter when adding that route.`, "OVERRIDE_ROUTE_ERROR", {
89
- type,
90
- path
43
+ const createHandler = (params)=>{
44
+ const definedRoutes = {
45
+ POST: [],
46
+ GET: [],
47
+ OPTIONS: [],
48
+ DELETE: [],
49
+ PATCH: [],
50
+ PUT: [],
51
+ HEAD: [],
52
+ COPY: [],
53
+ LOCK: [],
54
+ MKCOL: [],
55
+ MOVE: [],
56
+ PROPFIND: [],
57
+ PROPPATCH: [],
58
+ SEARCH: [],
59
+ TRACE: [],
60
+ UNLOCK: [],
61
+ REPORT: [],
62
+ MKCALENDAR: []
63
+ };
64
+ const throwOnDefinedRoute = (type, path, options)=>{
65
+ if ("ALL" === type) {
66
+ const all = Object.keys(definedRoutes).find((k)=>{
67
+ const key = k.toUpperCase();
68
+ const routes = definedRoutes[key];
69
+ return routes.includes(path);
70
+ });
71
+ if (!all) return;
72
+ console.error("Error while registering onAll route. One of the routes is already defined.");
73
+ console.error(JSON.stringify(all));
74
+ throw new _webiny_error("You cannot override a route with onAll() method, please remove unnecessary route from the system.", "OVERRIDE_ROUTE_ERROR", {
75
+ type,
76
+ path
77
+ });
78
+ }
79
+ if (false === definedRoutes[type].includes(path)) return;
80
+ if (options?.override === true) return;
81
+ console.error(`Error while trying to override route: [${type}] ${path}`);
82
+ throw new _webiny_error('When you are trying to override existing route, you must send "override" parameter when adding that route.', "OVERRIDE_ROUTE_ERROR", {
83
+ type,
84
+ path
85
+ });
86
+ };
87
+ const addDefinedRoute = (input, path)=>{
88
+ const type = input.toUpperCase();
89
+ if (!definedRoutes[type]) return;
90
+ if (definedRoutes[type].includes(path)) return;
91
+ definedRoutes[type].push(path);
92
+ };
93
+ const app = fastify({
94
+ bodyLimit: 536870912,
95
+ disableRequestLogging: true,
96
+ allowErrorHandlerOverride: true,
97
+ ...params.options || {}
91
98
  });
92
- };
93
- const addDefinedRoute = (input, path) => {
94
- const type = input.toUpperCase();
95
- if (!definedRoutes[type]) {
96
- return;
97
- } else if (definedRoutes[type].includes(path)) {
98
- return;
99
- }
100
- definedRoutes[type].push(path);
101
- };
102
-
103
- /**
104
- * We must attach the server to our internal context if we want to have it accessible.
105
- */
106
- const app = fastify({
107
- bodyLimit: 536870912,
108
- // 512MB
109
- disableRequestLogging: true,
110
- allowErrorHandlerOverride: true,
111
- ...(params.options || {})
112
- });
113
-
114
- /**
115
- * We need to register routes in our system to output headers later on, and disallow route overriding.
116
- */
117
- app.addHook("onRoute", route => {
118
- const method = route.method;
119
- if (Array.isArray(method)) {
120
- for (const m of method) {
121
- addDefinedRoute(m, route.path);
122
- }
123
- return;
124
- }
125
- addDefinedRoute(method, route.path);
126
- });
127
- /**
128
- * ############################
129
- * Register the Fastify plugins.
130
- */
131
- /**
132
- * Package @fastify/cookie
133
- *
134
- * https://github.com/fastify/fastify-cookie
135
- */
136
- app.register(fastifyCookie, {
137
- parseOptions: {} // options for parsing cookies
138
- });
139
- /**
140
- * Package @fastify/compress
141
- *
142
- * https://github.com/fastify/fastify-compress
143
- */
144
- app.register(fastifyCompress, {
145
- global: true,
146
- threshold: 1024,
147
- onUnsupportedEncoding: (encoding, _, reply) => {
148
- reply.code(406);
149
- return `We do not support the ${encoding} encoding.`;
150
- },
151
- inflateIfDeflated: true
152
- });
153
- /**
154
- * Route helpers - mostly for users.
155
- */
156
- const routes = {
157
- defined: definedRoutes,
158
- onPost: (path, handler, options) => {
159
- throwOnDefinedRoute("POST", path, options);
160
- app.post(path, handler);
161
- },
162
- onGet: (path, handler, options) => {
163
- throwOnDefinedRoute("GET", path, options);
164
- app.get(path, handler);
165
- },
166
- onOptions: (path, handler, options) => {
167
- throwOnDefinedRoute("OPTIONS", path, options);
168
- app.options(path, handler);
169
- },
170
- onDelete: (path, handler, options) => {
171
- throwOnDefinedRoute("DELETE", path, options);
172
- app.delete(path, handler);
173
- },
174
- onPatch: (path, handler, options) => {
175
- throwOnDefinedRoute("PATCH", path, options);
176
- app.patch(path, handler);
177
- },
178
- onPut: (path, handler, options) => {
179
- throwOnDefinedRoute("PUT", path, options);
180
- app.put(path, handler);
181
- },
182
- onAll: (path, handler, options) => {
183
- throwOnDefinedRoute("ALL", path, options);
184
- app.all(path, handler);
185
- },
186
- onHead: (path, handler, options) => {
187
- throwOnDefinedRoute("HEAD", path, options);
188
- app.head(path, handler);
189
- }
190
- };
191
- let context;
192
- const plugins = new PluginsContainer([
193
- /**
194
- * We must have handlerClient by default.
195
- * And it must be one of the first context plugins applied.
196
- */
197
- createHandlerClient()]);
198
- plugins.merge(params.plugins || []);
199
- try {
200
- context = new Context({
201
- plugins,
202
- /**
203
- * Inserted via webpack at build time.
204
- */
205
- WEBINY_VERSION: process.env.WEBINY_VERSION,
206
- routes
99
+ app.addHook("onRoute", (route)=>{
100
+ const method = route.method;
101
+ if (Array.isArray(method)) {
102
+ for (const m of method)addDefinedRoute(m, route.path);
103
+ return;
104
+ }
105
+ addDefinedRoute(method, route.path);
207
106
  });
208
- } catch (ex) {
209
- console.error(`Error while constructing the Context.`);
210
- console.error(stringifyError(ex));
211
- throw ex;
212
- }
213
-
214
- /**
215
- * We are attaching our custom context to webiny variable on the fastify app, so it is accessible everywhere.
216
- */
217
- app.decorate("webiny", context);
218
-
219
- /**
220
- * To prevent Unsupported Media Type errors on OPTIONS requests with a body,
221
- * we need to have a custom parser
222
- */
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);
229
- return;
230
- }
107
+ app.register(cookie, {
108
+ parseOptions: {}
109
+ });
110
+ app.register(compress, {
111
+ global: true,
112
+ threshold: 1024,
113
+ onUnsupportedEncoding: (encoding, _, reply)=>{
114
+ reply.code(406);
115
+ return `We do not support the ${encoding} encoding.`;
116
+ },
117
+ inflateIfDeflated: true
118
+ });
119
+ const routes = {
120
+ defined: definedRoutes,
121
+ onPost: (path, handler, options)=>{
122
+ throwOnDefinedRoute("POST", path, options);
123
+ app.post(path, handler);
124
+ },
125
+ onGet: (path, handler, options)=>{
126
+ throwOnDefinedRoute("GET", path, options);
127
+ app.get(path, handler);
128
+ },
129
+ onOptions: (path, handler, options)=>{
130
+ throwOnDefinedRoute("OPTIONS", path, options);
131
+ app.options(path, handler);
132
+ },
133
+ onDelete: (path, handler, options)=>{
134
+ throwOnDefinedRoute("DELETE", path, options);
135
+ app.delete(path, handler);
136
+ },
137
+ onPatch: (path, handler, options)=>{
138
+ throwOnDefinedRoute("PATCH", path, options);
139
+ app.patch(path, handler);
140
+ },
141
+ onPut: (path, handler, options)=>{
142
+ throwOnDefinedRoute("PUT", path, options);
143
+ app.put(path, handler);
144
+ },
145
+ onAll: (path, handler, options)=>{
146
+ throwOnDefinedRoute("ALL", path, options);
147
+ app.all(path, handler);
148
+ },
149
+ onHead: (path, handler, options)=>{
150
+ throwOnDefinedRoute("HEAD", path, options);
151
+ app.head(path, handler);
152
+ }
153
+ };
154
+ let context;
155
+ const plugins = new PluginsContainer([]);
156
+ plugins.merge(params.plugins || []);
231
157
  try {
232
- const json = typeof body === "string" ? body : body.toString("utf8");
233
- done(null, JSON.parse(json));
234
- } catch (err) {
235
- done(err);
236
- }
237
- });
238
-
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.
242
- *
243
- * @see https://fastify.dev/docs/latest/Reference/ContentTypeParser/#content-type-parser
244
- */
245
- app.addHook("onRequest", async request => {
246
- if (request.method === "OPTIONS" && request.body === undefined) {
247
- request.headers["content-type"] = undefined;
248
- }
249
- });
250
-
251
- /**
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`
258
- */
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);
158
+ context = new Context({
159
+ plugins,
160
+ WEBINY_VERSION: process.env.WEBINY_VERSION,
161
+ routes
162
+ });
163
+ } catch (ex) {
164
+ console.error("Error while constructing the Context.");
165
+ console.error(stringifyError(ex));
166
+ throw ex;
267
167
  }
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);
282
- let name;
168
+ app.decorate("webiny", context);
169
+ app.addContentTypeParser("application/json", {
170
+ parseAs: "string",
171
+ bodyLimit: 1048576
172
+ }, (req, body, done)=>{
173
+ if ("OPTIONS" === req.method) return void done(null, void 0);
174
+ try {
175
+ const json = "string" == typeof body ? body : body.toString("utf8");
176
+ done(null, JSON.parse(json));
177
+ } catch (err) {
178
+ done(err);
179
+ }
180
+ });
181
+ app.addHook("onRequest", async (request)=>{
182
+ if ("OPTIONS" === request.method && void 0 === request.body) request.headers["content-type"] = void 0;
183
+ });
184
+ app.addHook("preHandler", async (request, reply)=>{
185
+ app.webiny.request = request;
186
+ app.webiny.reply = reply;
187
+ if (app.webiny.container) {
188
+ app.webiny.container.registerInstance(Request, request);
189
+ app.webiny.container.registerInstance(Reply, reply);
190
+ }
191
+ reply.code(200);
192
+ const handlerOnRequestPlugins = app.webiny.plugins.byType(HandlerOnRequestPlugin.type);
193
+ const contextPlugins = app.webiny.plugins.byType(ContextPlugin.type);
194
+ const beforeHandlerPlugins = app.webiny.plugins.byType(BeforeHandlerPlugin.type);
195
+ const modifyHeadersPlugins = app.webiny.plugins.byType(ModifyResponseHeadersPlugin.type);
196
+ const registerExtensionPlugins = app.webiny.plugins.byType(RegisterExtensionPlugin.type);
197
+ const preHandler = new PreHandler([
198
+ new RegisterExtensions(registerExtensionPlugins),
199
+ new SetDefaultHeaders(definedRoutes),
200
+ new ProcessHandlerOnRequestPlugins(handlerOnRequestPlugins),
201
+ new IfNotOptionsRequest([
202
+ new ProcessContextPlugins(app.webiny, contextPlugins),
203
+ new ProcessBeforeHandlerPlugins(app.webiny, beforeHandlerPlugins)
204
+ ]),
205
+ new IfOptionsRequest([
206
+ new SendEarlyOptionsResponse(modifyHeadersPlugins)
207
+ ])
208
+ ]);
209
+ await preHandler.execute(request, reply, app.webiny);
210
+ });
211
+ app.addHook("preSerialization", async (_, __, payload)=>{
212
+ const plugins = app.webiny.plugins.byType(HandlerResultPlugin.type);
213
+ let name;
214
+ try {
215
+ for (const plugin of plugins){
216
+ name = plugin.name;
217
+ await plugin.handle(app.webiny, payload);
218
+ }
219
+ } catch (ex) {
220
+ console.error(`Error while running the "HandlerResultPlugin" ${name ? `(${name})` : ""} plugin in the preSerialization hook.`);
221
+ console.error(stringifyError(ex));
222
+ throw ex;
223
+ }
224
+ return payload;
225
+ });
226
+ app.setErrorHandler(async (error, _, reply)=>{
227
+ if (reply.sent) {
228
+ console.warn("Reply already sent, cannot send the result (handler:setErrorHandler).");
229
+ return reply;
230
+ }
231
+ if (error.code?.startsWith("Authentication/")) return reply.status(401).headers({
232
+ "Cache-Control": "no-store"
233
+ }).send(JSON.stringify({
234
+ message: error.message,
235
+ code: error.code
236
+ }));
237
+ if ("Tenancy/TenantDisabled" === error.code) return reply.status(503).headers({
238
+ "Cache-Control": "no-store"
239
+ }).send(JSON.stringify({
240
+ message: error.message,
241
+ code: error.code
242
+ }));
243
+ return reply.status(500).headers({
244
+ "Cache-Control": "no-store"
245
+ }).send(JSON.stringify({
246
+ message: error.message,
247
+ code: error.code,
248
+ data: error.data
249
+ }));
250
+ });
251
+ app.addHook("onError", async (_, reply, error)=>{
252
+ const plugins = app.webiny.plugins.byType(HandlerErrorPlugin.type);
253
+ console.error("Logging error in @webiny/handler");
254
+ try {
255
+ console.error(stringifyError(error));
256
+ } catch (ex) {
257
+ console.warn("Could not stringify error:");
258
+ console.log(error);
259
+ console.error("Stringify error:", ex);
260
+ }
261
+ if (reply.sent) console.warn("Reply already sent, cannot send the result (handler:addHook:onError).");
262
+ else reply.status(500).headers({
263
+ "Cache-Control": "no-store"
264
+ }).send(JSON.stringify({
265
+ message: error.message,
266
+ code: error.code,
267
+ data: error.data
268
+ }));
269
+ const handler = middleware(plugins.map((pl)=>(context, error, next)=>pl.handle(context, error, next)));
270
+ await handler(app.webiny, error);
271
+ return reply;
272
+ });
273
+ app.addHook("onSend", async (request, reply, input)=>{
274
+ modifyResponseHeaders(app, request, reply);
275
+ const plugins = app.webiny.plugins.byType(OnRequestResponseSendPlugin.type);
276
+ let payload = input;
277
+ for (const plugin of plugins)payload = await plugin.exec(request, reply, payload);
278
+ return payload;
279
+ });
280
+ app.addHook("onResponse", async ()=>{
281
+ await context.benchmark.output();
282
+ });
283
+ app.addHook("onTimeout", async (request, reply)=>{
284
+ const plugins = app.webiny.plugins.byType(OnRequestTimeoutPlugin.type);
285
+ for (const plugin of plugins)await plugin.exec(request, reply);
286
+ await context.benchmark.output();
287
+ });
288
+ const modifyPlugins = app.webiny.plugins.byType(ModifyFastifyPlugin.type);
289
+ let modifyFastifyPluginName;
283
290
  try {
284
- for (const plugin of plugins) {
285
- name = plugin.name;
286
- await plugin.handle(app.webiny, payload);
287
- }
291
+ for (const plugin of modifyPlugins){
292
+ modifyFastifyPluginName = plugin.name;
293
+ plugin.modify(app);
294
+ }
288
295
  } catch (ex) {
289
- console.error(`Error while running the "HandlerResultPlugin" ${name ? `(${name})` : ""} plugin in the preSerialization hook.`);
290
- console.error(stringifyError(ex));
291
- throw ex;
292
- }
293
- return payload;
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
- }));
296
+ console.error(`Error while running the "ModifyFastifyPlugin" ${modifyFastifyPluginName ? `(${modifyFastifyPluginName})` : ""} plugin in the end of the "createHandler" callable.`);
297
+ console.error(stringifyError(ex));
298
+ throw ex;
318
299
  }
319
- return reply.status(500).headers({
320
- "Cache-Control": "no-store"
321
- }).send(
322
- /**
323
- * When we are sending the error in the response, we cannot send the whole error object, as it might contain some sensitive data.
324
- */
325
- JSON.stringify({
326
- message: error.message,
327
- code: error.code,
328
- data: error.data
329
- }));
330
- });
331
- app.addHook("onError", async (_, reply, error) => {
332
- const plugins = app.webiny.plugins.byType(HandlerErrorPlugin.type);
333
- /**
334
- * Log error to cloud, as these can be extremely annoying to debug!
335
- */
336
- console.error("Logging error in @webiny/handler");
300
+ const routePlugins = app.webiny.plugins.byType(RoutePlugin.type);
301
+ let routePluginName;
337
302
  try {
338
- console.error(stringifyError(error));
303
+ for (const plugin of routePlugins){
304
+ routePluginName = plugin.name;
305
+ plugin.cb({
306
+ ...app.webiny.routes,
307
+ context: app.webiny
308
+ });
309
+ }
339
310
  } catch (ex) {
340
- console.warn("Could not stringify error:");
341
- console.log(error);
342
- console.error("Stringify error:", ex);
343
- }
344
- /**
345
- * IMPORTANT! Do not send anything if reply was already sent.
346
- */
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 => {
363
- return (context, error, next) => {
364
- return pl.handle(context, error, next);
365
- };
366
- }));
367
- await handler(app.webiny, error);
368
- return reply;
369
- });
370
-
371
- /**
372
- * Apply response headers modifier plugins.
373
- */
374
- app.addHook("onSend", async (request, reply, input) => {
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
- }
381
- return payload;
382
- });
383
-
384
- /**
385
- * We need to output the benchmark results at the end of the request in both response and timeout cases
386
- */
387
- app.addHook("onResponse", async () => {
388
- await context.benchmark.output();
389
- });
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
- }
395
- await context.benchmark.output();
396
- });
397
-
398
- /**
399
- * With these plugins we give users possibility to do anything they want on our fastify instance.
400
- */
401
- const modifyPlugins = app.webiny.plugins.byType(ModifyFastifyPlugin.type);
402
- let modifyFastifyPluginName;
403
- try {
404
- for (const plugin of modifyPlugins) {
405
- modifyFastifyPluginName = plugin.name;
406
- plugin.modify(app);
407
- }
408
- } catch (ex) {
409
- console.error(`Error while running the "ModifyFastifyPlugin" ${modifyFastifyPluginName ? `(${modifyFastifyPluginName})` : ""} plugin in the end of the "createHandler" callable.`);
410
- console.error(stringifyError(ex));
411
- throw ex;
412
- }
413
-
414
- /**
415
- * We have few types of triggers:
416
- * * Events - EventPlugin
417
- * * Routes - RoutePlugin
418
- *
419
- * Routes are registered in fastify but events must be handled in package which implements cloud specific methods.
420
- */
421
- const routePlugins = app.webiny.plugins.byType(RoutePlugin.type);
422
-
423
- /**
424
- * Add routes to the system.
425
- */
426
- let routePluginName;
427
- try {
428
- for (const plugin of routePlugins) {
429
- routePluginName = plugin.name;
430
- plugin.cb({
431
- ...app.webiny.routes,
432
- context: app.webiny
433
- });
311
+ console.error(`Error while running the "RoutePlugin" ${routePluginName ? `(${routePluginName})` : ""} plugin in the beginning of the "createHandler" callable.`);
312
+ console.error(stringifyError(ex));
313
+ throw ex;
434
314
  }
435
- } catch (ex) {
436
- console.error(`Error while running the "RoutePlugin" ${routePluginName ? `(${routePluginName})` : ""} plugin in the beginning of the "createHandler" callable.`);
437
- console.error(stringifyError(ex));
438
- throw ex;
439
- }
440
- return app;
315
+ return app;
441
316
  };
317
+ export { createHandler };
442
318
 
443
319
  //# sourceMappingURL=fastify.js.map