@ogygia/postrender 0.12.0 → 0.13.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/README.md +5 -2
- package/dist/adapters/akamai.d.ts +11 -2
- package/dist/adapters/akamai.js +36 -26
- package/dist/adapters/cloudflare-kv.d.ts +12 -2
- package/dist/adapters/cloudflare-kv.js +47 -42
- package/dist/adapters/cloudflare.d.ts +11 -2
- package/dist/adapters/cloudflare.js +38 -27
- package/dist/adapters/cloudfront.d.ts +11 -2
- package/dist/adapters/cloudfront.js +30 -21
- package/dist/adapters/upstash.d.ts +12 -2
- package/dist/adapters/upstash.js +76 -68
- package/dist/adapters/valkey.d.ts +12 -2
- package/dist/adapters/valkey.js +65 -51
- package/dist/handle.js +2 -1
- package/dist/index.d.ts +8 -8
- package/dist/index.js +10 -10
- package/dist/key.d.ts +3 -0
- package/dist/key.js +6 -1
- package/dist/memory-store.d.ts +13 -2
- package/dist/memory-store.js +39 -38
- package/dist/messages.d.ts +72 -0
- package/dist/messages.js +146 -0
- package/dist/registry.d.ts +52 -5
- package/dist/registry.js +190 -37
- package/dist/server.d.ts +4 -3
- package/dist/server.js +104 -51
- package/package.json +13 -5
package/README.md
CHANGED
|
@@ -22,9 +22,12 @@ export default defineConfig({
|
|
|
22
22
|
// src/hooks.server.ts — postrender's handle goes BEFORE ogygia's
|
|
23
23
|
import { sequence } from '@sveltejs/kit/hooks';
|
|
24
24
|
import * as ogygia from 'ogygia/server';
|
|
25
|
-
import { postrender,
|
|
25
|
+
import { postrender, ValkeyStore } from '@ogygia/postrender';
|
|
26
26
|
|
|
27
|
-
export const handle = sequence(
|
|
27
|
+
export const handle = sequence(
|
|
28
|
+
postrender.handle({ store: new ValkeyStore(client) }),
|
|
29
|
+
ogygia.handle()
|
|
30
|
+
);
|
|
28
31
|
```
|
|
29
32
|
|
|
30
33
|
With no `store`, pages are kept in an in-process LRU.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EdgeAdapter } from "../types.js";
|
|
1
|
+
import { EdgeAdapter, PostrenderMeta } from "../types.js";
|
|
2
2
|
export interface AkamaiConfig {
|
|
3
3
|
/** The EdgeGrid host, e.g. `akab-xxxx.purge.akamaiapis.net`. */
|
|
4
4
|
host: string;
|
|
@@ -14,4 +14,13 @@ export interface AkamaiConfig {
|
|
|
14
14
|
/** The s-maxage this edge is granted (defaults to the policy TTL in `headers(meta)`). */
|
|
15
15
|
maxAge?: number;
|
|
16
16
|
}
|
|
17
|
-
export declare
|
|
17
|
+
export declare class AkamaiEdge implements EdgeAdapter {
|
|
18
|
+
#private;
|
|
19
|
+
readonly name: string;
|
|
20
|
+
constructor(config: AkamaiConfig);
|
|
21
|
+
headers(meta: PostrenderMeta): Record<string, string>;
|
|
22
|
+
purgeUrl(url: string): Promise<void>;
|
|
23
|
+
purgeWhere({ prefix }: {
|
|
24
|
+
prefix: string;
|
|
25
|
+
}): Promise<void>;
|
|
26
|
+
}
|
package/dist/adapters/akamai.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { normalize_prefix, prefix_tags } from "../key.js";
|
|
2
|
+
import { OgygiaError } from "../messages.js";
|
|
2
3
|
import { createHash, createHmac, randomUUID } from "node:crypto";
|
|
3
4
|
// src/adapters/akamai.ts
|
|
4
5
|
/**
|
|
@@ -20,16 +21,38 @@ function edgegrid_timestamp() {
|
|
|
20
21
|
}
|
|
21
22
|
const b64_hmac = (key, data) => createHmac("sha256", key).update(data).digest("base64");
|
|
22
23
|
const b64_sha = (data) => createHash("sha256").update(data).digest("base64");
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
var AkamaiEdge = class {
|
|
25
|
+
name = "akamai";
|
|
26
|
+
#config;
|
|
27
|
+
#network;
|
|
28
|
+
#api_base;
|
|
29
|
+
constructor(config) {
|
|
30
|
+
this.#config = config;
|
|
31
|
+
this.#network = config.network ?? "production";
|
|
32
|
+
this.#api_base = (config.baseUrl ?? `https://${config.host}`).replace(TRAILING_SLASHES_RE, "");
|
|
33
|
+
}
|
|
34
|
+
headers(meta) {
|
|
35
|
+
const tags = prefix_tags(meta.url).map((p) => `p:${p}`);
|
|
36
|
+
const out = {};
|
|
37
|
+
if (tags.length) out["edge-cache-tag"] = tags.join(", ");
|
|
38
|
+
const max_age = this.#config.maxAge ?? meta.ttl;
|
|
39
|
+
if (max_age > 0) out["cache-control"] = `public, s-maxage=${max_age}`;
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
async purgeUrl(url) {
|
|
43
|
+
await this.#signed_post(`/ccu/v3/invalidate/url/${this.#network}`, { objects: [this.#config.site.replace(TRAILING_SLASHES_RE, "") + url] });
|
|
44
|
+
}
|
|
45
|
+
async purgeWhere({ prefix }) {
|
|
46
|
+
await this.#signed_post(`/ccu/v3/invalidate/tag/${this.#network}`, { objects: [`p:${normalize_prefix(prefix)}`] });
|
|
47
|
+
}
|
|
26
48
|
/** Real EdgeGrid signing (EG1-HMAC-SHA256) over the canonical request. */
|
|
27
|
-
|
|
49
|
+
async #signed_post(path, body) {
|
|
50
|
+
const config = this.#config;
|
|
28
51
|
const payload = JSON.stringify(body);
|
|
29
52
|
const timestamp = edgegrid_timestamp();
|
|
30
53
|
const nonce = randomUUID();
|
|
31
54
|
const auth_prefix = `EG1-HMAC-SHA256 client_token=${config.clientToken};access_token=${config.accessToken};timestamp=${timestamp};nonce=${nonce};`;
|
|
32
|
-
const url = new URL(api_base + path);
|
|
55
|
+
const url = new URL(this.#api_base + path);
|
|
33
56
|
const data_to_sign = [
|
|
34
57
|
"POST",
|
|
35
58
|
url.protocol.replace(":", ""),
|
|
@@ -49,24 +72,11 @@ function akamai(config) {
|
|
|
49
72
|
},
|
|
50
73
|
body: payload
|
|
51
74
|
});
|
|
52
|
-
if (!res.ok) throw new
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const max_age = config.maxAge ?? meta.ttl;
|
|
61
|
-
if (max_age > 0) out["cache-control"] = `public, s-maxage=${max_age}`;
|
|
62
|
-
return out;
|
|
63
|
-
},
|
|
64
|
-
async purgeUrl(url) {
|
|
65
|
-
await signed_post(`/ccu/v3/invalidate/url/${network}`, { objects: [config.site.replace(TRAILING_SLASHES_RE, "") + url] });
|
|
66
|
-
},
|
|
67
|
-
async purgeWhere({ prefix }) {
|
|
68
|
-
await signed_post(`/ccu/v3/invalidate/tag/${network}`, { objects: [`p:${normalize_prefix(prefix)}`] });
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
export { akamai };
|
|
75
|
+
if (!res.ok) throw new OgygiaError("postrender_adapter_request_failed", {
|
|
76
|
+
what: "akamai purge",
|
|
77
|
+
status: res.status,
|
|
78
|
+
body: await res.text().catch(() => "")
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
export { AkamaiEdge };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PostrenderStore } from "../types.js";
|
|
1
|
+
import { PostrenderEntry, PostrenderPutOptions, PostrenderStore } from "../types.js";
|
|
2
2
|
/** The minimal Workers KV binding surface (KVNamespace, structurally). */
|
|
3
3
|
export interface KvNamespaceLike {
|
|
4
4
|
get(key: string, type: "text"): Promise<string | null>;
|
|
@@ -17,4 +17,14 @@ export interface KvNamespaceLike {
|
|
|
17
17
|
cursor?: string;
|
|
18
18
|
}>;
|
|
19
19
|
}
|
|
20
|
-
export declare
|
|
20
|
+
export declare class CloudflareKvStore implements PostrenderStore {
|
|
21
|
+
#private;
|
|
22
|
+
constructor(kv: KvNamespaceLike);
|
|
23
|
+
get(key: string): Promise<PostrenderEntry | null>;
|
|
24
|
+
put(key: string, entry: PostrenderEntry, { ttl, tags }: PostrenderPutOptions): Promise<void>;
|
|
25
|
+
evict(key: string): Promise<void>;
|
|
26
|
+
evictWhere({ prefix }: {
|
|
27
|
+
prefix: string;
|
|
28
|
+
}): Promise<void>;
|
|
29
|
+
evictByTag(tag: string): Promise<string[]>;
|
|
30
|
+
}
|
|
@@ -3,12 +3,53 @@ import { normalize_prefix, subtree_prefix } from "../key.js";
|
|
|
3
3
|
const KEY_NS = "og:a:";
|
|
4
4
|
const TAG_NS = "og:t:";
|
|
5
5
|
const KV_MIN_TTL = 60;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
var CloudflareKvStore = class {
|
|
7
|
+
#kv;
|
|
8
|
+
constructor(kv) {
|
|
9
|
+
this.#kv = kv;
|
|
10
|
+
}
|
|
11
|
+
async get(key) {
|
|
12
|
+
const raw = await this.#kv.get(KEY_NS + key, "text");
|
|
13
|
+
if (!raw) return null;
|
|
14
|
+
try {
|
|
15
|
+
return JSON.parse(raw);
|
|
16
|
+
} catch {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async put(key, entry, { ttl, tags }) {
|
|
21
|
+
const kv = this.#kv;
|
|
22
|
+
const expirationTtl = Math.max(KV_MIN_TTL, ttl);
|
|
23
|
+
await kv.put(KEY_NS + key, JSON.stringify(entry), { expirationTtl });
|
|
24
|
+
for (const tag of tags ?? []) await kv.put(`${TAG_NS}${tag}:${key}`, "1", { expirationTtl });
|
|
25
|
+
}
|
|
26
|
+
async evict(key) {
|
|
27
|
+
await this.#kv.delete(KEY_NS + key);
|
|
28
|
+
}
|
|
29
|
+
async evictWhere({ prefix }) {
|
|
30
|
+
const kv = this.#kv;
|
|
31
|
+
const p = normalize_prefix(prefix);
|
|
32
|
+
for (const name of await this.#list_prefix(KEY_NS + subtree_prefix(p))) await kv.delete(name);
|
|
33
|
+
await kv.delete(KEY_NS + p);
|
|
34
|
+
}
|
|
35
|
+
async evictByTag(tag) {
|
|
36
|
+
const kv = this.#kv;
|
|
37
|
+
const sentinel_prefix = `${TAG_NS}${tag}:`;
|
|
38
|
+
const sentinels = await this.#list_prefix(sentinel_prefix);
|
|
39
|
+
const keys = [];
|
|
40
|
+
for (const name of sentinels) {
|
|
41
|
+
const key = name.slice(sentinel_prefix.length);
|
|
42
|
+
keys.push(key);
|
|
43
|
+
await kv.delete(KEY_NS + key);
|
|
44
|
+
await kv.delete(name);
|
|
45
|
+
}
|
|
46
|
+
return keys;
|
|
47
|
+
}
|
|
48
|
+
async #list_prefix(prefix) {
|
|
8
49
|
const names = [];
|
|
9
50
|
let cursor;
|
|
10
51
|
do {
|
|
11
|
-
const page = await kv.list({
|
|
52
|
+
const page = await this.#kv.list({
|
|
12
53
|
prefix,
|
|
13
54
|
cursor
|
|
14
55
|
});
|
|
@@ -16,42 +57,6 @@ function cloudflareKv(kv) {
|
|
|
16
57
|
cursor = page.list_complete ? void 0 : page.cursor;
|
|
17
58
|
} while (cursor);
|
|
18
59
|
return names;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const raw = await kv.get(KEY_NS + key, "text");
|
|
23
|
-
if (!raw) return null;
|
|
24
|
-
try {
|
|
25
|
-
return JSON.parse(raw);
|
|
26
|
-
} catch {
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
async put(key, entry, { ttl, tags }) {
|
|
31
|
-
const expirationTtl = Math.max(KV_MIN_TTL, ttl);
|
|
32
|
-
await kv.put(KEY_NS + key, JSON.stringify(entry), { expirationTtl });
|
|
33
|
-
for (const tag of tags ?? []) await kv.put(`${TAG_NS}${tag}:${key}`, "1", { expirationTtl });
|
|
34
|
-
},
|
|
35
|
-
async evict(key) {
|
|
36
|
-
await kv.delete(KEY_NS + key);
|
|
37
|
-
},
|
|
38
|
-
async evictWhere({ prefix }) {
|
|
39
|
-
const p = normalize_prefix(prefix);
|
|
40
|
-
for (const name of await list_prefix(KEY_NS + subtree_prefix(p))) await kv.delete(name);
|
|
41
|
-
await kv.delete(KEY_NS + p);
|
|
42
|
-
},
|
|
43
|
-
async evictByTag(tag) {
|
|
44
|
-
const sentinel_prefix = `${TAG_NS}${tag}:`;
|
|
45
|
-
const sentinels = await list_prefix(sentinel_prefix);
|
|
46
|
-
const keys = [];
|
|
47
|
-
for (const name of sentinels) {
|
|
48
|
-
const key = name.slice(sentinel_prefix.length);
|
|
49
|
-
keys.push(key);
|
|
50
|
-
await kv.delete(KEY_NS + key);
|
|
51
|
-
await kv.delete(name);
|
|
52
|
-
}
|
|
53
|
-
return keys;
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
export { cloudflareKv };
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
export { CloudflareKvStore };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EdgeAdapter } from "../types.js";
|
|
1
|
+
import { EdgeAdapter, PostrenderMeta } from "../types.js";
|
|
2
2
|
export interface CloudflareConfig {
|
|
3
3
|
zoneId: string;
|
|
4
4
|
/** An API token with `Zone.Cache Purge` permission (`authorization: Bearer <token>`). */
|
|
@@ -10,4 +10,13 @@ export interface CloudflareConfig {
|
|
|
10
10
|
/** The s-maxage this edge is granted (defaults to the policy TTL in `headers(meta)`). */
|
|
11
11
|
maxAge?: number;
|
|
12
12
|
}
|
|
13
|
-
export declare
|
|
13
|
+
export declare class CloudflareEdge implements EdgeAdapter {
|
|
14
|
+
#private;
|
|
15
|
+
readonly name: string;
|
|
16
|
+
constructor(config: CloudflareConfig);
|
|
17
|
+
headers(meta: PostrenderMeta): Record<string, string>;
|
|
18
|
+
purgeUrl(url: string): Promise<void>;
|
|
19
|
+
purgeWhere({ prefix }: {
|
|
20
|
+
prefix: string;
|
|
21
|
+
}): Promise<void>;
|
|
22
|
+
}
|
|
@@ -1,13 +1,37 @@
|
|
|
1
1
|
import { normalize_prefix, prefix_tags } from "../key.js";
|
|
2
|
+
import { OgygiaError } from "../messages.js";
|
|
2
3
|
// src/adapters/cloudflare.ts
|
|
3
4
|
const TRAILING_SLASHES_RE = /\/+$/;
|
|
4
5
|
const HTTP_SCHEME_RE = /^https?:\/\//;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
var CloudflareEdge = class {
|
|
7
|
+
name = "cloudflare";
|
|
8
|
+
#config;
|
|
9
|
+
#api_base;
|
|
10
|
+
#site;
|
|
11
|
+
#site_host;
|
|
12
|
+
constructor(config) {
|
|
13
|
+
this.#config = config;
|
|
14
|
+
this.#api_base = (config.baseUrl ?? "https://api.cloudflare.com").replace(TRAILING_SLASHES_RE, "");
|
|
15
|
+
this.#site = config.site.replace(TRAILING_SLASHES_RE, "");
|
|
16
|
+
this.#site_host = this.#site.replace(HTTP_SCHEME_RE, "");
|
|
17
|
+
}
|
|
18
|
+
headers(meta) {
|
|
19
|
+
const tags = prefix_tags(meta.url).map((p) => `p:${p}`);
|
|
20
|
+
const out = {};
|
|
21
|
+
if (tags.length) out["cache-tag"] = tags.join(",");
|
|
22
|
+
const max_age = this.#config.maxAge ?? meta.ttl;
|
|
23
|
+
if (max_age > 0) out["cache-control"] = `public, s-maxage=${max_age}`;
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
async purgeUrl(url) {
|
|
27
|
+
await this.#purge({ files: [this.#site + url] });
|
|
28
|
+
}
|
|
29
|
+
async purgeWhere({ prefix }) {
|
|
30
|
+
await this.#purge({ prefixes: [this.#site_host + normalize_prefix(prefix)] });
|
|
31
|
+
}
|
|
32
|
+
async #purge(body) {
|
|
33
|
+
const config = this.#config;
|
|
34
|
+
const res = await fetch(`${this.#api_base}/client/v4/zones/${config.zoneId}/purge_cache`, {
|
|
11
35
|
method: "POST",
|
|
12
36
|
headers: {
|
|
13
37
|
authorization: `Bearer ${config.apiToken}`,
|
|
@@ -15,24 +39,11 @@ function cloudflare(config) {
|
|
|
15
39
|
},
|
|
16
40
|
body: JSON.stringify(body)
|
|
17
41
|
});
|
|
18
|
-
if (!res.ok) throw new
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const max_age = config.maxAge ?? meta.ttl;
|
|
27
|
-
if (max_age > 0) out["cache-control"] = `public, s-maxage=${max_age}`;
|
|
28
|
-
return out;
|
|
29
|
-
},
|
|
30
|
-
async purgeUrl(url) {
|
|
31
|
-
await purge({ files: [site + url] });
|
|
32
|
-
},
|
|
33
|
-
async purgeWhere({ prefix }) {
|
|
34
|
-
await purge({ prefixes: [site_host + normalize_prefix(prefix)] });
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
export { cloudflare };
|
|
42
|
+
if (!res.ok) throw new OgygiaError("postrender_adapter_request_failed", {
|
|
43
|
+
what: "cloudflare purge",
|
|
44
|
+
status: res.status,
|
|
45
|
+
body: await res.text().catch(() => "")
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
export { CloudflareEdge };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EdgeAdapter } from "../types.js";
|
|
1
|
+
import { EdgeAdapter, PostrenderMeta } from "../types.js";
|
|
2
2
|
export interface CloudfrontConfig {
|
|
3
3
|
distributionId: string;
|
|
4
4
|
accessKeyId: string;
|
|
@@ -6,4 +6,13 @@ export interface CloudfrontConfig {
|
|
|
6
6
|
/** Harness override: send API calls here instead of the real AWS endpoint. */
|
|
7
7
|
baseUrl?: string;
|
|
8
8
|
}
|
|
9
|
-
export declare
|
|
9
|
+
export declare class CloudFrontEdge implements EdgeAdapter {
|
|
10
|
+
#private;
|
|
11
|
+
readonly name: string;
|
|
12
|
+
constructor(config: CloudfrontConfig);
|
|
13
|
+
headers(_meta?: PostrenderMeta): Record<string, string>;
|
|
14
|
+
purgeUrl(url: string): Promise<void>;
|
|
15
|
+
purgeWhere({ prefix }: {
|
|
16
|
+
prefix: string;
|
|
17
|
+
}): Promise<void>;
|
|
18
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { normalize_prefix } from "../key.js";
|
|
2
|
+
import { OgygiaError } from "../messages.js";
|
|
2
3
|
import { createHash, createHmac } from "node:crypto";
|
|
3
4
|
// src/adapters/cloudfront.ts
|
|
4
5
|
/**
|
|
@@ -28,9 +29,26 @@ function amz_dates() {
|
|
|
28
29
|
date: iso.slice(0, 8)
|
|
29
30
|
};
|
|
30
31
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
var CloudFrontEdge = class {
|
|
33
|
+
name = "cloudfront";
|
|
34
|
+
#config;
|
|
35
|
+
#api_base;
|
|
36
|
+
constructor(config) {
|
|
37
|
+
this.#config = config;
|
|
38
|
+
this.#api_base = (config.baseUrl ?? `https://${API_HOST}`).replace(TRAILING_SLASHES_RE, "");
|
|
39
|
+
}
|
|
40
|
+
headers(_meta) {
|
|
41
|
+
return {};
|
|
42
|
+
}
|
|
43
|
+
async purgeUrl(url) {
|
|
44
|
+
await this.#create_invalidation([url]);
|
|
45
|
+
}
|
|
46
|
+
async purgeWhere({ prefix }) {
|
|
47
|
+
const p = normalize_prefix(prefix);
|
|
48
|
+
await this.#create_invalidation([p, `${p}/*`]);
|
|
49
|
+
}
|
|
50
|
+
async #create_invalidation(paths) {
|
|
51
|
+
const config = this.#config;
|
|
34
52
|
const items = paths.map((p) => `<Path>${p}</Path>`).join("");
|
|
35
53
|
const body = `<?xml version="1.0" encoding="UTF-8"?><InvalidationBatch xmlns="http://cloudfront.amazonaws.com/doc/${API_VERSION}/"><CallerReference>og-${Date.now()}-${Math.floor(Math.random() * 1e6)}</CallerReference><Paths><Quantity>${paths.length}</Quantity><Items>${items}</Items></Paths></InvalidationBatch>`;
|
|
36
54
|
const path = `/${API_VERSION}/distribution/${config.distributionId}/invalidation`;
|
|
@@ -59,7 +77,7 @@ function cloudfront(config) {
|
|
|
59
77
|
const k_service = hmac(k_region, SERVICE);
|
|
60
78
|
const k_signing = hmac(k_service, "aws4_request");
|
|
61
79
|
const signature = createHmac("sha256", k_signing).update(string_to_sign).digest("hex");
|
|
62
|
-
const res = await fetch(api_base + path, {
|
|
80
|
+
const res = await fetch(this.#api_base + path, {
|
|
63
81
|
method: "POST",
|
|
64
82
|
headers: {
|
|
65
83
|
host: API_HOST,
|
|
@@ -70,20 +88,11 @@ function cloudfront(config) {
|
|
|
70
88
|
},
|
|
71
89
|
body
|
|
72
90
|
});
|
|
73
|
-
if (!res.ok) throw new
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
await create_invalidation([url]);
|
|
82
|
-
},
|
|
83
|
-
async purgeWhere({ prefix }) {
|
|
84
|
-
const p = normalize_prefix(prefix);
|
|
85
|
-
await create_invalidation([p, `${p}/*`]);
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
export { cloudfront };
|
|
91
|
+
if (!res.ok) throw new OgygiaError("postrender_adapter_request_failed", {
|
|
92
|
+
what: "cloudfront invalidation",
|
|
93
|
+
status: res.status,
|
|
94
|
+
body: await res.text().catch(() => "")
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
export { CloudFrontEdge };
|
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
import { PostrenderStore } from "../types.js";
|
|
1
|
+
import { PostrenderEntry, PostrenderPutOptions, PostrenderStore } from "../types.js";
|
|
2
2
|
export interface UpstashConfig {
|
|
3
3
|
/** The REST endpoint, e.g. `https://usw1-xxxx.upstash.io`. */
|
|
4
4
|
url: string;
|
|
5
5
|
token: string;
|
|
6
6
|
}
|
|
7
|
-
export declare
|
|
7
|
+
export declare class UpstashStore implements PostrenderStore {
|
|
8
|
+
#private;
|
|
9
|
+
constructor(config: UpstashConfig);
|
|
10
|
+
get(key: string): Promise<PostrenderEntry | null>;
|
|
11
|
+
put(key: string, entry: PostrenderEntry, { ttl, tags }: PostrenderPutOptions): Promise<void>;
|
|
12
|
+
evict(key: string): Promise<void>;
|
|
13
|
+
evictWhere({ prefix }: {
|
|
14
|
+
prefix: string;
|
|
15
|
+
}): Promise<void>;
|
|
16
|
+
evictByTag(tag: string): Promise<string[]>;
|
|
17
|
+
}
|
package/dist/adapters/upstash.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { normalize_prefix, subtree_prefix } from "../key.js";
|
|
2
|
+
import { OgygiaError } from "../messages.js";
|
|
2
3
|
// src/adapters/upstash.ts
|
|
3
4
|
const GLOB_ACTIVE_G = /[*?[\]\\]/g;
|
|
4
5
|
const TRAILING_SLASHES_RE = /\/+$/;
|
|
@@ -9,79 +10,86 @@ const SCAN_COUNT = 250;
|
|
|
9
10
|
function glob_escape(value) {
|
|
10
11
|
return value.replace(GLOB_ACTIVE_G, (c) => "\\" + c);
|
|
11
12
|
}
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
var UpstashStore = class {
|
|
14
|
+
#base;
|
|
15
|
+
#token;
|
|
16
|
+
constructor(config) {
|
|
17
|
+
this.#base = config.url.replace(TRAILING_SLASHES_RE, "");
|
|
18
|
+
this.#token = config.token;
|
|
19
|
+
}
|
|
20
|
+
async get(key) {
|
|
21
|
+
const raw = await this.#command(["GET", KEY_NS + key]);
|
|
22
|
+
if (!raw) return null;
|
|
23
|
+
try {
|
|
24
|
+
return JSON.parse(raw);
|
|
25
|
+
} catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async put(key, entry, { ttl, tags }) {
|
|
30
|
+
await this.#command([
|
|
31
|
+
"SET",
|
|
32
|
+
KEY_NS + key,
|
|
33
|
+
JSON.stringify(entry),
|
|
34
|
+
"EX",
|
|
35
|
+
ttl
|
|
36
|
+
]);
|
|
37
|
+
for (const tag of tags ?? []) {
|
|
38
|
+
await this.#command([
|
|
39
|
+
"SADD",
|
|
40
|
+
TAG_NS + tag,
|
|
41
|
+
key
|
|
42
|
+
]);
|
|
43
|
+
if (await this.#command(["TTL", TAG_NS + tag]) < ttl) await this.#command([
|
|
44
|
+
"EXPIRE",
|
|
45
|
+
TAG_NS + tag,
|
|
46
|
+
ttl
|
|
47
|
+
]);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async evict(key) {
|
|
51
|
+
await this.#command(["DEL", KEY_NS + key]);
|
|
52
|
+
}
|
|
53
|
+
async evictWhere({ prefix }) {
|
|
54
|
+
const p = normalize_prefix(prefix);
|
|
55
|
+
const pattern = glob_escape(KEY_NS + subtree_prefix(p)) + "*";
|
|
56
|
+
let cursor = "0";
|
|
57
|
+
do {
|
|
58
|
+
const reply = await this.#command([
|
|
59
|
+
"SCAN",
|
|
60
|
+
cursor,
|
|
61
|
+
"MATCH",
|
|
62
|
+
pattern,
|
|
63
|
+
"COUNT",
|
|
64
|
+
SCAN_COUNT
|
|
65
|
+
]);
|
|
66
|
+
cursor = String(reply[0]);
|
|
67
|
+
if (reply[1]?.length) await this.#command(["DEL", ...reply[1]]);
|
|
68
|
+
} while (cursor !== "0");
|
|
69
|
+
await this.#command(["DEL", KEY_NS + p]);
|
|
70
|
+
}
|
|
71
|
+
async evictByTag(tag) {
|
|
72
|
+
const members = await this.#command(["SMEMBERS", TAG_NS + tag]);
|
|
73
|
+
if (members.length) await this.#command(["DEL", ...members.map((m) => KEY_NS + m)]);
|
|
74
|
+
await this.#command(["DEL", TAG_NS + tag]);
|
|
75
|
+
return members;
|
|
76
|
+
}
|
|
14
77
|
/** One command, Upstash REST shape: POST the command as a JSON array → `{ result }`. */
|
|
15
|
-
|
|
16
|
-
const res = await fetch(base, {
|
|
78
|
+
async #command(parts) {
|
|
79
|
+
const res = await fetch(this.#base, {
|
|
17
80
|
method: "POST",
|
|
18
81
|
headers: {
|
|
19
|
-
authorization: `Bearer ${
|
|
82
|
+
authorization: `Bearer ${this.#token}`,
|
|
20
83
|
"content-type": "application/json"
|
|
21
84
|
},
|
|
22
85
|
body: JSON.stringify(parts.map(String))
|
|
23
86
|
});
|
|
24
|
-
if (!res.ok) throw new
|
|
87
|
+
if (!res.ok) throw new OgygiaError("postrender_adapter_request_failed", {
|
|
88
|
+
what: "upstash command",
|
|
89
|
+
status: res.status,
|
|
90
|
+
body: await res.text().catch(() => "")
|
|
91
|
+
});
|
|
25
92
|
return (await res.json()).result;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const raw = await command(["GET", KEY_NS + key]);
|
|
30
|
-
if (!raw) return null;
|
|
31
|
-
try {
|
|
32
|
-
return JSON.parse(raw);
|
|
33
|
-
} catch {
|
|
34
|
-
return null;
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
async put(key, entry, { ttl, tags }) {
|
|
38
|
-
await command([
|
|
39
|
-
"SET",
|
|
40
|
-
KEY_NS + key,
|
|
41
|
-
JSON.stringify(entry),
|
|
42
|
-
"EX",
|
|
43
|
-
ttl
|
|
44
|
-
]);
|
|
45
|
-
for (const tag of tags ?? []) {
|
|
46
|
-
await command([
|
|
47
|
-
"SADD",
|
|
48
|
-
TAG_NS + tag,
|
|
49
|
-
key
|
|
50
|
-
]);
|
|
51
|
-
if (await command(["TTL", TAG_NS + tag]) < ttl) await command([
|
|
52
|
-
"EXPIRE",
|
|
53
|
-
TAG_NS + tag,
|
|
54
|
-
ttl
|
|
55
|
-
]);
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
async evict(key) {
|
|
59
|
-
await command(["DEL", KEY_NS + key]);
|
|
60
|
-
},
|
|
61
|
-
async evictWhere({ prefix }) {
|
|
62
|
-
const p = normalize_prefix(prefix);
|
|
63
|
-
const pattern = glob_escape(KEY_NS + subtree_prefix(p)) + "*";
|
|
64
|
-
let cursor = "0";
|
|
65
|
-
do {
|
|
66
|
-
const reply = await command([
|
|
67
|
-
"SCAN",
|
|
68
|
-
cursor,
|
|
69
|
-
"MATCH",
|
|
70
|
-
pattern,
|
|
71
|
-
"COUNT",
|
|
72
|
-
SCAN_COUNT
|
|
73
|
-
]);
|
|
74
|
-
cursor = String(reply[0]);
|
|
75
|
-
if (reply[1]?.length) await command(["DEL", ...reply[1]]);
|
|
76
|
-
} while (cursor !== "0");
|
|
77
|
-
await command(["DEL", KEY_NS + p]);
|
|
78
|
-
},
|
|
79
|
-
async evictByTag(tag) {
|
|
80
|
-
const members = await command(["SMEMBERS", TAG_NS + tag]);
|
|
81
|
-
if (members.length) await command(["DEL", ...members.map((m) => KEY_NS + m)]);
|
|
82
|
-
await command(["DEL", TAG_NS + tag]);
|
|
83
|
-
return members;
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
export { upstash };
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
export { UpstashStore };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PostrenderStore } from "../types.js";
|
|
1
|
+
import { PostrenderEntry, PostrenderPutOptions, PostrenderStore } from "../types.js";
|
|
2
2
|
/** The minimal client surface — ioredis (lowercase) and node-redis v4 (camelCase) both satisfy
|
|
3
3
|
* it structurally; the camelCase variants are probed at call time. */
|
|
4
4
|
export interface ValkeyLike {
|
|
@@ -15,4 +15,14 @@ export interface ValkeyLike {
|
|
|
15
15
|
ttl?(key: string): Promise<number>;
|
|
16
16
|
TTL?(key: string): Promise<number>;
|
|
17
17
|
}
|
|
18
|
-
export declare
|
|
18
|
+
export declare class ValkeyStore implements PostrenderStore {
|
|
19
|
+
#private;
|
|
20
|
+
constructor(client: ValkeyLike);
|
|
21
|
+
get(key: string): Promise<PostrenderEntry | null>;
|
|
22
|
+
put(key: string, entry: PostrenderEntry, { ttl, tags }: PostrenderPutOptions): Promise<void>;
|
|
23
|
+
evict(key: string): Promise<void>;
|
|
24
|
+
evictWhere({ prefix }: {
|
|
25
|
+
prefix: string;
|
|
26
|
+
}): Promise<void>;
|
|
27
|
+
evictByTag(tag: string): Promise<string[]>;
|
|
28
|
+
}
|