@meshagent/meshagent 0.0.11 → 0.0.13
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/browser/agent-client.d.ts +88 -0
- package/dist/browser/agent-client.js +174 -0
- package/dist/browser/agent.d.ts +103 -0
- package/dist/browser/agent.js +229 -0
- package/dist/browser/client.d.ts +90 -0
- package/dist/browser/client.js +449 -0
- package/dist/browser/completer.d.ts +9 -0
- package/dist/browser/completer.js +25 -0
- package/dist/browser/data-types.d.ts +44 -0
- package/dist/browser/data-types.js +119 -0
- package/dist/browser/database-client.d.ts +77 -0
- package/dist/browser/database-client.js +113 -0
- package/dist/browser/developer-client.d.ts +13 -0
- package/dist/browser/developer-client.js +35 -0
- package/dist/browser/document.d.ts +84 -0
- package/dist/browser/document.js +531 -0
- package/dist/browser/entrypoint.d.ts +49722 -0
- package/dist/browser/entrypoint.js +8659 -0
- package/dist/browser/event-emitter.d.ts +13 -0
- package/dist/browser/event-emitter.js +38 -0
- package/dist/browser/helpers.d.ts +26 -0
- package/dist/browser/helpers.js +79 -0
- package/dist/browser/index.d.ts +24 -0
- package/dist/browser/index.js +40 -0
- package/dist/browser/messaging-client.d.ts +76 -0
- package/dist/browser/messaging-client.js +248 -0
- package/dist/browser/participant-token.d.ts +36 -0
- package/dist/browser/participant-token.js +104 -0
- package/dist/browser/participant.d.ts +18 -0
- package/dist/browser/participant.js +42 -0
- package/dist/browser/protocol.d.ts +87 -0
- package/dist/browser/protocol.js +305 -0
- package/dist/browser/queues-client.d.ts +26 -0
- package/dist/browser/queues-client.js +47 -0
- package/dist/browser/requirement.d.ts +25 -0
- package/dist/browser/requirement.js +49 -0
- package/dist/browser/response.d.ts +60 -0
- package/dist/browser/response.js +138 -0
- package/dist/browser/room-client.d.ts +43 -0
- package/dist/browser/room-client.js +109 -0
- package/dist/browser/room-event.d.ts +60 -0
- package/dist/browser/room-event.js +82 -0
- package/dist/browser/room-server-client.d.ts +19 -0
- package/dist/browser/room-server-client.js +50 -0
- package/dist/browser/runtime.d.ts +6 -0
- package/dist/browser/runtime.js +8 -0
- package/dist/browser/schema.d.ts +83 -0
- package/dist/browser/schema.js +321 -0
- package/dist/browser/storage-client.d.ts +38 -0
- package/dist/browser/storage-client.js +86 -0
- package/dist/browser/stream-controller.d.ts +8 -0
- package/dist/browser/stream-controller.js +55 -0
- package/dist/browser/sync-client.d.ts +33 -0
- package/dist/browser/sync-client.js +123 -0
- package/dist/browser/utils.d.ts +14 -0
- package/dist/browser/utils.js +59 -0
- package/dist/node/agent-client.d.ts +88 -0
- package/dist/node/agent-client.js +174 -0
- package/dist/node/agent.d.ts +103 -0
- package/dist/node/agent.js +229 -0
- package/dist/node/client.d.ts +90 -0
- package/dist/node/client.js +449 -0
- package/dist/node/completer.d.ts +9 -0
- package/dist/node/completer.js +25 -0
- package/dist/node/data-types.d.ts +44 -0
- package/dist/node/data-types.js +119 -0
- package/dist/node/database-client.d.ts +77 -0
- package/dist/node/database-client.js +113 -0
- package/dist/node/developer-client.d.ts +13 -0
- package/dist/node/developer-client.js +35 -0
- package/dist/node/document.d.ts +84 -0
- package/dist/node/document.js +531 -0
- package/dist/node/entrypoint.d.ts +49722 -0
- package/dist/node/entrypoint.js +6321 -0
- package/dist/node/event-emitter.d.ts +13 -0
- package/dist/node/event-emitter.js +38 -0
- package/dist/node/helpers.d.ts +26 -0
- package/dist/node/helpers.js +79 -0
- package/dist/node/index.d.ts +24 -0
- package/dist/node/index.js +40 -0
- package/dist/node/messaging-client.d.ts +76 -0
- package/dist/node/messaging-client.js +248 -0
- package/dist/node/participant-token.d.ts +36 -0
- package/dist/node/participant-token.js +104 -0
- package/dist/node/participant.d.ts +18 -0
- package/dist/node/participant.js +42 -0
- package/dist/node/protocol.d.ts +87 -0
- package/dist/node/protocol.js +305 -0
- package/dist/node/queues-client.d.ts +26 -0
- package/dist/node/queues-client.js +47 -0
- package/dist/node/requirement.d.ts +25 -0
- package/dist/node/requirement.js +49 -0
- package/dist/node/response.d.ts +60 -0
- package/dist/node/response.js +138 -0
- package/dist/node/room-client.d.ts +43 -0
- package/dist/node/room-client.js +109 -0
- package/dist/node/room-event.d.ts +60 -0
- package/dist/node/room-event.js +82 -0
- package/dist/node/room-server-client.d.ts +19 -0
- package/dist/node/room-server-client.js +50 -0
- package/dist/node/runtime.d.ts +6 -0
- package/dist/node/runtime.js +8 -0
- package/dist/node/schema.d.ts +83 -0
- package/dist/node/schema.js +321 -0
- package/dist/node/storage-client.d.ts +38 -0
- package/dist/node/storage-client.js +86 -0
- package/dist/node/stream-controller.d.ts +8 -0
- package/dist/node/stream-controller.js +55 -0
- package/dist/node/sync-client.d.ts +33 -0
- package/dist/node/sync-client.js +123 -0
- package/dist/node/utils.d.ts +14 -0
- package/dist/node/utils.js +59 -0
- package/package.json +35 -22
- package/README.md +0 -0
- package/babel.config.cjs +0 -3
- package/client.js +0 -422
- package/dist/entrypoint.js +0 -11262
- package/dist/entrypoint.js.map +0 -7
- package/document-server-client.js +0 -246
- package/entrypoint.js +0 -11568
- package/index.js +0 -2
- package/protocol.js +0 -459
- package/server.js +0 -310
- package/test/assert.js +0 -19
- package/test/document-server-client.test.js +0 -33
- package/test/document.test.js +0 -313
- package/test/protocol.test.js +0 -64
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { RoomClient } from "./room-client";
|
|
2
|
+
export declare abstract class Participant {
|
|
3
|
+
readonly id: string;
|
|
4
|
+
protected readonly client: RoomClient;
|
|
5
|
+
protected _attributes: Record<string, any>;
|
|
6
|
+
protected _connections: string[];
|
|
7
|
+
constructor(client: RoomClient, id: string);
|
|
8
|
+
get connections(): ReadonlyArray<string>;
|
|
9
|
+
getAttribute(name: string): any;
|
|
10
|
+
}
|
|
11
|
+
export declare class RemoteParticipant extends Participant {
|
|
12
|
+
readonly role: string;
|
|
13
|
+
constructor(client: RoomClient, id: string, role: string);
|
|
14
|
+
}
|
|
15
|
+
export declare class LocalParticipant extends Participant {
|
|
16
|
+
constructor(client: RoomClient, id: string);
|
|
17
|
+
setAttribute(name: string, value: any): Promise<void>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalParticipant = exports.RemoteParticipant = exports.Participant = void 0;
|
|
4
|
+
const utils_1 = require("./utils");
|
|
5
|
+
class Participant {
|
|
6
|
+
constructor(client, id) {
|
|
7
|
+
this._attributes = {};
|
|
8
|
+
this._connections = [];
|
|
9
|
+
this.client = client;
|
|
10
|
+
this.id = id;
|
|
11
|
+
}
|
|
12
|
+
get connections() {
|
|
13
|
+
return this._connections;
|
|
14
|
+
}
|
|
15
|
+
getAttribute(name) {
|
|
16
|
+
return this._attributes[name];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.Participant = Participant;
|
|
20
|
+
class RemoteParticipant extends Participant {
|
|
21
|
+
constructor(client, id, role) {
|
|
22
|
+
super(client, id);
|
|
23
|
+
this.role = role;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.RemoteParticipant = RemoteParticipant;
|
|
27
|
+
class LocalParticipant extends Participant {
|
|
28
|
+
constructor(client, id) {
|
|
29
|
+
super(client, id);
|
|
30
|
+
}
|
|
31
|
+
async setAttribute(name, value) {
|
|
32
|
+
this._attributes[name] = value;
|
|
33
|
+
try {
|
|
34
|
+
const payload = utils_1.encoder.encode(JSON.stringify({ [name]: value }));
|
|
35
|
+
await this.client.protocol.send("set_attributes", payload);
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
console.warn("Unable to send attribute changes", err);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.LocalParticipant = LocalParticipant;
|
|
@@ -0,0 +1,87 @@
|
|
|
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?: null): void;
|
|
79
|
+
dispose(): void;
|
|
80
|
+
onDataReceived(dataPacket: Uint8Array): void;
|
|
81
|
+
}
|
|
82
|
+
export declare class WebSocketClientProtocol extends Protocol {
|
|
83
|
+
constructor({ url, token }: {
|
|
84
|
+
url: string;
|
|
85
|
+
token: string;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.WebSocketClientProtocol = exports.Protocol = exports.ProtocolMessageStream = exports.WebSocketProtocolChannel = exports.StreamProtocolChannel = void 0;
|
|
7
|
+
const isomorphic_ws_1 = __importDefault(require("isomorphic-ws"));
|
|
8
|
+
const utils_1 = require("./utils");
|
|
9
|
+
const completer_1 = require("./completer");
|
|
10
|
+
class ProtocolMessage {
|
|
11
|
+
constructor({ id, type, data }) {
|
|
12
|
+
this.id = id;
|
|
13
|
+
this.type = type;
|
|
14
|
+
this.data = data;
|
|
15
|
+
this.sent = new completer_1.Completer();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
class StreamProtocolChannel {
|
|
19
|
+
constructor({ input, output }) {
|
|
20
|
+
this.started = false;
|
|
21
|
+
this._iterator = null;
|
|
22
|
+
this.input = input;
|
|
23
|
+
this.output = output;
|
|
24
|
+
}
|
|
25
|
+
start(onDataReceived, { onDone, onError }) {
|
|
26
|
+
if (this.started) {
|
|
27
|
+
throw new Error("Already started");
|
|
28
|
+
}
|
|
29
|
+
this.started = true;
|
|
30
|
+
(async () => {
|
|
31
|
+
this._iterator?.return(null);
|
|
32
|
+
try {
|
|
33
|
+
this._iterator = this.input.stream();
|
|
34
|
+
for await (const message of this._iterator) {
|
|
35
|
+
if (message) {
|
|
36
|
+
onDataReceived(message);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
if (onError) {
|
|
42
|
+
onError(error);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
finally {
|
|
46
|
+
if (onDone) {
|
|
47
|
+
onDone();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
})();
|
|
51
|
+
}
|
|
52
|
+
dispose() {
|
|
53
|
+
this._iterator?.return(null);
|
|
54
|
+
this._iterator = null;
|
|
55
|
+
this.input.close();
|
|
56
|
+
}
|
|
57
|
+
async sendData(data) {
|
|
58
|
+
this.output.add(data);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.StreamProtocolChannel = StreamProtocolChannel;
|
|
62
|
+
class WebSocketProtocolChannel {
|
|
63
|
+
constructor({ url, jwt }) {
|
|
64
|
+
this.webSocket = null;
|
|
65
|
+
this._opened = new completer_1.Completer();
|
|
66
|
+
this._onOpen = () => this._opened.resolve();
|
|
67
|
+
this._onData = (event) => {
|
|
68
|
+
const data = event.data;
|
|
69
|
+
if (data instanceof Blob) {
|
|
70
|
+
(0, utils_1.blobToArrayBuffer)(data).then((buffer) => {
|
|
71
|
+
if (this.onDataReceived) {
|
|
72
|
+
this.onDataReceived(new Uint8Array(buffer));
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
else if (typeof (data) == "string") {
|
|
77
|
+
if (this.onDataReceived) {
|
|
78
|
+
this.onDataReceived(new Uint8Array(utils_1.encoder.encode(data)));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else if (data instanceof ArrayBuffer || data instanceof Buffer) {
|
|
82
|
+
if (this.onDataReceived) {
|
|
83
|
+
this.onDataReceived(new Uint8Array(data));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
this.url = url;
|
|
88
|
+
this.jwt = jwt;
|
|
89
|
+
}
|
|
90
|
+
start(onDataReceived, { onDone, onError }) {
|
|
91
|
+
if (typeof (onDataReceived) != "function") {
|
|
92
|
+
throw new Error("onDataReceived must be a function");
|
|
93
|
+
}
|
|
94
|
+
const url = new URL(this.url);
|
|
95
|
+
url.searchParams.set("token", this.jwt);
|
|
96
|
+
this.onDataReceived = onDataReceived;
|
|
97
|
+
this.webSocket = new isomorphic_ws_1.default(url.toString());
|
|
98
|
+
this.webSocket.addEventListener("open", this._onOpen);
|
|
99
|
+
this.webSocket.addEventListener("message", this._onData);
|
|
100
|
+
if (onDone) {
|
|
101
|
+
this.webSocket.addEventListener("close", onDone);
|
|
102
|
+
}
|
|
103
|
+
if (onError) {
|
|
104
|
+
this.webSocket.addEventListener("error", onError);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
dispose() {
|
|
108
|
+
this.webSocket?.close();
|
|
109
|
+
this.webSocket = null;
|
|
110
|
+
}
|
|
111
|
+
async sendData(data) {
|
|
112
|
+
await this._opened.fut;
|
|
113
|
+
this.webSocket?.send(data);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.WebSocketProtocolChannel = WebSocketProtocolChannel;
|
|
117
|
+
class ProtocolMessageStream {
|
|
118
|
+
constructor() {
|
|
119
|
+
this._messages = [];
|
|
120
|
+
this._messageAdded = new completer_1.Completer();
|
|
121
|
+
this._closed = false;
|
|
122
|
+
}
|
|
123
|
+
async add(message) {
|
|
124
|
+
this._messages.push(message);
|
|
125
|
+
if (!this._messageAdded.completed) {
|
|
126
|
+
this._messageAdded.complete();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
close() {
|
|
130
|
+
if (!this._messageAdded.completed) {
|
|
131
|
+
this._closed = true;
|
|
132
|
+
this._messageAdded.complete();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
async *stream() {
|
|
136
|
+
while (!this._closed) {
|
|
137
|
+
await this._messageAdded.fut;
|
|
138
|
+
this._messageAdded = new completer_1.Completer();
|
|
139
|
+
while (this._messages.length > 0 && !this._closed) {
|
|
140
|
+
const msg = this._messages.shift();
|
|
141
|
+
if (msg) {
|
|
142
|
+
yield msg;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
exports.ProtocolMessageStream = ProtocolMessageStream;
|
|
149
|
+
class Protocol {
|
|
150
|
+
constructor({ channel }) {
|
|
151
|
+
this.handlers = {};
|
|
152
|
+
this._id = 0;
|
|
153
|
+
this._send = new ProtocolMessageStream();
|
|
154
|
+
this._recvPacketId = 0;
|
|
155
|
+
this._recvState = "ready";
|
|
156
|
+
this._recvPacketTotal = 0;
|
|
157
|
+
this._recvMessageId = -1;
|
|
158
|
+
this._recvType = "";
|
|
159
|
+
this._recvPackets = [];
|
|
160
|
+
this._iterator = null;
|
|
161
|
+
this.channel = channel;
|
|
162
|
+
}
|
|
163
|
+
addHandler(type, handler) {
|
|
164
|
+
this.handlers[type] = handler;
|
|
165
|
+
}
|
|
166
|
+
removeHandler(type) {
|
|
167
|
+
delete this.handlers[type];
|
|
168
|
+
}
|
|
169
|
+
async handleMessage(messageId, type, data) {
|
|
170
|
+
console.log(this.handlers, Object.keys(this.handlers));
|
|
171
|
+
const handler = this.handlers[type] ?? this.handlers["*"];
|
|
172
|
+
await handler(this, messageId, type, data);
|
|
173
|
+
}
|
|
174
|
+
getNextMessageId() {
|
|
175
|
+
return this._id++;
|
|
176
|
+
}
|
|
177
|
+
async send(type, data, id) {
|
|
178
|
+
const msg = new ProtocolMessage({ id: id ?? this.getNextMessageId(), type: type, data: data });
|
|
179
|
+
this._send.add(msg);
|
|
180
|
+
await msg.sent.fut;
|
|
181
|
+
}
|
|
182
|
+
async sendJson(object) {
|
|
183
|
+
return await this.send("application/json", utils_1.encoder.encode(JSON.stringify(object)));
|
|
184
|
+
}
|
|
185
|
+
start(onMessage = null) {
|
|
186
|
+
if (onMessage != null) {
|
|
187
|
+
this.addHandler("*", onMessage);
|
|
188
|
+
}
|
|
189
|
+
this.channel.start(this.onDataReceived.bind(this), {});
|
|
190
|
+
this._iterator?.return(null);
|
|
191
|
+
(async () => {
|
|
192
|
+
this._iterator = this._send.stream();
|
|
193
|
+
for await (const message of this._iterator) {
|
|
194
|
+
if (message) {
|
|
195
|
+
console.log(`message recv on protocol ${message.id} ${message.type}`);
|
|
196
|
+
const packets = Math.ceil((message.data.length / 1024));
|
|
197
|
+
const header = new Uint8Array(4 * 4);
|
|
198
|
+
const dataView = new DataView(header.buffer);
|
|
199
|
+
dataView.setUint32(0, (message.id & 0x000fffff00000000) / Math.pow(2, 32), false);
|
|
200
|
+
dataView.setUint32(4, message.id & 0xffffffff, false);
|
|
201
|
+
dataView.setUint32(8, 0, false);
|
|
202
|
+
dataView.setUint32(12, packets, false);
|
|
203
|
+
const headerPacket = (0, utils_1.mergeUint8Arrays)(header, utils_1.encoder.encode(message.type));
|
|
204
|
+
await this.channel.sendData(headerPacket);
|
|
205
|
+
for (var i = 0; i < packets; i++) {
|
|
206
|
+
const packetHeader = new Uint8Array(3 * 4);
|
|
207
|
+
const dataView = new DataView(packetHeader.buffer);
|
|
208
|
+
dataView.setUint32(0, (message.id & 0x000fffff00000000) / Math.pow(2, 32), false);
|
|
209
|
+
dataView.setUint32(4, message.id & 0xffffffff, false);
|
|
210
|
+
dataView.setUint32(8, i + 1, false);
|
|
211
|
+
const packet = (0, utils_1.mergeUint8Arrays)(packetHeader, message.data.subarray(i * 1024, Math.min((i + 1) * 1024, message.data.length)));
|
|
212
|
+
await this.channel.sendData(packet);
|
|
213
|
+
}
|
|
214
|
+
message.sent.resolve();
|
|
215
|
+
console.log(`message sent on protocol ${message.id} ${message.type}`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
console.log("protocol done");
|
|
219
|
+
})();
|
|
220
|
+
}
|
|
221
|
+
dispose() {
|
|
222
|
+
this.channel.dispose();
|
|
223
|
+
this._iterator?.return(null);
|
|
224
|
+
this._iterator = null;
|
|
225
|
+
}
|
|
226
|
+
onDataReceived(dataPacket) {
|
|
227
|
+
const dataView = new DataView(dataPacket.buffer);
|
|
228
|
+
const messageId = dataView.getUint32(4, false) + dataView.getUint32(0, false) * Math.pow(2, 32);
|
|
229
|
+
const packet = dataView.getUint32(8, false);
|
|
230
|
+
if (packet != this._recvPacketId) {
|
|
231
|
+
this._recvState = "error";
|
|
232
|
+
console.log(dataPacket);
|
|
233
|
+
console.log(`received out of order packet got ${packet} expected ${this._recvPacketId}, total ${this._recvPacketTotal} message ID: ${messageId}`);
|
|
234
|
+
}
|
|
235
|
+
if (packet == 0) {
|
|
236
|
+
if (this._recvState == "ready" || this._recvState == "error") {
|
|
237
|
+
this._recvPacketTotal = dataView.getUint32(12, false);
|
|
238
|
+
this._recvMessageId = messageId;
|
|
239
|
+
this._recvType = utils_1.decoder.decode(dataPacket.subarray(16));
|
|
240
|
+
console.log(`recieved packet ${this._recvType}`);
|
|
241
|
+
if (this._recvPacketTotal == 0) {
|
|
242
|
+
try {
|
|
243
|
+
const merged = (0, utils_1.mergeUint8Arrays)(...this._recvPackets);
|
|
244
|
+
this._recvPackets.length = 0;
|
|
245
|
+
this.handleMessage(messageId, this._recvType, merged);
|
|
246
|
+
}
|
|
247
|
+
finally {
|
|
248
|
+
console.log("expecting packet reset to 0");
|
|
249
|
+
this._recvState = "ready";
|
|
250
|
+
this._recvPacketId = 0;
|
|
251
|
+
this._recvType = "";
|
|
252
|
+
this._recvMessageId = -1;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
this._recvPacketId += 1;
|
|
257
|
+
console.log(`expecting packet ${this._recvPacketId}`);
|
|
258
|
+
this._recvState = "processing";
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
this._recvState = "error";
|
|
263
|
+
this._recvPacketId = 0;
|
|
264
|
+
console.log("received packet 0 in invalid state");
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
else if (this._recvState != "processing") {
|
|
268
|
+
this._recvState = "error";
|
|
269
|
+
this._recvPacketId = 0;
|
|
270
|
+
console.log("received datapacket in invalid state");
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
if (messageId != this._recvMessageId) {
|
|
274
|
+
this._recvState = "error";
|
|
275
|
+
this._recvPacketId = 0;
|
|
276
|
+
console.log("received packet from incorrect message");
|
|
277
|
+
}
|
|
278
|
+
this._recvPackets.push(dataPacket.subarray(12));
|
|
279
|
+
if (this._recvPacketTotal == this._recvPacketId) {
|
|
280
|
+
try {
|
|
281
|
+
const merged = (0, utils_1.mergeUint8Arrays)(...this._recvPackets);
|
|
282
|
+
this._recvPackets.length = 0;
|
|
283
|
+
this.handleMessage(messageId, this._recvType, merged);
|
|
284
|
+
}
|
|
285
|
+
finally {
|
|
286
|
+
this._recvState = "ready";
|
|
287
|
+
this._recvPacketId = 0;
|
|
288
|
+
this._recvType = "";
|
|
289
|
+
this._recvMessageId = -1;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
this._recvPacketId += 1;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
exports.Protocol = Protocol;
|
|
299
|
+
class WebSocketClientProtocol extends Protocol {
|
|
300
|
+
constructor({ url, token }) {
|
|
301
|
+
const channel = new WebSocketProtocolChannel({ url, jwt: token });
|
|
302
|
+
super({ channel });
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
exports.WebSocketClientProtocol = WebSocketClientProtocol;
|
|
@@ -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,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QueuesClient = exports.Queue = void 0;
|
|
4
|
+
const response_1 = require("./response");
|
|
5
|
+
class Queue {
|
|
6
|
+
constructor({ name, size }) {
|
|
7
|
+
this.name = name;
|
|
8
|
+
this.size = size;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.Queue = Queue;
|
|
12
|
+
class QueuesClient {
|
|
13
|
+
constructor({ room }) {
|
|
14
|
+
this.client = room;
|
|
15
|
+
}
|
|
16
|
+
async list() {
|
|
17
|
+
const response = (await this.client.sendRequest("queues.list", {}));
|
|
18
|
+
const queues = response.json["queues"];
|
|
19
|
+
return queues.map((q) => new Queue({ name: q["name"], size: q["size"] }));
|
|
20
|
+
}
|
|
21
|
+
async open(name) {
|
|
22
|
+
await this.client.sendRequest("queues.open", { name });
|
|
23
|
+
}
|
|
24
|
+
async drain(name) {
|
|
25
|
+
await this.client.sendRequest("queues.drain", { name });
|
|
26
|
+
}
|
|
27
|
+
async close(name) {
|
|
28
|
+
await this.client.sendRequest("queues.close", { name });
|
|
29
|
+
}
|
|
30
|
+
async send(name, message, { create = true }) {
|
|
31
|
+
await this.client.sendRequest("queues.send", { name, create, message });
|
|
32
|
+
}
|
|
33
|
+
async receive(name, { create = true, wait = true }) {
|
|
34
|
+
const response = await this.client.sendRequest("queues.receive", {
|
|
35
|
+
name,
|
|
36
|
+
create,
|
|
37
|
+
wait,
|
|
38
|
+
});
|
|
39
|
+
if (response instanceof response_1.EmptyResponse) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
return response.json;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.QueuesClient = QueuesClient;
|
|
@@ -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,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequiredSchema = exports.RequiredToolkit = exports.Requirement = exports.RoomException = void 0;
|
|
4
|
+
class RoomException extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "RoomException";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.RoomException = RoomException;
|
|
11
|
+
class Requirement {
|
|
12
|
+
constructor({ name }) {
|
|
13
|
+
this.name = name;
|
|
14
|
+
}
|
|
15
|
+
static fromJson(r) {
|
|
16
|
+
if ("toolkit" in r) {
|
|
17
|
+
return new RequiredToolkit({ name: r["toolkit"], tools: r["tools"] });
|
|
18
|
+
}
|
|
19
|
+
if ("schema" in r) {
|
|
20
|
+
return new RequiredSchema({ name: r["schema"] });
|
|
21
|
+
}
|
|
22
|
+
throw new RoomException("invalid requirement json");
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.Requirement = Requirement;
|
|
26
|
+
class RequiredToolkit extends Requirement {
|
|
27
|
+
constructor({ name, tools }) {
|
|
28
|
+
super({ name });
|
|
29
|
+
this.tools = tools;
|
|
30
|
+
}
|
|
31
|
+
toJson() {
|
|
32
|
+
return {
|
|
33
|
+
toolkit: this.name,
|
|
34
|
+
tools: this.tools,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.RequiredToolkit = RequiredToolkit;
|
|
39
|
+
class RequiredSchema extends Requirement {
|
|
40
|
+
constructor({ name }) {
|
|
41
|
+
super({ name });
|
|
42
|
+
}
|
|
43
|
+
toJson() {
|
|
44
|
+
return {
|
|
45
|
+
schema: this.name,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.RequiredSchema = RequiredSchema;
|
|
@@ -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;
|