@naturalcycles/nodejs-lib 15.129.0 → 15.130.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/dist/cache/lruMemoCache.d.ts +2 -1
- package/dist/cache/lruMemoCache.js +8 -2
- package/dist/fs/fs2.d.ts +4 -0
- package/dist/fs/fs2.js +13 -0
- package/package.json +1 -1
- package/src/cache/lruMemoCache.ts +9 -3
- package/src/fs/fs2.ts +18 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { AsyncMemoCache, MemoCache } from '@naturalcycles/js-lib/decorators';
|
|
2
|
+
import { MISS } from '@naturalcycles/js-lib/types';
|
|
2
3
|
import { LRUCache } from 'lru-cache';
|
|
3
4
|
export type LRUMemoCacheOptions<KEY, VALUE> = Partial<LRUCache.Options<KEY, VALUE, any>>;
|
|
4
5
|
/**
|
|
@@ -23,7 +24,7 @@ export declare class LRUAsyncMemoCache<KEY = any, VALUE = any> implements AsyncM
|
|
|
23
24
|
constructor(opt: LRUMemoCacheOptions<KEY, VALUE>);
|
|
24
25
|
private lru;
|
|
25
26
|
has(k: any): Promise<boolean>;
|
|
26
|
-
get(k: any): Promise<
|
|
27
|
+
get(k: any): Promise<VALUE | typeof MISS>;
|
|
27
28
|
set(k: any, v: any): Promise<void>;
|
|
28
29
|
clear(): Promise<void>;
|
|
29
30
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MISS } from '@naturalcycles/js-lib/types';
|
|
1
2
|
import { LRUCache } from 'lru-cache';
|
|
2
3
|
/**
|
|
3
4
|
* @example
|
|
@@ -27,6 +28,8 @@ export class LRUMemoCache {
|
|
|
27
28
|
this.lru.clear();
|
|
28
29
|
}
|
|
29
30
|
}
|
|
31
|
+
// lru-cache never stores `undefined` (`set(k, undefined)` deletes `k`), so it's stored as this sentinel
|
|
32
|
+
const UNDEFINED = Symbol('undefined');
|
|
30
33
|
/**
|
|
31
34
|
* Same as LRUMemoCache, but implements AsyncMemoCache interface, for when you need it.
|
|
32
35
|
*/
|
|
@@ -42,10 +45,13 @@ export class LRUAsyncMemoCache {
|
|
|
42
45
|
return this.lru.has(k);
|
|
43
46
|
}
|
|
44
47
|
async get(k) {
|
|
45
|
-
|
|
48
|
+
const v = this.lru.get(k);
|
|
49
|
+
if (v === undefined)
|
|
50
|
+
return MISS;
|
|
51
|
+
return v === UNDEFINED ? undefined : v;
|
|
46
52
|
}
|
|
47
53
|
async set(k, v) {
|
|
48
|
-
this.lru.set(k, v);
|
|
54
|
+
this.lru.set(k, v === undefined ? UNDEFINED : v);
|
|
49
55
|
}
|
|
50
56
|
async clear() {
|
|
51
57
|
this.lru.clear();
|
package/dist/fs/fs2.d.ts
CHANGED
|
@@ -24,11 +24,15 @@ declare class FS2 {
|
|
|
24
24
|
readJsonAsync<T = unknown>(filePath: string): Promise<T>;
|
|
25
25
|
writeFile(filePath: string, data: string | Buffer, opt?: WriteFileOptions): void;
|
|
26
26
|
writeFileAsync(filePath: string, data: string | Buffer, opt?: WriteFileOptions): Promise<void>;
|
|
27
|
+
writePrettyJson(filePath: string, data: any): void;
|
|
27
28
|
writeJson(filePath: string, data: any, opt?: JsonOptions): void;
|
|
29
|
+
writePrettyJsonAsync(filePath: string, data: any): Promise<void>;
|
|
28
30
|
writeJsonAsync(filePath: string, data: any, opt?: JsonOptions): Promise<void>;
|
|
29
31
|
appendFile(filePath: string, data: string | Buffer): void;
|
|
30
32
|
appendFileAsync(filePath: string, data: string | Buffer): Promise<void>;
|
|
33
|
+
outputPrettyJson(filePath: string, data: any): void;
|
|
31
34
|
outputJson(filePath: string, data: any, opt?: JsonOptions): void;
|
|
35
|
+
outputPrettyAsync(filePath: string, data: any): Promise<void>;
|
|
32
36
|
outputJsonAsync(filePath: string, data: any, opt?: JsonOptions): Promise<void>;
|
|
33
37
|
outputFile(filePath: string, data: string | Buffer): void;
|
|
34
38
|
outputFileAsync(filePath: string, data: string | Buffer): Promise<void>;
|
package/dist/fs/fs2.js
CHANGED
|
@@ -61,10 +61,16 @@ class FS2 {
|
|
|
61
61
|
async writeFileAsync(filePath, data, opt) {
|
|
62
62
|
await fsp.writeFile(filePath, data, opt);
|
|
63
63
|
}
|
|
64
|
+
writePrettyJson(filePath, data) {
|
|
65
|
+
this.writeJson(filePath, data, prettyJsonOptions);
|
|
66
|
+
}
|
|
64
67
|
writeJson(filePath, data, opt) {
|
|
65
68
|
const str = stringify(data, opt);
|
|
66
69
|
fs.writeFileSync(filePath, str);
|
|
67
70
|
}
|
|
71
|
+
async writePrettyJsonAsync(filePath, data) {
|
|
72
|
+
await this.writeJsonAsync(filePath, data, prettyJsonOptions);
|
|
73
|
+
}
|
|
68
74
|
async writeJsonAsync(filePath, data, opt) {
|
|
69
75
|
const str = stringify(data, opt);
|
|
70
76
|
await fsp.writeFile(filePath, str);
|
|
@@ -75,10 +81,16 @@ class FS2 {
|
|
|
75
81
|
async appendFileAsync(filePath, data) {
|
|
76
82
|
await fsp.appendFile(filePath, data);
|
|
77
83
|
}
|
|
84
|
+
outputPrettyJson(filePath, data) {
|
|
85
|
+
this.outputJson(filePath, data, prettyJsonOptions);
|
|
86
|
+
}
|
|
78
87
|
outputJson(filePath, data, opt) {
|
|
79
88
|
const str = stringify(data, opt);
|
|
80
89
|
this.outputFile(filePath, str);
|
|
81
90
|
}
|
|
91
|
+
async outputPrettyAsync(filePath, data) {
|
|
92
|
+
await this.outputJsonAsync(filePath, data, prettyJsonOptions);
|
|
93
|
+
}
|
|
82
94
|
async outputJsonAsync(filePath, data, opt) {
|
|
83
95
|
const str = stringify(data, opt);
|
|
84
96
|
await this.outputFileAsync(filePath, str);
|
|
@@ -266,6 +278,7 @@ class FS2 {
|
|
|
266
278
|
createReadStream = fs.createReadStream;
|
|
267
279
|
}
|
|
268
280
|
export const fs2 = new FS2();
|
|
281
|
+
const prettyJsonOptions = { spaces: 2 };
|
|
269
282
|
function stringify(data, opt) {
|
|
270
283
|
// If pretty-printing is enabled (spaces) - also add a newline at the end (to match our prettier config)
|
|
271
284
|
return JSON.stringify(data, null, opt?.spaces) + (opt?.spaces ? '\n' : '');
|
package/package.json
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { AsyncMemoCache, MemoCache } from '@naturalcycles/js-lib/decorators'
|
|
2
|
+
import { MISS } from '@naturalcycles/js-lib/types'
|
|
2
3
|
import { LRUCache } from 'lru-cache'
|
|
3
4
|
|
|
4
5
|
// Partial, to be able to provide default `max`
|
|
@@ -38,6 +39,9 @@ export class LRUMemoCache<KEY = any, VALUE = any> implements MemoCache<KEY, VALU
|
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
41
|
|
|
42
|
+
// lru-cache never stores `undefined` (`set(k, undefined)` deletes `k`), so it's stored as this sentinel
|
|
43
|
+
const UNDEFINED = Symbol('undefined')
|
|
44
|
+
|
|
41
45
|
/**
|
|
42
46
|
* Same as LRUMemoCache, but implements AsyncMemoCache interface, for when you need it.
|
|
43
47
|
*/
|
|
@@ -55,12 +59,14 @@ export class LRUAsyncMemoCache<KEY = any, VALUE = any> implements AsyncMemoCache
|
|
|
55
59
|
return this.lru.has(k)
|
|
56
60
|
}
|
|
57
61
|
|
|
58
|
-
async get(k: any): Promise<
|
|
59
|
-
|
|
62
|
+
async get(k: any): Promise<VALUE | typeof MISS> {
|
|
63
|
+
const v = this.lru.get(k)
|
|
64
|
+
if (v === undefined) return MISS
|
|
65
|
+
return v === UNDEFINED ? (undefined as VALUE) : v
|
|
60
66
|
}
|
|
61
67
|
|
|
62
68
|
async set(k: any, v: any): Promise<void> {
|
|
63
|
-
this.lru.set(k, v)
|
|
69
|
+
this.lru.set(k, v === undefined ? UNDEFINED : v)
|
|
64
70
|
}
|
|
65
71
|
|
|
66
72
|
async clear(): Promise<void> {
|
package/src/fs/fs2.ts
CHANGED
|
@@ -77,11 +77,19 @@ class FS2 {
|
|
|
77
77
|
await fsp.writeFile(filePath, data, opt)
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
writePrettyJson(filePath: string, data: any): void {
|
|
81
|
+
this.writeJson(filePath, data, prettyJsonOptions)
|
|
82
|
+
}
|
|
83
|
+
|
|
80
84
|
writeJson(filePath: string, data: any, opt?: JsonOptions): void {
|
|
81
85
|
const str = stringify(data, opt)
|
|
82
86
|
fs.writeFileSync(filePath, str)
|
|
83
87
|
}
|
|
84
88
|
|
|
89
|
+
async writePrettyJsonAsync(filePath: string, data: any): Promise<void> {
|
|
90
|
+
await this.writeJsonAsync(filePath, data, prettyJsonOptions)
|
|
91
|
+
}
|
|
92
|
+
|
|
85
93
|
async writeJsonAsync(filePath: string, data: any, opt?: JsonOptions): Promise<void> {
|
|
86
94
|
const str = stringify(data, opt)
|
|
87
95
|
await fsp.writeFile(filePath, str)
|
|
@@ -95,11 +103,19 @@ class FS2 {
|
|
|
95
103
|
await fsp.appendFile(filePath, data)
|
|
96
104
|
}
|
|
97
105
|
|
|
106
|
+
outputPrettyJson(filePath: string, data: any): void {
|
|
107
|
+
this.outputJson(filePath, data, prettyJsonOptions)
|
|
108
|
+
}
|
|
109
|
+
|
|
98
110
|
outputJson(filePath: string, data: any, opt?: JsonOptions): void {
|
|
99
111
|
const str = stringify(data, opt)
|
|
100
112
|
this.outputFile(filePath, str)
|
|
101
113
|
}
|
|
102
114
|
|
|
115
|
+
async outputPrettyAsync(filePath: string, data: any): Promise<void> {
|
|
116
|
+
await this.outputJsonAsync(filePath, data, prettyJsonOptions)
|
|
117
|
+
}
|
|
118
|
+
|
|
103
119
|
async outputJsonAsync(filePath: string, data: any, opt?: JsonOptions): Promise<void> {
|
|
104
120
|
const str = stringify(data, opt)
|
|
105
121
|
await this.outputFileAsync(filePath, str)
|
|
@@ -311,6 +327,8 @@ class FS2 {
|
|
|
311
327
|
|
|
312
328
|
export const fs2 = new FS2()
|
|
313
329
|
|
|
330
|
+
const prettyJsonOptions: JsonOptions = { spaces: 2 }
|
|
331
|
+
|
|
314
332
|
export interface JsonOptions {
|
|
315
333
|
spaces?: number
|
|
316
334
|
}
|