@dxos/edge-client 0.8.4-main.fffef41 → 0.9.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/LICENSE +102 -5
- package/dist/lib/{node-esm/chunk-JTBFRYNM.mjs → neutral/chunk-L5ZHLJ4B.mjs} +54 -47
- package/dist/lib/neutral/chunk-L5ZHLJ4B.mjs.map +7 -0
- package/dist/lib/neutral/chunk-WQKMEZJR.mjs +30 -0
- package/dist/lib/neutral/chunk-WQKMEZJR.mjs.map +7 -0
- package/dist/lib/neutral/cors-proxy.mjs +7 -0
- package/dist/lib/{browser → neutral}/edge-ws-muxer.mjs +1 -1
- package/dist/lib/{browser → neutral}/index.mjs +553 -467
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/{browser → neutral}/testing/index.mjs +6 -31
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/auth.d.ts.map +1 -1
- package/dist/types/src/base-http-client.d.ts +48 -0
- package/dist/types/src/base-http-client.d.ts.map +1 -0
- package/dist/types/src/cors-proxy.d.ts +6 -0
- package/dist/types/src/cors-proxy.d.ts.map +1 -0
- package/dist/types/src/edge-ai-http-client.d.ts +65 -0
- package/dist/types/src/edge-ai-http-client.d.ts.map +1 -0
- package/dist/types/src/edge-client.d.ts +6 -3
- package/dist/types/src/edge-client.d.ts.map +1 -1
- package/dist/types/src/edge-http-client.d.ts +76 -75
- package/dist/types/src/edge-http-client.d.ts.map +1 -1
- package/dist/types/src/edge-identity.d.ts.map +1 -1
- package/dist/types/src/edge-ws-connection.d.ts +1 -0
- package/dist/types/src/edge-ws-connection.d.ts.map +1 -1
- package/dist/types/src/edge-ws-muxer.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/http-client.d.ts +2 -2
- package/dist/types/src/http-client.d.ts.map +1 -1
- package/dist/types/src/hub-http-client.d.ts +39 -0
- package/dist/types/src/hub-http-client.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +4 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/protocol.d.ts +1 -1
- package/dist/types/src/protocol.d.ts.map +1 -1
- package/dist/types/src/testing/test-server.d.ts.map +1 -1
- package/dist/types/src/testing/test-utils.d.ts +2 -2
- package/dist/types/src/testing/test-utils.d.ts.map +1 -1
- package/dist/types/src/utils.d.ts +1 -1
- package/dist/types/src/utils.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +33 -32
- package/src/base-http-client.ts +243 -0
- package/src/cors-proxy.ts +38 -0
- package/src/edge-ai-http-client.ts +129 -0
- package/src/edge-client.test.ts +16 -11
- package/src/edge-client.ts +37 -7
- package/src/edge-http-client.test.ts +36 -2
- package/src/edge-http-client.ts +237 -270
- package/src/edge-ws-connection.ts +2 -1
- package/src/edge-ws-muxer.ts +49 -5
- package/src/http-client.test.ts +3 -2
- package/src/hub-http-client.ts +118 -0
- package/src/index.ts +4 -0
- package/src/testing/test-utils.ts +4 -4
- package/dist/lib/browser/chunk-VESGVCLQ.mjs +0 -301
- package/dist/lib/browser/chunk-VESGVCLQ.mjs.map +0 -7
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-JTBFRYNM.mjs.map +0 -7
- package/dist/lib/node-esm/edge-ws-muxer.mjs +0 -12
- package/dist/lib/node-esm/index.mjs +0 -1363
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/testing/index.mjs +0 -186
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- /package/dist/lib/{browser/edge-ws-muxer.mjs.map → neutral/cors-proxy.mjs.map} +0 -0
- /package/dist/lib/{node-esm → neutral}/edge-ws-muxer.mjs.map +0 -0
|
@@ -50,7 +50,7 @@ export class EdgeWsConnection extends Resource {
|
|
|
50
50
|
|
|
51
51
|
constructor(
|
|
52
52
|
private readonly _identity: EdgeIdentity,
|
|
53
|
-
private readonly _connectionInfo: { url: URL; protocolHeader?: string },
|
|
53
|
+
private readonly _connectionInfo: { url: URL; protocolHeader?: string; headers?: Record<string, string> },
|
|
54
54
|
private readonly _callbacks: EdgeWsConnectionCallbacks,
|
|
55
55
|
) {
|
|
56
56
|
super();
|
|
@@ -121,6 +121,7 @@ export class EdgeWsConnection extends Resource {
|
|
|
121
121
|
this._connectionInfo.protocolHeader
|
|
122
122
|
? [...baseProtocols, this._connectionInfo.protocolHeader]
|
|
123
123
|
: [...baseProtocols],
|
|
124
|
+
this._connectionInfo.headers ? { headers: this._connectionInfo.headers } : undefined,
|
|
124
125
|
);
|
|
125
126
|
const muxer = new WebSocketMuxer(this._ws);
|
|
126
127
|
this._wsMuxer = muxer;
|
package/src/edge-ws-muxer.ts
CHANGED
|
@@ -74,6 +74,15 @@ export class WebSocketMuxer {
|
|
|
74
74
|
return;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
const chunkCount = Math.ceil(binary.length / this._maxChunkLength);
|
|
78
|
+
log('muxer sending segmented message', {
|
|
79
|
+
byteLength: binary.byteLength,
|
|
80
|
+
chunkCount,
|
|
81
|
+
channelId,
|
|
82
|
+
serviceId: message.serviceId,
|
|
83
|
+
payload: protocol.getPayloadType(message),
|
|
84
|
+
});
|
|
85
|
+
|
|
77
86
|
const terminatorSentTrigger = new Trigger();
|
|
78
87
|
const messageChunks: MessageChunk[] = [];
|
|
79
88
|
for (let i = 0; i < binary.length; i += this._maxChunkLength) {
|
|
@@ -97,7 +106,13 @@ export class WebSocketMuxer {
|
|
|
97
106
|
|
|
98
107
|
this._sendChunkedMessages();
|
|
99
108
|
|
|
100
|
-
|
|
109
|
+
await terminatorSentTrigger.wait();
|
|
110
|
+
log.debug('muxer segmented message send enqueued', {
|
|
111
|
+
byteLength: binary.byteLength,
|
|
112
|
+
chunkCount,
|
|
113
|
+
channelId,
|
|
114
|
+
serviceId: message.serviceId,
|
|
115
|
+
});
|
|
101
116
|
}
|
|
102
117
|
|
|
103
118
|
public receiveData(data: Uint8Array): Message | undefined {
|
|
@@ -106,21 +121,45 @@ export class WebSocketMuxer {
|
|
|
106
121
|
}
|
|
107
122
|
|
|
108
123
|
const [flags, channelId, ...payload] = data;
|
|
124
|
+
const chunkPayload = Buffer.from(payload);
|
|
109
125
|
let chunkAccumulator = this._inMessageAccumulator.get(channelId);
|
|
110
126
|
if (chunkAccumulator) {
|
|
111
|
-
chunkAccumulator.push(
|
|
127
|
+
chunkAccumulator.push(chunkPayload);
|
|
112
128
|
} else {
|
|
113
|
-
chunkAccumulator = [
|
|
129
|
+
chunkAccumulator = [chunkPayload];
|
|
114
130
|
this._inMessageAccumulator.set(channelId, chunkAccumulator);
|
|
131
|
+
log.debug('muxer started receiving segmented message', {
|
|
132
|
+
channelId,
|
|
133
|
+
firstChunkBytes: chunkPayload.byteLength,
|
|
134
|
+
});
|
|
115
135
|
}
|
|
116
136
|
|
|
117
137
|
if ((flags & FLAG_SEGMENT_SEQ_TERMINATED) === 0) {
|
|
118
138
|
return undefined;
|
|
119
139
|
}
|
|
120
140
|
|
|
121
|
-
const
|
|
141
|
+
const reassembled = Buffer.concat(chunkAccumulator);
|
|
142
|
+
const chunkCount = chunkAccumulator.length;
|
|
122
143
|
this._inMessageAccumulator.delete(channelId);
|
|
123
|
-
|
|
144
|
+
try {
|
|
145
|
+
const message = buf.fromBinary(MessageSchema, reassembled);
|
|
146
|
+
log('muxer reassembled segmented message', {
|
|
147
|
+
channelId,
|
|
148
|
+
chunkCount,
|
|
149
|
+
byteLength: reassembled.byteLength,
|
|
150
|
+
serviceId: message.serviceId,
|
|
151
|
+
payloadBytes: message.payload?.value?.byteLength,
|
|
152
|
+
});
|
|
153
|
+
return message;
|
|
154
|
+
} catch (error) {
|
|
155
|
+
log.error('muxer failed to decode reassembled message', {
|
|
156
|
+
channelId,
|
|
157
|
+
chunkCount,
|
|
158
|
+
byteLength: reassembled.byteLength,
|
|
159
|
+
cause: error instanceof Error ? error.message : String(error),
|
|
160
|
+
});
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
124
163
|
}
|
|
125
164
|
|
|
126
165
|
public destroy(): void {
|
|
@@ -153,6 +192,11 @@ export class WebSocketMuxer {
|
|
|
153
192
|
for (const [channelId, messages] of this._outMessageChunks.entries()) {
|
|
154
193
|
if (this._ws.bufferedAmount != null) {
|
|
155
194
|
if (this._ws.bufferedAmount + MAX_CHUNK_LENGTH > MAX_BUFFERED_AMOUNT) {
|
|
195
|
+
log.debug('muxer send paused (websocket buffer full)', {
|
|
196
|
+
channelId,
|
|
197
|
+
bufferedAmount: this._ws.bufferedAmount,
|
|
198
|
+
pendingChannels: this._outMessageChunks.size,
|
|
199
|
+
});
|
|
156
200
|
timeout = BUFFER_FULL_BACKOFF_TIMEOUT;
|
|
157
201
|
break;
|
|
158
202
|
}
|
package/src/http-client.test.ts
CHANGED
|
@@ -8,6 +8,7 @@ import * as Effect from 'effect/Effect';
|
|
|
8
8
|
import * as Function from 'effect/Function';
|
|
9
9
|
import { afterEach, beforeEach, describe, it } from 'vitest';
|
|
10
10
|
|
|
11
|
+
import { EffectEx } from '@dxos/effect';
|
|
11
12
|
import { invariant } from '@dxos/invariant';
|
|
12
13
|
|
|
13
14
|
import { HttpConfig, withLogging, withRetry, withRetryConfig } from './http-client';
|
|
@@ -36,7 +37,7 @@ describe('HttpClient', () => {
|
|
|
36
37
|
withRetry(HttpClient.get(server.url)),
|
|
37
38
|
Effect.provide(FetchHttpClient.layer),
|
|
38
39
|
Effect.withSpan('EdgeHttpClient'),
|
|
39
|
-
|
|
40
|
+
EffectEx.runAndForwardErrors,
|
|
40
41
|
);
|
|
41
42
|
expect(result).toMatchObject({ success: true, data: { value: 100 } });
|
|
42
43
|
}
|
|
@@ -49,7 +50,7 @@ describe('HttpClient', () => {
|
|
|
49
50
|
Effect.provide(FetchHttpClient.layer),
|
|
50
51
|
Effect.provide(HttpConfig.default), // TODO(burdon): Swap out to mock.
|
|
51
52
|
Effect.withSpan('EdgeHttpClient'), // TODO(burdon): OTEL.
|
|
52
|
-
|
|
53
|
+
EffectEx.runAndForwardErrors,
|
|
53
54
|
);
|
|
54
55
|
expect(result).toMatchObject({ success: true, data: { value: 100 } });
|
|
55
56
|
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Context } from '@dxos/context';
|
|
6
|
+
import {
|
|
7
|
+
type CheckEmailExistsResponse,
|
|
8
|
+
type GetAccountResponse,
|
|
9
|
+
type IssueInvitationResponse,
|
|
10
|
+
type ListAccountInvitationsResponse,
|
|
11
|
+
type LoginRequest,
|
|
12
|
+
type LoginResponse,
|
|
13
|
+
type RedeemInvitationCodeRequest,
|
|
14
|
+
type RedeemInvitationCodeResponse,
|
|
15
|
+
type RequestAccessRequest,
|
|
16
|
+
type RequestAccessResponse,
|
|
17
|
+
type ResendVerificationEmailResponse,
|
|
18
|
+
type ValidateInvitationCodeResponse,
|
|
19
|
+
} from '@dxos/protocols';
|
|
20
|
+
|
|
21
|
+
import { BaseHttpClient, type BaseHttpClientOptions, type EdgeHttpCallArgs } from './base-http-client';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* HTTP client for the hub-service API (accounts, invitations, email verification).
|
|
25
|
+
*
|
|
26
|
+
* Hub-service and the edge worker are separate Cloudflare Workers deployed at different
|
|
27
|
+
* URLs (`DX_HUB_URL` vs `DX_EDGE_URL`). This client is never used to talk to the edge
|
|
28
|
+
* worker, and vice versa — keep them separate.
|
|
29
|
+
*
|
|
30
|
+
* NOTE: Do NOT set `auth: true` on any call here. Hub-service has no `/auth` VP-challenge
|
|
31
|
+
* endpoint (it has an admin login page that 302s and is not CORS-enabled). Auth is handled
|
|
32
|
+
* via the regular request → 401 → WWW-Authenticate challenge → retry path.
|
|
33
|
+
*/
|
|
34
|
+
export class HubHttpClient extends BaseHttpClient {
|
|
35
|
+
constructor(hubUrl: string, options?: BaseHttpClientOptions) {
|
|
36
|
+
super(hubUrl, options);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
//
|
|
40
|
+
// Public (unauthenticated) endpoints
|
|
41
|
+
//
|
|
42
|
+
|
|
43
|
+
public async checkEmailExists(
|
|
44
|
+
ctx: Context,
|
|
45
|
+
body: { email: string },
|
|
46
|
+
args?: EdgeHttpCallArgs,
|
|
47
|
+
): Promise<CheckEmailExistsResponse> {
|
|
48
|
+
return this._call(ctx, new URL('/account/email/exists', this.baseUrl), { ...args, body, method: 'POST' });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public async validateInvitationCode(
|
|
52
|
+
ctx: Context,
|
|
53
|
+
body: { code: string },
|
|
54
|
+
args?: EdgeHttpCallArgs,
|
|
55
|
+
): Promise<ValidateInvitationCodeResponse> {
|
|
56
|
+
return this._call(ctx, new URL('/account/invitation-code/validate', this.baseUrl), {
|
|
57
|
+
...args,
|
|
58
|
+
body,
|
|
59
|
+
method: 'POST',
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public async redeemInvitationCode(
|
|
64
|
+
ctx: Context,
|
|
65
|
+
body: RedeemInvitationCodeRequest,
|
|
66
|
+
args?: EdgeHttpCallArgs,
|
|
67
|
+
): Promise<RedeemInvitationCodeResponse> {
|
|
68
|
+
return this._call(ctx, new URL('/account/invitation-code/redeem', this.baseUrl), {
|
|
69
|
+
...args,
|
|
70
|
+
body,
|
|
71
|
+
method: 'POST',
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Existing-account email login. Server inlines `token` for test emails; regular
|
|
77
|
+
* emails are delivered out-of-band. Response is identical for unknown emails
|
|
78
|
+
* (enumeration-safe).
|
|
79
|
+
*/
|
|
80
|
+
public async login(ctx: Context, body: LoginRequest, args?: EdgeHttpCallArgs): Promise<LoginResponse> {
|
|
81
|
+
return this._call(ctx, new URL('/account/login', this.baseUrl), { ...args, body, method: 'POST' });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public async requestAccess(
|
|
85
|
+
ctx: Context,
|
|
86
|
+
body: RequestAccessRequest,
|
|
87
|
+
args?: EdgeHttpCallArgs,
|
|
88
|
+
): Promise<RequestAccessResponse> {
|
|
89
|
+
return this._call(ctx, new URL('/account/request-access', this.baseUrl), { ...args, body, method: 'POST' });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
//
|
|
93
|
+
// Authenticated (VP) endpoints
|
|
94
|
+
//
|
|
95
|
+
|
|
96
|
+
public async getAccount(ctx: Context, args?: EdgeHttpCallArgs): Promise<GetAccountResponse> {
|
|
97
|
+
return this._call(ctx, new URL('/account/me', this.baseUrl), { ...args, method: 'GET' });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
public async deleteAccount(ctx: Context, args?: EdgeHttpCallArgs): Promise<{ deleted: boolean }> {
|
|
101
|
+
return this._call(ctx, new URL('/account/me', this.baseUrl), { ...args, method: 'DELETE' });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
public async listAccountInvitations(ctx: Context, args?: EdgeHttpCallArgs): Promise<ListAccountInvitationsResponse> {
|
|
105
|
+
return this._call(ctx, new URL('/account/invitation', this.baseUrl), { ...args, method: 'GET' });
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public async issueAccountInvitation(ctx: Context, args?: EdgeHttpCallArgs): Promise<IssueInvitationResponse> {
|
|
109
|
+
return this._call(ctx, new URL('/account/invitation/issue', this.baseUrl), { ...args, method: 'POST' });
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
public async resendVerificationEmail(
|
|
113
|
+
ctx: Context,
|
|
114
|
+
args?: EdgeHttpCallArgs,
|
|
115
|
+
): Promise<ResendVerificationEmailResponse> {
|
|
116
|
+
return this._call(ctx, new URL('/account/email/resend-verification', this.baseUrl), { ...args, method: 'POST' });
|
|
117
|
+
}
|
|
118
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -5,11 +5,15 @@
|
|
|
5
5
|
export * from '@dxos/protocols/buf/dxos/edge/messenger_pb';
|
|
6
6
|
|
|
7
7
|
export * from './auth';
|
|
8
|
+
export * from './cors-proxy';
|
|
8
9
|
export * from './defs';
|
|
9
10
|
export * from './edge-client';
|
|
10
11
|
export * from './errors';
|
|
11
12
|
export * from './protocol';
|
|
13
|
+
export * from './base-http-client';
|
|
14
|
+
export * from './edge-ai-http-client';
|
|
12
15
|
export * from './edge-http-client';
|
|
16
|
+
export * from './hub-http-client';
|
|
13
17
|
export * from './edge-identity';
|
|
14
18
|
export * from './edge-ws-muxer';
|
|
15
19
|
export * from './http-client';
|
|
@@ -16,13 +16,13 @@ import { toUint8Array } from '../protocol';
|
|
|
16
16
|
|
|
17
17
|
export const DEFAULT_PORT = 8080;
|
|
18
18
|
|
|
19
|
-
type
|
|
19
|
+
type TestEdgeWsServerProps = {
|
|
20
20
|
admitConnection?: Trigger;
|
|
21
21
|
payloadDecoder?: (payload: Uint8Array) => any;
|
|
22
22
|
messageHandler?: (payload: any) => Promise<Uint8Array | undefined>;
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
-
export const createTestEdgeWsServer = async (port = DEFAULT_PORT, params?:
|
|
25
|
+
export const createTestEdgeWsServer = async (port = DEFAULT_PORT, params?: TestEdgeWsServerProps) => {
|
|
26
26
|
const wsServer = new WebSocket.Server({
|
|
27
27
|
port,
|
|
28
28
|
verifyClient: createConnectionDelayHandler(params),
|
|
@@ -86,7 +86,7 @@ export const createTestEdgeWsServer = async (port = DEFAULT_PORT, params?: TestE
|
|
|
86
86
|
};
|
|
87
87
|
};
|
|
88
88
|
|
|
89
|
-
const createConnectionDelayHandler = (params:
|
|
89
|
+
const createConnectionDelayHandler = (params: TestEdgeWsServerProps | undefined) => {
|
|
90
90
|
return (_: any, callback: (admit: boolean) => void) => {
|
|
91
91
|
if (params?.admitConnection) {
|
|
92
92
|
log('delaying edge connection admission');
|
|
@@ -116,7 +116,7 @@ const createResponseSender = (connection: () => WebSocketMuxer) => {
|
|
|
116
116
|
};
|
|
117
117
|
};
|
|
118
118
|
|
|
119
|
-
const decodePayload = async (request: Message, params:
|
|
119
|
+
const decodePayload = async (request: Message, params: TestEdgeWsServerProps | undefined) => {
|
|
120
120
|
const requestPayload = params?.payloadDecoder
|
|
121
121
|
? params.payloadDecoder(request.payload!.value!)
|
|
122
122
|
: protocol.getPayload(request, TextMessageSchema);
|
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
// src/edge-ws-muxer.ts
|
|
2
|
-
import { Trigger } from "@dxos/async";
|
|
3
|
-
import { log } from "@dxos/log";
|
|
4
|
-
import { buf as buf2 } from "@dxos/protocols/buf";
|
|
5
|
-
import { MessageSchema as MessageSchema2 } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
|
|
6
|
-
|
|
7
|
-
// src/defs.ts
|
|
8
|
-
import { bufWkt as bufWkt2 } from "@dxos/protocols/buf";
|
|
9
|
-
import { SwarmRequestSchema, SwarmResponseSchema, TextMessageSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
|
|
10
|
-
|
|
11
|
-
// src/protocol.ts
|
|
12
|
-
import { invariant } from "@dxos/invariant";
|
|
13
|
-
import { buf, bufWkt } from "@dxos/protocols/buf";
|
|
14
|
-
import { MessageSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
|
|
15
|
-
import { bufferToArray } from "@dxos/util";
|
|
16
|
-
var __dxlog_file = "/__w/dxos/dxos/packages/core/mesh/edge-client/src/protocol.ts";
|
|
17
|
-
var getTypename = (typeName) => `type.googleapis.com/${typeName}`;
|
|
18
|
-
var Protocol = class {
|
|
19
|
-
_typeRegistry;
|
|
20
|
-
constructor(types) {
|
|
21
|
-
this._typeRegistry = buf.createRegistry(...types);
|
|
22
|
-
}
|
|
23
|
-
get typeRegistry() {
|
|
24
|
-
return this._typeRegistry;
|
|
25
|
-
}
|
|
26
|
-
toJson(message) {
|
|
27
|
-
try {
|
|
28
|
-
return buf.toJson(MessageSchema, message, {
|
|
29
|
-
registry: this.typeRegistry
|
|
30
|
-
});
|
|
31
|
-
} catch (err) {
|
|
32
|
-
return {
|
|
33
|
-
type: this.getPayloadType(message)
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Return the payload with the given type.
|
|
39
|
-
*/
|
|
40
|
-
getPayload(message, type) {
|
|
41
|
-
invariant(message.payload, void 0, {
|
|
42
|
-
F: __dxlog_file,
|
|
43
|
-
L: 40,
|
|
44
|
-
S: this,
|
|
45
|
-
A: [
|
|
46
|
-
"message.payload",
|
|
47
|
-
""
|
|
48
|
-
]
|
|
49
|
-
});
|
|
50
|
-
const payloadTypename = this.getPayloadType(message);
|
|
51
|
-
if (type && type.typeName !== payloadTypename) {
|
|
52
|
-
throw new Error(`Unexpected payload type: ${payloadTypename}; expected ${type.typeName}`);
|
|
53
|
-
}
|
|
54
|
-
invariant(bufWkt.anyIs(message.payload, type), `Unexpected payload type: ${payloadTypename}}`, {
|
|
55
|
-
F: __dxlog_file,
|
|
56
|
-
L: 46,
|
|
57
|
-
S: this,
|
|
58
|
-
A: [
|
|
59
|
-
"bufWkt.anyIs(message.payload, type)",
|
|
60
|
-
"`Unexpected payload type: ${payloadTypename}}`"
|
|
61
|
-
]
|
|
62
|
-
});
|
|
63
|
-
const payload = bufWkt.anyUnpack(message.payload, this.typeRegistry);
|
|
64
|
-
invariant(payload, `Empty payload: ${payloadTypename}}`, {
|
|
65
|
-
F: __dxlog_file,
|
|
66
|
-
L: 48,
|
|
67
|
-
S: this,
|
|
68
|
-
A: [
|
|
69
|
-
"payload",
|
|
70
|
-
"`Empty payload: ${payloadTypename}}`"
|
|
71
|
-
]
|
|
72
|
-
});
|
|
73
|
-
return payload;
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Get the payload type.
|
|
77
|
-
*/
|
|
78
|
-
getPayloadType(message) {
|
|
79
|
-
if (!message.payload) {
|
|
80
|
-
return void 0;
|
|
81
|
-
}
|
|
82
|
-
const [, type] = message.payload.typeUrl.split("/");
|
|
83
|
-
return type;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Create a packed message.
|
|
87
|
-
*/
|
|
88
|
-
createMessage(type, { source, target, payload, serviceId }) {
|
|
89
|
-
return buf.create(MessageSchema, {
|
|
90
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
91
|
-
source,
|
|
92
|
-
target,
|
|
93
|
-
serviceId,
|
|
94
|
-
payload: payload ? bufWkt.anyPack(type, buf.create(type, payload)) : void 0
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
var toUint8Array = async (data) => {
|
|
99
|
-
if (data instanceof Buffer) {
|
|
100
|
-
return bufferToArray(data);
|
|
101
|
-
}
|
|
102
|
-
if (data instanceof Blob) {
|
|
103
|
-
return new Uint8Array(await data.arrayBuffer());
|
|
104
|
-
}
|
|
105
|
-
throw new Error(`Unexpected datatype: ${data}`);
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
// src/defs.ts
|
|
109
|
-
var protocol = new Protocol([
|
|
110
|
-
SwarmRequestSchema,
|
|
111
|
-
SwarmResponseSchema,
|
|
112
|
-
TextMessageSchema,
|
|
113
|
-
bufWkt2.AnySchema
|
|
114
|
-
]);
|
|
115
|
-
|
|
116
|
-
// src/edge-ws-muxer.ts
|
|
117
|
-
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/mesh/edge-client/src/edge-ws-muxer.ts";
|
|
118
|
-
var FLAG_SEGMENT_SEQ = 1;
|
|
119
|
-
var FLAG_SEGMENT_SEQ_TERMINATED = 1 << 1;
|
|
120
|
-
var CLOUDFLARE_MESSAGE_MAX_BYTES = 1e3 * 1e3;
|
|
121
|
-
var CLOUDFLARE_RPC_MAX_BYTES = 32 * 1e3 * 1e3;
|
|
122
|
-
var MAX_CHUNK_LENGTH = 16384;
|
|
123
|
-
var MAX_BUFFERED_AMOUNT = CLOUDFLARE_MESSAGE_MAX_BYTES;
|
|
124
|
-
var BUFFER_FULL_BACKOFF_TIMEOUT = 100;
|
|
125
|
-
var WebSocketMuxer = class {
|
|
126
|
-
_ws;
|
|
127
|
-
_inMessageAccumulator = /* @__PURE__ */ new Map();
|
|
128
|
-
_outMessageChunks = /* @__PURE__ */ new Map();
|
|
129
|
-
_outMessageChannelByService = /* @__PURE__ */ new Map();
|
|
130
|
-
_sendTimeout;
|
|
131
|
-
_maxChunkLength;
|
|
132
|
-
constructor(_ws, config) {
|
|
133
|
-
this._ws = _ws;
|
|
134
|
-
this._maxChunkLength = config?.maxChunkLength ?? MAX_CHUNK_LENGTH;
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Resolves when all the message chunks get enqueued for sending.
|
|
138
|
-
*/
|
|
139
|
-
async send(message) {
|
|
140
|
-
const binary = buf2.toBinary(MessageSchema2, message);
|
|
141
|
-
const channelId = this._resolveChannel(message);
|
|
142
|
-
if (channelId == null && binary.byteLength > CLOUDFLARE_MESSAGE_MAX_BYTES || binary.byteLength > CLOUDFLARE_RPC_MAX_BYTES) {
|
|
143
|
-
log.error("Large message dropped", {
|
|
144
|
-
byteLength: binary.byteLength,
|
|
145
|
-
serviceId: message.serviceId,
|
|
146
|
-
payload: protocol.getPayloadType(message),
|
|
147
|
-
channelId
|
|
148
|
-
}, {
|
|
149
|
-
F: __dxlog_file2,
|
|
150
|
-
L: 62,
|
|
151
|
-
S: this,
|
|
152
|
-
C: (f, a) => f(...a)
|
|
153
|
-
});
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
if (channelId == null || binary.length < this._maxChunkLength) {
|
|
157
|
-
const flags = Buffer.from([
|
|
158
|
-
0
|
|
159
|
-
]);
|
|
160
|
-
this._ws.send(Buffer.concat([
|
|
161
|
-
flags,
|
|
162
|
-
binary
|
|
163
|
-
]));
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
const terminatorSentTrigger = new Trigger();
|
|
167
|
-
const messageChunks = [];
|
|
168
|
-
for (let i = 0; i < binary.length; i += this._maxChunkLength) {
|
|
169
|
-
const chunk = binary.slice(i, i + this._maxChunkLength);
|
|
170
|
-
const isLastChunk = i + this._maxChunkLength >= binary.length;
|
|
171
|
-
if (isLastChunk) {
|
|
172
|
-
const flags = Buffer.from([
|
|
173
|
-
FLAG_SEGMENT_SEQ | FLAG_SEGMENT_SEQ_TERMINATED,
|
|
174
|
-
channelId
|
|
175
|
-
]);
|
|
176
|
-
messageChunks.push({
|
|
177
|
-
payload: Buffer.concat([
|
|
178
|
-
flags,
|
|
179
|
-
chunk
|
|
180
|
-
]),
|
|
181
|
-
trigger: terminatorSentTrigger
|
|
182
|
-
});
|
|
183
|
-
} else {
|
|
184
|
-
const flags = Buffer.from([
|
|
185
|
-
FLAG_SEGMENT_SEQ,
|
|
186
|
-
channelId
|
|
187
|
-
]);
|
|
188
|
-
messageChunks.push({
|
|
189
|
-
payload: Buffer.concat([
|
|
190
|
-
flags,
|
|
191
|
-
chunk
|
|
192
|
-
])
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
const queuedMessages = this._outMessageChunks.get(channelId);
|
|
197
|
-
if (queuedMessages) {
|
|
198
|
-
queuedMessages.push(...messageChunks);
|
|
199
|
-
} else {
|
|
200
|
-
this._outMessageChunks.set(channelId, messageChunks);
|
|
201
|
-
}
|
|
202
|
-
this._sendChunkedMessages();
|
|
203
|
-
return terminatorSentTrigger.wait();
|
|
204
|
-
}
|
|
205
|
-
receiveData(data) {
|
|
206
|
-
if ((data[0] & FLAG_SEGMENT_SEQ) === 0) {
|
|
207
|
-
return buf2.fromBinary(MessageSchema2, data.slice(1));
|
|
208
|
-
}
|
|
209
|
-
const [flags, channelId, ...payload] = data;
|
|
210
|
-
let chunkAccumulator = this._inMessageAccumulator.get(channelId);
|
|
211
|
-
if (chunkAccumulator) {
|
|
212
|
-
chunkAccumulator.push(Buffer.from(payload));
|
|
213
|
-
} else {
|
|
214
|
-
chunkAccumulator = [
|
|
215
|
-
Buffer.from(payload)
|
|
216
|
-
];
|
|
217
|
-
this._inMessageAccumulator.set(channelId, chunkAccumulator);
|
|
218
|
-
}
|
|
219
|
-
if ((flags & FLAG_SEGMENT_SEQ_TERMINATED) === 0) {
|
|
220
|
-
return void 0;
|
|
221
|
-
}
|
|
222
|
-
const message = buf2.fromBinary(MessageSchema2, Buffer.concat(chunkAccumulator));
|
|
223
|
-
this._inMessageAccumulator.delete(channelId);
|
|
224
|
-
return message;
|
|
225
|
-
}
|
|
226
|
-
destroy() {
|
|
227
|
-
if (this._sendTimeout) {
|
|
228
|
-
clearTimeout(this._sendTimeout);
|
|
229
|
-
this._sendTimeout = void 0;
|
|
230
|
-
}
|
|
231
|
-
for (const channelChunks of this._outMessageChunks.values()) {
|
|
232
|
-
channelChunks.forEach((chunk) => chunk.trigger?.wake());
|
|
233
|
-
}
|
|
234
|
-
this._outMessageChunks.clear();
|
|
235
|
-
this._inMessageAccumulator.clear();
|
|
236
|
-
this._outMessageChannelByService.clear();
|
|
237
|
-
}
|
|
238
|
-
_sendChunkedMessages() {
|
|
239
|
-
if (this._sendTimeout) {
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
const send = () => {
|
|
243
|
-
if (this._ws.readyState === 2 || this._ws.readyState === 3) {
|
|
244
|
-
log.warn("send called for closed websocket", void 0, {
|
|
245
|
-
F: __dxlog_file2,
|
|
246
|
-
L: 146,
|
|
247
|
-
S: this,
|
|
248
|
-
C: (f, a) => f(...a)
|
|
249
|
-
});
|
|
250
|
-
this._sendTimeout = void 0;
|
|
251
|
-
return;
|
|
252
|
-
}
|
|
253
|
-
let timeout = 0;
|
|
254
|
-
const emptyChannels = [];
|
|
255
|
-
for (const [channelId, messages] of this._outMessageChunks.entries()) {
|
|
256
|
-
if (this._ws.bufferedAmount != null) {
|
|
257
|
-
if (this._ws.bufferedAmount + MAX_CHUNK_LENGTH > MAX_BUFFERED_AMOUNT) {
|
|
258
|
-
timeout = BUFFER_FULL_BACKOFF_TIMEOUT;
|
|
259
|
-
break;
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
const nextMessage = messages.shift();
|
|
263
|
-
if (nextMessage) {
|
|
264
|
-
this._ws.send(nextMessage.payload);
|
|
265
|
-
nextMessage.trigger?.wake();
|
|
266
|
-
} else {
|
|
267
|
-
emptyChannels.push(channelId);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
emptyChannels.forEach((channelId) => this._outMessageChunks.delete(channelId));
|
|
271
|
-
if (this._outMessageChunks.size > 0) {
|
|
272
|
-
this._sendTimeout = setTimeout(send, timeout);
|
|
273
|
-
} else {
|
|
274
|
-
this._sendTimeout = void 0;
|
|
275
|
-
}
|
|
276
|
-
};
|
|
277
|
-
this._sendTimeout = setTimeout(send);
|
|
278
|
-
}
|
|
279
|
-
_resolveChannel(message) {
|
|
280
|
-
if (!message.serviceId) {
|
|
281
|
-
return void 0;
|
|
282
|
-
}
|
|
283
|
-
let id = this._outMessageChannelByService.get(message.serviceId);
|
|
284
|
-
if (!id) {
|
|
285
|
-
id = this._outMessageChannelByService.size + 1;
|
|
286
|
-
this._outMessageChannelByService.set(message.serviceId, id);
|
|
287
|
-
}
|
|
288
|
-
return id;
|
|
289
|
-
}
|
|
290
|
-
};
|
|
291
|
-
|
|
292
|
-
export {
|
|
293
|
-
getTypename,
|
|
294
|
-
Protocol,
|
|
295
|
-
toUint8Array,
|
|
296
|
-
protocol,
|
|
297
|
-
CLOUDFLARE_MESSAGE_MAX_BYTES,
|
|
298
|
-
CLOUDFLARE_RPC_MAX_BYTES,
|
|
299
|
-
WebSocketMuxer
|
|
300
|
-
};
|
|
301
|
-
//# sourceMappingURL=chunk-VESGVCLQ.mjs.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../src/edge-ws-muxer.ts", "../../../src/defs.ts", "../../../src/protocol.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\nimport { Trigger } from '@dxos/async';\nimport { log } from '@dxos/log';\nimport { buf } from '@dxos/protocols/buf';\nimport { type Message, MessageSchema } from '@dxos/protocols/buf/dxos/edge/messenger_pb';\n\nimport { protocol } from './defs';\n\n/**\n * 0000 0001 - message contains a part of segmented message chunk sequence.\n * The next byte defines a channel id and the rest of the message contains a part of Message proto binary.\n * Messages from different channels might interleave.\n * When the flag is NOT set the rest of the message should be interpreted as the valid Message proto binary.\n */\nconst FLAG_SEGMENT_SEQ = 1;\n/**\n * 0000 0010 - message terminates a segmented message chunk sequence.\n * All the chunks accumulated for the channel specified by the second byte can be concatenated\n * and interpreted as a valid Message proto binary.\n */\nconst FLAG_SEGMENT_SEQ_TERMINATED = 1 << 1;\n\n/**\n * https://developers.cloudflare.com/durable-objects/platform/limits/\n */\nexport const CLOUDFLARE_MESSAGE_MAX_BYTES = 1000 * 1000; // 1MB\nexport const CLOUDFLARE_RPC_MAX_BYTES = 32 * 1000 * 1000; // 32MB\n\nconst MAX_CHUNK_LENGTH = 16384;\nconst MAX_BUFFERED_AMOUNT = CLOUDFLARE_MESSAGE_MAX_BYTES;\nconst BUFFER_FULL_BACKOFF_TIMEOUT = 100;\n\nexport class WebSocketMuxer {\n private readonly _inMessageAccumulator = new Map<number, Buffer[]>();\n private readonly _outMessageChunks = new Map<number, MessageChunk[]>();\n private readonly _outMessageChannelByService = new Map<string, number>();\n\n private _sendTimeout: any | undefined;\n\n private readonly _maxChunkLength: number;\n\n constructor(\n private readonly _ws: WebSocketCompat,\n config?: { maxChunkLength: number },\n ) {\n this._maxChunkLength = config?.maxChunkLength ?? MAX_CHUNK_LENGTH;\n }\n\n /**\n * Resolves when all the message chunks get enqueued for sending.\n */\n public async send(message: Message): Promise<void> {\n const binary = buf.toBinary(MessageSchema, message);\n const channelId = this._resolveChannel(message);\n if (\n (channelId == null && binary.byteLength > CLOUDFLARE_MESSAGE_MAX_BYTES) ||\n binary.byteLength > CLOUDFLARE_RPC_MAX_BYTES\n ) {\n log.error('Large message dropped', {\n byteLength: binary.byteLength,\n serviceId: message.serviceId,\n payload: protocol.getPayloadType(message),\n channelId,\n });\n return;\n }\n\n if (channelId == null || binary.length < this._maxChunkLength) {\n const flags = Buffer.from([0]);\n this._ws.send(Buffer.concat([flags, binary]));\n return;\n }\n\n const terminatorSentTrigger = new Trigger();\n const messageChunks: MessageChunk[] = [];\n for (let i = 0; i < binary.length; i += this._maxChunkLength) {\n const chunk = binary.slice(i, i + this._maxChunkLength);\n const isLastChunk = i + this._maxChunkLength >= binary.length;\n if (isLastChunk) {\n const flags = Buffer.from([FLAG_SEGMENT_SEQ | FLAG_SEGMENT_SEQ_TERMINATED, channelId]);\n messageChunks.push({ payload: Buffer.concat([flags, chunk]), trigger: terminatorSentTrigger });\n } else {\n const flags = Buffer.from([FLAG_SEGMENT_SEQ, channelId]);\n messageChunks.push({ payload: Buffer.concat([flags, chunk]) });\n }\n }\n\n const queuedMessages = this._outMessageChunks.get(channelId);\n if (queuedMessages) {\n queuedMessages.push(...messageChunks);\n } else {\n this._outMessageChunks.set(channelId, messageChunks);\n }\n\n this._sendChunkedMessages();\n\n return terminatorSentTrigger.wait();\n }\n\n public receiveData(data: Uint8Array): Message | undefined {\n if ((data[0] & FLAG_SEGMENT_SEQ) === 0) {\n return buf.fromBinary(MessageSchema, data.slice(1));\n }\n\n const [flags, channelId, ...payload] = data;\n let chunkAccumulator = this._inMessageAccumulator.get(channelId);\n if (chunkAccumulator) {\n chunkAccumulator.push(Buffer.from(payload));\n } else {\n chunkAccumulator = [Buffer.from(payload)];\n this._inMessageAccumulator.set(channelId, chunkAccumulator);\n }\n\n if ((flags & FLAG_SEGMENT_SEQ_TERMINATED) === 0) {\n return undefined;\n }\n\n const message = buf.fromBinary(MessageSchema, Buffer.concat(chunkAccumulator));\n this._inMessageAccumulator.delete(channelId);\n return message;\n }\n\n public destroy(): void {\n if (this._sendTimeout) {\n clearTimeout(this._sendTimeout);\n this._sendTimeout = undefined;\n }\n for (const channelChunks of this._outMessageChunks.values()) {\n channelChunks.forEach((chunk) => chunk.trigger?.wake());\n }\n this._outMessageChunks.clear();\n this._inMessageAccumulator.clear();\n this._outMessageChannelByService.clear();\n }\n\n private _sendChunkedMessages(): void {\n if (this._sendTimeout) {\n return;\n }\n\n const send = () => {\n if (this._ws.readyState === WebSocket.CLOSING || this._ws.readyState === WebSocket.CLOSED) {\n log.warn('send called for closed websocket');\n this._sendTimeout = undefined;\n return;\n }\n\n let timeout = 0;\n const emptyChannels: number[] = [];\n for (const [channelId, messages] of this._outMessageChunks.entries()) {\n if (this._ws.bufferedAmount != null) {\n if (this._ws.bufferedAmount + MAX_CHUNK_LENGTH > MAX_BUFFERED_AMOUNT) {\n timeout = BUFFER_FULL_BACKOFF_TIMEOUT;\n break;\n }\n }\n\n const nextMessage = messages.shift();\n if (nextMessage) {\n this._ws.send(nextMessage.payload);\n nextMessage.trigger?.wake();\n } else {\n emptyChannels.push(channelId);\n }\n }\n\n emptyChannels.forEach((channelId) => this._outMessageChunks.delete(channelId));\n\n if (this._outMessageChunks.size > 0) {\n this._sendTimeout = setTimeout(send, timeout);\n } else {\n this._sendTimeout = undefined;\n }\n };\n this._sendTimeout = setTimeout(send);\n }\n\n private _resolveChannel(message: Message): number | undefined {\n if (!message.serviceId) {\n return undefined;\n }\n let id = this._outMessageChannelByService.get(message.serviceId);\n if (!id) {\n id = this._outMessageChannelByService.size + 1;\n this._outMessageChannelByService.set(message.serviceId, id);\n }\n return id;\n }\n}\n\ntype WebSocketCompat = {\n readonly readyState: number;\n /**\n * Not available in workerd.\n */\n bufferedAmount?: number;\n send(message: (ArrayBuffer | ArrayBufferView) | string): void;\n};\n\ntype MessageChunk = {\n payload: Buffer;\n /**\n * Wakes when the payload is enqueued by WebSocket.\n */\n trigger?: Trigger;\n};\n\n/**\n * To avoid using isomorphic-ws on edge.\n */\nenum WebSocket {\n CLOSING = 2,\n CLOSED = 3,\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { bufWkt } from '@dxos/protocols/buf';\nimport { SwarmRequestSchema, SwarmResponseSchema, TextMessageSchema } from '@dxos/protocols/buf/dxos/edge/messenger_pb';\n\nimport { Protocol } from './protocol';\n\nexport const protocol = new Protocol([SwarmRequestSchema, SwarmResponseSchema, TextMessageSchema, bufWkt.AnySchema]);\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { invariant } from '@dxos/invariant';\nimport { buf, bufWkt } from '@dxos/protocols/buf';\nimport { type Message, MessageSchema, type PeerSchema } from '@dxos/protocols/buf/dxos/edge/messenger_pb';\nimport { bufferToArray } from '@dxos/util';\n\nexport type PeerData = buf.MessageInitShape<typeof PeerSchema>;\n\nexport const getTypename = (typeName: string) => `type.googleapis.com/${typeName}`;\n\n/**\n * NOTE: The type registry should be extended with all message types.\n */\nexport class Protocol {\n private readonly _typeRegistry: buf.Registry;\n\n constructor(types: buf.DescMessage[]) {\n this._typeRegistry = buf.createRegistry(...types);\n }\n\n get typeRegistry(): buf.Registry {\n return this._typeRegistry;\n }\n\n toJson(message: Message): any {\n try {\n return buf.toJson(MessageSchema, message, { registry: this.typeRegistry });\n } catch (err) {\n return { type: this.getPayloadType(message) };\n }\n }\n\n /**\n * Return the payload with the given type.\n */\n getPayload<Desc extends buf.DescMessage>(message: Message, type: Desc): buf.MessageShape<Desc> {\n invariant(message.payload);\n const payloadTypename = this.getPayloadType(message);\n if (type && type.typeName !== payloadTypename) {\n throw new Error(`Unexpected payload type: ${payloadTypename}; expected ${type.typeName}`);\n }\n\n invariant(bufWkt.anyIs(message.payload, type), `Unexpected payload type: ${payloadTypename}}`);\n const payload = bufWkt.anyUnpack(message.payload, this.typeRegistry) as buf.MessageShape<Desc>;\n invariant(payload, `Empty payload: ${payloadTypename}}`);\n return payload;\n }\n\n /**\n * Get the payload type.\n */\n getPayloadType(message: Message): string | undefined {\n if (!message.payload) {\n return undefined;\n }\n\n const [, type] = message.payload.typeUrl.split('/');\n return type;\n }\n\n /**\n * Create a packed message.\n */\n createMessage<Desc extends buf.DescMessage>(\n type: Desc,\n {\n source,\n target,\n payload,\n serviceId,\n }: {\n source?: PeerData;\n target?: PeerData[];\n payload?: buf.MessageInitShape<Desc>;\n serviceId?: string;\n },\n ): Message {\n return buf.create(MessageSchema, {\n timestamp: new Date().toISOString(),\n source,\n target,\n serviceId,\n payload: payload ? bufWkt.anyPack(type, buf.create(type, payload)) : undefined,\n });\n }\n}\n\n/**\n * Convert websocket data to Uint8Array.\n */\nexport const toUint8Array = async (data: any): Promise<Uint8Array> => {\n // Node.\n if (data instanceof Buffer) {\n return bufferToArray(data);\n }\n\n // Browser.\n if (data instanceof Blob) {\n return new Uint8Array(await (data as Blob).arrayBuffer());\n }\n\n throw new Error(`Unexpected datatype: ${data}`);\n};\n"],
|
|
5
|
-
"mappings": ";AAIA,SAASA,eAAe;AACxB,SAASC,WAAW;AACpB,SAASC,OAAAA,YAAW;AACpB,SAAuBC,iBAAAA,sBAAqB;;;ACH5C,SAASC,UAAAA,eAAc;AACvB,SAASC,oBAAoBC,qBAAqBC,yBAAyB;;;ACD3E,SAASC,iBAAiB;AAC1B,SAASC,KAAKC,cAAc;AAC5B,SAAuBC,qBAAsC;AAC7D,SAASC,qBAAqB;;AAIvB,IAAMC,cAAc,CAACC,aAAqB,uBAAuBA,QAAAA;AAKjE,IAAMC,WAAN,MAAMA;EACMC;EAEjB,YAAYC,OAA0B;AACpC,SAAKD,gBAAgBP,IAAIS,eAAc,GAAID,KAAAA;EAC7C;EAEA,IAAIE,eAA6B;AAC/B,WAAO,KAAKH;EACd;EAEAI,OAAOC,SAAuB;AAC5B,QAAI;AACF,aAAOZ,IAAIW,OAAOT,eAAeU,SAAS;QAAEC,UAAU,KAAKH;MAAa,CAAA;IAC1E,SAASI,KAAK;AACZ,aAAO;QAAEC,MAAM,KAAKC,eAA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ACjC;EAEQO,gBAAgBN,SAAsC;AAC5D,QAAI,CAACA,QAAQU,WAAW;AACtB,aAAO2B;IACT;AACA,QAAIoB,KAAK,KAAKhE,4BAA4BoC,IAAI7B,QAAQU,SAAS;AAC/D,QAAI,CAAC+C,IAAI;AACPA,WAAK,KAAKhE,4BAA4B8D,OAAO;AAC7C,WAAK9D,4BAA4BqC,IAAI9B,QAAQU,WAAW+C,EAAAA;IAC1D;AACA,WAAOA;EACT;AACF;",
|
|
6
|
-
"names": ["Trigger", "log", "buf", "MessageSchema", "bufWkt", "SwarmRequestSchema", "SwarmResponseSchema", "TextMessageSchema", "invariant", "buf", "bufWkt", "MessageSchema", "bufferToArray", "getTypename", "typeName", "Protocol", "_typeRegistry", "types", "createRegistry", "typeRegistry", "toJson", "message", "registry", "err", "type", "getPayloadType", "getPayload", "payload", "payloadTypename", "Error", "anyIs", "anyUnpack", "undefined", "typeUrl", "split", "createMessage", "source", "target", "serviceId", "create", "timestamp", "Date", "toISOString", "anyPack", "toUint8Array", "data", "Buffer", "Blob", "Uint8Array", "arrayBuffer", "protocol", "Protocol", "SwarmRequestSchema", "SwarmResponseSchema", "TextMessageSchema", "bufWkt", "AnySchema", "FLAG_SEGMENT_SEQ", "FLAG_SEGMENT_SEQ_TERMINATED", "CLOUDFLARE_MESSAGE_MAX_BYTES", "CLOUDFLARE_RPC_MAX_BYTES", "MAX_CHUNK_LENGTH", "MAX_BUFFERED_AMOUNT", "BUFFER_FULL_BACKOFF_TIMEOUT", "WebSocketMuxer", "_inMessageAccumulator", "Map", "_outMessageChunks", "_outMessageChannelByService", "_sendTimeout", "_maxChunkLength", "_ws", "config", "maxChunkLength", "send", "message", "binary", "buf", "toBinary", "MessageSchema", "channelId", "_resolveChannel", "byteLength", "log", "error", "serviceId", "payload", "protocol", "getPayloadType", "length", "flags", "Buffer", "from", "concat", "terminatorSentTrigger", "Trigger", "messageChunks", "i", "chunk", "slice", "isLastChunk", "push", "trigger", "queuedMessages", "get", "set", "_sendChunkedMessages", "wait", "receiveData", "data", "fromBinary", "chunkAccumulator", "undefined", "delete", "destroy", "clearTimeout", "channelChunks", "values", "forEach", "wake", "clear", "readyState", "warn", "timeout", "emptyChannels", "messages", "entries", "bufferedAmount", "nextMessage", "shift", "size", "setTimeout", "id"]
|
|
7
|
-
}
|