@mtkruto/node 0.0.63 → 0.0.71

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,18 +1,26 @@
1
1
  import { gunzip } from "../deps.js";
2
2
  import { ackThreshold } from "../constants.js";
3
- import { getRandomBigInt } from "../utilities/0_bigint.js";
3
+ import { bigIntFromBuffer, getRandomBigInt } from "../utilities/0_bigint.js";
4
+ import { sha1 } from "../utilities/0_hash.js";
4
5
  import { decryptMessage, encryptMessage, getMessageId } from "../utilities/1_message.js";
5
- import { BadMsgNotification, BadServerSalt, GZIPPacked, MsgsAck, Pong, RPCError, Updates } from "../tl/2_types.js";
6
- import { Ping } from "../tl/3_functions.js";
6
+ import * as types from "../tl/2_types.js";
7
+ import * as functions from "../tl/3_functions.js";
7
8
  import { TLReader } from "../tl/3_tl_reader.js";
8
9
  import { RPCResult } from "../tl/4_rpc_result.js";
9
10
  import { Message } from "../tl/5_message.js";
10
11
  import { MessageContainer } from "../tl/6_message_container.js";
11
12
  import { ClientAbstract } from "./client_abstract.js";
12
13
  import { ClientPlain } from "./client_plain.js";
14
+ import { SessionMemory } from "../session/session_memory.js";
13
15
  export class Client extends ClientAbstract {
14
- constructor() {
15
- super(...arguments);
16
+ constructor(session = new SessionMemory(), params) {
17
+ super(params?.transportProvider);
18
+ Object.defineProperty(this, "session", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: session
23
+ });
16
24
  Object.defineProperty(this, "sessionId", {
17
25
  enumerable: true,
18
26
  configurable: true,
@@ -51,12 +59,29 @@ export class Client extends ClientAbstract {
51
59
  });
52
60
  }
53
61
  async connect() {
54
- const plain = new ClientPlain(this.transportProvider);
55
- await plain.connect();
56
- const { authKey: key, authKeyId: id, salt } = await plain.createAuthKey();
57
- await plain.disconnect();
62
+ await this.session.load();
63
+ let key;
64
+ let id;
65
+ if (this.session.authKey != null) {
66
+ key = this.session.authKey;
67
+ id = bigIntFromBuffer((await sha1(key)).slice(-8), true, false);
68
+ }
69
+ else {
70
+ const plain = new ClientPlain(this.transportProvider);
71
+ await plain.connect();
72
+ const { authKey, authKeyId, salt } = await plain.createAuthKey();
73
+ await plain.disconnect();
74
+ key = authKey;
75
+ id = authKeyId;
76
+ this.state.salt = salt;
77
+ }
58
78
  this.auth = { key, id };
59
- this.state.salt = salt;
79
+ if (this.session.dc != null) {
80
+ const { connection, transport, dcId } = this.transportProvider({ dc: this.session.dc, cdn: false });
81
+ this.connection = connection;
82
+ this.transport = transport;
83
+ this.dcId = dcId;
84
+ }
60
85
  await super.connect();
61
86
  // logger().debug("Client connected");
62
87
  this.receiveLoop();
@@ -68,7 +93,7 @@ export class Client extends ClientAbstract {
68
93
  }
69
94
  while (this.connected) {
70
95
  if (this.toAcknowledge.size >= ackThreshold) {
71
- await this.send(new MsgsAck({ msgIds: [...this.toAcknowledge] }));
96
+ await this.send(new types.MsgsAck({ msgIds: [...this.toAcknowledge] }));
72
97
  this.toAcknowledge.clear();
73
98
  }
74
99
  const buffer = await this.transport.receive();
@@ -83,21 +108,21 @@ export class Client extends ClientAbstract {
83
108
  const messages = decrypted instanceof MessageContainer ? decrypted.messages : [decrypted];
84
109
  for (const message of messages) {
85
110
  let body = message.body;
86
- if (body instanceof GZIPPacked) {
111
+ if (body instanceof types.GZIPPacked) {
87
112
  body = new TLReader(gunzip(body.packedData)).readObject();
88
113
  }
89
114
  // logger().debug(`Received ${body.constructor.name}`);
90
- if (body instanceof Updates) {
115
+ if (body instanceof types.Updates) {
91
116
  this.updatesHandler?.(this, body);
92
117
  }
93
118
  else if (message.body instanceof RPCResult) {
94
119
  let result = message.body.result;
95
- if (result instanceof GZIPPacked) {
120
+ if (result instanceof types.GZIPPacked) {
96
121
  result = new TLReader(gunzip(result.packedData)).readObject();
97
122
  }
98
123
  const promise = this.promises.get(message.body.messageId);
99
124
  if (promise) {
100
- if (result instanceof RPCError) {
125
+ if (result instanceof types.RPCError) {
101
126
  promise.reject(result);
102
127
  }
103
128
  else {
@@ -106,15 +131,15 @@ export class Client extends ClientAbstract {
106
131
  this.promises.delete(message.body.messageId);
107
132
  }
108
133
  }
109
- else if (message.body instanceof Pong) {
134
+ else if (message.body instanceof types.Pong) {
110
135
  const promise = this.promises.get(message.body.msgId);
111
136
  if (promise) {
112
137
  promise.resolve(message.body);
113
138
  this.promises.delete(message.body.msgId);
114
139
  }
115
140
  }
116
- else if (message.body instanceof BadMsgNotification || message.body instanceof BadServerSalt) {
117
- if (message.body instanceof BadServerSalt) {
141
+ else if (message.body instanceof types.BadMsgNotification || message.body instanceof types.BadServerSalt) {
142
+ if (message.body instanceof types.BadServerSalt) {
118
143
  this.state.salt = message.body.newServerSalt;
119
144
  }
120
145
  const promise = this.promises.get(message.body.badMsgId);
@@ -130,7 +155,7 @@ export class Client extends ClientAbstract {
130
155
  async pingLoop() {
131
156
  while (this.connected) {
132
157
  try {
133
- await this.invoke(new Ping({ pingId: getRandomBigInt(8, true, false) }));
158
+ await this.invoke(new functions.Ping({ pingId: getRandomBigInt(8, true, false) }));
134
159
  }
135
160
  catch (_err) {
136
161
  // logger().error(`Failed to invoke ping: ${err}`);
@@ -143,7 +168,7 @@ export class Client extends ClientAbstract {
143
168
  throw new Error("Not connected");
144
169
  }
145
170
  let seqNo = this.state.seqNo * 2;
146
- if (!(function_ instanceof Ping) && !(function_ instanceof MsgsAck)) {
171
+ if (!(function_ instanceof functions.Ping) && !(function_ instanceof types.MsgsAck)) {
147
172
  seqNo++;
148
173
  this.state.seqNo++;
149
174
  }
@@ -156,7 +181,7 @@ export class Client extends ClientAbstract {
156
181
  const result = await new Promise((resolve, reject) => {
157
182
  this.promises.set(message.id, { resolve, reject });
158
183
  });
159
- if (result instanceof BadServerSalt) {
184
+ if (result instanceof types.BadServerSalt) {
160
185
  return await this.invoke(function_);
161
186
  }
162
187
  else {
@@ -1,5 +1,5 @@
1
- import { defaultTransportProvider } from "../transport/transport_provider.js";
2
1
  import { initTgCrypto } from "../deps.js";
2
+ import { defaultDc, defaultTransportProvider } from "../transport/transport_provider.js";
3
3
  export class ClientAbstract {
4
4
  constructor(transportProvider = defaultTransportProvider()) {
5
5
  Object.defineProperty(this, "transportProvider", {
@@ -32,7 +32,7 @@ export class ClientAbstract {
32
32
  writable: true,
33
33
  value: false
34
34
  });
35
- const { connection, transport, dcId } = transportProvider(false);
35
+ const { connection, transport, dcId } = transportProvider({ dc: defaultDc, cdn: false });
36
36
  this.connection = connection;
37
37
  this.transport = transport;
38
38
  this.dcId = dcId;
package/esm/mod.js CHANGED
@@ -3,10 +3,17 @@ import { getRandomId } from "./utilities/0_bigint.js";
3
3
  export const utils = { checkPassword, getRandomId };
4
4
  export * as types from "./tl/2_types.js";
5
5
  export * as functions from "./tl/3_functions.js";
6
+ export * from "./tl/4_rpc_result.js";
7
+ export * from "./tl/5_message.js";
8
+ export * from "./tl/6_message_container.js";
6
9
  export * from "./client/client_plain.js";
7
10
  export * from "./client/client.js";
11
+ export * from "./session/session.js";
12
+ export * from "./session/session_memory.js";
13
+ export * from "./session/session_local_storage.js";
8
14
  export * from "./transport/transport_abridged.js";
9
15
  export * from "./transport/transport_intermediate.js";
10
16
  export * from "./transport/transport.js";
11
17
  export * from "./transport/transport_provider.js";
12
18
  export * from "./connection/connection.js";
19
+ export * from "./connection/connection_web_socket.js";
@@ -0,0 +1,16 @@
1
+ export class Session {
2
+ constructor() {
3
+ Object.defineProperty(this, "dc", {
4
+ enumerable: true,
5
+ configurable: true,
6
+ writable: true,
7
+ value: null
8
+ });
9
+ Object.defineProperty(this, "authKey", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: null
14
+ });
15
+ }
16
+ }
@@ -0,0 +1,26 @@
1
+ import { Session } from "./session.js";
2
+ export class SessionLocalStorage extends Session {
3
+ constructor(key) {
4
+ if (typeof localStorage == "undefined") {
5
+ throw new Error("Unavailable in current environment");
6
+ }
7
+ super();
8
+ Object.defineProperty(this, "key", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: key
13
+ });
14
+ }
15
+ load() {
16
+ const { dc = null, authKey = null } = JSON.parse(localStorage.getItem(this.key) ?? "{}");
17
+ this.dc = dc;
18
+ if (authKey != null) {
19
+ this.authKey = new Uint8Array(authKey.split(/([0-9a-f]{2})/).filter((v) => v).map((v) => parseInt(v, 16)));
20
+ }
21
+ }
22
+ save() {
23
+ const authKey = this.authKey == null ? undefined : Array.from(this.authKey).map((v) => v.toString(16)).map((v) => v.padStart(2, "0"));
24
+ localStorage.setItem(this.key, JSON.stringify({ dc: this.dc, authKey }));
25
+ }
26
+ }
@@ -0,0 +1,5 @@
1
+ import { Session } from "./session.js";
2
+ export class SessionMemory extends Session {
3
+ load() { }
4
+ save() { }
5
+ }
@@ -11,13 +11,13 @@ const dcToNameMap = {
11
11
  "4": "vesta",
12
12
  "5": "flora",
13
13
  };
14
- export const defaultTransportProvider = (params) => {
15
- return (cdn) => {
16
- const { dc, wss = typeof location !== "undefined" && location.protocol == "http:" ? false : true } = params ?? { dc: defaultDc };
14
+ export function defaultTransportProvider(wss) {
15
+ return ({ dc, cdn }) => {
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);
19
19
  const transport = new TransportIntermediate(connection, true);
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.63",
6
+ "version": "0.0.71",
7
7
  "description": "MTKruto for Node.js",
8
8
  "author": "Roj <rojvv@icloud.com>",
9
9
  "license": "LGPL-3.0-or-later",
@@ -1,21 +1,52 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
26
  exports.Client = void 0;
4
27
  const deps_js_1 = require("../deps.js");
5
28
  const constants_js_1 = require("../constants.js");
6
29
  const _0_bigint_js_1 = require("../utilities/0_bigint.js");
30
+ const _0_hash_js_1 = require("../utilities/0_hash.js");
7
31
  const _1_message_js_1 = require("../utilities/1_message.js");
8
- const _2_types_js_1 = require("../tl/2_types.js");
9
- const _3_functions_js_1 = require("../tl/3_functions.js");
32
+ const types = __importStar(require("../tl/2_types.js"));
33
+ const functions = __importStar(require("../tl/3_functions.js"));
10
34
  const _3_tl_reader_js_1 = require("../tl/3_tl_reader.js");
11
35
  const _4_rpc_result_js_1 = require("../tl/4_rpc_result.js");
12
36
  const _5_message_js_1 = require("../tl/5_message.js");
13
37
  const _6_message_container_js_1 = require("../tl/6_message_container.js");
14
38
  const client_abstract_js_1 = require("./client_abstract.js");
15
39
  const client_plain_js_1 = require("./client_plain.js");
40
+ const session_memory_js_1 = require("../session/session_memory.js");
16
41
  class Client extends client_abstract_js_1.ClientAbstract {
17
- constructor() {
18
- super(...arguments);
42
+ constructor(session = new session_memory_js_1.SessionMemory(), params) {
43
+ super(params?.transportProvider);
44
+ Object.defineProperty(this, "session", {
45
+ enumerable: true,
46
+ configurable: true,
47
+ writable: true,
48
+ value: session
49
+ });
19
50
  Object.defineProperty(this, "sessionId", {
20
51
  enumerable: true,
21
52
  configurable: true,
@@ -54,12 +85,29 @@ class Client extends client_abstract_js_1.ClientAbstract {
54
85
  });
55
86
  }
56
87
  async connect() {
57
- const plain = new client_plain_js_1.ClientPlain(this.transportProvider);
58
- await plain.connect();
59
- const { authKey: key, authKeyId: id, salt } = await plain.createAuthKey();
60
- await plain.disconnect();
88
+ await this.session.load();
89
+ let key;
90
+ let id;
91
+ if (this.session.authKey != null) {
92
+ key = this.session.authKey;
93
+ id = (0, _0_bigint_js_1.bigIntFromBuffer)((await (0, _0_hash_js_1.sha1)(key)).slice(-8), true, false);
94
+ }
95
+ else {
96
+ const plain = new client_plain_js_1.ClientPlain(this.transportProvider);
97
+ await plain.connect();
98
+ const { authKey, authKeyId, salt } = await plain.createAuthKey();
99
+ await plain.disconnect();
100
+ key = authKey;
101
+ id = authKeyId;
102
+ this.state.salt = salt;
103
+ }
61
104
  this.auth = { key, id };
62
- this.state.salt = salt;
105
+ if (this.session.dc != null) {
106
+ const { connection, transport, dcId } = this.transportProvider({ dc: this.session.dc, cdn: false });
107
+ this.connection = connection;
108
+ this.transport = transport;
109
+ this.dcId = dcId;
110
+ }
63
111
  await super.connect();
64
112
  // logger().debug("Client connected");
65
113
  this.receiveLoop();
@@ -71,7 +119,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
71
119
  }
72
120
  while (this.connected) {
73
121
  if (this.toAcknowledge.size >= constants_js_1.ackThreshold) {
74
- await this.send(new _2_types_js_1.MsgsAck({ msgIds: [...this.toAcknowledge] }));
122
+ await this.send(new types.MsgsAck({ msgIds: [...this.toAcknowledge] }));
75
123
  this.toAcknowledge.clear();
76
124
  }
77
125
  const buffer = await this.transport.receive();
@@ -86,21 +134,21 @@ class Client extends client_abstract_js_1.ClientAbstract {
86
134
  const messages = decrypted instanceof _6_message_container_js_1.MessageContainer ? decrypted.messages : [decrypted];
87
135
  for (const message of messages) {
88
136
  let body = message.body;
89
- if (body instanceof _2_types_js_1.GZIPPacked) {
137
+ if (body instanceof types.GZIPPacked) {
90
138
  body = new _3_tl_reader_js_1.TLReader((0, deps_js_1.gunzip)(body.packedData)).readObject();
91
139
  }
92
140
  // logger().debug(`Received ${body.constructor.name}`);
93
- if (body instanceof _2_types_js_1.Updates) {
141
+ if (body instanceof types.Updates) {
94
142
  this.updatesHandler?.(this, body);
95
143
  }
96
144
  else if (message.body instanceof _4_rpc_result_js_1.RPCResult) {
97
145
  let result = message.body.result;
98
- if (result instanceof _2_types_js_1.GZIPPacked) {
146
+ if (result instanceof types.GZIPPacked) {
99
147
  result = new _3_tl_reader_js_1.TLReader((0, deps_js_1.gunzip)(result.packedData)).readObject();
100
148
  }
101
149
  const promise = this.promises.get(message.body.messageId);
102
150
  if (promise) {
103
- if (result instanceof _2_types_js_1.RPCError) {
151
+ if (result instanceof types.RPCError) {
104
152
  promise.reject(result);
105
153
  }
106
154
  else {
@@ -109,15 +157,15 @@ class Client extends client_abstract_js_1.ClientAbstract {
109
157
  this.promises.delete(message.body.messageId);
110
158
  }
111
159
  }
112
- else if (message.body instanceof _2_types_js_1.Pong) {
160
+ else if (message.body instanceof types.Pong) {
113
161
  const promise = this.promises.get(message.body.msgId);
114
162
  if (promise) {
115
163
  promise.resolve(message.body);
116
164
  this.promises.delete(message.body.msgId);
117
165
  }
118
166
  }
119
- else if (message.body instanceof _2_types_js_1.BadMsgNotification || message.body instanceof _2_types_js_1.BadServerSalt) {
120
- if (message.body instanceof _2_types_js_1.BadServerSalt) {
167
+ else if (message.body instanceof types.BadMsgNotification || message.body instanceof types.BadServerSalt) {
168
+ if (message.body instanceof types.BadServerSalt) {
121
169
  this.state.salt = message.body.newServerSalt;
122
170
  }
123
171
  const promise = this.promises.get(message.body.badMsgId);
@@ -133,7 +181,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
133
181
  async pingLoop() {
134
182
  while (this.connected) {
135
183
  try {
136
- await this.invoke(new _3_functions_js_1.Ping({ pingId: (0, _0_bigint_js_1.getRandomBigInt)(8, true, false) }));
184
+ await this.invoke(new functions.Ping({ pingId: (0, _0_bigint_js_1.getRandomBigInt)(8, true, false) }));
137
185
  }
138
186
  catch (_err) {
139
187
  // logger().error(`Failed to invoke ping: ${err}`);
@@ -146,7 +194,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
146
194
  throw new Error("Not connected");
147
195
  }
148
196
  let seqNo = this.state.seqNo * 2;
149
- if (!(function_ instanceof _3_functions_js_1.Ping) && !(function_ instanceof _2_types_js_1.MsgsAck)) {
197
+ if (!(function_ instanceof functions.Ping) && !(function_ instanceof types.MsgsAck)) {
150
198
  seqNo++;
151
199
  this.state.seqNo++;
152
200
  }
@@ -159,7 +207,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
159
207
  const result = await new Promise((resolve, reject) => {
160
208
  this.promises.set(message.id, { resolve, reject });
161
209
  });
162
- if (result instanceof _2_types_js_1.BadServerSalt) {
210
+ if (result instanceof types.BadServerSalt) {
163
211
  return await this.invoke(function_);
164
212
  }
165
213
  else {
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ClientAbstract = void 0;
4
- const transport_provider_js_1 = require("../transport/transport_provider.js");
5
4
  const deps_js_1 = require("../deps.js");
5
+ const transport_provider_js_1 = require("../transport/transport_provider.js");
6
6
  class ClientAbstract {
7
7
  constructor(transportProvider = (0, transport_provider_js_1.defaultTransportProvider)()) {
8
8
  Object.defineProperty(this, "transportProvider", {
@@ -35,7 +35,7 @@ class ClientAbstract {
35
35
  writable: true,
36
36
  value: false
37
37
  });
38
- const { connection, transport, dcId } = transportProvider(false);
38
+ const { connection, transport, dcId } = transportProvider({ dc: transport_provider_js_1.defaultDc, cdn: false });
39
39
  this.connection = connection;
40
40
  this.transport = transport;
41
41
  this.dcId = dcId;
package/script/mod.js CHANGED
@@ -32,10 +32,17 @@ 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 };
33
33
  exports.types = __importStar(require("./tl/2_types.js"));
34
34
  exports.functions = __importStar(require("./tl/3_functions.js"));
35
+ __exportStar(require("./tl/4_rpc_result.js"), exports);
36
+ __exportStar(require("./tl/5_message.js"), exports);
37
+ __exportStar(require("./tl/6_message_container.js"), exports);
35
38
  __exportStar(require("./client/client_plain.js"), exports);
36
39
  __exportStar(require("./client/client.js"), exports);
40
+ __exportStar(require("./session/session.js"), exports);
41
+ __exportStar(require("./session/session_memory.js"), exports);
42
+ __exportStar(require("./session/session_local_storage.js"), exports);
37
43
  __exportStar(require("./transport/transport_abridged.js"), exports);
38
44
  __exportStar(require("./transport/transport_intermediate.js"), exports);
39
45
  __exportStar(require("./transport/transport.js"), exports);
40
46
  __exportStar(require("./transport/transport_provider.js"), exports);
41
47
  __exportStar(require("./connection/connection.js"), exports);
48
+ __exportStar(require("./connection/connection_web_socket.js"), exports);
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Session = void 0;
4
+ class Session {
5
+ constructor() {
6
+ Object.defineProperty(this, "dc", {
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true,
10
+ value: null
11
+ });
12
+ Object.defineProperty(this, "authKey", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: null
17
+ });
18
+ }
19
+ }
20
+ exports.Session = Session;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SessionLocalStorage = void 0;
4
+ const session_js_1 = require("./session.js");
5
+ class SessionLocalStorage extends session_js_1.Session {
6
+ constructor(key) {
7
+ if (typeof localStorage == "undefined") {
8
+ throw new Error("Unavailable in current environment");
9
+ }
10
+ super();
11
+ Object.defineProperty(this, "key", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: key
16
+ });
17
+ }
18
+ load() {
19
+ const { dc = null, authKey = null } = JSON.parse(localStorage.getItem(this.key) ?? "{}");
20
+ this.dc = dc;
21
+ if (authKey != null) {
22
+ this.authKey = new Uint8Array(authKey.split(/([0-9a-f]{2})/).filter((v) => v).map((v) => parseInt(v, 16)));
23
+ }
24
+ }
25
+ save() {
26
+ const authKey = this.authKey == null ? undefined : Array.from(this.authKey).map((v) => v.toString(16)).map((v) => v.padStart(2, "0"));
27
+ localStorage.setItem(this.key, JSON.stringify({ dc: this.dc, authKey }));
28
+ }
29
+ }
30
+ exports.SessionLocalStorage = SessionLocalStorage;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SessionMemory = void 0;
4
+ const session_js_1 = require("./session.js");
5
+ class SessionMemory extends session_js_1.Session {
6
+ load() { }
7
+ save() { }
8
+ }
9
+ exports.SessionMemory = SessionMemory;
@@ -14,14 +14,14 @@ const dcToNameMap = {
14
14
  "4": "vesta",
15
15
  "5": "flora",
16
16
  };
17
- const defaultTransportProvider = (params) => {
18
- return (cdn) => {
19
- const { dc, wss = typeof location !== "undefined" && location.protocol == "http:" ? false : true } = params ?? { dc: exports.defaultDc };
17
+ function defaultTransportProvider(wss) {
18
+ return ({ dc, cdn }) => {
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);
22
22
  const transport = new transport_intermediate_js_1.TransportIntermediate(connection, true);
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;
@@ -1,19 +1,25 @@
1
1
  import { MaybePromise } from "../types.js";
2
- import { Type, Updates } from "../tl/2_types.js";
3
- import { Function } from "../tl/3_functions.js";
2
+ import * as types from "../tl/2_types.js";
3
+ import * as functions from "../tl/3_functions.js";
4
4
  import { ClientAbstract } from "./client_abstract.js";
5
- export type UpdatesHandler = null | ((client: Client, update: Updates) => MaybePromise<void>);
5
+ import { Session } from "../session/session.js";
6
+ import { TransportProvider } from "../transport/transport_provider.js";
7
+ export type UpdatesHandler = null | ((client: Client, update: types.Updates) => MaybePromise<void>);
6
8
  export declare class Client extends ClientAbstract {
9
+ readonly session: Session;
7
10
  private sessionId;
8
11
  private auth?;
9
12
  private state;
10
13
  private promises;
11
14
  private toAcknowledge;
12
15
  updatesHandler: UpdatesHandler;
16
+ constructor(session?: Session, params?: {
17
+ transportProvider?: TransportProvider;
18
+ });
13
19
  connect(): Promise<void>;
14
20
  private receiveLoop;
15
21
  private pingLoop;
16
- invoke<T extends (Function<unknown> | Type) = Function<unknown>>(function_: T): Promise<T extends Function<unknown> ? T["__R"] : void>;
17
- invoke<T extends (Function<unknown> | Type) = Function<unknown>>(function_: T, noWait: true): Promise<void>;
18
- send<T extends (Function<unknown> | Type) = Function<unknown>>(function_: T): Promise<void>;
22
+ invoke<T extends (functions.Function<unknown> | types.Type) = functions.Function<unknown>>(function_: T): Promise<T extends functions.Function<unknown> ? T["__R"] : void>;
23
+ invoke<T extends (functions.Function<unknown> | types.Type) = functions.Function<unknown>>(function_: T, noWait: true): Promise<void>;
24
+ send<T extends (functions.Function<unknown> | types.Type) = functions.Function<unknown>>(function_: T): Promise<void>;
19
25
  }
@@ -1,20 +1,12 @@
1
1
  import { Connection } from "../connection/connection.js";
2
2
  import { Transport } from "../transport/transport.js";
3
3
  export declare abstract class ClientAbstract {
4
- protected transportProvider: (cdn: boolean) => {
5
- connection: Connection;
6
- transport: Transport;
7
- dcId: number;
8
- };
9
- connection: Connection;
10
- transport: Transport;
4
+ protected transportProvider: import("../transport/transport_provider.js").TransportProvider;
5
+ protected connection: Connection;
6
+ protected transport: Transport;
11
7
  dcId: number;
12
8
  protected connected: boolean;
13
- constructor(transportProvider?: (cdn: boolean) => {
14
- connection: Connection;
15
- transport: Transport;
16
- dcId: number;
17
- });
9
+ constructor(transportProvider?: import("../transport/transport_provider.js").TransportProvider);
18
10
  connect(): Promise<void>;
19
11
  disconnect(): Promise<void>;
20
12
  }
package/types/mod.d.ts CHANGED
@@ -6,10 +6,17 @@ export declare const utils: {
6
6
  };
7
7
  export * as types from "./tl/2_types.js";
8
8
  export * as functions from "./tl/3_functions.js";
9
+ export * from "./tl/4_rpc_result.js";
10
+ export * from "./tl/5_message.js";
11
+ export * from "./tl/6_message_container.js";
9
12
  export * from "./client/client_plain.js";
10
13
  export * from "./client/client.js";
14
+ export * from "./session/session.js";
15
+ export * from "./session/session_memory.js";
16
+ export * from "./session/session_local_storage.js";
11
17
  export * from "./transport/transport_abridged.js";
12
18
  export * from "./transport/transport_intermediate.js";
13
19
  export * from "./transport/transport.js";
14
20
  export * from "./transport/transport_provider.js";
15
21
  export * from "./connection/connection.js";
22
+ export * from "./connection/connection_web_socket.js";
@@ -0,0 +1,8 @@
1
+ import { MaybePromise } from "../types.js";
2
+ import { TransportProviderParams } from "../transport/transport_provider.js";
3
+ export declare abstract class Session {
4
+ dc: TransportProviderParams["dc"] | null;
5
+ authKey: Uint8Array | null;
6
+ abstract load(): MaybePromise<void>;
7
+ abstract save(): MaybePromise<void>;
8
+ }
@@ -0,0 +1,7 @@
1
+ import { Session } from "./session.js";
2
+ export declare class SessionLocalStorage extends Session implements Session {
3
+ private readonly key;
4
+ constructor(key: string);
5
+ load(): void;
6
+ save(): void;
7
+ }
@@ -0,0 +1,5 @@
1
+ import { Session } from "./session.js";
2
+ export declare class SessionMemory extends Session implements Session {
3
+ load(): void;
4
+ save(): void;
5
+ }
@@ -2,11 +2,12 @@ import { Connection } from "../connection/connection.js";
2
2
  import { Transport } from "./transport.js";
3
3
  export interface TransportProviderParams {
4
4
  dc: "1" | "2" | "3" | "4" | "5" | "1-test" | "2-test" | "3-test";
5
+ cdn: boolean;
5
6
  }
6
- export type TransportProvider = (params?: TransportProviderParams) => (cdn: boolean) => {
7
+ export type TransportProvider = (params: TransportProviderParams) => {
7
8
  connection: Connection;
8
9
  transport: Transport;
9
10
  dcId: number;
10
11
  };
11
12
  export declare const defaultDc: TransportProviderParams["dc"];
12
- export declare const defaultTransportProvider: TransportProvider;
13
+ export declare function defaultTransportProvider(wss?: boolean): TransportProvider;