@webiny/handler 0.0.0-unstable.97a151f74d → 0.0.0-unstable.99666aeb00
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 +19 -9
- package/Context.js +18 -7
- package/Context.js.map +1 -1
- package/fastify.js +188 -104
- package/fastify.js.map +1 -1
- package/index.d.ts +2 -1
- package/index.js +22 -27
- package/index.js.map +1 -1
- package/middleware.js +2 -8
- package/middleware.js.map +1 -1
- package/package.json +18 -19
- package/plugins/BeforeHandlerPlugin.js +0 -10
- package/plugins/BeforeHandlerPlugin.js.map +1 -1
- package/plugins/EventPlugin.js +1 -8
- package/plugins/EventPlugin.js.map +1 -1
- package/plugins/HandlerErrorPlugin.js +0 -9
- package/plugins/HandlerErrorPlugin.js.map +1 -1
- package/plugins/HandlerOnRequestPlugin.d.ts +20 -0
- package/plugins/HandlerOnRequestPlugin.js +25 -0
- package/plugins/HandlerOnRequestPlugin.js.map +1 -0
- package/plugins/HandlerResultPlugin.js +0 -9
- package/plugins/HandlerResultPlugin.js.map +1 -1
- package/plugins/ModifyFastifyPlugin.d.ts +13 -0
- package/plugins/ModifyFastifyPlugin.js +25 -0
- package/plugins/ModifyFastifyPlugin.js.map +1 -0
- package/plugins/RoutePlugin.js +0 -8
- package/plugins/RoutePlugin.js.map +1 -1
- package/types.d.ts +4 -0
- package/types.js +0 -1
- package/types.js.map +1 -1
package/Context.d.ts
CHANGED
|
@@ -1,13 +1,23 @@
|
|
|
1
|
-
import { Context as BaseContext, ContextParams as BaseContextParams } from "@webiny/api";
|
|
2
|
-
import { Context as
|
|
1
|
+
import { Context as BaseContext, ContextParams as BaseContextParams, ContextPlugin as BaseContextPlugin, ContextPluginCallable as BaseContextPluginCallable } from "@webiny/api";
|
|
2
|
+
import { Context as ContextInterface } from "./types";
|
|
3
3
|
export interface ContextParams extends BaseContextParams {
|
|
4
|
-
server:
|
|
5
|
-
routes:
|
|
4
|
+
server: ContextInterface["server"];
|
|
5
|
+
routes: ContextInterface["routes"];
|
|
6
6
|
}
|
|
7
|
-
export declare class Context extends BaseContext implements
|
|
8
|
-
readonly server:
|
|
9
|
-
readonly routes:
|
|
10
|
-
handlerClient:
|
|
11
|
-
request:
|
|
7
|
+
export declare class Context extends BaseContext implements ContextInterface {
|
|
8
|
+
readonly server: ContextInterface["server"];
|
|
9
|
+
readonly routes: ContextInterface["routes"];
|
|
10
|
+
handlerClient: ContextInterface["handlerClient"];
|
|
11
|
+
request: ContextInterface["request"];
|
|
12
|
+
reply: ContextInterface["reply"];
|
|
12
13
|
constructor(params: ContextParams);
|
|
13
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* We need to extend and reexport the ContextPlugin, ContextPluginCallable and createContextPlugin to support extended context.
|
|
17
|
+
*
|
|
18
|
+
* This can be removed when we introduce the type augmentation.
|
|
19
|
+
*/
|
|
20
|
+
export declare type ContextPluginCallable<T extends ContextInterface = ContextInterface> = BaseContextPluginCallable<T>;
|
|
21
|
+
export declare class ContextPlugin<T extends ContextInterface = ContextInterface> extends BaseContextPlugin<T> {
|
|
22
|
+
}
|
|
23
|
+
export declare const createContextPlugin: <T extends ContextInterface = ContextInterface>(callable: ContextPluginCallable<T>) => BaseContextPlugin<T>;
|
package/Context.js
CHANGED
|
@@ -1,29 +1,40 @@
|
|
|
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
|
-
exports.Context = void 0;
|
|
9
|
-
|
|
7
|
+
exports.createContextPlugin = exports.ContextPlugin = exports.Context = void 0;
|
|
10
8
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
-
|
|
12
9
|
var _api = require("@webiny/api");
|
|
13
|
-
|
|
14
10
|
class Context extends _api.Context {
|
|
15
11
|
// @ts-ignore
|
|
12
|
+
|
|
16
13
|
// @ts-ignore
|
|
14
|
+
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
|
|
17
17
|
constructor(params) {
|
|
18
18
|
super(params);
|
|
19
19
|
(0, _defineProperty2.default)(this, "server", void 0);
|
|
20
20
|
(0, _defineProperty2.default)(this, "routes", void 0);
|
|
21
21
|
(0, _defineProperty2.default)(this, "handlerClient", void 0);
|
|
22
22
|
(0, _defineProperty2.default)(this, "request", void 0);
|
|
23
|
+
(0, _defineProperty2.default)(this, "reply", void 0);
|
|
23
24
|
this.server = params.server;
|
|
24
25
|
this.routes = params.routes;
|
|
25
26
|
}
|
|
26
|
-
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
/**
|
|
30
|
+
* We need to extend and reexport the ContextPlugin, ContextPluginCallable and createContextPlugin to support extended context.
|
|
31
|
+
*
|
|
32
|
+
* This can be removed when we introduce the type augmentation.
|
|
33
|
+
*/
|
|
34
|
+
exports.Context = Context;
|
|
35
|
+
class ContextPlugin extends _api.ContextPlugin {}
|
|
36
|
+
exports.ContextPlugin = ContextPlugin;
|
|
37
|
+
const createContextPlugin = callable => {
|
|
38
|
+
return (0, _api.createContextPlugin)(callable);
|
|
39
|
+
};
|
|
40
|
+
exports.createContextPlugin = createContextPlugin;
|
package/Context.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["Context","BaseContext","constructor","params","server","routes"],"sources":["Context.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"names":["Context","BaseContext","constructor","params","server","routes","ContextPlugin","BaseContextPlugin","createContextPlugin","callable","baseCreateContextPlugin"],"sources":["Context.ts"],"sourcesContent":["import {\n Context as BaseContext,\n ContextParams as BaseContextParams,\n ContextPlugin as BaseContextPlugin,\n ContextPluginCallable as BaseContextPluginCallable,\n createContextPlugin as baseCreateContextPlugin\n} from \"@webiny/api\";\nimport { Context as ContextInterface } from \"~/types\";\n\nexport interface ContextParams extends BaseContextParams {\n server: ContextInterface[\"server\"];\n routes: ContextInterface[\"routes\"];\n}\n\nexport class Context extends BaseContext implements ContextInterface {\n public readonly server: ContextInterface[\"server\"];\n public readonly routes: ContextInterface[\"routes\"];\n // @ts-ignore\n public handlerClient: ContextInterface[\"handlerClient\"];\n // @ts-ignore\n public request: ContextInterface[\"request\"];\n // @ts-ignore\n public reply: ContextInterface[\"reply\"];\n\n public constructor(params: ContextParams) {\n super(params);\n this.server = params.server;\n this.routes = params.routes;\n }\n}\n\n/**\n * We need to extend and reexport the ContextPlugin, ContextPluginCallable and createContextPlugin to support extended context.\n *\n * This can be removed when we introduce the type augmentation.\n */\nexport type ContextPluginCallable<T extends ContextInterface = ContextInterface> =\n BaseContextPluginCallable<T>;\n\nexport class ContextPlugin<\n T extends ContextInterface = ContextInterface\n> extends BaseContextPlugin<T> {}\n\nexport const createContextPlugin = <T extends ContextInterface = ContextInterface>(\n callable: ContextPluginCallable<T>\n) => {\n return baseCreateContextPlugin<T>(callable);\n};\n"],"mappings":";;;;;;;;AAAA;AAcO,MAAMA,OAAO,SAASC,YAAW,CAA6B;EAGjE;;EAEA;;EAEA;;EAGOC,WAAW,CAACC,MAAqB,EAAE;IACtC,KAAK,CAACA,MAAM,CAAC;IAAC;IAAA;IAAA;IAAA;IAAA;IACd,IAAI,CAACC,MAAM,GAAGD,MAAM,CAACC,MAAM;IAC3B,IAAI,CAACC,MAAM,GAAGF,MAAM,CAACE,MAAM;EAC/B;AACJ;;AAEA;AACA;AACA;AACA;AACA;AAJA;AAQO,MAAMC,aAAa,SAEhBC,kBAAiB,CAAI;AAAE;AAE1B,MAAMC,mBAAmB,GAC5BC,QAAkC,IACjC;EACD,OAAO,IAAAC,wBAAuB,EAAID,QAAQ,CAAC;AAC/C,CAAC;AAAC"}
|
package/fastify.js
CHANGED
|
@@ -1,40 +1,26 @@
|
|
|
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
8
|
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
|
-
|
|
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
|
-
|
|
26
16
|
var _compress = _interopRequireDefault(require("@fastify/compress"));
|
|
27
|
-
|
|
28
17
|
var _middleware = require("./middleware");
|
|
29
|
-
|
|
30
18
|
var _api = require("@webiny/api");
|
|
31
|
-
|
|
32
19
|
var _BeforeHandlerPlugin = require("./plugins/BeforeHandlerPlugin");
|
|
33
|
-
|
|
34
20
|
var _HandlerResultPlugin = require("./plugins/HandlerResultPlugin");
|
|
35
|
-
|
|
36
21
|
var _HandlerErrorPlugin = require("./plugins/HandlerErrorPlugin");
|
|
37
|
-
|
|
22
|
+
var _ModifyFastifyPlugin = require("./plugins/ModifyFastifyPlugin");
|
|
23
|
+
var _HandlerOnRequestPlugin = require("./plugins/HandlerOnRequestPlugin");
|
|
38
24
|
const DEFAULT_HEADERS = (0, _objectSpread2.default)({
|
|
39
25
|
"Cache-Control": "no-store",
|
|
40
26
|
"Content-Type": "application/json; charset=utf-8",
|
|
@@ -42,38 +28,48 @@ const DEFAULT_HEADERS = (0, _objectSpread2.default)({
|
|
|
42
28
|
"Access-Control-Allow-Headers": "*",
|
|
43
29
|
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,DELETE,PUT,PATCH"
|
|
44
30
|
}, (0, _utils.getWebinyVersionHeaders)());
|
|
45
|
-
|
|
46
31
|
const getDefaultHeaders = routes => {
|
|
47
32
|
/**
|
|
48
33
|
* If we are accepting all headers, just output that one.
|
|
49
34
|
*/
|
|
50
35
|
const keys = Object.keys(routes);
|
|
51
36
|
const all = keys.every(key => routes[key].length > 0);
|
|
52
|
-
|
|
53
37
|
if (all) {
|
|
54
38
|
return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, DEFAULT_HEADERS), {}, {
|
|
55
39
|
"Access-Control-Allow-Methods": "*"
|
|
56
40
|
});
|
|
57
41
|
}
|
|
58
|
-
|
|
59
42
|
return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, DEFAULT_HEADERS), {}, {
|
|
60
|
-
"Access-Control-Allow-Methods": keys.filter(
|
|
61
|
-
const type = key;
|
|
62
|
-
|
|
43
|
+
"Access-Control-Allow-Methods": keys.filter(type => {
|
|
63
44
|
if (!routes[type] || Array.isArray(routes[type]) === false) {
|
|
64
45
|
return false;
|
|
65
46
|
}
|
|
66
|
-
|
|
67
47
|
return routes[type].length > 0;
|
|
68
48
|
}).sort().join(",")
|
|
69
49
|
});
|
|
70
50
|
};
|
|
71
|
-
|
|
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
|
+
};
|
|
72
69
|
const OPTIONS_HEADERS = {
|
|
73
70
|
"Access-Control-Max-Age": "86400",
|
|
74
71
|
"Cache-Control": "public, max-age=86400"
|
|
75
72
|
};
|
|
76
|
-
|
|
77
73
|
const createHandler = params => {
|
|
78
74
|
const definedRoutes = {
|
|
79
75
|
POST: [],
|
|
@@ -93,18 +89,17 @@ const createHandler = params => {
|
|
|
93
89
|
TRACE: [],
|
|
94
90
|
UNLOCK: []
|
|
95
91
|
};
|
|
96
|
-
|
|
97
92
|
const throwOnDefinedRoute = (type, path, options) => {
|
|
98
93
|
if (type === "ALL") {
|
|
99
|
-
const all = Object.keys(definedRoutes).
|
|
94
|
+
const all = Object.keys(definedRoutes).find(key => {
|
|
100
95
|
const routes = definedRoutes[key];
|
|
101
96
|
return routes.includes(path);
|
|
102
97
|
});
|
|
103
|
-
|
|
104
98
|
if (!all) {
|
|
105
99
|
return;
|
|
106
100
|
}
|
|
107
|
-
|
|
101
|
+
console.error(`Error while registering onAll route. One of the routes is already defined.`);
|
|
102
|
+
console.error(JSON.stringify(all));
|
|
108
103
|
throw new _error.default(`You cannot override a route with onAll() method, please remove unnecessary route from the system.`, "OVERRIDE_ROUTE_ERROR", {
|
|
109
104
|
type,
|
|
110
105
|
path
|
|
@@ -114,68 +109,54 @@ const createHandler = params => {
|
|
|
114
109
|
} else if ((options === null || options === void 0 ? void 0 : options.override) === true) {
|
|
115
110
|
return;
|
|
116
111
|
}
|
|
117
|
-
|
|
112
|
+
console.error(`Error while trying to override route: [${type}] ${path}`);
|
|
118
113
|
throw new _error.default(`When you are trying to override existing route, you must send "override" parameter when adding that route.`, "OVERRIDE_ROUTE_ERROR", {
|
|
119
114
|
type,
|
|
120
115
|
path
|
|
121
116
|
});
|
|
122
117
|
};
|
|
123
|
-
|
|
124
|
-
const addDefinedRoute = (inputType, path) => {
|
|
125
|
-
const type = inputType.toUpperCase();
|
|
126
|
-
|
|
118
|
+
const addDefinedRoute = (type, path) => {
|
|
127
119
|
if (!definedRoutes[type]) {
|
|
128
120
|
return;
|
|
129
121
|
} else if (definedRoutes[type].includes(path)) {
|
|
130
122
|
return;
|
|
131
123
|
}
|
|
132
|
-
|
|
133
124
|
definedRoutes[type].push(path);
|
|
134
125
|
};
|
|
135
126
|
/**
|
|
136
127
|
* We must attach the server to our internal context if we want to have it accessible.
|
|
137
128
|
*/
|
|
138
|
-
|
|
139
|
-
|
|
140
129
|
const app = (0, _fastify.default)((0, _objectSpread2.default)({}, params.options || {}));
|
|
141
130
|
/**
|
|
142
131
|
* We need to register routes in our system so we can output headers later on and dissallow overriding routes.
|
|
143
132
|
*/
|
|
144
|
-
|
|
145
133
|
app.addHook("onRoute", route => {
|
|
146
134
|
const method = route.method;
|
|
147
|
-
|
|
148
135
|
if (Array.isArray(method)) {
|
|
149
136
|
for (const m of method) {
|
|
150
137
|
addDefinedRoute(m, route.path);
|
|
151
138
|
}
|
|
152
|
-
|
|
153
139
|
return;
|
|
154
140
|
}
|
|
155
|
-
|
|
156
141
|
addDefinedRoute(method, route.path);
|
|
157
142
|
});
|
|
158
143
|
/**
|
|
159
144
|
* ############################
|
|
160
145
|
* Register the Fastify plugins.
|
|
161
146
|
*/
|
|
162
|
-
|
|
163
147
|
/**
|
|
164
148
|
* Package @fastify/cookie
|
|
165
149
|
*
|
|
166
150
|
* https://github.com/fastify/fastify-cookie
|
|
167
151
|
*/
|
|
168
|
-
|
|
169
152
|
app.register(_cookie.default, {
|
|
170
153
|
parseOptions: {} // options for parsing cookies
|
|
171
|
-
|
|
172
154
|
});
|
|
173
155
|
/**
|
|
174
156
|
* Package @fastify/compress
|
|
175
157
|
*
|
|
176
158
|
* https://github.com/fastify/fastify-compress
|
|
177
159
|
*/
|
|
178
|
-
|
|
179
160
|
app.register(_compress.default, {
|
|
180
161
|
global: true,
|
|
181
162
|
threshold: 1024,
|
|
@@ -188,7 +169,6 @@ const createHandler = params => {
|
|
|
188
169
|
/**
|
|
189
170
|
* Route helpers - mostly for users.
|
|
190
171
|
*/
|
|
191
|
-
|
|
192
172
|
const routes = {
|
|
193
173
|
defined: definedRoutes,
|
|
194
174
|
onPost: (path, handler, options) => {
|
|
@@ -224,26 +204,33 @@ const createHandler = params => {
|
|
|
224
204
|
app.head(path, handler);
|
|
225
205
|
}
|
|
226
206
|
};
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
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
|
|
222
|
+
});
|
|
223
|
+
} catch (ex) {
|
|
224
|
+
console.error(`Error while constructing the Context.`);
|
|
225
|
+
console.error(stringifyError(ex));
|
|
226
|
+
throw ex;
|
|
227
|
+
}
|
|
234
228
|
|
|
235
|
-
/**
|
|
236
|
-
* Inserted via webpack on build time.
|
|
237
|
-
*/
|
|
238
|
-
WEBINY_VERSION: process.env.WEBINY_VERSION,
|
|
239
|
-
server: app,
|
|
240
|
-
routes
|
|
241
|
-
});
|
|
242
229
|
/**
|
|
243
|
-
* We are attaching our custom context to webiny variable on the fastify app so it is accessible everywhere
|
|
230
|
+
* We are attaching our custom context to webiny variable on the fastify app, so it is accessible everywhere.
|
|
244
231
|
*/
|
|
245
|
-
|
|
246
232
|
app.decorate("webiny", context);
|
|
233
|
+
|
|
247
234
|
/**
|
|
248
235
|
* We have few types of triggers:
|
|
249
236
|
* * Events - EventPlugin
|
|
@@ -251,93 +238,190 @@ const createHandler = params => {
|
|
|
251
238
|
*
|
|
252
239
|
* Routes are registered in fastify but events must be handled in package which implements cloud specific methods.
|
|
253
240
|
*/
|
|
254
|
-
|
|
255
241
|
const routePlugins = app.webiny.plugins.byType(_RoutePlugin.RoutePlugin.type);
|
|
242
|
+
|
|
256
243
|
/**
|
|
257
244
|
* Add routes to the system.
|
|
258
245
|
*/
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
246
|
+
let routePluginName;
|
|
247
|
+
try {
|
|
248
|
+
for (const plugin of routePlugins) {
|
|
249
|
+
routePluginName = plugin.name;
|
|
250
|
+
plugin.cb((0, _objectSpread2.default)((0, _objectSpread2.default)({}, app.webiny.routes), {}, {
|
|
251
|
+
context: app.webiny
|
|
252
|
+
}));
|
|
253
|
+
}
|
|
254
|
+
} catch (ex) {
|
|
255
|
+
console.error(`Error while running the "RoutePlugin" ${routePluginName ? `(${routePluginName})` : ""} plugin in the beginning of the "createHandler" callable.`);
|
|
256
|
+
console.error(stringifyError(ex));
|
|
257
|
+
throw ex;
|
|
264
258
|
}
|
|
259
|
+
|
|
265
260
|
/**
|
|
266
261
|
* On every request we add default headers, which can be changed later.
|
|
267
262
|
* Also, if it is an options request, we skip everything after this hook and output options headers.
|
|
268
263
|
*/
|
|
269
|
-
|
|
270
|
-
|
|
271
264
|
app.addHook("onRequest", async (request, reply) => {
|
|
265
|
+
/**
|
|
266
|
+
* Our default headers are always set. Users can override them.
|
|
267
|
+
*/
|
|
272
268
|
const defaultHeaders = getDefaultHeaders(definedRoutes);
|
|
273
269
|
reply.headers(defaultHeaders);
|
|
274
|
-
|
|
270
|
+
/**
|
|
271
|
+
* Users can define their own custom handlers for the onRequest event - so let's run them first.
|
|
272
|
+
*/
|
|
273
|
+
const plugins = app.webiny.plugins.byType(_HandlerOnRequestPlugin.HandlerOnRequestPlugin.type);
|
|
274
|
+
let name;
|
|
275
|
+
try {
|
|
276
|
+
for (const plugin of plugins) {
|
|
277
|
+
name = plugin.name;
|
|
278
|
+
const result = await plugin.exec(request, reply);
|
|
279
|
+
if (result === false) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
} catch (ex) {
|
|
284
|
+
console.error(`Error while running the "HandlerOnRequestPlugin" ${name ? `(${name})` : ""} plugin in the onRequest hook.`);
|
|
285
|
+
console.error(stringifyError(ex));
|
|
286
|
+
throw ex;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* When we receive the OPTIONS request, we end it before it goes any further as there is no need for anything to run after this - at least for our use cases.
|
|
290
|
+
*
|
|
291
|
+
* Users can prevent this by creating their own HandlerOnRequestPlugin and returning false as the result of the callable.
|
|
292
|
+
*/
|
|
275
293
|
if (request.method !== "OPTIONS") {
|
|
276
294
|
return;
|
|
277
295
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
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;
|
|
284
305
|
}
|
|
285
|
-
|
|
286
|
-
raw.end("");
|
|
306
|
+
reply.headers((0, _objectSpread2.default)((0, _objectSpread2.default)({}, defaultHeaders), OPTIONS_HEADERS)).code(204).send("").hijack();
|
|
287
307
|
});
|
|
288
|
-
app.addHook("preParsing", async request => {
|
|
308
|
+
app.addHook("preParsing", async (request, reply) => {
|
|
289
309
|
app.webiny.request = request;
|
|
310
|
+
app.webiny.reply = reply;
|
|
290
311
|
const plugins = app.webiny.plugins.byType(_api.ContextPlugin.type);
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
312
|
+
let name;
|
|
313
|
+
try {
|
|
314
|
+
for (const plugin of plugins) {
|
|
315
|
+
name = plugin.name;
|
|
316
|
+
await plugin.apply(app.webiny);
|
|
317
|
+
}
|
|
318
|
+
} catch (ex) {
|
|
319
|
+
console.error(`Error while running the "ContextPlugin" ${name ? `(${name})` : ""} plugin in the preParsing hook.`);
|
|
320
|
+
console.error(stringifyError(ex));
|
|
321
|
+
throw ex;
|
|
294
322
|
}
|
|
295
323
|
});
|
|
296
324
|
/**
|
|
297
325
|
*
|
|
298
326
|
*/
|
|
299
|
-
|
|
300
327
|
app.addHook("preHandler", async () => {
|
|
301
328
|
const plugins = app.webiny.plugins.byType(_BeforeHandlerPlugin.BeforeHandlerPlugin.type);
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
329
|
+
let name;
|
|
330
|
+
try {
|
|
331
|
+
for (const plugin of plugins) {
|
|
332
|
+
name = plugin.name;
|
|
333
|
+
await plugin.apply(app.webiny);
|
|
334
|
+
}
|
|
335
|
+
} catch (ex) {
|
|
336
|
+
console.error(`Error while running the "BeforeHandlerPlugin" ${name ? `(${name})` : ""} plugin in the preHandler hook.`);
|
|
337
|
+
console.error(stringifyError(ex));
|
|
338
|
+
throw ex;
|
|
305
339
|
}
|
|
306
340
|
});
|
|
341
|
+
|
|
307
342
|
/**
|
|
308
343
|
*
|
|
309
344
|
*/
|
|
310
|
-
|
|
311
345
|
const preSerialization = async (_, __, payload) => {
|
|
312
346
|
const plugins = app.webiny.plugins.byType(_HandlerResultPlugin.HandlerResultPlugin.type);
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
347
|
+
let name;
|
|
348
|
+
try {
|
|
349
|
+
for (const plugin of plugins) {
|
|
350
|
+
name = plugin.name;
|
|
351
|
+
await plugin.handle(app.webiny, payload);
|
|
352
|
+
}
|
|
353
|
+
} catch (ex) {
|
|
354
|
+
console.error(`Error while running the "HandlerResultPlugin" ${name ? `(${name})` : ""} plugin in the preSerialization hook.`);
|
|
355
|
+
console.error(stringifyError(ex));
|
|
356
|
+
throw ex;
|
|
316
357
|
}
|
|
317
|
-
|
|
318
358
|
return payload;
|
|
319
359
|
};
|
|
320
|
-
|
|
321
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
|
+
});
|
|
322
374
|
app.addHook("onError", async (_, reply, error) => {
|
|
323
|
-
const plugins = app.webiny.plugins.byType(_HandlerErrorPlugin.HandlerErrorPlugin.type);
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
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
|
+
}));
|
|
330
392
|
const handler = (0, _middleware.middleware)(plugins.map(pl => {
|
|
331
393
|
return (context, error, next) => {
|
|
332
394
|
return pl.handle(context, error, next);
|
|
333
395
|
};
|
|
334
396
|
}));
|
|
335
397
|
await handler(app.webiny, error);
|
|
336
|
-
return reply
|
|
337
|
-
|
|
338
|
-
|
|
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();
|
|
339
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
|
+
}
|
|
340
425
|
return app;
|
|
341
426
|
};
|
|
342
|
-
|
|
343
427
|
exports.createHandler = createHandler;
|