Skip to content

Commit

Permalink
feat: websockets (#3877)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunozoric authored Mar 1, 2024
1 parent 8f7038c commit 700e820
Show file tree
Hide file tree
Showing 128 changed files with 6,076 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pullRequests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ jobs:
- 18
package: >-
${{
fromJson('[{"cmd":"packages/api","id":"api"},{"cmd":"packages/api-admin-settings","id":"api-admin-settings"},{"cmd":"packages/api-authentication","id":"api-authentication"},{"cmd":"packages/api-authentication-cognito","id":"api-authentication-cognito"},{"cmd":"packages/api-dynamodb-to-elasticsearch","id":"api-dynamodb-to-elasticsearch"},{"cmd":"packages/api-headless-cms-ddb","id":"api-headless-cms-ddb"},{"cmd":"packages/api-wcp","id":"api-wcp"},{"cmd":"packages/app-aco","id":"app-aco"},{"cmd":"packages/app-admin","id":"app-admin"},{"cmd":"packages/cwp-template-aws","id":"cwp-template-aws"},{"cmd":"packages/data-migration","id":"data-migration"},{"cmd":"packages/db-dynamodb","id":"db-dynamodb"},{"cmd":"packages/form","id":"form"},{"cmd":"packages/handler","id":"handler"},{"cmd":"packages/handler-aws","id":"handler-aws"},{"cmd":"packages/handler-graphql","id":"handler-graphql"},{"cmd":"packages/handler-logs","id":"handler-logs"},{"cmd":"packages/ioc","id":"ioc"},{"cmd":"packages/lexical-converter","id":"lexical-converter"},{"cmd":"packages/plugins","id":"plugins"},{"cmd":"packages/pubsub","id":"pubsub"},{"cmd":"packages/react-composition","id":"react-composition"},{"cmd":"packages/react-properties","id":"react-properties"},{"cmd":"packages/react-rich-text-lexical-renderer","id":"react-rich-text-lexical-renderer"},{"cmd":"packages/utils","id":"utils"},{"cmd":"packages/validation","id":"validation"}]')
fromJson('[{"cmd":"packages/api","id":"api"},{"cmd":"packages/api-admin-settings","id":"api-admin-settings"},{"cmd":"packages/api-authentication","id":"api-authentication"},{"cmd":"packages/api-authentication-cognito","id":"api-authentication-cognito"},{"cmd":"packages/api-dynamodb-to-elasticsearch","id":"api-dynamodb-to-elasticsearch"},{"cmd":"packages/api-headless-cms-ddb","id":"api-headless-cms-ddb"},{"cmd":"packages/api-wcp","id":"api-wcp"},{"cmd":"packages/api-websockets","id":"api-websockets"},{"cmd":"packages/app-aco","id":"app-aco"},{"cmd":"packages/app-admin","id":"app-admin"},{"cmd":"packages/cwp-template-aws","id":"cwp-template-aws"},{"cmd":"packages/data-migration","id":"data-migration"},{"cmd":"packages/db-dynamodb","id":"db-dynamodb"},{"cmd":"packages/form","id":"form"},{"cmd":"packages/handler","id":"handler"},{"cmd":"packages/handler-aws","id":"handler-aws"},{"cmd":"packages/handler-graphql","id":"handler-graphql"},{"cmd":"packages/handler-logs","id":"handler-logs"},{"cmd":"packages/ioc","id":"ioc"},{"cmd":"packages/lexical-converter","id":"lexical-converter"},{"cmd":"packages/plugins","id":"plugins"},{"cmd":"packages/pubsub","id":"pubsub"},{"cmd":"packages/react-composition","id":"react-composition"},{"cmd":"packages/react-properties","id":"react-properties"},{"cmd":"packages/react-rich-text-lexical-renderer","id":"react-rich-text-lexical-renderer"},{"cmd":"packages/utils","id":"utils"},{"cmd":"packages/validation","id":"validation"}]')
}}
runs-on: ${{ matrix.os }}
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pushDev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ jobs:
- 18
package: >-
${{
fromJson('[{"cmd":"packages/api","id":"api"},{"cmd":"packages/api-admin-settings","id":"api-admin-settings"},{"cmd":"packages/api-authentication","id":"api-authentication"},{"cmd":"packages/api-authentication-cognito","id":"api-authentication-cognito"},{"cmd":"packages/api-dynamodb-to-elasticsearch","id":"api-dynamodb-to-elasticsearch"},{"cmd":"packages/api-headless-cms-ddb","id":"api-headless-cms-ddb"},{"cmd":"packages/api-wcp","id":"api-wcp"},{"cmd":"packages/app-aco","id":"app-aco"},{"cmd":"packages/app-admin","id":"app-admin"},{"cmd":"packages/cwp-template-aws","id":"cwp-template-aws"},{"cmd":"packages/data-migration","id":"data-migration"},{"cmd":"packages/db-dynamodb","id":"db-dynamodb"},{"cmd":"packages/form","id":"form"},{"cmd":"packages/handler","id":"handler"},{"cmd":"packages/handler-aws","id":"handler-aws"},{"cmd":"packages/handler-graphql","id":"handler-graphql"},{"cmd":"packages/handler-logs","id":"handler-logs"},{"cmd":"packages/ioc","id":"ioc"},{"cmd":"packages/lexical-converter","id":"lexical-converter"},{"cmd":"packages/plugins","id":"plugins"},{"cmd":"packages/pubsub","id":"pubsub"},{"cmd":"packages/react-composition","id":"react-composition"},{"cmd":"packages/react-properties","id":"react-properties"},{"cmd":"packages/react-rich-text-lexical-renderer","id":"react-rich-text-lexical-renderer"},{"cmd":"packages/utils","id":"utils"},{"cmd":"packages/validation","id":"validation"}]')
fromJson('[{"cmd":"packages/api","id":"api"},{"cmd":"packages/api-admin-settings","id":"api-admin-settings"},{"cmd":"packages/api-authentication","id":"api-authentication"},{"cmd":"packages/api-authentication-cognito","id":"api-authentication-cognito"},{"cmd":"packages/api-dynamodb-to-elasticsearch","id":"api-dynamodb-to-elasticsearch"},{"cmd":"packages/api-headless-cms-ddb","id":"api-headless-cms-ddb"},{"cmd":"packages/api-wcp","id":"api-wcp"},{"cmd":"packages/api-websockets","id":"api-websockets"},{"cmd":"packages/app-aco","id":"app-aco"},{"cmd":"packages/app-admin","id":"app-admin"},{"cmd":"packages/cwp-template-aws","id":"cwp-template-aws"},{"cmd":"packages/data-migration","id":"data-migration"},{"cmd":"packages/db-dynamodb","id":"db-dynamodb"},{"cmd":"packages/form","id":"form"},{"cmd":"packages/handler","id":"handler"},{"cmd":"packages/handler-aws","id":"handler-aws"},{"cmd":"packages/handler-graphql","id":"handler-graphql"},{"cmd":"packages/handler-logs","id":"handler-logs"},{"cmd":"packages/ioc","id":"ioc"},{"cmd":"packages/lexical-converter","id":"lexical-converter"},{"cmd":"packages/plugins","id":"plugins"},{"cmd":"packages/pubsub","id":"pubsub"},{"cmd":"packages/react-composition","id":"react-composition"},{"cmd":"packages/react-properties","id":"react-properties"},{"cmd":"packages/react-rich-text-lexical-renderer","id":"react-rich-text-lexical-renderer"},{"cmd":"packages/utils","id":"utils"},{"cmd":"packages/validation","id":"validation"}]')
}}
runs-on: ${{ matrix.os }}
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pushNext.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ jobs:
- 18
package: >-
${{
fromJson('[{"cmd":"packages/api","id":"api"},{"cmd":"packages/api-admin-settings","id":"api-admin-settings"},{"cmd":"packages/api-authentication","id":"api-authentication"},{"cmd":"packages/api-authentication-cognito","id":"api-authentication-cognito"},{"cmd":"packages/api-dynamodb-to-elasticsearch","id":"api-dynamodb-to-elasticsearch"},{"cmd":"packages/api-headless-cms-ddb","id":"api-headless-cms-ddb"},{"cmd":"packages/api-wcp","id":"api-wcp"},{"cmd":"packages/app-aco","id":"app-aco"},{"cmd":"packages/app-admin","id":"app-admin"},{"cmd":"packages/cwp-template-aws","id":"cwp-template-aws"},{"cmd":"packages/data-migration","id":"data-migration"},{"cmd":"packages/db-dynamodb","id":"db-dynamodb"},{"cmd":"packages/form","id":"form"},{"cmd":"packages/handler","id":"handler"},{"cmd":"packages/handler-aws","id":"handler-aws"},{"cmd":"packages/handler-graphql","id":"handler-graphql"},{"cmd":"packages/handler-logs","id":"handler-logs"},{"cmd":"packages/ioc","id":"ioc"},{"cmd":"packages/lexical-converter","id":"lexical-converter"},{"cmd":"packages/plugins","id":"plugins"},{"cmd":"packages/pubsub","id":"pubsub"},{"cmd":"packages/react-composition","id":"react-composition"},{"cmd":"packages/react-properties","id":"react-properties"},{"cmd":"packages/react-rich-text-lexical-renderer","id":"react-rich-text-lexical-renderer"},{"cmd":"packages/utils","id":"utils"},{"cmd":"packages/validation","id":"validation"}]')
fromJson('[{"cmd":"packages/api","id":"api"},{"cmd":"packages/api-admin-settings","id":"api-admin-settings"},{"cmd":"packages/api-authentication","id":"api-authentication"},{"cmd":"packages/api-authentication-cognito","id":"api-authentication-cognito"},{"cmd":"packages/api-dynamodb-to-elasticsearch","id":"api-dynamodb-to-elasticsearch"},{"cmd":"packages/api-headless-cms-ddb","id":"api-headless-cms-ddb"},{"cmd":"packages/api-wcp","id":"api-wcp"},{"cmd":"packages/api-websockets","id":"api-websockets"},{"cmd":"packages/app-aco","id":"app-aco"},{"cmd":"packages/app-admin","id":"app-admin"},{"cmd":"packages/cwp-template-aws","id":"cwp-template-aws"},{"cmd":"packages/data-migration","id":"data-migration"},{"cmd":"packages/db-dynamodb","id":"db-dynamodb"},{"cmd":"packages/form","id":"form"},{"cmd":"packages/handler","id":"handler"},{"cmd":"packages/handler-aws","id":"handler-aws"},{"cmd":"packages/handler-graphql","id":"handler-graphql"},{"cmd":"packages/handler-logs","id":"handler-logs"},{"cmd":"packages/ioc","id":"ioc"},{"cmd":"packages/lexical-converter","id":"lexical-converter"},{"cmd":"packages/plugins","id":"plugins"},{"cmd":"packages/pubsub","id":"pubsub"},{"cmd":"packages/react-composition","id":"react-composition"},{"cmd":"packages/react-properties","id":"react-properties"},{"cmd":"packages/react-rich-text-lexical-renderer","id":"react-rich-text-lexical-renderer"},{"cmd":"packages/utils","id":"utils"},{"cmd":"packages/validation","id":"validation"}]')
}}
runs-on: ${{ matrix.os }}
env:
Expand Down
1 change: 1 addition & 0 deletions apps/api/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@webiny/api-tenancy-so-ddb": "0.0.0",
"@webiny/api-tenant-manager": "0.0.0",
"@webiny/api-wcp": "0.0.0",
"@webiny/api-websockets": "0.0.0",
"@webiny/aws-sdk": "0.0.0",
"@webiny/cli": "0.0.0",
"@webiny/db-dynamodb": "0.0.0",
Expand Down
2 changes: 2 additions & 0 deletions apps/api/graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import scaffoldsPlugins from "./plugins/scaffolds";
import { createBenchmarkEnablePlugin } from "~/plugins/benchmarkEnable";
import { createCountDynamoDbTask } from "~/plugins/countDynamoDbTask";
import { createContinuingTask } from "~/plugins/continuingTask";
import { createWebsockets } from "@webiny/api-websockets";

const debug = process.env.DEBUG === "true";
const documentClient = getDocumentClient();
Expand All @@ -60,6 +61,7 @@ export const handler = createHandler({
tenantManager(),
i18nPlugins(),
i18nDynamoDbStorageOperations(),
createWebsockets(),
createHeadlessCmsContext({
storageOperations: createHeadlessCmsStorageOperations({
documentClient
Expand Down
4 changes: 3 additions & 1 deletion apps/api/graphql/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@
"@webiny/api-background-tasks-ddb/*": ["../../../packages/api-background-tasks-ddb/src/*"],
"@webiny/api-background-tasks-ddb": ["../../../packages/api-background-tasks-ddb/src"],
"@webiny/tasks/*": ["../../../packages/tasks/src/*"],
"@webiny/tasks": ["../../../packages/tasks/src"]
"@webiny/tasks": ["../../../packages/tasks/src"],
"@webiny/api-websockets/*": ["../../../packages/api-websockets/src/*"],
"@webiny/api-websockets": ["../../../packages/api-websockets/src"]
},
"baseUrl": "."
}
Expand Down
18 changes: 17 additions & 1 deletion jest.config.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ const createDynaliteTables = (options = {}) => {
{ AttributeName: "PK", AttributeType: "S" },
{ AttributeName: "SK", AttributeType: "S" },
{ AttributeName: "GSI1_PK", AttributeType: "S" },
{ AttributeName: "GSI1_SK", AttributeType: "S" }
{ AttributeName: "GSI1_SK", AttributeType: "S" },
{ AttributeName: "GSI2_PK", AttributeType: "S" },
{ AttributeName: "GSI2_SK", AttributeType: "S" }
],
ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 },
GlobalSecondaryIndexes: [
Expand All @@ -124,6 +126,20 @@ const createDynaliteTables = (options = {}) => {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
},
{
IndexName: "GSI2",
KeySchema: [
{ AttributeName: "GSI2_PK", KeyType: "HASH" },
{ AttributeName: "GSI2_SK", KeyType: "RANGE" }
],
Projection: {
ProjectionType: "ALL"
},
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
}
],
data: options.data || []
Expand Down
1 change: 0 additions & 1 deletion packages/api-aco/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"@babel/preset-env": "^7.23.9",
"@babel/preset-typescript": "^7.23.3",
"@babel/runtime": "^7.23.9",
"@types/ungap__structured-clone": "^0.3.0",
"@webiny/api-admin-users": "0.0.0",
"@webiny/api-file-manager": "0.0.0",
"@webiny/api-i18n-ddb": "0.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/api-websockets/.babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@webiny/project-utils").createBabelConfigForNode({ path: __dirname });
21 changes: 21 additions & 0 deletions packages/api-websockets/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Webiny

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
10 changes: 10 additions & 0 deletions packages/api-websockets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# @webiny/api-websockets
[![](https://img.shields.io/npm/dw/@webiny/api-websockets.svg)](https://www.npmjs.com/package/@webiny/api-websockets)
[![](https://img.shields.io/npm/v/@webiny/api-websockets.svg)](https://www.npmjs.com/package/@webiny/api-websockets)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)

## Install
```
yarn add @webiny/api-websockets
```
103 changes: 103 additions & 0 deletions packages/api-websockets/__tests__/context/websocketsContext.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { getDocumentClient } from "@webiny/project-utils/testing/dynamodb";
import { WebsocketsContext } from "~/context/WebsocketsContext";
import { WebsocketsConnectionRegistry } from "~/registry";
import { MockWebsocketsTransport } from "~tests/mocks/MockWebsocketsTransport";

interface IMockData {
mockData?: boolean;
}

describe("websockets context", () => {
it("should properly list connections", async () => {
const documentClient = getDocumentClient();
const registry = new WebsocketsConnectionRegistry(documentClient);
const transport = new MockWebsocketsTransport();

const context = new WebsocketsContext(registry, transport);
expect(context).toBeInstanceOf(WebsocketsContext);

const resultNoConnections = await context.listConnections({
where: {
identityId: "id-1"
}
});
expect(resultNoConnections).toEqual([]);

await registry.register({
connectionId: "connection-1",
tenant: "root",
locale: "en-US",
identity: {
id: "id-1",
displayName: "John Doe",
type: "admin"
},
domainName: "https://webiny.com",
stage: "dev",
connectedOn: new Date().toISOString()
});

const resultWithConnections = await context.listConnections({
where: {
identityId: "id-1"
}
});
expect(resultWithConnections).toEqual([
{
connectionId: "connection-1",
tenant: "root",
locale: "en-US",
identity: {
id: "id-1",
displayName: "John Doe",
type: "admin"
},
domainName: "https://webiny.com",
stage: "dev",
connectedOn: expect.any(String)
}
]);
});

it("should properly send a message via transport", async () => {
const documentClient = getDocumentClient();
const registry = new WebsocketsConnectionRegistry(documentClient);
const transport = new MockWebsocketsTransport();

const context = new WebsocketsContext(registry, transport);

await registry.register({
connectionId: "connection-1",
tenant: "root",
locale: "en-US",
identity: {
id: "id-1",
displayName: "John Doe",
type: "admin"
},
domainName: "https://webiny.com",
stage: "dev",
connectedOn: new Date().toISOString()
});

await context.send<IMockData>(
{
id: "id-1",
displayName: "John Doe",
type: "admin"
},
{
data: {
mockData: true
}
}
);

expect(transport.messages.size).toBe(1);
expect(transport.messages.get("connection-1")).toEqual({
data: {
mockData: true
}
});
});
});
Loading

0 comments on commit 700e820

Please sign in to comment.