@mtkruto/node 0.0.77 → 0.0.79

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.
@@ -1,5 +1,5 @@
1
1
  import { gunzip } from "../deps.js";
2
- import { ackThreshold } from "../constants.js";
2
+ import { ackThreshold, DEFAULT_APP_VERSION, DEFAULT_DEVICE_MODEL, DEFAULT_LANG_CODE, DEFAULT_LANG_PACK, DEFAULT_SYSTEM_LANG_CODE, DEFAULT_SYSTEM_VERSION, LAYER } from "../constants.js";
3
3
  import { getRandomBigInt } from "../utilities/0_bigint.js";
4
4
  import { decryptMessage, encryptMessage, getMessageId } from "../utilities/1_message.js";
5
5
  import * as types from "../tl/2_types.js";
@@ -12,7 +12,7 @@ import { ClientAbstract } from "./client_abstract.js";
12
12
  import { ClientPlain } from "./client_plain.js";
13
13
  import { SessionMemory } from "../session/session_memory.js";
14
14
  export class Client extends ClientAbstract {
15
- constructor(session = new SessionMemory(), params) {
15
+ constructor(session = new SessionMemory(), apiId = 0, apiHash = "", params) {
16
16
  super(params?.transportProvider);
17
17
  Object.defineProperty(this, "session", {
18
18
  enumerable: true,
@@ -20,6 +20,18 @@ export class Client extends ClientAbstract {
20
20
  writable: true,
21
21
  value: session
22
22
  });
23
+ Object.defineProperty(this, "apiId", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: apiId
28
+ });
29
+ Object.defineProperty(this, "apiHash", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: apiHash
34
+ });
23
35
  Object.defineProperty(this, "sessionId", {
24
36
  enumerable: true,
25
37
  configurable: true,
@@ -50,29 +62,144 @@ export class Client extends ClientAbstract {
50
62
  writable: true,
51
63
  value: null
52
64
  });
65
+ Object.defineProperty(this, "appVersion", {
66
+ enumerable: true,
67
+ configurable: true,
68
+ writable: true,
69
+ value: void 0
70
+ });
71
+ Object.defineProperty(this, "deviceModel", {
72
+ enumerable: true,
73
+ configurable: true,
74
+ writable: true,
75
+ value: void 0
76
+ });
77
+ Object.defineProperty(this, "langCode", {
78
+ enumerable: true,
79
+ configurable: true,
80
+ writable: true,
81
+ value: void 0
82
+ });
83
+ Object.defineProperty(this, "langPack", {
84
+ enumerable: true,
85
+ configurable: true,
86
+ writable: true,
87
+ value: void 0
88
+ });
89
+ Object.defineProperty(this, "systemLangCode", {
90
+ enumerable: true,
91
+ configurable: true,
92
+ writable: true,
93
+ value: void 0
94
+ });
95
+ Object.defineProperty(this, "systemVersion", {
96
+ enumerable: true,
97
+ configurable: true,
98
+ writable: true,
99
+ value: void 0
100
+ });
101
+ Object.defineProperty(this, "sessionLoaded", {
102
+ enumerable: true,
103
+ configurable: true,
104
+ writable: true,
105
+ value: false
106
+ });
107
+ this.appVersion = params?.appVersion ?? DEFAULT_APP_VERSION;
108
+ this.deviceModel = params?.deviceModel ?? DEFAULT_DEVICE_MODEL;
109
+ this.langCode = params?.langCode ?? DEFAULT_LANG_CODE;
110
+ this.langPack = params?.langPack ?? DEFAULT_LANG_PACK;
111
+ this.systemLangCode = params?.systemLangCode ?? DEFAULT_SYSTEM_LANG_CODE;
112
+ this.systemVersion = params?.systemVersion ?? DEFAULT_SYSTEM_VERSION;
113
+ }
114
+ setDc(dc) {
115
+ if (this.session.dc != dc) {
116
+ this.session.dc = dc;
117
+ this.session.authKey = null;
118
+ }
119
+ super.setDc(dc);
53
120
  }
54
121
  async connect() {
55
- await this.session.load();
122
+ if (!this.sessionLoaded) {
123
+ await this.session.load();
124
+ this.sessionLoaded = true;
125
+ }
56
126
  if (this.session.authKey == null) {
57
127
  const plain = new ClientPlain(this.transportProvider);
128
+ if (this.session.dc != null) {
129
+ plain.setDc(this.session.dc);
130
+ }
58
131
  await plain.connect();
59
132
  const { authKey, salt } = await plain.createAuthKey();
60
133
  await plain.disconnect();
61
134
  this.state.salt = salt;
62
135
  this.session.authKey = authKey;
63
- await this.session.save();
64
136
  }
65
137
  if (this.session.dc != null) {
66
- const { connection, transport, dcId } = this.transportProvider({ dc: this.session.dc, cdn: false });
67
- this.connection = connection;
68
- this.transport = transport;
69
- this.dcId = dcId;
138
+ this.setDc(this.session.dc);
70
139
  }
140
+ await this.session.save();
71
141
  await super.connect();
72
142
  // logger().debug("Client connected");
73
143
  this.receiveLoop();
74
144
  this.pingLoop();
75
145
  }
146
+ async authorize(params) {
147
+ if (!this.apiId) {
148
+ throw new Error("apiId not set");
149
+ }
150
+ if (!this.apiHash) {
151
+ throw new Error("apiHash not set");
152
+ }
153
+ await this.invoke(new functions.InitConnection({
154
+ apiId: this.apiId,
155
+ appVersion: this.appVersion,
156
+ deviceModel: this.deviceModel,
157
+ langCode: this.langCode,
158
+ langPack: this.langPack,
159
+ query: new functions.InvokeWithLayer({
160
+ layer: LAYER,
161
+ query: new functions.HelpGetConfig(),
162
+ }),
163
+ systemLangCode: this.systemLangCode,
164
+ systemVersion: this.systemVersion,
165
+ }));
166
+ try {
167
+ await this.invoke(new functions.UpdatesGetState());
168
+ return;
169
+ }
170
+ catch (err) {
171
+ if (!(err instanceof types.RPCError) || err.errorMessage != "AUTH_KEY_UNREGISTERED") {
172
+ throw err;
173
+ }
174
+ }
175
+ try {
176
+ if (typeof params == "object") {
177
+ throw new Error("Not implemented");
178
+ }
179
+ else {
180
+ await this.invoke(new functions.AuthImportBotAuthorization({ apiId: this.apiId, apiHash: this.apiHash, botAuthToken: params, flags: 0 }));
181
+ }
182
+ }
183
+ catch (err) {
184
+ if (err instanceof types.RPCError) {
185
+ const match = err.errorMessage.match(/MIGRATE_(\d)$/);
186
+ if (match) {
187
+ let newDc = match[1];
188
+ if (Math.abs(this.dcId) >= 10000) {
189
+ newDc += "-test";
190
+ }
191
+ await this.reconnect(newDc);
192
+ await this.authorize(params);
193
+ }
194
+ else {
195
+ throw err;
196
+ }
197
+ }
198
+ else {
199
+ throw err;
200
+ }
201
+ }
202
+ }
76
203
  async receiveLoop() {
77
204
  if (!this.session.authKey) {
78
205
  throw new Error("Not connected");
@@ -82,7 +209,18 @@ export class Client extends ClientAbstract {
82
209
  await this.send(new types.MsgsAck({ msgIds: [...this.toAcknowledge] }));
83
210
  this.toAcknowledge.clear();
84
211
  }
85
- const buffer = await this.transport.receive();
212
+ let buffer;
213
+ try {
214
+ buffer = await this.transport.receive();
215
+ }
216
+ catch (err) {
217
+ if (!this.connected) {
218
+ break;
219
+ }
220
+ else {
221
+ throw err;
222
+ }
223
+ }
86
224
  let decrypted;
87
225
  try {
88
226
  decrypted = await decryptMessage(buffer, this.session.authKey, this.sessionId);
@@ -1,7 +1,8 @@
1
1
  import { initTgCrypto } from "../deps.js";
2
- import { defaultDc, defaultTransportProvider } from "../transport/transport_provider.js";
2
+ import { DEFAULT_INITIAL_DC } from "../constants.js";
3
+ import { defaultTransportProvider } from "../transport/transport_provider.js";
3
4
  export class ClientAbstract {
4
- constructor(transportProvider = defaultTransportProvider()) {
5
+ constructor(transportProvider = defaultTransportProvider({ initialDc: DEFAULT_INITIAL_DC })) {
5
6
  Object.defineProperty(this, "transportProvider", {
6
7
  enumerable: true,
7
8
  configurable: true,
@@ -20,7 +21,7 @@ export class ClientAbstract {
20
21
  writable: true,
21
22
  value: void 0
22
23
  });
23
- Object.defineProperty(this, "dcId", {
24
+ Object.defineProperty(this, "_dcId", {
24
25
  enumerable: true,
25
26
  configurable: true,
26
27
  writable: true,
@@ -32,10 +33,19 @@ export class ClientAbstract {
32
33
  writable: true,
33
34
  value: false
34
35
  });
35
- const { connection, transport, dcId } = transportProvider({ dc: defaultDc, cdn: false });
36
+ const { connection, transport, dcId } = transportProvider({ cdn: false });
36
37
  this.connection = connection;
37
38
  this.transport = transport;
38
- this.dcId = dcId;
39
+ this._dcId = dcId;
40
+ }
41
+ get dcId() {
42
+ return this._dcId;
43
+ }
44
+ setDc(dc) {
45
+ const { connection, transport, dcId } = this.transportProvider({ dc, cdn: false });
46
+ this.connection = connection;
47
+ this.transport = transport;
48
+ this._dcId = dcId;
39
49
  }
40
50
  async connect() {
41
51
  await initTgCrypto();
@@ -43,6 +53,13 @@ export class ClientAbstract {
43
53
  await this.transport.initialize();
44
54
  this.connected = true;
45
55
  }
56
+ async reconnect(dc) {
57
+ await this.disconnect();
58
+ if (dc) {
59
+ this.setDc(dc);
60
+ }
61
+ await this.connect();
62
+ }
46
63
  async disconnect() {
47
64
  await this.transport.deinitialize();
48
65
  await this.connection.close();
package/esm/constants.js CHANGED
@@ -59,3 +59,12 @@ export const publicKeys = new Map([
59
59
  ],
60
60
  ]);
61
61
  export const VECTOR_CONSTRUCTOR = 0x1CB5C415;
62
+ export const DEFAULT_INITIAL_DC = "2-test";
63
+ export const LAYER = 158;
64
+ // TODO
65
+ export const DEFAULT_APP_VERSION = "MTKruto Unstable <v1.0.0";
66
+ export const DEFAULT_DEVICE_MODEL = "Krutaya Device";
67
+ export const DEFAULT_LANG_CODE = "en";
68
+ export const DEFAULT_LANG_PACK = "";
69
+ export const DEFAULT_SYSTEM_LANG_CODE = "en";
70
+ export const DEFAULT_SYSTEM_VERSION = "1.0";
package/esm/mod.js CHANGED
@@ -17,3 +17,4 @@ export * from "./transport/transport.js";
17
17
  export * from "./transport/transport_provider.js";
18
18
  export * from "./connection/connection.js";
19
19
  export * from "./connection/connection_web_socket.js";
20
+ export { DEFAULT_APP_VERSION, DEFAULT_DEVICE_MODEL, DEFAULT_INITIAL_DC, DEFAULT_LANG_CODE, DEFAULT_LANG_PACK, DEFAULT_SYSTEM_LANG_CODE, DEFAULT_SYSTEM_VERSION, LAYER } from "./constants.js";
@@ -1,6 +1,5 @@
1
1
  import { ConnectionWebSocket } from "../connection/connection_web_socket.js";
2
2
  import { TransportIntermediate } from "./transport_intermediate.js";
3
- export const defaultDc = "2-test";
4
3
  const dcToNameMap = {
5
4
  "1": "pluto",
6
5
  "1-test": "pluto",
@@ -11,8 +10,9 @@ const dcToNameMap = {
11
10
  "4": "vesta",
12
11
  "5": "flora",
13
12
  };
14
- export function defaultTransportProvider(wss) {
13
+ export const defaultTransportProvider = ({ initialDc, wss }) => {
15
14
  return ({ dc, cdn }) => {
15
+ dc ??= initialDc;
16
16
  wss ??= typeof location !== "undefined" && location.protocol == "http:" ? false : true;
17
17
  const url = `${wss ? "wss" : "ws"}://${dcToNameMap[dc]}${cdn ? "-1" : ""}.web.telegram.org/${dc.endsWith("-test") ? "apiws_test" : "apiws"}`;
18
18
  const connection = new ConnectionWebSocket(url);
@@ -20,4 +20,4 @@ export function defaultTransportProvider(wss) {
20
20
  const dcId = Number(dc[0]) + (dc.endsWith("-test") ? 10000 : 0) * (cdn ? -1 : 1);
21
21
  return { connection, transport, dcId };
22
22
  };
23
- }
23
+ };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "main": "./script/mod.js",
4
4
  "types": "./types/mod.d.ts",
5
5
  "name": "@mtkruto/node",
6
- "version": "0.0.77",
6
+ "version": "0.0.79",
7
7
  "description": "MTKruto for Node.js",
8
8
  "author": "Roj <rojvv@icloud.com>",
9
9
  "license": "LGPL-3.0-or-later",
@@ -38,7 +38,7 @@ const client_abstract_js_1 = require("./client_abstract.js");
38
38
  const client_plain_js_1 = require("./client_plain.js");
39
39
  const session_memory_js_1 = require("../session/session_memory.js");
40
40
  class Client extends client_abstract_js_1.ClientAbstract {
41
- constructor(session = new session_memory_js_1.SessionMemory(), params) {
41
+ constructor(session = new session_memory_js_1.SessionMemory(), apiId = 0, apiHash = "", params) {
42
42
  super(params?.transportProvider);
43
43
  Object.defineProperty(this, "session", {
44
44
  enumerable: true,
@@ -46,6 +46,18 @@ class Client extends client_abstract_js_1.ClientAbstract {
46
46
  writable: true,
47
47
  value: session
48
48
  });
49
+ Object.defineProperty(this, "apiId", {
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true,
53
+ value: apiId
54
+ });
55
+ Object.defineProperty(this, "apiHash", {
56
+ enumerable: true,
57
+ configurable: true,
58
+ writable: true,
59
+ value: apiHash
60
+ });
49
61
  Object.defineProperty(this, "sessionId", {
50
62
  enumerable: true,
51
63
  configurable: true,
@@ -76,29 +88,144 @@ class Client extends client_abstract_js_1.ClientAbstract {
76
88
  writable: true,
77
89
  value: null
78
90
  });
91
+ Object.defineProperty(this, "appVersion", {
92
+ enumerable: true,
93
+ configurable: true,
94
+ writable: true,
95
+ value: void 0
96
+ });
97
+ Object.defineProperty(this, "deviceModel", {
98
+ enumerable: true,
99
+ configurable: true,
100
+ writable: true,
101
+ value: void 0
102
+ });
103
+ Object.defineProperty(this, "langCode", {
104
+ enumerable: true,
105
+ configurable: true,
106
+ writable: true,
107
+ value: void 0
108
+ });
109
+ Object.defineProperty(this, "langPack", {
110
+ enumerable: true,
111
+ configurable: true,
112
+ writable: true,
113
+ value: void 0
114
+ });
115
+ Object.defineProperty(this, "systemLangCode", {
116
+ enumerable: true,
117
+ configurable: true,
118
+ writable: true,
119
+ value: void 0
120
+ });
121
+ Object.defineProperty(this, "systemVersion", {
122
+ enumerable: true,
123
+ configurable: true,
124
+ writable: true,
125
+ value: void 0
126
+ });
127
+ Object.defineProperty(this, "sessionLoaded", {
128
+ enumerable: true,
129
+ configurable: true,
130
+ writable: true,
131
+ value: false
132
+ });
133
+ this.appVersion = params?.appVersion ?? constants_js_1.DEFAULT_APP_VERSION;
134
+ this.deviceModel = params?.deviceModel ?? constants_js_1.DEFAULT_DEVICE_MODEL;
135
+ this.langCode = params?.langCode ?? constants_js_1.DEFAULT_LANG_CODE;
136
+ this.langPack = params?.langPack ?? constants_js_1.DEFAULT_LANG_PACK;
137
+ this.systemLangCode = params?.systemLangCode ?? constants_js_1.DEFAULT_SYSTEM_LANG_CODE;
138
+ this.systemVersion = params?.systemVersion ?? constants_js_1.DEFAULT_SYSTEM_VERSION;
139
+ }
140
+ setDc(dc) {
141
+ if (this.session.dc != dc) {
142
+ this.session.dc = dc;
143
+ this.session.authKey = null;
144
+ }
145
+ super.setDc(dc);
79
146
  }
80
147
  async connect() {
81
- await this.session.load();
148
+ if (!this.sessionLoaded) {
149
+ await this.session.load();
150
+ this.sessionLoaded = true;
151
+ }
82
152
  if (this.session.authKey == null) {
83
153
  const plain = new client_plain_js_1.ClientPlain(this.transportProvider);
154
+ if (this.session.dc != null) {
155
+ plain.setDc(this.session.dc);
156
+ }
84
157
  await plain.connect();
85
158
  const { authKey, salt } = await plain.createAuthKey();
86
159
  await plain.disconnect();
87
160
  this.state.salt = salt;
88
161
  this.session.authKey = authKey;
89
- await this.session.save();
90
162
  }
91
163
  if (this.session.dc != null) {
92
- const { connection, transport, dcId } = this.transportProvider({ dc: this.session.dc, cdn: false });
93
- this.connection = connection;
94
- this.transport = transport;
95
- this.dcId = dcId;
164
+ this.setDc(this.session.dc);
96
165
  }
166
+ await this.session.save();
97
167
  await super.connect();
98
168
  // logger().debug("Client connected");
99
169
  this.receiveLoop();
100
170
  this.pingLoop();
101
171
  }
172
+ async authorize(params) {
173
+ if (!this.apiId) {
174
+ throw new Error("apiId not set");
175
+ }
176
+ if (!this.apiHash) {
177
+ throw new Error("apiHash not set");
178
+ }
179
+ await this.invoke(new functions.InitConnection({
180
+ apiId: this.apiId,
181
+ appVersion: this.appVersion,
182
+ deviceModel: this.deviceModel,
183
+ langCode: this.langCode,
184
+ langPack: this.langPack,
185
+ query: new functions.InvokeWithLayer({
186
+ layer: constants_js_1.LAYER,
187
+ query: new functions.HelpGetConfig(),
188
+ }),
189
+ systemLangCode: this.systemLangCode,
190
+ systemVersion: this.systemVersion,
191
+ }));
192
+ try {
193
+ await this.invoke(new functions.UpdatesGetState());
194
+ return;
195
+ }
196
+ catch (err) {
197
+ if (!(err instanceof types.RPCError) || err.errorMessage != "AUTH_KEY_UNREGISTERED") {
198
+ throw err;
199
+ }
200
+ }
201
+ try {
202
+ if (typeof params == "object") {
203
+ throw new Error("Not implemented");
204
+ }
205
+ else {
206
+ await this.invoke(new functions.AuthImportBotAuthorization({ apiId: this.apiId, apiHash: this.apiHash, botAuthToken: params, flags: 0 }));
207
+ }
208
+ }
209
+ catch (err) {
210
+ if (err instanceof types.RPCError) {
211
+ const match = err.errorMessage.match(/MIGRATE_(\d)$/);
212
+ if (match) {
213
+ let newDc = match[1];
214
+ if (Math.abs(this.dcId) >= 10000) {
215
+ newDc += "-test";
216
+ }
217
+ await this.reconnect(newDc);
218
+ await this.authorize(params);
219
+ }
220
+ else {
221
+ throw err;
222
+ }
223
+ }
224
+ else {
225
+ throw err;
226
+ }
227
+ }
228
+ }
102
229
  async receiveLoop() {
103
230
  if (!this.session.authKey) {
104
231
  throw new Error("Not connected");
@@ -108,7 +235,18 @@ class Client extends client_abstract_js_1.ClientAbstract {
108
235
  await this.send(new types.MsgsAck({ msgIds: [...this.toAcknowledge] }));
109
236
  this.toAcknowledge.clear();
110
237
  }
111
- const buffer = await this.transport.receive();
238
+ let buffer;
239
+ try {
240
+ buffer = await this.transport.receive();
241
+ }
242
+ catch (err) {
243
+ if (!this.connected) {
244
+ break;
245
+ }
246
+ else {
247
+ throw err;
248
+ }
249
+ }
112
250
  let decrypted;
113
251
  try {
114
252
  decrypted = await (0, _1_message_js_1.decryptMessage)(buffer, this.session.authKey, this.sessionId);
@@ -2,9 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ClientAbstract = void 0;
4
4
  const deps_js_1 = require("../deps.js");
5
+ const constants_js_1 = require("../constants.js");
5
6
  const transport_provider_js_1 = require("../transport/transport_provider.js");
6
7
  class ClientAbstract {
7
- constructor(transportProvider = (0, transport_provider_js_1.defaultTransportProvider)()) {
8
+ constructor(transportProvider = (0, transport_provider_js_1.defaultTransportProvider)({ initialDc: constants_js_1.DEFAULT_INITIAL_DC })) {
8
9
  Object.defineProperty(this, "transportProvider", {
9
10
  enumerable: true,
10
11
  configurable: true,
@@ -23,7 +24,7 @@ class ClientAbstract {
23
24
  writable: true,
24
25
  value: void 0
25
26
  });
26
- Object.defineProperty(this, "dcId", {
27
+ Object.defineProperty(this, "_dcId", {
27
28
  enumerable: true,
28
29
  configurable: true,
29
30
  writable: true,
@@ -35,10 +36,19 @@ class ClientAbstract {
35
36
  writable: true,
36
37
  value: false
37
38
  });
38
- const { connection, transport, dcId } = transportProvider({ dc: transport_provider_js_1.defaultDc, cdn: false });
39
+ const { connection, transport, dcId } = transportProvider({ cdn: false });
39
40
  this.connection = connection;
40
41
  this.transport = transport;
41
- this.dcId = dcId;
42
+ this._dcId = dcId;
43
+ }
44
+ get dcId() {
45
+ return this._dcId;
46
+ }
47
+ setDc(dc) {
48
+ const { connection, transport, dcId } = this.transportProvider({ dc, cdn: false });
49
+ this.connection = connection;
50
+ this.transport = transport;
51
+ this._dcId = dcId;
42
52
  }
43
53
  async connect() {
44
54
  await (0, deps_js_1.initTgCrypto)();
@@ -46,6 +56,13 @@ class ClientAbstract {
46
56
  await this.transport.initialize();
47
57
  this.connected = true;
48
58
  }
59
+ async reconnect(dc) {
60
+ await this.disconnect();
61
+ if (dc) {
62
+ this.setDc(dc);
63
+ }
64
+ await this.connect();
65
+ }
49
66
  async disconnect() {
50
67
  await this.transport.deinitialize();
51
68
  await this.connection.close();
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.VECTOR_CONSTRUCTOR = exports.publicKeys = exports.ackThreshold = void 0;
3
+ exports.DEFAULT_SYSTEM_VERSION = exports.DEFAULT_SYSTEM_LANG_CODE = exports.DEFAULT_LANG_PACK = exports.DEFAULT_LANG_CODE = exports.DEFAULT_DEVICE_MODEL = exports.DEFAULT_APP_VERSION = exports.LAYER = exports.DEFAULT_INITIAL_DC = exports.VECTOR_CONSTRUCTOR = exports.publicKeys = exports.ackThreshold = void 0;
4
4
  exports.ackThreshold = 10;
5
5
  exports.publicKeys = new Map([
6
6
  [
@@ -62,3 +62,12 @@ exports.publicKeys = new Map([
62
62
  ],
63
63
  ]);
64
64
  exports.VECTOR_CONSTRUCTOR = 0x1CB5C415;
65
+ exports.DEFAULT_INITIAL_DC = "2-test";
66
+ exports.LAYER = 158;
67
+ // TODO
68
+ exports.DEFAULT_APP_VERSION = "MTKruto Unstable <v1.0.0";
69
+ exports.DEFAULT_DEVICE_MODEL = "Krutaya Device";
70
+ exports.DEFAULT_LANG_CODE = "en";
71
+ exports.DEFAULT_LANG_PACK = "";
72
+ exports.DEFAULT_SYSTEM_LANG_CODE = "en";
73
+ exports.DEFAULT_SYSTEM_VERSION = "1.0";
package/script/mod.js CHANGED
@@ -26,7 +26,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
26
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.functions = exports.types = exports.utils = void 0;
29
+ exports.LAYER = exports.DEFAULT_SYSTEM_VERSION = exports.DEFAULT_SYSTEM_LANG_CODE = exports.DEFAULT_LANG_PACK = exports.DEFAULT_LANG_CODE = exports.DEFAULT_INITIAL_DC = exports.DEFAULT_DEVICE_MODEL = exports.DEFAULT_APP_VERSION = exports.functions = exports.types = exports.utils = void 0;
30
30
  const _1_password_js_1 = require("./utilities/1_password.js");
31
31
  const _0_bigint_js_1 = require("./utilities/0_bigint.js");
32
32
  exports.utils = { checkPassword: _1_password_js_1.checkPassword, getRandomId: _0_bigint_js_1.getRandomId };
@@ -46,3 +46,12 @@ __exportStar(require("./transport/transport.js"), exports);
46
46
  __exportStar(require("./transport/transport_provider.js"), exports);
47
47
  __exportStar(require("./connection/connection.js"), exports);
48
48
  __exportStar(require("./connection/connection_web_socket.js"), exports);
49
+ var constants_js_1 = require("./constants.js");
50
+ Object.defineProperty(exports, "DEFAULT_APP_VERSION", { enumerable: true, get: function () { return constants_js_1.DEFAULT_APP_VERSION; } });
51
+ Object.defineProperty(exports, "DEFAULT_DEVICE_MODEL", { enumerable: true, get: function () { return constants_js_1.DEFAULT_DEVICE_MODEL; } });
52
+ Object.defineProperty(exports, "DEFAULT_INITIAL_DC", { enumerable: true, get: function () { return constants_js_1.DEFAULT_INITIAL_DC; } });
53
+ Object.defineProperty(exports, "DEFAULT_LANG_CODE", { enumerable: true, get: function () { return constants_js_1.DEFAULT_LANG_CODE; } });
54
+ Object.defineProperty(exports, "DEFAULT_LANG_PACK", { enumerable: true, get: function () { return constants_js_1.DEFAULT_LANG_PACK; } });
55
+ Object.defineProperty(exports, "DEFAULT_SYSTEM_LANG_CODE", { enumerable: true, get: function () { return constants_js_1.DEFAULT_SYSTEM_LANG_CODE; } });
56
+ Object.defineProperty(exports, "DEFAULT_SYSTEM_VERSION", { enumerable: true, get: function () { return constants_js_1.DEFAULT_SYSTEM_VERSION; } });
57
+ Object.defineProperty(exports, "LAYER", { enumerable: true, get: function () { return constants_js_1.LAYER; } });
@@ -1,9 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.defaultTransportProvider = exports.defaultDc = void 0;
3
+ exports.defaultTransportProvider = void 0;
4
4
  const connection_web_socket_js_1 = require("../connection/connection_web_socket.js");
5
5
  const transport_intermediate_js_1 = require("./transport_intermediate.js");
6
- exports.defaultDc = "2-test";
7
6
  const dcToNameMap = {
8
7
  "1": "pluto",
9
8
  "1-test": "pluto",
@@ -14,8 +13,9 @@ const dcToNameMap = {
14
13
  "4": "vesta",
15
14
  "5": "flora",
16
15
  };
17
- function defaultTransportProvider(wss) {
16
+ const defaultTransportProvider = ({ initialDc, wss }) => {
18
17
  return ({ dc, cdn }) => {
18
+ dc ??= initialDc;
19
19
  wss ??= typeof location !== "undefined" && location.protocol == "http:" ? false : true;
20
20
  const url = `${wss ? "wss" : "ws"}://${dcToNameMap[dc]}${cdn ? "-1" : ""}.web.telegram.org/${dc.endsWith("-test") ? "apiws_test" : "apiws"}`;
21
21
  const connection = new connection_web_socket_js_1.ConnectionWebSocket(url);
@@ -23,5 +23,5 @@ function defaultTransportProvider(wss) {
23
23
  const dcId = Number(dc[0]) + (dc.endsWith("-test") ? 10000 : 0) * (cdn ? -1 : 1);
24
24
  return { connection, transport, dcId };
25
25
  };
26
- }
26
+ };
27
27
  exports.defaultTransportProvider = defaultTransportProvider;
@@ -3,19 +3,45 @@ import * as types from "../tl/2_types.js";
3
3
  import * as functions from "../tl/3_functions.js";
4
4
  import { ClientAbstract } from "./client_abstract.js";
5
5
  import { Session } from "../session/session.js";
6
- import { TransportProvider } from "../transport/transport_provider.js";
6
+ import { DC, TransportProvider } from "../transport/transport_provider.js";
7
+ export interface AuthorizeUserParams<S = string, N = {
8
+ first: S;
9
+ last: S;
10
+ }> {
11
+ phone: S | (() => MaybePromise<S>);
12
+ code: S | (() => MaybePromise<S>);
13
+ password: S | (() => MaybePromise<S>);
14
+ names: N | (() => MaybePromise<N>);
15
+ }
7
16
  export type UpdatesHandler = null | ((client: Client, update: types.Updates) => MaybePromise<void>);
8
17
  export declare class Client extends ClientAbstract {
9
18
  readonly session: Session;
19
+ readonly apiId: number;
20
+ readonly apiHash: string;
10
21
  private sessionId;
11
22
  private state;
12
23
  private promises;
13
24
  private toAcknowledge;
14
25
  updatesHandler: UpdatesHandler;
15
- constructor(session?: Session, params?: {
26
+ readonly appVersion: string;
27
+ readonly deviceModel: string;
28
+ readonly langCode: string;
29
+ readonly langPack: string;
30
+ readonly systemLangCode: string;
31
+ readonly systemVersion: string;
32
+ constructor(session?: Session, apiId?: number, apiHash?: string, params?: {
16
33
  transportProvider?: TransportProvider;
34
+ appVersion?: string;
35
+ deviceModel?: string;
36
+ langCode?: string;
37
+ langPack?: string;
38
+ systemLangCode?: string;
39
+ systemVersion?: string;
17
40
  });
41
+ setDc(dc: DC): void;
42
+ private sessionLoaded;
18
43
  connect(): Promise<void>;
44
+ authorize(params: string | AuthorizeUserParams): Promise<void>;
19
45
  private receiveLoop;
20
46
  private pingLoop;
21
47
  invoke<T extends (functions.Function<unknown> | types.Type) = functions.Function<unknown>>(function_: T): Promise<T extends functions.Function<unknown> ? T["__R"] : void>;
@@ -1,12 +1,16 @@
1
1
  import { Connection } from "../connection/connection.js";
2
2
  import { Transport } from "../transport/transport.js";
3
+ import { DC } from "../transport/transport_provider.js";
3
4
  export declare abstract class ClientAbstract {
4
5
  protected transportProvider: import("../transport/transport_provider.js").TransportProvider;
5
6
  protected connection: Connection;
6
7
  protected transport: Transport;
7
- dcId: number;
8
+ private _dcId;
8
9
  protected connected: boolean;
9
10
  constructor(transportProvider?: import("../transport/transport_provider.js").TransportProvider);
11
+ get dcId(): number;
12
+ setDc(dc: DC): void;
10
13
  connect(): Promise<void>;
14
+ reconnect(dc?: DC): Promise<void>;
11
15
  disconnect(): Promise<void>;
12
16
  }
@@ -1,3 +1,12 @@
1
+ import { DC } from "./transport/transport_provider.js";
1
2
  export declare const ackThreshold = 10;
2
3
  export declare const publicKeys: Map<bigint, [bigint, bigint]>;
3
4
  export declare const VECTOR_CONSTRUCTOR = 481674261;
5
+ export declare const DEFAULT_INITIAL_DC: DC;
6
+ export declare const LAYER = 158;
7
+ export declare const DEFAULT_APP_VERSION = "MTKruto Unstable <v1.0.0";
8
+ export declare const DEFAULT_DEVICE_MODEL = "Krutaya Device";
9
+ export declare const DEFAULT_LANG_CODE = "en";
10
+ export declare const DEFAULT_LANG_PACK = "";
11
+ export declare const DEFAULT_SYSTEM_LANG_CODE = "en";
12
+ export declare const DEFAULT_SYSTEM_VERSION = "1.0";
package/types/mod.d.ts CHANGED
@@ -20,3 +20,4 @@ export * from "./transport/transport.js";
20
20
  export * from "./transport/transport_provider.js";
21
21
  export * from "./connection/connection.js";
22
22
  export * from "./connection/connection_web_socket.js";
23
+ export { DEFAULT_APP_VERSION, DEFAULT_DEVICE_MODEL, DEFAULT_INITIAL_DC, DEFAULT_LANG_CODE, DEFAULT_LANG_PACK, DEFAULT_SYSTEM_LANG_CODE, DEFAULT_SYSTEM_VERSION, LAYER } from "./constants.js";
@@ -1,7 +1,7 @@
1
1
  import { MaybePromise } from "../types.js";
2
- import { TransportProviderParams } from "../transport/transport_provider.js";
2
+ import { DC } from "../transport/transport_provider.js";
3
3
  export declare abstract class Session {
4
- dc: TransportProviderParams["dc"] | null;
4
+ dc: DC | null;
5
5
  authKey: Uint8Array | null;
6
6
  abstract load(): MaybePromise<void>;
7
7
  abstract save(): MaybePromise<void>;
@@ -1,7 +1,8 @@
1
1
  import { Connection } from "../connection/connection.js";
2
2
  import { Transport } from "./transport.js";
3
+ export type DC = "1" | "2" | "3" | "4" | "5" | "1-test" | "2-test" | "3-test";
3
4
  export interface TransportProviderParams {
4
- dc: "1" | "2" | "3" | "4" | "5" | "1-test" | "2-test" | "3-test";
5
+ dc?: DC;
5
6
  cdn: boolean;
6
7
  }
7
8
  export type TransportProvider = (params: TransportProviderParams) => {
@@ -9,5 +10,8 @@ export type TransportProvider = (params: TransportProviderParams) => {
9
10
  transport: Transport;
10
11
  dcId: number;
11
12
  };
12
- export declare const defaultDc: TransportProviderParams["dc"];
13
- export declare function defaultTransportProvider(wss?: boolean): TransportProvider;
13
+ export interface TransportProviderCreatorParams {
14
+ initialDc: DC;
15
+ }
16
+ export type TransportProviderCreator = (params: TransportProviderCreatorParams) => TransportProvider;
17
+ export declare const defaultTransportProvider: TransportProviderCreator;