@mtkruto/node 0.0.964 → 0.0.966

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import { Message } from "../tl/6_message.js";
2
2
  import { MessageContainer } from "../tl/7_message_container.js";
3
- export declare function getMessageId(): bigint;
4
- export declare function packUnencryptedMessage(data: Uint8Array): Uint8Array;
3
+ export declare function getMessageId(lastMsgId: bigint): bigint;
4
+ export declare function packUnencryptedMessage(data: Uint8Array, messageId: bigint): Uint8Array;
5
5
  export declare function unpackUnencryptedMessage(buffer: Uint8Array): {
6
6
  messageId: bigint;
7
7
  message: Uint8Array;
@@ -8,8 +8,7 @@ import { Message } from "../tl/6_message.js";
8
8
  import { MessageContainer } from "../tl/7_message_container.js";
9
9
  import { bufferFromBigInt, concat } from "../utilities/0_buffer.js";
10
10
  import { sha256 } from "../utilities/0_hash.js";
11
- let lastMsgId = 0n;
12
- export function getMessageId() {
11
+ export function getMessageId(lastMsgId) {
13
12
  const now = new Date().getTime() / 1000 + 0;
14
13
  const nanoseconds = Math.floor((now - Math.floor(now)) * 1e9);
15
14
  let newMsgId = (BigInt(Math.floor(now)) <<
@@ -21,8 +20,8 @@ export function getMessageId() {
21
20
  lastMsgId = newMsgId;
22
21
  return newMsgId;
23
22
  }
24
- export function packUnencryptedMessage(data) {
25
- const message = concat(bufferFromBigInt(0x00, 8), bufferFromBigInt(getMessageId(), 8), bufferFromBigInt(data.length, 4), data);
23
+ export function packUnencryptedMessage(data, messageId) {
24
+ const message = concat(bufferFromBigInt(0x00, 8), bufferFromBigInt(getMessageId(messageId), 8), bufferFromBigInt(data.length, 4), data);
26
25
  return message;
27
26
  }
28
27
  export function unpackUnencryptedMessage(buffer) {
@@ -1,6 +1,7 @@
1
1
  import { Function } from "../tl/3_functions.js";
2
2
  import { ClientAbstract } from "./1_client_abstract.js";
3
3
  export declare class ClientPlain extends ClientAbstract {
4
+ private lastMsgId;
4
5
  invoke<T extends Function<unknown>>(function_: T): Promise<T["__R"]>;
5
6
  createAuthKey(): Promise<{
6
7
  authKey: Uint8Array;
@@ -10,11 +10,21 @@ import { ClientDHInnerData, DHGenOK, PQInnerDataDC, ResPQ, ServerDHInnerData, Se
10
10
  import { ReqDHParams, ReqPQMulti, SetClientDHParams } from "../tl/3_functions.js";
11
11
  import { TLReader } from "../tl/3_tl_reader.js";
12
12
  import { ClientAbstract } from "./1_client_abstract.js";
13
- import { packUnencryptedMessage, unpackUnencryptedMessage } from "./0_message.js";
13
+ import { getMessageId, packUnencryptedMessage, unpackUnencryptedMessage } from "./0_message.js";
14
14
  const d = debug("ClientPlain/createAuthKey");
15
15
  export class ClientPlain extends ClientAbstract {
16
+ constructor() {
17
+ super(...arguments);
18
+ Object.defineProperty(this, "lastMsgId", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: 0n
23
+ });
24
+ }
16
25
  async invoke(function_) {
17
- await this.transport.send(packUnencryptedMessage(function_[serialize]()));
26
+ const msgId = this.lastMsgId = getMessageId(this.lastMsgId);
27
+ await this.transport.send(packUnencryptedMessage(function_[serialize](), msgId));
18
28
  const buffer = await this.transport.receive();
19
29
  if (buffer.length == 4) {
20
30
  const int = bigIntFromBuffer(buffer, true, true);
@@ -131,6 +131,7 @@ export declare class Client extends ClientAbstract {
131
131
  authorize(params: string | types.AuthExportedAuthorization | AuthorizeUserParams): Promise<void>;
132
132
  private receiveLoop;
133
133
  private pingLoop;
134
+ private lastMsgId;
134
135
  /**
135
136
  * Invokes a function waiting and returning its reply if the second parameter is not `true`. Requires the client
136
137
  * to be connected.
@@ -159,6 +159,12 @@ export class Client extends ClientAbstract {
159
159
  writable: true,
160
160
  value: false
161
161
  });
162
+ Object.defineProperty(this, "lastMsgId", {
163
+ enumerable: true,
164
+ configurable: true,
165
+ writable: true,
166
+ value: 0n
167
+ });
162
168
  Object.defineProperty(this, "updateApplicationMutex", {
163
169
  enumerable: true,
164
170
  configurable: true,
@@ -558,7 +564,8 @@ export class Client extends ClientAbstract {
558
564
  seqNo++;
559
565
  this.state.seqNo++;
560
566
  }
561
- const message = new Message_(getMessageId(), seqNo, function_);
567
+ const messageId = this.lastMsgId = getMessageId(this.lastMsgId);
568
+ const message = new Message_(messageId, seqNo, function_);
562
569
  await this.transport.send(await encryptMessage(message, this.auth.key, this.auth.id, this.state.salt, this.sessionId));
563
570
  const d_ = () => d("invoked %s", function_.constructor.name);
564
571
  if (noWait) {
@@ -8,6 +8,8 @@ export declare class ConnectionWebSocket implements Connection {
8
8
  constructor(url: string | URL);
9
9
  private reinitWs;
10
10
  get connected(): boolean;
11
+ private isConnecting;
12
+ private connectionError;
11
13
  open(): Promise<void>;
12
14
  read(p: Uint8Array): Promise<void>;
13
15
  write(p: Uint8Array): Promise<void>;
@@ -33,17 +33,35 @@ export class ConnectionWebSocket {
33
33
  writable: true,
34
34
  value: null
35
35
  });
36
+ Object.defineProperty(this, "isConnecting", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: false
41
+ });
42
+ Object.defineProperty(this, "connectionError", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: null
47
+ });
36
48
  this.webSocket = this.reinitWs(url);
37
49
  // TODO
38
- this.webSocket.onclose = () => {
39
- this.webSocket = this.reinitWs(url);
40
- };
50
+ this.webSocket.addEventListener("close", (e) => {
51
+ if (e.code != 1000 && e.reason != "method") {
52
+ this.webSocket = this.reinitWs(url);
53
+ }
54
+ });
41
55
  }
42
56
  reinitWs(url) {
43
57
  const webSocket = new dntShim.WebSocket(url, "binary");
44
- webSocket.onmessage = async (e) => {
45
- // deno-lint-ignore no-explicit-any
46
- const data = e.data instanceof Blob ? new Uint8Array(await e.data.arrayBuffer()) : new Uint8Array(e.data);
58
+ const mutex = new Mutex();
59
+ webSocket.addEventListener("message", async (e) => {
60
+ if (typeof e.data === "string") {
61
+ return;
62
+ }
63
+ const release = await mutex.acquire();
64
+ const data = new Uint8Array(await new Blob([e.data].map((v) => v instanceof ArrayBuffer ? v : Array.isArray(v) ? v.map((v) => v.buffer) : v.buffer).flat()).arrayBuffer());
47
65
  for (const byte of data) {
48
66
  this.buffer.push(byte);
49
67
  }
@@ -51,24 +69,44 @@ export class ConnectionWebSocket {
51
69
  this.nextResolve[1]();
52
70
  this.nextResolve = null;
53
71
  }
54
- };
55
- webSocket.onerror = (err) => {
72
+ release();
73
+ });
74
+ webSocket.addEventListener("error", (err) => {
75
+ if (this.isConnecting) {
76
+ // @ts-ignore: Node.js
77
+ this.connectionError = err;
78
+ }
56
79
  d("WebSocket error: %o", err);
57
- };
80
+ });
58
81
  return webSocket;
59
82
  }
60
83
  get connected() {
61
84
  return this.webSocket.readyState == dntShim.WebSocket.OPEN;
62
85
  }
63
86
  async open() {
64
- while (this.webSocket.readyState != dntShim.WebSocket.OPEN) {
65
- if (this.webSocket.readyState == dntShim.WebSocket.CLOSED) {
66
- throw new Error("Connection was closed");
67
- }
68
- else {
69
- await new Promise((r) => setTimeout(r, 5));
87
+ if (this.isConnecting) {
88
+ throw new Error("Already connecting");
89
+ }
90
+ this.isConnecting = true;
91
+ try {
92
+ while (this.webSocket.readyState != dntShim.WebSocket.OPEN) {
93
+ if (this.webSocket.readyState == dntShim.WebSocket.CLOSED) {
94
+ if (this.connectionError instanceof ErrorEvent) {
95
+ throw new Error(this.connectionError.message);
96
+ }
97
+ else {
98
+ throw new Error("Connection was closed");
99
+ }
100
+ }
101
+ else {
102
+ await new Promise((r) => setTimeout(r, 5));
103
+ }
70
104
  }
71
105
  }
106
+ finally {
107
+ this.isConnecting = false;
108
+ this.connectionError = null;
109
+ }
72
110
  }
73
111
  async read(p) {
74
112
  if (this.webSocket.readyState != dntShim.WebSocket.OPEN) {
@@ -101,6 +139,7 @@ export class ConnectionWebSocket {
101
139
  if (this.webSocket.readyState == dntShim.WebSocket.CLOSED) {
102
140
  throw new Error("Connection not open");
103
141
  }
104
- this.webSocket.close();
142
+ this.webSocket.close(1000, "method");
143
+ console.trace("close called");
105
144
  }
106
145
  }
@@ -4,7 +4,7 @@ export declare const publicKeys: Map<bigint, [bigint, bigint]>;
4
4
  export declare const VECTOR_CONSTRUCTOR = 481674261;
5
5
  export declare const DEFAULT_INITIAL_DC: DC;
6
6
  export declare const LAYER = 158;
7
- export declare const DEFAULT_APP_VERSION = "MTKruto 0.0.964";
7
+ export declare const DEFAULT_APP_VERSION = "MTKruto 0.0.966";
8
8
  export declare const DEFAULT_DEVICE_MODEL: string;
9
9
  export declare const DEFAULT_LANG_CODE: string;
10
10
  export declare const DEFAULT_LANG_PACK = "";
package/esm/constants.js CHANGED
@@ -62,7 +62,7 @@ export const publicKeys = new Map([
62
62
  export const VECTOR_CONSTRUCTOR = 0x1CB5C415;
63
63
  export const DEFAULT_INITIAL_DC = "2-test";
64
64
  export const LAYER = 158;
65
- export const DEFAULT_APP_VERSION = "MTKruto 0.0.964";
65
+ export const DEFAULT_APP_VERSION = "MTKruto 0.0.966";
66
66
  // @ts-ignore: lib
67
67
  export const DEFAULT_DEVICE_MODEL = typeof dntShim.Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : dntShim.Deno.build.os + "-" + dntShim.Deno.build.arch;
68
68
  export const DEFAULT_LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "module": "./esm/mod.js",
3
3
  "main": "./script/mod.js",
4
4
  "name": "@mtkruto/node",
5
- "version": "0.0.964",
5
+ "version": "0.0.966",
6
6
  "description": "MTKruto for Node.js",
7
7
  "author": "Roj <rojvv@icloud.com>",
8
8
  "license": "LGPL-3.0-or-later",
@@ -1,7 +1,7 @@
1
1
  import { Message } from "../tl/6_message.js";
2
2
  import { MessageContainer } from "../tl/7_message_container.js";
3
- export declare function getMessageId(): bigint;
4
- export declare function packUnencryptedMessage(data: Uint8Array): Uint8Array;
3
+ export declare function getMessageId(lastMsgId: bigint): bigint;
4
+ export declare function packUnencryptedMessage(data: Uint8Array, messageId: bigint): Uint8Array;
5
5
  export declare function unpackUnencryptedMessage(buffer: Uint8Array): {
6
6
  messageId: bigint;
7
7
  message: Uint8Array;
@@ -11,8 +11,7 @@ const _6_message_js_1 = require("../tl/6_message.js");
11
11
  const _7_message_container_js_1 = require("../tl/7_message_container.js");
12
12
  const _0_buffer_js_1 = require("../utilities/0_buffer.js");
13
13
  const _0_hash_js_1 = require("../utilities/0_hash.js");
14
- let lastMsgId = 0n;
15
- function getMessageId() {
14
+ function getMessageId(lastMsgId) {
16
15
  const now = new Date().getTime() / 1000 + 0;
17
16
  const nanoseconds = Math.floor((now - Math.floor(now)) * 1e9);
18
17
  let newMsgId = (BigInt(Math.floor(now)) <<
@@ -25,8 +24,8 @@ function getMessageId() {
25
24
  return newMsgId;
26
25
  }
27
26
  exports.getMessageId = getMessageId;
28
- function packUnencryptedMessage(data) {
29
- const message = (0, _0_buffer_js_1.concat)((0, _0_buffer_js_1.bufferFromBigInt)(0x00, 8), (0, _0_buffer_js_1.bufferFromBigInt)(getMessageId(), 8), (0, _0_buffer_js_1.bufferFromBigInt)(data.length, 4), data);
27
+ function packUnencryptedMessage(data, messageId) {
28
+ const message = (0, _0_buffer_js_1.concat)((0, _0_buffer_js_1.bufferFromBigInt)(0x00, 8), (0, _0_buffer_js_1.bufferFromBigInt)(getMessageId(messageId), 8), (0, _0_buffer_js_1.bufferFromBigInt)(data.length, 4), data);
30
29
  return message;
31
30
  }
32
31
  exports.packUnencryptedMessage = packUnencryptedMessage;
@@ -1,6 +1,7 @@
1
1
  import { Function } from "../tl/3_functions.js";
2
2
  import { ClientAbstract } from "./1_client_abstract.js";
3
3
  export declare class ClientPlain extends ClientAbstract {
4
+ private lastMsgId;
4
5
  invoke<T extends Function<unknown>>(function_: T): Promise<T["__R"]>;
5
6
  createAuthKey(): Promise<{
6
7
  authKey: Uint8Array;
@@ -16,8 +16,18 @@ const _1_client_abstract_js_1 = require("./1_client_abstract.js");
16
16
  const _0_message_js_1 = require("./0_message.js");
17
17
  const d = (0, deps_js_1.debug)("ClientPlain/createAuthKey");
18
18
  class ClientPlain extends _1_client_abstract_js_1.ClientAbstract {
19
+ constructor() {
20
+ super(...arguments);
21
+ Object.defineProperty(this, "lastMsgId", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: 0n
26
+ });
27
+ }
19
28
  async invoke(function_) {
20
- await this.transport.send((0, _0_message_js_1.packUnencryptedMessage)(function_[_1_tl_object_js_1.serialize]()));
29
+ const msgId = this.lastMsgId = (0, _0_message_js_1.getMessageId)(this.lastMsgId);
30
+ await this.transport.send((0, _0_message_js_1.packUnencryptedMessage)(function_[_1_tl_object_js_1.serialize](), msgId));
21
31
  const buffer = await this.transport.receive();
22
32
  if (buffer.length == 4) {
23
33
  const int = (0, _0_bigint_js_1.bigIntFromBuffer)(buffer, true, true);
@@ -131,6 +131,7 @@ export declare class Client extends ClientAbstract {
131
131
  authorize(params: string | types.AuthExportedAuthorization | AuthorizeUserParams): Promise<void>;
132
132
  private receiveLoop;
133
133
  private pingLoop;
134
+ private lastMsgId;
134
135
  /**
135
136
  * Invokes a function waiting and returning its reply if the second parameter is not `true`. Requires the client
136
137
  * to be connected.
@@ -185,6 +185,12 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
185
185
  writable: true,
186
186
  value: false
187
187
  });
188
+ Object.defineProperty(this, "lastMsgId", {
189
+ enumerable: true,
190
+ configurable: true,
191
+ writable: true,
192
+ value: 0n
193
+ });
188
194
  Object.defineProperty(this, "updateApplicationMutex", {
189
195
  enumerable: true,
190
196
  configurable: true,
@@ -584,7 +590,8 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
584
590
  seqNo++;
585
591
  this.state.seqNo++;
586
592
  }
587
- const message = new _6_message_js_1.Message((0, _0_message_js_1.getMessageId)(), seqNo, function_);
593
+ const messageId = this.lastMsgId = (0, _0_message_js_1.getMessageId)(this.lastMsgId);
594
+ const message = new _6_message_js_1.Message(messageId, seqNo, function_);
588
595
  await this.transport.send(await (0, _0_message_js_1.encryptMessage)(message, this.auth.key, this.auth.id, this.state.salt, this.sessionId));
589
596
  const d_ = () => d("invoked %s", function_.constructor.name);
590
597
  if (noWait) {
@@ -8,6 +8,8 @@ export declare class ConnectionWebSocket implements Connection {
8
8
  constructor(url: string | URL);
9
9
  private reinitWs;
10
10
  get connected(): boolean;
11
+ private isConnecting;
12
+ private connectionError;
11
13
  open(): Promise<void>;
12
14
  read(p: Uint8Array): Promise<void>;
13
15
  write(p: Uint8Array): Promise<void>;
@@ -59,17 +59,35 @@ class ConnectionWebSocket {
59
59
  writable: true,
60
60
  value: null
61
61
  });
62
+ Object.defineProperty(this, "isConnecting", {
63
+ enumerable: true,
64
+ configurable: true,
65
+ writable: true,
66
+ value: false
67
+ });
68
+ Object.defineProperty(this, "connectionError", {
69
+ enumerable: true,
70
+ configurable: true,
71
+ writable: true,
72
+ value: null
73
+ });
62
74
  this.webSocket = this.reinitWs(url);
63
75
  // TODO
64
- this.webSocket.onclose = () => {
65
- this.webSocket = this.reinitWs(url);
66
- };
76
+ this.webSocket.addEventListener("close", (e) => {
77
+ if (e.code != 1000 && e.reason != "method") {
78
+ this.webSocket = this.reinitWs(url);
79
+ }
80
+ });
67
81
  }
68
82
  reinitWs(url) {
69
83
  const webSocket = new dntShim.WebSocket(url, "binary");
70
- webSocket.onmessage = async (e) => {
71
- // deno-lint-ignore no-explicit-any
72
- const data = e.data instanceof Blob ? new Uint8Array(await e.data.arrayBuffer()) : new Uint8Array(e.data);
84
+ const mutex = new deps_js_1.Mutex();
85
+ webSocket.addEventListener("message", async (e) => {
86
+ if (typeof e.data === "string") {
87
+ return;
88
+ }
89
+ const release = await mutex.acquire();
90
+ const data = new Uint8Array(await new Blob([e.data].map((v) => v instanceof ArrayBuffer ? v : Array.isArray(v) ? v.map((v) => v.buffer) : v.buffer).flat()).arrayBuffer());
73
91
  for (const byte of data) {
74
92
  this.buffer.push(byte);
75
93
  }
@@ -77,24 +95,44 @@ class ConnectionWebSocket {
77
95
  this.nextResolve[1]();
78
96
  this.nextResolve = null;
79
97
  }
80
- };
81
- webSocket.onerror = (err) => {
98
+ release();
99
+ });
100
+ webSocket.addEventListener("error", (err) => {
101
+ if (this.isConnecting) {
102
+ // @ts-ignore: Node.js
103
+ this.connectionError = err;
104
+ }
82
105
  d("WebSocket error: %o", err);
83
- };
106
+ });
84
107
  return webSocket;
85
108
  }
86
109
  get connected() {
87
110
  return this.webSocket.readyState == dntShim.WebSocket.OPEN;
88
111
  }
89
112
  async open() {
90
- while (this.webSocket.readyState != dntShim.WebSocket.OPEN) {
91
- if (this.webSocket.readyState == dntShim.WebSocket.CLOSED) {
92
- throw new Error("Connection was closed");
93
- }
94
- else {
95
- await new Promise((r) => setTimeout(r, 5));
113
+ if (this.isConnecting) {
114
+ throw new Error("Already connecting");
115
+ }
116
+ this.isConnecting = true;
117
+ try {
118
+ while (this.webSocket.readyState != dntShim.WebSocket.OPEN) {
119
+ if (this.webSocket.readyState == dntShim.WebSocket.CLOSED) {
120
+ if (this.connectionError instanceof ErrorEvent) {
121
+ throw new Error(this.connectionError.message);
122
+ }
123
+ else {
124
+ throw new Error("Connection was closed");
125
+ }
126
+ }
127
+ else {
128
+ await new Promise((r) => setTimeout(r, 5));
129
+ }
96
130
  }
97
131
  }
132
+ finally {
133
+ this.isConnecting = false;
134
+ this.connectionError = null;
135
+ }
98
136
  }
99
137
  async read(p) {
100
138
  if (this.webSocket.readyState != dntShim.WebSocket.OPEN) {
@@ -127,7 +165,8 @@ class ConnectionWebSocket {
127
165
  if (this.webSocket.readyState == dntShim.WebSocket.CLOSED) {
128
166
  throw new Error("Connection not open");
129
167
  }
130
- this.webSocket.close();
168
+ this.webSocket.close(1000, "method");
169
+ console.trace("close called");
131
170
  }
132
171
  }
133
172
  exports.ConnectionWebSocket = ConnectionWebSocket;
@@ -4,7 +4,7 @@ export declare const publicKeys: Map<bigint, [bigint, bigint]>;
4
4
  export declare const VECTOR_CONSTRUCTOR = 481674261;
5
5
  export declare const DEFAULT_INITIAL_DC: DC;
6
6
  export declare const LAYER = 158;
7
- export declare const DEFAULT_APP_VERSION = "MTKruto 0.0.964";
7
+ export declare const DEFAULT_APP_VERSION = "MTKruto 0.0.966";
8
8
  export declare const DEFAULT_DEVICE_MODEL: string;
9
9
  export declare const DEFAULT_LANG_CODE: string;
10
10
  export declare const DEFAULT_LANG_PACK = "";
@@ -88,7 +88,7 @@ exports.publicKeys = new Map([
88
88
  exports.VECTOR_CONSTRUCTOR = 0x1CB5C415;
89
89
  exports.DEFAULT_INITIAL_DC = "2-test";
90
90
  exports.LAYER = 158;
91
- exports.DEFAULT_APP_VERSION = "MTKruto 0.0.964";
91
+ exports.DEFAULT_APP_VERSION = "MTKruto 0.0.966";
92
92
  // @ts-ignore: lib
93
93
  exports.DEFAULT_DEVICE_MODEL = typeof dntShim.Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : dntShim.Deno.build.os + "-" + dntShim.Deno.build.arch;
94
94
  exports.DEFAULT_LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];