Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Miniflare 3] ✨ Implement *magic* proxy and add back support for Miniflare#get*() methods #639

Merged
merged 14 commits into from
Aug 17, 2023
Merged
4 changes: 4 additions & 0 deletions ava.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ const rewritePaths = Object.fromEntries(
export default {
files: ["packages/*/test/**/*.spec.ts"],
nodeArguments: ["--no-warnings", "--experimental-vm-modules"],
require: ["./packages/miniflare/test/setup.mjs"],
workerThreads: inspector.url() === undefined,
typescript: {
compile: false,
rewritePaths,
},
environmentVariables: {
MINIFLARE_ASSERT_BODIES_CONSUMED: "true",
},
};
27 changes: 9 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"lint:fix": "npm run lint -- --fix",
"prepublishOnly": "npm run lint && npm run clean && npm run build && npm run types:bundle && npm run test",
"release": "./scripts/release.sh",
"test": "npm run build && ava && rimraf ./.tmp",
"test": "npm run build && ava --serial && rimraf ./.tmp",
"types:build": "tsc && tsc -p packages/miniflare/src/workers/tsconfig.json",
"types:bundle": "npm run types:build && node scripts/types.mjs"
},
Expand Down
60 changes: 58 additions & 2 deletions packages/miniflare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,9 @@ defined at the top-level.

Updates the configuration for this Miniflare instance and restarts the
`workerd` server. Note unlike Miniflare 2, this does _not_ merge the new
configuration with the old configuration.
configuration with the old configuration. Note that calling this function will
invalidate any existing values returned by the `Miniflare#get*()` methods,
preventing them from being used.

- `ready: Promise<URL>`

Expand All @@ -592,7 +594,61 @@ defined at the top-level.
no need to do that yourself first. Additionally, the host of the request's URL
is always ignored and replaced with the `workerd` server's.

- `getBindings<Env extends Record<string, unknown> = Record<string, unknown>>(workerName?: string): Promise<Env>`

Returns a `Promise` that resolves with a record mapping binding names to
bindings, for all bindings in the Worker with the specified `workerName`. If
`workerName` is not specified, defaults to the entrypoint Worker.

- `getCaches(): Promise<CacheStorage>`

Returns a `Promise` that resolves with the
[`CacheStorage`](https://developers.cloudflare.com/workers/runtime-apis/cache/)
instance of the entrypoint Worker. This means if `cache: false` is set on the
entrypoint, calling methods on the resolved value won't do anything.

- `getD1Database(bindingName: string, workerName?: string): Promise<D1Database>`

Returns a `Promise` that resolves with the
[`D1Database`](https://developers.cloudflare.com/d1/platform/client-api/)
instance corresponding to the specified `bindingName` of `workerName`. Note
`bindingName` must not begin with `__D1_BETA__`. If `workerName` is not
specified, defaults to the entrypoint Worker.

- `getDurableObjectNamespace(bindingName: string, workerName?: string): Promise<DurableObjectNamespace>`

Returns a `Promise` that resolves with the
[`DurableObjectNamespace`](https://developers.cloudflare.com/workers/runtime-apis/durable-objects/#access-a-durable-object-from-a-worker)
instance corresponding to the specified `bindingName` of `workerName`. If
`workerName` is not specified, defaults to the entrypoint Worker.

- `getKVNamespace(bindingName: string, workerName?: string): Promise<KVNamespace>`

Returns a `Promise` that resolves with the
[`KVNamespace`](https://developers.cloudflare.com/workers/runtime-apis/kv/)
instance corresponding to the specified `bindingName` of `workerName`. If
`workerName` is not specified, defaults to the entrypoint Worker.

- `getQueueProducer<Body>(bindingName: string, workerName?: string): Promise<Queue<Body>>`

Returns a `Promise` that resolves with the
[`Queue`](https://developers.cloudflare.com/queues/platform/javascript-apis/)
producer instance corresponding to the specified `bindingName` of
`workerName`. If `workerName` is not specified, defaults to the entrypoint
Worker.

- `getR2Bucket(bindingName: string, workerName?: string): Promise<R2Bucket>`

Returns a `Promise` that resolves with the
[`R2Bucket`](https://developers.cloudflare.com/r2/api/workers/workers-api-reference/)
producer instance corresponding to the specified `bindingName` of
`workerName`. If `workerName` is not specified, defaults to the entrypoint
Worker.

- `dispose(): Promise<void>`

Cleans up the Miniflare instance, and shuts down the `workerd` server. Note
that after this is called, `setOptions` and `dispatchFetch` cannot be called.
that after this is called, `Miniflare#setOptions()` and
`Miniflare#dispatchFetch()` cannot be called. Additionally, calling this
function will invalidate any values returned by the `Miniflare#get*()`
methods, preventing them from being used.
3 changes: 1 addition & 2 deletions packages/miniflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@
"glob-to-regexp": "^0.4.1",
"http-cache-semantics": "^4.1.0",
"kleur": "^4.1.5",
"set-cookie-parser": "^2.6.0",
"source-map-support": "0.5.21",
"stoppable": "^1.1.0",
"undici": "^5.13.0",
"undici": "^5.22.1",
"workerd": "1.20230814.1",
"ws": "^8.11.0",
"youch": "^3.2.2",
Expand Down
4 changes: 3 additions & 1 deletion packages/miniflare/src/http/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ export class Request<
// error.
[kCf]?: CfType;

constructor(input: RequestInfo, init?: RequestInit<CfType>) {
constructor(input: RequestInfo | Request, init?: RequestInit<CfType>) {
super(input, init);
this[kCf] = init?.cf;
// Prefer `cf` from `init`, but if it's set on `input`, use that
if (input instanceof Request) this[kCf] ??= input.cf;
}

get cf() {
Expand Down
Loading