@runtime-inspector/transport-ws 0.0.0-stage → 0.1.0-alpha.1
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.
- package/LICENSE +21 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +205 -0
- package/package.json +39 -3
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Giulio Amato
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface BrokerOptions {
|
|
2
|
+
port?: number;
|
|
3
|
+
host?: string;
|
|
4
|
+
token?: string;
|
|
5
|
+
}
|
|
6
|
+
interface RuntimeInspectorBroker {
|
|
7
|
+
brokerId: string;
|
|
8
|
+
port: number;
|
|
9
|
+
host: string;
|
|
10
|
+
close(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
declare function startBroker(options?: BrokerOptions): RuntimeInspectorBroker;
|
|
13
|
+
|
|
14
|
+
export { type BrokerOptions, type RuntimeInspectorBroker, startBroker };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { createHash, randomUUID, timingSafeEqual } from "crypto";
|
|
3
|
+
import { WebSocketServer } from "ws";
|
|
4
|
+
import {
|
|
5
|
+
parseRIPMessage,
|
|
6
|
+
RIP_VERSION
|
|
7
|
+
} from "@runtime-inspector/protocol";
|
|
8
|
+
function startBroker(options = {}) {
|
|
9
|
+
const port = options.port ?? Number(process.env.RUNTIME_INSPECTOR_PORT ?? 4577);
|
|
10
|
+
const host = options.host ?? "127.0.0.1";
|
|
11
|
+
const token = options.token;
|
|
12
|
+
const brokerId = `broker-${randomUUID()}`;
|
|
13
|
+
const server = new WebSocketServer({ host, port });
|
|
14
|
+
const clients = /* @__PURE__ */ new Map();
|
|
15
|
+
const schemasByRuntime = /* @__PURE__ */ new Map();
|
|
16
|
+
const traceSchemasByRuntime = /* @__PURE__ */ new Map();
|
|
17
|
+
const onlineRuntimeIds = /* @__PURE__ */ new Set();
|
|
18
|
+
server.on("connection", (socket) => {
|
|
19
|
+
const record = {
|
|
20
|
+
id: `client-${randomUUID()}`,
|
|
21
|
+
socket
|
|
22
|
+
};
|
|
23
|
+
clients.set(socket, record);
|
|
24
|
+
socket.on("message", (data) => {
|
|
25
|
+
let message;
|
|
26
|
+
try {
|
|
27
|
+
message = parseRIPMessage(JSON.parse(data.toString()));
|
|
28
|
+
} catch (error) {
|
|
29
|
+
send(socket, {
|
|
30
|
+
type: "error",
|
|
31
|
+
code: "INVALID_MESSAGE",
|
|
32
|
+
message: "Message is not valid Runtime Inspector Protocol JSON.",
|
|
33
|
+
cause: error instanceof Error ? error.message : error
|
|
34
|
+
});
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (message.type === "handshake.hello") {
|
|
38
|
+
if (message.protocolVersion !== RIP_VERSION) {
|
|
39
|
+
send(socket, {
|
|
40
|
+
type: "error",
|
|
41
|
+
code: "VERSION_MISMATCH",
|
|
42
|
+
message: `Protocol version mismatch: client sent "${message.protocolVersion}", broker expects "${RIP_VERSION}".`
|
|
43
|
+
});
|
|
44
|
+
socket.close();
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (token && (message.role === "panel" || message.role === "workspace") && !tokensMatch(message.token, token)) {
|
|
48
|
+
send(socket, {
|
|
49
|
+
type: "error",
|
|
50
|
+
code: "UNAUTHORIZED",
|
|
51
|
+
message: `Missing or invalid ${message.role} token.`
|
|
52
|
+
});
|
|
53
|
+
socket.close();
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
record.role = message.role;
|
|
57
|
+
record.id = message.clientId;
|
|
58
|
+
const accept = {
|
|
59
|
+
type: "handshake.accept",
|
|
60
|
+
protocolVersion: RIP_VERSION,
|
|
61
|
+
brokerId,
|
|
62
|
+
clientId: message.clientId
|
|
63
|
+
};
|
|
64
|
+
send(socket, accept);
|
|
65
|
+
if (message.role === "panel") {
|
|
66
|
+
replaySchemas(socket, schemasByRuntime, onlineRuntimeIds);
|
|
67
|
+
replayTraceSchemas(socket, traceSchemasByRuntime);
|
|
68
|
+
}
|
|
69
|
+
if (message.role === "runtime") {
|
|
70
|
+
onlineRuntimeIds.add(record.id);
|
|
71
|
+
broadcastRuntimeStatus(clients, record, true, schemasByRuntime.get(record.id)?.schema.id);
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (message.type === "schema.publish" && record.role === "runtime") {
|
|
76
|
+
record.schemaId = message.schema.id;
|
|
77
|
+
schemasByRuntime.set(record.id, message);
|
|
78
|
+
}
|
|
79
|
+
if (message.type === "schema.dispose" && record.role === "runtime") {
|
|
80
|
+
if (record.schemaId === message.schemaId) {
|
|
81
|
+
record.schemaId = void 0;
|
|
82
|
+
}
|
|
83
|
+
schemasByRuntime.delete(record.id);
|
|
84
|
+
traceSchemasByRuntime.delete(record.id);
|
|
85
|
+
}
|
|
86
|
+
if (message.type === "trace.schema.publish" && record.role === "runtime") {
|
|
87
|
+
traceSchemasByRuntime.set(record.id, message);
|
|
88
|
+
}
|
|
89
|
+
if (message.type === "trace.schema.dispose" && record.role === "runtime") {
|
|
90
|
+
traceSchemasByRuntime.delete(record.id);
|
|
91
|
+
}
|
|
92
|
+
routeMessage(clients, record, message);
|
|
93
|
+
});
|
|
94
|
+
socket.on("close", () => {
|
|
95
|
+
clients.delete(socket);
|
|
96
|
+
if (record.role === "runtime") {
|
|
97
|
+
onlineRuntimeIds.delete(record.id);
|
|
98
|
+
broadcastRuntimeStatus(clients, record, false, schemasByRuntime.get(record.id)?.schema.id);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
return {
|
|
103
|
+
brokerId,
|
|
104
|
+
get port() {
|
|
105
|
+
const address = server.address();
|
|
106
|
+
return typeof address === "object" && address ? address.port : port;
|
|
107
|
+
},
|
|
108
|
+
host,
|
|
109
|
+
close: () => new Promise((resolve, reject) => {
|
|
110
|
+
for (const client of clients.values()) {
|
|
111
|
+
client.socket.close();
|
|
112
|
+
}
|
|
113
|
+
server.close((error) => error ? reject(error) : resolve());
|
|
114
|
+
})
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function replaySchemas(socket, schemasByRuntime, onlineRuntimeIds) {
|
|
118
|
+
for (const [runtimeId, schemaMessage] of schemasByRuntime.entries()) {
|
|
119
|
+
send(socket, schemaMessage);
|
|
120
|
+
if (!onlineRuntimeIds.has(runtimeId)) {
|
|
121
|
+
send(socket, {
|
|
122
|
+
type: "runtime.status",
|
|
123
|
+
online: false,
|
|
124
|
+
clientId: runtimeId,
|
|
125
|
+
schemaId: schemaMessage.schema.id
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function replayTraceSchemas(socket, traceSchemasByRuntime) {
|
|
131
|
+
for (const traceSchema of traceSchemasByRuntime.values()) {
|
|
132
|
+
send(socket, traceSchema);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function broadcastRuntimeStatus(clients, runtime, online, schemaId) {
|
|
136
|
+
for (const target of clients.values()) {
|
|
137
|
+
if (target.role !== "panel") continue;
|
|
138
|
+
send(target.socket, {
|
|
139
|
+
type: "runtime.status",
|
|
140
|
+
online,
|
|
141
|
+
clientId: runtime.id,
|
|
142
|
+
schemaId
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function routeMessage(clients, sender, message) {
|
|
147
|
+
if (message.type === "source.apply") {
|
|
148
|
+
if (sender.role !== "panel") return;
|
|
149
|
+
forwardToRole(clients, sender, message, "workspace");
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (message.type === "source.applyResult") {
|
|
153
|
+
if (sender.role !== "workspace") return;
|
|
154
|
+
forwardToRole(clients, sender, message, "panel");
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (sender.role === "panel") {
|
|
158
|
+
const schemaId = messageSchemaId(message);
|
|
159
|
+
if (schemaId) {
|
|
160
|
+
forwardToRuntimeSchema(clients, sender, message, schemaId);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
forwardToOppositeRole(clients, sender, message);
|
|
165
|
+
}
|
|
166
|
+
function messageSchemaId(message) {
|
|
167
|
+
return "schemaId" in message && typeof message.schemaId === "string" ? message.schemaId : void 0;
|
|
168
|
+
}
|
|
169
|
+
function forwardToRuntimeSchema(clients, sender, message, schemaId) {
|
|
170
|
+
for (const target of clients.values()) {
|
|
171
|
+
if (target.socket === sender.socket) continue;
|
|
172
|
+
if (target.role !== "runtime") continue;
|
|
173
|
+
if (target.schemaId !== schemaId) continue;
|
|
174
|
+
send(target.socket, message);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function forwardToOppositeRole(clients, sender, message) {
|
|
178
|
+
if (sender.role !== "runtime" && sender.role !== "panel") return;
|
|
179
|
+
for (const target of clients.values()) {
|
|
180
|
+
if (target.socket === sender.socket) continue;
|
|
181
|
+
if (target.role !== "runtime" && target.role !== "panel") continue;
|
|
182
|
+
if (target.role === sender.role) continue;
|
|
183
|
+
send(target.socket, message);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function forwardToRole(clients, sender, message, targetRole) {
|
|
187
|
+
for (const target of clients.values()) {
|
|
188
|
+
if (target.socket === sender.socket) continue;
|
|
189
|
+
if (target.role !== targetRole) continue;
|
|
190
|
+
send(target.socket, message);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
function tokensMatch(received, expected) {
|
|
194
|
+
if (typeof received !== "string") return false;
|
|
195
|
+
const digest = (value) => createHash("sha256").update(value).digest();
|
|
196
|
+
return timingSafeEqual(digest(received), digest(expected));
|
|
197
|
+
}
|
|
198
|
+
function send(socket, message) {
|
|
199
|
+
if (socket.readyState === socket.OPEN) {
|
|
200
|
+
socket.send(JSON.stringify(message));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
export {
|
|
204
|
+
startBroker
|
|
205
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@runtime-inspector/transport-ws",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0-alpha.1",
|
|
4
|
+
"description": "Local WebSocket broker for Runtime Inspector.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/AmatoGiulio/runtime-inspector.git",
|
|
9
|
+
"directory": "packages/transport-ws"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"ws": "^8.18.0",
|
|
28
|
+
"@runtime-inspector/protocol": "0.1.0-alpha.1"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/ws": "^8.5.13",
|
|
32
|
+
"tsup": "^8.3.5"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsup src/index.ts --format esm --dts",
|
|
36
|
+
"dev": "tsx src/dev.ts",
|
|
37
|
+
"test": "vitest run",
|
|
38
|
+
"typecheck": "tsc --noEmit",
|
|
39
|
+
"lint": "tsc --noEmit",
|
|
40
|
+
"clean": "rm -rf dist"
|
|
41
|
+
}
|
|
6
42
|
}
|
package/README.md
DELETED