-
Notifications
You must be signed in to change notification settings - Fork 702
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: rename
getBindingsProxy
to getPlatformProxy
- Loading branch information
1 parent
7ad8ddd
commit 110dc12
Showing
32 changed files
with
641 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
MY_DEV_VAR = "my-dev-var-value-from-a-custom-location" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
fixtures/get-platform-proxy/tests/get-platform-proxy.caches.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
fixtures/get-platform-proxy/tests/get-platform-proxy.cf.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
55
fixtures/get-platform-proxy/tests/get-platform-proxy.ctx.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.