Skip to content

Commit

Permalink
Add Browser Rendering JS wrapped binding (#3334)
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym authored Jan 15, 2025
1 parent a81daea commit a33c6d2
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/cloudflare/br.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2025 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

// This binding is managed by the browser rendering team (aka brapi)
// https://developers.cloudflare.com/browser-rendering/

export { BrowserRendering } from 'cloudflare-internal:br-api';
28 changes: 28 additions & 0 deletions src/cloudflare/internal/br-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2025 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

interface Fetcher {
fetch: typeof fetch;
}

export class BrowserRendering {
private readonly fetcher: Fetcher;

public constructor(fetcher: Fetcher) {
this.fetcher = fetcher;
}

public async fetch(
input: RequestInfo | URL,
init?: RequestInit
): Promise<Response> {
return this.fetcher.fetch(input, init);
}
}

export default function makeBinding(env: {
fetcher: Fetcher;
}): BrowserRendering {
return new BrowserRendering(env.fetcher);
}
22 changes: 22 additions & 0 deletions src/cloudflare/internal/test/br/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("//:build/wd_test.bzl", "wd_test")
load("//src/workerd/server/tests/python:py_wd_test.bzl", "py_wd_test")

wd_test(
src = "br-api-test.wd-test",
args = ["--experimental"],
data = glob(["*.js"]),
)

py_wd_test(
size = "large",
src = "python-br-api-test.wd-test",
args = ["--experimental"],
data = glob([
"*.js",
"*.py",
]),
tags = [
# TODO(someday): Fix asan failure for this, see https://github.com/cloudflare/workerd/pull/3140#discussion_r1858273318
"no-asan",
],
)
21 changes: 21 additions & 0 deletions src/cloudflare/internal/test/br/br-api-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2025 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

import * as assert from 'node:assert';

export const tests = {
async test(_, env) {
{
// Test legacy fetch
const resp = await env.browser.fetch('http://workers-binding.brapi/run', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
inputs: { test: true },
}),
});
assert.deepStrictEqual(await resp.json(), { success: true });
}
},
};
9 changes: 9 additions & 0 deletions src/cloudflare/internal/test/br/br-api-test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2025 Cloudflare, Inc.
# Licensed under the Apache 2.0 license found in the LICENSE file or at:
# https://opensource.org/licenses/Apache-2.0


async def test(context, env):
resp = await env.browser.fetch("http://workers-binding.brapi/run")
data = await resp.json()
assert data.success is True
36 changes: 36 additions & 0 deletions src/cloudflare/internal/test/br/br-api-test.wd-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Workerd = import "/workerd/workerd.capnp";

const unitTests :Workerd.Config = (
services = [
( name = "brapi-api-test",
worker = (
modules = [
(name = "worker", esModule = embed "br-api-test.js")
],
compatibilityDate = "2024-12-30",
compatibilityFlags = ["nodejs_compat"],
bindings = [
(
name = "browser",
wrapped = (
moduleName = "cloudflare-internal:br-api",
innerBindings = [(
name = "fetcher",
service = "br-mock"
)],
)
)
],
)
),
( name = "br-mock",
worker = (
compatibilityDate = "2024-12-30",
compatibilityFlags = ["experimental", "nodejs_compat"],
modules = [
(name = "worker", esModule = embed "br-mock.js")
],
)
)
]
);
16 changes: 16 additions & 0 deletions src/cloudflare/internal/test/br/br-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2025 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

export default {
async fetch(request, env, ctx) {
return Response.json(
{ success: true },
{
headers: {
'content-type': 'application/json',
},
}
);
},
};
36 changes: 36 additions & 0 deletions src/cloudflare/internal/test/br/python-br-api-test.wd-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Workerd = import "/workerd/workerd.capnp";

const unitTests :Workerd.Config = (
services = [
( name = "br-api-test",
worker = (
modules = [
(name = "worker.py", pythonModule = embed "br-api-test.py")
],
compatibilityDate = "2024-06-03",
compatibilityFlags = ["nodejs_compat", "python_workers_development"],
bindings = [
(
name = "browser",
wrapped = (
moduleName = "cloudflare-internal:br-api",
innerBindings = [(
name = "fetcher",
service = "br-mock"
)],
)
)
],
)
),
( name = "br-mock",
worker = (
compatibilityDate = "2024-06-03",
compatibilityFlags = ["experimental", "nodejs_compat"],
modules = [
(name = "worker", esModule = embed "br-mock.js")
],
)
)
]
);

0 comments on commit a33c6d2

Please sign in to comment.