@leofcoin/chain 1.7.54 → 1.7.65

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.
@@ -1,306 +0,0 @@
1
- const encode = (string) => {
2
- if (typeof string === 'string')
3
- return new TextEncoder().encode(string);
4
- throw Error(`expected typeof String instead got ${string}`);
5
- };
6
- const decode = (uint8Array) => {
7
- if (uint8Array instanceof Uint8Array)
8
- return new TextDecoder().decode(uint8Array);
9
- throw Error(`expected typeof uint8Array instead got ${uint8Array}`);
10
- };
11
-
12
- const pathSepS = '/';
13
- class KeyPath {
14
- uint8Array;
15
- constructor(input) {
16
- if (typeof input === 'string') {
17
- this.uint8Array = encode(input);
18
- }
19
- else if (input instanceof Uint8Array) {
20
- this.uint8Array = input;
21
- }
22
- else if (input instanceof KeyPath) {
23
- this.uint8Array = input.uint8Array;
24
- }
25
- else {
26
- throw new Error('Invalid keyPath, should be a String, Uint8Array or KeyPath');
27
- }
28
- }
29
- isKeyPath() {
30
- return true;
31
- }
32
- toString() {
33
- return decode(this.uint8Array);
34
- }
35
- /**
36
- * Returns the `list` representation of this path.
37
- *
38
- * @example
39
- * ```js
40
- * new Key('/Comedy/MontyPython/Actor:JohnCleese').list()
41
- * // => ['Comedy', 'MontyPythong', 'Actor:JohnCleese']
42
- * ```
43
- */
44
- list() {
45
- return this.toString().split(pathSepS).slice(1);
46
- }
47
- }
48
-
49
- class KeyValue {
50
- uint8Array;
51
- constructor(input) {
52
- if (typeof input === 'string') {
53
- this.uint8Array = encode(input);
54
- }
55
- else if (input instanceof Uint8Array) {
56
- this.uint8Array = input;
57
- }
58
- else if (input instanceof KeyValue) {
59
- this.uint8Array = input.uint8Array;
60
- }
61
- else {
62
- throw new Error('Invalid KeyValue, should be a String, Uint8Array or KeyValue');
63
- }
64
- }
65
- isKeyValue() {
66
- return true;
67
- }
68
- toString() {
69
- return decode(this.uint8Array);
70
- }
71
- }
72
-
73
- if (!globalThis.DEBUG) {
74
- let DEBUG = [];
75
- if (globalThis.localStorage) {
76
- DEBUG = globalThis.localStorage.getItem('DEBUG');
77
- globalThis.DEBUG = DEBUG ? DEBUG.split(',') : [DEBUG];
78
- }
79
- }
80
-
81
- const debug$1 = (target, text) => {
82
- if (!globalThis.DEBUG && globalThis.DEBUG.length === 0) return;
83
- if (
84
- globalThis.DEBUG === 'true' ||
85
- globalThis.DEBUG === true ||
86
- globalThis.DEBUG?.indexOf(target) !== -1 ||
87
- globalThis.DEBUG?.indexOf('*') !== -1 ||
88
- globalThis.DEBUG?.indexOf(target.split('/')[0]) !== -1
89
- )
90
- if (text) console.log('\x1b[34m\x1b[1m%s', `${target}: ${text}`, '\x1b[0m');
91
- else console.log('\x1b[34m\x1b[1m%s', `${target}`, '\x1b[0m');
92
- };
93
-
94
- if (!globalThis.debug) {
95
- globalThis.debug = debug$1;
96
-
97
- globalThis.createDebugger = (target) => (text) => debug$1(target, text);
98
- }
99
-
100
- const debug = globalThis.createDebugger('leofcoin/storage');
101
- const opfsRoot = await navigator.storage.getDirectory();
102
- class BrowerStore {
103
- db;
104
- name;
105
- root;
106
- inWorker;
107
- version;
108
- busy;
109
- queue = [];
110
- async init(name = 'storage', root = '.leofcoin', version = '1', inWorker = false) {
111
- this.version = version;
112
- this.name = name;
113
- this.root = opfsRoot;
114
- this.inWorker = inWorker;
115
- let directoryHandle;
116
- try {
117
- directoryHandle = await opfsRoot.getDirectoryHandle(this.name, {
118
- create: true
119
- });
120
- if (inWorker) {
121
- // it's in a worker so that's why typings invalid?
122
- // @ts-ignore
123
- directoryHandle = await directoryHandle.createSyncAccessHandle();
124
- }
125
- }
126
- catch (error) {
127
- console.error(error);
128
- }
129
- this.db = directoryHandle;
130
- }
131
- toKeyPath(key) {
132
- if (key instanceof KeyPath)
133
- key = new KeyPath(key);
134
- return key.toString();
135
- }
136
- toKeyValue(value) {
137
- if (value instanceof KeyValue)
138
- value = new KeyValue(value);
139
- // @ts-ignore
140
- return value.uint8Array;
141
- }
142
- async has(key) {
143
- debug(`has ${this.toKeyPath(key)}`);
144
- try {
145
- await this.db.getFileHandle(this.toKeyPath(key));
146
- return true;
147
- }
148
- catch (error) {
149
- return false;
150
- }
151
- }
152
- async get(key) {
153
- let promiseResolve;
154
- let promiseReject;
155
- let result = new Promise((resolve, reject) => {
156
- promiseResolve = resolve;
157
- promiseReject = reject;
158
- });
159
- const promise = () => new Promise(async (resolve, reject) => {
160
- debug(`get ${this.toKeyPath(key)}`);
161
- setTimeout(async () => {
162
- try {
163
- let handle = await this.db.getFileHandle(this.toKeyPath(key));
164
- let readBuffer;
165
- if (this.inWorker) {
166
- // it's in a worker so that's why typings invalid?
167
- // @ts-ignore
168
- handle = await handle.createSyncAccessHandle();
169
- // @ts-ignore
170
- const fileSize = handle.getSize();
171
- // Read file content to a buffer.
172
- const buffer = new DataView(new ArrayBuffer(fileSize));
173
- // @ts-ignore
174
- readBuffer = handle.read(buffer, { at: 0 });
175
- // @ts-ignore
176
- handle.close();
177
- }
178
- else {
179
- const file = await handle.getFile();
180
- readBuffer = await file.arrayBuffer();
181
- }
182
- this.runQueue();
183
- promiseResolve(new Uint8Array(readBuffer));
184
- resolve(new Uint8Array(readBuffer));
185
- }
186
- catch (error) {
187
- promiseReject(error);
188
- resolve(false);
189
- }
190
- }, 1);
191
- });
192
- this.queue.push(promise);
193
- this.runQueue();
194
- return result;
195
- }
196
- async put(key, value) {
197
- let promiseResolve;
198
- let promiseReject;
199
- let result = new Promise((resolve, reject) => {
200
- promiseResolve = resolve;
201
- promiseReject = reject;
202
- });
203
- const promise = () => new Promise(async (resolve, reject) => {
204
- debug(`put ${this.toKeyPath(key)}`);
205
- setTimeout(async () => {
206
- try {
207
- let handle = await this.db.getFileHandle(this.toKeyPath(key), { create: true });
208
- let writeable;
209
- if (this.inWorker) {
210
- // it's in a worker so that's why typings invalid?
211
- // @ts-ignore
212
- writeable = await handle.createSyncAccessHandle();
213
- }
214
- else {
215
- writeable = await handle.createWritable();
216
- }
217
- ;
218
- (await writeable).write(this.toKeyValue(value));
219
- (await writeable).close();
220
- this.runQueue();
221
- resolve(true);
222
- promiseResolve(true);
223
- }
224
- catch (error) {
225
- promiseReject(error);
226
- resolve(false);
227
- }
228
- }, 5);
229
- });
230
- this.queue.push(promise);
231
- this.runQueue();
232
- return result;
233
- }
234
- async runQueue() {
235
- if (this.queue.length > 0 && !this.busy) {
236
- this.busy = true;
237
- const next = this.queue.shift();
238
- await next();
239
- this.busy = false;
240
- return this.runQueue();
241
- }
242
- else if (this.queue.length === 0 && this.busy) {
243
- this.busy = false;
244
- }
245
- }
246
- async delete(key) {
247
- debug(`delete ${this.toKeyPath(key)}`);
248
- return new Promise(async (resolve, reject) => {
249
- try {
250
- await this.db.getFileHandle(`${this.toKeyPath(key)}.crswap`);
251
- setTimeout(() => resolve(this.delete(key)), 5);
252
- }
253
- catch (error) {
254
- try {
255
- await this.db.removeEntry(this.toKeyPath(key));
256
- resolve(true);
257
- }
258
- catch (error) {
259
- if (error.name === 'NoModificationAllowedError')
260
- setTimeout(() => resolve(this.delete(key)), 5);
261
- else
262
- reject(error);
263
- }
264
- }
265
- });
266
- }
267
- async clear() {
268
- for await (const key of this.db.keys()) {
269
- debug(`clear ${this.toKeyPath(key)}`);
270
- await this.delete(key);
271
- }
272
- }
273
- async values() {
274
- let values = [];
275
- for await (const cursor of this.db.values()) {
276
- // huh? Outdated typings?
277
- // @ts-ignore
278
- values.push(cursor.getFile());
279
- }
280
- values = await Promise.all(values);
281
- return Promise.all(values.map(async (file) => new Uint8Array(await file.arrayBuffer())));
282
- }
283
- async size() {
284
- let size = 0;
285
- for await (const cursor of this.db.values()) {
286
- // huh? Outdated typings?
287
- // @ts-ignore
288
- console.log((await cursor.getFile()).size);
289
-
290
- size += (await cursor.getFile()).size;
291
- }
292
- return size;
293
- }
294
- async keys() {
295
- const keys = [];
296
- for await (const cursor of this.db.keys()) {
297
- keys.push(cursor);
298
- }
299
- return keys;
300
- }
301
- async iterate() {
302
- return this.db.entries();
303
- }
304
- }
305
-
306
- export { BrowerStore as default };