sliftutils 0.36.0 → 0.38.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/index.d.ts +26 -25
- package/package.json +1 -1
- package/storage/CBORStorage.d.ts +16 -0
- package/storage/CBORStorage.ts +37 -0
- package/storage/DiskCollection.d.ts +6 -25
- package/storage/DiskCollection.ts +14 -77
package/index.d.ts
CHANGED
|
@@ -417,6 +417,26 @@ declare module "sliftutils/render-utils/observer" {
|
|
|
417
417
|
|
|
418
418
|
}
|
|
419
419
|
|
|
420
|
+
declare module "sliftutils/storage/CBORStorage" {
|
|
421
|
+
/// <reference types="node" />
|
|
422
|
+
/// <reference types="node" />
|
|
423
|
+
import { IStorage } from "./IStorage";
|
|
424
|
+
export declare class CBORStorage<T> implements IStorage<T> {
|
|
425
|
+
private storage;
|
|
426
|
+
constructor(storage: IStorage<Buffer>);
|
|
427
|
+
get(key: string): Promise<T | undefined>;
|
|
428
|
+
set(key: string, value: T): Promise<void>;
|
|
429
|
+
remove(key: string): Promise<void>;
|
|
430
|
+
getKeys(): Promise<string[]>;
|
|
431
|
+
getInfo(key: string): Promise<{
|
|
432
|
+
size: number;
|
|
433
|
+
lastModified: number;
|
|
434
|
+
} | undefined>;
|
|
435
|
+
reset(): Promise<void>;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
}
|
|
439
|
+
|
|
420
440
|
declare module "sliftutils/storage/CachedStorage" {
|
|
421
441
|
import { StorageSync } from "./StorageObservable";
|
|
422
442
|
export declare function newCachedStrStorage<T>(folder: string, getValue: (key: string) => Promise<T>): StorageSync<T>;
|
|
@@ -449,31 +469,12 @@ declare module "sliftutils/storage/DiskCollection" {
|
|
|
449
469
|
import { TransactionStorage } from "./TransactionStorage";
|
|
450
470
|
export declare class DiskCollection<T> implements IStorageSync<T> {
|
|
451
471
|
private collectionName;
|
|
452
|
-
private
|
|
453
|
-
constructor(collectionName: string,
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
get(key: string): T | undefined;
|
|
459
|
-
getPromise(key: string): Promise<T | undefined>;
|
|
460
|
-
set(key: string, value: T): void;
|
|
461
|
-
remove(key: string): void;
|
|
462
|
-
getKeys(): string[];
|
|
463
|
-
getKeysPromise(): Promise<string[]>;
|
|
464
|
-
getEntries(): [string, T][];
|
|
465
|
-
getValues(): T[];
|
|
466
|
-
getValuesPromise(): Promise<T[]>;
|
|
467
|
-
getInfo(key: string): {
|
|
468
|
-
size: number;
|
|
469
|
-
lastModified: number;
|
|
470
|
-
} | undefined;
|
|
471
|
-
reset(): Promise<void>;
|
|
472
|
-
}
|
|
473
|
-
export declare class DiskCollectionBrowser<T> implements IStorageSync<T> {
|
|
474
|
-
private collectionName;
|
|
475
|
-
private writeDelay?;
|
|
476
|
-
constructor(collectionName: string, writeDelay?: number | undefined);
|
|
472
|
+
private config?;
|
|
473
|
+
constructor(collectionName: string, config?: {
|
|
474
|
+
writeDelay?: number | undefined;
|
|
475
|
+
cbor?: boolean | undefined;
|
|
476
|
+
noPrompt?: boolean | undefined;
|
|
477
|
+
} | undefined);
|
|
477
478
|
transactionStorage: TransactionStorage | undefined;
|
|
478
479
|
initStorage(): Promise<IStorage<T>>;
|
|
479
480
|
baseStorage: Promise<IStorage<T>>;
|
package/package.json
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { IStorage } from "./IStorage";
|
|
4
|
+
export declare class CBORStorage<T> implements IStorage<T> {
|
|
5
|
+
private storage;
|
|
6
|
+
constructor(storage: IStorage<Buffer>);
|
|
7
|
+
get(key: string): Promise<T | undefined>;
|
|
8
|
+
set(key: string, value: T): Promise<void>;
|
|
9
|
+
remove(key: string): Promise<void>;
|
|
10
|
+
getKeys(): Promise<string[]>;
|
|
11
|
+
getInfo(key: string): Promise<{
|
|
12
|
+
size: number;
|
|
13
|
+
lastModified: number;
|
|
14
|
+
} | undefined>;
|
|
15
|
+
reset(): Promise<void>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { lazy } from "socket-function/src/caching";
|
|
2
|
+
import { IStorage } from "./IStorage";
|
|
3
|
+
import cborx from "cbor-x";
|
|
4
|
+
const cborEncoder = lazy(() => new cborx.Encoder({ structuredClone: true }));
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export class CBORStorage<T> implements IStorage<T> {
|
|
8
|
+
constructor(private storage: IStorage<Buffer>) { }
|
|
9
|
+
public async get(key: string): Promise<T | undefined> {
|
|
10
|
+
let buffer = await this.storage.get(key);
|
|
11
|
+
if (buffer === undefined) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
return cborEncoder().decode(buffer);
|
|
16
|
+
} catch (e) {
|
|
17
|
+
console.warn(`Failed to parse CBOR for key: ${key}`, e);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
public async set(key: string, value: T): Promise<void> {
|
|
21
|
+
await this.storage.set(key, cborEncoder().encode(value));
|
|
22
|
+
}
|
|
23
|
+
public async remove(key: string): Promise<void> {
|
|
24
|
+
await this.storage.remove(key);
|
|
25
|
+
}
|
|
26
|
+
public async getKeys(): Promise<string[]> {
|
|
27
|
+
return await this.storage.getKeys();
|
|
28
|
+
}
|
|
29
|
+
public async getInfo(key: string) {
|
|
30
|
+
return await this.storage.getInfo(key);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public async reset() {
|
|
34
|
+
await this.storage.reset();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
@@ -5,31 +5,12 @@ import { StorageSync } from "./StorageObservable";
|
|
|
5
5
|
import { TransactionStorage } from "./TransactionStorage";
|
|
6
6
|
export declare class DiskCollection<T> implements IStorageSync<T> {
|
|
7
7
|
private collectionName;
|
|
8
|
-
private
|
|
9
|
-
constructor(collectionName: string,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
get(key: string): T | undefined;
|
|
15
|
-
getPromise(key: string): Promise<T | undefined>;
|
|
16
|
-
set(key: string, value: T): void;
|
|
17
|
-
remove(key: string): void;
|
|
18
|
-
getKeys(): string[];
|
|
19
|
-
getKeysPromise(): Promise<string[]>;
|
|
20
|
-
getEntries(): [string, T][];
|
|
21
|
-
getValues(): T[];
|
|
22
|
-
getValuesPromise(): Promise<T[]>;
|
|
23
|
-
getInfo(key: string): {
|
|
24
|
-
size: number;
|
|
25
|
-
lastModified: number;
|
|
26
|
-
} | undefined;
|
|
27
|
-
reset(): Promise<void>;
|
|
28
|
-
}
|
|
29
|
-
export declare class DiskCollectionBrowser<T> implements IStorageSync<T> {
|
|
30
|
-
private collectionName;
|
|
31
|
-
private writeDelay?;
|
|
32
|
-
constructor(collectionName: string, writeDelay?: number | undefined);
|
|
8
|
+
private config?;
|
|
9
|
+
constructor(collectionName: string, config?: {
|
|
10
|
+
writeDelay?: number | undefined;
|
|
11
|
+
cbor?: boolean | undefined;
|
|
12
|
+
noPrompt?: boolean | undefined;
|
|
13
|
+
} | undefined);
|
|
33
14
|
transactionStorage: TransactionStorage | undefined;
|
|
34
15
|
initStorage(): Promise<IStorage<T>>;
|
|
35
16
|
baseStorage: Promise<IStorage<T>>;
|
|
@@ -9,97 +9,33 @@ import { PendingStorage } from "./PendingStorage";
|
|
|
9
9
|
import { isDefined } from "../misc/types";
|
|
10
10
|
import { PrivateFileSystemStorage } from "./PrivateFileSystemStorage";
|
|
11
11
|
import { isInChromeExtension } from "../misc/environment";
|
|
12
|
+
import { CBORStorage } from "./CBORStorage";
|
|
12
13
|
|
|
13
14
|
export class DiskCollection<T> implements IStorageSync<T> {
|
|
14
15
|
constructor(
|
|
15
16
|
private collectionName: string,
|
|
16
|
-
private
|
|
17
|
+
private config?: {
|
|
18
|
+
writeDelay?: number;
|
|
19
|
+
cbor?: boolean;
|
|
20
|
+
noPrompt?: boolean;
|
|
21
|
+
}
|
|
17
22
|
) {
|
|
18
23
|
}
|
|
19
24
|
public transactionStorage: TransactionStorage | undefined;
|
|
20
25
|
async initStorage(): Promise<IStorage<T>> {
|
|
21
26
|
// If a Chrome extension, just return null.
|
|
22
27
|
if (isInChromeExtension()) return null as any;
|
|
23
|
-
let
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
this.transactionStorage = baseStorage;
|
|
28
|
-
return new JSONStorage<T>(baseStorage);
|
|
29
|
-
}
|
|
30
|
-
public baseStorage = this.initStorage();
|
|
31
|
-
private synced = new StorageSync(
|
|
32
|
-
new PendingStorage(`Collection (${this.collectionName})`,
|
|
33
|
-
new DelayedStorage<T>(this.baseStorage)
|
|
34
|
-
)
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
public get(key: string): T | undefined {
|
|
38
|
-
return this.synced.get(key);
|
|
39
|
-
}
|
|
40
|
-
public async getPromise(key: string): Promise<T | undefined> {
|
|
41
|
-
let base = await this.baseStorage;
|
|
42
|
-
return base.get(key);
|
|
43
|
-
}
|
|
44
|
-
public set(key: string, value: T): void {
|
|
45
|
-
this.synced.set(key, value);
|
|
46
|
-
}
|
|
47
|
-
public remove(key: string): void {
|
|
48
|
-
this.synced.remove(key);
|
|
49
|
-
}
|
|
50
|
-
public getKeys(): string[] {
|
|
51
|
-
return this.synced.getKeys();
|
|
52
|
-
}
|
|
53
|
-
public getKeysPromise(): Promise<string[]> {
|
|
54
|
-
return this.synced.getKeysPromise();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
public getEntries(): [string, T][] {
|
|
58
|
-
let keys = this.getKeys();
|
|
59
|
-
return keys.map(key => [key, this.get(key)]).filter(([_, value]) => isDefined(value)) as [string, T][];
|
|
60
|
-
}
|
|
61
|
-
public getValues(): T[] {
|
|
62
|
-
let keys = this.getKeys();
|
|
63
|
-
return keys.map(key => this.get(key)).filter(isDefined);
|
|
64
|
-
}
|
|
65
|
-
public async getValuesPromise(): Promise<T[]> {
|
|
66
|
-
let keys = await this.getKeysPromise();
|
|
67
|
-
let values: T[] = [];
|
|
68
|
-
for (let key of keys) {
|
|
69
|
-
let value = await this.getPromise(key);
|
|
70
|
-
if (isDefined(value)) {
|
|
71
|
-
values.push(value);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return values;
|
|
75
|
-
}
|
|
76
|
-
public getInfo(key: string) {
|
|
77
|
-
return this.synced.getInfo(key);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
public async reset() {
|
|
81
|
-
await this.synced.reset();
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export class DiskCollectionBrowser<T> implements IStorageSync<T> {
|
|
86
|
-
constructor(
|
|
87
|
-
private collectionName: string,
|
|
88
|
-
private writeDelay?: number,
|
|
89
|
-
) {
|
|
90
|
-
}
|
|
91
|
-
public transactionStorage: TransactionStorage | undefined;
|
|
92
|
-
async initStorage(): Promise<IStorage<T>> {
|
|
93
|
-
let curCollection: IStorageRaw | undefined;
|
|
94
|
-
if (isNode()) {
|
|
28
|
+
let curCollection: IStorageRaw;
|
|
29
|
+
if (this.config?.noPrompt && !isNode()) {
|
|
30
|
+
curCollection = await new PrivateFileSystemStorage(`collections/${this.collectionName}`);
|
|
31
|
+
} else {
|
|
95
32
|
let fileStorage = await getFileStorage();
|
|
96
33
|
let collections = await fileStorage.folder.getStorage("collections");
|
|
97
34
|
curCollection = await collections.folder.getStorage(this.collectionName);
|
|
98
|
-
} else {
|
|
99
|
-
curCollection = await new PrivateFileSystemStorage(`collections/${this.collectionName}`);
|
|
100
35
|
}
|
|
101
|
-
let baseStorage = new TransactionStorage(curCollection, this.collectionName);
|
|
102
|
-
|
|
36
|
+
let baseStorage = new TransactionStorage(curCollection, this.collectionName, this.config?.writeDelay);
|
|
37
|
+
this.transactionStorage = baseStorage;
|
|
38
|
+
return this.config?.cbor ? new CBORStorage<T>(baseStorage) : new JSONStorage<T>(baseStorage);
|
|
103
39
|
}
|
|
104
40
|
public baseStorage = this.initStorage();
|
|
105
41
|
private synced = new StorageSync(
|
|
@@ -156,6 +92,7 @@ export class DiskCollectionBrowser<T> implements IStorageSync<T> {
|
|
|
156
92
|
}
|
|
157
93
|
}
|
|
158
94
|
|
|
95
|
+
|
|
159
96
|
export class DiskCollectionPromise<T> implements IStorage<T> {
|
|
160
97
|
constructor(
|
|
161
98
|
private collectionName: string,
|