@meshagent/meshagent 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/browser/entrypoint.js +7956 -5611
  3. package/dist/browser/helpers.d.ts +10 -4
  4. package/dist/browser/helpers.js +14 -39
  5. package/dist/browser/index.d.ts +1 -0
  6. package/dist/browser/index.js +1 -0
  7. package/dist/browser/participant-token.d.ts +4 -4
  8. package/dist/browser/participant-token.js +4 -9
  9. package/dist/browser/protocol.d.ts +5 -1
  10. package/dist/browser/protocol.js +7 -2
  11. package/dist/browser/room-client.d.ts +4 -1
  12. package/dist/browser/room-client.js +2 -2
  13. package/dist/browser/sync-client.d.ts +5 -1
  14. package/dist/browser/sync-client.js +13 -2
  15. package/dist/esm/agent-client.d.ts +88 -0
  16. package/dist/esm/agent-client.js +166 -0
  17. package/dist/esm/agent.d.ts +103 -0
  18. package/dist/esm/agent.js +218 -0
  19. package/dist/esm/client.d.ts +90 -0
  20. package/dist/esm/client.js +443 -0
  21. package/dist/esm/completer.d.ts +9 -0
  22. package/dist/esm/completer.js +21 -0
  23. package/dist/esm/data-types.d.ts +44 -0
  24. package/dist/esm/data-types.js +110 -0
  25. package/dist/esm/database-client.d.ts +77 -0
  26. package/dist/esm/database-client.js +109 -0
  27. package/dist/esm/developer-client.d.ts +13 -0
  28. package/dist/esm/developer-client.js +31 -0
  29. package/dist/esm/document.d.ts +84 -0
  30. package/dist/esm/document.js +522 -0
  31. package/dist/esm/entrypoint.d.ts +49722 -0
  32. package/dist/esm/entrypoint.js +6313 -0
  33. package/dist/esm/event-emitter.d.ts +13 -0
  34. package/dist/esm/event-emitter.js +34 -0
  35. package/dist/esm/helpers.d.ts +32 -0
  36. package/dist/esm/helpers.js +46 -0
  37. package/dist/esm/index.d.ts +25 -0
  38. package/dist/esm/index.js +25 -0
  39. package/dist/esm/messaging-client.d.ts +76 -0
  40. package/dist/esm/messaging-client.js +241 -0
  41. package/dist/esm/participant-token.d.ts +36 -0
  42. package/dist/esm/participant-token.js +91 -0
  43. package/dist/esm/participant.d.ts +18 -0
  44. package/dist/esm/participant.js +36 -0
  45. package/dist/esm/protocol.d.ts +91 -0
  46. package/dist/esm/protocol.js +287 -0
  47. package/dist/esm/queues-client.d.ts +26 -0
  48. package/dist/esm/queues-client.js +42 -0
  49. package/dist/esm/requirement.d.ts +25 -0
  50. package/dist/esm/requirement.js +42 -0
  51. package/dist/esm/response.d.ts +60 -0
  52. package/dist/esm/response.js +128 -0
  53. package/dist/esm/room-client.d.ts +46 -0
  54. package/dist/esm/room-client.js +106 -0
  55. package/dist/esm/room-event.d.ts +60 -0
  56. package/dist/esm/room-event.js +72 -0
  57. package/dist/esm/room-server-client.d.ts +19 -0
  58. package/dist/esm/room-server-client.js +45 -0
  59. package/dist/esm/runtime.d.ts +6 -0
  60. package/dist/esm/runtime.js +1 -0
  61. package/dist/esm/schema.d.ts +83 -0
  62. package/dist/esm/schema.js +312 -0
  63. package/dist/esm/storage-client.d.ts +38 -0
  64. package/dist/esm/storage-client.js +79 -0
  65. package/dist/esm/stream-controller.d.ts +8 -0
  66. package/dist/esm/stream-controller.js +51 -0
  67. package/dist/esm/sync-client.d.ts +37 -0
  68. package/dist/esm/sync-client.js +125 -0
  69. package/dist/esm/utils.d.ts +14 -0
  70. package/dist/esm/utils.js +44 -0
  71. package/dist/node/entrypoint.js +9 -4
  72. package/dist/node/helpers.d.ts +10 -4
  73. package/dist/node/helpers.js +14 -39
  74. package/dist/node/index.d.ts +1 -0
  75. package/dist/node/index.js +1 -0
  76. package/dist/node/participant-token.d.ts +4 -4
  77. package/dist/node/participant-token.js +4 -9
  78. package/dist/node/protocol.d.ts +5 -1
  79. package/dist/node/protocol.js +7 -2
  80. package/dist/node/room-client.d.ts +4 -1
  81. package/dist/node/room-client.js +2 -2
  82. package/dist/node/sync-client.d.ts +5 -1
  83. package/dist/node/sync-client.js +13 -2
  84. package/package.json +4 -3
@@ -0,0 +1,91 @@
1
+ import WebSocket from "isomorphic-ws";
2
+ export type UpdateCallback = (update: Uint8Array, origin?: any) => void;
3
+ export interface ProtocolChannel {
4
+ start(onDataReceived: (data: Uint8Array) => void, { onDone, onError }: {
5
+ onDone?: () => void;
6
+ onError?: (error: any) => void;
7
+ }): void;
8
+ dispose(): void;
9
+ sendData(data: Uint8Array): Promise<void>;
10
+ }
11
+ export declare class StreamProtocolChannel implements ProtocolChannel {
12
+ input: ProtocolMessageStream<Uint8Array>;
13
+ output: ProtocolMessageStream<Uint8Array>;
14
+ started: boolean;
15
+ private _iterator;
16
+ constructor({ input, output }: {
17
+ input: ProtocolMessageStream<Uint8Array>;
18
+ output: ProtocolMessageStream<Uint8Array>;
19
+ });
20
+ start(onDataReceived: (data: Uint8Array) => void, { onDone, onError }: {
21
+ onDone?: () => void;
22
+ onError?: (error: any) => void;
23
+ }): void;
24
+ dispose(): void;
25
+ sendData(data: Uint8Array): Promise<void>;
26
+ }
27
+ export declare class WebSocketProtocolChannel implements ProtocolChannel {
28
+ url: string;
29
+ jwt: string;
30
+ webSocket: WebSocket | null;
31
+ onDataReceived?: (data: Uint8Array) => void;
32
+ private _opened;
33
+ constructor({ url, jwt }: {
34
+ url: string;
35
+ jwt: string;
36
+ });
37
+ start(onDataReceived: (data: Uint8Array) => void, { onDone, onError }: {
38
+ onDone?: () => void;
39
+ onError?: (error: any) => void;
40
+ }): void;
41
+ private _onOpen;
42
+ private _onData;
43
+ dispose(): void;
44
+ sendData(data: Uint8Array): Promise<void>;
45
+ }
46
+ export declare class ProtocolMessageStream<T> {
47
+ private _messages;
48
+ private _messageAdded;
49
+ private _closed;
50
+ add(message: T): Promise<void>;
51
+ close(): void;
52
+ stream(): AsyncGenerator<T>;
53
+ }
54
+ export type MessageHandler = (protocol: Protocol, messageId: number, type: string, data?: Uint8Array) => Promise<void> | void;
55
+ export declare class Protocol {
56
+ channel: ProtocolChannel;
57
+ handlers: {
58
+ [type: string]: MessageHandler;
59
+ };
60
+ private _id;
61
+ private _send;
62
+ private _recvPacketId;
63
+ private _recvState;
64
+ private _recvPacketTotal;
65
+ private _recvMessageId;
66
+ private _recvType;
67
+ private _recvPackets;
68
+ private _iterator;
69
+ constructor({ channel }: {
70
+ channel: ProtocolChannel;
71
+ });
72
+ addHandler(type: string, handler: MessageHandler): void;
73
+ removeHandler(type: string): void;
74
+ handleMessage(messageId: number, type: string, data?: Uint8Array): Promise<void>;
75
+ getNextMessageId(): number;
76
+ send(type: string, data: Uint8Array, id?: number): Promise<void>;
77
+ sendJson(object: any): Promise<void>;
78
+ start({ onMessage, onDone, onError }?: {
79
+ onMessage?: MessageHandler;
80
+ onDone?: () => void;
81
+ onError?: (error: any) => void;
82
+ }): void;
83
+ dispose(): void;
84
+ onDataReceived(dataPacket: Uint8Array): void;
85
+ }
86
+ export declare class WebSocketClientProtocol extends Protocol {
87
+ constructor({ url, token }: {
88
+ url: string;
89
+ token: string;
90
+ });
91
+ }
@@ -0,0 +1,287 @@
1
+ import WebSocket from "isomorphic-ws";
2
+ import { mergeUint8Arrays, decoder, encoder, unpackMessage } from "./utils";
3
+ import { Completer } from "./completer";
4
+ class ProtocolMessage {
5
+ constructor({ id, type, data }) {
6
+ this.id = id;
7
+ this.type = type;
8
+ this.data = data;
9
+ this.sent = new Completer();
10
+ }
11
+ }
12
+ export class StreamProtocolChannel {
13
+ constructor({ input, output }) {
14
+ this.started = false;
15
+ this._iterator = null;
16
+ this.input = input;
17
+ this.output = output;
18
+ }
19
+ start(onDataReceived, { onDone, onError }) {
20
+ if (this.started) {
21
+ throw new Error("Already started");
22
+ }
23
+ this.started = true;
24
+ (async () => {
25
+ this._iterator?.return(null);
26
+ try {
27
+ this._iterator = this.input.stream();
28
+ for await (const message of this._iterator) {
29
+ if (message) {
30
+ onDataReceived(message);
31
+ }
32
+ }
33
+ }
34
+ catch (error) {
35
+ if (onError) {
36
+ onError(error);
37
+ }
38
+ }
39
+ finally {
40
+ if (onDone) {
41
+ onDone();
42
+ }
43
+ }
44
+ })();
45
+ }
46
+ dispose() {
47
+ this._iterator?.return(null);
48
+ this._iterator = null;
49
+ this.input.close();
50
+ }
51
+ async sendData(data) {
52
+ this.output.add(data);
53
+ }
54
+ }
55
+ export class WebSocketProtocolChannel {
56
+ constructor({ url, jwt }) {
57
+ this.webSocket = null;
58
+ this._opened = new Completer();
59
+ this._onOpen = () => this._opened.resolve();
60
+ this._onData = (event) => {
61
+ const data = event.data;
62
+ if (data instanceof Blob) {
63
+ data.arrayBuffer().then((buffer) => {
64
+ if (this.onDataReceived) {
65
+ this.onDataReceived(new Uint8Array(buffer));
66
+ }
67
+ });
68
+ }
69
+ else if (typeof (data) == "string") {
70
+ if (this.onDataReceived) {
71
+ this.onDataReceived(new Uint8Array(encoder.encode(data)));
72
+ }
73
+ }
74
+ else if (data instanceof ArrayBuffer || data instanceof Buffer) {
75
+ if (this.onDataReceived) {
76
+ this.onDataReceived(new Uint8Array(data));
77
+ }
78
+ }
79
+ };
80
+ this.url = url;
81
+ this.jwt = jwt;
82
+ }
83
+ start(onDataReceived, { onDone, onError }) {
84
+ if (typeof (onDataReceived) != "function") {
85
+ throw new Error("onDataReceived must be a function");
86
+ }
87
+ const url = new URL(this.url);
88
+ url.searchParams.set("token", this.jwt);
89
+ this.onDataReceived = onDataReceived;
90
+ this.webSocket = new WebSocket(url.toString());
91
+ this.webSocket.addEventListener("open", this._onOpen);
92
+ this.webSocket.addEventListener("message", this._onData);
93
+ if (onDone) {
94
+ this.webSocket.addEventListener("close", onDone);
95
+ }
96
+ if (onError) {
97
+ this.webSocket.addEventListener("error", onError);
98
+ }
99
+ }
100
+ dispose() {
101
+ this.webSocket?.close();
102
+ this.webSocket = null;
103
+ }
104
+ async sendData(data) {
105
+ await this._opened.fut;
106
+ this.webSocket?.send(data);
107
+ }
108
+ }
109
+ export class ProtocolMessageStream {
110
+ constructor() {
111
+ this._messages = [];
112
+ this._messageAdded = new Completer();
113
+ this._closed = false;
114
+ }
115
+ async add(message) {
116
+ this._messages.push(message);
117
+ if (!this._messageAdded.completed) {
118
+ this._messageAdded.complete();
119
+ }
120
+ }
121
+ close() {
122
+ if (!this._messageAdded.completed) {
123
+ this._closed = true;
124
+ this._messageAdded.complete();
125
+ }
126
+ }
127
+ async *stream() {
128
+ while (!this._closed) {
129
+ await this._messageAdded.fut;
130
+ this._messageAdded = new Completer();
131
+ while (this._messages.length > 0 && !this._closed) {
132
+ const msg = this._messages.shift();
133
+ if (msg) {
134
+ yield msg;
135
+ }
136
+ }
137
+ }
138
+ }
139
+ }
140
+ export class Protocol {
141
+ constructor({ channel }) {
142
+ this.handlers = {};
143
+ this._id = 0;
144
+ this._send = new ProtocolMessageStream();
145
+ this._recvPacketId = 0;
146
+ this._recvState = "ready";
147
+ this._recvPacketTotal = 0;
148
+ this._recvMessageId = -1;
149
+ this._recvType = "";
150
+ this._recvPackets = [];
151
+ this._iterator = null;
152
+ this.channel = channel;
153
+ }
154
+ addHandler(type, handler) {
155
+ this.handlers[type] = handler;
156
+ }
157
+ removeHandler(type) {
158
+ delete this.handlers[type];
159
+ }
160
+ async handleMessage(messageId, type, data) {
161
+ const handler = this.handlers[type] ?? this.handlers["*"];
162
+ if (!handler) {
163
+ const d = data ? unpackMessage(data) : null;
164
+ console.warn(`No handler for message type ${type}; data: ${d}`);
165
+ return;
166
+ }
167
+ await handler(this, messageId, type, data);
168
+ }
169
+ getNextMessageId() {
170
+ return this._id++;
171
+ }
172
+ async send(type, data, id) {
173
+ const msg = new ProtocolMessage({ id: id ?? this.getNextMessageId(), type: type, data: data });
174
+ this._send.add(msg);
175
+ await msg.sent.fut;
176
+ }
177
+ async sendJson(object) {
178
+ return await this.send("application/json", encoder.encode(JSON.stringify(object)));
179
+ }
180
+ start({ onMessage, onDone, onError } = {}) {
181
+ if (onMessage != null) {
182
+ this.addHandler("*", onMessage);
183
+ }
184
+ this.channel.start(this.onDataReceived.bind(this), { onDone, onError });
185
+ this._iterator?.return(null);
186
+ (async () => {
187
+ this._iterator = this._send.stream();
188
+ for await (const message of this._iterator) {
189
+ if (message) {
190
+ const packets = Math.ceil((message.data.length / 1024));
191
+ const header = new Uint8Array(4 * 4);
192
+ const dataView = new DataView(header.buffer);
193
+ dataView.setUint32(0, (message.id & 0x000fffff00000000) / Math.pow(2, 32), false);
194
+ dataView.setUint32(4, message.id & 0xffffffff, false);
195
+ dataView.setUint32(8, 0, false);
196
+ dataView.setUint32(12, packets, false);
197
+ const headerPacket = mergeUint8Arrays(header, encoder.encode(message.type));
198
+ await this.channel.sendData(headerPacket);
199
+ for (var i = 0; i < packets; i++) {
200
+ const packetHeader = new Uint8Array(3 * 4);
201
+ const dataView = new DataView(packetHeader.buffer);
202
+ dataView.setUint32(0, (message.id & 0x000fffff00000000) / Math.pow(2, 32), false);
203
+ dataView.setUint32(4, message.id & 0xffffffff, false);
204
+ dataView.setUint32(8, i + 1, false);
205
+ const packet = mergeUint8Arrays(packetHeader, message.data.subarray(i * 1024, Math.min((i + 1) * 1024, message.data.length)));
206
+ await this.channel.sendData(packet);
207
+ }
208
+ message.sent.resolve();
209
+ }
210
+ }
211
+ })();
212
+ }
213
+ dispose() {
214
+ this.channel.dispose();
215
+ this._iterator?.return(null);
216
+ this._iterator = null;
217
+ }
218
+ onDataReceived(dataPacket) {
219
+ const dataView = new DataView(dataPacket.buffer);
220
+ const messageId = dataView.getUint32(4, false) + dataView.getUint32(0, false) * Math.pow(2, 32);
221
+ const packet = dataView.getUint32(8, false);
222
+ if (packet != this._recvPacketId) {
223
+ this._recvState = "error";
224
+ }
225
+ if (packet == 0) {
226
+ if (this._recvState == "ready" || this._recvState == "error") {
227
+ this._recvPacketTotal = dataView.getUint32(12, false);
228
+ this._recvMessageId = messageId;
229
+ this._recvType = decoder.decode(dataPacket.subarray(16));
230
+ if (this._recvPacketTotal == 0) {
231
+ try {
232
+ const merged = mergeUint8Arrays(...this._recvPackets);
233
+ this._recvPackets.length = 0;
234
+ this.handleMessage(messageId, this._recvType, merged);
235
+ }
236
+ finally {
237
+ this._recvState = "ready";
238
+ this._recvPacketId = 0;
239
+ this._recvType = "";
240
+ this._recvMessageId = -1;
241
+ }
242
+ }
243
+ else {
244
+ this._recvPacketId += 1;
245
+ this._recvState = "processing";
246
+ }
247
+ }
248
+ else {
249
+ this._recvState = "error";
250
+ this._recvPacketId = 0;
251
+ }
252
+ }
253
+ else if (this._recvState != "processing") {
254
+ this._recvState = "error";
255
+ this._recvPacketId = 0;
256
+ }
257
+ else {
258
+ if (messageId != this._recvMessageId) {
259
+ this._recvState = "error";
260
+ this._recvPacketId = 0;
261
+ }
262
+ this._recvPackets.push(dataPacket.subarray(12));
263
+ if (this._recvPacketTotal == this._recvPacketId) {
264
+ try {
265
+ const merged = mergeUint8Arrays(...this._recvPackets);
266
+ this._recvPackets.length = 0;
267
+ this.handleMessage(messageId, this._recvType, merged);
268
+ }
269
+ finally {
270
+ this._recvState = "ready";
271
+ this._recvPacketId = 0;
272
+ this._recvType = "";
273
+ this._recvMessageId = -1;
274
+ }
275
+ }
276
+ else {
277
+ this._recvPacketId += 1;
278
+ }
279
+ }
280
+ }
281
+ }
282
+ export class WebSocketClientProtocol extends Protocol {
283
+ constructor({ url, token }) {
284
+ const channel = new WebSocketProtocolChannel({ url, jwt: token });
285
+ super({ channel });
286
+ }
287
+ }
@@ -0,0 +1,26 @@
1
+ import { RoomClient } from "./room-client";
2
+ export declare class Queue {
3
+ name: string;
4
+ size: number;
5
+ constructor({ name, size }: {
6
+ name: string;
7
+ size: number;
8
+ });
9
+ }
10
+ export declare class QueuesClient {
11
+ private client;
12
+ constructor({ room }: {
13
+ room: RoomClient;
14
+ });
15
+ list(): Promise<Queue[]>;
16
+ open(name: string): Promise<void>;
17
+ drain(name: string): Promise<void>;
18
+ close(name: string): Promise<void>;
19
+ send(name: string, message: Record<string, any>, { create }: {
20
+ create?: boolean;
21
+ }): Promise<void>;
22
+ receive(name: string, { create, wait }: {
23
+ create?: boolean;
24
+ wait?: boolean;
25
+ }): Promise<Record<string, any> | null>;
26
+ }
@@ -0,0 +1,42 @@
1
+ import { EmptyResponse } from "./response";
2
+ export class Queue {
3
+ constructor({ name, size }) {
4
+ this.name = name;
5
+ this.size = size;
6
+ }
7
+ }
8
+ export class QueuesClient {
9
+ constructor({ room }) {
10
+ this.client = room;
11
+ }
12
+ async list() {
13
+ const response = (await this.client.sendRequest("queues.list", {}));
14
+ const queues = response.json["queues"];
15
+ return queues.map((q) => new Queue({ name: q["name"], size: q["size"] }));
16
+ }
17
+ async open(name) {
18
+ await this.client.sendRequest("queues.open", { name });
19
+ }
20
+ async drain(name) {
21
+ await this.client.sendRequest("queues.drain", { name });
22
+ }
23
+ async close(name) {
24
+ await this.client.sendRequest("queues.close", { name });
25
+ }
26
+ async send(name, message, { create = true }) {
27
+ await this.client.sendRequest("queues.send", { name, create, message });
28
+ }
29
+ async receive(name, { create = true, wait = true }) {
30
+ const response = await this.client.sendRequest("queues.receive", {
31
+ name,
32
+ create,
33
+ wait,
34
+ });
35
+ if (response instanceof EmptyResponse) {
36
+ return null;
37
+ }
38
+ else {
39
+ return response.json;
40
+ }
41
+ }
42
+ }
@@ -0,0 +1,25 @@
1
+ export declare class RoomException extends Error {
2
+ constructor(message: string);
3
+ }
4
+ export declare abstract class Requirement {
5
+ readonly name: string;
6
+ constructor({ name }: {
7
+ name: string;
8
+ });
9
+ static fromJson(r: Record<string, any>): Requirement;
10
+ abstract toJson(): Record<string, any>;
11
+ }
12
+ export declare class RequiredToolkit extends Requirement {
13
+ readonly tools?: string[];
14
+ constructor({ name, tools }: {
15
+ name: string;
16
+ tools?: string[];
17
+ });
18
+ toJson(): Record<string, any>;
19
+ }
20
+ export declare class RequiredSchema extends Requirement {
21
+ constructor({ name }: {
22
+ name: string;
23
+ });
24
+ toJson(): Record<string, any>;
25
+ }
@@ -0,0 +1,42 @@
1
+ export class RoomException extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ this.name = "RoomException";
5
+ }
6
+ }
7
+ export class Requirement {
8
+ constructor({ name }) {
9
+ this.name = name;
10
+ }
11
+ static fromJson(r) {
12
+ if ("toolkit" in r) {
13
+ return new RequiredToolkit({ name: r["toolkit"], tools: r["tools"] });
14
+ }
15
+ if ("schema" in r) {
16
+ return new RequiredSchema({ name: r["schema"] });
17
+ }
18
+ throw new RoomException("invalid requirement json");
19
+ }
20
+ }
21
+ export class RequiredToolkit extends Requirement {
22
+ constructor({ name, tools }) {
23
+ super({ name });
24
+ this.tools = tools;
25
+ }
26
+ toJson() {
27
+ return {
28
+ toolkit: this.name,
29
+ tools: this.tools,
30
+ };
31
+ }
32
+ }
33
+ export class RequiredSchema extends Requirement {
34
+ constructor({ name }) {
35
+ super({ name });
36
+ }
37
+ toJson() {
38
+ return {
39
+ schema: this.name,
40
+ };
41
+ }
42
+ }
@@ -0,0 +1,60 @@
1
+ export interface Response {
2
+ pack(): Uint8Array;
3
+ }
4
+ export declare class LinkResponse implements Response {
5
+ url: string;
6
+ name: string;
7
+ constructor({ url, name }: {
8
+ url: string;
9
+ name: string;
10
+ });
11
+ static unpack(header: Record<string, any>, payload: Uint8Array): LinkResponse;
12
+ pack(): Uint8Array;
13
+ toString(): string;
14
+ }
15
+ export declare class FileResponse implements Response {
16
+ data: Uint8Array;
17
+ name: string;
18
+ mimeType: string;
19
+ constructor({ data, name, mimeType }: {
20
+ data: Uint8Array;
21
+ name: string;
22
+ mimeType: string;
23
+ });
24
+ static unpack(header: Record<string, any>, payload: Uint8Array): FileResponse;
25
+ pack(): Uint8Array;
26
+ toString(): string;
27
+ }
28
+ export declare class TextResponse implements Response {
29
+ text: string;
30
+ constructor({ text }: {
31
+ text: string;
32
+ });
33
+ static unpack(header: Record<string, any>, payload: Uint8Array): TextResponse;
34
+ pack(): Uint8Array;
35
+ toString(): string;
36
+ }
37
+ export declare class JsonResponse implements Response {
38
+ json: Record<string, any>;
39
+ constructor({ json }: {
40
+ json: Record<string, any>;
41
+ });
42
+ static unpack(header: Record<string, any>, payload: Uint8Array): JsonResponse;
43
+ pack(): Uint8Array;
44
+ toString(): string;
45
+ }
46
+ export declare class ErrorResponse implements Response {
47
+ text: string;
48
+ constructor({ text }: {
49
+ text: string;
50
+ });
51
+ static unpack(header: Record<string, any>, payload: Uint8Array): ErrorResponse;
52
+ pack(): Uint8Array;
53
+ toString(): string;
54
+ }
55
+ export declare class EmptyResponse implements Response {
56
+ static unpack(header: Record<string, any>, payload: Uint8Array): EmptyResponse;
57
+ pack(): Uint8Array;
58
+ toString(): string;
59
+ }
60
+ export declare function unpackResponse(data: Uint8Array): Response;
@@ -0,0 +1,128 @@
1
+ import { packMessage, splitMessageHeader, splitMessagePayload } from "./utils";
2
+ export class LinkResponse {
3
+ constructor({ url, name }) {
4
+ this.url = url;
5
+ this.name = name;
6
+ }
7
+ static unpack(header, payload) {
8
+ return new LinkResponse({
9
+ url: header["url"],
10
+ name: header["name"],
11
+ });
12
+ }
13
+ pack() {
14
+ return packMessage({
15
+ type: "link",
16
+ name: this.name,
17
+ url: this.url,
18
+ });
19
+ }
20
+ toString() {
21
+ return `LinkResponse (${this.name}): ${this.url}`;
22
+ }
23
+ }
24
+ export class FileResponse {
25
+ constructor({ data, name, mimeType }) {
26
+ this.data = data;
27
+ this.name = name;
28
+ this.mimeType = mimeType;
29
+ }
30
+ static unpack(header, payload) {
31
+ return new FileResponse({
32
+ data: payload,
33
+ name: header["name"],
34
+ mimeType: header["mime_type"],
35
+ });
36
+ }
37
+ pack() {
38
+ return packMessage({
39
+ type: "file",
40
+ name: this.name,
41
+ mime_type: this.mimeType,
42
+ }, this.data);
43
+ }
44
+ toString() {
45
+ return `FileResponse (${this.name}): ${this.mimeType}`;
46
+ }
47
+ }
48
+ export class TextResponse {
49
+ constructor({ text }) {
50
+ this.text = text;
51
+ }
52
+ static unpack(header, payload) {
53
+ return new TextResponse({
54
+ text: header["text"],
55
+ });
56
+ }
57
+ pack() {
58
+ return packMessage({
59
+ type: "text",
60
+ text: this.text,
61
+ });
62
+ }
63
+ toString() {
64
+ return `TextResponse: ${this.text}`;
65
+ }
66
+ }
67
+ export class JsonResponse {
68
+ constructor({ json }) {
69
+ this.json = json;
70
+ }
71
+ static unpack(header, payload) {
72
+ return new JsonResponse({ json: header["json"] });
73
+ }
74
+ pack() {
75
+ return packMessage({
76
+ type: "json",
77
+ json: this.json,
78
+ });
79
+ }
80
+ toString() {
81
+ return `JsonResponse: ${JSON.stringify(this.json)}`;
82
+ }
83
+ }
84
+ export class ErrorResponse {
85
+ constructor({ text }) {
86
+ this.text = text;
87
+ }
88
+ static unpack(header, payload) {
89
+ return new ErrorResponse({ text: header["text"] });
90
+ }
91
+ pack() {
92
+ return packMessage({
93
+ type: "error",
94
+ text: this.text,
95
+ });
96
+ }
97
+ toString() {
98
+ return `ErrorResponse: ${this.text}`;
99
+ }
100
+ }
101
+ export class EmptyResponse {
102
+ static unpack(header, payload) {
103
+ return new EmptyResponse();
104
+ }
105
+ pack() {
106
+ return packMessage({ type: "empty" });
107
+ }
108
+ toString() {
109
+ return `EmptyResponse`;
110
+ }
111
+ }
112
+ const _responseTypes = {
113
+ empty: EmptyResponse.unpack,
114
+ error: ErrorResponse.unpack,
115
+ file: FileResponse.unpack,
116
+ json: JsonResponse.unpack,
117
+ link: LinkResponse.unpack,
118
+ text: TextResponse.unpack,
119
+ };
120
+ export function unpackResponse(data) {
121
+ const header = JSON.parse(splitMessageHeader(data));
122
+ const payload = splitMessagePayload(data);
123
+ const typeKey = header["type"];
124
+ if (!_responseTypes[typeKey]) {
125
+ throw new Error(`Unknown response type: ${typeKey}`);
126
+ }
127
+ return _responseTypes[typeKey](header, payload);
128
+ }