-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic support for
wrappedBindings
and RPC entrypoints to Minifl…
…are's Magic Proxy (#5599) * [miniflare] fix: add support for wrapped bindings in magic proxy * [wrangler] chore: add an e2e test for AI bindings with `getPlatformProxy()` * [miniflare] fix: add partial support for RPC in magic proxy * skip `getPlatformProxy()` wrangler e2e --------- Co-authored-by: Dario Piotrowicz <dario@cloudflare.com>
- Loading branch information
1 parent
8f470d9
commit c9f081a
Showing
26 changed files
with
406 additions
and
70 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,48 @@ | ||
--- | ||
"miniflare": patch | ||
--- | ||
|
||
fix: add support for wrapped bindings in magic proxy | ||
|
||
currently `Miniflare#getBindings()` does not return proxies to provided `wrappedBindings`, make sure that appropriate proxies are instead returned | ||
|
||
Example: | ||
|
||
```ts | ||
import { Miniflare } from "miniflare"; | ||
|
||
const mf = new Miniflare({ | ||
workers: [ | ||
{ | ||
wrappedBindings: { | ||
Greeter: { | ||
scriptName: "impl", | ||
}, | ||
}, | ||
modules: true, | ||
script: `export default { fetch(){ return new Response(''); } }`, | ||
}, | ||
{ | ||
modules: true, | ||
name: "impl", | ||
script: ` | ||
class Greeter { | ||
sayHello(name) { | ||
return "Hello " + name; | ||
} | ||
} | ||
export default function (env) { | ||
return new Greeter(); | ||
} | ||
`, | ||
}, | ||
], | ||
}); | ||
|
||
const { Greeter } = await mf.getBindings(); | ||
|
||
console.log(Greeter.sayHello("world")); // <--- prints 'Hello world' | ||
|
||
await mf.dispose(); | ||
``` |
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,51 @@ | ||
--- | ||
"miniflare": patch | ||
--- | ||
|
||
fix: add support for RPC in magic proxy | ||
|
||
currently `Miniflare#getBindings()` does not return valid proxies to provided `serviceBindings` using RPC, make sure that appropriate proxies are instead returned | ||
|
||
Example: | ||
|
||
```ts | ||
import { Miniflare } from "miniflare"; | ||
|
||
const mf = new Miniflare({ | ||
workers: [ | ||
{ | ||
modules: true, | ||
script: `export default { fetch() { return new Response(''); } }`, | ||
serviceBindings: { | ||
SUM: { | ||
name: "sum-worker", | ||
entrypoint: "SumEntrypoint", | ||
}, | ||
}, | ||
}, | ||
{ | ||
modules: true, | ||
name: "sum-worker", | ||
script: ` | ||
import { WorkerEntrypoint } from 'cloudflare:workers'; | ||
export default { fetch() { return new Response(''); } } | ||
export class SumEntrypoint extends WorkerEntrypoint { | ||
sum(args) { | ||
return args.reduce((a, b) => a + b); | ||
} | ||
} | ||
`, | ||
}, | ||
], | ||
}); | ||
|
||
const { SUM } = await mf.getBindings(); | ||
|
||
const numbers = [1, 2, 3]; | ||
|
||
console.log(`The sum of ${numbers.join(", ")} is ${await SUM.sum(numbers)}`); // <--- prints 'The sum of 1, 2, 3 is 6' | ||
|
||
await mf.dispose(); | ||
``` |
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,7 @@ | ||
{ | ||
"extends": "@cloudflare/workers-tsconfig/tsconfig.json", | ||
"compilerOptions": { | ||
"types": ["node"] | ||
}, | ||
"include": ["*.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 |
---|---|---|
@@ -1,16 +1,9 @@ | ||
{ | ||
"include": [ | ||
"workers/hello-worker-a", | ||
"module-worker-b", | ||
"service-worker-a", | ||
"module-worker-c", | ||
"module-worker-d", | ||
"pages-functions-app" | ||
], | ||
"include": ["workers/*/*.ts"], | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"module": "CommonJS", | ||
"lib": ["ES2020"], | ||
"types": ["@cloudflare/workers-types"] | ||
"types": ["@cloudflare/workers-types/experimental"] | ||
} | ||
} |
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,37 @@ | ||
import { RpcTarget, WorkerEntrypoint } from "cloudflare:workers"; | ||
|
||
export default { | ||
async fetch(request: Request, env: Record<string, unknown>, ctx: unknown) { | ||
throw new Error( | ||
"Worker only used for RPC calls, there's no default fetch handler" | ||
); | ||
}, | ||
}; | ||
|
||
export class NamedEntrypoint extends WorkerEntrypoint { | ||
sum(args: number[]): number { | ||
return args.reduce((a, b) => a + b); | ||
} | ||
asJsonResponse(args: unknown): { | ||
status: number; | ||
text: () => Promise<string>; | ||
} { | ||
return Response.json(args); | ||
} | ||
getCounter() { | ||
return new Counter(); | ||
} | ||
} | ||
|
||
class Counter extends RpcTarget { | ||
#value = 0; | ||
|
||
increment(amount: number) { | ||
this.#value += amount; | ||
return this.#value; | ||
} | ||
|
||
get value() { | ||
return this.#value; | ||
} | ||
} |
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 @@ | ||
name = "rpc-worker" |
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
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
Oops, something went wrong.