@jskit-ai/realtime 0.1.4

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.
@@ -0,0 +1,134 @@
1
+ import { Server as SocketIoServer } from "socket.io";
2
+ import { createAdapter as createSocketIoRedisAdapter } from "@socket.io/redis-adapter";
3
+ import { createClient as createRedisClient } from "redis";
4
+ import { normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
5
+
6
+ const SOCKET_IO_PATH = "/socket.io";
7
+ const REALTIME_REDIS_URL_ENV_KEY = "REALTIME_REDIS_URL";
8
+
9
+ function resolveHttpServer({ httpServer = null, fastify = null } = {}) {
10
+ if (httpServer && typeof httpServer === "object") {
11
+ return httpServer;
12
+ }
13
+ if (fastify && typeof fastify === "object" && fastify.server && typeof fastify.server === "object") {
14
+ return fastify.server;
15
+ }
16
+ throw new Error("createSocketIoServer requires httpServer or fastify.server.");
17
+ }
18
+
19
+ function createSocketIoServer({
20
+ httpServer = null,
21
+ fastify = null,
22
+ options = {},
23
+ ServerCtor = SocketIoServer
24
+ } = {}) {
25
+ if (typeof ServerCtor !== "function") {
26
+ throw new Error("createSocketIoServer requires a valid socket.io Server constructor.");
27
+ }
28
+
29
+ const server = resolveHttpServer({
30
+ httpServer,
31
+ fastify
32
+ });
33
+ const source = options && typeof options === "object" && !Array.isArray(options) ? options : {};
34
+ const normalizedOptions = {
35
+ ...source,
36
+ path: SOCKET_IO_PATH
37
+ };
38
+ return new ServerCtor(server, normalizedOptions);
39
+ }
40
+
41
+ async function closeSocketIoServer(io) {
42
+ if (!io || typeof io.close !== "function") {
43
+ return;
44
+ }
45
+ await new Promise((resolve, reject) => {
46
+ io.close((error) => {
47
+ if (error) {
48
+ if (error.code === "ERR_SERVER_NOT_RUNNING") {
49
+ resolve();
50
+ return;
51
+ }
52
+ reject(error);
53
+ return;
54
+ }
55
+ resolve();
56
+ });
57
+ });
58
+ }
59
+
60
+ function resolveRealtimeRedisUrl(env = {}) {
61
+ const source = env && typeof env === "object" && !Array.isArray(env) ? env : {};
62
+ return normalizeText(source[REALTIME_REDIS_URL_ENV_KEY]);
63
+ }
64
+
65
+ async function configureSocketIoRedisAdapter(
66
+ io,
67
+ { redisUrl = "" } = {}
68
+ ) {
69
+ const normalizedRedisUrl = normalizeText(redisUrl);
70
+ if (!normalizedRedisUrl) {
71
+ return Object.freeze({
72
+ enabled: false,
73
+ redisUrl: "",
74
+ pubClient: null,
75
+ subClient: null
76
+ });
77
+ }
78
+ if (!io || typeof io.adapter !== "function") {
79
+ throw new Error("configureSocketIoRedisAdapter requires socket.io server instance with adapter().");
80
+ }
81
+
82
+ const pubClient = createRedisClient({
83
+ url: normalizedRedisUrl
84
+ });
85
+ const subClient = pubClient.duplicate();
86
+
87
+ try {
88
+ await pubClient.connect();
89
+ await subClient.connect();
90
+ io.adapter(createSocketIoRedisAdapter(pubClient, subClient));
91
+ } catch (error) {
92
+ await closeSocketIoRedisConnections({
93
+ pubClient,
94
+ subClient
95
+ });
96
+ throw error;
97
+ }
98
+
99
+ return Object.freeze({
100
+ enabled: true,
101
+ redisUrl: normalizedRedisUrl,
102
+ pubClient,
103
+ subClient
104
+ });
105
+ }
106
+
107
+ async function closeSocketIoRedisConnections({ pubClient = null, subClient = null } = {}) {
108
+ const connections = [subClient, pubClient];
109
+ for (const connection of connections) {
110
+ if (!connection) {
111
+ continue;
112
+ }
113
+ if (typeof connection.quit === "function") {
114
+ try {
115
+ await connection.quit();
116
+ } catch {}
117
+ continue;
118
+ }
119
+ if (typeof connection.disconnect === "function") {
120
+ try {
121
+ await connection.disconnect();
122
+ } catch {}
123
+ }
124
+ }
125
+ }
126
+
127
+ export {
128
+ createSocketIoServer,
129
+ closeSocketIoServer,
130
+ REALTIME_REDIS_URL_ENV_KEY,
131
+ resolveRealtimeRedisUrl,
132
+ configureSocketIoRedisAdapter,
133
+ closeSocketIoRedisConnections
134
+ };
@@ -0,0 +1,7 @@
1
+ const REALTIME_RUNTIME_SERVER_TOKEN = "runtime.realtime";
2
+ const REALTIME_SOCKET_IO_SERVER_TOKEN = "runtime.realtime.io";
3
+
4
+ export {
5
+ REALTIME_RUNTIME_SERVER_TOKEN,
6
+ REALTIME_SOCKET_IO_SERVER_TOKEN
7
+ };
@@ -0,0 +1,66 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import {
4
+ normalizeRealtimeClientListener,
5
+ registerRealtimeClientListener,
6
+ resolveRealtimeClientListeners
7
+ } from "../src/client/listeners.js";
8
+
9
+ function createSingletonApp() {
10
+ const instances = new Map();
11
+ const singletons = new Map();
12
+ const tags = new Map();
13
+ return {
14
+ singleton(token, factory) {
15
+ singletons.set(token, factory);
16
+ },
17
+ tag(token, tagName) {
18
+ const normalizedTagName = String(tagName || "").trim();
19
+ if (!tags.has(normalizedTagName)) {
20
+ tags.set(normalizedTagName, new Set());
21
+ }
22
+ tags.get(normalizedTagName).add(token);
23
+ },
24
+ resolveTag(tagName) {
25
+ const normalizedTagName = String(tagName || "").trim();
26
+ const tagged = tags.get(normalizedTagName);
27
+ if (!tagged || tagged.size < 1) {
28
+ return [];
29
+ }
30
+ return [...tagged].map((token) => this.make(token));
31
+ },
32
+ make(token) {
33
+ if (instances.has(token)) {
34
+ return instances.get(token);
35
+ }
36
+ if (!singletons.has(token)) {
37
+ throw new Error(`Missing token: ${String(token)}`);
38
+ }
39
+ const resolved = singletons.get(token)(this);
40
+ instances.set(token, resolved);
41
+ return resolved;
42
+ }
43
+ };
44
+ }
45
+
46
+ test("normalizeRealtimeClientListener supports function shorthand", () => {
47
+ const listener = normalizeRealtimeClientListener(function onAnyEvent() {});
48
+ assert.equal(listener.listenerId, "onAnyEvent");
49
+ assert.equal(listener.event, "*");
50
+ assert.equal(typeof listener.handle, "function");
51
+ });
52
+
53
+ test("registerRealtimeClientListener + resolveRealtimeClientListeners round-trip", () => {
54
+ const app = createSingletonApp();
55
+ registerRealtimeClientListener(app, "listener.customers.changed", () => ({
56
+ listenerId: "listener.customers.changed",
57
+ event: "customers.record.changed",
58
+ handle() {}
59
+ }));
60
+
61
+ const listeners = resolveRealtimeClientListeners(app);
62
+ assert.equal(listeners.length, 1);
63
+ assert.equal(listeners[0].listenerId, "listener.customers.changed");
64
+ assert.equal(listeners[0].event, "customers.record.changed");
65
+ assert.equal(typeof listeners[0].handle, "function");
66
+ });
@@ -0,0 +1,81 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+
4
+ import { createSocketIoClient, disconnectSocketIoClient } from "../src/client/runtime.js";
5
+
6
+ test("createSocketIoClient calls connect with fixed socket path", () => {
7
+ const calls = [];
8
+ const socket = {
9
+ id: "socket-1"
10
+ };
11
+ const connect = (...args) => {
12
+ calls.push(args);
13
+ return socket;
14
+ };
15
+
16
+ const created = createSocketIoClient({
17
+ url: " https://example.com ",
18
+ options: {
19
+ path: "realtime",
20
+ withCredentials: true
21
+ },
22
+ connect
23
+ });
24
+
25
+ assert.equal(created, socket);
26
+ assert.equal(calls.length, 1);
27
+ assert.deepEqual(calls[0], [
28
+ "https://example.com",
29
+ {
30
+ path: "/socket.io",
31
+ withCredentials: true
32
+ }
33
+ ]);
34
+ });
35
+
36
+ test("createSocketIoClient supports url-less connection with fixed socket path", () => {
37
+ const calls = [];
38
+ const connect = (...args) => {
39
+ calls.push(args);
40
+ return {
41
+ id: "socket-2"
42
+ };
43
+ };
44
+
45
+ createSocketIoClient({
46
+ options: {
47
+ path: "ws"
48
+ },
49
+ connect
50
+ });
51
+
52
+ assert.equal(calls.length, 1);
53
+ assert.deepEqual(calls[0], [
54
+ {
55
+ path: "/socket.io"
56
+ }
57
+ ]);
58
+ });
59
+
60
+ test("createSocketIoClient rejects invalid connect function", () => {
61
+ assert.throws(
62
+ () => createSocketIoClient({ connect: null }),
63
+ /requires a valid socket\.io client connect function/
64
+ );
65
+ });
66
+
67
+ test("disconnectSocketIoClient calls socket.disconnect when available", () => {
68
+ let disconnected = false;
69
+ disconnectSocketIoClient({
70
+ disconnect() {
71
+ disconnected = true;
72
+ }
73
+ });
74
+ assert.equal(disconnected, true);
75
+ });
76
+
77
+ test("disconnectSocketIoClient is a no-op for missing socket", () => {
78
+ disconnectSocketIoClient(null);
79
+ disconnectSocketIoClient({});
80
+ assert.equal(true, true);
81
+ });
@@ -0,0 +1,45 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+
4
+ import * as serverApi from "../src/server/RealtimeServiceProvider.js";
5
+ import * as serverRuntimeApi from "../src/server/runtime.js";
6
+ import * as clientApi from "../src/client/RealtimeClientProvider.js";
7
+ import * as clientRuntimeApi from "../src/client/runtime.js";
8
+ import * as clientListenerApi from "../src/client/listeners.js";
9
+ import * as serverTokens from "../src/server/tokens.js";
10
+ import * as clientTokens from "../src/client/tokens.js";
11
+
12
+ test("server entrypoint exports provider only", () => {
13
+ assert.equal(typeof serverApi.RealtimeServiceProvider, "function");
14
+ assert.deepEqual(Object.keys(serverApi).sort(), ["RealtimeServiceProvider"]);
15
+ });
16
+
17
+ test("client entrypoint exports provider only", () => {
18
+ assert.equal(typeof clientApi.RealtimeClientProvider, "function");
19
+ assert.deepEqual(Object.keys(clientApi).sort(), ["RealtimeClientProvider"]);
20
+ });
21
+
22
+ test("token entrypoints export runtime token constants", () => {
23
+ assert.equal(serverTokens.REALTIME_RUNTIME_SERVER_TOKEN, "runtime.realtime");
24
+ assert.equal(serverTokens.REALTIME_SOCKET_IO_SERVER_TOKEN, "runtime.realtime.io");
25
+ assert.equal(clientTokens.REALTIME_RUNTIME_CLIENT_TOKEN, "runtime.realtime.client");
26
+ assert.equal(clientTokens.REALTIME_SOCKET_CLIENT_TOKEN, "runtime.realtime.client.socket");
27
+ assert.equal(typeof clientTokens.REALTIME_SOCKET_CLIENT_INJECTION_KEY, "symbol");
28
+ assert.equal(typeof clientTokens.REALTIME_CLIENT_LISTENER_TAG, "symbol");
29
+ });
30
+
31
+ test("server runtime entrypoint exports server-only helpers", () => {
32
+ assert.equal(typeof serverRuntimeApi.createSocketIoServer, "function");
33
+ assert.equal(typeof serverRuntimeApi.closeSocketIoServer, "function");
34
+ });
35
+
36
+ test("client runtime entrypoint exports client-only helpers", () => {
37
+ assert.equal(typeof clientRuntimeApi.createSocketIoClient, "function");
38
+ assert.equal(typeof clientRuntimeApi.disconnectSocketIoClient, "function");
39
+ });
40
+
41
+ test("client listeners entrypoint exports realtime listener registration helpers", () => {
42
+ assert.equal(typeof clientListenerApi.registerRealtimeClientListener, "function");
43
+ assert.equal(typeof clientListenerApi.resolveRealtimeClientListeners, "function");
44
+ assert.equal(typeof clientListenerApi.normalizeRealtimeClientListener, "function");
45
+ });