vortez-io 1.0.0
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/.gitignore +15 -0
- package/LICENSE +201 -0
- package/PROTOCOL.md +245 -0
- package/README.md +103 -0
- package/build/Frame.d.ts +118 -0
- package/build/Frame.js +241 -0
- package/build/Frame.js.map +1 -0
- package/build/Isomorphic.d.ts +7 -0
- package/build/Isomorphic.js +2 -0
- package/build/Isomorphic.js.map +1 -0
- package/build/RequestManager.d.ts +74 -0
- package/build/RequestManager.js +61 -0
- package/build/RequestManager.js.map +1 -0
- package/build/VIO.d.ts +82 -0
- package/build/VIO.js +219 -0
- package/build/VIO.js.map +1 -0
- package/build/VIOError.d.ts +17 -0
- package/build/VIOError.js +25 -0
- package/build/VIOError.js.map +1 -0
- package/build/VIOStrict.d.ts +62 -0
- package/build/VIOStrict.js +103 -0
- package/build/VIOStrict.js.map +1 -0
- package/build/index.d.ts +4 -0
- package/build/index.js +5 -0
- package/build/index.js.map +1 -0
- package/build/support/LineParser.d.ts +25 -0
- package/build/support/LineParser.js +50 -0
- package/build/support/LineParser.js.map +1 -0
- package/build/support/UTF8Codec.d.ts +34 -0
- package/build/support/UTF8Codec.js +155 -0
- package/build/support/UTF8Codec.js.map +1 -0
- package/build/support/UUID.d.ts +62 -0
- package/build/support/UUID.js +109 -0
- package/build/support/UUID.js.map +1 -0
- package/build/support/Varint.d.ts +26 -0
- package/build/support/Varint.js +61 -0
- package/build/support/Varint.js.map +1 -0
- package/docs/PROTOCOL-PROPOSE.md +252 -0
- package/package.json +35 -0
- package/scripts/build.sh +14 -0
- package/src/Frame.ts +312 -0
- package/src/Isomorphic.ts +7 -0
- package/src/RequestManager.ts +114 -0
- package/src/VIO.ts +194 -0
- package/src/VIOError.ts +22 -0
- package/src/VIOStrict.ts +123 -0
- package/src/index.ts +4 -0
- package/src/support/LineParser.ts +55 -0
- package/src/support/UTF8Codec.ts +166 -0
- package/src/support/UUID.ts +125 -0
- package/src/support/Varint.ts +62 -0
- package/tsconfig.json +17 -0
package/build/Frame.js
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module vortez-io
|
|
3
|
+
* @author NetFeez <netfeez.dev@gemail.com>
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
* @description Implements the core Frame structure of the Vortez IO protocol, including encoding and decoding logic for binary frames.
|
|
6
|
+
* The Frame class provides static methods to encode and decode frames according to the protocol specification, handling various frame types, modes, and identifier formats.
|
|
7
|
+
* It also includes validation to ensure that frames adhere to the protocol rules regarding type and identifier combinations.
|
|
8
|
+
* This module is essential for constructing and interpreting the binary messages exchanged in the Vortez IO protocol.
|
|
9
|
+
* The Frame structure consists of a header byte that encodes the frame type and mode, followed by an optional identifier (which can be a UUID or a string), and the payload data.
|
|
10
|
+
* The encoding and decoding processes involve careful parsing of the header and identifier fields, as well as proper handling of the payload data to ensure compliance with the protocol.
|
|
11
|
+
* The Frame class also defines the various types, modes, and identifier formats supported by the protocol, along with utility methods for converting between different representations (e.g., UUID to binary).
|
|
12
|
+
* The implementation emphasizes robustness and error handling, throwing VIOError exceptions when encountering invalid input or protocol violations during encoding and decoding operations.
|
|
13
|
+
* This module is a critical component of the Vortez IO protocol implementation, enabling the construction and interpretation of the binary frames that form the basis of communication within the protocol.
|
|
14
|
+
* The Frame class is designed to be flexible and extensible, allowing for future additions of frame types, modes, and identifier formats as the protocol evolves.
|
|
15
|
+
* The encoding and decoding logic is optimized for performance while maintaining strict adherence to the protocol specification, ensuring efficient processing of frames in real-world applications.
|
|
16
|
+
* Overall, this module serves as the foundation for handling the binary communication aspects of the Vortez IO protocol, providing a clear and robust implementation of the Frame structure and its associated encoding and decoding mechanisms.
|
|
17
|
+
*
|
|
18
|
+
* ⚠️ IMPORTANT: This module assumes that the input data for decoding is well-formed according to the protocol specification.
|
|
19
|
+
* It performs basic validations but does not attempt to recover from malformed frames.
|
|
20
|
+
* Users of this module should ensure that they are providing valid input to avoid unexpected errors during decoding.
|
|
21
|
+
*
|
|
22
|
+
* @see https://github.com/NetFeez/Vortez-IO-Protocol/blob/main/PROTOCOL.md for the full protocol specification and documentation.
|
|
23
|
+
*/
|
|
24
|
+
import UTF8Codec from "./support/UTF8Codec.js";
|
|
25
|
+
import UUID from "./support/UUID.js";
|
|
26
|
+
import Varint from "./support/Varint.js";
|
|
27
|
+
import VIOError from "./VIOError.js";
|
|
28
|
+
const MAX_ID_LENGTH = 1024;
|
|
29
|
+
export class Frame {
|
|
30
|
+
type;
|
|
31
|
+
mode;
|
|
32
|
+
identifier;
|
|
33
|
+
data;
|
|
34
|
+
reservedRaw;
|
|
35
|
+
static TYPE_TO_BITS = {
|
|
36
|
+
RAW: 0x0, EVENT: 0x1, REQUEST: 0x2, RESPONSE: 0x3, ERROR: 0x4
|
|
37
|
+
};
|
|
38
|
+
static TYPE_MAP = {
|
|
39
|
+
0x0: 'RAW', 0x1: 'EVENT', 0x2: 'REQUEST', 0x3: 'RESPONSE', 0x4: 'ERROR'
|
|
40
|
+
};
|
|
41
|
+
static MODE_TO_BITS = {
|
|
42
|
+
BINARY: 0x0, JSON: 0x1, LINE: 0x2, CUSTOM: 0x3
|
|
43
|
+
};
|
|
44
|
+
static MODE_MAP = {
|
|
45
|
+
0x0: 'BINARY', 0x1: 'JSON', 0x2: 'LINE', 0x3: 'CUSTOM'
|
|
46
|
+
};
|
|
47
|
+
static ID_TYPE_TO_BITS = {
|
|
48
|
+
NONE: 0x0, UUID: 0x1, STRING: 0x2
|
|
49
|
+
};
|
|
50
|
+
static ID_TYPE_MAP = {
|
|
51
|
+
0x0: 'NONE', 0x1: 'UUID', 0x2: 'STRING',
|
|
52
|
+
};
|
|
53
|
+
constructor(type, mode, identifier, data, reservedRaw) {
|
|
54
|
+
this.type = type;
|
|
55
|
+
this.mode = mode;
|
|
56
|
+
this.identifier = identifier;
|
|
57
|
+
this.data = data;
|
|
58
|
+
this.reservedRaw = reservedRaw;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Decodes a binary frame from the given ArrayBuffer or Uint8Array according to the protocol specification.
|
|
62
|
+
* The method parses the frame header to determine the type, mode, and identifier, and then extracts the payload data.
|
|
63
|
+
* It performs various validations to ensure the frame adheres to the protocol rules, such as checking reserved bits and validating identifier types based on frame type.
|
|
64
|
+
* @param buffer The input buffer containing the binary frame data to decode.
|
|
65
|
+
* @returns A Frame object representing the decoded frame with its components (type, mode, identifier, data).
|
|
66
|
+
* @throws VIOError if the buffer is too short, contains invalid header values, or violates protocol rules regarding type and identifier combinations.
|
|
67
|
+
*/
|
|
68
|
+
static decode(buffer) {
|
|
69
|
+
const view = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
70
|
+
if (view.length < 3)
|
|
71
|
+
throw new VIOError(VIOError.Code.BUFFER_TOO_SHORT, 'Frame too short');
|
|
72
|
+
const head = view[0];
|
|
73
|
+
const typeBits = head & 0x0F;
|
|
74
|
+
const modeBits = (head >> 4) & 0x0F;
|
|
75
|
+
const type = this.assertDefined(this.TYPE_MAP[typeBits], VIOError.Code.INVALID_HEADER, `Invalid type: ${typeBits}`);
|
|
76
|
+
const mode = this.assertDefined(this.MODE_MAP[modeBits], VIOError.Code.INVALID_HEADER, `Invalid mode: ${modeBits}`);
|
|
77
|
+
const reserved = view[1];
|
|
78
|
+
if (reserved !== 0)
|
|
79
|
+
throw new VIOError(VIOError.Code.INVALID_HEADER, `Reserved byte must be 0 (got ${reserved})`);
|
|
80
|
+
const idMeta = view[2];
|
|
81
|
+
const idTypeBits = idMeta & 0x03;
|
|
82
|
+
const idReservedBits = idMeta >> 2;
|
|
83
|
+
if (idReservedBits !== 0)
|
|
84
|
+
throw new VIOError(VIOError.Code.INVALID_HEADER, 'ID_META reserved bits must be 0');
|
|
85
|
+
const idType = this.assertDefined(this.ID_TYPE_MAP[idTypeBits], VIOError.Code.INVALID_HEADER, `Invalid ID_TYPE: ${idTypeBits}`);
|
|
86
|
+
let identifier;
|
|
87
|
+
let offset = 3;
|
|
88
|
+
switch (idType) {
|
|
89
|
+
case 'NONE':
|
|
90
|
+
identifier = { type: 'NONE' };
|
|
91
|
+
break;
|
|
92
|
+
case 'UUID':
|
|
93
|
+
if (view.length < offset + 16)
|
|
94
|
+
throw new VIOError(VIOError.Code.BUFFER_TOO_SHORT, 'UUID missing');
|
|
95
|
+
const raw = view.slice(offset, offset + 16);
|
|
96
|
+
identifier = {
|
|
97
|
+
type: 'UUID',
|
|
98
|
+
id: UUID.binToUuid(raw),
|
|
99
|
+
idRaw: raw
|
|
100
|
+
};
|
|
101
|
+
offset += 16;
|
|
102
|
+
break;
|
|
103
|
+
case 'STRING': {
|
|
104
|
+
const { value: len, offset: next } = Varint.readVarint(view, offset);
|
|
105
|
+
if (len < 0 || len > MAX_ID_LENGTH)
|
|
106
|
+
throw new VIOError(VIOError.Code.INVALID_HEADER, `Invalid String ID length: ${len}`);
|
|
107
|
+
offset = next;
|
|
108
|
+
if (view.length < offset + len)
|
|
109
|
+
throw new VIOError(VIOError.Code.BUFFER_TOO_SHORT, 'String ID truncated');
|
|
110
|
+
const raw = view.slice(offset, offset + len);
|
|
111
|
+
identifier = {
|
|
112
|
+
type: 'STRING',
|
|
113
|
+
id: UTF8Codec.decode(raw),
|
|
114
|
+
idRaw: raw
|
|
115
|
+
};
|
|
116
|
+
offset += len;
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const data = this.toArrayBuffer(view.slice(offset));
|
|
121
|
+
return this.assemble({ type, mode, identifier, data, reservedRaw: reserved });
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Assembles a Frame object from the given components, performing additional validation to ensure that the combination of type and identifier adheres to protocol rules.
|
|
125
|
+
* This method is called after decoding the raw components of a frame to construct the final Frame object with proper typing and validation.
|
|
126
|
+
* It checks that REQUEST and RESPONSE frames use UUID identifiers, EVENT frames use STRING identifiers, and ERROR frames can use either NONE or UUID identifiers.
|
|
127
|
+
* If the components are valid, it returns a new Frame object; otherwise, it throws a VIOError indicating the specific protocol violation.
|
|
128
|
+
* @param parts An object containing the decoded components of the frame (type, mode, identifier, data, reservedRaw) to be assembled into a Frame object.
|
|
129
|
+
* @returns A Frame object representing the assembled frame with its components.
|
|
130
|
+
* @throws VIOError if the combination of type and identifier is invalid according to protocol rules.
|
|
131
|
+
*/
|
|
132
|
+
static assemble(parts) {
|
|
133
|
+
const { type, mode, identifier, data, reservedRaw } = parts;
|
|
134
|
+
let frame;
|
|
135
|
+
switch (type) {
|
|
136
|
+
case 'REQUEST':
|
|
137
|
+
case 'RESPONSE':
|
|
138
|
+
if (identifier.type !== 'UUID')
|
|
139
|
+
throw new VIOError(VIOError.Code.INVALID_HEADER, `${type} MUST use UUID`);
|
|
140
|
+
frame = new Frame(type, mode, identifier, data, reservedRaw);
|
|
141
|
+
break;
|
|
142
|
+
case 'EVENT':
|
|
143
|
+
if (identifier.type !== 'STRING')
|
|
144
|
+
throw new VIOError(VIOError.Code.INVALID_HEADER, `EVENT MUST use STRING`);
|
|
145
|
+
frame = new Frame(type, mode, identifier, data, reservedRaw);
|
|
146
|
+
break;
|
|
147
|
+
case 'ERROR':
|
|
148
|
+
if (identifier.type !== 'NONE' && identifier.type !== 'UUID')
|
|
149
|
+
throw new VIOError(VIOError.Code.INVALID_HEADER, `ERROR MUST use NONE or UUID`);
|
|
150
|
+
frame = new Frame(type, mode, identifier, data, reservedRaw);
|
|
151
|
+
break;
|
|
152
|
+
case 'RAW':
|
|
153
|
+
frame = new Frame(type, mode, identifier, data, reservedRaw);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
return frame;
|
|
157
|
+
}
|
|
158
|
+
static encode(type, mode, data, identifier = { type: 'NONE' }) {
|
|
159
|
+
const typeBits = this.assertDefined(this.TYPE_TO_BITS[type], VIOError.Code.UNKNOWN_TYPE, `Unknown type: ${type}`);
|
|
160
|
+
const modeBits = this.assertDefined(this.MODE_TO_BITS[mode], VIOError.Code.UNKNOWN_MODE, `Unknown mode: ${mode}`);
|
|
161
|
+
const idTypeBits = this.assertDefined(this.ID_TYPE_TO_BITS[identifier.type], VIOError.Code.INVALID_HEADER, `Unknown ID_TYPE: ${identifier.type}`);
|
|
162
|
+
this.validate(type, identifier.type);
|
|
163
|
+
let idBytes = new Uint8Array(0);
|
|
164
|
+
if (identifier.type === 'UUID') {
|
|
165
|
+
if (!UUID.isValid(identifier.id))
|
|
166
|
+
throw new VIOError(VIOError.Code.INVALID_HEADER, `Invalid UUID: ${identifier.id}`);
|
|
167
|
+
idBytes = UUID.uuidToBin(identifier.id);
|
|
168
|
+
}
|
|
169
|
+
else if (identifier.type === 'STRING') {
|
|
170
|
+
const encoded = UTF8Codec.encode(identifier.id);
|
|
171
|
+
const varint = Varint.writeVarint(encoded.length);
|
|
172
|
+
if (encoded.length > MAX_ID_LENGTH)
|
|
173
|
+
throw new VIOError(VIOError.Code.INVALID_HEADER, `String ID too long (max ${MAX_ID_LENGTH} bytes)`);
|
|
174
|
+
idBytes = new Uint8Array(varint.length + encoded.length);
|
|
175
|
+
idBytes.set(varint, 0);
|
|
176
|
+
idBytes.set(encoded, varint.length);
|
|
177
|
+
}
|
|
178
|
+
const payload = data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
179
|
+
const totalLength = 3 + idBytes.length + payload.length;
|
|
180
|
+
const buffer = new Uint8Array(totalLength);
|
|
181
|
+
buffer[0] = (modeBits << 4) | typeBits;
|
|
182
|
+
buffer[1] = 0;
|
|
183
|
+
buffer[2] = idTypeBits & 0x03;
|
|
184
|
+
let offset = 3;
|
|
185
|
+
buffer.set(idBytes, offset);
|
|
186
|
+
offset += idBytes.length;
|
|
187
|
+
buffer.set(payload, offset);
|
|
188
|
+
return this.toArrayBuffer(buffer);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Validates the combination of frame type and identifier type according to protocol rules:
|
|
192
|
+
* - REQUEST and RESPONSE frames MUST use UUID identifiers.
|
|
193
|
+
* - EVENT frames MUST NOT use UUID identifiers (they can use STRING or NONE).
|
|
194
|
+
* - ERROR and RAW frames have no restrictions on identifier type.
|
|
195
|
+
* @param type The frame type to validate.
|
|
196
|
+
* @param idType The identifier type to validate against the frame type.
|
|
197
|
+
* @throws VIOError if the combination of type and idType is invalid according to the protocol rules.
|
|
198
|
+
*/
|
|
199
|
+
static validate(type, idType) {
|
|
200
|
+
switch (type) {
|
|
201
|
+
case 'REQUEST':
|
|
202
|
+
case 'RESPONSE':
|
|
203
|
+
if (idType !== 'UUID')
|
|
204
|
+
throw new VIOError(VIOError.Code.INVALID_HEADER, `${type} MUST use UUID`);
|
|
205
|
+
break;
|
|
206
|
+
case 'EVENT':
|
|
207
|
+
if (idType !== 'STRING')
|
|
208
|
+
throw new VIOError(VIOError.Code.INVALID_HEADER, `EVENT MUST use STRING`);
|
|
209
|
+
break;
|
|
210
|
+
case 'ERROR': break;
|
|
211
|
+
case 'RAW': break;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Creates a copy of the ArrayBuffer from a Uint8Array view, ensuring it is not a shared buffer.
|
|
216
|
+
* This is necessary because the input Uint8Array may be a view into a larger buffer, and we want to ensure that the Frame's data is isolated and immutable.
|
|
217
|
+
* @param view The Uint8Array view to convert into an ArrayBuffer.
|
|
218
|
+
* @returns A new ArrayBuffer containing the data from the view.
|
|
219
|
+
*/
|
|
220
|
+
static toArrayBuffer(view) {
|
|
221
|
+
const out = new Uint8Array(view.byteLength);
|
|
222
|
+
out.set(view);
|
|
223
|
+
return out.buffer;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Utility method to assert that a value is defined (not undefined) and throw a VIOError with the provided message if it is not.
|
|
227
|
+
* This is used to ensure that required values are present during decoding and validation processes.
|
|
228
|
+
* @param value The value to check for being defined.
|
|
229
|
+
* @param code The VIOError code to use if the value is undefined.
|
|
230
|
+
* @param msg The error message to include in the VIOError if the value is undefined.
|
|
231
|
+
* @returns The input value if it is defined, otherwise throws a VIOError.
|
|
232
|
+
* @throws VIOError if the value is undefined, with the provided message.
|
|
233
|
+
*/
|
|
234
|
+
static assertDefined(value, code, msg) {
|
|
235
|
+
if (value === undefined)
|
|
236
|
+
throw new VIOError(code, msg);
|
|
237
|
+
return value;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
export default Frame;
|
|
241
|
+
//# sourceMappingURL=Frame.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Frame.js","sourceRoot":"","sources":["../src/Frame.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,OAAO,SAAS,MAAM,wBAAwB,CAAC;AAC/C,OAAO,IAAI,MAAM,mBAAmB,CAAC;AACrC,OAAO,MAAM,MAAM,qBAAqB,CAAC;AACzC,OAAO,QAAQ,MAAM,eAAe,CAAC;AAErC,MAAM,aAAa,GAAG,IAAI,CAAC;AAE3B,MAAM,OAAO,KAAK;IAuBM;IACA;IACA;IACA;IACA;IA1BV,MAAM,CAAU,YAAY,GAA+B;QACjE,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG;KAChE,CAAC;IACQ,MAAM,CAAU,QAAQ,GAA+B;QAC7D,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,SAAS,EAAE,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,OAAO;KAC1E,CAAC;IAEQ,MAAM,CAAU,YAAY,GAA+B;QACjE,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG;KACjD,CAAC;IACQ,MAAM,CAAU,QAAQ,GAA+B;QAC7D,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,QAA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AAK,MAAM;oBAAE,MAAM,IAAI,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,EAAE,GAAG,IAAI,gBAAgB,CAAC,CAAC;gBACjG,MAAM;YACV,KAAK,OAAO;gBACR,IAAI,MAAM,KAAK,QAAQ;oBAAE,MAAM,IAAI,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,EAAE,uBAAuB,CAAC,CAAC;gBACnG,MAAM;YACV,KAAK,OAAO,CAAC,CAAC,MAAM;YACpB,KAAK,KAAK,CAAC,CAAC,MAAM;QACtB,CAAC;IACL,CAAC;IACD;;;;;OAKG;IACO,MAAM,CAAC,aAAa,CAAC,IAAgB;QAC3C,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAC5C,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACd,OAAO,GAAG,CAAC,MAAM,CAAC;IACtB,CAAC;IACD;;;;;;;;OAQG;IACO,MAAM,CAAC,aAAa,CAAI,KAAoB,EAAE,IAAmB,EAAE,GAAW;QACpF,IAAI,KAAK,KAAK,SAAS;YAAG,MAAM,IAAI,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QACxD,OAAO,KAAK,CAAC;IACjB,CAAC;;AA4CL,eAAe,KAAK,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare namespace Isomorphic {
|
|
2
|
+
type TimeoutHandler = () => void | Promise<void>;
|
|
3
|
+
type setTimeout<T> = (handler: TimeoutHandler, timeout: number) => T;
|
|
4
|
+
type clearTimeout<T> = (id: T) => void;
|
|
5
|
+
type createUUID = () => string;
|
|
6
|
+
}
|
|
7
|
+
export default Isomorphic;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Isomorphic.js","sourceRoot":"","sources":["../src/Isomorphic.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Events } from "@netfeez/common";
|
|
2
|
+
import VIOError from "./VIOError.js";
|
|
3
|
+
import type Frame from "./Frame.js";
|
|
4
|
+
import type Isomorphic from "./Isomorphic.js";
|
|
5
|
+
export declare class RequestManager<T extends any> extends Events<RequestManager.EventMap> {
|
|
6
|
+
protected pending: Map<string, RequestManager.PendingEntry<T>>;
|
|
7
|
+
protected setTimeout: Isomorphic.setTimeout<T>;
|
|
8
|
+
protected clearTimeout: Isomorphic.clearTimeout<T>;
|
|
9
|
+
protected createUUID: Isomorphic.createUUID;
|
|
10
|
+
constructor(dependencies: RequestManager.DEPENDENCIES<T>);
|
|
11
|
+
/**
|
|
12
|
+
* Send a request with the given data and return a Promise that resolves with the response or rejects with an error.
|
|
13
|
+
* The method generates a unique UUID for the request, stores the resolve and reject functions in the pending map, and emits a 'send' event with the UUID and data.
|
|
14
|
+
* The handler method should be called when a response or error is received, which will look up the corresponding entry in the pending map and resolve or reject the Promise accordingly.
|
|
15
|
+
* @param data The data to be sent with the request. This can be of any type depending on the application's needs.
|
|
16
|
+
* @param options The options for the request, including a timeout duration.
|
|
17
|
+
*/
|
|
18
|
+
send(data: any, options?: RequestManager.SendOptions): Promise<RequestManager.Response>;
|
|
19
|
+
/**
|
|
20
|
+
* Handle a response or error for a request identified by the given UUID. This method should be called when a response or error is received for a previously sent request.
|
|
21
|
+
* It looks up the corresponding entry in the pending map using the UUID. If an entry is found, it resolves the Promise with the provided data if there is no error, or rejects it with the provided error if there is one. Finally, it removes the entry from the pending map to clean up resources.
|
|
22
|
+
* @param uuid The unique identifier for the request, which should match the UUID generated when the request was sent.
|
|
23
|
+
* @param data The response data to resolve the Promise with if there is no error.
|
|
24
|
+
* @param error The error to reject the Promise with if an error occurred.
|
|
25
|
+
*/
|
|
26
|
+
handle(uuid: string, data?: RequestManager.Response, error?: VIOError): void;
|
|
27
|
+
}
|
|
28
|
+
export declare namespace RequestManager {
|
|
29
|
+
interface DEPENDENCIES<T extends any> {
|
|
30
|
+
setTimeout: Isomorphic.setTimeout<T>;
|
|
31
|
+
clearTimeout: Isomorphic.clearTimeout<T>;
|
|
32
|
+
createUUID?: Isomorphic.createUUID;
|
|
33
|
+
}
|
|
34
|
+
namespace Response {
|
|
35
|
+
type Base = {
|
|
36
|
+
mode: Frame.Mode;
|
|
37
|
+
};
|
|
38
|
+
interface Binary extends Base {
|
|
39
|
+
mode: 'BINARY' | 'CUSTOM';
|
|
40
|
+
data: Uint8Array;
|
|
41
|
+
}
|
|
42
|
+
interface Json extends Base {
|
|
43
|
+
mode: 'JSON';
|
|
44
|
+
data: any;
|
|
45
|
+
}
|
|
46
|
+
interface Line extends Base {
|
|
47
|
+
mode: 'LINE';
|
|
48
|
+
data: Record<string, string>;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
type Response = Response.Binary | Response.Json | Response.Line;
|
|
52
|
+
type EventMap = {
|
|
53
|
+
send: [uuid: string, data?: any];
|
|
54
|
+
error: [error: VIOError];
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Represents a pending request entry in the RequestManager.
|
|
58
|
+
* Each entry contains the resolve and reject functions for the Promise associated with the request, as well as a timeout identifier if a timeout mechanism is implemented.
|
|
59
|
+
* The resolve function is called with the response data when a successful response is received, while the reject function is called with an error if an error occurs or if a timeout is reached.
|
|
60
|
+
* The timeout field can be used to store the identifier returned by a setTimeout function, allowing for the implementation of request timeouts where the Promise is rejected if a response is not received within a certain timeframe.
|
|
61
|
+
* @template T The type of the timeout identifier, which can vary depending on the environment (e.g., NodeJS.Timeout in Node.js or number in browsers).
|
|
62
|
+
*/
|
|
63
|
+
interface PendingEntry<T> {
|
|
64
|
+
resolve: (value: Response) => void;
|
|
65
|
+
reject: (reason: VIOError) => void;
|
|
66
|
+
timeout: T;
|
|
67
|
+
}
|
|
68
|
+
interface SendOptions {
|
|
69
|
+
timeoutMs?: number;
|
|
70
|
+
}
|
|
71
|
+
type Done<T> = (data: T) => void;
|
|
72
|
+
type Fail = (error: VIOError) => void;
|
|
73
|
+
}
|
|
74
|
+
export default RequestManager;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Events } from "@netfeez/common";
|
|
2
|
+
import UUID from "./support/UUID.js";
|
|
3
|
+
import VIOError from "./VIOError.js";
|
|
4
|
+
export class RequestManager extends Events {
|
|
5
|
+
pending = new Map();
|
|
6
|
+
setTimeout;
|
|
7
|
+
clearTimeout;
|
|
8
|
+
createUUID;
|
|
9
|
+
constructor(dependencies) {
|
|
10
|
+
super();
|
|
11
|
+
this.setTimeout = dependencies.setTimeout;
|
|
12
|
+
this.clearTimeout = dependencies.clearTimeout;
|
|
13
|
+
this.createUUID = dependencies.createUUID || UUID.create;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Send a request with the given data and return a Promise that resolves with the response or rejects with an error.
|
|
17
|
+
* The method generates a unique UUID for the request, stores the resolve and reject functions in the pending map, and emits a 'send' event with the UUID and data.
|
|
18
|
+
* The handler method should be called when a response or error is received, which will look up the corresponding entry in the pending map and resolve or reject the Promise accordingly.
|
|
19
|
+
* @param data The data to be sent with the request. This can be of any type depending on the application's needs.
|
|
20
|
+
* @param options The options for the request, including a timeout duration.
|
|
21
|
+
*/
|
|
22
|
+
send(data, options = {}) {
|
|
23
|
+
const { timeoutMs = 30000 } = options;
|
|
24
|
+
const uuid = this.createUUID();
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
const timeout = this.setTimeout(() => {
|
|
27
|
+
this.pending.delete(uuid);
|
|
28
|
+
reject(new VIOError(VIOError.Code.INVALID_DATA, 'Request timed out'));
|
|
29
|
+
}, timeoutMs);
|
|
30
|
+
this.pending.set(uuid, { resolve, reject, timeout });
|
|
31
|
+
try {
|
|
32
|
+
this.emit('send', uuid, data);
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
this.clearTimeout(timeout);
|
|
36
|
+
this.pending.delete(uuid);
|
|
37
|
+
reject(error instanceof VIOError ? error : new VIOError(VIOError.Code.INVALID_DATA, 'Failed to queue request'));
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Handle a response or error for a request identified by the given UUID. This method should be called when a response or error is received for a previously sent request.
|
|
43
|
+
* It looks up the corresponding entry in the pending map using the UUID. If an entry is found, it resolves the Promise with the provided data if there is no error, or rejects it with the provided error if there is one. Finally, it removes the entry from the pending map to clean up resources.
|
|
44
|
+
* @param uuid The unique identifier for the request, which should match the UUID generated when the request was sent.
|
|
45
|
+
* @param data The response data to resolve the Promise with if there is no error.
|
|
46
|
+
* @param error The error to reject the Promise with if an error occurred.
|
|
47
|
+
*/
|
|
48
|
+
handle(uuid, data, error) {
|
|
49
|
+
const entry = this.pending.get(uuid);
|
|
50
|
+
if (!entry)
|
|
51
|
+
return;
|
|
52
|
+
this.clearTimeout(entry.timeout);
|
|
53
|
+
if (error)
|
|
54
|
+
entry.reject(error);
|
|
55
|
+
else
|
|
56
|
+
entry.resolve(data ?? { mode: 'BINARY', data: new Uint8Array(0) });
|
|
57
|
+
this.pending.delete(uuid);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export default RequestManager;
|
|
61
|
+
//# sourceMappingURL=RequestManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RequestManager.js","sourceRoot":"","sources":["../src/RequestManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEzC,OAAO,IAAI,MAAM,mBAAmB,CAAC;AACrC,OAAO,QAAQ,MAAM,eAAe,CAAC;AAKrC,MAAM,OAAO,cAA8B,SAAQ,MAA+B;IACpE,OAAO,GAAgD,IAAI,GAAG,EAAE,CAAC;IAEjE,UAAU,CAA2B;IACrC,YAAY,CAA6B;IACzC,UAAU,CAAwB;IAE5C,YAAmB,YAA4C;QAAI,KAAK,EAAE,CAAC;QACvE,IAAI,CAAC,UAAU,GAAG,YAAY,CAAC,UAAU,CAAC;QAC1C,I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,CAAC,IAAY,EAAE,IAA8B,EAAE,KAAgB;QACxE,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACrC,IAAI,CAAC,KAAK;YAAE,OAAO;QAEnB,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAEjC,IAAI,KAAK;YAAE,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;;YAC1B,KAAK,CAAC,OAAO,CAAC,IAAI,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QAExE,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC9B,CAAC;CACJ;AAgDD,eAAe,cAAc,CAAC"}
|
package/build/VIO.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Events } from "@netfeez/common";
|
|
2
|
+
import RequestManager from "./RequestManager.js";
|
|
3
|
+
import VIOError from "./VIOError.js";
|
|
4
|
+
import Frame from "./Frame.js";
|
|
5
|
+
import LineCodec from "./support/LineParser.js";
|
|
6
|
+
export declare class VIO<T extends any> extends Events<VIO.EventMap> {
|
|
7
|
+
protected mRequest: RequestManager<T>;
|
|
8
|
+
vTransport: Events.Emitter<VIO.TransportEventMap>;
|
|
9
|
+
constructor(dependencies: RequestManager.DEPENDENCIES<T>);
|
|
10
|
+
get transport(): Events<VIO.TransportEventMap>;
|
|
11
|
+
request(data: any, options?: RequestManager.SendOptions): Promise<RequestManager.Response>;
|
|
12
|
+
event(name: string, data: any): void;
|
|
13
|
+
handle(data: ArrayBuffer | Uint8Array): void;
|
|
14
|
+
protected handleRaw(frame: Frame): Promise<void>;
|
|
15
|
+
protected handleEvent(frame: Frame<'EVENT'>): Promise<void>;
|
|
16
|
+
protected handleRequest(frame: Frame<'REQUEST'>): Promise<void>;
|
|
17
|
+
protected handleResponse(frame: Frame<'RESPONSE'>): Promise<void>;
|
|
18
|
+
protected handleError(frame: Frame<'ERROR'>): Promise<void>;
|
|
19
|
+
protected sendEvent(name: string, data: Uint8Array | ArrayBuffer): void;
|
|
20
|
+
protected sendEvent(name: string, data: LineCodec.ContentMap, mode: 'LINE'): void;
|
|
21
|
+
protected sendEvent(name: string, data: any): void;
|
|
22
|
+
protected sendRequest(uuid: string, data: Uint8Array | ArrayBuffer): void;
|
|
23
|
+
protected sendRequest(uuid: string, data: LineCodec.ContentMap, mode: 'LINE'): void;
|
|
24
|
+
protected sendRequest(uuid: string, data: any): void;
|
|
25
|
+
protected sendResponse(uuid: string, data: Uint8Array | ArrayBuffer): void;
|
|
26
|
+
protected sendResponse(uuid: string, data: LineCodec.ContentMap, mode: 'LINE'): void;
|
|
27
|
+
protected sendResponse(uuid: string, data: any): void;
|
|
28
|
+
protected sendError(error: VIOError, uuid?: string): void;
|
|
29
|
+
protected serialize(data: any, mode?: 'LINE'): {
|
|
30
|
+
content: Uint8Array;
|
|
31
|
+
finalMode: Frame.Mode;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export declare namespace VIO {
|
|
35
|
+
export type TransportEventMap = {
|
|
36
|
+
send: [data: ArrayBuffer];
|
|
37
|
+
'transport:error': [err: Error, ctx?: {
|
|
38
|
+
stage: 'send' | 'recv' | 'connect';
|
|
39
|
+
frameType?: Frame.Type | string;
|
|
40
|
+
uuid?: string;
|
|
41
|
+
info?: any;
|
|
42
|
+
}];
|
|
43
|
+
};
|
|
44
|
+
type EventTuple = ([
|
|
45
|
+
name: string,
|
|
46
|
+
type: 'BINARY' | 'CUSTOM',
|
|
47
|
+
data: Uint8Array
|
|
48
|
+
] | [
|
|
49
|
+
name: string,
|
|
50
|
+
type: 'JSON',
|
|
51
|
+
data: any
|
|
52
|
+
] | [
|
|
53
|
+
name: string,
|
|
54
|
+
type: 'LINE',
|
|
55
|
+
data: Record<string, string>
|
|
56
|
+
]);
|
|
57
|
+
type requestTuple = ([
|
|
58
|
+
type: 'BINARY' | 'CUSTOM',
|
|
59
|
+
data: Uint8Array,
|
|
60
|
+
done: RequestManager.Done<Uint8Array>,
|
|
61
|
+
fail: RequestManager.Fail
|
|
62
|
+
] | [
|
|
63
|
+
type: 'JSON',
|
|
64
|
+
data: any,
|
|
65
|
+
done: RequestManager.Done<any>,
|
|
66
|
+
fail: RequestManager.Fail
|
|
67
|
+
] | [
|
|
68
|
+
type: 'LINE',
|
|
69
|
+
data: Record<string, string>,
|
|
70
|
+
done: RequestManager.Done<Record<string, string>>,
|
|
71
|
+
fail: RequestManager.Fail
|
|
72
|
+
]);
|
|
73
|
+
export type EventMap = {
|
|
74
|
+
request: requestTuple;
|
|
75
|
+
event: EventTuple;
|
|
76
|
+
error: [error: VIOError];
|
|
77
|
+
raw: [data: Frame];
|
|
78
|
+
response: [uuid: string, response: RequestManager.Response];
|
|
79
|
+
};
|
|
80
|
+
export {};
|
|
81
|
+
}
|
|
82
|
+
export default VIO;
|