@replit/river 0.10.0 → 0.10.2
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/dist/{router/builder.d.ts → builder-3c4485f0.d.ts} +76 -21
- package/dist/chunk-AJQU4AZG.js +284 -0
- package/dist/chunk-IYRPZPSQ.js +964 -0
- package/dist/chunk-ORAG7IAU.js +0 -0
- package/dist/chunk-PC65ZFWJ.js +29 -0
- package/dist/chunk-R6H2BIMC.js +49 -0
- package/dist/chunk-RGMHF6PF.js +65 -0
- package/dist/chunk-SLUSVGQH.js +30 -0
- package/dist/chunk-UU2Z7LDR.js +113 -0
- package/dist/chunk-WVT5QXMZ.js +20 -0
- package/dist/chunk-ZE4MX7DF.js +75 -0
- package/dist/codec/index.cjs +94 -0
- package/dist/codec/index.d.cts +15 -0
- package/dist/codec/index.d.ts +15 -4
- package/dist/codec/index.js +10 -2
- package/dist/connection-8e19874c.d.ts +11 -0
- package/dist/connection-f7688cc1.d.ts +11 -0
- package/dist/logging/index.cjs +56 -0
- package/dist/logging/index.d.cts +28 -0
- package/dist/logging/index.d.ts +6 -6
- package/dist/logging/index.js +9 -40
- package/dist/router/index.cjs +1046 -0
- package/dist/router/index.d.cts +114 -0
- package/dist/router/index.d.ts +114 -12
- package/dist/router/index.js +24 -5
- package/dist/transport/impls/ws/client.cjs +505 -0
- package/dist/transport/impls/ws/client.d.cts +42 -0
- package/dist/transport/impls/ws/client.d.ts +8 -8
- package/dist/transport/impls/ws/client.js +10 -100
- package/dist/transport/impls/ws/server.cjs +457 -0
- package/dist/transport/impls/ws/server.d.cts +21 -0
- package/dist/transport/impls/ws/server.d.ts +11 -10
- package/dist/transport/impls/ws/server.js +11 -52
- package/dist/transport/index.cjs +362 -0
- package/dist/transport/{transport.d.ts → index.d.cts} +119 -7
- package/dist/transport/index.d.ts +273 -4
- package/dist/transport/index.js +20 -2
- package/dist/{codec/types.d.ts → types-3e5768ec.d.ts} +3 -2
- package/dist/util/testHelpers.cjs +1010 -0
- package/dist/util/testHelpers.d.cts +79 -0
- package/dist/util/testHelpers.d.ts +22 -19
- package/dist/util/testHelpers.js +135 -163
- package/package.json +42 -14
- package/dist/__tests__/bandwidth.bench.d.ts +0 -2
- package/dist/__tests__/bandwidth.bench.d.ts.map +0 -1
- package/dist/__tests__/bandwidth.bench.js +0 -90
- package/dist/__tests__/cleanup.test.d.ts +0 -2
- package/dist/__tests__/cleanup.test.d.ts.map +0 -1
- package/dist/__tests__/cleanup.test.js +0 -165
- package/dist/__tests__/disconnects.test.d.ts +0 -2
- package/dist/__tests__/disconnects.test.d.ts.map +0 -1
- package/dist/__tests__/disconnects.test.js +0 -163
- package/dist/__tests__/e2e.test.d.ts +0 -2
- package/dist/__tests__/e2e.test.d.ts.map +0 -1
- package/dist/__tests__/e2e.test.js +0 -317
- package/dist/__tests__/fixtures/cleanup.d.ts +0 -12
- package/dist/__tests__/fixtures/cleanup.d.ts.map +0 -1
- package/dist/__tests__/fixtures/cleanup.js +0 -36
- package/dist/__tests__/fixtures/largePayload.json +0 -33
- package/dist/__tests__/fixtures/observable.d.ts +0 -26
- package/dist/__tests__/fixtures/observable.d.ts.map +0 -1
- package/dist/__tests__/fixtures/observable.js +0 -38
- package/dist/__tests__/fixtures/observable.test.d.ts +0 -2
- package/dist/__tests__/fixtures/observable.test.d.ts.map +0 -1
- package/dist/__tests__/fixtures/observable.test.js +0 -39
- package/dist/__tests__/fixtures/services.d.ts +0 -288
- package/dist/__tests__/fixtures/services.d.ts.map +0 -1
- package/dist/__tests__/fixtures/services.js +0 -207
- package/dist/__tests__/handler.test.d.ts +0 -2
- package/dist/__tests__/handler.test.d.ts.map +0 -1
- package/dist/__tests__/handler.test.js +0 -120
- package/dist/__tests__/serialize.test.d.ts +0 -2
- package/dist/__tests__/serialize.test.d.ts.map +0 -1
- package/dist/__tests__/serialize.test.js +0 -208
- package/dist/__tests__/typescript-stress.test.d.ts +0 -1583
- package/dist/__tests__/typescript-stress.test.d.ts.map +0 -1
- package/dist/__tests__/typescript-stress.test.js +0 -123
- package/dist/codec/binary.d.ts +0 -7
- package/dist/codec/binary.d.ts.map +0 -1
- package/dist/codec/binary.js +0 -20
- package/dist/codec/codec.test.d.ts +0 -5
- package/dist/codec/codec.test.d.ts.map +0 -1
- package/dist/codec/codec.test.js +0 -41
- package/dist/codec/index.d.ts.map +0 -1
- package/dist/codec/json.d.ts +0 -7
- package/dist/codec/json.d.ts.map +0 -1
- package/dist/codec/json.js +0 -51
- package/dist/codec/types.d.ts.map +0 -1
- package/dist/codec/types.js +0 -1
- package/dist/logging/index.d.ts.map +0 -1
- package/dist/router/builder.d.ts.map +0 -1
- package/dist/router/builder.js +0 -91
- package/dist/router/client.d.ts +0 -72
- package/dist/router/client.d.ts.map +0 -1
- package/dist/router/client.js +0 -257
- package/dist/router/context.d.ts +0 -30
- package/dist/router/context.d.ts.map +0 -1
- package/dist/router/context.js +0 -1
- package/dist/router/defs.d.ts +0 -16
- package/dist/router/defs.d.ts.map +0 -1
- package/dist/router/defs.js +0 -11
- package/dist/router/index.d.ts.map +0 -1
- package/dist/router/result.d.ts +0 -26
- package/dist/router/result.d.ts.map +0 -1
- package/dist/router/result.js +0 -22
- package/dist/router/server.d.ts +0 -39
- package/dist/router/server.d.ts.map +0 -1
- package/dist/router/server.js +0 -260
- package/dist/transport/events.d.ts +0 -19
- package/dist/transport/events.d.ts.map +0 -1
- package/dist/transport/events.js +0 -26
- package/dist/transport/impls/stdio/stdio.d.ts +0 -33
- package/dist/transport/impls/stdio/stdio.d.ts.map +0 -1
- package/dist/transport/impls/stdio/stdio.js +0 -75
- package/dist/transport/impls/stdio/stdio.test.d.ts +0 -2
- package/dist/transport/impls/stdio/stdio.test.d.ts.map +0 -1
- package/dist/transport/impls/stdio/stdio.test.js +0 -24
- package/dist/transport/impls/ws/client.d.ts.map +0 -1
- package/dist/transport/impls/ws/connection.d.ts +0 -11
- package/dist/transport/impls/ws/connection.d.ts.map +0 -1
- package/dist/transport/impls/ws/connection.js +0 -23
- package/dist/transport/impls/ws/server.d.ts.map +0 -1
- package/dist/transport/impls/ws/ws.test.d.ts +0 -2
- package/dist/transport/impls/ws/ws.test.d.ts.map +0 -1
- package/dist/transport/impls/ws/ws.test.js +0 -185
- package/dist/transport/index.d.ts.map +0 -1
- package/dist/transport/message.d.ts +0 -142
- package/dist/transport/message.d.ts.map +0 -1
- package/dist/transport/message.js +0 -113
- package/dist/transport/message.test.d.ts +0 -2
- package/dist/transport/message.test.d.ts.map +0 -1
- package/dist/transport/message.test.js +0 -52
- package/dist/transport/transport.d.ts.map +0 -1
- package/dist/transport/transport.js +0 -281
- package/dist/util/testHelpers.d.ts.map +0 -1
|
File without changes
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Connection
|
|
3
|
+
} from "./chunk-AJQU4AZG.js";
|
|
4
|
+
|
|
5
|
+
// transport/impls/ws/connection.ts
|
|
6
|
+
var WebSocketConnection = class extends Connection {
|
|
7
|
+
ws;
|
|
8
|
+
constructor(transport, connectedTo, ws) {
|
|
9
|
+
super(transport, connectedTo);
|
|
10
|
+
this.ws = ws;
|
|
11
|
+
ws.binaryType = "arraybuffer";
|
|
12
|
+
this.ws.onmessage = (msg) => transport.onMessage(msg.data);
|
|
13
|
+
}
|
|
14
|
+
send(payload) {
|
|
15
|
+
if (this.ws.readyState === this.ws.OPEN) {
|
|
16
|
+
this.ws.send(payload);
|
|
17
|
+
return true;
|
|
18
|
+
} else {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async close() {
|
|
23
|
+
this.ws.close();
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export {
|
|
28
|
+
WebSocketConnection
|
|
29
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// codec/json.ts
|
|
2
|
+
var encoder = new TextEncoder();
|
|
3
|
+
var decoder = new TextDecoder();
|
|
4
|
+
function uint8ArrayToBase64(uint8Array) {
|
|
5
|
+
let binary = "";
|
|
6
|
+
uint8Array.forEach((byte) => {
|
|
7
|
+
binary += String.fromCharCode(byte);
|
|
8
|
+
});
|
|
9
|
+
return btoa(binary);
|
|
10
|
+
}
|
|
11
|
+
function base64ToUint8Array(base64) {
|
|
12
|
+
const binaryString = atob(base64);
|
|
13
|
+
const uint8Array = new Uint8Array(binaryString.length);
|
|
14
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
15
|
+
uint8Array[i] = binaryString.charCodeAt(i);
|
|
16
|
+
}
|
|
17
|
+
return uint8Array;
|
|
18
|
+
}
|
|
19
|
+
var NaiveJsonCodec = {
|
|
20
|
+
toBuffer: (obj) => {
|
|
21
|
+
return encoder.encode(
|
|
22
|
+
JSON.stringify(obj, function replacer(key) {
|
|
23
|
+
let val = this[key];
|
|
24
|
+
if (val instanceof Uint8Array) {
|
|
25
|
+
return { $t: uint8ArrayToBase64(val) };
|
|
26
|
+
} else {
|
|
27
|
+
return val;
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
);
|
|
31
|
+
},
|
|
32
|
+
fromBuffer: (buff) => {
|
|
33
|
+
try {
|
|
34
|
+
return JSON.parse(decoder.decode(buff), function reviver(_key, val) {
|
|
35
|
+
if (val?.$t) {
|
|
36
|
+
return base64ToUint8Array(val.$t);
|
|
37
|
+
} else {
|
|
38
|
+
return val;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
} catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export {
|
|
48
|
+
NaiveJsonCodec
|
|
49
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {
|
|
2
|
+
WebSocketConnection
|
|
3
|
+
} from "./chunk-PC65ZFWJ.js";
|
|
4
|
+
import {
|
|
5
|
+
NaiveJsonCodec
|
|
6
|
+
} from "./chunk-R6H2BIMC.js";
|
|
7
|
+
import {
|
|
8
|
+
Transport
|
|
9
|
+
} from "./chunk-AJQU4AZG.js";
|
|
10
|
+
import {
|
|
11
|
+
log
|
|
12
|
+
} from "./chunk-SLUSVGQH.js";
|
|
13
|
+
|
|
14
|
+
// transport/impls/ws/server.ts
|
|
15
|
+
var defaultOptions = {
|
|
16
|
+
codec: NaiveJsonCodec
|
|
17
|
+
};
|
|
18
|
+
var WebSocketServerTransport = class extends Transport {
|
|
19
|
+
wss;
|
|
20
|
+
clientId;
|
|
21
|
+
constructor(wss, clientId, providedOptions) {
|
|
22
|
+
const options = { ...defaultOptions, ...providedOptions };
|
|
23
|
+
super(options.codec, clientId);
|
|
24
|
+
this.wss = wss;
|
|
25
|
+
this.clientId = clientId;
|
|
26
|
+
this.setupConnectionStatusListeners();
|
|
27
|
+
}
|
|
28
|
+
connectionHandler = (ws) => {
|
|
29
|
+
let conn = void 0;
|
|
30
|
+
ws.onmessage = (msg) => {
|
|
31
|
+
const parsedMsg = this.parseMsg(msg.data);
|
|
32
|
+
if (parsedMsg && !conn) {
|
|
33
|
+
conn = new WebSocketConnection(this, parsedMsg.from, ws);
|
|
34
|
+
this.onConnect(conn);
|
|
35
|
+
this.handleMsg(parsedMsg);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
ws.onclose = () => {
|
|
39
|
+
if (conn) {
|
|
40
|
+
this.onDisconnect(conn);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
ws.onerror = (msg) => {
|
|
44
|
+
log?.warn(
|
|
45
|
+
`${this.clientId} -- ws error from client ${conn?.connectedTo ?? "unknown"}: ${msg}`
|
|
46
|
+
);
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
setupConnectionStatusListeners() {
|
|
50
|
+
this.wss.on("connection", this.connectionHandler);
|
|
51
|
+
}
|
|
52
|
+
async createNewConnection(to) {
|
|
53
|
+
const err = `${this.clientId} -- failed to send msg to ${to}, client probably dropped`;
|
|
54
|
+
log?.warn(err);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
async close() {
|
|
58
|
+
super.close();
|
|
59
|
+
this.wss.off("connection", this.connectionHandler);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export {
|
|
64
|
+
WebSocketServerTransport
|
|
65
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// logging/index.ts
|
|
2
|
+
var LoggingLevels = {
|
|
3
|
+
info: 0,
|
|
4
|
+
warn: 1,
|
|
5
|
+
error: 2
|
|
6
|
+
};
|
|
7
|
+
var log;
|
|
8
|
+
var defaultLoggingLevel = "warn";
|
|
9
|
+
function bindLogger(write, color) {
|
|
10
|
+
const info = color ? "\x1B[37minfo\x1B[0m" : "info";
|
|
11
|
+
const warn = color ? "\x1B[33mwarn\x1B[0m" : "warn";
|
|
12
|
+
const error = color ? "\x1B[31merr\x1B[0m" : "err";
|
|
13
|
+
log = {
|
|
14
|
+
info: (msg) => log && LoggingLevels[log.minLevel] <= 0 && write(`[river:${info}] ${msg}`),
|
|
15
|
+
warn: (msg) => log && LoggingLevels[log.minLevel] <= 1 && write(`[river:${warn}] ${msg}`),
|
|
16
|
+
error: (msg) => log && LoggingLevels[log.minLevel] <= 2 && write(`[river:${error}] ${msg}`),
|
|
17
|
+
minLevel: log?.minLevel ?? defaultLoggingLevel
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function setLevel(level) {
|
|
21
|
+
if (log) {
|
|
22
|
+
log.minLevel = level;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export {
|
|
27
|
+
log,
|
|
28
|
+
bindLogger,
|
|
29
|
+
setLevel
|
|
30
|
+
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {
|
|
2
|
+
WebSocketConnection
|
|
3
|
+
} from "./chunk-PC65ZFWJ.js";
|
|
4
|
+
import {
|
|
5
|
+
NaiveJsonCodec
|
|
6
|
+
} from "./chunk-R6H2BIMC.js";
|
|
7
|
+
import {
|
|
8
|
+
Transport
|
|
9
|
+
} from "./chunk-AJQU4AZG.js";
|
|
10
|
+
import {
|
|
11
|
+
log
|
|
12
|
+
} from "./chunk-SLUSVGQH.js";
|
|
13
|
+
|
|
14
|
+
// transport/impls/ws/client.ts
|
|
15
|
+
var defaultOptions = {
|
|
16
|
+
retryIntervalMs: 250,
|
|
17
|
+
retryAttemptsMax: 5,
|
|
18
|
+
codec: NaiveJsonCodec
|
|
19
|
+
};
|
|
20
|
+
var WebSocketClientTransport = class extends Transport {
|
|
21
|
+
/**
|
|
22
|
+
* A function that returns a Promise that resolves to a WebSocket instance.
|
|
23
|
+
*/
|
|
24
|
+
wsGetter;
|
|
25
|
+
options;
|
|
26
|
+
serverId;
|
|
27
|
+
reconnectPromises;
|
|
28
|
+
tryReconnecting = true;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a new WebSocketTransport instance.
|
|
31
|
+
* @param wsGetter A function that returns a Promise that resolves to a WebSocket instance.
|
|
32
|
+
* @param clientId The ID of the client using the transport.
|
|
33
|
+
* @param providedOptions An optional object containing configuration options for the transport.
|
|
34
|
+
*/
|
|
35
|
+
constructor(wsGetter, clientId, serverId, providedOptions) {
|
|
36
|
+
const options = { ...defaultOptions, ...providedOptions };
|
|
37
|
+
super(options.codec, clientId);
|
|
38
|
+
this.wsGetter = wsGetter;
|
|
39
|
+
this.serverId = serverId;
|
|
40
|
+
this.options = options;
|
|
41
|
+
this.reconnectPromises = /* @__PURE__ */ new Map();
|
|
42
|
+
this.setupConnectionStatusListeners();
|
|
43
|
+
}
|
|
44
|
+
setupConnectionStatusListeners() {
|
|
45
|
+
this.createNewConnection(this.serverId);
|
|
46
|
+
}
|
|
47
|
+
async createNewConnection(to, attempt = 0) {
|
|
48
|
+
if (this.state === "destroyed") {
|
|
49
|
+
throw new Error("cant reopen a destroyed connection");
|
|
50
|
+
}
|
|
51
|
+
let reconnectPromise = this.reconnectPromises.get(to);
|
|
52
|
+
if (!reconnectPromise) {
|
|
53
|
+
if (!this.tryReconnecting) {
|
|
54
|
+
log?.info(
|
|
55
|
+
`${this.clientId} -- tryReconnecting is false, not attempting reconnect`
|
|
56
|
+
);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
reconnectPromise = new Promise(async (resolve) => {
|
|
60
|
+
log?.info(`${this.clientId} -- establishing a new websocket to ${to}`);
|
|
61
|
+
const ws = await this.wsGetter(to);
|
|
62
|
+
if (ws.readyState === ws.OPEN) {
|
|
63
|
+
return resolve({ ws });
|
|
64
|
+
}
|
|
65
|
+
if (ws.readyState === ws.CLOSING || ws.readyState === ws.CLOSED) {
|
|
66
|
+
return resolve({ err: "ws is closing or closed" });
|
|
67
|
+
}
|
|
68
|
+
ws.addEventListener("open", function onOpen() {
|
|
69
|
+
ws.removeEventListener("open", onOpen);
|
|
70
|
+
resolve({ ws });
|
|
71
|
+
});
|
|
72
|
+
ws.addEventListener("close", function onClose(evt) {
|
|
73
|
+
ws.removeEventListener("close", onClose);
|
|
74
|
+
resolve({ err: evt.reason });
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
this.reconnectPromises.set(to, reconnectPromise);
|
|
78
|
+
}
|
|
79
|
+
const res = await reconnectPromise;
|
|
80
|
+
if ("ws" in res && res.ws.readyState === res.ws.OPEN) {
|
|
81
|
+
if (res.ws === this.connections.get(to)?.ws) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
log?.info(`${this.clientId} -- websocket ok`);
|
|
85
|
+
const conn = new WebSocketConnection(this, to, res.ws);
|
|
86
|
+
this.onConnect(conn);
|
|
87
|
+
res.ws.onclose = () => {
|
|
88
|
+
this.reconnectPromises.delete(to);
|
|
89
|
+
this.onDisconnect(conn);
|
|
90
|
+
};
|
|
91
|
+
this.state = "open";
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
this.reconnectPromises.delete(to);
|
|
95
|
+
if (attempt >= this.options.retryAttemptsMax) {
|
|
96
|
+
throw new Error(
|
|
97
|
+
`${this.clientId} -- websocket to ${to} failed after ${attempt} attempts, giving up`
|
|
98
|
+
);
|
|
99
|
+
} else {
|
|
100
|
+
log?.warn(
|
|
101
|
+
`${this.clientId} -- websocket to ${to} failed, trying again in ${this.options.retryIntervalMs * attempt}ms`
|
|
102
|
+
);
|
|
103
|
+
setTimeout(
|
|
104
|
+
() => this.createNewConnection(to, attempt + 1),
|
|
105
|
+
this.options.retryIntervalMs * attempt
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export {
|
|
112
|
+
WebSocketClientTransport
|
|
113
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// codec/binary.ts
|
|
2
|
+
import { decode, encode } from "@msgpack/msgpack";
|
|
3
|
+
var BinaryCodec = {
|
|
4
|
+
toBuffer: encode,
|
|
5
|
+
fromBuffer: (buff) => {
|
|
6
|
+
try {
|
|
7
|
+
const res = decode(buff);
|
|
8
|
+
if (typeof res !== "object") {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
return res;
|
|
12
|
+
} catch {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
BinaryCodec
|
|
20
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// transport/message.ts
|
|
2
|
+
import { Type } from "@sinclair/typebox";
|
|
3
|
+
import { nanoid } from "nanoid";
|
|
4
|
+
var TransportMessageSchema = (t) => Type.Object({
|
|
5
|
+
id: Type.String(),
|
|
6
|
+
from: Type.String(),
|
|
7
|
+
to: Type.String(),
|
|
8
|
+
serviceName: Type.Optional(Type.Union([Type.String(), Type.Null()])),
|
|
9
|
+
procedureName: Type.Optional(Type.Union([Type.String(), Type.Null()])),
|
|
10
|
+
streamId: Type.String(),
|
|
11
|
+
controlFlags: Type.Integer(),
|
|
12
|
+
payload: t
|
|
13
|
+
});
|
|
14
|
+
var TransportAckSchema = TransportMessageSchema(
|
|
15
|
+
Type.Object({
|
|
16
|
+
ack: Type.String()
|
|
17
|
+
})
|
|
18
|
+
);
|
|
19
|
+
var ControlMessagePayloadSchema = Type.Object({
|
|
20
|
+
type: Type.Literal("CLOSE")
|
|
21
|
+
});
|
|
22
|
+
var OpaqueTransportMessageSchema = TransportMessageSchema(
|
|
23
|
+
Type.Unknown()
|
|
24
|
+
);
|
|
25
|
+
function msg(from, to, streamId, payload, serviceName, procedureName) {
|
|
26
|
+
return {
|
|
27
|
+
id: nanoid(),
|
|
28
|
+
to,
|
|
29
|
+
from,
|
|
30
|
+
serviceName,
|
|
31
|
+
procedureName,
|
|
32
|
+
streamId,
|
|
33
|
+
controlFlags: 0,
|
|
34
|
+
payload
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function reply(msg2, response) {
|
|
38
|
+
return {
|
|
39
|
+
id: nanoid(),
|
|
40
|
+
streamId: msg2.streamId,
|
|
41
|
+
controlFlags: 0,
|
|
42
|
+
to: msg2.from,
|
|
43
|
+
from: msg2.to,
|
|
44
|
+
payload: response
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function closeStream(from, to, stream) {
|
|
48
|
+
const closeMessage = msg(from, to, stream, {
|
|
49
|
+
type: "CLOSE"
|
|
50
|
+
});
|
|
51
|
+
closeMessage.controlFlags |= 4 /* StreamClosedBit */;
|
|
52
|
+
return closeMessage;
|
|
53
|
+
}
|
|
54
|
+
function isAck(controlFlag) {
|
|
55
|
+
return (controlFlag & 1 /* AckBit */) === 1 /* AckBit */;
|
|
56
|
+
}
|
|
57
|
+
function isStreamOpen(controlFlag) {
|
|
58
|
+
return (controlFlag & 2 /* StreamOpenBit */) === 2 /* StreamOpenBit */;
|
|
59
|
+
}
|
|
60
|
+
function isStreamClose(controlFlag) {
|
|
61
|
+
return (controlFlag & 4 /* StreamClosedBit */) === 4 /* StreamClosedBit */;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export {
|
|
65
|
+
TransportMessageSchema,
|
|
66
|
+
TransportAckSchema,
|
|
67
|
+
ControlMessagePayloadSchema,
|
|
68
|
+
OpaqueTransportMessageSchema,
|
|
69
|
+
msg,
|
|
70
|
+
reply,
|
|
71
|
+
closeStream,
|
|
72
|
+
isAck,
|
|
73
|
+
isStreamOpen,
|
|
74
|
+
isStreamClose
|
|
75
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// codec/index.ts
|
|
21
|
+
var codec_exports = {};
|
|
22
|
+
__export(codec_exports, {
|
|
23
|
+
BinaryCodec: () => BinaryCodec,
|
|
24
|
+
NaiveJsonCodec: () => NaiveJsonCodec
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(codec_exports);
|
|
27
|
+
|
|
28
|
+
// codec/binary.ts
|
|
29
|
+
var import_msgpack = require("@msgpack/msgpack");
|
|
30
|
+
var BinaryCodec = {
|
|
31
|
+
toBuffer: import_msgpack.encode,
|
|
32
|
+
fromBuffer: (buff) => {
|
|
33
|
+
try {
|
|
34
|
+
const res = (0, import_msgpack.decode)(buff);
|
|
35
|
+
if (typeof res !== "object") {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
return res;
|
|
39
|
+
} catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// codec/json.ts
|
|
46
|
+
var encoder = new TextEncoder();
|
|
47
|
+
var decoder = new TextDecoder();
|
|
48
|
+
function uint8ArrayToBase64(uint8Array) {
|
|
49
|
+
let binary = "";
|
|
50
|
+
uint8Array.forEach((byte) => {
|
|
51
|
+
binary += String.fromCharCode(byte);
|
|
52
|
+
});
|
|
53
|
+
return btoa(binary);
|
|
54
|
+
}
|
|
55
|
+
function base64ToUint8Array(base64) {
|
|
56
|
+
const binaryString = atob(base64);
|
|
57
|
+
const uint8Array = new Uint8Array(binaryString.length);
|
|
58
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
59
|
+
uint8Array[i] = binaryString.charCodeAt(i);
|
|
60
|
+
}
|
|
61
|
+
return uint8Array;
|
|
62
|
+
}
|
|
63
|
+
var NaiveJsonCodec = {
|
|
64
|
+
toBuffer: (obj) => {
|
|
65
|
+
return encoder.encode(
|
|
66
|
+
JSON.stringify(obj, function replacer(key) {
|
|
67
|
+
let val = this[key];
|
|
68
|
+
if (val instanceof Uint8Array) {
|
|
69
|
+
return { $t: uint8ArrayToBase64(val) };
|
|
70
|
+
} else {
|
|
71
|
+
return val;
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
);
|
|
75
|
+
},
|
|
76
|
+
fromBuffer: (buff) => {
|
|
77
|
+
try {
|
|
78
|
+
return JSON.parse(decoder.decode(buff), function reviver(_key, val) {
|
|
79
|
+
if (val?.$t) {
|
|
80
|
+
return base64ToUint8Array(val.$t);
|
|
81
|
+
} else {
|
|
82
|
+
return val;
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
} catch {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
91
|
+
0 && (module.exports = {
|
|
92
|
+
BinaryCodec,
|
|
93
|
+
NaiveJsonCodec
|
|
94
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { C as Codec } from '../types-3e5768ec.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Binary codec, uses [msgpack](https://www.npmjs.com/package/@msgpack/msgpack) under the hood
|
|
5
|
+
* @type {Codec}
|
|
6
|
+
*/
|
|
7
|
+
declare const BinaryCodec: Codec;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Naive JSON codec implementation using JSON.stringify and JSON.parse.
|
|
11
|
+
* @type {Codec}
|
|
12
|
+
*/
|
|
13
|
+
declare const NaiveJsonCodec: Codec;
|
|
14
|
+
|
|
15
|
+
export { BinaryCodec, Codec, NaiveJsonCodec };
|
package/dist/codec/index.d.ts
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { C as Codec } from '../types-3e5768ec.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Binary codec, uses [msgpack](https://www.npmjs.com/package/@msgpack/msgpack) under the hood
|
|
5
|
+
* @type {Codec}
|
|
6
|
+
*/
|
|
7
|
+
declare const BinaryCodec: Codec;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Naive JSON codec implementation using JSON.stringify and JSON.parse.
|
|
11
|
+
* @type {Codec}
|
|
12
|
+
*/
|
|
13
|
+
declare const NaiveJsonCodec: Codec;
|
|
14
|
+
|
|
15
|
+
export { BinaryCodec, Codec, NaiveJsonCodec };
|
package/dist/codec/index.js
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Connection, Transport, TransportClientId } from './transport/index.js';
|
|
2
|
+
import WebSocket from 'isomorphic-ws';
|
|
3
|
+
|
|
4
|
+
declare class WebSocketConnection extends Connection {
|
|
5
|
+
ws: WebSocket;
|
|
6
|
+
constructor(transport: Transport<WebSocketConnection>, connectedTo: TransportClientId, ws: WebSocket);
|
|
7
|
+
send(payload: Uint8Array): boolean;
|
|
8
|
+
close(): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { WebSocketConnection as W };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Connection, Transport, TransportClientId } from './transport/index.cjs';
|
|
2
|
+
import WebSocket from 'isomorphic-ws';
|
|
3
|
+
|
|
4
|
+
declare class WebSocketConnection extends Connection {
|
|
5
|
+
ws: WebSocket;
|
|
6
|
+
constructor(transport: Transport<WebSocketConnection>, connectedTo: TransportClientId, ws: WebSocket);
|
|
7
|
+
send(payload: Uint8Array): boolean;
|
|
8
|
+
close(): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { WebSocketConnection as W };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// logging/index.ts
|
|
21
|
+
var logging_exports = {};
|
|
22
|
+
__export(logging_exports, {
|
|
23
|
+
bindLogger: () => bindLogger,
|
|
24
|
+
log: () => log,
|
|
25
|
+
setLevel: () => setLevel
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(logging_exports);
|
|
28
|
+
var LoggingLevels = {
|
|
29
|
+
info: 0,
|
|
30
|
+
warn: 1,
|
|
31
|
+
error: 2
|
|
32
|
+
};
|
|
33
|
+
var log;
|
|
34
|
+
var defaultLoggingLevel = "warn";
|
|
35
|
+
function bindLogger(write, color) {
|
|
36
|
+
const info = color ? "\x1B[37minfo\x1B[0m" : "info";
|
|
37
|
+
const warn = color ? "\x1B[33mwarn\x1B[0m" : "warn";
|
|
38
|
+
const error = color ? "\x1B[31merr\x1B[0m" : "err";
|
|
39
|
+
log = {
|
|
40
|
+
info: (msg) => log && LoggingLevels[log.minLevel] <= 0 && write(`[river:${info}] ${msg}`),
|
|
41
|
+
warn: (msg) => log && LoggingLevels[log.minLevel] <= 1 && write(`[river:${warn}] ${msg}`),
|
|
42
|
+
error: (msg) => log && LoggingLevels[log.minLevel] <= 2 && write(`[river:${error}] ${msg}`),
|
|
43
|
+
minLevel: log?.minLevel ?? defaultLoggingLevel
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function setLevel(level) {
|
|
47
|
+
if (log) {
|
|
48
|
+
log.minLevel = level;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
52
|
+
0 && (module.exports = {
|
|
53
|
+
bindLogger,
|
|
54
|
+
log,
|
|
55
|
+
setLevel
|
|
56
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
declare const LoggingLevels: {
|
|
2
|
+
readonly info: 0;
|
|
3
|
+
readonly warn: 1;
|
|
4
|
+
readonly error: 2;
|
|
5
|
+
};
|
|
6
|
+
type LoggingLevel = keyof typeof LoggingLevels;
|
|
7
|
+
type Logger = {
|
|
8
|
+
minLevel: LoggingLevel;
|
|
9
|
+
} & {
|
|
10
|
+
[key in LoggingLevel]: (msg: string) => void;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The global River logger instance.
|
|
14
|
+
*/
|
|
15
|
+
declare let log: Logger | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Binds the given write function to River's logger {@link log}.
|
|
18
|
+
* @param write - The function to write log messages.
|
|
19
|
+
* @param color - Whether to use colored log levels.
|
|
20
|
+
*/
|
|
21
|
+
declare function bindLogger(write: (msg: string) => void, color?: boolean): void;
|
|
22
|
+
/**
|
|
23
|
+
* Sets the minimum logging level for the logger.
|
|
24
|
+
* @param level - The minimum logging level to set.
|
|
25
|
+
*/
|
|
26
|
+
declare function setLevel(level: LoggingLevel): void;
|
|
27
|
+
|
|
28
|
+
export { Logger, bindLogger, log, setLevel };
|
package/dist/logging/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ declare const LoggingLevels: {
|
|
|
4
4
|
readonly error: 2;
|
|
5
5
|
};
|
|
6
6
|
type LoggingLevel = keyof typeof LoggingLevels;
|
|
7
|
-
|
|
7
|
+
type Logger = {
|
|
8
8
|
minLevel: LoggingLevel;
|
|
9
9
|
} & {
|
|
10
10
|
[key in LoggingLevel]: (msg: string) => void;
|
|
@@ -12,17 +12,17 @@ export type Logger = {
|
|
|
12
12
|
/**
|
|
13
13
|
* The global River logger instance.
|
|
14
14
|
*/
|
|
15
|
-
|
|
15
|
+
declare let log: Logger | undefined;
|
|
16
16
|
/**
|
|
17
17
|
* Binds the given write function to River's logger {@link log}.
|
|
18
18
|
* @param write - The function to write log messages.
|
|
19
19
|
* @param color - Whether to use colored log levels.
|
|
20
20
|
*/
|
|
21
|
-
|
|
21
|
+
declare function bindLogger(write: (msg: string) => void, color?: boolean): void;
|
|
22
22
|
/**
|
|
23
23
|
* Sets the minimum logging level for the logger.
|
|
24
24
|
* @param level - The minimum logging level to set.
|
|
25
25
|
*/
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
declare function setLevel(level: LoggingLevel): void;
|
|
27
|
+
|
|
28
|
+
export { Logger, bindLogger, log, setLevel };
|