@mtkruto/node 0.1.112 → 0.1.113

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.
@@ -10,9 +10,9 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _Client_instances, _a, _Client_auth, _Client_sessionId, _Client_state, _Client_promises, _Client_toAcknowledge, _Client_updateState, _Client_errorHandler, _Client_publicKeys, _Client_autoStart, _Client_propagateConnectionState, _Client_lastPropagatedConnectionState, _Client_storageInited, _Client_setAuth, _Client_authKeyWasCreated, _Client_connectMutex, _Client_assertUser, _Client_assertBot, _Client_fetchState, _Client_connectionInited, _Client_initConnection, _Client_lastPropagatedAuthorizationState, _Client_propagateAuthorizationState, _Client_selfId, _Client_getSelfId, _Client_receiveLoop, _Client_pingLoop, _Client_lastOutgoing, _Client_pingLoopStarted, _Client_autoStarted, _Client_lastMsgId, _Client_processChats, _Client_processUsers, _Client_handleUpdateQueue, _Client_processUpdatesQueue, _Client_checkChannelGap, _Client_processUpdates, _Client_setUpdateStateDate, _Client_getLocalState, _Client_recoverUpdateGap, _Client_recoverChannelUpdateGap, _Client_getChannelAccessHash, _Client_getInputPeerInner, _Client_updatesToMessages, _Client_resolveSendAs, _Client_parseText, _Client_getMessagesInner, _Client_downloadInner, _Client_handleUpdate, _Client_usernameResolver, _Client_constructReplyMarkup, _Client_assertMsgHas, _Client_handle, _Client_setMyInfo, _Client_getMyInfo;
13
+ var _Client_instances, _a, _Client_auth, _Client_sessionId, _Client_state, _Client_promises, _Client_toAcknowledge, _Client_updateState, _Client_publicKeys, _Client_autoStart, _Client_constructContext, _Client_propagateConnectionState, _Client_lastPropagatedConnectionState, _Client_storageInited, _Client_setAuth, _Client_authKeyWasCreated, _Client_connectMutex, _Client_assertUser, _Client_assertBot, _Client_fetchState, _Client_connectionInited, _Client_initConnection, _Client_lastPropagatedAuthorizationState, _Client_propagateAuthorizationState, _Client_selfId, _Client_getSelfId, _Client_receiveLoop, _Client_pingInterval, _Client_pingLoop, _Client_pingLoopStarted, _Client_autoStarted, _Client_lastMsgId, _Client_invoke, _Client_handleInvokeError, _Client_processChats, _Client_processUsers, _Client_handleUpdateQueue, _Client_processUpdatesQueue, _Client_checkChannelGap, _Client_processUpdates, _Client_setUpdateStateDate, _Client_getLocalState, _Client_recoverUpdateGap, _Client_recoverChannelUpdateGap, _Client_getChannelAccessHash, _Client_getInputPeerInner, _Client_updatesToMessages, _Client_resolveSendAs, _Client_parseText, _Client_getMessagesInner, _Client_downloadInner, _Client_handleUpdate, _Client_usernameResolver, _Client_constructReplyMarkup, _Client_assertMsgHas, _Client_handle, _Client_setMyInfo, _Client_getMyInfo;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.Client = exports.ConnectionError = exports.restartAuth = exports.handleMigrationError = void 0;
15
+ exports.Client = exports.ConnectionError = exports.restartAuth = exports.skipInvoke = exports.handleMigrationError = void 0;
16
16
  const _0_deps_js_1 = require("../0_deps.js");
17
17
  const _1_utilities_js_1 = require("../1_utilities.js");
18
18
  const _2_tl_js_1 = require("../2_tl.js");
@@ -26,7 +26,7 @@ const _0_password_js_1 = require("./0_password.js");
26
26
  const _0_utilities_js_1 = require("./0_utilities.js");
27
27
  const _1_client_abstract_js_1 = require("./1_client_abstract.js");
28
28
  const _2_client_plain_js_1 = require("./2_client_plain.js");
29
- const _3_types_js_2 = require("./3_types.js");
29
+ const _4_composer_js_1 = require("./4_composer.js");
30
30
  const d = (0, _0_deps_js_1.debug)("Client");
31
31
  const dGap = (0, _0_deps_js_1.debug)("Client/recoverUpdateGap");
32
32
  const dGapC = (0, _0_deps_js_1.debug)("Client/recoverChannelUpdateGap");
@@ -37,6 +37,10 @@ const getEntity = Symbol();
37
37
  const getStickerSetName = Symbol();
38
38
  exports.handleMigrationError = Symbol();
39
39
  const getMessageWithReply = Symbol();
40
+ function skipInvoke() {
41
+ return (_ctx, next) => next();
42
+ }
43
+ exports.skipInvoke = skipInvoke;
40
44
  exports.restartAuth = Symbol();
41
45
  class ConnectionError extends Error {
42
46
  }
@@ -70,7 +74,6 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
70
74
  _Client_promises.set(this, new Map());
71
75
  _Client_toAcknowledge.set(this, new Set());
72
76
  _Client_updateState.set(this, void 0);
73
- _Client_errorHandler.set(this, void 0);
74
77
  Object.defineProperty(this, "storage", {
75
78
  enumerable: true,
76
79
  configurable: true,
@@ -121,17 +124,81 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
121
124
  });
122
125
  _Client_publicKeys.set(this, void 0);
123
126
  _Client_autoStart.set(this, void 0);
127
+ _Client_constructContext.set(this, (update) => {
128
+ const msg = update.message ?? update.editedMessage ?? update.callbackQuery?.message;
129
+ const mustGetMsg = () => {
130
+ if (msg !== undefined) {
131
+ return msg;
132
+ }
133
+ else {
134
+ (0, _1_utilities_js_1.UNREACHABLE)();
135
+ }
136
+ };
137
+ const chat = msg?.chat;
138
+ const from = update.callbackQuery?.from ?? update.inlineQuery?.from ?? update.message?.from ?? update.editedMessage?.from;
139
+ const senderChat = msg?.senderChat;
140
+ return {
141
+ ...update,
142
+ client: this,
143
+ msg,
144
+ chat,
145
+ from,
146
+ senderChat,
147
+ reply: (text, params) => {
148
+ const effectiveMessage = mustGetMsg();
149
+ const shouldQuote = params?.quote === undefined ? effectiveMessage.chat.type != "private" : params.quote;
150
+ return this.sendMessage(effectiveMessage.chat.id, text, { ...params, replyToMessageId: shouldQuote ? effectiveMessage.id : undefined });
151
+ },
152
+ replyPoll: (question, options, params) => {
153
+ const effectiveMessage = mustGetMsg();
154
+ return this.sendPoll(effectiveMessage.chat.id, question, options, params);
155
+ },
156
+ answerCallbackQuery: (params) => {
157
+ const { callbackQuery } = update;
158
+ if (callbackQuery === undefined) {
159
+ (0, _1_utilities_js_1.UNREACHABLE)();
160
+ }
161
+ return this.answerCallbackQuery(callbackQuery.id, params);
162
+ },
163
+ answerInlineQuery: (results, params) => {
164
+ const { inlineQuery } = update;
165
+ if (inlineQuery == undefined) {
166
+ (0, _1_utilities_js_1.UNREACHABLE)();
167
+ }
168
+ return this.answerInlineQuery(inlineQuery.id, results, params);
169
+ },
170
+ sendChatAction: (chatAction, params) => {
171
+ const effectiveMessage = mustGetMsg();
172
+ return this.sendChatAction(effectiveMessage.chat.id, chatAction, params);
173
+ },
174
+ editMessageText: (messageId, text, params) => {
175
+ const effectiveMessage = mustGetMsg();
176
+ return this.editMessageText(effectiveMessage.chat.id, messageId, text, params);
177
+ },
178
+ getMessage: (messageId) => {
179
+ const effectiveMessage = mustGetMsg();
180
+ return this.getMessage(effectiveMessage.chat.id, messageId);
181
+ },
182
+ getMessages: (messageIds) => {
183
+ const effectiveMessage = mustGetMsg();
184
+ return this.getMessages(effectiveMessage.chat.id, messageIds);
185
+ },
186
+ get toJSON() {
187
+ return () => update;
188
+ },
189
+ };
190
+ });
124
191
  _Client_lastPropagatedConnectionState.set(this, null);
125
192
  Object.defineProperty(this, "stateChangeHandler", {
126
193
  enumerable: true,
127
194
  configurable: true,
128
195
  writable: true,
129
196
  value: ((connected) => {
130
- __classPrivateFieldGet(this, _Client_connectMutex, "f").acquire().then(async (release) => {
197
+ __classPrivateFieldGet(this, _Client_connectMutex, "f").acquire().then((release) => {
131
198
  try {
132
199
  const connectionState = connected ? "ready" : "notConnected";
133
200
  if (this.connected == connected && __classPrivateFieldGet(this, _Client_lastPropagatedConnectionState, "f") != connectionState) {
134
- await __classPrivateFieldGet(this, _Client_instances, "m", _Client_propagateConnectionState).call(this, connectionState);
201
+ __classPrivateFieldGet(this, _Client_instances, "m", _Client_propagateConnectionState).call(this, connectionState);
135
202
  __classPrivateFieldSet(this, _Client_lastPropagatedConnectionState, connectionState, "f");
136
203
  }
137
204
  }
@@ -147,17 +214,44 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
147
214
  _Client_connectionInited.set(this, false);
148
215
  _Client_lastPropagatedAuthorizationState.set(this, null);
149
216
  _Client_selfId.set(this, null);
150
- _Client_lastOutgoing.set(this, null);
217
+ _Client_pingInterval.set(this, 60 * 1000); // 60 seconds
151
218
  _Client_pingLoopStarted.set(this, false);
152
219
  _Client_autoStarted.set(this, false);
153
220
  _Client_lastMsgId.set(this, 0n);
221
+ _Client_handleInvokeError.set(this, skipInvoke());
222
+ /**
223
+ * Invokes a function waiting and returning its reply if the second parameter is not `true`. Requires the client
224
+ * to be connected.
225
+ *
226
+ * @param function_ The function to invoke.
227
+ */
228
+ Object.defineProperty(this, "invoke", {
229
+ enumerable: true,
230
+ configurable: true,
231
+ writable: true,
232
+ value: Object.assign(__classPrivateFieldGet(this, _Client_instances, "m", _Client_invoke), {
233
+ use: (handler) => {
234
+ const handle = __classPrivateFieldGet(this, _Client_handleInvokeError, "f");
235
+ __classPrivateFieldSet(this, _Client_handleInvokeError, async (ctx, next) => {
236
+ let result = null;
237
+ return await handle(ctx, async () => {
238
+ if (result != null)
239
+ return result;
240
+ result = await handler(ctx, next);
241
+ return result;
242
+ });
243
+ }, "f");
244
+ },
245
+ })
246
+ });
154
247
  _Client_handleUpdateQueue.set(this, new _1_utilities_js_1.Queue("handleUpdate"));
155
248
  _Client_processUpdatesQueue.set(this, new _1_utilities_js_1.Queue("processUpdates"));
156
249
  _Client_usernameResolver.set(this, async (v) => {
157
250
  const inputPeer = await this.getInputPeer(v).then((v) => v[_2_tl_js_1.as](_2_tl_js_1.types.InputPeerUser));
158
251
  return new _2_tl_js_1.types.InputUser({ userId: inputPeer.userId, accessHash: inputPeer.accessHash });
159
252
  });
160
- _Client_handle.set(this, _3_types_js_2.skip);
253
+ //#region Composer
254
+ _Client_handle.set(this, _4_composer_js_1.skip);
161
255
  this.storage = storage ?? new _3_storage_js_1.StorageMemory();
162
256
  this.parseMode = params?.parseMode ?? null;
163
257
  this.appVersion = params?.appVersion ?? _4_constants_js_1.APP_VERSION;
@@ -168,7 +262,41 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
168
262
  this.systemVersion = params?.systemVersion ?? _4_constants_js_1.SYSTEM_VERSION;
169
263
  __classPrivateFieldSet(this, _Client_publicKeys, params?.publicKeys, "f");
170
264
  __classPrivateFieldSet(this, _Client_autoStart, params?.autoStart ?? true, "f");
171
- __classPrivateFieldSet(this, _Client_errorHandler, params?.errorHandler, "f");
265
+ if (params?.defaultHandlers ?? true) {
266
+ this.on("connectionState", ({ connectionState }, next) => {
267
+ (0, _1_utilities_js_1.drop)((async () => {
268
+ if (connectionState == "notConnected") {
269
+ let delay = 5;
270
+ while (!this.connected) {
271
+ d("reconnecting");
272
+ try {
273
+ await this.connect();
274
+ d("reconnected");
275
+ break;
276
+ }
277
+ catch (err) {
278
+ d("failed to reconnect, retrying in %d: %o", delay, err);
279
+ }
280
+ await new Promise((r) => setTimeout(r, delay * 1000));
281
+ if (delay < 15) {
282
+ delay += 5;
283
+ }
284
+ }
285
+ }
286
+ })());
287
+ return next();
288
+ });
289
+ this.invoke.use(async ({ error }, next) => {
290
+ if (error instanceof _4_errors_js_1.FloodWait && error.seconds <= 10) {
291
+ d("sleeping for %d because of: %o", error.seconds, error);
292
+ await new Promise((r) => setTimeout(r, 1000 * error.seconds));
293
+ return true;
294
+ }
295
+ else {
296
+ return next();
297
+ }
298
+ });
299
+ }
172
300
  }
173
301
  /**
174
302
  * Sets the DC and resets the auth key stored in the session provider
@@ -235,14 +363,17 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
235
363
  }
236
364
  d("encrypted client connected");
237
365
  (0, _1_utilities_js_1.drop)(__classPrivateFieldGet(this, _Client_instances, "m", _Client_receiveLoop).call(this));
366
+ if (__classPrivateFieldGet(this, _Client_pingLoopStarted, "f")) {
367
+ (0, _1_utilities_js_1.drop)(__classPrivateFieldGet(this, _Client_instances, "m", _Client_pingLoop).call(this));
368
+ }
238
369
  }
239
370
  finally {
240
371
  release();
241
372
  }
242
373
  }
243
- async [(_Client_auth = new WeakMap(), _Client_sessionId = new WeakMap(), _Client_state = new WeakMap(), _Client_promises = new WeakMap(), _Client_toAcknowledge = new WeakMap(), _Client_updateState = new WeakMap(), _Client_errorHandler = new WeakMap(), _Client_publicKeys = new WeakMap(), _Client_autoStart = new WeakMap(), _Client_lastPropagatedConnectionState = new WeakMap(), _Client_storageInited = new WeakMap(), _Client_authKeyWasCreated = new WeakMap(), _Client_connectMutex = new WeakMap(), _Client_connectionInited = new WeakMap(), _Client_lastPropagatedAuthorizationState = new WeakMap(), _Client_selfId = new WeakMap(), _Client_lastOutgoing = new WeakMap(), _Client_pingLoopStarted = new WeakMap(), _Client_autoStarted = new WeakMap(), _Client_lastMsgId = new WeakMap(), _Client_handleUpdateQueue = new WeakMap(), _Client_processUpdatesQueue = new WeakMap(), _Client_usernameResolver = new WeakMap(), _Client_handle = new WeakMap(), _Client_instances = new WeakSet(), _Client_propagateConnectionState = function _Client_propagateConnectionState(connectionState) {
374
+ async [(_Client_auth = new WeakMap(), _Client_sessionId = new WeakMap(), _Client_state = new WeakMap(), _Client_promises = new WeakMap(), _Client_toAcknowledge = new WeakMap(), _Client_updateState = new WeakMap(), _Client_publicKeys = new WeakMap(), _Client_autoStart = new WeakMap(), _Client_constructContext = new WeakMap(), _Client_lastPropagatedConnectionState = new WeakMap(), _Client_storageInited = new WeakMap(), _Client_authKeyWasCreated = new WeakMap(), _Client_connectMutex = new WeakMap(), _Client_connectionInited = new WeakMap(), _Client_lastPropagatedAuthorizationState = new WeakMap(), _Client_selfId = new WeakMap(), _Client_pingInterval = new WeakMap(), _Client_pingLoopStarted = new WeakMap(), _Client_autoStarted = new WeakMap(), _Client_lastMsgId = new WeakMap(), _Client_handleInvokeError = new WeakMap(), _Client_handleUpdateQueue = new WeakMap(), _Client_processUpdatesQueue = new WeakMap(), _Client_usernameResolver = new WeakMap(), _Client_handle = new WeakMap(), _Client_instances = new WeakSet(), _Client_propagateConnectionState = function _Client_propagateConnectionState(connectionState) {
244
375
  __classPrivateFieldGet(this, _Client_handleUpdateQueue, "f").add(async () => {
245
- await __classPrivateFieldGet(this, _Client_handle, "f").call(this, { connectionState }, _0_utilities_js_1.resolve);
376
+ await __classPrivateFieldGet(this, _Client_handle, "f").call(this, __classPrivateFieldGet(this, _Client_constructContext, "f").call(this, { connectionState }), _0_utilities_js_1.resolve);
246
377
  });
247
378
  }, _Client_setAuth = async function _Client_setAuth(key) {
248
379
  const hash = await (0, _1_utilities_js_1.sha1)(key);
@@ -472,70 +603,6 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
472
603
  }
473
604
  await this.authorize(params);
474
605
  }
475
- async invoke(function_, noWait) {
476
- if (!__classPrivateFieldGet(this, _Client_auth, "f") || !this.transport) {
477
- if (__classPrivateFieldGet(this, _Client_autoStart, "f") && !__classPrivateFieldGet(this, _Client_autoStarted, "f")) {
478
- await this.start();
479
- }
480
- else {
481
- throw new ConnectionError("Not connected");
482
- }
483
- }
484
- if (!__classPrivateFieldGet(this, _Client_auth, "f") || !this.transport) {
485
- (0, _1_utilities_js_1.UNREACHABLE)();
486
- }
487
- let n = 1;
488
- while (true) {
489
- try {
490
- let seqNo = __classPrivateFieldGet(this, _Client_state, "f").seqNo * 2;
491
- if (!(function_ instanceof _2_tl_js_1.functions.Ping) && !(function_ instanceof _2_tl_js_1.types.MsgsAck)) {
492
- seqNo++;
493
- __classPrivateFieldGet(this, _Client_state, "f").seqNo++;
494
- }
495
- const messageId = __classPrivateFieldSet(this, _Client_lastMsgId, (0, _0_message_js_1.getMessageId)(__classPrivateFieldGet(this, _Client_lastMsgId, "f")), "f");
496
- const message = new _2_tl_js_1.Message_(messageId, seqNo, function_);
497
- await this.transport.transport.send(await (0, _0_message_js_1.encryptMessage)(message, __classPrivateFieldGet(this, _Client_auth, "f").key, __classPrivateFieldGet(this, _Client_auth, "f").id, __classPrivateFieldGet(this, _Client_state, "f").salt, __classPrivateFieldGet(this, _Client_sessionId, "f")));
498
- __classPrivateFieldSet(this, _Client_lastOutgoing, new Date(), "f");
499
- d("invoked %s", function_.constructor.name);
500
- if (noWait) {
501
- return;
502
- }
503
- let result;
504
- try {
505
- result = await new Promise((resolve, reject) => {
506
- __classPrivateFieldGet(this, _Client_promises, "f").set(message.id, { resolve, reject });
507
- });
508
- }
509
- catch (err) {
510
- if (err instanceof _4_errors_js_1.AuthKeyUnregistered) {
511
- await __classPrivateFieldGet(this, _Client_instances, "m", _Client_propagateAuthorizationState).call(this, false);
512
- }
513
- throw err;
514
- }
515
- if (result instanceof _2_tl_js_1.types.BadServerSalt) {
516
- return await this.invoke(function_);
517
- }
518
- else {
519
- if (!__classPrivateFieldGet(this, _Client_pingLoopStarted, "f")) {
520
- (0, _1_utilities_js_1.drop)(__classPrivateFieldGet(this, _Client_instances, "m", _Client_pingLoop).call(this));
521
- __classPrivateFieldSet(this, _Client_pingLoopStarted, true, "f");
522
- }
523
- return result;
524
- }
525
- }
526
- catch (err) {
527
- if (__classPrivateFieldGet(this, _Client_errorHandler, "f") === undefined) {
528
- throw err;
529
- }
530
- if (__classPrivateFieldGet(this, _Client_errorHandler, "f") !== undefined && await __classPrivateFieldGet(this, _Client_errorHandler, "f").call(this, err, function_, n++)) {
531
- continue;
532
- }
533
- else {
534
- throw err;
535
- }
536
- }
537
- }
538
- }
539
606
  /**
540
607
  * Alias for `invoke` with its second parameter being `true`.
541
608
  */
@@ -590,7 +657,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
590
657
  }
591
658
  }, _Client_propagateAuthorizationState = async function _Client_propagateAuthorizationState(authorized) {
592
659
  if (__classPrivateFieldGet(this, _Client_lastPropagatedAuthorizationState, "f") != authorized) {
593
- await __classPrivateFieldGet(this, _Client_handle, "f").call(this, { authorizationState: { authorized } }, _0_utilities_js_1.resolve);
660
+ await __classPrivateFieldGet(this, _Client_handle, "f").call(this, __classPrivateFieldGet(this, _Client_constructContext, "f").call(this, { authorizationState: { authorized } }), _0_utilities_js_1.resolve);
594
661
  __classPrivateFieldSet(this, _Client_lastPropagatedAuthorizationState, authorized, "f");
595
662
  }
596
663
  }, _Client_getSelfId = async function _Client_getSelfId() {
@@ -710,17 +777,81 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
710
777
  }
711
778
  }, _Client_pingLoop = async function _Client_pingLoop() {
712
779
  while (this.connected) {
713
- await new Promise((r) => setTimeout(r, 60 * 1000));
714
- if (!__classPrivateFieldGet(this, _Client_lastOutgoing, "f") || (Date.now() - __classPrivateFieldGet(this, _Client_lastOutgoing, "f").getTime() < 60 * 1000)) {
715
- continue;
716
- }
780
+ await new Promise((r) => setTimeout(r, __classPrivateFieldGet(this, _Client_pingInterval, "f")));
717
781
  try {
718
- await this.invoke(new _2_tl_js_1.functions.Ping({ pingId: (0, _1_utilities_js_1.getRandomBigInt)(8, true, false) }));
782
+ await this.invoke(new _2_tl_js_1.functions.PingDelayDisconnect({ pingId: (0, _1_utilities_js_1.getRandomBigInt)(8, true, false), disconnectDelay: __classPrivateFieldGet(this, _Client_pingInterval, "f") + 15 }));
719
783
  }
720
784
  catch (err) {
721
785
  d("ping loop error: %o", err);
722
786
  }
723
787
  }
788
+ }, _Client_invoke = async function _Client_invoke(function_, noWait) {
789
+ if (!__classPrivateFieldGet(this, _Client_auth, "f") || !this.transport) {
790
+ if (__classPrivateFieldGet(this, _Client_autoStart, "f") && !__classPrivateFieldGet(this, _Client_autoStarted, "f")) {
791
+ await this.start();
792
+ }
793
+ else {
794
+ throw new ConnectionError("Not connected");
795
+ }
796
+ }
797
+ if (!__classPrivateFieldGet(this, _Client_auth, "f") || !this.transport) {
798
+ (0, _1_utilities_js_1.UNREACHABLE)();
799
+ }
800
+ let n = 1;
801
+ while (true) {
802
+ try {
803
+ let seqNo = __classPrivateFieldGet(this, _Client_state, "f").seqNo * 2;
804
+ if (!(function_ instanceof _2_tl_js_1.functions.Ping) && !(function_ instanceof _2_tl_js_1.types.MsgsAck)) {
805
+ seqNo++;
806
+ __classPrivateFieldGet(this, _Client_state, "f").seqNo++;
807
+ }
808
+ const messageId = __classPrivateFieldSet(this, _Client_lastMsgId, (0, _0_message_js_1.getMessageId)(__classPrivateFieldGet(this, _Client_lastMsgId, "f")), "f");
809
+ const message = new _2_tl_js_1.Message_(messageId, seqNo, function_);
810
+ await this.transport.transport.send(await (0, _0_message_js_1.encryptMessage)(message, __classPrivateFieldGet(this, _Client_auth, "f").key, __classPrivateFieldGet(this, _Client_auth, "f").id, __classPrivateFieldGet(this, _Client_state, "f").salt, __classPrivateFieldGet(this, _Client_sessionId, "f")));
811
+ d("invoked %s", function_.constructor.name);
812
+ if (noWait) {
813
+ __classPrivateFieldGet(this, _Client_promises, "f").set(message.id, {
814
+ resolve: (result) => {
815
+ if (result instanceof _2_tl_js_1.types.BadServerSalt) {
816
+ (0, _1_utilities_js_1.drop)(this.invoke(function_, true));
817
+ }
818
+ },
819
+ reject: () => { },
820
+ });
821
+ return;
822
+ }
823
+ let result;
824
+ try {
825
+ result = await new Promise((resolve, reject) => {
826
+ __classPrivateFieldGet(this, _Client_promises, "f").set(message.id, { resolve, reject });
827
+ });
828
+ }
829
+ catch (err) {
830
+ if (err instanceof _4_errors_js_1.AuthKeyUnregistered) {
831
+ await __classPrivateFieldGet(this, _Client_instances, "m", _Client_propagateAuthorizationState).call(this, false);
832
+ }
833
+ throw err;
834
+ }
835
+ if (result instanceof _2_tl_js_1.types.BadServerSalt) {
836
+ return await this.invoke(function_);
837
+ }
838
+ else {
839
+ if (!__classPrivateFieldGet(this, _Client_pingLoopStarted, "f")) {
840
+ (0, _1_utilities_js_1.drop)(__classPrivateFieldGet(this, _Client_instances, "m", _Client_pingLoop).call(this));
841
+ __classPrivateFieldSet(this, _Client_pingLoopStarted, true, "f");
842
+ }
843
+ return result;
844
+ }
845
+ }
846
+ catch (err) {
847
+ if (await __classPrivateFieldGet(this, _Client_handleInvokeError, "f").call(this, Object.freeze({ client: this, error: err, function: function_, n: n++ }), () => Promise.resolve(false))) {
848
+ continue;
849
+ }
850
+ else {
851
+ throw err;
852
+ }
853
+ }
854
+ }
724
855
  }, _Client_processChats = async function _Client_processChats(chats) {
725
856
  for (const chat of chats) {
726
857
  if (chat instanceof _2_tl_js_1.types.Channel && chat.accessHash) {
@@ -912,7 +1043,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
912
1043
  return localState;
913
1044
  }, _Client_recoverUpdateGap = async function _Client_recoverUpdateGap(source) {
914
1045
  dGap("recovering from update gap [%s]", source);
915
- await __classPrivateFieldGet(this, _Client_instances, "m", _Client_propagateConnectionState).call(this, "updating");
1046
+ __classPrivateFieldGet(this, _Client_instances, "m", _Client_propagateConnectionState).call(this, "updating");
916
1047
  try {
917
1048
  let state = await __classPrivateFieldGet(this, _Client_instances, "m", _Client_getLocalState).call(this);
918
1049
  while (true) {
@@ -1689,20 +1820,15 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
1689
1820
  nextOffset: params?.nextOffset,
1690
1821
  }));
1691
1822
  }
1692
- use(handler) {
1693
- const handle = __classPrivateFieldGet(this, _Client_handle, "f");
1694
- __classPrivateFieldSet(this, _Client_handle, async (upd, next) => {
1695
- let called = false;
1696
- await handle(upd, async () => {
1697
- if (called)
1698
- return;
1699
- called = true;
1700
- await handler(upd, next);
1701
- });
1702
- }, "f");
1823
+ use(...middleware) {
1824
+ const composer = new _4_composer_js_1.Composer(...middleware);
1825
+ __classPrivateFieldSet(this, _Client_handle, (0, _4_composer_js_1.concat)(__classPrivateFieldGet(this, _Client_handle, "f"), (0, _4_composer_js_1.flatten)(composer)), "f");
1826
+ return composer;
1703
1827
  }
1704
- branch(predicate, trueHandler, falseHandler) {
1705
- this.use(async (upd, next) => {
1828
+ branch(predicate, trueHandler_, falseHandler_) {
1829
+ const trueHandler = (0, _4_composer_js_1.flatten)(trueHandler_);
1830
+ const falseHandler = (0, _4_composer_js_1.flatten)(falseHandler_);
1831
+ return this.use(async (upd, next) => {
1706
1832
  if (await predicate(upd)) {
1707
1833
  await trueHandler(upd, next);
1708
1834
  }
@@ -1711,19 +1837,19 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
1711
1837
  }
1712
1838
  });
1713
1839
  }
1714
- filter(predicate, handler) {
1715
- this.branch(predicate, handler, _3_types_js_2.skip);
1840
+ filter(predicate, ...middleware) {
1841
+ return this.branch(predicate, middleware.length == 0 ? _4_composer_js_1.skip : middleware.map(_4_composer_js_1.flatten).reduce(_4_composer_js_1.concat), _4_composer_js_1.skip);
1716
1842
  }
1717
- on(filter, handler) {
1843
+ on(filter, ...middleawre) {
1718
1844
  const type = typeof filter === "string" ? filter : filter[0];
1719
1845
  const keys = Array.isArray(filter) ? filter.slice(1) : [];
1720
- this.filter((update) => {
1721
- if (type in update) {
1846
+ return this.filter((ctx) => {
1847
+ if (type in ctx) {
1722
1848
  if (keys.length > 0) {
1723
1849
  for (const key of keys) {
1724
1850
  // deno-lint-ignore ban-ts-comment
1725
1851
  // @ts-ignore
1726
- if (!(key in update[type])) {
1852
+ if (!(key in ctx[type])) {
1727
1853
  return false;
1728
1854
  }
1729
1855
  }
@@ -1733,7 +1859,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
1733
1859
  else {
1734
1860
  return false;
1735
1861
  }
1736
- }, handler);
1862
+ }, ...middleawre);
1737
1863
  }
1738
1864
  /**
1739
1865
  * Set the bot's description in the given language. Bot-only.
@@ -1850,7 +1976,7 @@ async function _Client_handleUpdate(update) {
1850
1976
  const key = update instanceof _2_tl_js_1.types.UpdateNewMessage || update instanceof _2_tl_js_1.types.UpdateNewChannelMessage ? "message" : "editedMessage";
1851
1977
  if (!(update.message instanceof _2_tl_js_1.types.MessageEmpty)) {
1852
1978
  const message = await (0, _3_types_js_1.constructMessage)(update.message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this));
1853
- await __classPrivateFieldGet(this, _Client_handle, "f").call(this, { [key]: message }, _0_utilities_js_1.resolve);
1979
+ await __classPrivateFieldGet(this, _Client_handle, "f").call(this, __classPrivateFieldGet(this, _Client_constructContext, "f").call(this, { [key]: message }), _0_utilities_js_1.resolve);
1854
1980
  }
1855
1981
  }
1856
1982
  if (update instanceof _2_tl_js_1.types.UpdateDeleteMessages) {
@@ -1866,7 +1992,7 @@ async function _Client_handleUpdate(update) {
1866
1992
  }
1867
1993
  }
1868
1994
  if (deletedMessages.length > 0) {
1869
- await __classPrivateFieldGet(this, _Client_handle, "f").call(this, { deletedMessages: deletedMessages }, _0_utilities_js_1.resolve);
1995
+ await __classPrivateFieldGet(this, _Client_handle, "f").call(this, __classPrivateFieldGet(this, _Client_constructContext, "f").call(this, { deletedMessages: deletedMessages }), _0_utilities_js_1.resolve);
1870
1996
  }
1871
1997
  }
1872
1998
  else if (update instanceof _2_tl_js_1.types.UpdateDeleteChannelMessages) {
@@ -1880,14 +2006,14 @@ async function _Client_handleUpdate(update) {
1880
2006
  await this.storage.setMessage(chatId, messageId, null);
1881
2007
  }
1882
2008
  if (deletedMessages.length > 0) {
1883
- await __classPrivateFieldGet(this, _Client_handle, "f").call(this, { deletedMessages: deletedMessages }, _0_utilities_js_1.resolve);
2009
+ await __classPrivateFieldGet(this, _Client_handle, "f").call(this, __classPrivateFieldGet(this, _Client_constructContext, "f").call(this, { deletedMessages: deletedMessages }), _0_utilities_js_1.resolve);
1884
2010
  }
1885
2011
  }
1886
2012
  if (update instanceof _2_tl_js_1.types.UpdateBotCallbackQuery || update instanceof _2_tl_js_1.types.UpdateInlineBotCallbackQuery) {
1887
- await __classPrivateFieldGet(this, _Client_handle, "f").call(this, { callbackQuery: await (0, _3_types_js_1.constructCallbackQuery)(update, this[getEntity].bind(this), this[getMessageWithReply].bind(this)) }, _0_utilities_js_1.resolve);
2013
+ await __classPrivateFieldGet(this, _Client_handle, "f").call(this, __classPrivateFieldGet(this, _Client_constructContext, "f").call(this, { callbackQuery: await (0, _3_types_js_1.constructCallbackQuery)(update, this[getEntity].bind(this), this[getMessageWithReply].bind(this)) }), _0_utilities_js_1.resolve);
1888
2014
  }
1889
2015
  else if (update instanceof _2_tl_js_1.types.UpdateBotInlineQuery) {
1890
- await __classPrivateFieldGet(this, _Client_handle, "f").call(this, { inlineQuery: await (0, _3_types_js_1.constructInlineQuery)(update, this[getEntity].bind(this)) }, _0_utilities_js_1.resolve);
2016
+ await __classPrivateFieldGet(this, _Client_handle, "f").call(this, __classPrivateFieldGet(this, _Client_constructContext, "f").call(this, { inlineQuery: await (0, _3_types_js_1.constructInlineQuery)(update, this[getEntity].bind(this)) }), _0_utilities_js_1.resolve);
1891
2017
  }
1892
2018
  }, _Client_constructReplyMarkup = async function _Client_constructReplyMarkup(params) {
1893
2019
  if (params?.replyMarkup) {
@@ -1899,7 +2025,9 @@ async function _Client_handleUpdate(update) {
1899
2025
  (0, _1_utilities_js_1.UNREACHABLE)();
1900
2026
  }
1901
2027
  return message;
1902
- }, _Client_setMyInfo = async function _Client_setMyInfo(info) {
2028
+ }, _Client_setMyInfo =
2029
+ //#endregion
2030
+ async function _Client_setMyInfo(info) {
1903
2031
  await this.invoke(new _2_tl_js_1.functions.BotsSetBotInfo({ bot: new _2_tl_js_1.types.InputUserSelf(), ...info }));
1904
2032
  }, _Client_getMyInfo = function _Client_getMyInfo(languageCode) {
1905
2033
  return this.invoke(new _2_tl_js_1.functions.BotsGetBotInfo({ bot: new _2_tl_js_1.types.InputUserSelf(), langCode: languageCode ?? "" }));
@@ -0,0 +1,4 @@
1
+ import { Composer as Composer_ } from "./4_composer.js";
2
+ import { Context } from "./5_client.js";
3
+ export declare class Composer<C extends Context = Context> extends Composer_<C> {
4
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Composer = void 0;
4
+ const _4_composer_js_1 = require("./4_composer.js");
5
+ class Composer extends _4_composer_js_1.Composer {
6
+ }
7
+ exports.Composer = Composer;
@@ -8,6 +8,7 @@ export declare class TLRawReader {
8
8
  readInt24(signed?: boolean): number;
9
9
  readInt32(signed?: boolean): number;
10
10
  readInt64(signed?: boolean): bigint;
11
+ readDouble(): number;
11
12
  readInt128(signed?: boolean): bigint;
12
13
  readInt256(signed?: boolean): bigint;
13
14
  readBytes(): Uint8Array;
@@ -37,6 +37,9 @@ class TLRawReader {
37
37
  const buffer = this.read(64 / 8);
38
38
  return (0, _1_utilities_js_1.bigIntFromBuffer)(buffer, true, signed);
39
39
  }
40
+ readDouble() {
41
+ return new DataView(this.read(8).buffer).getFloat64(0, true); // TODO: cover in tests
42
+ }
40
43
  readInt128(signed = true) {
41
44
  const buffer = this.read(128 / 8);
42
45
  return (0, _1_utilities_js_1.bigIntFromBuffer)(buffer, true, signed);
@@ -31,7 +31,12 @@ function deserializeSingleParam(reader, type, ntype) {
31
31
  case "boolean":
32
32
  return reader.readInt32(false) == 0x997275B5;
33
33
  case "number":
34
- return reader.readInt32();
34
+ if (ntype == "double") {
35
+ return reader.readDouble();
36
+ }
37
+ else {
38
+ return reader.readInt32();
39
+ }
35
40
  case "string":
36
41
  return reader.readString();
37
42
  case "true":
@@ -67,7 +72,7 @@ function deserialize(reader, paramDesc, constructor) {
67
72
  continue;
68
73
  }
69
74
  const value = deserializeSingleParam(reader, type, ntype);
70
- if (value) {
75
+ if (typeof value !== "boolean" || value) {
71
76
  params[name] = value;
72
77
  }
73
78
  }
@@ -161,11 +161,13 @@ async function inlineQueryResultToTlObject(result_, parseText, usernameResolver)
161
161
  replyMarkup,
162
162
  });
163
163
  const title = "title" in result_ ? result_.title : undefined;
164
+ const description = "description" in result_ ? result_.description : undefined;
164
165
  if (document != null) {
165
166
  return new _2_tl_js_1.types.InputBotInlineResult({
166
167
  id,
167
168
  type,
168
169
  title,
170
+ description,
169
171
  thumb: thumb == null ? undefined : thumb,
170
172
  content: document,
171
173
  sendMessage: new _2_tl_js_1.types.InputBotInlineMessageMediaAuto({
@@ -181,6 +183,7 @@ async function inlineQueryResultToTlObject(result_, parseText, usernameResolver)
181
183
  id,
182
184
  type,
183
185
  title,
186
+ description,
184
187
  document: new _2_tl_js_1.types.InputDocument({
185
188
  id: fileId.params.mediaId,
186
189
  accessHash: fileId.params.accessHash,
@@ -194,6 +197,7 @@ async function inlineQueryResultToTlObject(result_, parseText, usernameResolver)
194
197
  id,
195
198
  type,
196
199
  title,
200
+ description,
197
201
  sendMessage: new _2_tl_js_1.types.InputBotInlineMessageMediaGeo({
198
202
  geoPoint: new _2_tl_js_1.types.InputGeoPoint({
199
203
  lat: result_.latitude,
@@ -212,6 +216,7 @@ async function inlineQueryResultToTlObject(result_, parseText, usernameResolver)
212
216
  id,
213
217
  type,
214
218
  title,
219
+ description,
215
220
  sendMessage: new _2_tl_js_1.types.InputBotInlineMessageGame({
216
221
  replyMarkup,
217
222
  }),
@@ -226,6 +231,7 @@ async function inlineQueryResultToTlObject(result_, parseText, usernameResolver)
226
231
  id,
227
232
  type,
228
233
  title,
234
+ description,
229
235
  sendMessage: new _2_tl_js_1.types.InputBotInlineMessageText({
230
236
  message,
231
237
  entities,
@@ -242,6 +248,7 @@ async function inlineQueryResultToTlObject(result_, parseText, usernameResolver)
242
248
  id,
243
249
  type,
244
250
  title,
251
+ description,
245
252
  sendMessage: new _2_tl_js_1.types.InputBotInlineMessageMediaVenue({
246
253
  geoPoint: new _2_tl_js_1.types.InputGeoPoint({ long: result_.longitude, lat: result_.latitude }),
247
254
  address: result_.address,