flat-cache 6.1.9 → 6.1.11
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/README.md +27 -0
- package/dist/index.cjs +38 -5
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +40 -7
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
- [API](#api)
|
|
30
30
|
- [Events (FlatCacheEvents)](#events-flatcacheevents)
|
|
31
31
|
- [Parse and Stringify for File Caching](#parse-and-stringify-for-file-caching)
|
|
32
|
+
- [Loading Data via Streams](#loading-data-via-streams)
|
|
32
33
|
- [How to Contribute](#how-to-contribute)
|
|
33
34
|
- [License and Copyright](#license-and-copyright)
|
|
34
35
|
|
|
@@ -148,6 +149,7 @@ In version 6 we attempted to keep as much as the functionality as possible which
|
|
|
148
149
|
- `changesSinceLastSave` - If there have been changes since the last save
|
|
149
150
|
- `load(cacheId: string, cacheDir?: string)` - Loads the data from disk
|
|
150
151
|
- `loadFile(pathToFile: string)` - Loads the data from disk
|
|
152
|
+
- `loadFileStream(pathToFile: string, onProgress: function, onEnd: function, onError?: function)` - Loads the data from disk as a stream
|
|
151
153
|
- `all()` - Gets all the data in the cache
|
|
152
154
|
- `items()` - Gets all the items in the cache
|
|
153
155
|
- `keys()` - Gets all the keys in the cache
|
|
@@ -200,6 +202,31 @@ This will use `JSON.parse` and `JSON.stringify` to parse and stringify the data.
|
|
|
200
202
|
|
|
201
203
|
**NOTE: This could cause issues if you are trying to load data that was saved with a different parser or stringifier.**
|
|
202
204
|
|
|
205
|
+
# Loading Data via Streams
|
|
206
|
+
|
|
207
|
+
Because of some large files we have added a helper function to load data from a file using streams. This is useful if you have a large file and want to load it in chunks. Here is an example:
|
|
208
|
+
|
|
209
|
+
```javascript
|
|
210
|
+
import { FlatCache } from 'flat-cache';
|
|
211
|
+
const cache = new FlatCache();
|
|
212
|
+
let progressCount = 0;
|
|
213
|
+
const onProgress = (progress: number, total: number) => {
|
|
214
|
+
progressCount++;
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
let errorCount = 0;
|
|
218
|
+
const onError = (error: Error) => {
|
|
219
|
+
errorCount++;
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
let endCount = 0;
|
|
223
|
+
const onEnd = () => {
|
|
224
|
+
console.log(`Loaded ${progressCount} chunks with ${errorCount} errors.`);
|
|
225
|
+
};
|
|
226
|
+
cache.loadFileStream('/path/to/cache/file', onProgress, onEnd, onError);
|
|
227
|
+
```
|
|
228
|
+
This will load the data from the file in chunks and emit the `onProgress`, `onEnd`, and `onError` events. You can use these events to track the progress of the loading and handle any errors that may occur.
|
|
229
|
+
|
|
203
230
|
# How to Contribute
|
|
204
231
|
|
|
205
232
|
You can contribute by forking the repo and submitting a pull request. Please make sure to add tests and update the documentation. To learn more about how to contribute go to our main README [https://github.com/jaredwray/cacheable](https://github.com/jaredwray/cacheable). This will talk about how to `Open a Pull Request`, `Ask a Question`, or `Post an Issue`.
|
package/dist/index.cjs
CHANGED
|
@@ -170,7 +170,6 @@ var FlatCache = class extends import_hookified.Hookified {
|
|
|
170
170
|
* @param cacheId {String} the id of the cache, would also be used as the name of the file cache
|
|
171
171
|
* @param cacheDir {String} directory for the cache entry
|
|
172
172
|
*/
|
|
173
|
-
// eslint-disable-next-line unicorn/prevent-abbreviations
|
|
174
173
|
load(cacheId, cacheDir) {
|
|
175
174
|
try {
|
|
176
175
|
const filePath = import_node_path.default.resolve(`${cacheDir ?? this._cacheDir}/${cacheId ?? this._cacheId}`);
|
|
@@ -195,6 +194,40 @@ var FlatCache = class extends import_hookified.Hookified {
|
|
|
195
194
|
this._changesSinceLastSave = true;
|
|
196
195
|
}
|
|
197
196
|
}
|
|
197
|
+
loadFileStream(pathToFile, onProgress, onEnd, onError) {
|
|
198
|
+
if (import_node_fs.default.existsSync(pathToFile)) {
|
|
199
|
+
const stats = import_node_fs.default.statSync(pathToFile);
|
|
200
|
+
const total = stats.size;
|
|
201
|
+
let loaded = 0;
|
|
202
|
+
let streamData = "";
|
|
203
|
+
const readStream = import_node_fs.default.createReadStream(pathToFile, { encoding: "utf8" });
|
|
204
|
+
readStream.on("data", (chunk) => {
|
|
205
|
+
loaded += chunk.length;
|
|
206
|
+
streamData += chunk;
|
|
207
|
+
onProgress(loaded, total);
|
|
208
|
+
});
|
|
209
|
+
readStream.on("end", () => {
|
|
210
|
+
const items = this._parse(streamData);
|
|
211
|
+
for (const key of Object.keys(items)) {
|
|
212
|
+
this._cache.set(items[key].key, items[key].value, { expire: items[key].expires });
|
|
213
|
+
}
|
|
214
|
+
this._changesSinceLastSave = true;
|
|
215
|
+
onEnd();
|
|
216
|
+
});
|
|
217
|
+
readStream.on("error", (error) => {
|
|
218
|
+
this.emit("error" /* ERROR */, error);
|
|
219
|
+
if (onError) {
|
|
220
|
+
onError(error);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
} else {
|
|
224
|
+
const error = new Error(`Cache file ${pathToFile} does not exist`);
|
|
225
|
+
this.emit("error" /* ERROR */, error);
|
|
226
|
+
if (onError) {
|
|
227
|
+
onError(error);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
198
231
|
/**
|
|
199
232
|
* Returns the entire persisted object
|
|
200
233
|
* @method all
|
|
@@ -202,7 +235,7 @@ var FlatCache = class extends import_hookified.Hookified {
|
|
|
202
235
|
*/
|
|
203
236
|
all() {
|
|
204
237
|
const result = {};
|
|
205
|
-
const items =
|
|
238
|
+
const items = [...this._cache.items];
|
|
206
239
|
for (const item of items) {
|
|
207
240
|
result[item.key] = item.value;
|
|
208
241
|
}
|
|
@@ -214,7 +247,7 @@ var FlatCache = class extends import_hookified.Hookified {
|
|
|
214
247
|
* @returns {Array}
|
|
215
248
|
*/
|
|
216
249
|
get items() {
|
|
217
|
-
return
|
|
250
|
+
return [...this._cache.items];
|
|
218
251
|
}
|
|
219
252
|
/**
|
|
220
253
|
* Returns the path to the file where the cache is persisted
|
|
@@ -238,7 +271,7 @@ var FlatCache = class extends import_hookified.Hookified {
|
|
|
238
271
|
* @returns {Array}
|
|
239
272
|
*/
|
|
240
273
|
keys() {
|
|
241
|
-
return
|
|
274
|
+
return [...this._cache.keys];
|
|
242
275
|
}
|
|
243
276
|
/**
|
|
244
277
|
* (Legacy) set key method. This method will be deprecated in the future
|
|
@@ -319,7 +352,7 @@ var FlatCache = class extends import_hookified.Hookified {
|
|
|
319
352
|
try {
|
|
320
353
|
if (this._changesSinceLastSave || force) {
|
|
321
354
|
const filePath = this.cacheFilePath;
|
|
322
|
-
const items =
|
|
355
|
+
const items = [...this._cache.items];
|
|
323
356
|
const data = this._stringify(items);
|
|
324
357
|
if (!import_node_fs.default.existsSync(this._cacheDir)) {
|
|
325
358
|
import_node_fs.default.mkdirSync(this._cacheDir, { recursive: true });
|
package/dist/index.d.cts
CHANGED
|
@@ -102,6 +102,7 @@ declare class FlatCache extends Hookified {
|
|
|
102
102
|
* @param {String} pathToFile the path to the file containing the info for the cache
|
|
103
103
|
*/
|
|
104
104
|
loadFile(pathToFile: string): void;
|
|
105
|
+
loadFileStream(pathToFile: string, onProgress: (progress: number, total: number) => void, onEnd: () => void, onError?: (error: Error) => void): void;
|
|
105
106
|
/**
|
|
106
107
|
* Returns the entire persisted object
|
|
107
108
|
* @method all
|
package/dist/index.d.ts
CHANGED
|
@@ -102,6 +102,7 @@ declare class FlatCache extends Hookified {
|
|
|
102
102
|
* @param {String} pathToFile the path to the file containing the info for the cache
|
|
103
103
|
*/
|
|
104
104
|
loadFile(pathToFile: string): void;
|
|
105
|
+
loadFileStream(pathToFile: string, onProgress: (progress: number, total: number) => void, onEnd: () => void, onError?: (error: Error) => void): void;
|
|
105
106
|
/**
|
|
106
107
|
* Returns the entire persisted object
|
|
107
108
|
* @method all
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import path from "
|
|
3
|
-
import fs from "
|
|
2
|
+
import path from "path";
|
|
3
|
+
import fs from "fs";
|
|
4
4
|
import { CacheableMemory } from "cacheable";
|
|
5
5
|
import { parse, stringify } from "flatted";
|
|
6
6
|
import { Hookified } from "hookified";
|
|
@@ -130,7 +130,6 @@ var FlatCache = class extends Hookified {
|
|
|
130
130
|
* @param cacheId {String} the id of the cache, would also be used as the name of the file cache
|
|
131
131
|
* @param cacheDir {String} directory for the cache entry
|
|
132
132
|
*/
|
|
133
|
-
// eslint-disable-next-line unicorn/prevent-abbreviations
|
|
134
133
|
load(cacheId, cacheDir) {
|
|
135
134
|
try {
|
|
136
135
|
const filePath = path.resolve(`${cacheDir ?? this._cacheDir}/${cacheId ?? this._cacheId}`);
|
|
@@ -155,6 +154,40 @@ var FlatCache = class extends Hookified {
|
|
|
155
154
|
this._changesSinceLastSave = true;
|
|
156
155
|
}
|
|
157
156
|
}
|
|
157
|
+
loadFileStream(pathToFile, onProgress, onEnd, onError) {
|
|
158
|
+
if (fs.existsSync(pathToFile)) {
|
|
159
|
+
const stats = fs.statSync(pathToFile);
|
|
160
|
+
const total = stats.size;
|
|
161
|
+
let loaded = 0;
|
|
162
|
+
let streamData = "";
|
|
163
|
+
const readStream = fs.createReadStream(pathToFile, { encoding: "utf8" });
|
|
164
|
+
readStream.on("data", (chunk) => {
|
|
165
|
+
loaded += chunk.length;
|
|
166
|
+
streamData += chunk;
|
|
167
|
+
onProgress(loaded, total);
|
|
168
|
+
});
|
|
169
|
+
readStream.on("end", () => {
|
|
170
|
+
const items = this._parse(streamData);
|
|
171
|
+
for (const key of Object.keys(items)) {
|
|
172
|
+
this._cache.set(items[key].key, items[key].value, { expire: items[key].expires });
|
|
173
|
+
}
|
|
174
|
+
this._changesSinceLastSave = true;
|
|
175
|
+
onEnd();
|
|
176
|
+
});
|
|
177
|
+
readStream.on("error", (error) => {
|
|
178
|
+
this.emit("error" /* ERROR */, error);
|
|
179
|
+
if (onError) {
|
|
180
|
+
onError(error);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
} else {
|
|
184
|
+
const error = new Error(`Cache file ${pathToFile} does not exist`);
|
|
185
|
+
this.emit("error" /* ERROR */, error);
|
|
186
|
+
if (onError) {
|
|
187
|
+
onError(error);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
158
191
|
/**
|
|
159
192
|
* Returns the entire persisted object
|
|
160
193
|
* @method all
|
|
@@ -162,7 +195,7 @@ var FlatCache = class extends Hookified {
|
|
|
162
195
|
*/
|
|
163
196
|
all() {
|
|
164
197
|
const result = {};
|
|
165
|
-
const items =
|
|
198
|
+
const items = [...this._cache.items];
|
|
166
199
|
for (const item of items) {
|
|
167
200
|
result[item.key] = item.value;
|
|
168
201
|
}
|
|
@@ -174,7 +207,7 @@ var FlatCache = class extends Hookified {
|
|
|
174
207
|
* @returns {Array}
|
|
175
208
|
*/
|
|
176
209
|
get items() {
|
|
177
|
-
return
|
|
210
|
+
return [...this._cache.items];
|
|
178
211
|
}
|
|
179
212
|
/**
|
|
180
213
|
* Returns the path to the file where the cache is persisted
|
|
@@ -198,7 +231,7 @@ var FlatCache = class extends Hookified {
|
|
|
198
231
|
* @returns {Array}
|
|
199
232
|
*/
|
|
200
233
|
keys() {
|
|
201
|
-
return
|
|
234
|
+
return [...this._cache.keys];
|
|
202
235
|
}
|
|
203
236
|
/**
|
|
204
237
|
* (Legacy) set key method. This method will be deprecated in the future
|
|
@@ -279,7 +312,7 @@ var FlatCache = class extends Hookified {
|
|
|
279
312
|
try {
|
|
280
313
|
if (this._changesSinceLastSave || force) {
|
|
281
314
|
const filePath = this.cacheFilePath;
|
|
282
|
-
const items =
|
|
315
|
+
const items = [...this._cache.items];
|
|
283
316
|
const data = this._stringify(items);
|
|
284
317
|
if (!fs.existsSync(this._cacheDir)) {
|
|
285
318
|
fs.mkdirSync(this._cacheDir, { recursive: true });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "flat-cache",
|
|
3
|
-
"version": "6.1.
|
|
3
|
+
"version": "6.1.11",
|
|
4
4
|
"description": "A simple key/value storage using files to persist the data",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -52,18 +52,18 @@
|
|
|
52
52
|
"file-system-cache"
|
|
53
53
|
],
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@types/node": "^
|
|
56
|
-
"@vitest/coverage-v8": "^3.
|
|
55
|
+
"@types/node": "^24.0.7",
|
|
56
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
57
57
|
"rimraf": "^6.0.1",
|
|
58
|
-
"tsup": "^8.
|
|
58
|
+
"tsup": "^8.5.0",
|
|
59
59
|
"typescript": "^5.8.3",
|
|
60
|
-
"vitest": "^3.
|
|
61
|
-
"xo": "^
|
|
60
|
+
"vitest": "^3.2.4",
|
|
61
|
+
"xo": "^1.1.1"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
64
|
"flatted": "^3.3.3",
|
|
65
|
-
"hookified": "^1.
|
|
66
|
-
"cacheable": "^1.
|
|
65
|
+
"hookified": "^1.10.0",
|
|
66
|
+
"cacheable": "^1.10.1"
|
|
67
67
|
},
|
|
68
68
|
"files": [
|
|
69
69
|
"dist",
|
|
@@ -73,7 +73,7 @@
|
|
|
73
73
|
"build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean",
|
|
74
74
|
"prepublish": "pnpm build",
|
|
75
75
|
"test": "xo --fix && vitest run --coverage",
|
|
76
|
-
"test:ci": "xo && vitest run",
|
|
76
|
+
"test:ci": "xo && vitest run --coverage",
|
|
77
77
|
"clean": "rimraf ./dist ./coverage ./node_modules"
|
|
78
78
|
}
|
|
79
79
|
}
|