@webiny/api-websockets-aws 0.0.0-unstable.b6d7105cee → 0.0.0-unstable.e1999482d0

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,22 @@
1
+ import type { Container } from "@webiny/feature/api";
2
+ import { WebSocketInputHandler } from "@webiny/api-kernel-aws/abstractions/handlers/WebSocketInputHandler.js";
3
+ import type { IWebSocketEvent } from "@webiny/api-kernel-aws/inputTypes/WebSocketInputType.js";
4
+ import type { IAuthenticationContext } from "@webiny/api-core/features/security/authentication/AuthenticationContext/abstractions.js";
5
+ import type { IIdentityContext } from "@webiny/api-core/features/security/IdentityContext/abstractions.js";
6
+ import type { ITenantContext } from "@webiny/api-core/features/tenancy/TenantContext/abstractions.js";
7
+ import type { IGetTenantByIdUseCase } from "@webiny/api-core/features/tenancy/GetTenantById/abstractions.js";
8
+ import type { InputContext, NextFunction } from "@webiny/api-kernel-core";
9
+ import type { APIGatewayProxyResult } from "@webiny/aws-sdk/types/index.js";
10
+ declare class WebSocketLambdaHandlerImpl implements WebSocketInputHandler.Interface {
11
+ private container;
12
+ private authCtx;
13
+ private identityCtx;
14
+ private tenantCtx;
15
+ private getTenantById;
16
+ constructor(container: Container, authCtx: IAuthenticationContext, identityCtx: IIdentityContext, tenantCtx: ITenantContext, getTenantById: IGetTenantByIdUseCase);
17
+ execute(inputCtx: InputContext<IWebSocketEvent>, _next: NextFunction): Promise<APIGatewayProxyResult>;
18
+ }
19
+ export declare const WebSocketLambdaHandler: typeof WebSocketLambdaHandlerImpl & {
20
+ __abstraction: import("@webiny/di").Abstraction<import("@webiny/api-kernel-aws/abstractions/handlers/WebSocketInputHandler.js").IWebSocketInputHandler>;
21
+ };
22
+ export {};
@@ -0,0 +1,90 @@
1
+ import { parseJson } from "@webiny/utils";
2
+ import { WebSocketInputHandler } from "@webiny/api-kernel-aws/abstractions/handlers/WebSocketInputHandler.js";
3
+ import { RequestContainer } from "@webiny/api-kernel-core";
4
+ import { AuthenticationContext } from "@webiny/api-core/features/security/authentication/AuthenticationContext/index.js";
5
+ import { IdentityContext } from "@webiny/api-core/features/security/IdentityContext/index.js";
6
+ import { TenantContext } from "@webiny/api-core/features/tenancy/TenantContext/index.js";
7
+ import { GetTenantByIdUseCase } from "@webiny/api-core/features/tenancy/GetTenantById/index.js";
8
+ import { WebsocketsRunner } from "@webiny/api-websockets/runner/index.js";
9
+ import { WebsocketsResponse } from "@webiny/api-websockets/response/index.js";
10
+ import { getEventValues } from "@webiny/api-websockets/handler/headers.js";
11
+ import { WebsocketsEventRequestContextEventType, WebsocketsEventRoute } from "./handler/types.js";
12
+ const getBody = (body)=>{
13
+ if ("string" == typeof body) try {
14
+ return parseJson(body);
15
+ } catch {
16
+ return {};
17
+ }
18
+ return body;
19
+ };
20
+ const toWebsocketsEvent = (raw, endpoint)=>{
21
+ const rc = raw.requestContext ?? {};
22
+ const eventTypeMap = {
23
+ [WebsocketsEventRequestContextEventType.message]: "message",
24
+ [WebsocketsEventRequestContextEventType.connect]: "connect",
25
+ [WebsocketsEventRequestContextEventType.disconnect]: "disconnect"
26
+ };
27
+ const routeMap = {
28
+ [WebsocketsEventRoute.connect]: "connect",
29
+ [WebsocketsEventRoute.disconnect]: "disconnect",
30
+ [WebsocketsEventRoute["default"]]: "default"
31
+ };
32
+ const routeKey = rc.routeKey;
33
+ const route = routeMap[routeKey ?? ""] ?? "default";
34
+ return {
35
+ headers: raw.headers,
36
+ context: {
37
+ connectionId: rc.connectionId ?? "",
38
+ connectedAt: rc.connectedAt ?? 0,
39
+ host: rc.domainName ?? "",
40
+ eventType: eventTypeMap[rc.eventType ?? ""] ?? "message",
41
+ route,
42
+ endpoint
43
+ },
44
+ body: getBody(raw.body)
45
+ };
46
+ };
47
+ class WebSocketLambdaHandlerImpl {
48
+ constructor(container, authCtx, identityCtx, tenantCtx, getTenantById){
49
+ this.container = container;
50
+ this.authCtx = authCtx;
51
+ this.identityCtx = identityCtx;
52
+ this.tenantCtx = tenantCtx;
53
+ this.getTenantById = getTenantById;
54
+ }
55
+ async execute(inputCtx, _next) {
56
+ const ctx = {
57
+ container: this.container
58
+ };
59
+ const raw = inputCtx.input;
60
+ const { token, tenant, endpoint } = getEventValues(raw);
61
+ const tenantResult = await this.getTenantById.execute(tenant);
62
+ if (tenantResult.isOk()) this.tenantCtx.setTenant(tenantResult.value);
63
+ const identity = await this.authCtx.authenticate(token ?? "");
64
+ this.identityCtx.setIdentity(identity);
65
+ const response = this.container.resolve(WebsocketsResponse);
66
+ const runner = new WebsocketsRunner(ctx, response);
67
+ const event = toWebsocketsEvent(raw, endpoint);
68
+ const result = await runner.run(event);
69
+ return {
70
+ statusCode: result.statusCode,
71
+ headers: {
72
+ "sec-websocket-protocol": "webiny-ws-v1"
73
+ },
74
+ body: ""
75
+ };
76
+ }
77
+ }
78
+ const WebSocketLambdaHandler = WebSocketInputHandler.createImplementation({
79
+ implementation: WebSocketLambdaHandlerImpl,
80
+ dependencies: [
81
+ RequestContainer,
82
+ AuthenticationContext,
83
+ IdentityContext,
84
+ TenantContext,
85
+ GetTenantByIdUseCase
86
+ ]
87
+ });
88
+ export { WebSocketLambdaHandler };
89
+
90
+ //# sourceMappingURL=WebSocketLambdaHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WebSocketLambdaHandler.js","sources":["../src/WebSocketLambdaHandler.ts"],"sourcesContent":["import type { Container } from \"@webiny/feature/api\";\nimport { parseJson } from \"@webiny/utils\";\nimport { WebSocketInputHandler } from \"@webiny/api-kernel-aws/abstractions/handlers/WebSocketInputHandler.js\";\nimport type { IWebSocketEvent } from \"@webiny/api-kernel-aws/inputTypes/WebSocketInputType.js\";\nimport { RequestContainer } from \"@webiny/api-kernel-core\";\nimport { AuthenticationContext } from \"@webiny/api-core/features/security/authentication/AuthenticationContext/index.js\";\nimport { IdentityContext } from \"@webiny/api-core/features/security/IdentityContext/index.js\";\nimport { TenantContext } from \"@webiny/api-core/features/tenancy/TenantContext/index.js\";\nimport { GetTenantByIdUseCase } from \"@webiny/api-core/features/tenancy/GetTenantById/index.js\";\nimport type { IAuthenticationContext } from \"@webiny/api-core/features/security/authentication/AuthenticationContext/abstractions.js\";\nimport type { IIdentityContext } from \"@webiny/api-core/features/security/IdentityContext/abstractions.js\";\nimport type { ITenantContext } from \"@webiny/api-core/features/tenancy/TenantContext/abstractions.js\";\nimport type { IGetTenantByIdUseCase } from \"@webiny/api-core/features/tenancy/GetTenantById/abstractions.js\";\nimport type { InputContext, NextFunction } from \"@webiny/api-kernel-core\";\nimport type { APIGatewayProxyResult } from \"@webiny/aws-sdk/types/index.js\";\nimport { WebsocketsRunner } from \"@webiny/api-websockets/runner/index.js\";\nimport { WebsocketsResponse } from \"@webiny/api-websockets/response/index.js\";\nimport type {\n Context,\n IWebsocketsEvent,\n WebsocketsEventType,\n WebsocketsRoute\n} from \"@webiny/api-websockets/types.js\";\nimport { getEventValues } from \"@webiny/api-websockets/handler/headers.js\";\nimport type { IWebsocketsIncomingEvent } from \"@webiny/api-websockets/handler/types.js\";\nimport { WebsocketsEventRequestContextEventType, WebsocketsEventRoute } from \"~/handler/types.js\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\n\nconst getBody = (body: GenericRecord | string | undefined) => {\n if (typeof body === \"string\") {\n try {\n return parseJson(body) as GenericRecord;\n } catch {\n return {};\n }\n }\n return body;\n};\n\nconst toWebsocketsEvent = (raw: IWebsocketsIncomingEvent, endpoint: string): IWebsocketsEvent => {\n const rc = raw.requestContext ?? {};\n const eventTypeMap: Record<string, WebsocketsEventType> = {\n [WebsocketsEventRequestContextEventType.message]: \"message\",\n [WebsocketsEventRequestContextEventType.connect]: \"connect\",\n [WebsocketsEventRequestContextEventType.disconnect]: \"disconnect\"\n };\n // AWS delivers routeKey as \"$connect\"/\"$disconnect\"/\"$default\"; route handlers register under the\n // un-prefixed WebsocketsRoute (\"connect\"/\"disconnect\"/\"default\"). Without this map the runner\n // matches the raw \"$connect\" and finds no handler (\"There are no handlers for the route: $connect\").\n const routeMap: Record<string, WebsocketsRoute> = {\n [WebsocketsEventRoute.connect]: \"connect\",\n [WebsocketsEventRoute.disconnect]: \"disconnect\",\n [WebsocketsEventRoute.default]: \"default\"\n };\n const routeKey = rc.routeKey as string | undefined;\n const route = routeMap[routeKey ?? \"\"] ?? \"default\";\n\n return {\n headers: raw.headers as Record<string, string> | undefined,\n context: {\n connectionId: rc.connectionId ?? \"\",\n connectedAt: rc.connectedAt ?? 0,\n host: rc.domainName ?? \"\",\n eventType: eventTypeMap[rc.eventType ?? \"\"] ?? \"message\",\n route,\n endpoint\n },\n body: getBody(raw.body)\n };\n};\n\nclass WebSocketLambdaHandlerImpl implements WebSocketInputHandler.Interface {\n constructor(\n private container: Container,\n private authCtx: IAuthenticationContext,\n private identityCtx: IIdentityContext,\n private tenantCtx: ITenantContext,\n private getTenantById: IGetTenantByIdUseCase\n ) {}\n\n async execute(\n inputCtx: InputContext<IWebSocketEvent>,\n _next: NextFunction\n ): Promise<APIGatewayProxyResult> {\n // Route handlers and all websockets services are resolved from the DI container;\n // the runner only needs `container` on the context object.\n const ctx: Record<string, any> = { container: this.container };\n\n const raw = inputCtx.input as IWebsocketsIncomingEvent;\n const { token, tenant, endpoint } = getEventValues(raw);\n\n // The tenant first: an API key is looked up in the current tenant.\n const tenantResult = await this.getTenantById.execute(tenant);\n if (tenantResult.isOk()) {\n this.tenantCtx.setTenant(tenantResult.value);\n }\n\n const identity = await this.authCtx.authenticate(token ?? \"\");\n this.identityCtx.setIdentity(identity);\n\n const response = this.container.resolve(WebsocketsResponse);\n const runner = new WebsocketsRunner(ctx as Context, response);\n const event = toWebsocketsEvent(raw, endpoint);\n const result = await runner.run(event);\n\n return {\n statusCode: result.statusCode,\n headers: {\n \"sec-websocket-protocol\": \"webiny-ws-v1\"\n },\n body: \"\"\n };\n }\n}\n\nexport const WebSocketLambdaHandler = WebSocketInputHandler.createImplementation({\n implementation: WebSocketLambdaHandlerImpl,\n dependencies: [\n RequestContainer,\n AuthenticationContext,\n IdentityContext,\n TenantContext,\n GetTenantByIdUseCase\n ]\n});\n"],"names":["getBody","body","parseJson","toWebsocketsEvent","raw","endpoint","rc","eventTypeMap","WebsocketsEventRequestContextEventType","routeMap","WebsocketsEventRoute","routeKey","route","WebSocketLambdaHandlerImpl","container","authCtx","identityCtx","tenantCtx","getTenantById","inputCtx","_next","ctx","token","tenant","getEventValues","tenantResult","identity","response","WebsocketsResponse","runner","WebsocketsRunner","event","result","WebSocketLambdaHandler","WebSocketInputHandler","RequestContainer","AuthenticationContext","IdentityContext","TenantContext","GetTenantByIdUseCase"],"mappings":";;;;;;;;;;;AA4BA,MAAMA,UAAU,CAACC;IACb,IAAI,AAAgB,YAAhB,OAAOA,MACP,IAAI;QACA,OAAOC,UAAUD;IACrB,EAAE,OAAM;QACJ,OAAO,CAAC;IACZ;IAEJ,OAAOA;AACX;AAEA,MAAME,oBAAoB,CAACC,KAA+BC;IACtD,MAAMC,KAAKF,IAAI,cAAc,IAAI,CAAC;IAClC,MAAMG,eAAoD;QACtD,CAACC,uCAAuC,OAAO,CAAC,EAAE;QAClD,CAACA,uCAAuC,OAAO,CAAC,EAAE;QAClD,CAACA,uCAAuC,UAAU,CAAC,EAAE;IACzD;IAIA,MAAMC,WAA4C;QAC9C,CAACC,qBAAqB,OAAO,CAAC,EAAE;QAChC,CAACA,qBAAqB,UAAU,CAAC,EAAE;QACnC,CAACA,oBAAoB,CAApBA,UAA4B,CAAC,EAAE;IACpC;IACA,MAAMC,WAAWL,GAAG,QAAQ;IAC5B,MAAMM,QAAQH,QAAQ,CAACE,YAAY,GAAG,IAAI;IAE1C,OAAO;QACH,SAASP,IAAI,OAAO;QACpB,SAAS;YACL,cAAcE,GAAG,YAAY,IAAI;YACjC,aAAaA,GAAG,WAAW,IAAI;YAC/B,MAAMA,GAAG,UAAU,IAAI;YACvB,WAAWC,YAAY,CAACD,GAAG,SAAS,IAAI,GAAG,IAAI;YAC/CM;YACAP;QACJ;QACA,MAAML,QAAQI,IAAI,IAAI;IAC1B;AACJ;AAEA,MAAMS;IACF,YACYC,SAAoB,EACpBC,OAA+B,EAC/BC,WAA6B,EAC7BC,SAAyB,EACzBC,aAAoC,CAC9C;aALUJ,SAAS,GAATA;aACAC,OAAO,GAAPA;aACAC,WAAW,GAAXA;aACAC,SAAS,GAATA;aACAC,aAAa,GAAbA;IACT;IAEH,MAAM,QACFC,QAAuC,EACvCC,KAAmB,EACW;QAG9B,MAAMC,MAA2B;YAAE,WAAW,IAAI,CAAC,SAAS;QAAC;QAE7D,MAAMjB,MAAMe,SAAS,KAAK;QAC1B,MAAM,EAAEG,KAAK,EAAEC,MAAM,EAAElB,QAAQ,EAAE,GAAGmB,eAAepB;QAGnD,MAAMqB,eAAe,MAAM,IAAI,CAAC,aAAa,CAAC,OAAO,CAACF;QACtD,IAAIE,aAAa,IAAI,IACjB,IAAI,CAAC,SAAS,CAAC,SAAS,CAACA,aAAa,KAAK;QAG/C,MAAMC,WAAW,MAAM,IAAI,CAAC,OAAO,CAAC,YAAY,CAACJ,SAAS;QAC1D,IAAI,CAAC,WAAW,CAAC,WAAW,CAACI;QAE7B,MAAMC,WAAW,IAAI,CAAC,SAAS,CAAC,OAAO,CAACC;QACxC,MAAMC,SAAS,IAAIC,iBAAiBT,KAAgBM;QACpD,MAAMI,QAAQ5B,kBAAkBC,KAAKC;QACrC,MAAM2B,SAAS,MAAMH,OAAO,GAAG,CAACE;QAEhC,OAAO;YACH,YAAYC,OAAO,UAAU;YAC7B,SAAS;gBACL,0BAA0B;YAC9B;YACA,MAAM;QACV;IACJ;AACJ;AAEO,MAAMC,yBAAyBC,sBAAsB,oBAAoB,CAAC;IAC7E,gBAAgBrB;IAChB,cAAc;QACVsB;QACAC;QACAC;QACAC;QACAC;KACH;AACL"}
@@ -0,0 +1,16 @@
1
+ import { ConnectionRegistry } from "@webiny/api-websockets/features/ConnectionRegistry/abstractions.js";
2
+ import type { DynamoDBDocument } from "@webiny/aws-sdk/client-dynamodb/index.js";
3
+ export declare class WebsocketsConnectionRegistry implements ConnectionRegistry.Interface {
4
+ private readonly entity;
5
+ constructor(documentClient: DynamoDBDocument);
6
+ register(params: ConnectionRegistry.RegisterParams): Promise<ConnectionRegistry.Data>;
7
+ unregister(params: ConnectionRegistry.UnregisterParams): Promise<void>;
8
+ private getViaConnection;
9
+ listViaConnections(connections: string[]): Promise<ConnectionRegistry.Data[]>;
10
+ listViaIdentity(identity: string): Promise<ConnectionRegistry.Data[]>;
11
+ listViaTenant(tenant: string): Promise<ConnectionRegistry.Data[]>;
12
+ listAll(): Promise<ConnectionRegistry.Data[]>;
13
+ updateLastSeen(_connectionId: string): Promise<void>;
14
+ listStale(_olderThan: Date): Promise<ConnectionRegistry.Data[]>;
15
+ private store;
16
+ }
@@ -0,0 +1,121 @@
1
+ import error from "@webiny/error";
2
+ import { createEntity } from "./entity.js";
3
+ const PK = "WS#CONNECTIONS";
4
+ const GSI1_PK = "WS#CONNECTIONS#IDENTITY";
5
+ const GSI2_PK = "WS#CONNECTIONS#TENANT";
6
+ class WebsocketsConnectionRegistry {
7
+ constructor(documentClient){
8
+ this.entity = createEntity(documentClient);
9
+ }
10
+ async register(params) {
11
+ const { connectionId, tenant, identity, endpoint, connectedOn } = params;
12
+ const data = {
13
+ connectionId,
14
+ identity,
15
+ tenant,
16
+ endpoint,
17
+ connectedOn
18
+ };
19
+ await this.store(data);
20
+ return data;
21
+ }
22
+ async unregister(params) {
23
+ const { connectionId } = params;
24
+ const keys = {
25
+ PK,
26
+ SK: connectionId
27
+ };
28
+ const original = await this.getViaConnection(connectionId);
29
+ if (!original) {
30
+ const message = `There is no connection with ID "${connectionId}".`;
31
+ console.error(message);
32
+ throw new error(message, "CONNECTION_NOT_FOUND", keys);
33
+ }
34
+ try {
35
+ await this.entity.delete(keys);
36
+ } catch (ex) {
37
+ console.error(`Could not remove connection from the database: ${original.connectionId}`);
38
+ throw new error(ex.message, ex.code, keys);
39
+ }
40
+ }
41
+ async getViaConnection(connectionId) {
42
+ const item = await this.entity.get({
43
+ PK,
44
+ SK: connectionId
45
+ });
46
+ if (!item) return null;
47
+ return item?.data || null;
48
+ }
49
+ async listViaConnections(connections) {
50
+ const reader = this.entity.createEntityReader({
51
+ read: connections.map((id)=>({
52
+ PK,
53
+ SK: id
54
+ }))
55
+ });
56
+ const results = await reader.execute();
57
+ return results.map((item)=>item.data);
58
+ }
59
+ async listViaIdentity(identity) {
60
+ const items = await this.entity.queryAll({
61
+ partitionKey: GSI1_PK,
62
+ options: {
63
+ index: "GSI1",
64
+ eq: identity
65
+ }
66
+ });
67
+ return items.map((item)=>item.data);
68
+ }
69
+ async listViaTenant(tenant) {
70
+ const options = {
71
+ eq: `T#${tenant}`
72
+ };
73
+ const items = await this.entity.queryAll({
74
+ partitionKey: GSI2_PK,
75
+ options: {
76
+ ...options,
77
+ index: "GSI2"
78
+ }
79
+ });
80
+ return items.map((item)=>item.data);
81
+ }
82
+ async listAll() {
83
+ const items = await this.entity.queryAll({
84
+ partitionKey: PK,
85
+ options: {
86
+ gte: " "
87
+ }
88
+ });
89
+ return items.map((item)=>item.data);
90
+ }
91
+ async updateLastSeen(_connectionId) {}
92
+ async listStale(_olderThan) {
93
+ return [];
94
+ }
95
+ async store(data) {
96
+ const { connectionId, tenant, identity } = data;
97
+ const item = {
98
+ PK,
99
+ SK: connectionId,
100
+ GSI1_PK,
101
+ GSI1_SK: identity.id,
102
+ GSI2_PK,
103
+ GSI2_SK: `T#${tenant}`,
104
+ GSI_TENANT: tenant,
105
+ TYPE: "ws.connection",
106
+ data
107
+ };
108
+ try {
109
+ return await this.entity.put(item);
110
+ } catch (err) {
111
+ throw error.from(err, {
112
+ message: "Could not store websockets connection data.",
113
+ code: "STORE_WEBSOCKETS_CONNECTION_DATA_ERROR",
114
+ data: item
115
+ });
116
+ }
117
+ }
118
+ }
119
+ export { WebsocketsConnectionRegistry };
120
+
121
+ //# sourceMappingURL=WebsocketsConnectionRegistry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ddb/WebsocketsConnectionRegistry.js","sources":["../../src/ddb/WebsocketsConnectionRegistry.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport { ConnectionRegistry } from \"@webiny/api-websockets/features/ConnectionRegistry/abstractions.js\";\nimport { createEntity } from \"./entity.js\";\nimport type { DynamoDBDocument } from \"@webiny/aws-sdk/client-dynamodb/index.js\";\nimport type { EntityQueryOptions } from \"@webiny/db-dynamodb/toolbox.js\";\n\nconst PK = `WS#CONNECTIONS`;\nconst GSI1_PK = \"WS#CONNECTIONS#IDENTITY\";\nconst GSI2_PK = \"WS#CONNECTIONS#TENANT\";\n\nexport class WebsocketsConnectionRegistry implements ConnectionRegistry.Interface {\n private readonly entity;\n\n public constructor(documentClient: DynamoDBDocument) {\n this.entity = createEntity(documentClient);\n }\n\n public async register(\n params: ConnectionRegistry.RegisterParams\n ): Promise<ConnectionRegistry.Data> {\n const { connectionId, tenant, identity, endpoint, connectedOn } = params;\n\n const data: ConnectionRegistry.Data = {\n connectionId,\n identity,\n tenant,\n endpoint,\n connectedOn\n };\n await this.store(data);\n return data;\n }\n\n public async unregister(params: ConnectionRegistry.UnregisterParams): 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 this.entity.delete(keys);\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(connectionId: string): Promise<ConnectionRegistry.Data | null> {\n const item = await this.entity.get({\n PK,\n SK: connectionId\n });\n if (!item) {\n return null;\n }\n return item?.data || null;\n }\n\n public async listViaConnections(connections: string[]): Promise<ConnectionRegistry.Data[]> {\n const reader = this.entity.createEntityReader({\n read: connections.map(id => {\n return {\n PK,\n SK: id\n };\n })\n });\n\n const results = await reader.execute();\n\n return results.map(item => {\n return item.data;\n });\n }\n\n public async listViaIdentity(identity: string): Promise<ConnectionRegistry.Data[]> {\n const items = await this.entity.queryAll({\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 public async listViaTenant(tenant: string): Promise<ConnectionRegistry.Data[]> {\n const options: Partial<EntityQueryOptions> = {\n eq: `T#${tenant}`\n };\n\n const items = await this.entity.queryAll({\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<ConnectionRegistry.Data[]> {\n const items = await this.entity.queryAll({\n partitionKey: PK,\n options: {\n gte: \" \"\n }\n });\n return items.map(item => {\n return item.data;\n });\n }\n\n /* No-op: heartbeat-based tracking is server-only; DDB registry does not track lastSeen. */\n public async updateLastSeen(_connectionId: string): Promise<void> {\n return;\n }\n\n /* No-op: stale connection cleanup is server-only; DDB registry always returns empty. */\n public async listStale(_olderThan: Date): Promise<ConnectionRegistry.Data[]> {\n return [];\n }\n\n private async store(data: ConnectionRegistry.Data) {\n const { connectionId, tenant, identity } = data;\n const item = {\n PK,\n SK: connectionId,\n GSI1_PK,\n GSI1_SK: identity.id,\n GSI2_PK,\n GSI2_SK: `T#${tenant}`,\n GSI_TENANT: tenant,\n TYPE: \"ws.connection\",\n data\n };\n try {\n return await this.entity.put(item);\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"],"names":["PK","GSI1_PK","GSI2_PK","WebsocketsConnectionRegistry","documentClient","createEntity","params","connectionId","tenant","identity","endpoint","connectedOn","data","keys","original","message","console","WebinyError","ex","item","connections","reader","id","results","items","options","_connectionId","_olderThan","err"],"mappings":";;AAMA,MAAMA,KAAK;AACX,M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AAM,CAAC,GAAG,CAACO;QACjC,EAAE,OAAOS,KAAK;YACV,MAAMX,MAAAA,IAAgB,CAACW,KAAK;gBACxB,SAAS;gBACT,MAAM;gBACN,MAAMT;YACV;QACJ;IACJ;AACJ"}
@@ -0,0 +1,5 @@
1
+ import { type Container } from "@webiny/feature/api";
2
+ export declare const WebsocketsDdbFeature: {
3
+ name: string;
4
+ register(container: Container): void;
5
+ };
@@ -0,0 +1,16 @@
1
+ import { createFeature } from "@webiny/feature/api";
2
+ import { DynamoDBClient } from "@webiny/db-dynamodb";
3
+ import { ConnectionRegistry } from "@webiny/api-websockets/features/ConnectionRegistry/abstractions.js";
4
+ import { WebsocketsConnectionRegistry } from "./WebsocketsConnectionRegistry.js";
5
+ const WebsocketsDdbFeature = createFeature({
6
+ name: "WebsocketsDdb",
7
+ register (container) {
8
+ container.registerFactory(ConnectionRegistry, ()=>{
9
+ const db = container.resolve(DynamoDBClient);
10
+ return new WebsocketsConnectionRegistry(db.client);
11
+ });
12
+ }
13
+ });
14
+ export { WebsocketsDdbFeature };
15
+
16
+ //# sourceMappingURL=WebsocketsDdbFeature.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ddb/WebsocketsDdbFeature.js","sources":["../../src/ddb/WebsocketsDdbFeature.ts"],"sourcesContent":["import { type Container, createFeature } from \"@webiny/feature/api\";\nimport { DynamoDBClient } from \"@webiny/db-dynamodb\";\nimport { ConnectionRegistry } from \"@webiny/api-websockets/features/ConnectionRegistry/abstractions.js\";\nimport { WebsocketsConnectionRegistry } from \"./WebsocketsConnectionRegistry.js\";\n\nexport const WebsocketsDdbFeature = createFeature({\n name: \"WebsocketsDdb\",\n register(container: Container) {\n container.registerFactory(ConnectionRegistry, () => {\n const db = container.resolve(DynamoDBClient);\n return new WebsocketsConnectionRegistry(db.client);\n });\n }\n});\n"],"names":["WebsocketsDdbFeature","createFeature","container","ConnectionRegistry","db","DynamoDBClient","WebsocketsConnectionRegistry"],"mappings":";;;;AAKO,MAAMA,uBAAuBC,cAAc;IAC9C,MAAM;IACN,UAASC,SAAoB;QACzBA,UAAU,eAAe,CAACC,oBAAoB;YAC1C,MAAMC,KAAKF,UAAU,OAAO,CAACG;YAC7B,OAAO,IAAIC,6BAA6BF,GAAG,MAAM;QACrD;IACJ;AACJ"}
@@ -0,0 +1,2 @@
1
+ import type { DynamoDBDocument } from "@webiny/aws-sdk/client-dynamodb/index.js";
2
+ export declare const createEntity: (documentClient: DynamoDBDocument) => import("@webiny/db-dynamodb").IEntity<import("@webiny/db-dynamodb").IStandardEntityAttributes<import("@webiny/api-websockets/features/ConnectionRegistry/abstractions.js").IWebsocketsConnectionRegistryData>>;
package/ddb/entity.js ADDED
@@ -0,0 +1,48 @@
1
+ import { createStandardEntity, createTable } from "@webiny/db-dynamodb";
2
+ const entity_name = "SocketsConnectionRegistry";
3
+ const createEntity = (documentClient)=>{
4
+ const table = createTable({
5
+ name: String(process.env.DB_TABLE),
6
+ documentClient
7
+ });
8
+ return createStandardEntity({
9
+ name: entity_name,
10
+ table: table.table,
11
+ attributes: {
12
+ PK: {
13
+ partitionKey: true
14
+ },
15
+ SK: {
16
+ sortKey: true
17
+ },
18
+ GSI1_PK: {
19
+ type: "string",
20
+ required: true
21
+ },
22
+ GSI1_SK: {
23
+ type: "string",
24
+ required: true
25
+ },
26
+ GSI2_PK: {
27
+ type: "string",
28
+ required: true
29
+ },
30
+ GSI2_SK: {
31
+ type: "string",
32
+ required: true
33
+ },
34
+ TYPE: {
35
+ type: "string",
36
+ default: entity_name,
37
+ required: true
38
+ },
39
+ data: {
40
+ type: "map",
41
+ required: true
42
+ }
43
+ }
44
+ });
45
+ };
46
+ export { createEntity };
47
+
48
+ //# sourceMappingURL=entity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ddb/entity.js","sources":["../../src/ddb/entity.ts"],"sourcesContent":["import type { DynamoDBDocument } from \"@webiny/aws-sdk/client-dynamodb/index.js\";\nimport { createStandardEntity, createTable } from \"@webiny/db-dynamodb\";\nimport { ConnectionRegistry } from \"@webiny/api-websockets/features/ConnectionRegistry/abstractions.js\";\n\nconst name = \"SocketsConnectionRegistry\";\n\nexport const createEntity = (documentClient: DynamoDBDocument) => {\n const table = createTable({\n name: String(process.env.DB_TABLE),\n documentClient\n });\n\n return createStandardEntity<ConnectionRegistry.Data>({\n name,\n table: table.table,\n attributes: {\n PK: {\n partitionKey: true\n },\n SK: {\n sortKey: true\n },\n GSI1_PK: {\n type: \"string\",\n required: true\n },\n GSI1_SK: {\n type: \"string\",\n required: true\n },\n GSI2_PK: {\n type: \"string\",\n required: true\n },\n GSI2_SK: {\n type: \"string\",\n required: true\n },\n TYPE: {\n type: \"string\",\n default: name,\n required: true\n },\n data: {\n type: \"map\",\n required: true\n }\n }\n });\n};\n"],"names":["name","createEntity","documentClient","table","createTable","String","process","createStandardEntity"],"mappings":";AAIA,MAAMA,cAAO;AAEN,MAAMC,eAAe,CAACC;IACzB,MAAMC,QAAQC,YAAY;QACtB,MAAMC,OAAOC,QAAQ,GAAG,CAAC,QAAQ;QACjCJ;IACJ;IAEA,OAAOK,qBAA8C;QACjDP,MAAIA;QACJ,OAAOG,MAAM,KAAK;QAClB,YAAY;YACR,IAAI;gBACA,cAAc;YAClB;YACA,IAAI;gBACA,SAAS;YACb;YACA,SAAS;gBACL,MAAM;gBACN,UAAU;YACd;YACA,SAAS;gBACL,MAAM;gBACN,UAAU;YACd;YACA,SAAS;gBACL,MAAM;gBACN,UAAU;YACd;YACA,SAAS;gBACL,MAAM;gBACN,UAAU;YACd;YACA,MAAM;gBACF,MAAM;gBACN,SAASH;gBACT,UAAU;YACd;YACA,MAAM;gBACF,MAAM;gBACN,UAAU;YACd;QACJ;IACJ;AACJ"}
package/ddb/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * The websocket connection registry on DynamoDB, for the DynamoDB storage variants. It is a subpath
3
+ * of its own (`@webiny/api-websockets-aws/ddb/index.js`), not part of the package index: the index
4
+ * is the AWS transport, which every AWS API loads, including the Postgres one, and that must not
5
+ * load DynamoDB code.
6
+ */
7
+ export { WebsocketsConnectionRegistry } from "./WebsocketsConnectionRegistry.js";
8
+ export { WebsocketsDdbFeature } from "./WebsocketsDdbFeature.js";
package/ddb/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { WebsocketsConnectionRegistry } from "./WebsocketsConnectionRegistry.js";
2
+ export { WebsocketsDdbFeature } from "./WebsocketsDdbFeature.js";
@@ -1,15 +1,6 @@
1
- import type { HandlerFactoryParams } from "@webiny/handler-aws/types.js";
2
- import type { WebsocketsResponse } from "@webiny/api-websockets/exports/api.js";
3
- import type { APIGatewayProxyResult, Context as LambdaContext } from "@webiny/aws-sdk/types/index.js";
4
- import type { WebsocketsRunner } from "@webiny/api-websockets/exports/api.js";
1
+ import type { WebsocketsRunner } from "@webiny/api-websockets/runner/WebsocketsRunner.js";
5
2
  import type { GenericRecord } from "@webiny/api/types.js";
6
3
  import type { PartialDeep } from "type-fest";
7
- export interface HandlerCallable {
8
- (event: IAwsWebsocketsIncomingEvent, context: LambdaContext): Promise<APIGatewayProxyResult>;
9
- }
10
- export interface HandlerParams extends HandlerFactoryParams {
11
- response?: WebsocketsResponse.Interface;
12
- }
13
4
  export declare enum WebsocketsEventRoute {
14
5
  connect = "$connect",
15
6
  disconnect = "$disconnect",
@@ -1 +1 @@
1
- {"version":3,"file":"handler/types.js","sources":["../../src/handler/types.ts"],"sourcesContent":["import type { HandlerFactoryParams } from \"@webiny/handler-aws/types.js\";\nimport type { WebsocketsResponse } from \"@webiny/api-websockets/exports/api.js\";\nimport type {\n APIGatewayProxyResult,\n Context as LambdaContext\n} from \"@webiny/aws-sdk/types/index.js\";\nimport type { WebsocketsRunner } from \"@webiny/api-websockets/exports/api.js\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\nimport type { PartialDeep } from \"type-fest\";\n\nexport interface HandlerCallable {\n (event: IAwsWebsocketsIncomingEvent, context: LambdaContext): Promise<APIGatewayProxyResult>;\n}\n\nexport interface HandlerParams extends HandlerFactoryParams {\n response?: WebsocketsResponse.Interface;\n}\n\nexport enum WebsocketsEventRoute {\n connect = \"$connect\",\n disconnect = \"$disconnect\",\n default = \"$default\"\n}\n\nexport enum WebsocketsEventRequestContextEventType {\n message = \"MESSAGE\",\n connect = \"CONNECT\",\n disconnect = \"DISCONNECT\"\n}\n\nexport interface IAwsWebsocketsEventRequestContext {\n connectionId: string;\n connectedAt: number;\n domainName: string;\n eventType: WebsocketsEventRequestContextEventType;\n routeKey: WebsocketsEventRoute | string;\n stage: string;\n}\n\nexport interface IAwsWebsocketsEventHeaders {\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-endpoint\"]?: string;\n}\n\nexport interface IAwsWebsocketsEventQueryStringParameters {\n tenant?: string;\n token?: string;\n}\n\nexport interface IAwsWebsocketsEvent<\n T extends WebsocketsRunner.EventData = WebsocketsRunner.EventData\n> {\n headers?: IAwsWebsocketsEventHeaders;\n queryStringParameters?: IAwsWebsocketsEventQueryStringParameters;\n requestContext: IAwsWebsocketsEventRequestContext;\n body?: T;\n}\n\nexport interface IAwsWebsocketsIncomingEvent extends PartialDeep<\n Omit<IAwsWebsocketsEvent, \"body\">\n> {\n body?: string | GenericRecord;\n}\n"],"names":["WebsocketsEventRoute","WebsocketsEventRequestContextEventType"],"mappings":"AAkBO,IAAKA,6BAAoBA,WAAAA,GAAAA,SAApBA,oBAAoB;;;;WAApBA;;AAML,IAAKC,+CAAsCA,WAAAA,GAAAA,SAAtCA,sCAAsC;;;;WAAtCA"}
1
+ {"version":3,"file":"handler/types.js","sources":["../../src/handler/types.ts"],"sourcesContent":["import type { WebsocketsRunner } from \"@webiny/api-websockets/runner/WebsocketsRunner.js\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\nimport type { PartialDeep } from \"type-fest\";\n\nexport enum WebsocketsEventRoute {\n connect = \"$connect\",\n disconnect = \"$disconnect\",\n default = \"$default\"\n}\n\nexport enum WebsocketsEventRequestContextEventType {\n message = \"MESSAGE\",\n connect = \"CONNECT\",\n disconnect = \"DISCONNECT\"\n}\n\nexport interface IAwsWebsocketsEventRequestContext {\n connectionId: string;\n connectedAt: number;\n domainName: string;\n eventType: WebsocketsEventRequestContextEventType;\n routeKey: WebsocketsEventRoute | string;\n stage: string;\n}\n\nexport interface IAwsWebsocketsEventHeaders {\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-endpoint\"]?: string;\n}\n\nexport interface IAwsWebsocketsEventQueryStringParameters {\n tenant?: string;\n token?: string;\n}\n\nexport interface IAwsWebsocketsEvent<\n T extends WebsocketsRunner.EventData = WebsocketsRunner.EventData\n> {\n headers?: IAwsWebsocketsEventHeaders;\n queryStringParameters?: IAwsWebsocketsEventQueryStringParameters;\n requestContext: IAwsWebsocketsEventRequestContext;\n body?: T;\n}\n\nexport interface IAwsWebsocketsIncomingEvent extends PartialDeep<\n Omit<IAwsWebsocketsEvent, \"body\">\n> {\n body?: string | GenericRecord;\n}\n"],"names":["WebsocketsEventRoute","WebsocketsEventRequestContextEventType"],"mappings":"AAIO,IAAKA,6BAAoBA,WAAAA,GAAAA,SAAAA,oBAAAA;;;;;;AAMzB,IAAKC,+CAAsCA,WAAAA,GAAAA,SAAAA,sCAAAA"}
package/index.d.ts CHANGED
@@ -1,2 +1,7 @@
1
- import "./handler/register.js";
2
- export declare const createAwsWebsockets: () => import("@webiny/handler").RegisterExtensionPlugin<import("@webiny/handler/types.js").Context>[];
1
+ export { AwsWebsocketsTransport } from "./transport/AwsWebsocketsTransport.js";
2
+ export declare const WebsocketsAwsFeature: {
3
+ name: string;
4
+ register(container: import("@webiny/di").Container): void;
5
+ };
6
+ /** @deprecated use WebsocketsAwsFeature.register(container) */
7
+ export declare const createAwsWebsockets: () => never[];
package/index.js CHANGED
@@ -1,15 +1,17 @@
1
- import "./handler/register.js";
2
- import { createRegisterExtensionPlugin } from "@webiny/handler";
1
+ import { createFeature } from "@webiny/feature/api";
3
2
  import { AwsWebsocketsTransport } from "./transport/AwsWebsocketsTransport.js";
3
+ import { WebSocketLambdaHandler } from "./WebSocketLambdaHandler.js";
4
+ const WebsocketsAwsFeature = createFeature({
5
+ name: "WebsocketsAws",
6
+ register (container) {
7
+ container.register(WebSocketLambdaHandler);
8
+ container.register(AwsWebsocketsTransport);
9
+ }
10
+ });
4
11
  const createAwsWebsockets = ()=>{
5
- const plugin = createRegisterExtensionPlugin((context)=>{
6
- context.container.register(AwsWebsocketsTransport).inSingletonScope();
7
- });
8
- plugin.name = "websockets.aws.transport";
9
- return [
10
- plugin
11
- ];
12
+ console.warn("[api-websockets-aws] createAwsWebsockets() is deprecated. Use WebsocketsAwsFeature.register(container) instead.");
13
+ return [];
12
14
  };
13
- export { createAwsWebsockets };
15
+ export { AwsWebsocketsTransport, WebsocketsAwsFeature, createAwsWebsockets };
14
16
 
15
17
  //# sourceMappingURL=index.js.map
package/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import \"./handler/register.js\";\nimport { createRegisterExtensionPlugin } from \"@webiny/handler\";\nimport { AwsWebsocketsTransport } from \"~/transport/AwsWebsocketsTransport.js\";\n\nexport const createAwsWebsockets = () => {\n const plugin = createRegisterExtensionPlugin(context => {\n context.container.register(AwsWebsocketsTransport).inSingletonScope();\n });\n plugin.name = \"websockets.aws.transport\";\n return [plugin];\n};\n"],"names":["createAwsWebsockets","plugin","createRegisterExtensionPlugin","context","AwsWebsocketsTransport"],"mappings":";;;AAIO,MAAMA,sBAAsB;IAC/B,MAAMC,SAASC,8BAA8BC,CAAAA;QACzCA,QAAQ,SAAS,CAAC,QAAQ,CAACC,wBAAwB,gBAAgB;IACvE;IACAH,OAAO,IAAI,GAAG;IACd,OAAO;QAACA;KAAO;AACnB"}
1
+ {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport { AwsWebsocketsTransport } from \"~/transport/AwsWebsocketsTransport.js\";\nimport { WebSocketLambdaHandler } from \"~/WebSocketLambdaHandler.js\";\n\nexport { AwsWebsocketsTransport } from \"~/transport/AwsWebsocketsTransport.js\";\n// The DynamoDB connection registry is at `@webiny/api-websockets-aws/ddb/index.js`, so loading the\n// transport loads no DynamoDB code.\n\nexport const WebsocketsAwsFeature = createFeature({\n name: \"WebsocketsAws\",\n register(container) {\n container.register(WebSocketLambdaHandler);\n container.register(AwsWebsocketsTransport);\n }\n});\n\n/** @deprecated use WebsocketsAwsFeature.register(container) */\nexport const createAwsWebsockets = () => {\n console.warn(\n \"[api-websockets-aws] createAwsWebsockets() is deprecated. Use WebsocketsAwsFeature.register(container) instead.\"\n );\n return [];\n};\n"],"names":["WebsocketsAwsFeature","createFeature","container","WebSocketLambdaHandler","AwsWebsocketsTransport","createAwsWebsockets","console"],"mappings":";;;AAQO,MAAMA,uBAAuBC,cAAc;IAC9C,MAAM;IACN,UAASC,SAAS;QACdA,UAAU,QAAQ,CAACC;QACnBD,UAAU,QAAQ,CAACE;IACvB;AACJ;AAGO,MAAMC,sBAAsB;IAC/BC,QAAQ,IAAI,CACR;IAEJ,OAAO,EAAE;AACb"}
package/package.json CHANGED
@@ -1,7 +1,14 @@
1
1
  {
2
2
  "name": "@webiny/api-websockets-aws",
3
- "version": "0.0.0-unstable.b6d7105cee",
3
+ "version": "0.0.0-unstable.e1999482d0",
4
4
  "type": "module",
5
+ "keywords": [
6
+ "@webiny/api-websockets",
7
+ "storage-operations",
8
+ "dynamodb",
9
+ "ddb",
10
+ "sau:ddb"
11
+ ],
5
12
  "repository": {
6
13
  "type": "git",
7
14
  "url": "https://github.com/webiny/webiny-js.git"
@@ -16,29 +23,33 @@
16
23
  },
17
24
  "license": "MIT",
18
25
  "dependencies": {
19
- "@webiny/api": "0.0.0-unstable.b6d7105cee",
20
- "@webiny/api-websockets": "0.0.0-unstable.b6d7105cee",
21
- "@webiny/aws-sdk": "0.0.0-unstable.b6d7105cee",
22
- "@webiny/error": "0.0.0-unstable.b6d7105cee",
23
- "@webiny/handler": "0.0.0-unstable.b6d7105cee",
24
- "@webiny/handler-aws": "0.0.0-unstable.b6d7105cee",
25
- "@webiny/plugins": "0.0.0-unstable.b6d7105cee",
26
- "@webiny/utils": "0.0.0-unstable.b6d7105cee",
27
- "type-fest": "5.8.0",
28
- "zod": "4.4.3"
26
+ "@webiny/api": "0.0.0-unstable.e1999482d0",
27
+ "@webiny/api-kernel-aws": "0.0.0-unstable.e1999482d0",
28
+ "@webiny/api-websockets": "0.0.0-unstable.e1999482d0",
29
+ "@webiny/aws-sdk": "0.0.0-unstable.e1999482d0",
30
+ "@webiny/db-dynamodb": "0.0.0-unstable.e1999482d0",
31
+ "@webiny/error": "0.0.0-unstable.e1999482d0",
32
+ "@webiny/feature": "0.0.0-unstable.e1999482d0",
33
+ "@webiny/handler": "0.0.0-unstable.e1999482d0",
34
+ "@webiny/utils": "0.0.0-unstable.e1999482d0",
35
+ "type-fest": "6.0.3",
36
+ "zod": "4.6.7"
29
37
  },
30
38
  "devDependencies": {
31
- "@webiny/api-core": "0.0.0-unstable.b6d7105cee",
32
- "@webiny/api-headless-cms": "0.0.0-unstable.b6d7105cee",
33
- "@webiny/build-tools": "0.0.0-unstable.b6d7105cee",
34
- "@webiny/handler-db": "0.0.0-unstable.b6d7105cee",
35
- "@webiny/handler-graphql": "0.0.0-unstable.b6d7105cee",
36
- "@webiny/project-utils": "0.0.0-unstable.b6d7105cee",
37
- "graphql": "16.14.2",
38
- "rimraf": "6.1.3",
39
- "type-fest": "5.8.0",
39
+ "@webiny/api-core": "0.0.0-unstable.e1999482d0",
40
+ "@webiny/api-core-testing": "0.0.0-unstable.e1999482d0",
41
+ "@webiny/api-graphql": "0.0.0-unstable.e1999482d0",
42
+ "@webiny/api-headless-cms": "0.0.0-unstable.e1999482d0",
43
+ "@webiny/api-kernel-core": "0.0.0-unstable.e1999482d0",
44
+ "@webiny/build-tools": "0.0.0-unstable.e1999482d0",
45
+ "@webiny/di": "2.0.0",
46
+ "@webiny/wcp": "0.0.0-unstable.e1999482d0",
47
+ "aws-sdk-client-mock": "4.1.0",
48
+ "graphql": "16.15.0",
49
+ "rimraf": "6.1.4",
50
+ "type-fest": "6.0.3",
40
51
  "typescript": "7.0.2",
41
- "vitest": "4.1.10"
52
+ "vitest": "5.1.1"
42
53
  },
43
54
  "publishConfig": {
44
55
  "access": "public"
@@ -1,4 +1,4 @@
1
- import { WebsocketsTransport } from "@webiny/api-websockets/exports/api.js";
1
+ import { WebsocketsTransport } from "@webiny/api-websockets/transport/index.js";
2
2
  import type { GenericRecord } from "@webiny/api/types.js";
3
3
  declare class AwsWebsocketsTransportImpl implements WebsocketsTransport.Interface {
4
4
  private readonly clients;
@@ -7,6 +7,6 @@ declare class AwsWebsocketsTransportImpl implements WebsocketsTransport.Interfac
7
7
  private getClient;
8
8
  }
9
9
  export declare const AwsWebsocketsTransport: typeof AwsWebsocketsTransportImpl & {
10
- __abstraction: import("@webiny/di").Abstraction<import("@webiny/api-websockets/transport").IWebsocketsTransport>;
10
+ __abstraction: import("@webiny/di").Abstraction<import("@webiny/api-websockets/transport/index.js").IWebsocketsTransport>;
11
11
  };
12
12
  export {};
@@ -1,5 +1,5 @@
1
1
  import { ApiGatewayManagementApiClient, DeleteConnectionCommand, PostToConnectionCommand } from "@webiny/aws-sdk/client-apigatewaymanagementapi/index.js";
2
- import { WebsocketsTransport } from "@webiny/api-websockets/exports/api.js";
2
+ import { WebsocketsTransport } from "@webiny/api-websockets/transport/index.js";
3
3
  class AwsWebsocketsTransportImpl {
4
4
  async send(connections, data) {
5
5
  for (const connection of connections)try {
@@ -1 +1 @@
1
- {"version":3,"file":"transport/AwsWebsocketsTransport.js","sources":["../../src/transport/AwsWebsocketsTransport.ts"],"sourcesContent":["import {\n ApiGatewayManagementApiClient,\n DeleteConnectionCommand,\n PostToConnectionCommand\n} from \"@webiny/aws-sdk/client-apigatewaymanagementapi/index.js\";\nimport { WebsocketsTransport } from \"@webiny/api-websockets/exports/api.js\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\n\nclass AwsWebsocketsTransportImpl implements WebsocketsTransport.Interface {\n private readonly clients = new Map<string, ApiGatewayManagementApiClient>();\n\n public async send<T extends GenericRecord = GenericRecord>(\n connections: WebsocketsTransport.SendConnection[],\n data: WebsocketsTransport.SendData<T>\n ): Promise<void> {\n for (const connection of connections) {\n try {\n const client = this.getClient(connection.endpoint);\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: WebsocketsTransport.DisconnectConnection[]) {\n for (const connection of connections) {\n try {\n const client = this.getClient(connection.endpoint);\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(endpoint: string): ApiGatewayManagementApiClient {\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\nexport const AwsWebsocketsTransport = WebsocketsTransport.createImplementation({\n implementation: AwsWebsocketsTransportImpl,\n dependencies: []\n});\n"],"names":["AwsWebsocketsTransportImpl","connections","data","connection","client","command","PostToConnectionCommand","JSON","ex","console","DeleteConnectionCommand","endpoint","newClient","ApiGatewayManagementApiClient","Map","AwsWebsocketsTransport","WebsocketsTransport"],"mappings":";;AAQA,MAAMA;IAGF,MAAa,KACTC,WAAiD,EACjDC,IAAqC,EACxB;QACb,KAAK,MAAMC,cAAcF,YACrB,IAAI;YACA,MAAMG,SAAS,IAAI,CAAC,SAAS,CAACD,WAAW,QAAQ;YAEjD,MAAME,UAAU,IAAIC,wBAAwB;gBACxC,cAAcH,WAAW,YAAY;gBACrC,MAAMI,KAAK,SAAS,CAACL;YACzB;YACA,MAAME,OAAO,IAAI,CAACC;QACtB,EAAE,OAAOG,IAAI;YACTC,QAAQ,KAAK,CACT,CAAC,sCAAsC,EAAEN,WAAW,YAAY,CAAC,mCAAmC,CAAC;YAEzGM,QAAQ,GAAG,CAACD;QAChB;IAER;IAEA,MAAa,WAAWP,WAAuD,EAAE;QAC7E,KAAK,MAAME,cAAcF,YACrB,IAAI;YACA,MAAMG,SAAS,IAAI,CAAC,SAAS,CAACD,WAAW,QAAQ;YACjD,MAAME,UAAU,IAAIK,wBAAwB;gBACxC,cAAcP,WAAW,YAAY;YACzC;YACA,MAAMC,OAAO,IAAI,CAACC;QACtB,EAAE,OAAOG,IAAI;YACTC,QAAQ,KAAK,CACT,CAAC,iCAAiC,EAAEN,WAAW,YAAY,CAAC,mCAAmC,CAAC;YAEpGM,QAAQ,GAAG,CAACD;QAChB;IAER;IAEQ,UAAUG,QAAgB,EAAiC;QAC/D,MAAMP,SAAS,IAAI,CAAC,OAAO,CAAC,GAAG,CAACO;QAChC,IAAIP,QACA,OAAOA;QAEX,MAAMQ,YAAY,IAAIC,8BAA8B;YAChDF;QACJ;QACA,IAAI,CAAC,OAAO,CAAC,GAAG,CAACA,UAAUC;QAC3B,OAAOA;IACX;;aAnDiB,OAAO,GAAG,IAAIE;;AAoDnC;AAEO,MAAMC,yBAAyBC,oBAAoB,oBAAoB,CAAC;IAC3E,gBAAgBhB;IAChB,cAAc,EAAE;AACpB"}
1
+ {"version":3,"file":"transport/AwsWebsocketsTransport.js","sources":["../../src/transport/AwsWebsocketsTransport.ts"],"sourcesContent":["import {\n ApiGatewayManagementApiClient,\n DeleteConnectionCommand,\n PostToConnectionCommand\n} from \"@webiny/aws-sdk/client-apigatewaymanagementapi/index.js\";\nimport { WebsocketsTransport } from \"@webiny/api-websockets/transport/index.js\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\n\nclass AwsWebsocketsTransportImpl implements WebsocketsTransport.Interface {\n private readonly clients = new Map<string, ApiGatewayManagementApiClient>();\n\n public async send<T extends GenericRecord = GenericRecord>(\n connections: WebsocketsTransport.SendConnection[],\n data: WebsocketsTransport.SendData<T>\n ): Promise<void> {\n for (const connection of connections) {\n try {\n const client = this.getClient(connection.endpoint);\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: WebsocketsTransport.DisconnectConnection[]) {\n for (const connection of connections) {\n try {\n const client = this.getClient(connection.endpoint);\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(endpoint: string): ApiGatewayManagementApiClient {\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\nexport const AwsWebsocketsTransport = WebsocketsTransport.createImplementation({\n implementation: AwsWebsocketsTransportImpl,\n dependencies: []\n});\n"],"names":["AwsWebsocketsTransportImpl","connections","data","connection","client","command","PostToConnectionCommand","JSON","ex","console","DeleteConnectionCommand","endpoint","newClient","ApiGatewayManagementApiClient","Map","AwsWebsocketsTransport","WebsocketsTransport"],"mappings":";;AAQA,MAAMA;IAGF,MAAa,KACTC,WAAiD,EACjDC,IAAqC,EACxB;QACb,KAAK,MAAMC,cAAcF,YACrB,IAAI;YACA,MAAMG,SAAS,IAAI,CAAC,SAAS,CAACD,WAAW,QAAQ;YAEjD,MAAME,UAAU,IAAIC,wBAAwB;gBACxC,cAAcH,WAAW,YAAY;gBACrC,MAAMI,KAAK,SAAS,CAACL;YACzB;YACA,MAAME,OAAO,IAAI,CAACC;QACtB,EAAE,OAAOG,IAAI;YACTC,QAAQ,KAAK,CACT,CAAC,sCAAsC,EAAEN,WAAW,YAAY,CAAC,mCAAmC,CAAC;YAEzGM,QAAQ,GAAG,CAACD;QAChB;IAER;IAEA,MAAa,WAAWP,WAAuD,EAAE;QAC7E,KAAK,MAAME,cAAcF,YACrB,IAAI;YACA,MAAMG,SAAS,IAAI,CAAC,SAAS,CAACD,WAAW,QAAQ;YACjD,MAAME,UAAU,IAAIK,wBAAwB;gBACxC,cAAcP,WAAW,YAAY;YACzC;YACA,MAAMC,OAAO,IAAI,CAACC;QACtB,EAAE,OAAOG,IAAI;YACTC,QAAQ,KAAK,CACT,CAAC,iCAAiC,EAAEN,WAAW,YAAY,CAAC,mCAAmC,CAAC;YAEpGM,QAAQ,GAAG,CAACD;QAChB;IAER;IAEQ,UAAUG,QAAgB,EAAiC;QAC/D,MAAMP,SAAS,IAAI,CAAC,OAAO,CAAC,GAAG,CAACO;QAChC,IAAIP,QACA,OAAOA;QAEX,MAAMQ,YAAY,IAAIC,8BAA8B;YAChDF;QACJ;QACA,IAAI,CAAC,OAAO,CAAC,GAAG,CAACA,UAAUC;QAC3B,OAAOA;IACX;;aAnDiB,OAAO,GAAG,IAAIE;;AAoDnC;AAEO,MAAMC,yBAAyBC,oBAAoB,oBAAoB,CAAC;IAC3E,gBAAgBhB;IAChB,cAAc,EAAE;AACpB"}
@@ -1,4 +1,5 @@
1
- import { WebsocketsEventValidator, WebsocketsRunner } from "@webiny/api-websockets/exports/api.js";
1
+ import { WebsocketsEventValidator } from "@webiny/api-websockets/validator/index.js";
2
+ import { WebsocketsRunner } from "@webiny/api-websockets/runner/WebsocketsRunner.js";
2
3
  export declare class AwsWebsocketsEventValidator implements WebsocketsEventValidator.Interface {
3
4
  validate<T extends WebsocketsRunner.EventData = WebsocketsRunner.EventData>(input: unknown): Promise<WebsocketsRunner.Event<T>>;
4
5
  }
@@ -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 JSON.parse(value);
32
+ return parseJson(value);
32
33
  } catch (ex) {
33
34
  console.error(`Failed body validation: ${ex.message}`);
34
35
  console.log(`Body: ${value}`);
@@ -42,7 +43,7 @@ const validation = zod.object({
42
43
  }).optional()
43
44
  }).superRefine((output, context)=>{
44
45
  if (output.requestContext.eventType !== WebsocketsEventRequestContextEventType.message) return;
45
- if (output.body) return;
46
+ else if (output.body) return;
46
47
  context.addIssue({
47
48
  path: [
48
49
  "body"
@@ -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 JSON.parse(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","JSON","ex","console","output","bodyValidation","AwsWebsocketsEventValidator","input","result","createZodError","bodyResult","rc"],"mappings":";;;AAKA,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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SAAS;gBACL,cAAcG,GAAG,YAAY;gBAC7B,aAAaA,GAAG,WAAW;gBAC3B,MAAMA,GAAG,UAAU;gBACnB,WAAWjB,YAAY,CAACiB,GAAG,SAAS,CAA2C;gBAC/E,OAAOf,WAAW,CAACe,GAAG,QAAQ,CAAC,IAAIA,GAAG,QAAQ;gBAC9C,UAAU,CAAC,QAAQ,EAAEA,GAAG,UAAU,CAAC,CAAC,EAAEA,GAAG,KAAK,EAAE;YACpD;YACA,MAAM;gBACF,GAAKD,WAAW,IAAI,IAAI,CAAC,CAAC;YAC9B;QACJ;IACJ;AACJ"}
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 } from \"@webiny/api-websockets/validator/index.js\";\nimport { WebsocketsRunner } from \"@webiny/api-websockets/runner/WebsocketsRunner.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":";;;;AAOA,MAAMA,eAA2F;IAC7F,CAACC,uCAAuC,OAAO,CAAC,EAAE;IAClD,CAACA,uCAAuC,OAAO,CAAC,EA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QAAQA,IAAI,MAAM;IAClB,WAAWA,IAAI,MAAM,GAAG,OAAO;IAC/B,QAAQA,IAAI,MAAM;IAClB,MAAMA,IAAI,WAAW,CAAC,CAAC,GAAG,OAAO;AACrC,GACC,QAAQ;AAEN,MAAMQ;IACT,MAAa,SACTC,KAAc,EACoB;QAClC,MAAMC,SAAS,MAAMX,WAAW,cAAc,CAACU;QAC/C,IAAI,CAACC,OAAO,OAAO,EACf,MAAMC,eAAeD,OAAO,KAAK;QAErC,MAAME,aAAa,MAAML,eAAe,cAAc,CAACG,OAAO,IAAI,CAAC,IAAI;QACvE,IAAI,CAACE,WAAW,OAAO,EACnB,MAAMD,eAAeC,WAAW,KAAK;QAGzC,MAAMC,KAAKH,OAAO,IAAI,CAAC,cAAc;QAErC,OAAO;YACH,SAASA,OAAO,IAAI,CAAC,OAAO;YAC5B,SAAS;gBACL,cAAcG,GAAG,YAAY;gBAC7B,aAAaA,GAAG,WAAW;gBAC3B,MAAMA,GAAG,UAAU;gBACnB,WAAWjB,YAAY,CAACiB,GAAG,SAAS,CAA2C;gBAC/E,OAAOf,WAAW,CAACe,GAAG,QAAQ,CAAC,IAAIA,GAAG,QAAQ;gBAC9C,UAAU,CAAC,QAAQ,EAAEA,GAAG,UAAU,CAAC,CAAC,EAAEA,GAAG,KAAK,EAAE;YACpD;YACA,MAAM;gBACF,GAAKD,WAAW,IAAI,IAAI,CAAC,CAAC;YAC9B;QACJ;IACJ;AACJ"}
@@ -1,2 +0,0 @@
1
- import type { HandlerCallable, HandlerParams } from "./types.js";
2
- export declare const createHandler: (params: HandlerParams) => HandlerCallable;
@@ -1,105 +0,0 @@
1
- import { createHandler } from "@webiny/handler";
2
- import { registerDefaultPlugins } from "@webiny/handler-aws/plugins/index.js";
3
- import { execute } from "@webiny/handler-aws/execute.js";
4
- import { PluginsContainer } from "@webiny/plugins";
5
- import { WebsocketsResponse, WebsocketsRunner, WebsocketsTransport, createWebsocketsRoutePlugins } from "@webiny/api-websockets/exports/api.js";
6
- import { getEventValues } from "./headers.js";
7
- import { AwsWebsocketsEventValidator } from "../validator/AwsWebsocketsEventValidator.js";
8
- const url = "/webiny-websockets";
9
- const createPluginsContainer = (plugins)=>{
10
- if (plugins instanceof PluginsContainer) return plugins;
11
- return new PluginsContainer(plugins || []);
12
- };
13
- const handler_createHandler = (params)=>{
14
- const plugins = createPluginsContainer(params.plugins);
15
- plugins.register(...createWebsocketsRoutePlugins());
16
- const validator = new AwsWebsocketsEventValidator();
17
- return async (event)=>{
18
- const app = createHandler({
19
- ...params,
20
- plugins,
21
- options: {
22
- logger: true === params.debug,
23
- ...params.options || {}
24
- }
25
- });
26
- registerDefaultPlugins(app.webiny);
27
- await app.register(async (wsApp)=>{
28
- wsApp.setErrorHandler(async (error, _, reply)=>{
29
- app.__webiny_raw_result = {
30
- error: {
31
- message: error.message,
32
- code: error.code,
33
- data: error.data
34
- },
35
- statusCode: 200
36
- };
37
- return reply.send({});
38
- });
39
- wsApp.post(url, async (_, reply)=>{
40
- const { response } = params;
41
- const context = app.webiny;
42
- const responseObj = response || context.container.resolve(WebsocketsResponse);
43
- const runner = new WebsocketsRunner(context, responseObj);
44
- let validatedEvent;
45
- try {
46
- validatedEvent = await validator.validate(event);
47
- } catch (ex) {
48
- const errorResult = responseObj.error({
49
- message: "Validation failed.",
50
- error: {
51
- message: ex.message,
52
- code: ex.code,
53
- data: ex.data,
54
- stack: ex.stack
55
- }
56
- });
57
- const { connectionId, domainName, stage, eventType } = event.requestContext || {};
58
- if (connectionId && domainName && stage && "MESSAGE" === eventType) try {
59
- const transport = context.container.resolve(WebsocketsTransport);
60
- const endpoint = `https://${domainName}/${stage}`;
61
- await transport.send([
62
- {
63
- connectionId,
64
- endpoint
65
- }
66
- ], errorResult);
67
- } catch {}
68
- app.__webiny_raw_result = {
69
- statusCode: errorResult.statusCode,
70
- headers: {
71
- "sec-websocket-protocol": "webiny-ws-v1"
72
- }
73
- };
74
- return reply.send();
75
- }
76
- const result = await runner.run(validatedEvent);
77
- app.__webiny_raw_result = {
78
- statusCode: result.statusCode,
79
- headers: {
80
- "sec-websocket-protocol": "webiny-ws-v1"
81
- }
82
- };
83
- return reply.send();
84
- });
85
- });
86
- const { tenant, endpoint, token } = getEventValues(event);
87
- const headers = {
88
- Authorization: `Bearer ${token}`,
89
- ["x-tenant"]: tenant,
90
- ["x-webiny-cms-endpoint"]: endpoint,
91
- ...event.headers
92
- };
93
- return execute({
94
- app,
95
- url: url,
96
- payload: {
97
- ...event,
98
- headers
99
- }
100
- });
101
- };
102
- };
103
- export { handler_createHandler as createHandler };
104
-
105
- //# sourceMappingURL=handler.js.map
@@ -1 +0,0 @@
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;QAEjC,MAAMA,IAAI,QAAQ,CAAC,OAAMG;YACrBA,MAAM,eAAe,CAAc,OAAOC,OAAOC,GAAGC;gBAChDN,IAAI,mBAAmB,GAAG;oBACtB,OAAO;wBACH,SAASI,MAAM,OAAO;wBACtB,MAAMA,MAAM,IAAI;wBAChB,MAAMA,MAAM,IAAI;oBACpB;oBACA,YAAY;gBAChB;gBACA,OAAOE,MAAM,IAAI,CAAC,CAAC;YACvB;YAEAH,MAAM,IAAI,CAACb,KAAK,OAAOe,GAAGC;gBACtB,MAAM,EAAEC,QAAQ,EAAE,GAAGZ;gBACrB,MAAMa,UAAUR,IAAI,MAAM;gBAE1B,MAAMS,cAAcF,YAAYC,QAAQ,SAAS,CAAC,OAAO,CAACE;gBAC1D,MAAMC,SAAS,IAAIC,iBAAiBJ,SAASC;gBAE7C,IAAII;gBACJ,IAAI;oBACAA,iBAAiB,MAAMhB,UAAU,QAAQ,CAACE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;IACJ;AACJ"}
@@ -1,6 +0,0 @@
1
- import type { IAwsWebsocketsIncomingEvent } from "../handler/types.js";
2
- export declare const getEventValues: (event: IAwsWebsocketsIncomingEvent) => {
3
- tenant: string;
4
- token: string | null;
5
- endpoint: string;
6
- };
@@ -1,27 +0,0 @@
1
- const getEventBody = (event)=>{
2
- if (!event.body) return {};
3
- if ("object" == typeof event.body) return event.body;
4
- if ("string" == typeof event.body) try {
5
- return JSON.parse(event.body);
6
- } catch (ex) {
7
- console.log(ex.message);
8
- return {};
9
- }
10
- console.log("Unexpected event.body type:", typeof event.body);
11
- return {};
12
- };
13
- const getToken = (body, event)=>body?.token || event.queryStringParameters?.token || null;
14
- const getTenant = (body, event)=>body?.tenant || event.queryStringParameters?.tenant || "root";
15
- const getEventValues = (event)=>{
16
- const body = getEventBody(event);
17
- const token = getToken(body, event);
18
- const tenant = getTenant(body, event);
19
- return {
20
- tenant,
21
- token,
22
- endpoint: "manage"
23
- };
24
- };
25
- export { getEventValues };
26
-
27
- //# sourceMappingURL=headers.js.map
@@ -1 +0,0 @@
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 JSON.parse(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","JSON","ex","console","getToken","body","getTenant","getEventValues","token","tenant"],"mappings":"AAGA,MAAMA,eAAe,CAACC;IAClB,IAAI,CAACA,MAAM,IAAI,EACX,OAAO,CAAC;IACL,IAAI,AAAsB,YAAtB,OAAOA,MAAM,IAAI,EACxB,OAAOA,MAAM,IAAI;IACd,IAAI,AAAsB,YAAtB,OAAOA,MAAM,IAAI,EACxB,IAAI;QACA,OAAOC,KAAK,KAAK,CAACD,MAAM,IAAI;IAChC,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"}
@@ -1 +0,0 @@
1
- export {};
@@ -1,16 +0,0 @@
1
- import { registry } from "@webiny/handler-aws/registry.js";
2
- import { createSourceHandler } from "@webiny/handler-aws";
3
- const handler = createSourceHandler({
4
- name: "handler-webiny-websockets",
5
- canUse: (event)=>{
6
- const { routeKey, connectionId, eventType } = event.requestContext || {};
7
- return !!routeKey && !!connectionId && !!eventType;
8
- },
9
- handle: async ({ params, event, context })=>{
10
- const { createHandler } = await import("./handler.js");
11
- return createHandler(params)(event, context);
12
- }
13
- });
14
- registry.register(handler);
15
-
16
- //# sourceMappingURL=register.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"handler/register.js","sources":["../../src/handler/register.ts"],"sourcesContent":["import { registry } from \"@webiny/handler-aws/registry.js\";\nimport { createSourceHandler } from \"@webiny/handler-aws\";\nimport type { HandlerParams, IAwsWebsocketsIncomingEvent } from \"./types.js\";\n\nconst handler = createSourceHandler<IAwsWebsocketsIncomingEvent, 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.js\"\n );\n return createHandler(params)(event, context);\n }\n});\n\nregistry.register(handler);\n"],"names":["handler","createSourceHandler","event","routeKey","connectionId","eventType","params","context","createHandler","registry"],"mappings":";;AAIA,MAAMA,UAAUC,oBAAgE;IAC5E,MAAM;IACN,QAAQC,CAAAA;QACJ,MAAM,EAAEC,QAAQ,EAAEC,YAAY,EAAEC,SAAS,EAAE,GAAGH,MAAM,cAAc,IAAI,CAAC;QACvE,OAAO,CAAC,CAACC,YAAY,CAAC,CAACC,gBAAgB,CAAC,CAACC;IAC7C;IACA,QAAQ,OAAO,EAAEC,MAAM,EAAEJ,KAAK,EAAEK,OAAO,EAAE;QACrC,MAAM,EAAEC,aAAa,EAAE,GAAG,MAAM,MAAM,CAAN;QAIhC,OAAOA,cAAcF,QAAQJ,OAAOK;IACxC;AACJ;AAEAE,SAAS,QAAQ,CAACT"}