Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit f0b4344

Browse files
committed
Add cache plugin stubs
1 parent 4d84a89 commit f0b4344

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Clock, Storage } from "@miniflare/shared";
2+
3+
export class CacheGateway {
4+
constructor(
5+
private readonly storage: Storage,
6+
private readonly clock: Clock
7+
) {}
8+
9+
async match(key: string) {
10+
throw new Error("Not yet implemented!");
11+
}
12+
13+
async put(key: string, value: Uint8Array) {
14+
throw new Error("Not yet implemented!");
15+
}
16+
17+
async delete(key: string) {
18+
throw new Error("Not yet implemented!");
19+
}
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { z } from "zod";
2+
import { PersistenceSchema, Plugin } from "../shared";
3+
import { CacheGateway } from "./gateway";
4+
import { CacheRouter } from "./router";
5+
6+
export const CacheOptionsSchema = z.object({
7+
cache: z.boolean().optional(),
8+
cacheWarnUsage: z.boolean().optional(),
9+
});
10+
export const CacheSharedOptionsSchema = z.object({
11+
cachePersist: PersistenceSchema,
12+
});
13+
14+
export const CACHE_PLUGIN_NAME = "cache";
15+
export const CACHE_PLUGIN: Plugin<
16+
typeof CacheOptionsSchema,
17+
typeof CacheSharedOptionsSchema,
18+
CacheGateway
19+
> = {
20+
gateway: CacheGateway,
21+
router: CacheRouter,
22+
options: CacheOptionsSchema,
23+
sharedOptions: CacheSharedOptionsSchema,
24+
getBindings(options) {
25+
return undefined;
26+
},
27+
getServices(options) {
28+
return undefined;
29+
},
30+
};
31+
32+
export * from "./gateway";
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Response } from "undici";
2+
import {
3+
DELETE,
4+
GET,
5+
PUT,
6+
RouteHandler,
7+
Router,
8+
decodePersist,
9+
} from "../shared";
10+
import { CacheGateway } from "./gateway";
11+
12+
export interface CacheParams {
13+
namespace: string;
14+
key: string;
15+
}
16+
export class CacheRouter extends Router<CacheGateway> {
17+
@GET("/:namespace/:key")
18+
match: RouteHandler<CacheParams> = async (req, params) => {
19+
const persist = decodePersist(req.headers);
20+
const gateway = this.gatewayFactory.get(params.namespace, persist);
21+
await gateway.match(params.key);
22+
return new Response();
23+
};
24+
25+
@PUT("/:namespace/:key")
26+
put: RouteHandler<CacheParams> = async (req, params) => {
27+
const persist = decodePersist(req.headers);
28+
const gateway = this.gatewayFactory.get(params.namespace, persist);
29+
await gateway.put(params.key, new Uint8Array(await req.arrayBuffer()));
30+
return new Response();
31+
};
32+
33+
@DELETE("/:namespace/:key")
34+
delete: RouteHandler<CacheParams> = async (req, params) => {
35+
const persist = decodePersist(req.headers);
36+
const gateway = this.gatewayFactory.get(params.namespace, persist);
37+
await gateway.delete(params.key);
38+
return new Response();
39+
};
40+
}

0 commit comments

Comments
 (0)