@mtkruto/node 0.0.956 → 0.0.958

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 (33) hide show
  1. package/esm/client/3_client.d.ts +12 -1
  2. package/esm/client/3_client.js +39 -17
  3. package/esm/constants.d.ts +1 -1
  4. package/esm/constants.js +1 -1
  5. package/esm/storage/0_storage.d.ts +10 -17
  6. package/esm/storage/0_storage.js +40 -133
  7. package/esm/storage/0_utilities.d.ts +13 -0
  8. package/esm/storage/0_utilities.js +62 -0
  9. package/esm/storage/1_storage_indexed_db.d.ts +3 -3
  10. package/esm/storage/1_storage_indexed_db.js +4 -3
  11. package/esm/storage/1_storage_local_storage.d.ts +3 -3
  12. package/esm/storage/1_storage_local_storage.js +13 -6
  13. package/esm/storage/1_storage_memory.d.ts +4 -4
  14. package/esm/storage/1_storage_memory.js +4 -2
  15. package/esm/storage/1_storage_session_storage.d.ts +3 -3
  16. package/esm/storage/1_storage_session_storage.js +13 -6
  17. package/package.json +1 -1
  18. package/script/client/3_client.d.ts +12 -1
  19. package/script/client/3_client.js +39 -17
  20. package/script/constants.d.ts +1 -1
  21. package/script/constants.js +1 -1
  22. package/script/storage/0_storage.d.ts +10 -17
  23. package/script/storage/0_storage.js +40 -133
  24. package/script/storage/0_utilities.d.ts +13 -0
  25. package/script/storage/0_utilities.js +68 -0
  26. package/script/storage/1_storage_indexed_db.d.ts +3 -3
  27. package/script/storage/1_storage_indexed_db.js +4 -3
  28. package/script/storage/1_storage_local_storage.d.ts +3 -3
  29. package/script/storage/1_storage_local_storage.js +13 -6
  30. package/script/storage/1_storage_memory.d.ts +4 -4
  31. package/script/storage/1_storage_memory.js +4 -2
  32. package/script/storage/1_storage_session_storage.d.ts +3 -3
  33. package/script/storage/1_storage_session_storage.js +13 -6
@@ -59,6 +59,14 @@ export interface ClientParams {
59
59
  */
60
60
  systemVersion?: string;
61
61
  }
62
+ export interface ForwardMessagesParams {
63
+ messageThreadId?: number;
64
+ disableNotification?: boolean;
65
+ protectContent?: boolean;
66
+ sendAs?: number | string;
67
+ dropSenderName?: boolean;
68
+ dropCaption?: boolean;
69
+ }
62
70
  export declare class Client extends ClientAbstract {
63
71
  readonly storage: Storage;
64
72
  readonly apiId: number;
@@ -152,6 +160,7 @@ export declare class Client extends ClientAbstract {
152
160
  [getEntity](peer: types.PeerChat): Promise<types.Chat | null>;
153
161
  [getEntity](peer: types.PeerChannel): Promise<types.Channel | null>;
154
162
  processResult(result: ReadObject): Promise<void>;
163
+ private updatesToMessages;
155
164
  sendMessage(chatId: number | string, text: string, params?: {
156
165
  parseMode?: ParseMode;
157
166
  entities?: MessageEntity[];
@@ -162,10 +171,12 @@ export declare class Client extends ClientAbstract {
162
171
  messageThreadId?: number;
163
172
  sendAs?: number | string;
164
173
  replyMarkup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
165
- }): Promise<Omit<Message, "replyToMessage">>;
174
+ }): Promise<Message>;
166
175
  getMessages(chatId: number | string, messageIds: number[]): Promise<Omit<Message, "replyToMessage">[]>;
167
176
  getMessage(chatId: number | string, messageId: number): Promise<Omit<Message, "replyToMessage"> | null>;
168
177
  private downloadInner;
169
178
  download(fileId_: string): Promise<AsyncGenerator<Uint8Array, void, unknown>>;
170
179
  [getStickerSetName](inputStickerSet: types.InputStickerSetID, hash?: number): Promise<string>;
180
+ forwardMessages(from: number | string, to: number | string, messageIds: number[], params?: ForwardMessagesParams): Promise<Message[]>;
181
+ forwardMessage(from: number | string, to: number | string, messageId: number, params?: ForwardMessagesParams): Promise<Message>;
171
182
  }
@@ -1041,6 +1041,26 @@ export class Client extends ClientAbstract {
1041
1041
  await this.processUsers(result.users);
1042
1042
  }
1043
1043
  }
1044
+ async updatesToMessages(chatId, updates) {
1045
+ const messages = new Array();
1046
+ if (updates instanceof types.Updates) {
1047
+ for (const update of updates.updates) {
1048
+ if (update instanceof types.UpdateNewMessage) {
1049
+ messages.push(await constructMessage(update.message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this)));
1050
+ }
1051
+ else if (update instanceof types.UpdateNewChannelMessage) {
1052
+ messages.push(await constructMessage(update.message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this)));
1053
+ }
1054
+ }
1055
+ }
1056
+ else if (updates instanceof types.UpdateShortSentMessage || updates instanceof types.UpdateShortSentMessage) {
1057
+ const message = await this.getMessage(chatId, updates.id);
1058
+ if (message != null) {
1059
+ messages.push(message);
1060
+ }
1061
+ }
1062
+ return messages;
1063
+ }
1044
1064
  async sendMessage(chatId, text, params) {
1045
1065
  const entities_ = params?.entities ?? [];
1046
1066
  const parseMode = params?.parseMode ?? this.parseMode;
@@ -1099,23 +1119,7 @@ export class Client extends ClientAbstract {
1099
1119
  entities,
1100
1120
  replyMarkup,
1101
1121
  }));
1102
- if (result instanceof types.Updates) {
1103
- for (const update of result.updates) {
1104
- if (update instanceof types.UpdateNewMessage) {
1105
- return constructMessage(update.message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this));
1106
- }
1107
- else if (update instanceof types.UpdateNewChannelMessage) {
1108
- return constructMessage(update.message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this));
1109
- }
1110
- }
1111
- }
1112
- else if (result instanceof types.UpdateShortSentMessage || result instanceof types.UpdateShortSentMessage) {
1113
- const message = await this.getMessage(chatId, result.id);
1114
- if (message != null) {
1115
- return message;
1116
- }
1117
- }
1118
- UNREACHABLE();
1122
+ return await this.updatesToMessages(chatId, result).then((v) => v[0]);
1119
1123
  }
1120
1124
  async getMessages(chatId, messageIds) {
1121
1125
  const peer = await this.getInputPeer(chatId);
@@ -1217,4 +1221,22 @@ export class Client extends ClientAbstract {
1217
1221
  return name;
1218
1222
  }
1219
1223
  }
1224
+ async forwardMessages(from, to, messageIds, params) {
1225
+ const result = await this.invoke(new functions.MessagesForwardMessages({
1226
+ fromPeer: await this.getInputPeer(from),
1227
+ toPeer: await this.getInputPeer(to),
1228
+ id: messageIds,
1229
+ randomId: messageIds.map(() => getRandomId()),
1230
+ silent: params?.disableNotification || undefined,
1231
+ topMsgId: params?.messageThreadId,
1232
+ noforwards: params?.disableNotification || undefined,
1233
+ sendAs: params?.sendAs ? await this.getInputPeer(params.sendAs) : undefined,
1234
+ dropAuthor: params?.dropSenderName || undefined,
1235
+ dropMediaCaptions: params?.dropCaption || undefined,
1236
+ }));
1237
+ return await this.updatesToMessages(to, result);
1238
+ }
1239
+ async forwardMessage(from, to, messageId, params) {
1240
+ return await this.forwardMessages(from, to, [messageId], params).then((v) => v[0]);
1241
+ }
1220
1242
  }
@@ -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.956";
7
+ export declare const DEFAULT_APP_VERSION = "MTKruto 0.0.958";
8
8
  export declare const DEFAULT_DEVICE_MODEL: string;
9
9
  export declare const DEFAULT_LANG_CODE: string;
10
10
  export declare const DEFAULT_LANG_PACK = "";
package/esm/constants.js CHANGED
@@ -62,7 +62,7 @@ export const publicKeys = new Map([
62
62
  export const VECTOR_CONSTRUCTOR = 0x1CB5C415;
63
63
  export const DEFAULT_INITIAL_DC = "2-test";
64
64
  export const LAYER = 158;
65
- export const DEFAULT_APP_VERSION = "MTKruto 0.0.956";
65
+ export const DEFAULT_APP_VERSION = "MTKruto 0.0.958";
66
66
  // @ts-ignore: lib
67
67
  export const DEFAULT_DEVICE_MODEL = typeof dntShim.Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : dntShim.Deno.build.os + "-" + dntShim.Deno.build.arch;
68
68
  export const DEFAULT_LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];
@@ -1,33 +1,28 @@
1
1
  import { MaybePromise } from "../utilities/0_types.js";
2
2
  import { DC } from "../transport/2_transport_provider.js";
3
3
  import * as types from "../tl/2_types.js";
4
+ export type StorageKeyPart = string | number | bigint | Uint8Array;
4
5
  export declare abstract class Storage {
5
6
  private _authKeyId;
6
7
  abstract init(): MaybePromise<void>;
7
- abstract set(key: string, value: string | null): MaybePromise<void>;
8
- abstract get(key: string): MaybePromise<string | null>;
8
+ abstract set(key: readonly StorageKeyPart[], value: unknown): MaybePromise<void>;
9
+ abstract get<T>(key: readonly StorageKeyPart[]): MaybePromise<T | null>;
9
10
  setDc(dc: DC | null): MaybePromise<void>;
10
- getDc(): Promise<DC | null>;
11
+ getDc(): MaybePromise<DC | null>;
11
12
  private resetAuthKeyId;
12
13
  getAuthKey(): Promise<Uint8Array | null>;
13
14
  setAuthKey(authKey: Uint8Array | null): Promise<void>;
14
15
  get authKeyId(): bigint | null;
15
- private readonly channelAccessHash__;
16
16
  setChannelAccessHash(id: bigint, accessHash: bigint): MaybePromise<void>;
17
- getChannelAccessHash(id: bigint): Promise<bigint | null>;
18
- private readonly userAccessHash__;
17
+ getChannelAccessHash(id: bigint): MaybePromise<bigint | null>;
19
18
  setUserAccessHash(id: bigint, accessHash: bigint): MaybePromise<void>;
20
- getUserAccessHash(id: bigint): Promise<bigint | null>;
21
- private readonly username__;
19
+ getUserAccessHash(id: bigint): MaybePromise<bigint | null>;
22
20
  updateUsernames(type: "user" | "channel", id: bigint, usernames: string[]): Promise<void>;
23
- getUsername(username: string): Promise<readonly ["channel" | "user", bigint, Date] | null>;
24
- private readonly state__;
21
+ getUsername(username: string): MaybePromise<["channel" | "user", bigint, Date] | null>;
25
22
  setState(state: types.UpdatesState): Promise<void>;
26
23
  getState(): Promise<types.UpdatesState | null>;
27
- private readonly channelPts__;
28
24
  setChannelPts(channelId: bigint, pts: number): Promise<void>;
29
- getChannelPts(channelId: bigint): Promise<number | null>;
30
- private readonly peer__;
25
+ getChannelPts(channelId: bigint): MaybePromise<number | null>;
31
26
  setEntity(peer: types.Channel): Promise<void>;
32
27
  setEntity(peer: types.Chat): Promise<void>;
33
28
  setEntity(peer: types.User): Promise<void>;
@@ -35,10 +30,8 @@ export declare abstract class Storage {
35
30
  getEntity(type: "chat", id: bigint): Promise<types.Chat | null>;
36
31
  getEntity(type: "user", id: bigint): Promise<types.User | null>;
37
32
  getEntity(type: "channel" | "chat" | "user", id: bigint): Promise<types.Channel | types.Chat | types.User | null>;
38
- private readonly accountType__;
39
33
  setAccountType(type: "user" | "bot"): Promise<void>;
40
- getAccountType(): Promise<"bot" | "user">;
41
- private readonly stickerSetName__;
34
+ getAccountType(): MaybePromise<"bot" | "user" | null>;
42
35
  updateStickerSetName(id: bigint, accessHash: bigint, name: string): Promise<void>;
43
- getStickerSetName(id: bigint, accessHash: bigint): Promise<[string, Date] | null>;
36
+ getStickerSetName(id: bigint, accessHash: bigint): MaybePromise<[string, Date] | null>;
44
37
  }
@@ -1,11 +1,19 @@
1
- import { base64Decode, base64Encode } from "../deps.js";
2
1
  import { UNREACHABLE } from "../utilities/0_control.js";
3
2
  import { sha1 } from "../utilities/0_hash.js";
4
3
  import { bigIntFromBuffer } from "../utilities/0_bigint.js";
5
4
  import { serialize } from "../tl/1_tl_object.js";
6
5
  import * as types from "../tl/2_types.js";
7
6
  import { TLReader } from "../tl/3_tl_reader.js";
8
- import { rleDecode, rleEncode } from "../utilities/0_rle.js";
7
+ const KPARTS__DC = ["dc"];
8
+ const KPARTS__AUTH_KEY = ["authKey"];
9
+ const KPARTS__CHANNEL_ACCESS_HASH = (v) => ["channelAccessHash", v];
10
+ const KPARTS__USER_ACCESS_HASH = (v) => ["userAccessHash", v];
11
+ const KPARTS__USERNAME = (v) => ["username", v];
12
+ const KPARTS__STATE = ["state"];
13
+ const KPARTS__CHANNEL_PTS = (v) => ["channelPts", v];
14
+ const KPARTS__PEER = (type, id) => ["peer", type, id];
15
+ const KPARTS__ACCOUNT_TYPE = ["accountType"];
16
+ const KPARTS__STICKER_SET_NAME = (id, accessHash) => ["stickerSetName", id, accessHash];
9
17
  export class Storage {
10
18
  constructor() {
11
19
  Object.defineProperty(this, "_authKeyId", {
@@ -14,60 +22,12 @@ export class Storage {
14
22
  writable: true,
15
23
  value: null
16
24
  });
17
- Object.defineProperty(this, "channelAccessHash__", {
18
- enumerable: true,
19
- configurable: true,
20
- writable: true,
21
- value: "channelAccessHash__"
22
- });
23
- Object.defineProperty(this, "userAccessHash__", {
24
- enumerable: true,
25
- configurable: true,
26
- writable: true,
27
- value: "userAccessHash__"
28
- });
29
- Object.defineProperty(this, "username__", {
30
- enumerable: true,
31
- configurable: true,
32
- writable: true,
33
- value: "username__"
34
- });
35
- Object.defineProperty(this, "state__", {
36
- enumerable: true,
37
- configurable: true,
38
- writable: true,
39
- value: "state__"
40
- });
41
- Object.defineProperty(this, "channelPts__", {
42
- enumerable: true,
43
- configurable: true,
44
- writable: true,
45
- value: "channelPts__"
46
- });
47
- Object.defineProperty(this, "peer__", {
48
- enumerable: true,
49
- configurable: true,
50
- writable: true,
51
- value: "peer__"
52
- });
53
- Object.defineProperty(this, "accountType__", {
54
- enumerable: true,
55
- configurable: true,
56
- writable: true,
57
- value: "accountType__"
58
- });
59
- Object.defineProperty(this, "stickerSetName__", {
60
- enumerable: true,
61
- configurable: true,
62
- writable: true,
63
- value: "stickerSetName__"
64
- });
65
25
  }
66
26
  setDc(dc) {
67
- return this.set("dc", dc);
27
+ return this.set(KPARTS__DC, dc);
68
28
  }
69
- async getDc() {
70
- return await this.get("dc");
29
+ getDc() {
30
+ return this.get(KPARTS__DC);
71
31
  }
72
32
  async resetAuthKeyId(authKey) {
73
33
  if (authKey != null) {
@@ -78,105 +38,65 @@ export class Storage {
78
38
  }
79
39
  }
80
40
  async getAuthKey() {
81
- const authKey_ = await this.get("authKey");
82
- const authKey = authKey_ == null ? null : new Uint8Array(authKey_.split(/([0-9a-f]{2})/).filter((v) => v).map((v) => parseInt(v, 16)));
41
+ const authKey = await this.get(KPARTS__AUTH_KEY);
83
42
  await this.resetAuthKeyId(authKey);
84
43
  return authKey;
85
44
  }
86
45
  async setAuthKey(authKey) {
87
- await this.set("authKey", authKey == null ? null : Array.from(authKey).map((v) => v.toString(16)).map((v) => v.padStart(2, "0")).join(""));
46
+ await this.set(KPARTS__AUTH_KEY, authKey);
88
47
  await this.resetAuthKeyId(authKey);
89
48
  }
90
49
  get authKeyId() {
91
50
  return this._authKeyId;
92
51
  }
93
52
  setChannelAccessHash(id, accessHash) {
94
- return this.set(`${this.channelAccessHash__}${id}`, String(accessHash));
53
+ return this.set(KPARTS__CHANNEL_ACCESS_HASH(id), accessHash);
95
54
  }
96
- async getChannelAccessHash(id) {
97
- const accessHash = await this.get(`${this.channelAccessHash__}${id}`);
98
- if (accessHash != null) {
99
- return BigInt(accessHash);
100
- }
101
- else {
102
- return null;
103
- }
55
+ getChannelAccessHash(id) {
56
+ return this.get(KPARTS__CHANNEL_ACCESS_HASH(id));
104
57
  }
105
58
  setUserAccessHash(id, accessHash) {
106
- return this.set(`${this.userAccessHash__}${id}`, String(accessHash));
59
+ return this.set(KPARTS__USER_ACCESS_HASH(id), accessHash);
107
60
  }
108
- async getUserAccessHash(id) {
109
- const accessHash = await this.get(`${this.userAccessHash__}${id}`);
110
- if (accessHash != null) {
111
- return BigInt(accessHash);
112
- }
113
- else {
114
- return null;
115
- }
61
+ getUserAccessHash(id) {
62
+ return this.get(KPARTS__USER_ACCESS_HASH(id));
116
63
  }
117
64
  async updateUsernames(type, id, usernames) {
118
65
  for (let username of usernames) {
119
66
  username = username.toLowerCase();
120
- await this.set(`${this.username__}${username}`, JSON.stringify([type, String(id), new Date()]));
67
+ await this.set(KPARTS__USERNAME(username), [type, String(id), new Date()]);
121
68
  }
122
69
  }
123
- async getUsername(username) {
70
+ getUsername(username) {
124
71
  username = username.toLowerCase();
125
- const username_ = await this.get(`${this.username__}${username}`);
126
- if (username_ != null) {
127
- const [type, id, updatedAt] = JSON.parse(username_);
128
- return [type, BigInt(id), new Date(updatedAt)];
129
- }
130
- else {
131
- return null;
132
- }
72
+ return this.get(KPARTS__USERNAME(username));
133
73
  }
134
74
  async setState(state) {
135
- await this.set(this.state__, JSON.stringify({
136
- date: state.date,
137
- pts: state.pts,
138
- qts: state.qts,
139
- seq: state.seq,
140
- unreadCount: state.unreadCount,
141
- }));
75
+ await this.set(KPARTS__STATE, state[serialize]());
142
76
  }
143
77
  async getState() {
144
- const state__ = await this.get(this.state__);
145
- if (state__ != null) {
146
- const state_ = JSON.parse(state__);
147
- return new types.UpdatesState({
148
- date: state_.date,
149
- pts: state_.pts,
150
- qts: state_.qts,
151
- seq: state_.seq,
152
- unreadCount: state_.unreadCount,
153
- });
78
+ const state = await this.get(KPARTS__STATE);
79
+ if (state != null) {
80
+ return new TLReader(state).readObject();
154
81
  }
155
82
  else {
156
83
  return null;
157
84
  }
158
85
  }
159
86
  async setChannelPts(channelId, pts) {
160
- await this.set(`${this.channelPts__}${channelId}`, String(pts));
87
+ await this.set(KPARTS__CHANNEL_PTS(channelId), pts);
161
88
  }
162
- async getChannelPts(channelId) {
163
- const pts = await this.get(`${this.channelPts__}${channelId}`);
164
- if (pts != null) {
165
- return Number(pts);
166
- }
167
- else {
168
- return null;
169
- }
89
+ getChannelPts(channelId) {
90
+ return this.get(KPARTS__CHANNEL_PTS(channelId));
170
91
  }
171
92
  async setEntity(peer) {
172
93
  const type = peer instanceof types.Channel ? "channel" : peer instanceof types.Chat ? "chat" : peer instanceof types.User ? "user" : UNREACHABLE();
173
- await this.set(`${this.peer__}${type}${peer.id}`, base64Encode(rleEncode(peer[serialize]())));
94
+ await this.set(KPARTS__PEER(type, peer.id), peer[serialize]());
174
95
  }
175
96
  async getEntity(type, id) {
176
- const peer_ = await this.get(`${this.peer__}${type}${id}`);
97
+ const peer_ = await this.get(KPARTS__PEER(type, id));
177
98
  if (peer_ != null) {
178
- const reader = new TLReader(rleDecode(base64Decode(peer_)));
179
- return reader.readObject();
99
+ return new TLReader(peer_).readObject();
180
100
  }
181
101
  else {
182
102
  return null;
@@ -192,30 +112,17 @@ export class Storage {
192
112
  throw err;
193
113
  }
194
114
  else {
195
- await this.set(this.accountType__, type);
115
+ await this.set(KPARTS__ACCOUNT_TYPE, type);
196
116
  }
197
117
  }
198
118
  }
199
- async getAccountType() {
200
- const accountType = await this.get(this.accountType__);
201
- if (accountType != null) {
202
- return accountType;
203
- }
204
- else {
205
- UNREACHABLE();
206
- }
119
+ getAccountType() {
120
+ return this.get(KPARTS__ACCOUNT_TYPE);
207
121
  }
208
122
  async updateStickerSetName(id, accessHash, name) {
209
- await this.set(`${this.stickerSetName__}${id}${accessHash}`, JSON.stringify([name, new Date()]));
123
+ await this.set(KPARTS__STICKER_SET_NAME(id, accessHash), [name, new Date()]);
210
124
  }
211
- async getStickerSetName(id, accessHash) {
212
- const stickerSetName_ = await this.get(`${this.stickerSetName__}${id}${accessHash}`);
213
- if (stickerSetName_ != null) {
214
- const [name, updatedAt] = JSON.parse(stickerSetName_);
215
- return [name, new Date(updatedAt)];
216
- }
217
- else {
218
- return null;
219
- }
125
+ getStickerSetName(id, accessHash) {
126
+ return this.get(KPARTS__STICKER_SET_NAME(id, accessHash));
220
127
  }
221
128
  }
@@ -0,0 +1,13 @@
1
+ import { StorageKeyPart } from "./0_storage.js";
2
+ export declare enum ValueType {
3
+ Boolean = 0,
4
+ Number = 1,
5
+ String = 2,
6
+ BigInt = 3,
7
+ Date = 4,
8
+ Uint8Array = 5,
9
+ Array = 6
10
+ }
11
+ export declare function toString(value: unknown): string;
12
+ export declare function fromString<T>(string: string): any;
13
+ export declare function fixKey(key: StorageKeyPart[]): (string | number | Uint8Array)[];
@@ -0,0 +1,62 @@
1
+ import { base64Decode, base64Encode } from "../deps.js";
2
+ import { UNREACHABLE } from "../utilities/0_control.js";
3
+ export var ValueType;
4
+ (function (ValueType) {
5
+ ValueType[ValueType["Boolean"] = 0] = "Boolean";
6
+ ValueType[ValueType["Number"] = 1] = "Number";
7
+ ValueType[ValueType["String"] = 2] = "String";
8
+ ValueType[ValueType["BigInt"] = 3] = "BigInt";
9
+ ValueType[ValueType["Date"] = 4] = "Date";
10
+ ValueType[ValueType["Uint8Array"] = 5] = "Uint8Array";
11
+ ValueType[ValueType["Array"] = 6] = "Array";
12
+ })(ValueType || (ValueType = {}));
13
+ export function toString(value) {
14
+ if (typeof value === "boolean") {
15
+ return JSON.stringify([ValueType.Boolean, value]);
16
+ }
17
+ else if (typeof value === "number") {
18
+ return JSON.stringify([ValueType.Number, value]);
19
+ }
20
+ else if (typeof value === "string") {
21
+ return JSON.stringify([ValueType.String, value]);
22
+ }
23
+ else if (typeof value == "bigint") {
24
+ return JSON.stringify([ValueType.BigInt, String(value)]);
25
+ }
26
+ else if (value instanceof Date) {
27
+ return JSON.stringify([ValueType.Date, value.getTime()]);
28
+ }
29
+ else if (value instanceof Uint8Array) {
30
+ return JSON.stringify([ValueType.Uint8Array, base64Encode(value)]);
31
+ }
32
+ else if (Array.isArray(value)) {
33
+ return JSON.stringify([ValueType.Array, value.map(toString)]);
34
+ }
35
+ else {
36
+ UNREACHABLE();
37
+ }
38
+ }
39
+ 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();
58
+ }
59
+ }
60
+ export function fixKey(key) {
61
+ return key.map((v) => typeof v === "bigint" ? String(v) : v);
62
+ }
@@ -1,9 +1,9 @@
1
- import { Storage } from "./0_storage.js";
1
+ import { Storage, StorageKeyPart } from "./0_storage.js";
2
2
  export declare class StorageIndexedDB extends Storage {
3
3
  readonly name: string;
4
4
  database: IDBDatabase | null;
5
5
  constructor(name: string);
6
6
  init(): Promise<void>;
7
- set(k: string, v: string | null): Promise<void>;
8
- get(k: string): Promise<string | null>;
7
+ set(k: StorageKeyPart[], v: unknown): Promise<void>;
8
+ get<T>(k: StorageKeyPart[]): Promise<T | null>;
9
9
  }
@@ -1,4 +1,5 @@
1
1
  import { Storage } from "./0_storage.js";
2
+ import { fixKey } from "./0_utilities.js";
2
3
  const VERSION = 1;
3
4
  const KV_OBJECT_STORE = "kv";
4
5
  export class StorageIndexedDB extends Storage {
@@ -44,10 +45,10 @@ export class StorageIndexedDB extends Storage {
44
45
  // deno-lint-ignore no-explicit-any
45
46
  let tx;
46
47
  if (v == null) {
47
- tx = store.delete(k);
48
+ tx = store.delete(fixKey(k));
48
49
  }
49
50
  else {
50
- tx = store.put(v, k);
51
+ tx = store.put(v, fixKey(k));
51
52
  }
52
53
  return new Promise((res, rej) => {
53
54
  tx.onerror = rej;
@@ -63,7 +64,7 @@ export class StorageIndexedDB extends Storage {
63
64
  const tx = this.database
64
65
  .transaction(KV_OBJECT_STORE, "readonly")
65
66
  .objectStore(KV_OBJECT_STORE)
66
- .get(k);
67
+ .get(fixKey(k));
67
68
  return new Promise((res, rej) => {
68
69
  tx.onerror = rej;
69
70
  tx.onsuccess = () => {
@@ -1,9 +1,9 @@
1
1
  import { MaybePromise } from "../utilities/0_types.js";
2
- import { Storage } from "./0_storage.js";
2
+ import { Storage, StorageKeyPart } from "./0_storage.js";
3
3
  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: string): string | null;
8
- set(key: string, value: string | null): MaybePromise<void>;
7
+ get(key_: readonly StorageKeyPart[]): any;
8
+ set(key_: readonly StorageKeyPart[], value: unknown): MaybePromise<void>;
9
9
  }
@@ -1,4 +1,5 @@
1
1
  import { Storage } from "./0_storage.js";
2
+ import { fromString, toString } from "./0_utilities.js";
2
3
  export class StorageLocalStorage extends Storage {
3
4
  constructor(prefix) {
4
5
  if (typeof localStorage === "undefined") {
@@ -20,14 +21,20 @@ export class StorageLocalStorage extends Storage {
20
21
  }
21
22
  init() {
22
23
  }
23
- get(key) {
24
- key = this.prefix + key;
25
- return localStorage.getItem(key);
24
+ get(key_) {
25
+ const key = this.prefix + toString(key_);
26
+ const value = localStorage.getItem(key);
27
+ if (value != null) {
28
+ return fromString(value);
29
+ }
30
+ else {
31
+ return null;
32
+ }
26
33
  }
27
- set(key, value) {
28
- key = this.prefix + key;
34
+ set(key_, value) {
35
+ const key = this.prefix + toString(key_);
29
36
  if (value != null) {
30
- localStorage.setItem(key, value);
37
+ localStorage.setItem(key, toString(value));
31
38
  }
32
39
  else {
33
40
  localStorage.removeItem(key);
@@ -1,8 +1,8 @@
1
1
  import { MaybePromise } from "../utilities/0_types.js";
2
- import { Storage } from "./0_storage.js";
2
+ import { Storage, StorageKeyPart } from "./0_storage.js";
3
3
  export declare class StorageMemory extends Storage implements Storage {
4
- protected map: Map<string, string>;
4
+ protected map: Map<string, unknown>;
5
5
  init(): void;
6
- get(key: string): string | null;
7
- set(key: string, value: string | null): MaybePromise<void>;
6
+ get<T>(key: readonly StorageKeyPart[]): NonNullable<T> | null;
7
+ set(key_: readonly StorageKeyPart[], value: unknown): MaybePromise<void>;
8
8
  }
@@ -1,4 +1,5 @@
1
1
  import { Storage } from "./0_storage.js";
2
+ import { toString } from "./0_utilities.js";
2
3
  export class StorageMemory extends Storage {
3
4
  constructor() {
4
5
  super(...arguments);
@@ -12,9 +13,10 @@ export class StorageMemory extends Storage {
12
13
  init() {
13
14
  }
14
15
  get(key) {
15
- return this.map.get(key) ?? null;
16
+ return this.map.get(toString(key)) ?? null;
16
17
  }
17
- set(key, value) {
18
+ set(key_, value) {
19
+ const key = toString(key_);
18
20
  if (value != null) {
19
21
  this.map.set(key, value);
20
22
  }
@@ -1,9 +1,9 @@
1
1
  import { MaybePromise } from "../utilities/0_types.js";
2
- import { Storage } from "./0_storage.js";
2
+ import { Storage, StorageKeyPart } from "./0_storage.js";
3
3
  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: string): string | null;
8
- set(key: string, value: string | null): MaybePromise<void>;
7
+ get(key_: readonly StorageKeyPart[]): any;
8
+ set(key_: readonly StorageKeyPart[], value: unknown): MaybePromise<void>;
9
9
  }