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

Commit 6498024

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 920cf61 commit 6498024

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
SourceMapRegistry,
5657
WorkerOptions,
@@ -1207,23 +1208,24 @@ export class Miniflare {
12071208
return proxy as T;
12081209
}
12091210
// TODO(someday): would be nice to define these in plugins
1210-
async getCaches(): Promise<CacheStorage> {
1211+
async getCaches(): Promise<ReplaceWorkersTypes<CacheStorage>> {
12111212
const proxyClient = await this._getProxyClient();
1212-
return proxyClient.global.caches as unknown as CacheStorage;
1213+
return proxyClient.global
1214+
.caches as unknown as ReplaceWorkersTypes<CacheStorage>;
12131215
}
12141216
getD1Database(bindingName: string, workerName?: string): Promise<D1Database> {
12151217
return this.#getProxy(D1_PLUGIN_NAME, bindingName, workerName);
12161218
}
12171219
getDurableObjectNamespace(
12181220
bindingName: string,
12191221
workerName?: string
1220-
): Promise<DurableObjectNamespace> {
1222+
): Promise<ReplaceWorkersTypes<DurableObjectNamespace>> {
12211223
return this.#getProxy(DURABLE_OBJECTS_PLUGIN_NAME, bindingName, workerName);
12221224
}
12231225
getKVNamespace(
12241226
bindingName: string,
12251227
workerName?: string
1226-
): Promise<KVNamespace> {
1228+
): Promise<ReplaceWorkersTypes<KVNamespace>> {
12271229
return this.#getProxy(KV_PLUGIN_NAME, bindingName, workerName);
12281230
}
12291231
getQueueProducer<Body = unknown>(
@@ -1232,7 +1234,10 @@ export class Miniflare {
12321234
): Promise<Queue<Body>> {
12331235
return this.#getProxy(QUEUES_PLUGIN_NAME, bindingName, workerName);
12341236
}
1235-
getR2Bucket(bindingName: string, workerName?: string): Promise<R2Bucket> {
1237+
getR2Bucket(
1238+
bindingName: string,
1239+
workerName?: string
1240+
): Promise<ReplaceWorkersTypes<R2Bucket>> {
12361241
return this.#getProxy(R2_PLUGIN_NAME, bindingName, workerName);
12371242
}
12381243

0 commit comments

Comments
 (0)