@mtcute/core 0.27.7 → 0.27.9

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.
@@ -158,6 +158,10 @@ async function* iterDialogs(client, params) {
158
158
  }
159
159
  for (const d of dialogs) {
160
160
  if (filterFolder && !filterFolder(d)) continue;
161
+ if (d.isPinned) {
162
+ if (archived === "exclude" && d.isArchived) continue;
163
+ if (archived === "only" && !d.isArchived) continue;
164
+ }
161
165
  yield d;
162
166
  if (++current >= limit) return;
163
167
  }
@@ -151,6 +151,10 @@ async function* iterDialogs(client, params) {
151
151
  }
152
152
  for (const d of dialogs) {
153
153
  if (filterFolder && !filterFolder(d)) continue;
154
+ if (d.isPinned) {
155
+ if (archived === "exclude" && d.isArchived) continue;
156
+ if (archived === "only" && !d.isArchived) continue;
157
+ }
154
158
  yield d;
155
159
  if (++current >= limit) return;
156
160
  }
@@ -64,6 +64,10 @@ class UpdatesManager {
64
64
  // channel id or 0 => pts
65
65
  noDispatchPts = /* @__PURE__ */ new Map();
66
66
  noDispatchQts = /* @__PURE__ */ new Set();
67
+ // message IDs from common message box that were dispatched recently,
68
+ // used to deduplicate messages from getDifference.newMessages that may have
69
+ // already been processed via regular pts-ordered updates
70
+ _recentlyDispatchedCommonMsgs = new utils.LruSet(100);
67
71
  lock = new utils.AsyncLock();
68
72
  // rpsIncoming?: RpsMeter
69
73
  // rpsProcessing?: RpsMeter
@@ -777,6 +781,10 @@ class UpdatesManager {
777
781
  diff.newMessages.forEach((message) => {
778
782
  log.debug("processing message %d in %j (%s) from common diff", message.id, message.peerId, message._);
779
783
  if (message._ === "messageEmpty") return;
784
+ if (this._recentlyDispatchedCommonMsgs.has(message.id)) {
785
+ log.debug("skipping message %d from common diff because it was already dispatched", message.id);
786
+ return;
787
+ }
780
788
  pendingUnorderedUpdates.pushBack(utils$2.toPendingUpdate(utils$2.messageToUpdate(message), peers, true));
781
789
  });
782
790
  diff.otherUpdates.forEach((upd) => {
@@ -984,6 +992,9 @@ class UpdatesManager {
984
992
  }
985
993
  }
986
994
  log.debug("dispatching %s (postponed = %s)", upd._, postponed);
995
+ if (upd._ === "updateNewMessage" && upd.message._ !== "messageEmpty") {
996
+ this._recentlyDispatchedCommonMsgs.add(upd.message.id);
997
+ }
987
998
  client.onRawUpdate.emit(new types.RawUpdateInfo(upd, pending.peers));
988
999
  }
989
1000
  async _loop() {
@@ -1,7 +1,7 @@
1
1
  import { Logger, EarlyTimer, SortedArray } from '../../utils/index.js';
2
2
  import { BaseTelegramClient } from '../base.js';
3
3
  import { PendingUpdate, PendingUpdateContainer, UpdatesManagerParams } from './types.js';
4
- import { AsyncLock, ConditionVariable, Deque, timers } from '@fuman/utils';
4
+ import { AsyncLock, ConditionVariable, Deque, LruSet, timers } from '@fuman/utils';
5
5
  import { tl } from '@mtcute/tl';
6
6
  import { PeersIndex } from '../types/peers/peers-index.js';
7
7
  export declare class UpdatesManager {
@@ -21,6 +21,7 @@ export declare class UpdatesManager {
21
21
  noDispatchMsg: Map<number, Set<number>>;
22
22
  noDispatchPts: Map<number, Set<number>>;
23
23
  noDispatchQts: Set<number>;
24
+ _recentlyDispatchedCommonMsgs: LruSet<number>;
24
25
  lock: AsyncLock;
25
26
  pts?: number;
26
27
  qts?: number;
@@ -1,7 +1,7 @@
1
1
  import { Logger, EarlyTimer, SortedArray } from '../../utils/index.js';
2
2
  import { BaseTelegramClient } from '../base.js';
3
3
  import { PendingUpdate, PendingUpdateContainer, UpdatesManagerParams } from './types.js';
4
- import { AsyncLock, ConditionVariable, Deque, timers } from '@fuman/utils';
4
+ import { AsyncLock, ConditionVariable, Deque, LruSet, timers } from '@fuman/utils';
5
5
  import { tl } from '@mtcute/tl';
6
6
  import { PeersIndex } from '../types/peers/peers-index.js';
7
7
  export declare class UpdatesManager {
@@ -21,6 +21,7 @@ export declare class UpdatesManager {
21
21
  noDispatchMsg: Map<number, Set<number>>;
22
22
  noDispatchPts: Map<number, Set<number>>;
23
23
  noDispatchQts: Set<number>;
24
+ _recentlyDispatchedCommonMsgs: LruSet<number>;
24
25
  lock: AsyncLock;
25
26
  pts?: number;
26
27
  qts?: number;
@@ -1,4 +1,4 @@
1
- import { ConditionVariable, Deque, AsyncLock, unknownToError, timers } from "@fuman/utils";
1
+ import { ConditionVariable, Deque, LruSet, AsyncLock, unknownToError, timers } from "@fuman/utils";
2
2
  import { tl } from "@mtcute/tl";
3
3
  import Long from "long";
4
4
  import { MtArgumentError } from "../../types/errors.js";
@@ -57,6 +57,10 @@ class UpdatesManager {
57
57
  // channel id or 0 => pts
58
58
  noDispatchPts = /* @__PURE__ */ new Map();
59
59
  noDispatchQts = /* @__PURE__ */ new Set();
60
+ // message IDs from common message box that were dispatched recently,
61
+ // used to deduplicate messages from getDifference.newMessages that may have
62
+ // already been processed via regular pts-ordered updates
63
+ _recentlyDispatchedCommonMsgs = new LruSet(100);
60
64
  lock = new AsyncLock();
61
65
  // rpsIncoming?: RpsMeter
62
66
  // rpsProcessing?: RpsMeter
@@ -770,6 +774,10 @@ class UpdatesManager {
770
774
  diff.newMessages.forEach((message) => {
771
775
  log.debug("processing message %d in %j (%s) from common diff", message.id, message.peerId, message._);
772
776
  if (message._ === "messageEmpty") return;
777
+ if (this._recentlyDispatchedCommonMsgs.has(message.id)) {
778
+ log.debug("skipping message %d from common diff because it was already dispatched", message.id);
779
+ return;
780
+ }
773
781
  pendingUnorderedUpdates.pushBack(toPendingUpdate(messageToUpdate(message), peers, true));
774
782
  });
775
783
  diff.otherUpdates.forEach((upd) => {
@@ -977,6 +985,9 @@ class UpdatesManager {
977
985
  }
978
986
  }
979
987
  log.debug("dispatching %s (postponed = %s)", upd._, postponed);
988
+ if (upd._ === "updateNewMessage" && upd.message._ !== "messageEmpty") {
989
+ this._recentlyDispatchedCommonMsgs.add(upd.message.id);
990
+ }
980
991
  client.onRawUpdate.emit(new RawUpdateInfo(upd, pending.peers));
981
992
  }
982
993
  async _loop() {
@@ -11,7 +11,7 @@ function determinePartSize(fileSize) {
11
11
  if (fileSize <= 262078465) return 128;
12
12
  if (fileSize <= 786432e3) return 256;
13
13
  if (fileSize <= 2097152e3) return 512;
14
- throw new errors.MtArgumentError("File is too large");
14
+ return 1024;
15
15
  }
16
16
  function isProbablyPlainText(buf) {
17
17
  return !buf.some(
@@ -4,7 +4,7 @@ function determinePartSize(fileSize) {
4
4
  if (fileSize <= 262078465) return 128;
5
5
  if (fileSize <= 786432e3) return 256;
6
6
  if (fileSize <= 2097152e3) return 512;
7
- throw new MtArgumentError("File is too large");
7
+ return 1024;
8
8
  }
9
9
  function isProbablyPlainText(buf) {
10
10
  return !buf.some(
package/index.cjs CHANGED
@@ -145,6 +145,7 @@ const sessionConnection = require("./network/session-connection.cjs");
145
145
  const abstract = require("./network/transports/abstract.cjs");
146
146
  const intermediate = require("./network/transports/intermediate.cjs");
147
147
  const obfuscated = require("./network/transports/obfuscated.cjs");
148
+ const proxy = require("./network/transports/proxy.cjs");
148
149
  const driver = require("./storage/driver.cjs");
149
150
  const driver$1 = require("./storage/memory/driver.cjs");
150
151
  const authKeys = require("./storage/memory/repository/auth-keys.cjs");
@@ -334,6 +335,7 @@ exports.TransportError = abstract.TransportError;
334
335
  exports.IntermediatePacketCodec = intermediate.IntermediatePacketCodec;
335
336
  exports.PaddedIntermediatePacketCodec = intermediate.PaddedIntermediatePacketCodec;
336
337
  exports.ObfuscatedPacketCodec = obfuscated.ObfuscatedPacketCodec;
338
+ exports.createProxyTransportFactory = proxy.createProxyTransportFactory;
337
339
  exports.BaseStorageDriver = driver.BaseStorageDriver;
338
340
  exports.MemoryStorageDriver = driver$1.MemoryStorageDriver;
339
341
  exports.MemoryAuthKeysRepository = authKeys.MemoryAuthKeysRepository;
package/index.js CHANGED
@@ -138,6 +138,7 @@ import { SessionConnection } from "./network/session-connection.js";
138
138
  import { TransportError } from "./network/transports/abstract.js";
139
139
  import { IntermediatePacketCodec, PaddedIntermediatePacketCodec } from "./network/transports/intermediate.js";
140
140
  import { ObfuscatedPacketCodec } from "./network/transports/obfuscated.js";
141
+ import { createProxyTransportFactory } from "./network/transports/proxy.js";
141
142
  import { BaseStorageDriver } from "./storage/driver.js";
142
143
  import { MemoryStorageDriver } from "./storage/memory/driver.js";
143
144
  import { MemoryAuthKeysRepository } from "./storage/memory/repository/auth-keys.js";
@@ -323,6 +324,7 @@ export {
323
324
  _storyInteractiveElementFromTl,
324
325
  assertNever,
325
326
  businessWorkHoursDaysToRaw,
327
+ createProxyTransportFactory,
326
328
  getAllPeersFrom,
327
329
  getBarePeerId,
328
330
  getBasicPeerType,
@@ -36,7 +36,6 @@ class MtprotoSession {
36
36
  // recent msg ids
37
37
  recentOutgoingMsgIds = new utils.LruSet(1e3, longUtils.LongSet);
38
38
  recentIncomingMsgIds = new utils.LruSet(1e3, longUtils.LongSet);
39
- // eslint-disable-next-line ts/no-unsafe-argument
40
39
  recentStateRequests = new utils.LruMap(1e3, longUtils.LongMap);
41
40
  // queues
42
41
  queuedRpc = new utils.Deque();
@@ -29,7 +29,6 @@ class MtprotoSession {
29
29
  // recent msg ids
30
30
  recentOutgoingMsgIds = new LruSet(1e3, LongSet);
31
31
  recentIncomingMsgIds = new LruSet(1e3, LongSet);
32
- // eslint-disable-next-line ts/no-unsafe-argument
33
32
  recentStateRequests = new LruMap(1e3, LongMap);
34
33
  // queues
35
34
  queuedRpc = new Deque();
@@ -225,7 +225,7 @@ class NetworkManager {
225
225
  _: "initConnection",
226
226
  deviceModel,
227
227
  systemVersion: "1.0",
228
- appVersion: "0.27.7",
228
+ appVersion: "0.27.9",
229
229
  systemLangCode: "en",
230
230
  langPack: "",
231
231
  // "langPacks are for official apps only"
@@ -218,7 +218,7 @@ class NetworkManager {
218
218
  _: "initConnection",
219
219
  deviceModel,
220
220
  systemVersion: "1.0",
221
- appVersion: "0.27.7",
221
+ appVersion: "0.27.9",
222
222
  systemLangCode: "en",
223
223
  langPack: "",
224
224
  // "langPacks are for official apps only"
@@ -17,13 +17,11 @@ class ServerSaltManager {
17
17
  setFutureSalts(salts) {
18
18
  this._futureSalts = salts;
19
19
  const now = Date.now() / 1e3;
20
- while (salts.length > 0 && now > salts[0].validUntil) {
20
+ while (salts.length > 0 && salts[0].validSince <= now) {
21
21
  this.currentSalt = salts[0].salt;
22
22
  this._futureSalts.shift();
23
23
  }
24
- if (!this._futureSalts.length) {
25
- this.currentSalt = Long.ZERO;
26
- } else {
24
+ if (this._futureSalts.length) {
27
25
  this._scheduleReplace(this._futureSalts[0]);
28
26
  }
29
27
  }
@@ -10,13 +10,11 @@ class ServerSaltManager {
10
10
  setFutureSalts(salts) {
11
11
  this._futureSalts = salts;
12
12
  const now = Date.now() / 1e3;
13
- while (salts.length > 0 && now > salts[0].validUntil) {
13
+ while (salts.length > 0 && salts[0].validSince <= now) {
14
14
  this.currentSalt = salts[0].salt;
15
15
  this._futureSalts.shift();
16
16
  }
17
- if (!this._futureSalts.length) {
18
- this.currentSalt = Long.ZERO;
19
- } else {
17
+ if (this._futureSalts.length) {
20
18
  this._scheduleReplace(this._futureSalts[0]);
21
19
  }
22
20
  }
@@ -1,3 +1,4 @@
1
1
  export * from './abstract.js';
2
2
  export * from './intermediate.js';
3
3
  export * from './obfuscated.js';
4
+ export * from './proxy.js';
@@ -1,3 +1,4 @@
1
1
  export * from './abstract.js';
2
2
  export * from './intermediate.js';
3
3
  export * from './obfuscated.js';
4
+ export * from './proxy.js';
@@ -0,0 +1,46 @@
1
+ if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WARNED) {
2
+ globalThis._MTCUTE_CJS_DEPRECATION_WARNED = true;
3
+ console.warn("[@mtcute/core] CommonJS bundles are deprecated. They will be removed completely in one of the upcoming releases. No support is provided for CommonJS users. Please consider switching to ESM, it's " + (/* @__PURE__ */ new Date()).getFullYear() + " already.");
4
+ console.warn("[@mtcute/core] Learn more about switching to ESM: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c");
5
+ }
6
+ "use strict";
7
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
8
+ const errors = require("../../types/errors.cjs");
9
+ const proxy = require("../../utils/links/proxy.cjs");
10
+ function createProxyTransportFactory(options) {
11
+ const { SocksProxyTcpTransport, MtProxyTcpTransport, HttpProxyTcpTransport } = options;
12
+ return (url) => {
13
+ const obj = new URL(url);
14
+ if (SocksProxyTcpTransport != null && (obj.protocol === "socks4:" || obj.protocol === "socks5:")) {
15
+ return new SocksProxyTcpTransport({
16
+ version: obj.protocol === "socks4:" ? 4 : 5,
17
+ host: obj.hostname,
18
+ port: Number(obj.port) || 1080,
19
+ user: obj.username,
20
+ password: obj.password
21
+ });
22
+ }
23
+ if (MtProxyTcpTransport != null) {
24
+ const mtproxy = proxy.mtproxy.parse(url);
25
+ if (mtproxy) {
26
+ return new MtProxyTcpTransport({
27
+ host: mtproxy.server,
28
+ port: mtproxy.port,
29
+ secret: mtproxy.secret
30
+ });
31
+ }
32
+ }
33
+ if (HttpProxyTcpTransport && (obj.protocol === "http:" || obj.protocol === "https:")) {
34
+ return new HttpProxyTcpTransport({
35
+ tls: obj.protocol === "https:",
36
+ host: obj.hostname,
37
+ port: Number(obj.port) || (obj.protocol === "https:" ? 443 : 80),
38
+ user: obj.username,
39
+ password: obj.password
40
+ });
41
+ } else {
42
+ throw new errors.MtArgumentError(`Unsupported proxy protocol: ${obj.protocol}`);
43
+ }
44
+ };
45
+ }
46
+ exports.createProxyTransportFactory = createProxyTransportFactory;
@@ -0,0 +1,10 @@
1
+ import { HttpProxySettings, SocksProxySettings } from '@fuman/net';
2
+ import { MtProxySettings } from '../mtproxy/index.js';
3
+ import { TelegramTransport } from './abstract.js';
4
+ export declare function createProxyTransportFactory(options: {
5
+ SocksProxyTcpTransport?: new (options: SocksProxySettings) => TelegramTransport;
6
+ MtProxyTcpTransport?: new (options: MtProxySettings) => TelegramTransport;
7
+ HttpProxyTcpTransport?: new (options: HttpProxySettings & {
8
+ tls?: boolean;
9
+ }) => TelegramTransport;
10
+ }): (url: string) => TelegramTransport;
@@ -0,0 +1,10 @@
1
+ import { HttpProxySettings, SocksProxySettings } from '@fuman/net';
2
+ import { MtProxySettings } from '../mtproxy/index.js';
3
+ import { TelegramTransport } from './abstract.js';
4
+ export declare function createProxyTransportFactory(options: {
5
+ SocksProxyTcpTransport?: new (options: SocksProxySettings) => TelegramTransport;
6
+ MtProxyTcpTransport?: new (options: MtProxySettings) => TelegramTransport;
7
+ HttpProxyTcpTransport?: new (options: HttpProxySettings & {
8
+ tls?: boolean;
9
+ }) => TelegramTransport;
10
+ }): (url: string) => TelegramTransport;
@@ -0,0 +1,41 @@
1
+ import { MtArgumentError } from "../../types/errors.js";
2
+ import { mtproxy } from "../../utils/links/proxy.js";
3
+ function createProxyTransportFactory(options) {
4
+ const { SocksProxyTcpTransport, MtProxyTcpTransport, HttpProxyTcpTransport } = options;
5
+ return (url) => {
6
+ const obj = new URL(url);
7
+ if (SocksProxyTcpTransport != null && (obj.protocol === "socks4:" || obj.protocol === "socks5:")) {
8
+ return new SocksProxyTcpTransport({
9
+ version: obj.protocol === "socks4:" ? 4 : 5,
10
+ host: obj.hostname,
11
+ port: Number(obj.port) || 1080,
12
+ user: obj.username,
13
+ password: obj.password
14
+ });
15
+ }
16
+ if (MtProxyTcpTransport != null) {
17
+ const mtproxy$1 = mtproxy.parse(url);
18
+ if (mtproxy$1) {
19
+ return new MtProxyTcpTransport({
20
+ host: mtproxy$1.server,
21
+ port: mtproxy$1.port,
22
+ secret: mtproxy$1.secret
23
+ });
24
+ }
25
+ }
26
+ if (HttpProxyTcpTransport && (obj.protocol === "http:" || obj.protocol === "https:")) {
27
+ return new HttpProxyTcpTransport({
28
+ tls: obj.protocol === "https:",
29
+ host: obj.hostname,
30
+ port: Number(obj.port) || (obj.protocol === "https:" ? 443 : 80),
31
+ user: obj.username,
32
+ password: obj.password
33
+ });
34
+ } else {
35
+ throw new MtArgumentError(`Unsupported proxy protocol: ${obj.protocol}`);
36
+ }
37
+ };
38
+ }
39
+ export {
40
+ createProxyTransportFactory
41
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@mtcute/core",
3
3
  "type": "module",
4
- "version": "0.27.7",
4
+ "version": "0.27.9",
5
5
  "description": "Type-safe library for MTProto (Telegram API)",
6
6
  "license": "MIT",
7
7
  "scripts": {},
8
8
  "dependencies": {
9
- "@fuman/io": "0.0.17",
10
- "@fuman/net": "0.0.17",
11
- "@fuman/utils": "0.0.17",
12
- "@mtcute/file-id": "^0.27.6",
9
+ "@fuman/io": "0.0.19",
10
+ "@fuman/net": "0.0.19",
11
+ "@fuman/utils": "0.0.19",
12
+ "@mtcute/file-id": "^0.27.8",
13
13
  "@mtcute/tl": "^221.0.0",
14
14
  "@mtcute/tl-runtime": "^0.24.3",
15
15
  "@types/events": "3.0.0",
@@ -95,7 +95,7 @@ class BaseSqliteStorageDriver extends driver.BaseStorageDriver {
95
95
  this.db = this._createDatabase();
96
96
  this._runMany = this.db.transaction((stmts) => {
97
97
  stmts.forEach((stmt) => {
98
- stmt[0].run(stmt[1]);
98
+ stmt[0].run(...stmt[1]);
99
99
  });
100
100
  });
101
101
  this.db.transaction(() => this._initialize())();
@@ -88,7 +88,7 @@ class BaseSqliteStorageDriver extends BaseStorageDriver {
88
88
  this.db = this._createDatabase();
89
89
  this._runMany = this.db.transaction((stmts) => {
90
90
  stmts.forEach((stmt) => {
91
- stmt[0].run(stmt[1]);
91
+ stmt[0].run(...stmt[1]);
92
92
  });
93
93
  });
94
94
  this.db.transaction(() => this._initialize())();