@webiny/handler 0.0.0-unstable.5e7233243f → 0.0.0-unstable.606fc9c866
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 +19 -8
- package/Context.js.map +1 -1
- package/fastify.d.ts +1 -1
- package/fastify.js +225 -113
- 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 +17 -16
- package/plugins/BeforeHandlerPlugin.js +1 -11
- package/plugins/BeforeHandlerPlugin.js.map +1 -1
- package/plugins/EventPlugin.js +2 -9
- package/plugins/EventPlugin.js.map +1 -1
- package/plugins/HandlerErrorPlugin.js +1 -10
- 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 +1 -10
- 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 +1 -9
- package/plugins/RoutePlugin.js.map +1 -1
- package/types.d.ts +5 -2
- 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
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
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.d.ts
CHANGED
|
@@ -5,4 +5,4 @@ export interface CreateHandlerParams {
|
|
|
5
5
|
plugins: PluginCollection;
|
|
6
6
|
options?: ServerOptions;
|
|
7
7
|
}
|
|
8
|
-
export declare const createHandler: (params: CreateHandlerParams) => import("fastify").FastifyInstance<import("
|
|
8
|
+
export declare const createHandler: (params: CreateHandlerParams) => import("fastify").FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault> & PromiseLike<import("fastify").FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>>;
|
package/fastify.js
CHANGED
|
@@ -1,81 +1,75 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.createHandler = void 0;
|
|
9
|
-
|
|
10
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
-
|
|
8
|
+
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
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
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
39
|
-
|
|
40
|
-
const DEFAULT_HEADERS = _objectSpread({
|
|
22
|
+
var _ModifyFastifyPlugin = require("./plugins/ModifyFastifyPlugin");
|
|
23
|
+
var _HandlerOnRequestPlugin = require("./plugins/HandlerOnRequestPlugin");
|
|
24
|
+
const DEFAULT_HEADERS = (0, _objectSpread2.default)({
|
|
41
25
|
"Cache-Control": "no-store",
|
|
42
26
|
"Content-Type": "application/json; charset=utf-8",
|
|
43
27
|
"Access-Control-Allow-Origin": "*",
|
|
44
28
|
"Access-Control-Allow-Headers": "*",
|
|
45
29
|
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,DELETE,PUT,PATCH"
|
|
46
30
|
}, (0, _utils.getWebinyVersionHeaders)());
|
|
47
|
-
|
|
48
31
|
const getDefaultHeaders = routes => {
|
|
49
32
|
/**
|
|
50
33
|
* If we are accepting all headers, just output that one.
|
|
51
34
|
*/
|
|
52
35
|
const keys = Object.keys(routes);
|
|
53
36
|
const all = keys.every(key => routes[key].length > 0);
|
|
54
|
-
|
|
55
37
|
if (all) {
|
|
56
|
-
return
|
|
38
|
+
return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, DEFAULT_HEADERS), {}, {
|
|
57
39
|
"Access-Control-Allow-Methods": "*"
|
|
58
40
|
});
|
|
59
41
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
"Access-Control-Allow-Methods": keys.filter(key => {
|
|
63
|
-
const type = key;
|
|
64
|
-
|
|
42
|
+
return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, DEFAULT_HEADERS), {}, {
|
|
43
|
+
"Access-Control-Allow-Methods": keys.filter(type => {
|
|
65
44
|
if (!routes[type] || Array.isArray(routes[type]) === false) {
|
|
66
45
|
return false;
|
|
67
46
|
}
|
|
68
|
-
|
|
69
47
|
return routes[type].length > 0;
|
|
70
48
|
}).sort().join(",")
|
|
71
49
|
});
|
|
72
50
|
};
|
|
73
|
-
|
|
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
|
+
};
|
|
74
69
|
const OPTIONS_HEADERS = {
|
|
75
70
|
"Access-Control-Max-Age": "86400",
|
|
76
71
|
"Cache-Control": "public, max-age=86400"
|
|
77
72
|
};
|
|
78
|
-
|
|
79
73
|
const createHandler = params => {
|
|
80
74
|
const definedRoutes = {
|
|
81
75
|
POST: [],
|
|
@@ -84,20 +78,28 @@ const createHandler = params => {
|
|
|
84
78
|
DELETE: [],
|
|
85
79
|
PATCH: [],
|
|
86
80
|
PUT: [],
|
|
87
|
-
HEAD: []
|
|
81
|
+
HEAD: [],
|
|
82
|
+
COPY: [],
|
|
83
|
+
LOCK: [],
|
|
84
|
+
MKCOL: [],
|
|
85
|
+
MOVE: [],
|
|
86
|
+
PROPFIND: [],
|
|
87
|
+
PROPPATCH: [],
|
|
88
|
+
SEARCH: [],
|
|
89
|
+
TRACE: [],
|
|
90
|
+
UNLOCK: []
|
|
88
91
|
};
|
|
89
|
-
|
|
90
92
|
const throwOnDefinedRoute = (type, path, options) => {
|
|
91
93
|
if (type === "ALL") {
|
|
92
|
-
const all = Object.keys(definedRoutes).
|
|
94
|
+
const all = Object.keys(definedRoutes).find(key => {
|
|
93
95
|
const routes = definedRoutes[key];
|
|
94
96
|
return routes.includes(path);
|
|
95
97
|
});
|
|
96
|
-
|
|
97
98
|
if (!all) {
|
|
98
99
|
return;
|
|
99
100
|
}
|
|
100
|
-
|
|
101
|
+
console.error(`Error while registering onAll route. One of the routes is already defined.`);
|
|
102
|
+
console.error(JSON.stringify(all));
|
|
101
103
|
throw new _error.default(`You cannot override a route with onAll() method, please remove unnecessary route from the system.`, "OVERRIDE_ROUTE_ERROR", {
|
|
102
104
|
type,
|
|
103
105
|
path
|
|
@@ -107,59 +109,66 @@ const createHandler = params => {
|
|
|
107
109
|
} else if ((options === null || options === void 0 ? void 0 : options.override) === true) {
|
|
108
110
|
return;
|
|
109
111
|
}
|
|
110
|
-
|
|
112
|
+
console.error(`Error while trying to override route: [${type}] ${path}`);
|
|
111
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", {
|
|
112
114
|
type,
|
|
113
115
|
path
|
|
114
116
|
});
|
|
115
117
|
};
|
|
116
|
-
|
|
117
|
-
const addDefinedRoute = (inputType, path) => {
|
|
118
|
-
const type = inputType.toUpperCase();
|
|
119
|
-
|
|
118
|
+
const addDefinedRoute = (type, path) => {
|
|
120
119
|
if (!definedRoutes[type]) {
|
|
121
120
|
return;
|
|
122
121
|
} else if (definedRoutes[type].includes(path)) {
|
|
123
122
|
return;
|
|
124
123
|
}
|
|
125
|
-
|
|
126
124
|
definedRoutes[type].push(path);
|
|
127
125
|
};
|
|
128
126
|
/**
|
|
129
127
|
* We must attach the server to our internal context if we want to have it accessible.
|
|
130
128
|
*/
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const app = (0, _fastify.default)(_objectSpread({}, params.options || {}));
|
|
129
|
+
const app = (0, _fastify.default)((0, _objectSpread2.default)({}, params.options || {}));
|
|
134
130
|
/**
|
|
135
131
|
* We need to register routes in our system so we can output headers later on and dissallow overriding routes.
|
|
136
132
|
*/
|
|
137
|
-
|
|
138
133
|
app.addHook("onRoute", route => {
|
|
139
134
|
const method = route.method;
|
|
140
|
-
|
|
141
135
|
if (Array.isArray(method)) {
|
|
142
136
|
for (const m of method) {
|
|
143
137
|
addDefinedRoute(m, route.path);
|
|
144
138
|
}
|
|
145
|
-
|
|
146
139
|
return;
|
|
147
140
|
}
|
|
148
|
-
|
|
149
141
|
addDefinedRoute(method, route.path);
|
|
150
142
|
});
|
|
151
143
|
/**
|
|
144
|
+
* ############################
|
|
145
|
+
* Register the Fastify plugins.
|
|
146
|
+
*/
|
|
147
|
+
/**
|
|
148
|
+
* Package @fastify/cookie
|
|
152
149
|
*
|
|
150
|
+
* https://github.com/fastify/fastify-cookie
|
|
153
151
|
*/
|
|
154
|
-
|
|
155
152
|
app.register(_cookie.default, {
|
|
156
153
|
parseOptions: {} // options for parsing cookies
|
|
157
|
-
|
|
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
|
|
158
168
|
});
|
|
159
169
|
/**
|
|
160
170
|
* Route helpers - mostly for users.
|
|
161
171
|
*/
|
|
162
|
-
|
|
163
172
|
const routes = {
|
|
164
173
|
defined: definedRoutes,
|
|
165
174
|
onPost: (path, handler, options) => {
|
|
@@ -195,26 +204,33 @@ const createHandler = params => {
|
|
|
195
204
|
app.head(path, handler);
|
|
196
205
|
}
|
|
197
206
|
};
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
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
|
+
}
|
|
205
228
|
|
|
206
|
-
/**
|
|
207
|
-
* Inserted via webpack on build time.
|
|
208
|
-
*/
|
|
209
|
-
WEBINY_VERSION: process.env.WEBINY_VERSION,
|
|
210
|
-
server: app,
|
|
211
|
-
routes
|
|
212
|
-
});
|
|
213
229
|
/**
|
|
214
|
-
* 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.
|
|
215
231
|
*/
|
|
216
|
-
|
|
217
232
|
app.decorate("webiny", context);
|
|
233
|
+
|
|
218
234
|
/**
|
|
219
235
|
* We have few types of triggers:
|
|
220
236
|
* * Events - EventPlugin
|
|
@@ -222,94 +238,190 @@ const createHandler = params => {
|
|
|
222
238
|
*
|
|
223
239
|
* Routes are registered in fastify but events must be handled in package which implements cloud specific methods.
|
|
224
240
|
*/
|
|
225
|
-
|
|
226
241
|
const routePlugins = app.webiny.plugins.byType(_RoutePlugin.RoutePlugin.type);
|
|
242
|
+
|
|
227
243
|
/**
|
|
228
244
|
* Add routes to the system.
|
|
229
245
|
*/
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
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;
|
|
235
258
|
}
|
|
259
|
+
|
|
236
260
|
/**
|
|
237
261
|
* On every request we add default headers, which can be changed later.
|
|
238
262
|
* Also, if it is an options request, we skip everything after this hook and output options headers.
|
|
239
263
|
*/
|
|
240
|
-
|
|
241
|
-
|
|
242
264
|
app.addHook("onRequest", async (request, reply) => {
|
|
265
|
+
/**
|
|
266
|
+
* Our default headers are always set. Users can override them.
|
|
267
|
+
*/
|
|
243
268
|
const defaultHeaders = getDefaultHeaders(definedRoutes);
|
|
244
269
|
reply.headers(defaultHeaders);
|
|
245
|
-
|
|
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
|
+
*/
|
|
246
293
|
if (request.method !== "OPTIONS") {
|
|
247
294
|
return;
|
|
248
295
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
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;
|
|
256
305
|
}
|
|
257
|
-
|
|
258
|
-
raw.end("");
|
|
306
|
+
reply.headers((0, _objectSpread2.default)((0, _objectSpread2.default)({}, defaultHeaders), OPTIONS_HEADERS)).code(204).send("").hijack();
|
|
259
307
|
});
|
|
260
|
-
app.addHook("preParsing", async request => {
|
|
308
|
+
app.addHook("preParsing", async (request, reply) => {
|
|
261
309
|
app.webiny.request = request;
|
|
310
|
+
app.webiny.reply = reply;
|
|
262
311
|
const plugins = app.webiny.plugins.byType(_api.ContextPlugin.type);
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
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;
|
|
266
322
|
}
|
|
267
323
|
});
|
|
268
324
|
/**
|
|
269
325
|
*
|
|
270
326
|
*/
|
|
271
|
-
|
|
272
327
|
app.addHook("preHandler", async () => {
|
|
273
328
|
const plugins = app.webiny.plugins.byType(_BeforeHandlerPlugin.BeforeHandlerPlugin.type);
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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;
|
|
277
339
|
}
|
|
278
340
|
});
|
|
341
|
+
|
|
279
342
|
/**
|
|
280
343
|
*
|
|
281
344
|
*/
|
|
282
|
-
|
|
283
345
|
const preSerialization = async (_, __, payload) => {
|
|
284
346
|
const plugins = app.webiny.plugins.byType(_HandlerResultPlugin.HandlerResultPlugin.type);
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
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;
|
|
288
357
|
}
|
|
289
|
-
|
|
290
358
|
return payload;
|
|
291
359
|
};
|
|
292
|
-
|
|
293
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
|
+
});
|
|
294
374
|
app.addHook("onError", async (_, reply, error) => {
|
|
295
|
-
const plugins = app.webiny.plugins.byType(_HandlerErrorPlugin.HandlerErrorPlugin.type);
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
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
|
+
}));
|
|
302
392
|
const handler = (0, _middleware.middleware)(plugins.map(pl => {
|
|
303
393
|
return (context, error, next) => {
|
|
304
394
|
return pl.handle(context, error, next);
|
|
305
395
|
};
|
|
306
396
|
}));
|
|
307
397
|
await handler(app.webiny, error);
|
|
308
|
-
return reply
|
|
309
|
-
|
|
310
|
-
|
|
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();
|
|
311
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
|
+
}
|
|
312
425
|
return app;
|
|
313
426
|
};
|
|
314
|
-
|
|
315
427
|
exports.createHandler = createHandler;
|