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

Commit ceb8c09

Browse files
committed
Add Miniflare#getCaches method for accessing caches outside worker
1 parent 043e5bf commit ceb8c09

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@
5858
"node": ">=16.7"
5959
},
6060
"volta": {
61-
"node": "16.9.0"
61+
"node": "16.13.0"
6262
}
6363
}

packages/cache/src/plugin.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,24 @@ export class CachePlugin extends Plugin<CacheOptions> implements CacheOptions {
121121
})
122122
cacheWarnUsage?: boolean;
123123

124+
#caches?: CacheStorage;
125+
124126
constructor(log: Log, compat: Compatibility, options?: CacheOptions) {
125127
super(log, compat);
126128
this.assignOptions(options);
127129
}
128130

129131
setup(storageFactory: StorageFactory): SetupResult {
130-
const caches = new CacheStorage(
132+
this.#caches = new CacheStorage(
131133
this,
132134
this.log,
133135
storageFactory,
134136
this.compat.isEnabled("formdata_parser_supports_files")
135137
);
136-
return { globals: { caches } };
138+
return { globals: { caches: this.#caches } };
139+
}
140+
141+
getCaches(): CacheStorage {
142+
return this.#caches!;
137143
}
138144
}

packages/cache/test/plugin.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ test("CachePlugin: setup: includes CacheStorage in globals", async (t) => {
146146
let caches = result.globals?.caches;
147147
t.true(caches instanceof CacheStorage);
148148
assert(caches instanceof CacheStorage);
149+
t.is(caches, plugin.getCaches());
149150
await caches.default.put("http://localhost:8787/", testResponse());
150151
t.true(map.has("http://localhost:8787/"));
151152

packages/miniflare/src/api.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import http from "http";
22
import https from "https";
3-
import { CachePlugin } from "@miniflare/cache";
3+
import { CachePlugin, CacheStorage } from "@miniflare/cache";
44
import {
55
BindingsPlugin,
66
BuildPlugin,
@@ -93,6 +93,11 @@ export class Miniflare extends MiniflareCore<Plugins> {
9393
return plugin.getNamespace(storage, namespace);
9494
}
9595

96+
async getCaches(): Promise<CacheStorage> {
97+
const plugin = (await this.getPlugins()).CachePlugin;
98+
return plugin.getCaches();
99+
}
100+
96101
async getDurableObjectNamespace(
97102
objectName: string
98103
): Promise<DurableObjectNamespace> {

packages/miniflare/test/api.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ test("Miniflare: getKVNamespace: gets KV namespace", async (t) => {
6565
const res = await mf.dispatchFetch("http://localhost/");
6666
t.is(await res.text(), "value");
6767
});
68+
test("Miniflare: getCaches: gets CacheStorage instance", async (t) => {
69+
const mf = new Miniflare({ script: "//" });
70+
const caches = await mf.getCaches();
71+
const globalScope = await mf.getGlobalScope();
72+
t.is(caches, globalScope.caches);
73+
});
6874
test("Miniflare: getDurableObjectNamespace: gets Durable Object namespace", async (t) => {
6975
const mf = new Miniflare({
7076
script: `export class TestObject {

0 commit comments

Comments
 (0)