Skip to content

Commit 34c71ce

Browse files
feat: introduce defaultPersistRoot option (#9330)
* feat: introduce defaultPersistRoot option * add changeset * Update packages/miniflare/src/plugins/shared/index.ts Co-authored-by: Pete Bacon Darwin <[email protected]> * add missin g getPersistPath on secret store plugin * fix format error --------- Co-authored-by: Pete Bacon Darwin <[email protected]>
1 parent 6c03bde commit 34c71ce

File tree

18 files changed

+134
-90
lines changed

18 files changed

+134
-90
lines changed

.changeset/blue-streets-thank.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
"miniflare": minor
3+
---
4+
5+
Add a new `defaultPersistRoot` option to control where plugins persist data when no path is provided.
6+
7+
```js
8+
// Before this change / No `defaultPersistRoot`
9+
new Miniflare({
10+
kvPersist: undefined, // → "/(tmp)/kv"
11+
d1Persist: true, // → "/.mf/d1"
12+
r2Persist: false, // → "/(tmp)/d1"
13+
cachePersist: "/my-cache", // → "/my-cache"
14+
});
15+
16+
// With `defaultPersistRoot`
17+
new Miniflare({
18+
defaultPersistRoot: "/storage",
19+
kvPersist: undefined, // → "/storage/kv"
20+
d1Persist: true, // → "/storage/d1"
21+
r2Persist: false, // → "/(tmp)/d1"
22+
cachePersist: "/my-cache", // → "/my-cache"
23+
});
24+
```

.changeset/twenty-cats-clap.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@cloudflare/vite-plugin": minor
3+
"wrangler": minor
4+
---
5+
6+
Updated internal configuration to use Miniflare’s new `defaultPersistRoot` instead of per-plugin `persist` flags

packages/miniflare/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,7 @@ export class Miniflare {
12931293
workerIndex: i,
12941294
additionalModules,
12951295
tmpPath: this.#tmpPath,
1296+
defaultPersistRoot: sharedOpts.core.defaultPersistRoot,
12961297
workerNames,
12971298
loopbackPort,
12981299
unsafeStickyBlobs,

packages/miniflare/src/plugins/cache/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const CACHE_PLUGIN: Plugin<
5656
options,
5757
workerIndex,
5858
tmpPath,
59+
defaultPersistRoot,
5960
unsafeStickyBlobs,
6061
}) {
6162
const cache = options.cache ?? true;
@@ -100,7 +101,12 @@ export const CACHE_PLUGIN: Plugin<
100101
const uniqueKey = `miniflare-${CACHE_OBJECT_CLASS_NAME}`;
101102

102103
const persist = sharedOptions.cachePersist;
103-
const persistPath = getPersistPath(CACHE_PLUGIN_NAME, tmpPath, persist);
104+
const persistPath = getPersistPath(
105+
CACHE_PLUGIN_NAME,
106+
tmpPath,
107+
defaultPersistRoot,
108+
persist
109+
);
104110
await fs.mkdir(persistPath, { recursive: true });
105111
const storageService: Service = {
106112
name: CACHE_STORAGE_SERVICE_NAME,
@@ -148,6 +154,6 @@ export const CACHE_PLUGIN: Plugin<
148154
return services;
149155
},
150156
getPersistPath({ cachePersist }, tmpPath) {
151-
return getPersistPath(CACHE_PLUGIN_NAME, tmpPath, cachePersist);
157+
return getPersistPath(CACHE_PLUGIN_NAME, tmpPath, undefined, cachePersist);
152158
},
153159
};

packages/miniflare/src/plugins/core/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ export const CoreSharedOptionsSchema = z.object({
226226
unsafeTriggerHandlers: z.boolean().optional(),
227227
// Enable logging requests
228228
logRequests: z.boolean().default(true),
229+
230+
// Path to the root directory for persisting data
231+
// Used as the default for all plugins with the plugin name as the subdirectory name
232+
defaultPersistRoot: z.string().optional(),
229233
});
230234

231235
export const CORE_PLUGIN_NAME = "core";

packages/miniflare/src/plugins/d1/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export const D1_PLUGIN: Plugin<
9999
options,
100100
sharedOptions,
101101
tmpPath,
102+
defaultPersistRoot,
102103
log,
103104
unsafeStickyBlobs,
104105
}) {
@@ -115,7 +116,12 @@ export const D1_PLUGIN: Plugin<
115116

116117
if (databases.length > 0) {
117118
const uniqueKey = `miniflare-${D1_DATABASE_OBJECT_CLASS_NAME}`;
118-
const persistPath = getPersistPath(D1_PLUGIN_NAME, tmpPath, persist);
119+
const persistPath = getPersistPath(
120+
D1_PLUGIN_NAME,
121+
tmpPath,
122+
defaultPersistRoot,
123+
persist
124+
);
119125
await fs.mkdir(persistPath, { recursive: true });
120126

121127
const storageService: Service = {
@@ -165,6 +171,6 @@ export const D1_PLUGIN: Plugin<
165171
return services;
166172
},
167173
getPersistPath({ d1Persist }, tmpPath) {
168-
return getPersistPath(D1_PLUGIN_NAME, tmpPath, d1Persist);
174+
return getPersistPath(D1_PLUGIN_NAME, tmpPath, undefined, d1Persist);
169175
},
170176
};

packages/miniflare/src/plugins/do/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export const DURABLE_OBJECTS_PLUGIN: Plugin<
103103
async getServices({
104104
sharedOptions,
105105
tmpPath,
106+
defaultPersistRoot,
106107
durableObjectClassNames,
107108
unsafeEphemeralDurableObjects,
108109
}) {
@@ -125,6 +126,7 @@ export const DURABLE_OBJECTS_PLUGIN: Plugin<
125126
const storagePath = getPersistPath(
126127
DURABLE_OBJECTS_PLUGIN_NAME,
127128
tmpPath,
129+
defaultPersistRoot,
128130
sharedOptions.durableObjectsPersist
129131
);
130132
// `workerd` requires the `disk.path` to exist. Setting `recursive: true`
@@ -145,6 +147,7 @@ export const DURABLE_OBJECTS_PLUGIN: Plugin<
145147
return getPersistPath(
146148
DURABLE_OBJECTS_PLUGIN_NAME,
147149
tmpPath,
150+
undefined,
148151
durableObjectsPersist
149152
);
150153
},

packages/miniflare/src/plugins/kv/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export const KV_PLUGIN: Plugin<
106106
options,
107107
sharedOptions,
108108
tmpPath,
109+
defaultPersistRoot,
109110
log,
110111
unsafeStickyBlobs,
111112
}) {
@@ -122,7 +123,12 @@ export const KV_PLUGIN: Plugin<
122123

123124
if (services.length > 0) {
124125
const uniqueKey = `miniflare-${KV_NAMESPACE_OBJECT_CLASS_NAME}`;
125-
const persistPath = getPersistPath(KV_PLUGIN_NAME, tmpPath, persist);
126+
const persistPath = getPersistPath(
127+
KV_PLUGIN_NAME,
128+
tmpPath,
129+
defaultPersistRoot,
130+
persist
131+
);
126132
await fs.mkdir(persistPath, { recursive: true });
127133
const storageService: Service = {
128134
name: KV_STORAGE_SERVICE_NAME,
@@ -178,7 +184,7 @@ export const KV_PLUGIN: Plugin<
178184
},
179185

180186
getPersistPath({ kvPersist }, tmpPath) {
181-
return getPersistPath(KV_PLUGIN_NAME, tmpPath, kvPersist);
187+
return getPersistPath(KV_PLUGIN_NAME, tmpPath, undefined, kvPersist);
182188
},
183189
};
184190

packages/miniflare/src/plugins/r2/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export const R2_PLUGIN: Plugin<
7474
options,
7575
sharedOptions,
7676
tmpPath,
77+
defaultPersistRoot,
7778
log,
7879
unsafeStickyBlobs,
7980
}) {
@@ -90,7 +91,12 @@ export const R2_PLUGIN: Plugin<
9091

9192
if (buckets.length > 0) {
9293
const uniqueKey = `miniflare-${R2_BUCKET_OBJECT_CLASS_NAME}`;
93-
const persistPath = getPersistPath(R2_PLUGIN_NAME, tmpPath, persist);
94+
const persistPath = getPersistPath(
95+
R2_PLUGIN_NAME,
96+
tmpPath,
97+
defaultPersistRoot,
98+
persist
99+
);
94100
await fs.mkdir(persistPath, { recursive: true });
95101
const storageService: Service = {
96102
name: R2_STORAGE_SERVICE_NAME,
@@ -139,6 +145,6 @@ export const R2_PLUGIN: Plugin<
139145
return services;
140146
},
141147
getPersistPath({ r2Persist }, tmpPath) {
142-
return getPersistPath(R2_PLUGIN_NAME, tmpPath, r2Persist);
148+
return getPersistPath(R2_PLUGIN_NAME, tmpPath, undefined, r2Persist);
143149
},
144150
};

packages/miniflare/src/plugins/secret-store/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ export const SECRET_STORE_PLUGIN: Plugin<
6767
])
6868
);
6969
},
70-
async getServices({ options, sharedOptions, tmpPath, unsafeStickyBlobs }) {
70+
async getServices({
71+
options,
72+
sharedOptions,
73+
tmpPath,
74+
defaultPersistRoot,
75+
unsafeStickyBlobs,
76+
}) {
7177
const configs = options.secretsStoreSecrets
7278
? Object.values(options.secretsStoreSecrets)
7379
: [];
@@ -79,6 +85,7 @@ export const SECRET_STORE_PLUGIN: Plugin<
7985
const persistPath = getPersistPath(
8086
SECRET_STORE_PLUGIN_NAME,
8187
tmpPath,
88+
defaultPersistRoot,
8289
sharedOptions.secretsStorePersist
8390
);
8491

@@ -162,4 +169,12 @@ export const SECRET_STORE_PLUGIN: Plugin<
162169

163170
return [...services, storageService, objectService];
164171
},
172+
getPersistPath({ secretsStorePersist }, tmpPath) {
173+
return getPersistPath(
174+
SECRET_STORE_PLUGIN_NAME,
175+
tmpPath,
176+
undefined,
177+
secretsStorePersist
178+
);
179+
},
165180
};

0 commit comments

Comments
 (0)