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