@webiny/handler 6.1.0 → 6.2.0-beta.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.
@@ -0,0 +1,10 @@
1
+ import { FastifyRequest, FastifyReply } from "fastify";
2
+ import type { Context } from "../types.js";
3
+ import { Action } from "./IPreHandler.js";
4
+ import type { IPreHandler } from "./IPreHandler.js";
5
+ import { RegisterExtensionPlugin } from "../plugins/RegisterExtensionPlugin.js";
6
+ export declare class RegisterExtensions implements IPreHandler {
7
+ private readonly plugins;
8
+ constructor(plugins: RegisterExtensionPlugin[]);
9
+ execute<C extends Context = Context>(_: FastifyRequest, __: FastifyReply, context: C): Promise<Action>;
10
+ }
@@ -0,0 +1,14 @@
1
+ import { Action } from "./IPreHandler.js";
2
+ export class RegisterExtensions {
3
+ constructor(plugins) {
4
+ this.plugins = plugins;
5
+ }
6
+ async execute(_, __, context) {
7
+ for (const plugin of this.plugins) {
8
+ await plugin.apply(context);
9
+ }
10
+ return Action.CONTINUE;
11
+ }
12
+ }
13
+
14
+ //# sourceMappingURL=RegisterExtensions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["Action","RegisterExtensions","constructor","plugins","execute","_","__","context","plugin","apply","CONTINUE"],"sources":["RegisterExtensions.ts"],"sourcesContent":["import { FastifyRequest, FastifyReply } from \"fastify\";\nimport type { Context } from \"~/types.js\";\nimport { Action } from \"./IPreHandler.js\";\nimport type { IPreHandler } from \"./IPreHandler.js\";\nimport { RegisterExtensionPlugin } from \"~/plugins/RegisterExtensionPlugin.js\";\n\nexport class RegisterExtensions implements IPreHandler {\n public constructor(private readonly plugins: RegisterExtensionPlugin[]) {}\n\n public async execute<C extends Context = Context>(\n _: FastifyRequest,\n __: FastifyReply,\n context: C\n ): Promise<Action> {\n for (const plugin of this.plugins) {\n await plugin.apply(context);\n }\n\n return Action.CONTINUE;\n }\n}\n"],"mappings":"AAEA,SAASA,MAAM;AAIf,OAAO,MAAMC,kBAAkB,CAAwB;EAC5CC,WAAWA,CAAkBC,OAAkC,EAAE;IAAA,KAApCA,OAAkC,GAAlCA,OAAkC;EAAG;EAEzE,MAAaC,OAAOA,CAChBC,CAAiB,EACjBC,EAAgB,EAChBC,OAAU,EACK;IACf,KAAK,MAAMC,MAAM,IAAI,IAAI,CAACL,OAAO,EAAE;MAC/B,MAAMK,MAAM,CAACC,KAAK,CAACF,OAAO,CAAC;IAC/B;IAEA,OAAOP,MAAM,CAACU,QAAQ;EAC1B;AACJ","ignoreList":[]}
@@ -0,0 +1,25 @@
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;
@@ -0,0 +1,29 @@
1
+ import { createAbstraction } from "@webiny/feature/api";
2
+ export const Route = createAbstraction("Route");
3
+ export 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
+ export 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
+
29
+ //# sourceMappingURL=Route.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["createAbstraction","Route","toRouteRequest","req","body","headers","method","url","params","query","toRouteReply","reply","code","statusCode","send","data","header","key","value"],"sources":["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"],"mappings":"AAAA,SAASA,iBAAiB,QAAQ,qBAAqB;AAsBvD,OAAO,MAAMC,KAAK,GAAGD,iBAAiB,CAAS,OAAO,CAAC;AAQvD,OAAO,SAASE,cAAcA,CAACC,GAAmB,EAAiB;EAC/D,OAAO;IACHC,IAAI,EAAED,GAAG,CAACC,IAAI;IACdC,OAAO,EAAEF,GAAG,CAACE,OAAwD;IACrEC,MAAM,EAAEH,GAAG,CAACG,MAAM;IAClBC,GAAG,EAAEJ,GAAG,CAACI,GAAG;IACZC,MAAM,EAAGL,GAAG,CAACK,MAAM,IAA+B,CAAC,CAAC;IACpDC,KAAK,EAAGN,GAAG,CAACM,KAAK,IAA0C,CAAC;EAChE,CAAC;AACL;AAEA,OAAO,SAASC,YAAYA,CAACC,KAAmB,EAAe;EAC3D,OAAO;IACHC,IAAIA,CAACC,UAAU,EAAE;MACbF,KAAK,CAACC,IAAI,CAACC,UAAU,CAAC;MACtB,OAAO,IAAI;IACf,CAAC;IACDC,IAAIA,CAACC,IAAI,EAAE;MACPJ,KAAK,CAACG,IAAI,CAACC,IAAI,CAAC;IACpB,CAAC;IACDC,MAAMA,CAACC,GAAG,EAAEC,KAAK,EAAE;MACfP,KAAK,CAACK,MAAM,CAACC,GAAG,EAAEC,KAAe,CAAC;MAClC,OAAO,IAAI;IACf;EACJ,CAAC;AACL","ignoreList":[]}
@@ -0,0 +1 @@
1
+ export { Route } from "../abstractions/Route.js";
package/exports/api.js ADDED
@@ -0,0 +1,3 @@
1
+ export { Route } from "../abstractions/Route.js";
2
+
3
+ //# sourceMappingURL=api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["Route"],"sources":["api.ts"],"sourcesContent":["export { Route } from \"~/abstractions/Route.js\";\n"],"mappings":"AAAA,SAASA,KAAK","ignoreList":[]}
package/fastify.js CHANGED
@@ -27,6 +27,8 @@ import { OnRequestTimeoutPlugin } from "./plugins/OnRequestTimeoutPlugin.js";
27
27
  import { OnRequestResponseSendPlugin } from "./plugins/OnRequestResponseSendPlugin.js";
28
28
  import { Request } from "./abstractions/Request.js";
29
29
  import { Reply } from "./abstractions/Reply.js";
30
+ import { RegisterExtensionPlugin } from "./plugins/RegisterExtensionPlugin.js";
31
+ import { RegisterExtensions } from "./PreHandler/RegisterExtensions.js";
30
32
  const modifyResponseHeaders = (app, request, reply) => {
31
33
  const modifyHeaders = app.webiny.plugins.byType(ModifyResponseHeadersPlugin.type);
32
34
  const replyHeaders = reply.getHeaders();
@@ -268,7 +270,8 @@ export const createHandler = params => {
268
270
  const contextPlugins = app.webiny.plugins.byType(ContextPlugin.type);
269
271
  const beforeHandlerPlugins = app.webiny.plugins.byType(BeforeHandlerPlugin.type);
270
272
  const modifyHeadersPlugins = app.webiny.plugins.byType(ModifyResponseHeadersPlugin.type);
271
- const preHandler = new PreHandler([new SetDefaultHeaders(definedRoutes), new ProcessHandlerOnRequestPlugins(handlerOnRequestPlugins), new IfNotOptionsRequest([new ProcessContextPlugins(app.webiny, contextPlugins), new ProcessBeforeHandlerPlugins(app.webiny, beforeHandlerPlugins)]), new IfOptionsRequest([new SendEarlyOptionsResponse(modifyHeadersPlugins)])]);
273
+ const registerExtensionPlugins = app.webiny.plugins.byType(RegisterExtensionPlugin.type);
274
+ const preHandler = new PreHandler([new RegisterExtensions(registerExtensionPlugins), new SetDefaultHeaders(definedRoutes), new ProcessHandlerOnRequestPlugins(handlerOnRequestPlugins), new IfNotOptionsRequest([new ProcessContextPlugins(app.webiny, contextPlugins), new ProcessBeforeHandlerPlugins(app.webiny, beforeHandlerPlugins)]), new IfOptionsRequest([new SendEarlyOptionsResponse(modifyHeadersPlugins)])]);
272
275
  await preHandler.execute(request, reply, app.webiny);
273
276
  });
274
277
  app.addHook("preSerialization", async (_, __, payload) => {
package/fastify.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["PluginsContainer","fastify","middleware","Context","WebinyError","RoutePlugin","fastifyCookie","fastifyCompress","ContextPlugin","BeforeHandlerPlugin","HandlerResultPlugin","HandlerErrorPlugin","ModifyFastifyPlugin","HandlerOnRequestPlugin","ResponseHeaders","ModifyResponseHeadersPlugin","SetDefaultHeaders","PreHandler","stringifyError","ProcessHandlerOnRequestPlugins","ProcessContextPlugins","IfNotOptionsRequest","ProcessBeforeHandlerPlugins","IfOptionsRequest","SendEarlyOptionsResponse","OnRequestTimeoutPlugin","OnRequestResponseSendPlugin","Request","Reply","modifyResponseHeaders","app","request","reply","modifyHeaders","webiny","plugins","byType","type","replyHeaders","getHeaders","headers","create","forEach","plugin","modify","headersToSet","createHandler","params","definedRoutes","POST","GET","OPTIONS","DELETE","PATCH","PUT","HEAD","COPY","LOCK","MKCOL","MOVE","PROPFIND","PROPPATCH","SEARCH","TRACE","UNLOCK","REPORT","MKCALENDAR","throwOnDefinedRoute","path","options","all","Object","keys","find","k","key","toUpperCase","routes","includes","console","error","JSON","stringify","override","addDefinedRoute","input","push","bodyLimit","disableRequestLogging","allowErrorHandlerOverride","addHook","route","method","Array","isArray","m","register","parseOptions","global","threshold","onUnsupportedEncoding","encoding","_","code","inflateIfDeflated","defined","onPost","handler","post","onGet","get","onOptions","onDelete","delete","onPatch","patch","onPut","put","onAll","onHead","head","context","merge","WEBINY_VERSION","process","env","ex","decorate","addContentTypeParser","parseAs","req","body","done","undefined","json","toString","parse","err","container","registerInstance","handlerOnRequestPlugins","contextPlugins","beforeHandlerPlugins","modifyHeadersPlugins","preHandler","execute","__","payload","name","handle","setErrorHandler","sent","warn","startsWith","status","send","message","data","log","map","pl","next","exec","benchmark","output","modifyPlugins","modifyFastifyPluginName","routePlugins","routePluginName","cb"],"sources":["fastify.ts"],"sourcesContent":["import type { PluginCollection } from \"@webiny/plugins/types.js\";\nimport { PluginsContainer } from \"@webiny/plugins/types.js\";\nimport type { FastifyInstance, FastifyServerOptions as ServerOptions } 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\";\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 disableRequestLogging: true,\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 preHandler = new PreHandler([\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"],"mappings":"AACA,SAASA,gBAAgB,QAAQ,0BAA0B;AAE3D,OAAOC,OAAO,MAAM,SAAS;AAE7B,SAASC,UAAU,QAAQ,eAAe;AAO1C,SAASC,OAAO;AAChB,OAAOC,WAAW,MAAM,eAAe;AACvC,SAASC,WAAW;AACpB,OAAOC,aAAa,MAAM,iBAAiB;AAC3C,OAAOC,eAAe,MAAM,mBAAmB;AAC/C,SAASC,aAAa,QAAQ,aAAa;AAC3C,SAASC,mBAAmB;AAC5B,SAASC,mBAAmB;AAC5B,SAASC,kBAAkB;AAC3B,SAASC,mBAAmB;AAC5B,SAASC,sBAAsB;AAE/B,SAASC,eAAe;AACxB,SAASC,2BAA2B;AACpC,SAASC,iBAAiB;AAC1B,SAASC,UAAU;AACnB,SAASC,cAAc;AACvB,SAASC,8BAA8B;AACvC,SAASC,qBAAqB;AAC9B,SAASC,mBAAmB;AAC5B,SAASC,2BAA2B;AACpC,SAASC,gBAAgB;AACzB,SAASC,wBAAwB;AACjC,SAASC,sBAAsB;AAC/B,SAAS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K,CAAC9D,cAAc,CAAC4G,EAAE,CAAC,CAAC;IACjC,MAAMA,EAAE;EACZ;EAEA,OAAOhG,GAAG;AACd,CAAC","ignoreList":[]}
1
+ {"version":3,"names":["PluginsContainer","fastify","middleware","Context","WebinyError","RoutePlugin","fastifyCookie","fastifyCompress","ContextPlugin","BeforeHandlerPlugin","HandlerResultPlugin","HandlerErrorPlugin","ModifyFastifyPlugin","HandlerOnRequestPlugin","ResponseHeaders","ModifyResponseHeadersPlugin","SetDefaultHeaders","PreHandler","stringifyError","ProcessHandlerOnRequestPlugins","ProcessContextPlugins","IfNotOptionsRequest","ProcessBeforeHandlerPlugins","IfOptionsRequest","SendEarlyOptionsResponse","OnRequestTimeoutPlugin","OnRequestResponseSendPlugin","Request","Reply","RegisterExtensionPlugin","RegisterExtensions","modifyResponseHeaders","app","request","reply","modifyHeaders","webiny","plugins","byType","type","replyHeaders","getHeaders","headers","create","forEach","plugin","modify","headersToSet","createHandler","params","definedRoutes","POST","GET","OPTIONS","DELETE","PATCH","PUT","HEAD","COPY","LOCK","MKCOL","MOVE","PROPFIND","PROPPATCH","SEARCH","TRACE","UNLOCK","REPORT","MKCALENDAR","throwOnDefinedRoute","path","options","all","Object","keys","find","k","key","toUpperCase","routes","includes","console","error","JSON","stringify","override","addDefinedRoute","input","push","bodyLimit","disableRequestLogging","allowErrorHandlerOverride","addHook","route","method","Array","isArray","m","register","parseOptions","global","threshold","onUnsupportedEncoding","encoding","_","code","inflateIfDeflated","defined","onPost","handler","post","onGet","get","onOptions","onDelete","delete","onPatch","patch","onPut","put","onAll","onHead","head","context","merge","WEBINY_VERSION","process","env","ex","decorate","addContentTypeParser","parseAs","req","body","done","undefined","json","toString","parse","err","container","registerInstance","handlerOnRequestPlugins","contextPlugins","beforeHandlerPlugins","modifyHeadersPlugins","registerExtensionPlugins","preHandler","execute","__","payload","name","handle","setErrorHandler","sent","warn","startsWith","status","send","message","data","log","map","pl","next","exec","benchmark","output","modifyPlugins","modifyFastifyPluginName","routePlugins","routePluginName","cb"],"sources":["fastify.ts"],"sourcesContent":["import type { PluginCollection } from \"@webiny/plugins/types.js\";\nimport { PluginsContainer } from \"@webiny/plugins/types.js\";\nimport type { FastifyInstance, FastifyServerOptions as ServerOptions } 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 disableRequestLogging: true,\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"],"mappings":"AACA,SAASA,gBAAgB,QAAQ,0BAA0B;AAE3D,OAAOC,OAAO,MAAM,SAAS;AAE7B,SAASC,UAAU,QAAQ,eAAe;AAO1C,SAASC,OAAO;AAChB,OAAOC,WAAW,MAAM,eAAe;AACvC,SAASC,WAAW;AACpB,OAAOC,aAAa,MAAM,iBAAiB;AAC3C,OAAOC,eAAe,MAAM,mBAAmB;AAC/C,SAASC,aAAa,QAAQ,aAAa;AAC3C,SAASC,mBAAmB;AAC5B,SAASC,mBAAmB;AAC5B,SAASC,kBAAkB;AAC3B,SAASC,mBAAmB;AAC5B,SAASC,sBAAsB;AAE/B,SAASC,eAAe;AACxB,SAASC,2BAA2B;AACpC,SAASC,iBAAiB;AAC1B,SAASC,UAAU;AACnB,SAASC,cAAc;AACvB,SAASC,8BAA8B;AACvC,SAASC,qBAAqB;AAC9B,SAASC,mBAAmB;AAC5B,SAASC,2BAA2B;AACpC,SAASC,gBAAgB;AACzB,SAASC,wBAAwB;AACjC,SAASC,sBAAsB;AAC/B,SAASC,2BAA2B;AACpC,SAASC,OAAO;AAChB,SAASC,KAAK;AACd,SAASC,uBAAuB;AAChC,SAASC,kBAAkB;AAE3B,MAAMC,qBAAqB,GAAGA,CAC1BC,GAAoB,EACpBC,OAA0B,EAC1BC,KAAsB,KACrB;EACD,MAAMC,aAAa,GAAGH,GAAG,CAACI,MAAM,CAACC,OAAO,CAACC,MAAM,CAC3CvB,2BAA2B,CAACwB,IAChC,CAAC;EAED,MAAMC,YAAY,GAAGN,KAAK,CAACO,UAAU,CAAC,CAAoB;EAC1D,MAAMC,OAAO,GAAG5B,eAAe,CAAC6B,MAAM,CAACH,YAAY,CAAC;EAEpDL,aAAa,C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B,mBAAmB,CAAC2B,IAAI,CAAC;EAE9F,IAAIkI,uBAA2C;EAC/C,IAAI;IACA,KAAK,MAAM5H,MAAM,IAAI2H,aAAa,EAAE;MAChCC,uBAAuB,GAAG5H,MAAM,CAAC0G,IAAI;MACrC1G,MAAM,CAACC,MAAM,CAACd,GAAG,CAAC;IACtB;EACJ,CAAC,CAAC,OAAOgG,EAAE,EAAE;IACT/C,OAAO,CAACC,KAAK,CACT,iDACIuF,uBAAuB,GAAG,IAAIA,uBAAuB,GAAG,GAAG,EAAE,qDAErE,CAAC;IACDxF,OAAO,CAACC,KAAK,CAAChE,cAAc,CAAC8G,EAAE,CAAC,CAAC;IACjC,MAAMA,EAAE;EACZ;;EAEA;AACJ;AACA;AACA;AACA;AACA;AACA;EACI,MAAM0C,YAAY,GAAG1I,GAAG,CAACI,MAAM,CAACC,OAAO,CAACC,MAAM,CAAcjC,WAAW,CAACkC,IAAI,CAAC;;EAE7E;AACJ;AACA;EACI,IAAIoI,eAAmC;EACvC,IAAI;IACA,KAAK,MAAM9H,MAAM,IAAI6H,YAAY,EAAE;MAC/BC,eAAe,GAAG9H,MAAM,CAAC0G,IAAI;MAC7B1G,MAAM,CAAC+H,EAAE,CAAC;QACN,GAAG5I,GAAG,CAACI,MAAM,CAAC2C,MAAM;QACpB4C,OAAO,EAAE3F,GAAG,CAACI;MACjB,CAAC,CAAC;IACN;EACJ,CAAC,CAAC,OAAO4F,EAAE,EAAE;IACT/C,OAAO,CAACC,KAAK,CACT,yCACIyF,eAAe,GAAG,IAAIA,eAAe,GAAG,GAAG,EAAE,2DAErD,CAAC;IACD1F,OAAO,CAACC,KAAK,CAAChE,cAAc,CAAC8G,EAAE,CAAC,CAAC;IACjC,MAAMA,EAAE;EACZ;EAEA,OAAOhG,GAAG;AACd,CAAC","ignoreList":[]}
package/index.d.ts CHANGED
@@ -12,6 +12,8 @@ export * from "./plugins/ModifyFastifyPlugin.js";
12
12
  export * from "./plugins/ModifyResponseHeadersPlugin.js";
13
13
  export * from "./plugins/OnRequestResponseSendPlugin.js";
14
14
  export * from "./plugins/OnRequestTimeoutPlugin.js";
15
+ export { RegisterExtensionPlugin, createRegisterExtensionPlugin } from "./plugins/RegisterExtensionPlugin.js";
15
16
  export * from "./ResponseHeaders.js";
16
17
  export { Request } from "./abstractions/Request.js";
17
18
  export { Reply } from "./abstractions/Reply.js";
19
+ export { Route, toRouteRequest, toRouteReply } from "./abstractions/Route.js";
package/index.js CHANGED
@@ -13,8 +13,10 @@ export * from "./plugins/ModifyFastifyPlugin.js";
13
13
  export * from "./plugins/ModifyResponseHeadersPlugin.js";
14
14
  export * from "./plugins/OnRequestResponseSendPlugin.js";
15
15
  export * from "./plugins/OnRequestTimeoutPlugin.js";
16
+ export { RegisterExtensionPlugin, createRegisterExtensionPlugin } from "./plugins/RegisterExtensionPlugin.js";
16
17
  export * from "./ResponseHeaders.js";
17
18
  export { Request } from "./abstractions/Request.js";
18
19
  export { Reply } from "./abstractions/Reply.js";
20
+ export { Route, toRouteRequest, toRouteReply } from "./abstractions/Route.js";
19
21
 
20
22
  //# sourceMappingURL=index.js.map
package/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["Request","Reply"],"sources":["index.ts"],"sourcesContent":["// Suppress punycode warnings. This is a known issue which we can't fix.\nimport \"./suppressPunycodeWarnings.js\";\n\nexport * from \"~/fastify.js\";\nexport * from \"~/Context.js\";\nexport * from \"~/ResponseHeaders.js\";\nexport * from \"~/plugins/EventPlugin.js\";\nexport * from \"~/plugins/RoutePlugin.js\";\nexport * from \"~/plugins/BeforeHandlerPlugin.js\";\nexport * from \"~/plugins/HandlerErrorPlugin.js\";\nexport * from \"~/plugins/HandlerResultPlugin.js\";\nexport * from \"~/plugins/HandlerOnRequestPlugin.js\";\nexport * from \"~/plugins/ModifyFastifyPlugin.js\";\nexport * from \"~/plugins/ModifyResponseHeadersPlugin.js\";\nexport * from \"~/plugins/OnRequestResponseSendPlugin.js\";\nexport * from \"~/plugins/OnRequestTimeoutPlugin.js\";\nexport * from \"./ResponseHeaders.js\";\n\nexport { Request } from \"./abstractions/Request.js\";\nexport { Reply } from \"./abstractions/Reply.js\";\n"],"mappings":"AAAA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA,SAASA,OAAO;AAChB,SAASC,KAAK","ignoreList":[]}
1
+ {"version":3,"names":["RegisterExtensionPlugin","createRegisterExtensionPlugin","Request","Reply","Route","toRouteRequest","toRouteReply"],"sources":["index.ts"],"sourcesContent":["// Suppress punycode warnings. This is a known issue which we can't fix.\nimport \"./suppressPunycodeWarnings.js\";\n\nexport * from \"~/fastify.js\";\nexport * from \"~/Context.js\";\nexport * from \"~/ResponseHeaders.js\";\nexport * from \"~/plugins/EventPlugin.js\";\nexport * from \"~/plugins/RoutePlugin.js\";\nexport * from \"~/plugins/BeforeHandlerPlugin.js\";\nexport * from \"~/plugins/HandlerErrorPlugin.js\";\nexport * from \"~/plugins/HandlerResultPlugin.js\";\nexport * from \"~/plugins/HandlerOnRequestPlugin.js\";\nexport * from \"~/plugins/ModifyFastifyPlugin.js\";\nexport * from \"~/plugins/ModifyResponseHeadersPlugin.js\";\nexport * from \"~/plugins/OnRequestResponseSendPlugin.js\";\nexport * from \"~/plugins/OnRequestTimeoutPlugin.js\";\nexport {\n RegisterExtensionPlugin,\n createRegisterExtensionPlugin\n} from \"~/plugins/RegisterExtensionPlugin.js\";\nexport * from \"./ResponseHeaders.js\";\n\nexport { Request } from \"./abstractions/Request.js\";\nexport { Reply } from \"./abstractions/Reply.js\";\nexport { Route, toRouteRequest, toRouteReply } from \"./abstractions/Route.js\";\n"],"mappings":"AAAA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SACIA,uBAAuB,EACvBC,6BAA6B;AAEjC;AAEA,SAASC,OAAO;AAChB,SAASC,KAAK;AACd,SAASC,KAAK,EAAEC,cAAc,EAAEC,YAAY","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/handler",
3
- "version": "6.1.0",
3
+ "version": "6.2.0-beta.0",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -15,22 +15,22 @@
15
15
  "dependencies": {
16
16
  "@fastify/compress": "8.3.1",
17
17
  "@fastify/cookie": "11.0.2",
18
- "@webiny/api": "6.1.0",
19
- "@webiny/error": "6.1.0",
20
- "@webiny/feature": "6.1.0",
21
- "@webiny/plugins": "6.1.0",
22
- "@webiny/utils": "6.1.0",
18
+ "@webiny/api": "6.2.0-beta.0",
19
+ "@webiny/error": "6.2.0-beta.0",
20
+ "@webiny/feature": "6.2.0-beta.0",
21
+ "@webiny/plugins": "6.2.0-beta.0",
22
+ "@webiny/utils": "6.2.0-beta.0",
23
23
  "fastify": "5.8.4"
24
24
  },
25
25
  "devDependencies": {
26
- "@webiny/build-tools": "6.1.0",
26
+ "@webiny/build-tools": "6.2.0-beta.0",
27
27
  "rimraf": "6.1.3",
28
28
  "typescript": "5.9.3",
29
- "vitest": "4.1.2"
29
+ "vitest": "4.1.4"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public",
33
33
  "directory": "dist"
34
34
  },
35
- "gitHead": "65e0ac1889b3392c99b8cac6cde508e1e831c715"
35
+ "gitHead": "3d3148358b6febbc857371930871743bec3b3939"
36
36
  }
@@ -0,0 +1,12 @@
1
+ import { Plugin } from "@webiny/plugins";
2
+ import type { Context } from "../types.js";
3
+ export interface IRegisterExtensionPluginCb<C extends Context = Context> {
4
+ (context: C): Promise<void> | void;
5
+ }
6
+ export declare class RegisterExtensionPlugin<C extends Context = Context> extends Plugin {
7
+ private readonly cb;
8
+ static readonly type: string;
9
+ constructor(cb: IRegisterExtensionPluginCb<C>);
10
+ apply(context: C): Promise<void> | void;
11
+ }
12
+ export declare const createRegisterExtensionPlugin: <C extends Context = Context>(cb: IRegisterExtensionPluginCb<C>) => RegisterExtensionPlugin<C>;
@@ -0,0 +1,16 @@
1
+ import { Plugin } from "@webiny/plugins";
2
+ export class RegisterExtensionPlugin extends Plugin {
3
+ static type = "handler.register.extension";
4
+ constructor(cb) {
5
+ super();
6
+ this.cb = cb;
7
+ }
8
+ apply(context) {
9
+ return this.cb(context);
10
+ }
11
+ }
12
+ export const createRegisterExtensionPlugin = cb => {
13
+ return new RegisterExtensionPlugin(cb);
14
+ };
15
+
16
+ //# sourceMappingURL=RegisterExtensionPlugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["Plugin","RegisterExtensionPlugin","type","constructor","cb","apply","context","createRegisterExtensionPlugin"],"sources":["RegisterExtensionPlugin.ts"],"sourcesContent":["import { Plugin } from \"@webiny/plugins\";\nimport type { Context } from \"~/types.js\";\n\nexport interface IRegisterExtensionPluginCb<C extends Context = Context> {\n (context: C): Promise<void> | void;\n}\n\nexport class RegisterExtensionPlugin<C extends Context = Context> extends Plugin {\n public static override readonly type: string = \"handler.register.extension\";\n\n public constructor(private readonly cb: IRegisterExtensionPluginCb<C>) {\n super();\n }\n\n public apply(context: C): Promise<void> | void {\n return this.cb(context);\n }\n}\n\nexport const createRegisterExtensionPlugin = <C extends Context = Context>(\n cb: IRegisterExtensionPluginCb<C>\n) => {\n return new RegisterExtensionPlugin<C>(cb);\n};\n"],"mappings":"AAAA,SAASA,MAAM,QAAQ,iBAAiB;AAOxC,OAAO,MAAMC,uBAAuB,SAAsCD,MAAM,CAAC;EAC7E,OAAgCE,IAAI,GAAW,4BAA4B;EAEpEC,WAAWA,CAAkBC,EAAiC,EAAE;IACnE,KAAK,CAAC,CAAC;IAAC,KADwBA,EAAiC,GAAjCA,EAAiC;EAErE;EAEOC,KAAKA,CAACC,OAAU,EAAwB;IAC3C,OAAO,IAAI,CAACF,EAAE,CAACE,OAAO,CAAC;EAC3B;AACJ;AAEA,OAAO,MAAMC,6BAA6B,GACtCH,EAAiC,IAChC;EACD,OAAO,IAAIH,uBAAuB,CAAIG,EAAE,CAAC;AAC7C,CAAC","ignoreList":[]}