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

feat: add browser render local dev #6332

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions fixtures/browser-rendering/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
dist
23 changes: 23 additions & 0 deletions fixtures/browser-rendering/package.json
Original file line number Diff line number Diff line change
@@ -0,0 1,23 @@
{
"name": "browser-rendering-app",
"private": true,
"description": "",
"license": "ISC",
"author": "",
"main": "src/index.js",
"scripts": {
"check:type": "tsc",
"test:ci": "vitest run",
"test:watch": "vitest",
"type:tests": "tsc -p ./tests/tsconfig.json"
},
"devDependencies": {
"@cloudflare/puppeteer": "^0.0.11",
"@cloudflare/workers-tsconfig": "workspace:^",
"undici": "^5.28.4",
"wrangler": "workspace:*"
},
"volta": {
"extends": "../../package.json"
}
}
10 changes: 10 additions & 0 deletions fixtures/browser-rendering/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 1,10 @@

export default {
async fetch(request, env) {
console.log(env.BROWSER)
return Response.json({
binding: env.BROWSER,
fetch: typeof env.BROWSER,
});
},
};
42 changes: 42 additions & 0 deletions fixtures/browser-rendering/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 1,42 @@
import { resolve } from "path";
import { fetch } from "undici";
import { afterAll, beforeAll, describe, it } from "vitest";
import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived";

describe("'wrangler dev' lauches a browser", () => {
let ip: string,
port: number,
stop: (() => Promise<unknown>) | undefined,
getOutput: () => string;

beforeAll(async () => {
({ ip, port, stop, getOutput } = await runWranglerDev(
resolve(__dirname, ".."),
["--local", "--port=0", "--inspector-port=0"]
));


});

afterAll(async () => {
await stop?.();
});

it("borwser binding methods ", async ({ expect }) => {
const response = await fetch(`http://${ip}:${port}/`);
// console.log(await response.text())
const content = await response.json();
expect((content as Record<string, object>).fetch).toEqual("function");

Check failure on line 29 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (macos-13, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > borwser binding methods

AssertionError: expected 'undefined' to deeply equal 'function' - Expected Received - function undefined ❯ tests/index.test.ts:29:53

Check failure on line 29 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (macos-13, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > borwser binding methods

AssertionError: expected 'undefined' to deeply equal 'function' - Expected Received - function undefined ❯ tests/index.test.ts:29:53

Check failure on line 29 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (macos-13, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > borwser binding methods

AssertionError: expected 'undefined' to deeply equal 'function' - Expected Received - function undefined ❯ tests/index.test.ts:29:53

Check failure on line 29 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > borwser binding methods

AssertionError: expected 'undefined' to deeply equal 'function' - Expected Received - function undefined ❯ tests/index.test.ts:29:53

Check failure on line 29 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > borwser binding methods

AssertionError: expected 'undefined' to deeply equal 'function' - Expected Received - function undefined ❯ tests/index.test.ts:29:53

Check failure on line 29 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > borwser binding methods

AssertionError: expected 'undefined' to deeply equal 'function' - Expected Received - function undefined ❯ tests/index.test.ts:29:53

Check failure on line 29 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > borwser binding methods

AssertionError: expected 'undefined' to deeply equal 'function' - Expected Received - function undefined ❯ tests/index.test.ts:29:53

Check failure on line 29 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > borwser binding methods

AssertionError: expected 'undefined' to deeply equal 'function' - Expected Received - function undefined ❯ tests/index.test.ts:29:53

Check failure on line 29 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > borwser binding methods

AssertionError: expected 'undefined' to deeply equal 'function' - Expected Received - function undefined ❯ tests/index.test.ts:29:53
});

it("browser rendering binding properties", async ({ expect }) => {
const response = await fetch(`http://${ip}:${port}/`);
const content = await response.json();
expect((content as Record<string, object>).binding).toEqual({

Check failure on line 35 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (macos-13, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > browser rendering binding properties

AssertionError: expected undefined to deeply equal { fetcher: {}, …(3) } - Expected: Object { "fetcher": Object {}, "lastRequestId": null, "logs": Array [], "options": Object {}, } Received: undefined ❯ tests/index.test.ts:35:55

Check failure on line 35 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (macos-13, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > browser rendering binding properties

AssertionError: expected undefined to deeply equal { fetcher: {}, …(3) } - Expected: Object { "fetcher": Object {}, "lastRequestId": null, "logs": Array [], "options": Object {}, } Received: undefined ❯ tests/index.test.ts:35:55

Check failure on line 35 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (macos-13, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > browser rendering binding properties

AssertionError: expected undefined to deeply equal { fetcher: {}, …(3) } - Expected: Object { "fetcher": Object {}, "lastRequestId": null, "logs": Array [], "options": Object {}, } Received: undefined ❯ tests/index.test.ts:35:55

Check failure on line 35 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > browser rendering binding properties

AssertionError: expected undefined to deeply equal { fetcher: {}, …(3) } - Expected: Object { "fetcher": Object {}, "lastRequestId": null, "logs": Array [], "options": Object {}, } Received: undefined ❯ tests/index.test.ts:35:55

Check failure on line 35 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > browser rendering binding properties

AssertionError: expected undefined to deeply equal { fetcher: {}, …(3) } - Expected: Object { "fetcher": Object {}, "lastRequestId": null, "logs": Array [], "options": Object {}, } Received: undefined ❯ tests/index.test.ts:35:55

Check failure on line 35 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > browser rendering binding properties

AssertionError: expected undefined to deeply equal { fetcher: {}, …(3) } - Expected: Object { "fetcher": Object {}, "lastRequestId": null, "logs": Array [], "options": Object {}, } Received: undefined ❯ tests/index.test.ts:35:55

Check failure on line 35 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > browser rendering binding properties

AssertionError: expected undefined to deeply equal { fetcher: {}, …(3) } - Expected: Object { "fetcher": Object {}, "lastRequestId": null, "logs": Array [], "options": Object {}, } Received: undefined ❯ tests/index.test.ts:35:55

Check failure on line 35 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > browser rendering binding properties

AssertionError: expected undefined to deeply equal { fetcher: {}, …(3) } - Expected: Object { "fetcher": Object {}, "lastRequestId": null, "logs": Array [], "options": Object {}, } Received: undefined ❯ tests/index.test.ts:35:55

Check failure on line 35 in fixtures/browser-rendering/tests/index.test.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, ./fixtures/*)

tests/index.test.ts > 'wrangler dev' lauches a browser > browser rendering binding properties

AssertionError: expected undefined to deeply equal { fetcher: {}, …(3) } - Expected: Object { "fetcher": Object {}, "lastRequestId": null, "logs": Array [], "options": Object {}, } Received: undefined ❯ tests/index.test.ts:35:55
fetcher: {},
lastRequestId: null,
logs: [],
options: {},
});
});
});
7 changes: 7 additions & 0 deletions fixtures/browser-rendering/tests/tsconfig.json
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", "../../../node-types.d.ts"]
}
13 changes: 13 additions & 0 deletions fixtures/browser-rendering/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 1,13 @@
{
"compilerOptions": {
"target": "ES2020",
"esModuleInterop": true,
"module": "CommonJS",
"lib": ["ES2020"],
"types": ["node"],
"skipLibCheck": true,
"moduleResolution": "node",
"noEmit": true
},
"include": ["tests", "../../node-types.d.ts"]
}
9 changes: 9 additions & 0 deletions fixtures/browser-rendering/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 1,9 @@
import { defineProject, mergeConfig } from "vitest/config";
import configShared from "../../vitest.shared";

export default mergeConfig(
configShared,
defineProject({
test: {},
})
);
9 changes: 9 additions & 0 deletions fixtures/browser-rendering/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 1,9 @@
name = "browser-rendering-app"
compatibility_date = "2024-06-20"

compatibility_flags = ["nodejs_compat"]
main = "src/index.js"


[browser]
binding = "BROWSER"
3 changes: 3 additions & 0 deletions packages/wrangler/src/api/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 58,9 @@ export interface UnstableDevOptions {
ai?: {
binding: string;
};
browser?: {
binding: string;
}
version_metadata?: {
binding: string;
};
Expand Down
21 changes: 21 additions & 0 deletions packages/wrangler/src/browser-rendering/fetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 1,21 @@
import { Headers, Response, fetch } from "miniflare";
import type { Request } from "miniflare";

export const WORKERS_DEV_ENDPOINT = "https://staging.api.browser.run";



export async function BrowserFetcher(request: Request): Promise<Response> {

request.headers.set("Host", WORKERS_DEV_ENDPOINT)

const res = await fetch(request.url, {
method: request.method,
headers: Object.fromEntries(request.headers.entries()),
body: request.body,
});

const respHeaders = new Headers(res.headers);

return new Response(res.body, { status: res.status, headers: respHeaders });
}
1 change: 1 addition & 0 deletions packages/wrangler/src/browser-rendering/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
export * from './fetcher'
5 changes: 5 additions & 0 deletions packages/wrangler/src/dev/miniflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 46,7 @@ import type { MiniflareOptions, SourceOptions, WorkerOptions } from "miniflare";
import type { UUID } from "node:crypto";
import type { Abortable } from "node:events";
import type { Readable } from "node:stream";
import { BrowserFetcher } from "../browser-rendering/fetcher";

// This worker proxies all external Durable Objects to the Wrangler session
// where they're defined, and receives all requests from other Wrangler sessions
Expand Down Expand Up @@ -587,6 588,10 @@ export function buildMiniflareBindingOptions(config: MiniflareBindingsConfig): {
};
}

if (bindings.browser?.binding) {
config.serviceBindings[bindings.browser.binding] = BrowserFetcher;
}

const bindingOptions = {
bindings: {
...bindings.vars,
Expand Down
67 changes: 67 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading