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

chore: rename getBindingsProxy to getPlatformProxy #5002

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .changeset/strange-geckos-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"wrangler": patch
---

chore: rename `getBindingsProxy` to `getPlatformProxy`

initially `getBindingsProxy` was supposed to only provide proxies for bindings,
the utility has however grown, including now `cf`, `ctx` and `caches`, to
clarify the increased scope the utility is getting renamed to `getPlatformProxy`
and its `bindings` field is getting renamed `env`

_note_: `getBindingProxy` with its signature is still kept available, making this
a non breaking change
2 changes: 1 addition & 1 deletion fixtures/get-bindings-proxy/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "get-bindings-proxy-fixture",
"private": true,
"description": "A test for the getBindingsProxy utility",
"description": "A test for the getBindingsProxy (deprecated) utility",
"scripts": {
"test": "vitest run",
"test:watch": "vitest",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import {
R2Bucket,
} from "@cloudflare/workers-types";
import { describe, expect, it } from "vitest";
import {
getBindingsProxy as originalGetBindingsProxy,
unstable_dev,
} from "wrangler";
import { unstable_dev } from "wrangler";
import { getBindingsProxy } from "./shared";
import type { KVNamespace } from "@cloudflare/workers-types";
import type { GetBindingsProxyOptions, UnstableDevWorker } from "wrangler";
import type { UnstableDevWorker } from "wrangler";

type Bindings = {
MY_VAR: string;
Expand All @@ -30,17 +28,6 @@ type Bindings = {

const wranglerTomlFilePath = path.join(__dirname, "..", "wrangler.toml");

// Here we wrap the actual original getBindingsProxy function and disable its persistance, this is to make sure
// that we don't implement any persistance during these tests (which would add unnecessary extra complexity)
function getBindingsProxy<T>(
options: Omit<GetBindingsProxyOptions, "persist">
): ReturnType<typeof originalGetBindingsProxy<T>> {
return originalGetBindingsProxy({
...options,
persist: false,
});
}

describe("getBindingsProxy - bindings", () => {
let devWorkers: UnstableDevWorker[];

Expand Down
3 changes: 3 additions & 0 deletions fixtures/get-platform-proxy/.dev.vars
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MY_DEV_VAR = "my-dev-var-value"
MY_VAR_A = "my-dev-var-a"
MY_KV = "my-dev-kv"
1 change: 1 addition & 0 deletions fixtures/get-platform-proxy/custom-toml/path/.dev.vars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MY_DEV_VAR = "my-dev-var-value-from-a-custom-location"
7 changes: 7 additions & 0 deletions fixtures/get-platform-proxy/custom-toml/path/test-toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name = "get-bindings-proxy-fixture"
main = "src/index.ts"
compatibility_date = "2023-11-21"

[vars]
MY_VAR = "my-var-value-from-a-custom-toml"
MY_JSON_VAR = { test = true, customToml = true }
16 changes: 16 additions & 0 deletions fixtures/get-platform-proxy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "get-platform-proxy-fixture",
"private": true,
"description": "A test for the getPlatformProxy utility",
"scripts": {
"test": "vitest run",
"test:watch": "vitest",
"type:tests": "tsc --noEmit"
},
"devDependencies": {
"@cloudflare/workers-tsconfig": "workspace:*",
"@cloudflare/workers-types": "^4.20221111.1",
"wrangler": "workspace:*",
"undici": "^5.28.2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Request, Response } from "undici";
import { describe, expect, it } from "vitest";
import { getPlatformProxy } from "./shared";

describe("getPlatformProxy - caches", () => {
(["default", "named"] as const).forEach((cacheType) =>
it(`correctly obtains a no-op ${cacheType} cache`, async () => {
const { caches, dispose } = await getPlatformProxy();
try {
const cache =
cacheType === "default"
? caches.default
: await caches.open("my-cache");
testNoOpCache(cache);
} finally {
await dispose();
}
})
);
});

async function testNoOpCache(
cache: Awaited<ReturnType<typeof getPlatformProxy>>["caches"]["default"]
) {
let match = await cache.match("http://0.0.0.0/test");
expect(match).toBeUndefined();

const req = new Request("http://0.0.0.0/test");
await cache.put(req, new Response("test"));
const resp = await cache.match(req);
expect(resp).toBeUndefined();
const deleted = await cache.delete(req);
expect(deleted).toBe(false);
}
43 changes: 43 additions & 0 deletions fixtures/get-platform-proxy/tests/get-platform-proxy.cf.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, expect, it } from "vitest";
import { getPlatformProxy } from "./shared";

describe("getPlatformProxy - cf", () => {
it("should provide mock data", async () => {
const { cf, dispose } = await getPlatformProxy();
try {
expect(cf).toMatchObject({
colo: "DFW",
city: "Austin",
regionCode: "TX",
});
} finally {
await dispose();
}
});

it("should match the production runtime cf object", async () => {
const { cf, dispose } = await getPlatformProxy();
try {
expect(cf.constructor.name).toBe("Object");

expect(() => {
cf.city = "test city";
}).toThrowError(
"Cannot assign to read only property 'city' of object '#<Object>'"
);
expect(cf.city).not.toBe("test city");

expect(() => {
cf.newField = "test new field";
}).toThrowError("Cannot add property newField, object is not extensible");
expect("newField" in cf).toBe(false);

expect(cf.botManagement).toMatchObject({
score: 99,
});
expect(Object.isFrozen(cf.botManagement)).toBe(true);
} finally {
await dispose();
}
});
});
55 changes: 55 additions & 0 deletions fixtures/get-platform-proxy/tests/get-platform-proxy.ctx.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { describe, expect, it } from "vitest";
import { getPlatformProxy } from "./shared";

describe("getPlatformProxy - ctx", () => {
it("should provide a no-op waitUntil method", async () => {
const { ctx, dispose } = await getPlatformProxy();
try {
let value = 4;
ctx.waitUntil(
new Promise((resolve) => {
value++;
resolve(value);
})
);
expect(value).toBe(5);
} finally {
await dispose();
}
});

it("should provide a no-op passThroughOnException method", async () => {
const { ctx, dispose } = await getPlatformProxy();
try {
expect(ctx.passThroughOnException()).toBe(undefined);
} finally {
await dispose();
}
});

it("should match the production runtime ctx object", async () => {
const { ctx, dispose } = await getPlatformProxy();
try {
expect(ctx.constructor.name).toBe("ExecutionContext");
expect(typeof ctx.waitUntil).toBe("function");
expect(typeof ctx.passThroughOnException).toBe("function");

ctx.waitUntil = ((str: string) => `- ${str} -`) as any;
expect(ctx.waitUntil("waitUntil can be overridden" as any)).toBe(
"- waitUntil can be overridden -"
);

ctx.passThroughOnException = ((str: string) => `_ ${str} _`) as any;
expect(
(ctx.passThroughOnException as any)(
"passThroughOnException can be overridden"
)
).toBe("_ passThroughOnException can be overridden _");

(ctx as any).text = "the ExecutionContext can be extended";
expect((ctx as any).text).toBe("the ExecutionContext can be extended");
} finally {
await dispose();
}
});
});
Loading
Loading