@mtkruto/node 0.1.111 → 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) {
@@ -1200,6 +1331,9 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
1200
1331
  const messages = new Array();
1201
1332
  if (updates instanceof _2_tl_js_1.types.Updates) {
1202
1333
  for (const update of updates.updates) {
1334
+ if ("message" in update && update.message instanceof _2_tl_js_1.types.MessageEmpty) {
1335
+ continue;
1336
+ }
1203
1337
  if (update instanceof _2_tl_js_1.types.UpdateNewMessage || update instanceof _2_tl_js_1.types.UpdateEditMessage) {
1204
1338
  messages.push(await (0, _3_types_js_1.constructMessage)(update.message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this)));
1205
1339
  }
@@ -1271,6 +1405,9 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
1271
1405
  }
1272
1406
  const messages = new Array();
1273
1407
  for (const message_ of messages_) {
1408
+ if (message_ instanceof _2_tl_js_1.types.MessageEmpty) {
1409
+ continue;
1410
+ }
1274
1411
  const message = await (0, _3_types_js_1.constructMessage)(message_, this[getEntity].bind(this), null, this[getStickerSetName].bind(this));
1275
1412
  const isReplyToMessage = message_ instanceof _2_tl_js_1.types.Message && message_.replyTo instanceof _2_tl_js_1.types.MessageReplyHeader;
1276
1413
  messages.push({ message, isReplyToMessage });
@@ -1683,20 +1820,15 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
1683
1820
  nextOffset: params?.nextOffset,
1684
1821
  }));
1685
1822
  }
1686
- use(handler) {
1687
- const handle = __classPrivateFieldGet(this, _Client_handle, "f");
1688
- __classPrivateFieldSet(this, _Client_handle, async (upd, next) => {
1689
- let called = false;
1690
- await handle(upd, async () => {
1691
- if (called)
1692
- return;
1693
- called = true;
1694
- await handler(upd, next);
1695
- });
1696
- }, "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;
1697
1827
  }
1698
- branch(predicate, trueHandler, falseHandler) {
1699
- 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) => {
1700
1832
  if (await predicate(upd)) {
1701
1833
  await trueHandler(upd, next);
1702
1834
  }
@@ -1705,19 +1837,19 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
1705
1837
  }
1706
1838
  });
1707
1839
  }
1708
- filter(predicate, handler) {
1709
- 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);
1710
1842
  }
1711
- on(filter, handler) {
1843
+ on(filter, ...middleawre) {
1712
1844
  const type = typeof filter === "string" ? filter : filter[0];
1713
1845
  const keys = Array.isArray(filter) ? filter.slice(1) : [];
1714
- this.filter((update) => {
1715
- if (type in update) {
1846
+ return this.filter((ctx) => {
1847
+ if (type in ctx) {
1716
1848
  if (keys.length > 0) {
1717
1849
  for (const key of keys) {
1718
1850
  // deno-lint-ignore ban-ts-comment
1719
1851
  // @ts-ignore
1720
- if (!(key in update[type])) {
1852
+ if (!(key in ctx[type])) {
1721
1853
  return false;
1722
1854
  }
1723
1855
  }
@@ -1727,7 +1859,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
1727
1859
  else {
1728
1860
  return false;
1729
1861
  }
1730
- }, handler);
1862
+ }, ...middleawre);
1731
1863
  }
1732
1864
  /**
1733
1865
  * Set the bot's description in the given language. Bot-only.
@@ -1842,8 +1974,10 @@ async function _Client_handleUpdate(update) {
1842
1974
  update instanceof _2_tl_js_1.types.UpdateEditMessage ||
1843
1975
  update instanceof _2_tl_js_1.types.UpdateEditChannelMessage) {
1844
1976
  const key = update instanceof _2_tl_js_1.types.UpdateNewMessage || update instanceof _2_tl_js_1.types.UpdateNewChannelMessage ? "message" : "editedMessage";
1845
- const message = await (0, _3_types_js_1.constructMessage)(update.message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this));
1846
- await __classPrivateFieldGet(this, _Client_handle, "f").call(this, { [key]: message }, _0_utilities_js_1.resolve);
1977
+ if (!(update.message instanceof _2_tl_js_1.types.MessageEmpty)) {
1978
+ const message = await (0, _3_types_js_1.constructMessage)(update.message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this));
1979
+ await __classPrivateFieldGet(this, _Client_handle, "f").call(this, __classPrivateFieldGet(this, _Client_constructContext, "f").call(this, { [key]: message }), _0_utilities_js_1.resolve);
1980
+ }
1847
1981
  }
1848
1982
  if (update instanceof _2_tl_js_1.types.UpdateDeleteMessages) {
1849
1983
  const deletedMessages = new Array();
@@ -1851,14 +1985,14 @@ async function _Client_handleUpdate(update) {
1851
1985
  const chatId = await this.storage.getMessageChat(messageId);
1852
1986
  if (chatId) {
1853
1987
  const message = await this.storage.getMessage(chatId, messageId);
1854
- if (message != null) {
1988
+ if (message != null && !(message instanceof _2_tl_js_1.types.MessageEmpty)) {
1855
1989
  deletedMessages.push(await (0, _3_types_js_1.constructMessage)(message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this)));
1856
1990
  }
1857
1991
  await this.storage.setMessage(chatId, messageId, null);
1858
1992
  }
1859
1993
  }
1860
1994
  if (deletedMessages.length > 0) {
1861
- 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);
1862
1996
  }
1863
1997
  }
1864
1998
  else if (update instanceof _2_tl_js_1.types.UpdateDeleteChannelMessages) {
@@ -1866,20 +2000,20 @@ async function _Client_handleUpdate(update) {
1866
2000
  const deletedMessages = new Array();
1867
2001
  for (const messageId of update.messages) {
1868
2002
  const message = await this.storage.getMessage(chatId, messageId);
1869
- if (message) {
2003
+ if (message != null && !(message instanceof _2_tl_js_1.types.MessageEmpty)) {
1870
2004
  deletedMessages.push(await (0, _3_types_js_1.constructMessage)(message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this)));
1871
2005
  }
1872
2006
  await this.storage.setMessage(chatId, messageId, null);
1873
2007
  }
1874
2008
  if (deletedMessages.length > 0) {
1875
- 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);
1876
2010
  }
1877
2011
  }
1878
2012
  if (update instanceof _2_tl_js_1.types.UpdateBotCallbackQuery || update instanceof _2_tl_js_1.types.UpdateInlineBotCallbackQuery) {
1879
- 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);
1880
2014
  }
1881
2015
  else if (update instanceof _2_tl_js_1.types.UpdateBotInlineQuery) {
1882
- 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);
1883
2017
  }
1884
2018
  }, _Client_constructReplyMarkup = async function _Client_constructReplyMarkup(params) {
1885
2019
  if (params?.replyMarkup) {
@@ -1891,7 +2025,9 @@ async function _Client_handleUpdate(update) {
1891
2025
  (0, _1_utilities_js_1.UNREACHABLE)();
1892
2026
  }
1893
2027
  return message;
1894
- }, _Client_setMyInfo = async function _Client_setMyInfo(info) {
2028
+ }, _Client_setMyInfo =
2029
+ //#endregion
2030
+ async function _Client_setMyInfo(info) {
1895
2031
  await this.invoke(new _2_tl_js_1.functions.BotsSetBotInfo({ bot: new _2_tl_js_1.types.InputUserSelf(), ...info }));
1896
2032
  }, _Client_getMyInfo = function _Client_getMyInfo(languageCode) {
1897
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
  }