@b9g/cache-redis 0.1.4 → 0.1.5

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.
Files changed (3) hide show
  1. package/package.json +5 -9
  2. package/src/index.js +26 -6
  3. package/src/index.cjs +0 -18812
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@b9g/cache-redis",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Redis cache adapter for Shovel cache system",
5
5
  "keywords": [
6
6
  "cache",
@@ -24,7 +24,7 @@
24
24
  "@b9g/libuild": "^0.1.18"
25
25
  },
26
26
  "peerDependencies": {
27
- "@b9g/cache": "^0.1.4"
27
+ "@b9g/cache": "^0.1.5"
28
28
  },
29
29
  "type": "module",
30
30
  "types": "src/index.d.ts",
@@ -32,23 +32,19 @@
32
32
  "README.md",
33
33
  "src/"
34
34
  ],
35
- "main": "src/index.cjs",
36
35
  "module": "src/index.js",
37
36
  "exports": {
38
37
  ".": {
39
38
  "types": "./src/index.d.ts",
40
- "import": "./src/index.js",
41
- "require": "./src/index.cjs"
39
+ "import": "./src/index.js"
42
40
  },
43
41
  "./index": {
44
42
  "types": "./src/index.d.ts",
45
- "import": "./src/index.js",
46
- "require": "./src/index.cjs"
43
+ "import": "./src/index.js"
47
44
  },
48
45
  "./index.js": {
49
46
  "types": "./src/index.d.ts",
50
- "import": "./src/index.js",
51
- "require": "./src/index.cjs"
47
+ "import": "./src/index.js"
52
48
  },
53
49
  "./package.json": "./package.json"
54
50
  }
package/src/index.js CHANGED
@@ -3,7 +3,22 @@
3
3
  import { Cache, generateCacheKey } from "@b9g/cache";
4
4
  import { createClient } from "redis";
5
5
  import { getLogger } from "@logtape/logtape";
6
- var logger = getLogger(["cache-redis"]);
6
+ var logger = getLogger(["platform"]);
7
+ function uint8ArrayToBase64(bytes) {
8
+ let binary = "";
9
+ for (let i = 0; i < bytes.length; i++) {
10
+ binary += String.fromCharCode(bytes[i]);
11
+ }
12
+ return btoa(binary);
13
+ }
14
+ function base64ToUint8Array(base64) {
15
+ const binary = atob(base64);
16
+ const bytes = new Uint8Array(binary.length);
17
+ for (let i = 0; i < binary.length; i++) {
18
+ bytes[i] = binary.charCodeAt(i);
19
+ }
20
+ return bytes;
21
+ }
7
22
  var RedisCache = class extends Cache {
8
23
  #client;
9
24
  #prefix;
@@ -63,7 +78,7 @@ var RedisCache = class extends Cache {
63
78
  status: response.status,
64
79
  statusText: response.statusText,
65
80
  headers,
66
- body: btoa(String.fromCharCode(...new Uint8Array(body))),
81
+ body: uint8ArrayToBase64(new Uint8Array(body)),
67
82
  cachedAt: Date.now(),
68
83
  TTL: this.#defaultTTL
69
84
  };
@@ -72,7 +87,7 @@ var RedisCache = class extends Cache {
72
87
  * Deserialize cache entry to Response
73
88
  */
74
89
  #deserializeResponse(entry) {
75
- const body = Uint8Array.from(atob(entry.body), (c) => c.charCodeAt(0));
90
+ const body = base64ToUint8Array(entry.body);
76
91
  return new Response(body, {
77
92
  status: entry.status,
78
93
  statusText: entry.statusText,
@@ -164,7 +179,9 @@ var RedisCache = class extends Cache {
164
179
  if (method && url) {
165
180
  requests.push(new Request(url, { method }));
166
181
  }
167
- } catch {
182
+ } catch (err) {
183
+ if (!(err instanceof TypeError))
184
+ throw err;
168
185
  }
169
186
  }
170
187
  return requests;
@@ -192,7 +209,8 @@ var RedisCache = class extends Cache {
192
209
  if (value) {
193
210
  totalSize += new TextEncoder().encode(value).length;
194
211
  }
195
- } catch {
212
+ } catch (err) {
213
+ logger.debug("Error reading key {key}: {error}", { key, error: err });
196
214
  }
197
215
  }
198
216
  return {
@@ -229,7 +247,9 @@ var RedisCache = class extends Cache {
229
247
  try {
230
248
  await this.#client.disconnect();
231
249
  } catch (disconnectError) {
232
- logger.error("Error forcing Redis disconnect: {error}", { error: disconnectError });
250
+ logger.error("Error forcing Redis disconnect: {error}", {
251
+ error: disconnectError
252
+ });
233
253
  }
234
254
  }
235
255
  }