@mtkruto/node 0.79.6 → 0.80.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/esm/5_client.d.ts +1 -1
- package/esm/5_client.js +1 -1
- package/esm/client/0_utilities.d.ts +1 -0
- package/esm/client/0_utilities.d.ts.map +1 -1
- package/esm/client/0_worker_error.d.ts +26 -0
- package/esm/client/0_worker_error.d.ts.map +1 -0
- package/esm/client/0_worker_error.js +83 -0
- package/esm/client/0_worker_request.d.ts +3 -2
- package/esm/client/0_worker_request.d.ts.map +1 -1
- package/esm/client/1_invoke_middleware.d.ts +32 -0
- package/esm/client/1_invoke_middleware.d.ts.map +1 -0
- package/esm/client/1_invoke_middleware.js +22 -0
- package/esm/client/{0_worker_response.d.ts → 1_worker_response.d.ts} +5 -7
- package/esm/client/1_worker_response.d.ts.map +1 -0
- package/esm/client/3_message_manager.d.ts.map +1 -1
- package/esm/client/3_message_manager.js +35 -1
- package/esm/client/4_composer.d.ts +1 -1
- package/esm/client/4_composer.d.ts.map +1 -1
- package/esm/client/5_client.d.ts +4 -12
- package/esm/client/5_client.d.ts.map +1 -1
- package/esm/client/5_client.js +3 -5
- package/esm/client/5_client_dispatcher.d.ts +47 -53
- package/esm/client/5_client_dispatcher.d.ts.map +1 -1
- package/esm/client/5_client_dispatcher.js +60 -39
- package/esm/client/6_client_worker.d.ts +1 -1
- package/esm/client/6_client_worker.d.ts.map +1 -1
- package/esm/client/6_client_worker.js +30 -10
- package/package.json +1 -1
- package/script/5_client.d.ts +1 -1
- package/script/5_client.js +1 -1
- package/script/client/0_utilities.d.ts +1 -0
- package/script/client/0_utilities.d.ts.map +1 -1
- package/script/client/0_worker_error.d.ts +26 -0
- package/script/client/0_worker_error.d.ts.map +1 -0
- package/script/client/0_worker_error.js +87 -0
- package/script/client/0_worker_request.d.ts +3 -2
- package/script/client/0_worker_request.d.ts.map +1 -1
- package/script/client/1_invoke_middleware.d.ts +32 -0
- package/script/client/1_invoke_middleware.d.ts.map +1 -0
- package/script/client/1_invoke_middleware.js +25 -0
- package/script/client/{0_worker_response.d.ts → 1_worker_response.d.ts} +5 -7
- package/script/client/1_worker_response.d.ts.map +1 -0
- package/script/client/3_message_manager.d.ts.map +1 -1
- package/script/client/3_message_manager.js +35 -1
- package/script/client/4_composer.d.ts +1 -1
- package/script/client/4_composer.d.ts.map +1 -1
- package/script/client/5_client.d.ts +4 -12
- package/script/client/5_client.d.ts.map +1 -1
- package/script/client/5_client.js +4 -6
- package/script/client/5_client_dispatcher.d.ts +47 -53
- package/script/client/5_client_dispatcher.d.ts.map +1 -1
- package/script/client/5_client_dispatcher.js +79 -58
- package/script/client/6_client_worker.d.ts +1 -1
- package/script/client/6_client_worker.d.ts.map +1 -1
- package/script/client/6_client_worker.js +30 -10
- package/esm/client/0_worker_response.d.ts.map +0 -1
- package/script/client/0_worker_response.d.ts.map +0 -1
- /package/esm/client/{0_worker_response.js → 1_worker_response.js} +0 -0
- /package/script/client/{0_worker_response.js → 1_worker_response.js} +0 -0
|
@@ -1,23 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* MTKruto - Cross-runtime JavaScript library for building Telegram clients
|
|
4
|
-
* Copyright (C) 2023-2026 Roj <https://roj.im/>
|
|
5
|
-
*
|
|
6
|
-
* This file is part of MTKruto.
|
|
7
|
-
*
|
|
8
|
-
* This program is free software: you can redistribute it and/or modify
|
|
9
|
-
* it under the terms of the GNU Lesser General Public License as published by
|
|
10
|
-
* the Free Software Foundation, either version 3 of the License, or
|
|
11
|
-
* (at your option) any later version.
|
|
12
|
-
*
|
|
13
|
-
* This program is distributed in the hope that it will be useful,
|
|
14
|
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
-
* GNU Lesser General Public License for more details.
|
|
17
|
-
*
|
|
18
|
-
* You should have received a copy of the GNU Lesser General Public License
|
|
19
|
-
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
20
|
-
*/
|
|
21
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
22
3
|
if (k2 === undefined) k2 = k;
|
|
23
4
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -53,9 +34,30 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
53
34
|
})();
|
|
54
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
55
36
|
exports.ClientDispatcher = void 0;
|
|
37
|
+
/**
|
|
38
|
+
* MTKruto - Cross-runtime JavaScript library for building Telegram clients
|
|
39
|
+
* Copyright (C) 2023-2026 Roj <https://roj.im/>
|
|
40
|
+
*
|
|
41
|
+
* This file is part of MTKruto.
|
|
42
|
+
*
|
|
43
|
+
* This program is free software: you can redistribute it and/or modify
|
|
44
|
+
* it under the terms of the GNU Lesser General Public License as published by
|
|
45
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
46
|
+
* (at your option) any later version.
|
|
47
|
+
*
|
|
48
|
+
* This program is distributed in the hope that it will be useful,
|
|
49
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
50
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
51
|
+
* GNU Lesser General Public License for more details.
|
|
52
|
+
*
|
|
53
|
+
* You should have received a copy of the GNU Lesser General Public License
|
|
54
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
55
|
+
*/
|
|
56
|
+
const dntShim = __importStar(require("../_dnt.shims.js"));
|
|
56
57
|
const _1_utilities_js_1 = require("../1_utilities.js");
|
|
57
58
|
const _4_constants_js_1 = require("../4_constants.js");
|
|
58
|
-
const
|
|
59
|
+
const _0_worker_error_js_1 = require("./0_worker_error.js");
|
|
60
|
+
const _1_invoke_middleware_js_1 = require("./1_invoke_middleware.js");
|
|
59
61
|
const _2_sign_in_js_1 = require("./2_sign_in.js");
|
|
60
62
|
const _4_composer_js_1 = require("./4_composer.js");
|
|
61
63
|
class ClientDispatcher extends _4_composer_js_1.Composer {
|
|
@@ -64,12 +66,12 @@ class ClientDispatcher extends _4_composer_js_1.Composer {
|
|
|
64
66
|
#L;
|
|
65
67
|
#LsignIn;
|
|
66
68
|
// deno-lint-ignore no-explicit-any
|
|
67
|
-
#pendingRequests = new
|
|
69
|
+
#pendingRequests = new Map();
|
|
68
70
|
constructor(worker, id) {
|
|
69
71
|
super();
|
|
70
72
|
this.#worker = worker;
|
|
71
73
|
this.#id = id;
|
|
72
|
-
this.#L = (0, _1_utilities_js_1.getLogger)("
|
|
74
|
+
this.#L = (0, _1_utilities_js_1.getLogger)("ClientDispatcher").branch(this.#id);
|
|
73
75
|
this.#LsignIn = this.#L.branch("signIn");
|
|
74
76
|
}
|
|
75
77
|
/** @internal */
|
|
@@ -79,10 +81,11 @@ class ClientDispatcher extends _4_composer_js_1.Composer {
|
|
|
79
81
|
}
|
|
80
82
|
this.#L.debug("handling response message", response);
|
|
81
83
|
if (response.isError) {
|
|
82
|
-
this.#pendingRequests
|
|
84
|
+
this.#pendingRequests.get(response.id)?.reject((0, _0_worker_error_js_1.deserializeWorkerError)(response.data));
|
|
85
|
+
this.#pendingRequests.delete(response.id);
|
|
83
86
|
}
|
|
84
87
|
else {
|
|
85
|
-
if (response.id ===
|
|
88
|
+
if (response.id === "") {
|
|
86
89
|
try {
|
|
87
90
|
await this.handleUpdate(this, response.data);
|
|
88
91
|
}
|
|
@@ -91,38 +94,10 @@ class ClientDispatcher extends _4_composer_js_1.Composer {
|
|
|
91
94
|
}
|
|
92
95
|
}
|
|
93
96
|
else {
|
|
94
|
-
this.#pendingRequests
|
|
97
|
+
this.#pendingRequests.get(response.id)?.resolve(response.data);
|
|
98
|
+
this.#pendingRequests.delete(response.id);
|
|
95
99
|
}
|
|
96
100
|
}
|
|
97
|
-
// clean up pending requests
|
|
98
|
-
if (response.id === 0) {
|
|
99
|
-
this.#pendingRequests.shift();
|
|
100
|
-
}
|
|
101
|
-
else if (response.id === this.#pendingRequests.length - 1) {
|
|
102
|
-
this.#pendingRequests.pop();
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
#constructError(error) {
|
|
106
|
-
switch (error.name) {
|
|
107
|
-
case "TelegramError":
|
|
108
|
-
return errors.constructTelegramError({
|
|
109
|
-
_: "rpc_error",
|
|
110
|
-
error_code: error.args[0].error_code,
|
|
111
|
-
error_message: error.args[0].error_message,
|
|
112
|
-
}, error.args.call);
|
|
113
|
-
case "ConnectionError":
|
|
114
|
-
return new errors.ConnectionError(error.args[0]);
|
|
115
|
-
case "AccessError":
|
|
116
|
-
return new errors.AccessError(error.args[0]);
|
|
117
|
-
case "InputError":
|
|
118
|
-
return new errors.InputError(error.args[0]);
|
|
119
|
-
case "TransportError":
|
|
120
|
-
return new errors.TransportError(error.args[0]);
|
|
121
|
-
case "TLError":
|
|
122
|
-
return new errors.TLError(error.args[0], error.args[1]);
|
|
123
|
-
default:
|
|
124
|
-
return new TypeError("Unknown error");
|
|
125
|
-
}
|
|
126
101
|
}
|
|
127
102
|
get id() {
|
|
128
103
|
return this.#id;
|
|
@@ -130,10 +105,12 @@ class ClientDispatcher extends _4_composer_js_1.Composer {
|
|
|
130
105
|
async #dispatch(method, ...args) {
|
|
131
106
|
// deno-lint-ignore no-explicit-any
|
|
132
107
|
const promiseWithResolvers = Promise.withResolvers();
|
|
133
|
-
const
|
|
108
|
+
const id = dntShim.crypto.randomUUID();
|
|
109
|
+
this.#pendingRequests.set(id, promiseWithResolvers);
|
|
134
110
|
const request = {
|
|
111
|
+
type: "request",
|
|
135
112
|
clientId: this.#id,
|
|
136
|
-
id
|
|
113
|
+
id,
|
|
137
114
|
method,
|
|
138
115
|
args,
|
|
139
116
|
};
|
|
@@ -159,9 +136,53 @@ class ClientDispatcher extends _4_composer_js_1.Composer {
|
|
|
159
136
|
await this.connect();
|
|
160
137
|
await this.signIn(params);
|
|
161
138
|
}
|
|
162
|
-
async invoke(function_, params) {
|
|
163
|
-
|
|
139
|
+
async #invoke(function_, params) {
|
|
140
|
+
let n = 1;
|
|
141
|
+
while (true) {
|
|
142
|
+
try {
|
|
143
|
+
await this.#dispatch("invoke", function_, params);
|
|
144
|
+
}
|
|
145
|
+
catch (err) {
|
|
146
|
+
if (await this.#handleInvokeError(Object.freeze({ client: this, error: err, function: function_, n: n++ }), () => Promise.resolve(false))) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
throw err;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
164
154
|
}
|
|
155
|
+
#handleInvokeError = (0, _1_invoke_middleware_js_1.skipInvoke)();
|
|
156
|
+
/** @internal */
|
|
157
|
+
async handleInvokeError(request) {
|
|
158
|
+
const args = request.args[0];
|
|
159
|
+
return await this.#handleInvokeError({
|
|
160
|
+
client: this,
|
|
161
|
+
error: (0, _0_worker_error_js_1.deserializeWorkerError)(args.error),
|
|
162
|
+
function: args.function,
|
|
163
|
+
n: args.n,
|
|
164
|
+
}, () => Promise.resolve(false));
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Invokes a function waiting and returning its reply.
|
|
168
|
+
* Requires the client to be connected.
|
|
169
|
+
*
|
|
170
|
+
* @param function_ The function to invoke.
|
|
171
|
+
*/
|
|
172
|
+
invoke = Object.assign(this.#invoke, {
|
|
173
|
+
use: (handler) => {
|
|
174
|
+
const handle = this.#handleInvokeError;
|
|
175
|
+
this.#handleInvokeError = async (ctx, next) => {
|
|
176
|
+
let result = null;
|
|
177
|
+
return await handle(ctx, async () => {
|
|
178
|
+
if (result !== null)
|
|
179
|
+
return result;
|
|
180
|
+
result = await handler(ctx, next);
|
|
181
|
+
return result;
|
|
182
|
+
});
|
|
183
|
+
};
|
|
184
|
+
},
|
|
185
|
+
});
|
|
165
186
|
/**
|
|
166
187
|
* Send a user verification code.
|
|
167
188
|
*
|
|
@@ -23,6 +23,6 @@ export declare class ClientWorker {
|
|
|
23
23
|
constructor(specifier: Worker);
|
|
24
24
|
constructor(specifier: string | URL, options?: WorkerOptions);
|
|
25
25
|
terminate(): void;
|
|
26
|
-
createClient(params?: ClientDispatcherParams): Promise<ClientDispatcher>;
|
|
26
|
+
createClient(id: string, params?: ClientDispatcherParams): Promise<ClientDispatcher>;
|
|
27
27
|
}
|
|
28
28
|
//# sourceMappingURL=6_client_worker.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"6_client_worker.d.ts","sourceRoot":"","sources":["../../src/client/6_client_worker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;
|
|
1
|
+
{"version":3,"file":"6_client_worker.d.ts","sourceRoot":"","sources":["../../src/client/6_client_worker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAMH,OAAO,EAAE,gBAAgB,EAAE,KAAK,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAEzF,qBAAa,YAAY;;gBAKX,SAAS,EAAE,MAAM;gBACjB,SAAS,EAAE,MAAM,GAAG,GAAG,EAAE,OAAO,CAAC,EAAE,aAAa;IA4B5D,SAAS;IAIH,YAAY,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,sBAAsB,GAAG,OAAO,CAAC,gBAAgB,CAAC;CAe3F"}
|
|
@@ -20,33 +20,53 @@
|
|
|
20
20
|
*/
|
|
21
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
22
|
exports.ClientWorker = void 0;
|
|
23
|
+
const _0_errors_js_1 = require("../0_errors.js");
|
|
23
24
|
const _1_logger_js_1 = require("../utilities/1_logger.js");
|
|
24
25
|
const _5_client_dispatcher_js_1 = require("./5_client_dispatcher.js");
|
|
25
26
|
class ClientWorker {
|
|
26
27
|
#worker;
|
|
27
|
-
#
|
|
28
|
-
#clients = new Array();
|
|
28
|
+
#clients = new Map();
|
|
29
29
|
#L = (0, _1_logger_js_1.getLogger)("ClientWorker");
|
|
30
30
|
constructor(specifier, options) {
|
|
31
31
|
this.#worker = specifier instanceof Worker ? specifier : new Worker(specifier, options);
|
|
32
|
-
this.#worker.addEventListener("message", (e) => {
|
|
32
|
+
this.#worker.addEventListener("message", async (e) => {
|
|
33
33
|
this.#L.debug("received message from worker", e.data);
|
|
34
|
-
const
|
|
35
|
-
|
|
34
|
+
const message = e.data;
|
|
35
|
+
if (message.type === "response") {
|
|
36
|
+
this.#clients.get(message.clientId)?.handleResponse(message);
|
|
37
|
+
}
|
|
38
|
+
else if (message.type === "request") {
|
|
39
|
+
if (message.method === "handleInvokeError") {
|
|
40
|
+
const client = this.#clients.get(message.clientId);
|
|
41
|
+
if (client) {
|
|
42
|
+
const result = await client.handleInvokeError(message);
|
|
43
|
+
const response = {
|
|
44
|
+
type: "response",
|
|
45
|
+
clientId: message.clientId,
|
|
46
|
+
id: message.id,
|
|
47
|
+
isError: false,
|
|
48
|
+
data: result,
|
|
49
|
+
};
|
|
50
|
+
this.#worker.postMessage(response);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
36
54
|
});
|
|
37
55
|
}
|
|
38
56
|
terminate() {
|
|
39
57
|
this.#worker.terminate();
|
|
40
58
|
}
|
|
41
|
-
async createClient(params) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
59
|
+
async createClient(id, params) {
|
|
60
|
+
if (this.#clients.has(id)) {
|
|
61
|
+
throw new _0_errors_js_1.InputError("Client already created");
|
|
62
|
+
}
|
|
63
|
+
const client = new _5_client_dispatcher_js_1.ClientDispatcher(this.#worker, id);
|
|
64
|
+
this.#clients.set(id, client);
|
|
45
65
|
try {
|
|
46
66
|
await client.init(params);
|
|
47
67
|
}
|
|
48
68
|
catch (err) {
|
|
49
|
-
this.#clients
|
|
69
|
+
this.#clients.delete(id);
|
|
50
70
|
throw err;
|
|
51
71
|
}
|
|
52
72
|
return client;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"0_worker_response.d.ts","sourceRoot":"","sources":["../../src/client/0_worker_response.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,eAAe,GAAG,iBAAiB,GAAG,aAAa,GAAG,YAAY,GAAG,gBAAgB,GAAG,SAAS,CAAC;IAExG,IAAI,EAAE,GAAG,CAAC;CACX;AAED,MAAM,CAAC,OAAO,WAAW,cAAc,CAAC;IACtC,UAAiB,IAAI;QACnB,QAAQ,EAAE,MAAM,CAAC;QACjB,EAAE,EAAE,MAAM,CAAC;KACZ;IAED,UAAiB,KAAM,SAAQ,IAAI;QACjC,OAAO,EAAE,IAAI,CAAC;QACd,IAAI,EAAE,WAAW,CAAC;KACnB;IAED,UAAiB,IAAK,SAAQ,IAAI;QAChC,OAAO,EAAE,KAAK,CAAC;QACf,IAAI,EAAE,OAAO,CAAC;KACf;CACF;AAED,MAAM,MAAM,cAAc,GAAG,cAAc,CAAC,KAAK,GAAG,cAAc,CAAC,IAAI,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"0_worker_response.d.ts","sourceRoot":"","sources":["../../src/client/0_worker_response.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,eAAe,GAAG,iBAAiB,GAAG,aAAa,GAAG,YAAY,GAAG,gBAAgB,GAAG,SAAS,CAAC;IAExG,IAAI,EAAE,GAAG,CAAC;CACX;AAED,MAAM,CAAC,OAAO,WAAW,cAAc,CAAC;IACtC,UAAiB,IAAI;QACnB,QAAQ,EAAE,MAAM,CAAC;QACjB,EAAE,EAAE,MAAM,CAAC;KACZ;IAED,UAAiB,KAAM,SAAQ,IAAI;QACjC,OAAO,EAAE,IAAI,CAAC;QACd,IAAI,EAAE,WAAW,CAAC;KACnB;IAED,UAAiB,IAAK,SAAQ,IAAI;QAChC,OAAO,EAAE,KAAK,CAAC;QACf,IAAI,EAAE,OAAO,CAAC;KACf;CACF;AAED,MAAM,MAAM,cAAc,GAAG,cAAc,CAAC,KAAK,GAAG,cAAc,CAAC,IAAI,CAAC"}
|
|
File without changes
|
|
File without changes
|