@mtkruto/browser 0.1.108 → 0.1.110

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.
Files changed (65) hide show
  1. package/esm/0_deps.d.ts +1 -1
  2. package/esm/0_deps.js +1 -1
  3. package/esm/4_constants.d.ts +1 -1
  4. package/esm/4_constants.js +1 -1
  5. package/esm/client/0_html.js +26 -14
  6. package/esm/client/0_utilities.d.ts +1 -1
  7. package/esm/client/0_utilities.js +1 -2
  8. package/esm/client/1_client_abstract.d.ts +1 -1
  9. package/esm/client/1_client_abstract.js +16 -8
  10. package/esm/client/2_client_plain.d.ts +1 -2
  11. package/esm/client/2_client_plain.js +20 -15
  12. package/esm/client/4_client.d.ts +4 -55
  13. package/esm/client/4_client.js +619 -716
  14. package/esm/connection/1_connection_web_socket.d.ts +1 -9
  15. package/esm/connection/1_connection_web_socket.js +92 -114
  16. package/esm/storage/0_storage.d.ts +3 -2
  17. package/esm/storage/0_storage.js +35 -26
  18. package/esm/storage/0_utilities.d.ts +1 -1
  19. package/esm/storage/0_utilities.js +57 -27
  20. package/esm/storage/1_storage_local_storage.d.ts +1 -1
  21. package/esm/storage/1_storage_session_storage.d.ts +1 -1
  22. package/esm/transport/1_transport_abridged.d.ts +1 -2
  23. package/esm/transport/1_transport_abridged.js +24 -19
  24. package/esm/transport/1_transport_intermediate.d.ts +1 -2
  25. package/esm/transport/1_transport_intermediate.js +23 -18
  26. package/esm/types/0_color.d.ts +2 -1
  27. package/esm/types/0_color.js +21 -1
  28. package/esm/types/1_chat.js +5 -5
  29. package/esm/types/1_user.js +2 -2
  30. package/esm/utilities/0_base64.js +3 -3
  31. package/esm/utilities/0_queue.d.ts +2 -4
  32. package/esm/utilities/0_queue.js +39 -36
  33. package/package.json +1 -1
  34. package/script/0_deps.d.ts +1 -1
  35. package/script/0_deps.js +3 -3
  36. package/script/4_constants.d.ts +1 -1
  37. package/script/4_constants.js +1 -1
  38. package/script/client/0_html.js +26 -14
  39. package/script/client/0_utilities.d.ts +1 -1
  40. package/script/client/0_utilities.js +1 -2
  41. package/script/client/1_client_abstract.d.ts +1 -1
  42. package/script/client/1_client_abstract.js +16 -8
  43. package/script/client/2_client_plain.d.ts +1 -2
  44. package/script/client/2_client_plain.js +20 -15
  45. package/script/client/4_client.d.ts +4 -55
  46. package/script/client/4_client.js +619 -716
  47. package/script/connection/1_connection_web_socket.d.ts +1 -9
  48. package/script/connection/1_connection_web_socket.js +93 -115
  49. package/script/storage/0_storage.d.ts +3 -2
  50. package/script/storage/0_storage.js +35 -26
  51. package/script/storage/0_utilities.d.ts +1 -1
  52. package/script/storage/0_utilities.js +56 -26
  53. package/script/storage/1_storage_local_storage.d.ts +1 -1
  54. package/script/storage/1_storage_session_storage.d.ts +1 -1
  55. package/script/transport/1_transport_abridged.d.ts +1 -2
  56. package/script/transport/1_transport_abridged.js +24 -19
  57. package/script/transport/1_transport_intermediate.d.ts +1 -2
  58. package/script/transport/1_transport_intermediate.js +23 -18
  59. package/script/types/0_color.d.ts +2 -1
  60. package/script/types/0_color.js +24 -3
  61. package/script/types/1_chat.js +4 -4
  62. package/script/types/1_user.js +1 -1
  63. package/script/utilities/0_base64.js +2 -2
  64. package/script/utilities/0_queue.d.ts +2 -4
  65. 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,117 +1,56 @@
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, _ConnectionWebSocket_assertConnected, _ConnectionWebSocket_rejectRead;
1
13
  import { debug, Mutex } from "../0_deps.js";
2
14
  import { UNREACHABLE } from "../1_utilities.js";
3
15
  import { ConnectionUnframed } from "./0_connection.js";
4
16
  const d = debug("ConnectionWebSocket");
17
+ const errConnectionNotOpen = new Error("Connection not open");
5
18
  export class ConnectionWebSocket extends ConnectionUnframed {
6
19
  constructor(url) {
7
20
  super();
21
+ _ConnectionWebSocket_instances.add(this);
8
22
  Object.defineProperty(this, "url", {
9
23
  enumerable: true,
10
24
  configurable: true,
11
25
  writable: true,
12
26
  value: url
13
27
  });
14
- Object.defineProperty(this, "webSocket", {
15
- enumerable: true,
16
- configurable: true,
17
- writable: true,
18
- value: void 0
19
- });
20
- Object.defineProperty(this, "rMutex", {
21
- enumerable: true,
22
- configurable: true,
23
- writable: true,
24
- value: new Mutex()
25
- });
26
- Object.defineProperty(this, "wMutex", {
27
- enumerable: true,
28
- configurable: true,
29
- writable: true,
30
- value: new Mutex()
31
- });
32
- Object.defineProperty(this, "buffer", {
33
- enumerable: true,
34
- configurable: true,
35
- writable: true,
36
- value: new Array()
37
- });
38
- Object.defineProperty(this, "nextResolve", {
39
- enumerable: true,
40
- configurable: true,
41
- writable: true,
42
- value: null
43
- });
44
- Object.defineProperty(this, "wasConnected", {
45
- enumerable: true,
46
- configurable: true,
47
- writable: true,
48
- value: false
49
- });
50
- Object.defineProperty(this, "isConnecting", {
51
- enumerable: true,
52
- configurable: true,
53
- writable: true,
54
- value: false
55
- });
56
- Object.defineProperty(this, "connectionError", {
57
- enumerable: true,
58
- configurable: true,
59
- writable: true,
60
- value: null
61
- });
62
- this.webSocket = this.initWs();
63
- }
64
- initWs() {
65
- const webSocket = new WebSocket(this.url, "binary");
66
- const mutex = new Mutex();
67
- webSocket.addEventListener("close", () => {
68
- this.stateChangeHandler?.(false);
69
- });
70
- webSocket.addEventListener("open", () => {
71
- this.stateChangeHandler?.(true);
72
- });
73
- webSocket.addEventListener("message", async (e) => {
74
- if (typeof e.data === "string") {
75
- return;
76
- }
77
- const release = await mutex.acquire();
78
- const data = new Uint8Array(await new Blob([e.data].map((v) => v instanceof Blob || v instanceof Uint8Array ? v : v instanceof ArrayBuffer ? v : UNREACHABLE())).arrayBuffer());
79
- for (const byte of data) {
80
- this.buffer.push(byte);
81
- }
82
- if (this.nextResolve != null && this.buffer.length >= this.nextResolve[0]) {
83
- this.nextResolve[1].resolve();
84
- this.nextResolve = null;
85
- }
86
- release();
87
- });
88
- webSocket.addEventListener("error", (err) => {
89
- if (this.isConnecting) {
90
- // @ts-ignore: Node.js
91
- this.connectionError = err;
92
- }
93
- if (this.connected) {
94
- d("WebSocket error: %o", err);
95
- }
96
- });
97
- 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");
98
37
  }
99
38
  get connected() {
100
- return this.webSocket.readyState == WebSocket.OPEN;
39
+ return __classPrivateFieldGet(this, _ConnectionWebSocket_webSocket, "f").readyState == WebSocket.OPEN;
101
40
  }
102
41
  async open() {
103
- if (this.isConnecting) {
42
+ if (__classPrivateFieldGet(this, _ConnectionWebSocket_isConnecting, "f")) {
104
43
  throw new Error("Already connecting");
105
44
  }
106
- this.isConnecting = true;
107
- if (!this.connected && this.wasConnected) {
108
- this.webSocket = this.initWs();
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");
109
48
  }
110
49
  try {
111
- while (this.webSocket.readyState != WebSocket.OPEN) {
112
- if (this.webSocket.readyState == WebSocket.CLOSED) {
113
- if (this.connectionError instanceof ErrorEvent) {
114
- throw new Error(this.connectionError.message);
50
+ while (__classPrivateFieldGet(this, _ConnectionWebSocket_webSocket, "f").readyState != WebSocket.OPEN) {
51
+ if (__classPrivateFieldGet(this, _ConnectionWebSocket_webSocket, "f").readyState == WebSocket.CLOSED) {
52
+ if (__classPrivateFieldGet(this, _ConnectionWebSocket_connectionError, "f") instanceof ErrorEvent) {
53
+ throw new Error(__classPrivateFieldGet(this, _ConnectionWebSocket_connectionError, "f").message);
115
54
  }
116
55
  else {
117
56
  throw new Error("Connection was closed");
@@ -121,47 +60,86 @@ export class ConnectionWebSocket extends ConnectionUnframed {
121
60
  await new Promise((r) => setTimeout(r, 5));
122
61
  }
123
62
  }
124
- this.wasConnected = true;
63
+ __classPrivateFieldSet(this, _ConnectionWebSocket_wasConnected, true, "f");
125
64
  }
126
65
  finally {
127
- this.isConnecting = false;
128
- this.connectionError = null;
66
+ __classPrivateFieldSet(this, _ConnectionWebSocket_isConnecting, false, "f");
67
+ __classPrivateFieldSet(this, _ConnectionWebSocket_connectionError, null, "f");
129
68
  }
130
69
  }
131
70
  async read(p) {
132
- if (this.webSocket.readyState != WebSocket.OPEN) {
133
- throw new Error("Connection not open");
134
- }
135
- const release = await this.rMutex.acquire();
71
+ __classPrivateFieldGet(this, _ConnectionWebSocket_instances, "m", _ConnectionWebSocket_assertConnected).call(this);
72
+ const release = await __classPrivateFieldGet(this, _ConnectionWebSocket_rMutex, "f").acquire();
136
73
  try {
137
- if (this.buffer.length < p.length) {
138
- await new Promise((resolve, reject) => this.nextResolve = [p.length, { resolve, reject }]);
74
+ __classPrivateFieldGet(this, _ConnectionWebSocket_instances, "m", _ConnectionWebSocket_assertConnected).call(this);
75
+ if (__classPrivateFieldGet(this, _ConnectionWebSocket_buffer, "f").length < p.length) {
76
+ await new Promise((resolve, reject) => __classPrivateFieldSet(this, _ConnectionWebSocket_nextResolve, [p.length, { resolve, reject }], "f"));
139
77
  }
140
- p.set(this.buffer.splice(0, p.length));
78
+ p.set(__classPrivateFieldGet(this, _ConnectionWebSocket_buffer, "f").splice(0, p.length));
141
79
  }
142
80
  finally {
143
81
  release();
144
82
  }
145
83
  }
146
84
  async write(p) {
147
- if (this.webSocket.readyState == WebSocket.CLOSED) {
148
- throw new Error("Connection not open");
149
- }
150
- const release = await this.wMutex.acquire();
85
+ __classPrivateFieldGet(this, _ConnectionWebSocket_instances, "m", _ConnectionWebSocket_assertConnected).call(this);
86
+ const release = await __classPrivateFieldGet(this, _ConnectionWebSocket_wMutex, "f").acquire();
151
87
  try {
152
- this.webSocket.send(p);
88
+ __classPrivateFieldGet(this, _ConnectionWebSocket_instances, "m", _ConnectionWebSocket_assertConnected).call(this);
89
+ __classPrivateFieldGet(this, _ConnectionWebSocket_webSocket, "f").send(p);
153
90
  }
154
91
  finally {
155
92
  release();
156
93
  }
157
94
  }
158
95
  close() {
159
- if (this.webSocket.readyState == WebSocket.CLOSED) {
160
- throw new Error("Connection not open");
96
+ __classPrivateFieldGet(this, _ConnectionWebSocket_instances, "m", _ConnectionWebSocket_assertConnected).call(this);
97
+ __classPrivateFieldGet(this, _ConnectionWebSocket_webSocket, "f").close(1000, "method");
98
+ __classPrivateFieldGet(this, _ConnectionWebSocket_instances, "m", _ConnectionWebSocket_rejectRead).call(this);
99
+ }
100
+ }
101
+ _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() {
102
+ const webSocket = new WebSocket(this.url, "binary");
103
+ const mutex = new Mutex();
104
+ webSocket.addEventListener("close", () => {
105
+ __classPrivateFieldGet(this, _ConnectionWebSocket_instances, "m", _ConnectionWebSocket_rejectRead).call(this);
106
+ this.stateChangeHandler?.(false);
107
+ });
108
+ webSocket.addEventListener("open", () => {
109
+ this.stateChangeHandler?.(true);
110
+ });
111
+ webSocket.addEventListener("message", async (e) => {
112
+ if (typeof e.data === "string") {
113
+ return;
114
+ }
115
+ const release = await mutex.acquire();
116
+ const data = new Uint8Array(await new Blob([e.data].map((v) => v instanceof Blob || v instanceof Uint8Array ? v : v instanceof ArrayBuffer ? v : UNREACHABLE())).arrayBuffer());
117
+ for (const byte of data) {
118
+ __classPrivateFieldGet(this, _ConnectionWebSocket_buffer, "f").push(byte);
119
+ }
120
+ if (__classPrivateFieldGet(this, _ConnectionWebSocket_nextResolve, "f") != null && __classPrivateFieldGet(this, _ConnectionWebSocket_buffer, "f").length >= __classPrivateFieldGet(this, _ConnectionWebSocket_nextResolve, "f")[0]) {
121
+ __classPrivateFieldGet(this, _ConnectionWebSocket_nextResolve, "f")[1].resolve();
122
+ __classPrivateFieldSet(this, _ConnectionWebSocket_nextResolve, null, "f");
161
123
  }
162
- this.webSocket.close(1000, "method");
163
- if (this.nextResolve != null) {
164
- this.nextResolve[1].reject(new Error("Connection not open"));
124
+ release();
125
+ });
126
+ webSocket.addEventListener("error", (err) => {
127
+ if (__classPrivateFieldGet(this, _ConnectionWebSocket_isConnecting, "f")) {
128
+ // @ts-ignore: Node.js
129
+ __classPrivateFieldSet(this, _ConnectionWebSocket_connectionError, err, "f");
165
130
  }
131
+ if (this.connected) {
132
+ d("WebSocket error: %o", err);
133
+ }
134
+ });
135
+ return webSocket;
136
+ }, _ConnectionWebSocket_assertConnected = function _ConnectionWebSocket_assertConnected() {
137
+ if (!this.connected) {
138
+ throw errConnectionNotOpen;
166
139
  }
167
- }
140
+ }, _ConnectionWebSocket_rejectRead = function _ConnectionWebSocket_rejectRead() {
141
+ if (__classPrivateFieldGet(this, _ConnectionWebSocket_nextResolve, "f") != null) {
142
+ __classPrivateFieldGet(this, _ConnectionWebSocket_nextResolve, "f")[1].reject(errConnectionNotOpen);
143
+ __classPrivateFieldSet(this, _ConnectionWebSocket_nextResolve, null, "f");
144
+ }
145
+ };
@@ -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 _authKeyId;
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
  }
@@ -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
- Object.defineProperty(this, "_authKeyId", {
19
- enumerable: true,
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.resetAuthKeyId(authKey);
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.resetAuthKeyId(authKey);
46
+ await __classPrivateFieldGet(this, _Storage_instances, "m", _Storage_resetAuthKeyId).call(this, authKey);
47
47
  }
48
48
  get authKeyId() {
49
- return this._authKeyId;
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, String(id), new Date()]);
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
- const v = await this.get(KPARTS__USERNAME(username));
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): any;
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 { base64Decode, base64Encode } from "../0_deps.js";
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,53 +12,83 @@ export var ValueType;
12
12
  })(ValueType || (ValueType = {}));
13
13
  export function toString(value) {
14
14
  if (typeof value === "boolean") {
15
- return JSON.stringify([ValueType.Boolean, value]);
15
+ return `${ValueType.Boolean}${Number(value)}`;
16
16
  }
17
17
  else if (typeof value === "number") {
18
- return JSON.stringify([ValueType.Number, value]);
18
+ return `${ValueType.Number}${value}`;
19
19
  }
20
20
  else if (typeof value === "string") {
21
- return JSON.stringify([ValueType.String, value]);
21
+ return `${ValueType.String}${value}`;
22
22
  }
23
23
  else if (typeof value == "bigint") {
24
- return JSON.stringify([ValueType.BigInt, String(value)]);
24
+ return `${ValueType.BigInt}${value}`;
25
25
  }
26
26
  else if (value instanceof Date) {
27
- return JSON.stringify([ValueType.Date, value.getTime()]);
27
+ return `${ValueType.Date}${value.getTime()}`;
28
28
  }
29
29
  else if (value instanceof Uint8Array) {
30
- return JSON.stringify([ValueType.Uint8Array, base64Encode(value)]);
30
+ return `${ValueType.Uint8Array}${encodeBase64(value)}`;
31
31
  }
32
32
  else if (Array.isArray(value)) {
33
- return JSON.stringify([ValueType.Array, value.map(toString)]);
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] = JSON.parse(string);
41
- if (type == ValueType.Boolean || type == ValueType.Number || type == ValueType.String) {
42
- return value;
43
- }
44
- else if (type == ValueType.BigInt) {
45
- return BigInt(value);
46
- }
47
- else if (type == ValueType.Date) {
48
- return new Date(value);
49
- }
50
- else if (type == ValueType.Uint8Array) {
51
- return base64Decode(value);
52
- }
53
- else if (type == ValueType.Array) {
54
- return value.map(fromString);
55
- }
56
- else {
57
- UNREACHABLE();
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) {
61
- return key.map((v) => typeof v === "bigint" ? String(v) : v);
91
+ return key.map((v) => typeof v === "bigint" ? String(ValueType.BigInt) + String(v) : typeof v === "string" ? String(ValueType.String) + v : v);
62
92
  }
63
93
  // Source: https://gist.github.com/inexorabletash/5462871
64
94
  // deno-lint-ignore no-explicit-any
@@ -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[]): any;
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[]): any;
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 readonly connection;
5
- private readonly obfuscated;
4
+ #private;
6
5
  constructor(connection: Connection, obfuscated?: boolean);
7
6
  initialize(): Promise<void>;
8
7
  receive(): Promise<Uint8Array>;