polystore 0.11.2 → 0.11.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polystore",
3
- "version": "0.11.2",
3
+ "version": "0.11.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",
@@ -23,8 +23,7 @@ export default class Cloudflare {
23
23
 
24
24
  async set(key, value, { expires } = {}) {
25
25
  const expirationTtl = expires ? Math.round(expires) : undefined;
26
- this.client.put(key, JSON.stringify(value), { expirationTtl });
27
- return key;
26
+ return this.client.put(key, JSON.stringify(value), { expirationTtl });
28
27
  }
29
28
 
30
29
  async del(key) {
@@ -40,8 +39,7 @@ export default class Cloudflare {
40
39
  const keys = raw.keys.map((k) => k.name);
41
40
  for (let key of keys) {
42
41
  const value = await this.get(key);
43
- // By the time this specific value is read, it could
44
- // already be gone!
42
+ // By the time this specific value is read, it could be gone!
45
43
  if (!value) continue;
46
44
  yield [key, value];
47
45
  }
@@ -63,7 +61,7 @@ export default class Cloudflare {
63
61
  async entries(prefix = "") {
64
62
  const keys = await this.keys(prefix);
65
63
  const values = await Promise.all(keys.map((k) => this.get(k)));
66
- return keys.map((key, i) => [key, values[i]]);
64
+ return keys.map((k, i) => [k, values[i]]);
67
65
  }
68
66
 
69
67
  async clear(prefix = "") {
@@ -31,22 +31,19 @@ export default class Cookie {
31
31
 
32
32
  set(key, data = null, { expires } = {}) {
33
33
  // Setting it to null deletes it
34
- if (data === null) {
35
- data = "";
36
- expires = -100;
37
- }
38
-
39
34
  let expireStr = "";
40
35
  // NOTE: 0 is already considered here!
41
36
  if (expires !== null) {
42
- const now = new Date().getTime();
43
- const time = new Date(now + expires * 1000).toUTCString();
37
+ const time = new Date(Date.now() + expires * 1000).toUTCString();
44
38
  expireStr = `; expires=${time}`;
45
39
  }
46
40
 
47
- const value = encodeURIComponent(JSON.stringify(data));
41
+ const value = encodeURIComponent(JSON.stringify(data || ""));
48
42
  document.cookie = encodeURIComponent(key) + "=" + value + expireStr;
49
- return key;
43
+ }
44
+
45
+ del(key) {
46
+ this.set(key, "", { expires: -100 });
50
47
  }
51
48
 
52
49
  async *iterate(prefix = "") {
@@ -14,11 +14,11 @@ export default class Etcd {
14
14
  }
15
15
 
16
16
  async set(key, value) {
17
- if (value === null) {
18
- return this.client.delete().key(key).exec();
19
- }
17
+ return this.client.put(key).value(JSON.stringify(value));
18
+ }
20
19
 
21
- await this.client.put(key).value(JSON.stringify(value));
20
+ async del(key) {
21
+ return this.client.delete().key(key).exec();
22
22
  }
23
23
 
24
24
  async *iterate(prefix = "") {
@@ -28,19 +28,18 @@ export default class Etcd {
28
28
  }
29
29
  }
30
30
 
31
+ async keys(prefix = "") {
32
+ return this.client.getAll().prefix(prefix).keys();
33
+ }
34
+
31
35
  async entries(prefix = "") {
32
- const keys = await this.client.getAll().prefix(prefix).keys();
36
+ const keys = await this.keys(prefix);
33
37
  const values = await Promise.all(keys.map((k) => this.get(k)));
34
38
  return keys.map((k, i) => [k, values[i]]);
35
39
  }
36
40
 
37
41
  async clear(prefix = "") {
38
42
  if (!prefix) return this.client.delete().all();
39
-
40
43
  return this.client.delete().prefix(prefix);
41
44
  }
42
-
43
- async close() {
44
- // return this.client.close();
45
- }
46
45
  }
@@ -14,12 +14,11 @@ export default class Forage {
14
14
  }
15
15
 
16
16
  async set(key, value) {
17
- if (value === null) {
18
- await this.client.removeItem(key);
19
- } else {
20
- await this.client.setItem(key, value);
21
- }
22
- return key;
17
+ return this.client.setItem(key, value);
18
+ }
19
+
20
+ async del(key) {
21
+ return this.client.removeItem(key);
23
22
  }
24
23
 
25
24
  async *iterate(prefix = "") {
@@ -14,33 +14,27 @@ export default class Memory {
14
14
  }
15
15
 
16
16
  set(key, data) {
17
- this.client.set(key, data);
17
+ return this.client.set(key, data);
18
18
  }
19
19
 
20
20
  del(key) {
21
- this.client.delete(key);
21
+ return this.client.delete(key);
22
22
  }
23
23
 
24
24
  *iterate(prefix = "") {
25
- const entries = this.entries();
26
- for (const entry of entries) {
25
+ for (const entry of this.client.entries()) {
27
26
  if (!entry[0].startsWith(prefix)) continue;
28
27
  yield entry;
29
28
  }
30
29
  }
31
30
 
32
- // Group methods
33
- entries(prefix = "") {
34
- const entries = [...this.client.entries()];
35
- return entries.filter((p) => p[0].startsWith(prefix));
36
- }
37
-
38
31
  clear(prefix = "") {
39
32
  // Delete the whole dataset
40
33
  if (!prefix) return this.client.clear();
41
34
 
42
35
  // Delete them in a map
43
- const list = this.entries(prefix);
44
- return Promise.all(list.map((e) => e[0]).map((k) => this.del(k)));
36
+ return [...this.client.keys()]
37
+ .filter((k) => k.startsWith(prefix))
38
+ .map((k) => this.del(k));
45
39
  }
46
40
  }
@@ -19,13 +19,12 @@ export default class Redis {
19
19
  }
20
20
 
21
21
  async set(key, value, { expires } = {}) {
22
- if (value === null || expires === 0) {
23
- return this.client.del(key);
24
- }
25
-
26
22
  const EX = expires ? Math.round(expires) : undefined;
27
- await this.client.set(key, JSON.stringify(value), { EX });
28
- return key;
23
+ return this.client.set(key, JSON.stringify(value), { EX });
24
+ }
25
+
26
+ async del(key) {
27
+ return this.client.del(key);
29
28
  }
30
29
 
31
30
  async has(key) {
@@ -42,6 +41,8 @@ export default class Redis {
42
41
  const MATCH = prefix + "*";
43
42
  for await (const key of this.client.scanIterator({ MATCH })) {
44
43
  const value = await this.get(key);
44
+ // By the time this specific value is read, it could be gone!
45
+ if (!value) continue;
45
46
  yield [key, value];
46
47
  }
47
48
  }
@@ -68,7 +69,7 @@ export default class Redis {
68
69
  if (!prefix) return this.client.flushAll();
69
70
 
70
71
  const list = await this.keys(prefix);
71
- return Promise.all(list.map((k) => this.set(k, null)));
72
+ return Promise.all(list.map((k) => this.client.del(k)));
72
73
  }
73
74
 
74
75
  async close() {
@@ -17,27 +17,20 @@ export default class WebStorage {
17
17
  }
18
18
 
19
19
  set(key, data) {
20
- if (data === null) {
21
- this.client.removeItem(key);
22
- } else {
23
- this.client.setItem(key, JSON.stringify(data));
24
- }
25
- return key;
20
+ return this.client.setItem(key, JSON.stringify(data));
26
21
  }
27
22
 
28
- *iterate(prefix = "") {
29
- const entries = this.entries(prefix);
30
- for (const entry of entries) {
31
- yield entry;
32
- }
23
+ del(key) {
24
+ return this.client.removeItem(key);
33
25
  }
34
26
 
35
- // Group methods
36
- entries(prefix = "") {
37
- const entries = Object.entries(this.client);
38
- return entries
39
- .map((p) => [p[0], p[1] ? JSON.parse(p[1]) : null])
40
- .filter((p) => p[0].startsWith(prefix));
27
+ *iterate(prefix = "") {
28
+ for (const key of Object.keys(this.client)) {
29
+ if (!key.startsWith(prefix)) continue;
30
+ const value = this.get(key);
31
+ if (!value) continue;
32
+ yield [key, value];
33
+ }
41
34
  }
42
35
 
43
36
  clear(prefix = "") {
@@ -45,6 +38,8 @@ export default class WebStorage {
45
38
  if (!prefix) return this.client.clear();
46
39
 
47
40
  // Delete them in a map
48
- return this.entries(prefix).map((e) => this.set(e[0], null));
41
+ return Object.keys(this.client)
42
+ .filter((k) => k.startsWith(prefix))
43
+ .map((k) => this.del(k));
49
44
  }
50
45
  }
package/src/index.js CHANGED
@@ -1,19 +1,11 @@
1
- /**
2
- * A number, or a string containing a number.
3
- * @typedef {(number|string|object|array)} Value
4
- */
5
-
6
1
  import clients from "./clients/index.js";
7
2
  import { createId, isClass, parse } from "./utils.js";
8
3
 
9
- // #region Store
10
4
  class Store {
11
5
  PREFIX = "";
12
6
 
13
7
  constructor(clientPromise = new Map()) {
14
8
  this.promise = Promise.resolve(clientPromise).then(async (client) => {
15
- if (client?.open) await client.open();
16
- if (client?.connect) await client.connect();
17
9
  this.client = this.#find(client);
18
10
  this.#validate(this.client);
19
11
  this.promise = null;
@@ -21,7 +13,6 @@ class Store {
21
13
  });
22
14
  }
23
15
 
24
- // #region #client()
25
16
  #find(store) {
26
17
  // Already a fully compliant KV store
27
18
  if (store instanceof Store) return store.client;
@@ -41,7 +32,6 @@ class Store {
41
32
  return store;
42
33
  }
43
34
 
44
- // #region #validate()
45
35
  #validate(client) {
46
36
  if (!client.set || !client.get || !client.iterate) {
47
37
  throw new Error(
@@ -50,20 +40,12 @@ class Store {
50
40
  }
51
41
 
52
42
  if (!client.EXPIRES) {
53
- if (client.has) {
54
- throw new Error(
55
- `You can only define client.has() when the client manages the expiration; otherwise please do NOT define .has() and let us manage it`
56
- );
57
- }
58
- if (client.keys) {
59
- throw new Error(
60
- `You can only define client.keys() when the client manages the expiration; otherwise please do NOT define .keys() and let us manage them`
61
- );
62
- }
63
- if (client.values) {
64
- console.warn(
65
- `Since this KV client does not manage expiration, it's better not to define client.values() since it doesn't allow us to evict expired keys`
66
- );
43
+ for (let method of ["has", "keys", "values"]) {
44
+ if (client[method]) {
45
+ throw new Error(
46
+ `You can only define client.${method}() when the client manages the expiration; otherwise please do NOT define .${method}() and let us manage it`
47
+ );
48
+ }
67
49
  }
68
50
  }
69
51
  }
@@ -93,21 +75,6 @@ class Store {
93
75
  return false;
94
76
  }
95
77
 
96
- // #region .add()
97
- /**
98
- * Save the data on an autogenerated key, can add expiration as well:
99
- *
100
- * ```js
101
- * const key1 = await store.add("value1");
102
- * const key2 = await store.add({ hello: "world" });
103
- * const key3 = await store.add("value3", { expires: "1h" });
104
- * ```
105
- *
106
- * **[→ Full .add() Docs](https://polystore.dev/documentation#add)**
107
- * @param {Value} value
108
- * @param {{ expires: string }} options
109
- * @returns {Promise<string>}
110
- */
111
78
  async add(value, options = {}) {
112
79
  await this.promise;
113
80
  const expires = parse(options.expire ?? options.expires);
@@ -130,22 +97,6 @@ class Store {
130
97
  return id; // The plain one without the prefix
131
98
  }
132
99
 
133
- // #region .set()
134
- /**
135
- * Save the data on the given key, can add expiration as well:
136
- *
137
- * ```js
138
- * const key = await store.set("key1", "value1");
139
- * await store.set("key2", { hello: "world" });
140
- * await store.set("key3", "value3", { expires: "1h" });
141
- * ```
142
- *
143
- * **[→ Full .set() Docs](https://polystore.dev/documentation#set)**
144
- * @param {string} key
145
- * @param {Value} value
146
- * @param {{ expires: string }} options
147
- * @returns {Promise<string>}
148
- */
149
100
  async set(key, value, options = {}) {
150
101
  await this.promise;
151
102
  const id = this.PREFIX + key;
@@ -168,23 +119,6 @@ class Store {
168
119
  return key;
169
120
  }
170
121
 
171
- // #region .get()
172
- /**
173
- * Read a single value from the KV store:
174
- *
175
- * ```js
176
- * const value1 = await store.get("key1");
177
- * // null (doesn't exist or has expired)
178
- * const value2 = await store.get("key2");
179
- * // "value2"
180
- * const value3 = await store.get("key3");
181
- * // { hello: "world" }
182
- * ```
183
- *
184
- * **[→ Full .get() Docs](https://polystore.dev/documentation#get)**
185
- * @param {string} key
186
- * @returns {Promise<Value>}
187
- */
188
122
  async get(key) {
189
123
  await this.promise;
190
124
  const id = this.PREFIX + key;
@@ -202,26 +136,6 @@ class Store {
202
136
  return data.value;
203
137
  }
204
138
 
205
- // #region .has()
206
- /**
207
- * Check whether a key exists or not:
208
- *
209
- * ```js
210
- * if (await store.has("key1")) { ... }
211
- * ```
212
- *
213
- * If you are going to use the value, it's better to just read it:
214
- *
215
- * ```js
216
- * const val = await store.get("key1");
217
- * if (val) { ... }
218
- * ```
219
- *
220
- *
221
- * **[→ Full .has() Docs](https://polystore.dev/documentation#has)**
222
- * @param {string} key
223
- * @returns {Promise<boolean>}
224
- */
225
139
  async has(key) {
226
140
  await this.promise;
227
141
  const id = this.PREFIX + key;
@@ -234,18 +148,6 @@ class Store {
234
148
  return value !== null;
235
149
  }
236
150
 
237
- // #region .del()
238
- /**
239
- * Remove a single key and its value from the store:
240
- *
241
- * ```js
242
- * const key = await store.del("key1");
243
- * ```
244
- *
245
- * **[→ Full .del() Docs](https://polystore.dev/documentation#del)**
246
- * @param {string} key
247
- * @returns {Promise<string>}
248
- */
249
151
  async del(key) {
250
152
  await this.promise;
251
153
  const id = this.PREFIX + key;
@@ -272,33 +174,19 @@ class Store {
272
174
  }
273
175
  }
274
176
 
275
- // #region .entries()
276
- /**
277
- * Return an array of the entries, in the [key, value] format:
278
- *
279
- * ```js
280
- * const entries = await store.entries();
281
- * // [["key1", "value1"], ["key2", { hello: "world" }], ...]
282
- *
283
- * // To limit it to a given prefix, use `.prefix()`:
284
- * const sessions = await store.prefix("session:").entries();
285
- * ```
286
- *
287
- * **[→ Full .entries() Docs](https://polystore.dev/documentation#entries)**
288
- * @returns {Promise<[string, Value][]>}
289
- */
290
177
  async entries() {
291
178
  await this.promise;
292
179
 
180
+ // Cut the key to size
181
+ const trim = (key) => key.slice(this.PREFIX.length);
182
+
293
183
  let list = [];
294
184
  if (this.client.entries) {
295
- list = (await this.client.entries(this.PREFIX)).map(([key, value]) => [
296
- key.slice(this.PREFIX.length),
297
- value,
298
- ]);
185
+ const entries = await this.client.entries(this.PREFIX);
186
+ list = entries.map(([key, value]) => [trim(key), value]);
299
187
  } else {
300
188
  for await (const [key, value] of this.client.iterate(this.PREFIX)) {
301
- list.push([key.slice(this.PREFIX.length), value]);
189
+ list.push([trim(key), value]);
302
190
  }
303
191
  }
304
192
 
@@ -312,21 +200,6 @@ class Store {
312
200
  .map(([key, data]) => [key, data.value]);
313
201
  }
314
202
 
315
- // #region .keys()
316
- /**
317
- * Return an array of the keys in the store:
318
- *
319
- * ```js
320
- * const keys = await store.keys();
321
- * // ["key1", "key2", ...]
322
- *
323
- * // To limit it to a given prefix, use `.prefix()`:
324
- * const sessions = await store.prefix("session:").keys();
325
- * ```
326
- *
327
- * **[→ Full .keys() Docs](https://polystore.dev/documentation#keys)**
328
- * @returns {Promise<string[]>}
329
- */
330
203
  async keys() {
331
204
  await this.promise;
332
205
 
@@ -340,21 +213,6 @@ class Store {
340
213
  return entries.map((e) => e[0]);
341
214
  }
342
215
 
343
- // #region .values()
344
- /**
345
- * Return an array of the values in the store:
346
- *
347
- * ```js
348
- * const values = await store.values();
349
- * // ["value1", { hello: "world" }, ...]
350
- *
351
- * // To limit it to a given prefix, use `.prefix()`:
352
- * const sessions = await store.prefix("session:").values();
353
- * ```
354
- *
355
- * **[→ Full .values() Docs](https://polystore.dev/documentation#values)**
356
- * @returns {Promise<Value[]>}
357
- */
358
216
  async values() {
359
217
  await this.promise;
360
218
 
@@ -370,21 +228,6 @@ class Store {
370
228
  return entries.map((e) => e[1]);
371
229
  }
372
230
 
373
- // #region .all()
374
- /**
375
- * Return an object with the keys:values in the store:
376
- *
377
- * ```js
378
- * const obj = await store.all();
379
- * // { key1: "value1", key2: { hello: "world" }, ... }
380
- *
381
- * // To limit it to a given prefix, use `.prefix()`:
382
- * const sessions = await store.prefix("session:").all();
383
- * ```
384
- *
385
- * **[→ Full .all() Docs](https://polystore.dev/documentation#all)**
386
- * @returns {Promise<{ [key:string]: Value }>}
387
- */
388
231
  async all() {
389
232
  await this.promise;
390
233
 
@@ -402,19 +245,6 @@ class Store {
402
245
  return Object.fromEntries(entries);
403
246
  }
404
247
 
405
- // #region .clear()
406
- /**
407
- * Delete all of the records of the store:
408
- *
409
- * ```js
410
- * await store.clear();
411
- * ```
412
- *
413
- * It's useful for cache invalidation, clearing the data, and testing.
414
- *
415
- * **[→ Full .clear() Docs](https://polystore.dev/documentation#clear)**
416
- * @returns {Promise<null>}
417
- */
418
248
  async clear() {
419
249
  await this.promise;
420
250
 
@@ -427,23 +257,6 @@ class Store {
427
257
  await Promise.all(keys.map((key) => this.del(key)));
428
258
  }
429
259
 
430
- // #region .prefix()
431
- /**
432
- * Create a substore where all the keys are stored with
433
- * the given prefix:
434
- *
435
- * ```js
436
- * const session = store.prefix("session:");
437
- * await session.set("key1", "value1");
438
- * console.log(await session.entries()); // session.
439
- * // [["key1", "value1"]]
440
- * console.log(await store.entries()); // store.
441
- * // [["session:key1", "value1"]]
442
- * ```
443
- *
444
- * **[→ Full .prefix() Docs](https://polystore.dev/documentation#prefix)**
445
- * @returns {Store}
446
- */
447
260
  prefix(prefix = "") {
448
261
  const store = new Store(
449
262
  Promise.resolve(this.promise).then((client) => client || this.client)
@@ -452,19 +265,6 @@ class Store {
452
265
  return store;
453
266
  }
454
267
 
455
- // #region .close()
456
- /**
457
- * Stop the connection to the store, if any:
458
- *
459
- * ```js
460
- * await session.set("key1", "value1");
461
- * await store.close();
462
- * await session.set("key2", "value2"); // error
463
- * ```
464
- *
465
- * **[→ Full .close() Docs](https://polystore.dev/documentation#close)**
466
- * @returns {Store}
467
- */
468
268
  async close() {
469
269
  await this.promise;
470
270