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