This repository was archived by the owner on Mar 13, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +92
-0
lines changed
packages/tre/src/plugins/cache Expand file tree Collapse file tree 3 files changed +92
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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" ;
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments