@leofcoin/chain 1.4.78 → 1.4.80

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,64 +1,73 @@
1
- // import base32 from '@vandeurenglenn/base32'
2
- // import base58 from '@vandeurenglenn/base58'
3
-
4
- // export const encodings = {
5
- // base58,
6
- // base32
7
- // }
8
-
9
- const encode$1 = (string, encoding = 'utf-8') => {
10
- if (typeof string === 'string') {
11
- let encoded;
12
-
13
- // if (encodings[encoding]) encoded = encodings[encoding].encode(encoded)
14
- encoded = new TextEncoder().encode(string);
15
- return encoded
16
- }
17
- throw Error(`expected typeof String instead got ${string}`)
1
+ const encode = (string) => {
2
+ if (typeof string === 'string')
3
+ return new TextEncoder().encode(string);
4
+ throw Error(`expected typeof String instead got ${string}`);
18
5
  };
19
-
20
- const decode$1 = (uint8Array, encoding) => {
21
- if (uint8Array instanceof Uint8Array) {
22
- let decoded;
23
- // if (encodings[encoding]) decoded = encodings[encoding].decode(decoded)
24
- decoded = new TextDecoder().decode(uint8Array);
25
-
26
- return decoded
27
- }
28
- throw Error(`expected typeof uint8Array instead got ${uint8Array}`)
6
+ const decode = (uint8Array) => {
7
+ if (uint8Array instanceof Uint8Array)
8
+ return new TextDecoder().decode(uint8Array);
9
+ throw Error(`expected typeof uint8Array instead got ${uint8Array}`);
29
10
  };
30
11
 
31
- class KeyValue {
32
-
33
- /**
34
- * @param {string | Uint8Array} input
35
- */
36
- constructor(input) {
37
- if (typeof input === 'string') {
38
- this.uint8Array = encode$1(input);
39
- } else if (input instanceof Uint8Array) {
40
- this.uint8Array = input;
41
- } else if (input instanceof KeyValue) {
42
- this.uint8Array = input.uint8Array;
43
- } else {
44
- throw new Error('Invalid KeyValue, should be a String, Uint8Array or KeyValue')
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
+ }
45
28
  }
46
- }
47
-
48
- isKeyValue() {
49
- return true
50
- }
51
-
52
- /**
53
- * Convert to the string representation
54
- *
55
- * @param {import('uint8arrays/to-string').SupportedEncodings} [encoding='utf8'] - The encoding to use.
56
- * @returns {string}
57
- */
58
- toString(encoding = 'utf8') {
59
- return decode$1(this.uint8Array)
60
- }
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
+ }
61
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
+ }
62
71
  }
63
72
 
64
73
  const instanceOfAny = (object, constructors) => constructors.some((c) => object instanceof c);
@@ -376,149 +385,66 @@ replaceTraps((oldTraps) => ({
376
385
  },
377
386
  }));
378
387
 
379
- // import base32 from '@vandeurenglenn/base32'
380
- // import base58 from '@vandeurenglenn/base58'
381
-
382
- // export const encodings = {
383
- // base58,
384
- // base32
385
- // }
386
-
387
- const encode = (string, encoding = 'utf-8') => {
388
- if (typeof string === 'string') {
389
- let encoded;
390
-
391
- // if (encodings[encoding]) encoded = encodings[encoding].encode(encoded)
392
- encoded = new TextEncoder().encode(string);
393
- return encoded
394
- }
395
- throw Error(`expected typeof String instead got ${string}`)
396
- };
397
-
398
- const decode = (uint8Array, encoding) => {
399
- if (uint8Array instanceof Uint8Array) {
400
- let decoded;
401
- // if (encodings[encoding]) decoded = encodings[encoding].decode(decoded)
402
- decoded = new TextDecoder().decode(uint8Array);
403
-
404
- return decoded
405
- }
406
- throw Error(`expected typeof uint8Array instead got ${uint8Array}`)
407
- };
408
-
409
- const pathSepS = '/';
410
- class KeyPath {
411
-
412
- /**
413
- * @param {string | Uint8Array} input
414
- */
415
- constructor(input) {
416
- if (typeof input === 'string') {
417
- this.uint8Array = encode(input);
418
- } else if (input instanceof Uint8Array) {
419
- this.uint8Array = input;
420
- } else if (input instanceof KeyPath) {
421
- this.uint8Array = input.uint8Array;
422
- } else {
423
- throw new Error('Invalid keyPath, should be a String, Uint8Array or KeyPath')
424
- }
425
- }
426
-
427
- isKeyPath() {
428
- return true
429
- }
430
-
431
- /**
432
- * Convert to the string representation
433
- *
434
- * @param {import('uint8arrays/to-string').SupportedEncodings} [encoding='utf8'] - The encoding to use.
435
- * @returns {string}
436
- */
437
- toString(encoding = 'hex') {
438
- return decode(this.uint8Array)
439
- }
440
-
441
- /**
442
- * Returns the `list` representation of this path.
443
- *
444
- * @returns string[]
445
- *
446
- * @example
447
- * ```js
448
- * new Key('/Comedy/MontyPython/Actor:JohnCleese').list()
449
- * // => ['Comedy', 'MontyPythong', 'Actor:JohnCleese']
450
- * ```
451
- */
452
- list() {
453
- return this.toString().split(pathSepS).slice(1)
454
- }
455
-
456
- }
457
-
458
388
  class BrowerStore {
459
- constructor(name = 'storage', root = '.leofcoin', version = 1) {
460
- this.version = version;
461
- this.name = name;
462
- this.root = root;
463
- this.db = openDB(`${root}/${name}`, version, {
464
- upgrade(db) {
465
- db.createObjectStore(name);
466
- }
467
- });
468
- }
469
-
470
- toKeyPath(key) {
471
- if (!key.isKeyPath()) key = new KeyPath(key);
472
- return key.toString('base32')
473
- }
474
-
475
- toKeyValue(value) {
476
- if (!value.isKeyValue()) value = new KeyValue(value);
477
- return value.uint8Array
478
- }
479
-
480
- async get(key) {
481
- return (await this.db).get(this.name, this.toKeyPath(key))
482
- }
483
-
484
- async put(key, value) {
485
- return (await this.db).put(this.name, this.toKeyValue(value), this.toKeyPath(key))
486
- }
487
-
488
- async delete(key) {
489
- return (await this.db).delete(this.name, this.toKeyPath(key))
490
- }
491
-
492
- async clear() {
493
- return (await this.db).clear(this.name)
494
- }
495
-
496
- async values(limit = -1) {
497
- const values = [];
498
- const tx = (await this.db).transaction(this.name);
499
-
500
- for await (const cursor of tx.store) {
501
- values.push(cursor.value);
502
- if (limit && values.length === limit) return values
389
+ db;
390
+ name;
391
+ root;
392
+ version;
393
+ constructor(name = 'storage', root = '.leofcoin', version = '1') {
394
+ this.version = version;
395
+ this.name = name;
396
+ this.root = root;
397
+ this.db = openDB(`${root}/${name}`, Number(version), {
398
+ upgrade(db) {
399
+ db.createObjectStore(name);
400
+ }
401
+ });
503
402
  }
504
- return values
505
- }
506
-
507
- async keys(limit = -1) {
508
- const keys = [];
509
- const tx = (await this.db).transaction(this.name);
510
-
511
- for await (const cursor of tx.store) {
512
- keys.push(cursor.key);
513
- if (limit && keys.length === limit) return keys
403
+ toKeyPath(key) {
404
+ if (!key.isKeyPath())
405
+ key = new KeyPath(key);
406
+ return key.toString('base32');
407
+ }
408
+ toKeyValue(value) {
409
+ if (!value.isKeyValue())
410
+ value = new KeyValue(value);
411
+ return value.uint8Array;
412
+ }
413
+ async get(key) {
414
+ return (await this.db).get(this.name, this.toKeyPath(key));
415
+ }
416
+ async put(key, value) {
417
+ return (await this.db).put(this.name, this.toKeyValue(value), this.toKeyPath(key));
418
+ }
419
+ async delete(key) {
420
+ return (await this.db).delete(this.name, this.toKeyPath(key));
421
+ }
422
+ async clear() {
423
+ return (await this.db).clear(this.name);
424
+ }
425
+ async values(limit = -1) {
426
+ const values = [];
427
+ const tx = (await this.db).transaction(this.name);
428
+ for await (const cursor of tx.store) {
429
+ values.push(cursor.value);
430
+ if (limit && values.length === limit)
431
+ return values;
432
+ }
433
+ return values;
434
+ }
435
+ async keys(limit = -1) {
436
+ const keys = [];
437
+ const tx = (await this.db).transaction(this.name);
438
+ for await (const cursor of tx.store) {
439
+ keys.push(cursor.key);
440
+ if (limit && keys.length === limit)
441
+ return keys;
442
+ }
443
+ return keys;
444
+ }
445
+ async iterate() {
446
+ return (await this.db).transaction(this.name).store;
514
447
  }
515
- return keys
516
- }
517
-
518
- async iterate() {
519
- return (await this.db).transaction(this.name).store
520
- }
521
-
522
448
  }
523
449
 
524
450
  export { BrowerStore as default };