-
Notifications
You must be signed in to change notification settings - Fork 769
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Service bindings in Pages * Add env support + test * run prettify * update workers-types dependency in external service bindings fixture * update pnpm-lock file * fix copy-pasta in external-service-bindings fixture * fix and improve external-service-bindings-app fixture * add test for service environments * fix types in external-service-bindings-app fixture * add environment to service flag description * add changeset * Update fixtures/external-service-bindings-app/package.json Co-authored-by: Greg Brimble <gbrimble@cloudflare.com> * add missing newlines * tweak flag description * improve changeset * add warning about service binding env being experimental * only print the env warning once * fix bad wording * add comment for SERVICE_BINDING_REGEXP * remove unneeded jest section in package.json --------- Co-authored-by: Daniel Walsh <walshydev@gmail.com> Co-authored-by: Dario Piotrowicz <dario@cloudflare.com> Co-authored-by: Greg Brimble <gbrimble@cloudflare.com>
- Loading branch information
1 parent
980df04
commit a1f212e
Showing
22 changed files
with
376 additions
and
46 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,19 @@ | ||
--- | ||
"wrangler": minor | ||
--- | ||
|
||
add support for service bindings in `wrangler pages dev` by providing the | ||
new `--service`|`-s` flag which accepts an array of `BINDING_NAME=SCRIPT_NAME` | ||
where `BINDING_NAME` is the name of the binding and `SCRIPT_NAME` is the name | ||
of the worker (as defined in its `wrangler.toml`), such workers need to be | ||
running locally with with `wrangler dev`. | ||
|
||
For example if a user has a worker named `worker-a`, in order to locally bind | ||
to that they'll need to open two different terminals, in each navigate to the | ||
respective worker/pages application and then run respectively `wrangler dev` and | ||
`wrangler pages ./publicDir --service MY_SERVICE=worker-a` this will add the | ||
`MY_SERVICE` binding to pages' worker `env` object. | ||
|
||
Note: additionally after the `SCRIPT_NAME` the name of an environment can be specified, | ||
prefixed by an `@` (as in: `MY_SERVICE=SCRIPT_NAME@PRODUCTION`), this behavior is however | ||
experimental and not fully properly defined. |
5 changes: 5 additions & 0 deletions
5
fixtures/external-service-bindings-app/module-worker-a/index.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,5 @@ | ||
export default { | ||
fetch() { | ||
return new Response("Hello from module worker a"); | ||
}, | ||
}; |
1 change: 1 addition & 0 deletions
1
fixtures/external-service-bindings-app/module-worker-a/wrangler.toml
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 = "module-worker-a" |
9 changes: 9 additions & 0 deletions
9
fixtures/external-service-bindings-app/module-worker-b/index.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,9 @@ | ||
export default { | ||
async fetch(request: Request, env: { SERVICE: Fetcher }) { | ||
const serviceResp = await env.SERVICE.fetch(request); | ||
const serviceRespTxt = await serviceResp.text(); | ||
return new Response( | ||
`Hello from module worker b and also: ${serviceRespTxt}` | ||
); | ||
}, | ||
}; |
5 changes: 5 additions & 0 deletions
5
fixtures/external-service-bindings-app/module-worker-b/wrangler.toml
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,5 @@ | ||
name = "module-worker-b" | ||
|
||
services = [ | ||
{ binding = "SERVICE", service = "module-worker-a" } | ||
] |
5 changes: 5 additions & 0 deletions
5
fixtures/external-service-bindings-app/module-worker-c/index.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,5 @@ | ||
export default { | ||
fetch(req, env) { | ||
return new Response(`Hello from module worker c (${env.MY_ENV})`); | ||
}, | ||
}; |
7 changes: 7 additions & 0 deletions
7
fixtures/external-service-bindings-app/module-worker-c/wrangler.toml
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 = "module-worker-c" | ||
|
||
[env.staging.vars] | ||
MY_ENV = "staging" | ||
|
||
[env.production.vars] | ||
MY_ENV = "prod" |
5 changes: 5 additions & 0 deletions
5
fixtures/external-service-bindings-app/module-worker-d/index.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,5 @@ | ||
export default { | ||
fetch(req, env) { | ||
return new Response(`Hello from module worker d (${env.MY_ENV})`); | ||
}, | ||
}; |
7 changes: 7 additions & 0 deletions
7
fixtures/external-service-bindings-app/module-worker-d/wrangler.toml
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 = "module-worker-d" | ||
|
||
[env.staging.vars] | ||
MY_ENV = "staging" | ||
|
||
[env.production.vars] | ||
MY_ENV = "prod" |
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,25 @@ | ||
{ | ||
"name": "external-service-bindings-app", | ||
"private": true, | ||
"description": "A test for external service bindings", | ||
"scripts": { | ||
"mod-a-dev": "wrangler dev module-worker-a/index.ts --local --port 8500", | ||
"mod-b-dev": "wrangler dev module-worker-b/index.ts --local --port 8501", | ||
"ser-a-dev": "wrangler dev service-worker-a/index.ts --local --port 8502", | ||
"mod-c-dev": "wrangler dev module-worker-c/index.ts --local --port 8503 --env staging", | ||
"mod-d-dev": "wrangler dev module-worker-d/index.ts --local --port 8504 --env production", | ||
"pages-dev": "cd pages-functions-app && npx wrangler pages dev public --port 8505 --service MODULE_A_SERVICE=module-worker-a MODULE_B_SERVICE=module-worker-b SERVICE_A_SERVICE=service-worker-a STAGING_MODULE_C_SERVICE=module-worker-c@staging STAGING_MODULE_D_SERVICE=module-worker-d@staging", | ||
"dev": "npx concurrently -s first -k 'npm run mod-a-dev' 'npm run mod-b-dev' 'npm run ser-a-dev' 'npm run mod-c-dev' 'npm run mod-d-dev' 'npm run pages-dev'", | ||
"test": "npx vitest run", | ||
"test:ci": "npx vitest run", | ||
"test:watch": "npx vitest", | ||
"type:tests": "tsc --noEmit" | ||
}, | ||
"devDependencies": { | ||
"undici": "^5.23.0", | ||
"concurrently": "^8.2.1", | ||
"@cloudflare/workers-tsconfig": "workspace:*", | ||
"@cloudflare/workers-types": "^4.20221111.1", | ||
"wrangler": "workspace:*" | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
fixtures/external-service-bindings-app/pages-functions-app/functions/env.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,9 @@ | ||
export const onRequest = async ({ env, request }) => { | ||
const getTextFrom = (fetcher: Fetcher) => | ||
fetcher.fetch(request).then((resp) => resp.text()); | ||
|
||
return Response.json({ | ||
moduleWorkerCResponse: await getTextFrom(env.STAGING_MODULE_C_SERVICE), | ||
moduleWorkerDResponse: await getTextFrom(env.STAGING_MODULE_D_SERVICE), | ||
}); | ||
}; |
10 changes: 10 additions & 0 deletions
10
fixtures/external-service-bindings-app/pages-functions-app/functions/index.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,10 @@ | ||
export const onRequest = async ({ env, request }) => { | ||
const getTextFrom = (fetcher: Fetcher) => | ||
fetcher.fetch(request).then((resp) => resp.text()); | ||
|
||
return Response.json({ | ||
moduleWorkerAResponse: await getTextFrom(env.MODULE_A_SERVICE), | ||
moduleWorkerBResponse: await getTextFrom(env.MODULE_B_SERVICE), | ||
serviceWorkerAResponse: await getTextFrom(env.SERVICE_A_SERVICE), | ||
}); | ||
}; |
4 changes: 4 additions & 0 deletions
4
fixtures/external-service-bindings-app/pages-functions-app/package.json
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,4 @@ | ||
{ | ||
"name": "pages-functions-app", | ||
"private": true | ||
} |
1 change: 1 addition & 0 deletions
1
fixtures/external-service-bindings-app/pages-functions-app/public/index.html
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 @@ | ||
<h1>This will never be served.</h1> |
3 changes: 3 additions & 0 deletions
3
fixtures/external-service-bindings-app/service-worker-a/index.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,3 @@ | ||
addEventListener("fetch", (event) => { | ||
event.respondWith(new Response("Hello from service worker a")); | ||
}); |
2 changes: 2 additions & 0 deletions
2
fixtures/external-service-bindings-app/service-worker-a/wrangler.toml
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,2 @@ | ||
name = "service-worker-a" | ||
main = "src/index.ts" |
75 changes: 75 additions & 0 deletions
75
fixtures/external-service-bindings-app/tests/index.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,75 @@ | ||
import { spawn } from "child_process"; | ||
import * as path from "path"; | ||
import type { ChildProcess } from "child_process"; | ||
import { describe, expect, it, beforeAll, afterAll } from "vitest"; | ||
import { fetch, type Response } from "undici"; | ||
|
||
const waitUntilReady = async (url: string): Promise<Response> => { | ||
let response: Response | undefined = undefined; | ||
|
||
while (response === undefined) { | ||
await new Promise((resolvePromise) => setTimeout(resolvePromise, 500)); | ||
|
||
try { | ||
response = await fetch(url); | ||
} catch {} | ||
} | ||
|
||
return response as Response; | ||
}; | ||
|
||
const isWindows = process.platform === "win32"; | ||
|
||
describe("Pages Functions", () => { | ||
let wranglerProcess: ChildProcess; | ||
|
||
beforeAll(() => { | ||
wranglerProcess = spawn("npm", ["run", "dev"], { | ||
shell: isWindows, | ||
cwd: path.resolve(__dirname, "../"), | ||
env: { BROWSER: "none", ...process.env }, | ||
}); | ||
wranglerProcess.stdout?.on("data", (chunk) => { | ||
console.log(chunk.toString()); | ||
}); | ||
wranglerProcess.stderr?.on("data", (chunk) => { | ||
console.log(chunk.toString()); | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await new Promise((resolve, reject) => { | ||
wranglerProcess.once("exit", (code) => { | ||
if (!code) { | ||
resolve(code); | ||
} else { | ||
reject(code); | ||
} | ||
}); | ||
wranglerProcess.kill("SIGTERM"); | ||
}); | ||
}); | ||
|
||
it("connects up Workers (both module and service ones) and fetches from them", async () => { | ||
const combinedResponse = await waitUntilReady("http://localhost:8505/"); | ||
const json = await combinedResponse.json(); | ||
expect(json).toMatchInlineSnapshot(` | ||
{ | ||
"moduleWorkerAResponse": "Hello from module worker a", | ||
"moduleWorkerBResponse": "Hello from module worker b and also: Hello from module worker a", | ||
"serviceWorkerAResponse": "Hello from service worker a", | ||
} | ||
`); | ||
}); | ||
|
||
it("respects the environments specified for the service bindings (and doesn't connect if the env doesn't match)", async () => { | ||
const combinedResponse = await waitUntilReady("http://localhost:8505/env"); | ||
const json = await combinedResponse.json(); | ||
expect(json).toMatchInlineSnapshot(` | ||
{ | ||
"moduleWorkerCResponse": "Hello from module worker c (staging)", | ||
"moduleWorkerDResponse": "You should start up wrangler dev --local on the STAGING_MODULE_D_SERVICE 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"include": [ | ||
"module-worker-a", | ||
"module-worker-b", | ||
"service-worker-a", | ||
"module-worker-c", | ||
"module-worker-d", | ||
"pages-functions-app" | ||
], | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"module": "CommonJS", | ||
"lib": ["ES2020"], | ||
"types": ["@cloudflare/workers-types"] | ||
} | ||
} |
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.