@k3000/store 0.3.1 → 0.5.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
 
@@ -1296,7 +1382,7 @@ const s2b = (value, length) => {
1296
1382
 
1297
1383
  let len = 0
1298
1384
 
1299
- for (let i = value.length - 1; i >= 0; i--) {
1385
+ for (let i = 0; i < value.length; i++) {
1300
1386
 
1301
1387
  const byteLength = Buffer.byteLength(value[i])
1302
1388
 
@@ -1305,7 +1391,7 @@ const s2b = (value, length) => {
1305
1391
  len += byteLength
1306
1392
  }
1307
1393
 
1308
- buffer = buffer.subarray(buffer.length - len)
1394
+ buffer = buffer.subarray(0, len)
1309
1395
  }
1310
1396
 
1311
1397
  return Buffer.concat([Buffer.alloc(length - buffer.length), buffer])
package/generator.mjs CHANGED
@@ -479,7 +479,7 @@ export const Text = remark => new class Text extends Type {
479
479
 
480
480
  }()
481
481
 
482
- export const struct = (storage, remark) => {
482
+ export const struct = (storage, remark, name) => {
483
483
 
484
484
  const struct = {}, base = storage.struct.base
485
485
 
@@ -520,6 +520,12 @@ export const struct = (storage, remark) => {
520
520
  }
521
521
  }
522
522
 
523
+ if (name) return {
524
+ name,
525
+ struct: struct[name],
526
+ remark: storage.struct.extra[name].remark
527
+ }
528
+
523
529
  return struct
524
530
  }
525
531
 
@@ -755,24 +761,17 @@ function submit() {
755
761
  writeFileSync(`${this.dir}index.mjs`, `
756
762
  const index = import.meta.url.indexOf('?')
757
763
 
758
- const url = \`./${version}/index.mjs\${index > -1 ? import.meta.url.substring(index) : ''}\`
764
+ export const modules = await import(\`./${version}/index.mjs\${index > -1 ? import.meta.url.substring(index) : ''}\`)
759
765
 
760
- let remark0, close0, getStruct0
766
+ export const close = modules.close
767
+ export const remark = modules.remark
768
+ export const version = ${version}
769
+ export const getStruct = modules.getStruct
761
770
 
762
771
  /**
763
772
  * @type {import('./${version}/type').Storage}
764
773
  */
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
774
+ export const storage = modules.default
776
775
 
777
776
  export default storage
778
777
  `)
@@ -838,7 +837,7 @@ export const remark = Symbol('remark')
838
837
 
839
838
  export const close = () => storage.close()
840
839
 
841
- export const getStruct = () => struct(storage, remark)
840
+ export const getStruct = name => struct(storage, remark, name)
842
841
 
843
842
  export default new class extends Store {
844
843
 
@@ -1211,7 +1210,7 @@ export const remark = Symbol('remark')
1211
1210
 
1212
1211
  export const close = () => storage.close()
1213
1212
 
1214
- export const getStruct = () => struct(storage, remark)
1213
+ export const getStruct = name => struct(storage, remark, name)
1215
1214
 
1216
1215
  /**
1217
1216
  * @type {import('./type').Storage}
@@ -1234,6 +1233,7 @@ export default store
1234
1233
  * @param {string} newPwd
1235
1234
  * @param {string} password
1236
1235
  * @param {number} version
1236
+ * @param {Storage} store
1237
1237
  * @return {import('./type').Gen}
1238
1238
  * @example
1239
1239
  * const {submit} = upgrade('test')
@@ -1243,6 +1243,7 @@ export default function upgrade(name, {
1243
1243
  newPwd,
1244
1244
  password = '',
1245
1245
  version = 1,
1246
+ store,
1246
1247
  } = {}) {
1247
1248
 
1248
1249
  const dir = name[name.length - 1] === '/' ? name : name + '/'
@@ -1256,7 +1257,11 @@ export default function upgrade(name, {
1256
1257
 
1257
1258
  let prev
1258
1259
 
1259
- if (v === undefined) {
1260
+ if (store) {
1261
+
1262
+ prev = store
1263
+
1264
+ } else if (v === undefined) {
1260
1265
 
1261
1266
  v = 0
1262
1267
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@k3000/store",
3
- "version": "0.3.1",
3
+ "version": "0.5.0",
4
4
  "description": "storage",
5
5
  "main": "generator.mjs",
6
6
  "scripts": {
package/test/1/index 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/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/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/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/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/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