@webiny/api-websockets 6.6.0-alpha.10 → 6.6.0-alpha.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/exports/api.d.ts +0 -1
- package/exports/api.js +0 -1
- package/features/ConnectionRegistry/abstractions.d.ts +1 -1
- package/features/ConnectionRegistry/abstractions.js.map +1 -1
- package/features/ListConnections/ListConnectionsUseCase.js +1 -1
- package/features/ListConnections/ListConnectionsUseCase.js.map +1 -1
- package/features/SendToConnections/SendToConnectionsUseCase.js +1 -1
- package/features/SendToConnections/SendToConnectionsUseCase.js.map +1 -1
- package/features/SendToConnections/abstractions.d.ts +1 -1
- package/features/SendToConnections/abstractions.js.map +1 -1
- package/features/SendToIdentity/SendToIdentityUseCase.d.ts +4 -4
- package/features/SendToIdentity/SendToIdentityUseCase.js +2 -3
- package/features/SendToIdentity/SendToIdentityUseCase.js.map +1 -1
- package/features/shared/errors.d.ts +1 -6
- package/features/shared/errors.js +1 -11
- package/features/shared/errors.js.map +1 -1
- package/package.json +13 -13
- package/runner/WebsocketsRunner.js +2 -1
- package/runner/WebsocketsRunner.js.map +1 -1
- package/runner/routes/connect.js +31 -12
- package/runner/routes/connect.js.map +1 -1
- package/transport/abstractions/WebsocketsTransport.d.ts +1 -12
- package/transport/abstractions/WebsocketsTransport.js.map +1 -1
- package/transport/index.d.ts +1 -1
- package/types.d.ts +0 -2
- package/features/SendToIdentity/abstractions.d.ts +0 -14
- package/features/SendToIdentity/abstractions.js +0 -5
- package/features/SendToIdentity/abstractions.js.map +0 -1
package/exports/api.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export { WebsocketsTransport } from "../transport/index.js";
|
|
2
2
|
export { WebsocketsListConnectionsUseCase } from "../features/ListConnections/abstractions.js";
|
|
3
|
-
export { WebsocketsSendToIdentityUseCase } from "../features/SendToIdentity/abstractions.js";
|
|
4
3
|
export { WebsocketsSendToConnectionsUseCase } from "../features/SendToConnections/abstractions.js";
|
|
5
4
|
export { WebsocketsDisconnectUseCase } from "../features/Disconnect/abstractions.js";
|
|
6
5
|
export { ConnectionRegistry } from "../features/ConnectionRegistry/abstractions.js";
|
package/exports/api.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export { WebsocketsTransport } from "../transport/index.js";
|
|
2
2
|
export { WebsocketsListConnectionsUseCase } from "../features/ListConnections/abstractions.js";
|
|
3
|
-
export { WebsocketsSendToIdentityUseCase } from "../features/SendToIdentity/abstractions.js";
|
|
4
3
|
export { WebsocketsSendToConnectionsUseCase } from "../features/SendToConnections/abstractions.js";
|
|
5
4
|
export { WebsocketsDisconnectUseCase } from "../features/Disconnect/abstractions.js";
|
|
6
5
|
export { ConnectionRegistry } from "../features/ConnectionRegistry/abstractions.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"features/ConnectionRegistry/abstractions.js","sources":["../../../src/features/ConnectionRegistry/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { IWebsocketsIdentity } from \"
|
|
1
|
+
{"version":3,"file":"features/ConnectionRegistry/abstractions.js","sources":["../../../src/features/ConnectionRegistry/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { IWebsocketsIdentity } from \"@webiny/api-core/features/websockets/index.js\";\n\nexport interface IWebsocketsConnectionRegistryData {\n connectionId: string;\n identity: IWebsocketsIdentity;\n tenant: string;\n connectedOn: string;\n endpoint: string;\n /*\n * The process holding the live socket, on a server that runs several processes. Absent where\n * one service holds every socket (API Gateway on AWS) and on rows written before it existed.\n */\n owner?: string;\n}\n\nexport interface IWebsocketsConnectionRegistryRegisterParams {\n connectionId: string;\n tenant: string;\n identity: IWebsocketsIdentity;\n endpoint: string;\n connectedOn: string;\n owner?: string;\n}\n\nexport interface IWebsocketsConnectionRegistryUnregisterParams {\n connectionId: string;\n}\n\nexport interface IWebsocketsConnectionRegistry {\n register(\n event: IWebsocketsConnectionRegistryRegisterParams\n ): Promise<IWebsocketsConnectionRegistryData>;\n unregister(event: IWebsocketsConnectionRegistryUnregisterParams): Promise<void>;\n\n listViaConnections(connections: string[]): Promise<IWebsocketsConnectionRegistryData[]>;\n listViaIdentity(identity: string): Promise<IWebsocketsConnectionRegistryData[]>;\n listViaTenant(tenant: string): Promise<IWebsocketsConnectionRegistryData[]>;\n listAll(): Promise<IWebsocketsConnectionRegistryData[]>;\n\n updateLastSeen(connectionId: string): Promise<void>;\n listStale(olderThan: Date): Promise<IWebsocketsConnectionRegistryData[]>;\n}\n\nexport const ConnectionRegistry =\n createAbstraction<IWebsocketsConnectionRegistry>(\"ConnectionRegistry\");\n\nexport namespace ConnectionRegistry {\n export type Interface = IWebsocketsConnectionRegistry;\n export type Identity = IWebsocketsIdentity;\n export type Data = IWebsocketsConnectionRegistryData;\n export type RegisterParams = IWebsocketsConnectionRegistryRegisterParams;\n export type UnregisterParams = IWebsocketsConnectionRegistryUnregisterParams;\n}\n"],"names":["ConnectionRegistry","createAbstraction"],"mappings":";AA4CO,MAAMA,qBACTC,kBAAiD"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Result } from "@webiny/feature/api";
|
|
2
2
|
import { WebsocketsListConnectionsUseCase } from "./abstractions.js";
|
|
3
|
-
import { WebsocketServiceError } from "
|
|
3
|
+
import { WebsocketServiceError } from "@webiny/api-core/features/websockets/index.js";
|
|
4
4
|
import { ConnectionRegistry } from "../ConnectionRegistry/abstractions.js";
|
|
5
5
|
class ListConnectionsUseCaseImpl {
|
|
6
6
|
constructor(registry){
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"features/ListConnections/ListConnectionsUseCase.js","sources":["../../../src/features/ListConnections/ListConnectionsUseCase.ts"],"sourcesContent":["import { Result } from \"@webiny/feature/api\";\nimport { WebsocketsListConnectionsUseCase } from \"./abstractions.js\";\nimport type { WebsocketsError } from \"~/features/shared/errors.js\";\nimport { WebsocketServiceError } from \"
|
|
1
|
+
{"version":3,"file":"features/ListConnections/ListConnectionsUseCase.js","sources":["../../../src/features/ListConnections/ListConnectionsUseCase.ts"],"sourcesContent":["import { Result } from \"@webiny/feature/api\";\nimport { WebsocketsListConnectionsUseCase } from \"./abstractions.js\";\nimport type { WebsocketsError } from \"~/features/shared/errors.js\";\nimport { WebsocketServiceError } from \"@webiny/api-core/features/websockets/index.js\";\nimport { ConnectionRegistry } from \"~/features/ConnectionRegistry/abstractions.js\";\n\nclass ListConnectionsUseCaseImpl implements WebsocketsListConnectionsUseCase.Interface {\n public constructor(private readonly registry: ConnectionRegistry.Interface) {}\n\n public async execute(\n params?: WebsocketsListConnectionsUseCase.Params\n ): Promise<Result<WebsocketsListConnectionsUseCase.RegistryData[], WebsocketsError>> {\n let connections: WebsocketsListConnectionsUseCase.RegistryData[] = [];\n\n try {\n const where = params?.where || {};\n if (where.identityId) {\n connections = await this.registry.listViaIdentity(where.identityId);\n } else if (where.connections) {\n connections = await this.registry.listViaConnections(where.connections);\n } else if (where.tenant) {\n connections = await this.registry.listViaTenant(where.tenant);\n } else {\n connections = await this.registry.listAll();\n }\n } catch (error) {\n return Result.fail(new WebsocketServiceError(error as Error));\n }\n\n const threeHoursAgo = new Date(Date.now() - 3 * 60 * 60 * 1000).toISOString();\n connections = connections.filter(c => c.connectedOn >= threeHoursAgo);\n\n return Result.ok(connections);\n }\n}\n\nexport const ListConnectionsUseCase = WebsocketsListConnectionsUseCase.createImplementation({\n implementation: ListConnectionsUseCaseImpl,\n dependencies: [ConnectionRegistry]\n});\n"],"names":["ListConnectionsUseCaseImpl","registry","params","connections","where","error","Result","WebsocketServiceError","threeHoursAgo","Date","c","ListConnectionsUseCase","WebsocketsListConnectionsUseCase","ConnectionRegistry"],"mappings":";;;;AAMA,MAAMA;IACF,YAAoCC,QAAsC,CAAE;aAAxCA,QAAQ,GAARA;IAAyC;IAE7E,MAAa,QACTC,MAAgD,EACiC;QACjF,IAAIC,cAA+D,EAAE;QAErE,IAAI;YACA,MAAMC,QAAQF,QAAQ,SAAS,CAAC;YAE5BC,cADAC,MAAM,UAAU,GACF,MAAM,IAAI,CAAC,QAAQ,CAAC,eAAe,CAACA,MAAM,UAAU,IAC3DA,MAAM,WAAW,GACV,MAAM,IAAI,CAAC,QAAQ,CAAC,kBAAkB,CAACA,MAAM,WAAW,IAC/DA,MAAM,MAAM,GACL,MAAM,IAAI,CAAC,QAAQ,CAAC,aAAa,CAACA,MAAM,MAAM,IAE9C,MAAM,IAAI,CAAC,QAAQ,CAAC,OAAO;QAEjD,EAAE,OAAOC,OAAO;YACZ,OAAOC,OAAO,IAAI,CAAC,IAAIC,sBAAsBF;QACjD;QAEA,MAAMG,gBAAgB,IAAIC,KAAKA,KAAK,GAAG,KAAK,UAAoB,WAAW;QAC3EN,cAAcA,YAAY,MAAM,CAACO,CAAAA,IAAKA,EAAE,WAAW,IAAIF;QAEvD,OAAOF,OAAO,EAAE,CAACH;IACrB;AACJ;AAEO,MAAMQ,yBAAyBC,iCAAiC,oBAAoB,CAAC;IACxF,gBAAgBZ;IAChB,cAAc;QAACa;KAAmB;AACtC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Result } from "@webiny/feature/api";
|
|
2
2
|
import { WebsocketsTransport } from "../../transport/index.js";
|
|
3
3
|
import { WebsocketsSendToConnectionsUseCase } from "./abstractions.js";
|
|
4
|
-
import { WebsocketServiceError } from "
|
|
4
|
+
import { WebsocketServiceError } from "@webiny/api-core/features/websockets/index.js";
|
|
5
5
|
class SendToConnectionsUseCaseImpl {
|
|
6
6
|
constructor(transport){
|
|
7
7
|
this.transport = transport;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"features/SendToConnections/SendToConnectionsUseCase.js","sources":["../../../src/features/SendToConnections/SendToConnectionsUseCase.ts"],"sourcesContent":["import { Result } from \"@webiny/feature/api\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\nimport { WebsocketsTransport } from \"~/transport/index.js\";\nimport { WebsocketsSendToConnectionsUseCase } from \"./abstractions.js\";\nimport type { WebsocketsError } from \"~/features/shared/errors.js\";\nimport { WebsocketServiceError } from \"
|
|
1
|
+
{"version":3,"file":"features/SendToConnections/SendToConnectionsUseCase.js","sources":["../../../src/features/SendToConnections/SendToConnectionsUseCase.ts"],"sourcesContent":["import { Result } from \"@webiny/feature/api\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\nimport { WebsocketsTransport } from \"~/transport/index.js\";\nimport { WebsocketsSendToConnectionsUseCase } from \"./abstractions.js\";\nimport type { WebsocketsError } from \"~/features/shared/errors.js\";\nimport { WebsocketServiceError } from \"@webiny/api-core/features/websockets/index.js\";\n\nclass SendToConnectionsUseCaseImpl implements WebsocketsSendToConnectionsUseCase.Interface {\n public constructor(private readonly transport: WebsocketsTransport.Interface) {}\n\n public async execute<T extends GenericRecord = GenericRecord>(\n connections: WebsocketsSendToConnectionsUseCase.Connection[],\n data: WebsocketsSendToConnectionsUseCase.Data<T>\n ): Promise<Result<void, WebsocketsError>> {\n try {\n await this.transport.send<T>(connections, data);\n } catch (error) {\n return Result.fail(new WebsocketServiceError(error as Error));\n }\n\n return Result.ok();\n }\n}\n\nexport const SendToConnectionsUseCase = WebsocketsSendToConnectionsUseCase.createImplementation({\n implementation: SendToConnectionsUseCaseImpl,\n dependencies: [WebsocketsTransport]\n});\n"],"names":["SendToConnectionsUseCaseImpl","transport","connections","data","error","Result","WebsocketServiceError","SendToConnectionsUseCase","WebsocketsSendToConnectionsUseCase","WebsocketsTransport"],"mappings":";;;;AAOA,MAAMA;IACF,YAAoCC,SAAwC,CAAE;aAA1CA,SAAS,GAATA;IAA2C;IAE/E,MAAa,QACTC,WAA4D,EAC5DC,IAAgD,EACV;QACtC,IAAI;YACA,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAID,aAAaC;QAC9C,EAAE,OAAOC,OAAO;YACZ,OAAOC,OAAO,IAAI,CAAC,IAAIC,sBAAsBF;QACjD;QAEA,OAAOC,OAAO,EAAE;IACpB;AACJ;AAEO,MAAME,2BAA2BC,mCAAmC,oBAAoB,CAAC;IAC5F,gBAAgBR;IAChB,cAAc;QAACS;KAAoB;AACvC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Result } from "@webiny/feature/api";
|
|
2
2
|
import type { GenericRecord } from "@webiny/api/types.js";
|
|
3
3
|
import type { IWebsocketsTransportSendConnection } from "../../transport/index.js";
|
|
4
|
-
import type { IWebsocketsTransportSendData } from "
|
|
4
|
+
import type { IWebsocketsTransportSendData } from "@webiny/api-core/features/websockets/index.js";
|
|
5
5
|
import type { WebsocketsError } from "../../features/shared/errors.js";
|
|
6
6
|
export interface ISendToConnectionsUseCase {
|
|
7
7
|
execute<T extends GenericRecord = GenericRecord>(connections: IWebsocketsTransportSendConnection[], data: IWebsocketsTransportSendData<T>): Promise<Result<void, WebsocketsError>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"features/SendToConnections/abstractions.js","sources":["../../../src/features/SendToConnections/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { Result } from \"@webiny/feature/api\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\nimport type { IWebsocketsTransportSendConnection } from \"~/transport/index.js\";\nimport type { IWebsocketsTransportSendData } from \"
|
|
1
|
+
{"version":3,"file":"features/SendToConnections/abstractions.js","sources":["../../../src/features/SendToConnections/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { Result } from \"@webiny/feature/api\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\nimport type { IWebsocketsTransportSendConnection } from \"~/transport/index.js\";\nimport type { IWebsocketsTransportSendData } from \"@webiny/api-core/features/websockets/index.js\";\nimport type { WebsocketsError } from \"~/features/shared/errors.js\";\n\nexport interface ISendToConnectionsUseCase {\n execute<T extends GenericRecord = GenericRecord>(\n connections: IWebsocketsTransportSendConnection[],\n data: IWebsocketsTransportSendData<T>\n ): Promise<Result<void, WebsocketsError>>;\n}\n\nexport const WebsocketsSendToConnectionsUseCase = createAbstraction<ISendToConnectionsUseCase>(\n \"Websockets/SendToConnections\"\n);\n\nexport namespace WebsocketsSendToConnectionsUseCase {\n export type Interface = ISendToConnectionsUseCase;\n export type Connection = IWebsocketsTransportSendConnection;\n export type Data<T extends GenericRecord> = IWebsocketsTransportSendData<T>;\n}\n"],"names":["WebsocketsSendToConnectionsUseCase","createAbstraction"],"mappings":";AAcO,MAAMA,qCAAqCC,kBAC9C"}
|
|
@@ -3,16 +3,16 @@ import type { GenericRecord } from "@webiny/api/types.js";
|
|
|
3
3
|
import { WebsocketsListConnectionsUseCase } from "../../features/ListConnections/abstractions.js";
|
|
4
4
|
import { WebsocketsTransport } from "../../transport/index.js";
|
|
5
5
|
import { Logger } from "@webiny/api-core/features/logger/index.js";
|
|
6
|
-
import { WebsocketsSendToIdentityUseCase } from "
|
|
7
|
-
import
|
|
6
|
+
import { WebsocketsSendToIdentityUseCase } from "@webiny/api-core/features/websockets/index.js";
|
|
7
|
+
import { WebsocketServiceError } from "@webiny/api-core/features/websockets/index.js";
|
|
8
8
|
declare class SendToIdentityUseCaseImpl implements WebsocketsSendToIdentityUseCase.Interface {
|
|
9
9
|
private readonly listConnections;
|
|
10
10
|
private readonly transport;
|
|
11
11
|
private readonly logger;
|
|
12
12
|
constructor(listConnections: WebsocketsListConnectionsUseCase.Interface, transport: WebsocketsTransport.Interface, logger: Logger.Interface);
|
|
13
|
-
execute<T extends GenericRecord = GenericRecord>(identity: WebsocketsSendToIdentityUseCase.Identity, data: WebsocketsSendToIdentityUseCase.Data<T>): Promise<Result<void,
|
|
13
|
+
execute<T extends GenericRecord = GenericRecord>(identity: WebsocketsSendToIdentityUseCase.Identity, data: WebsocketsSendToIdentityUseCase.Data<T>): Promise<Result<void, WebsocketServiceError>>;
|
|
14
14
|
}
|
|
15
15
|
export declare const SendToIdentityUseCase: typeof SendToIdentityUseCaseImpl & {
|
|
16
|
-
__abstraction: import("@webiny/di").Abstraction<import("
|
|
16
|
+
__abstraction: import("@webiny/di").Abstraction<import("@webiny/api-core/features/websockets/SendToIdentity/abstractions").ISendToIdentityUseCase>;
|
|
17
17
|
};
|
|
18
18
|
export {};
|
|
@@ -2,8 +2,7 @@ import { Result } from "@webiny/feature/api";
|
|
|
2
2
|
import { WebsocketsListConnectionsUseCase } from "../ListConnections/abstractions.js";
|
|
3
3
|
import { WebsocketsTransport } from "../../transport/index.js";
|
|
4
4
|
import { Logger } from "@webiny/api-core/features/logger/index.js";
|
|
5
|
-
import { WebsocketsSendToIdentityUseCase } from "
|
|
6
|
-
import { WebsocketServiceError } from "../shared/errors.js";
|
|
5
|
+
import { WebsocketServiceError, WebsocketsSendToIdentityUseCase } from "@webiny/api-core/features/websockets/index.js";
|
|
7
6
|
class SendToIdentityUseCaseImpl {
|
|
8
7
|
constructor(listConnections, transport, logger){
|
|
9
8
|
this.listConnections = listConnections;
|
|
@@ -16,7 +15,7 @@ class SendToIdentityUseCaseImpl {
|
|
|
16
15
|
identityId: identity.id
|
|
17
16
|
}
|
|
18
17
|
});
|
|
19
|
-
if (result.isFail()) return Result.fail(result.error);
|
|
18
|
+
if (result.isFail()) return Result.fail(new WebsocketServiceError(result.error));
|
|
20
19
|
if (0 === result.value.length) this.logger.warn({
|
|
21
20
|
identityId: identity.id,
|
|
22
21
|
action: data.action
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"features/SendToIdentity/SendToIdentityUseCase.js","sources":["../../../src/features/SendToIdentity/SendToIdentityUseCase.ts"],"sourcesContent":["import { Result } from \"@webiny/feature/api\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\nimport { WebsocketsListConnectionsUseCase } from \"~/features/ListConnections/abstractions.js\";\nimport { WebsocketsTransport } from \"~/transport/index.js\";\nimport { Logger } from \"@webiny/api-core/features/logger/index.js\";\nimport { WebsocketsSendToIdentityUseCase } from \"
|
|
1
|
+
{"version":3,"file":"features/SendToIdentity/SendToIdentityUseCase.js","sources":["../../../src/features/SendToIdentity/SendToIdentityUseCase.ts"],"sourcesContent":["import { Result } from \"@webiny/feature/api\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\nimport { WebsocketsListConnectionsUseCase } from \"~/features/ListConnections/abstractions.js\";\nimport { WebsocketsTransport } from \"~/transport/index.js\";\nimport { Logger } from \"@webiny/api-core/features/logger/index.js\";\nimport { WebsocketsSendToIdentityUseCase } from \"@webiny/api-core/features/websockets/index.js\";\nimport { WebsocketServiceError } from \"@webiny/api-core/features/websockets/index.js\";\n\nclass SendToIdentityUseCaseImpl implements WebsocketsSendToIdentityUseCase.Interface {\n public constructor(\n private readonly listConnections: WebsocketsListConnectionsUseCase.Interface,\n private readonly transport: WebsocketsTransport.Interface,\n private readonly logger: Logger.Interface\n ) {}\n\n public async execute<T extends GenericRecord = GenericRecord>(\n identity: WebsocketsSendToIdentityUseCase.Identity,\n data: WebsocketsSendToIdentityUseCase.Data<T>\n ): Promise<Result<void, WebsocketServiceError>> {\n const result = await this.listConnections.execute({\n where: {\n identityId: identity.id\n }\n });\n\n if (result.isFail()) {\n // The send declares one error, so a listing failure is carried inside it.\n return Result.fail(new WebsocketServiceError(result.error));\n }\n\n if (result.value.length === 0) {\n // Not an error for the caller (the user may simply have no tab open), but without this\n // an identity mismatch between sender and connection is indistinguishable from success.\n this.logger.warn(\n { identityId: identity.id, action: data.action },\n \"No websocket connections found for identity; message not sent.\"\n );\n }\n\n try {\n await this.transport.send<T>(result.value, data);\n } catch (error) {\n return Result.fail(new WebsocketServiceError(error as Error));\n }\n\n return Result.ok();\n }\n}\n\nexport const SendToIdentityUseCase = WebsocketsSendToIdentityUseCase.createImplementation({\n implementation: SendToIdentityUseCaseImpl,\n dependencies: [WebsocketsListConnectionsUseCase, WebsocketsTransport, Logger]\n});\n"],"names":["SendToIdentityUseCaseImpl","listConnections","transport","logger","identity","data","result","Result","WebsocketServiceError","error","SendToIdentityUseCase","WebsocketsSendToIdentityUseCase","WebsocketsListConnectionsUseCase","WebsocketsTransport","Logger"],"mappings":";;;;;AAQA,MAAMA;IACF,YACqBC,eAA2D,EAC3DC,SAAwC,EACxCC,MAAwB,CAC3C;aAHmBF,eAAe,GAAfA;aACAC,SAAS,GAATA;aACAC,MAAM,GAANA;IAClB;IAEH,MAAa,QACTC,QAAkD,EAClDC,IAA6C,EACD;QAC5C,MAAMC,SAAS,MAAM,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC;YAC9C,OAAO;gBACH,YAAYF,SAAS,EAAE;YAC3B;QACJ;QAEA,IAAIE,OAAO,MAAM,IAEb,OAAOC,OAAO,IAAI,CAAC,IAAIC,sBAAsBF,OAAO,KAAK;QAG7D,IAAIA,AAAwB,MAAxBA,OAAO,KAAK,CAAC,MAAM,EAGnB,IAAI,CAAC,MAAM,CAAC,IAAI,CACZ;YAAE,YAAYF,SAAS,EAAE;YAAE,QAAQC,KAAK,MAAM;QAAC,GAC/C;QAIR,IAAI;YACA,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAIC,OAAO,KAAK,EAAED;QAC/C,EAAE,OAAOI,OAAO;YACZ,OAAOF,OAAO,IAAI,CAAC,IAAIC,sBAAsBC;QACjD;QAEA,OAAOF,OAAO,EAAE;IACpB;AACJ;AAEO,MAAMG,wBAAwBC,gCAAgC,oBAAoB,CAAC;IACtF,gBAAgBX;IAChB,cAAc;QAACY;QAAkCC;QAAqBC;KAAO;AACjF"}
|
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
import { BaseError } from "@webiny/feature/api";
|
|
2
|
-
|
|
3
|
-
error: Error;
|
|
4
|
-
}> {
|
|
5
|
-
readonly code: "Websocket/Service";
|
|
6
|
-
constructor(error: Error);
|
|
7
|
-
}
|
|
2
|
+
import type { WebsocketServiceError } from "@webiny/api-core/features/websockets/index.js";
|
|
8
3
|
export declare class WebsocketForceDisconnectNotificationError extends BaseError<{
|
|
9
4
|
error: Error;
|
|
10
5
|
}> {
|
|
@@ -1,14 +1,4 @@
|
|
|
1
1
|
import { BaseError } from "@webiny/feature/api";
|
|
2
|
-
class WebsocketServiceError extends BaseError {
|
|
3
|
-
constructor(error){
|
|
4
|
-
super({
|
|
5
|
-
message: "WebsocketService encountered an error.",
|
|
6
|
-
data: {
|
|
7
|
-
error
|
|
8
|
-
}
|
|
9
|
-
}), this.code = "Websocket/Service";
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
2
|
class WebsocketForceDisconnectNotificationError extends BaseError {
|
|
13
3
|
constructor(error){
|
|
14
4
|
super({
|
|
@@ -29,6 +19,6 @@ class WebsocketForceDisconnectError extends BaseError {
|
|
|
29
19
|
}), this.code = "Websocket/Service/ForceDisconnect";
|
|
30
20
|
}
|
|
31
21
|
}
|
|
32
|
-
export { WebsocketForceDisconnectError, WebsocketForceDisconnectNotificationError
|
|
22
|
+
export { WebsocketForceDisconnectError, WebsocketForceDisconnectNotificationError };
|
|
33
23
|
|
|
34
24
|
//# sourceMappingURL=errors.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"features/shared/errors.js","sources":["../../../src/features/shared/errors.ts"],"sourcesContent":["import { BaseError } from \"@webiny/feature/api\";\
|
|
1
|
+
{"version":3,"file":"features/shared/errors.js","sources":["../../../src/features/shared/errors.ts"],"sourcesContent":["import { BaseError } from \"@webiny/feature/api\";\nimport type { WebsocketServiceError } from \"@webiny/api-core/features/websockets/index.js\";\n\nexport class WebsocketForceDisconnectNotificationError extends BaseError<{ error: Error }> {\n override readonly code = \"Websocket/Service/ForceDisconnectNotification\" as const;\n\n constructor(error: Error) {\n super({\n message: \"Failed to notify the clients about the forced disconnect.\",\n data: { error }\n });\n }\n}\n\nexport class WebsocketForceDisconnectError extends BaseError<{ error: Error }> {\n override readonly code = \"Websocket/Service/ForceDisconnect\" as const;\n\n constructor(error: Error) {\n super({\n message: \"Failed to forcefully disconnect the Websocket clients.\",\n data: { error }\n });\n }\n}\n\nexport type WebsocketsError =\n | WebsocketServiceError\n | WebsocketForceDisconnectNotificationError\n | WebsocketForceDisconnectError;\n"],"names":["WebsocketForceDisconnectNotificationError","BaseError","error","WebsocketForceDisconnectError"],"mappings":";AAGO,MAAMA,kDAAkDC;IAG3D,YAAYC,KAAY,CAAE;QACtB,KAAK,CAAC;YACF,SAAS;YACT,MAAM;gBAAEA;YAAM;QAClB,SANc,IAAI,GAAG;IAOzB;AACJ;AAEO,MAAMC,sCAAsCF;IAG/C,YAAYC,KAAY,CAAE;QACtB,KAAK,CAAC;YACF,SAAS;YACT,MAAM;gBAAEA;YAAM;QAClB,SANc,IAAI,GAAG;IAOzB;AACJ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/api-websockets",
|
|
3
|
-
"version": "6.6.0-alpha.
|
|
3
|
+
"version": "6.6.0-alpha.12",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -16,22 +16,22 @@
|
|
|
16
16
|
},
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@webiny/api": "6.6.0-alpha.
|
|
20
|
-
"@webiny/api-core": "6.6.0-alpha.
|
|
21
|
-
"@webiny/api-graphql": "6.6.0-alpha.
|
|
22
|
-
"@webiny/error": "6.6.0-alpha.
|
|
23
|
-
"@webiny/feature": "6.6.0-alpha.
|
|
24
|
-
"@webiny/handler": "6.6.0-alpha.
|
|
19
|
+
"@webiny/api": "6.6.0-alpha.12",
|
|
20
|
+
"@webiny/api-core": "6.6.0-alpha.12",
|
|
21
|
+
"@webiny/api-graphql": "6.6.0-alpha.12",
|
|
22
|
+
"@webiny/error": "6.6.0-alpha.12",
|
|
23
|
+
"@webiny/feature": "6.6.0-alpha.12",
|
|
24
|
+
"@webiny/handler": "6.6.0-alpha.12",
|
|
25
25
|
"type-fest": "5.10.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@webiny/api-core-testing": "6.6.0-alpha.
|
|
29
|
-
"@webiny/api-graphql": "6.6.0-alpha.
|
|
30
|
-
"@webiny/api-headless-cms": "6.6.0-alpha.
|
|
31
|
-
"@webiny/build-tools": "6.6.0-alpha.
|
|
28
|
+
"@webiny/api-core-testing": "6.6.0-alpha.12",
|
|
29
|
+
"@webiny/api-graphql": "6.6.0-alpha.12",
|
|
30
|
+
"@webiny/api-headless-cms": "6.6.0-alpha.12",
|
|
31
|
+
"@webiny/build-tools": "6.6.0-alpha.12",
|
|
32
32
|
"@webiny/di": "2.0.0",
|
|
33
|
-
"@webiny/event-handler-core": "6.6.0-alpha.
|
|
34
|
-
"@webiny/wcp": "6.6.0-alpha.
|
|
33
|
+
"@webiny/event-handler-core": "6.6.0-alpha.12",
|
|
34
|
+
"@webiny/wcp": "6.6.0-alpha.12",
|
|
35
35
|
"graphql": "16.14.2",
|
|
36
36
|
"rimraf": "6.1.3",
|
|
37
37
|
"type-fest": "5.10.0",
|
|
@@ -66,7 +66,8 @@ class WebsocketsRunner {
|
|
|
66
66
|
};
|
|
67
67
|
const getIdentity = ()=>{
|
|
68
68
|
const identity = this.context.container.resolve(IdentityContext).getIdentity();
|
|
69
|
-
|
|
69
|
+
if (identity.isAnonymous()) return null;
|
|
70
|
+
return identity;
|
|
70
71
|
};
|
|
71
72
|
const action = middleware(handlers.map((handler)=>async (params, next)=>handler.run({
|
|
72
73
|
registry: params.registry,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runner/WebsocketsRunner.js","sources":["../../src/runner/WebsocketsRunner.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport type {\n IWebsocketsEvent,\n IWebsocketsEventContext,\n IWebsocketsEventData,\n WebsocketsRoute,\n WebsocketsEventType,\n Context\n} from \"~/types.js\";\nimport { ConnectionRegistry } from \"~/features/ConnectionRegistry/abstractions.js\";\nimport type { IWebsocketsRunner } from \"./abstractions/WebsocketsRunner.js\";\nimport type { IWebsocketsRunnerResponse } from \"./abstractions/WebsocketsRunner.js\";\nimport { WebsocketsRouteHandler } from \"~/features/Routes/abstractions.js\";\nimport { middleware } from \"~/utils/middleware.js\";\nimport { WebsocketsResponse } from \"~/response/index.js\";\nimport { WebsocketsTransport } from \"~/transport/index.js\";\nimport { WebsocketsSendToConnectionsUseCase } from \"~/features/SendToConnections/abstractions.js\";\nimport { TenantContext } from \"@webiny/api-core/features/tenancy/TenantContext/index.js\";\nimport { IdentityContext } from \"@webiny/api-core/features/security/IdentityContext/abstractions.js\";\n\ntype MiddlewareParams = Pick<WebsocketsRouteHandler.Params, \"event\" | \"registry\" | \"container\">;\n\ninterface IWebsocketsRunnerRespondParams extends Pick<\n IWebsocketsEventContext,\n \"connectionId\" | \"endpoint\" | \"eventType\"\n> {\n messageId?: string;\n result: WebsocketsResponse.OkResult | WebsocketsResponse.ErrorResult;\n}\n\nexport class WebsocketsRunner implements IWebsocketsRunner {\n private readonly context: Context;\n private readonly registry: ConnectionRegistry.Interface;\n private readonly response: WebsocketsResponse.Interface;\n private readonly sendToConnections: WebsocketsSendToConnectionsUseCase.Interface;\n\n public constructor(context: Context, response: WebsocketsResponse.Interface) {\n this.context = context;\n this.registry = context.container.resolve(ConnectionRegistry);\n this.response = response;\n this.sendToConnections = context.container.resolve(WebsocketsSendToConnectionsUseCase);\n }\n\n public async run<T extends IWebsocketsEventData = IWebsocketsEventData>(\n event: IWebsocketsEvent<T>\n ): Promise<IWebsocketsRunnerResponse> {\n let result: WebsocketsResponse.OkResult | WebsocketsResponse.ErrorResult;\n try {\n result = await this.executeRoute(event);\n } catch (ex) {\n result = this.response.error({\n message: `Route \"${event.context.route}\" action failed.`,\n error: {\n message: ex.message,\n code: ex.code,\n data: ex.data,\n stack: ex.stack\n }\n });\n }\n try {\n await this.respond({\n connectionId: event.context.connectionId,\n endpoint: event.context.endpoint,\n eventType: event.context.eventType,\n messageId: event.body?.messageId,\n result\n });\n return result;\n } catch (ex) {\n return this.response.error({\n message: \"Failed to respond to the request.\",\n error: {\n message: ex.message,\n code: ex.code,\n data: {\n ...ex.data,\n result\n },\n stack: ex.stack\n }\n });\n }\n }\n\n private getRouteHandlers(route: WebsocketsRoute | string): WebsocketsRouteHandler.Interface[] {\n const handlers = this.context.container\n .resolveAll(WebsocketsRouteHandler)\n .filter(handler => handler.route === route);\n if (handlers.length === 0) {\n throw new WebinyError(\n `There are no handlers for the route: ${route}.`,\n \"NO_ROUTE_HANDLERS\",\n {\n route\n }\n );\n }\n return handlers;\n }\n\n private async executeRoute(event: IWebsocketsEvent): Promise<IWebsocketsRunnerResponse> {\n const handlers = this.getRouteHandlers(event.context.route).reverse();\n\n const getTenant = () => {\n const tenant = this.context.container.resolve(TenantContext).getTenant();\n return tenant?.id || null;\n };\n\n const getIdentity = (): ConnectionRegistry.Identity | null => {\n const identity = this.context.container.resolve(IdentityContext).getIdentity();\n return identity || null;\n };\n\n const action = middleware<MiddlewareParams, IWebsocketsRunnerResponse>(\n handlers.map(handler => {\n return async (params, next) => {\n return handler.run({\n registry: params.registry,\n event: params.event,\n container: params.container,\n getTenant,\n getIdentity,\n response: this.response,\n next\n });\n };\n })\n );\n\n const result = await action({\n event,\n registry: this.registry,\n container: this.context.container\n });\n if (result) {\n return result;\n }\n const message = \"No response from the route action.\";\n return this.response.error({\n message,\n error: {\n message,\n code: \"NO_RESPONSE\"\n },\n statusCode: 404\n });\n }\n\n private async respond(params: IWebsocketsRunnerRespondParams): Promise<void> {\n const { connectionId, endpoint, eventType, result, messageId } = params;\n if (eventType !== \"message\") {\n return;\n } else if (!connectionId || !endpoint) {\n const message = \"No connectionId or endpoint.\";\n const data = {\n connectionId,\n endpoint\n };\n console.error(message, JSON.stringify(data));\n throw new WebinyError(message, \"GENERAL_ERROR\", data);\n }\n const connection: WebsocketsTransport.SendConnection = {\n connectionId,\n endpoint\n };\n\n const dataToSend = {\n ...result,\n messageId\n };\n await this.sendToConnections.execute([connection], dataToSend);\n }\n}\n\nexport namespace WebsocketsRunner {\n export type Event<T extends IWebsocketsEventData = IWebsocketsEventData> = IWebsocketsEvent<T>;\n export type EventData = IWebsocketsEventData;\n export type EventContext = IWebsocketsEventContext;\n export type EventType = WebsocketsEventType;\n export type Route = WebsocketsRoute;\n export type Response = IWebsocketsRunnerResponse;\n}\n"],"names":["WebsocketsRunner","context","response","ConnectionRegistry","WebsocketsSendToConnectionsUseCase","event","result","ex","route","handlers","WebsocketsRouteHandler","handler","WebinyError","getTenant","tenant","TenantContext","getIdentity","identity","IdentityContext","action","middleware","params","next","message","connectionId","endpoint","eventType","messageId","data","console","JSON","connection","dataToSend"],"mappings":"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"}
|
|
1
|
+
{"version":3,"file":"runner/WebsocketsRunner.js","sources":["../../src/runner/WebsocketsRunner.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport type {\n IWebsocketsEvent,\n IWebsocketsEventContext,\n IWebsocketsEventData,\n WebsocketsRoute,\n WebsocketsEventType,\n Context\n} from \"~/types.js\";\nimport { ConnectionRegistry } from \"~/features/ConnectionRegistry/abstractions.js\";\nimport type { IWebsocketsRunner } from \"./abstractions/WebsocketsRunner.js\";\nimport type { IWebsocketsRunnerResponse } from \"./abstractions/WebsocketsRunner.js\";\nimport { WebsocketsRouteHandler } from \"~/features/Routes/abstractions.js\";\nimport { middleware } from \"~/utils/middleware.js\";\nimport { WebsocketsResponse } from \"~/response/index.js\";\nimport { WebsocketsTransport } from \"~/transport/index.js\";\nimport { WebsocketsSendToConnectionsUseCase } from \"~/features/SendToConnections/abstractions.js\";\nimport { TenantContext } from \"@webiny/api-core/features/tenancy/TenantContext/index.js\";\nimport { IdentityContext } from \"@webiny/api-core/features/security/IdentityContext/abstractions.js\";\n\ntype MiddlewareParams = Pick<WebsocketsRouteHandler.Params, \"event\" | \"registry\" | \"container\">;\n\ninterface IWebsocketsRunnerRespondParams extends Pick<\n IWebsocketsEventContext,\n \"connectionId\" | \"endpoint\" | \"eventType\"\n> {\n messageId?: string;\n result: WebsocketsResponse.OkResult | WebsocketsResponse.ErrorResult;\n}\n\nexport class WebsocketsRunner implements IWebsocketsRunner {\n private readonly context: Context;\n private readonly registry: ConnectionRegistry.Interface;\n private readonly response: WebsocketsResponse.Interface;\n private readonly sendToConnections: WebsocketsSendToConnectionsUseCase.Interface;\n\n public constructor(context: Context, response: WebsocketsResponse.Interface) {\n this.context = context;\n this.registry = context.container.resolve(ConnectionRegistry);\n this.response = response;\n this.sendToConnections = context.container.resolve(WebsocketsSendToConnectionsUseCase);\n }\n\n public async run<T extends IWebsocketsEventData = IWebsocketsEventData>(\n event: IWebsocketsEvent<T>\n ): Promise<IWebsocketsRunnerResponse> {\n let result: WebsocketsResponse.OkResult | WebsocketsResponse.ErrorResult;\n try {\n result = await this.executeRoute(event);\n } catch (ex) {\n result = this.response.error({\n message: `Route \"${event.context.route}\" action failed.`,\n error: {\n message: ex.message,\n code: ex.code,\n data: ex.data,\n stack: ex.stack\n }\n });\n }\n try {\n await this.respond({\n connectionId: event.context.connectionId,\n endpoint: event.context.endpoint,\n eventType: event.context.eventType,\n messageId: event.body?.messageId,\n result\n });\n return result;\n } catch (ex) {\n return this.response.error({\n message: \"Failed to respond to the request.\",\n error: {\n message: ex.message,\n code: ex.code,\n data: {\n ...ex.data,\n result\n },\n stack: ex.stack\n }\n });\n }\n }\n\n private getRouteHandlers(route: WebsocketsRoute | string): WebsocketsRouteHandler.Interface[] {\n const handlers = this.context.container\n .resolveAll(WebsocketsRouteHandler)\n .filter(handler => handler.route === route);\n if (handlers.length === 0) {\n throw new WebinyError(\n `There are no handlers for the route: ${route}.`,\n \"NO_ROUTE_HANDLERS\",\n {\n route\n }\n );\n }\n return handlers;\n }\n\n private async executeRoute(event: IWebsocketsEvent): Promise<IWebsocketsRunnerResponse> {\n const handlers = this.getRouteHandlers(event.context.route).reverse();\n\n const getTenant = () => {\n const tenant = this.context.container.resolve(TenantContext).getTenant();\n return tenant?.id || null;\n };\n\n const getIdentity = (): ConnectionRegistry.Identity | null => {\n const identity = this.context.container.resolve(IdentityContext).getIdentity();\n // A request without a token gets the anonymous identity, never `undefined`.\n if (identity.isAnonymous()) {\n return null;\n }\n return identity;\n };\n\n const action = middleware<MiddlewareParams, IWebsocketsRunnerResponse>(\n handlers.map(handler => {\n return async (params, next) => {\n return handler.run({\n registry: params.registry,\n event: params.event,\n container: params.container,\n getTenant,\n getIdentity,\n response: this.response,\n next\n });\n };\n })\n );\n\n const result = await action({\n event,\n registry: this.registry,\n container: this.context.container\n });\n if (result) {\n return result;\n }\n const message = \"No response from the route action.\";\n return this.response.error({\n message,\n error: {\n message,\n code: \"NO_RESPONSE\"\n },\n statusCode: 404\n });\n }\n\n private async respond(params: IWebsocketsRunnerRespondParams): Promise<void> {\n const { connectionId, endpoint, eventType, result, messageId } = params;\n if (eventType !== \"message\") {\n return;\n } else if (!connectionId || !endpoint) {\n const message = \"No connectionId or endpoint.\";\n const data = {\n connectionId,\n endpoint\n };\n console.error(message, JSON.stringify(data));\n throw new WebinyError(message, \"GENERAL_ERROR\", data);\n }\n const connection: WebsocketsTransport.SendConnection = {\n connectionId,\n endpoint\n };\n\n const dataToSend = {\n ...result,\n messageId\n };\n await this.sendToConnections.execute([connection], dataToSend);\n }\n}\n\nexport namespace WebsocketsRunner {\n export type Event<T extends IWebsocketsEventData = IWebsocketsEventData> = IWebsocketsEvent<T>;\n export type EventData = IWebsocketsEventData;\n export type EventContext = IWebsocketsEventContext;\n export type EventType = WebsocketsEventType;\n export type Route = WebsocketsRoute;\n export type Response = IWebsocketsRunnerResponse;\n}\n"],"names":["WebsocketsRunner","context","response","ConnectionRegistry","WebsocketsSendToConnectionsUseCase","event","result","ex","route","handlers","WebsocketsRouteHandler","handler","WebinyError","getTenant","tenant","TenantContext","getIdentity","identity","IdentityContext","action","middleware","params","next","message","connectionId","endpoint","eventType","messageId","data","console","JSON","connection","dataToSend"],"mappings":";;;;;;;AA8BO,MAAMA;IAMT,YAAmBC,OAAgB,EAAEC,QAAsC,CAAE;QACzE,IAAI,CAAC,OAAO,GAAGD;QACf,IAAI,CAAC,QAAQ,GAAGA,QAAQ,SAAS,CAAC,OAAO,CAACE;QAC1C,IAAI,CAAC,QAAQ,GAAGD;QAChB,IAAI,CAAC,iBAAiB,GAAGD,QAAQ,SAAS,CAAC,OAAO,CAACG;IACvD;IAEA,MAAa,IACTC,KAA0B,EACQ;QAClC,IAAIC;QACJ,IAAI;YACAA,SAAS,MAAM,IAAI,CAAC,YAAY,CAACD;QACrC,EAAE,OAAOE,IAAI;YACTD,SAAS,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC;gBACzB,SAAS,CAAC,OAAO,EAAED,MAAM,OAAO,CAAC,KAAK,CAAC,gBAAgB,CAAC;gBACxD,OAAO;oBACH,SAASE,GAAG,OAAO;oBACnB,MAAMA,GAAG,IAAI;oBACb,MAAMA,GAAG,IAAI;oBACb,OAAOA,GAAG,KAAK;gBACnB;YACJ;QACJ;QACA,IAAI;YACA,MAAM,IAAI,CAAC,OAAO,CAAC;gBACf,cAAcF,MAAM,OAAO,CAAC,YAAY;gBACxC,UAAUA,MAAM,OAAO,CAAC,QAAQ;gBAChC,WAAWA,MAAM,OAAO,CAAC,SAAS;gBAClC,WAAWA,MAAM,IAAI,EAAE;gBACvBC;YACJ;YACA,OAAOA;QACX,EAAE,OAAOC,IAAI;YACT,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC;gBACvB,SAAS;gBACT,OAAO;oBACH,SAASA,GAAG,OAAO;oBACnB,MAAMA,GAAG,IAAI;oBACb,MAAM;wBACF,GAAGA,GAAG,IAAI;wBACVD;oBACJ;oBACA,OAAOC,GAAG,KAAK;gBACnB;YACJ;QACJ;IACJ;IAEQ,iBAAiBC,KAA+B,EAAsC;QAC1F,MAAMC,WAAW,IAAI,CAAC,OAAO,CAAC,SAAS,CAClC,UAAU,CAACC,wBACX,MAAM,CAACC,CAAAA,UAAWA,QAAQ,KAAK,KAAKH;QACzC,IAAIC,AAAoB,MAApBA,SAAS,MAAM,EACf,MAAM,IAAIG,MACN,CAAC,qCAAqC,EAAEJ,MAAM,CAAC,CAAC,EAChD,qBACA;YACIA;QACJ;QAGR,OAAOC;IACX;IAEA,MAAc,aAAaJ,KAAuB,EAAsC;QACpF,MAAMI,WAAW,IAAI,CAAC,gBAAgB,CAACJ,MAAM,OAAO,CAAC,KAAK,EAAE,OAAO;QAEnE,MAAMQ,YAAY;YACd,MAAMC,SAAS,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,OAAO,CAACC,eAAe,SAAS;YACtE,OAAOD,QAAQ,MAAM;QACzB;QAEA,MAAME,cAAc;YAChB,MAAMC,WAAW,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,OAAO,CAACC,iBAAiB,WAAW;YAE5E,IAAID,SAAS,WAAW,IACpB,OAAO;YAEX,OAAOA;QACX;QAEA,MAAME,SAASC,WACXX,SAAS,GAAG,CAACE,CAAAA,UACF,OAAOU,QAAQC,OACXX,QAAQ,GAAG,CAAC;oBACf,UAAUU,OAAO,QAAQ;oBACzB,OAAOA,OAAO,KAAK;oBACnB,WAAWA,OAAO,SAAS;oBAC3BR;oBACAG;oBACA,UAAU,IAAI,CAAC,QAAQ;oBACvBM;gBACJ;QAKZ,MAAMhB,SAAS,MAAMa,OAAO;YACxBd;YACA,UAAU,IAAI,CAAC,QAAQ;YACvB,WAAW,IAAI,CAAC,OAAO,CAAC,SAAS;QACrC;QACA,IAAIC,QACA,OAAOA;QAEX,MAAMiB,UAAU;QAChB,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC;YACvBA;YACA,OAAO;gBACHA;gBACA,MAAM;YACV;YACA,YAAY;QAChB;IACJ;IAEA,MAAc,QAAQF,MAAsC,EAAiB;QACzE,MAAM,EAAEG,YAAY,EAAEC,QAAQ,EAAEC,SAAS,EAAEpB,MAAM,EAAEqB,SAAS,EAAE,GAAGN;QACjE,IAAIK,AAAc,cAAdA,WACA;aACG,IAAI,CAACF,gBAAgB,CAACC,UAAU;YACnC,MAAMF,UAAU;YAChB,MAAMK,OAAO;gBACTJ;gBACAC;YACJ;YACAI,QAAQ,KAAK,CAACN,SAASO,KAAK,SAAS,CAACF;YACtC,MAAM,IAAIhB,MAAYW,SAAS,iBAAiBK;QACpD;QACA,MAAMG,aAAiD;YACnDP;YACAC;QACJ;QAEA,MAAMO,aAAa;YACf,GAAG1B,MAAM;YACTqB;QACJ;QACA,MAAM,IAAI,CAAC,iBAAiB,CAAC,OAAO,CAAC;YAACI;SAAW,EAAEC;IACvD;AACJ"}
|
package/runner/routes/connect.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CanAccessCurrentTenantUseCase } from "@webiny/api-core/features/security/CanAccessCurrentTenant/index.js";
|
|
1
2
|
const CONNECT = "connect";
|
|
2
3
|
const getConnectedOn = (connectedAt)=>{
|
|
3
4
|
if (!connectedAt) return new Date().toISOString();
|
|
@@ -5,26 +6,44 @@ const getConnectedOn = (connectedAt)=>{
|
|
|
5
6
|
};
|
|
6
7
|
const websocketsRouteConnect = {
|
|
7
8
|
route: CONNECT,
|
|
8
|
-
async run ({ registry, event, response, getTenant, getIdentity }) {
|
|
9
|
+
async run ({ registry, event, container, response, getTenant, getIdentity }) {
|
|
9
10
|
const tenant = getTenant();
|
|
10
11
|
const identity = getIdentity();
|
|
11
12
|
if (!tenant) return response.error({
|
|
13
|
+
statusCode: 400,
|
|
12
14
|
message: "Missing tenant."
|
|
13
15
|
});
|
|
14
16
|
else if (!identity) return response.error({
|
|
17
|
+
statusCode: 401,
|
|
15
18
|
message: "Missing identity."
|
|
16
19
|
});
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
try {
|
|
21
|
+
const canAccessTenant = await container.resolve(CanAccessCurrentTenantUseCase).execute();
|
|
22
|
+
if (!canAccessTenant) return response.error({
|
|
23
|
+
statusCode: 403,
|
|
24
|
+
message: "No access to this tenant."
|
|
25
|
+
});
|
|
26
|
+
await registry.register({
|
|
27
|
+
identity: {
|
|
28
|
+
id: identity.id,
|
|
29
|
+
displayName: identity.displayName,
|
|
30
|
+
type: identity.type
|
|
31
|
+
},
|
|
32
|
+
connectionId: event.context.connectionId,
|
|
33
|
+
tenant,
|
|
34
|
+
endpoint: event.context.endpoint,
|
|
35
|
+
connectedOn: getConnectedOn(event.context.connectedAt)
|
|
36
|
+
});
|
|
37
|
+
} catch (ex) {
|
|
38
|
+
return response.error({
|
|
39
|
+
statusCode: 500,
|
|
40
|
+
message: "Could not accept the connection.",
|
|
41
|
+
error: {
|
|
42
|
+
message: ex.message,
|
|
43
|
+
code: ex.code
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
28
47
|
return response.ok();
|
|
29
48
|
}
|
|
30
49
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runner/routes/connect.js","sources":["../../../src/runner/routes/connect.ts"],"sourcesContent":["import type { WebsocketsRoute } from \"~/types.js\";\nimport type { WebsocketsRouteHandler } from \"~/features/Routes/abstractions.js\";\n\nconst CONNECT: WebsocketsRoute = \"connect\";\n\nconst getConnectedOn = (connectedAt?: number) => {\n if (!connectedAt) {\n return new Date().toISOString();\n }\n return new Date(connectedAt).toISOString();\n};\n\nexport const websocketsRouteConnect: WebsocketsRouteHandler.Interface = {\n route: CONNECT,\n async run({ registry, event, response, getTenant, getIdentity }) {\n const tenant = getTenant();\n const identity = getIdentity();\n if (!tenant) {\n return response.error({\n message: \"Missing tenant.\"\n });\n } else if (!identity) {\n return response.error({\n message: \"Missing identity.\"\n });\n }\n\n await registry.register({\n
|
|
1
|
+
{"version":3,"file":"runner/routes/connect.js","sources":["../../../src/runner/routes/connect.ts"],"sourcesContent":["import type { WebsocketsRoute } from \"~/types.js\";\nimport type { WebsocketsRouteHandler } from \"~/features/Routes/abstractions.js\";\nimport { CanAccessCurrentTenantUseCase } from \"@webiny/api-core/features/security/CanAccessCurrentTenant/index.js\";\n\nconst CONNECT: WebsocketsRoute = \"connect\";\n\nconst getConnectedOn = (connectedAt?: number) => {\n if (!connectedAt) {\n return new Date().toISOString();\n }\n return new Date(connectedAt).toISOString();\n};\n\nexport const websocketsRouteConnect: WebsocketsRouteHandler.Interface = {\n route: CONNECT,\n async run({ registry, event, container, response, getTenant, getIdentity }) {\n const tenant = getTenant();\n const identity = getIdentity();\n // On `$connect`, API Gateway accepts the socket on any 2xx, so every refusal needs a status.\n if (!tenant) {\n return response.error({\n statusCode: 400,\n message: \"Missing tenant.\"\n });\n } else if (!identity) {\n return response.error({\n statusCode: 401,\n message: \"Missing identity.\"\n });\n }\n\n /*\n * Caught here rather than left to the runner, which answers a thrown error with a 200 and\n * would open the socket.\n */\n try {\n /*\n * The tenant is whatever the client names, so a valid token alone would let a user of one\n * tenant receive another tenant's messages.\n */\n const canAccessTenant = await container\n .resolve(CanAccessCurrentTenantUseCase)\n .execute();\n if (!canAccessTenant) {\n return response.error({\n statusCode: 403,\n message: \"No access to this tenant.\"\n });\n }\n\n await registry.register({\n identity: {\n id: identity.id,\n displayName: identity.displayName,\n type: identity.type\n },\n connectionId: event.context.connectionId,\n tenant,\n endpoint: event.context.endpoint,\n connectedOn: getConnectedOn(event.context.connectedAt)\n });\n } catch (ex) {\n return response.error({\n statusCode: 500,\n message: \"Could not accept the connection.\",\n error: {\n message: ex.message,\n code: ex.code\n }\n });\n }\n\n return response.ok();\n }\n};\n"],"names":["CONNECT","getConnectedOn","connectedAt","Date","websocketsRouteConnect","registry","event","container","response","getTenant","getIdentity","tenant","identity","canAccessTenant","CanAccessCurrentTenantUseCase","ex"],"mappings":";AAIA,MAAMA,UAA2B;AAEjC,MAAMC,iBAAiB,CAACC;IACpB,IAAI,CAACA,aACD,OAAO,IAAIC,OAAO,WAAW;IAEjC,OAAO,IAAIA,KAAKD,aAAa,WAAW;AAC5C;AAEO,MAAME,yBAA2D;IACpE,OAAOJ;IACP,MAAM,KAAI,EAAEK,QAAQ,EAAEC,KAAK,EAAEC,SAAS,EAAEC,QAAQ,EAAEC,SAAS,EAAEC,WAAW,EAAE;QACtE,MAAMC,SAASF;QACf,MAAMG,WAAWF;QAEjB,IAAI,CAACC,QACD,OAAOH,SAAS,KAAK,CAAC;YAClB,YAAY;YACZ,SAAS;QACb;aACG,IAAI,CAACI,UACR,OAAOJ,SAAS,KAAK,CAAC;YAClB,YAAY;YACZ,SAAS;QACb;QAOJ,IAAI;YAKA,MAAMK,kBAAkB,MAAMN,UACzB,OAAO,CAACO,+BACR,OAAO;YACZ,IAAI,CAACD,iBACD,OAAOL,SAAS,KAAK,CAAC;gBAClB,YAAY;gBACZ,SAAS;YACb;YAGJ,MAAMH,SAAS,QAAQ,CAAC;gBACpB,UAAU;oBACN,IAAIO,SAAS,EAAE;oBACf,aAAaA,SAAS,WAAW;oBACjC,MAAMA,SAAS,IAAI;gBACvB;gBACA,cAAcN,MAAM,OAAO,CAAC,YAAY;gBACxCK;gBACA,UAAUL,MAAM,OAAO,CAAC,QAAQ;gBAChC,aAAaL,eAAeK,MAAM,OAAO,CAAC,WAAW;YACzD;QACJ,EAAE,OAAOS,IAAI;YACT,OAAOP,SAAS,KAAK,CAAC;gBAClB,YAAY;gBACZ,SAAS;gBACT,OAAO;oBACH,SAASO,GAAG,OAAO;oBACnB,MAAMA,GAAG,IAAI;gBACjB;YACJ;QACJ;QAEA,OAAOP,SAAS,EAAE;IACtB;AACJ"}
|
|
@@ -1,16 +1,5 @@
|
|
|
1
1
|
import type { GenericRecord } from "@webiny/api/types.js";
|
|
2
|
-
|
|
3
|
-
message: string;
|
|
4
|
-
code: string;
|
|
5
|
-
data?: GenericRecord;
|
|
6
|
-
stack?: string;
|
|
7
|
-
}
|
|
8
|
-
export interface IWebsocketsTransportSendData<T extends GenericRecord> {
|
|
9
|
-
messageId?: string;
|
|
10
|
-
action?: string;
|
|
11
|
-
data?: T;
|
|
12
|
-
error?: IWebsocketsTransportSendDataError;
|
|
13
|
-
}
|
|
2
|
+
import type { IWebsocketsTransportSendData } from "@webiny/api-core/features/websockets/index.js";
|
|
14
3
|
export interface IWebsocketsTransportSendConnection {
|
|
15
4
|
connectionId: string;
|
|
16
5
|
endpoint: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transport/abstractions/WebsocketsTransport.js","sources":["../../../src/transport/abstractions/WebsocketsTransport.ts"],"sourcesContent":["import type { GenericRecord } from \"@webiny/api/types.js\";\nimport { createAbstraction } from \"@webiny/feature/api\";\
|
|
1
|
+
{"version":3,"file":"transport/abstractions/WebsocketsTransport.js","sources":["../../../src/transport/abstractions/WebsocketsTransport.ts"],"sourcesContent":["import type { GenericRecord } from \"@webiny/api/types.js\";\nimport { createAbstraction } from \"@webiny/feature/api\";\nimport type { IWebsocketsTransportSendData } from \"@webiny/api-core/features/websockets/index.js\";\n\nexport interface IWebsocketsTransportSendConnection {\n connectionId: string;\n endpoint: string;\n}\n\nexport interface IWebsocketsTransportDisconnectConnection {\n connectionId: string;\n endpoint: string;\n}\n\nexport interface IWebsocketsTransport {\n send<T extends GenericRecord = GenericRecord>(\n connections: IWebsocketsTransportSendConnection[],\n data: IWebsocketsTransportSendData<T>\n ): Promise<void>;\n\n disconnect(connections: IWebsocketsTransportDisconnectConnection[]): Promise<void>;\n}\n\nexport const WebsocketsTransport = createAbstraction<IWebsocketsTransport>(\"WebsocketsTransport\");\n\nexport namespace WebsocketsTransport {\n export type Interface = IWebsocketsTransport;\n export type SendConnection = IWebsocketsTransportSendConnection;\n export type SendData<T extends GenericRecord> = IWebsocketsTransportSendData<T>;\n export type DisconnectConnection = IWebsocketsTransportDisconnectConnection;\n}\n"],"names":["WebsocketsTransport","createAbstraction"],"mappings":";AAuBO,MAAMA,sBAAsBC,kBAAwC"}
|
package/transport/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { WebsocketsTransport, type IWebsocketsTransport, type IWebsocketsTransportSendConnection, type
|
|
1
|
+
export { WebsocketsTransport, type IWebsocketsTransport, type IWebsocketsTransportSendConnection, type IWebsocketsTransportDisconnectConnection } from "./abstractions/WebsocketsTransport.js";
|
package/types.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import type { ApiCoreContext } from "@webiny/api-core/types/core.js";
|
|
2
2
|
import type { SecurityPermission } from "@webiny/api-core/types/security.js";
|
|
3
|
-
import type { SecurityIdentity } from "@webiny/api-core/types/security.js";
|
|
4
3
|
import type { GenericRecord } from "@webiny/api/types.js";
|
|
5
|
-
export type IWebsocketsIdentity = Pick<SecurityIdentity, "id" | "displayName" | "type">;
|
|
6
4
|
export interface Context extends ApiCoreContext {
|
|
7
5
|
}
|
|
8
6
|
export interface WebsocketsPermission extends SecurityPermission {
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { Result } from "@webiny/feature/api";
|
|
2
|
-
import type { GenericRecord } from "@webiny/api/types.js";
|
|
3
|
-
import type { IWebsocketsIdentity } from "../../types.js";
|
|
4
|
-
import type { IWebsocketsTransportSendData } from "../../transport/index.js";
|
|
5
|
-
import type { WebsocketsError } from "../../features/shared/errors.js";
|
|
6
|
-
export interface ISendToIdentityUseCase {
|
|
7
|
-
execute<T extends GenericRecord = GenericRecord>(identity: Pick<IWebsocketsIdentity, "id">, data: IWebsocketsTransportSendData<T>): Promise<Result<void, WebsocketsError>>;
|
|
8
|
-
}
|
|
9
|
-
export declare const WebsocketsSendToIdentityUseCase: import("@webiny/di").Abstraction<ISendToIdentityUseCase>;
|
|
10
|
-
export declare namespace WebsocketsSendToIdentityUseCase {
|
|
11
|
-
type Interface = ISendToIdentityUseCase;
|
|
12
|
-
type Identity = Pick<IWebsocketsIdentity, "id">;
|
|
13
|
-
type Data<T extends GenericRecord> = IWebsocketsTransportSendData<T>;
|
|
14
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"features/SendToIdentity/abstractions.js","sources":["../../../src/features/SendToIdentity/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { Result } from \"@webiny/feature/api\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\nimport type { IWebsocketsIdentity } from \"~/types.js\";\nimport type { IWebsocketsTransportSendData } from \"~/transport/index.js\";\nimport type { WebsocketsError } from \"~/features/shared/errors.js\";\n\nexport interface ISendToIdentityUseCase {\n execute<T extends GenericRecord = GenericRecord>(\n identity: Pick<IWebsocketsIdentity, \"id\">,\n data: IWebsocketsTransportSendData<T>\n ): Promise<Result<void, WebsocketsError>>;\n}\n\nexport const WebsocketsSendToIdentityUseCase = createAbstraction<ISendToIdentityUseCase>(\n \"Websockets/SendToIdentity\"\n);\n\nexport namespace WebsocketsSendToIdentityUseCase {\n export type Interface = ISendToIdentityUseCase;\n export type Identity = Pick<IWebsocketsIdentity, \"id\">;\n export type Data<T extends GenericRecord> = IWebsocketsTransportSendData<T>;\n}\n"],"names":["WebsocketsSendToIdentityUseCase","createAbstraction"],"mappings":";AAcO,MAAMA,kCAAkCC,kBAC3C"}
|