@webiny/handler 6.4.5-beta.0 → 6.6.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/exports/api.d.ts +0 -1
- package/exports/api.js +0 -1
- package/index.d.ts +4 -17
- package/index.js +3 -17
- package/package.json +6 -11
- package/plugins/RegisterExtensionPlugin.d.ts +10 -0
- package/plugins/RegisterExtensionPlugin.js +9 -1
- package/plugins/RegisterExtensionPlugin.js.map +1 -1
- package/types.d.ts +21 -43
- package/types.js +0 -1
- package/Context.d.ts +0 -22
- package/Context.js +0 -13
- package/Context.js.map +0 -1
- package/PreHandler/IPreHandler.d.ts +0 -9
- package/PreHandler/IPreHandler.js +0 -8
- package/PreHandler/IPreHandler.js.map +0 -1
- package/PreHandler/IfNotOptionsRequest.d.ts +0 -9
- package/PreHandler/IfNotOptionsRequest.js +0 -18
- package/PreHandler/IfNotOptionsRequest.js.map +0 -1
- package/PreHandler/IfOptionsRequest.d.ts +0 -9
- package/PreHandler/IfOptionsRequest.js +0 -18
- package/PreHandler/IfOptionsRequest.js.map +0 -1
- package/PreHandler/PreHandler.d.ts +0 -9
- package/PreHandler/PreHandler.js +0 -16
- package/PreHandler/PreHandler.js.map +0 -1
- package/PreHandler/ProcessBeforeHandlerPlugins.d.ts +0 -10
- package/PreHandler/ProcessBeforeHandlerPlugins.js +0 -25
- package/PreHandler/ProcessBeforeHandlerPlugins.js.map +0 -1
- package/PreHandler/ProcessContextPlugins.d.ts +0 -10
- package/PreHandler/ProcessContextPlugins.js +0 -25
- package/PreHandler/ProcessContextPlugins.js.map +0 -1
- package/PreHandler/ProcessHandlerOnRequestPlugins.d.ts +0 -10
- package/PreHandler/ProcessHandlerOnRequestPlugins.js +0 -25
- package/PreHandler/ProcessHandlerOnRequestPlugins.js.map +0 -1
- package/PreHandler/RegisterExtensions.d.ts +0 -10
- package/PreHandler/RegisterExtensions.js +0 -13
- package/PreHandler/RegisterExtensions.js.map +0 -1
- package/PreHandler/SendEarlyOptionsResponse.d.ts +0 -9
- package/PreHandler/SendEarlyOptionsResponse.js +0 -26
- package/PreHandler/SendEarlyOptionsResponse.js.map +0 -1
- package/PreHandler/SetDefaultHeaders.d.ts +0 -9
- package/PreHandler/SetDefaultHeaders.js +0 -46
- package/PreHandler/SetDefaultHeaders.js.map +0 -1
- package/abstractions/Route.d.ts +0 -25
- package/abstractions/Route.js +0 -30
- package/abstractions/Route.js.map +0 -1
- package/fastify.d.ts +0 -11
- package/fastify.js +0 -321
- package/fastify.js.map +0 -1
- package/plugins/BeforeHandlerPlugin.d.ts +0 -12
- package/plugins/BeforeHandlerPlugin.js +0 -18
- package/plugins/BeforeHandlerPlugin.js.map +0 -1
- package/plugins/EventPlugin.d.ts +0 -25
- package/plugins/EventPlugin.js +0 -14
- package/plugins/EventPlugin.js.map +0 -1
- package/plugins/HandlerErrorPlugin.d.ts +0 -15
- package/plugins/HandlerErrorPlugin.js +0 -17
- package/plugins/HandlerErrorPlugin.js.map +0 -1
- package/plugins/HandlerOnRequestPlugin.d.ts +0 -21
- package/plugins/HandlerOnRequestPlugin.js +0 -17
- package/plugins/HandlerOnRequestPlugin.js.map +0 -1
- package/plugins/HandlerResultPlugin.d.ts +0 -12
- package/plugins/HandlerResultPlugin.js +0 -17
- package/plugins/HandlerResultPlugin.js.map +0 -1
- package/plugins/ModifyFastifyPlugin.d.ts +0 -13
- package/plugins/ModifyFastifyPlugin.js +0 -17
- package/plugins/ModifyFastifyPlugin.js.map +0 -1
- package/plugins/ModifyResponseHeadersPlugin.d.ts +0 -14
- package/plugins/ModifyResponseHeadersPlugin.js +0 -19
- package/plugins/ModifyResponseHeadersPlugin.js.map +0 -1
- package/plugins/OnRequestResponseSendPlugin.d.ts +0 -26
- package/plugins/OnRequestResponseSendPlugin.js +0 -17
- package/plugins/OnRequestResponseSendPlugin.js.map +0 -1
- package/plugins/OnRequestTimeoutPlugin.d.ts +0 -12
- package/plugins/OnRequestTimeoutPlugin.js +0 -17
- package/plugins/OnRequestTimeoutPlugin.js.map +0 -1
- package/plugins/RoutePlugin.d.ts +0 -23
- package/plugins/RoutePlugin.js +0 -14
- package/plugins/RoutePlugin.js.map +0 -1
- package/suppressPunycodeWarnings.d.ts +0 -1
- package/suppressPunycodeWarnings.js +0 -7
- package/suppressPunycodeWarnings.js.map +0 -1
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { getWebinyVersionHeaders } from "@webiny/utils";
|
|
2
|
-
import { ResponseHeaders } from "../ResponseHeaders.js";
|
|
3
|
-
import { Action } from "./IPreHandler.js";
|
|
4
|
-
function createDefaultHeaders() {
|
|
5
|
-
return ResponseHeaders.create({
|
|
6
|
-
"content-type": "application/json; charset=utf-8",
|
|
7
|
-
"cache-control": "no-store",
|
|
8
|
-
"access-control-allow-origin": "*",
|
|
9
|
-
"access-control-allow-headers": "*",
|
|
10
|
-
"access-control-allow-methods": "OPTIONS,POST,GET,DELETE,PUT,PATCH",
|
|
11
|
-
...getWebinyVersionHeaders()
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
const getDefaultOptionsHeaders = ()=>ResponseHeaders.create({
|
|
15
|
-
"access-control-max-age": "86400",
|
|
16
|
-
"cache-control": "public, max-age=86400"
|
|
17
|
-
});
|
|
18
|
-
const getDefaultHeaders = (routes)=>{
|
|
19
|
-
const headers = createDefaultHeaders();
|
|
20
|
-
const keys = Object.keys(routes);
|
|
21
|
-
const all = keys.every((key)=>routes[key].length > 0);
|
|
22
|
-
if (all) headers.set("access-control-allow-methods", "*");
|
|
23
|
-
else {
|
|
24
|
-
const allowedMethods = keys.filter((type)=>{
|
|
25
|
-
if (!routes[type] || !Array.isArray(routes[type])) return false;
|
|
26
|
-
return routes[type].length > 0;
|
|
27
|
-
}).sort().join(",");
|
|
28
|
-
headers.set("access-control-allow-methods", allowedMethods);
|
|
29
|
-
}
|
|
30
|
-
return headers;
|
|
31
|
-
};
|
|
32
|
-
class SetDefaultHeaders {
|
|
33
|
-
constructor(definedRoutes){
|
|
34
|
-
this.definedRoutes = definedRoutes;
|
|
35
|
-
}
|
|
36
|
-
execute(request, reply) {
|
|
37
|
-
const isOptionsRequest = "OPTIONS" === request.method;
|
|
38
|
-
const defaultHeaders = getDefaultHeaders(this.definedRoutes);
|
|
39
|
-
const initialHeaders = isOptionsRequest ? defaultHeaders.merge(getDefaultOptionsHeaders()) : defaultHeaders;
|
|
40
|
-
reply.headers(initialHeaders.getHeaders());
|
|
41
|
-
return Action.CONTINUE;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
export { SetDefaultHeaders };
|
|
45
|
-
|
|
46
|
-
//# sourceMappingURL=SetDefaultHeaders.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"PreHandler/SetDefaultHeaders.js","sources":["../../src/PreHandler/SetDefaultHeaders.ts"],"sourcesContent":["import type { FastifyReply, FastifyRequest } from \"fastify\";\nimport { getWebinyVersionHeaders } from \"@webiny/utils\";\nimport { ResponseHeaders } from \"~/ResponseHeaders.js\";\nimport type { DefinedContextRoutes, HTTPMethods } from \"~/types.js\";\nimport type { IPreHandler } from \"~/PreHandler/IPreHandler.js\";\nimport { Action } from \"~/PreHandler/IPreHandler.js\";\n\nfunction createDefaultHeaders() {\n return ResponseHeaders.create({\n \"content-type\": \"application/json; charset=utf-8\",\n \"cache-control\": \"no-store\",\n \"access-control-allow-origin\": \"*\",\n \"access-control-allow-headers\": \"*\",\n \"access-control-allow-methods\": \"OPTIONS,POST,GET,DELETE,PUT,PATCH\",\n ...getWebinyVersionHeaders()\n });\n}\n\nconst getDefaultOptionsHeaders = () => {\n return ResponseHeaders.create({\n \"access-control-max-age\": \"86400\",\n \"cache-control\": \"public, max-age=86400\"\n });\n};\n\nconst getDefaultHeaders = (routes: DefinedContextRoutes): ResponseHeaders => {\n const headers = createDefaultHeaders();\n\n /**\n * If we are accepting all headers, just output that one.\n */\n const keys = Object.keys(routes) as HTTPMethods[];\n const all = keys.every(key => routes[key].length > 0);\n if (all) {\n headers.set(\"access-control-allow-methods\", \"*\");\n } else {\n const allowedMethods = keys\n .filter(type => {\n if (!routes[type] || !Array.isArray(routes[type])) {\n return false;\n }\n return routes[type].length > 0;\n })\n .sort()\n .join(\",\");\n\n headers.set(\"access-control-allow-methods\", allowedMethods);\n }\n\n return headers;\n};\n\nexport class SetDefaultHeaders implements IPreHandler {\n private readonly definedRoutes: DefinedContextRoutes;\n\n constructor(definedRoutes: DefinedContextRoutes) {\n this.definedRoutes = definedRoutes;\n }\n\n execute(request: FastifyRequest, reply: FastifyReply) {\n const isOptionsRequest = request.method === \"OPTIONS\";\n /**\n * Our default headers are always set. Users can override them.\n */\n const defaultHeaders = getDefaultHeaders(this.definedRoutes);\n\n const initialHeaders = isOptionsRequest\n ? defaultHeaders.merge(getDefaultOptionsHeaders())\n : defaultHeaders;\n\n reply.headers(initialHeaders.getHeaders());\n\n return Action.CONTINUE;\n }\n}\n"],"names":["createDefaultHeaders","ResponseHeaders","getWebinyVersionHeaders","getDefaultOptionsHeaders","getDefaultHeaders","routes","headers","keys","Object","all","key","allowedMethods","type","Array","SetDefaultHeaders","definedRoutes","request","reply","isOptionsRequest","defaultHeaders","initialHeaders","Action"],"mappings":";;;AAOA,SAASA;IACL,OAAOC,gBAAgB,MAAM,CAAC;QAC1B,gBAAgB;QAChB,iBAAiB;QACjB,+BAA+B;QAC/B,gCAAgC;QAChC,gCAAgC;QAChC,GAAGC,yBAAyB;IAChC;AACJ;AAEA,MAAMC,2BAA2B,IACtBF,gBAAgB,MAAM,CAAC;QAC1B,0BAA0B;QAC1B,iBAAiB;IACrB;AAGJ,MAAMG,oBAAoB,CAACC;IACvB,MAAMC,UAAUN;IAKhB,MAAMO,OAAOC,OAAO,IAAI,CAACH;IACzB,MAAMI,MAAMF,KAAK,KAAK,CAACG,CAAAA,MAAOL,MAAM,CAACK,IAAI,CAAC,MAAM,GAAG;IACnD,IAAID,KACAH,QAAQ,GAAG,CAAC,gCAAgC;SACzC;QACH,MAAMK,iBAAiBJ,KAClB,MAAM,CAACK,CAAAA;YACJ,IAAI,CAACP,MAAM,CAACO,KAAK,IAAI,CAACC,MAAM,OAAO,CAACR,MAAM,CAACO,KAAK,GAC5C,OAAO;YAEX,OAAOP,MAAM,CAACO,KAAK,CAAC,MAAM,GAAG;QACjC,GACC,IAAI,GACJ,IAAI,CAAC;QAEVN,QAAQ,GAAG,CAAC,gCAAgCK;IAChD;IAEA,OAAOL;AACX;AAEO,MAAMQ;IAGT,YAAYC,aAAmC,CAAE;QAC7C,IAAI,CAAC,aAAa,GAAGA;IACzB;IAEA,QAAQC,OAAuB,EAAEC,KAAmB,EAAE;QAClD,MAAMC,mBAAmBF,AAAmB,cAAnBA,QAAQ,MAAM;QAIvC,MAAMG,iBAAiBf,kBAAkB,IAAI,CAAC,aAAa;QAE3D,MAAMgB,iBAAiBF,mBACjBC,eAAe,KAAK,CAAChB,8BACrBgB;QAENF,MAAM,OAAO,CAACG,eAAe,UAAU;QAEvC,OAAOC,OAAO,QAAQ;IAC1B;AACJ"}
|
package/abstractions/Route.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { FastifyRequest, FastifyReply } from "fastify";
|
|
2
|
-
export interface IRouteRequest {
|
|
3
|
-
body: unknown;
|
|
4
|
-
headers: Record<string, string | string[] | undefined>;
|
|
5
|
-
method: string;
|
|
6
|
-
url: string;
|
|
7
|
-
params: unknown;
|
|
8
|
-
query: unknown;
|
|
9
|
-
}
|
|
10
|
-
export interface IRouteReply {
|
|
11
|
-
code(statusCode: number): this;
|
|
12
|
-
send(data?: unknown): void;
|
|
13
|
-
header(key: string, value: unknown): this;
|
|
14
|
-
}
|
|
15
|
-
export interface IRoute {
|
|
16
|
-
execute(request: IRouteRequest, reply: IRouteReply): Promise<void>;
|
|
17
|
-
}
|
|
18
|
-
export declare const Route: import("@webiny/di").Abstraction<IRoute>;
|
|
19
|
-
export declare namespace Route {
|
|
20
|
-
type Interface = IRoute;
|
|
21
|
-
type Request = IRouteRequest;
|
|
22
|
-
type Reply = IRouteReply;
|
|
23
|
-
}
|
|
24
|
-
export declare function toRouteRequest(req: FastifyRequest): IRouteRequest;
|
|
25
|
-
export declare function toRouteReply(reply: FastifyReply): IRouteReply;
|
package/abstractions/Route.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { createAbstraction } from "@webiny/feature/api";
|
|
2
|
-
const Route = createAbstraction("Route");
|
|
3
|
-
function toRouteRequest(req) {
|
|
4
|
-
return {
|
|
5
|
-
body: req.body,
|
|
6
|
-
headers: req.headers,
|
|
7
|
-
method: req.method,
|
|
8
|
-
url: req.url,
|
|
9
|
-
params: req.params ?? {},
|
|
10
|
-
query: req.query ?? {}
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
function toRouteReply(reply) {
|
|
14
|
-
return {
|
|
15
|
-
code (statusCode) {
|
|
16
|
-
reply.code(statusCode);
|
|
17
|
-
return this;
|
|
18
|
-
},
|
|
19
|
-
send (data) {
|
|
20
|
-
reply.send(data);
|
|
21
|
-
},
|
|
22
|
-
header (key, value) {
|
|
23
|
-
reply.header(key, value);
|
|
24
|
-
return this;
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
export { Route, toRouteReply, toRouteRequest };
|
|
29
|
-
|
|
30
|
-
//# sourceMappingURL=Route.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"abstractions/Route.js","sources":["../../src/abstractions/Route.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { FastifyRequest, FastifyReply } from \"fastify\";\n\nexport interface IRouteRequest {\n body: unknown;\n headers: Record<string, string | string[] | undefined>;\n method: string;\n url: string;\n params: unknown;\n query: unknown;\n}\n\nexport interface IRouteReply {\n code(statusCode: number): this;\n send(data?: unknown): void;\n header(key: string, value: unknown): this;\n}\n\nexport interface IRoute {\n execute(request: IRouteRequest, reply: IRouteReply): Promise<void>;\n}\n\nexport const Route = createAbstraction<IRoute>(\"Route\");\n\nexport namespace Route {\n export type Interface = IRoute;\n export type Request = IRouteRequest;\n export type Reply = IRouteReply;\n}\n\nexport function toRouteRequest(req: FastifyRequest): IRouteRequest {\n return {\n body: req.body,\n headers: req.headers as Record<string, string | string[] | undefined>,\n method: req.method,\n url: req.url,\n params: (req.params as Record<string, string>) ?? {},\n query: (req.query as Record<string, string | string[]>) ?? {}\n };\n}\n\nexport function toRouteReply(reply: FastifyReply): IRouteReply {\n return {\n code(statusCode) {\n reply.code(statusCode);\n return this;\n },\n send(data) {\n reply.send(data);\n },\n header(key, value) {\n reply.header(key, value as string);\n return this;\n }\n };\n}\n"],"names":["Route","createAbstraction","toRouteRequest","req","toRouteReply","reply","statusCode","data","key","value"],"mappings":";AAsBO,MAAMA,QAAQC,kBAA0B;AAQxC,SAASC,eAAeC,GAAmB;IAC9C,OAAO;QACH,MAAMA,IAAI,IAAI;QACd,SAASA,IAAI,OAAO;QACpB,QAAQA,IAAI,MAAM;QAClB,KAAKA,IAAI,GAAG;QACZ,QAASA,IAAI,MAAM,IAA+B,CAAC;QACnD,OAAQA,IAAI,KAAK,IAA0C,CAAC;IAChE;AACJ;AAEO,SAASC,aAAaC,KAAmB;IAC5C,OAAO;QACH,MAAKC,UAAU;YACXD,MAAM,IAAI,CAACC;YACX,OAAO,IAAI;QACf;QACA,MAAKC,IAAI;YACLF,MAAM,IAAI,CAACE;QACf;QACA,QAAOC,GAAG,EAAEC,KAAK;YACbJ,MAAM,MAAM,CAACG,KAAKC;YAClB,OAAO,IAAI;QACf;IACJ;AACJ"}
|
package/fastify.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { PluginCollection } from "@webiny/plugins/types.js";
|
|
2
|
-
import { PluginsContainer } from "@webiny/plugins/types.js";
|
|
3
|
-
import { type FastifyInstance, type FastifyServerOptions as ServerOptions } from "fastify";
|
|
4
|
-
export interface CreateHandlerParams {
|
|
5
|
-
plugins: PluginCollection | PluginsContainer;
|
|
6
|
-
options?: ServerOptions;
|
|
7
|
-
debug?: boolean;
|
|
8
|
-
}
|
|
9
|
-
export declare const createHandler: (params: CreateHandlerParams) => FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault> & PromiseLike<FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>> & {
|
|
10
|
-
__linterBrands: "SafePromiseLike";
|
|
11
|
-
};
|
package/fastify.js
DELETED
|
@@ -1,321 +0,0 @@
|
|
|
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);
|
|
38
|
-
});
|
|
39
|
-
const headersToSet = headers.getHeaders();
|
|
40
|
-
delete headersToSet["set-cookie"];
|
|
41
|
-
reply.headers(headersToSet);
|
|
42
|
-
};
|
|
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 || {}
|
|
100
|
-
});
|
|
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);
|
|
108
|
-
});
|
|
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);
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
let context;
|
|
157
|
-
const plugins = new PluginsContainer([]);
|
|
158
|
-
plugins.merge(params.plugins || []);
|
|
159
|
-
try {
|
|
160
|
-
context = new Context({
|
|
161
|
-
plugins,
|
|
162
|
-
WEBINY_VERSION: process.env.WEBINY_VERSION,
|
|
163
|
-
routes
|
|
164
|
-
});
|
|
165
|
-
} catch (ex) {
|
|
166
|
-
console.error("Error while constructing the Context.");
|
|
167
|
-
console.error(stringifyError(ex));
|
|
168
|
-
throw ex;
|
|
169
|
-
}
|
|
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;
|
|
292
|
-
try {
|
|
293
|
-
for (const plugin of modifyPlugins){
|
|
294
|
-
modifyFastifyPluginName = plugin.name;
|
|
295
|
-
plugin.modify(app);
|
|
296
|
-
}
|
|
297
|
-
} catch (ex) {
|
|
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;
|
|
301
|
-
}
|
|
302
|
-
const routePlugins = app.webiny.plugins.byType(RoutePlugin.type);
|
|
303
|
-
let routePluginName;
|
|
304
|
-
try {
|
|
305
|
-
for (const plugin of routePlugins){
|
|
306
|
-
routePluginName = plugin.name;
|
|
307
|
-
plugin.cb({
|
|
308
|
-
...app.webiny.routes,
|
|
309
|
-
context: app.webiny
|
|
310
|
-
});
|
|
311
|
-
}
|
|
312
|
-
} catch (ex) {
|
|
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;
|
|
316
|
-
}
|
|
317
|
-
return app;
|
|
318
|
-
};
|
|
319
|
-
export { createHandler };
|
|
320
|
-
|
|
321
|
-
//# sourceMappingURL=fastify.js.map
|
package/fastify.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"fastify.js","sources":["../src/fastify.ts"],"sourcesContent":["import type { PluginCollection } from \"@webiny/plugins/types.js\";\nimport { PluginsContainer } from \"@webiny/plugins/types.js\";\nimport {\n type FastifyInstance,\n type FastifyServerOptions as ServerOptions,\n LogController\n} from \"fastify\";\nimport fastify from \"fastify\";\nimport type { MiddlewareCallable } from \"@webiny/utils\";\nimport { middleware } from \"@webiny/utils\";\nimport type {\n ContextRoutes,\n DefinedContextRoutes,\n HTTPMethods,\n RouteMethodOptions\n} from \"~/types.js\";\nimport { Context } from \"~/Context.js\";\nimport WebinyError from \"@webiny/error\";\nimport { RoutePlugin } from \"./plugins/RoutePlugin.js\";\nimport fastifyCookie from \"@fastify/cookie\";\nimport fastifyCompress from \"@fastify/compress\";\nimport { ContextPlugin } from \"@webiny/api\";\nimport { BeforeHandlerPlugin } from \"./plugins/BeforeHandlerPlugin.js\";\nimport { HandlerResultPlugin } from \"./plugins/HandlerResultPlugin.js\";\nimport { HandlerErrorPlugin } from \"./plugins/HandlerErrorPlugin.js\";\nimport { ModifyFastifyPlugin } from \"~/plugins/ModifyFastifyPlugin.js\";\nimport { HandlerOnRequestPlugin } from \"~/plugins/HandlerOnRequestPlugin.js\";\nimport type { StandardHeaders } from \"~/ResponseHeaders.js\";\nimport { ResponseHeaders } from \"~/ResponseHeaders.js\";\nimport { ModifyResponseHeadersPlugin } from \"~/plugins/ModifyResponseHeadersPlugin.js\";\nimport { SetDefaultHeaders } from \"./PreHandler/SetDefaultHeaders.js\";\nimport { PreHandler } from \"./PreHandler/PreHandler.js\";\nimport { stringifyError } from \"./stringifyError.js\";\nimport { ProcessHandlerOnRequestPlugins } from \"./PreHandler/ProcessHandlerOnRequestPlugins.js\";\nimport { ProcessContextPlugins } from \"./PreHandler/ProcessContextPlugins.js\";\nimport { IfNotOptionsRequest } from \"./PreHandler/IfNotOptionsRequest.js\";\nimport { ProcessBeforeHandlerPlugins } from \"./PreHandler/ProcessBeforeHandlerPlugins.js\";\nimport { IfOptionsRequest } from \"./PreHandler/IfOptionsRequest.js\";\nimport { SendEarlyOptionsResponse } from \"./PreHandler/SendEarlyOptionsResponse.js\";\nimport { OnRequestTimeoutPlugin } from \"~/plugins/OnRequestTimeoutPlugin.js\";\nimport { OnRequestResponseSendPlugin } from \"~/plugins/OnRequestResponseSendPlugin.js\";\nimport { Request } from \"./abstractions/Request.js\";\nimport { Reply } from \"./abstractions/Reply.js\";\nimport { RegisterExtensionPlugin } from \"~/plugins/RegisterExtensionPlugin.js\";\nimport { RegisterExtensions } from \"~/PreHandler/RegisterExtensions.js\";\n\nconst modifyResponseHeaders = (\n app: FastifyInstance,\n request: Request.Interface,\n reply: Reply.Interface\n) => {\n const modifyHeaders = app.webiny.plugins.byType<ModifyResponseHeadersPlugin>(\n ModifyResponseHeadersPlugin.type\n );\n\n const replyHeaders = reply.getHeaders() as StandardHeaders;\n const headers = ResponseHeaders.create(replyHeaders);\n\n modifyHeaders.forEach(plugin => {\n plugin.modify(request, headers);\n });\n\n // Exclude 'set-cookie' header to avoid duplication.\n // Cookies are managed by @fastify/cookie and calling reply.headers() with 'set-cookie' duplicates them.\n const headersToSet = headers.getHeaders();\n delete headersToSet[\"set-cookie\"];\n\n reply.headers(headersToSet);\n};\n\nexport interface CreateHandlerParams {\n plugins: PluginCollection | PluginsContainer;\n options?: ServerOptions;\n debug?: boolean;\n}\n\nexport const createHandler = (params: CreateHandlerParams) => {\n const definedRoutes: DefinedContextRoutes = {\n POST: [],\n GET: [],\n OPTIONS: [],\n DELETE: [],\n PATCH: [],\n PUT: [],\n HEAD: [],\n COPY: [],\n LOCK: [],\n MKCOL: [],\n MOVE: [],\n PROPFIND: [],\n PROPPATCH: [],\n SEARCH: [],\n TRACE: [],\n UNLOCK: [],\n REPORT: [],\n MKCALENDAR: []\n };\n\n const throwOnDefinedRoute = (\n type: HTTPMethods | \"ALL\",\n path: string,\n options?: RouteMethodOptions\n ): void => {\n if (type === \"ALL\") {\n const all = Object.keys(definedRoutes).find(k => {\n const key = k.toUpperCase() as HTTPMethods;\n const routes = definedRoutes[key];\n return routes.includes(path);\n });\n if (!all) {\n return;\n }\n console.error(\n `Error while registering onAll route. One of the routes is already defined.`\n );\n console.error(JSON.stringify(all));\n throw new WebinyError(\n `You cannot override a route with onAll() method, please remove unnecessary route from the system.`,\n \"OVERRIDE_ROUTE_ERROR\",\n {\n type,\n path\n }\n );\n } else if (definedRoutes[type].includes(path) === false) {\n return;\n } else if (options?.override === true) {\n return;\n }\n console.error(`Error while trying to override route: [${type}] ${path}`);\n throw new WebinyError(\n `When you are trying to override existing route, you must send \"override\" parameter when adding that route.`,\n \"OVERRIDE_ROUTE_ERROR\",\n {\n type,\n path\n }\n );\n };\n\n const addDefinedRoute = (input: HTTPMethods, path: string): void => {\n const type = input.toUpperCase() as HTTPMethods;\n if (!definedRoutes[type]) {\n return;\n } else if (definedRoutes[type].includes(path)) {\n return;\n }\n definedRoutes[type].push(path);\n };\n\n /**\n * We must attach the server to our internal context if we want to have it accessible.\n */\n const app = fastify({\n bodyLimit: 536870912, // 512MB\n logController: new LogController({\n disableRequestLogging: true\n }),\n allowErrorHandlerOverride: true,\n ...(params.options || {})\n });\n\n /**\n * We need to register routes in our system to output headers later on, and disallow route overriding.\n */\n app.addHook(\"onRoute\", route => {\n const method = route.method as HTTPMethods | HTTPMethods[];\n if (Array.isArray(method)) {\n for (const m of method) {\n addDefinedRoute(m, route.path);\n }\n return;\n }\n addDefinedRoute(method, route.path);\n });\n /**\n * ############################\n * Register the Fastify plugins.\n */\n /**\n * Package @fastify/cookie\n *\n * https://github.com/fastify/fastify-cookie\n */\n app.register(fastifyCookie, {\n parseOptions: {} // options for parsing cookies\n });\n /**\n * Package @fastify/compress\n *\n * https://github.com/fastify/fastify-compress\n */\n app.register(fastifyCompress, {\n global: true,\n threshold: 1024,\n onUnsupportedEncoding: (encoding, _, reply) => {\n reply.code(406);\n return `We do not support the ${encoding} encoding.`;\n },\n inflateIfDeflated: true\n });\n /**\n * Route helpers - mostly for users.\n */\n const routes: ContextRoutes = {\n defined: definedRoutes,\n onPost: (path, handler, options) => {\n throwOnDefinedRoute(\"POST\", path, options);\n app.post(path, handler);\n },\n onGet: (path, handler, options) => {\n throwOnDefinedRoute(\"GET\", path, options);\n app.get(path, handler);\n },\n onOptions: (path, handler, options) => {\n throwOnDefinedRoute(\"OPTIONS\", path, options);\n app.options(path, handler);\n },\n onDelete: (path, handler, options) => {\n throwOnDefinedRoute(\"DELETE\", path, options);\n app.delete(path, handler);\n },\n onPatch: (path, handler, options) => {\n throwOnDefinedRoute(\"PATCH\", path, options);\n app.patch(path, handler);\n },\n onPut: (path, handler, options) => {\n throwOnDefinedRoute(\"PUT\", path, options);\n app.put(path, handler);\n },\n onAll: (path, handler, options) => {\n throwOnDefinedRoute(\"ALL\", path, options);\n app.all(path, handler);\n },\n onHead: (path, handler, options) => {\n throwOnDefinedRoute(\"HEAD\", path, options);\n app.head(path, handler);\n }\n };\n let context: Context;\n\n const plugins = new PluginsContainer([]);\n plugins.merge(params.plugins || []);\n\n try {\n context = new Context({\n plugins,\n /**\n * Inserted via webpack at build time.\n */\n WEBINY_VERSION: process.env.WEBINY_VERSION as string,\n routes\n });\n } catch (ex) {\n console.error(`Error while constructing the Context.`);\n console.error(stringifyError(ex));\n throw ex;\n }\n\n /**\n * We are attaching our custom context to webiny variable on the fastify app, so it is accessible everywhere.\n */\n app.decorate(\"webiny\", context);\n\n /**\n * To prevent Unsupported Media Type errors on OPTIONS requests with a body,\n * we need to have a custom parser\n */\n app.addContentTypeParser(\n \"application/json\",\n { parseAs: \"string\", bodyLimit: 1024 * 1024 },\n (req, body, done) => {\n if (req.method === \"OPTIONS\") {\n done(null, undefined);\n return;\n }\n\n try {\n const json = typeof body === \"string\" ? body : body.toString(\"utf8\");\n done(null, JSON.parse(json));\n } catch (err) {\n done(err as Error);\n }\n }\n );\n\n /**\n * With this we ensure that an undefined request body is not parsed on OPTIONS requests,\n * in case there's a `content-type` header set for whatever reason.\n *\n * @see https://fastify.dev/docs/latest/Reference/ContentTypeParser/#content-type-parser\n */\n app.addHook(\"onRequest\", async request => {\n if (request.method === \"OPTIONS\" && request.body === undefined) {\n request.headers[\"content-type\"] = undefined;\n }\n });\n\n /**\n * At this point, request body is properly parsed, and we can execute Webiny business logic.\n * - set default headers\n * - process `HandlerOnRequestPlugin`\n * - if OPTIONS request, exit early\n * - process `ContextPlugin`\n * - process `BeforeHandlerPlugin`\n */\n app.addHook(\"preHandler\", async (request, reply) => {\n app.webiny.request = request;\n app.webiny.reply = reply;\n\n // Bind request and reply to DI container for runtime access\n if (app.webiny.container) {\n app.webiny.container.registerInstance(Request, request);\n app.webiny.container.registerInstance(Reply, reply);\n }\n /**\n * Default code to 200 - so we do not need to set it again.\n * Usually we set errors manually when we use reply.send.\n */\n reply.code(200);\n\n const handlerOnRequestPlugins = app.webiny.plugins.byType<HandlerOnRequestPlugin>(\n HandlerOnRequestPlugin.type\n );\n\n const contextPlugins = app.webiny.plugins.byType<ContextPlugin>(ContextPlugin.type);\n\n const beforeHandlerPlugins = app.webiny.plugins.byType<BeforeHandlerPlugin>(\n BeforeHandlerPlugin.type\n );\n\n const modifyHeadersPlugins = app.webiny.plugins.byType<ModifyResponseHeadersPlugin>(\n ModifyResponseHeadersPlugin.type\n );\n\n const registerExtensionPlugins = app.webiny.plugins.byType<RegisterExtensionPlugin>(\n RegisterExtensionPlugin.type\n );\n\n const preHandler = new PreHandler([\n new RegisterExtensions(registerExtensionPlugins),\n new SetDefaultHeaders(definedRoutes),\n new ProcessHandlerOnRequestPlugins(handlerOnRequestPlugins),\n new IfNotOptionsRequest([\n new ProcessContextPlugins(app.webiny, contextPlugins),\n new ProcessBeforeHandlerPlugins(app.webiny, beforeHandlerPlugins)\n ]),\n new IfOptionsRequest([new SendEarlyOptionsResponse(modifyHeadersPlugins)])\n ]);\n\n await preHandler.execute(request, reply, app.webiny);\n });\n\n app.addHook(\"preSerialization\", async (_, __, payload) => {\n const plugins = app.webiny.plugins.byType<HandlerResultPlugin>(HandlerResultPlugin.type);\n let name: string | undefined;\n try {\n for (const plugin of plugins) {\n name = plugin.name;\n await plugin.handle(app.webiny, payload);\n }\n } catch (ex) {\n console.error(\n `Error while running the \"HandlerResultPlugin\" ${\n name ? `(${name})` : \"\"\n } plugin in the preSerialization hook.`\n );\n console.error(stringifyError(ex));\n throw ex;\n }\n return payload;\n });\n\n app.setErrorHandler<WebinyError>(async (error, _, reply) => {\n /**\n * IMPORTANT! Do not send anything if reply was already sent.\n */\n if (reply.sent) {\n console.warn(\"Reply already sent, cannot send the result (handler:setErrorHandler).\");\n return reply;\n }\n\n if (error.code?.startsWith(\"Authentication/\")) {\n return reply\n .status(401)\n .headers({ \"Cache-Control\": \"no-store\" })\n .send(\n JSON.stringify({\n message: error.message,\n code: error.code\n })\n );\n }\n\n if (error.code === \"Tenancy/TenantDisabled\") {\n return reply\n .status(503)\n .headers({ \"Cache-Control\": \"no-store\" })\n .send(\n JSON.stringify({\n message: error.message,\n code: error.code\n })\n );\n }\n\n return reply\n .status(500)\n .headers({\n \"Cache-Control\": \"no-store\"\n })\n .send(\n /**\n * When we are sending the error in the response, we cannot send the whole error object, as it might contain some sensitive data.\n */\n JSON.stringify({\n message: error.message,\n code: error.code,\n data: error.data\n })\n );\n });\n\n app.addHook(\"onError\", async (_, reply, error: any) => {\n const plugins = app.webiny.plugins.byType<HandlerErrorPlugin>(HandlerErrorPlugin.type);\n /**\n * Log error to cloud, as these can be extremely annoying to debug!\n */\n console.error(\"Logging error in @webiny/handler\");\n try {\n console.error(stringifyError(error));\n } catch (ex) {\n console.warn(\"Could not stringify error:\");\n console.log(error);\n console.error(\"Stringify error:\", ex);\n }\n /**\n * IMPORTANT! Do not send anything if reply was already sent.\n */\n if (!reply.sent) {\n reply\n .status(500)\n .headers({\n \"Cache-Control\": \"no-store\"\n })\n .send(\n /**\n * When we are sending the error in the response, we cannot send the whole error object, as it might contain some sensitive data.\n */\n JSON.stringify({\n message: error.message,\n code: error.code,\n data: error.data\n })\n );\n } else {\n console.warn(\"Reply already sent, cannot send the result (handler:addHook:onError).\");\n }\n\n const handler = middleware(\n plugins.map(pl => {\n return (context: Context, error: Error, next: MiddlewareCallable) => {\n return pl.handle(context, error, next);\n };\n })\n );\n await handler(app.webiny, error);\n\n return reply;\n });\n\n /**\n * Apply response headers modifier plugins.\n */\n app.addHook(\"onSend\", async (request, reply, input) => {\n modifyResponseHeaders(app, request, reply);\n const plugins = app.webiny.plugins.byType<OnRequestResponseSendPlugin>(\n OnRequestResponseSendPlugin.type\n );\n let payload = input;\n for (const plugin of plugins) {\n payload = await plugin.exec(request, reply, payload);\n }\n return payload;\n });\n\n /**\n * We need to output the benchmark results at the end of the request in both response and timeout cases\n */\n app.addHook(\"onResponse\", async () => {\n await context.benchmark.output();\n });\n\n app.addHook(\"onTimeout\", async (request, reply) => {\n const plugins = app.webiny.plugins.byType<OnRequestTimeoutPlugin>(\n OnRequestTimeoutPlugin.type\n );\n for (const plugin of plugins) {\n await plugin.exec(request, reply);\n }\n await context.benchmark.output();\n });\n\n /**\n * With these plugins we give users possibility to do anything they want on our fastify instance.\n */\n const modifyPlugins = app.webiny.plugins.byType<ModifyFastifyPlugin>(ModifyFastifyPlugin.type);\n\n let modifyFastifyPluginName: string | undefined;\n try {\n for (const plugin of modifyPlugins) {\n modifyFastifyPluginName = plugin.name;\n plugin.modify(app);\n }\n } catch (ex) {\n console.error(\n `Error while running the \"ModifyFastifyPlugin\" ${\n modifyFastifyPluginName ? `(${modifyFastifyPluginName})` : \"\"\n } plugin in the end of the \"createHandler\" callable.`\n );\n console.error(stringifyError(ex));\n throw ex;\n }\n\n /**\n * We have few types of triggers:\n * * Events - EventPlugin\n * * Routes - RoutePlugin\n *\n * Routes are registered in fastify but events must be handled in package which implements cloud specific methods.\n */\n const routePlugins = app.webiny.plugins.byType<RoutePlugin>(RoutePlugin.type);\n\n /**\n * Add routes to the system.\n */\n let routePluginName: string | undefined;\n try {\n for (const plugin of routePlugins) {\n routePluginName = plugin.name;\n plugin.cb({\n ...app.webiny.routes,\n context: app.webiny\n });\n }\n } catch (ex) {\n console.error(\n `Error while running the \"RoutePlugin\" ${\n routePluginName ? `(${routePluginName})` : \"\"\n } plugin in the beginning of the \"createHandler\" callable.`\n );\n console.error(stringifyError(ex));\n throw ex;\n }\n\n return app;\n};\n"],"names":["modifyResponseHeaders","app","request","reply","modifyHeaders","ModifyResponseHeadersPlugin","replyHeaders","headers","ResponseHeaders","plugin","headersToSet","createHandler","params","definedRoutes","throwOnDefinedRoute","type","path","options","all","Object","k","key","routes","console","JSON","WebinyError","addDefinedRoute","input","fastify","LogController","route","method","Array","m","fastifyCookie","fastifyCompress","encoding","_","handler","context","plugins","PluginsContainer","Context","process","ex","stringifyError","req","body","done","undefined","json","err","Request","Reply","handlerOnRequestPlugins","HandlerOnRequestPlugin","contextPlugins","ContextPlugin","beforeHandlerPlugins","BeforeHandlerPlugin","modifyHeadersPlugins","registerExtensionPlugins","RegisterExtensionPlugin","preHandler","PreHandler","RegisterExtensions","SetDefaultHeaders","ProcessHandlerOnRequestPlugins","IfNotOptionsRequest","ProcessContextPlugins","ProcessBeforeHandlerPlugins","IfOptionsRequest","SendEarlyOptionsResponse","__","payload","HandlerResultPlugin","name","error","HandlerErrorPlugin","middleware","pl","next","OnRequestResponseSendPlugin","OnRequestTimeoutPlugin","modifyPlugins","ModifyFastifyPlugin","modifyFastifyPluginName","routePlugins","RoutePlugin","routePluginName"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8C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plugin } from "@webiny/plugins";
|
|
2
|
-
import type { Context } from "../types.js";
|
|
3
|
-
export interface BeforeHandlerCallable<T extends Context = Context> {
|
|
4
|
-
(context: T): void | Promise<void>;
|
|
5
|
-
}
|
|
6
|
-
export declare class BeforeHandlerPlugin<T extends Context = Context> extends Plugin {
|
|
7
|
-
static readonly type: string;
|
|
8
|
-
private readonly _callable;
|
|
9
|
-
constructor(callable: BeforeHandlerCallable<T>);
|
|
10
|
-
apply(context: T): Promise<void>;
|
|
11
|
-
}
|
|
12
|
-
export declare const createBeforeHandlerPlugin: <T extends Context = Context>(callable: BeforeHandlerCallable<T>) => BeforeHandlerPlugin<T>;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Plugin } from "@webiny/plugins";
|
|
2
|
-
class BeforeHandlerPlugin extends Plugin {
|
|
3
|
-
static{
|
|
4
|
-
this.type = "before-handler";
|
|
5
|
-
}
|
|
6
|
-
constructor(callable){
|
|
7
|
-
super();
|
|
8
|
-
this._callable = callable;
|
|
9
|
-
}
|
|
10
|
-
async apply(context) {
|
|
11
|
-
if ("function" != typeof this._callable) throw Error('Missing callable in BeforeHandlerPlugin! Either pass a callable to plugin constructor or extend the plugin and override the "apply" method.');
|
|
12
|
-
return this._callable(context);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
const createBeforeHandlerPlugin = (callable)=>new BeforeHandlerPlugin(callable);
|
|
16
|
-
export { BeforeHandlerPlugin, createBeforeHandlerPlugin };
|
|
17
|
-
|
|
18
|
-
//# sourceMappingURL=BeforeHandlerPlugin.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"plugins/BeforeHandlerPlugin.js","sources":["../../src/plugins/BeforeHandlerPlugin.ts"],"sourcesContent":["import { Plugin } from \"@webiny/plugins\";\nimport type { Context } from \"~/types.js\";\n\nexport interface BeforeHandlerCallable<T extends Context = Context> {\n (context: T): void | Promise<void>;\n}\n\nexport class BeforeHandlerPlugin<T extends Context = Context> extends Plugin {\n public static override readonly type: string = \"before-handler\";\n private readonly _callable: BeforeHandlerCallable<T>;\n\n constructor(callable: BeforeHandlerCallable<T>) {\n super();\n this._callable = callable;\n }\n\n public async apply(context: T): Promise<void> {\n if (typeof this._callable !== \"function\") {\n throw Error(\n `Missing callable in BeforeHandlerPlugin! Either pass a callable to plugin constructor or extend the plugin and override the \"apply\" method.`\n );\n }\n\n return this._callable(context);\n }\n}\n\nexport const createBeforeHandlerPlugin = <T extends Context = Context>(\n callable: BeforeHandlerCallable<T>\n): BeforeHandlerPlugin<T> => {\n return new BeforeHandlerPlugin<T>(callable);\n};\n"],"names":["BeforeHandlerPlugin","Plugin","callable","context","Error","createBeforeHandlerPlugin"],"mappings":";AAOO,MAAMA,4BAAyDC;;aAClC,IAAI,GAAW;;IAG/C,YAAYC,QAAkC,CAAE;QAC5C,KAAK;QACL,IAAI,CAAC,SAAS,GAAGA;IACrB;IAEA,MAAa,MAAMC,OAAU,EAAiB;QAC1C,IAAI,AAA0B,cAA1B,OAAO,IAAI,CAAC,SAAS,EACrB,MAAMC,MACF;QAIR,OAAO,IAAI,CAAC,SAAS,CAACD;IAC1B;AACJ;AAEO,MAAME,4BAA4B,CACrCH,WAEO,IAAIF,oBAAuBE"}
|
package/plugins/EventPlugin.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* EventPlugin must be handled in the package which implements fastify for certain cloud.
|
|
3
|
-
* There is no standard input for AWS Lambda, Google Cloud Functions and MS Azure Functions so we let that
|
|
4
|
-
* to be sorted out in the implementation package.
|
|
5
|
-
*
|
|
6
|
-
* Note that only one EventPlugin can be defined per fastify initialisation.
|
|
7
|
-
* If more is needed, check ~/fastify.ts and implement that possibility.
|
|
8
|
-
*/
|
|
9
|
-
import { Plugin } from "@webiny/plugins/Plugin.js";
|
|
10
|
-
import type { Request, Context as BaseContext, Reply } from "../types.js";
|
|
11
|
-
export interface EventPluginCallableParams<Payload, Context extends BaseContext> {
|
|
12
|
-
context: Context;
|
|
13
|
-
payload: Payload;
|
|
14
|
-
request: Request;
|
|
15
|
-
reply: Reply;
|
|
16
|
-
}
|
|
17
|
-
export interface EventPluginCallable<Payload, Context extends BaseContext, Response> {
|
|
18
|
-
(params: EventPluginCallableParams<Payload, Context>): Promise<Response | Reply>;
|
|
19
|
-
}
|
|
20
|
-
export declare class EventPlugin<Payload = any, Context extends BaseContext = BaseContext, Response = any> extends Plugin {
|
|
21
|
-
static type: string;
|
|
22
|
-
readonly cb: EventPluginCallable<Payload, Context, Response>;
|
|
23
|
-
constructor(cb: EventPluginCallable<Payload, Context, Response>);
|
|
24
|
-
}
|
|
25
|
-
export declare const createEvent: <Payload = any, Context extends BaseContext = BaseContext, Response = any>(cb: EventPluginCallable<Payload, Context, Response>) => EventPlugin<Payload, Context, Response>;
|
package/plugins/EventPlugin.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { Plugin } from "@webiny/plugins/Plugin.js";
|
|
2
|
-
class EventPlugin extends Plugin {
|
|
3
|
-
static{
|
|
4
|
-
this.type = "handler.fastify.event";
|
|
5
|
-
}
|
|
6
|
-
constructor(cb){
|
|
7
|
-
super();
|
|
8
|
-
this.cb = cb;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
const createEvent = (cb)=>new EventPlugin(cb);
|
|
12
|
-
export { EventPlugin, createEvent };
|
|
13
|
-
|
|
14
|
-
//# sourceMappingURL=EventPlugin.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"plugins/EventPlugin.js","sources":["../../src/plugins/EventPlugin.ts"],"sourcesContent":["/**\n * EventPlugin must be handled in the package which implements fastify for certain cloud.\n * There is no standard input for AWS Lambda, Google Cloud Functions and MS Azure Functions so we let that\n * to be sorted out in the implementation package.\n *\n * Note that only one EventPlugin can be defined per fastify initialisation.\n * If more is needed, check ~/fastify.ts and implement that possibility.\n */\nimport { Plugin } from \"@webiny/plugins/Plugin.js\";\nimport type { Request, Context as BaseContext, Reply } from \"~/types.js\";\n\nexport interface EventPluginCallableParams<Payload, Context extends BaseContext> {\n context: Context;\n payload: Payload;\n request: Request;\n reply: Reply;\n}\n\nexport interface EventPluginCallable<Payload, Context extends BaseContext, Response> {\n (params: EventPluginCallableParams<Payload, Context>): Promise<Response | Reply>;\n}\n\nexport class EventPlugin<\n Payload = any,\n Context extends BaseContext = BaseContext,\n Response = any\n> extends Plugin {\n public static override type = \"handler.fastify.event\";\n\n public readonly cb: EventPluginCallable<Payload, Context, Response>;\n\n public constructor(cb: EventPluginCallable<Payload, Context, Response>) {\n super();\n this.cb = cb;\n }\n}\n\nexport const createEvent = <\n Payload = any,\n Context extends BaseContext = BaseContext,\n Response = any\n>(\n cb: EventPluginCallable<Payload, Context, Response>\n) => {\n return new EventPlugin<Payload, Context, Response>(cb);\n};\n"],"names":["EventPlugin","Plugin","cb","createEvent"],"mappings":";AAsBO,MAAMA,oBAIHC;;aACiB,IAAI,GAAG;;IAI9B,YAAmBC,EAAmD,CAAE;QACpE,KAAK;QACL,IAAI,CAAC,EAAE,GAAGA;IACd;AACJ;AAEO,MAAMC,cAAc,CAKvBD,KAEO,IAAIF,YAAwCE"}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Plugin } from "@webiny/plugins";
|
|
2
|
-
import type { Context } from "../types.js";
|
|
3
|
-
export interface NextCallable {
|
|
4
|
-
(): Promise<any>;
|
|
5
|
-
}
|
|
6
|
-
export interface HandlerErrorCallable<T extends Context = Context> {
|
|
7
|
-
(context: T, error: Error, next: NextCallable): Promise<any>;
|
|
8
|
-
}
|
|
9
|
-
export declare class HandlerErrorPlugin<T extends Context = Context> extends Plugin {
|
|
10
|
-
static readonly type: string;
|
|
11
|
-
private readonly _callable;
|
|
12
|
-
constructor(callable: HandlerErrorCallable<T>);
|
|
13
|
-
handle(context: T, error: Error, next: NextCallable): Promise<any>;
|
|
14
|
-
}
|
|
15
|
-
export declare const createHandlerErrorPlugin: <T extends Context = Context>(callable: HandlerErrorCallable<T>) => HandlerErrorPlugin<T>;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { Plugin } from "@webiny/plugins";
|
|
2
|
-
class HandlerErrorPlugin extends Plugin {
|
|
3
|
-
static{
|
|
4
|
-
this.type = "handler-error";
|
|
5
|
-
}
|
|
6
|
-
constructor(callable){
|
|
7
|
-
super();
|
|
8
|
-
this._callable = callable;
|
|
9
|
-
}
|
|
10
|
-
async handle(context, error, next) {
|
|
11
|
-
return this._callable(context, error, next);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
const createHandlerErrorPlugin = (callable)=>new HandlerErrorPlugin(callable);
|
|
15
|
-
export { HandlerErrorPlugin, createHandlerErrorPlugin };
|
|
16
|
-
|
|
17
|
-
//# sourceMappingURL=HandlerErrorPlugin.js.map
|