Skip to content

Commit 227f3bb

Browse files
committed
Adapter loading robustness
1 parent 1ac4340 commit 227f3bb

File tree

8 files changed

+110
-65
lines changed

8 files changed

+110
-65
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8-
## Version 1.4.1 - 2024-12-xx
8+
## Version 1.4.1 - 2024-12-02
99

1010
### Fixed
1111

package-lock.json

Lines changed: 28 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@
5151
"devDependencies": {
5252
"@cap-js-community/websocket": "./",
5353
"@cap-js/sqlite": "^1.7.7",
54-
"@eslint/js": "^9.15.0",
54+
"@eslint/js": "^9.16.0",
5555
"@sap/cds": "^8.5.0",
5656
"@sap/cds-dk": "^8.5.0",
5757
"@socket.io/redis-adapter": "^8.3.0",
5858
"@socket.io/redis-streams-adapter": "^0.2.2",
59-
"eslint": "^9.15.0",
59+
"eslint": "^9.16.0",
6060
"eslint-config-prettier": "^9.1.0",
6161
"eslint-plugin-jest": "^28.9.0",
6262
"eslint-plugin-n": "^17.14.0",
63-
"globals": "^15.12.0",
63+
"globals": "^15.13.0",
6464
"jest": "^29.7.0",
6565
"passport": "^0.7.0",
6666
"prettier": "^3.4.1",

src/socket/base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ class SocketServer {
358358
* @returns {*} Implementation module
359359
*/
360360
static require(impl, context = "") {
361-
if (impl.startsWith("./") || impl.startsWith("../")) {
361+
if (impl.startsWith("./") || impl.startsWith("../") || impl.startsWith("/")) {
362362
return require(path.join(process.cwd(), impl));
363363
} else if (context) {
364364
try {

src/socket/socket.io.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -261,34 +261,36 @@ class SocketIOServer extends SocketServer {
261261
let client;
262262
let subClient;
263263
const options = { ...config?.options };
264-
const adapterFactory = SocketServer.require(config.impl);
265-
switch (config.impl) {
266-
case "@socket.io/redis-adapter":
267-
if (await redis.connectionCheck(config)) {
268-
client = await redis.createPrimaryClientAndConnect(config);
269-
if (client) {
270-
subClient = await redis.createSecondaryClientAndConnect(config);
271-
if (subClient) {
272-
this.adapter = adapterFactory.createAdapter(client, subClient, options);
264+
const adapterFactory = SocketServer.require(config.impl, "adapter");
265+
if (adapterFactory) {
266+
switch (config.impl) {
267+
case "@socket.io/redis-adapter":
268+
if (await redis.connectionCheck(config)) {
269+
client = await redis.createPrimaryClientAndConnect(config);
270+
if (client) {
271+
subClient = await redis.createSecondaryClientAndConnect(config);
272+
if (subClient) {
273+
this.adapter = adapterFactory.createAdapter(client, subClient, options);
274+
}
273275
}
274276
}
275-
}
276-
break;
277-
case "@socket.io/redis-streams-adapter":
278-
if (await redis.connectionCheck(config)) {
279-
client = await redis.createPrimaryClientAndConnect(config);
280-
if (client) {
281-
this.adapter = adapterFactory.createAdapter(client, options);
277+
break;
278+
case "@socket.io/redis-streams-adapter":
279+
if (await redis.connectionCheck(config)) {
280+
client = await redis.createPrimaryClientAndConnect(config);
281+
if (client) {
282+
this.adapter = adapterFactory.createAdapter(client, options);
283+
}
282284
}
283-
}
284-
break;
285-
default:
286-
this.adapter = adapterFactory.createAdapter(this, options, config);
287-
break;
288-
}
289-
if (this.adapter) {
290-
this.io.adapter(this.adapter);
291-
this.adapterActive = true;
285+
break;
286+
default:
287+
this.adapter = adapterFactory.createAdapter(this, options, config);
288+
break;
289+
}
290+
if (this.adapter) {
291+
this.io.adapter(this.adapter);
292+
this.adapterActive = true;
293+
}
292294
}
293295
}
294296
} catch (err) {

src/socket/ws.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,11 @@ class SocketWSServer extends SocketServer {
267267
const config = { ...this.config?.adapter };
268268
if (config.impl) {
269269
const adapterFactory = SocketServer.require(config.impl, "adapter");
270-
this.adapter = new adapterFactory(this, config);
271-
await this.adapter?.setup?.();
272-
this.adapterActive = !!this.adapter?.client;
270+
if (adapterFactory) {
271+
this.adapter = new adapterFactory(this, config);
272+
await this.adapter?.setup?.();
273+
this.adapterActive = !!this.adapter?.client;
274+
}
273275
}
274276
} catch (err) {
275277
LOG?.error(err);

test/_env/package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
"websocket": {
3232
"kind": "ws",
3333
"impl": null,
34-
"adapter": {
35-
"impl": "redis",
36-
"local": true,
37-
"options": {
38-
"key": "websocket"
34+
"[ws]": {
35+
"adapter": {
36+
"impl": "redis",
37+
"local": true,
38+
"options": {
39+
"key": "websocket"
40+
}
3941
}
4042
},
4143
"[socketio]": {

test/socketio/redis_custom.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use strict";
2+
3+
const cds = require("@sap/cds");
4+
5+
const { connect, disconnect, emitEvent } = require("../_env/util/socket.io");
6+
const xsenv = require("@sap/xsenv");
7+
8+
jest.mock("redis", () => require("../_env/mocks/redis"));
9+
10+
jest.spyOn(xsenv, "serviceCredentials").mockReturnValue({ uri: "uri" });
11+
12+
cds.test(__dirname + "/../_env");
13+
14+
cds.env.websocket.kind = "socket.io";
15+
cds.env.websocket.adapter = {
16+
impl: "./test/_env/mocks/redisCustomAdapter.js",
17+
local: true,
18+
options: {
19+
key: "websocket",
20+
},
21+
};
22+
23+
describe("Redis", () => {
24+
let socket;
25+
26+
beforeAll(async () => {
27+
socket = await connect("/ws/chat");
28+
});
29+
30+
afterAll(async () => {
31+
disconnect(socket);
32+
});
33+
34+
test("Redis Custom Adapter", async () => {
35+
const result = await emitEvent(socket, "message", { text: "test" });
36+
expect(result).toBe("test");
37+
});
38+
});

0 commit comments

Comments
 (0)