-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Browser Rendering JS wrapped binding (#3334)
- Loading branch information
Showing
8 changed files
with
176 additions
and
0 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,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'; |
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,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); | ||
} |
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,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", | ||
], | ||
) |
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,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 }); | ||
} | ||
}, | ||
}; |
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 @@ | ||
# 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 |
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,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") | ||
], | ||
) | ||
) | ||
] | ||
); |
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 @@ | ||
// 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
36
src/cloudflare/internal/test/br/python-br-api-test.wd-test
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,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") | ||
], | ||
) | ||
) | ||
] | ||
); |