flat-cache 6.1.10 → 6.1.12

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 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
 
@@ -122,7 +123,7 @@ In version 6 we attempted to keep as much as the functionality as possible which
122
123
 
123
124
  - `create(options?: FlatCacheOptions)` - Creates a new cache and will load the data from disk if it exists
124
125
  - `createFromFile(filePath, options?: FlatCacheOptions)` - Creates a new cache from a file
125
- - `clearCacheById(cacheId: string, cacheDir?: string)` - Clears the cache by the cacheId
126
+ - `clearCacheById(cacheId: string, cacheDir?: string)` - Clears the cache by the cacheId on the file system not in memory.
126
127
  - `clearAll(cacheDirectory?: string)` - Clears all the caches
127
128
 
128
129
 
@@ -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
@@ -194,6 +194,40 @@ var FlatCache = class extends import_hookified.Hookified {
194
194
  this._changesSinceLastSave = true;
195
195
  }
196
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
+ }
197
231
  /**
198
232
  * Returns the entire persisted object
199
233
  * @method all
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
@@ -238,7 +239,7 @@ declare function create(options?: FlatCacheOptions): FlatCache;
238
239
  */
239
240
  declare function createFromFile(filePath: string, options?: FlatCacheOptions): FlatCache;
240
241
  /**
241
- * Clear the cache identified by the given Id
242
+ * Clear the cache identified by the given Id. This will only remove the cache from disk.
242
243
  * @method clearCacheById
243
244
  * @param cacheId {String} the id of the cache
244
245
  * @param cacheDirectory {String} directory for the cache entry
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
@@ -238,7 +239,7 @@ declare function create(options?: FlatCacheOptions): FlatCache;
238
239
  */
239
240
  declare function createFromFile(filePath: string, options?: FlatCacheOptions): FlatCache;
240
241
  /**
241
- * Clear the cache identified by the given Id
242
+ * Clear the cache identified by the given Id. This will only remove the cache from disk.
242
243
  * @method clearCacheById
243
244
  * @param cacheId {String} the id of the cache
244
245
  * @param cacheDirectory {String} directory for the cache entry
package/dist/index.js CHANGED
@@ -154,6 +154,40 @@ var FlatCache = class extends Hookified {
154
154
  this._changesSinceLastSave = true;
155
155
  }
156
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
+ }
157
191
  /**
158
192
  * Returns the entire persisted object
159
193
  * @method all
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flat-cache",
3
- "version": "6.1.10",
3
+ "version": "6.1.12",
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,19 @@
52
52
  "file-system-cache"
53
53
  ],
54
54
  "devDependencies": {
55
- "@types/node": "^22.15.30",
56
- "@vitest/coverage-v8": "^3.2.2",
55
+ "@faker-js/faker": "^9.9.0",
56
+ "@types/node": "^24.1.0",
57
+ "@vitest/coverage-v8": "^3.2.4",
57
58
  "rimraf": "^6.0.1",
58
59
  "tsup": "^8.5.0",
59
60
  "typescript": "^5.8.3",
60
- "vitest": "^3.2.2",
61
- "xo": "^1.1.0"
61
+ "vitest": "^3.2.4",
62
+ "xo": "^1.2.1"
62
63
  },
63
64
  "dependencies": {
64
65
  "flatted": "^3.3.3",
65
- "hookified": "^1.9.1",
66
- "cacheable": "^1.10.0"
66
+ "hookified": "^1.10.0",
67
+ "cacheable": "^1.10.3"
67
68
  },
68
69
  "files": [
69
70
  "dist",
@@ -73,7 +74,7 @@
73
74
  "build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean",
74
75
  "prepublish": "pnpm build",
75
76
  "test": "xo --fix && vitest run --coverage",
76
- "test:ci": "xo && vitest run",
77
+ "test:ci": "xo && vitest run --coverage",
77
78
  "clean": "rimraf ./dist ./coverage ./node_modules"
78
79
  }
79
80
  }