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