@webiny/api-websockets 5.41.4 → 5.42.0-beta.1

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.
@@ -1 +1 @@
1
- {"version":3,"names":["createTypeDefs","exports"],"sources":["createTypeDefs.ts"],"sourcesContent":["export const createTypeDefs = () => {\n return /* GraphQL */ `\n type WebsocketsIdentity {\n id: String!\n type: String\n displayName: String\n }\n type WebsocketsConnection {\n connectionId: String!\n domainName: String!\n stage: String!\n identity: WebsocketsIdentity!\n connectedOn: DateTime!\n tenant: String!\n locale: String!\n }\n\n type WebsocketsError {\n message: String!\n code: String!\n data: JSON\n }\n\n type WebsocketsListConnectionsResponse {\n data: [WebsocketsConnection!]\n error: WebsocketsError\n }\n\n input WebsocketsListConnectionsWhereInput {\n identityId: String\n tenant: String\n locale: String\n }\n\n type WebsocketsDisconnectResponse {\n data: [WebsocketsConnection!]\n error: WebsocketsError\n }\n\n type WebsocketsQuery {\n _empty: String\n }\n\n type WebsocketsMutation {\n _empty: String\n }\n\n extend type Query {\n websockets: WebsocketsQuery\n }\n\n extend type Mutation {\n websockets: WebsocketsMutation\n }\n\n extend type WebsocketsQuery {\n listConnections(\n where: WebsocketsListConnectionsWhereInput\n ): WebsocketsListConnectionsResponse!\n }\n\n extend type WebsocketsMutation {\n disconnect(connections: [String!]!): WebsocketsDisconnectResponse!\n disconnectIdentity(identityId: String!): WebsocketsDisconnectResponse!\n disconnectTenant(tenant: String!, locale: String): WebsocketsDisconnectResponse!\n disconnectAll: WebsocketsDisconnectResponse!\n }\n `;\n};\n"],"mappings":";;;;;;AAAO,MAAMA,cAAc,GAAGA,CAAA,KAAM;EAChC,OAAO,aAAe;AAC1B;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,KAAK;AACL,CAAC;AAACC,OAAA,CAAAD,cAAA,GAAAA,cAAA","ignoreList":[]}
1
+ {"version":3,"names":["createTypeDefs","exports"],"sources":["createTypeDefs.ts"],"sourcesContent":["export const createTypeDefs = () => {\n return /* GraphQL */ `\n type WebsocketsIdentity {\n id: String!\n type: String\n displayName: String\n }\n type WebsocketsConnection {\n connectionId: String!\n domainName: String!\n stage: String!\n identity: WebsocketsIdentity!\n connectedOn: DateTime!\n tenant: String!\n locale: String!\n }\n\n type WebsocketsError {\n message: String!\n code: String!\n data: JSON\n }\n\n type WebsocketsListConnectionsResponse {\n data: [WebsocketsConnection!]\n error: WebsocketsError\n }\n\n input WebsocketsListConnectionsWhereInput {\n identityId: String\n tenant: String\n locale: String\n }\n\n type WebsocketsDisconnectResponse {\n data: [WebsocketsConnection!]\n error: WebsocketsError\n }\n\n type WebsocketsQuery {\n _empty: String\n }\n\n type WebsocketsMutation {\n _empty: String\n }\n\n extend type Query {\n websockets: WebsocketsQuery\n }\n\n extend type Mutation {\n websockets: WebsocketsMutation\n }\n\n extend type WebsocketsQuery {\n listConnections(\n where: WebsocketsListConnectionsWhereInput\n ): WebsocketsListConnectionsResponse!\n }\n\n extend type WebsocketsMutation {\n disconnect(connections: [String!]!): WebsocketsDisconnectResponse!\n disconnectIdentity(identityId: String!): WebsocketsDisconnectResponse!\n disconnectTenant(tenant: String!, locale: String): WebsocketsDisconnectResponse!\n disconnectAll: WebsocketsDisconnectResponse!\n }\n `;\n};\n"],"mappings":";;;;;;AAAO,MAAMA,cAAc,GAAGA,CAAA,KAAM;EAChC,OAAO,aAAc;AACzB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,KAAK;AACL,CAAC;AAACC,OAAA,CAAAD,cAAA,GAAAA,cAAA","ignoreList":[]}
@@ -1 +1 @@
1
- {"version":3,"names":["_handler","require","_plugins","_execute","_plugins2","_routes","_validator","_response","_runner","_headers","url","createPluginsContainer","plugins","PluginsContainer","createHandler","params","register","createWebsocketsRoutePlugins","event","app","createBaseHandler","options","logger","debug","registerDefaultPlugins","webiny","setErrorHandler","error","_","reply","__webiny_raw_result","message","code","data","statusCode","send","post","validator","response","context","handler","WebsocketsRunner","websockets","registry","WebsocketsEventValidator","WebsocketsResponse","result","run","status","headers","tenant","locale","endpoint","token","getEventValues","Authorization","execute","payload","exports"],"sources":["handler.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport { createHandler as createBaseHandler } from \"@webiny/handler\";\nimport { registerDefaultPlugins } from \"@webiny/handler-aws/plugins\";\nimport { execute } from \"@webiny/handler-aws/execute\";\nimport { PluginsContainer } from \"@webiny/plugins\";\nimport { createWebsocketsRoutePlugins } from \"~/runner/routes\";\nimport { WebsocketsEventValidator } from \"~/validator\";\nimport { WebsocketsResponse } from \"~/response\";\nimport { Context } from \"~/types\";\nimport { WebsocketsRunner } from \"~/runner\";\nimport { PluginCollection } from \"@webiny/plugins/types\";\nimport { HandlerCallable, HandlerParams } from \"./types\";\nimport { getEventValues } from \"./headers\";\n\nconst url = \"/webiny-websockets\";\n\nconst createPluginsContainer = (\n plugins?: PluginsContainer | PluginCollection\n): PluginsContainer => {\n if (plugins instanceof PluginsContainer) {\n return plugins;\n }\n return new PluginsContainer(plugins || []);\n};\n\nexport const createHandler = (params: HandlerParams): HandlerCallable => {\n const plugins = createPluginsContainer(params.plugins);\n plugins.register(...createWebsocketsRoutePlugins());\n\n return async event => {\n const app = createBaseHandler({\n ...params,\n plugins,\n options: {\n logger: params.debug === true,\n ...(params.options || {})\n }\n });\n\n registerDefaultPlugins(app.webiny);\n\n app.setErrorHandler<WebinyError>(async (error, _, reply) => {\n app.__webiny_raw_result = {\n error: {\n message: error.message,\n code: error.code,\n data: error.data\n },\n statusCode: 200\n };\n return reply.send({});\n });\n\n app.post(url, async (_, reply) => {\n const { validator, response } = params;\n const context = app.webiny as Context;\n const handler = new WebsocketsRunner(\n context,\n context.websockets.registry,\n validator || new WebsocketsEventValidator(),\n response || new WebsocketsResponse()\n );\n\n const result = await handler.run(event);\n\n return reply\n .status(result.statusCode)\n .headers({\n \"Sec-WebSocket-Protocol\": \"webiny-ws-v1\"\n })\n .send(result);\n });\n\n const { tenant, locale, endpoint, token } = getEventValues(event);\n\n const headers = {\n Authorization: `Bearer ${token}`,\n [\"x-tenant\"]: tenant,\n [\"x-webiny-cms-locale\"]: locale,\n [\"x-webiny-cms-endpoint\"]: endpoint,\n ...event.headers\n };\n\n return execute({\n app,\n url,\n payload: {\n ...event,\n headers\n }\n });\n };\n};\n"],"mappings":";;;;;;AACA,IAAAA,QAAA,GAAAC,OAAA;AACA,IAAAC,QAAA,GAAAD,OAAA;AACA,IAAAE,QAAA,GAAAF,OAAA;AACA,IAAAG,SAAA,GAAAH,OAAA;AACA,IAAAI,OAAA,GAAAJ,OAAA;AACA,IAAAK,UAAA,GAAAL,OAAA;AACA,IAAAM,SAAA,GAAAN,OAAA;AAEA,IAAAO,OAAA,GAAAP,OAAA;AAGA,IAAAQ,QAAA,GAAAR,OAAA;AAEA,MAAMS,GAAG,GAAG,oBAAoB;AAEhC,MAAMC,sBAAsB,GACxBC,OAA6C,IAC1B;EACnB,IAAIA,OAAO,YAAYC,0BAAgB,EAAE;IACrC,OAAOD,OAAO;EAClB;EACA,OAAO,IAAIC,0BAAgB,CAACD,OAAO,IAAI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,CAAC,CACvC,CAAC;MAED,MAAMC,MAAM,GAAG,MAAMN,OAAO,CAACO,GAAG,CAAC7B,KAAK,CAAC;MAEvC,OAAOW,KAAK,CACPmB,MAAM,CAACF,MAAM,CAACZ,UAAU,CAAC,CACzBe,OAAO,CAAC;QACL,wBAAwB,EAAE;MAC9B,CAAC,CAAC,CACDd,IAAI,CAACW,MAAM,CAAC;IACrB,CAAC,CAAC;IAEF,MAAM;MAAEI,MAAM;MAAEC,MAAM;MAAEC,QAAQ;MAAEC;IAAM,CAAC,GAAG,IAAAC,uBAAc,EAACpC,KAAK,CAAC;IAEjE,MAAM+B,OAAO,GAAG;MACZM,aAAa,EAAG,UAASF,KAAM,EAAC;MAChC,CAAC,UAAU,GAAGH,MAAM;MACpB,CAAC,qBAAqB,GAAGC,MAAM;MAC/B,CAAC,uBAAuB,GAAGC,QAAQ;MACnC,GAAGlC,KAAK,CAAC+B;IACb,CAAC;IAED,OAAO,IAAAO,gBAAO,EAAC;MACXrC,GAAG;MACHT,GAAG;MACH+C,OAAO,EAAE;QACL,GAAGvC,KAAK;QACR+B;MACJ;IACJ,CAAC,CAAC;EACN,CAAC;AACL,CAAC;AAACS,OAAA,CAAA5C,aAAA,GAAAA,aAAA","ignoreList":[]}
1
+ {"version":3,"names":["_handler","require","_plugins","_execute","_plugins2","_routes","_validator","_response","_runner","_headers","url","createPluginsContainer","plugins","PluginsContainer","createHandler","params","register","createWebsocketsRoutePlugins","event","app","createBaseHandler","options","logger","debug","registerDefaultPlugins","webiny","setErrorHandler","error","_","reply","__webiny_raw_result","message","code","data","statusCode","send","post","validator","response","context","handler","WebsocketsRunner","websockets","registry","WebsocketsEventValidator","WebsocketsResponse","result","run","status","headers","tenant","locale","endpoint","token","getEventValues","Authorization","execute","payload","exports"],"sources":["handler.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport { createHandler as createBaseHandler } from \"@webiny/handler\";\nimport { registerDefaultPlugins } from \"@webiny/handler-aws/plugins\";\nimport { execute } from \"@webiny/handler-aws/execute\";\nimport { PluginsContainer } from \"@webiny/plugins\";\nimport { createWebsocketsRoutePlugins } from \"~/runner/routes\";\nimport { WebsocketsEventValidator } from \"~/validator\";\nimport { WebsocketsResponse } from \"~/response\";\nimport { Context } from \"~/types\";\nimport { WebsocketsRunner } from \"~/runner\";\nimport { PluginCollection } from \"@webiny/plugins/types\";\nimport { HandlerCallable, HandlerParams } from \"./types\";\nimport { getEventValues } from \"./headers\";\n\nconst url = \"/webiny-websockets\";\n\nconst createPluginsContainer = (\n plugins?: PluginsContainer | PluginCollection\n): PluginsContainer => {\n if (plugins instanceof PluginsContainer) {\n return plugins;\n }\n return new PluginsContainer(plugins || []);\n};\n\nexport const createHandler = (params: HandlerParams): HandlerCallable => {\n const plugins = createPluginsContainer(params.plugins);\n plugins.register(...createWebsocketsRoutePlugins());\n\n return async event => {\n const app = createBaseHandler({\n ...params,\n plugins,\n options: {\n logger: params.debug === true,\n ...(params.options || {})\n }\n });\n\n registerDefaultPlugins(app.webiny);\n\n app.setErrorHandler<WebinyError>(async (error, _, reply) => {\n app.__webiny_raw_result = {\n error: {\n message: error.message,\n code: error.code,\n data: error.data\n },\n statusCode: 200\n };\n return reply.send({});\n });\n\n app.post(url, async (_, reply) => {\n const { validator, response } = params;\n const context = app.webiny as Context;\n const handler = new WebsocketsRunner(\n context,\n context.websockets.registry,\n validator || new WebsocketsEventValidator(),\n response || new WebsocketsResponse()\n );\n\n const result = await handler.run(event);\n\n return reply\n .status(result.statusCode)\n .headers({\n \"Sec-WebSocket-Protocol\": \"webiny-ws-v1\"\n })\n .send(result);\n });\n\n const { tenant, locale, endpoint, token } = getEventValues(event);\n\n const headers = {\n Authorization: `Bearer ${token}`,\n [\"x-tenant\"]: tenant,\n [\"x-webiny-cms-locale\"]: locale,\n [\"x-webiny-cms-endpoint\"]: endpoint,\n ...event.headers\n };\n\n return execute({\n app,\n url,\n payload: {\n ...event,\n headers\n }\n });\n };\n};\n"],"mappings":";;;;;;AACA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AA","ignoreList":[]}
@@ -19,7 +19,7 @@ const handler = (0, _handlerAws.createSourceHandler)({
19
19
  }) => {
20
20
  const {
21
21
  createHandler
22
- } = await import( /* webpackChunkName: "SocketsHandler" */"./handler");
22
+ } = await import(/* webpackChunkName: "SocketsHandler" */"./handler");
23
23
  return createHandler(params)(event, context);
24
24
  }
25
25
  });
@@ -1 +1 @@
1
- {"version":3,"names":["_registry","require","_handlerAws","handler","createSourceHandler","name","canUse","event","routeKey","connectionId","eventType","requestContext","handle","params","context","createHandler","registry","register"],"sources":["register.ts"],"sourcesContent":["import { registry } from \"@webiny/handler-aws/registry\";\nimport { createSourceHandler } from \"@webiny/handler-aws\";\nimport { HandlerParams, IWebsocketsIncomingEvent } from \"./types\";\n\nconst handler = createSourceHandler<IWebsocketsIncomingEvent, HandlerParams>({\n name: \"handler-webiny-websockets\",\n canUse: event => {\n const { routeKey, connectionId, eventType } = event.requestContext || {};\n return !!routeKey && !!connectionId && !!eventType;\n },\n handle: async ({ params, event, context }) => {\n const { createHandler } = await import(\n /* webpackChunkName: \"SocketsHandler\" */\n \"./handler\"\n );\n return createHandler(params)(event, context);\n }\n});\n\nregistry.register(handler);\n"],"mappings":";;AAAA,IAAAA,SAAA,GAAAC,OAAA;AACA,IAAAC,WAAA,GAAAD,OAAA;AAGA,MAAME,OAAO,GAAG,IAAAC,+BAAmB,EAA0C;EACzEC,IAAI,EAAE,2BAA2B;EACjCC,MAAM,EAAEC,KAAK,IAAI;IACb,MAAM;MAAEC,QAAQ;MAAEC,YAAY;MAAEC;IAAU,CAAC,GAAGH,KAAK,CAACI,cAAc,IAAI,CAAC,CAAC;IACxE,OAAO,CAAC,CAACH,QAAQ,IAAI,CAAC,CAACC,YAAY,IAAI,CAAC,CAACC,SAAS;EACtD,CAAC;EACDE,MAAM,EAAE,MAAAA,CAAO;IAAEC,MAAM;IAAEN,KAAK;IAAEO;EAAQ,CAAC,KAAK;IAC1C,MAAM;MAAEC;IAAc,CAAC,GAAG,MAAM,MAAM,EAClC,mDAEJ,CAAC;IACD,OAAOA,aAAa,CAACF,MAAM,CAAC,CAACN,KAAK,EAAEO,OAAO,CAAC;EAChD;AACJ,CAAC,CAAC;AAEFE,kBAAQ,CAACC,QAAQ,CAACd,OAAO,CAAC","ignoreList":[]}
1
+ {"version":3,"names":["_registry","require","_handlerAws","handler","createSourceHandler","name","canUse","event","routeKey","connectionId","eventType","requestContext","handle","params","context","createHandler","registry","register"],"sources":["register.ts"],"sourcesContent":["import { registry } from \"@webiny/handler-aws/registry\";\nimport { createSourceHandler } from \"@webiny/handler-aws\";\nimport { HandlerParams, IWebsocketsIncomingEvent } from \"./types\";\n\nconst handler = createSourceHandler<IWebsocketsIncomingEvent, HandlerParams>({\n name: \"handler-webiny-websockets\",\n canUse: event => {\n const { routeKey, connectionId, eventType } = event.requestContext || {};\n return !!routeKey && !!connectionId && !!eventType;\n },\n handle: async ({ params, event, context }) => {\n const { createHandler } = await import(\n /* webpackChunkName: \"SocketsHandler\" */\n \"./handler\"\n );\n return createHandler(params)(event, context);\n }\n});\n\nregistry.register(handler);\n"],"mappings":";;AAAA,IAAAA,SAAA,GAAAC,OAAA;AACA,IAAAC,WAAA,GAAAD,OAAA;AAGA,MAAME,OAAO,GAAG,IAAAC,+BAAmB,EAA0C;EACzEC,IAAI,EAAE,2BAA2B;EACjCC,MAAM,EAAEC,KAAK,IAAI;IACb,MAAM;MAAEC,QAAQ;MAAEC,YAAY;MAAEC;IAAU,CAAC,GAAGH,KAAK,CAACI,cAAc,IAAI,CAAC,CAAC;IACxE,OAAO,CAAC,CAACH,QAAQ,IAAI,CAAC,CAACC,YAAY,IAAI,CAAC,CAACC,SAAS;EACtD,CAAC;EACDE,MAAM,EAAE,MAAAA,CAAO;IAAEC,MAAM;IAAEN,KAAK;IAAEO;EAAQ,CAAC,KAAK;IAC1C,MAAM;MAAEC;IAAc,CAAC,GAAG,MAAM,MAAM,CAClC,mDAEJ,CAAC;IACD,OAAOA,aAAa,CAACF,MAAM,CAAC,CAACN,KAAK,EAAEO,OAAO,CAAC;EAChD;AACJ,CAAC,CAAC;AAEFE,kBAAQ,CAACC,QAAQ,CAACd,OAAO,CAAC","ignoreList":[]}
@@ -1,9 +1,9 @@
1
- import { HandlerFactoryParams } from "@webiny/handler-aws/types";
2
- import { IWebsocketsEventValidator } from "../validator";
3
- import { IWebsocketsResponse } from "../response";
4
- import { APIGatewayProxyResult, Context as LambdaContext } from "aws-lambda";
5
- import { GenericRecord } from "@webiny/api/types";
6
- import { PartialDeep } from "type-fest";
1
+ import type { HandlerFactoryParams } from "@webiny/handler-aws/types";
2
+ import type { IWebsocketsEventValidator } from "../validator";
3
+ import type { IWebsocketsResponse } from "../response";
4
+ import type { APIGatewayProxyResult, Context as LambdaContext } from "@webiny/aws-sdk/types";
5
+ import type { GenericRecord } from "@webiny/api/types";
6
+ import type { PartialDeep } from "type-fest";
7
7
  export interface HandlerCallable {
8
8
  (event: IWebsocketsIncomingEvent, context: LambdaContext): Promise<APIGatewayProxyResult>;
9
9
  }
@@ -1 +1 @@
1
- {"version":3,"names":["WebsocketsEventRoute","exports","WebsocketsEventRequestContextEventType"],"sources":["types.ts"],"sourcesContent":["import { HandlerFactoryParams } from \"@webiny/handler-aws/types\";\nimport { IWebsocketsEventValidator } from \"~/validator\";\nimport { IWebsocketsResponse } from \"~/response\";\nimport { APIGatewayProxyResult, Context as LambdaContext } from \"aws-lambda\";\nimport { GenericRecord } from \"@webiny/api/types\";\nimport { PartialDeep } from \"type-fest\";\n\nexport interface HandlerCallable {\n (event: IWebsocketsIncomingEvent, context: LambdaContext): Promise<APIGatewayProxyResult>;\n}\n\nexport interface HandlerParams extends HandlerFactoryParams {\n validator?: IWebsocketsEventValidator;\n response?: IWebsocketsResponse;\n}\n\nexport enum WebsocketsEventRoute {\n \"connect\" = \"$connect\",\n \"disconnect\" = \"$disconnect\",\n \"default\" = \"$default\"\n}\n\nexport interface IWebsocketsEventData {\n token?: string;\n tenant?: string;\n locale?: string;\n messageId?: string;\n action?: string;\n data?: GenericRecord;\n}\n\nexport enum WebsocketsEventRequestContextEventType {\n \"message\" = \"MESSAGE\",\n \"connect\" = \"CONNECT\",\n \"disconnect\" = \"DISCONNECT\"\n}\n\nexport interface IWebsocketsEventRequestContext {\n connectionId: string;\n connectedAt: number;\n domainName: string;\n eventType: WebsocketsEventRequestContextEventType;\n routeKey: WebsocketsEventRoute | string;\n stage: string;\n}\n\nexport interface IWebsocketsEventHeaders {\n \"Accept-Encoding\"?: string;\n \"Accept-Language\"?: string;\n \"Cache-Control\"?: string;\n Host?: string;\n Origin?: string;\n Pragma?: string;\n \"Sec-WebSocket-Extensions\"?: string;\n \"Sec-WebSocket-Key\"?: string;\n \"Sec-WebSocket-Version\"?: string;\n \"Sec-WebSocket-Protocol\"?: string;\n \"User-Agent\"?: string;\n \"X-Amzn-Trace-Id\"?: string;\n \"X-Forwarded-For\"?: string;\n \"X-Forwarded-Port\"?: `${number}`;\n \"X-Forwarded-Proto\"?: \"https\" | \"http\";\n [\"x-tenant\"]?: string;\n [\"x-webiny-cms-locale\"]?: string;\n [\"x-webiny-cms-endpoint\"]?: string;\n}\n\nexport interface IWebsocketsEventQueryStringParameters {\n tenant?: string;\n locale?: string;\n token?: string;\n}\n\nexport interface IWebsocketsEvent<T extends IWebsocketsEventData = IWebsocketsEventData> {\n headers?: IWebsocketsEventHeaders;\n queryStringParameters?: IWebsocketsEventQueryStringParameters;\n requestContext: IWebsocketsEventRequestContext;\n body?: T;\n}\n\nexport interface IWebsocketsIncomingEvent extends PartialDeep<Omit<IWebsocketsEvent, \"body\">> {\n body?: string | GenericRecord;\n}\n"],"mappings":";;;;;;IAgBYA,oBAAoB,GAAAC,OAAA,CAAAD,oBAAA,0BAApBA,oBAAoB;EAApBA,oBAAoB;EAApBA,oBAAoB;EAApBA,oBAAoB;EAAA,OAApBA,oBAAoB;AAAA;AAAA,IAepBE,sCAAsC,GAAAD,OAAA,CAAAC,sCAAA,0BAAtCA,sCAAsC;EAAtCA,sCAAsC;EAAtCA,sCAAsC;EAAtCA,sCAAsC;EAAA,OAAtCA,sCAAsC;AAAA","ignoreList":[]}
1
+ {"version":3,"names":["WebsocketsEventRoute","exports","WebsocketsEventRequestContextEventType"],"sources":["types.ts"],"sourcesContent":["import type { HandlerFactoryParams } from \"@webiny/handler-aws/types\";\nimport type { IWebsocketsEventValidator } from \"~/validator\";\nimport type { IWebsocketsResponse } from \"~/response\";\nimport type { APIGatewayProxyResult, Context as LambdaContext } from \"@webiny/aws-sdk/types\";\nimport type { GenericRecord } from \"@webiny/api/types\";\nimport type { PartialDeep } from \"type-fest\";\n\nexport interface HandlerCallable {\n (event: IWebsocketsIncomingEvent, context: LambdaContext): Promise<APIGatewayProxyResult>;\n}\n\nexport interface HandlerParams extends HandlerFactoryParams {\n validator?: IWebsocketsEventValidator;\n response?: IWebsocketsResponse;\n}\n\nexport enum WebsocketsEventRoute {\n \"connect\" = \"$connect\",\n \"disconnect\" = \"$disconnect\",\n \"default\" = \"$default\"\n}\n\nexport interface IWebsocketsEventData {\n token?: string;\n tenant?: string;\n locale?: string;\n messageId?: string;\n action?: string;\n data?: GenericRecord;\n}\n\nexport enum WebsocketsEventRequestContextEventType {\n \"message\" = \"MESSAGE\",\n \"connect\" = \"CONNECT\",\n \"disconnect\" = \"DISCONNECT\"\n}\n\nexport interface IWebsocketsEventRequestContext {\n connectionId: string;\n connectedAt: number;\n domainName: string;\n eventType: WebsocketsEventRequestContextEventType;\n routeKey: WebsocketsEventRoute | string;\n stage: string;\n}\n\nexport interface IWebsocketsEventHeaders {\n \"Accept-Encoding\"?: string;\n \"Accept-Language\"?: string;\n \"Cache-Control\"?: string;\n Host?: string;\n Origin?: string;\n Pragma?: string;\n \"Sec-WebSocket-Extensions\"?: string;\n \"Sec-WebSocket-Key\"?: string;\n \"Sec-WebSocket-Version\"?: string;\n \"Sec-WebSocket-Protocol\"?: string;\n \"User-Agent\"?: string;\n \"X-Amzn-Trace-Id\"?: string;\n \"X-Forwarded-For\"?: string;\n \"X-Forwarded-Port\"?: `${number}`;\n \"X-Forwarded-Proto\"?: \"https\" | \"http\";\n [\"x-tenant\"]?: string;\n [\"x-webiny-cms-locale\"]?: string;\n [\"x-webiny-cms-endpoint\"]?: string;\n}\n\nexport interface IWebsocketsEventQueryStringParameters {\n tenant?: string;\n locale?: string;\n token?: string;\n}\n\nexport interface IWebsocketsEvent<T extends IWebsocketsEventData = IWebsocketsEventData> {\n headers?: IWebsocketsEventHeaders;\n queryStringParameters?: IWebsocketsEventQueryStringParameters;\n requestContext: IWebsocketsEventRequestContext;\n body?: T;\n}\n\nexport interface IWebsocketsIncomingEvent extends PartialDeep<Omit<IWebsocketsEvent, \"body\">> {\n body?: string | GenericRecord;\n}\n"],"mappings":";;;;;;IAgBYA,oBAAoB,GAAAC,OAAA,CAAAD,oBAAA,0BAApBA,oBAAoB;EAApBA,oBAAoB;EAApBA,oBAAoB;EAApBA,oBAAoB;EAAA,OAApBA,oBAAoB;AAAA;AAAA,IAepBE,sCAAsC,GAAAD,OAAA,CAAAC,sCAAA,0BAAtCA,sCAAsC;EAAtCA,sCAAsC;EAAtCA,sCAAsC;EAAtCA,sCAAsC;EAAA,OAAtCA,sCAAsC;AAAA","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/api-websockets",
3
- "version": "5.41.4",
3
+ "version": "5.42.0-beta.1",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,36 +12,29 @@
12
12
  ],
13
13
  "license": "MIT",
14
14
  "dependencies": {
15
- "@babel/runtime": "7.24.1",
16
- "@webiny/api": "5.41.4",
17
- "@webiny/api-i18n": "5.41.4",
18
- "@webiny/api-security": "5.41.4",
19
- "@webiny/api-tenancy": "5.41.4",
20
- "@webiny/aws-sdk": "5.41.4",
21
- "@webiny/db-dynamodb": "5.41.4",
22
- "@webiny/error": "5.41.4",
23
- "@webiny/handler": "5.41.4",
24
- "@webiny/handler-aws": "5.41.4",
25
- "@webiny/plugins": "5.41.4",
26
- "@webiny/utils": "5.41.4",
27
- "type-fest": "2.19.0",
15
+ "@webiny/api": "5.42.0-beta.1",
16
+ "@webiny/api-i18n": "5.42.0-beta.1",
17
+ "@webiny/api-security": "5.42.0-beta.1",
18
+ "@webiny/api-tenancy": "5.42.0-beta.1",
19
+ "@webiny/aws-sdk": "5.42.0-beta.1",
20
+ "@webiny/db-dynamodb": "5.42.0-beta.1",
21
+ "@webiny/error": "5.42.0-beta.1",
22
+ "@webiny/handler": "5.42.0-beta.1",
23
+ "@webiny/handler-aws": "5.42.0-beta.1",
24
+ "@webiny/plugins": "5.42.0-beta.1",
25
+ "@webiny/utils": "5.42.0-beta.1",
26
+ "type-fest": "3.13.1",
28
27
  "zod": "3.23.8"
29
28
  },
30
29
  "devDependencies": {
31
- "@babel/cli": "7.24.1",
32
- "@babel/core": "7.24.3",
33
- "@babel/preset-env": "7.24.3",
34
- "@babel/preset-typescript": "7.24.1",
35
- "@types/aws-lambda": "8.10.136",
36
- "@webiny/api-headless-cms": "5.41.4",
37
- "@webiny/api-wcp": "5.41.4",
38
- "@webiny/cli": "5.41.4",
39
- "@webiny/handler-db": "5.41.4",
40
- "@webiny/handler-graphql": "5.41.4",
41
- "@webiny/project-utils": "5.41.4",
42
- "aws-lambda": "1.0.7",
43
- "graphql": "15.8.0",
44
- "rimraf": "5.0.5",
30
+ "@webiny/api-headless-cms": "5.42.0-beta.1",
31
+ "@webiny/api-wcp": "5.42.0-beta.1",
32
+ "@webiny/cli": "5.42.0-beta.1",
33
+ "@webiny/handler-db": "5.42.0-beta.1",
34
+ "@webiny/handler-graphql": "5.42.0-beta.1",
35
+ "@webiny/project-utils": "5.42.0-beta.1",
36
+ "graphql": "15.9.0",
37
+ "rimraf": "6.0.1",
45
38
  "ttypescript": "1.5.15",
46
39
  "typescript": "4.9.5"
47
40
  },
@@ -53,5 +46,5 @@
53
46
  "build": "yarn webiny run build",
54
47
  "watch": "yarn webiny run watch"
55
48
  },
56
- "gitHead": "94922b33af59db5afe75127bb07443ce7f1448c4"
49
+ "gitHead": "5e69da579efa4f2c8268e0c97ac6407ddc3f5f07"
57
50
  }
@@ -1 +1 @@
1
- {"version":3,"names":["_error","_interopRequireDefault","require","_entity","_dbDynamodb","PK","GSI1_PK","GSI2_PK","WebsocketsConnectionRegistry","constructor","documentClient","entity","createEntity","register","params","connectionId","tenant","locale","identity","domainName","stage","connectedOn","data","store","unregister","keys","SK","original","getViaConnection","message","console","error","WebinyError","deleteItem","ex","code","item","get","listViaConnections","connections","items","map","getBatch","results","batchReadAll","table","listViaIdentity","queryAll","partitionKey","options","index","eq","listViaTenant","beginsWith","listAll","gte","GSI1_SK","id","GSI2_SK","put","err","from","exports"],"sources":["WebsocketsConnectionRegistry.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport {\n IWebsocketsConnectionRegistry,\n IWebsocketsConnectionRegistryData,\n IWebsocketsConnectionRegistryRegisterParams,\n IWebsocketsConnectionRegistryUnregisterParams\n} from \"./abstractions/IWebsocketsConnectionRegistry\";\nimport { createEntity } from \"~/registry/entity\";\nimport { batchReadAll, deleteItem, get, put, queryAll } from \"@webiny/db-dynamodb\";\nimport { DynamoDBDocument } from \"@webiny/aws-sdk/client-dynamodb\";\nimport { EntityQueryOptions } from \"@webiny/db-dynamodb/toolbox\";\n\nconst PK = `WS#CONNECTIONS`;\nconst GSI1_PK = \"WS#CONNECTIONS#IDENTITY\";\nconst GSI2_PK = \"WS#CONNECTIONS#TENANT#LOCALE\";\n\ninterface IWebsocketsConnectionRegistryDbItem {\n PK: string;\n SK: string;\n GSI1_PK: string;\n GSI1_SK: string;\n GSI2_PK: string;\n GSI2_SK: string;\n data: IWebsocketsConnectionRegistryData;\n}\n\nexport class WebsocketsConnectionRegistry implements IWebsocketsConnectionRegistry {\n private readonly entity: ReturnType<typeof createEntity>;\n\n public constructor(documentClient: DynamoDBDocument) {\n this.entity = createEntity(documentClient);\n }\n\n public async register(\n params: IWebsocketsConnectionRegistryRegisterParams\n ): Promise<IWebsocketsConnectionRegistryData> {\n const { connectionId, tenant, locale, identity, domainName, stage, connectedOn } = params;\n\n const data: IWebsocketsConnectionRegistryData = {\n connectionId,\n identity,\n tenant,\n locale,\n domainName,\n stage,\n connectedOn\n };\n await this.store(data);\n return data;\n }\n\n public async unregister(params: IWebsocketsConnectionRegistryUnregisterParams): Promise<void> {\n const { connectionId } = params;\n\n const keys = {\n PK,\n SK: connectionId\n };\n const original = await this.getViaConnection(connectionId);\n if (!original) {\n const message = `There is no connection with ID \"${connectionId}\".`;\n console.error(message);\n throw new WebinyError(message, \"CONNECTION_NOT_FOUND\", keys);\n }\n\n try {\n await deleteItem({\n entity: this.entity,\n keys\n });\n } catch (ex) {\n console.error(\n `Could not remove connection from the database: ${original.connectionId}`\n );\n throw new WebinyError(ex.message, ex.code, keys);\n }\n }\n\n private async getViaConnection(\n connectionId: string\n ): Promise<IWebsocketsConnectionRegistryData | null> {\n const item = await get<IWebsocketsConnectionRegistryDbItem>({\n entity: this.entity,\n keys: {\n PK,\n SK: connectionId\n }\n });\n return item?.data || null;\n }\n\n /**\n * Uses Primary keys\n */\n public async listViaConnections(\n connections: string[]\n ): Promise<IWebsocketsConnectionRegistryData[]> {\n const items = connections.map(connectionId => {\n return this.entity.getBatch({\n PK,\n SK: connectionId\n });\n });\n\n const results = await batchReadAll<IWebsocketsConnectionRegistryDbItem>({\n table: this.entity.table,\n items\n });\n\n return results.map(item => {\n return item.data;\n });\n }\n\n /**\n * Uses GSI1 keys\n */\n public async listViaIdentity(identity: string): Promise<IWebsocketsConnectionRegistryData[]> {\n const items = await queryAll<IWebsocketsConnectionRegistryDbItem>({\n entity: this.entity,\n partitionKey: GSI1_PK,\n options: {\n index: \"GSI1\",\n eq: identity\n }\n });\n return items.map(item => {\n return item.data;\n });\n }\n\n /**\n * Uses GSI2 keys\n */\n public async listViaTenant(\n tenant: string,\n locale?: string\n ): Promise<IWebsocketsConnectionRegistryData[]> {\n let options: Partial<EntityQueryOptions> = {\n beginsWith: `T#${tenant}#L#`\n };\n if (locale) {\n options = {\n eq: `T#${tenant}#L#${locale}`\n };\n }\n const items = await queryAll<IWebsocketsConnectionRegistryDbItem>({\n entity: this.entity,\n partitionKey: GSI2_PK,\n options: {\n ...options,\n index: \"GSI2\"\n }\n });\n return items.map(item => {\n return item.data;\n });\n }\n\n public async listAll(): Promise<IWebsocketsConnectionRegistryData[]> {\n const items = await queryAll<IWebsocketsConnectionRegistryDbItem>({\n entity: this.entity,\n partitionKey: PK,\n options: {\n gte: \" \"\n }\n });\n return items.map(item => {\n return item.data;\n });\n }\n\n private async store(data: IWebsocketsConnectionRegistryData) {\n const { connectionId, tenant, locale, identity } = data;\n const item: IWebsocketsConnectionRegistryDbItem = {\n // to find specific identity related to given connection\n PK,\n SK: connectionId,\n // to find all connections related to given identity\n GSI1_PK,\n GSI1_SK: identity.id,\n // to find all connections related to given tenant/locale combination\n GSI2_PK,\n GSI2_SK: `T#${tenant}#L#${locale}`,\n data\n };\n try {\n return await put({\n entity: this.entity,\n item\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not store websockets connection data.\",\n code: \"STORE_WEBSOCKETS_CONNECTION_DATA_ERROR\",\n data: item\n });\n }\n }\n}\n"],"mappings":";;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AAOA,IAAAC,OAAA,GAAAD,OAAA;AACA,IAAAE,WAAA,GAAAF,OAAA;AAIA,MAAMG,EAAE,GAAI,gBAAe;AAC3B,MAAMC,OAAO,GAAG,yBAAyB;AACzC,MAAMC,OAAO,GAAG,8BAA8B;AAYvC,MAAMC,4BAA4B,CAA0C;EAGxEC,WAAWA,CAACC,cAAgC,EAAE;IACjD,IAAI,CAACC,MAAM,GAAG,IAAAC,oBAAY,EAACF,cAAc,CAAC;EAC9C;EAEA,MAAaG,QAAQA,CACjBC,MAAmD,EACT;IAC1C,MAAM;MAAEC,YAAY;MAAEC,MAAM;MA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C0B,OAAA,CAAAtD,4BAAA,GAAAA,4BAAA","ignoreList":[]}
1
+ {"version":3,"names":["_error","_interopRequireDefault","require","_entity","_dbDynamodb","PK","GSI1_PK","GSI2_PK","WebsocketsConnectionRegistry","constructor","documentClient","entity","createEntity","register","params","connectionId","tenant","locale","identity","domainName","stage","connectedOn","data","store","unregister","keys","SK","original","getViaConnection","message","console","error","WebinyError","deleteItem","ex","code","item","get","listViaConnections","connections","items","map","getBatch","results","batchReadAll","table","listViaIdentity","queryAll","partitionKey","options","index","eq","listViaTenant","beginsWith","listAll","gte","GSI1_SK","id","GSI2_SK","put","err","from","exports"],"sources":["WebsocketsConnectionRegistry.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport {\n IWebsocketsConnectionRegistry,\n IWebsocketsConnectionRegistryData,\n IWebsocketsConnectionRegistryRegisterParams,\n IWebsocketsConnectionRegistryUnregisterParams\n} from \"./abstractions/IWebsocketsConnectionRegistry\";\nimport { createEntity } from \"~/registry/entity\";\nimport { batchReadAll, deleteItem, get, put, queryAll } from \"@webiny/db-dynamodb\";\nimport { DynamoDBDocument } from \"@webiny/aws-sdk/client-dynamodb\";\nimport { EntityQueryOptions } from \"@webiny/db-dynamodb/toolbox\";\n\nconst PK = `WS#CONNECTIONS`;\nconst GSI1_PK = \"WS#CONNECTIONS#IDENTITY\";\nconst GSI2_PK = \"WS#CONNECTIONS#TENANT#LOCALE\";\n\ninterface IWebsocketsConnectionRegistryDbItem {\n PK: string;\n SK: string;\n GSI1_PK: string;\n GSI1_SK: string;\n GSI2_PK: string;\n GSI2_SK: string;\n data: IWebsocketsConnectionRegistryData;\n}\n\nexport class WebsocketsConnectionRegistry implements IWebsocketsConnectionRegistry {\n private readonly entity: ReturnType<typeof createEntity>;\n\n public constructor(documentClient: DynamoDBDocument) {\n this.entity = createEntity(documentClient);\n }\n\n public async register(\n params: IWebsocketsConnectionRegistryRegisterParams\n ): Promise<IWebsocketsConnectionRegistryData> {\n const { connectionId, tenant, locale, identity, domainName, stage, connectedOn } = params;\n\n const data: IWebsocketsConnectionRegistryData = {\n connectionId,\n identity,\n tenant,\n locale,\n domainName,\n stage,\n connectedOn\n };\n await this.store(data);\n return data;\n }\n\n public async unregister(params: IWebsocketsConnectionRegistryUnregisterParams): Promise<void> {\n const { connectionId } = params;\n\n const keys = {\n PK,\n SK: connectionId\n };\n const original = await this.getViaConnection(connectionId);\n if (!original) {\n const message = `There is no connection with ID \"${connectionId}\".`;\n console.error(message);\n throw new WebinyError(message, \"CONNECTION_NOT_FOUND\", keys);\n }\n\n try {\n await deleteItem({\n entity: this.entity,\n keys\n });\n } catch (ex) {\n console.error(\n `Could not remove connection from the database: ${original.connectionId}`\n );\n throw new WebinyError(ex.message, ex.code, keys);\n }\n }\n\n private async getViaConnection(\n connectionId: string\n ): Promise<IWebsocketsConnectionRegistryData | null> {\n const item = await get<IWebsocketsConnectionRegistryDbItem>({\n entity: this.entity,\n keys: {\n PK,\n SK: connectionId\n }\n });\n return item?.data || null;\n }\n\n /**\n * Uses Primary keys\n */\n public async listViaConnections(\n connections: string[]\n ): Promise<IWebsocketsConnectionRegistryData[]> {\n const items = connections.map(connectionId => {\n return this.entity.getBatch({\n PK,\n SK: connectionId\n });\n });\n\n const results = await batchReadAll<IWebsocketsConnectionRegistryDbItem>({\n table: this.entity.table,\n items\n });\n\n return results.map(item => {\n return item.data;\n });\n }\n\n /**\n * Uses GSI1 keys\n */\n public async listViaIdentity(identity: string): Promise<IWebsocketsConnectionRegistryData[]> {\n const items = await queryAll<IWebsocketsConnectionRegistryDbItem>({\n entity: this.entity,\n partitionKey: GSI1_PK,\n options: {\n index: \"GSI1\",\n eq: identity\n }\n });\n return items.map(item => {\n return item.data;\n });\n }\n\n /**\n * Uses GSI2 keys\n */\n public async listViaTenant(\n tenant: string,\n locale?: string\n ): Promise<IWebsocketsConnectionRegistryData[]> {\n let options: Partial<EntityQueryOptions> = {\n beginsWith: `T#${tenant}#L#`\n };\n if (locale) {\n options = {\n eq: `T#${tenant}#L#${locale}`\n };\n }\n const items = await queryAll<IWebsocketsConnectionRegistryDbItem>({\n entity: this.entity,\n partitionKey: GSI2_PK,\n options: {\n ...options,\n index: \"GSI2\"\n }\n });\n return items.map(item => {\n return item.data;\n });\n }\n\n public async listAll(): Promise<IWebsocketsConnectionRegistryData[]> {\n const items = await queryAll<IWebsocketsConnectionRegistryDbItem>({\n entity: this.entity,\n partitionKey: PK,\n options: {\n gte: \" \"\n }\n });\n return items.map(item => {\n return item.data;\n });\n }\n\n private async store(data: IWebsocketsConnectionRegistryData) {\n const { connectionId, tenant, locale, identity } = data;\n const item: IWebsocketsConnectionRegistryDbItem = {\n // to find specific identity related to given connection\n PK,\n SK: connectionId,\n // to find all connections related to given identity\n GSI1_PK,\n GSI1_SK: identity.id,\n // to find all connections related to given tenant/locale combination\n GSI2_PK,\n GSI2_SK: `T#${tenant}#L#${locale}`,\n data\n };\n try {\n return await put({\n entity: this.entity,\n item\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not store websockets connection data.\",\n code: \"STORE_WEBSOCKETS_CONNECTION_DATA_ERROR\",\n data: item\n });\n }\n }\n}\n"],"mappings":";;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AAOA,IAAAC,OAAA,GAAAD,OAAA;AACA,IAAAE,WAAA,GAAAF,OAAA;AAIA,MAAMG,EAAE,GAAG,gBAAgB;AAC3B,MAAMC,OAAO,GAAG,yBAAyB;AACzC,MAAMC,OAAO,GAAG,8BAA8B;AAYvC,MAAMC,4BAA4B,CAA0C;EAGxEC,WAAWA,CAACC,cAAgC,EAAE;IACjD,IAAI,CAACC,MAAM,GAAG,IAAAC,oBAAY,EAACF,cAAc,CAAC;EAC9C;EAEA,MAAaG,QAAQA,CACjBC,MAAmD,EACT;IAC1C,MAAM;MAAEC,YAAY;MAAEC,MAAM;MAAEC,MAAM;MAAEC,QAAQ;MAAEC,UAAU;MAAEC,KAAK;MAAEC;IAAY,CAAC,G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- {"version":3,"names":["_error","_interopRequireDefault","require","_types","_plugins","_middleware","WebsocketsRunner","constructor","context","registry","validator","response","run","input","event","validate","ex","result","error","message","code","data","stack","connectionId","domainName","stage","eventType","requestContext","messageId","body","JSON","stringify","json","parse","respond","executeRoute","routeKey","getRoutePlugins","route","plugins","byType","WebsocketsRoutePlugin","type","filter","plugin","length","WebinyError","reverse","getTenant","tenant","tenancy","getCurrentTenant","id","getLocale","locale","i18n","getCurrentLocale","getIdentity","identity","security","action","middleware","map","params","next","statusCode","WebsocketsEventRequestContextEventType","console","connection","dataToSend","websockets","sendToConnections","exports"],"sources":["WebsocketsRunner.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport {\n IWebsocketsEvent,\n IWebsocketsEventData,\n IWebsocketsEventRequestContext,\n IWebsocketsIncomingEvent,\n WebsocketsEventRequestContextEventType,\n WebsocketsEventRoute\n} from \"~/handler/types\";\nimport { Context } from \"~/types\";\nimport { IWebsocketsEventValidator } from \"~/validator\";\nimport { IWebsocketsRunner, IWebsocketsRunnerResponse } from \"./abstractions/IWebsocketsRunner\";\nimport { IWebsocketsRoutePluginCallableParams, WebsocketsRoutePlugin } from \"~/plugins\";\nimport { middleware } from \"~/utils/middleware\";\nimport { IWebsocketsConnectionRegistry } from \"~/registry\";\nimport {\n IWebsocketsResponse,\n IWebsocketsResponseErrorResult,\n IWebsocketsResponseOkResult\n} from \"~/response\";\nimport { IWebsocketsTransportSendConnection } from \"~/transport\";\nimport { IWebsocketsIdentity } from \"~/context\";\n\ntype MiddlewareParams<C extends Context = Context> = Pick<\n IWebsocketsRoutePluginCallableParams<C>,\n \"context\" | \"event\" | \"registry\"\n>;\n\ninterface IWebsocketsRunnerRespondParams\n extends Pick<\n IWebsocketsEventRequestContext,\n \"connectionId\" | \"domainName\" | \"stage\" | \"eventType\"\n > {\n messageId?: string;\n result: IWebsocketsResponseOkResult | IWebsocketsResponseErrorResult;\n}\n\nexport class WebsocketsRunner implements IWebsocketsRunner {\n private readonly context: Context;\n private readonly registry: IWebsocketsConnectionRegistry;\n private readonly validator: IWebsocketsEventValidator;\n private readonly response: IWebsocketsResponse;\n\n public constructor(\n context: Context,\n registry: IWebsocketsConnectionRegistry,\n validator: IWebsocketsEventValidator,\n response: IWebsocketsResponse\n ) {\n this.context = context;\n this.registry = registry;\n this.validator = validator;\n this.response = response;\n }\n\n public async run<T extends IWebsocketsEventData = IWebsocketsEventData>(\n input: IWebsocketsIncomingEvent\n ): Promise<IWebsocketsRunnerResponse> {\n let event: IWebsocketsEvent<T> | undefined;\n try {\n event = await this.validator.validate<T>(input);\n } catch (ex) {\n const result = this.response.error({\n message: \"Validation failed.\",\n error: {\n message: ex.message,\n code: ex.code,\n data: ex.data,\n stack: ex.stack\n }\n });\n\n const { connectionId, domainName, stage, eventType } = input.requestContext || {};\n let messageId: string | undefined;\n try {\n const body =\n typeof input.body === \"string\" ? input.body : JSON.stringify(input.body || {});\n const json = JSON.parse(body);\n messageId = json.messageId;\n } catch {\n // Do nothing\n }\n if (!connectionId || !stage || !domainName || !eventType) {\n return result;\n }\n\n await this.respond({\n connectionId,\n domainName,\n stage,\n eventType,\n messageId,\n result\n });\n return result;\n }\n\n let result: IWebsocketsResponseOkResult | IWebsocketsResponseErrorResult;\n try {\n result = await this.executeRoute(event);\n } catch (ex) {\n result = this.response.error({\n message: `Route \"${event.requestContext.routeKey}\" action failed.`,\n error: {\n message: ex.message,\n code: ex.code,\n data: ex.data,\n stack: ex.stack\n }\n });\n }\n try {\n await this.respond({\n ...event.requestContext,\n messageId: event.body?.messageId,\n result\n });\n return result;\n } catch (ex) {\n return this.response.error({\n message: \"Failed to respond to the request.\",\n error: {\n message: ex.message,\n code: ex.code,\n data: {\n ...ex.data,\n result\n },\n stack: ex.stack\n }\n });\n }\n }\n\n private getRoutePlugins(route: WebsocketsEventRoute | string): WebsocketsRoutePlugin[] {\n const plugins = this.context.plugins\n .byType<WebsocketsRoutePlugin>(WebsocketsRoutePlugin.type)\n .filter(plugin => {\n return plugin.route === route;\n });\n if (plugins.length === 0) {\n throw new WebinyError(\n `There are no plugins for the route: ${route}.`,\n \"NO_ROUTE_PLUGINS\",\n {\n route\n }\n );\n }\n return plugins;\n }\n\n private async executeRoute(event: IWebsocketsEvent): Promise<IWebsocketsRunnerResponse> {\n /**\n * We will always fetch plugins in reverse order, so that users can override our default ones if necessary.\n */\n const plugins = this.getRoutePlugins(event.requestContext.routeKey).reverse();\n\n const getTenant = () => {\n const tenant = this.context.tenancy.getCurrentTenant();\n return tenant?.id || null;\n };\n const getLocale = (): string | null => {\n const locale = this.context.i18n.getCurrentLocale(\"content\");\n return locale?.code || null;\n };\n\n const getIdentity = (): IWebsocketsIdentity | null => {\n const identity = this.context.security.getIdentity();\n return identity || null;\n };\n\n const action = middleware<MiddlewareParams, IWebsocketsRunnerResponse>(\n plugins.map(plugin => {\n return async (params, next) => {\n return plugin.run({\n registry: params.registry,\n event: params.event,\n context: params.context,\n getTenant,\n getLocale,\n getIdentity,\n response: this.response,\n next\n });\n };\n })\n );\n\n const result = await action({\n event,\n registry: this.registry,\n context: this.context\n });\n if (result) {\n return result;\n }\n const message = \"No response from the route action.\";\n return this.response.error({\n message,\n error: {\n message,\n code: \"NO_RESPONSE\"\n },\n statusCode: 404\n });\n }\n\n private async respond(params: IWebsocketsRunnerRespondParams): Promise<void> {\n const { connectionId, domainName, stage, eventType, result, messageId } = params;\n if (eventType !== WebsocketsEventRequestContextEventType.message) {\n return;\n } else if (!connectionId || !domainName || !stage) {\n const message = \"No connectionId, domainName or stage.\";\n const data = {\n connectionId,\n domainName,\n stage\n };\n console.error(message, JSON.stringify(data));\n throw new WebinyError(message, \"GENERAL_ERROR\", data);\n }\n const connection: IWebsocketsTransportSendConnection = {\n connectionId,\n domainName,\n stage\n };\n\n const dataToSend = {\n ...result,\n messageId\n };\n await this.context.websockets.sendToConnections([connection], dataToSend);\n }\n}\n"],"mappings":";;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAA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,CAAC,CAAC;MAC5C,MAAM,IAAIyB,cAAW,CAAC3B,OAAO,EAAE,eAAe,EAAEE,IAAI,CAAC;IACzD;IACA,MAAM+C,UAA8C,GAAG;MACnD7C,YAAY;MACZC,UAAU;MACVC;IACJ,CAAC;IAED,MAAM4C,UAAU,GAAG;MACf,GAAGpD,MAAM;MACTW;IACJ,CAAC;IACD,MAAM,IAAI,CAACpB,OAAO,CAAC8D,UAAU,CAACC,iBAAiB,CAAC,CAACH,UAAU,CAAC,EAAEC,UAAU,CAAC;EAC7E;AACJ;AAACG,OAAA,CAAAlE,gBAAA,GAAAA,gBAAA","ignoreList":[]}
1
+ {"version":3,"names":["_error","_interopRequireDefault","require","_types","_plugins","_middleware","WebsocketsRunner","constructor","context","registry","validator","response","run","input","event","validate","ex","result","error","message","code","data","stack","connectionId","domainName","stage","eventType","requestContext","messageId","body","JSON","stringify","json","parse","respond","executeRoute","routeKey","getRoutePlugins","route","plugins","byType","WebsocketsRoutePlugin","type","filter","plugin","length","WebinyError","reverse","getTenant","tenant","tenancy","getCurrentTenant","id","getLocale","locale","i18n","getCurrentLocale","getIdentity","identity","security","action","middleware","map","params","next","statusCode","WebsocketsEventRequestContextEventType","console","connection","dataToSend","websockets","sendToConnections","exports"],"sources":["WebsocketsRunner.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport {\n IWebsocketsEvent,\n IWebsocketsEventData,\n IWebsocketsEventRequestContext,\n IWebsocketsIncomingEvent,\n WebsocketsEventRequestContextEventType,\n WebsocketsEventRoute\n} from \"~/handler/types\";\nimport { Context } from \"~/types\";\nimport { IWebsocketsEventValidator } from \"~/validator\";\nimport { IWebsocketsRunner, IWebsocketsRunnerResponse } from \"./abstractions/IWebsocketsRunner\";\nimport { IWebsocketsRoutePluginCallableParams, WebsocketsRoutePlugin } from \"~/plugins\";\nimport { middleware } from \"~/utils/middleware\";\nimport { IWebsocketsConnectionRegistry } from \"~/registry\";\nimport {\n IWebsocketsResponse,\n IWebsocketsResponseErrorResult,\n IWebsocketsResponseOkResult\n} from \"~/response\";\nimport { IWebsocketsTransportSendConnection } from \"~/transport\";\nimport { IWebsocketsIdentity } from \"~/context\";\n\ntype MiddlewareParams<C extends Context = Context> = Pick<\n IWebsocketsRoutePluginCallableParams<C>,\n \"context\" | \"event\" | \"registry\"\n>;\n\ninterface IWebsocketsRunnerRespondParams\n extends Pick<\n IWebsocketsEventRequestContext,\n \"connectionId\" | \"domainName\" | \"stage\" | \"eventType\"\n > {\n messageId?: string;\n result: IWebsocketsResponseOkResult | IWebsocketsResponseErrorResult;\n}\n\nexport class WebsocketsRunner implements IWebsocketsRunner {\n private readonly context: Context;\n private readonly registry: IWebsocketsConnectionRegistry;\n private readonly validator: IWebsocketsEventValidator;\n private readonly response: IWebsocketsResponse;\n\n public constructor(\n context: Context,\n registry: IWebsocketsConnectionRegistry,\n validator: IWebsocketsEventValidator,\n response: IWebsocketsResponse\n ) {\n this.context = context;\n this.registry = registry;\n this.validator = validator;\n this.response = response;\n }\n\n public async run<T extends IWebsocketsEventData = IWebsocketsEventData>(\n input: IWebsocketsIncomingEvent\n ): Promise<IWebsocketsRunnerResponse> {\n let event: IWebsocketsEvent<T> | undefined;\n try {\n event = await this.validator.validate<T>(input);\n } catch (ex) {\n const result = this.response.error({\n message: \"Validation failed.\",\n error: {\n message: ex.message,\n code: ex.code,\n data: ex.data,\n stack: ex.stack\n }\n });\n\n const { connectionId, domainName, stage, eventType } = input.requestContext || {};\n let messageId: string | undefined;\n try {\n const body =\n typeof input.body === \"string\" ? input.body : JSON.stringify(input.body || {});\n const json = JSON.parse(body);\n messageId = json.messageId;\n } catch {\n // Do nothing\n }\n if (!connectionId || !stage || !domainName || !eventType) {\n return result;\n }\n\n await this.respond({\n connectionId,\n domainName,\n stage,\n eventType,\n messageId,\n result\n });\n return result;\n }\n\n let result: IWebsocketsResponseOkResult | IWebsocketsResponseErrorResult;\n try {\n result = await this.executeRoute(event);\n } catch (ex) {\n result = this.response.error({\n message: `Route \"${event.requestContext.routeKey}\" action failed.`,\n error: {\n message: ex.message,\n code: ex.code,\n data: ex.data,\n stack: ex.stack\n }\n });\n }\n try {\n await this.respond({\n ...event.requestContext,\n messageId: event.body?.messageId,\n result\n });\n return result;\n } catch (ex) {\n return this.response.error({\n message: \"Failed to respond to the request.\",\n error: {\n message: ex.message,\n code: ex.code,\n data: {\n ...ex.data,\n result\n },\n stack: ex.stack\n }\n });\n }\n }\n\n private getRoutePlugins(route: WebsocketsEventRoute | string): WebsocketsRoutePlugin[] {\n const plugins = this.context.plugins\n .byType<WebsocketsRoutePlugin>(WebsocketsRoutePlugin.type)\n .filter(plugin => {\n return plugin.route === route;\n });\n if (plugins.length === 0) {\n throw new WebinyError(\n `There are no plugins for the route: ${route}.`,\n \"NO_ROUTE_PLUGINS\",\n {\n route\n }\n );\n }\n return plugins;\n }\n\n private async executeRoute(event: IWebsocketsEvent): Promise<IWebsocketsRunnerResponse> {\n /**\n * We will always fetch plugins in reverse order, so that users can override our default ones if necessary.\n */\n const plugins = this.getRoutePlugins(event.requestContext.routeKey).reverse();\n\n const getTenant = () => {\n const tenant = this.context.tenancy.getCurrentTenant();\n return tenant?.id || null;\n };\n const getLocale = (): string | null => {\n const locale = this.context.i18n.getCurrentLocale(\"content\");\n return locale?.code || null;\n };\n\n const getIdentity = (): IWebsocketsIdentity | null => {\n const identity = this.context.security.getIdentity();\n return identity || null;\n };\n\n const action = middleware<MiddlewareParams, IWebsocketsRunnerResponse>(\n plugins.map(plugin => {\n return async (params, next) => {\n return plugin.run({\n registry: params.registry,\n event: params.event,\n context: params.context,\n getTenant,\n getLocale,\n getIdentity,\n response: this.response,\n next\n });\n };\n })\n );\n\n const result = await action({\n event,\n registry: this.registry,\n context: this.context\n });\n if (result) {\n return result;\n }\n const message = \"No response from the route action.\";\n return this.response.error({\n message,\n error: {\n message,\n code: \"NO_RESPONSE\"\n },\n statusCode: 404\n });\n }\n\n private async respond(params: IWebsocketsRunnerRespondParams): Promise<void> {\n const { connectionId, domainName, stage, eventType, result, messageId } = params;\n if (eventType !== WebsocketsEventRequestContextEventType.message) {\n return;\n } else if (!connectionId || !domainName || !stage) {\n const message = \"No connectionId, domainName or stage.\";\n const data = {\n connectionId,\n domainName,\n stage\n };\n console.error(message, JSON.stringify(data));\n throw new WebinyError(message, \"GENERAL_ERROR\", data);\n }\n const connection: IWebsocketsTransportSendConnection = {\n connectionId,\n domainName,\n stage\n };\n\n const dataToSend = {\n ...result,\n messageId\n };\n await this.context.websockets.sendToConnections([connection], dataToSend);\n }\n}\n"],"mappings":";;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AAWA,IAAAE,QAAA,GAAAF,OAAA;AACA,IAAAG,WAAA,GAAAH,OAAA;AAwBO,MAAMI,gBAAgB,CAA8B;EAMhDC,WAAWA,CACdC,OAAgB,EAChBC,QAAuC,EACvCC,SAAoC,EACpCC,QAA6B,EAC/B;IACE,IAAI,CAACH,OAAO,GAAGA,OAAO;IACtB,IAAI,CAACC,QAAQ,GAAGA,QAAQ;IACxB,IAAI,CAACC,SAAS,GAAGA,SAAS;IAC1B,IAAI,CAACC,QAAQ,GAAGA,QAAQ;EAC5B;EAEA,MAAaC,GAAGA,CACZC,KAA+B,EACG;IAClC,IAAIC,KAAsC;IAC1C,IAAI;MACAA,KAAK,GAAG,MAAM,IAAI,CAACJ,SAAS,CAACK,QAAQ,CAAIF,KAAK,CAAC;IACnD,CAAC,CAAC,OAAOG,EAAE,EAAE;MACT,MAAMC,MAAM,GAAG,IAAI,CAACN,QAAQ,CAACO,KAAK,CAAC;QAC/BC,OAAO,EAAE,oBAAoB;QAC7BD,KAAK,EAAE;UACHC,OAAO,EAAEH,EAAE,CAACG,OAAO;UACnBC,IAAI,EAAEJ,EAAE,CAACI,IAAI;UACbC,IAAI,EAAEL,EAAE,CAACK,IAAI;UACbC,KAAK,EAAEN,EAAE,CAACM;QACd;MACJ,CAAC,CAAC;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AEC,UAAU;MAAEC,KAAK;MAAEC,SAAS;MAAET,MAAM;MAAEW;IAAU,CAAC,GAAGmC,MAAM;IAChF,IAAIrC,SAAS,KAAKwC,6CAAsC,CAAC/C,OAAO,EAAE;MAC9D;IACJ,CAAC,MAAM,IAAI,CAACI,YAAY,IAAI,CAACC,UAAU,IAAI,CAACC,KAAK,EAAE;MAC/C,MAAMN,OAAO,GAAG,uCAAuC;MACvD,MAAME,IAAI,GAAG;QACTE,YAAY;QACZC,UAAU;QACVC;MACJ,CAAC;MACD0C,OAAO,CAACjD,KAAK,CAACC,OAAO,EAAEW,IAAI,CAACC,SAAS,CAACV,IAAI,CAAC,CAAC;MAC5C,MAAM,IAAIyB,cAAW,CAAC3B,OAAO,EAAE,eAAe,EAAEE,IAAI,CAAC;IACzD;IACA,MAAM+C,UAA8C,GAAG;MACnD7C,YAAY;MACZC,UAAU;MACVC;IACJ,CAAC;IAED,MAAM4C,UAAU,GAAG;MACf,GAAGpD,MAAM;MACTW;IACJ,CAAC;IACD,MAAM,IAAI,CAACpB,OAAO,CAAC8D,UAAU,CAACC,iBAAiB,CAAC,CAACH,UAAU,CAAC,EAAEC,UAAU,CAAC;EAC7E;AACJ;AAACG,OAAA,CAAAlE,gBAAA,GAAAA,gBAAA","ignoreList":[]}
@@ -1 +1 @@
1
- {"version":3,"names":["_clientApigatewaymanagementapi","require","WebsocketsTransport","clients","Map","send","connections","data","connection","client","getClient","command","PostToConnectionCommand","ConnectionId","connectionId","Data","JSON","stringify","ex","console","error","log","disconnect","DeleteConnectionCommand","endpoint","domainName","stage","get","newClient","ApiGatewayManagementApiClient","set","exports"],"sources":["WebsocketsTransport.ts"],"sourcesContent":["import {\n ApiGatewayManagementApiClient,\n DeleteConnectionCommand,\n PostToConnectionCommand\n} from \"@webiny/aws-sdk/client-apigatewaymanagementapi\";\nimport {\n IWebsocketsTransport,\n IWebsocketsTransportDisconnectConnection,\n IWebsocketsTransportSendConnection,\n IWebsocketsTransportSendData\n} from \"./abstractions/IWebsocketsTransport\";\nimport { GenericRecord } from \"@webiny/api/types\";\n\nexport class WebsocketsTransport implements IWebsocketsTransport {\n private readonly clients = new Map<string, ApiGatewayManagementApiClient>();\n\n public async send<T extends GenericRecord = GenericRecord>(\n connections: IWebsocketsTransportSendConnection[],\n data: IWebsocketsTransportSendData<T>\n ): Promise<void> {\n for (const connection of connections) {\n try {\n const client = this.getClient(connection);\n\n const command = new PostToConnectionCommand({\n ConnectionId: connection.connectionId,\n Data: JSON.stringify(data)\n });\n await client.send(command);\n } catch (ex) {\n console.error(\n `Failed to send message to connection \"${connection.connectionId}\". Check logs for more information.`\n );\n console.log(ex);\n }\n }\n }\n\n public async disconnect(connections: IWebsocketsTransportDisconnectConnection[]) {\n for (const connection of connections) {\n try {\n const client = this.getClient(connection);\n const command = new DeleteConnectionCommand({\n ConnectionId: connection.connectionId\n });\n await client.send(command);\n } catch (ex) {\n console.error(\n `Failed to disconnect connection \"${connection.connectionId}\". Check logs for more information.`\n );\n console.log(ex);\n }\n }\n }\n\n private getClient(\n connection: IWebsocketsTransportSendConnection\n ): ApiGatewayManagementApiClient {\n const endpoint = `https://${connection.domainName}/${connection.stage}`;\n const client = this.clients.get(endpoint);\n if (client) {\n return client;\n }\n const newClient = new ApiGatewayManagementApiClient({\n endpoint\n });\n this.clients.set(endpoint, newClient);\n return newClient;\n }\n}\n"],"mappings":";;;;;;AAAA,IAAAA,8BAAA,GAAAC,OAAA;AAaO,MAAMC,mBAAmB,CAAiC;EAC5CC,OAAO,GAAG,IAAIC,GAAG,CAAwC,CAAC;EAE3E,MAAaC,IAAIA,CACbC,WAAiD,EACjDC,IAAqC,EACxB;IACb,KAAK,MAAMC,UAAU,IAAIF,WAAW,EAAE;MAClC,IAAI;QACA,MAAMG,MAAM,GAAG,IAAI,CAACC,SAAS,CAACF,UAAU,CAAC;QAEzC,MAAMG,OAAO,GAAG,IAAIC,sDAAuB,CAAC;UACxCC,YAAY,EAAEL,UAAU,CAACM,YAAY;UACrC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}
1
+ {"version":3,"names":["_clientApigatewaymanagementapi","require","WebsocketsTransport","clients","Map","send","connections","data","connection","client","getClient","command","PostToConnectionCommand","ConnectionId","connectionId","Data","JSON","stringify","ex","console","error","log","disconnect","DeleteConnectionCommand","endpoint","domainName","stage","get","newClient","ApiGatewayManagementApiClient","set","exports"],"sources":["WebsocketsTransport.ts"],"sourcesContent":["import {\n ApiGatewayManagementApiClient,\n DeleteConnectionCommand,\n PostToConnectionCommand\n} from \"@webiny/aws-sdk/client-apigatewaymanagementapi\";\nimport {\n IWebsocketsTransport,\n IWebsocketsTransportDisconnectConnection,\n IWebsocketsTransportSendConnection,\n IWebsocketsTransportSendData\n} from \"./abstractions/IWebsocketsTransport\";\nimport { GenericRecord } from \"@webiny/api/types\";\n\nexport class WebsocketsTransport implements IWebsocketsTransport {\n private readonly clients = new Map<string, ApiGatewayManagementApiClient>();\n\n public async send<T extends GenericRecord = GenericRecord>(\n connections: IWebsocketsTransportSendConnection[],\n data: IWebsocketsTransportSendData<T>\n ): Promise<void> {\n for (const connection of connections) {\n try {\n const client = this.getClient(connection);\n\n const command = new PostToConnectionCommand({\n ConnectionId: connection.connectionId,\n Data: JSON.stringify(data)\n });\n await client.send(command);\n } catch (ex) {\n console.error(\n `Failed to send message to connection \"${connection.connectionId}\". Check logs for more information.`\n );\n console.log(ex);\n }\n }\n }\n\n public async disconnect(connections: IWebsocketsTransportDisconnectConnection[]) {\n for (const connection of connections) {\n try {\n const client = this.getClient(connection);\n const command = new DeleteConnectionCommand({\n ConnectionId: connection.connectionId\n });\n await client.send(command);\n } catch (ex) {\n console.error(\n `Failed to disconnect connection \"${connection.connectionId}\". Check logs for more information.`\n );\n console.log(ex);\n }\n }\n }\n\n private getClient(\n connection: IWebsocketsTransportSendConnection\n ): ApiGatewayManagementApiClient {\n const endpoint = `https://${connection.domainName}/${connection.stage}`;\n const client = this.clients.get(endpoint);\n if (client) {\n return client;\n }\n const newClient = new ApiGatewayManagementApiClient({\n endpoint\n });\n this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AQ,GAAG,WAAWhB,UAAU,CAACiB,UAAU,IAAIjB,UAAU,CAACkB,KAAK,EAAE;IACvE,MAAMjB,MAAM,GAAG,IAAI,CAACN,OAAO,CAACwB,GAAG,CAACH,QAAQ,CAAC;IACzC,IAAIf,MAAM,EAAE;MACR,OAAOA,MAAM;IACjB;IACA,MAAMmB,SAAS,GAAG,IAAIC,4DAA6B,CAAC;MAChDL;IACJ,CAAC,CAAC;IACF,IAAI,CAACrB,OAAO,CAAC2B,GAAG,CAACN,QAAQ,EAAEI,SAAS,CAAC;IACrC,OAAOA,SAAS;EACpB;AACJ;AAACG,OAAA,CAAA7B,mBAAA,GAAAA,mBAAA","ignoreList":[]}
@@ -1 +1 @@
1
- {"version":3,"names":["_zod","_interopRequireDefault","require","_types","_utils","validation","zod","object","headers","passthrough","optional","requestContext","connectionId","string","stage","connectedAt","number","domainName","eventType","enum","WebsocketsEventRequestContextEventType","connect","message","disconnect","routeKey","body","transform","value","context","undefined","JSON","parse","ex","console","error","log","addIssue","path","code","ZodIssueCode","custom","fatal","superRefine","output","bodyValidation","token","tenant","locale","messageId","nullish","action","data","WebsocketsEventValidator","validate","input","result","safeParseAsync","success","createZodError","bodyResult","exports"],"sources":["WebsocketsEventValidator.ts"],"sourcesContent":["import zod from \"zod\";\nimport {\n IWebsocketsEvent,\n IWebsocketsEventData,\n WebsocketsEventRequestContextEventType\n} from \"~/handler/types\";\nimport {\n IWebsocketsEventValidator,\n IWebsocketsEventValidatorValidateParams\n} from \"./abstractions/IWebsocketsEventValidator\";\nimport { createZodError } from \"@webiny/utils\";\n\nconst validation = zod\n .object({\n headers: zod.object({}).passthrough().optional(),\n requestContext: zod.object({\n connectionId: zod.string(),\n stage: zod.string(),\n connectedAt: zod.number(),\n domainName: zod.string(),\n eventType: zod.enum([\n WebsocketsEventRequestContextEventType.connect,\n WebsocketsEventRequestContextEventType.message,\n WebsocketsEventRequestContextEventType.disconnect\n ]),\n routeKey: zod.string()\n }),\n body: zod\n .string()\n .transform<IWebsocketsEventData>((value, context) => {\n if (!value) {\n return undefined;\n }\n try {\n return JSON.parse(value);\n } catch (ex) {\n /**\n * We want to log the error, for easier debugging.\n */\n console.error(`Failed body validation: ${ex.message}`);\n console.log(`Body: ${value}`);\n /**\n * And we want to add an issue to the context, so that the user knows what went wrong.\n */\n context.addIssue({\n path: [],\n message: `Invalid JSON: ${ex.message}`,\n code: zod.ZodIssueCode.custom,\n fatal: true\n });\n }\n })\n .optional()\n })\n .superRefine((output, context) => {\n if (output.requestContext.eventType !== WebsocketsEventRequestContextEventType.message) {\n return;\n } else if (output.body) {\n return;\n }\n context.addIssue({\n path: [\"body\"],\n message: \"There must be a body defined when having a message event.\",\n code: zod.ZodIssueCode.custom,\n fatal: true\n });\n });\n\nconst bodyValidation = zod\n .object({\n token: zod.string(),\n tenant: zod.string(),\n locale: zod.string(),\n messageId: zod.string().nullish(),\n action: zod.string(),\n data: zod.object({}).passthrough().nullish()\n })\n .passthrough()\n .optional();\n\nexport class WebsocketsEventValidator implements IWebsocketsEventValidator {\n public async validate<T extends IWebsocketsEventData = IWebsocketsEventData>(\n input: IWebsocketsEventValidatorValidateParams\n ): Promise<IWebsocketsEvent<T>> {\n const result = await validation.safeParseAsync(input);\n if (!result.success) {\n throw createZodError(result.error);\n }\n const bodyResult = await bodyValidation.safeParseAsync(result.data.body);\n if (!bodyResult.success) {\n throw createZodError(bodyResult.error);\n }\n return {\n ...result.data,\n body: {\n ...((bodyResult.data || {}) as T)\n }\n };\n }\n}\n"],"mappings":";;;;;;;AAAA,IAAAA,IAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AASA,IAAAE,MAAA,GAAAF,OAAA;AAEA,MAAMG,UAAU,GAAGC,YAAG,CACjBC,MAAM,CAAC;EACJC,OAAO,EAAEF,YAAG,CAACC,MAAM,CAAC,CAAC,CAAC,CAAC,CAACE,WAAW,CAAC,CAAC,CAACC,QAAQ,CAAC,CAAC;EAChDC,cAA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U,CAACzB,KAAK,CAAC;IAC1C;IACA,OAAO;MACH,GAAGqB,MAAM,CAACJ,IAAI;MACd1B,IAAI,EAAE;QACF,IAAKkC,UAAU,CAACR,IAAI,IAAI,CAAC,CAAC;MAC9B;IACJ,CAAC;EACL;AACJ;AAACS,OAAA,CAAAR,wBAAA,GAAAA,wBAAA","ignoreList":[]}
1
+ {"version":3,"names":["_zod","_interopRequireDefault","require","_types","_utils","validation","zod","object","headers","passthrough","optional","requestContext","connectionId","string","stage","connectedAt","number","domainName","eventType","enum","WebsocketsEventRequestContextEventType","connect","message","disconnect","routeKey","body","transform","value","context","undefined","JSON","parse","ex","console","error","log","addIssue","path","code","ZodIssueCode","custom","fatal","superRefine","output","bodyValidation","token","tenant","locale","messageId","nullish","action","data","WebsocketsEventValidator","validate","input","result","safeParseAsync","success","createZodError","bodyResult","exports"],"sources":["WebsocketsEventValidator.ts"],"sourcesContent":["import zod from \"zod\";\nimport {\n IWebsocketsEvent,\n IWebsocketsEventData,\n WebsocketsEventRequestContextEventType\n} from \"~/handler/types\";\nimport {\n IWebsocketsEventValidator,\n IWebsocketsEventValidatorValidateParams\n} from \"./abstractions/IWebsocketsEventValidator\";\nimport { createZodError } from \"@webiny/utils\";\n\nconst validation = zod\n .object({\n headers: zod.object({}).passthrough().optional(),\n requestContext: zod.object({\n connectionId: zod.string(),\n stage: zod.string(),\n connectedAt: zod.number(),\n domainName: zod.string(),\n eventType: zod.enum([\n WebsocketsEventRequestContextEventType.connect,\n WebsocketsEventRequestContextEventType.message,\n WebsocketsEventRequestContextEventType.disconnect\n ]),\n routeKey: zod.string()\n }),\n body: zod\n .string()\n .transform<IWebsocketsEventData>((value, context) => {\n if (!value) {\n return undefined;\n }\n try {\n return JSON.parse(value);\n } catch (ex) {\n /**\n * We want to log the error, for easier debugging.\n */\n console.error(`Failed body validation: ${ex.message}`);\n console.log(`Body: ${value}`);\n /**\n * And we want to add an issue to the context, so that the user knows what went wrong.\n */\n context.addIssue({\n path: [],\n message: `Invalid JSON: ${ex.message}`,\n code: zod.ZodIssueCode.custom,\n fatal: true\n });\n }\n })\n .optional()\n })\n .superRefine((output, context) => {\n if (output.requestContext.eventType !== WebsocketsEventRequestContextEventType.message) {\n return;\n } else if (output.body) {\n return;\n }\n context.addIssue({\n path: [\"body\"],\n message: \"There must be a body defined when having a message event.\",\n code: zod.ZodIssueCode.custom,\n fatal: true\n });\n });\n\nconst bodyValidation = zod\n .object({\n token: zod.string(),\n tenant: zod.string(),\n locale: zod.string(),\n messageId: zod.string().nullish(),\n action: zod.string(),\n data: zod.object({}).passthrough().nullish()\n })\n .passthrough()\n .optional();\n\nexport class WebsocketsEventValidator implements IWebsocketsEventValidator {\n public async validate<T extends IWebsocketsEventData = IWebsocketsEventData>(\n input: IWebsocketsEventValidatorValidateParams\n ): Promise<IWebsocketsEvent<T>> {\n const result = await validation.safeParseAsync(input);\n if (!result.success) {\n throw createZodError(result.error);\n }\n const bodyResult = await bodyValidation.safeParseAsync(result.data.body);\n if (!bodyResult.success) {\n throw createZodError(bodyResult.error);\n }\n return {\n ...result.data,\n body: {\n ...((bodyResult.data || {}) as T)\n }\n };\n }\n}\n"],"mappings":";;;;;;;AAAA,IAAAA,IAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AASA,IAAAE,MAAA,GAAAF,OAAA;AAEA,MAAMG,UAAU,GAAGC,YAAG,CACjBC,MAAM,CAAC;EACJC,OAAO,EAAEF,YAAG,CAACC,MAAM,CAAC,CAAC,CAAC,CAAC,CAACE,WAAW,CAAC,CAAC,CAACC,QAAQ,CAAC,CAAC;EAChDC,cAAc,EAAEL,YAAG,CAACC,MAAM,CAAC;IACvBK,YAAY,EAAEN,YAAG,CAACO,MAAM,CAAC,CAAC;IAC1BC,KAAK,EAAER,YAAG,CAACO,MAAM,CAAC,CAAC;IACnBE,WAAW,EAAET,YAAG,CAACU,MAAM,CAAC,CAAC;IACzBC,UAAU,EAAEX,YAAG,CAACO,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