Skip to content

Commit

Permalink
expose cf object from Miniflare instances (#4905)
Browse files Browse the repository at this point in the history
* expose cf object from Miniflare instances

---------

Co-authored-by: MrBBot <bcoll@cloudflare.com>
  • Loading branch information
dario-piotrowicz and mrbbot committed Feb 5, 2024
1 parent fcbde34 commit 148feff
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
21 changes: 21 additions & 0 deletions .changeset/gentle-chairs-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
"miniflare": minor
---

feature: add a `getCf` method to Miniflare instances

add a new `getCf` method attached to instances of `Miniflare`, this `getCf` returns
the `cf` object that the Miniflare instance provides to the actual workers and it
depends of the core option of the same name

Example:

```ts
import { Miniflare } from "miniflare";

const mf = new Miniflare({ ... });

const cf = await mf.getCf();

console.log(`country = ${cf.country} ; colo = ${cf.colo}`); // logs 'country = GB ; colo = LHR'
```
6 changes: 5 additions & 1 deletion packages/miniflare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ parameter in module format Workers.
await this.STORE.fetch(this.baseURL + key, { method: "DELETE" });
}
}
// env has the type { STORE: Fetcher, NAMESPACE?: string }
export default function (env) {
return new MiniKV(env);
Expand Down Expand Up @@ -770,3 +770,7 @@ defined at the top-level.
`Miniflare#dispatchFetch()` cannot be called. Additionally, calling this
function will invalidate any values returned by the `Miniflare#get*()`
methods, preventing them from being used.

- `getCf(): Promise<Record<string, any>>`

Returns the same object returned from incoming `Request`'s `cf` property. This object depends on the `cf` property from `SharedOptions`.
10 changes: 10 additions & 0 deletions packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,8 @@ export class Miniflare {
#runtimeDispatcher?: Dispatcher;
#proxyClient?: ProxyClient;

#cfObject?: Record<string, any> = {};

// Path to temporary directory for use as scratch space/"in-memory" Durable
// Object storage. Note this may not exist, it's up to the consumers to
// create this if needed. Deleted on `dispose()`.
Expand Down Expand Up @@ -962,6 +964,7 @@ export class Miniflare {
const sharedOpts = this.#sharedOpts;

sharedOpts.core.cf = await setupCf(this.#log, sharedOpts.core.cf);
this.#cfObject = sharedOpts.core.cf;

const durableObjectClassNames = getDurableObjectClassNames(allWorkerOpts);
const wrappedBindingNames = getWrappedBindingNames(
Expand Down Expand Up @@ -1324,6 +1327,13 @@ export class Miniflare {
return this.#waitForReady();
}

async getCf(): Promise<Record<string, any>> {
this.#checkDisposed();
await this.ready;

return JSON.parse(JSON.stringify(this.#cfObject));
}

async getInspectorURL(): Promise<URL> {
this.#checkDisposed();
await this.ready;
Expand Down
33 changes: 29 additions & 4 deletions packages/miniflare/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,17 +733,16 @@ test("Miniflare: python modules", async (t) => {
{
type: "PythonModule",
path: "index",
contents: "from test_module import add; from js import Response;\ndef fetch(request):\n return Response.new(add(2,2))",
contents:
"from test_module import add; from js import Response;\ndef fetch(request):\n return Response.new(add(2,2))",
},
{
type: "PythonModule",
path: "test_module",
contents: `def add(a, b):\n return a + b`,
},
],
compatibilityFlags: [
"experimental"
]
compatibilityFlags: ["experimental"],
});
t.teardown(() => mf.dispose());
const res = await mf.dispatchFetch("http://localhost");
Expand Down Expand Up @@ -1736,3 +1735,29 @@ test("Miniflare: prohibits invalid wrapped bindings", async (t) => {
}
);
});

test("Miniflare: getCf() returns a standard cf object", async (t) => {
const mf = new Miniflare({ script: "", modules: true });
t.teardown(() => mf.dispose());

const cf = await mf.getCf();
t.like(cf, {
colo: "DFW",
city: "Austin",
regionCode: "TX",
});
});

test("Miniflare: getCf() returns a user provided cf object", async (t) => {
const mf = new Miniflare({
script: "",
modules: true,
cf: {
myFakeField: "test",
},
});
t.teardown(() => mf.dispose());

const cf = await mf.getCf();
t.deepEqual(cf, { myFakeField: "test" });
});

0 comments on commit 148feff

Please sign in to comment.