Skip to content

Commit

Permalink
Enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
oklemenz2 committed Feb 9, 2024
1 parent 46bc684 commit b4a82e6
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 27 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Version 0.6.2 - 2024-02-xx
## Version 0.7.0 - 2024-02-09

### Added

- Allow custom server implementations via `cds.websocket.impl`
- Allow custom adapter implementations via `cds.websocket.adapter.impl` (kind `ws` only)
- Allow processing of multiple event contexts by annotating event type elements of `many` or `array of` type
- Support for type `date` event contexts as ISO string representation
- Support for type `object` event contexts as JSON stringified representation

## Version 0.6.1 - 2024-01-31

Expand Down
49 changes: 27 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cap-js-community/websocket",
"version": "0.6.2",
"version": "0.7.0",
"description": "WebSocket adapter for CDS",
"homepage": "https://cap.cloud.sap/",
"engines": {
Expand Down
17 changes: 16 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,29 @@ function deriveContexts(event, data) {
if (context) {
isContextEvent = true;
if (data[name] !== undefined && data[name] !== null) {
contexts.push(String(data[name]));
if (Array.isArray(data[name])) {
data[name].forEach((entry) => {
contexts.push(contextString(entry));
});
} else {
contexts.push(contextString(data[name]));
}
}
}
}
}
return isContextEvent ? contexts : undefined;
}

function contextString(value) {
if (value instanceof Date) {
return value.toISOString();
} else if (value instanceof Object) {
return JSON.stringify(value);
}
return String(value);
}

function getDeepEntityColumns(entity) {
const columns = [];
for (const element of Object.values(entity.elements)) {
Expand Down
8 changes: 8 additions & 0 deletions test/_env/srv/handlers/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ module.exports = (srv) => {
return text;
});

srv.on("triggerCustomContextMassEvent", async (req) => {
const ID1 = req.data.ID1;
const ID2 = req.data.ID2;
const text = req.data.text + req.data.num;
await srv.emit("customContextMassEvent", { IDs: [ID1, ID2], text });
return text;
});

srv.on("wsConnect", async (req) => {});

srv.on("wsDisconnect", async (req) => {});
Expand Down
8 changes: 8 additions & 0 deletions test/_env/srv/main.cds
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ service MainService {
}

function triggerCustomContextEvent(ID: UUID, num: Integer, text: String) returns Result;
function triggerCustomContextMassEvent(ID1: UUID, ID2: UUID, num: Integer, text: String) returns Result;

event customContextEvent {
@websocket.context
Expand All @@ -33,6 +34,13 @@ service MainService {
text: String;
}

event customContextMassEvent {
@websocket.context
IDs: array of UUID;
num: Integer;
text: String;
}

action wsConnect();
action wsDisconnect();
action wsContext(context: String, exit: Boolean);
Expand Down
38 changes: 37 additions & 1 deletion test/main_socket.io.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ describe("Chat", () => {
let result = await emitEvent(socket, "triggerCustomContextEvent", { ID, num: 1, text: "test" });
expect(result).toBe("test1");
let eventResult = await eventResultPromise;
expect(eventResult.ID).toBe(ID);
expect(eventResult.text).toBe("test1");
await eventNoResultPromise;

Expand Down Expand Up @@ -174,7 +175,42 @@ describe("Chat", () => {
await eventNoOtherResultPromise;

await enterContext(socket, ID);
await disconnect(socket);
});

test("Event Context Mass", async () => {
const ID1 = "f67af09e-71bc-4293-80f9-cf1ed7fba973";
const ID2 = "e67af09e-71bc-4293-80f9-cf1ed7fba973";

await enterContext(socket, ID1);
await enterContext(socketOther, ID2);
await wait();
let eventResultPromise = waitForEvent(socket, "customContextMassEvent");
let eventResultPromiseOther = waitForEvent(socketOther, "customContextMassEvent");
let result = await emitEvent(socket, "triggerCustomContextMassEvent", { ID1, ID2, num: 1, text: "test" });
expect(result).toBe("test1");
const eventResult = await eventResultPromise;
expect(eventResult.text).toBe("test1");
expect(eventResult.IDs).toEqual([ID1, ID2]);
const eventResultOther = await eventResultPromiseOther;
expect(eventResultOther.text).toBe("test1");
expect(eventResultOther.IDs).toEqual([ID1, ID2]);

await exitContext(socket, ID1);
await exitContext(socketOther, ID2);
await wait();
const eventNoResultPromise = waitForNoEvent(socket, "customContextMassEvent");
const eventNoOtherResultPromise = waitForNoEvent(socketOther, "customContextMassEvent");
result = await emitEvent(socket, "triggerCustomContextMassEvent", { ID1, ID2, num: 1, text: "test" });
expect(result).toBe("test1");
await eventNoResultPromise;
await eventNoOtherResultPromise;

await enterContext(socket, ID1);
await enterContext(socketOther, ID2);
});

test("Disconnects socket (last test)", async () => {
await disconnect(socket); // for test coverage
await wait();
});
});
40 changes: 39 additions & 1 deletion test/main_ws.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ describe("Chat", () => {
let result = await emitEvent(socket, "triggerCustomContextEvent", { ID, num: 1, text: "test" });
expect(result).toBeNull();
let eventResult = await eventResultPromise;
expect(eventResult.ID).toBe(ID);
expect(eventResult.text).toBe("test1");
await eventNoResultPromise;

Expand Down Expand Up @@ -170,7 +171,44 @@ describe("Chat", () => {
await eventNoOtherResultPromise;

await enterContext(socket, ID);
await disconnect(socket);
});

test("Event Context Mass", async () => {
const ID1 = "f67af09e-71bc-4293-80f9-cf1ed7fba973";
const ID2 = "e67af09e-71bc-4293-80f9-cf1ed7fba973";

await enterContext(socket, ID1);
await enterContext(socketOther, ID2);
await wait();
let eventResultPromise = waitForEvent(socket, "customContextMassEvent");
let eventResultPromiseOther = waitForEvent(socketOther, "customContextMassEvent");
let result = await emitEvent(socket, "triggerCustomContextMassEvent", { ID1, ID2, num: 1, text: "test" });
expect(result).toBeNull();
const eventResult = await eventResultPromise;
expect(eventResult.text).toBe("test1");
expect(eventResult.IDs).toEqual([ID1, ID2]);
const eventResultOther = await eventResultPromiseOther;
expect(eventResultOther.text).toBe("test1");
expect(eventResultOther.IDs).toEqual([ID1, ID2]);

await exitContext(socket, ID1);
await exitContext(socketOther, ID2);
await wait();
const eventNoResultPromise = waitForNoEvent(socket, "customContextMassEvent");
const eventNoOtherResultPromise = waitForNoEvent(socketOther, "customContextMassEvent");
result = await emitEvent(socket, "triggerCustomContextMassEvent", { ID1, ID2, num: 1, text: "test" });
expect(result).toBeNull();
await eventNoResultPromise;
await eventNoOtherResultPromise;

await enterContext(socket, ID1);
await enterContext(socketOther, ID2);
});

test("Disconnects socket (last test)", async () => {
await disconnect(socket); // for test coverage
await wait();
const result = await emitEvent(socket, "triggerCustomEvent", { ID: "1234", num: 1, text: "test" });
expect(result).toEqual(new Error("WebSocket is not open: readyState 3 (CLOSED)"));
});
});

0 comments on commit b4a82e6

Please sign in to comment.