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

Commit 4d84a89

Browse files
committed
Add Durable Objects plugin stubs
1 parent 35a0333 commit 4d84a89

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Clock, Storage } from "@miniflare/shared";
2+
3+
export class DurableObjectsStorageGateway {
4+
constructor(
5+
private readonly storage: Storage,
6+
private readonly clock: Clock
7+
) {}
8+
9+
async get(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+
21+
async list() {
22+
throw new Error("Not yet implemented!");
23+
}
24+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { z } from "zod";
2+
import { PersistenceSchema, Plugin } from "../shared";
3+
import { DurableObjectsStorageGateway } from "./gateway";
4+
import { DurableObjectsStorageRouter } from "./router";
5+
6+
export const DurableObjectsOptionsSchema = z.object({
7+
durableObjects: z
8+
.record(
9+
z.union([
10+
z.string(),
11+
z.object({
12+
className: z.string(),
13+
scriptName: z.string().optional(),
14+
}),
15+
])
16+
)
17+
.optional(),
18+
});
19+
export const DurableObjectsSharedOptionsSchema = z.object({
20+
durableObjectsPersist: PersistenceSchema,
21+
});
22+
23+
export const DURABLE_OBJECTS_PLUGIN_NAME = "do";
24+
export const DURABLE_OBJECTS_PLUGIN: Plugin<
25+
typeof DurableObjectsOptionsSchema,
26+
typeof DurableObjectsSharedOptionsSchema,
27+
DurableObjectsStorageGateway
28+
> = {
29+
gateway: DurableObjectsStorageGateway,
30+
router: DurableObjectsStorageRouter,
31+
options: DurableObjectsOptionsSchema,
32+
sharedOptions: DurableObjectsSharedOptionsSchema,
33+
getBindings(options) {
34+
return undefined;
35+
},
36+
getServices(options) {
37+
return undefined;
38+
},
39+
};
40+
41+
export * from "./gateway";
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Response } from "undici";
2+
import {
3+
DELETE,
4+
GET,
5+
PUT,
6+
RouteHandler,
7+
Router,
8+
decodePersist,
9+
} from "../shared";
10+
import { DurableObjectsStorageGateway } from "./gateway";
11+
12+
export interface DurableObjectStorageParams {
13+
namespace: string;
14+
key: string;
15+
}
16+
export class DurableObjectsStorageRouter extends Router<DurableObjectsStorageGateway> {
17+
@GET("/:bucket/:key")
18+
get: RouteHandler<DurableObjectStorageParams> = async (req, params) => {
19+
const persist = decodePersist(req.headers);
20+
const gateway = this.gatewayFactory.get(params.namespace, persist);
21+
await gateway.get(params.key);
22+
return new Response();
23+
};
24+
25+
@PUT("/:bucket/:key")
26+
put: RouteHandler<DurableObjectStorageParams> = 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("/:bucket/:key")
34+
delete: RouteHandler<DurableObjectStorageParams> = 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+
41+
@GET("/:bucket/")
42+
list: RouteHandler<Omit<DurableObjectStorageParams, "key">> = async (
43+
req,
44+
params
45+
) => {
46+
const persist = decodePersist(req.headers);
47+
const gateway = this.gatewayFactory.get(params.namespace, persist);
48+
await gateway.list();
49+
return new Response();
50+
};
51+
}

0 commit comments

Comments
 (0)