@ogygia/postrender 0.0.0-stage → 0.11.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/LICENSE +21 -0
- package/README.md +48 -2
- package/dist/adapters/akamai.d.ts +17 -0
- package/dist/adapters/akamai.js +72 -0
- package/dist/adapters/cloudflare-kv.d.ts +20 -0
- package/dist/adapters/cloudflare-kv.js +57 -0
- package/dist/adapters/cloudflare.d.ts +13 -0
- package/dist/adapters/cloudflare.js +38 -0
- package/dist/adapters/cloudfront.d.ts +9 -0
- package/dist/adapters/cloudfront.js +89 -0
- package/dist/adapters/upstash.d.ts +7 -0
- package/dist/adapters/upstash.js +87 -0
- package/dist/adapters/valkey.d.ts +18 -0
- package/dist/adapters/valkey.js +90 -0
- package/dist/app.d.ts +4 -0
- package/dist/app.js +9 -0
- package/dist/handle.d.ts +3 -0
- package/dist/handle.js +16 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +42 -0
- package/dist/key.d.ts +22 -0
- package/dist/key.js +37 -0
- package/dist/memory-store.d.ts +2 -0
- package/dist/memory-store.js +46 -0
- package/dist/observe.d.ts +17 -0
- package/dist/observe.js +77 -0
- package/dist/registry.d.ts +50 -0
- package/dist/registry.js +177 -0
- package/dist/server.d.ts +32 -0
- package/dist/server.js +334 -0
- package/dist/stitch.d.ts +61 -0
- package/dist/stitch.js +126 -0
- package/dist/types.d.ts +94 -0
- package/dist/types.js +0 -0
- package/dist/vite.d.ts +9 -0
- package/dist/vite.js +30 -0
- package/package.json +59 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Puru Vijayvargia
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,49 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ogygia/postrender
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Kit's prerender, at request time, for [ogygia](https://ogygiajs.dev). A page whose render read nothing personal is stored whole on its first request, and later visitors get the stored bytes until an invalidation clears them. Kit, your loads and Svelte never run on a hit. `render: 'deferred'` holes inside a stored page stay per visitor.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @ogygia/postrender
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
// vite.config.ts
|
|
13
|
+
import { ogygia } from 'ogygia/vite';
|
|
14
|
+
import { postrender } from '@ogygia/postrender/vite';
|
|
15
|
+
|
|
16
|
+
export default defineConfig({
|
|
17
|
+
plugins: [ogygia(), postrender({ ttl: 86400 }), sveltekit()]
|
|
18
|
+
});
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
// src/hooks.server.ts — postrender's handle goes BEFORE ogygia's
|
|
23
|
+
import { sequence } from '@sveltejs/kit/hooks';
|
|
24
|
+
import * as ogygia from 'ogygia/server';
|
|
25
|
+
import { postrender, valkey } from '@ogygia/postrender';
|
|
26
|
+
|
|
27
|
+
export const handle = sequence(postrender.handle({ store: valkey(client) }), ogygia.handle());
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
With no `store`, pages are kept in an in-process LRU.
|
|
31
|
+
|
|
32
|
+
## Use
|
|
33
|
+
|
|
34
|
+
A route opts out in Kit's own `config` (layouts merge into pages, the page's key wins):
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
// src/routes/account/+page.ts
|
|
38
|
+
export const config = { postrender: false };
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`postrender({ default: false })` makes it opt-in: a page or layout then sets `config = { postrender: true }`.
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
// a CMS webhook endpoint
|
|
45
|
+
await postrender.invalidate('/blog/hello/'); // one URL
|
|
46
|
+
await postrender.invalidateWhere({ prefix: '/blog/' }); // a subtree
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Docs: [Postrender](https://ogygiajs.dev/docs/data-state/postrender).
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { EdgeAdapter } from "../types.js";
|
|
2
|
+
export interface AkamaiConfig {
|
|
3
|
+
/** The EdgeGrid host, e.g. `akab-xxxx.purge.akamaiapis.net`. */
|
|
4
|
+
host: string;
|
|
5
|
+
clientToken: string;
|
|
6
|
+
clientSecret: string;
|
|
7
|
+
accessToken: string;
|
|
8
|
+
/** The public site origin purged URLs are absolute against, e.g. `https://www.example.com`. */
|
|
9
|
+
site: string;
|
|
10
|
+
/** Fast Purge network (default `production`). */
|
|
11
|
+
network?: "production" | "staging";
|
|
12
|
+
/** Harness override: send API calls here instead of `https://<host>` (the edge emulator). */
|
|
13
|
+
baseUrl?: string;
|
|
14
|
+
/** The s-maxage this edge is granted (defaults to the policy TTL in `headers(meta)`). */
|
|
15
|
+
maxAge?: number;
|
|
16
|
+
}
|
|
17
|
+
export declare function akamai(config: AkamaiConfig): EdgeAdapter;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { normalize_prefix, prefix_tags } from "../key.js";
|
|
2
|
+
import { createHash, createHmac, randomUUID } from "node:crypto";
|
|
3
|
+
// src/adapters/akamai.ts
|
|
4
|
+
/**
|
|
5
|
+
* postrender — Akamai edge adapter: Fast Purge v3 (url + tag) with real EdgeGrid request signing.
|
|
6
|
+
*
|
|
7
|
+
* `headers(meta)` stamps the depth-capped PREFIX tags (`edge-cache-tag: p:/fr, p:/fr/fr, …`) the
|
|
8
|
+
* property stores by; `purgeWhere({ prefix })` purges the matching tag — Fast Purge has no
|
|
9
|
+
* arbitrary-prefix purge, tags ARE the prefix mechanism. `purgeUrl` purges by absolute URL.
|
|
10
|
+
*
|
|
11
|
+
* `baseUrl` override points the SAME adapter at the harness's edge emulator — e2e exercises this
|
|
12
|
+
* exact signing/requesting code, not a mock of it.
|
|
13
|
+
*/
|
|
14
|
+
const TRAILING_SLASHES_RE = /\/+$/;
|
|
15
|
+
/** EdgeGrid timestamp: `yyyyMMddTHH:mm:ss+0000`. */
|
|
16
|
+
function edgegrid_timestamp() {
|
|
17
|
+
const d = /* @__PURE__ */ new Date();
|
|
18
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
19
|
+
return `${d.getUTCFullYear()}${pad(d.getUTCMonth() + 1)}${pad(d.getUTCDate())}T${pad(d.getUTCHours())}:${pad(d.getUTCMinutes())}:${pad(d.getUTCSeconds())}+0000`;
|
|
20
|
+
}
|
|
21
|
+
const b64_hmac = (key, data) => createHmac("sha256", key).update(data).digest("base64");
|
|
22
|
+
const b64_sha = (data) => createHash("sha256").update(data).digest("base64");
|
|
23
|
+
function akamai(config) {
|
|
24
|
+
const network = config.network ?? "production";
|
|
25
|
+
const api_base = (config.baseUrl ?? `https://${config.host}`).replace(TRAILING_SLASHES_RE, "");
|
|
26
|
+
/** Real EdgeGrid signing (EG1-HMAC-SHA256) over the canonical request. */
|
|
27
|
+
const signed_post = async (path, body) => {
|
|
28
|
+
const payload = JSON.stringify(body);
|
|
29
|
+
const timestamp = edgegrid_timestamp();
|
|
30
|
+
const nonce = randomUUID();
|
|
31
|
+
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);
|
|
33
|
+
const data_to_sign = [
|
|
34
|
+
"POST",
|
|
35
|
+
url.protocol.replace(":", ""),
|
|
36
|
+
config.host,
|
|
37
|
+
url.pathname + url.search,
|
|
38
|
+
"",
|
|
39
|
+
b64_sha(payload),
|
|
40
|
+
auth_prefix
|
|
41
|
+
].join(" ");
|
|
42
|
+
const signing_key = b64_hmac(config.clientSecret, timestamp);
|
|
43
|
+
const signature = b64_hmac(signing_key, data_to_sign);
|
|
44
|
+
const res = await fetch(url, {
|
|
45
|
+
method: "POST",
|
|
46
|
+
headers: {
|
|
47
|
+
"content-type": "application/json",
|
|
48
|
+
authorization: `${auth_prefix}signature=${signature}`
|
|
49
|
+
},
|
|
50
|
+
body: payload
|
|
51
|
+
});
|
|
52
|
+
if (!res.ok) throw new Error(`[ogygia] akamai purge failed: ${res.status} ${await res.text().catch(() => "")}`);
|
|
53
|
+
};
|
|
54
|
+
return {
|
|
55
|
+
name: "akamai",
|
|
56
|
+
headers(meta) {
|
|
57
|
+
const tags = prefix_tags(meta.url).map((p) => `p:${p}`);
|
|
58
|
+
const out = {};
|
|
59
|
+
if (tags.length) out["edge-cache-tag"] = tags.join(", ");
|
|
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 };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { PostrenderStore } from "../types.js";
|
|
2
|
+
/** The minimal Workers KV binding surface (KVNamespace, structurally). */
|
|
3
|
+
export interface KvNamespaceLike {
|
|
4
|
+
get(key: string, type: "text"): Promise<string | null>;
|
|
5
|
+
put(key: string, value: string, options?: {
|
|
6
|
+
expirationTtl?: number;
|
|
7
|
+
}): Promise<void>;
|
|
8
|
+
delete(key: string): Promise<void>;
|
|
9
|
+
list(options: {
|
|
10
|
+
prefix: string;
|
|
11
|
+
cursor?: string;
|
|
12
|
+
}): Promise<{
|
|
13
|
+
keys: {
|
|
14
|
+
name: string;
|
|
15
|
+
}[];
|
|
16
|
+
list_complete: boolean;
|
|
17
|
+
cursor?: string;
|
|
18
|
+
}>;
|
|
19
|
+
}
|
|
20
|
+
export declare function cloudflareKv(kv: KvNamespaceLike): PostrenderStore;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { normalize_prefix, subtree_prefix } from "../key.js";
|
|
2
|
+
// src/adapters/cloudflare-kv.ts
|
|
3
|
+
const KEY_NS = "og:a:";
|
|
4
|
+
const TAG_NS = "og:t:";
|
|
5
|
+
const KV_MIN_TTL = 60;
|
|
6
|
+
function cloudflareKv(kv) {
|
|
7
|
+
const list_prefix = async (prefix) => {
|
|
8
|
+
const names = [];
|
|
9
|
+
let cursor;
|
|
10
|
+
do {
|
|
11
|
+
const page = await kv.list({
|
|
12
|
+
prefix,
|
|
13
|
+
cursor
|
|
14
|
+
});
|
|
15
|
+
for (const k of page.keys) names.push(k.name);
|
|
16
|
+
cursor = page.list_complete ? void 0 : page.cursor;
|
|
17
|
+
} while (cursor);
|
|
18
|
+
return names;
|
|
19
|
+
};
|
|
20
|
+
return {
|
|
21
|
+
async get(key) {
|
|
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 };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EdgeAdapter } from "../types.js";
|
|
2
|
+
export interface CloudflareConfig {
|
|
3
|
+
zoneId: string;
|
|
4
|
+
/** An API token with `Zone.Cache Purge` permission (`authorization: Bearer <token>`). */
|
|
5
|
+
apiToken: string;
|
|
6
|
+
/** The public site origin purged URLs are absolute against, e.g. `https://www.example.com`. */
|
|
7
|
+
site: string;
|
|
8
|
+
/** Harness override: send API calls here instead of `https://api.cloudflare.com`. */
|
|
9
|
+
baseUrl?: string;
|
|
10
|
+
/** The s-maxage this edge is granted (defaults to the policy TTL in `headers(meta)`). */
|
|
11
|
+
maxAge?: number;
|
|
12
|
+
}
|
|
13
|
+
export declare function cloudflare(config: CloudflareConfig): EdgeAdapter;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { normalize_prefix, prefix_tags } from "../key.js";
|
|
2
|
+
// src/adapters/cloudflare.ts
|
|
3
|
+
const TRAILING_SLASHES_RE = /\/+$/;
|
|
4
|
+
const HTTP_SCHEME_RE = /^https?:\/\//;
|
|
5
|
+
function cloudflare(config) {
|
|
6
|
+
const api_base = (config.baseUrl ?? "https://api.cloudflare.com").replace(TRAILING_SLASHES_RE, "");
|
|
7
|
+
const site = config.site.replace(TRAILING_SLASHES_RE, "");
|
|
8
|
+
const site_host = site.replace(HTTP_SCHEME_RE, "");
|
|
9
|
+
const purge = async (body) => {
|
|
10
|
+
const res = await fetch(`${api_base}/client/v4/zones/${config.zoneId}/purge_cache`, {
|
|
11
|
+
method: "POST",
|
|
12
|
+
headers: {
|
|
13
|
+
authorization: `Bearer ${config.apiToken}`,
|
|
14
|
+
"content-type": "application/json"
|
|
15
|
+
},
|
|
16
|
+
body: JSON.stringify(body)
|
|
17
|
+
});
|
|
18
|
+
if (!res.ok) throw new Error(`[ogygia] cloudflare purge failed: ${res.status} ${await res.text().catch(() => "")}`);
|
|
19
|
+
};
|
|
20
|
+
return {
|
|
21
|
+
name: "cloudflare",
|
|
22
|
+
headers(meta) {
|
|
23
|
+
const tags = prefix_tags(meta.url).map((p) => `p:${p}`);
|
|
24
|
+
const out = {};
|
|
25
|
+
if (tags.length) out["cache-tag"] = tags.join(",");
|
|
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 };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EdgeAdapter } from "../types.js";
|
|
2
|
+
export interface CloudfrontConfig {
|
|
3
|
+
distributionId: string;
|
|
4
|
+
accessKeyId: string;
|
|
5
|
+
secretAccessKey: string;
|
|
6
|
+
/** Harness override: send API calls here instead of the real AWS endpoint. */
|
|
7
|
+
baseUrl?: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function cloudfront(config: CloudfrontConfig): EdgeAdapter;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { normalize_prefix } from "../key.js";
|
|
2
|
+
import { createHash, createHmac } from "node:crypto";
|
|
3
|
+
// src/adapters/cloudfront.ts
|
|
4
|
+
/**
|
|
5
|
+
* postrender — CloudFront edge adapter: CreateInvalidation with real SigV4 request signing.
|
|
6
|
+
*
|
|
7
|
+
* CloudFront has NO tag purge — path invalidation with wildcards is native, so
|
|
8
|
+
* `purgeWhere({ prefix })` becomes `<prefix>/*` (+ the bare prefix path). `headers()` adds
|
|
9
|
+
* nothing: the distribution honors the origin's `cache-control` (stamped by the verdict).
|
|
10
|
+
*
|
|
11
|
+
* `baseUrl` override points the SAME adapter at the harness's edge emulator — e2e exercises
|
|
12
|
+
* this exact signing/requesting code, not a mock of it.
|
|
13
|
+
*/
|
|
14
|
+
const DASH_COLON_G = /[-:]/g;
|
|
15
|
+
const MILLISECONDS_RE = /\.\d{3}/;
|
|
16
|
+
const TRAILING_SLASHES_RE = /\/+$/;
|
|
17
|
+
const SERVICE = "cloudfront";
|
|
18
|
+
const REGION = "us-east-1";
|
|
19
|
+
const API_HOST = "cloudfront.amazonaws.com";
|
|
20
|
+
const API_VERSION = "2020-05-31";
|
|
21
|
+
const hex_sha = (data) => createHash("sha256").update(data).digest("hex");
|
|
22
|
+
const hmac = (key, data) => createHmac("sha256", key).update(data).digest();
|
|
23
|
+
/** amz dates: `20260901T120000Z` / `20260901`. */
|
|
24
|
+
function amz_dates() {
|
|
25
|
+
const iso = (/* @__PURE__ */ new Date()).toISOString().replace(DASH_COLON_G, "").replace(MILLISECONDS_RE, "");
|
|
26
|
+
return {
|
|
27
|
+
datetime: iso,
|
|
28
|
+
date: iso.slice(0, 8)
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function cloudfront(config) {
|
|
32
|
+
const api_base = (config.baseUrl ?? `https://${API_HOST}`).replace(TRAILING_SLASHES_RE, "");
|
|
33
|
+
const create_invalidation = async (paths) => {
|
|
34
|
+
const items = paths.map((p) => `<Path>${p}</Path>`).join("");
|
|
35
|
+
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
|
+
const path = `/${API_VERSION}/distribution/${config.distributionId}/invalidation`;
|
|
37
|
+
const { datetime, date } = amz_dates();
|
|
38
|
+
const payload_hash = hex_sha(body);
|
|
39
|
+
const canonical = [
|
|
40
|
+
"POST",
|
|
41
|
+
path,
|
|
42
|
+
"",
|
|
43
|
+
`host:${API_HOST}`,
|
|
44
|
+
`x-amz-content-sha256:${payload_hash}`,
|
|
45
|
+
`x-amz-date:${datetime}`,
|
|
46
|
+
"",
|
|
47
|
+
"host;x-amz-content-sha256;x-amz-date",
|
|
48
|
+
payload_hash
|
|
49
|
+
].join("\n");
|
|
50
|
+
const scope = `${date}/${REGION}/${SERVICE}/aws4_request`;
|
|
51
|
+
const string_to_sign = [
|
|
52
|
+
"AWS4-HMAC-SHA256",
|
|
53
|
+
datetime,
|
|
54
|
+
scope,
|
|
55
|
+
hex_sha(canonical)
|
|
56
|
+
].join("\n");
|
|
57
|
+
const k_date = hmac("AWS4" + config.secretAccessKey, date);
|
|
58
|
+
const k_region = hmac(k_date, REGION);
|
|
59
|
+
const k_service = hmac(k_region, SERVICE);
|
|
60
|
+
const k_signing = hmac(k_service, "aws4_request");
|
|
61
|
+
const signature = createHmac("sha256", k_signing).update(string_to_sign).digest("hex");
|
|
62
|
+
const res = await fetch(api_base + path, {
|
|
63
|
+
method: "POST",
|
|
64
|
+
headers: {
|
|
65
|
+
host: API_HOST,
|
|
66
|
+
"content-type": "text/xml",
|
|
67
|
+
"x-amz-content-sha256": payload_hash,
|
|
68
|
+
"x-amz-date": datetime,
|
|
69
|
+
authorization: `AWS4-HMAC-SHA256 Credential=${config.accessKeyId}/${scope}, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=${signature}`
|
|
70
|
+
},
|
|
71
|
+
body
|
|
72
|
+
});
|
|
73
|
+
if (!res.ok) throw new Error(`[ogygia] cloudfront invalidation failed: ${res.status} ${await res.text().catch(() => "")}`);
|
|
74
|
+
};
|
|
75
|
+
return {
|
|
76
|
+
name: "cloudfront",
|
|
77
|
+
headers() {
|
|
78
|
+
return {};
|
|
79
|
+
},
|
|
80
|
+
async purgeUrl(url) {
|
|
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 };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { normalize_prefix, subtree_prefix } from "../key.js";
|
|
2
|
+
// src/adapters/upstash.ts
|
|
3
|
+
const GLOB_ACTIVE_G = /[*?[\]\\]/g;
|
|
4
|
+
const TRAILING_SLASHES_RE = /\/+$/;
|
|
5
|
+
const KEY_NS = "og:a:";
|
|
6
|
+
const TAG_NS = "og:t:";
|
|
7
|
+
const SCAN_COUNT = 250;
|
|
8
|
+
/** Escape glob-active characters so a literal pathname can ride a SCAN MATCH pattern. */
|
|
9
|
+
function glob_escape(value) {
|
|
10
|
+
return value.replace(GLOB_ACTIVE_G, (c) => "\\" + c);
|
|
11
|
+
}
|
|
12
|
+
function upstash(config) {
|
|
13
|
+
const base = config.url.replace(TRAILING_SLASHES_RE, "");
|
|
14
|
+
/** One command, Upstash REST shape: POST the command as a JSON array → `{ result }`. */
|
|
15
|
+
const command = async (parts) => {
|
|
16
|
+
const res = await fetch(base, {
|
|
17
|
+
method: "POST",
|
|
18
|
+
headers: {
|
|
19
|
+
authorization: `Bearer ${config.token}`,
|
|
20
|
+
"content-type": "application/json"
|
|
21
|
+
},
|
|
22
|
+
body: JSON.stringify(parts.map(String))
|
|
23
|
+
});
|
|
24
|
+
if (!res.ok) throw new Error(`[ogygia] upstash command failed: ${res.status} ${await res.text().catch(() => "")}`);
|
|
25
|
+
return (await res.json()).result;
|
|
26
|
+
};
|
|
27
|
+
return {
|
|
28
|
+
async get(key) {
|
|
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 };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { PostrenderStore } from "../types.js";
|
|
2
|
+
/** The minimal client surface — ioredis (lowercase) and node-redis v4 (camelCase) both satisfy
|
|
3
|
+
* it structurally; the camelCase variants are probed at call time. */
|
|
4
|
+
export interface ValkeyLike {
|
|
5
|
+
get(key: string): Promise<string | null>;
|
|
6
|
+
/** ioredis-style positional EX; node-redis v4 accepts (key, value, { EX }) — both supported. */
|
|
7
|
+
set(key: string, value: string, ...args: unknown[]): Promise<unknown>;
|
|
8
|
+
del(...keys: string[]): Promise<unknown>;
|
|
9
|
+
scan(cursor: string | number, ...args: unknown[]): Promise<unknown>;
|
|
10
|
+
sadd?(key: string, ...members: string[]): Promise<unknown>;
|
|
11
|
+
sAdd?(key: string, members: string | string[]): Promise<unknown>;
|
|
12
|
+
smembers?(key: string): Promise<string[]>;
|
|
13
|
+
sMembers?(key: string): Promise<string[]>;
|
|
14
|
+
expire?(key: string, seconds: number): Promise<unknown>;
|
|
15
|
+
ttl?(key: string): Promise<number>;
|
|
16
|
+
TTL?(key: string): Promise<number>;
|
|
17
|
+
}
|
|
18
|
+
export declare function valkey(client: ValkeyLike): PostrenderStore;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { normalize_prefix, subtree_prefix } from "../key.js";
|
|
2
|
+
// src/adapters/valkey.ts
|
|
3
|
+
const GLOB_ACTIVE_G = /[*?[\]\\]/g;
|
|
4
|
+
const KEY_NS = "og:a:";
|
|
5
|
+
const TAG_NS = "og:t:";
|
|
6
|
+
const SCAN_COUNT = 250;
|
|
7
|
+
/** Both client families' scan shapes → one { cursor, keys }. */
|
|
8
|
+
function read_scan_reply(reply) {
|
|
9
|
+
if (Array.isArray(reply)) return {
|
|
10
|
+
cursor: String(reply[0]),
|
|
11
|
+
keys: reply[1] ?? []
|
|
12
|
+
};
|
|
13
|
+
const obj = reply;
|
|
14
|
+
return {
|
|
15
|
+
cursor: String(obj.cursor),
|
|
16
|
+
keys: obj.keys ?? []
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/** Escape glob-active characters so a literal pathname can ride a SCAN MATCH pattern. */
|
|
20
|
+
function glob_escape(value) {
|
|
21
|
+
return value.replace(GLOB_ACTIVE_G, (c) => "\\" + c);
|
|
22
|
+
}
|
|
23
|
+
function valkey(client) {
|
|
24
|
+
const set_with_ttl = async (key, value, ttl) => {
|
|
25
|
+
try {
|
|
26
|
+
await client.set(key, value, "EX", ttl);
|
|
27
|
+
} catch {
|
|
28
|
+
await client.set(key, value, { EX: ttl });
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const sadd = async (key, member) => {
|
|
32
|
+
if (client.sadd) await client.sadd(key, member);
|
|
33
|
+
else if (client.sAdd) await client.sAdd(key, member);
|
|
34
|
+
else throw new Error("[ogygia] valkey client has no sadd/sAdd — og.source tags need it");
|
|
35
|
+
};
|
|
36
|
+
const smembers = async (key) => {
|
|
37
|
+
if (client.smembers) return client.smembers(key);
|
|
38
|
+
if (client.sMembers) return client.sMembers(key);
|
|
39
|
+
throw new Error("[ogygia] valkey client has no smembers/sMembers — og.source tags need it");
|
|
40
|
+
};
|
|
41
|
+
/** Seconds left on a key (-1 = no expiry), or null when the client has no TTL command. */
|
|
42
|
+
const read_ttl = async (key) => {
|
|
43
|
+
if (client.ttl) return Number(await client.ttl(key));
|
|
44
|
+
if (client.TTL) return Number(await client.TTL(key));
|
|
45
|
+
return null;
|
|
46
|
+
};
|
|
47
|
+
const scan_prefix = async (prefix, on_keys) => {
|
|
48
|
+
const pattern = glob_escape(KEY_NS + prefix) + "*";
|
|
49
|
+
let cursor = "0";
|
|
50
|
+
do {
|
|
51
|
+
const page = read_scan_reply(await client.scan(cursor, "MATCH", pattern, "COUNT", SCAN_COUNT));
|
|
52
|
+
cursor = page.cursor;
|
|
53
|
+
if (page.keys.length) await on_keys(page.keys);
|
|
54
|
+
} while (cursor !== "0");
|
|
55
|
+
};
|
|
56
|
+
return {
|
|
57
|
+
async get(key) {
|
|
58
|
+
const raw = await client.get(KEY_NS + key);
|
|
59
|
+
if (!raw) return null;
|
|
60
|
+
try {
|
|
61
|
+
return JSON.parse(raw);
|
|
62
|
+
} catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
async put(key, entry, { ttl, tags }) {
|
|
67
|
+
await set_with_ttl(KEY_NS + key, JSON.stringify(entry), ttl);
|
|
68
|
+
for (const tag of tags ?? []) {
|
|
69
|
+
await sadd(TAG_NS + tag, key);
|
|
70
|
+
const left = await read_ttl(TAG_NS + tag);
|
|
71
|
+
if (left === null || left < ttl) await client.expire?.(TAG_NS + tag, ttl);
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
async evict(key) {
|
|
75
|
+
await client.del(KEY_NS + key);
|
|
76
|
+
},
|
|
77
|
+
async evictWhere({ prefix }) {
|
|
78
|
+
const p = normalize_prefix(prefix);
|
|
79
|
+
await scan_prefix(subtree_prefix(p), async (keys) => void await client.del(...keys));
|
|
80
|
+
await client.del(KEY_NS + p);
|
|
81
|
+
},
|
|
82
|
+
async evictByTag(tag) {
|
|
83
|
+
const members = await smembers(TAG_NS + tag);
|
|
84
|
+
if (members.length) await client.del(...members.map((m) => KEY_NS + m));
|
|
85
|
+
await client.del(TAG_NS + tag);
|
|
86
|
+
return members;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
export { valkey };
|
package/dist/app.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { PostrenderServerOptions } from "./server.js";
|
|
2
|
+
import { ServerApp } from "@ogygia/core/plugin";
|
|
3
|
+
export declare function set_postrender_app(app: ServerApp<PostrenderServerOptions> | null): void;
|
|
4
|
+
export declare function postrender_app(): ServerApp<PostrenderServerOptions> | null;
|
package/dist/app.js
ADDED
package/dist/handle.d.ts
ADDED
package/dist/handle.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { install, mark_handled } from "./registry.js";
|
|
2
|
+
import { postrender_app } from "./app.js";
|
|
3
|
+
// src/handle.ts
|
|
4
|
+
let warned_unlisted = false;
|
|
5
|
+
function handle(options = {}) {
|
|
6
|
+
const runtime = install(options);
|
|
7
|
+
return ({ event, resolve }) => {
|
|
8
|
+
mark_handled(event, runtime);
|
|
9
|
+
if (!postrender_app() && !warned_unlisted) {
|
|
10
|
+
warned_unlisted = true;
|
|
11
|
+
console.warn("[@ogygia/postrender] postrender.handle() runs, but `postrender()` is not listed in vite.config, so no page is stored. Add it: `plugins: [ogygia(), postrender(), sveltekit()]` (from '@ogygia/postrender/vite').");
|
|
12
|
+
}
|
|
13
|
+
return resolve(event);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export { handle };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { handle } from "./handle.js";
|
|
2
|
+
import { invalidate, invalidateWhere } from "./registry.js";
|
|
3
|
+
import { memory_store } from "./memory-store.js";
|
|
4
|
+
import { ValkeyLike, valkey } from "./adapters/valkey.js";
|
|
5
|
+
import { UpstashConfig, upstash } from "./adapters/upstash.js";
|
|
6
|
+
import { KvNamespaceLike, cloudflareKv } from "./adapters/cloudflare-kv.js";
|
|
7
|
+
import { AkamaiConfig, akamai } from "./adapters/akamai.js";
|
|
8
|
+
import { CloudfrontConfig, cloudfront } from "./adapters/cloudfront.js";
|
|
9
|
+
import { CloudflareConfig, cloudflare } from "./adapters/cloudflare.js";
|
|
10
|
+
import { EdgeAdapter, PostrenderEntry, PostrenderHandleOptions, PostrenderMeta, PostrenderPutOptions, PostrenderStore } from "./types.js";
|
|
11
|
+
export declare const postrender: {
|
|
12
|
+
handle: typeof handle;
|
|
13
|
+
invalidate: typeof invalidate;
|
|
14
|
+
invalidateWhere: typeof invalidateWhere;
|
|
15
|
+
};
|
|
16
|
+
export { type AkamaiConfig, type CloudflareConfig, type CloudfrontConfig, type EdgeAdapter, type KvNamespaceLike, type PostrenderEntry, type PostrenderHandleOptions, type PostrenderMeta, type PostrenderPutOptions, type PostrenderStore, type UpstashConfig, type ValkeyLike, akamai, cloudfront as awsCloudfront, cloudflare, cloudflareKv, cloudfront, memory_store as memoryStore, upstash, valkey };
|