polystore 0.15.2 → 0.15.4
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/package.json +2 -1
- package/src/clients/file.js +22 -33
- package/src/index.js +8 -3
- package/src/utils.js +0 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "polystore",
|
|
3
|
-
"version": "0.15.
|
|
3
|
+
"version": "0.15.4",
|
|
4
4
|
"description": "A small compatibility layer for many popular KV stores like localStorage, Redis, FileSystem, etc.",
|
|
5
5
|
"homepage": "https://polystore.dev/",
|
|
6
6
|
"repository": "https://github.com/franciscop/polystore.git",
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
"funding": "https://www.paypal.me/franciscopresencia/19",
|
|
9
9
|
"author": "Francisco Presencia <public@francisco.io> (https://francisco.io/)",
|
|
10
10
|
"type": "module",
|
|
11
|
+
"sideEffects": false,
|
|
11
12
|
"main": "src/index.js",
|
|
12
13
|
"types": "src/index.d.ts",
|
|
13
14
|
"files": [
|
package/src/clients/file.js
CHANGED
|
@@ -2,34 +2,25 @@
|
|
|
2
2
|
export default class File {
|
|
3
3
|
// Check if this is the right class for the given client
|
|
4
4
|
static test(client) {
|
|
5
|
-
if (
|
|
5
|
+
if (client instanceof URL) client = client.href;
|
|
6
|
+
return (
|
|
6
7
|
typeof client === "string" &&
|
|
7
|
-
client.startsWith("file
|
|
8
|
+
client.startsWith("file://") &&
|
|
8
9
|
client.includes(".")
|
|
9
|
-
)
|
|
10
|
-
return true;
|
|
11
|
-
return (
|
|
12
|
-
client instanceof URL &&
|
|
13
|
-
client.protocol === "file:" &&
|
|
14
|
-
client.pathname.includes(".")
|
|
15
10
|
);
|
|
16
11
|
}
|
|
17
12
|
|
|
18
13
|
constructor(file) {
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
if (file instanceof URL) file = file.href;
|
|
15
|
+
this.file = file.replace(/^file:\/\//, "");
|
|
21
16
|
|
|
22
17
|
// Run this once on launch; import the FS module and reset the file
|
|
23
18
|
this.promise = (async () => {
|
|
24
|
-
const [fsp, path] = await Promise.all([
|
|
25
|
-
import("node:fs/promises"),
|
|
26
|
-
import("node:path"),
|
|
27
|
-
]);
|
|
28
|
-
|
|
29
19
|
// We want to make sure the file already exists, so attempt to
|
|
30
20
|
// create the folders and the file (but not OVERWRITE it, that's why the x flag)
|
|
31
21
|
// It fails if it already exists, hence the catch case
|
|
32
|
-
const
|
|
22
|
+
const fsp = await import("node:fs/promises");
|
|
23
|
+
const folder = this.file.split("/").slice(0, -1).join("/");
|
|
33
24
|
await fsp.mkdir(folder, { recursive: true }).catch(() => {});
|
|
34
25
|
await fsp.writeFile(this.file, "{}", { flag: "wx" }).catch((err) => {
|
|
35
26
|
if (err.code !== "EEXIST") throw err;
|
|
@@ -39,24 +30,23 @@ export default class File {
|
|
|
39
30
|
}
|
|
40
31
|
|
|
41
32
|
// Internal
|
|
42
|
-
async
|
|
33
|
+
#read = async () => {
|
|
43
34
|
const fsp = await this.promise;
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
35
|
+
const data = await fsp.readFile(this.file, "utf8");
|
|
36
|
+
return JSON.parse(data || "{}");
|
|
37
|
+
};
|
|
48
38
|
|
|
49
|
-
async
|
|
39
|
+
#write = async (data) => {
|
|
50
40
|
const fsp = await this.promise;
|
|
51
|
-
|
|
52
|
-
}
|
|
41
|
+
fsp.writeFile(this.file, JSON.stringify(data, null, 2));
|
|
42
|
+
};
|
|
53
43
|
|
|
54
|
-
async
|
|
44
|
+
get = async (key) => {
|
|
55
45
|
const data = await this.#read();
|
|
56
46
|
return data[key] ?? null;
|
|
57
|
-
}
|
|
47
|
+
};
|
|
58
48
|
|
|
59
|
-
async
|
|
49
|
+
set = async (key, value) => {
|
|
60
50
|
const data = await this.#read();
|
|
61
51
|
if (value === null) {
|
|
62
52
|
delete data[key];
|
|
@@ -65,7 +55,7 @@ export default class File {
|
|
|
65
55
|
}
|
|
66
56
|
await this.#write(data);
|
|
67
57
|
return key;
|
|
68
|
-
}
|
|
58
|
+
};
|
|
69
59
|
|
|
70
60
|
async *iterate(prefix = "") {
|
|
71
61
|
const data = await this.#read();
|
|
@@ -75,10 +65,9 @@ export default class File {
|
|
|
75
65
|
}
|
|
76
66
|
}
|
|
77
67
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
68
|
+
// Bulk updates are worth creating a custom method here
|
|
69
|
+
clear = async (prefix = "") => {
|
|
70
|
+
if (!prefix) return this.#write({});
|
|
82
71
|
|
|
83
72
|
const data = await this.#read();
|
|
84
73
|
for (let key in data) {
|
|
@@ -87,5 +76,5 @@ export default class File {
|
|
|
87
76
|
}
|
|
88
77
|
}
|
|
89
78
|
await this.#write(data);
|
|
90
|
-
}
|
|
79
|
+
};
|
|
91
80
|
}
|
package/src/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import clients from "./clients/index.js";
|
|
2
|
-
import { createId,
|
|
2
|
+
import { createId, parse, unix } from "./utils.js";
|
|
3
3
|
|
|
4
4
|
class Store {
|
|
5
5
|
PREFIX = "";
|
|
@@ -25,10 +25,15 @@ class Store {
|
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
//
|
|
29
|
-
if (
|
|
28
|
+
// We get passed a class
|
|
29
|
+
if (
|
|
30
|
+
typeof store === "function" &&
|
|
31
|
+
/^class\s/.test(Function.prototype.toString.call(store))
|
|
32
|
+
) {
|
|
30
33
|
return new store();
|
|
31
34
|
}
|
|
35
|
+
|
|
36
|
+
// A raw one, we just receive the single instance to use directly
|
|
32
37
|
return store;
|
|
33
38
|
}
|
|
34
39
|
|
package/src/utils.js
CHANGED
|
@@ -41,13 +41,6 @@ export function createId() {
|
|
|
41
41
|
return id;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
export function isClass(func) {
|
|
45
|
-
return (
|
|
46
|
-
typeof func === "function" &&
|
|
47
|
-
/^class\s/.test(Function.prototype.toString.call(func))
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
44
|
export function unix(expires) {
|
|
52
45
|
const now = new Date().getTime();
|
|
53
46
|
return expires === null ? null : now + expires * 1000;
|