@dxos/edge-client 0.8.4-main.fd6878d → 0.8.4-main.fffef41
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/lib/browser/{chunk-SUXH7FH6.mjs → chunk-VESGVCLQ.mjs} +4 -7
- package/dist/lib/browser/{chunk-SUXH7FH6.mjs.map → chunk-VESGVCLQ.mjs.map} +2 -2
- package/dist/lib/browser/edge-ws-muxer.mjs +1 -1
- package/dist/lib/browser/index.mjs +525 -287
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +1 -1
- package/dist/lib/browser/testing/index.mjs.map +2 -2
- package/dist/lib/node-esm/{chunk-R6K4IIBW.mjs → chunk-JTBFRYNM.mjs} +4 -7
- package/dist/lib/node-esm/{chunk-R6K4IIBW.mjs.map → chunk-JTBFRYNM.mjs.map} +2 -2
- package/dist/lib/node-esm/edge-ws-muxer.mjs +1 -1
- package/dist/lib/node-esm/index.mjs +525 -287
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +1 -1
- package/dist/lib/node-esm/testing/index.mjs.map +2 -2
- package/dist/types/src/edge-client.d.ts +14 -14
- package/dist/types/src/edge-client.d.ts.map +1 -1
- package/dist/types/src/edge-http-client.d.ts +30 -4
- package/dist/types/src/edge-http-client.d.ts.map +1 -1
- package/dist/types/src/edge-ws-connection.d.ts +19 -0
- package/dist/types/src/edge-ws-connection.d.ts.map +1 -1
- package/dist/types/src/http-client.d.ts +10 -7
- package/dist/types/src/http-client.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +4 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/testing/test-utils.d.ts +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +15 -15
- package/src/edge-client.test.ts +4 -4
- package/src/edge-client.ts +72 -41
- package/src/edge-http-client.test.ts +1 -1
- package/src/edge-http-client.ts +188 -33
- package/src/edge-ws-connection.ts +118 -5
- package/src/http-client.test.ts +8 -5
- package/src/http-client.ts +18 -8
- package/src/index.ts +4 -3
- package/src/testing/test-utils.ts +3 -3
|
@@ -32,6 +32,22 @@ export class EdgeWsConnection extends Resource {
|
|
|
32
32
|
private _wsMuxer: WebSocketMuxer | undefined;
|
|
33
33
|
private _lastReceivedMessageTimestamp = Date.now();
|
|
34
34
|
|
|
35
|
+
private _openTimestamp: number | undefined;
|
|
36
|
+
|
|
37
|
+
// Latency tracking.
|
|
38
|
+
private _pingTimestamp: number | undefined;
|
|
39
|
+
private _rtt = 0;
|
|
40
|
+
|
|
41
|
+
// Rate tracking with sliding window.
|
|
42
|
+
private _uploadRate = 0;
|
|
43
|
+
private _downloadRate = 0;
|
|
44
|
+
private readonly _rateWindow = 10000; // 10 second sliding window.
|
|
45
|
+
private readonly _rateUpdateInterval = 1000; // Update rates every second.
|
|
46
|
+
private _bytesSamples: Array<{ timestamp: number; sent: number; received: number }> = [];
|
|
47
|
+
|
|
48
|
+
private _messagesSent = 0;
|
|
49
|
+
private _messagesReceived = 0;
|
|
50
|
+
|
|
35
51
|
constructor(
|
|
36
52
|
private readonly _identity: EdgeIdentity,
|
|
37
53
|
private readonly _connectionInfo: { url: URL; protocolHeader?: string },
|
|
@@ -49,10 +65,35 @@ export class EdgeWsConnection extends Resource {
|
|
|
49
65
|
};
|
|
50
66
|
}
|
|
51
67
|
|
|
68
|
+
public get rtt(): number {
|
|
69
|
+
return this._rtt;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public get uptime(): number {
|
|
73
|
+
return this._openTimestamp ? (Date.now() - this._openTimestamp) / 1000 : 0;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public get uploadRate(): number {
|
|
77
|
+
return this._uploadRate;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
public get downloadRate(): number {
|
|
81
|
+
return this._downloadRate;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public get messagesSent(): number {
|
|
85
|
+
return this._messagesSent;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
public get messagesReceived(): number {
|
|
89
|
+
return this._messagesReceived;
|
|
90
|
+
}
|
|
91
|
+
|
|
52
92
|
public send(message: Message): void {
|
|
53
93
|
invariant(this._ws);
|
|
54
94
|
invariant(this._wsMuxer);
|
|
55
95
|
log('sending...', { peerKey: this._identity.peerKey, payload: protocol.getPayloadType(message) });
|
|
96
|
+
this._messagesSent++;
|
|
56
97
|
if (this._ws?.protocol.includes(EdgeWebsocketProtocol.V0)) {
|
|
57
98
|
const binary = buf.toBinary(MessageSchema, message);
|
|
58
99
|
if (binary.length > CLOUDFLARE_MESSAGE_MAX_BYTES) {
|
|
@@ -63,8 +104,12 @@ export class EdgeWsConnection extends Resource {
|
|
|
63
104
|
});
|
|
64
105
|
return;
|
|
65
106
|
}
|
|
107
|
+
this._recordBytes(binary.byteLength, 0);
|
|
66
108
|
this._ws.send(binary);
|
|
67
109
|
} else {
|
|
110
|
+
// For muxer, we need to track the size of the message being sent.
|
|
111
|
+
const binary = buf.toBinary(MessageSchema, message);
|
|
112
|
+
this._recordBytes(binary.byteLength, 0);
|
|
68
113
|
this._wsMuxer.send(message).catch((e) => log.catch(e));
|
|
69
114
|
}
|
|
70
115
|
}
|
|
@@ -83,20 +128,22 @@ export class EdgeWsConnection extends Resource {
|
|
|
83
128
|
this._ws.onopen = () => {
|
|
84
129
|
if (this.isOpen) {
|
|
85
130
|
log('connected');
|
|
131
|
+
this._openTimestamp = Date.now();
|
|
86
132
|
this._callbacks.onConnected();
|
|
87
133
|
this._scheduleHeartbeats();
|
|
134
|
+
this._scheduleRateCalculation();
|
|
88
135
|
} else {
|
|
89
136
|
log.verbose('connected after becoming inactive', { currentIdentity: this._identity });
|
|
90
137
|
}
|
|
91
138
|
};
|
|
92
|
-
this._ws.onclose = (event) => {
|
|
139
|
+
this._ws.onclose = (event: WebSocket.CloseEvent) => {
|
|
93
140
|
if (this.isOpen) {
|
|
94
|
-
log.warn('disconnected
|
|
141
|
+
log.warn('server disconnected', { code: event.code, reason: event.reason });
|
|
95
142
|
this._callbacks.onRestartRequired();
|
|
96
143
|
muxer.destroy();
|
|
97
144
|
}
|
|
98
145
|
};
|
|
99
|
-
this._ws.onerror = (event) => {
|
|
146
|
+
this._ws.onerror = (event: WebSocket.ErrorEvent) => {
|
|
100
147
|
if (this.isOpen) {
|
|
101
148
|
log.warn('edge connection socket error', { error: event.error, info: event.message });
|
|
102
149
|
this._callbacks.onRestartRequired();
|
|
@@ -107,21 +154,29 @@ export class EdgeWsConnection extends Resource {
|
|
|
107
154
|
/**
|
|
108
155
|
* https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/data
|
|
109
156
|
*/
|
|
110
|
-
this._ws.onmessage = async (event) => {
|
|
157
|
+
this._ws.onmessage = async (event: WebSocket.MessageEvent) => {
|
|
111
158
|
if (!this.isOpen) {
|
|
112
159
|
log.verbose('message ignored on closed connection', { event: event.type });
|
|
113
160
|
return;
|
|
114
161
|
}
|
|
115
162
|
this._lastReceivedMessageTimestamp = Date.now();
|
|
116
163
|
if (event.data === '__pong__') {
|
|
164
|
+
// Calculate latency.
|
|
165
|
+
if (this._pingTimestamp) {
|
|
166
|
+
this._rtt = Date.now() - this._pingTimestamp;
|
|
167
|
+
this._pingTimestamp = undefined;
|
|
168
|
+
}
|
|
117
169
|
this._rescheduleHeartbeatTimeout();
|
|
118
170
|
return;
|
|
119
171
|
}
|
|
120
172
|
const bytes = await toUint8Array(event.data);
|
|
173
|
+
this._recordBytes(0, bytes.byteLength);
|
|
121
174
|
if (!this.isOpen) {
|
|
122
175
|
return;
|
|
123
176
|
}
|
|
124
177
|
|
|
178
|
+
this._messagesReceived++;
|
|
179
|
+
|
|
125
180
|
const message = this._ws?.protocol?.includes(EdgeWebsocketProtocol.V0)
|
|
126
181
|
? buf.fromBinary(MessageSchema, bytes)
|
|
127
182
|
: muxer.receiveData(bytes);
|
|
@@ -145,7 +200,7 @@ export class EdgeWsConnection extends Resource {
|
|
|
145
200
|
if (err instanceof Error && err.message.includes('WebSocket is closed before the connection is established.')) {
|
|
146
201
|
return;
|
|
147
202
|
}
|
|
148
|
-
log.warn('
|
|
203
|
+
log.warn('error closing websocket', { err });
|
|
149
204
|
}
|
|
150
205
|
}
|
|
151
206
|
|
|
@@ -156,10 +211,12 @@ export class EdgeWsConnection extends Resource {
|
|
|
156
211
|
async () => {
|
|
157
212
|
// TODO(mykola): use RFC6455 ping/pong once implemented in the browser?
|
|
158
213
|
// Cloudflare's worker responds to this `without interrupting hibernation`. https://developers.cloudflare.com/durable-objects/api/websockets/#setwebsocketautoresponse
|
|
214
|
+
this._pingTimestamp = Date.now();
|
|
159
215
|
this._ws?.send('__ping__');
|
|
160
216
|
},
|
|
161
217
|
SIGNAL_KEEPALIVE_INTERVAL,
|
|
162
218
|
);
|
|
219
|
+
this._pingTimestamp = Date.now();
|
|
163
220
|
this._ws.send('__ping__');
|
|
164
221
|
this._rescheduleHeartbeatTimeout();
|
|
165
222
|
}
|
|
@@ -187,4 +244,60 @@ export class EdgeWsConnection extends Resource {
|
|
|
187
244
|
SIGNAL_KEEPALIVE_TIMEOUT,
|
|
188
245
|
);
|
|
189
246
|
}
|
|
247
|
+
|
|
248
|
+
private _recordBytes(sent: number, received: number): void {
|
|
249
|
+
const now = Date.now();
|
|
250
|
+
|
|
251
|
+
// Find if we have a sample for the current second.
|
|
252
|
+
const currentSecond = Math.floor(now / 1000) * 1000;
|
|
253
|
+
const existingSample = this._bytesSamples.find((s) => Math.floor(s.timestamp / 1000) * 1000 === currentSecond);
|
|
254
|
+
|
|
255
|
+
if (existingSample) {
|
|
256
|
+
existingSample.sent += sent;
|
|
257
|
+
existingSample.received += received;
|
|
258
|
+
} else {
|
|
259
|
+
this._bytesSamples.push({ timestamp: now, sent, received });
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
private _scheduleRateCalculation(): void {
|
|
264
|
+
scheduleTaskInterval(
|
|
265
|
+
this._ctx,
|
|
266
|
+
async () => {
|
|
267
|
+
this._calculateRates();
|
|
268
|
+
},
|
|
269
|
+
this._rateUpdateInterval,
|
|
270
|
+
);
|
|
271
|
+
// Calculate initial rates.
|
|
272
|
+
this._calculateRates();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private _calculateRates(): void {
|
|
276
|
+
const now = Date.now();
|
|
277
|
+
const cutoff = now - this._rateWindow;
|
|
278
|
+
|
|
279
|
+
// Remove old samples.
|
|
280
|
+
this._bytesSamples = this._bytesSamples.filter((s) => s.timestamp > cutoff);
|
|
281
|
+
|
|
282
|
+
if (this._bytesSamples.length === 0) {
|
|
283
|
+
this._uploadRate = 0;
|
|
284
|
+
this._downloadRate = 0;
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Calculate total bytes and time span.
|
|
289
|
+
let totalSent = 0;
|
|
290
|
+
let totalReceived = 0;
|
|
291
|
+
const oldestTimestamp = Math.min(...this._bytesSamples.map((s) => s.timestamp));
|
|
292
|
+
const timeSpan = (now - oldestTimestamp) / 1000; // Convert to seconds.
|
|
293
|
+
|
|
294
|
+
for (const sample of this._bytesSamples) {
|
|
295
|
+
totalSent += sample.sent;
|
|
296
|
+
totalReceived += sample.received;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Calculate rates (bytes per second).
|
|
300
|
+
this._uploadRate = timeSpan > 0 ? Math.round(totalSent / timeSpan) : 0;
|
|
301
|
+
this._downloadRate = timeSpan > 0 ? Math.round(totalReceived / timeSpan) : 0;
|
|
302
|
+
}
|
|
190
303
|
}
|
package/src/http-client.test.ts
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
import
|
|
5
|
+
import * as FetchHttpClient from '@effect/platform/FetchHttpClient';
|
|
6
|
+
import * as HttpClient from '@effect/platform/HttpClient';
|
|
7
|
+
import * as Effect from 'effect/Effect';
|
|
8
|
+
import * as Function from 'effect/Function';
|
|
7
9
|
import { afterEach, beforeEach, describe, it } from 'vitest';
|
|
8
10
|
|
|
9
11
|
import { invariant } from '@dxos/invariant';
|
|
@@ -23,13 +25,14 @@ describe('HttpClient', () => {
|
|
|
23
25
|
server = undefined;
|
|
24
26
|
});
|
|
25
27
|
|
|
26
|
-
// TODO(burdon): Auth headers.
|
|
28
|
+
// TODO(burdon): Auth headers/API key for admin.
|
|
27
29
|
// TODO(burdon): Add request/response schema type checking.
|
|
30
|
+
// TODO(burdon): Test swarm.
|
|
28
31
|
it.skipIf(process.env.CI)('should retry', async ({ expect }) => {
|
|
29
32
|
invariant(server);
|
|
30
33
|
|
|
31
34
|
{
|
|
32
|
-
const result = await pipe(
|
|
35
|
+
const result = await Function.pipe(
|
|
33
36
|
withRetry(HttpClient.get(server.url)),
|
|
34
37
|
Effect.provide(FetchHttpClient.layer),
|
|
35
38
|
Effect.withSpan('EdgeHttpClient'),
|
|
@@ -39,7 +42,7 @@ describe('HttpClient', () => {
|
|
|
39
42
|
}
|
|
40
43
|
|
|
41
44
|
{
|
|
42
|
-
const result = await pipe(
|
|
45
|
+
const result = await Function.pipe(
|
|
43
46
|
HttpClient.get(server.url),
|
|
44
47
|
withLogging,
|
|
45
48
|
withRetryConfig,
|
package/src/http-client.ts
CHANGED
|
@@ -2,10 +2,14 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
5
|
+
import type * as HttpClient from '@effect/platform/HttpClient';
|
|
6
|
+
import type * as HttpClientError from '@effect/platform/HttpClientError';
|
|
7
|
+
import type * as HttpClientResponse from '@effect/platform/HttpClientResponse';
|
|
8
|
+
import * as Context from 'effect/Context';
|
|
9
|
+
import * as Duration from 'effect/Duration';
|
|
10
|
+
import * as Effect from 'effect/Effect';
|
|
11
|
+
import * as Layer from 'effect/Layer';
|
|
12
|
+
import * as Schedule from 'effect/Schedule';
|
|
9
13
|
|
|
10
14
|
import { log } from '@dxos/log';
|
|
11
15
|
|
|
@@ -28,7 +32,7 @@ export class HttpConfig extends Context.Tag('HttpConfig')<HttpConfig, RetryOptio
|
|
|
28
32
|
|
|
29
33
|
// HOC pattern.
|
|
30
34
|
export const withRetry = (
|
|
31
|
-
effect: Effect.Effect<HttpClientResponse, HttpClientError, HttpClient.HttpClient>,
|
|
35
|
+
effect: Effect.Effect<HttpClientResponse.HttpClientResponse, HttpClientError.HttpClientError, HttpClient.HttpClient>,
|
|
32
36
|
{
|
|
33
37
|
timeout = Duration.millis(1_000),
|
|
34
38
|
retryBaseDelay = Duration.millis(1_000),
|
|
@@ -48,14 +52,20 @@ export const withRetry = (
|
|
|
48
52
|
);
|
|
49
53
|
};
|
|
50
54
|
|
|
51
|
-
export const withRetryConfig = (
|
|
55
|
+
export const withRetryConfig = (
|
|
56
|
+
effect: Effect.Effect<HttpClientResponse.HttpClientResponse, HttpClientError.HttpClientError, HttpClient.HttpClient>,
|
|
57
|
+
) =>
|
|
52
58
|
Effect.gen(function* () {
|
|
53
59
|
const config = yield* HttpConfig;
|
|
54
60
|
return yield* withRetry(effect, config);
|
|
55
61
|
});
|
|
56
62
|
|
|
57
|
-
export const withLogging = <A extends HttpClientResponse, E, R>(effect: Effect.Effect<A, E, R>) =>
|
|
58
|
-
effect.pipe(
|
|
63
|
+
export const withLogging = <A extends HttpClientResponse.HttpClientResponse, E, R>(effect: Effect.Effect<A, E, R>) =>
|
|
64
|
+
effect.pipe(
|
|
65
|
+
Effect.tap((res) => {
|
|
66
|
+
log.info('response', { status: res.status });
|
|
67
|
+
}),
|
|
68
|
+
);
|
|
59
69
|
|
|
60
70
|
/**
|
|
61
71
|
*
|
package/src/index.ts
CHANGED
|
@@ -4,11 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
export * from '@dxos/protocols/buf/dxos/edge/messenger_pb';
|
|
6
6
|
|
|
7
|
-
export * from './
|
|
7
|
+
export * from './auth';
|
|
8
8
|
export * from './defs';
|
|
9
|
-
export * from './
|
|
9
|
+
export * from './edge-client';
|
|
10
10
|
export * from './errors';
|
|
11
|
-
export * from './
|
|
11
|
+
export * from './protocol';
|
|
12
12
|
export * from './edge-http-client';
|
|
13
13
|
export * from './edge-identity';
|
|
14
14
|
export * from './edge-ws-muxer';
|
|
15
|
+
export * from './http-client';
|
|
@@ -36,11 +36,11 @@ export const createTestEdgeWsServer = async (port = DEFAULT_PORT, params?: TestE
|
|
|
36
36
|
const closeTrigger = new Trigger();
|
|
37
37
|
const sendResponseMessage = createResponseSender(() => connection!.muxer);
|
|
38
38
|
|
|
39
|
-
wsServer.on('connection', (ws) => {
|
|
39
|
+
wsServer.on('connection', (ws: WebSocket) => {
|
|
40
40
|
const muxer = new WebSocketMuxer(ws);
|
|
41
41
|
connection = { ws, muxer };
|
|
42
|
-
ws.on('error', (err) => log.catch(err));
|
|
43
|
-
ws.on('message', async (data) => {
|
|
42
|
+
ws.on('error', (err: Error) => log.catch(err));
|
|
43
|
+
ws.on('message', async (data: any) => {
|
|
44
44
|
if (String(data) === '__ping__') {
|
|
45
45
|
ws.send('__pong__');
|
|
46
46
|
return;
|