@k3000/store 0.4.0 → 0.6.0

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.
package/architect.mjs CHANGED
@@ -8,7 +8,7 @@ import {createCipheriv, createDecipheriv, createHash, scryptSync} from "node:cry
8
8
  * @param {Function | String} predicate
9
9
  * @return {Array}
10
10
  */
11
- Array.prototype.eachFlat = function (array, predicate) {
11
+ Array.prototype.eachFlat = function (array, predicate, map) {
12
12
 
13
13
  if (typeof predicate === "string") {
14
14
 
@@ -17,15 +17,30 @@ Array.prototype.eachFlat = function (array, predicate) {
17
17
  predicate = (a, b) => a[key] === b[key]
18
18
  }
19
19
 
20
- return this.map(item => {
20
+ if (typeof map === "function") {
21
21
 
22
- const result = array.filter(entry => predicate(item, entry))
22
+ return this.map(item => {
23
23
 
24
- if (result.length === 0) return item
24
+ const result = array.filter(entry => predicate(item, entry))
25
25
 
26
- return result.map(entry => ({...entry, ...item}))
26
+ if (result.length === 0) return item
27
27
 
28
- }).flat()
28
+ return result.map(entry => map({...entry, ...item}, entry))
29
+
30
+ }).flat()
31
+
32
+ } else {
33
+
34
+ return this.map(item => {
35
+
36
+ const result = array.filter(entry => predicate(item, entry))
37
+
38
+ if (result.length === 0) return item
39
+
40
+ return result.map(entry => ({...entry, ...item}))
41
+
42
+ }).flat()
43
+ }
29
44
  }
30
45
 
31
46
  /**
@@ -34,7 +49,7 @@ Array.prototype.eachFlat = function (array, predicate) {
34
49
  * @param {Function | String} predicate
35
50
  * @return {Array}
36
51
  */
37
- Array.prototype.filterFlat = function (array, predicate) {
52
+ Array.prototype.filterFlat = function (array, predicate, map) {
38
53
 
39
54
  if (typeof predicate === "string") {
40
55
 
@@ -43,10 +58,20 @@ Array.prototype.filterFlat = function (array, predicate) {
43
58
  predicate = (a, b) => a[key] === b[key]
44
59
  }
45
60
 
46
- return this.map(item => array
47
- .filter(entry => predicate(item, entry))
48
- .map(entry => ({...entry, ...item}))
49
- ).flat()
61
+ if (typeof map === "function") {
62
+
63
+ return this.map(item => array
64
+ .filter(entry => predicate(item, entry))
65
+ .map(entry => map(item, entry))
66
+ ).flat()
67
+
68
+ } else {
69
+
70
+ return this.map(item => array
71
+ .filter(entry => predicate({...entry, ...item}, entry))
72
+ .map(entry => ({...entry, ...item}))
73
+ ).flat()
74
+ }
50
75
  }
51
76
 
52
77
  /**
@@ -414,6 +439,11 @@ export class Entities extends Array {
414
439
  }
415
440
  }
416
441
 
442
+ get name() {
443
+
444
+ return this.#name
445
+ }
446
+
417
447
  #recycle(index) {
418
448
 
419
449
  let i
@@ -856,7 +886,14 @@ export class Storage {
856
886
 
857
887
  item.needsSave = false
858
888
 
859
- writeSync(this.#fd_data, item.value, 0, item.value.length, position)
889
+ try {
890
+
891
+ writeSync(this.#fd_data, item.value, 0, item.value.length, position)
892
+
893
+ } catch (e) {
894
+
895
+ console.error(e)
896
+ }
860
897
  }
861
898
  }
862
899
 
@@ -890,7 +927,14 @@ export class Storage {
890
927
 
891
928
  buffer.writeUint32BE(number, 0)
892
929
 
893
- writeSync(this.#fd_index, buffer, 0, 4, position)
930
+ try {
931
+
932
+ writeSync(this.#fd_index, buffer, 0, 4, position)
933
+
934
+ } catch (e) {
935
+
936
+ console.error(e)
937
+ }
894
938
  }
895
939
 
896
940
  #updateId(id = 0) {
@@ -928,18 +972,32 @@ export class Storage {
928
972
 
929
973
  this.#writeNumber(object.length, 4 + position)
930
974
 
931
- writeSync(this.#fd_index, object, 0, object.length, 8 + position)
975
+ try {
976
+
977
+ writeSync(this.#fd_index, object, 0, object.length, 8 + position)
978
+
979
+ } catch (e) {
980
+
981
+ console.error(e)
982
+ }
932
983
  }
933
984
 
934
985
  #getId() {
935
986
 
936
987
  const buffer = Buffer.alloc(4)
937
988
 
938
- readSync(this.#fd_index, buffer, {
939
- offset: 0,
940
- length: 4,
941
- position: 0,
942
- })
989
+ try {
990
+
991
+ readSync(this.#fd_index, buffer, {
992
+ offset: 0,
993
+ length: 4,
994
+ position: 0,
995
+ })
996
+
997
+ } catch (e) {
998
+
999
+ console.error(e)
1000
+ }
943
1001
 
944
1002
  return buffer.readUInt32BE(0)
945
1003
  }
@@ -948,7 +1006,14 @@ export class Storage {
948
1006
 
949
1007
  const buffer = Buffer.alloc(4)
950
1008
 
951
- readSync(this.#fd_index, buffer, 0, 4, 4 + position)
1009
+ try {
1010
+
1011
+ readSync(this.#fd_index, buffer, 0, 4, 4 + position)
1012
+
1013
+ } catch (e) {
1014
+
1015
+ console.error(e)
1016
+ }
952
1017
 
953
1018
  return buffer.readUInt32BE(0)
954
1019
  }
@@ -957,7 +1022,14 @@ export class Storage {
957
1022
 
958
1023
  const buffer = Buffer.alloc(length)
959
1024
 
960
- readSync(this.#fd_index, buffer, 0, length, 8 + position)
1025
+ try {
1026
+
1027
+ readSync(this.#fd_index, buffer, 0, length, 8 + position)
1028
+
1029
+ } catch (e) {
1030
+
1031
+ console.error(e)
1032
+ }
961
1033
 
962
1034
  return JSON.parse(this.#cipher.decrypt(buffer).toString() || '{}')
963
1035
  }
@@ -1024,7 +1096,14 @@ export class Storage {
1024
1096
 
1025
1097
  const value = Buffer.alloc(length)
1026
1098
 
1027
- readSync(this.#fd_data, value, 0, length, position)
1099
+ try {
1100
+
1101
+ readSync(this.#fd_data, value, 0, length, position)
1102
+
1103
+ } catch (e) {
1104
+
1105
+ console.error(e)
1106
+ }
1028
1107
 
1029
1108
  this.#values.set(position, {
1030
1109
  value,
@@ -1060,7 +1139,14 @@ export class Storage {
1060
1139
 
1061
1140
  const val = Buffer.alloc(length)
1062
1141
 
1063
- readSync(this.#fd_data, val, 0, length, position)
1142
+ try {
1143
+
1144
+ readSync(this.#fd_data, val, 0, length, position)
1145
+
1146
+ } catch (e) {
1147
+
1148
+ console.error(e)
1149
+ }
1064
1150
 
1065
1151
  const needsSave = !val.equals(value)
1066
1152
 
package/generator.mjs CHANGED
@@ -236,6 +236,13 @@ export const Uint = size => new class Uint extends Type {
236
236
  return super.step(SCOPE, step > 0 ? step : 1);
237
237
  }
238
238
 
239
+ length(length) {
240
+
241
+ length = Number.parseInt(length) || TypeLen.int
242
+
243
+ return super.length(SCOPE, length > 0 && length < TypeLen.int ? length : TypeLen.int);
244
+ }
245
+
239
246
  }()
240
247
 
241
248
  /**
@@ -287,6 +294,13 @@ export const Int = size => new class Int extends Type {
287
294
  return super.step(SCOPE, step === 0 ? 1 : step);
288
295
  }
289
296
 
297
+ length(length) {
298
+
299
+ length = Number.parseInt(length) || TypeLen.int
300
+
301
+ return super.length(SCOPE, length > 0 && length < TypeLen.int ? length : TypeLen.int);
302
+ }
303
+
290
304
  }()
291
305
 
292
306
  export const BigUint = remark => new class BigUint extends Type {
@@ -479,7 +493,7 @@ export const Text = remark => new class Text extends Type {
479
493
 
480
494
  }()
481
495
 
482
- export const struct = (storage, remark) => {
496
+ export const struct = (storage, remark, name) => {
483
497
 
484
498
  const struct = {}, base = storage.struct.base
485
499
 
@@ -520,6 +534,12 @@ export const struct = (storage, remark) => {
520
534
  }
521
535
  }
522
536
 
537
+ if (name) return {
538
+ name,
539
+ struct: struct[name],
540
+ remark: storage.struct.extra[name].remark
541
+ }
542
+
523
543
  return struct
524
544
  }
525
545
 
@@ -755,24 +775,17 @@ function submit() {
755
775
  writeFileSync(`${this.dir}index.mjs`, `
756
776
  const index = import.meta.url.indexOf('?')
757
777
 
758
- const url = \`./${version}/index.mjs\${index > -1 ? import.meta.url.substring(index) : ''}\`
778
+ export const modules = await import(\`./${version}/index.mjs\${index > -1 ? import.meta.url.substring(index) : ''}\`)
759
779
 
760
- let remark0, close0, getStruct0
780
+ export const close = modules.close
781
+ export const remark = modules.remark
782
+ export const version = ${version}
783
+ export const getStruct = modules.getStruct
761
784
 
762
785
  /**
763
786
  * @type {import('./${version}/type').Storage}
764
787
  */
765
- const storage = await new Promise((resolve, reject) => import(url)
766
- .then(modules => {
767
- remark0 = modules.remark
768
- close0 = modules.close
769
- getStruct0 = modules.getStruct
770
- resolve(modules.default)
771
- }).catch(error => reject(error)))
772
-
773
- export const remark = remark0
774
- export const close = close0
775
- export const getStruct = getStruct0
788
+ export const storage = modules.default
776
789
 
777
790
  export default storage
778
791
  `)
@@ -838,7 +851,7 @@ export const remark = Symbol('remark')
838
851
 
839
852
  export const close = () => storage.close()
840
853
 
841
- export const getStruct = () => struct(storage, remark)
854
+ export const getStruct = name => struct(storage, remark, name)
842
855
 
843
856
  export default new class extends Store {
844
857
 
@@ -1211,7 +1224,7 @@ export const remark = Symbol('remark')
1211
1224
 
1212
1225
  export const close = () => storage.close()
1213
1226
 
1214
- export const getStruct = () => struct(storage, remark)
1227
+ export const getStruct = name => struct(storage, remark, name)
1215
1228
 
1216
1229
  /**
1217
1230
  * @type {import('./type').Storage}
@@ -1234,6 +1247,7 @@ export default store
1234
1247
  * @param {string} newPwd
1235
1248
  * @param {string} password
1236
1249
  * @param {number} version
1250
+ * @param {Storage} store
1237
1251
  * @return {import('./type').Gen}
1238
1252
  * @example
1239
1253
  * const {submit} = upgrade('test')
@@ -1243,6 +1257,7 @@ export default function upgrade(name, {
1243
1257
  newPwd,
1244
1258
  password = '',
1245
1259
  version = 1,
1260
+ store,
1246
1261
  } = {}) {
1247
1262
 
1248
1263
  const dir = name[name.length - 1] === '/' ? name : name + '/'
@@ -1256,7 +1271,11 @@ export default function upgrade(name, {
1256
1271
 
1257
1272
  let prev
1258
1273
 
1259
- if (v === undefined) {
1274
+ if (store) {
1275
+
1276
+ prev = store
1277
+
1278
+ } else if (v === undefined) {
1260
1279
 
1261
1280
  v = 0
1262
1281
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@k3000/store",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "storage",
5
5
  "main": "generator.mjs",
6
6
  "scripts": {
package/test/0/index.mjs CHANGED
@@ -8,7 +8,7 @@ export const remark = Symbol('remark')
8
8
 
9
9
  export const close = () => storage.close()
10
10
 
11
- export const getStruct = () => struct(storage, remark)
11
+ export const getStruct = name => struct(storage, remark, name)
12
12
 
13
13
  export default new class extends Store {
14
14
 
package/test/1/data CHANGED
Binary file
package/test/1/index CHANGED
Binary file
package/test/1/index.mjs CHANGED
@@ -387,7 +387,7 @@ export const remark = Symbol('remark')
387
387
 
388
388
  export const close = () => storage.close()
389
389
 
390
- export const getStruct = () => struct(storage, remark)
390
+ export const getStruct = name => struct(storage, remark, name)
391
391
 
392
392
  /**
393
393
  * @type {import('./type').Storage}
package/test/2/data CHANGED
Binary file
package/test/2/index CHANGED
Binary file
package/test/2/index.mjs CHANGED
@@ -474,7 +474,7 @@ export const remark = Symbol('remark')
474
474
 
475
475
  export const close = () => storage.close()
476
476
 
477
- export const getStruct = () => struct(storage, remark)
477
+ export const getStruct = name => struct(storage, remark, name)
478
478
 
479
479
  /**
480
480
  * @type {import('./type').Storage}
package/test/3/data CHANGED
Binary file
package/test/3/index CHANGED
Binary file
package/test/3/index.mjs CHANGED
@@ -481,7 +481,7 @@ export const remark = Symbol('remark')
481
481
 
482
482
  export const close = () => storage.close()
483
483
 
484
- export const getStruct = () => struct(storage, remark)
484
+ export const getStruct = name => struct(storage, remark, name)
485
485
 
486
486
  /**
487
487
  * @type {import('./type').Storage}
package/test/4/data CHANGED
Binary file
package/test/4/index CHANGED
Binary file
package/test/4/index.mjs CHANGED
@@ -481,7 +481,7 @@ export const remark = Symbol('remark')
481
481
 
482
482
  export const close = () => storage.close()
483
483
 
484
- export const getStruct = () => struct(storage, remark)
484
+ export const getStruct = name => struct(storage, remark, name)
485
485
 
486
486
  /**
487
487
  * @type {import('./type').Storage}
package/test/5/data CHANGED
Binary file
package/test/5/index CHANGED
Binary file
package/test/5/index.mjs CHANGED
@@ -481,7 +481,7 @@ export const remark = Symbol('remark')
481
481
 
482
482
  export const close = () => storage.close()
483
483
 
484
- export const getStruct = () => struct(storage, remark)
484
+ export const getStruct = name => struct(storage, remark, name)
485
485
 
486
486
  /**
487
487
  * @type {import('./type').Storage}
package/test/6/data CHANGED
Binary file
package/test/6/index CHANGED
Binary file
package/test/6/index.mjs CHANGED
@@ -341,7 +341,7 @@ export const remark = Symbol('remark')
341
341
 
342
342
  export const close = () => storage.close()
343
343
 
344
- export const getStruct = () => struct(storage, remark)
344
+ export const getStruct = name => struct(storage, remark, name)
345
345
 
346
346
  /**
347
347
  * @type {import('./type').Storage}
package/test/7/data CHANGED
Binary file
package/test/7/index CHANGED
Binary file
package/test/7/index.mjs CHANGED
@@ -341,7 +341,7 @@ export const remark = Symbol('remark')
341
341
 
342
342
  export const close = () => storage.close()
343
343
 
344
- export const getStruct = () => struct(storage, remark)
344
+ export const getStruct = name => struct(storage, remark, name)
345
345
 
346
346
  /**
347
347
  * @type {import('./type').Storage}
package/test/index.mjs CHANGED
@@ -1,23 +1,16 @@
1
1
 
2
2
  const index = import.meta.url.indexOf('?')
3
3
 
4
- const url = `./7/index.mjs${index > -1 ? import.meta.url.substring(index) : ''}`
4
+ export const modules = await import(`./7/index.mjs${index > -1 ? import.meta.url.substring(index) : ''}`)
5
5
 
6
- let remark0, close0, getStruct0
6
+ export const close = modules.close
7
+ export const remark = modules.remark
8
+ export const version = 7
9
+ export const getStruct = modules.getStruct
7
10
 
8
11
  /**
9
12
  * @type {import('./7/type').Storage}
10
13
  */
11
- const storage = await new Promise((resolve, reject) => import(url)
12
- .then(modules => {
13
- remark0 = modules.remark
14
- close0 = modules.close
15
- getStruct0 = modules.getStruct
16
- resolve(modules.default)
17
- }).catch(error => reject(error)))
18
-
19
- export const remark = remark0
20
- export const close = close0
21
- export const getStruct = getStruct0
14
+ export const storage = modules.default
22
15
 
23
16
  export default storage