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

Commit 35a0333

Browse files
committed
Add R2 plugin stubs
1 parent ff3c347 commit 35a0333

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-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 R2Gateway {
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { z } from "zod";
2+
import { PersistenceSchema, Plugin } from "../shared";
3+
import { R2Gateway } from "./gateway";
4+
import { R2Router } from "./router";
5+
6+
export const R2OptionsSchema = z.object({
7+
r2Buckets: z.record(z.string()).optional(),
8+
});
9+
export const R2SharedOptionsSchema = z.object({
10+
r2Persist: PersistenceSchema,
11+
});
12+
13+
export const R2_PLUGIN_NAME = "r2";
14+
export const R2_PLUGIN: Plugin<
15+
typeof R2OptionsSchema,
16+
typeof R2SharedOptionsSchema,
17+
R2Gateway
18+
> = {
19+
gateway: R2Gateway,
20+
router: R2Router,
21+
options: R2OptionsSchema,
22+
sharedOptions: R2SharedOptionsSchema,
23+
getBindings(options) {
24+
return undefined;
25+
},
26+
getServices(options) {
27+
return undefined;
28+
},
29+
};
30+
31+
export * from "./gateway";
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Response } from "undici";
2+
import {
3+
DELETE,
4+
GET,
5+
PUT,
6+
RouteHandler,
7+
Router,
8+
decodePersist,
9+
} from "../shared";
10+
import { R2Gateway } from "./gateway";
11+
12+
export interface R2Params {
13+
bucket: string;
14+
key: string;
15+
}
16+
export class R2Router extends Router<R2Gateway> {
17+
@GET("/:bucket/:key")
18+
get: RouteHandler<R2Params> = async (req, params) => {
19+
// console.log(await req.json());
20+
21+
const persist = decodePersist(req.headers);
22+
const gateway = this.gatewayFactory.get(params.bucket, persist);
23+
await gateway.get(params.key);
24+
return new Response();
25+
};
26+
27+
@PUT("/:bucket/:key")
28+
put: RouteHandler<R2Params> = async (req, params) => {
29+
const persist = decodePersist(req.headers);
30+
const gateway = this.gatewayFactory.get(params.bucket, persist);
31+
await gateway.put(params.key, new Uint8Array(await req.arrayBuffer()));
32+
return new Response();
33+
};
34+
35+
@DELETE("/:bucket/:key")
36+
delete: RouteHandler<R2Params> = async (req, params) => {
37+
const persist = decodePersist(req.headers);
38+
const gateway = this.gatewayFactory.get(params.bucket, persist);
39+
await gateway.delete(params.key);
40+
return new Response();
41+
};
42+
43+
@GET("/:bucket/")
44+
list: RouteHandler<Omit<R2Params, "key">> = async (req, params) => {
45+
const persist = decodePersist(req.headers);
46+
const gateway = this.gatewayFactory.get(params.bucket, persist);
47+
await gateway.list();
48+
return new Response();
49+
};
50+
}

0 commit comments

Comments
 (0)