@mtkruto/browser 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>;
@@ -32,17 +32,35 @@ export class ConnectionWebSocket {
32
32
  writable: true,
33
33
  value: null
34
34
  });
35
+ Object.defineProperty(this, "isConnecting", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: false
40
+ });
41
+ Object.defineProperty(this, "connectionError", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: null
46
+ });
35
47
  this.webSocket = this.reinitWs(url);
36
48
  // TODO
37
- this.webSocket.onclose = () => {
38
- this.webSocket = this.reinitWs(url);
39
- };
49
+ this.webSocket.addEventListener("close", (e) => {
50
+ if (e.code != 1000 && e.reason != "method") {
51
+ this.webSocket = this.reinitWs(url);
52
+ }
53
+ });
40
54
  }
41
55
  reinitWs(url) {
42
56
  const webSocket = new WebSocket(url, "binary");
43
- webSocket.onmessage = async (e) => {
44
- // deno-lint-ignore no-explicit-any
45
- const data = e.data instanceof Blob ? new Uint8Array(await e.data.arrayBuffer()) : new Uint8Array(e.data);
57
+ const mutex = new Mutex();
58
+ webSocket.addEventListener("message", async (e) => {
59
+ if (typeof e.data === "string") {
60
+ return;
61
+ }
62
+ const release = await mutex.acquire();
63
+ 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());
46
64
  for (const byte of data) {
47
65
  this.buffer.push(byte);
48
66
  }
@@ -50,24 +68,44 @@ export class ConnectionWebSocket {
50
68
  this.nextResolve[1]();
51
69
  this.nextResolve = null;
52
70
  }
53
- };
54
- webSocket.onerror = (err) => {
71
+ release();
72
+ });
73
+ webSocket.addEventListener("error", (err) => {
74
+ if (this.isConnecting) {
75
+ // @ts-ignore: Node.js
76
+ this.connectionError = err;
77
+ }
55
78
  d("WebSocket error: %o", err);
56
- };
79
+ });
57
80
  return webSocket;
58
81
  }
59
82
  get connected() {
60
83
  return this.webSocket.readyState == WebSocket.OPEN;
61
84
  }
62
85
  async open() {
63
- while (this.webSocket.readyState != WebSocket.OPEN) {
64
- if (this.webSocket.readyState == WebSocket.CLOSED) {
65
- throw new Error("Connection was closed");
66
- }
67
- else {
68
- await new Promise((r) => setTimeout(r, 5));
86
+ if (this.isConnecting) {
87
+ throw new Error("Already connecting");
88
+ }
89
+ this.isConnecting = true;
90
+ try {
91
+ while (this.webSocket.readyState != WebSocket.OPEN) {
92
+ if (this.webSocket.readyState == WebSocket.CLOSED) {
93
+ if (this.connectionError instanceof ErrorEvent) {
94
+ throw new Error(this.connectionError.message);
95
+ }
96
+ else {
97
+ throw new Error("Connection was closed");
98
+ }
99
+ }
100
+ else {
101
+ await new Promise((r) => setTimeout(r, 5));
102
+ }
69
103
  }
70
104
  }
105
+ finally {
106
+ this.isConnecting = false;
107
+ this.connectionError = null;
108
+ }
71
109
  }
72
110
  async read(p) {
73
111
  if (this.webSocket.readyState != WebSocket.OPEN) {
@@ -100,6 +138,7 @@ export class ConnectionWebSocket {
100
138
  if (this.webSocket.readyState == WebSocket.CLOSED) {
101
139
  throw new Error("Connection not open");
102
140
  }
103
- this.webSocket.close();
141
+ this.webSocket.close(1000, "method");
142
+ console.trace("close called");
104
143
  }
105
144
  }
@@ -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
@@ -61,7 +61,7 @@ export const publicKeys = new Map([
61
61
  export const VECTOR_CONSTRUCTOR = 0x1CB5C415;
62
62
  export const DEFAULT_INITIAL_DC = "2-test";
63
63
  export const LAYER = 158;
64
- export const DEFAULT_APP_VERSION = "MTKruto 0.0.964";
64
+ export const DEFAULT_APP_VERSION = "MTKruto 0.0.965";
65
65
  // @ts-ignore: lib
66
66
  export const DEFAULT_DEVICE_MODEL = typeof Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : Deno.build.os + "-" + Deno.build.arch;
67
67
  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/browser",
5
- "version": "0.0.964",
5
+ "version": "0.0.965",
6
6
  "description": "MTKruto for browsers",
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>;
@@ -35,17 +35,35 @@ class ConnectionWebSocket {
35
35
  writable: true,
36
36
  value: null
37
37
  });
38
+ Object.defineProperty(this, "isConnecting", {
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true,
42
+ value: false
43
+ });
44
+ Object.defineProperty(this, "connectionError", {
45
+ enumerable: true,
46
+ configurable: true,
47
+ writable: true,
48
+ value: null
49
+ });
38
50
  this.webSocket = this.reinitWs(url);
39
51
  // TODO
40
- this.webSocket.onclose = () => {
41
- this.webSocket = this.reinitWs(url);
42
- };
52
+ this.webSocket.addEventListener("close", (e) => {
53
+ if (e.code != 1000 && e.reason != "method") {
54
+ this.webSocket = this.reinitWs(url);
55
+ }
56
+ });
43
57
  }
44
58
  reinitWs(url) {
45
59
  const webSocket = new WebSocket(url, "binary");
46
- webSocket.onmessage = async (e) => {
47
- // deno-lint-ignore no-explicit-any
48
- const data = e.data instanceof Blob ? new Uint8Array(await e.data.arrayBuffer()) : new Uint8Array(e.data);
60
+ const mutex = new deps_js_1.Mutex();
61
+ webSocket.addEventListener("message", async (e) => {
62
+ if (typeof e.data === "string") {
63
+ return;
64
+ }
65
+ const release = await mutex.acquire();
66
+ 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());
49
67
  for (const byte of data) {
50
68
  this.buffer.push(byte);
51
69
  }
@@ -53,24 +71,44 @@ class ConnectionWebSocket {
53
71
  this.nextResolve[1]();
54
72
  this.nextResolve = null;
55
73
  }
56
- };
57
- webSocket.onerror = (err) => {
74
+ release();
75
+ });
76
+ webSocket.addEventListener("error", (err) => {
77
+ if (this.isConnecting) {
78
+ // @ts-ignore: Node.js
79
+ this.connectionError = err;
80
+ }
58
81
  d("WebSocket error: %o", err);
59
- };
82
+ });
60
83
  return webSocket;
61
84
  }
62
85
  get connected() {
63
86
  return this.webSocket.readyState == WebSocket.OPEN;
64
87
  }
65
88
  async open() {
66
- while (this.webSocket.readyState != WebSocket.OPEN) {
67
- if (this.webSocket.readyState == WebSocket.CLOSED) {
68
- throw new Error("Connection was closed");
69
- }
70
- else {
71
- await new Promise((r) => setTimeout(r, 5));
89
+ if (this.isConnecting) {
90
+ throw new Error("Already connecting");
91
+ }
92
+ this.isConnecting = true;
93
+ try {
94
+ while (this.webSocket.readyState != WebSocket.OPEN) {
95
+ if (this.webSocket.readyState == WebSocket.CLOSED) {
96
+ if (this.connectionError instanceof ErrorEvent) {
97
+ throw new Error(this.connectionError.message);
98
+ }
99
+ else {
100
+ throw new Error("Connection was closed");
101
+ }
102
+ }
103
+ else {
104
+ await new Promise((r) => setTimeout(r, 5));
105
+ }
72
106
  }
73
107
  }
108
+ finally {
109
+ this.isConnecting = false;
110
+ this.connectionError = null;
111
+ }
74
112
  }
75
113
  async read(p) {
76
114
  if (this.webSocket.readyState != WebSocket.OPEN) {
@@ -103,7 +141,8 @@ class ConnectionWebSocket {
103
141
  if (this.webSocket.readyState == WebSocket.CLOSED) {
104
142
  throw new Error("Connection not open");
105
143
  }
106
- this.webSocket.close();
144
+ this.webSocket.close(1000, "method");
145
+ console.trace("close called");
107
146
  }
108
147
  }
109
148
  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 = "";
@@ -64,7 +64,7 @@ exports.publicKeys = new Map([
64
64
  exports.VECTOR_CONSTRUCTOR = 0x1CB5C415;
65
65
  exports.DEFAULT_INITIAL_DC = "2-test";
66
66
  exports.LAYER = 158;
67
- exports.DEFAULT_APP_VERSION = "MTKruto 0.0.964";
67
+ exports.DEFAULT_APP_VERSION = "MTKruto 0.0.965";
68
68
  // @ts-ignore: lib
69
69
  exports.DEFAULT_DEVICE_MODEL = typeof Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : Deno.build.os + "-" + Deno.build.arch;
70
70
  exports.DEFAULT_LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];