@webiny/api-websockets 6.0.0-beta.0 → 6.0.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/README.md +10 -9
  2. package/context/WebsocketsContext.d.ts +10 -8
  3. package/context/WebsocketsContext.js +44 -33
  4. package/context/WebsocketsContext.js.map +1 -1
  5. package/context/abstractions/IWebsocketsContext.d.ts +12 -11
  6. package/context/abstractions/IWebsocketsContext.js +1 -5
  7. package/context/abstractions/IWebsocketsContext.js.map +1 -1
  8. package/context/index.d.ts +2 -3
  9. package/context/index.js +11 -42
  10. package/context/index.js.map +1 -1
  11. package/features/WebsocketService/abstractions.d.ts +9 -0
  12. package/features/WebsocketService/abstractions.js +4 -0
  13. package/features/WebsocketService/abstractions.js.map +1 -0
  14. package/features/WebsocketService/errors.d.ts +19 -0
  15. package/features/WebsocketService/errors.js +36 -0
  16. package/features/WebsocketService/errors.js.map +1 -0
  17. package/features/WebsocketService/index.d.ts +1 -0
  18. package/features/WebsocketService/index.js +3 -0
  19. package/features/WebsocketService/index.js.map +1 -0
  20. package/graphql/checkPermissions.d.ts +1 -1
  21. package/graphql/checkPermissions.js +6 -11
  22. package/graphql/checkPermissions.js.map +1 -1
  23. package/graphql/createResolvers.d.ts +2 -3
  24. package/graphql/createResolvers.js +41 -32
  25. package/graphql/createResolvers.js.map +1 -1
  26. package/graphql/createTypeDefs.js +2 -11
  27. package/graphql/createTypeDefs.js.map +1 -1
  28. package/graphql/index.d.ts +1 -1
  29. package/graphql/index.js +7 -14
  30. package/graphql/index.js.map +1 -1
  31. package/graphql/utils.js +5 -13
  32. package/graphql/utils.js.map +1 -1
  33. package/handler/handler.d.ts +1 -1
  34. package/handler/handler.js +45 -48
  35. package/handler/handler.js.map +1 -1
  36. package/handler/headers.d.ts +1 -2
  37. package/handler/headers.js +1 -13
  38. package/handler/headers.js.map +1 -1
  39. package/handler/register.js +5 -7
  40. package/handler/register.js.map +1 -1
  41. package/handler/types.d.ts +6 -9
  42. package/handler/types.js +2 -8
  43. package/handler/types.js.map +1 -1
  44. package/index.d.ts +9 -9
  45. package/index.js +11 -98
  46. package/index.js.map +1 -1
  47. package/package.json +28 -36
  48. package/plugins/WebsocketsActionPlugin.d.ts +3 -3
  49. package/plugins/WebsocketsActionPlugin.js +3 -11
  50. package/plugins/WebsocketsActionPlugin.js.map +1 -1
  51. package/plugins/WebsocketsRoutePlugin.d.ts +6 -7
  52. package/plugins/WebsocketsRoutePlugin.js +3 -11
  53. package/plugins/WebsocketsRoutePlugin.js.map +1 -1
  54. package/plugins/abstrations/IWebsocketsActionPlugin.d.ts +3 -4
  55. package/plugins/abstrations/IWebsocketsActionPlugin.js +1 -5
  56. package/plugins/abstrations/IWebsocketsActionPlugin.js.map +1 -1
  57. package/plugins/index.d.ts +3 -3
  58. package/plugins/index.js +2 -38
  59. package/plugins/index.js.map +1 -1
  60. package/registry/WebsocketsConnectionRegistry.d.ts +3 -3
  61. package/registry/WebsocketsConnectionRegistry.js +34 -59
  62. package/registry/WebsocketsConnectionRegistry.js.map +1 -1
  63. package/registry/abstractions/IWebsocketsConnectionRegistry.d.ts +2 -4
  64. package/registry/abstractions/IWebsocketsConnectionRegistry.js +1 -5
  65. package/registry/abstractions/IWebsocketsConnectionRegistry.js.map +1 -1
  66. package/registry/entity.d.ts +3 -108
  67. package/registry/entity.js +18 -33
  68. package/registry/entity.js.map +1 -1
  69. package/registry/index.d.ts +2 -2
  70. package/registry/index.js +1 -27
  71. package/registry/index.js.map +1 -1
  72. package/response/WebsocketsResponse.d.ts +1 -1
  73. package/response/WebsocketsResponse.js +1 -8
  74. package/response/WebsocketsResponse.js.map +1 -1
  75. package/response/abstractions/IWebsocketsResponse.d.ts +1 -1
  76. package/response/abstractions/IWebsocketsResponse.js +1 -5
  77. package/response/abstractions/IWebsocketsResponse.js.map +1 -1
  78. package/response/index.d.ts +2 -2
  79. package/response/index.js +1 -27
  80. package/response/index.js.map +1 -1
  81. package/runner/WebsocketsRunner.d.ts +6 -6
  82. package/runner/WebsocketsRunner.js +10 -23
  83. package/runner/WebsocketsRunner.js.map +1 -1
  84. package/runner/abstractions/IWebsocketsRunner.d.ts +2 -2
  85. package/runner/abstractions/IWebsocketsRunner.js +1 -5
  86. package/runner/abstractions/IWebsocketsRunner.js.map +1 -1
  87. package/runner/index.d.ts +2 -2
  88. package/runner/index.js +1 -27
  89. package/runner/index.js.map +1 -1
  90. package/runner/routes/connect.d.ts +1 -1
  91. package/runner/routes/connect.js +9 -19
  92. package/runner/routes/connect.js.map +1 -1
  93. package/runner/routes/default.d.ts +1 -1
  94. package/runner/routes/default.js +4 -17
  95. package/runner/routes/default.js.map +1 -1
  96. package/runner/routes/disconnect.d.ts +1 -1
  97. package/runner/routes/disconnect.js +4 -11
  98. package/runner/routes/disconnect.js.map +1 -1
  99. package/runner/routes/index.d.ts +1 -1
  100. package/runner/routes/index.js +5 -12
  101. package/runner/routes/index.js.map +1 -1
  102. package/transport/WebsocketsTransport.d.ts +2 -2
  103. package/transport/WebsocketsTransport.js +5 -12
  104. package/transport/WebsocketsTransport.js.map +1 -1
  105. package/transport/abstractions/IWebsocketsTransport.d.ts +4 -4
  106. package/transport/abstractions/IWebsocketsTransport.js +1 -5
  107. package/transport/abstractions/IWebsocketsTransport.js.map +1 -1
  108. package/transport/index.d.ts +2 -2
  109. package/transport/index.js +1 -27
  110. package/transport/index.js.map +1 -1
  111. package/types.d.ts +5 -5
  112. package/types.js +1 -5
  113. package/types.js.map +1 -1
  114. package/utils/middleware.d.ts +2 -2
  115. package/utils/middleware.js +1 -8
  116. package/utils/middleware.js.map +1 -1
  117. package/validator/WebsocketsEventValidator.d.ts +2 -2
  118. package/validator/WebsocketsEventValidator.js +25 -34
  119. package/validator/WebsocketsEventValidator.js.map +1 -1
  120. package/validator/abstractions/IWebsocketsEventValidator.d.ts +2 -2
  121. package/validator/abstractions/IWebsocketsEventValidator.js +1 -5
  122. package/validator/abstractions/IWebsocketsEventValidator.js.map +1 -1
  123. package/validator/index.d.ts +2 -2
  124. package/validator/index.js +1 -27
  125. package/validator/index.js.map +1 -1
package/README.md CHANGED
@@ -1,10 +1,11 @@
1
1
  # @webiny/api-websockets
2
- [![](https://img.shields.io/npm/dw/@webiny/api-websockets.svg)](https://www.npmjs.com/package/@webiny/api-websockets)
3
- [![](https://img.shields.io/npm/v/@webiny/api-websockets.svg)](https://www.npmjs.com/package/@webiny/api-websockets)
4
- [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
5
- [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
6
-
7
- ## Install
8
- ```
9
- yarn add @webiny/api-websockets
10
- ```
2
+
3
+ > [!NOTE]
4
+ > This package is part of the [Webiny](https://www.webiny.com) monorepo.
5
+ > It’s **included in every Webiny project by default** and is not meant to be used as a standalone package.
6
+
7
+ 📘 **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
8
+
9
+ ---
10
+
11
+ _This README file is automatically generated during the publish process._
@@ -1,13 +1,15 @@
1
- import { IWebsocketsConnectionRegistry, IWebsocketsConnectionRegistryData } from "../registry";
2
- import { IWebsocketsContextObject, IWebsocketsContextDisconnectConnectionsParams, IWebsocketsContextListConnectionsParams, IWebsocketsIdentity } from "./abstractions/IWebsocketsContext";
3
- import { IWebsocketsTransport, IWebsocketsTransportSendConnection, IWebsocketsTransportSendData } from "../transport";
4
- import { GenericRecord } from "@webiny/api/types";
1
+ import { Result } from "@webiny/feature/api";
2
+ import type { IWebsocketsConnectionRegistry, IWebsocketsConnectionRegistryData } from "../registry/index.js";
3
+ import type { IWebsocketsContextObject, IWebsocketsContextDisconnectConnectionsParams, IWebsocketsContextListConnectionsParams, IWebsocketsIdentity } from "./abstractions/IWebsocketsContext.js";
4
+ import type { IWebsocketsTransport, IWebsocketsTransportSendConnection, IWebsocketsTransportSendData } from "../transport/index.js";
5
+ import type { GenericRecord } from "@webiny/api/types.js";
6
+ import { WebsocketService } from "../features/WebsocketService/index.js";
5
7
  export declare class WebsocketsContext implements IWebsocketsContextObject {
6
8
  readonly registry: IWebsocketsConnectionRegistry;
7
9
  private readonly transport;
8
10
  constructor(registry: IWebsocketsConnectionRegistry, transport: IWebsocketsTransport);
9
- send<T extends GenericRecord = GenericRecord>(identity: Pick<IWebsocketsIdentity, "id">, data: IWebsocketsTransportSendData<T>): Promise<void>;
10
- sendToConnections<T extends GenericRecord = GenericRecord>(connections: IWebsocketsTransportSendConnection[], data: IWebsocketsTransportSendData<T>): Promise<void>;
11
- listConnections(params?: IWebsocketsContextListConnectionsParams): Promise<IWebsocketsConnectionRegistryData[]>;
12
- disconnect(params?: IWebsocketsContextDisconnectConnectionsParams, notify?: boolean): Promise<IWebsocketsConnectionRegistryData[]>;
11
+ send<T extends GenericRecord = GenericRecord>(identity: Pick<IWebsocketsIdentity, "id">, data: IWebsocketsTransportSendData<T>): Promise<Result<void, WebsocketService.Error>>;
12
+ sendToConnections<T extends GenericRecord = GenericRecord>(connections: IWebsocketsTransportSendConnection[], data: IWebsocketsTransportSendData<T>): Promise<Result<void, WebsocketService.Error>>;
13
+ listConnections(params?: IWebsocketsContextListConnectionsParams): Promise<Result<IWebsocketsConnectionRegistryData[], WebsocketService.Error>>;
14
+ disconnect(params?: IWebsocketsContextDisconnectConnectionsParams, notify?: boolean): Promise<Result<IWebsocketsConnectionRegistryData[], WebsocketService.Error>>;
13
15
  }
@@ -1,40 +1,58 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.WebsocketsContext = void 0;
8
- var _error = _interopRequireDefault(require("@webiny/error"));
9
- class WebsocketsContext {
1
+ import { Result } from "@webiny/feature/api";
2
+ import { WebsocketServiceError, WebsocketForceDisconnectError, WebsocketForceDisconnectNotificationError } from "../features/WebsocketService/errors.js";
3
+ export class WebsocketsContext {
10
4
  constructor(registry, transport) {
11
5
  this.registry = registry;
12
6
  this.transport = transport;
13
7
  }
14
8
  async send(identity, data) {
15
- const connections = await this.listConnections({
9
+ const result = await this.listConnections({
16
10
  where: {
17
11
  identityId: identity.id
18
12
  }
19
13
  });
20
- return this.transport.send(connections, data);
14
+ if (result.isFail()) {
15
+ return Result.fail(result.error);
16
+ }
17
+ try {
18
+ await this.transport.send(result.value, data);
19
+ } catch (error) {
20
+ return Result.fail(new WebsocketServiceError(error));
21
+ }
22
+ return Result.ok();
21
23
  }
22
24
  async sendToConnections(connections, data) {
23
- return this.transport.send(connections, data);
25
+ try {
26
+ await this.transport.send(connections, data);
27
+ } catch (error) {
28
+ return Result.fail(new WebsocketServiceError(error));
29
+ }
30
+ return Result.ok();
24
31
  }
25
32
  async listConnections(params) {
26
- const where = params?.where || {};
27
- if (where.identityId) {
28
- return await this.registry.listViaIdentity(where.identityId);
29
- } else if (where.connections) {
30
- return await this.registry.listViaConnections(where.connections);
31
- } else if (where.tenant) {
32
- return await this.registry.listViaTenant(where.tenant, where.locale);
33
+ let connections = [];
34
+ try {
35
+ const where = params?.where || {};
36
+ if (where.identityId) {
37
+ connections = await this.registry.listViaIdentity(where.identityId);
38
+ } else if (where.connections) {
39
+ connections = await this.registry.listViaConnections(where.connections);
40
+ } else if (where.tenant) {
41
+ connections = await this.registry.listViaTenant(where.tenant);
42
+ } else {
43
+ connections = await this.registry.listAll();
44
+ }
45
+ } catch (error) {
46
+ return Result.fail(new WebsocketServiceError(error));
33
47
  }
34
- return await this.registry.listAll();
48
+ return Result.ok(connections);
35
49
  }
36
50
  async disconnect(params, notify = true) {
37
- const connections = await this.listConnections(params);
51
+ const result = await this.listConnections(params);
52
+ if (result.isFail()) {
53
+ return Result.fail(result.error);
54
+ }
55
+ const connections = result.value;
38
56
  for (const connection of connections) {
39
57
  try {
40
58
  await this.registry.unregister(connection);
@@ -47,24 +65,17 @@ class WebsocketsContext {
47
65
  await this.transport.send(connections, {
48
66
  action: "forcedDisconnect"
49
67
  });
50
- } catch (ex) {
51
- throw new _error.default("Failed to notify the clients about the forced disconnect.", "FORCED_DISCONNECT_NOTIFICATION_ERROR", {
52
- connections,
53
- error: ex
54
- });
68
+ } catch (error) {
69
+ return Result.fail(new WebsocketForceDisconnectNotificationError(error));
55
70
  }
56
71
  }
57
72
  try {
58
73
  await this.transport.disconnect(connections);
59
- } catch (ex) {
60
- throw new _error.default("Failed to forcefully disconnect the Websocket clients.", "FORCED_DISCONNECT_ERROR", {
61
- connections,
62
- error: ex
63
- });
74
+ } catch (error) {
75
+ return Result.fail(new WebsocketForceDisconnectError(error));
64
76
  }
65
- return connections;
77
+ return Result.ok(connections);
66
78
  }
67
79
  }
68
- exports.WebsocketsContext = WebsocketsContext;
69
80
 
70
81
  //# sourceMappingURL=WebsocketsContext.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_error","_interopRequireDefault","require","WebsocketsContext","constructor","registry","transport","send","identity","data","connections","listConnections","where","identityId","id","sendToConnections","params","listViaIdentity","listViaConnections","tenant","listViaTenant","locale","listAll","disconnect","notify","connection","unregister","action","ex","WebinyError","error","exports"],"sources":["WebsocketsContext.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport { IWebsocketsConnectionRegistry, IWebsocketsConnectionRegistryData } from \"~/registry\";\nimport {\n IWebsocketsContextObject,\n IWebsocketsContextDisconnectConnectionsParams,\n IWebsocketsContextListConnectionsParams,\n IWebsocketsIdentity\n} from \"./abstractions/IWebsocketsContext\";\nimport {\n IWebsocketsTransport,\n IWebsocketsTransportSendConnection,\n IWebsocketsTransportSendData\n} from \"~/transport\";\nimport { GenericRecord } from \"@webiny/api/types\";\n\nexport class WebsocketsContext implements IWebsocketsContextObject {\n public readonly registry: IWebsocketsConnectionRegistry;\n private readonly transport: IWebsocketsTransport;\n\n constructor(registry: IWebsocketsConnectionRegistry, transport: IWebsocketsTransport) {\n this.registry = registry;\n this.transport = transport;\n }\n\n public async send<T extends GenericRecord = GenericRecord>(\n identity: Pick<IWebsocketsIdentity, \"id\">,\n data: IWebsocketsTransportSendData<T>\n ): Promise<void> {\n const connections = await this.listConnections({\n where: {\n identityId: identity.id\n }\n });\n return this.transport.send<T>(connections, data);\n }\n\n public async sendToConnections<T extends GenericRecord = GenericRecord>(\n connections: IWebsocketsTransportSendConnection[],\n data: IWebsocketsTransportSendData<T>\n ): Promise<void> {\n return this.transport.send<T>(connections, data);\n }\n\n public async listConnections(\n params?: IWebsocketsContextListConnectionsParams\n ): Promise<IWebsocketsConnectionRegistryData[]> {\n const where = params?.where || {};\n if (where.identityId) {\n return await this.registry.listViaIdentity(where.identityId);\n } else if (where.connections) {\n return await this.registry.listViaConnections(where.connections);\n } else if (where.tenant) {\n return await this.registry.listViaTenant(where.tenant, where.locale);\n }\n return await this.registry.listAll();\n }\n\n public async disconnect(\n params?: IWebsocketsContextDisconnectConnectionsParams,\n notify = true\n ): Promise<IWebsocketsConnectionRegistryData[]> {\n const connections = await this.listConnections(params);\n\n for (const connection of connections) {\n try {\n await this.registry.unregister(connection);\n } catch {\n // do nothing\n }\n }\n if (notify) {\n try {\n await this.transport.send(connections, {\n action: \"forcedDisconnect\"\n });\n } catch (ex) {\n throw new WebinyError(\n \"Failed to notify the clients about the forced disconnect.\",\n \"FORCED_DISCONNECT_NOTIFICATION_ERROR\",\n {\n connections,\n error: ex\n }\n );\n }\n }\n try {\n await this.transport.disconnect(connections);\n } catch (ex) {\n throw new WebinyError(\n \"Failed to forcefully disconnect the Websocket clients.\",\n \"FORCED_DISCONNECT_ERROR\",\n {\n connections,\n error: ex\n }\n );\n }\n\n return connections;\n }\n}\n"],"mappings":";;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AAeO,MAAMC,iBAAiB,CAAqC;EAI/DC,WAAWA,CAACC,QAAuC,EAAEC,SAA+B,EAAE;IAClF,IAAI,CAACD,QAAQ,GAAGA,QAAQ;IACxB,IAAI,CAAC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QAAQ,CAACqB,UAAU,CAACD,UAAU,CAAC;MAC9C,CAAC,CAAC,MAAM;QACJ;MAAA;IAER;IACA,IAAID,MAAM,EAAE;MACR,IAAI;QACA,MAAM,IAAI,CAAClB,SAAS,CAACC,IAAI,CAACG,WAAW,EAAE;UACnCiB,MAAM,EAAE;QACZ,CAAC,CAAC;MACN,CAAC,CAAC,OAAOC,EAAE,EAAE;QACT,MAAM,IAAIC,cAAW,CACjB,2DAA2D,EAC3D,sCAAsC,EACtC;UACInB,WAAW;UACXoB,KAAK,EAAEF;QACX,CACJ,CAAC;MACL;IACJ;IACA,IAAI;MACA,MAAM,IAAI,CAACtB,SAAS,CAACiB,UAAU,CAACb,WAAW,CAAC;IAChD,CAAC,CAAC,OAAOkB,EAAE,EAAE;MACT,MAAM,IAAIC,cAAW,CACjB,wDAAwD,EACxD,yBAAyB,EACzB;QACInB,WAAW;QACXoB,KAAK,EAAEF;MACX,CACJ,CAAC;IACL;IAEA,OAAOlB,WAAW;EACtB;AACJ;AAACqB,OAAA,CAAA5B,iBAAA,GAAAA,iBAAA","ignoreList":[]}
1
+ {"version":3,"names":["Result","WebsocketServiceError","WebsocketForceDisconnectError","WebsocketForceDisconnectNotificationError","WebsocketsContext","constructor","registry","transport","send","identity","data","result","listConnections","where","identityId","id","isFail","fail","error","value","ok","sendToConnections","connections","params","listViaIdentity","listViaConnections","tenant","listViaTenant","listAll","disconnect","notify","connection","unregister","action"],"sources":["WebsocketsContext.ts"],"sourcesContent":["import { Result } from \"@webiny/feature/api\";\nimport type {\n IWebsocketsConnectionRegistry,\n IWebsocketsConnectionRegistryData\n} from \"~/registry/index.js\";\nimport type {\n IWebsocketsContextObject,\n IWebsocketsContextDisconnectConnectionsParams,\n IWebsocketsContextListConnectionsParams,\n IWebsocketsIdentity\n} from \"./abstractions/IWebsocketsContext.js\";\nimport type {\n IWebsocketsTransport,\n IWebsocketsTransportSendConnection,\n IWebsocketsTransportSendData\n} from \"~/transport/index.js\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\nimport {\n WebsocketServiceError,\n WebsocketForceDisconnectError,\n WebsocketForceDisconnectNotificationError\n} from \"~/features/WebsocketService/errors.js\";\nimport { WebsocketService } from \"~/features/WebsocketService/index.js\";\n\nexport class WebsocketsContext implements IWebsocketsContextObject {\n public readonly registry: IWebsocketsConnectionRegistry;\n private readonly transport: IWebsocketsTransport;\n\n constructor(registry: IWebsocketsConnectionRegistry, transport: IWebsocketsTransport) {\n this.registry = registry;\n this.transport = transport;\n }\n\n public async send<T extends GenericRecord = GenericRecord>(\n identity: Pick<IWebsocketsIdentity, \"id\">,\n data: IWebsocketsTransportSendData<T>\n ): Promise<Result<void, WebsocketService.Error>> {\n const result = await this.listConnections({\n where: {\n identityId: identity.id\n }\n });\n\n if (result.isFail()) {\n return Result.fail(result.error);\n }\n\n try {\n await this.transport.send<T>(result.value, data);\n } catch (error) {\n return Result.fail(new WebsocketServiceError(error));\n }\n\n return Result.ok();\n }\n\n public async sendToConnections<T extends GenericRecord = GenericRecord>(\n connections: IWebsocketsTransportSendConnection[],\n data: IWebsocketsTransportSendData<T>\n ): Promise<Result<void, WebsocketService.Error>> {\n try {\n await this.transport.send<T>(connections, data);\n } catch (error) {\n return Result.fail(new WebsocketServiceError(error));\n }\n\n return Result.ok();\n }\n\n public async listConnections(\n params?: IWebsocketsContextListConnectionsParams\n ): Promise<Result<IWebsocketsConnectionRegistryData[], WebsocketService.Error>> {\n let connections: IWebsocketsConnectionRegistryData[] = [];\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));\n }\n\n return Result.ok(connections);\n }\n\n public async disconnect(\n params?: IWebsocketsContextDisconnectConnectionsParams,\n notify = true\n ): Promise<Result<IWebsocketsConnectionRegistryData[], WebsocketService.Error>> {\n const result = await this.listConnections(params);\n if (result.isFail()) {\n return Result.fail(result.error);\n }\n\n const connections = result.value;\n\n for (const connection of connections) {\n try {\n await this.registry.unregister(connection);\n } catch {\n // do nothing\n }\n }\n\n if (notify) {\n try {\n await this.transport.send(connections, {\n action: \"forcedDisconnect\"\n });\n } catch (error) {\n return Result.fail(new WebsocketForceDisconnectNotificationError(error));\n }\n }\n\n try {\n await this.transport.disconnect(connections);\n } catch (error) {\n return Result.fail(new WebsocketForceDisconnectError(error));\n }\n\n return Result.ok(connections);\n }\n}\n"],"mappings":"AAAA,SAASA,MAAM,QAAQ,qBAAqB;AAiB5C,SACIC,qBAAqB,EACrBC,6BAA6B,EAC7BC,yCAAyC;AAI7C,OAAO,MAAMC,iBAAiB,CAAqC;EAI/DC,WAAWA,CAACC,QAAuC,EAAEC,SAA+B,EAAE;IAClF,IAAI,CAACD,QAAQ,GAAGA,QAAQ;IACxB,IAAI,CAACC,SAAS,GAAGA,SAAS;EAC9B;EAEA,MAAaC,IAAIA,CACbC,QAAyC,EACzCC,IAAqC,EACQ;IAC7C,MAAMC,MAAM,GAAG,MAAM,IAAI,CAACC,eAAe,CAAC;MACtCC,KAAK,EAAE;QACHC,UAAU,EAAEL,QAAQ,CAACM;MACzB;IACJ,CAAC,CAAC;IAEF,IAAIJ,MAAM,CAACK,MAAM,CAAC,CAAC,EAAE;MACjB,OAAOhB,MAAM,CAACiB,IAAI,CAACN,MAAM,CAACO,KAAK,CAAC;IACpC;IAEA,IAAI;MACA,MAAM,IAAI,CAACX,SAAS,CAACC,IAAI,CAAIG,MAAM,CAACQ,KAAK,EAAET,IAAI,CAAC;IACpD,CAAC,CAAC,OAAOQ,KAAK,EAAE;MACZ,OAAOlB,MAAM,CAACiB,IAAI,CAAC,IAAIhB,qBAAqB,CAACiB,KAAK,CAAC,CAAC;IACxD;IAEA,OAAOlB,MAAM,CAACoB,EAAE,CAAC,CAAC;EACtB;EAEA,MAA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AAMnB,MAAM,GAAG,MAAM,IAAI,CAACC,eAAe,CAACW,MAAM,CAAC;IACjD,IAAIZ,MAAM,CAACK,MAAM,CAAC,CAAC,EAAE;MACjB,OAAOhB,MAAM,CAACiB,IAAI,CAACN,MAAM,CAACO,KAAK,CAAC;IACpC;IAEA,MAAMI,WAAW,GAAGX,MAAM,CAACQ,KAAK;IAEhC,KAAK,MAAMY,UAAU,IAAIT,WAAW,EAAE;MAClC,IAAI;QACA,MAAM,IAAI,CAAChB,QAAQ,CAAC0B,UAAU,CAACD,UAAU,CAAC;MAC9C,CAAC,CAAC,MAAM;QACJ;MAAA;IAER;IAEA,IAAID,MAAM,EAAE;MACR,IAAI;QACA,MAAM,IAAI,CAACvB,SAAS,CAACC,IAAI,CAACc,WAAW,EAAE;UACnCW,MAAM,EAAE;QACZ,CAAC,CAAC;MACN,CAAC,CAAC,OAAOf,KAAK,EAAE;QACZ,OAAOlB,MAAM,CAACiB,IAAI,CAAC,IAAId,yCAAyC,CAACe,KAAK,CAAC,CAAC;MAC5E;IACJ;IAEA,IAAI;MACA,MAAM,IAAI,CAACX,SAAS,CAACsB,UAAU,CAACP,WAAW,CAAC;IAChD,CAAC,CAAC,OAAOJ,KAAK,EAAE;MACZ,OAAOlB,MAAM,CAACiB,IAAI,CAAC,IAAIf,6BAA6B,CAACgB,KAAK,CAAC,CAAC;IAChE;IAEA,OAAOlB,MAAM,CAACoB,EAAE,CAACE,WAAW,CAAC;EACjC;AACJ","ignoreList":[]}
@@ -1,22 +1,23 @@
1
- import { IWebsocketsConnectionRegistry, IWebsocketsConnectionRegistryData } from "../../registry";
2
- import { IWebsocketsTransportSendConnection, IWebsocketsTransportSendData } from "../../transport";
3
- import { SecurityIdentity } from "@webiny/api-security/types";
4
- import { GenericRecord } from "@webiny/api/types";
5
- export declare type IWebsocketsIdentity = Pick<SecurityIdentity, "id" | "displayName" | "type">;
1
+ import type { IWebsocketsConnectionRegistry, IWebsocketsConnectionRegistryData } from "../../registry/index.js";
2
+ import type { IWebsocketsTransportSendConnection, IWebsocketsTransportSendData } from "../../transport/index.js";
3
+ import type { GenericRecord } from "@webiny/api/types.js";
4
+ import type { SecurityIdentity } from "@webiny/api-core/types/security.js";
5
+ import { Result } from "@webiny/feature/api";
6
+ import { WebsocketService } from "../../features/WebsocketService/abstractions.js";
7
+ export type IWebsocketsIdentity = Pick<SecurityIdentity, "id" | "displayName" | "type">;
6
8
  export interface IWebsocketsContextListConnectionsParamsWhere {
7
9
  identityId?: string;
8
10
  tenant?: string;
9
- locale?: string;
10
11
  connections?: string[];
11
12
  }
12
13
  export interface IWebsocketsContextListConnectionsParams {
13
14
  where?: IWebsocketsContextListConnectionsParamsWhere;
14
15
  }
15
- export declare type IWebsocketsContextDisconnectConnectionsParams = IWebsocketsContextListConnectionsParams;
16
+ export type IWebsocketsContextDisconnectConnectionsParams = IWebsocketsContextListConnectionsParams;
16
17
  export interface IWebsocketsContextObject {
17
18
  readonly registry: IWebsocketsConnectionRegistry;
18
- send<T extends GenericRecord = GenericRecord>(identity: Pick<IWebsocketsIdentity, "id">, data: IWebsocketsTransportSendData<T>): Promise<void>;
19
- sendToConnections<T extends GenericRecord = GenericRecord>(connections: IWebsocketsTransportSendConnection[], data: IWebsocketsTransportSendData<T>): Promise<void>;
20
- listConnections(params?: IWebsocketsContextListConnectionsParams): Promise<IWebsocketsConnectionRegistryData[]>;
21
- disconnect(params?: IWebsocketsContextDisconnectConnectionsParams, notify?: boolean): Promise<IWebsocketsConnectionRegistryData[]>;
19
+ send<T extends GenericRecord = GenericRecord>(identity: Pick<IWebsocketsIdentity, "id">, data: IWebsocketsTransportSendData<T>): Promise<Result<void, WebsocketService.Error>>;
20
+ sendToConnections<T extends GenericRecord = GenericRecord>(connections: IWebsocketsTransportSendConnection[], data: IWebsocketsTransportSendData<T>): Promise<Result<void, WebsocketService.Error>>;
21
+ listConnections(params?: IWebsocketsContextListConnectionsParams): Promise<Result<IWebsocketsConnectionRegistryData[], WebsocketService.Error>>;
22
+ disconnect(params?: IWebsocketsContextDisconnectConnectionsParams, notify?: boolean): Promise<Result<IWebsocketsConnectionRegistryData[], WebsocketService.Error>>;
22
23
  }
@@ -1,7 +1,3 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
1
+ export {};
6
2
 
7
3
  //# sourceMappingURL=IWebsocketsContext.js.map
@@ -1 +1 @@
1
- {"version":3,"names":[],"sources":["IWebsocketsContext.ts"],"sourcesContent":["import { IWebsocketsConnectionRegistry, IWebsocketsConnectionRegistryData } from \"~/registry\";\nimport { IWebsocketsTransportSendConnection, IWebsocketsTransportSendData } from \"~/transport\";\nimport { SecurityIdentity } from \"@webiny/api-security/types\";\nimport { GenericRecord } from \"@webiny/api/types\";\n\nexport type IWebsocketsIdentity = Pick<SecurityIdentity, \"id\" | \"displayName\" | \"type\">;\n\nexport interface IWebsocketsContextListConnectionsParamsWhere {\n identityId?: string;\n tenant?: string;\n locale?: string;\n connections?: string[];\n}\n\nexport interface IWebsocketsContextListConnectionsParams {\n where?: IWebsocketsContextListConnectionsParamsWhere;\n}\n\nexport type IWebsocketsContextDisconnectConnectionsParams = IWebsocketsContextListConnectionsParams;\n\nexport interface IWebsocketsContextObject {\n readonly registry: IWebsocketsConnectionRegistry;\n\n send<T extends GenericRecord = GenericRecord>(\n identity: Pick<IWebsocketsIdentity, \"id\">,\n data: IWebsocketsTransportSendData<T>\n ): Promise<void>;\n sendToConnections<T extends GenericRecord = GenericRecord>(\n connections: IWebsocketsTransportSendConnection[],\n data: IWebsocketsTransportSendData<T>\n ): Promise<void>;\n listConnections(\n params?: IWebsocketsContextListConnectionsParams\n ): Promise<IWebsocketsConnectionRegistryData[]>;\n disconnect(\n params?: IWebsocketsContextDisconnectConnectionsParams,\n notify?: boolean\n ): Promise<IWebsocketsConnectionRegistryData[]>;\n}\n"],"mappings":"","ignoreList":[]}
1
+ {"version":3,"names":[],"sources":["IWebsocketsContext.ts"],"sourcesContent":["import type {\n IWebsocketsConnectionRegistry,\n IWebsocketsConnectionRegistryData\n} from \"~/registry/index.js\";\nimport type {\n IWebsocketsTransportSendConnection,\n IWebsocketsTransportSendData\n} from \"~/transport/index.js\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\nimport type { SecurityIdentity } from \"@webiny/api-core/types/security.js\";\nimport { Result } from \"@webiny/feature/api\";\nimport { WebsocketService } from \"~/features/WebsocketService/abstractions.js\";\n\nexport type IWebsocketsIdentity = Pick<SecurityIdentity, \"id\" | \"displayName\" | \"type\">;\n\nexport interface IWebsocketsContextListConnectionsParamsWhere {\n identityId?: string;\n tenant?: string;\n connections?: string[];\n}\n\nexport interface IWebsocketsContextListConnectionsParams {\n where?: IWebsocketsContextListConnectionsParamsWhere;\n}\n\nexport type IWebsocketsContextDisconnectConnectionsParams = IWebsocketsContextListConnectionsParams;\n\nexport interface IWebsocketsContextObject {\n readonly registry: IWebsocketsConnectionRegistry;\n\n send<T extends GenericRecord = GenericRecord>(\n identity: Pick<IWebsocketsIdentity, \"id\">,\n data: IWebsocketsTransportSendData<T>\n ): Promise<Result<void, WebsocketService.Error>>;\n sendToConnections<T extends GenericRecord = GenericRecord>(\n connections: IWebsocketsTransportSendConnection[],\n data: IWebsocketsTransportSendData<T>\n ): Promise<Result<void, WebsocketService.Error>>;\n listConnections(\n params?: IWebsocketsContextListConnectionsParams\n ): Promise<Result<IWebsocketsConnectionRegistryData[], WebsocketService.Error>>;\n disconnect(\n params?: IWebsocketsContextDisconnectConnectionsParams,\n notify?: boolean\n ): Promise<Result<IWebsocketsConnectionRegistryData[], WebsocketService.Error>>;\n}\n"],"mappings":"","ignoreList":[]}
@@ -1,5 +1,4 @@
1
1
  import { ContextPlugin } from "@webiny/handler";
2
- import { Context } from "../types";
3
- export * from "./WebsocketsContext";
4
- export * from "./abstractions/IWebsocketsContext";
2
+ import type { Context } from "../types.js";
3
+ export type * from "./abstractions/IWebsocketsContext.js";
5
4
  export declare const createWebsocketsContext: () => ContextPlugin<Context>;
package/context/index.js CHANGED
@@ -1,53 +1,22 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- var _exportNames = {
7
- createWebsocketsContext: true
8
- };
9
- exports.createWebsocketsContext = void 0;
10
- var _handler = require("@webiny/handler");
11
- var _WebsocketsContext = require("./WebsocketsContext");
12
- Object.keys(_WebsocketsContext).forEach(function (key) {
13
- if (key === "default" || key === "__esModule") return;
14
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
15
- if (key in exports && exports[key] === _WebsocketsContext[key]) return;
16
- Object.defineProperty(exports, key, {
17
- enumerable: true,
18
- get: function () {
19
- return _WebsocketsContext[key];
20
- }
21
- });
22
- });
23
- var _registry = require("../registry");
24
- var _transport = require("../transport");
25
- var _IWebsocketsContext = require("./abstractions/IWebsocketsContext");
26
- Object.keys(_IWebsocketsContext).forEach(function (key) {
27
- if (key === "default" || key === "__esModule") return;
28
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
29
- if (key in exports && exports[key] === _IWebsocketsContext[key]) return;
30
- Object.defineProperty(exports, key, {
31
- enumerable: true,
32
- get: function () {
33
- return _IWebsocketsContext[key];
34
- }
35
- });
36
- });
37
- const createWebsocketsContext = () => {
38
- const plugin = new _handler.ContextPlugin(async context => {
1
+ import { ContextPlugin } from "@webiny/handler";
2
+ import { WebsocketsContext as WebsocketsImplementation } from "./WebsocketsContext.js";
3
+ import { WebsocketsConnectionRegistry } from "../registry/index.js";
4
+ import { WebsocketsTransport } from "../transport/index.js";
5
+ import { WebsocketService } from "../features/WebsocketService/abstractions.js";
6
+ export const createWebsocketsContext = () => {
7
+ const plugin = new ContextPlugin(async context => {
39
8
  /**
40
9
  * TODO Find a better way to send the documentClient to the registry.
41
10
  */
42
11
  // @ts-expect-error
43
12
  const documentClient = context.db.driver.documentClient;
44
- const registry = new _registry.WebsocketsConnectionRegistry(documentClient);
45
- const transport = new _transport.WebsocketsTransport();
46
- context.websockets = new _WebsocketsContext.WebsocketsContext(registry, transport);
13
+ const registry = new WebsocketsConnectionRegistry(documentClient);
14
+ const transport = new WebsocketsTransport();
15
+ context.websockets = new WebsocketsImplementation(registry, transport);
16
+ context.container.registerInstance(WebsocketService, context.websockets);
47
17
  });
48
18
  plugin.name = "websockets.context";
49
19
  return plugin;
50
20
  };
51
- exports.createWebsocketsContext = createWebsocketsContext;
52
21
 
53
22
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_handler","require","_WebsocketsContext","Object","keys","forEach","key","prototype","hasOwnProperty","call","_exportNames","exports","defineProperty","enumerable","get","_registry","_transport","_IWebsocketsContext","createWebsocketsContext","plugin","ContextPlugin","context","documentClient","db","driver","registry","WebsocketsConnectionRegistry","transport","WebsocketsTransport","websockets","WebsocketsContext","name"],"sources":["index.ts"],"sourcesContent":["import { ContextPlugin } from \"@webiny/handler\";\nimport { Context } from \"~/types\";\nimport { WebsocketsContext } from \"./WebsocketsContext\";\nimport { WebsocketsConnectionRegistry } from \"~/registry\";\nimport { WebsocketsTransport } from \"~/transport\";\n\nexport * from \"./WebsocketsContext\";\nexport * from \"./abstractions/IWebsocketsContext\";\n\nexport const createWebsocketsContext = () => {\n const plugin = new ContextPlugin<Context>(async context => {\n /**\n * TODO Find a better way to send the documentClient to the registry.\n */\n // @ts-expect-error\n const documentClient = context.db.driver.documentClient;\n const registry = new WebsocketsConnectionRegistry(documentClient);\n const transport = new WebsocketsTransport();\n context.websockets = new WebsocketsContext(registry, transport);\n });\n\n plugin.name = \"websockets.context\";\n\n return plugin;\n};\n"],"mappings":";;;;;;;;;AAAA,IAAAA,QAAA,GAAAC,OAAA;AAEA,IAAAC,kBAAA,GAAAD,OAAA;AAIAE,MAAA,CAAAC,IAAA,CAAAF,kBAAA,EAAAG,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAC,YAAA,EAAAJ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ACjB,CAAC;AAACR,OAAA,CAAAO,uBAAA,GAAAA,uBAAA","ignoreList":[]}
1
+ {"version":3,"names":["ContextPlugin","WebsocketsContext","WebsocketsImplementation","WebsocketsConnectionRegistry","WebsocketsTransport","WebsocketService","createWebsocketsContext","plugin","context","documentClient","db","driver","registry","transport","websockets","container","registerInstance","name"],"sources":["index.ts"],"sourcesContent":["import { ContextPlugin } from \"@webiny/handler\";\nimport type { Context } from \"~/types.js\";\nimport { WebsocketsContext as WebsocketsImplementation } from \"./WebsocketsContext.js\";\nimport { WebsocketsConnectionRegistry } from \"~/registry/index.js\";\nimport { WebsocketsTransport } from \"~/transport/index.js\";\nimport { WebsocketService } from \"~/features/WebsocketService/abstractions.js\";\n\nexport type * from \"./abstractions/IWebsocketsContext.js\";\n\nexport const createWebsocketsContext = () => {\n const plugin = new ContextPlugin<Context>(async context => {\n /**\n * TODO Find a better way to send the documentClient to the registry.\n */\n // @ts-expect-error\n const documentClient = context.db.driver.documentClient;\n const registry = new WebsocketsConnectionRegistry(documentClient);\n const transport = new WebsocketsTransport();\n context.websockets = new WebsocketsImplementation(registry, transport);\n\n context.container.registerInstance(WebsocketService, context.websockets);\n });\n\n plugin.name = \"websockets.context\";\n\n return plugin;\n};\n"],"mappings":"AAAA,SAASA,aAAa,QAAQ,iBAAiB;AAE/C,SAASC,iBAAiB,IAAIC,wBAAwB;AACtD,SAASC,4BAA4B;AACrC,SAASC,mBAAmB;AAC5B,SAASC,gBAAgB;AAIzB,OAAO,MAAMC,uBAAuB,GAAGA,CAAA,KAAM;EACzC,MAAMC,MAAM,GAAG,IAAIP,aAAa,CAAU,MAAMQ,OAAO,IAAI;IACvD;AACR;AACA;IACQ;IACA,MAAMC,cAAc,GAAGD,OAAO,CAACE,EAAE,CAACC,MAAM,CAACF,cAAc;IACvD,MAAMG,QAAQ,GAAG,IAAIT,4BAA4B,CAACM,cAAc,CAAC;IACjE,MAAMI,SAAS,GAAG,IAAIT,mBAAmB,CAAC,CAAC;IAC3CI,OAAO,CAACM,UAAU,GAAG,IAAIZ,wBAAwB,CAACU,QAAQ,EAAEC,SAAS,CAAC;IAEtEL,OAAO,CAACO,SAAS,CAACC,gBAAgB,CAACX,gBAAgB,EAAEG,OAAO,CAACM,UAAU,CAAC;EAC5E,CAAC,CAAC;EAEFP,MAAM,CAACU,IAAI,GAAG,oBAAoB;EAElC,OAAOV,MAAM;AACjB,CAAC","ignoreList":[]}
@@ -0,0 +1,9 @@
1
+ import type { IWebsocketsContextObject } from "../../context/abstractions/IWebsocketsContext.js";
2
+ import { WebsocketForceDisconnectError, WebsocketForceDisconnectNotificationError, type WebsocketServiceError } from "../../features/WebsocketService/errors.js";
3
+ import type { IWebsocketsConnectionRegistryData } from "../../registry/index.js";
4
+ export declare const WebsocketService: import("@webiny/di").Abstraction<IWebsocketsContextObject>;
5
+ export declare namespace WebsocketService {
6
+ type Interface = IWebsocketsContextObject;
7
+ type Connection = IWebsocketsConnectionRegistryData;
8
+ type Error = WebsocketServiceError | WebsocketForceDisconnectNotificationError | WebsocketForceDisconnectError;
9
+ }
@@ -0,0 +1,4 @@
1
+ import { createAbstraction } from "@webiny/feature/api";
2
+ export const WebsocketService = createAbstraction("WebsocketService");
3
+
4
+ //# sourceMappingURL=abstractions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["createAbstraction","WebsocketService"],"sources":["abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { IWebsocketsContextObject } from \"~/context/abstractions/IWebsocketsContext.js\";\nimport {\n WebsocketForceDisconnectError,\n WebsocketForceDisconnectNotificationError,\n type WebsocketServiceError\n} from \"~/features/WebsocketService/errors.js\";\nimport type { IWebsocketsConnectionRegistryData } from \"~/registry/index.js\";\n\nexport const WebsocketService = createAbstraction<IWebsocketsContextObject>(\"WebsocketService\");\n\nexport namespace WebsocketService {\n export type Interface = IWebsocketsContextObject;\n export type Connection = IWebsocketsConnectionRegistryData;\n export type Error =\n | WebsocketServiceError\n | WebsocketForceDisconnectNotificationError\n | WebsocketForceDisconnectError;\n}\n"],"mappings":"AAAA,SAASA,iBAAiB,QAAQ,qBAAqB;AASvD,OAAO,MAAMC,gBAAgB,GAAGD,iBAAiB,CAA2B,kBAAkB,CAAC","ignoreList":[]}
@@ -0,0 +1,19 @@
1
+ import { BaseError } from "@webiny/feature/api";
2
+ export declare class WebsocketServiceError extends BaseError<{
3
+ error: Error;
4
+ }> {
5
+ readonly code: "Websocket/Service";
6
+ constructor(error: Error);
7
+ }
8
+ export declare class WebsocketForceDisconnectNotificationError extends BaseError<{
9
+ error: Error;
10
+ }> {
11
+ readonly code: "Websocket/Service/ForceDisconnectNotification";
12
+ constructor(error: Error);
13
+ }
14
+ export declare class WebsocketForceDisconnectError extends BaseError<{
15
+ error: Error;
16
+ }> {
17
+ readonly code: "Websocket/Service/ForceDisconnect";
18
+ constructor(error: Error);
19
+ }
@@ -0,0 +1,36 @@
1
+ import { BaseError } from "@webiny/feature/api";
2
+ export class WebsocketServiceError extends BaseError {
3
+ code = "Websocket/Service";
4
+ constructor(error) {
5
+ super({
6
+ message: "WebsocketService encountered an error.",
7
+ data: {
8
+ error
9
+ }
10
+ });
11
+ }
12
+ }
13
+ export class WebsocketForceDisconnectNotificationError extends BaseError {
14
+ code = "Websocket/Service/ForceDisconnectNotification";
15
+ constructor(error) {
16
+ super({
17
+ message: "Failed to notify the clients about the forced disconnect.",
18
+ data: {
19
+ error
20
+ }
21
+ });
22
+ }
23
+ }
24
+ export class WebsocketForceDisconnectError extends BaseError {
25
+ code = "Websocket/Service/ForceDisconnect";
26
+ constructor(error) {
27
+ super({
28
+ message: "Failed to forcefully disconnect the Websocket clients.",
29
+ data: {
30
+ error
31
+ }
32
+ });
33
+ }
34
+ }
35
+
36
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["BaseError","WebsocketServiceError","code","constructor","error","message","data","WebsocketForceDisconnectNotificationError","WebsocketForceDisconnectError"],"sources":["errors.ts"],"sourcesContent":["import { BaseError } from \"@webiny/feature/api\";\n\nexport class WebsocketServiceError extends BaseError<{ error: Error }> {\n override readonly code = \"Websocket/Service\" as const;\n\n constructor(error: Error) {\n super({\n message: \"WebsocketService encountered an error.\",\n data: { error }\n });\n }\n}\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"],"mappings":"AAAA,SAASA,SAAS,QAAQ,qBAAqB;AAE/C,OAAO,MAAMC,qBAAqB,SAASD,SAAS,CAAmB;EACjDE,IAAI,GAAG,mBAAmB;EAE5CC,WAAWA,CAACC,KAAY,EAAE;IACtB,KAAK,CAAC;MACFC,OAAO,EAAE,wCAAwC;MACjDC,IAAI,EAAE;QAAEF;MAAM;IAClB,CAAC,CAAC;EACN;AACJ;AAEA,OAAO,MAAMG,yCAAyC,SAASP,SAAS,CAAmB;EACrEE,IAAI,GAAG,+CAA+C;EAExEC,WAAWA,CAACC,KAAY,EAAE;IACtB,KAAK,CAAC;MACFC,OAAO,EAAE,2DAA2D;MACpEC,IAAI,EAAE;QAAEF;MAAM;IAClB,CAAC,CAAC;EACN;AACJ;AAEA,OAAO,MAAMI,6BAA6B,SAASR,SAAS,CAAmB;EACzDE,IAAI,GAAG,mCAAmC;EAE5DC,WAAWA,CAACC,KAAY,EAAE;IACtB,KAAK,CAAC;MACFC,OAAO,EAAE,wDAAwD;MACjEC,IAAI,EAAE;QAAEF;MAAM;IAClB,CAAC,CAAC;EACN;AACJ","ignoreList":[]}
@@ -0,0 +1 @@
1
+ export { WebsocketService } from "./abstractions.js";
@@ -0,0 +1,3 @@
1
+ export { WebsocketService } from "./abstractions.js";
2
+
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["WebsocketService"],"sources":["index.ts"],"sourcesContent":["export { WebsocketService } from \"./abstractions.js\";\n"],"mappings":"AAAA,SAASA,gBAAgB","ignoreList":[]}
@@ -1,4 +1,4 @@
1
- import { Context } from "../types";
1
+ import type { Context } from "../types.js";
2
2
  /**
3
3
  * Simple permission check. Only full access can access the websockets API via GraphQL - ({name: "*"})
4
4
  *
@@ -1,21 +1,16 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.checkPermissions = void 0;
7
- var _apiSecurity = require("@webiny/api-security");
1
+ import { IdentityContext } from "@webiny/api-core/features/IdentityContext";
2
+ import { NotAuthorizedError } from "@webiny/api-core/features/security/shared/index.js";
8
3
  /**
9
4
  * Simple permission check. Only full access can access the websockets API via GraphQL - ({name: "*"})
10
5
  *
11
6
  * @throws
12
7
  */
13
- const checkPermissions = async context => {
14
- const permissions = await context.security.getPermissions("websockets");
8
+ export const checkPermissions = async context => {
9
+ const identityContext = context.container.resolve(IdentityContext);
10
+ const permissions = await identityContext.getPermissions("websockets");
15
11
  if (permissions.length === 0) {
16
- throw new _apiSecurity.NotAuthorizedError();
12
+ throw new NotAuthorizedError();
17
13
  }
18
14
  };
19
- exports.checkPermissions = checkPermissions;
20
15
 
21
16
  //# sourceMappingURL=checkPermissions.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_apiSecurity","require","checkPermissions","context","permissions","security","getPermissions","length","NotAuthorizedError","exports"],"sources":["checkPermissions.ts"],"sourcesContent":["import { NotAuthorizedError } from \"@webiny/api-security\";\nimport { Context, WebsocketsPermission } from \"~/types\";\n\n/**\n * Simple permission check. Only full access can access the websockets API via GraphQL - ({name: \"*\"})\n *\n * @throws\n */\nexport const checkPermissions = async (context: Context): Promise<void> => {\n const permissions = await context.security.getPermissions<WebsocketsPermission>(\"websockets\");\n\n if (permissions.length === 0) {\n throw new NotAuthorizedError();\n }\n};\n"],"mappings":";;;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AAGA;AACA;AACA;AACA;AACA;AACO,MAAMC,gBAAgB,GAAG,MAAOC,OAAgB,IAAoB;EACvE,MAAMC,WAAW,GAAG,MAAMD,OAAO,CAACE,QAAQ,CAACC,cAAc,CAAuB,YAAY,CAAC;EAE7F,IAAIF,WAAW,CAACG,MAAM,KAAK,CAAC,EAAE;IAC1B,MAAM,IAAIC,+BAAkB,CAAC,CAAC;EAClC;AACJ,CAAC;AAACC,OAAA,CAAAP,gBAAA,GAAAA,gBAAA","ignoreList":[]}
1
+ {"version":3,"names":["IdentityContext","NotAuthorizedError","checkPermissions","context","identityContext","container","resolve","permissions","getPermissions","length"],"sources":["checkPermissions.ts"],"sourcesContent":["import { IdentityContext } from \"@webiny/api-core/features/IdentityContext\";\nimport { NotAuthorizedError } from \"@webiny/api-core/features/security/shared/index.js\";\nimport type { Context, WebsocketsPermission } from \"~/types.js\";\n\n/**\n * Simple permission check. Only full access can access the websockets API via GraphQL - ({name: \"*\"})\n *\n * @throws\n */\nexport const checkPermissions = async (context: Context): Promise<void> => {\n const identityContext = context.container.resolve(IdentityContext);\n const permissions = await identityContext.getPermissions<WebsocketsPermission>(\"websockets\");\n\n if (permissions.length === 0) {\n throw new NotAuthorizedError();\n }\n};\n"],"mappings":"AAAA,SAASA,eAAe,QAAQ,2CAA2C;AAC3E,SAASC,kBAAkB,QAAQ,oDAAoD;AAGvF;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,gBAAgB,GAAG,MAAOC,OAAgB,IAAoB;EACvE,MAAMC,eAAe,GAAGD,OAAO,CAACE,SAAS,CAACC,OAAO,CAACN,eAAe,CAAC;EAClE,MAAMO,WAAW,GAAG,MAAMH,eAAe,CAACI,cAAc,CAAuB,YAAY,CAAC;EAE5F,IAAID,WAAW,CAACE,MAAM,KAAK,CAAC,EAAE;IAC1B,MAAM,IAAIR,kBAAkB,CAAC,CAAC;EAClC;AACJ,CAAC","ignoreList":[]}
@@ -1,5 +1,5 @@
1
- import { Resolvers } from "@webiny/handler-graphql/types";
2
- import { Context } from "../types";
1
+ import type { Resolvers } from "@webiny/handler-graphql/types.js";
2
+ import type { Context } from "../types.js";
3
3
  export interface IWebsocketsMutationDisconnectConnectionsArgs {
4
4
  connections: string[];
5
5
  }
@@ -8,6 +8,5 @@ export interface IWebsocketsMutationDisconnectIdentityArgs {
8
8
  }
9
9
  export interface IWebsocketsMutationDisconnectTenantArgs {
10
10
  tenant: string;
11
- locale?: string;
12
11
  }
13
12
  export declare const createResolvers: () => Resolvers<Context>;