@webiny/handler 0.0.0-unstable.1e66d121db → 0.0.0-unstable.2696f9d9e8
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.d.ts +17 -9
- package/Context.js +20 -15
- package/Context.js.map +1 -1
- package/ResponseHeaders.d.ts +25 -0
- package/ResponseHeaders.js +46 -0
- package/ResponseHeaders.js.map +1 -0
- package/fastify.d.ts +3 -2
- package/fastify.js +272 -134
- package/fastify.js.map +1 -1
- package/index.d.ts +5 -1
- package/index.js +44 -25
- package/index.js.map +1 -1
- package/middleware.d.ts +4 -1
- package/middleware.js +4 -8
- package/middleware.js.map +1 -1
- package/package.json +18 -19
- package/plugins/BeforeHandlerPlugin.js +3 -14
- package/plugins/BeforeHandlerPlugin.js.map +1 -1
- package/plugins/EventPlugin.js +4 -12
- package/plugins/EventPlugin.js.map +1 -1
- package/plugins/HandlerErrorPlugin.d.ts +5 -2
- package/plugins/HandlerErrorPlugin.js +3 -13
- package/plugins/HandlerErrorPlugin.js.map +1 -1
- package/plugins/HandlerOnRequestPlugin.d.ts +20 -0
- package/plugins/HandlerOnRequestPlugin.js +31 -0
- package/plugins/HandlerOnRequestPlugin.js.map +1 -0
- package/plugins/HandlerResultPlugin.js +3 -13
- package/plugins/HandlerResultPlugin.js.map +1 -1
- package/plugins/ModifyFastifyPlugin.d.ts +13 -0
- package/plugins/ModifyFastifyPlugin.js +24 -0
- package/plugins/ModifyFastifyPlugin.js.map +1 -0
- package/plugins/ModifyResponseHeadersPlugin.d.ts +14 -0
- package/plugins/ModifyResponseHeadersPlugin.js +24 -0
- package/plugins/ModifyResponseHeadersPlugin.js.map +1 -0
- package/plugins/RoutePlugin.js +3 -12
- package/plugins/RoutePlugin.js.map +1 -1
- package/types.d.ts +6 -7
- package/types.js +3 -1
- package/types.js.map +1 -1
package/fastify.js
CHANGED
|
@@ -1,77 +1,83 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.createHandler = void 0;
|
|
9
|
-
|
|
10
|
-
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
|
-
|
|
8
|
+
var _types = require("@webiny/plugins/types");
|
|
12
9
|
var _fastify = _interopRequireDefault(require("fastify"));
|
|
13
|
-
|
|
14
10
|
var _utils = require("@webiny/utils");
|
|
15
|
-
|
|
16
11
|
var _Context = require("./Context");
|
|
17
|
-
|
|
18
12
|
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
19
|
-
|
|
20
13
|
var _RoutePlugin = require("./plugins/RoutePlugin");
|
|
21
|
-
|
|
22
14
|
var _handlerClient = require("@webiny/handler-client");
|
|
23
|
-
|
|
24
15
|
var _cookie = _interopRequireDefault(require("@fastify/cookie"));
|
|
25
|
-
|
|
16
|
+
var _compress = _interopRequireDefault(require("@fastify/compress"));
|
|
26
17
|
var _middleware = require("./middleware");
|
|
27
|
-
|
|
28
18
|
var _api = require("@webiny/api");
|
|
29
|
-
|
|
30
19
|
var _BeforeHandlerPlugin = require("./plugins/BeforeHandlerPlugin");
|
|
31
|
-
|
|
32
20
|
var _HandlerResultPlugin = require("./plugins/HandlerResultPlugin");
|
|
33
|
-
|
|
34
21
|
var _HandlerErrorPlugin = require("./plugins/HandlerErrorPlugin");
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
22
|
+
var _ModifyFastifyPlugin = require("./plugins/ModifyFastifyPlugin");
|
|
23
|
+
var _HandlerOnRequestPlugin = require("./plugins/HandlerOnRequestPlugin");
|
|
24
|
+
var _ResponseHeaders = require("./ResponseHeaders");
|
|
25
|
+
var _ModifyResponseHeadersPlugin = require("./plugins/ModifyResponseHeadersPlugin");
|
|
26
|
+
function createDefaultHeaders() {
|
|
27
|
+
return _ResponseHeaders.ResponseHeaders.create({
|
|
28
|
+
"content-type": "application/json; charset=utf-8",
|
|
29
|
+
"cache-control": "no-store",
|
|
30
|
+
"access-control-allow-origin": "*",
|
|
31
|
+
"access-control-allow-headers": "*",
|
|
32
|
+
"access-control-allow-methods": "OPTIONS,POST,GET,DELETE,PUT,PATCH",
|
|
33
|
+
...(0, _utils.getWebinyVersionHeaders)()
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
const getDefaultOptionsHeaders = () => {
|
|
37
|
+
return _ResponseHeaders.ResponseHeaders.create({
|
|
38
|
+
"access-control-max-age": "86400",
|
|
39
|
+
"cache-control": "public, max-age=86400"
|
|
40
|
+
});
|
|
41
|
+
};
|
|
44
42
|
const getDefaultHeaders = routes => {
|
|
43
|
+
const headers = createDefaultHeaders();
|
|
44
|
+
|
|
45
45
|
/**
|
|
46
46
|
* If we are accepting all headers, just output that one.
|
|
47
47
|
*/
|
|
48
48
|
const keys = Object.keys(routes);
|
|
49
49
|
const all = keys.every(key => routes[key].length > 0);
|
|
50
|
-
|
|
51
50
|
if (all) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, DEFAULT_HEADERS), {}, {
|
|
58
|
-
"Access-Control-Allow-Methods": keys.filter(key => {
|
|
59
|
-
const type = key;
|
|
60
|
-
|
|
61
|
-
if (!routes[type] || Array.isArray(routes[type]) === false) {
|
|
51
|
+
headers.set("access-control-allow-methods", "*");
|
|
52
|
+
} else {
|
|
53
|
+
const allowedMethods = keys.filter(type => {
|
|
54
|
+
if (!routes[type] || !Array.isArray(routes[type])) {
|
|
62
55
|
return false;
|
|
63
56
|
}
|
|
64
|
-
|
|
65
57
|
return routes[type].length > 0;
|
|
66
|
-
}).sort().join(",")
|
|
67
|
-
|
|
58
|
+
}).sort().join(",");
|
|
59
|
+
headers.set("access-control-allow-methods", allowedMethods);
|
|
60
|
+
}
|
|
61
|
+
return headers;
|
|
68
62
|
};
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
63
|
+
const stringifyError = error => {
|
|
64
|
+
const {
|
|
65
|
+
name,
|
|
66
|
+
message,
|
|
67
|
+
code,
|
|
68
|
+
stack,
|
|
69
|
+
data
|
|
70
|
+
} = error;
|
|
71
|
+
return JSON.stringify({
|
|
72
|
+
...error,
|
|
73
|
+
constructorName: error.constructor?.name || "UnknownError",
|
|
74
|
+
name: name || "No error name",
|
|
75
|
+
message: message || "No error message",
|
|
76
|
+
code: code || "NO_CODE",
|
|
77
|
+
data,
|
|
78
|
+
stack: process.env.DEBUG === "true" ? stack : "Turn on the debug flag to see the stack."
|
|
79
|
+
});
|
|
73
80
|
};
|
|
74
|
-
|
|
75
81
|
const createHandler = params => {
|
|
76
82
|
const definedRoutes = {
|
|
77
83
|
POST: [],
|
|
@@ -91,80 +97,88 @@ const createHandler = params => {
|
|
|
91
97
|
TRACE: [],
|
|
92
98
|
UNLOCK: []
|
|
93
99
|
};
|
|
94
|
-
|
|
95
100
|
const throwOnDefinedRoute = (type, path, options) => {
|
|
96
101
|
if (type === "ALL") {
|
|
97
|
-
const all = Object.keys(definedRoutes).
|
|
102
|
+
const all = Object.keys(definedRoutes).find(key => {
|
|
98
103
|
const routes = definedRoutes[key];
|
|
99
104
|
return routes.includes(path);
|
|
100
105
|
});
|
|
101
|
-
|
|
102
106
|
if (!all) {
|
|
103
107
|
return;
|
|
104
108
|
}
|
|
105
|
-
|
|
109
|
+
console.error(`Error while registering onAll route. One of the routes is already defined.`);
|
|
110
|
+
console.error(JSON.stringify(all));
|
|
106
111
|
throw new _error.default(`You cannot override a route with onAll() method, please remove unnecessary route from the system.`, "OVERRIDE_ROUTE_ERROR", {
|
|
107
112
|
type,
|
|
108
113
|
path
|
|
109
114
|
});
|
|
110
115
|
} else if (definedRoutes[type].includes(path) === false) {
|
|
111
116
|
return;
|
|
112
|
-
} else if (
|
|
117
|
+
} else if (options?.override === true) {
|
|
113
118
|
return;
|
|
114
119
|
}
|
|
115
|
-
|
|
120
|
+
console.error(`Error while trying to override route: [${type}] ${path}`);
|
|
116
121
|
throw new _error.default(`When you are trying to override existing route, you must send "override" parameter when adding that route.`, "OVERRIDE_ROUTE_ERROR", {
|
|
117
122
|
type,
|
|
118
123
|
path
|
|
119
124
|
});
|
|
120
125
|
};
|
|
121
|
-
|
|
122
|
-
const addDefinedRoute = (inputType, path) => {
|
|
123
|
-
const type = inputType.toUpperCase();
|
|
124
|
-
|
|
126
|
+
const addDefinedRoute = (type, path) => {
|
|
125
127
|
if (!definedRoutes[type]) {
|
|
126
128
|
return;
|
|
127
129
|
} else if (definedRoutes[type].includes(path)) {
|
|
128
130
|
return;
|
|
129
131
|
}
|
|
130
|
-
|
|
131
132
|
definedRoutes[type].push(path);
|
|
132
133
|
};
|
|
133
134
|
/**
|
|
134
135
|
* We must attach the server to our internal context if we want to have it accessible.
|
|
135
136
|
*/
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
137
|
+
const app = (0, _fastify.default)({
|
|
138
|
+
...(params.options || {})
|
|
139
|
+
});
|
|
139
140
|
/**
|
|
140
141
|
* We need to register routes in our system so we can output headers later on and dissallow overriding routes.
|
|
141
142
|
*/
|
|
142
|
-
|
|
143
143
|
app.addHook("onRoute", route => {
|
|
144
144
|
const method = route.method;
|
|
145
|
-
|
|
146
145
|
if (Array.isArray(method)) {
|
|
147
146
|
for (const m of method) {
|
|
148
147
|
addDefinedRoute(m, route.path);
|
|
149
148
|
}
|
|
150
|
-
|
|
151
149
|
return;
|
|
152
150
|
}
|
|
153
|
-
|
|
154
151
|
addDefinedRoute(method, route.path);
|
|
155
152
|
});
|
|
156
153
|
/**
|
|
154
|
+
* ############################
|
|
155
|
+
* Register the Fastify plugins.
|
|
156
|
+
*/
|
|
157
|
+
/**
|
|
158
|
+
* Package @fastify/cookie
|
|
157
159
|
*
|
|
160
|
+
* https://github.com/fastify/fastify-cookie
|
|
158
161
|
*/
|
|
159
|
-
|
|
160
162
|
app.register(_cookie.default, {
|
|
161
163
|
parseOptions: {} // options for parsing cookies
|
|
162
|
-
|
|
164
|
+
});
|
|
165
|
+
/**
|
|
166
|
+
* Package @fastify/compress
|
|
167
|
+
*
|
|
168
|
+
* https://github.com/fastify/fastify-compress
|
|
169
|
+
*/
|
|
170
|
+
app.register(_compress.default, {
|
|
171
|
+
global: true,
|
|
172
|
+
threshold: 1024,
|
|
173
|
+
onUnsupportedEncoding: (encoding, _, reply) => {
|
|
174
|
+
reply.code(406);
|
|
175
|
+
return `We do not support the ${encoding} encoding.`;
|
|
176
|
+
},
|
|
177
|
+
inflateIfDeflated: true
|
|
163
178
|
});
|
|
164
179
|
/**
|
|
165
180
|
* Route helpers - mostly for users.
|
|
166
181
|
*/
|
|
167
|
-
|
|
168
182
|
const routes = {
|
|
169
183
|
defined: definedRoutes,
|
|
170
184
|
onPost: (path, handler, options) => {
|
|
@@ -200,120 +214,244 @@ const createHandler = params => {
|
|
|
200
214
|
app.head(path, handler);
|
|
201
215
|
}
|
|
202
216
|
};
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* We must have handlerClient by default.
|
|
207
|
-
* And it must be one of the first context plugins applied.
|
|
208
|
-
*/
|
|
209
|
-
(0, _handlerClient.createHandlerClient)(), ...(params.plugins || [])],
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Inserted via webpack on build time.
|
|
213
|
-
*/
|
|
214
|
-
WEBINY_VERSION: process.env.WEBINY_VERSION,
|
|
215
|
-
server: app,
|
|
216
|
-
routes
|
|
217
|
-
});
|
|
218
|
-
/**
|
|
219
|
-
* We are attaching our custom context to webiny variable on the fastify app so it is accessible everywhere
|
|
220
|
-
*/
|
|
221
|
-
|
|
222
|
-
app.decorate("webiny", context);
|
|
217
|
+
let context;
|
|
218
|
+
const plugins = new _types.PluginsContainer([
|
|
223
219
|
/**
|
|
224
|
-
* We have
|
|
225
|
-
*
|
|
226
|
-
* * Routes - RoutePlugin
|
|
227
|
-
*
|
|
228
|
-
* Routes are registered in fastify but events must be handled in package which implements cloud specific methods.
|
|
220
|
+
* We must have handlerClient by default.
|
|
221
|
+
* And it must be one of the first context plugins applied.
|
|
229
222
|
*/
|
|
223
|
+
(0, _handlerClient.createHandlerClient)()]);
|
|
224
|
+
plugins.merge(params.plugins || []);
|
|
225
|
+
try {
|
|
226
|
+
context = new _Context.Context({
|
|
227
|
+
plugins,
|
|
228
|
+
/**
|
|
229
|
+
* Inserted via webpack at build time.
|
|
230
|
+
*/
|
|
231
|
+
WEBINY_VERSION: process.env.WEBINY_VERSION,
|
|
232
|
+
routes
|
|
233
|
+
});
|
|
234
|
+
} catch (ex) {
|
|
235
|
+
console.error(`Error while constructing the Context.`);
|
|
236
|
+
console.error(stringifyError(ex));
|
|
237
|
+
throw ex;
|
|
238
|
+
}
|
|
230
239
|
|
|
231
|
-
const routePlugins = app.webiny.plugins.byType(_RoutePlugin.RoutePlugin.type);
|
|
232
240
|
/**
|
|
233
|
-
*
|
|
241
|
+
* We are attaching our custom context to webiny variable on the fastify app, so it is accessible everywhere.
|
|
234
242
|
*/
|
|
243
|
+
app.decorate("webiny", context);
|
|
235
244
|
|
|
236
|
-
for (const plugin of routePlugins) {
|
|
237
|
-
plugin.cb((0, _objectSpread2.default)((0, _objectSpread2.default)({}, app.webiny.routes), {}, {
|
|
238
|
-
context: app.webiny
|
|
239
|
-
}));
|
|
240
|
-
}
|
|
241
245
|
/**
|
|
242
246
|
* On every request we add default headers, which can be changed later.
|
|
243
247
|
* Also, if it is an options request, we skip everything after this hook and output options headers.
|
|
244
248
|
*/
|
|
245
|
-
|
|
246
|
-
|
|
247
249
|
app.addHook("onRequest", async (request, reply) => {
|
|
250
|
+
const isOptionsRequest = request.method === "OPTIONS";
|
|
251
|
+
/**
|
|
252
|
+
* Our default headers are always set. Users can override them.
|
|
253
|
+
*/
|
|
248
254
|
const defaultHeaders = getDefaultHeaders(definedRoutes);
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
255
|
+
const initialHeaders = isOptionsRequest ? defaultHeaders.merge(getDefaultOptionsHeaders()) : defaultHeaders;
|
|
256
|
+
reply.headers(initialHeaders.getHeaders());
|
|
257
|
+
/**
|
|
258
|
+
* Users can define their own custom handlers for the onRequest event - so let's run them first.
|
|
259
|
+
*/
|
|
260
|
+
const plugins = app.webiny.plugins.byType(_HandlerOnRequestPlugin.HandlerOnRequestPlugin.type);
|
|
261
|
+
let name;
|
|
262
|
+
try {
|
|
263
|
+
for (const plugin of plugins) {
|
|
264
|
+
name = plugin.name;
|
|
265
|
+
const result = await plugin.exec(request, reply);
|
|
266
|
+
if (result === false) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
} catch (ex) {
|
|
271
|
+
console.error(`Error while running the "HandlerOnRequestPlugin" ${name ? `(${name})` : ""} plugin in the onRequest hook.`);
|
|
272
|
+
console.error(stringifyError(ex));
|
|
273
|
+
throw ex;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* 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.
|
|
277
|
+
*
|
|
278
|
+
* Users can prevent this by creating their own HandlerOnRequestPlugin and returning false as the result of the callable.
|
|
279
|
+
*/
|
|
280
|
+
if (!isOptionsRequest) {
|
|
252
281
|
return;
|
|
253
282
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
283
|
+
if (reply.sent) {
|
|
284
|
+
/**
|
|
285
|
+
* At this point throwing an exception will not do anything with the response. So just log it.
|
|
286
|
+
*/
|
|
287
|
+
console.error(JSON.stringify({
|
|
288
|
+
message: `Output was already sent. Please check custom plugins of type "HandlerOnRequestPlugin".`,
|
|
289
|
+
explanation: "This error can happen if the user plugin ended the reply, but did not return false as response."
|
|
290
|
+
}));
|
|
291
|
+
return;
|
|
260
292
|
}
|
|
261
|
-
|
|
262
|
-
raw.end("");
|
|
293
|
+
reply.code(204).send("").hijack();
|
|
263
294
|
});
|
|
264
|
-
app.addHook("preParsing", async request => {
|
|
295
|
+
app.addHook("preParsing", async (request, reply) => {
|
|
265
296
|
app.webiny.request = request;
|
|
297
|
+
app.webiny.reply = reply;
|
|
266
298
|
const plugins = app.webiny.plugins.byType(_api.ContextPlugin.type);
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
299
|
+
let name;
|
|
300
|
+
try {
|
|
301
|
+
for (const plugin of plugins) {
|
|
302
|
+
name = plugin.name;
|
|
303
|
+
await plugin.apply(app.webiny);
|
|
304
|
+
}
|
|
305
|
+
} catch (ex) {
|
|
306
|
+
console.error(`Error while running the "ContextPlugin" ${name ? `(${name})` : ""} plugin in the preParsing hook.`);
|
|
307
|
+
console.error(stringifyError(ex));
|
|
308
|
+
throw ex;
|
|
270
309
|
}
|
|
271
310
|
});
|
|
272
311
|
/**
|
|
273
312
|
*
|
|
274
313
|
*/
|
|
275
|
-
|
|
276
314
|
app.addHook("preHandler", async () => {
|
|
277
315
|
const plugins = app.webiny.plugins.byType(_BeforeHandlerPlugin.BeforeHandlerPlugin.type);
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
316
|
+
let name;
|
|
317
|
+
try {
|
|
318
|
+
for (const plugin of plugins) {
|
|
319
|
+
name = plugin.name;
|
|
320
|
+
await plugin.apply(app.webiny);
|
|
321
|
+
}
|
|
322
|
+
} catch (ex) {
|
|
323
|
+
console.error(`Error while running the "BeforeHandlerPlugin" ${name ? `(${name})` : ""} plugin in the preHandler hook.`);
|
|
324
|
+
console.error(stringifyError(ex));
|
|
325
|
+
throw ex;
|
|
281
326
|
}
|
|
282
327
|
});
|
|
328
|
+
|
|
283
329
|
/**
|
|
284
330
|
*
|
|
285
331
|
*/
|
|
286
|
-
|
|
287
332
|
const preSerialization = async (_, __, payload) => {
|
|
288
333
|
const plugins = app.webiny.plugins.byType(_HandlerResultPlugin.HandlerResultPlugin.type);
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
334
|
+
let name;
|
|
335
|
+
try {
|
|
336
|
+
for (const plugin of plugins) {
|
|
337
|
+
name = plugin.name;
|
|
338
|
+
await plugin.handle(app.webiny, payload);
|
|
339
|
+
}
|
|
340
|
+
} catch (ex) {
|
|
341
|
+
console.error(`Error while running the "HandlerResultPlugin" ${name ? `(${name})` : ""} plugin in the preSerialization hook.`);
|
|
342
|
+
console.error(stringifyError(ex));
|
|
343
|
+
throw ex;
|
|
292
344
|
}
|
|
293
|
-
|
|
294
345
|
return payload;
|
|
295
346
|
};
|
|
296
|
-
|
|
297
347
|
app.addHook("preSerialization", preSerialization);
|
|
348
|
+
app.setErrorHandler(async (error, request, reply) => {
|
|
349
|
+
return reply.status(500).headers({
|
|
350
|
+
"Cache-Control": "no-store"
|
|
351
|
+
}).send(
|
|
352
|
+
/**
|
|
353
|
+
* When we are sending the error in the response, we cannot send the whole error object, as it might contain some sensitive data.
|
|
354
|
+
*/
|
|
355
|
+
JSON.stringify({
|
|
356
|
+
message: error.message,
|
|
357
|
+
code: error.code,
|
|
358
|
+
data: error.data
|
|
359
|
+
}));
|
|
360
|
+
});
|
|
298
361
|
app.addHook("onError", async (_, reply, error) => {
|
|
299
|
-
const plugins = app.webiny.plugins.byType(_HandlerErrorPlugin.HandlerErrorPlugin.type);
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
362
|
+
const plugins = app.webiny.plugins.byType(_HandlerErrorPlugin.HandlerErrorPlugin.type);
|
|
363
|
+
/**
|
|
364
|
+
* Log error to cloud, as these can be extremely annoying to debug!
|
|
365
|
+
*/
|
|
366
|
+
console.error("@webiny/handler");
|
|
367
|
+
console.error(stringifyError(error));
|
|
368
|
+
reply.status(500).headers({
|
|
369
|
+
"Cache-Control": "no-store"
|
|
370
|
+
}).send(
|
|
371
|
+
/**
|
|
372
|
+
* When we are sending the error in the response, we cannot send the whole error object, as it might contain some sensitive data.
|
|
373
|
+
*/
|
|
374
|
+
JSON.stringify({
|
|
375
|
+
message: error.message,
|
|
376
|
+
code: error.code,
|
|
377
|
+
data: error.data
|
|
378
|
+
}));
|
|
306
379
|
const handler = (0, _middleware.middleware)(plugins.map(pl => {
|
|
307
380
|
return (context, error, next) => {
|
|
308
381
|
return pl.handle(context, error, next);
|
|
309
382
|
};
|
|
310
383
|
}));
|
|
311
384
|
await handler(app.webiny, error);
|
|
312
|
-
return reply
|
|
313
|
-
"Cache-Control": "no-store"
|
|
314
|
-
}).status(500);
|
|
385
|
+
return reply;
|
|
315
386
|
});
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Apply response headers modifier plugins.
|
|
390
|
+
*/
|
|
391
|
+
app.addHook("onSend", async (request, reply, payload) => {
|
|
392
|
+
const modifyHeaders = app.webiny.plugins.byType(_ModifyResponseHeadersPlugin.ModifyResponseHeadersPlugin.type);
|
|
393
|
+
const headers = _ResponseHeaders.ResponseHeaders.create(reply.getHeaders());
|
|
394
|
+
modifyHeaders.forEach(plugin => {
|
|
395
|
+
plugin.modify(request, headers);
|
|
396
|
+
});
|
|
397
|
+
reply.headers(headers.getHeaders());
|
|
398
|
+
return payload;
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* We need to output the benchmark results at the end of the request in both response and timeout cases
|
|
403
|
+
*/
|
|
404
|
+
app.addHook("onResponse", async () => {
|
|
405
|
+
await context.benchmark.output();
|
|
406
|
+
});
|
|
407
|
+
app.addHook("onTimeout", async () => {
|
|
408
|
+
await context.benchmark.output();
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* With these plugins we give users possibility to do anything they want on our fastify instance.
|
|
413
|
+
*/
|
|
414
|
+
const modifyPlugins = app.webiny.plugins.byType(_ModifyFastifyPlugin.ModifyFastifyPlugin.type);
|
|
415
|
+
let modifyFastifyPluginName;
|
|
416
|
+
try {
|
|
417
|
+
for (const plugin of modifyPlugins) {
|
|
418
|
+
modifyFastifyPluginName = plugin.name;
|
|
419
|
+
plugin.modify(app);
|
|
420
|
+
}
|
|
421
|
+
} catch (ex) {
|
|
422
|
+
console.error(`Error while running the "ModifyFastifyPlugin" ${modifyFastifyPluginName ? `(${modifyFastifyPluginName})` : ""} plugin in the end of the "createHandler" callable.`);
|
|
423
|
+
console.error(stringifyError(ex));
|
|
424
|
+
throw ex;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* We have few types of triggers:
|
|
429
|
+
* * Events - EventPlugin
|
|
430
|
+
* * Routes - RoutePlugin
|
|
431
|
+
*
|
|
432
|
+
* Routes are registered in fastify but events must be handled in package which implements cloud specific methods.
|
|
433
|
+
*/
|
|
434
|
+
const routePlugins = app.webiny.plugins.byType(_RoutePlugin.RoutePlugin.type);
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Add routes to the system.
|
|
438
|
+
*/
|
|
439
|
+
let routePluginName;
|
|
440
|
+
try {
|
|
441
|
+
for (const plugin of routePlugins) {
|
|
442
|
+
routePluginName = plugin.name;
|
|
443
|
+
plugin.cb({
|
|
444
|
+
...app.webiny.routes,
|
|
445
|
+
context: app.webiny
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
} catch (ex) {
|
|
449
|
+
console.error(`Error while running the "RoutePlugin" ${routePluginName ? `(${routePluginName})` : ""} plugin in the beginning of the "createHandler" callable.`);
|
|
450
|
+
console.error(stringifyError(ex));
|
|
451
|
+
throw ex;
|
|
452
|
+
}
|
|
316
453
|
return app;
|
|
317
454
|
};
|
|
455
|
+
exports.createHandler = createHandler;
|
|
318
456
|
|
|
319
|
-
|
|
457
|
+
//# sourceMappingURL=fastify.js.map
|