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

Commit da9e434

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 7433a09 commit da9e434

File tree

5 files changed

+195
-247
lines changed

5 files changed

+195
-247
lines changed

packages/miniflare/src/index.ts

+14-9
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";
@@ -30,16 +34,13 @@ import {
3034
registerAllowUnauthorizedDispatcher,
3135
} from "./http";
3236
import {
33-
CacheStorage,
3437
D1_PLUGIN_NAME,
3538
DURABLE_OBJECTS_PLUGIN_NAME,
3639
DispatchFetch,
3740
DurableObjectClassNames,
38-
DurableObjectNamespace,
3941
GatewayConstructor,
4042
GatewayFactory,
4143
HEADER_CF_BLOB,
42-
KVNamespace,
4344
KV_PLUGIN_NAME,
4445
PLUGIN_ENTRIES,
4546
Persistence,
@@ -49,8 +50,8 @@ import {
4950
QUEUES_PLUGIN_NAME,
5051
QueueConsumers,
5152
QueuesError,
52-
R2Bucket,
5353
R2_PLUGIN_NAME,
54+
ReplaceWorkersTypes,
5455
SharedOptions,
5556
WorkerOptions,
5657
getGlobalServices,
@@ -1197,23 +1198,24 @@ export class Miniflare {
11971198
return proxy as T;
11981199
}
11991200
// TODO(someday): would be nice to define these in plugins
1200-
async getCaches(): Promise<CacheStorage> {
1201+
async getCaches(): Promise<ReplaceWorkersTypes<CacheStorage>> {
12011202
const proxyClient = await this._getProxyClient();
1202-
return proxyClient.global.caches as unknown as CacheStorage;
1203+
return proxyClient.global
1204+
.caches as unknown as ReplaceWorkersTypes<CacheStorage>;
12031205
}
12041206
getD1Database(bindingName: string, workerName?: string): Promise<D1Database> {
12051207
return this.#getProxy(D1_PLUGIN_NAME, bindingName, workerName);
12061208
}
12071209
getDurableObjectNamespace(
12081210
bindingName: string,
12091211
workerName?: string
1210-
): Promise<DurableObjectNamespace> {
1212+
): Promise<ReplaceWorkersTypes<DurableObjectNamespace>> {
12111213
return this.#getProxy(DURABLE_OBJECTS_PLUGIN_NAME, bindingName, workerName);
12121214
}
12131215
getKVNamespace(
12141216
bindingName: string,
12151217
workerName?: string
1216-
): Promise<KVNamespace> {
1218+
): Promise<ReplaceWorkersTypes<KVNamespace>> {
12171219
return this.#getProxy(KV_PLUGIN_NAME, bindingName, workerName);
12181220
}
12191221
getQueueProducer<Body = unknown>(
@@ -1222,7 +1224,10 @@ export class Miniflare {
12221224
): Promise<Queue<Body>> {
12231225
return this.#getProxy(QUEUES_PLUGIN_NAME, bindingName, workerName);
12241226
}
1225-
getR2Bucket(bindingName: string, workerName?: string): Promise<R2Bucket> {
1227+
getR2Bucket(
1228+
bindingName: string,
1229+
workerName?: string
1230+
): Promise<ReplaceWorkersTypes<R2Bucket>> {
12261231
return this.#getProxy(R2_PLUGIN_NAME, bindingName, workerName);
12271232
}
12281233

0 commit comments

Comments
 (0)