@mtkruto/node 0.1.108 → 0.1.109
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.
- package/esm/0_deps.d.ts +1 -1
- package/esm/0_deps.js +1 -1
- package/esm/4_constants.d.ts +1 -1
- package/esm/4_constants.js +1 -1
- package/esm/client/0_html.js +26 -14
- package/esm/client/0_utilities.d.ts +1 -1
- package/esm/client/0_utilities.js +1 -2
- package/esm/client/1_client_abstract.d.ts +1 -1
- package/esm/client/1_client_abstract.js +16 -8
- package/esm/client/2_client_plain.d.ts +1 -2
- package/esm/client/2_client_plain.js +20 -15
- package/esm/client/4_client.d.ts +4 -55
- package/esm/client/4_client.js +619 -716
- package/esm/connection/1_connection_web_socket.d.ts +1 -9
- package/esm/connection/1_connection_web_socket.js +81 -108
- package/esm/storage/0_storage.d.ts +3 -2
- package/esm/storage/0_storage.js +35 -26
- package/esm/storage/0_utilities.d.ts +1 -1
- package/esm/storage/0_utilities.js +56 -26
- package/esm/storage/1_storage_local_storage.d.ts +1 -1
- package/esm/storage/1_storage_session_storage.d.ts +1 -1
- package/esm/transport/1_transport_abridged.d.ts +1 -2
- package/esm/transport/1_transport_abridged.js +24 -19
- package/esm/transport/1_transport_intermediate.d.ts +1 -2
- package/esm/transport/1_transport_intermediate.js +23 -18
- package/esm/types/0_color.d.ts +2 -1
- package/esm/types/0_color.js +21 -1
- package/esm/types/1_chat.js +5 -5
- package/esm/types/1_user.js +2 -2
- package/esm/utilities/0_base64.js +3 -3
- package/esm/utilities/0_queue.d.ts +2 -4
- package/esm/utilities/0_queue.js +39 -36
- package/package.json +1 -1
- package/script/0_deps.d.ts +1 -1
- package/script/0_deps.js +3 -3
- package/script/4_constants.d.ts +1 -1
- package/script/4_constants.js +1 -1
- package/script/client/0_html.js +26 -14
- package/script/client/0_utilities.d.ts +1 -1
- package/script/client/0_utilities.js +1 -2
- package/script/client/1_client_abstract.d.ts +1 -1
- package/script/client/1_client_abstract.js +16 -8
- package/script/client/2_client_plain.d.ts +1 -2
- package/script/client/2_client_plain.js +20 -15
- package/script/client/4_client.d.ts +4 -55
- package/script/client/4_client.js +619 -716
- package/script/connection/1_connection_web_socket.d.ts +1 -9
- package/script/connection/1_connection_web_socket.js +81 -108
- package/script/storage/0_storage.d.ts +3 -2
- package/script/storage/0_storage.js +35 -26
- package/script/storage/0_utilities.d.ts +1 -1
- package/script/storage/0_utilities.js +55 -25
- package/script/storage/1_storage_local_storage.d.ts +1 -1
- package/script/storage/1_storage_session_storage.d.ts +1 -1
- package/script/transport/1_transport_abridged.d.ts +1 -2
- package/script/transport/1_transport_abridged.js +24 -19
- package/script/transport/1_transport_intermediate.d.ts +1 -2
- package/script/transport/1_transport_intermediate.js +23 -18
- package/script/types/0_color.d.ts +2 -1
- package/script/types/0_color.js +24 -3
- package/script/types/1_chat.js +4 -4
- package/script/types/1_user.js +1 -1
- package/script/utilities/0_base64.js +2 -2
- package/script/utilities/0_queue.d.ts +2 -4
- package/script/utilities/0_queue.js +39 -36
|
@@ -1,17 +1,9 @@
|
|
|
1
1
|
import { ConnectionUnframed } from "./0_connection.js";
|
|
2
2
|
export declare class ConnectionWebSocket extends ConnectionUnframed implements ConnectionUnframed {
|
|
3
|
+
#private;
|
|
3
4
|
private readonly url;
|
|
4
|
-
private webSocket;
|
|
5
|
-
private rMutex;
|
|
6
|
-
private wMutex;
|
|
7
|
-
private buffer;
|
|
8
|
-
private nextResolve;
|
|
9
5
|
constructor(url: string | URL);
|
|
10
|
-
private initWs;
|
|
11
6
|
get connected(): boolean;
|
|
12
|
-
private wasConnected;
|
|
13
|
-
private isConnecting;
|
|
14
|
-
private connectionError;
|
|
15
7
|
open(): Promise<void>;
|
|
16
8
|
read(p: Uint8Array): Promise<void>;
|
|
17
9
|
write(p: Uint8Array): Promise<void>;
|
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
7
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
|
+
};
|
|
12
|
+
var _ConnectionWebSocket_instances, _ConnectionWebSocket_webSocket, _ConnectionWebSocket_rMutex, _ConnectionWebSocket_wMutex, _ConnectionWebSocket_buffer, _ConnectionWebSocket_nextResolve, _ConnectionWebSocket_initWs, _ConnectionWebSocket_wasConnected, _ConnectionWebSocket_isConnecting, _ConnectionWebSocket_connectionError;
|
|
1
13
|
import * as dntShim from "../_dnt.shims.js";
|
|
2
14
|
import { debug, Mutex } from "../0_deps.js";
|
|
3
15
|
import { UNREACHABLE } from "../1_utilities.js";
|
|
@@ -6,113 +18,39 @@ const d = debug("ConnectionWebSocket");
|
|
|
6
18
|
export class ConnectionWebSocket extends ConnectionUnframed {
|
|
7
19
|
constructor(url) {
|
|
8
20
|
super();
|
|
21
|
+
_ConnectionWebSocket_instances.add(this);
|
|
9
22
|
Object.defineProperty(this, "url", {
|
|
10
23
|
enumerable: true,
|
|
11
24
|
configurable: true,
|
|
12
25
|
writable: true,
|
|
13
26
|
value: url
|
|
14
27
|
});
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
writable: true,
|
|
25
|
-
value: new Mutex()
|
|
26
|
-
});
|
|
27
|
-
Object.defineProperty(this, "wMutex", {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
configurable: true,
|
|
30
|
-
writable: true,
|
|
31
|
-
value: new Mutex()
|
|
32
|
-
});
|
|
33
|
-
Object.defineProperty(this, "buffer", {
|
|
34
|
-
enumerable: true,
|
|
35
|
-
configurable: true,
|
|
36
|
-
writable: true,
|
|
37
|
-
value: new Array()
|
|
38
|
-
});
|
|
39
|
-
Object.defineProperty(this, "nextResolve", {
|
|
40
|
-
enumerable: true,
|
|
41
|
-
configurable: true,
|
|
42
|
-
writable: true,
|
|
43
|
-
value: null
|
|
44
|
-
});
|
|
45
|
-
Object.defineProperty(this, "wasConnected", {
|
|
46
|
-
enumerable: true,
|
|
47
|
-
configurable: true,
|
|
48
|
-
writable: true,
|
|
49
|
-
value: false
|
|
50
|
-
});
|
|
51
|
-
Object.defineProperty(this, "isConnecting", {
|
|
52
|
-
enumerable: true,
|
|
53
|
-
configurable: true,
|
|
54
|
-
writable: true,
|
|
55
|
-
value: false
|
|
56
|
-
});
|
|
57
|
-
Object.defineProperty(this, "connectionError", {
|
|
58
|
-
enumerable: true,
|
|
59
|
-
configurable: true,
|
|
60
|
-
writable: true,
|
|
61
|
-
value: null
|
|
62
|
-
});
|
|
63
|
-
this.webSocket = this.initWs();
|
|
64
|
-
}
|
|
65
|
-
initWs() {
|
|
66
|
-
const webSocket = new dntShim.WebSocket(this.url, "binary");
|
|
67
|
-
const mutex = new Mutex();
|
|
68
|
-
webSocket.addEventListener("close", () => {
|
|
69
|
-
this.stateChangeHandler?.(false);
|
|
70
|
-
});
|
|
71
|
-
webSocket.addEventListener("open", () => {
|
|
72
|
-
this.stateChangeHandler?.(true);
|
|
73
|
-
});
|
|
74
|
-
webSocket.addEventListener("message", async (e) => {
|
|
75
|
-
if (typeof e.data === "string") {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
const release = await mutex.acquire();
|
|
79
|
-
const data = new Uint8Array(await new Blob([e.data].map((v) => v instanceof Blob || v instanceof Uint8Array ? v : v instanceof ArrayBuffer ? v : UNREACHABLE())).arrayBuffer());
|
|
80
|
-
for (const byte of data) {
|
|
81
|
-
this.buffer.push(byte);
|
|
82
|
-
}
|
|
83
|
-
if (this.nextResolve != null && this.buffer.length >= this.nextResolve[0]) {
|
|
84
|
-
this.nextResolve[1].resolve();
|
|
85
|
-
this.nextResolve = null;
|
|
86
|
-
}
|
|
87
|
-
release();
|
|
88
|
-
});
|
|
89
|
-
webSocket.addEventListener("error", (err) => {
|
|
90
|
-
if (this.isConnecting) {
|
|
91
|
-
// @ts-ignore: Node.js
|
|
92
|
-
this.connectionError = err;
|
|
93
|
-
}
|
|
94
|
-
if (this.connected) {
|
|
95
|
-
d("WebSocket error: %o", err);
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
return webSocket;
|
|
28
|
+
_ConnectionWebSocket_webSocket.set(this, void 0);
|
|
29
|
+
_ConnectionWebSocket_rMutex.set(this, new Mutex());
|
|
30
|
+
_ConnectionWebSocket_wMutex.set(this, new Mutex());
|
|
31
|
+
_ConnectionWebSocket_buffer.set(this, new Array());
|
|
32
|
+
_ConnectionWebSocket_nextResolve.set(this, null);
|
|
33
|
+
_ConnectionWebSocket_wasConnected.set(this, false);
|
|
34
|
+
_ConnectionWebSocket_isConnecting.set(this, false);
|
|
35
|
+
_ConnectionWebSocket_connectionError.set(this, null);
|
|
36
|
+
__classPrivateFieldSet(this, _ConnectionWebSocket_webSocket, __classPrivateFieldGet(this, _ConnectionWebSocket_instances, "m", _ConnectionWebSocket_initWs).call(this), "f");
|
|
99
37
|
}
|
|
100
38
|
get connected() {
|
|
101
|
-
return this.
|
|
39
|
+
return __classPrivateFieldGet(this, _ConnectionWebSocket_webSocket, "f").readyState == dntShim.WebSocket.OPEN;
|
|
102
40
|
}
|
|
103
41
|
async open() {
|
|
104
|
-
if (this
|
|
42
|
+
if (__classPrivateFieldGet(this, _ConnectionWebSocket_isConnecting, "f")) {
|
|
105
43
|
throw new Error("Already connecting");
|
|
106
44
|
}
|
|
107
|
-
this
|
|
108
|
-
if (!this.connected && this
|
|
109
|
-
this
|
|
45
|
+
__classPrivateFieldSet(this, _ConnectionWebSocket_isConnecting, true, "f");
|
|
46
|
+
if (!this.connected && __classPrivateFieldGet(this, _ConnectionWebSocket_wasConnected, "f")) {
|
|
47
|
+
__classPrivateFieldSet(this, _ConnectionWebSocket_webSocket, __classPrivateFieldGet(this, _ConnectionWebSocket_instances, "m", _ConnectionWebSocket_initWs).call(this), "f");
|
|
110
48
|
}
|
|
111
49
|
try {
|
|
112
|
-
while (this.
|
|
113
|
-
if (this.
|
|
114
|
-
if (this
|
|
115
|
-
throw new Error(this.
|
|
50
|
+
while (__classPrivateFieldGet(this, _ConnectionWebSocket_webSocket, "f").readyState != dntShim.WebSocket.OPEN) {
|
|
51
|
+
if (__classPrivateFieldGet(this, _ConnectionWebSocket_webSocket, "f").readyState == dntShim.WebSocket.CLOSED) {
|
|
52
|
+
if (__classPrivateFieldGet(this, _ConnectionWebSocket_connectionError, "f") instanceof ErrorEvent) {
|
|
53
|
+
throw new Error(__classPrivateFieldGet(this, _ConnectionWebSocket_connectionError, "f").message);
|
|
116
54
|
}
|
|
117
55
|
else {
|
|
118
56
|
throw new Error("Connection was closed");
|
|
@@ -122,47 +60,82 @@ export class ConnectionWebSocket extends ConnectionUnframed {
|
|
|
122
60
|
await new Promise((r) => setTimeout(r, 5));
|
|
123
61
|
}
|
|
124
62
|
}
|
|
125
|
-
this
|
|
63
|
+
__classPrivateFieldSet(this, _ConnectionWebSocket_wasConnected, true, "f");
|
|
126
64
|
}
|
|
127
65
|
finally {
|
|
128
|
-
this
|
|
129
|
-
this
|
|
66
|
+
__classPrivateFieldSet(this, _ConnectionWebSocket_isConnecting, false, "f");
|
|
67
|
+
__classPrivateFieldSet(this, _ConnectionWebSocket_connectionError, null, "f");
|
|
130
68
|
}
|
|
131
69
|
}
|
|
132
70
|
async read(p) {
|
|
133
|
-
if (this.
|
|
71
|
+
if (__classPrivateFieldGet(this, _ConnectionWebSocket_webSocket, "f").readyState != dntShim.WebSocket.OPEN) {
|
|
134
72
|
throw new Error("Connection not open");
|
|
135
73
|
}
|
|
136
|
-
const release = await this.
|
|
74
|
+
const release = await __classPrivateFieldGet(this, _ConnectionWebSocket_rMutex, "f").acquire();
|
|
137
75
|
try {
|
|
138
|
-
if (this.
|
|
139
|
-
await new Promise((resolve, reject) => this
|
|
76
|
+
if (__classPrivateFieldGet(this, _ConnectionWebSocket_buffer, "f").length < p.length) {
|
|
77
|
+
await new Promise((resolve, reject) => __classPrivateFieldSet(this, _ConnectionWebSocket_nextResolve, [p.length, { resolve, reject }], "f"));
|
|
140
78
|
}
|
|
141
|
-
p.set(this.
|
|
79
|
+
p.set(__classPrivateFieldGet(this, _ConnectionWebSocket_buffer, "f").splice(0, p.length));
|
|
142
80
|
}
|
|
143
81
|
finally {
|
|
144
82
|
release();
|
|
145
83
|
}
|
|
146
84
|
}
|
|
147
85
|
async write(p) {
|
|
148
|
-
if (this.
|
|
86
|
+
if (__classPrivateFieldGet(this, _ConnectionWebSocket_webSocket, "f").readyState == dntShim.WebSocket.CLOSED) {
|
|
149
87
|
throw new Error("Connection not open");
|
|
150
88
|
}
|
|
151
|
-
const release = await this.
|
|
89
|
+
const release = await __classPrivateFieldGet(this, _ConnectionWebSocket_wMutex, "f").acquire();
|
|
152
90
|
try {
|
|
153
|
-
this.
|
|
91
|
+
__classPrivateFieldGet(this, _ConnectionWebSocket_webSocket, "f").send(p);
|
|
154
92
|
}
|
|
155
93
|
finally {
|
|
156
94
|
release();
|
|
157
95
|
}
|
|
158
96
|
}
|
|
159
97
|
close() {
|
|
160
|
-
if (this.
|
|
98
|
+
if (__classPrivateFieldGet(this, _ConnectionWebSocket_webSocket, "f").readyState == dntShim.WebSocket.CLOSED) {
|
|
161
99
|
throw new Error("Connection not open");
|
|
162
100
|
}
|
|
163
|
-
this.
|
|
164
|
-
if (this
|
|
165
|
-
this
|
|
101
|
+
__classPrivateFieldGet(this, _ConnectionWebSocket_webSocket, "f").close(1000, "method");
|
|
102
|
+
if (__classPrivateFieldGet(this, _ConnectionWebSocket_nextResolve, "f") != null) {
|
|
103
|
+
__classPrivateFieldGet(this, _ConnectionWebSocket_nextResolve, "f")[1].reject(new Error("Connection not open"));
|
|
166
104
|
}
|
|
167
105
|
}
|
|
168
106
|
}
|
|
107
|
+
_ConnectionWebSocket_webSocket = new WeakMap(), _ConnectionWebSocket_rMutex = new WeakMap(), _ConnectionWebSocket_wMutex = new WeakMap(), _ConnectionWebSocket_buffer = new WeakMap(), _ConnectionWebSocket_nextResolve = new WeakMap(), _ConnectionWebSocket_wasConnected = new WeakMap(), _ConnectionWebSocket_isConnecting = new WeakMap(), _ConnectionWebSocket_connectionError = new WeakMap(), _ConnectionWebSocket_instances = new WeakSet(), _ConnectionWebSocket_initWs = function _ConnectionWebSocket_initWs() {
|
|
108
|
+
const webSocket = new dntShim.WebSocket(this.url, "binary");
|
|
109
|
+
const mutex = new Mutex();
|
|
110
|
+
webSocket.addEventListener("close", () => {
|
|
111
|
+
this.stateChangeHandler?.(false);
|
|
112
|
+
});
|
|
113
|
+
webSocket.addEventListener("open", () => {
|
|
114
|
+
this.stateChangeHandler?.(true);
|
|
115
|
+
});
|
|
116
|
+
webSocket.addEventListener("message", async (e) => {
|
|
117
|
+
if (typeof e.data === "string") {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const release = await mutex.acquire();
|
|
121
|
+
const data = new Uint8Array(await new Blob([e.data].map((v) => v instanceof Blob || v instanceof Uint8Array ? v : v instanceof ArrayBuffer ? v : UNREACHABLE())).arrayBuffer());
|
|
122
|
+
for (const byte of data) {
|
|
123
|
+
__classPrivateFieldGet(this, _ConnectionWebSocket_buffer, "f").push(byte);
|
|
124
|
+
}
|
|
125
|
+
if (__classPrivateFieldGet(this, _ConnectionWebSocket_nextResolve, "f") != null && __classPrivateFieldGet(this, _ConnectionWebSocket_buffer, "f").length >= __classPrivateFieldGet(this, _ConnectionWebSocket_nextResolve, "f")[0]) {
|
|
126
|
+
__classPrivateFieldGet(this, _ConnectionWebSocket_nextResolve, "f")[1].resolve();
|
|
127
|
+
__classPrivateFieldSet(this, _ConnectionWebSocket_nextResolve, null, "f");
|
|
128
|
+
}
|
|
129
|
+
release();
|
|
130
|
+
});
|
|
131
|
+
webSocket.addEventListener("error", (err) => {
|
|
132
|
+
if (__classPrivateFieldGet(this, _ConnectionWebSocket_isConnecting, "f")) {
|
|
133
|
+
// @ts-ignore: Node.js
|
|
134
|
+
__classPrivateFieldSet(this, _ConnectionWebSocket_connectionError, err, "f");
|
|
135
|
+
}
|
|
136
|
+
if (this.connected) {
|
|
137
|
+
d("WebSocket error: %o", err);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
return webSocket;
|
|
141
|
+
};
|
|
@@ -3,14 +3,13 @@ import { TLObject, types } from "../2_tl.js";
|
|
|
3
3
|
import { DC } from "../3_transport.js";
|
|
4
4
|
export type StorageKeyPart = string | number | bigint;
|
|
5
5
|
export declare abstract class Storage {
|
|
6
|
-
private
|
|
6
|
+
#private;
|
|
7
7
|
abstract init(): MaybePromise<void>;
|
|
8
8
|
abstract set(key: readonly StorageKeyPart[], value: unknown): MaybePromise<void>;
|
|
9
9
|
abstract get<T>(key: readonly StorageKeyPart[]): MaybePromise<T | null>;
|
|
10
10
|
abstract getMany<T>(prefix: readonly StorageKeyPart[]): MaybePromise<Generator<[readonly StorageKeyPart[], T]> | AsyncGenerator<[readonly StorageKeyPart[], T]>>;
|
|
11
11
|
setDc(dc: DC | null): MaybePromise<void>;
|
|
12
12
|
getDc(): MaybePromise<DC | null>;
|
|
13
|
-
private resetAuthKeyId;
|
|
14
13
|
getAuthKey(): Promise<Uint8Array | null>;
|
|
15
14
|
setAuthKey(authKey: Uint8Array | null): Promise<void>;
|
|
16
15
|
get authKeyId(): bigint | null;
|
|
@@ -41,4 +40,6 @@ export declare abstract class Storage {
|
|
|
41
40
|
getAccountType(): MaybePromise<"bot" | "user" | null>;
|
|
42
41
|
updateStickerSetName(id: bigint, accessHash: bigint, name: string): Promise<void>;
|
|
43
42
|
getStickerSetName(id: bigint, accessHash: bigint): MaybePromise<[string, Date] | null>;
|
|
43
|
+
setServerSalt(serverSalt: bigint): Promise<void>;
|
|
44
|
+
getServerSalt(): MaybePromise<bigint | null>;
|
|
44
45
|
}
|
package/esm/storage/0_storage.js
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _Storage_instances, _Storage__authKeyId, _Storage_resetAuthKeyId;
|
|
1
13
|
import { bigIntFromBuffer, rleDecode, rleEncode, sha1, UNREACHABLE } from "../1_utilities.js";
|
|
2
14
|
import { serialize, TLReader, types } from "../2_tl.js";
|
|
3
15
|
import { ZERO_CHANNEL_ID } from "../4_constants.js";
|
|
@@ -15,12 +27,8 @@ const KPARTS_MESSAGE = (chatId, messageId) => ["messages", chatId, messageId];
|
|
|
15
27
|
const KPARTS_MESSAGE_REF = (messageId) => ["messageRefs", messageId];
|
|
16
28
|
export class Storage {
|
|
17
29
|
constructor() {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
configurable: true,
|
|
21
|
-
writable: true,
|
|
22
|
-
value: null
|
|
23
|
-
});
|
|
30
|
+
_Storage_instances.add(this);
|
|
31
|
+
_Storage__authKeyId.set(this, null);
|
|
24
32
|
}
|
|
25
33
|
setDc(dc) {
|
|
26
34
|
return this.set(KPARTS__DC, dc);
|
|
@@ -28,25 +36,17 @@ export class Storage {
|
|
|
28
36
|
getDc() {
|
|
29
37
|
return this.get(KPARTS__DC);
|
|
30
38
|
}
|
|
31
|
-
async resetAuthKeyId(authKey) {
|
|
32
|
-
if (authKey != null) {
|
|
33
|
-
this._authKeyId = await sha1(authKey).then((hash) => bigIntFromBuffer(hash.slice(-8), true, false));
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
this._authKeyId = null;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
39
|
async getAuthKey() {
|
|
40
40
|
const authKey = await this.get(KPARTS__AUTH_KEY);
|
|
41
|
-
await this.
|
|
41
|
+
await __classPrivateFieldGet(this, _Storage_instances, "m", _Storage_resetAuthKeyId).call(this, authKey);
|
|
42
42
|
return authKey;
|
|
43
43
|
}
|
|
44
44
|
async setAuthKey(authKey) {
|
|
45
45
|
await this.set(KPARTS__AUTH_KEY, authKey);
|
|
46
|
-
await this.
|
|
46
|
+
await __classPrivateFieldGet(this, _Storage_instances, "m", _Storage_resetAuthKeyId).call(this, authKey);
|
|
47
47
|
}
|
|
48
48
|
get authKeyId() {
|
|
49
|
-
return this
|
|
49
|
+
return __classPrivateFieldGet(this, _Storage__authKeyId, "f");
|
|
50
50
|
}
|
|
51
51
|
setChannelAccessHash(id, accessHash) {
|
|
52
52
|
return this.set(KPARTS__CHANNEL_ACCESS_HASH(id), accessHash);
|
|
@@ -63,17 +63,12 @@ export class Storage {
|
|
|
63
63
|
async updateUsernames(type, id, usernames) {
|
|
64
64
|
for (let username of usernames) {
|
|
65
65
|
username = username.toLowerCase();
|
|
66
|
-
await this.set(KPARTS__USERNAME(username), [type,
|
|
66
|
+
await this.set(KPARTS__USERNAME(username), [type, id, new Date()]);
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
async getUsername(username) {
|
|
70
70
|
username = username.toLowerCase();
|
|
71
|
-
|
|
72
|
-
if (v != null) {
|
|
73
|
-
v[1] = BigInt(v[1]);
|
|
74
|
-
v[2] = new Date(v[2]);
|
|
75
|
-
}
|
|
76
|
-
return v;
|
|
71
|
+
return await this.get(KPARTS__USERNAME(username));
|
|
77
72
|
}
|
|
78
73
|
async setTlObject(key, value) {
|
|
79
74
|
if (value == null) {
|
|
@@ -125,12 +120,12 @@ export class Storage {
|
|
|
125
120
|
}
|
|
126
121
|
async setEntity(peer) {
|
|
127
122
|
const type = peer instanceof types.Channel ? "channel" : peer instanceof types.Chat ? "chat" : peer instanceof types.User ? "user" : UNREACHABLE();
|
|
128
|
-
await this.set(KPARTS__PEER(type, peer.id), peer[serialize]());
|
|
123
|
+
await this.set(KPARTS__PEER(type, peer.id), rleEncode(peer[serialize]()));
|
|
129
124
|
}
|
|
130
125
|
async getEntity(type, id) {
|
|
131
126
|
const peer_ = await this.get(KPARTS__PEER(type, id));
|
|
132
127
|
if (peer_ != null) {
|
|
133
|
-
return new TLReader(peer_).readObject();
|
|
128
|
+
return new TLReader(rleDecode(peer_)).readObject();
|
|
134
129
|
}
|
|
135
130
|
else {
|
|
136
131
|
return null;
|
|
@@ -159,4 +154,18 @@ export class Storage {
|
|
|
159
154
|
getStickerSetName(id, accessHash) {
|
|
160
155
|
return this.get(KPARTS__STICKER_SET_NAME(id, accessHash));
|
|
161
156
|
}
|
|
157
|
+
async setServerSalt(serverSalt) {
|
|
158
|
+
await this.set(["serverSalt"], serverSalt);
|
|
159
|
+
}
|
|
160
|
+
getServerSalt() {
|
|
161
|
+
return this.get(["serverSalt"]);
|
|
162
|
+
}
|
|
162
163
|
}
|
|
164
|
+
_Storage__authKeyId = new WeakMap(), _Storage_instances = new WeakSet(), _Storage_resetAuthKeyId = async function _Storage_resetAuthKeyId(authKey) {
|
|
165
|
+
if (authKey != null) {
|
|
166
|
+
__classPrivateFieldSet(this, _Storage__authKeyId, await sha1(authKey).then((hash) => bigIntFromBuffer(hash.slice(-8), true, false)), "f");
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
__classPrivateFieldSet(this, _Storage__authKeyId, null, "f");
|
|
170
|
+
}
|
|
171
|
+
};
|
|
@@ -9,6 +9,6 @@ export declare enum ValueType {
|
|
|
9
9
|
Array = 6
|
|
10
10
|
}
|
|
11
11
|
export declare function toString(value: unknown): string;
|
|
12
|
-
export declare function fromString<T>(string: string):
|
|
12
|
+
export declare function fromString<T>(string: string): T;
|
|
13
13
|
export declare function fixKey(key: readonly StorageKeyPart[]): (string | number)[];
|
|
14
14
|
export declare function getPrefixKeyRange(prefix: any): IDBKeyRange;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { decodeBase64, encodeBase64 } from "../0_deps.js";
|
|
2
2
|
import { UNREACHABLE } from "../1_utilities.js";
|
|
3
3
|
export var ValueType;
|
|
4
4
|
(function (ValueType) {
|
|
@@ -12,49 +12,79 @@ export var ValueType;
|
|
|
12
12
|
})(ValueType || (ValueType = {}));
|
|
13
13
|
export function toString(value) {
|
|
14
14
|
if (typeof value === "boolean") {
|
|
15
|
-
return
|
|
15
|
+
return `${ValueType.Boolean}${Number(value)}`;
|
|
16
16
|
}
|
|
17
17
|
else if (typeof value === "number") {
|
|
18
|
-
return
|
|
18
|
+
return `${ValueType.Number}${value}`;
|
|
19
19
|
}
|
|
20
20
|
else if (typeof value === "string") {
|
|
21
|
-
return
|
|
21
|
+
return `${ValueType.String}${value}`;
|
|
22
22
|
}
|
|
23
23
|
else if (typeof value == "bigint") {
|
|
24
|
-
return
|
|
24
|
+
return `${ValueType.BigInt}${value}`;
|
|
25
25
|
}
|
|
26
26
|
else if (value instanceof Date) {
|
|
27
|
-
return
|
|
27
|
+
return `${ValueType.Date}${value.getTime()}`;
|
|
28
28
|
}
|
|
29
29
|
else if (value instanceof Uint8Array) {
|
|
30
|
-
return
|
|
30
|
+
return `${ValueType.Uint8Array}${encodeBase64(value)}`;
|
|
31
31
|
}
|
|
32
32
|
else if (Array.isArray(value)) {
|
|
33
|
-
|
|
33
|
+
const items = value.map((v) => {
|
|
34
|
+
if (typeof v === "string" || v instanceof Uint8Array || Array.isArray(v)) {
|
|
35
|
+
const s = toString(v).slice(1);
|
|
36
|
+
return String(typeof v === "string" ? ValueType.String : v instanceof Uint8Array ? ValueType.Uint8Array : ValueType.Array) + toString(s.length).slice(1) + "\n" + s;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
return toString(v);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return `${ValueType.Array}${items.join("\n")}`;
|
|
34
43
|
}
|
|
35
44
|
else {
|
|
36
45
|
UNREACHABLE();
|
|
37
46
|
}
|
|
38
47
|
}
|
|
39
48
|
export function fromString(string) {
|
|
40
|
-
const [type, value] =
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
49
|
+
const [type, value] = [Number(string[0]), string.slice(1)];
|
|
50
|
+
switch (type) {
|
|
51
|
+
case ValueType.Boolean:
|
|
52
|
+
return Boolean(Number(value));
|
|
53
|
+
case ValueType.Number:
|
|
54
|
+
return Number(value);
|
|
55
|
+
case ValueType.String:
|
|
56
|
+
return value;
|
|
57
|
+
case ValueType.BigInt:
|
|
58
|
+
return BigInt(value);
|
|
59
|
+
case ValueType.Date:
|
|
60
|
+
return new Date(Number(value));
|
|
61
|
+
case ValueType.Uint8Array:
|
|
62
|
+
return decodeBase64(value);
|
|
63
|
+
case ValueType.Array: {
|
|
64
|
+
const arr = [];
|
|
65
|
+
for (let i = 0; i < value.length; ++i) {
|
|
66
|
+
const type = Number(value[i]);
|
|
67
|
+
let value_ = "";
|
|
68
|
+
while (value[++i] != "\n") {
|
|
69
|
+
value_ += value[i];
|
|
70
|
+
if (i == value.length - 1) {
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
switch (type) {
|
|
75
|
+
case ValueType.String:
|
|
76
|
+
case ValueType.Uint8Array:
|
|
77
|
+
case ValueType.Array: {
|
|
78
|
+
const len = Number(value_);
|
|
79
|
+
++i;
|
|
80
|
+
value_ = value.slice(i, i + Number(value_));
|
|
81
|
+
i += len;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
arr.push(fromString(`${type}${value_}`));
|
|
85
|
+
}
|
|
86
|
+
return arr;
|
|
87
|
+
}
|
|
58
88
|
}
|
|
59
89
|
}
|
|
60
90
|
export function fixKey(key) {
|
|
@@ -4,7 +4,7 @@ export declare class StorageLocalStorage extends Storage implements Storage {
|
|
|
4
4
|
private readonly prefix;
|
|
5
5
|
constructor(prefix: string);
|
|
6
6
|
init(): void;
|
|
7
|
-
get(key_: readonly StorageKeyPart[]):
|
|
7
|
+
get<T>(key_: readonly StorageKeyPart[]): T | null;
|
|
8
8
|
getMany<T>(prefix: readonly StorageKeyPart[]): Generator<[readonly StorageKeyPart[], T], void, unknown>;
|
|
9
9
|
set(key_: readonly StorageKeyPart[], value: unknown): MaybePromise<void>;
|
|
10
10
|
}
|
|
@@ -4,7 +4,7 @@ export declare class StorageSessionStorage extends Storage implements Storage {
|
|
|
4
4
|
private readonly prefix;
|
|
5
5
|
constructor(prefix: string);
|
|
6
6
|
init(): void;
|
|
7
|
-
get(key_: readonly StorageKeyPart[]):
|
|
7
|
+
get<T>(key_: readonly StorageKeyPart[]): T | null;
|
|
8
8
|
getMany<T>(prefix: readonly StorageKeyPart[]): Generator<[readonly StorageKeyPart[], T], void, unknown>;
|
|
9
9
|
set(key_: readonly StorageKeyPart[], value: unknown): MaybePromise<void>;
|
|
10
10
|
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { Connection } from "../2_connection.js";
|
|
2
2
|
import { Transport } from "./0_transport.js";
|
|
3
3
|
export declare class TransportAbridged extends Transport implements Transport {
|
|
4
|
-
private
|
|
5
|
-
private readonly obfuscated;
|
|
4
|
+
#private;
|
|
6
5
|
constructor(connection: Connection, obfuscated?: boolean);
|
|
7
6
|
initialize(): Promise<void>;
|
|
8
7
|
receive(): Promise<Uint8Array>;
|
|
@@ -1,29 +1,33 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _TransportAbridged_connection, _TransportAbridged_obfuscated;
|
|
1
13
|
import { bufferFromBigInt, concat } from "../1_utilities.js";
|
|
2
14
|
import { getObfuscationParameters } from "./0_obfuscation.js";
|
|
3
15
|
import { Transport } from "./0_transport.js";
|
|
4
16
|
export class TransportAbridged extends Transport {
|
|
5
17
|
constructor(connection, obfuscated = false) {
|
|
6
18
|
super();
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
value: connection
|
|
12
|
-
});
|
|
13
|
-
Object.defineProperty(this, "obfuscated", {
|
|
14
|
-
enumerable: true,
|
|
15
|
-
configurable: true,
|
|
16
|
-
writable: true,
|
|
17
|
-
value: obfuscated
|
|
18
|
-
});
|
|
19
|
+
_TransportAbridged_connection.set(this, void 0);
|
|
20
|
+
_TransportAbridged_obfuscated.set(this, void 0);
|
|
21
|
+
__classPrivateFieldSet(this, _TransportAbridged_connection, connection, "f");
|
|
22
|
+
__classPrivateFieldSet(this, _TransportAbridged_obfuscated, obfuscated, "f");
|
|
19
23
|
}
|
|
20
24
|
async initialize() {
|
|
21
25
|
if (!this.initialized) {
|
|
22
|
-
if (this
|
|
23
|
-
this.obfuscationParameters = await getObfuscationParameters(0xEFEFEFEF, this
|
|
26
|
+
if (__classPrivateFieldGet(this, _TransportAbridged_obfuscated, "f")) {
|
|
27
|
+
this.obfuscationParameters = await getObfuscationParameters(0xEFEFEFEF, __classPrivateFieldGet(this, _TransportAbridged_connection, "f"));
|
|
24
28
|
}
|
|
25
29
|
else {
|
|
26
|
-
await this.
|
|
30
|
+
await __classPrivateFieldGet(this, _TransportAbridged_connection, "f").write(new Uint8Array([0xEF]));
|
|
27
31
|
}
|
|
28
32
|
this.initialized = true;
|
|
29
33
|
}
|
|
@@ -35,14 +39,14 @@ export class TransportAbridged extends Transport {
|
|
|
35
39
|
let length;
|
|
36
40
|
{
|
|
37
41
|
const buffer = new Uint8Array(1);
|
|
38
|
-
await this.
|
|
42
|
+
await __classPrivateFieldGet(this, _TransportAbridged_connection, "f").read(buffer);
|
|
39
43
|
this.decrypt(buffer);
|
|
40
44
|
if (buffer[0] < 0x7F) {
|
|
41
45
|
length = buffer[0];
|
|
42
46
|
}
|
|
43
47
|
else {
|
|
44
48
|
const buffer = new Uint8Array(3);
|
|
45
|
-
await this.
|
|
49
|
+
await __classPrivateFieldGet(this, _TransportAbridged_connection, "f").read(buffer);
|
|
46
50
|
this.decrypt(buffer);
|
|
47
51
|
const dataView = new DataView(buffer.buffer);
|
|
48
52
|
length = dataView.getUint16(0, true);
|
|
@@ -50,7 +54,7 @@ export class TransportAbridged extends Transport {
|
|
|
50
54
|
}
|
|
51
55
|
length *= 4;
|
|
52
56
|
const buffer = new Uint8Array(length);
|
|
53
|
-
await this.
|
|
57
|
+
await __classPrivateFieldGet(this, _TransportAbridged_connection, "f").read(buffer);
|
|
54
58
|
this.decrypt(buffer);
|
|
55
59
|
return buffer;
|
|
56
60
|
}
|
|
@@ -63,9 +67,10 @@ export class TransportAbridged extends Transport {
|
|
|
63
67
|
const length = bufferLength >= 0x7F ? bufferFromBigInt(bufferLength, 3) : new Uint8Array();
|
|
64
68
|
const data = concat(header, length, buffer);
|
|
65
69
|
this.encrypt(data);
|
|
66
|
-
await this.
|
|
70
|
+
await __classPrivateFieldGet(this, _TransportAbridged_connection, "f").write(data);
|
|
67
71
|
}
|
|
68
72
|
deinitialize() {
|
|
69
73
|
this.initialized = false;
|
|
70
74
|
}
|
|
71
75
|
}
|
|
76
|
+
_TransportAbridged_connection = new WeakMap(), _TransportAbridged_obfuscated = new WeakMap();
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { Connection } from "../2_connection.js";
|
|
2
2
|
import { Transport } from "./0_transport.js";
|
|
3
3
|
export declare class TransportIntermediate extends Transport implements Transport {
|
|
4
|
-
private
|
|
5
|
-
private readonly obfuscated;
|
|
4
|
+
#private;
|
|
6
5
|
constructor(connection: Connection, obfuscated?: boolean);
|
|
7
6
|
initialize(): Promise<void>;
|
|
8
7
|
receive(): Promise<Uint8Array>;
|