@@ -11,12 +11,10 @@ import {
11
11
sanitisePath ,
12
12
} from "../../shared" ;
13
13
import {
14
- FileStorage ,
15
- MemoryStorage ,
16
- RemoteStorage ,
17
- SqliteStorage ,
18
- Storage ,
19
- } from "../../storage" ;
14
+ NewStorage ,
15
+ createFileStorage ,
16
+ createMemoryStorage ,
17
+ } from "../../storage2" ;
20
18
21
19
// TODO: explain why persist passed as header, want options set to be atomic,
22
20
// if set gateway before script update, may be using new persist before new script
@@ -42,21 +40,13 @@ export type CloudflareFetch = z.infer<typeof CloudflareFetchSchema>;
42
40
export interface GatewayConstructor < Gateway > {
43
41
new (
44
42
log : Log ,
45
- storage : Storage ,
43
+ storage : NewStorage ,
46
44
timers : Timers ,
47
45
namespace : string ,
48
46
dispatchFetch : DispatchFetch
49
47
) : Gateway ;
50
48
}
51
49
52
- export interface RemoteStorageConstructor {
53
- new (
54
- cache : Storage ,
55
- cloudflareFetch : CloudflareFetch ,
56
- namespace : string
57
- ) : RemoteStorage ;
58
- }
59
-
60
50
export const DEFAULT_PERSIST_ROOT = ".mf" ;
61
51
62
52
export const PARAM_FILE_UNSANITISE = "unsanitise" ;
@@ -69,30 +59,25 @@ export function maybeParseURL(url: Persistence): URL | undefined {
69
59
}
70
60
71
61
export class GatewayFactory < Gateway > {
72
- readonly #memoryStorages = new Map < string , MemoryStorage > ( ) ;
62
+ readonly #memoryStorages = new Map < string , NewStorage > ( ) ;
73
63
readonly #gateways = new Map < string , [ Persistence , Gateway ] > ( ) ;
74
64
75
65
constructor (
76
66
private readonly log : Log ,
77
67
private readonly timers : Timers ,
78
68
private readonly dispatchFetch : DispatchFetch ,
79
- private readonly cloudflareFetch : CloudflareFetch | undefined ,
80
69
private readonly pluginName : string ,
81
- private readonly gatewayClass : GatewayConstructor < Gateway > ,
82
- private readonly remoteStorageClass ?: RemoteStorageConstructor
70
+ private readonly gatewayClass : GatewayConstructor < Gateway >
83
71
) { }
84
72
85
73
#getMemoryStorage( namespace : string ) {
86
74
let storage = this . #memoryStorages. get ( namespace ) ;
87
75
if ( storage !== undefined ) return storage ;
88
- this . #memoryStorages. set (
89
- namespace ,
90
- ( storage = new MemoryStorage ( undefined , this . timers . now ) )
91
- ) ;
76
+ this . #memoryStorages. set ( namespace , ( storage = createMemoryStorage ( ) ) ) ;
92
77
return storage ;
93
78
}
94
79
95
- getStorage ( namespace : string , persist : Persistence ) : Storage {
80
+ getStorage ( namespace : string , persist : Persistence ) : NewStorage {
96
81
// If persistence is disabled, use memory storage
97
82
if ( persist === undefined || persist === false ) {
98
83
return this . #getMemoryStorage( namespace ) ;
@@ -109,47 +94,20 @@ export class GatewayFactory<Gateway> {
109
94
}
110
95
if ( url . protocol === "file:" ) {
111
96
const root = path . join ( fileURLToPath ( url ) , sanitisedNamespace ) ;
112
- const unsanitise =
113
- url . searchParams . get ( PARAM_FILE_UNSANITISE ) === "true" ;
114
- return new FileStorage ( root , ! unsanitise , this . timers . now ) ;
115
- } else if ( url . protocol === "sqlite:" ) {
116
- return new SqliteStorage (
117
- url . pathname ,
118
- sanitisedNamespace ,
119
- this . timers . now
120
- ) ;
121
- }
122
- // TODO: support Redis storage?
123
- if ( url . protocol === "remote:" ) {
124
- const { cloudflareFetch, remoteStorageClass } = this ;
125
- if ( cloudflareFetch === undefined ) {
126
- throw new MiniflareCoreError (
127
- "ERR_PERSIST_REMOTE_UNAUTHENTICATED" ,
128
- "Authenticated Cloudflare API `cloudflareFetch` option not provided but required for remote storage"
129
- ) ;
130
- }
131
- if ( remoteStorageClass === undefined ) {
132
- throw new MiniflareCoreError (
133
- "ERR_PERSIST_REMOTE_UNSUPPORTED" ,
134
- `The "${ this . pluginName } " plugin does not support remote storage`
135
- ) ;
136
- }
137
- const cachePersist = url . searchParams . get ( "cache" ) ?? undefined ;
138
- const cache = this . getStorage ( namespace , cachePersist ) ;
139
- return new remoteStorageClass ( cache , cloudflareFetch , namespace ) ;
97
+ return createFileStorage ( root ) ;
140
98
}
141
99
throw new MiniflareCoreError (
142
100
"ERR_PERSIST_UNSUPPORTED" ,
143
101
`Unsupported "${ url . protocol } " persistence protocol for storage: ${ url . href } `
144
102
) ;
145
103
}
146
104
147
- // Otherwise, fallback to sanitised file storage
105
+ // Otherwise, fallback to file storage
148
106
const root =
149
107
persist === true
150
108
? path . join ( DEFAULT_PERSIST_ROOT , this . pluginName , sanitisedNamespace )
151
109
: path . join ( persist , sanitisedNamespace ) ;
152
- return new FileStorage ( root , undefined , this . timers . now ) ;
110
+ return createFileStorage ( root ) ;
153
111
}
154
112
155
113
get ( namespace : string , persist : Persistence ) : Gateway {
0 commit comments