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

Commit f6e1543

Browse files
committed
Automatically substitute Workers types for Node equivalents
Declares a `ReplaceWorkersTypes` type instead of manually declaring types for each of the Workers APIs.
1 parent 60c7ce4 commit f6e1543

File tree

5 files changed

+195
-247
lines changed

5 files changed

+195
-247
lines changed

packages/miniflare/src/index.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ import { Duplex, Transform, Writable } from "stream";
99
import { ReadableStream } from "stream/web";
1010
import zlib from "zlib";
1111
import type {
12+
CacheStorage,
1213
D1Database,
14+
DurableObjectNamespace,
15+
KVNamespace,
1316
Queue,
17+
R2Bucket,
1418
RequestInitCfProperties,
1519
} from "@cloudflare/workers-types/experimental";
1620
import exitHook from "exit-hook";
@@ -29,16 +33,13 @@ import {
2933
fetch,
3034
} from "./http";
3135
import {
32-
CacheStorage,
3336
D1_PLUGIN_NAME,
3437
DURABLE_OBJECTS_PLUGIN_NAME,
3538
DispatchFetch,
3639
DurableObjectClassNames,
37-
DurableObjectNamespace,
3840
GatewayConstructor,
3941
GatewayFactory,
4042
HEADER_CF_BLOB,
41-
KVNamespace,
4243
KV_PLUGIN_NAME,
4344
PLUGIN_ENTRIES,
4445
Persistence,
@@ -48,8 +49,8 @@ import {
4849
QUEUES_PLUGIN_NAME,
4950
QueueConsumers,
5051
QueuesError,
51-
R2Bucket,
5252
R2_PLUGIN_NAME,
53+
ReplaceWorkersTypes,
5354
SharedOptions,
5455
SourceMapRegistry,
5556
WorkerOptions,
@@ -1198,23 +1199,24 @@ export class Miniflare {
11981199
return proxy as T;
11991200
}
12001201
// TODO(someday): would be nice to define these in plugins
1201-
async getCaches(): Promise<CacheStorage> {
1202+
async getCaches(): Promise<ReplaceWorkersTypes<CacheStorage>> {
12021203
const proxyClient = await this._getProxyClient();
1203-
return proxyClient.global.caches as unknown as CacheStorage;
1204+
return proxyClient.global
1205+
.caches as unknown as ReplaceWorkersTypes<CacheStorage>;
12041206
}
12051207
getD1Database(bindingName: string, workerName?: string): Promise<D1Database> {
12061208
return this.#getProxy(D1_PLUGIN_NAME, bindingName, workerName);
12071209
}
12081210
getDurableObjectNamespace(
12091211
bindingName: string,
12101212
workerName?: string
1211-
): Promise<DurableObjectNamespace> {
1213+
): Promise<ReplaceWorkersTypes<DurableObjectNamespace>> {
12121214
return this.#getProxy(DURABLE_OBJECTS_PLUGIN_NAME, bindingName, workerName);
12131215
}
12141216
getKVNamespace(
12151217
bindingName: string,
12161218
workerName?: string
1217-
): Promise<KVNamespace> {
1219+
): Promise<ReplaceWorkersTypes<KVNamespace>> {
12181220
return this.#getProxy(KV_PLUGIN_NAME, bindingName, workerName);
12191221
}
12201222
getQueueProducer<Body = unknown>(
@@ -1223,7 +1225,10 @@ export class Miniflare {
12231225
): Promise<Queue<Body>> {
12241226
return this.#getProxy(QUEUES_PLUGIN_NAME, bindingName, workerName);
12251227
}
1226-
getR2Bucket(bindingName: string, workerName?: string): Promise<R2Bucket> {
1228+
getR2Bucket(
1229+
bindingName: string,
1230+
workerName?: string
1231+
): Promise<ReplaceWorkersTypes<R2Bucket>> {
12271232
return this.#getProxy(R2_PLUGIN_NAME, bindingName, workerName);
12281233
}
12291234

0 commit comments

Comments
 (0)