flash-store 1.3.5 → 2.0.1

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 (101) hide show
  1. package/README.md +124 -157
  2. package/dist/package.json +64 -0
  3. package/dist/src/config.d.ts +4 -0
  4. package/dist/src/config.d.ts.map +1 -0
  5. package/dist/src/config.js +16 -0
  6. package/dist/src/config.js.map +1 -0
  7. package/dist/src/flash-store-sync.d.ts +73 -0
  8. package/dist/src/flash-store-sync.d.ts.map +1 -0
  9. package/dist/src/flash-store-sync.js +178 -0
  10. package/dist/src/flash-store-sync.js.map +1 -0
  11. package/dist/src/flash-store.d.ts +69 -0
  12. package/dist/src/flash-store.d.ts.map +1 -0
  13. package/dist/src/flash-store.js +246 -0
  14. package/dist/src/flash-store.js.map +1 -0
  15. package/dist/src/mod.d.ts +10 -0
  16. package/dist/src/mod.d.ts.map +1 -0
  17. package/dist/src/mod.js +9 -0
  18. package/dist/src/mod.js.map +1 -0
  19. package/package.json +29 -52
  20. package/dist/cjs/package.json +0 -3
  21. package/dist/cjs/src/config.d.ts +0 -4
  22. package/dist/cjs/src/config.d.ts.map +0 -1
  23. package/dist/cjs/src/config.js +0 -9
  24. package/dist/cjs/src/config.js.map +0 -1
  25. package/dist/cjs/src/flash-store-sync.d.ts +0 -44
  26. package/dist/cjs/src/flash-store-sync.d.ts.map +0 -1
  27. package/dist/cjs/src/flash-store-sync.js +0 -118
  28. package/dist/cjs/src/flash-store-sync.js.map +0 -1
  29. package/dist/cjs/src/flash-store-sync.spec.d.ts +0 -3
  30. package/dist/cjs/src/flash-store-sync.spec.d.ts.map +0 -1
  31. package/dist/cjs/src/flash-store-sync.spec.js +0 -131
  32. package/dist/cjs/src/flash-store-sync.spec.js.map +0 -1
  33. package/dist/cjs/src/flash-store.d.ts +0 -124
  34. package/dist/cjs/src/flash-store.d.ts.map +0 -1
  35. package/dist/cjs/src/flash-store.js +0 -298
  36. package/dist/cjs/src/flash-store.js.map +0 -1
  37. package/dist/cjs/src/flash-store.spec.d.ts +0 -3
  38. package/dist/cjs/src/flash-store.spec.d.ts.map +0 -1
  39. package/dist/cjs/src/flash-store.spec.js +0 -155
  40. package/dist/cjs/src/flash-store.spec.js.map +0 -1
  41. package/dist/cjs/src/mod.d.ts +0 -6
  42. package/dist/cjs/src/mod.d.ts.map +0 -1
  43. package/dist/cjs/src/mod.js +0 -12
  44. package/dist/cjs/src/mod.js.map +0 -1
  45. package/dist/cjs/src/package-json.d.ts +0 -6
  46. package/dist/cjs/src/package-json.d.ts.map +0 -1
  47. package/dist/cjs/src/package-json.js +0 -91
  48. package/dist/cjs/src/package-json.js.map +0 -1
  49. package/dist/cjs/src/package-json.spec.d.ts +0 -3
  50. package/dist/cjs/src/package-json.spec.d.ts.map +0 -1
  51. package/dist/cjs/src/package-json.spec.js +0 -10
  52. package/dist/cjs/src/package-json.spec.js.map +0 -1
  53. package/dist/cjs/tests/integration.spec.d.ts +0 -3
  54. package/dist/cjs/tests/integration.spec.d.ts.map +0 -1
  55. package/dist/cjs/tests/integration.spec.js +0 -8
  56. package/dist/cjs/tests/integration.spec.js.map +0 -1
  57. package/dist/esm/src/config.d.ts +0 -4
  58. package/dist/esm/src/config.d.ts.map +0 -1
  59. package/dist/esm/src/config.js +0 -6
  60. package/dist/esm/src/config.js.map +0 -1
  61. package/dist/esm/src/flash-store-sync.d.ts +0 -44
  62. package/dist/esm/src/flash-store-sync.d.ts.map +0 -1
  63. package/dist/esm/src/flash-store-sync.js +0 -111
  64. package/dist/esm/src/flash-store-sync.js.map +0 -1
  65. package/dist/esm/src/flash-store-sync.spec.d.ts +0 -3
  66. package/dist/esm/src/flash-store-sync.spec.d.ts.map +0 -1
  67. package/dist/esm/src/flash-store-sync.spec.js +0 -126
  68. package/dist/esm/src/flash-store-sync.spec.js.map +0 -1
  69. package/dist/esm/src/flash-store.d.ts +0 -124
  70. package/dist/esm/src/flash-store.d.ts.map +0 -1
  71. package/dist/esm/src/flash-store.js +0 -291
  72. package/dist/esm/src/flash-store.js.map +0 -1
  73. package/dist/esm/src/flash-store.spec.d.ts +0 -3
  74. package/dist/esm/src/flash-store.spec.d.ts.map +0 -1
  75. package/dist/esm/src/flash-store.spec.js +0 -150
  76. package/dist/esm/src/flash-store.spec.js.map +0 -1
  77. package/dist/esm/src/mod.d.ts +0 -6
  78. package/dist/esm/src/mod.d.ts.map +0 -1
  79. package/dist/esm/src/mod.js +0 -6
  80. package/dist/esm/src/mod.js.map +0 -1
  81. package/dist/esm/src/package-json.d.ts +0 -6
  82. package/dist/esm/src/package-json.d.ts.map +0 -1
  83. package/dist/esm/src/package-json.js +0 -88
  84. package/dist/esm/src/package-json.js.map +0 -1
  85. package/dist/esm/src/package-json.spec.d.ts +0 -3
  86. package/dist/esm/src/package-json.spec.d.ts.map +0 -1
  87. package/dist/esm/src/package-json.spec.js +0 -8
  88. package/dist/esm/src/package-json.spec.js.map +0 -1
  89. package/dist/esm/tests/integration.spec.d.ts +0 -3
  90. package/dist/esm/tests/integration.spec.d.ts.map +0 -1
  91. package/dist/esm/tests/integration.spec.js +0 -6
  92. package/dist/esm/tests/integration.spec.js.map +0 -1
  93. package/src/config.ts +0 -12
  94. package/src/flash-store-sync.spec.ts +0 -158
  95. package/src/flash-store-sync.ts +0 -163
  96. package/src/flash-store.spec.ts +0 -197
  97. package/src/flash-store.ts +0 -371
  98. package/src/mod.ts +0 -18
  99. package/src/package-json.spec.ts +0 -10
  100. package/src/package-json.ts +0 -91
  101. package/src/typings.d.ts +0 -0
@@ -1,158 +0,0 @@
1
- #!/usr/bin/env -S node --no-warnings --loader ts-node/esm
2
-
3
- import fs from 'fs'
4
- import path from 'path'
5
- import os from 'os'
6
-
7
- import { test } from 'tstest'
8
-
9
- // import { log } from './config'
10
- // log.level('silly')
11
-
12
- import {
13
- FlashStoreSync,
14
- } from './flash-store-sync.js'
15
-
16
- async function * storeSyncFixture () {
17
- const tmpDir = fs.mkdtempSync(
18
- path.join(
19
- os.tmpdir(),
20
- path.sep,
21
- 'cache-store-',
22
- ),
23
- )
24
- const storeSync = new FlashStoreSync(tmpDir)
25
-
26
- yield storeSync
27
-
28
- await storeSync.destroy()
29
- }
30
-
31
- const KEY = 'test-key'
32
- const VAL = 'test-val'
33
- const VAL_OBJ = { obj_key: 'obj_val' }
34
-
35
- test('version()', async t => {
36
- for await (const storeSync of storeSyncFixture()) {
37
- t.ok(storeSync.version().match(/^\d+\.\d+\.\d+$/), 'should get semver version')
38
- }
39
- })
40
-
41
- test('Store as iterator', async t => {
42
-
43
- await t.test('iterator for empty store', async t => {
44
- for await (const storeSync of storeSyncFixture()) {
45
- let n = 0
46
- for (const _ of storeSync) {
47
- n++
48
- // break
49
- }
50
- t.equal(n, 0, 'should get empty iterator')
51
- }
52
- })
53
-
54
- await t.test('async iterator', async t => {
55
- for await (const storeSync of storeSyncFixture()) {
56
- storeSync.set(KEY, VAL)
57
- let n = 0
58
- for (const [key, val] of storeSync) {
59
- t.equal(key, KEY, 'should get key back')
60
- t.equal(val, VAL, 'should get val back')
61
- n++
62
- }
63
- t.equal(n, 1, 'should iterate once')
64
- }
65
- })
66
-
67
- })
68
-
69
- test('get()', async t => {
70
- await t.test('return null for non existing key', async t => {
71
- for await (const storeSync of storeSyncFixture()) {
72
- const val = storeSync.get(KEY)
73
- t.equal(val, undefined, 'should get undefined for not exist key')
74
- }
75
- })
76
-
77
- await t.test('store string key/val', async t => {
78
- for await (const storeSync of storeSyncFixture()) {
79
- storeSync.set(KEY, VAL)
80
- const val = storeSync.get(KEY)
81
- t.equal(val, VAL, 'should get VAL after set KEY')
82
- }
83
- })
84
-
85
- await t.test('store object value', async t => {
86
- for await (const storeSync of storeSyncFixture()) {
87
- storeSync.set(KEY, VAL_OBJ)
88
- const val = storeSync.get(KEY)
89
- t.same(val, VAL_OBJ, 'should get VAL_OBJ after set KEY')
90
- }
91
- })
92
- })
93
-
94
- test('set()', async t => {
95
- for await (const storeSync of storeSyncFixture()) {
96
- storeSync.set(KEY, VAL)
97
- const val = storeSync.get(KEY)
98
- t.equal(val, VAL, 'should set VAL for KEY')
99
- }
100
- })
101
-
102
- test('size()', async t => {
103
- for await (const storeSync of storeSyncFixture()) {
104
- let size = storeSync.size
105
- t.equal(size, 0, 'should get size 0 after init')
106
- storeSync.set(KEY, VAL)
107
- size = storeSync.size
108
- t.equal(size, 1, 'should get count 1 after put')
109
- }
110
- })
111
-
112
- test('keys()', async t => {
113
- for await (const storeSync of storeSyncFixture()) {
114
- let count = 0
115
- for (const _ of storeSync.keys()) {
116
- count++
117
- }
118
- t.equal(count, 0, 'should get 0 key after init')
119
-
120
- storeSync.set(KEY, VAL)
121
- for (const key of storeSync.keys()) {
122
- t.equal(key, KEY, 'should get back the key')
123
- count++
124
- }
125
- t.equal(count, 1, 'should get 1 key after 1 put')
126
- }
127
- })
128
-
129
- test('values()', async t => {
130
- for await (const storeSync of storeSyncFixture()) {
131
- let count = 0
132
- for (const _ of storeSync.values()) {
133
- count++
134
- }
135
- t.equal(count, 0, 'should get 0 value after init')
136
-
137
- storeSync.set(KEY, VAL)
138
-
139
- for (const value of storeSync.values()) {
140
- t.equal(value, VAL, 'should get back the value')
141
- count++
142
- }
143
- t.equal(count, 1, 'should get 1 value after 1 put')
144
- }
145
- })
146
-
147
- test('close()', async t => {
148
- for await (const storeSync of storeSyncFixture()) {
149
- storeSync.set('KEY1', VAL)
150
- storeSync.set('KEY2', VAL)
151
- storeSync.set('KEY3', VAL)
152
- storeSync.set('KEY4', VAL)
153
- storeSync.set('KEY5', VAL)
154
-
155
- await storeSync.close()
156
- t.pass('should close')
157
- }
158
- })
@@ -1,163 +0,0 @@
1
- import cuid from 'cuid'
2
- import {
3
- StateSwitch,
4
- } from 'state-switch'
5
-
6
- import {
7
- log,
8
- } from './config.js'
9
-
10
- import {
11
- FlashStore,
12
- } from './flash-store.js'
13
-
14
- export class FlashStoreSync<K = any, V = any> implements Map<K, V> {
15
-
16
- static VERSION = FlashStore.VERSION
17
-
18
- private cacheMap : Map<K, V>
19
- private flashStore : FlashStore<K, V>
20
-
21
- private asyncBusyState: StateSwitch
22
- private asyncBusyDict: {
23
- [id: string]: Promise<void>,
24
- }
25
-
26
- constructor (
27
- public workdir?: string,
28
- ) {
29
- log.verbose('CacheStore', 'constructor(%s)', workdir)
30
-
31
- workdir = workdir || '.flash-store-sync'
32
-
33
- this.asyncBusyDict = {}
34
- this.asyncBusyState = new StateSwitch(
35
- 'Busy:' + workdir.split('/').pop(), // get the latest folder name
36
- { log },
37
- )
38
-
39
- this.cacheMap = new Map<K, V>()
40
- this.flashStore = new FlashStore<K, V>(workdir)
41
-
42
- this.asyncBusyAdd(this.loadStoreToCache())
43
-
44
- }
45
-
46
- private async loadStoreToCache (): Promise<void> {
47
- this.cacheMap.clear()
48
- for await (const [key, val] of this.flashStore) {
49
- this.cacheMap.set(key, val)
50
- }
51
- }
52
-
53
- private asyncBusyAdd (task: Promise<any>): void {
54
- this.asyncBusyState.on(true)
55
-
56
- const id = cuid()
57
- this.asyncBusyDict[id] = task.finally(() => {
58
- delete this.asyncBusyDict[id]
59
- if (Object.keys(this.asyncBusyDict).length <= 0) {
60
-
61
- this.asyncBusyState.off(true)
62
-
63
- }
64
- })
65
- }
66
-
67
- public version (): string {
68
- return this.flashStore.version()
69
- }
70
-
71
- /**
72
- *
73
- * Async methods:
74
- *
75
- */
76
- public async close (): Promise<void> {
77
- await this.ready()
78
- await this.flashStore.close()
79
- }
80
-
81
- public async destroy (): Promise<void> {
82
- // this.clear()
83
- // add destroy task at the end of the event loop
84
- this.asyncBusyState.ready('off')
85
- .then(
86
- () => this.flashStore.destroy(),
87
- ).catch(console.error)
88
- }
89
-
90
- public async ready (): Promise<void> {
91
- await this.asyncBusyState.ready('off')
92
- }
93
-
94
- /*******************************************************
95
- *
96
- *
97
- * The following methods is all for ES6 Map Interface
98
- *
99
- *
100
- *******************************************************/
101
-
102
- public get size (): number {
103
- return this.cacheMap.size
104
- }
105
-
106
- get [Symbol.toStringTag] () {
107
- return 'FlashStoreSync' as any as 'Map'
108
- }
109
-
110
- public [Symbol.iterator] () {
111
- return this.cacheMap[Symbol.iterator]()
112
- }
113
-
114
- public entries () {
115
- return this.cacheMap.entries()
116
- }
117
-
118
- public keys () {
119
- return this.cacheMap.keys()
120
- }
121
-
122
- public values () {
123
- return this.cacheMap.values()
124
- }
125
-
126
- public clear (): void {
127
- this.asyncBusyAdd(this.flashStore.clear())
128
- return this.cacheMap.clear()
129
- }
130
-
131
- public delete (key: K): boolean {
132
- this.asyncBusyAdd(this.flashStore.delete(key))
133
- return this.cacheMap.delete(key)
134
- }
135
-
136
- /**
137
- * Do not mutate the key/value in the forEach loop!
138
- */
139
- public forEach (callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any): void {
140
- /**
141
- * 1. no need to call flashStore
142
- * 2. callbackfn should not mutate the data, or the data will be lost sync between cache & store
143
- */
144
- return this.cacheMap.forEach(callbackfn, thisArg)
145
- }
146
-
147
- public get (key: K): V | undefined {
148
- return this.cacheMap.get(key)
149
- }
150
-
151
- public has (key: K): boolean {
152
- return this.cacheMap.has(key)
153
- }
154
-
155
- public set (key: K, value: V): this {
156
- this.asyncBusyAdd(this.flashStore.set(key, value))
157
- this.cacheMap.set(key, value)
158
- return this
159
- }
160
-
161
- }
162
-
163
- export default FlashStoreSync
@@ -1,197 +0,0 @@
1
- #!/usr/bin/env -S node --no-warnings --loader ts-node/esm
2
-
3
- import fs from 'fs'
4
- import os from 'os'
5
- import path from 'path'
6
-
7
- // import rimraf from 'rimraf'
8
-
9
- import { test } from 'tstest'
10
-
11
- // import { log } from './config'
12
- // log.level('silly')
13
-
14
- import {
15
- FlashStore,
16
- } from './flash-store.js'
17
-
18
- const KEY = 'test-key'
19
- const VAL = 'test-val'
20
- const VAL_OBJ = { obj_key: 'obj_val' }
21
-
22
- test('constructor()', async t => {
23
- const tmpDir = fs.mkdtempSync(
24
- path.join(
25
- os.tmpdir(),
26
- 'flash-store-',
27
- ),
28
- )
29
-
30
- await t.resolves(async () => {
31
- const store = new FlashStore(tmpDir)
32
-
33
- // need to do something to create the db directory
34
- await store.delete('init')
35
-
36
- t.ok(fs.existsSync(tmpDir), 'should create the workDir')
37
- await store.destroy()
38
- }, 'should not reject with a non existing workDir')
39
-
40
- t.ok(!fs.existsSync(tmpDir), 'should destroyed the workDir')
41
- })
42
-
43
- test('version()', async t => {
44
- for await (const store of storeFixture()) {
45
- t.ok(store.version().match(/^\d+\.\d+\.\d+$/), 'should get semver version')
46
- }
47
- })
48
-
49
- test('Store as async iterator', async t => {
50
-
51
- await t.test('async iterator for empty store', async t => {
52
- for await (const store of storeFixture()) {
53
- let n = 0
54
- for await (const _ of store) {
55
- n++
56
- // break
57
- }
58
- t.equal(n, 0, 'should get empty iterator')
59
- }
60
- })
61
-
62
- await t.test('async iterator', async t => {
63
- for await (const store of storeFixture()) {
64
- await store.set(KEY, VAL)
65
- let n = 0
66
- for await (const [key, val] of store) {
67
- t.equal(key, KEY, 'should get key back')
68
- t.equal(val, VAL, 'should get val back')
69
- n++
70
- }
71
- t.equal(n, 1, 'should iterate once')
72
- }
73
- })
74
-
75
- })
76
-
77
- test('async get()', async t => {
78
- await t.test('return null for non existing key', async t => {
79
- for await (const store of storeFixture()) {
80
- const val = await store.get(KEY)
81
- t.equal(val, undefined, 'should get undefined for not exist key')
82
- }
83
- })
84
-
85
- await t.test('store string key/val', async t => {
86
- for await (const store of storeFixture()) {
87
- await store.set(KEY, VAL)
88
- const val = await store.get(KEY)
89
- t.equal(val, VAL, 'should get VAL after set KEY')
90
- }
91
- })
92
-
93
- await t.test('store object value', async t => {
94
- for await (const store of storeFixture()) {
95
- await store.set(KEY, VAL_OBJ)
96
- const val = await store.get(KEY)
97
- t.same(val, VAL_OBJ, 'should get VAL_OBJ after set KEY')
98
- }
99
- })
100
- })
101
-
102
- test('async set()', async t => {
103
- for await (const store of storeFixture()) {
104
- await store.set(KEY, VAL)
105
- const val = await store.get(KEY)
106
- t.equal(val, VAL, 'should set VAL for KEY')
107
- }
108
- })
109
-
110
- test('async size()', async t => {
111
- for await (const store of storeFixture()) {
112
- let size = await store.size
113
- t.equal(size, 0, 'should get size 0 after init')
114
- await store.set(KEY, VAL)
115
- size = await store.size
116
- t.equal(size, 1, 'should get count 1 after put')
117
- }
118
- })
119
-
120
- test('async keys()', async t => {
121
- for await (const store of storeFixture()) {
122
- let count = 0
123
- for await (const _ of store.keys()) {
124
- count++
125
- }
126
- t.equal(count, 0, 'should get 0 key after init')
127
-
128
- await store.set(KEY, VAL)
129
- for await (const key of store.keys()) {
130
- t.equal(key, KEY, 'should get back the key')
131
- count++
132
- }
133
- t.equal(count, 1, 'should get 1 key after 1 put')
134
- }
135
- })
136
-
137
- test('async values()', async t => {
138
- for await (const store of storeFixture()) {
139
- let count = 0
140
- for await (const _ of store.values()) {
141
- count++
142
- }
143
- t.equal(count, 0, 'should get 0 value after init')
144
-
145
- await store.set(KEY, VAL)
146
-
147
- for await (const value of store.values()) {
148
- t.equal(value, VAL, 'should get back the value')
149
- count++
150
- }
151
- t.equal(count, 1, 'should get 1 value after 1 put')
152
- }
153
- })
154
-
155
- // test('deferred-leveldown json bug(fixed on version 2.0.2', async t => {
156
- // const encoding = (await import('encoding-down')).default
157
- // const leveldown = (await import('leveldown')).default
158
- // const levelup = (await import('levelup')).default
159
-
160
- // const tmpDir = fs.mkdtempSync(
161
- // path.join(
162
- // os.tmpdir(),
163
- // path.sep,
164
- // 'flash-store-',
165
- // ),
166
- // )
167
-
168
- // const encoded = encoding(leveldown(tmpDir) as any, {
169
- // valueEncoding: 'json',
170
- // })
171
- // const levelDb = levelup(encoded)
172
-
173
- // const EXPECTED_OBJ = {a: 1}
174
- // await levelDb.put('test', EXPECTED_OBJ)
175
- // const value = await levelDb.get('test')
176
-
177
- // t.equal(typeof value, 'object', 'value type should be object')
178
- // t.deepEqual(value, EXPECTED_OBJ, 'should get back the original object')
179
-
180
- // // `rm -fr tmpDir`
181
- // await new Promise(r => rimraf(tmpDir, r))
182
- // })
183
-
184
- async function * storeFixture () {
185
- const tmpDir = fs.mkdtempSync(
186
- path.join(
187
- os.tmpdir(),
188
- path.sep,
189
- 'flash-store-',
190
- ),
191
- )
192
- const store = new FlashStore(tmpDir)
193
-
194
- yield store
195
-
196
- await store.destroy()
197
- }