@mtkruto/node 0.0.74 → 0.0.76

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.
@@ -86,8 +86,10 @@ export class Client extends ClientAbstract {
86
86
  let decrypted;
87
87
  try {
88
88
  decrypted = await decryptMessage(buffer, this.session.authKey, this.sessionId);
89
+ console.log({ decrypted });
89
90
  }
90
91
  catch (_err) {
92
+ console.log("failed to decrypt msg", { _err });
91
93
  // logger().error(`Failed to decrypt message: ${err}`);
92
94
  continue;
93
95
  }
@@ -97,6 +99,10 @@ export class Client extends ClientAbstract {
97
99
  if (body instanceof types.GZIPPacked) {
98
100
  body = new TLReader(gunzip(body.packedData)).readObject();
99
101
  }
102
+ console.log("---");
103
+ console.log("message.body", message.body.constructor.name);
104
+ console.log("body", body.constructor.name);
105
+ console.log("---");
100
106
  // logger().debug(`Received ${body.constructor.name}`);
101
107
  if (body instanceof types.Updates) {
102
108
  this.updatesHandler?.(this, body);
@@ -106,6 +112,7 @@ export class Client extends ClientAbstract {
106
112
  if (result instanceof types.GZIPPacked) {
107
113
  result = new TLReader(gunzip(result.packedData)).readObject();
108
114
  }
115
+ // console.log(result.constructor.name)
109
116
  const promise = this.promises.get(message.body.messageId);
110
117
  if (promise) {
111
118
  if (result instanceof types.RPCError) {
@@ -153,6 +160,7 @@ export class Client extends ClientAbstract {
153
160
  if (!this.session.authKey) {
154
161
  throw new Error("Not connected");
155
162
  }
163
+ console.log("invoking", function_.constructor.name);
156
164
  let seqNo = this.state.seqNo * 2;
157
165
  if (!(function_ instanceof functions.Ping) && !(function_ instanceof types.MsgsAck)) {
158
166
  seqNo++;
package/esm/constants.js CHANGED
@@ -58,3 +58,4 @@ export const publicKeys = new Map([
58
58
  ],
59
59
  ],
60
60
  ]);
61
+ export const VECTOR_CONSTRUCTOR = 0x1CB5C415;
@@ -26,7 +26,7 @@ function serializeSingleParam(writer, value, type, ntype) {
26
26
  return;
27
27
  }
28
28
  else {
29
- throw new Error("a");
29
+ throw new TypeError(`Expected ${type.name}`);
30
30
  }
31
31
  }
32
32
  if (type == Uint8Array) {
@@ -34,7 +34,7 @@ function serializeSingleParam(writer, value, type, ntype) {
34
34
  writer.writeBytes(value);
35
35
  }
36
36
  else {
37
- throw new Error("Expected Uint8Array");
37
+ throw new TypeError("Expected Uint8Array");
38
38
  }
39
39
  }
40
40
  switch (type) {
@@ -51,7 +51,7 @@ function serializeSingleParam(writer, value, type, ntype) {
51
51
  }
52
52
  }
53
53
  else {
54
- throw new Error("Expected bigint");
54
+ throw new TypeError("Expected bigint");
55
55
  }
56
56
  break;
57
57
  case "boolean":
@@ -64,7 +64,7 @@ function serializeSingleParam(writer, value, type, ntype) {
64
64
  }
65
65
  }
66
66
  else {
67
- throw new Error("Expected boolean");
67
+ throw new TypeError("Expected boolean");
68
68
  }
69
69
  break;
70
70
  case "number":
@@ -72,7 +72,7 @@ function serializeSingleParam(writer, value, type, ntype) {
72
72
  writer.writeInt32(value);
73
73
  }
74
74
  else {
75
- throw new Error("Expected number");
75
+ throw new TypeError("Expected number");
76
76
  }
77
77
  break;
78
78
  case "string":
@@ -83,7 +83,7 @@ function serializeSingleParam(writer, value, type, ntype) {
83
83
  writer.writeBytes(value);
84
84
  }
85
85
  else {
86
- throw new Error("Expected string or Uint8Array");
86
+ throw new TypeError("Expected string or Uint8Array");
87
87
  }
88
88
  break;
89
89
  case "true":
@@ -91,7 +91,7 @@ function serializeSingleParam(writer, value, type, ntype) {
91
91
  writer.writeInt32(0x997275B5);
92
92
  }
93
93
  else {
94
- throw new Error("Expected true");
94
+ throw new TypeError("Expected true");
95
95
  }
96
96
  }
97
97
  }
@@ -129,7 +129,7 @@ export class TLObject {
129
129
  if (type instanceof Array) {
130
130
  const itemsType = type[0];
131
131
  if (!Array.isArray(value)) {
132
- throw new Error("Expected array");
132
+ throw new TypeError("Expected array");
133
133
  }
134
134
  writer.writeInt32(0x1CB5C415); // vector constructor
135
135
  writer.writeInt32(value.length);
@@ -7,6 +7,14 @@ export class TLReader extends TLRawReader {
7
7
  if (!id) {
8
8
  id = this.readInt32(false);
9
9
  }
10
+ if (id == 0x1CB5C415) {
11
+ const count = this.readInt32();
12
+ const items = new Array();
13
+ for (let i = 0; i < count; i++) {
14
+ items.push(this.readObject());
15
+ }
16
+ return items;
17
+ }
10
18
  const constructor = map.get(id);
11
19
  if (constructor) {
12
20
  return deserialize(this, constructor[paramDesc], constructor);
@@ -1,7 +1,17 @@
1
+ import { VECTOR_CONSTRUCTOR } from "../constants.js";
1
2
  import { TLRawWriter } from "./0_tl_raw_writer.js";
2
3
  export class TLWriter extends TLRawWriter {
3
4
  writeObject(object) {
4
- this.write(object.serialize());
5
+ if (Array.isArray(object)) {
6
+ this.writeInt32(VECTOR_CONSTRUCTOR, false);
7
+ this.writeInt32(object.length);
8
+ for (const item of object) {
9
+ this.writeObject(item);
10
+ }
11
+ }
12
+ else {
13
+ this.write(object.serialize());
14
+ }
5
15
  return this;
6
16
  }
7
17
  }
@@ -1,7 +1,22 @@
1
- import { id, length } from "./1_tl_object.js";
1
+ import { id } from "./1_tl_object.js";
2
2
  import { TLReader } from "./3_tl_reader.js";
3
3
  import { TLWriter } from "./3_tl_writer.js";
4
4
  import { RPCResult } from "./4_rpc_result.js";
5
+ // TODO: test
6
+ function calculateLength(object) {
7
+ let length = 0;
8
+ if (Array.isArray(object)) {
9
+ length += 32 / 8; // vector constructor
10
+ length += 32 / 8; // number of items
11
+ for (const item of object) {
12
+ length += calculateLength(item);
13
+ }
14
+ }
15
+ else {
16
+ length += object.serialize().length;
17
+ }
18
+ return length;
19
+ }
5
20
  export class Message {
6
21
  constructor(id, seqNo, body) {
7
22
  Object.defineProperty(this, "id", {
@@ -30,7 +45,7 @@ export class Message {
30
45
  return new TLWriter()
31
46
  .writeInt64(this.id)
32
47
  .writeInt32(this.seqNo)
33
- .writeInt32(this.body[length])
48
+ .writeInt32(calculateLength(this.body))
34
49
  .writeObject(this.body)
35
50
  .buffer;
36
51
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "main": "./script/mod.js",
4
4
  "types": "./types/mod.d.ts",
5
5
  "name": "@mtkruto/node",
6
- "version": "0.0.74",
6
+ "version": "0.0.76",
7
7
  "description": "MTKruto for Node.js",
8
8
  "author": "Roj <rojvv@icloud.com>",
9
9
  "license": "LGPL-3.0-or-later",
@@ -112,8 +112,10 @@ class Client extends client_abstract_js_1.ClientAbstract {
112
112
  let decrypted;
113
113
  try {
114
114
  decrypted = await (0, _1_message_js_1.decryptMessage)(buffer, this.session.authKey, this.sessionId);
115
+ console.log({ decrypted });
115
116
  }
116
117
  catch (_err) {
118
+ console.log("failed to decrypt msg", { _err });
117
119
  // logger().error(`Failed to decrypt message: ${err}`);
118
120
  continue;
119
121
  }
@@ -123,6 +125,10 @@ class Client extends client_abstract_js_1.ClientAbstract {
123
125
  if (body instanceof types.GZIPPacked) {
124
126
  body = new _3_tl_reader_js_1.TLReader((0, deps_js_1.gunzip)(body.packedData)).readObject();
125
127
  }
128
+ console.log("---");
129
+ console.log("message.body", message.body.constructor.name);
130
+ console.log("body", body.constructor.name);
131
+ console.log("---");
126
132
  // logger().debug(`Received ${body.constructor.name}`);
127
133
  if (body instanceof types.Updates) {
128
134
  this.updatesHandler?.(this, body);
@@ -132,6 +138,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
132
138
  if (result instanceof types.GZIPPacked) {
133
139
  result = new _3_tl_reader_js_1.TLReader((0, deps_js_1.gunzip)(result.packedData)).readObject();
134
140
  }
141
+ // console.log(result.constructor.name)
135
142
  const promise = this.promises.get(message.body.messageId);
136
143
  if (promise) {
137
144
  if (result instanceof types.RPCError) {
@@ -179,6 +186,7 @@ class Client extends client_abstract_js_1.ClientAbstract {
179
186
  if (!this.session.authKey) {
180
187
  throw new Error("Not connected");
181
188
  }
189
+ console.log("invoking", function_.constructor.name);
182
190
  let seqNo = this.state.seqNo * 2;
183
191
  if (!(function_ instanceof functions.Ping) && !(function_ instanceof types.MsgsAck)) {
184
192
  seqNo++;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.publicKeys = exports.ackThreshold = void 0;
3
+ exports.VECTOR_CONSTRUCTOR = exports.publicKeys = exports.ackThreshold = void 0;
4
4
  exports.ackThreshold = 10;
5
5
  exports.publicKeys = new Map([
6
6
  [
@@ -61,3 +61,4 @@ exports.publicKeys = new Map([
61
61
  ],
62
62
  ],
63
63
  ]);
64
+ exports.VECTOR_CONSTRUCTOR = 0x1CB5C415;
@@ -31,7 +31,7 @@ function serializeSingleParam(writer, value, type, ntype) {
31
31
  return;
32
32
  }
33
33
  else {
34
- throw new Error("a");
34
+ throw new TypeError(`Expected ${type.name}`);
35
35
  }
36
36
  }
37
37
  if (type == Uint8Array) {
@@ -39,7 +39,7 @@ function serializeSingleParam(writer, value, type, ntype) {
39
39
  writer.writeBytes(value);
40
40
  }
41
41
  else {
42
- throw new Error("Expected Uint8Array");
42
+ throw new TypeError("Expected Uint8Array");
43
43
  }
44
44
  }
45
45
  switch (type) {
@@ -56,7 +56,7 @@ function serializeSingleParam(writer, value, type, ntype) {
56
56
  }
57
57
  }
58
58
  else {
59
- throw new Error("Expected bigint");
59
+ throw new TypeError("Expected bigint");
60
60
  }
61
61
  break;
62
62
  case "boolean":
@@ -69,7 +69,7 @@ function serializeSingleParam(writer, value, type, ntype) {
69
69
  }
70
70
  }
71
71
  else {
72
- throw new Error("Expected boolean");
72
+ throw new TypeError("Expected boolean");
73
73
  }
74
74
  break;
75
75
  case "number":
@@ -77,7 +77,7 @@ function serializeSingleParam(writer, value, type, ntype) {
77
77
  writer.writeInt32(value);
78
78
  }
79
79
  else {
80
- throw new Error("Expected number");
80
+ throw new TypeError("Expected number");
81
81
  }
82
82
  break;
83
83
  case "string":
@@ -88,7 +88,7 @@ function serializeSingleParam(writer, value, type, ntype) {
88
88
  writer.writeBytes(value);
89
89
  }
90
90
  else {
91
- throw new Error("Expected string or Uint8Array");
91
+ throw new TypeError("Expected string or Uint8Array");
92
92
  }
93
93
  break;
94
94
  case "true":
@@ -96,7 +96,7 @@ function serializeSingleParam(writer, value, type, ntype) {
96
96
  writer.writeInt32(0x997275B5);
97
97
  }
98
98
  else {
99
- throw new Error("Expected true");
99
+ throw new TypeError("Expected true");
100
100
  }
101
101
  }
102
102
  }
@@ -134,7 +134,7 @@ class TLObject {
134
134
  if (type instanceof Array) {
135
135
  const itemsType = type[0];
136
136
  if (!Array.isArray(value)) {
137
- throw new Error("Expected array");
137
+ throw new TypeError("Expected array");
138
138
  }
139
139
  writer.writeInt32(0x1CB5C415); // vector constructor
140
140
  writer.writeInt32(value.length);
@@ -10,6 +10,14 @@ class TLReader extends _0_tl_raw_reader_js_1.TLRawReader {
10
10
  if (!id) {
11
11
  id = this.readInt32(false);
12
12
  }
13
+ if (id == 0x1CB5C415) {
14
+ const count = this.readInt32();
15
+ const items = new Array();
16
+ for (let i = 0; i < count; i++) {
17
+ items.push(this.readObject());
18
+ }
19
+ return items;
20
+ }
13
21
  const constructor = _2_types_js_1.map.get(id);
14
22
  if (constructor) {
15
23
  return (0, _3_tl_object_deserializer_js_1.deserialize)(this, constructor[_1_tl_object_js_1.paramDesc], constructor);
@@ -1,10 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TLWriter = void 0;
4
+ const constants_js_1 = require("../constants.js");
4
5
  const _0_tl_raw_writer_js_1 = require("./0_tl_raw_writer.js");
5
6
  class TLWriter extends _0_tl_raw_writer_js_1.TLRawWriter {
6
7
  writeObject(object) {
7
- this.write(object.serialize());
8
+ if (Array.isArray(object)) {
9
+ this.writeInt32(constants_js_1.VECTOR_CONSTRUCTOR, false);
10
+ this.writeInt32(object.length);
11
+ for (const item of object) {
12
+ this.writeObject(item);
13
+ }
14
+ }
15
+ else {
16
+ this.write(object.serialize());
17
+ }
8
18
  return this;
9
19
  }
10
20
  }
@@ -5,6 +5,21 @@ const _1_tl_object_js_1 = require("./1_tl_object.js");
5
5
  const _3_tl_reader_js_1 = require("./3_tl_reader.js");
6
6
  const _3_tl_writer_js_1 = require("./3_tl_writer.js");
7
7
  const _4_rpc_result_js_1 = require("./4_rpc_result.js");
8
+ // TODO: test
9
+ function calculateLength(object) {
10
+ let length = 0;
11
+ if (Array.isArray(object)) {
12
+ length += 32 / 8; // vector constructor
13
+ length += 32 / 8; // number of items
14
+ for (const item of object) {
15
+ length += calculateLength(item);
16
+ }
17
+ }
18
+ else {
19
+ length += object.serialize().length;
20
+ }
21
+ return length;
22
+ }
8
23
  class Message {
9
24
  constructor(id, seqNo, body) {
10
25
  Object.defineProperty(this, "id", {
@@ -33,7 +48,7 @@ class Message {
33
48
  return new _3_tl_writer_js_1.TLWriter()
34
49
  .writeInt64(this.id)
35
50
  .writeInt32(this.seqNo)
36
- .writeInt32(this.body[_1_tl_object_js_1.length])
51
+ .writeInt32(calculateLength(this.body))
37
52
  .writeObject(this.body)
38
53
  .buffer;
39
54
  }
@@ -1,2 +1,3 @@
1
1
  export declare const ackThreshold = 10;
2
2
  export declare const publicKeys: Map<bigint, [bigint, bigint]>;
3
+ export declare const VECTOR_CONSTRUCTOR = 481674261;
@@ -28,6 +28,7 @@ export declare abstract class TLObject {
28
28
  get [length](): number;
29
29
  serialize(): Uint8Array;
30
30
  }
31
+ export type MaybeVectorTLObject = TLObject | Array<MaybeVectorTLObject | TLObject>;
31
32
  export interface TLObjectConstructor<T = TLObject> {
32
33
  new (params: Record<string, Param>): T;
33
34
  [paramDesc]: ParamDesc;
@@ -1,4 +1,6 @@
1
1
  import { TLRawReader } from "./0_tl_raw_reader.js";
2
+ import { TLObject } from "./1_tl_object.js";
3
+ export type ReadObject = TLObject | Array<ReadObject | TLObject>;
2
4
  export declare class TLReader extends TLRawReader {
3
- readObject(id?: number): import("./1_tl_object.js").TLObject;
5
+ readObject(id?: number): ReadObject;
4
6
  }
@@ -1,5 +1,5 @@
1
1
  import { TLRawWriter } from "./0_tl_raw_writer.js";
2
- import { TLObject } from "./1_tl_object.js";
2
+ import { MaybeVectorTLObject } from "./1_tl_object.js";
3
3
  export declare class TLWriter extends TLRawWriter {
4
- writeObject(object: TLObject): this;
4
+ writeObject(object: MaybeVectorTLObject): this;
5
5
  }
@@ -1,8 +1,8 @@
1
- import { id, TLObject } from "./1_tl_object.js";
1
+ import { id, MaybeVectorTLObject } from "./1_tl_object.js";
2
2
  export declare class RPCResult {
3
3
  readonly messageId: bigint;
4
- readonly result: TLObject;
4
+ readonly result: MaybeVectorTLObject;
5
5
  static get [id](): number;
6
- constructor(messageId: bigint, result: TLObject);
6
+ constructor(messageId: bigint, result: MaybeVectorTLObject);
7
7
  static deserialize(buffer: Uint8Array): RPCResult;
8
8
  }
@@ -1,11 +1,11 @@
1
- import { TLObject } from "./1_tl_object.js";
1
+ import { MaybeVectorTLObject } from "./1_tl_object.js";
2
2
  import { TLReader } from "./3_tl_reader.js";
3
3
  import { RPCResult } from "./4_rpc_result.js";
4
4
  export declare class Message {
5
5
  readonly id: bigint;
6
6
  readonly seqNo: number;
7
- readonly body: TLObject | RPCResult;
8
- constructor(id: bigint, seqNo: number, body: TLObject | RPCResult);
7
+ readonly body: MaybeVectorTLObject | RPCResult;
8
+ constructor(id: bigint, seqNo: number, body: MaybeVectorTLObject | RPCResult);
9
9
  serialize(): Uint8Array;
10
10
  static deserialize(reader: TLReader): Message;
11
11
  }