@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,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;
|
|
@@ -18,3 +18,25 @@ export function peerToChatId(peer) {
|
|
|
18
18
|
UNREACHABLE();
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
|
+
export function hasPts(v) {
|
|
22
|
+
return v instanceof types.UpdateShortMessage ||
|
|
23
|
+
v instanceof types.UpdateShortChatMessage ||
|
|
24
|
+
v instanceof types.UpdateShortSentMessage ||
|
|
25
|
+
v instanceof types.UpdateNewMessage ||
|
|
26
|
+
v instanceof types.UpdateDeleteMessages ||
|
|
27
|
+
v instanceof types.UpdateReadHistoryInbox ||
|
|
28
|
+
v instanceof types.UpdateReadHistoryOutbox ||
|
|
29
|
+
v instanceof types.UpdatePinnedChannelMessages ||
|
|
30
|
+
v instanceof types.UpdatePinnedMessages ||
|
|
31
|
+
v instanceof types.UpdateFolderPeers ||
|
|
32
|
+
v instanceof types.UpdateChannelWebPage ||
|
|
33
|
+
v instanceof types.UpdateEditMessage ||
|
|
34
|
+
v instanceof types.UpdateReadMessagesContents ||
|
|
35
|
+
v instanceof types.UpdateWebPage;
|
|
36
|
+
}
|
|
37
|
+
export function hasChannelPts(v) {
|
|
38
|
+
return v instanceof types.UpdateNewChannelMessage ||
|
|
39
|
+
v instanceof types.UpdateEditChannelMessage ||
|
|
40
|
+
v instanceof types.UpdateDeleteChannelMessages ||
|
|
41
|
+
v instanceof types.UpdateReadChannelInbox;
|
|
42
|
+
}
|
|
@@ -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;
|
|
@@ -2,23 +2,20 @@ import { initTgCrypto } from "../deps.js";
|
|
|
2
2
|
import { INITIAL_DC } from "../constants.js";
|
|
3
3
|
import { webSocketTransportProvider } from "../transport/2_transport_provider.js";
|
|
4
4
|
export class ClientAbstract {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
constructor(transportProvider = webSocketTransportProvider({ initialDc: INITIAL_DC }), cdn = false) {
|
|
9
|
-
Object.defineProperty(this, "transportProvider", {
|
|
5
|
+
constructor(params) {
|
|
6
|
+
Object.defineProperty(this, "initialDc", {
|
|
10
7
|
enumerable: true,
|
|
11
8
|
configurable: true,
|
|
12
9
|
writable: true,
|
|
13
|
-
value:
|
|
10
|
+
value: void 0
|
|
14
11
|
});
|
|
15
|
-
Object.defineProperty(this, "
|
|
12
|
+
Object.defineProperty(this, "transportProvider", {
|
|
16
13
|
enumerable: true,
|
|
17
14
|
configurable: true,
|
|
18
15
|
writable: true,
|
|
19
|
-
value:
|
|
16
|
+
value: void 0
|
|
20
17
|
});
|
|
21
|
-
Object.defineProperty(this, "
|
|
18
|
+
Object.defineProperty(this, "cdn", {
|
|
22
19
|
enumerable: true,
|
|
23
20
|
configurable: true,
|
|
24
21
|
writable: true,
|
|
@@ -30,42 +27,41 @@ export class ClientAbstract {
|
|
|
30
27
|
writable: true,
|
|
31
28
|
value: void 0
|
|
32
29
|
});
|
|
33
|
-
Object.defineProperty(this, "
|
|
30
|
+
Object.defineProperty(this, "dc", {
|
|
34
31
|
enumerable: true,
|
|
35
32
|
configurable: true,
|
|
36
33
|
writable: true,
|
|
37
34
|
value: void 0
|
|
38
35
|
});
|
|
39
|
-
Object.defineProperty(this, "
|
|
36
|
+
Object.defineProperty(this, "stateChangeHandler", {
|
|
40
37
|
enumerable: true,
|
|
41
38
|
configurable: true,
|
|
42
39
|
writable: true,
|
|
43
40
|
value: void 0
|
|
44
41
|
});
|
|
45
|
-
|
|
46
|
-
this.
|
|
47
|
-
|
|
48
|
-
this.connection = connection;
|
|
49
|
-
this.transport = transport;
|
|
50
|
-
this._dcId = dcId;
|
|
42
|
+
this.initialDc = params?.initialDc ?? INITIAL_DC;
|
|
43
|
+
this.transportProvider = params?.transportProvider ?? webSocketTransportProvider();
|
|
44
|
+
this.cdn = params?.cdn ?? false;
|
|
51
45
|
}
|
|
52
46
|
get dcId() {
|
|
53
|
-
|
|
47
|
+
if (!this.transport) {
|
|
48
|
+
throw new Error("Not connected");
|
|
49
|
+
}
|
|
50
|
+
return this.transport.dcId;
|
|
54
51
|
}
|
|
55
52
|
// MaybePromise since `Client` has to deal with `Storage.set()`
|
|
56
53
|
setDc(dc) {
|
|
57
|
-
|
|
58
|
-
this.connection = connection;
|
|
59
|
-
this.transport = transport;
|
|
60
|
-
this._dcId = dcId;
|
|
54
|
+
this.dc = dc;
|
|
61
55
|
}
|
|
62
56
|
get connected() {
|
|
63
|
-
return this.connection.connected;
|
|
57
|
+
return this.transport === undefined ? false : this.transport.connection.connected;
|
|
64
58
|
}
|
|
65
59
|
async connect() {
|
|
60
|
+
this.transport = this.transportProvider({ dc: this.dc ?? this.initialDc, cdn: this.cdn });
|
|
61
|
+
this.transport.connection.stateChangeHandler = this.stateChangeHandler;
|
|
66
62
|
await initTgCrypto();
|
|
67
|
-
await this.connection.open();
|
|
68
|
-
await this.transport.initialize();
|
|
63
|
+
await this.transport.connection.open();
|
|
64
|
+
await this.transport.transport.initialize();
|
|
69
65
|
}
|
|
70
66
|
async reconnect(dc) {
|
|
71
67
|
await this.disconnect();
|
|
@@ -75,7 +71,10 @@ export class ClientAbstract {
|
|
|
75
71
|
await this.connect();
|
|
76
72
|
}
|
|
77
73
|
async disconnect() {
|
|
78
|
-
|
|
79
|
-
|
|
74
|
+
if (!this.transport) {
|
|
75
|
+
throw new Error("Not connected");
|
|
76
|
+
}
|
|
77
|
+
await this.transport.transport.deinitialize();
|
|
78
|
+
await this.transport.connection.close();
|
|
80
79
|
}
|
|
81
80
|
}
|
|
@@ -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;
|
|
@@ -13,13 +13,13 @@ import { ClientAbstract } from "./1_client_abstract.js";
|
|
|
13
13
|
import { getMessageId, packUnencryptedMessage, unpackUnencryptedMessage } from "./0_message.js";
|
|
14
14
|
const d = debug("ClientPlain/createAuthKey");
|
|
15
15
|
export class ClientPlain extends ClientAbstract {
|
|
16
|
-
constructor(
|
|
17
|
-
super(
|
|
16
|
+
constructor(params) {
|
|
17
|
+
super(params);
|
|
18
18
|
Object.defineProperty(this, "publicKeys", {
|
|
19
19
|
enumerable: true,
|
|
20
20
|
configurable: true,
|
|
21
21
|
writable: true,
|
|
22
|
-
value:
|
|
22
|
+
value: void 0
|
|
23
23
|
});
|
|
24
24
|
Object.defineProperty(this, "lastMsgId", {
|
|
25
25
|
enumerable: true,
|
|
@@ -27,11 +27,15 @@ export class ClientPlain extends ClientAbstract {
|
|
|
27
27
|
writable: true,
|
|
28
28
|
value: 0n
|
|
29
29
|
});
|
|
30
|
+
this.publicKeys = params?.publicKeys ?? PUBLIC_KEYS;
|
|
30
31
|
}
|
|
31
32
|
async invoke(function_) {
|
|
33
|
+
if (!this.transport) {
|
|
34
|
+
throw new Error("Not connected");
|
|
35
|
+
}
|
|
32
36
|
const msgId = this.lastMsgId = getMessageId(this.lastMsgId);
|
|
33
|
-
await this.transport.send(packUnencryptedMessage(function_[serialize](), msgId));
|
|
34
|
-
const buffer = await this.transport.receive();
|
|
37
|
+
await this.transport.transport.send(packUnencryptedMessage(function_[serialize](), msgId));
|
|
38
|
+
const buffer = await this.transport.transport.receive();
|
|
35
39
|
if (buffer.length == 4) {
|
|
36
40
|
const int = bigIntFromBuffer(buffer, true, true);
|
|
37
41
|
if (int == -404n) {
|
package/esm/client/3_client.d.ts
CHANGED
|
@@ -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 {};
|