Files
bun.sh/examples/html-rewriter.ts
Colin McDonnell f7f1b60444 Add bun-types, add typechecking, add child_process types (#1475)
* Add bun-types to packages

* Improve typing

* Fix types in tests

* Fix dts tests

* Run formatter

* Fix all type errors

* Add strict mode, fix type errors

* Add ffi changes

* Move workflows to root

* Add workflows

* Remove labeler

* Add child_process types

* Fix synthetic defaults issue

* Remove docs

* Move scripts

* Run prettier

* Include examples in typechecking

* captureStackTrace types

* moved captureStackTrace types to globals

* Address reviews

Co-authored-by: Colin McDonnell <colinmcd@alum.mit.edu>
Co-authored-by: Dylan Conway <dylan.conway567@gmail.com>
2022-11-09 15:40:40 -08:00

49 lines
1.2 KiB
TypeScript

// Start a fast HTTP server from a function
Bun.serve({
async fetch(req) {
const { pathname } = new URL(req.url);
if (
!(pathname.startsWith("/https://") || pathname.startsWith("/http://"))
) {
return new Response(
"Enter a path that starts with https:// or http://\n",
{
status: 400,
},
);
}
const response = await fetch(
req.url.substring("http://localhost:3000/".length),
req.clone(),
);
return new HTMLRewriter()
.on("a[href]", {
element(element) {
element.setAttribute(
"href",
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
);
},
})
.transform(response);
},
// this is called when fetch() throws or rejects
// error(err: Error) {
// },
// this boolean enables the bun's default error handler
// sometime after the initial release, it will auto reload as well
development: process.env.NODE_ENV !== "production",
// note: this isn't node, but for compatibility bun supports process.env + more stuff in process
// SSL is enabled if these two are set
// certFile: './cert.pem',
// keyFile: './key.pem',
port: 3000, // number or string
});