@mtkruto/node 0.0.964 → 0.0.965

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.
@@ -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.965";
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.965";
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.965",
6
6
  "description": "MTKruto for Node.js",
7
7
  "author": "Roj <rojvv@icloud.com>",
8
8
  "license": "LGPL-3.0-or-later",
@@ -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.965";
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.965";
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];