@mtkruto/node 0.0.986 → 0.0.988
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/client/0_utilities.d.ts +2 -0
- package/esm/client/0_utilities.js +22 -0
- package/esm/client/1_client_abstract.d.ts +15 -10
- package/esm/client/1_client_abstract.js +26 -27
- package/esm/client/2_client_plain.d.ts +9 -3
- package/esm/client/2_client_plain.js +9 -5
- package/esm/client/3_client.d.ts +28 -19
- package/esm/client/3_client.js +314 -291
- package/esm/connection/0_connection.d.ts +1 -0
- package/esm/connection/0_connection.js +8 -0
- package/esm/connection/1_connection_web_socket.d.ts +3 -1
- package/esm/connection/1_connection_web_socket.js +28 -9
- package/esm/constants.d.ts +1 -1
- package/esm/constants.js +1 -1
- package/esm/deps.js +1 -1
- package/esm/transport/2_transport_provider.d.ts +7 -14
- package/esm/transport/2_transport_provider.js +9 -12
- package/esm/utilities/0_queue.d.ts +6 -0
- package/esm/utilities/0_queue.js +38 -0
- package/package.json +1 -1
- package/script/client/0_utilities.d.ts +2 -0
- package/script/client/0_utilities.js +25 -1
- package/script/client/1_client_abstract.d.ts +15 -10
- package/script/client/1_client_abstract.js +26 -27
- package/script/client/2_client_plain.d.ts +9 -3
- package/script/client/2_client_plain.js +9 -5
- package/script/client/3_client.d.ts +28 -19
- package/script/client/3_client.js +313 -290
- package/script/connection/0_connection.d.ts +1 -0
- package/script/connection/0_connection.js +8 -0
- package/script/connection/1_connection_web_socket.d.ts +3 -1
- package/script/connection/1_connection_web_socket.js +28 -9
- package/script/constants.d.ts +1 -1
- package/script/constants.js +1 -1
- package/script/deps.js +1 -1
- package/script/transport/2_transport_provider.d.ts +7 -14
- package/script/transport/2_transport_provider.js +9 -12
- package/script/utilities/0_queue.d.ts +6 -0
- package/script/utilities/0_queue.js +42 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { MaybePromise } from "../utilities/0_types.js";
|
|
2
2
|
declare abstract class Foundation {
|
|
3
3
|
abstract get connected(): boolean;
|
|
4
|
+
stateChangeHandler?: (connected: boolean) => void;
|
|
4
5
|
abstract open(): MaybePromise<void>;
|
|
5
6
|
abstract write(p: Uint8Array): MaybePromise<void>;
|
|
6
7
|
abstract close(): MaybePromise<void>;
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { ConnectionUnframed } from "./0_connection.js";
|
|
2
2
|
export declare class ConnectionWebSocket extends ConnectionUnframed implements ConnectionUnframed {
|
|
3
|
+
private readonly url;
|
|
3
4
|
private webSocket;
|
|
4
5
|
private rMutex;
|
|
5
6
|
private wMutex;
|
|
6
7
|
private buffer;
|
|
7
8
|
private nextResolve;
|
|
8
9
|
constructor(url: string | URL);
|
|
9
|
-
private
|
|
10
|
+
private initWs;
|
|
10
11
|
get connected(): boolean;
|
|
12
|
+
private wasConnected;
|
|
11
13
|
private isConnecting;
|
|
12
14
|
private connectionError;
|
|
13
15
|
open(): Promise<void>;
|
|
@@ -5,6 +5,12 @@ const d = debug("ConnectionWebSocket");
|
|
|
5
5
|
export class ConnectionWebSocket extends ConnectionUnframed {
|
|
6
6
|
constructor(url) {
|
|
7
7
|
super();
|
|
8
|
+
Object.defineProperty(this, "url", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
configurable: true,
|
|
11
|
+
writable: true,
|
|
12
|
+
value: url
|
|
13
|
+
});
|
|
8
14
|
Object.defineProperty(this, "webSocket", {
|
|
9
15
|
enumerable: true,
|
|
10
16
|
configurable: true,
|
|
@@ -35,6 +41,12 @@ export class ConnectionWebSocket extends ConnectionUnframed {
|
|
|
35
41
|
writable: true,
|
|
36
42
|
value: null
|
|
37
43
|
});
|
|
44
|
+
Object.defineProperty(this, "wasConnected", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
writable: true,
|
|
48
|
+
value: false
|
|
49
|
+
});
|
|
38
50
|
Object.defineProperty(this, "isConnecting", {
|
|
39
51
|
enumerable: true,
|
|
40
52
|
configurable: true,
|
|
@@ -47,16 +59,17 @@ export class ConnectionWebSocket extends ConnectionUnframed {
|
|
|
47
59
|
writable: true,
|
|
48
60
|
value: null
|
|
49
61
|
});
|
|
50
|
-
this.webSocket = this.
|
|
51
|
-
this.webSocket.addEventListener("close", (e) => {
|
|
52
|
-
if (e.code != 1000 && e.reason != "method") {
|
|
53
|
-
this.webSocket = this.reinitWs(url);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
62
|
+
this.webSocket = this.initWs();
|
|
56
63
|
}
|
|
57
|
-
|
|
58
|
-
const webSocket = new dntShim.WebSocket(url, "binary");
|
|
64
|
+
initWs() {
|
|
65
|
+
const webSocket = new dntShim.WebSocket(this.url, "binary");
|
|
59
66
|
const mutex = new Mutex();
|
|
67
|
+
webSocket.addEventListener("close", () => {
|
|
68
|
+
this.stateChangeHandler?.(false);
|
|
69
|
+
});
|
|
70
|
+
webSocket.addEventListener("open", () => {
|
|
71
|
+
this.stateChangeHandler?.(true);
|
|
72
|
+
});
|
|
60
73
|
webSocket.addEventListener("message", async (e) => {
|
|
61
74
|
if (typeof e.data === "string") {
|
|
62
75
|
return;
|
|
@@ -77,7 +90,9 @@ export class ConnectionWebSocket extends ConnectionUnframed {
|
|
|
77
90
|
// @ts-ignore: Node.js
|
|
78
91
|
this.connectionError = err;
|
|
79
92
|
}
|
|
80
|
-
|
|
93
|
+
if (this.connected) {
|
|
94
|
+
d("WebSocket error: %o", err);
|
|
95
|
+
}
|
|
81
96
|
});
|
|
82
97
|
return webSocket;
|
|
83
98
|
}
|
|
@@ -89,6 +104,9 @@ export class ConnectionWebSocket extends ConnectionUnframed {
|
|
|
89
104
|
throw new Error("Already connecting");
|
|
90
105
|
}
|
|
91
106
|
this.isConnecting = true;
|
|
107
|
+
if (!this.connected && this.wasConnected) {
|
|
108
|
+
this.webSocket = this.initWs();
|
|
109
|
+
}
|
|
92
110
|
try {
|
|
93
111
|
while (this.webSocket.readyState != dntShim.WebSocket.OPEN) {
|
|
94
112
|
if (this.webSocket.readyState == dntShim.WebSocket.CLOSED) {
|
|
@@ -103,6 +121,7 @@ export class ConnectionWebSocket extends ConnectionUnframed {
|
|
|
103
121
|
await new Promise((r) => setTimeout(r, 5));
|
|
104
122
|
}
|
|
105
123
|
}
|
|
124
|
+
this.wasConnected = true;
|
|
106
125
|
}
|
|
107
126
|
finally {
|
|
108
127
|
this.isConnecting = false;
|
package/esm/constants.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export declare const PUBLIC_KEYS: PublicKeys;
|
|
|
5
5
|
export declare const VECTOR_CONSTRUCTOR = 481674261;
|
|
6
6
|
export declare const INITIAL_DC: DC;
|
|
7
7
|
export declare const LAYER = 161;
|
|
8
|
-
export declare const APP_VERSION = "MTKruto 0.0.
|
|
8
|
+
export declare const APP_VERSION = "MTKruto 0.0.988";
|
|
9
9
|
export declare const DEVICE_MODEL: string;
|
|
10
10
|
export declare const LANG_CODE: string;
|
|
11
11
|
export declare const LANG_PACK = "";
|
package/esm/constants.js
CHANGED
|
@@ -54,7 +54,7 @@ export const PUBLIC_KEYS = Object.freeze([
|
|
|
54
54
|
export const VECTOR_CONSTRUCTOR = 0x1CB5C415;
|
|
55
55
|
export const INITIAL_DC = "2-test";
|
|
56
56
|
export const LAYER = 161;
|
|
57
|
-
export const APP_VERSION = "MTKruto 0.0.
|
|
57
|
+
export const APP_VERSION = "MTKruto 0.0.988";
|
|
58
58
|
// @ts-ignore: lib
|
|
59
59
|
export const DEVICE_MODEL = typeof dntShim.Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : dntShim.Deno.build.os + "-" + dntShim.Deno.build.arch;
|
|
60
60
|
export const LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];
|
package/esm/deps.js
CHANGED
|
@@ -4,5 +4,5 @@ export { gunzip, gzip } from "./deps/raw.githubusercontent.com/MTKruto/compress/
|
|
|
4
4
|
export { Mutex } from "async-mutex";
|
|
5
5
|
export { Parser } from "./deps/deno.land/x/html_parser@v0.1.3/src/mod.js";
|
|
6
6
|
import { debug as debug_ } from "./deps/raw.githubusercontent.com/MTKruto/debug/main/mod.js";
|
|
7
|
-
export const debug = (v) => debug_(
|
|
7
|
+
export const debug = (v) => debug_(v);
|
|
8
8
|
export { decode as base64Decode, encode as base64Encode } from "./deps/deno.land/std@0.200.0/encoding/base64.js";
|
|
@@ -2,21 +2,14 @@ import { Connection } from "../connection/0_connection.js";
|
|
|
2
2
|
import { Transport } from "./0_transport.js";
|
|
3
3
|
export type DC = "1" | "2" | "3" | "4" | "5" | "1-test" | "2-test" | "3-test";
|
|
4
4
|
export interface TransportProviderParams {
|
|
5
|
-
dc
|
|
5
|
+
dc: DC;
|
|
6
6
|
cdn: boolean;
|
|
7
7
|
}
|
|
8
|
-
export type TransportProvider = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
transport: Transport;
|
|
13
|
-
dcId: number;
|
|
14
|
-
};
|
|
8
|
+
export type TransportProvider = (params: TransportProviderParams) => {
|
|
9
|
+
connection: Connection;
|
|
10
|
+
transport: Transport;
|
|
11
|
+
dcId: number;
|
|
15
12
|
};
|
|
16
|
-
export
|
|
17
|
-
initialDc: DC;
|
|
18
|
-
}
|
|
19
|
-
export type TransportProviderCreator<E = Record<never, never>> = (params: TransportProviderCreatorParams & E) => TransportProvider;
|
|
20
|
-
export declare const webSocketTransportProvider: TransportProviderCreator<{
|
|
13
|
+
export declare const webSocketTransportProvider: (params?: {
|
|
21
14
|
wss?: boolean;
|
|
22
|
-
}
|
|
15
|
+
}) => TransportProvider;
|
|
@@ -13,17 +13,14 @@ const dcToNameMap = {
|
|
|
13
13
|
function getDcId(dc, cdn) {
|
|
14
14
|
return Number(dc[0]) + (dc.endsWith("-test") ? 10000 : 0) * (cdn ? -1 : 1);
|
|
15
15
|
}
|
|
16
|
-
export const webSocketTransportProvider = (
|
|
17
|
-
return {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const dcId = getDcId(dc, cdn);
|
|
26
|
-
return { connection, transport, dcId };
|
|
27
|
-
},
|
|
16
|
+
export const webSocketTransportProvider = (params) => {
|
|
17
|
+
return ({ dc, cdn }) => {
|
|
18
|
+
params ??= {};
|
|
19
|
+
params.wss ??= typeof location !== "undefined" && location.protocol == "http:" && location.hostname != "localhost" ? false : true;
|
|
20
|
+
const url = `${params.wss ? "wss" : "ws"}://${dcToNameMap[dc]}${cdn ? "-1" : ""}.web.telegram.org/${dc.endsWith("-test") ? "apiws_test" : "apiws"}`;
|
|
21
|
+
const connection = new ConnectionWebSocket(url);
|
|
22
|
+
const transport = new TransportIntermediate(connection, true);
|
|
23
|
+
const dcId = getDcId(dc, cdn);
|
|
24
|
+
return { connection, transport, dcId };
|
|
28
25
|
};
|
|
29
26
|
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export class Queue {
|
|
2
|
+
constructor() {
|
|
3
|
+
Object.defineProperty(this, "functions", {
|
|
4
|
+
enumerable: true,
|
|
5
|
+
configurable: true,
|
|
6
|
+
writable: true,
|
|
7
|
+
value: new Array()
|
|
8
|
+
});
|
|
9
|
+
Object.defineProperty(this, "busy", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true,
|
|
13
|
+
value: false
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
add(fn) {
|
|
17
|
+
this.functions.push(fn);
|
|
18
|
+
this.check();
|
|
19
|
+
}
|
|
20
|
+
check() {
|
|
21
|
+
if (this.busy) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
this.busy = true;
|
|
26
|
+
}
|
|
27
|
+
const fn = this.functions.shift();
|
|
28
|
+
if (fn !== undefined) {
|
|
29
|
+
fn().finally(() => {
|
|
30
|
+
this.busy = false;
|
|
31
|
+
this.check();
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
this.busy = false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
package/package.json
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
import * as types from "../tl/2_types.js";
|
|
2
2
|
export declare function getChannelChatId(channelId: bigint): number;
|
|
3
3
|
export declare function peerToChatId(peer: types.TypePeer | types.TypeInputPeer): number;
|
|
4
|
+
export declare function hasPts(v: types.TypeUpdate | types.TypeUpdates): v is types.UpdateShortMessage | types.UpdateShortChatMessage | types.UpdateShortSentMessage | types.UpdateNewMessage | types.UpdateDeleteMessages | types.UpdateReadHistoryInbox | types.UpdateReadHistoryOutbox | types.UpdatePinnedChannelMessages | types.UpdatePinnedMessages | types.UpdateFolderPeers | types.UpdateChannelWebPage | types.UpdateEditMessage | types.UpdateReadMessagesContents | types.UpdateWebPage;
|
|
5
|
+
export declare function hasChannelPts(v: types.TypeUpdate | types.TypeUpdates): v is types.UpdateNewChannelMessage | types.UpdateEditChannelMessage | types.UpdateDeleteChannelMessages | types.UpdateReadChannelInbox;
|
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.peerToChatId = exports.getChannelChatId = void 0;
|
|
26
|
+
exports.hasChannelPts = exports.hasPts = exports.peerToChatId = exports.getChannelChatId = void 0;
|
|
27
27
|
const constants_js_1 = require("../constants.js");
|
|
28
28
|
const types = __importStar(require("../tl/2_types.js"));
|
|
29
29
|
const _0_control_js_1 = require("../utilities/0_control.js");
|
|
@@ -46,3 +46,27 @@ function peerToChatId(peer) {
|
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
exports.peerToChatId = peerToChatId;
|
|
49
|
+
function hasPts(v) {
|
|
50
|
+
return v instanceof types.UpdateShortMessage ||
|
|
51
|
+
v instanceof types.UpdateShortChatMessage ||
|
|
52
|
+
v instanceof types.UpdateShortSentMessage ||
|
|
53
|
+
v instanceof types.UpdateNewMessage ||
|
|
54
|
+
v instanceof types.UpdateDeleteMessages ||
|
|
55
|
+
v instanceof types.UpdateReadHistoryInbox ||
|
|
56
|
+
v instanceof types.UpdateReadHistoryOutbox ||
|
|
57
|
+
v instanceof types.UpdatePinnedChannelMessages ||
|
|
58
|
+
v instanceof types.UpdatePinnedMessages ||
|
|
59
|
+
v instanceof types.UpdateFolderPeers ||
|
|
60
|
+
v instanceof types.UpdateChannelWebPage ||
|
|
61
|
+
v instanceof types.UpdateEditMessage ||
|
|
62
|
+
v instanceof types.UpdateReadMessagesContents ||
|
|
63
|
+
v instanceof types.UpdateWebPage;
|
|
64
|
+
}
|
|
65
|
+
exports.hasPts = hasPts;
|
|
66
|
+
function hasChannelPts(v) {
|
|
67
|
+
return v instanceof types.UpdateNewChannelMessage ||
|
|
68
|
+
v instanceof types.UpdateEditChannelMessage ||
|
|
69
|
+
v instanceof types.UpdateDeleteChannelMessages ||
|
|
70
|
+
v instanceof types.UpdateReadChannelInbox;
|
|
71
|
+
}
|
|
72
|
+
exports.hasChannelPts = hasChannelPts;
|
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
import { MaybePromise } from "../utilities/0_types.js";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import { DC, TransportProvider } from "../transport/2_transport_provider.js";
|
|
3
|
+
export interface ClientAbstractParams {
|
|
4
|
+
initialDc?: DC;
|
|
5
|
+
/**
|
|
6
|
+
* The transport provider to use. Defaults to `webSocketTransportProvider` with its default options.
|
|
7
|
+
*/
|
|
8
|
+
transportProvider?: TransportProvider;
|
|
9
|
+
cdn?: boolean;
|
|
10
|
+
}
|
|
5
11
|
export declare abstract class ClientAbstract {
|
|
6
|
-
protected
|
|
12
|
+
protected readonly initialDc: DC;
|
|
13
|
+
protected readonly transportProvider: TransportProvider;
|
|
7
14
|
protected readonly cdn: boolean;
|
|
8
|
-
protected
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
get initialDc(): DC;
|
|
13
|
-
constructor(transportProvider?: import("../transport/2_transport_provider.js").TransportProvider, cdn?: boolean);
|
|
15
|
+
protected transport?: ReturnType<TransportProvider>;
|
|
16
|
+
private dc?;
|
|
17
|
+
constructor(params?: ClientAbstractParams);
|
|
18
|
+
protected stateChangeHandler?: (connected: boolean) => void;
|
|
14
19
|
get dcId(): number;
|
|
15
20
|
setDc(dc: DC): MaybePromise<void>;
|
|
16
21
|
get connected(): boolean;
|
|
@@ -5,23 +5,20 @@ const deps_js_1 = require("../deps.js");
|
|
|
5
5
|
const constants_js_1 = require("../constants.js");
|
|
6
6
|
const _2_transport_provider_js_1 = require("../transport/2_transport_provider.js");
|
|
7
7
|
class ClientAbstract {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
constructor(transportProvider = (0, _2_transport_provider_js_1.webSocketTransportProvider)({ initialDc: constants_js_1.INITIAL_DC }), cdn = false) {
|
|
12
|
-
Object.defineProperty(this, "transportProvider", {
|
|
8
|
+
constructor(params) {
|
|
9
|
+
Object.defineProperty(this, "initialDc", {
|
|
13
10
|
enumerable: true,
|
|
14
11
|
configurable: true,
|
|
15
12
|
writable: true,
|
|
16
|
-
value:
|
|
13
|
+
value: void 0
|
|
17
14
|
});
|
|
18
|
-
Object.defineProperty(this, "
|
|
15
|
+
Object.defineProperty(this, "transportProvider", {
|
|
19
16
|
enumerable: true,
|
|
20
17
|
configurable: true,
|
|
21
18
|
writable: true,
|
|
22
|
-
value:
|
|
19
|
+
value: void 0
|
|
23
20
|
});
|
|
24
|
-
Object.defineProperty(this, "
|
|
21
|
+
Object.defineProperty(this, "cdn", {
|
|
25
22
|
enumerable: true,
|
|
26
23
|
configurable: true,
|
|
27
24
|
writable: true,
|
|
@@ -33,42 +30,41 @@ class ClientAbstract {
|
|
|
33
30
|
writable: true,
|
|
34
31
|
value: void 0
|
|
35
32
|
});
|
|
36
|
-
Object.defineProperty(this, "
|
|
33
|
+
Object.defineProperty(this, "dc", {
|
|
37
34
|
enumerable: true,
|
|
38
35
|
configurable: true,
|
|
39
36
|
writable: true,
|
|
40
37
|
value: void 0
|
|
41
38
|
});
|
|
42
|
-
Object.defineProperty(this, "
|
|
39
|
+
Object.defineProperty(this, "stateChangeHandler", {
|
|
43
40
|
enumerable: true,
|
|
44
41
|
configurable: true,
|
|
45
42
|
writable: true,
|
|
46
43
|
value: void 0
|
|
47
44
|
});
|
|
48
|
-
|
|
49
|
-
this.
|
|
50
|
-
|
|
51
|
-
this.connection = connection;
|
|
52
|
-
this.transport = transport;
|
|
53
|
-
this._dcId = dcId;
|
|
45
|
+
this.initialDc = params?.initialDc ?? constants_js_1.INITIAL_DC;
|
|
46
|
+
this.transportProvider = params?.transportProvider ?? (0, _2_transport_provider_js_1.webSocketTransportProvider)();
|
|
47
|
+
this.cdn = params?.cdn ?? false;
|
|
54
48
|
}
|
|
55
49
|
get dcId() {
|
|
56
|
-
|
|
50
|
+
if (!this.transport) {
|
|
51
|
+
throw new Error("Not connected");
|
|
52
|
+
}
|
|
53
|
+
return this.transport.dcId;
|
|
57
54
|
}
|
|
58
55
|
// MaybePromise since `Client` has to deal with `Storage.set()`
|
|
59
56
|
setDc(dc) {
|
|
60
|
-
|
|
61
|
-
this.connection = connection;
|
|
62
|
-
this.transport = transport;
|
|
63
|
-
this._dcId = dcId;
|
|
57
|
+
this.dc = dc;
|
|
64
58
|
}
|
|
65
59
|
get connected() {
|
|
66
|
-
return this.connection.connected;
|
|
60
|
+
return this.transport === undefined ? false : this.transport.connection.connected;
|
|
67
61
|
}
|
|
68
62
|
async connect() {
|
|
63
|
+
this.transport = this.transportProvider({ dc: this.dc ?? this.initialDc, cdn: this.cdn });
|
|
64
|
+
this.transport.connection.stateChangeHandler = this.stateChangeHandler;
|
|
69
65
|
await (0, deps_js_1.initTgCrypto)();
|
|
70
|
-
await this.connection.open();
|
|
71
|
-
await this.transport.initialize();
|
|
66
|
+
await this.transport.connection.open();
|
|
67
|
+
await this.transport.transport.initialize();
|
|
72
68
|
}
|
|
73
69
|
async reconnect(dc) {
|
|
74
70
|
await this.disconnect();
|
|
@@ -78,8 +74,11 @@ class ClientAbstract {
|
|
|
78
74
|
await this.connect();
|
|
79
75
|
}
|
|
80
76
|
async disconnect() {
|
|
81
|
-
|
|
82
|
-
|
|
77
|
+
if (!this.transport) {
|
|
78
|
+
throw new Error("Not connected");
|
|
79
|
+
}
|
|
80
|
+
await this.transport.transport.deinitialize();
|
|
81
|
+
await this.transport.connection.close();
|
|
83
82
|
}
|
|
84
83
|
}
|
|
85
84
|
exports.ClientAbstract = ClientAbstract;
|
|
@@ -1,10 +1,16 @@
|
|
|
1
|
+
import { PublicKeys } from "../constants.js";
|
|
1
2
|
import { Function } from "../tl/3_functions.js";
|
|
2
|
-
import {
|
|
3
|
-
|
|
3
|
+
import { ClientAbstract, ClientAbstractParams } from "./1_client_abstract.js";
|
|
4
|
+
export interface ClientPlainParams extends ClientAbstractParams {
|
|
5
|
+
/**
|
|
6
|
+
* MTProto public keys to use in the `[keyId, [key, exponent]][]` format. Don't set this unless you know what you are doing.
|
|
7
|
+
*/
|
|
8
|
+
publicKeys?: PublicKeys;
|
|
9
|
+
}
|
|
4
10
|
export declare class ClientPlain extends ClientAbstract {
|
|
5
11
|
private readonly publicKeys;
|
|
6
12
|
private lastMsgId;
|
|
7
|
-
constructor(
|
|
13
|
+
constructor(params?: ClientPlainParams);
|
|
8
14
|
invoke<T extends Function<unknown>>(function_: T): Promise<T["__R"]>;
|
|
9
15
|
createAuthKey(): Promise<{
|
|
10
16
|
authKey: Uint8Array;
|
|
@@ -16,13 +16,13 @@ const _1_client_abstract_js_1 = require("./1_client_abstract.js");
|
|
|
16
16
|
const _0_message_js_1 = require("./0_message.js");
|
|
17
17
|
const d = (0, deps_js_1.debug)("ClientPlain/createAuthKey");
|
|
18
18
|
class ClientPlain extends _1_client_abstract_js_1.ClientAbstract {
|
|
19
|
-
constructor(
|
|
20
|
-
super(
|
|
19
|
+
constructor(params) {
|
|
20
|
+
super(params);
|
|
21
21
|
Object.defineProperty(this, "publicKeys", {
|
|
22
22
|
enumerable: true,
|
|
23
23
|
configurable: true,
|
|
24
24
|
writable: true,
|
|
25
|
-
value:
|
|
25
|
+
value: void 0
|
|
26
26
|
});
|
|
27
27
|
Object.defineProperty(this, "lastMsgId", {
|
|
28
28
|
enumerable: true,
|
|
@@ -30,11 +30,15 @@ class ClientPlain extends _1_client_abstract_js_1.ClientAbstract {
|
|
|
30
30
|
writable: true,
|
|
31
31
|
value: 0n
|
|
32
32
|
});
|
|
33
|
+
this.publicKeys = params?.publicKeys ?? constants_js_1.PUBLIC_KEYS;
|
|
33
34
|
}
|
|
34
35
|
async invoke(function_) {
|
|
36
|
+
if (!this.transport) {
|
|
37
|
+
throw new Error("Not connected");
|
|
38
|
+
}
|
|
35
39
|
const msgId = this.lastMsgId = (0, _0_message_js_1.getMessageId)(this.lastMsgId);
|
|
36
|
-
await this.transport.send((0, _0_message_js_1.packUnencryptedMessage)(function_[_1_tl_object_js_1.serialize](), msgId));
|
|
37
|
-
const buffer = await this.transport.receive();
|
|
40
|
+
await this.transport.transport.send((0, _0_message_js_1.packUnencryptedMessage)(function_[_1_tl_object_js_1.serialize](), msgId));
|
|
41
|
+
const buffer = await this.transport.transport.receive();
|
|
38
42
|
if (buffer.length == 4) {
|
|
39
43
|
const int = (0, _0_bigint_js_1.bigIntFromBuffer)(buffer, true, true);
|
|
40
44
|
if (int == -404n) {
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { PublicKeys } from "../constants.js";
|
|
2
1
|
import { MaybePromise } from "../utilities/0_types.js";
|
|
3
2
|
import * as types from "../tl/2_types.js";
|
|
4
3
|
import * as functions from "../tl/3_functions.js";
|
|
5
4
|
import { ReadObject } from "../tl/3_tl_reader.js";
|
|
6
5
|
import { Storage } from "../storage/0_storage.js";
|
|
7
|
-
import { DC
|
|
6
|
+
import { DC } from "../transport/2_transport_provider.js";
|
|
8
7
|
import { MessageEntity } from "../types/0_message_entity.js";
|
|
9
8
|
import { ReplyKeyboardRemove } from "../types/0_reply_keyboard_remove.js";
|
|
10
9
|
import { ForceReply } from "../types/0_force_reply.js";
|
|
@@ -12,6 +11,7 @@ import { ReplyKeyboardMarkup } from "../types/2_reply_keyboard_markup.js";
|
|
|
12
11
|
import { InlineKeyboardMarkup } from "../types/2_inline_keyboard_markup.js";
|
|
13
12
|
import { Message } from "../types/3_message.js";
|
|
14
13
|
import { ClientAbstract } from "./1_client_abstract.js";
|
|
14
|
+
import { ClientPlainParams } from "./2_client_plain.js";
|
|
15
15
|
export declare const getEntity: unique symbol;
|
|
16
16
|
export declare const getStickerSetName: unique symbol;
|
|
17
17
|
export declare const handleMigrationError: unique symbol;
|
|
@@ -25,16 +25,11 @@ export interface AuthorizeUserParams<S = string> {
|
|
|
25
25
|
code: S | (() => MaybePromise<S>);
|
|
26
26
|
password: S | ((hint: string | null) => MaybePromise<S>);
|
|
27
27
|
}
|
|
28
|
-
export
|
|
29
|
-
export interface ClientParams {
|
|
28
|
+
export interface ClientParams extends ClientPlainParams {
|
|
30
29
|
/**
|
|
31
30
|
* Default parse mode. Defauls to `ParseMode.None`.
|
|
32
31
|
*/
|
|
33
32
|
parseMode?: ParseMode;
|
|
34
|
-
/**
|
|
35
|
-
* The transport provider to use. Defaults to `webSocketTransportProvider`.
|
|
36
|
-
*/
|
|
37
|
-
transportProvider?: TransportProvider;
|
|
38
33
|
/**
|
|
39
34
|
* The app_version parameter to be passed to initConnection when calling `authorize`.
|
|
40
35
|
*/
|
|
@@ -59,10 +54,6 @@ export interface ClientParams {
|
|
|
59
54
|
* The system_version parameter to be passed to initConnection when calling `authorize`.
|
|
60
55
|
*/
|
|
61
56
|
systemVersion?: string;
|
|
62
|
-
/**
|
|
63
|
-
* MTProto public keys to use in the `[keyId, [key, exponent]][]` format. Don't set this unless you know what you are doing.
|
|
64
|
-
*/
|
|
65
|
-
publicKeys?: PublicKeys;
|
|
66
57
|
/**
|
|
67
58
|
* Whether to automatically call `start` with no parameters in the first `invoke` call.
|
|
68
59
|
*/
|
|
@@ -161,7 +152,6 @@ export declare class Client extends ClientAbstract {
|
|
|
161
152
|
private promises;
|
|
162
153
|
private toAcknowledge;
|
|
163
154
|
private updateState?;
|
|
164
|
-
updateHandler: UpdateHandler;
|
|
165
155
|
readonly parseMode: ParseMode;
|
|
166
156
|
readonly appVersion: string;
|
|
167
157
|
readonly deviceModel: string;
|
|
@@ -179,7 +169,9 @@ export declare class Client extends ClientAbstract {
|
|
|
179
169
|
* @param apiHash App's API hash from [my.telegram.org/apps](https://my.telegram.org/apps). Defaults to empty string (unset).
|
|
180
170
|
* @param params Other parameters.
|
|
181
171
|
*/
|
|
182
|
-
constructor(storage?: Storage, apiId?: number | null, apiHash?: string | null, params?: ClientParams
|
|
172
|
+
constructor(storage?: Storage, apiId?: number | null, apiHash?: string | null, params?: ClientParams);
|
|
173
|
+
private propagateConnectionState;
|
|
174
|
+
protected stateChangeHandler: (connected: boolean) => void;
|
|
183
175
|
private storageInited;
|
|
184
176
|
/**
|
|
185
177
|
* Sets the DC and resets the auth key stored in the session provider
|
|
@@ -189,6 +181,7 @@ export declare class Client extends ClientAbstract {
|
|
|
189
181
|
*/
|
|
190
182
|
setDc(dc: DC): Promise<void>;
|
|
191
183
|
private setAuth;
|
|
184
|
+
private connectMutex;
|
|
192
185
|
/**
|
|
193
186
|
* Loads the session if `setDc` was not called, initializes and connnects
|
|
194
187
|
* a `ClientPlain` to generate auth key if there was none, and connects the client.
|
|
@@ -240,14 +233,13 @@ export declare class Client extends ClientAbstract {
|
|
|
240
233
|
send<T extends (functions.Function<unknown> | types.Type) = functions.Function<unknown>>(function_: T): Promise<void>;
|
|
241
234
|
private processChats;
|
|
242
235
|
private processUsers;
|
|
243
|
-
private
|
|
244
|
-
private
|
|
245
|
-
private
|
|
246
|
-
private
|
|
236
|
+
private handleUpdateQueue;
|
|
237
|
+
private processUpdatesQueue;
|
|
238
|
+
private checkGap;
|
|
239
|
+
private checkChannelGap;
|
|
247
240
|
private processUpdates;
|
|
248
241
|
private setUpdateStateDate;
|
|
249
242
|
private getLocalState;
|
|
250
|
-
private updateGapRecoveryMutex;
|
|
251
243
|
private recoverUpdateGap;
|
|
252
244
|
private recoverChannelUpdateGap;
|
|
253
245
|
getInputPeer(id: ChatID): Promise<types.InputPeerChat | types.InputPeerUser | types.InputPeerChannel>;
|
|
@@ -313,4 +305,21 @@ export declare class Client extends ClientAbstract {
|
|
|
313
305
|
* Get information on the currently authorized user.
|
|
314
306
|
*/
|
|
315
307
|
getMe(): Promise<import("../types/1_user.js").User>;
|
|
308
|
+
private handleUpdate;
|
|
309
|
+
handler: Handler;
|
|
310
|
+
use(middleware: Handler): void;
|
|
311
|
+
on<U extends keyof Update, K extends keyof Update[U]>(filter: Update[U] extends string ? U : U | [U, ...K[]], handler: Handler<Pick<{
|
|
312
|
+
[P in U]: With<Update[U], K>;
|
|
313
|
+
}, U>>): void;
|
|
314
|
+
}
|
|
315
|
+
type With<T, K extends keyof T> = T & Required<Pick<T, K>>;
|
|
316
|
+
export type ConnectionState = "not-connected" | "updating" | "ready";
|
|
317
|
+
export interface Update {
|
|
318
|
+
message: Message;
|
|
319
|
+
editedMessage: Message;
|
|
320
|
+
connectionState: ConnectionState;
|
|
321
|
+
}
|
|
322
|
+
export interface Handler<U extends Partial<Update> = Partial<Update>> {
|
|
323
|
+
(update: U, next: () => Promise<void>): MaybePromise<void>;
|
|
316
324
|
}
|
|
325
|
+
export {};
|