@mtcute/core 0.25.0 → 0.25.1

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.
@@ -97,12 +97,14 @@ class UpdatesManager {
97
97
  this._onCatchingUp = handler;
98
98
  }
99
99
  destroy() {
100
+ this.log.debug("destroyed, stopping updates loop");
100
101
  this.stopLoop();
101
102
  }
102
103
  notifyLoggedIn() {
103
104
  this.startLoop().catch((err) => this.client.onError.emit(utils.unknownToError(err)));
104
105
  }
105
106
  notifyLoggedOut() {
107
+ this.log.debug("notifyLoggedOut, stopping updates loop");
106
108
  this.stopLoop();
107
109
  this.cpts.clear();
108
110
  this.cptsMod.clear();
@@ -316,6 +318,7 @@ class UpdatesManager {
316
318
  } catch (e) {
317
319
  if (tl.tl.RpcError.is(e, "AUTH_KEY_UNREGISTERED")) {
318
320
  lock.release();
321
+ this.log.debug("received AUTH_KEY_UNREGISTERED, stopping updates loop");
319
322
  this.stopLoop();
320
323
  return;
321
324
  }
@@ -812,6 +815,7 @@ class UpdatesManager {
812
815
  0,
813
816
  this._fetchDifference(requestedDiff).catch((err) => {
814
817
  if (tl.tl.RpcError.is(err, "AUTH_KEY_UNREGISTERED")) {
818
+ this.log.debug("received AUTH_KEY_UNREGISTERED, stopping updates loop");
815
819
  this.stopLoop();
816
820
  return;
817
821
  }
@@ -90,12 +90,14 @@ class UpdatesManager {
90
90
  this._onCatchingUp = handler;
91
91
  }
92
92
  destroy() {
93
+ this.log.debug("destroyed, stopping updates loop");
93
94
  this.stopLoop();
94
95
  }
95
96
  notifyLoggedIn() {
96
97
  this.startLoop().catch((err) => this.client.onError.emit(unknownToError(err)));
97
98
  }
98
99
  notifyLoggedOut() {
100
+ this.log.debug("notifyLoggedOut, stopping updates loop");
99
101
  this.stopLoop();
100
102
  this.cpts.clear();
101
103
  this.cptsMod.clear();
@@ -309,6 +311,7 @@ class UpdatesManager {
309
311
  } catch (e) {
310
312
  if (tl.RpcError.is(e, "AUTH_KEY_UNREGISTERED")) {
311
313
  lock.release();
314
+ this.log.debug("received AUTH_KEY_UNREGISTERED, stopping updates loop");
312
315
  this.stopLoop();
313
316
  return;
314
317
  }
@@ -805,6 +808,7 @@ class UpdatesManager {
805
808
  0,
806
809
  this._fetchDifference(requestedDiff).catch((err) => {
807
810
  if (tl.RpcError.is(err, "AUTH_KEY_UNREGISTERED")) {
811
+ this.log.debug("received AUTH_KEY_UNREGISTERED, stopping updates loop");
808
812
  this.stopLoop();
809
813
  return;
810
814
  }
@@ -29,7 +29,7 @@ class MtprotoSession {
29
29
  _authKey;
30
30
  _authKeyTemp;
31
31
  _authKeyTempSecondary;
32
- _timeOffset = performance.timeOrigin;
32
+ _timeOffset = Math.floor(performance.timeOrigin / 1e3);
33
33
  _lastMessageId = Long.ZERO;
34
34
  _seqNo = 0;
35
35
  /// state ///
@@ -22,7 +22,7 @@ class MtprotoSession {
22
22
  _authKey;
23
23
  _authKeyTemp;
24
24
  _authKeyTempSecondary;
25
- _timeOffset = performance.timeOrigin;
25
+ _timeOffset = Math.floor(performance.timeOrigin / 1e3);
26
26
  _lastMessageId = Long.ZERO;
27
27
  _seqNo = 0;
28
28
  /// state ///
@@ -32,25 +32,27 @@ class DcConnectionManager {
32
32
  this.isPrimary = isPrimary;
33
33
  this._log = this.manager._log.create("dc-manager");
34
34
  this._log.prefix = `[DC ${dcId}] `;
35
+ const managerParams = this.manager.params;
35
36
  const baseConnectionParams = () => ({
36
- crypto: this.manager.params.crypto,
37
+ crypto: managerParams.crypto,
37
38
  initConnection: this.manager._initConnectionParams,
38
39
  transport: this.manager._transport,
39
40
  dc: this._dcs.media,
40
- testMode: this.manager.params.testMode,
41
+ testMode: managerParams.testMode,
41
42
  reconnectionStrategy: this.manager._reconnectionStrategy,
42
- layer: this.manager.params.layer,
43
- disableUpdates: this.manager.params.disableUpdates,
44
- readerMap: this.manager.params.readerMap,
45
- writerMap: this.manager.params.writerMap,
46
- usePfs: this.manager.params.usePfs,
43
+ layer: managerParams.layer,
44
+ disableUpdates: managerParams.disableUpdates,
45
+ readerMap: managerParams.readerMap,
46
+ writerMap: managerParams.writerMap,
47
+ usePfs: managerParams.usePfs,
47
48
  isMainConnection: false,
48
49
  isMainDcConnection: this.isPrimary,
49
- inactivityTimeout: this.manager.params.inactivityTimeout ?? 6e4,
50
- enableErrorReporting: this.manager.params.enableErrorReporting,
51
- salts: this._salts,
52
- platform: this.manager.params.platform,
53
- pingInterval: this.manager.params.pingInterval ?? 6e4
50
+ inactivityTimeout: managerParams.inactivityTimeout ?? 6e4,
51
+ enableErrorReporting: managerParams.enableErrorReporting,
52
+ // NB: we need a separate salts manager for each pfs connection, because server salts are auth_key-bound
53
+ salts: managerParams.usePfs ? new serverSalt.ServerSaltManager() : this._salts,
54
+ platform: managerParams.platform,
55
+ pingInterval: managerParams.pingInterval ?? 6e4
54
56
  });
55
57
  const mainParams = baseConnectionParams();
56
58
  mainParams.isMainConnection = true;
@@ -70,19 +72,19 @@ class DcConnectionManager {
70
72
  this.main = new multiSessionConnection.MultiSessionConnection(mainParams, mainCount, this._log, "MAIN");
71
73
  this.upload = new multiSessionConnection.MultiSessionConnection(
72
74
  baseConnectionParams(),
73
- this.manager._connectionCount("upload", this.dcId, this.manager.params.isPremium),
75
+ this.manager._connectionCount("upload", this.dcId, managerParams.isPremium),
74
76
  this._log,
75
77
  "UPLOAD"
76
78
  );
77
79
  this.download = new multiSessionConnection.MultiSessionConnection(
78
80
  baseConnectionParams(),
79
- this.manager._connectionCount("download", this.dcId, this.manager.params.isPremium),
81
+ this.manager._connectionCount("download", this.dcId, managerParams.isPremium),
80
82
  this._log,
81
83
  "DOWNLOAD"
82
84
  );
83
85
  this.downloadSmall = new multiSessionConnection.MultiSessionConnection(
84
86
  baseConnectionParams(),
85
- this.manager._connectionCount("downloadSmall", this.dcId, this.manager.params.isPremium),
87
+ this.manager._connectionCount("downloadSmall", this.dcId, managerParams.isPremium),
86
88
  this._log,
87
89
  "DOWNLOAD_SMALL"
88
90
  );
@@ -135,6 +137,9 @@ class DcConnectionManager {
135
137
  });
136
138
  });
137
139
  connection.onFutureSalts.add((salts) => {
140
+ if (this.manager.params.usePfs && kind !== "main") {
141
+ return;
142
+ }
138
143
  Promise.resolve(this.manager._storage.salts.store(this.dcId, salts)).catch(
139
144
  (e) => this.manager.params.emitError(e)
140
145
  );
@@ -220,7 +225,7 @@ class NetworkManager {
220
225
  _: "initConnection",
221
226
  deviceModel,
222
227
  systemVersion: "1.0",
223
- appVersion: "0.25.0",
228
+ appVersion: "0.25.1",
224
229
  systemLangCode: "en",
225
230
  langPack: "",
226
231
  // "langPacks are for official apps only"
@@ -296,9 +301,7 @@ class NetworkManager {
296
301
  async _getOtherDc(dcId) {
297
302
  if (!this._dcConnections.has(dcId)) {
298
303
  if (this._dcCreationPromise.has(dcId)) {
299
- this._log.debug("waiting for dc %d to be created", dcId);
300
304
  await this._dcCreationPromise.get(dcId);
301
- this._log.debug("dc %d was created", dcId);
302
305
  return utils.asNonNull(this._dcConnections.get(dcId));
303
306
  }
304
307
  const promise = new utils.Deferred();
@@ -313,7 +316,9 @@ class NetworkManager {
313
316
  this._dcConnections.set(dcId, dc);
314
317
  promise.resolve();
315
318
  this._dcCreationPromise.delete(dcId);
319
+ this._log.debug("dc %d was created", dcId);
316
320
  } catch (e) {
321
+ this._log.debug("dc %d was not created: %e", dcId, e);
317
322
  promise.reject(e);
318
323
  this._dcCreationPromise.delete(dcId);
319
324
  return this._getOtherDc(dcId);
@@ -25,25 +25,27 @@ class DcConnectionManager {
25
25
  this.isPrimary = isPrimary;
26
26
  this._log = this.manager._log.create("dc-manager");
27
27
  this._log.prefix = `[DC ${dcId}] `;
28
+ const managerParams = this.manager.params;
28
29
  const baseConnectionParams = () => ({
29
- crypto: this.manager.params.crypto,
30
+ crypto: managerParams.crypto,
30
31
  initConnection: this.manager._initConnectionParams,
31
32
  transport: this.manager._transport,
32
33
  dc: this._dcs.media,
33
- testMode: this.manager.params.testMode,
34
+ testMode: managerParams.testMode,
34
35
  reconnectionStrategy: this.manager._reconnectionStrategy,
35
- layer: this.manager.params.layer,
36
- disableUpdates: this.manager.params.disableUpdates,
37
- readerMap: this.manager.params.readerMap,
38
- writerMap: this.manager.params.writerMap,
39
- usePfs: this.manager.params.usePfs,
36
+ layer: managerParams.layer,
37
+ disableUpdates: managerParams.disableUpdates,
38
+ readerMap: managerParams.readerMap,
39
+ writerMap: managerParams.writerMap,
40
+ usePfs: managerParams.usePfs,
40
41
  isMainConnection: false,
41
42
  isMainDcConnection: this.isPrimary,
42
- inactivityTimeout: this.manager.params.inactivityTimeout ?? 6e4,
43
- enableErrorReporting: this.manager.params.enableErrorReporting,
44
- salts: this._salts,
45
- platform: this.manager.params.platform,
46
- pingInterval: this.manager.params.pingInterval ?? 6e4
43
+ inactivityTimeout: managerParams.inactivityTimeout ?? 6e4,
44
+ enableErrorReporting: managerParams.enableErrorReporting,
45
+ // NB: we need a separate salts manager for each pfs connection, because server salts are auth_key-bound
46
+ salts: managerParams.usePfs ? new ServerSaltManager() : this._salts,
47
+ platform: managerParams.platform,
48
+ pingInterval: managerParams.pingInterval ?? 6e4
47
49
  });
48
50
  const mainParams = baseConnectionParams();
49
51
  mainParams.isMainConnection = true;
@@ -63,19 +65,19 @@ class DcConnectionManager {
63
65
  this.main = new MultiSessionConnection(mainParams, mainCount, this._log, "MAIN");
64
66
  this.upload = new MultiSessionConnection(
65
67
  baseConnectionParams(),
66
- this.manager._connectionCount("upload", this.dcId, this.manager.params.isPremium),
68
+ this.manager._connectionCount("upload", this.dcId, managerParams.isPremium),
67
69
  this._log,
68
70
  "UPLOAD"
69
71
  );
70
72
  this.download = new MultiSessionConnection(
71
73
  baseConnectionParams(),
72
- this.manager._connectionCount("download", this.dcId, this.manager.params.isPremium),
74
+ this.manager._connectionCount("download", this.dcId, managerParams.isPremium),
73
75
  this._log,
74
76
  "DOWNLOAD"
75
77
  );
76
78
  this.downloadSmall = new MultiSessionConnection(
77
79
  baseConnectionParams(),
78
- this.manager._connectionCount("downloadSmall", this.dcId, this.manager.params.isPremium),
80
+ this.manager._connectionCount("downloadSmall", this.dcId, managerParams.isPremium),
79
81
  this._log,
80
82
  "DOWNLOAD_SMALL"
81
83
  );
@@ -128,6 +130,9 @@ class DcConnectionManager {
128
130
  });
129
131
  });
130
132
  connection.onFutureSalts.add((salts) => {
133
+ if (this.manager.params.usePfs && kind !== "main") {
134
+ return;
135
+ }
131
136
  Promise.resolve(this.manager._storage.salts.store(this.dcId, salts)).catch(
132
137
  (e) => this.manager.params.emitError(e)
133
138
  );
@@ -213,7 +218,7 @@ class NetworkManager {
213
218
  _: "initConnection",
214
219
  deviceModel,
215
220
  systemVersion: "1.0",
216
- appVersion: "0.25.0",
221
+ appVersion: "0.25.1",
217
222
  systemLangCode: "en",
218
223
  langPack: "",
219
224
  // "langPacks are for official apps only"
@@ -289,9 +294,7 @@ class NetworkManager {
289
294
  async _getOtherDc(dcId) {
290
295
  if (!this._dcConnections.has(dcId)) {
291
296
  if (this._dcCreationPromise.has(dcId)) {
292
- this._log.debug("waiting for dc %d to be created", dcId);
293
297
  await this._dcCreationPromise.get(dcId);
294
- this._log.debug("dc %d was created", dcId);
295
298
  return asNonNull(this._dcConnections.get(dcId));
296
299
  }
297
300
  const promise = new Deferred();
@@ -306,7 +309,9 @@ class NetworkManager {
306
309
  this._dcConnections.set(dcId, dc);
307
310
  promise.resolve();
308
311
  this._dcCreationPromise.delete(dcId);
312
+ this._log.debug("dc %d was created", dcId);
309
313
  } catch (e) {
314
+ this._log.debug("dc %d was not created: %e", dcId, e);
310
315
  promise.reject(e);
311
316
  this._dcCreationPromise.delete(dcId);
312
317
  return this._getOtherDc(dcId);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mtcute/core",
3
3
  "type": "module",
4
- "version": "0.25.0",
4
+ "version": "0.25.1",
5
5
  "description": "Type-safe library for MTProto (Telegram API)",
6
6
  "license": "MIT",
7
7
  "scripts": {},