@webiny/api-websockets-aws 6.5.2-beta.0 → 6.5.2-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/handler/handler.js
CHANGED
package/handler/handler.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler/handler.js","sources":["../../src/handler/handler.ts"],"sourcesContent":["import type WebinyError from \"@webiny/error\";\nimport { createHandler as createBaseHandler } from \"@webiny/handler\";\nimport { registerDefaultPlugins } from \"@webiny/handler-aws/plugins/index.js\";\nimport { execute } from \"@webiny/handler-aws/execute.js\";\nimport { PluginsContainer } from \"@webiny/plugins\";\nimport {\n createWebsocketsRoutePlugins,\n WebsocketsRunner,\n WebsocketsTransport,\n WebsocketsResponse\n} from \"@webiny/api-websockets/exports/api.js\";\nimport type { DbContext } from \"@webiny/handler-db/types.js\";\nimport type { ApiCoreContext } from \"@webiny/api-core/types/core.js\";\n\ntype Context = DbContext & ApiCoreContext;\nimport type { PluginCollection } from \"@webiny/plugins/types.js\";\nimport type { HandlerCallable, HandlerParams } from \"./types.js\";\nimport { getEventValues } from \"./headers.js\";\nimport { AwsWebsocketsEventValidator } from \"~/validator/AwsWebsocketsEventValidator.js\";\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 const validator = new AwsWebsocketsEventValidator();\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 await app.register(async wsApp => {\n wsApp.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 wsApp.post(url, async (_, reply) => {\n const { response } = params;\n const context = app.webiny as Context;\n\n const responseObj = response || context.container.resolve(WebsocketsResponse);\n const runner = new WebsocketsRunner(context, responseObj);\n\n let validatedEvent;\n try {\n validatedEvent = await validator.validate(event);\n } catch (ex) {\n const errorResult = responseObj.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 } =\n event.requestContext || {};\n if (connectionId && domainName && stage && eventType === \"MESSAGE\") {\n try {\n const transport = context.container.resolve(WebsocketsTransport);\n const endpoint = `https://${domainName}/${stage}`;\n await transport.send([{ connectionId, endpoint }], errorResult);\n } catch {\n /* best effort. */\n }\n }\n\n app.__webiny_raw_result = {\n statusCode: errorResult.statusCode,\n headers: {\n \"sec-websocket-protocol\": \"webiny-ws-v1\"\n }\n };\n return reply.send();\n }\n\n const result = await runner.run(validatedEvent);\n\n app.__webiny_raw_result = {\n statusCode: result.statusCode,\n headers: {\n \"sec-websocket-protocol\": \"webiny-ws-v1\"\n }\n };\n\n return reply.send();\n });\n });\n\n const { tenant, endpoint, token } = getEventValues(event);\n\n const headers = {\n Authorization: `Bearer ${token}`,\n [\"x-tenant\"]: tenant,\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"],"names":["url","createPluginsContainer","plugins","PluginsContainer","createHandler","params","createWebsocketsRoutePlugins","validator","AwsWebsocketsEventValidator","event","app","createBaseHandler","registerDefaultPlugins","wsApp","error","_","reply","response","context","responseObj","WebsocketsResponse","runner","WebsocketsRunner","validatedEvent","ex","errorResult","connectionId","domainName","stage","eventType","transport","WebsocketsTransport","endpoint","result","tenant","token","getEventValues","headers","execute"],"mappings":";;;;;;;AAoBA,MAAMA,MAAM;AAEZ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oBAEA,MAAM,EAAEE,YAAY,EAAEC,UAAU,EAAEC,KAAK,EAAEC,SAAS,EAAE,GAChDpB,MAAM,cAAc,IAAI,CAAC;oBAC7B,IAAIiB,gBAAgBC,cAAcC,SAASC,AAAc,cAAdA,WACvC,IAAI;wBACA,MAAMC,YAAYZ,QAAQ,SAAS,CAAC,OAAO,CAACa;wBAC5C,MAAMC,WAAW,CAAC,QAAQ,EAAEL,WAAW,CAAC,EAAEC,OAAO;wBACjD,MAAME,UAAU,IAAI,CAAC;4BAAC;gCAAEJ;gCAAcM;4BAAS;yBAAE,EAAEP;oBACvD,EAAE,OAAM,CAER;oBAGJf,IAAI,mBAAmB,GAAG;wBACtB,YAAYe,YAAY,UAAU;wBAClC,SAAS;4BACL,0BAA0B;wBAC9B;oBACJ;oBACA,OAAOT,MAAM,IAAI;gBACrB;gBAEA,MAAMiB,SAAS,MAAMZ,OAAO,GAAG,CAACE;gBAEhCb,IAAI,mBAAmB,GAAG;oBACtB,YAAYuB,OAAO,UAAU;oBAC7B,SAAS;wBACL,0BAA0B;oBAC9B;gBACJ;gBAEA,OAAOjB,MAAM,IAAI;YACrB;QACJ;QAEA,MAAM,EAAEkB,MAAM,EAAEF,QAAQ,EAAEG,KAAK,EAAE,GAAGC,eAAe3B;QAEnD,MAAM4B,UAAU;YACZ,eAAe,CAAC,OAAO,EAAEF,OAAO;YAChC,CAAC,WAAW,EAAED;YACd,CAAC,wBAAwB,EAAEF;YAC3B,GAAGvB,MAAM,OAAO;QACpB;QAEA,OAAO6B,QAAQ;YACX5B;YACAV
|
|
1
|
+
{"version":3,"file":"handler/handler.js","sources":["../../src/handler/handler.ts"],"sourcesContent":["import type WebinyError from \"@webiny/error\";\nimport { createHandler as createBaseHandler } from \"@webiny/handler\";\nimport { registerDefaultPlugins } from \"@webiny/handler-aws/plugins/index.js\";\nimport { execute } from \"@webiny/handler-aws/execute.js\";\nimport { PluginsContainer } from \"@webiny/plugins\";\nimport {\n createWebsocketsRoutePlugins,\n WebsocketsRunner,\n WebsocketsTransport,\n WebsocketsResponse\n} from \"@webiny/api-websockets/exports/api.js\";\nimport type { DbContext } from \"@webiny/handler-db/types.js\";\nimport type { ApiCoreContext } from \"@webiny/api-core/types/core.js\";\n\ntype Context = DbContext & ApiCoreContext;\nimport type { PluginCollection } from \"@webiny/plugins/types.js\";\nimport type { HandlerCallable, HandlerParams } from \"./types.js\";\nimport { getEventValues } from \"./headers.js\";\nimport { AwsWebsocketsEventValidator } from \"~/validator/AwsWebsocketsEventValidator.js\";\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 const validator = new AwsWebsocketsEventValidator();\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 await app.register(async wsApp => {\n wsApp.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 wsApp.post(url, async (_, reply) => {\n const { response } = params;\n const context = app.webiny as Context;\n\n const responseObj = response || context.container.resolve(WebsocketsResponse);\n const runner = new WebsocketsRunner(context, responseObj);\n\n let validatedEvent;\n try {\n validatedEvent = await validator.validate(event);\n } catch (ex) {\n const errorResult = responseObj.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 } =\n event.requestContext || {};\n if (connectionId && domainName && stage && eventType === \"MESSAGE\") {\n try {\n const transport = context.container.resolve(WebsocketsTransport);\n const endpoint = `https://${domainName}/${stage}`;\n await transport.send([{ connectionId, endpoint }], errorResult);\n } catch {\n /* best effort. */\n }\n }\n\n app.__webiny_raw_result = {\n statusCode: errorResult.statusCode,\n headers: {\n \"sec-websocket-protocol\": \"webiny-ws-v1\"\n }\n };\n return reply.send();\n }\n\n const result = await runner.run(validatedEvent);\n\n app.__webiny_raw_result = {\n statusCode: result.statusCode,\n headers: {\n \"sec-websocket-protocol\": \"webiny-ws-v1\"\n }\n };\n\n return reply.send();\n });\n });\n\n const { tenant, endpoint, token } = getEventValues(event);\n\n const headers = {\n Authorization: `Bearer ${token}`,\n [\"x-tenant\"]: tenant,\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"],"names":["url","createPluginsContainer","plugins","PluginsContainer","createHandler","params","createWebsocketsRoutePlugins","validator","AwsWebsocketsEventValidator","event","app","createBaseHandler","registerDefaultPlugins","wsApp","error","_","reply","response","context","responseObj","WebsocketsResponse","runner","WebsocketsRunner","validatedEvent","ex","errorResult","connectionId","domainName","stage","eventType","transport","WebsocketsTransport","endpoint","result","tenant","token","getEventValues","headers","execute"],"mappings":";;;;;;;AAoBA,MAAMA,MAAM;AAEZ,MAAMC,yBAAyB,CAC3BC;IAEA,IAAIA,mBAAmBC,kBACnB,OAAOD;IAEX,OAAO,IAAIC,iBAAiBD,WAAW,EAAE;AAC7C;AAEO,MAAME,wBAAgB,CAACC;IAC1B,MAAMH,UAAUD,uBAAuBI,OAAO,OAAO;IACrDH,QAAQ,QAAQ,IAAII;IAEpB,MAAMC,YAAY,IAAIC;IAEtB,OAAO,OAAMC;QACT,MAAMC,MAAMC,cAAkB;YAC1B,GAAGN,MAAM;YACTH;YACA,SAAS;gBACL,QAAQG,AAAiB,SAAjBA,OAAO,KAAK;gBACpB,GAAIA,OAAO,OAAO,IAAI,CAAC,CAAC;YAC5B;QACJ;QAEAO,uBAAuBF,IAAI,MAAM;QAEj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,SAAS;4BACL,0BAA0B;wBAC9B;oBACJ;oBACA,OAAOT,MAAM,IAAI;gBACrB;gBAEA,MAAMiB,SAAS,MAAMZ,OAAO,GAAG,CAACE;gBAEhCb,IAAI,mBAAmB,GAAG;oBACtB,YAAYuB,OAAO,UAAU;oBAC7B,SAAS;wBACL,0BAA0B;oBAC9B;gBACJ;gBAEA,OAAOjB,MAAM,IAAI;YACrB;QACJ;QAEA,MAAM,EAAEkB,MAAM,EAAEF,QAAQ,EAAEG,KAAK,EAAE,GAAGC,eAAe3B;QAEnD,MAAM4B,UAAU;YACZ,eAAe,CAAC,OAAO,EAAEF,OAAO;YAChC,CAAC,WAAW,EAAED;YACd,CAAC,wBAAwB,EAAEF;YAC3B,GAAGvB,MAAM,OAAO;QACpB;QAEA,OAAO6B,QAAQ;YACX5B;YACAV;YACA,SAAS;gBACL,GAAGS,KAAK;gBACR4B;YACJ;QACJ;IACJ;AACJ"}
|
package/handler/headers.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { parseJson } from "@webiny/utils/parseJson.js";
|
|
1
2
|
const getEventBody = (event)=>{
|
|
2
3
|
if (!event.body) return {};
|
|
3
4
|
else if ("object" == typeof event.body) return event.body;
|
|
4
5
|
else if ("string" == typeof event.body) try {
|
|
5
|
-
return
|
|
6
|
+
return parseJson(event.body);
|
|
6
7
|
} catch (ex) {
|
|
7
8
|
console.log(ex.message);
|
|
8
9
|
return {};
|
package/handler/headers.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler/headers.js","sources":["../../src/handler/headers.ts"],"sourcesContent":["import type { WebsocketsRunner } from \"@webiny/api-websockets/exports/api.js\";\nimport type { IAwsWebsocketsIncomingEvent } from \"~/handler/types.js\";\n\nconst getEventBody = (event: IAwsWebsocketsIncomingEvent): WebsocketsRunner.EventData => {\n if (!event.body) {\n return {};\n } else if (typeof event.body === \"object\") {\n return event.body;\n } else if (typeof event.body === \"string\") {\n try {\n return
|
|
1
|
+
{"version":3,"file":"handler/headers.js","sources":["../../src/handler/headers.ts"],"sourcesContent":["import { parseJson } from \"@webiny/utils/parseJson.js\";\nimport type { WebsocketsRunner } from \"@webiny/api-websockets/exports/api.js\";\nimport type { IAwsWebsocketsIncomingEvent } from \"~/handler/types.js\";\n\nconst getEventBody = (event: IAwsWebsocketsIncomingEvent): WebsocketsRunner.EventData => {\n if (!event.body) {\n return {};\n } else if (typeof event.body === \"object\") {\n return event.body;\n } else if (typeof event.body === \"string\") {\n try {\n return parseJson(event.body);\n } catch (ex) {\n console.log(ex.message);\n return {};\n }\n }\n console.log(\"Unexpected event.body type:\", typeof event.body);\n return {};\n};\n\nconst getToken = (\n body: WebsocketsRunner.EventData,\n event: IAwsWebsocketsIncomingEvent\n): string | null => {\n return body?.token || event.queryStringParameters?.token || null;\n};\n\nconst getTenant = (\n body: WebsocketsRunner.EventData,\n event: IAwsWebsocketsIncomingEvent\n): string => {\n return body?.tenant || event.queryStringParameters?.tenant || \"root\";\n};\n\nexport const getEventValues = (event: IAwsWebsocketsIncomingEvent) => {\n const body = getEventBody(event);\n\n const token = getToken(body, event);\n const tenant = getTenant(body, event);\n return {\n tenant,\n token,\n endpoint: \"manage\"\n };\n};\n"],"names":["getEventBody","event","parseJson","ex","console","getToken","body","getTenant","getEventValues","token","tenant"],"mappings":";AAIA,MAAMA,eAAe,CAACC;IAClB,IAAI,CAACA,MAAM,IAAI,EACX,OAAO,CAAC;SACL,IAAI,AAAsB,YAAtB,OAAOA,MAAM,IAAI,EACxB,OAAOA,MAAM,IAAI;SACd,IAAI,AAAsB,YAAtB,OAAOA,MAAM,IAAI,EACxB,IAAI;QACA,OAAOC,UAAUD,MAAM,IAAI;IAC/B,EAAE,OAAOE,IAAI;QACTC,QAAQ,GAAG,CAACD,GAAG,OAAO;QACtB,OAAO,CAAC;IACZ;IAEJC,QAAQ,GAAG,CAAC,+BAA+B,OAAOH,MAAM,IAAI;IAC5D,OAAO,CAAC;AACZ;AAEA,MAAMI,WAAW,CACbC,MACAL,QAEOK,MAAM,SAASL,MAAM,qBAAqB,EAAE,SAAS;AAGhE,MAAMM,YAAY,CACdD,MACAL,QAEOK,MAAM,UAAUL,MAAM,qBAAqB,EAAE,UAAU;AAG3D,MAAMO,iBAAiB,CAACP;IAC3B,MAAMK,OAAON,aAAaC;IAE1B,MAAMQ,QAAQJ,SAASC,MAAML;IAC7B,MAAMS,SAASH,UAAUD,MAAML;IAC/B,OAAO;QACHS;QACAD;QACA,UAAU;IACd;AACJ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/api-websockets-aws",
|
|
3
|
-
"version": "6.5.2-beta.
|
|
3
|
+
"version": "6.5.2-beta.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -16,29 +16,29 @@
|
|
|
16
16
|
},
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@webiny/api": "6.5.2-beta.
|
|
20
|
-
"@webiny/api-websockets": "6.5.2-beta.
|
|
21
|
-
"@webiny/aws-sdk": "6.5.2-beta.
|
|
22
|
-
"@webiny/error": "6.5.2-beta.
|
|
23
|
-
"@webiny/handler": "6.5.2-beta.
|
|
24
|
-
"@webiny/handler-aws": "6.5.2-beta.
|
|
25
|
-
"@webiny/plugins": "6.5.2-beta.
|
|
26
|
-
"@webiny/utils": "6.5.2-beta.
|
|
19
|
+
"@webiny/api": "6.5.2-beta.2",
|
|
20
|
+
"@webiny/api-websockets": "6.5.2-beta.2",
|
|
21
|
+
"@webiny/aws-sdk": "6.5.2-beta.2",
|
|
22
|
+
"@webiny/error": "6.5.2-beta.2",
|
|
23
|
+
"@webiny/handler": "6.5.2-beta.2",
|
|
24
|
+
"@webiny/handler-aws": "6.5.2-beta.2",
|
|
25
|
+
"@webiny/plugins": "6.5.2-beta.2",
|
|
26
|
+
"@webiny/utils": "6.5.2-beta.2",
|
|
27
27
|
"type-fest": "5.10.0",
|
|
28
28
|
"zod": "4.6.7"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@webiny/api-core": "6.5.2-beta.
|
|
32
|
-
"@webiny/api-headless-cms": "6.5.2-beta.
|
|
33
|
-
"@webiny/build-tools": "6.5.2-beta.
|
|
34
|
-
"@webiny/handler-db": "6.5.2-beta.
|
|
35
|
-
"@webiny/handler-graphql": "6.5.2-beta.
|
|
36
|
-
"@webiny/project-utils": "6.5.2-beta.
|
|
37
|
-
"graphql": "16.
|
|
38
|
-
"rimraf": "6.1.
|
|
31
|
+
"@webiny/api-core": "6.5.2-beta.2",
|
|
32
|
+
"@webiny/api-headless-cms": "6.5.2-beta.2",
|
|
33
|
+
"@webiny/build-tools": "6.5.2-beta.2",
|
|
34
|
+
"@webiny/handler-db": "6.5.2-beta.2",
|
|
35
|
+
"@webiny/handler-graphql": "6.5.2-beta.2",
|
|
36
|
+
"@webiny/project-utils": "6.5.2-beta.2",
|
|
37
|
+
"graphql": "16.15.0",
|
|
38
|
+
"rimraf": "6.1.4",
|
|
39
39
|
"type-fest": "5.10.0",
|
|
40
40
|
"typescript": "7.0.2",
|
|
41
|
-
"vitest": "5.0
|
|
41
|
+
"vitest": "5.1.0"
|
|
42
42
|
},
|
|
43
43
|
"publishConfig": {
|
|
44
44
|
"access": "public"
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { parseJson } from "@webiny/utils/parseJson.js";
|
|
1
2
|
import zod from "zod";
|
|
2
3
|
import { WebsocketsEventRequestContextEventType } from "../handler/types.js";
|
|
3
4
|
import { createZodError } from "@webiny/utils";
|
|
@@ -28,7 +29,7 @@ const validation = zod.object({
|
|
|
28
29
|
body: zod.string().transform((value, context)=>{
|
|
29
30
|
if (!value) return;
|
|
30
31
|
try {
|
|
31
|
-
return
|
|
32
|
+
return parseJson(value);
|
|
32
33
|
} catch (ex) {
|
|
33
34
|
console.error(`Failed body validation: ${ex.message}`);
|
|
34
35
|
console.log(`Body: ${value}`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator/AwsWebsocketsEventValidator.js","sources":["../../src/validator/AwsWebsocketsEventValidator.ts"],"sourcesContent":["import zod from \"zod\";\nimport { WebsocketsEventValidator, WebsocketsRunner } from \"@webiny/api-websockets/exports/api.js\";\nimport { WebsocketsEventRequestContextEventType } from \"~/handler/types.js\";\nimport { createZodError } from \"@webiny/utils\";\n\nconst eventTypeMap: Record<WebsocketsEventRequestContextEventType, WebsocketsRunner.EventType> = {\n [WebsocketsEventRequestContextEventType.message]: \"message\",\n [WebsocketsEventRequestContextEventType.connect]: \"connect\",\n [WebsocketsEventRequestContextEventType.disconnect]: \"disconnect\"\n};\n\nconst routeKeyMap: Record<string, string> = {\n $connect: \"connect\",\n $disconnect: \"disconnect\",\n $default: \"default\"\n};\n\nconst validation = zod\n .object({\n headers: zod.looseObject({}).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<WebsocketsRunner.EventData>((value, context) => {\n if (!value) {\n return undefined;\n }\n try {\n return
|
|
1
|
+
{"version":3,"file":"validator/AwsWebsocketsEventValidator.js","sources":["../../src/validator/AwsWebsocketsEventValidator.ts"],"sourcesContent":["import { parseJson } from \"@webiny/utils/parseJson.js\";\nimport zod from \"zod\";\nimport { WebsocketsEventValidator, WebsocketsRunner } from \"@webiny/api-websockets/exports/api.js\";\nimport { WebsocketsEventRequestContextEventType } from \"~/handler/types.js\";\nimport { createZodError } from \"@webiny/utils\";\n\nconst eventTypeMap: Record<WebsocketsEventRequestContextEventType, WebsocketsRunner.EventType> = {\n [WebsocketsEventRequestContextEventType.message]: \"message\",\n [WebsocketsEventRequestContextEventType.connect]: \"connect\",\n [WebsocketsEventRequestContextEventType.disconnect]: \"disconnect\"\n};\n\nconst routeKeyMap: Record<string, string> = {\n $connect: \"connect\",\n $disconnect: \"disconnect\",\n $default: \"default\"\n};\n\nconst validation = zod\n .object({\n headers: zod.looseObject({}).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<WebsocketsRunner.EventData>((value, context) => {\n if (!value) {\n return undefined;\n }\n try {\n return parseJson(value);\n } catch (ex) {\n console.error(`Failed body validation: ${ex.message}`);\n console.log(`Body: ${value}`);\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 .looseObject({\n token: zod.string(),\n tenant: zod.string(),\n messageId: zod.string().nullish(),\n action: zod.string(),\n data: zod.looseObject({}).nullish()\n })\n .optional();\n\nexport class AwsWebsocketsEventValidator implements WebsocketsEventValidator.Interface {\n public async validate<T extends WebsocketsRunner.EventData = WebsocketsRunner.EventData>(\n input: unknown\n ): Promise<WebsocketsRunner.Event<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\n const rc = result.data.requestContext;\n\n return {\n headers: result.data.headers as Record<string, string>,\n context: {\n connectionId: rc.connectionId,\n connectedAt: rc.connectedAt,\n host: rc.domainName,\n eventType: eventTypeMap[rc.eventType as WebsocketsEventRequestContextEventType],\n route: routeKeyMap[rc.routeKey] || rc.routeKey,\n endpoint: `https://${rc.domainName}/${rc.stage}`\n },\n body: {\n ...((bodyResult.data || {}) as T)\n }\n };\n }\n}\n"],"names":["eventTypeMap","WebsocketsEventRequestContextEventType","routeKeyMap","validation","zod","value","context","parseJson","ex","console","output","bodyValidation","AwsWebsocketsEventValidator","input","result","createZodError","bodyResult","rc"],"mappings":";;;;AAMA,MAAMA,eAA2F;IAC7F,CAACC,uCAAuC,OAAO,CAAC,EAAE;IAClD,CAACA,uCAAuC,OAAO,CAAC,EAAE;IAClD,CAACA,uCAAuC,UAAU,CAAC,EAAE;AACzD;AAEA,MAAMC,cAAsC;IACxC,UAAU;IACV,aAAa;IACb,UAAU;AACd;AAEA,MAAMC,aAAaC,IAAAA,MACR,CAAC;IACJ,SAASA,IAAI,WAAW,CAAC,CAAC,GAAG,QAAQ;IACrC,gBAAgBA,IAAI,MAAM,CAAC;QACvB,cAAcA,IAAI,MAAM;QACxB,OAAOA,IAAI,MAAM;QACjB,aAAaA,IAAI,MAAM;QACvB,YAAYA,IAAI,MAAM;QACtB,WAAWA,GAAG,CAAHA,OAAQ,CAAC;YAChBH,uCAAuC,OAAO;YAC9CA,uCAAuC,OAAO;YAC9CA,uCAAuC,UAAU;SACpD;QACD,UAAUG,IAAI,MAAM;IACxB;IACA,MAAMA,IAAAA,MACK,GACN,SAAS,CAA6B,CAACC,OAAOC;QAC3C,IAAI,CAACD,OACD;QAEJ,IAAI;YACA,OAAOE,UAAUF;QACrB,EAAE,OAAOG,IAAI;YACTC,QAAQ,KAAK,CAAC,CAAC,wBAAwB,EAAED,GAAG,OAAO,EAAE;YACrDC,QAAQ,GAAG,CAAC,CAAC,MAAM,EAAEJ,OAAO;YAC5BC,QAAQ,QAAQ,CAAC;gBACb,MAAM,EAAE;gBACR,SAAS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