@procwire/transport 0.1.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/README.md +844 -0
- package/dist/channel/builder.d.ts +68 -0
- package/dist/channel/builder.d.ts.map +1 -0
- package/dist/channel/builder.js +120 -0
- package/dist/channel/builder.js.map +1 -0
- package/dist/channel/index.d.ts +6 -0
- package/dist/channel/index.d.ts.map +1 -0
- package/dist/channel/index.js +6 -0
- package/dist/channel/index.js.map +1 -0
- package/dist/channel/quickstart.d.ts +94 -0
- package/dist/channel/quickstart.d.ts.map +1 -0
- package/dist/channel/quickstart.js +104 -0
- package/dist/channel/quickstart.js.map +1 -0
- package/dist/channel/request-channel.d.ts +119 -0
- package/dist/channel/request-channel.d.ts.map +1 -0
- package/dist/channel/request-channel.js +476 -0
- package/dist/channel/request-channel.js.map +1 -0
- package/dist/channel/types.d.ts +226 -0
- package/dist/channel/types.d.ts.map +1 -0
- package/dist/channel/types.js +2 -0
- package/dist/channel/types.js.map +1 -0
- package/dist/framing/index.d.ts +4 -0
- package/dist/framing/index.d.ts.map +1 -0
- package/dist/framing/index.js +4 -0
- package/dist/framing/index.js.map +1 -0
- package/dist/framing/length-prefixed.d.ts +55 -0
- package/dist/framing/length-prefixed.d.ts.map +1 -0
- package/dist/framing/length-prefixed.js +102 -0
- package/dist/framing/length-prefixed.js.map +1 -0
- package/dist/framing/line-delimited.d.ts +61 -0
- package/dist/framing/line-delimited.d.ts.map +1 -0
- package/dist/framing/line-delimited.js +94 -0
- package/dist/framing/line-delimited.js.map +1 -0
- package/dist/framing/types.d.ts +35 -0
- package/dist/framing/types.d.ts.map +1 -0
- package/dist/framing/types.js +2 -0
- package/dist/framing/types.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/process/handle.d.ts +64 -0
- package/dist/process/handle.d.ts.map +1 -0
- package/dist/process/handle.js +107 -0
- package/dist/process/handle.js.map +1 -0
- package/dist/process/index.d.ts +37 -0
- package/dist/process/index.d.ts.map +1 -0
- package/dist/process/index.js +37 -0
- package/dist/process/index.js.map +1 -0
- package/dist/process/manager.d.ts +58 -0
- package/dist/process/manager.d.ts.map +1 -0
- package/dist/process/manager.js +360 -0
- package/dist/process/manager.js.map +1 -0
- package/dist/process/types.d.ts +322 -0
- package/dist/process/types.d.ts.map +1 -0
- package/dist/process/types.js +2 -0
- package/dist/process/types.js.map +1 -0
- package/dist/protocol/index.d.ts +4 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +6 -0
- package/dist/protocol/index.js.map +1 -0
- package/dist/protocol/jsonrpc.d.ts +146 -0
- package/dist/protocol/jsonrpc.d.ts.map +1 -0
- package/dist/protocol/jsonrpc.js +288 -0
- package/dist/protocol/jsonrpc.js.map +1 -0
- package/dist/protocol/simple.d.ts +139 -0
- package/dist/protocol/simple.d.ts.map +1 -0
- package/dist/protocol/simple.js +297 -0
- package/dist/protocol/simple.js.map +1 -0
- package/dist/protocol/types.d.ts +117 -0
- package/dist/protocol/types.d.ts.map +1 -0
- package/dist/protocol/types.js +2 -0
- package/dist/protocol/types.js.map +1 -0
- package/dist/serialization/index.d.ts +5 -0
- package/dist/serialization/index.d.ts.map +1 -0
- package/dist/serialization/index.js +5 -0
- package/dist/serialization/index.js.map +1 -0
- package/dist/serialization/json.d.ts +66 -0
- package/dist/serialization/json.d.ts.map +1 -0
- package/dist/serialization/json.js +66 -0
- package/dist/serialization/json.js.map +1 -0
- package/dist/serialization/raw.d.ts +38 -0
- package/dist/serialization/raw.d.ts.map +1 -0
- package/dist/serialization/raw.js +41 -0
- package/dist/serialization/raw.js.map +1 -0
- package/dist/serialization/registry.d.ts +91 -0
- package/dist/serialization/registry.d.ts.map +1 -0
- package/dist/serialization/registry.js +119 -0
- package/dist/serialization/registry.js.map +1 -0
- package/dist/serialization/types.d.ts +27 -0
- package/dist/serialization/types.d.ts.map +1 -0
- package/dist/serialization/types.js +2 -0
- package/dist/serialization/types.js.map +1 -0
- package/dist/transport/factory.d.ts +139 -0
- package/dist/transport/factory.d.ts.map +1 -0
- package/dist/transport/factory.js +162 -0
- package/dist/transport/factory.js.map +1 -0
- package/dist/transport/index.d.ts +6 -0
- package/dist/transport/index.d.ts.map +1 -0
- package/dist/transport/index.js +9 -0
- package/dist/transport/index.js.map +1 -0
- package/dist/transport/socket-server.d.ts +48 -0
- package/dist/transport/socket-server.d.ts.map +1 -0
- package/dist/transport/socket-server.js +215 -0
- package/dist/transport/socket-server.js.map +1 -0
- package/dist/transport/socket-transport.d.ts +67 -0
- package/dist/transport/socket-transport.d.ts.map +1 -0
- package/dist/transport/socket-transport.js +193 -0
- package/dist/transport/socket-transport.js.map +1 -0
- package/dist/transport/stdio-transport.d.ts +94 -0
- package/dist/transport/stdio-transport.d.ts.map +1 -0
- package/dist/transport/stdio-transport.js +234 -0
- package/dist/transport/stdio-transport.js.map +1 -0
- package/dist/transport/types.d.ts +131 -0
- package/dist/transport/types.d.ts.map +1 -0
- package/dist/transport/types.js +2 -0
- package/dist/transport/types.js.map +1 -0
- package/dist/utils/assert.d.ts +16 -0
- package/dist/utils/assert.d.ts.map +1 -0
- package/dist/utils/assert.js +31 -0
- package/dist/utils/assert.js.map +1 -0
- package/dist/utils/disposables.d.ts +38 -0
- package/dist/utils/disposables.d.ts.map +1 -0
- package/dist/utils/disposables.js +59 -0
- package/dist/utils/disposables.js.map +1 -0
- package/dist/utils/errors.d.ts +43 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +69 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/events.d.ts +58 -0
- package/dist/utils/events.d.ts.map +1 -0
- package/dist/utils/events.js +95 -0
- package/dist/utils/events.js.map +1 -0
- package/dist/utils/index.d.ts +8 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +8 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/pipe-path.d.ts +48 -0
- package/dist/utils/pipe-path.d.ts.map +1 -0
- package/dist/utils/pipe-path.js +89 -0
- package/dist/utils/pipe-path.js.map +1 -0
- package/dist/utils/platform.d.ts +16 -0
- package/dist/utils/platform.d.ts.map +1 -0
- package/dist/utils/platform.js +22 -0
- package/dist/utils/platform.js.map +1 -0
- package/dist/utils/time.d.ts +38 -0
- package/dist/utils/time.d.ts.map +1 -0
- package/dist/utils/time.js +55 -0
- package/dist/utils/time.js.map +1 -0
- package/package.json +85 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { ProtocolError } from "../utils/errors.js";
|
|
2
|
+
/**
|
|
3
|
+
* Minimal protocol implementation with no JSON-RPC overhead.
|
|
4
|
+
*
|
|
5
|
+
* Provides a lightweight request/response/notification protocol
|
|
6
|
+
* suitable for high-performance or custom IPC scenarios.
|
|
7
|
+
*
|
|
8
|
+
* Message format:
|
|
9
|
+
* - Request: `{ type: 'request', id, method, params? }`
|
|
10
|
+
* - Response: `{ type: 'response', id, result?, error? }`
|
|
11
|
+
* - Notification: `{ type: 'notification', method, params? }`
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const protocol = new SimpleProtocol();
|
|
16
|
+
* const req = protocol.createRequest('getUser', { id: 42 });
|
|
17
|
+
* const res = protocol.createResponse(req.id, { name: 'Alice' });
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export class SimpleProtocol {
|
|
21
|
+
name = "simple";
|
|
22
|
+
version = "1.0";
|
|
23
|
+
idCounter = 0;
|
|
24
|
+
/**
|
|
25
|
+
* Creates a simple protocol request message.
|
|
26
|
+
*
|
|
27
|
+
* @param method - Method name (must be non-empty string)
|
|
28
|
+
* @param params - Optional parameters
|
|
29
|
+
* @param id - Optional request ID (auto-generated if not provided)
|
|
30
|
+
* @returns Simple request object
|
|
31
|
+
* @throws {ProtocolError} if method is invalid
|
|
32
|
+
*/
|
|
33
|
+
createRequest(method, params, id) {
|
|
34
|
+
if (typeof method !== "string" || method.length === 0) {
|
|
35
|
+
throw new ProtocolError("Method must be a non-empty string");
|
|
36
|
+
}
|
|
37
|
+
const requestId = id ?? this.generateId();
|
|
38
|
+
const request = {
|
|
39
|
+
type: "request",
|
|
40
|
+
id: requestId,
|
|
41
|
+
method,
|
|
42
|
+
};
|
|
43
|
+
if (params !== undefined) {
|
|
44
|
+
request.params = params;
|
|
45
|
+
}
|
|
46
|
+
return request;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Creates a simple protocol success response message.
|
|
50
|
+
*
|
|
51
|
+
* @param id - Request ID
|
|
52
|
+
* @param result - Response result
|
|
53
|
+
* @returns Simple response object
|
|
54
|
+
*/
|
|
55
|
+
createResponse(id, result) {
|
|
56
|
+
return {
|
|
57
|
+
type: "response",
|
|
58
|
+
id,
|
|
59
|
+
result,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Creates a simple protocol error response message.
|
|
64
|
+
*
|
|
65
|
+
* @param id - Request ID
|
|
66
|
+
* @param error - Error details
|
|
67
|
+
* @returns Simple error response object
|
|
68
|
+
*/
|
|
69
|
+
createErrorResponse(id, error) {
|
|
70
|
+
return {
|
|
71
|
+
type: "response",
|
|
72
|
+
id,
|
|
73
|
+
error,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Creates a simple protocol notification message.
|
|
78
|
+
*
|
|
79
|
+
* @param method - Method name (must be non-empty string)
|
|
80
|
+
* @param params - Optional parameters
|
|
81
|
+
* @returns Simple notification object
|
|
82
|
+
* @throws {ProtocolError} if method is invalid
|
|
83
|
+
*/
|
|
84
|
+
createNotification(method, params) {
|
|
85
|
+
if (typeof method !== "string" || method.length === 0) {
|
|
86
|
+
throw new ProtocolError("Method must be a non-empty string");
|
|
87
|
+
}
|
|
88
|
+
const notification = {
|
|
89
|
+
type: "notification",
|
|
90
|
+
method,
|
|
91
|
+
};
|
|
92
|
+
if (params !== undefined) {
|
|
93
|
+
notification.params = params;
|
|
94
|
+
}
|
|
95
|
+
return notification;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Parses incoming data as simple protocol message.
|
|
99
|
+
* Does not throw - returns 'invalid' kind for malformed messages.
|
|
100
|
+
*
|
|
101
|
+
* @param data - Raw incoming data
|
|
102
|
+
* @returns ParsedMessage discriminated union
|
|
103
|
+
*/
|
|
104
|
+
parseMessage(data) {
|
|
105
|
+
// Must be an object
|
|
106
|
+
if (!isObject(data)) {
|
|
107
|
+
return {
|
|
108
|
+
kind: "invalid",
|
|
109
|
+
error: {
|
|
110
|
+
code: -1,
|
|
111
|
+
message: "Message must be an object",
|
|
112
|
+
},
|
|
113
|
+
raw: data,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
// Must have type field
|
|
117
|
+
if (!("type" in data) || typeof data.type !== "string") {
|
|
118
|
+
return {
|
|
119
|
+
kind: "invalid",
|
|
120
|
+
error: {
|
|
121
|
+
code: -1,
|
|
122
|
+
message: "Message must have type field",
|
|
123
|
+
},
|
|
124
|
+
raw: data,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
switch (data.type) {
|
|
128
|
+
case "request":
|
|
129
|
+
return this.parseRequest(data);
|
|
130
|
+
case "response":
|
|
131
|
+
return this.parseResponse(data);
|
|
132
|
+
case "notification":
|
|
133
|
+
return this.parseNotification(data);
|
|
134
|
+
default:
|
|
135
|
+
return {
|
|
136
|
+
kind: "invalid",
|
|
137
|
+
error: {
|
|
138
|
+
code: -1,
|
|
139
|
+
message: `Unknown message type: ${data.type}`,
|
|
140
|
+
},
|
|
141
|
+
raw: data,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Type guard for request messages.
|
|
147
|
+
*/
|
|
148
|
+
isRequest(msg) {
|
|
149
|
+
return msg.kind === "request";
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Type guard for response messages.
|
|
153
|
+
*/
|
|
154
|
+
isResponse(msg) {
|
|
155
|
+
return msg.kind === "response";
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Type guard for notification messages.
|
|
159
|
+
*/
|
|
160
|
+
isNotification(msg) {
|
|
161
|
+
return msg.kind === "notification";
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Parses a request message.
|
|
165
|
+
*/
|
|
166
|
+
parseRequest(data) {
|
|
167
|
+
// Must have id
|
|
168
|
+
if (!("id" in data)) {
|
|
169
|
+
return {
|
|
170
|
+
kind: "invalid",
|
|
171
|
+
error: {
|
|
172
|
+
code: -1,
|
|
173
|
+
message: "Request must have id",
|
|
174
|
+
},
|
|
175
|
+
raw: data,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
// Validate id type
|
|
179
|
+
if (!isValidId(data.id)) {
|
|
180
|
+
return {
|
|
181
|
+
kind: "invalid",
|
|
182
|
+
error: {
|
|
183
|
+
code: -1,
|
|
184
|
+
message: "Request id must be string or number",
|
|
185
|
+
},
|
|
186
|
+
raw: data,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
// Must have method
|
|
190
|
+
if (!("method" in data) || typeof data.method !== "string" || data.method.length === 0) {
|
|
191
|
+
return {
|
|
192
|
+
kind: "invalid",
|
|
193
|
+
error: {
|
|
194
|
+
code: -1,
|
|
195
|
+
message: "Request must have non-empty method string",
|
|
196
|
+
},
|
|
197
|
+
raw: data,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
return {
|
|
201
|
+
kind: "request",
|
|
202
|
+
message: data,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Parses a response message.
|
|
207
|
+
*/
|
|
208
|
+
parseResponse(data) {
|
|
209
|
+
// Must have id
|
|
210
|
+
if (!("id" in data)) {
|
|
211
|
+
return {
|
|
212
|
+
kind: "invalid",
|
|
213
|
+
error: {
|
|
214
|
+
code: -1,
|
|
215
|
+
message: "Response must have id",
|
|
216
|
+
},
|
|
217
|
+
raw: data,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
// Validate id type
|
|
221
|
+
if (!isValidId(data.id)) {
|
|
222
|
+
return {
|
|
223
|
+
kind: "invalid",
|
|
224
|
+
error: {
|
|
225
|
+
code: -1,
|
|
226
|
+
message: "Response id must be string or number",
|
|
227
|
+
},
|
|
228
|
+
raw: data,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
// Must have either result or error (or both is acceptable in simple protocol)
|
|
232
|
+
if (!("result" in data) && !("error" in data)) {
|
|
233
|
+
return {
|
|
234
|
+
kind: "invalid",
|
|
235
|
+
error: {
|
|
236
|
+
code: -1,
|
|
237
|
+
message: "Response must have result or error",
|
|
238
|
+
},
|
|
239
|
+
raw: data,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
return {
|
|
243
|
+
kind: "response",
|
|
244
|
+
message: data,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Parses a notification message.
|
|
249
|
+
*/
|
|
250
|
+
parseNotification(data) {
|
|
251
|
+
// Must have method
|
|
252
|
+
if (!("method" in data) || typeof data.method !== "string" || data.method.length === 0) {
|
|
253
|
+
return {
|
|
254
|
+
kind: "invalid",
|
|
255
|
+
error: {
|
|
256
|
+
code: -1,
|
|
257
|
+
message: "Notification must have non-empty method string",
|
|
258
|
+
},
|
|
259
|
+
raw: data,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
// Must NOT have id
|
|
263
|
+
if ("id" in data) {
|
|
264
|
+
return {
|
|
265
|
+
kind: "invalid",
|
|
266
|
+
error: {
|
|
267
|
+
code: -1,
|
|
268
|
+
message: "Notification must not have id",
|
|
269
|
+
},
|
|
270
|
+
raw: data,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
return {
|
|
274
|
+
kind: "notification",
|
|
275
|
+
message: data,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Generates a unique request ID.
|
|
280
|
+
*/
|
|
281
|
+
generateId() {
|
|
282
|
+
return ++this.idCounter;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Type guard to check if value is an object (non-null).
|
|
287
|
+
*/
|
|
288
|
+
function isObject(value) {
|
|
289
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Type guard to validate request ID type.
|
|
293
|
+
*/
|
|
294
|
+
function isValidId(value) {
|
|
295
|
+
return typeof value === "string" || typeof value === "number";
|
|
296
|
+
}
|
|
297
|
+
//# sourceMappingURL=simple.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simple.js","sourceRoot":"","sources":["../../src/protocol/simple.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AA6CnD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,OAAO,cAAc;IAGT,IAAI,GAAG,QAAQ,CAAC;IAChB,OAAO,GAAG,KAAK,CAAC;IAExB,SAAS,GAAG,CAAC,CAAC;IAEtB;;;;;;;;OAQG;IACH,aAAa,CAAC,MAAc,EAAE,MAAgB,EAAE,EAAc;QAC5D,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACtD,MAAM,IAAI,aAAa,CAAC,mCAAmC,CAAC,CAAC;QAC/D,CAAC;QAED,MAAM,SAAS,GAAG,EAAE,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;QAE1C,MAAM,OAAO,GAAkB;YAC7B,IAAI,EAAE,SAAS;YACf,EAAE,EAAE,SAAS;YACb,MAAM;SACP,CAAC;QAEF,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;YACzB,OAAO,CAAC,MAAM,GAAG,MAA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"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request identifier (string or number).
|
|
3
|
+
*/
|
|
4
|
+
export type RequestId = string | number;
|
|
5
|
+
/**
|
|
6
|
+
* Protocol error data structure (not the runtime error class).
|
|
7
|
+
* Used in error responses at protocol level.
|
|
8
|
+
*/
|
|
9
|
+
export interface ProtocolDataError {
|
|
10
|
+
/**
|
|
11
|
+
* Error code (e.g., -32600 for JSON-RPC, or custom codes).
|
|
12
|
+
*/
|
|
13
|
+
code: number;
|
|
14
|
+
/**
|
|
15
|
+
* Human-readable error message.
|
|
16
|
+
*/
|
|
17
|
+
message: string;
|
|
18
|
+
/**
|
|
19
|
+
* Optional additional error data.
|
|
20
|
+
*/
|
|
21
|
+
data?: unknown;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Parsed message discriminated union.
|
|
25
|
+
* Represents the result of parsing an incoming protocol message.
|
|
26
|
+
*/
|
|
27
|
+
export type ParsedMessage<TReq = unknown, TRes = unknown, TNotif = unknown> = {
|
|
28
|
+
kind: "request";
|
|
29
|
+
message: TReq;
|
|
30
|
+
} | {
|
|
31
|
+
kind: "response";
|
|
32
|
+
message: TRes;
|
|
33
|
+
} | {
|
|
34
|
+
kind: "notification";
|
|
35
|
+
message: TNotif;
|
|
36
|
+
} | {
|
|
37
|
+
kind: "invalid";
|
|
38
|
+
error: ProtocolDataError;
|
|
39
|
+
raw: unknown;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Protocol layer interface for request/response messaging.
|
|
43
|
+
* Implementations: JSON-RPC 2.0, SimpleProtocol.
|
|
44
|
+
*
|
|
45
|
+
* Generic type parameters represent the wire format of messages,
|
|
46
|
+
* not application-level data types.
|
|
47
|
+
*
|
|
48
|
+
* @template TReq - Request message type (wire format)
|
|
49
|
+
* @template TRes - Response message type (wire format)
|
|
50
|
+
* @template TNotif - Notification message type (wire format)
|
|
51
|
+
*/
|
|
52
|
+
export interface Protocol<TReq = unknown, TRes = unknown, TNotif = unknown> {
|
|
53
|
+
/**
|
|
54
|
+
* Protocol name identifier.
|
|
55
|
+
*/
|
|
56
|
+
readonly name: string;
|
|
57
|
+
/**
|
|
58
|
+
* Protocol version.
|
|
59
|
+
*/
|
|
60
|
+
readonly version: string;
|
|
61
|
+
/**
|
|
62
|
+
* Creates a request message.
|
|
63
|
+
* @param method - Method name
|
|
64
|
+
* @param params - Optional parameters
|
|
65
|
+
* @param id - Optional request ID (auto-generated if not provided)
|
|
66
|
+
* @returns Wire format request message
|
|
67
|
+
*/
|
|
68
|
+
createRequest(method: string, params?: unknown, id?: RequestId): TReq;
|
|
69
|
+
/**
|
|
70
|
+
* Creates a success response message.
|
|
71
|
+
* @param id - Request ID this response corresponds to
|
|
72
|
+
* @param result - Response result data
|
|
73
|
+
* @returns Wire format response message
|
|
74
|
+
*/
|
|
75
|
+
createResponse(id: RequestId, result: unknown): TRes;
|
|
76
|
+
/**
|
|
77
|
+
* Creates an error response message.
|
|
78
|
+
* @param id - Request ID this error corresponds to
|
|
79
|
+
* @param error - Error details
|
|
80
|
+
* @returns Wire format error response message
|
|
81
|
+
*/
|
|
82
|
+
createErrorResponse(id: RequestId, error: ProtocolDataError): TRes;
|
|
83
|
+
/**
|
|
84
|
+
* Creates a notification message (no response expected).
|
|
85
|
+
* @param method - Method name
|
|
86
|
+
* @param params - Optional parameters
|
|
87
|
+
* @returns Wire format notification message
|
|
88
|
+
*/
|
|
89
|
+
createNotification(method: string, params?: unknown): TNotif;
|
|
90
|
+
/**
|
|
91
|
+
* Parses incoming message and determines its type.
|
|
92
|
+
* Does not throw - returns 'invalid' kind for malformed messages.
|
|
93
|
+
*
|
|
94
|
+
* @param data - Raw incoming data
|
|
95
|
+
* @returns ParsedMessage discriminated union
|
|
96
|
+
*/
|
|
97
|
+
parseMessage(data: unknown): ParsedMessage<TReq, TRes, TNotif>;
|
|
98
|
+
/**
|
|
99
|
+
* Type guard for request messages.
|
|
100
|
+
*/
|
|
101
|
+
isRequest(msg: ParsedMessage<TReq, TRes, TNotif>): msg is Extract<ParsedMessage<TReq, TRes, TNotif>, {
|
|
102
|
+
kind: "request";
|
|
103
|
+
}>;
|
|
104
|
+
/**
|
|
105
|
+
* Type guard for response messages.
|
|
106
|
+
*/
|
|
107
|
+
isResponse(msg: ParsedMessage<TReq, TRes, TNotif>): msg is Extract<ParsedMessage<TReq, TRes, TNotif>, {
|
|
108
|
+
kind: "response";
|
|
109
|
+
}>;
|
|
110
|
+
/**
|
|
111
|
+
* Type guard for notification messages.
|
|
112
|
+
*/
|
|
113
|
+
isNotification(msg: ParsedMessage<TReq, TRes, TNotif>): msg is Extract<ParsedMessage<TReq, TRes, TNotif>, {
|
|
114
|
+
kind: "notification";
|
|
115
|
+
}>;
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/protocol/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,MAAM,CAAC;AAExC;;;GAGG;AACH,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED;;;GAGG;AACH,MAAM,MAAM,aAAa,CAAC,IAAI,GAAG,OAAO,EAAE,IAAI,GAAG,OAAO,EAAE,MAAM,GAAG,OAAO,IACtE;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,OAAO,EAAE,IAAI,CAAA;CAAE,GAClC;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,OAAO,EAAE,IAAI,CAAA;CAAE,GACnC;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GACzC;IAAE,IAAI,EAAE,SAAS,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AE,MAAM,CAAC,EAAE;QAAE,IAAI,EAAE,UAAU,CAAA;KAAE,CAAC,CAAC;IAE3E;;OAEG;IACH,cAAc,CACZ,GAAG,EAAE,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,GACrC,GAAG,IAAI,OAAO,CAAC,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,EAAE;QAAE,IAAI,EAAE,cAAc,CAAA;KAAE,CAAC,CAAC;CAChF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/protocol/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/serialization/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/serialization/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { SerializationCodec } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Options for JSON codec configuration.
|
|
4
|
+
*/
|
|
5
|
+
export interface JsonCodecOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Function to transform values during serialization.
|
|
8
|
+
* Applied during JSON.stringify().
|
|
9
|
+
*/
|
|
10
|
+
replacer?: (key: string, value: unknown) => unknown;
|
|
11
|
+
/**
|
|
12
|
+
* Function to transform values during deserialization.
|
|
13
|
+
* Applied during JSON.parse().
|
|
14
|
+
*/
|
|
15
|
+
reviver?: (key: string, value: unknown) => unknown;
|
|
16
|
+
/**
|
|
17
|
+
* Indentation for formatted JSON output.
|
|
18
|
+
* Useful for debugging. Default: undefined (compact).
|
|
19
|
+
*/
|
|
20
|
+
space?: number | string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* JSON serialization codec with zero dependencies.
|
|
24
|
+
*
|
|
25
|
+
* Converts objects to/from UTF-8 encoded JSON buffers.
|
|
26
|
+
* Supports custom replacer/reviver functions for advanced serialization logic.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const codec = new JsonCodec();
|
|
31
|
+
* const buffer = codec.serialize({ foo: 'bar' });
|
|
32
|
+
* const obj = codec.deserialize(buffer);
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @example With custom replacer
|
|
36
|
+
* ```ts
|
|
37
|
+
* const codec = new JsonCodec({
|
|
38
|
+
* replacer: (key, value) => key === 'password' ? undefined : value
|
|
39
|
+
* });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare class JsonCodec<T = unknown> implements SerializationCodec<T> {
|
|
43
|
+
readonly name = "json";
|
|
44
|
+
readonly contentType = "application/json";
|
|
45
|
+
private readonly replacer;
|
|
46
|
+
private readonly reviver;
|
|
47
|
+
private readonly space;
|
|
48
|
+
constructor(options?: JsonCodecOptions);
|
|
49
|
+
/**
|
|
50
|
+
* Serializes a value to a UTF-8 encoded JSON buffer.
|
|
51
|
+
*
|
|
52
|
+
* @param value - Value to serialize
|
|
53
|
+
* @returns Buffer containing UTF-8 encoded JSON
|
|
54
|
+
* @throws {SerializationError} if JSON.stringify fails
|
|
55
|
+
*/
|
|
56
|
+
serialize(value: T): Buffer;
|
|
57
|
+
/**
|
|
58
|
+
* Deserializes a UTF-8 encoded JSON buffer to a value.
|
|
59
|
+
*
|
|
60
|
+
* @param buffer - Buffer containing UTF-8 encoded JSON
|
|
61
|
+
* @returns Deserialized value
|
|
62
|
+
* @throws {SerializationError} if JSON.parse fails
|
|
63
|
+
*/
|
|
64
|
+
deserialize(buffer: Buffer): T;
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=json.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/serialization/json.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAErD;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;IAEpD;;;OAGG;IACH,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;IAEnD;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACzB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,SAAS,CAAC,CAAC,GAAG,OAAO,CAAE,YAAW,kBAAkB,CAAC,CAAC,CAAC;IAClE,SAAgB,IAAI,UAAU;IAC9B,SAAgB,WAAW,sBAAsB;IAEjD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAyD;IAClF,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAyD;IACjF,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA8B;gBAExC,OAAO,GAAE,gBAAqB;IAM1C;;;;;;OAMG;IACH,SAAS,CAAC,KAAK,EAAE,CAAC,GAAG,MAAM;IAY3B;;;;;;OAMG;IACH,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,CAAC;CAW/B"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { SerializationError } from "../utils/errors.js";
|
|
2
|
+
/**
|
|
3
|
+
* JSON serialization codec with zero dependencies.
|
|
4
|
+
*
|
|
5
|
+
* Converts objects to/from UTF-8 encoded JSON buffers.
|
|
6
|
+
* Supports custom replacer/reviver functions for advanced serialization logic.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const codec = new JsonCodec();
|
|
11
|
+
* const buffer = codec.serialize({ foo: 'bar' });
|
|
12
|
+
* const obj = codec.deserialize(buffer);
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @example With custom replacer
|
|
16
|
+
* ```ts
|
|
17
|
+
* const codec = new JsonCodec({
|
|
18
|
+
* replacer: (key, value) => key === 'password' ? undefined : value
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export class JsonCodec {
|
|
23
|
+
name = "json";
|
|
24
|
+
contentType = "application/json";
|
|
25
|
+
replacer;
|
|
26
|
+
reviver;
|
|
27
|
+
space;
|
|
28
|
+
constructor(options = {}) {
|
|
29
|
+
this.replacer = options.replacer;
|
|
30
|
+
this.reviver = options.reviver;
|
|
31
|
+
this.space = options.space;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Serializes a value to a UTF-8 encoded JSON buffer.
|
|
35
|
+
*
|
|
36
|
+
* @param value - Value to serialize
|
|
37
|
+
* @returns Buffer containing UTF-8 encoded JSON
|
|
38
|
+
* @throws {SerializationError} if JSON.stringify fails
|
|
39
|
+
*/
|
|
40
|
+
serialize(value) {
|
|
41
|
+
try {
|
|
42
|
+
const json = JSON.stringify(value, this.replacer, this.space);
|
|
43
|
+
return Buffer.from(json, "utf8");
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
throw new SerializationError(`Failed to serialize value to JSON: ${error instanceof Error ? error.message : String(error)}`, error);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Deserializes a UTF-8 encoded JSON buffer to a value.
|
|
51
|
+
*
|
|
52
|
+
* @param buffer - Buffer containing UTF-8 encoded JSON
|
|
53
|
+
* @returns Deserialized value
|
|
54
|
+
* @throws {SerializationError} if JSON.parse fails
|
|
55
|
+
*/
|
|
56
|
+
deserialize(buffer) {
|
|
57
|
+
try {
|
|
58
|
+
const json = buffer.toString("utf8");
|
|
59
|
+
return JSON.parse(json, this.reviver);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
throw new SerializationError(`Failed to deserialize JSON: ${error instanceof Error ? error.message : String(error)}`, error);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=json.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.js","sourceRoot":"","sources":["../../src/serialization/json.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AA0BxD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,SAAS;IACJ,IAAI,GAAG,MAAM,CAAC;IACd,WAAW,GAAG,kBAAkB,CAAC;IAEhC,QAAQ,CAAyD;IACjE,OAAO,CAAyD;IAChE,KAAK,CAA8B;IAEpD,YAAY,UAA4B,EAAE;QACxC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QACjC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;QAC/B,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;IAC7B,CAAC;IAED;;;;;;OAMG;IACH,SAAS,CAAC,KAAQ;QAChB,IAAI,CAAC;YACH,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,CAAC,QAAoD,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;YAC1G,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACnC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,kBAAkB,CAC1B,sCAAsC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EAC9F,KAAK,CACN,CAAC;QACJ,CAAC;IACH,CAAC;IAED;;;;;;OAMG;IACH,WAAW,CAAC,MAAc;QACxB,IAAI,CAAC;YACH,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;YACrC,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,OAAmD,CAAM,CAAC;QACzF,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,kBAAkB,CAC1B,+BAA+B,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EACvF,KAAK,CACN,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { SerializationCodec } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Raw binary passthrough codec (zero-copy).
|
|
4
|
+
*
|
|
5
|
+
* Performs no serialization - simply passes Buffer instances through unchanged.
|
|
6
|
+
* Useful for binary protocols or when pre-serialized data is available.
|
|
7
|
+
*
|
|
8
|
+
* **Important**: This codec does not copy buffers. Mutations to the returned
|
|
9
|
+
* buffer will affect the original data. If you need isolation, copy the buffer
|
|
10
|
+
* yourself using `Buffer.from(buffer)`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const codec = new RawCodec();
|
|
15
|
+
* const buffer = Buffer.from([1, 2, 3]);
|
|
16
|
+
* const serialized = codec.serialize(buffer);
|
|
17
|
+
* console.log(serialized === buffer); // true (no copy)
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class RawCodec implements SerializationCodec<Buffer> {
|
|
21
|
+
readonly name = "raw";
|
|
22
|
+
readonly contentType = "application/octet-stream";
|
|
23
|
+
/**
|
|
24
|
+
* Returns the buffer unchanged (passthrough, no copy).
|
|
25
|
+
*
|
|
26
|
+
* @param value - Buffer to serialize
|
|
27
|
+
* @returns The same buffer instance
|
|
28
|
+
*/
|
|
29
|
+
serialize(value: Buffer): Buffer;
|
|
30
|
+
/**
|
|
31
|
+
* Returns the buffer unchanged (passthrough, no copy).
|
|
32
|
+
*
|
|
33
|
+
* @param buffer - Buffer to deserialize
|
|
34
|
+
* @returns The same buffer instance
|
|
35
|
+
*/
|
|
36
|
+
deserialize(buffer: Buffer): Buffer;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=raw.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"raw.d.ts","sourceRoot":"","sources":["../../src/serialization/raw.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAErD;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,QAAS,YAAW,kBAAkB,CAAC,MAAM,CAAC;IACzD,SAAgB,IAAI,SAAS;IAC7B,SAAgB,WAAW,8BAA8B;IAEzD;;;;;OAKG;IACH,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAIhC;;;;;OAKG;IACH,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;CAGpC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Raw binary passthrough codec (zero-copy).
|
|
3
|
+
*
|
|
4
|
+
* Performs no serialization - simply passes Buffer instances through unchanged.
|
|
5
|
+
* Useful for binary protocols or when pre-serialized data is available.
|
|
6
|
+
*
|
|
7
|
+
* **Important**: This codec does not copy buffers. Mutations to the returned
|
|
8
|
+
* buffer will affect the original data. If you need isolation, copy the buffer
|
|
9
|
+
* yourself using `Buffer.from(buffer)`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const codec = new RawCodec();
|
|
14
|
+
* const buffer = Buffer.from([1, 2, 3]);
|
|
15
|
+
* const serialized = codec.serialize(buffer);
|
|
16
|
+
* console.log(serialized === buffer); // true (no copy)
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export class RawCodec {
|
|
20
|
+
name = "raw";
|
|
21
|
+
contentType = "application/octet-stream";
|
|
22
|
+
/**
|
|
23
|
+
* Returns the buffer unchanged (passthrough, no copy).
|
|
24
|
+
*
|
|
25
|
+
* @param value - Buffer to serialize
|
|
26
|
+
* @returns The same buffer instance
|
|
27
|
+
*/
|
|
28
|
+
serialize(value) {
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Returns the buffer unchanged (passthrough, no copy).
|
|
33
|
+
*
|
|
34
|
+
* @param buffer - Buffer to deserialize
|
|
35
|
+
* @returns The same buffer instance
|
|
36
|
+
*/
|
|
37
|
+
deserialize(buffer) {
|
|
38
|
+
return buffer;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=raw.js.map
|