flash-store 1.3.5 → 2.0.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.
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,371 +0,0 @@
1
- import rimraf from 'rimraf'
2
- // import encoding from 'encoding-down'
3
- // import leveldown from 'leveldown'
4
- // import levelup from 'levelup'
5
- import level from 'level'
6
- import levelErrors from 'level-errors'
7
-
8
- // https://github.com/rollup/rollup/issues/1267#issuecomment-296395734
9
- // const rimraf = (<any>rimrafProxy).default || rimrafProxy
10
- // const encoding = (<any>encodingProxy).default || encodingProxy
11
- // // const leveldown = (<any>leveldownProxy).default || leveldownProxy
12
- // const levelup = (<any>levelupProxy).default || levelupProxy
13
-
14
- import type {
15
- AsyncMapLike,
16
- } from 'async-map-like'
17
-
18
- import {
19
- log,
20
- VERSION,
21
- } from './config.js'
22
-
23
- export interface IteratorOptions {
24
- gt? : any,
25
- gte? : any,
26
- lt? : any,
27
- lte? : any,
28
- reverse? : boolean,
29
- limit? : number,
30
-
31
- prefix? : any,
32
- }
33
-
34
- export class FlashStore<K = any, V = any> implements AsyncMapLike<K, V> {
35
-
36
- static VERSION = VERSION
37
-
38
- private levelDb: level.LevelDB<K, V>
39
-
40
- /**
41
- * FlashStore is a Key-Value database tool and makes using leveldb more easy for Node.js
42
- *
43
- * Creates an instance of FlashStore.
44
- * @param {string} [workdir=path.join(appRoot, 'flash-store.workdir')]
45
- * @example
46
- * import { FlashStore } from 'flash-store'
47
- * const flashStore = new FlashStore('flashstore.workdir')
48
- */
49
- constructor (
50
- public workdir: string,
51
- ) {
52
- log.verbose('FlashStore', 'constructor(%s)', workdir)
53
-
54
- /**
55
- * `valueEncoding` is a `encoding-down` options.
56
- * See: https://github.com/Level/encoding-down#db--requireencoding-downdb-options
57
- */
58
- const levelDb = level(workdir, {
59
- valueEncoding: 'json',
60
- })
61
- levelDb.setMaxListeners(17) // default is Infinity
62
-
63
- this.levelDb = levelDb
64
- }
65
-
66
- public version (): string {
67
- return VERSION
68
- }
69
-
70
- /**
71
- * Set data in database
72
- *
73
- * @param {K} key
74
- * @param {V} value
75
- * @returns {Promise<void>}
76
- * @example
77
- * await flashStore.put(1, 1)
78
- */
79
- public async set (key: K, value: V): Promise<AsyncMapLike<K, V>> {
80
- log.verbose('FlashStore', 'set(%s, %s)', key, typeof value)
81
- log.silly('FlashStore', 'set(%s, %s)', key, JSON.stringify(value))
82
- await this.levelDb.put(key, value)
83
- return this
84
- }
85
-
86
- /**
87
- * Get value from database by key
88
- *
89
- * @param {K} key
90
- * @returns {(Promise<V | null>)}
91
- * @example
92
- * console.log(await flashStore.get(1))
93
- */
94
- public async get (key: K): Promise<V | undefined> {
95
- log.verbose('FlashStore', 'get(%s)', key)
96
- try {
97
- const val = await this.levelDb.get(key)
98
- /**
99
- * We must `await` inside to
100
- * catch the `NotFoundError`
101
- */
102
- return val
103
- } catch (e) {
104
- if (e instanceof levelErrors.NotFoundError) {
105
- return undefined
106
- }
107
- throw e
108
- }
109
- }
110
-
111
- /**
112
- * Del data by key
113
- *
114
- * @param {K} key
115
- * @returns {Promise<void>}
116
- * @example
117
- * await flashStore.del(1)
118
- */
119
- // public async del (key: K): Promise<void> {
120
- // log.verbose('FlashStore', '`del()` DEPRECATED. use `delete()` instead')
121
- // await this.delete(key)
122
- // }
123
-
124
- public async delete (key: K): Promise<boolean> {
125
- log.verbose('FlashStore', 'delete(%s)', key)
126
- await this.levelDb.del(key)
127
- // TODO: `del` returns `true` or `false`
128
- return true
129
- }
130
-
131
- /**
132
- * @typedef IteratorOptions
133
- *
134
- * @property { any } gt - Matches values that are greater than a specified value
135
- * @property { any } gte - Matches values that are greater than or equal to a specified value.
136
- * @property { any } lt - Matches values that are less than a specified value.
137
- * @property { any } lte - Matches values that are less than or equal to a specified value.
138
- * @property { boolean } reverse - Reverse the result set
139
- * @property { number } limit - Limits the number in the result set.
140
- * @property { any } prefix - Make the same prefix key get together.
141
- */
142
-
143
- /**
144
- * Find keys by IteratorOptions
145
- *
146
- * @param {IteratorOptions} [options={}]
147
- * @returns {AsyncIterableIterator<K>}
148
- * @example
149
- * const flashStore = new FlashStore('flashstore.workdir')
150
- * for await(const key of flashStore.keys({gte: 1})) {
151
- * console.log(key)
152
- * }
153
- */
154
- public async * keys (options: IteratorOptions = {}): AsyncIterableIterator<K> {
155
- log.verbose('FlashStore', 'keys()')
156
-
157
- // options = Object.assign(options, {
158
- // keys : true,
159
- // values : false,
160
- // })
161
-
162
- if (options.prefix) {
163
- if (options.gte || options.lte) {
164
- throw new Error('can not specify `prefix` with `gte`/`lte` together.')
165
- }
166
- options.gte = options.prefix
167
- options.lte = options.prefix + '\xff'
168
- }
169
-
170
- for await (const [key] of this.entries(options)) {
171
- yield key
172
- }
173
- }
174
-
175
- /**
176
- * Find all values
177
- *
178
- * @returns {AsyncIterableIterator<V>}
179
- * @example
180
- * const flashStore = new FlashStore('flashstore.workdir')
181
- * for await(const value of flashStore.values()) {
182
- * console.log(value)
183
- * }
184
- */
185
- public async * values (options: IteratorOptions = {}): AsyncIterableIterator<V> {
186
- log.verbose('FlashStore', 'values()')
187
-
188
- // options = Object.assign(options, {
189
- // keys : false,
190
- // values : true,
191
- // })
192
-
193
- for await (const [, value] of this.entries(options)) {
194
- yield value
195
- }
196
-
197
- }
198
-
199
- /**
200
- * Get the size of the database
201
- * @returns {Promise<number>}
202
- * @example
203
- * const size = await flashStore.size
204
- * console.log(`database size: ${size}`)
205
- */
206
- // * @deprecated use property `size` instead
207
- // public async count (): Promise<number> {
208
- // log.warn('FlashStore', '`count()` DEPRECATED. use `size()` instead.')
209
- // const size = await this.size
210
- // return size
211
- // }
212
-
213
- public get size (): Promise<number> {
214
- log.verbose('FlashStore', 'size()')
215
-
216
- /* eslint no-async-promise-executor: 0 */
217
- /**
218
- * Get the total number (size) of the LevelDB keys #96
219
- * - https://github.com/huan/flash-store/issues/96
220
- *
221
- * TODO: is there a better way to count all items from the db?
222
- */
223
- return new Promise<number>(async (resolve, reject) => {
224
- try {
225
- let count = 0
226
- for await (const _ of this) {
227
- count++
228
- }
229
- resolve(count)
230
- } catch (e) {
231
- reject(e)
232
- }
233
- })
234
- }
235
-
236
- /**
237
- * FIXME: use better way to do this
238
- */
239
- public async has (key: K): Promise<boolean> {
240
- const val = await this.get(key)
241
- return !!val
242
- }
243
-
244
- /**
245
- * TODO: use better way to do this with leveldb
246
- */
247
- public async clear (): Promise<void> {
248
- for await (const key of this.keys()) {
249
- await this.delete(key)
250
- }
251
- }
252
-
253
- get [Symbol.toStringTag] () {
254
- return Promise.resolve('FlashStore')
255
- }
256
-
257
- [Symbol.iterator] (): AsyncIterableIterator<[K, V]> {
258
- log.verbose('FlashStore', '[Symbol.iterator]()')
259
- /**
260
- * Huan(202108): what is this???
261
- * does it equals to `entries()`?
262
- */
263
- return this.entries()
264
- }
265
-
266
- /**
267
- * @private
268
- */
269
- public async * entries (options?: IteratorOptions): AsyncIterableIterator<[K, V]> {
270
- log.verbose('FlashStore', '*entries(%s)', JSON.stringify(options))
271
-
272
- const iterator = (this.levelDb as any).db.iterator(options)
273
-
274
- while (true) {
275
- const pair = await new Promise<[K, V] | null>((resolve, reject) => {
276
- iterator.next(function (err: any, key: K, val: V) {
277
- if (err) {
278
- reject(err)
279
- }
280
- if (!key && !val) {
281
- return resolve(null)
282
- }
283
- // if (val) {
284
- // val = JSON.parse(val as any)
285
- // }
286
- return resolve([key, val])
287
- })
288
- })
289
- if (!pair) {
290
- break
291
- }
292
- yield pair
293
- }
294
- }
295
-
296
- public async * [Symbol.asyncIterator] (): AsyncIterableIterator<[K, V]> {
297
- log.verbose('FlashStore', '*[Symbol.asyncIterator]()')
298
- yield * this.entries()
299
- }
300
-
301
- /**
302
- * @private
303
- */
304
- // public async * streamAsyncIterator (): AsyncIterator<[K, V]> {
305
- // log.warn('FlashStore', 'DEPRECATED *[Symbol.asyncIterator]()')
306
-
307
- // const readStream = this.levelDb.createReadStream()
308
-
309
- // const endPromise = new Promise<false>((resolve, reject) => {
310
- // readStream
311
- // .once('end', () => resolve(false))
312
- // .once('error', reject)
313
- // })
314
-
315
- // let pair: [K, V] | false
316
-
317
- // do {
318
- // const dataPromise = new Promise<[K, V]>(resolve => {
319
- // readStream.once('data', (data: any) => resolve([data.key, data.value]))
320
- // })
321
-
322
- // pair = await Promise.race([
323
- // dataPromise,
324
- // endPromise,
325
- // ])
326
-
327
- // if (pair) {
328
- // yield pair
329
- // }
330
-
331
- // } while (pair)
332
-
333
- // }
334
-
335
- async forEach (
336
- callbackfn: (
337
- value: V,
338
- key: K,
339
- // map: TestAsyncMapLike,
340
- // FIXME(huan) 202007: we have to use any at here, because the typing system is very hard to
341
- // rename `Map` to `TestAsyncMapLike` in this method function parameters.
342
- map: any,
343
- ) => void,
344
- thisArg?: any,
345
- ): Promise<void> {
346
- log.verbose('FlashStore', 'forEach()')
347
-
348
- for await (const [key, value] of this) {
349
- callbackfn.call(thisArg, value, key, this)
350
- }
351
- }
352
-
353
- public async close (): Promise<void> {
354
- log.verbose('FlashStore', 'close()')
355
- await this.levelDb.close()
356
- }
357
-
358
- /**
359
- * Destroy the database
360
- *
361
- * @returns {Promise<void>}
362
- */
363
- public async destroy (): Promise<void> {
364
- log.verbose('FlashStore', 'destroy()')
365
- await this.levelDb.close()
366
- await new Promise(resolve => rimraf(this.workdir, resolve))
367
- }
368
-
369
- }
370
-
371
- export default FlashStore
package/src/mod.ts DELETED
@@ -1,18 +0,0 @@
1
- import {
2
- log,
3
- VERSION,
4
- } from './config.js'
5
- import {
6
- FlashStore,
7
- } from './flash-store.js'
8
- import {
9
- FlashStoreSync,
10
- } from './flash-store-sync.js'
11
-
12
- export default FlashStore
13
- export {
14
- log,
15
- FlashStoreSync,
16
- FlashStore,
17
- VERSION,
18
- }
@@ -1,10 +0,0 @@
1
- #!/usr/bin/env -S node --no-warnings --loader ts-node/esm
2
-
3
- import { test } from 'tstest'
4
-
5
- import { packageJson } from './package-json.js'
6
-
7
- test('Make sure the packageJson is fresh in source code', async t => {
8
- const keyNum = Object.keys(packageJson).length
9
- t.equal(keyNum, 0, 'packageJson should be empty in source code, only updated before publish to NPM')
10
- })
@@ -1,91 +0,0 @@
1
- /**
2
- * This file was auto generated from scripts/generate-version.sh
3
- */
4
- import type { PackageJson } from 'type-fest'
5
- export const packageJson: PackageJson = {
6
- "name": "flash-store",
7
- "version": "1.3.5",
8
- "description": "FlashStore is a Key-Value persistent storage with easy to use ES6 Map-like API(both Async and Sync support), powered by LevelDB and TypeScript.",
9
- "type": "module",
10
- "exports": {
11
- ".": {
12
- "import": "./dist/esm/src/mod.js",
13
- "require": "./dist/cjs/src/mod.js"
14
- }
15
- },
16
- "typings": "./dist/esm/src/mod.d.ts",
17
- "engines": {
18
- "node": ">=14"
19
- },
20
- "scripts": {
21
- "clean": "shx rm -fr dist/*",
22
- "dist": "npm-run-all clean build dist:commonjs",
23
- "build": "tsc && tsc -p tsconfig.cjs.json",
24
- "dist:commonjs": "jq -n \"{ type: \\\"commonjs\\\" }\" > dist/cjs/package.json",
25
- "pack": "npm pack",
26
- "doc": "npm run dist && echo '# FlashStore v'$(jq -r .version package.json)' Documentation\n' > docs/index.md && jsdoc2md dist/flash-store.js>> docs/index.md",
27
- "demo": "ts-node example/demo.ts",
28
- "lint": "npm-run-all lint:es lint:ts lint:md",
29
- "lint:md": "markdownlint README.md",
30
- "lint:ts": "tsc --isolatedModules --noEmit",
31
- "lint:es": "eslint --ignore-pattern fixtures/ \"src/**/*.ts\" \"tests/**/*.ts\"",
32
- "test": "npm run lint && npm run test:unit",
33
- "test:pack": "bash -x scripts/npm-pack-testing.sh",
34
- "test:unit": "tap --node-arg=--loader=ts-node/esm --node-arg=--no-warnings \"src/**/*.spec.ts\" \"tests/**/*.spec.ts\""
35
- },
36
- "repository": {
37
- "type": "git",
38
- "url": "git+https://github.com/huan/flash-store.git"
39
- },
40
- "keywords": [
41
- "leveldb",
42
- "nosql",
43
- "database"
44
- ],
45
- "author": "Huan LI <zixia@zixia.net>",
46
- "license": "Apache-2.0",
47
- "bugs": {
48
- "url": "https://github.com/huan/flash-store/issues"
49
- },
50
- "homepage": "https://github.com/huan/flash-store#readme",
51
- "dependencies": {
52
- "async-map-like": "^0.2.5",
53
- "brolog": "^1.12.4",
54
- "cuid": "^2.1.8",
55
- "level": "^7.0.0",
56
- "rimraf": "^3.0.2",
57
- "state-switch": "^0.14.1"
58
- },
59
- "devDependencies": {
60
- "@chatie/eslint-config": "^0.14.1",
61
- "@chatie/git-scripts": "^0.6.2",
62
- "@chatie/semver": "^0.4.7",
63
- "@chatie/tsconfig": "^0.20.2",
64
- "@types/app-root-path": "^1.2.4",
65
- "@types/level": "^6.0.0",
66
- "@types/level-errors": "^3.0.0",
67
- "@types/rimraf": "^3.0.1",
68
- "jsdoc-to-markdown": "^7.0.1",
69
- "npm-run-all": "^4.1.5",
70
- "shx": "^0.3.3",
71
- "source-map-support": "^0.5.19",
72
- "tstest": "^0.5.16",
73
- "typescript": "^4.4.3"
74
- },
75
- "files": [
76
- "src/",
77
- "dist/"
78
- ],
79
- "publishConfig": {
80
- "access": "public",
81
- "tag": "next"
82
- },
83
- "tap": {
84
- "check-coverage": false
85
- },
86
- "git": {
87
- "scripts": {
88
- "pre-push": "npx git-scripts-pre-push"
89
- }
90
- }
91
- } as any
package/src/typings.d.ts DELETED
File without changes