@mtkruto/browser 0.119.0 → 0.120.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/README.md +1 -1
- package/esm/0_errors.d.ts.map +1 -1
- package/esm/0_errors.js +9 -31
- package/esm/3_errors.js +2 -12
- package/esm/4_errors.js +2 -12
- package/esm/_dnt.polyfills.d.ts +0 -99
- package/esm/_dnt.polyfills.d.ts.map +1 -1
- package/esm/_dnt.polyfills.js +1 -127
- package/esm/client/0_abortable_loop.js +26 -39
- package/esm/client/0_storage_operations.js +179 -218
- package/esm/client/1_client_plain.js +4 -22
- package/esm/client/2_account_manager.js +140 -149
- package/esm/client/2_bot_info_manager.js +26 -38
- package/esm/client/2_business_connection_manager.js +10 -23
- package/esm/client/2_client_encrypted.js +198 -215
- package/esm/client/2_file_manager.js +255 -262
- package/esm/client/2_network_statistics_manager.js +31 -44
- package/esm/client/2_payment_manager.js +7 -20
- package/esm/client/2_reaction_manager.js +7 -20
- package/esm/client/2_translations_manager.js +101 -111
- package/esm/client/2_update_manager.js +750 -745
- package/esm/client/3_client_encrypted_pool.js +10 -26
- package/esm/client/3_message_manager.js +503 -508
- package/esm/client/3_video_chat_manager.js +57 -68
- package/esm/client/4_callback_query_manager.js +18 -30
- package/esm/client/4_chat_list_manager.js +140 -146
- package/esm/client/4_chat_manager.js +161 -169
- package/esm/client/4_checklist_manager.js +26 -39
- package/esm/client/4_context.js +244 -259
- package/esm/client/4_forum_manager.js +67 -73
- package/esm/client/4_gift_manager.js +22 -35
- package/esm/client/4_inline_query_manager.js +16 -28
- package/esm/client/4_link_preview_manager.js +6 -19
- package/esm/client/4_poll_manager.js +44 -57
- package/esm/client/4_story_manager.js +41 -53
- package/esm/client/5_composer.js +13 -26
- package/esm/client/6_client.js +866 -896
- package/esm/client/6_client_dispatcher.js +308 -325
- package/esm/client/7_client_worker.js +16 -29
- package/esm/connection/1_connection_tcp.js +55 -82
- package/esm/connection/1_connection_web_socket.js +75 -91
- package/esm/deps/jsr.io/@roj/tgcrypto/1.0.1/dist/tgcrypto.js +3 -11
- package/esm/deps/jsr.io/@std/async/1.2.0/mux_async_iterator.js +31 -47
- package/esm/deps/jsr.io/@std/async/1.2.0/tee.js +11 -34
- package/esm/deps/jsr.io/@std/cache/0.2.2/lru_cache.js +30 -47
- package/esm/deps/jsr.io/@std/datetime/0.225.7/_date_time_formatter.js +4 -17
- package/esm/session/0_session_state.js +12 -38
- package/esm/session/1_session.js +49 -72
- package/esm/session/2_session_encrypted.js +422 -420
- package/esm/storage/2_storage_indexed_db.js +26 -44
- package/esm/storage/2_storage_local_storage.js +3 -16
- package/esm/storage/2_storage_memory.js +24 -41
- package/esm/storage/2_storage_session_storage.js +3 -16
- package/esm/tl/1_tl_reader.d.ts +1 -1
- package/esm/tl/1_tl_reader.d.ts.map +1 -1
- package/esm/tl/1_tl_reader.js +95 -103
- package/esm/tl/1_tl_writer.js +169 -178
- package/esm/transport/0_transport.js +1 -8
- package/esm/transport/1_transport_abridged.js +11 -24
- package/esm/transport/1_transport_intermediate.js +10 -23
- package/esm/utilities/0_mutex.js +4 -19
- package/esm/utilities/0_part_stream.js +11 -25
- package/esm/utilities/1_crypto.js +42 -53
- package/esm/utilities/2_queue.js +29 -47
- package/package.json +1 -1
- package/script/0_errors.d.ts.map +1 -1
- package/script/0_errors.js +9 -31
- package/script/3_errors.js +2 -12
- package/script/4_errors.js +2 -12
- package/script/_dnt.polyfills.d.ts +0 -99
- package/script/_dnt.polyfills.d.ts.map +1 -1
- package/script/_dnt.polyfills.js +0 -128
- package/script/client/0_abortable_loop.js +27 -40
- package/script/client/0_storage_operations.js +179 -218
- package/script/client/1_client_plain.js +4 -22
- package/script/client/2_account_manager.js +140 -149
- package/script/client/2_bot_info_manager.js +26 -38
- package/script/client/2_business_connection_manager.js +10 -23
- package/script/client/2_client_encrypted.js +199 -216
- package/script/client/2_file_manager.js +255 -262
- package/script/client/2_network_statistics_manager.js +32 -45
- package/script/client/2_payment_manager.js +7 -20
- package/script/client/2_reaction_manager.js +7 -20
- package/script/client/2_translations_manager.js +102 -112
- package/script/client/2_update_manager.js +750 -745
- package/script/client/3_client_encrypted_pool.js +10 -26
- package/script/client/3_message_manager.js +503 -508
- package/script/client/3_video_chat_manager.js +57 -68
- package/script/client/4_callback_query_manager.js +18 -30
- package/script/client/4_chat_list_manager.js +140 -146
- package/script/client/4_chat_manager.js +161 -169
- package/script/client/4_checklist_manager.js +26 -39
- package/script/client/4_context.js +244 -259
- package/script/client/4_forum_manager.js +67 -73
- package/script/client/4_gift_manager.js +22 -35
- package/script/client/4_inline_query_manager.js +16 -28
- package/script/client/4_link_preview_manager.js +6 -19
- package/script/client/4_poll_manager.js +44 -57
- package/script/client/4_story_manager.js +41 -53
- package/script/client/5_composer.js +13 -26
- package/script/client/6_client.js +866 -896
- package/script/client/6_client_dispatcher.js +308 -325
- package/script/client/7_client_worker.js +16 -29
- package/script/connection/1_connection_tcp.js +55 -82
- package/script/connection/1_connection_web_socket.js +75 -91
- package/script/deps/jsr.io/@roj/tgcrypto/1.0.1/dist/tgcrypto.js +3 -11
- package/script/deps/jsr.io/@std/async/1.2.0/mux_async_iterator.js +31 -47
- package/script/deps/jsr.io/@std/async/1.2.0/tee.js +11 -34
- package/script/deps/jsr.io/@std/cache/0.2.2/lru_cache.js +30 -47
- package/script/deps/jsr.io/@std/datetime/0.225.7/_date_time_formatter.js +4 -17
- package/script/session/0_session_state.js +12 -38
- package/script/session/1_session.js +49 -72
- package/script/session/2_session_encrypted.js +423 -421
- package/script/storage/2_storage_indexed_db.js +26 -44
- package/script/storage/2_storage_local_storage.js +3 -16
- package/script/storage/2_storage_memory.js +24 -41
- package/script/storage/2_storage_session_storage.js +3 -16
- package/script/tl/1_tl_reader.d.ts +1 -1
- package/script/tl/1_tl_reader.d.ts.map +1 -1
- package/script/tl/1_tl_reader.js +96 -104
- package/script/tl/1_tl_writer.js +170 -179
- package/script/transport/0_transport.js +1 -8
- package/script/transport/1_transport_abridged.js +11 -24
- package/script/transport/1_transport_intermediate.js +10 -23
- package/script/utilities/0_mutex.js +4 -19
- package/script/utilities/0_part_stream.js +11 -25
- package/script/utilities/1_crypto.js +43 -54
- package/script/utilities/2_queue.js +30 -48
|
@@ -17,36 +17,24 @@
|
|
|
17
17
|
* You should have received a copy of the GNU Lesser General Public License
|
|
18
18
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
19
19
|
*/
|
|
20
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
21
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
22
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
23
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
24
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
25
|
-
};
|
|
26
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
27
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
28
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
29
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
30
|
-
};
|
|
31
|
-
var _ClientWorker_worker, _ClientWorker_clients, _ClientWorker_L;
|
|
32
20
|
import { InputError } from "../0_errors.js";
|
|
33
21
|
import { getLogger } from "../utilities/1_logger.js";
|
|
34
22
|
import { ClientDispatcher } from "./6_client_dispatcher.js";
|
|
35
23
|
export class ClientWorker {
|
|
24
|
+
#worker;
|
|
25
|
+
#clients = new Map();
|
|
26
|
+
#L = getLogger("ClientWorker");
|
|
36
27
|
constructor(worker) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
_ClientWorker_L.set(this, getLogger("ClientWorker"));
|
|
40
|
-
__classPrivateFieldSet(this, _ClientWorker_worker, worker, "f");
|
|
41
|
-
__classPrivateFieldGet(this, _ClientWorker_worker, "f").addEventListener("message", async (e) => {
|
|
28
|
+
this.#worker = worker;
|
|
29
|
+
this.#worker.addEventListener("message", async (e) => {
|
|
42
30
|
const message = e.data;
|
|
43
|
-
|
|
31
|
+
this.#L.debug("received message from worker", message);
|
|
44
32
|
if (message.type === "response") {
|
|
45
|
-
|
|
33
|
+
this.#clients.get(message.clientId)?.handleResponse(message);
|
|
46
34
|
}
|
|
47
35
|
else if (message.type === "request") {
|
|
48
36
|
if (message.method === "handleInvokeError") {
|
|
49
|
-
const client =
|
|
37
|
+
const client = this.#clients.get(message.clientId);
|
|
50
38
|
if (client) {
|
|
51
39
|
const result = await client.handleInvokeError(message);
|
|
52
40
|
const response = {
|
|
@@ -56,34 +44,33 @@ export class ClientWorker {
|
|
|
56
44
|
isError: false,
|
|
57
45
|
data: result,
|
|
58
46
|
};
|
|
59
|
-
|
|
47
|
+
this.#worker.postMessage(response);
|
|
60
48
|
}
|
|
61
49
|
}
|
|
62
50
|
}
|
|
63
51
|
});
|
|
64
52
|
}
|
|
65
53
|
async createClient(id, params) {
|
|
66
|
-
if (
|
|
54
|
+
if (this.#clients.has(id)) {
|
|
67
55
|
throw new InputError("Client already created");
|
|
68
56
|
}
|
|
69
|
-
const client = new ClientDispatcher(
|
|
70
|
-
|
|
57
|
+
const client = new ClientDispatcher(this.#worker, id);
|
|
58
|
+
this.#clients.set(id, client);
|
|
71
59
|
try {
|
|
72
60
|
await client.init(params);
|
|
73
61
|
}
|
|
74
62
|
catch (err) {
|
|
75
|
-
|
|
63
|
+
this.#clients.delete(id);
|
|
76
64
|
throw err;
|
|
77
65
|
}
|
|
78
66
|
return client;
|
|
79
67
|
}
|
|
80
68
|
getClient(id) {
|
|
81
|
-
let client =
|
|
69
|
+
let client = this.#clients.get(id);
|
|
82
70
|
if (!client) {
|
|
83
|
-
client = new ClientDispatcher(
|
|
84
|
-
|
|
71
|
+
client = new ClientDispatcher(this.#worker, id);
|
|
72
|
+
this.#clients.set(id, client);
|
|
85
73
|
}
|
|
86
74
|
return client;
|
|
87
75
|
}
|
|
88
76
|
}
|
|
89
|
-
_ClientWorker_worker = new WeakMap(), _ClientWorker_clients = new WeakMap(), _ClientWorker_L = new WeakMap();
|
|
@@ -17,127 +17,110 @@
|
|
|
17
17
|
* You should have received a copy of the GNU Lesser General Public License
|
|
18
18
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
19
19
|
*/
|
|
20
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
21
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
22
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
23
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
24
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
25
|
-
};
|
|
26
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
27
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
28
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
29
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
30
|
-
};
|
|
31
|
-
var _ConnectionTCP_instances, _ConnectionTCP_hostname, _ConnectionTCP_port, _ConnectionTCP_connection, _ConnectionTCP_rMutex, _ConnectionTCP_wMutex, _ConnectionTCP_buffer, _ConnectionTCP_nextResolve, _ConnectionTCP_canRead, _ConnectionTCP_canWrite, _ConnectionTCP_assertConnected, _ConnectionTCP_rejectRead;
|
|
32
20
|
import { concat, iterateReader } from "../0_deps.js";
|
|
33
21
|
import { ConnectionError } from "../0_errors.js";
|
|
34
22
|
import { getLogger, Mutex } from "../1_utilities.js";
|
|
35
23
|
const L = getLogger("ConnectionTCP");
|
|
36
24
|
export class ConnectionTCP {
|
|
25
|
+
#hostname;
|
|
26
|
+
#port;
|
|
27
|
+
#connection;
|
|
28
|
+
#rMutex = new Mutex();
|
|
29
|
+
#wMutex = new Mutex();
|
|
30
|
+
#buffer = new Uint8Array();
|
|
31
|
+
#nextResolve = null;
|
|
32
|
+
#canRead = false;
|
|
33
|
+
#canWrite = false;
|
|
34
|
+
connect = Deno.connect;
|
|
35
|
+
stateChangeHandler;
|
|
36
|
+
callback;
|
|
37
37
|
constructor(hostname, port) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
_ConnectionTCP_port.set(this, void 0);
|
|
41
|
-
_ConnectionTCP_connection.set(this, void 0);
|
|
42
|
-
_ConnectionTCP_rMutex.set(this, new Mutex());
|
|
43
|
-
_ConnectionTCP_wMutex.set(this, new Mutex());
|
|
44
|
-
_ConnectionTCP_buffer.set(this, new Uint8Array());
|
|
45
|
-
_ConnectionTCP_nextResolve.set(this, null);
|
|
46
|
-
_ConnectionTCP_canRead.set(this, false);
|
|
47
|
-
_ConnectionTCP_canWrite.set(this, false);
|
|
48
|
-
Object.defineProperty(this, "connect", {
|
|
49
|
-
enumerable: true,
|
|
50
|
-
configurable: true,
|
|
51
|
-
writable: true,
|
|
52
|
-
value: Deno.connect
|
|
53
|
-
});
|
|
54
|
-
Object.defineProperty(this, "stateChangeHandler", {
|
|
55
|
-
enumerable: true,
|
|
56
|
-
configurable: true,
|
|
57
|
-
writable: true,
|
|
58
|
-
value: void 0
|
|
59
|
-
});
|
|
60
|
-
Object.defineProperty(this, "callback", {
|
|
61
|
-
enumerable: true,
|
|
62
|
-
configurable: true,
|
|
63
|
-
writable: true,
|
|
64
|
-
value: void 0
|
|
65
|
-
});
|
|
66
|
-
__classPrivateFieldSet(this, _ConnectionTCP_hostname, hostname, "f");
|
|
67
|
-
__classPrivateFieldSet(this, _ConnectionTCP_port, port, "f");
|
|
38
|
+
this.#hostname = hostname;
|
|
39
|
+
this.#port = port;
|
|
68
40
|
}
|
|
69
41
|
get isConnected() {
|
|
70
|
-
return !!
|
|
42
|
+
return !!this.#connection && this.#canRead && this.#canWrite;
|
|
43
|
+
}
|
|
44
|
+
#assertConnected() {
|
|
45
|
+
if (!this.isConnected) {
|
|
46
|
+
throw new ConnectionError("The connection is not open.");
|
|
47
|
+
}
|
|
71
48
|
}
|
|
72
49
|
async open() {
|
|
73
50
|
if (this.isConnected) {
|
|
74
51
|
return;
|
|
75
52
|
}
|
|
76
53
|
const connection = await this.connect({
|
|
77
|
-
hostname:
|
|
78
|
-
port:
|
|
54
|
+
hostname: this.#hostname,
|
|
55
|
+
port: this.#port,
|
|
79
56
|
});
|
|
80
57
|
connection.setNoDelay(true);
|
|
81
58
|
connection.setKeepAlive(true);
|
|
82
|
-
|
|
59
|
+
this.#canRead = this.#canWrite = true;
|
|
83
60
|
this.stateChangeHandler?.(true);
|
|
84
|
-
L.debug("connected to",
|
|
61
|
+
L.debug("connected to", this.#hostname, "port", this.#port);
|
|
85
62
|
Promise.resolve().then(async () => {
|
|
86
63
|
do {
|
|
87
64
|
try {
|
|
88
65
|
for await (const chunk of iterateReader(connection)) {
|
|
89
66
|
this.callback?.read(chunk.length);
|
|
90
|
-
|
|
91
|
-
if (
|
|
92
|
-
|
|
93
|
-
|
|
67
|
+
this.#buffer = concat([this.#buffer, chunk]);
|
|
68
|
+
if (this.#nextResolve !== null && this.#buffer.length >= this.#nextResolve[0]) {
|
|
69
|
+
this.#nextResolve[1].resolve();
|
|
70
|
+
this.#nextResolve = null;
|
|
94
71
|
}
|
|
95
72
|
}
|
|
96
|
-
|
|
73
|
+
this.#canRead = false;
|
|
97
74
|
break;
|
|
98
75
|
}
|
|
99
76
|
catch (err) {
|
|
100
|
-
|
|
77
|
+
this.#canRead = false;
|
|
101
78
|
this.stateChangeHandler?.(false);
|
|
102
|
-
|
|
79
|
+
this.#rejectRead();
|
|
103
80
|
L.error(err);
|
|
104
81
|
}
|
|
105
82
|
} while (this.isConnected);
|
|
106
83
|
this.stateChangeHandler?.(false);
|
|
107
84
|
});
|
|
108
|
-
|
|
85
|
+
this.#connection = connection;
|
|
86
|
+
}
|
|
87
|
+
#rejectRead() {
|
|
88
|
+
if (this.#nextResolve !== null) {
|
|
89
|
+
this.#nextResolve[1].reject(new ConnectionError("The connection was closed."));
|
|
90
|
+
this.#nextResolve = null;
|
|
91
|
+
}
|
|
109
92
|
}
|
|
110
93
|
async read(p) {
|
|
111
|
-
|
|
112
|
-
const unlock = await
|
|
94
|
+
this.#assertConnected();
|
|
95
|
+
const unlock = await this.#rMutex.lock();
|
|
113
96
|
try {
|
|
114
|
-
|
|
115
|
-
if (
|
|
116
|
-
await new Promise((resolve, reject) =>
|
|
97
|
+
this.#assertConnected();
|
|
98
|
+
if (this.#buffer.length < p.length) {
|
|
99
|
+
await new Promise((resolve, reject) => this.#nextResolve = [p.length, { resolve, reject }]);
|
|
117
100
|
}
|
|
118
|
-
const slice =
|
|
101
|
+
const slice = this.#buffer.slice(0, p.length);
|
|
119
102
|
p.set(slice);
|
|
120
|
-
|
|
103
|
+
this.#buffer = this.#buffer.slice(slice.length);
|
|
121
104
|
}
|
|
122
105
|
finally {
|
|
123
106
|
unlock();
|
|
124
107
|
}
|
|
125
108
|
}
|
|
126
109
|
async write(p) {
|
|
127
|
-
|
|
128
|
-
const unlock = await
|
|
110
|
+
this.#assertConnected();
|
|
111
|
+
const unlock = await this.#wMutex.lock();
|
|
129
112
|
try {
|
|
130
|
-
|
|
113
|
+
this.#assertConnected();
|
|
131
114
|
let written = 0;
|
|
132
115
|
while (written < p.length) {
|
|
133
116
|
try {
|
|
134
|
-
const wrote = await
|
|
117
|
+
const wrote = await this.#connection.write(p.subarray(written));
|
|
135
118
|
this.callback?.write(wrote);
|
|
136
119
|
written += wrote;
|
|
137
120
|
}
|
|
138
121
|
catch (err) {
|
|
139
122
|
if (err instanceof Deno.errors.BrokenPipe || err instanceof Deno.errors.ConnectionReset) {
|
|
140
|
-
|
|
123
|
+
this.#canWrite = false;
|
|
141
124
|
}
|
|
142
125
|
if (!this.isConnected) {
|
|
143
126
|
this.stateChangeHandler?.(false);
|
|
@@ -154,20 +137,10 @@ export class ConnectionTCP {
|
|
|
154
137
|
}
|
|
155
138
|
}
|
|
156
139
|
close() {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
140
|
+
this.#assertConnected();
|
|
141
|
+
this.#connection.close();
|
|
142
|
+
this.#canRead = this.#canWrite = false;
|
|
143
|
+
this.#connection = undefined;
|
|
144
|
+
this.#rejectRead();
|
|
162
145
|
}
|
|
163
146
|
}
|
|
164
|
-
_ConnectionTCP_hostname = new WeakMap(), _ConnectionTCP_port = new WeakMap(), _ConnectionTCP_connection = new WeakMap(), _ConnectionTCP_rMutex = new WeakMap(), _ConnectionTCP_wMutex = new WeakMap(), _ConnectionTCP_buffer = new WeakMap(), _ConnectionTCP_nextResolve = new WeakMap(), _ConnectionTCP_canRead = new WeakMap(), _ConnectionTCP_canWrite = new WeakMap(), _ConnectionTCP_instances = new WeakSet(), _ConnectionTCP_assertConnected = function _ConnectionTCP_assertConnected() {
|
|
165
|
-
if (!this.isConnected) {
|
|
166
|
-
throw new ConnectionError("The connection is not open.");
|
|
167
|
-
}
|
|
168
|
-
}, _ConnectionTCP_rejectRead = function _ConnectionTCP_rejectRead() {
|
|
169
|
-
if (__classPrivateFieldGet(this, _ConnectionTCP_nextResolve, "f") !== null) {
|
|
170
|
-
__classPrivateFieldGet(this, _ConnectionTCP_nextResolve, "f")[1].reject(new ConnectionError("The connection was closed."));
|
|
171
|
-
__classPrivateFieldSet(this, _ConnectionTCP_nextResolve, null, "f");
|
|
172
|
-
}
|
|
173
|
-
};
|
|
@@ -17,132 +17,116 @@
|
|
|
17
17
|
* You should have received a copy of the GNU Lesser General Public License
|
|
18
18
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
19
19
|
*/
|
|
20
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
21
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
22
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
23
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
24
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
25
|
-
};
|
|
26
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
27
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
28
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
29
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
30
|
-
};
|
|
31
|
-
var _ConnectionWebSocket_instances, _ConnectionWebSocket_url, _ConnectionWebSocket_webSocket, _ConnectionWebSocket_rMutex, _ConnectionWebSocket_wMutex, _ConnectionWebSocket_buffer, _ConnectionWebSocket_nextResolve, _ConnectionWebSocket_initWs, _ConnectionWebSocket_isConnecting, _ConnectionWebSocket_assertConnected, _ConnectionWebSocket_rejectRead;
|
|
32
20
|
import { concat, unreachable } from "../0_deps.js";
|
|
33
21
|
import { ConnectionError } from "../0_errors.js";
|
|
34
22
|
import { getLogger, Mutex } from "../1_utilities.js";
|
|
35
23
|
const L = getLogger("ConnectionWebSocket");
|
|
36
24
|
const errConnectionNotOpen = new ConnectionError("The connection is not open.");
|
|
37
25
|
export class ConnectionWebSocket {
|
|
26
|
+
#url;
|
|
27
|
+
#webSocket;
|
|
28
|
+
#rMutex = new Mutex();
|
|
29
|
+
#wMutex = new Mutex();
|
|
30
|
+
#buffer = new Uint8Array();
|
|
31
|
+
#nextResolve = null;
|
|
32
|
+
stateChangeHandler;
|
|
38
33
|
constructor(url) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
34
|
+
this.#url = url;
|
|
35
|
+
}
|
|
36
|
+
#initWs() {
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
const webSocket = new WebSocket(this.#url, "binary");
|
|
39
|
+
const mutex = new Mutex();
|
|
40
|
+
webSocket.addEventListener("close", () => {
|
|
41
|
+
this.#rejectRead();
|
|
42
|
+
this.stateChangeHandler?.(false);
|
|
43
|
+
});
|
|
44
|
+
webSocket.addEventListener("open", () => {
|
|
45
|
+
this.stateChangeHandler?.(true);
|
|
46
|
+
resolve(webSocket);
|
|
47
|
+
L.debug("connected to", this.#url);
|
|
48
|
+
});
|
|
49
|
+
webSocket.addEventListener("message", async (e) => {
|
|
50
|
+
if (typeof e.data === "string") {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const unlock = await mutex.lock();
|
|
54
|
+
const data = new Uint8Array(await new Blob([e.data].map((v) => v instanceof Blob || v instanceof Uint8Array ? v : v instanceof ArrayBuffer ? v : unreachable())).arrayBuffer());
|
|
55
|
+
this.#buffer = concat([this.#buffer, data]);
|
|
56
|
+
if (this.#nextResolve !== null && this.#buffer.length >= this.#nextResolve[0]) {
|
|
57
|
+
this.#nextResolve[1].resolve();
|
|
58
|
+
this.#nextResolve = null;
|
|
59
|
+
}
|
|
60
|
+
unlock();
|
|
61
|
+
});
|
|
62
|
+
webSocket.addEventListener("error", (err) => {
|
|
63
|
+
if (this.#isConnecting) {
|
|
64
|
+
reject("message" in err ? new ConnectionError(err.message) : new ConnectionError("Failed to connect."));
|
|
65
|
+
}
|
|
66
|
+
if (this.isConnected) {
|
|
67
|
+
L.error(err);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
51
70
|
});
|
|
52
|
-
_ConnectionWebSocket_isConnecting.set(this, false);
|
|
53
|
-
__classPrivateFieldSet(this, _ConnectionWebSocket_url, url, "f");
|
|
54
71
|
}
|
|
55
72
|
get isConnected() {
|
|
56
|
-
return !!
|
|
73
|
+
return !!this.#webSocket && this.#webSocket.readyState === WebSocket.OPEN;
|
|
57
74
|
}
|
|
75
|
+
#isConnecting = false;
|
|
58
76
|
async open() {
|
|
59
|
-
if (
|
|
77
|
+
if (this.#isConnecting) {
|
|
60
78
|
return;
|
|
61
79
|
}
|
|
62
|
-
|
|
80
|
+
this.#isConnecting = true;
|
|
63
81
|
try {
|
|
64
|
-
|
|
82
|
+
this.#webSocket = await this.#initWs();
|
|
65
83
|
}
|
|
66
84
|
finally {
|
|
67
|
-
|
|
85
|
+
this.#isConnecting = false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
#assertConnected() {
|
|
89
|
+
if (!this.isConnected) {
|
|
90
|
+
throw errConnectionNotOpen;
|
|
68
91
|
}
|
|
69
92
|
}
|
|
70
93
|
async read(p) {
|
|
71
|
-
|
|
72
|
-
const unlock = await
|
|
94
|
+
this.#assertConnected();
|
|
95
|
+
const unlock = await this.#rMutex.lock();
|
|
73
96
|
try {
|
|
74
|
-
|
|
75
|
-
if (
|
|
76
|
-
await new Promise((resolve, reject) =>
|
|
97
|
+
this.#assertConnected();
|
|
98
|
+
if (this.#buffer.length < p.length) {
|
|
99
|
+
await new Promise((resolve, reject) => this.#nextResolve = [p.length, { resolve, reject }]);
|
|
77
100
|
}
|
|
78
|
-
const slice =
|
|
101
|
+
const slice = this.#buffer.slice(0, p.length);
|
|
79
102
|
p.set(slice);
|
|
80
|
-
|
|
103
|
+
this.#buffer = this.#buffer.slice(slice.length);
|
|
81
104
|
}
|
|
82
105
|
finally {
|
|
83
106
|
unlock();
|
|
84
107
|
}
|
|
85
108
|
}
|
|
86
109
|
async write(p) {
|
|
87
|
-
|
|
88
|
-
const unlock = await
|
|
110
|
+
this.#assertConnected();
|
|
111
|
+
const unlock = await this.#wMutex.lock();
|
|
89
112
|
try {
|
|
90
|
-
|
|
91
|
-
|
|
113
|
+
this.#assertConnected();
|
|
114
|
+
this.#webSocket.send(p);
|
|
92
115
|
}
|
|
93
116
|
finally {
|
|
94
117
|
unlock();
|
|
95
118
|
}
|
|
96
119
|
}
|
|
120
|
+
#rejectRead() {
|
|
121
|
+
if (this.#nextResolve !== null) {
|
|
122
|
+
this.#nextResolve[1].reject(errConnectionNotOpen);
|
|
123
|
+
this.#nextResolve = null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
97
126
|
close() {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
127
|
+
this.#assertConnected();
|
|
128
|
+
this.#webSocket.close(1000, "method");
|
|
129
|
+
this.#webSocket = undefined;
|
|
130
|
+
this.#rejectRead();
|
|
102
131
|
}
|
|
103
132
|
}
|
|
104
|
-
_ConnectionWebSocket_url = new WeakMap(), _ConnectionWebSocket_webSocket = new WeakMap(), _ConnectionWebSocket_rMutex = new WeakMap(), _ConnectionWebSocket_wMutex = new WeakMap(), _ConnectionWebSocket_buffer = new WeakMap(), _ConnectionWebSocket_nextResolve = new WeakMap(), _ConnectionWebSocket_isConnecting = new WeakMap(), _ConnectionWebSocket_instances = new WeakSet(), _ConnectionWebSocket_initWs = function _ConnectionWebSocket_initWs() {
|
|
105
|
-
return new Promise((resolve, reject) => {
|
|
106
|
-
const webSocket = new WebSocket(__classPrivateFieldGet(this, _ConnectionWebSocket_url, "f"), "binary");
|
|
107
|
-
const mutex = new Mutex();
|
|
108
|
-
webSocket.addEventListener("close", () => {
|
|
109
|
-
__classPrivateFieldGet(this, _ConnectionWebSocket_instances, "m", _ConnectionWebSocket_rejectRead).call(this);
|
|
110
|
-
this.stateChangeHandler?.(false);
|
|
111
|
-
});
|
|
112
|
-
webSocket.addEventListener("open", () => {
|
|
113
|
-
this.stateChangeHandler?.(true);
|
|
114
|
-
resolve(webSocket);
|
|
115
|
-
L.debug("connected to", __classPrivateFieldGet(this, _ConnectionWebSocket_url, "f"));
|
|
116
|
-
});
|
|
117
|
-
webSocket.addEventListener("message", async (e) => {
|
|
118
|
-
if (typeof e.data === "string") {
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
const unlock = await mutex.lock();
|
|
122
|
-
const data = new Uint8Array(await new Blob([e.data].map((v) => v instanceof Blob || v instanceof Uint8Array ? v : v instanceof ArrayBuffer ? v : unreachable())).arrayBuffer());
|
|
123
|
-
__classPrivateFieldSet(this, _ConnectionWebSocket_buffer, concat([__classPrivateFieldGet(this, _ConnectionWebSocket_buffer, "f"), data]), "f");
|
|
124
|
-
if (__classPrivateFieldGet(this, _ConnectionWebSocket_nextResolve, "f") !== null && __classPrivateFieldGet(this, _ConnectionWebSocket_buffer, "f").length >= __classPrivateFieldGet(this, _ConnectionWebSocket_nextResolve, "f")[0]) {
|
|
125
|
-
__classPrivateFieldGet(this, _ConnectionWebSocket_nextResolve, "f")[1].resolve();
|
|
126
|
-
__classPrivateFieldSet(this, _ConnectionWebSocket_nextResolve, null, "f");
|
|
127
|
-
}
|
|
128
|
-
unlock();
|
|
129
|
-
});
|
|
130
|
-
webSocket.addEventListener("error", (err) => {
|
|
131
|
-
if (__classPrivateFieldGet(this, _ConnectionWebSocket_isConnecting, "f")) {
|
|
132
|
-
reject("message" in err ? new ConnectionError(err.message) : new ConnectionError("Failed to connect."));
|
|
133
|
-
}
|
|
134
|
-
if (this.isConnected) {
|
|
135
|
-
L.error(err);
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
}, _ConnectionWebSocket_assertConnected = function _ConnectionWebSocket_assertConnected() {
|
|
140
|
-
if (!this.isConnected) {
|
|
141
|
-
throw errConnectionNotOpen;
|
|
142
|
-
}
|
|
143
|
-
}, _ConnectionWebSocket_rejectRead = function _ConnectionWebSocket_rejectRead() {
|
|
144
|
-
if (__classPrivateFieldGet(this, _ConnectionWebSocket_nextResolve, "f") !== null) {
|
|
145
|
-
__classPrivateFieldGet(this, _ConnectionWebSocket_nextResolve, "f")[1].reject(errConnectionNotOpen);
|
|
146
|
-
__classPrivateFieldSet(this, _ConnectionWebSocket_nextResolve, null, "f");
|
|
147
|
-
}
|
|
148
|
-
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
var Module = (() => {
|
|
2
|
-
var _scriptName =
|
|
2
|
+
var _scriptName = import.meta.url;
|
|
3
3
|
return (function (moduleArg = {}) {
|
|
4
4
|
var moduleRtn;
|
|
5
5
|
var Module = moduleArg;
|
|
@@ -117,16 +117,8 @@ var Module = (() => {
|
|
|
117
117
|
}
|
|
118
118
|
} wasmBinaryFile ??= findWasmBinary(); instantiateAsync(wasmBinary, wasmBinaryFile, info, receiveInstantiationResult).catch(readyPromiseReject); return {}; }
|
|
119
119
|
class ExitStatus {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
enumerable: true,
|
|
123
|
-
configurable: true,
|
|
124
|
-
writable: true,
|
|
125
|
-
value: "ExitStatus"
|
|
126
|
-
});
|
|
127
|
-
this.message = `Program terminated with exit(${status})`;
|
|
128
|
-
this.status = status;
|
|
129
|
-
}
|
|
120
|
+
name = "ExitStatus";
|
|
121
|
+
constructor(status) { this.message = `Program terminated with exit(${status})`; this.status = status; }
|
|
130
122
|
}
|
|
131
123
|
var callRuntimeCallbacks = callbacks => { while (callbacks.length > 0) {
|
|
132
124
|
callbacks.shift()(Module);
|