polystore 0.15.1 → 0.15.3

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.15.1",
3
+ "version": "0.15.3",
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": [
@@ -1,71 +1,39 @@
1
- // Use fetch()
1
+ const enc = encodeURIComponent; // Optimization of size
2
+
3
+ // Handle an API endpoint with fetch()
2
4
  export default class Api {
3
- // Indicate that the file handler does NOT handle expirations
5
+ // Indicate that the file handler DOES handle expirations
4
6
  EXPIRES = true;
5
7
 
6
- // Check whether the given store is a FILE-type
7
- static test(client) {
8
- return (
9
- typeof client === "string" &&
10
- (client.startsWith("https://") || client.startsWith("http://"))
11
- );
12
- }
8
+ static test = (client) =>
9
+ typeof client === "string" && /^https?:\/\//.test(client);
13
10
 
14
11
  constructor(client) {
15
- client = client.replace(/\/$/, "") + "/";
16
- this.client = async (path, opts = {}) => {
17
- const query = Object.entries(opts.query || {})
18
- .map(([k, v]) => `${k}=${encodeURIComponent(v)}`)
19
- .join("&");
20
- let url = client + path.replace(/^\//, "") + "?" + query;
21
- opts.headers = opts.headers || {};
22
- opts.headers.accept = "application/json";
23
- if (opts.body) opts.headers["content-type"] = "application/json";
24
- const res = await fetch(url, opts);
25
- if (!res.ok) return null;
26
- if (!res.headers["content-type"] !== "application/json") {
27
- console.warn("Not a JSON API");
28
- }
29
- return res.json();
12
+ client = client.replace(/\/$/, "");
13
+ this.client = async (path, method = "GET", body) => {
14
+ const url = `${client}/${path.replace(/^\//, "")}`;
15
+ const headers = { accept: "application/json" };
16
+ if (body) headers["content-type"] = "application/json";
17
+ const res = await fetch(url, { method, headers, body });
18
+ return res.ok ? res.json() : null;
30
19
  };
31
20
  }
32
21
 
33
- async get(key) {
34
- return await this.client(`/${key}`);
35
- }
22
+ get = (key) => this.client(`/${enc(key)}`);
36
23
 
37
- async set(key, value, { expires } = {}) {
38
- return await this.client(`/${encodeURIComponent(key)}`, {
39
- query: { expires },
40
- method: "put",
41
- body: JSON.stringify(value),
42
- });
43
- }
24
+ set = (key, value, { expires } = {}) =>
25
+ this.client(
26
+ `/${enc(key)}?expires=${enc(expires || "")}`,
27
+ "PUT",
28
+ JSON.stringify(value),
29
+ );
44
30
 
45
- async del(key) {
46
- return await this.client(`/${encodeURIComponent(key)}`, {
47
- method: "delete",
48
- });
49
- }
31
+ del = (key) => this.client(`/${enc(key)}`, "DELETE");
50
32
 
51
- // Since we have pagination, we don't want to get all of the
52
- // keys at once if we can avoid it
53
33
  async *iterate(prefix = "") {
54
- const data = await this.client("/", { query: { prefix } });
55
- if (!data) return [];
56
- for (let [key, value] of Object.entries(data)) {
34
+ const data = await this.client(`/?prefix=${enc(prefix)}`);
35
+ for (let [key, value] of Object.entries(data || {})) {
57
36
  yield [prefix + key, value];
58
37
  }
59
38
  }
60
-
61
- async keys(prefix = "") {
62
- const data = await this.client(`/`, { query: { prefix } });
63
- if (!data) return [];
64
- return Object.keys(data).map((k) => prefix + k);
65
- }
66
-
67
- async clear(prefix = "") {
68
- const list = await this.keys(prefix);
69
- return Promise.all(list.map((k) => this.del(k)));
70
- }
71
39
  }
package/src/server.js CHANGED
@@ -1,7 +1,6 @@
1
1
  // This is an example server implementation of the HTTP library!
2
2
  import http from "node:http";
3
3
  import kv from "./index.js";
4
- import { parse } from "./utils.js";
5
4
 
6
5
  // Modify this to use any sub-store as desired. It's nice
7
6
  // to use polystore itself for the polystore server library!'