diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/html-rewriter.ts | 23 | ||||
-rw-r--r-- | examples/lambda.ts | 130 | ||||
-rw-r--r-- | examples/macros/components/covid19.tsx | 11 | ||||
-rw-r--r-- | examples/macros/components/example.jsx | 12 | ||||
-rw-r--r-- | examples/macros/fetchCSV.tsx | 6 | ||||
-rw-r--r-- | examples/macros/matchInFile.tsx | 4 | ||||
-rw-r--r-- | examples/macros/mystery-box.ts | 5 | ||||
-rw-r--r-- | examples/openInEditor.js | 3 | ||||
-rw-r--r-- | examples/react-fast-refresh-test/src/main.tsx | 15 | ||||
-rw-r--r-- | examples/react-file-system-router/index.tsx | 22 | ||||
-rw-r--r-- | examples/react-file-system-router/pages/index.tsx | 6 | ||||
-rw-r--r-- | examples/react-file-system-router/pages/one.tsx | 2 | ||||
-rw-r--r-- | examples/react-file-system-router/pages/two.tsx | 2 | ||||
-rw-r--r-- | examples/sha.js | 15 | ||||
-rw-r--r-- | examples/spawn.ts | 4 |
15 files changed, 96 insertions, 164 deletions
diff --git a/examples/html-rewriter.ts b/examples/html-rewriter.ts index 877b957b7..3017350e7 100644 --- a/examples/html-rewriter.ts +++ b/examples/html-rewriter.ts @@ -3,29 +3,18 @@ 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, - }, - ); + 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(), - ); + 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", - ); + element.setAttribute("href", "https://www.youtube.com/watch?v=dQw4w9WgXcQ"); }, }) .transform(response); diff --git a/examples/lambda.ts b/examples/lambda.ts index da1a7d9ee..66746ecad 100644 --- a/examples/lambda.ts +++ b/examples/lambda.ts @@ -38,21 +38,17 @@ try { } catch (e: any) { console.error("Error loading sourcefile:", e); try { - await fetch( - new URL(`http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/init/error`) - .href, - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - errorMessage: e.message, - errorType: e.name, - stackTrace: e?.stack?.split("\n") ?? [], - }), + await fetch(new URL(`http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/init/error`).href, { + method: "POST", + headers: { + "Content-Type": "application/json", }, - ); + body: JSON.stringify({ + errorMessage: e.message, + errorType: e.name, + stackTrace: e?.stack?.split("\n") ?? [], + }), + }); } catch (e2) { console.error("Error sending error to runtime:", e2); } @@ -79,21 +75,17 @@ export default { console.error(e); try { - await fetch( - new URL(`http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/init/error`) - .href, - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - errorMessage: e.message, - errorType: e.name, - stackTrace: e?.stack?.split("\n") ?? [], - }), + await fetch(new URL(`http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/init/error`).href, { + method: "POST", + headers: { + "Content-Type": "application/json", }, - ); + body: JSON.stringify({ + errorMessage: e.message, + errorType: e.name, + stackTrace: e?.stack?.split("\n") ?? [], + }), + }); } catch (e2) { console.error("Error sending error to runtime:", e2); } @@ -112,21 +104,17 @@ try { } catch (e: any) { console.error("Error parsing baseURI:", e); try { - await fetch( - new URL(`http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/init/error`) - .href, - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - errorMessage: e.message, - errorType: e.name, - stackTrace: e?.stack?.split("\n") || [], - }), + await fetch(new URL(`http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/init/error`).href, { + method: "POST", + headers: { + "Content-Type": "application/json", }, - ); + body: JSON.stringify({ + errorMessage: e.message, + errorType: e.name, + stackTrace: e?.stack?.split("\n") || [], + }), + }); } catch (e2) { console.error("Error sending error to runtime:", e2); } @@ -140,10 +128,7 @@ async function runHandler(response: Response) { var request = new Request(baseURL.href, { method, headers: response.headers, - body: - parseInt(response.headers.get("Content-Length") || "0", 10) > 0 - ? await response.blob() - : undefined, + body: parseInt(response.headers.get("Content-Length") || "0", 10) > 0 ? await response.blob() : undefined, }); // we are done with the Response object here // allow it to be GC'd @@ -162,18 +147,15 @@ async function runHandler(response: Response) { if (typeof process.env.VERBOSE !== "undefined") { console.error(`[${traceID}] Error running handler:`, e1); } - fetch( - `http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/${requestID}/error`, - { - method: "POST", - - body: JSON.stringify({ - errorMessage: e1.message, - errorType: e1.name, - stackTrace: e1?.stack?.split("\n") ?? [], - }), - }, - ).finally(noop); + fetch(`http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/${requestID}/error`, { + method: "POST", + + body: JSON.stringify({ + errorMessage: e1.message, + errorType: e1.name, + stackTrace: e1?.stack?.split("\n") ?? [], + }), + }).finally(noop); return; } finally { if (typeof process.env.VERBOSE !== "undefined") { @@ -182,28 +164,22 @@ async function runHandler(response: Response) { } if (!result || !("headers" in result)) { - await fetch( - `http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/${requestID}/error`, - { - method: "POST", - body: JSON.stringify({ - errorMessage: "Expected Response object", - errorType: "ExpectedResponseObject", - stackTrace: [], - }), - }, - ); + await fetch(`http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/${requestID}/error`, { + method: "POST", + body: JSON.stringify({ + errorMessage: "Expected Response object", + errorType: "ExpectedResponseObject", + stackTrace: [], + }), + }); return; } - await fetch( - `http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/${requestID}/response`, - { - method: "POST", - headers: result.headers, - body: await result.blob(), - }, - ); + await fetch(`http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/${requestID}/response`, { + method: "POST", + headers: result.headers, + body: await result.blob(), + }); (result as any) = undefined; } diff --git a/examples/macros/components/covid19.tsx b/examples/macros/components/covid19.tsx index 330661c82..16877b5ce 100644 --- a/examples/macros/components/covid19.tsx +++ b/examples/macros/components/covid19.tsx @@ -1,13 +1,10 @@ import { fetchCSV } from "macro:fetchCSV"; export const Covid19 = () => { - const rows = fetchCSV( - "https://covid19.who.int/WHO-COVID-19-global-data.csv", - { - last: 100, - columns: ["New_cases", "Date_reported", "Country"], - }, - ); + const rows = fetchCSV("https://covid19.who.int/WHO-COVID-19-global-data.csv", { + last: 100, + columns: ["New_cases", "Date_reported", "Country"], + }); return ( <div> diff --git a/examples/macros/components/example.jsx b/examples/macros/components/example.jsx index 84d2d92e9..6f2760b76 100644 --- a/examples/macros/components/example.jsx +++ b/examples/macros/components/example.jsx @@ -5,13 +5,11 @@ export const IPAddresses = () => ( <div> <h2>recent ip addresses</h2> <div className="Lines"> - {matchInFile("access.log", /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}/).map( - (ipAddress, index) => ( - <div className="Line" key={index}> - {ipAddress} - </div> - ), - )} + {matchInFile("access.log", /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}/).map((ipAddress, index) => ( + <div className="Line" key={index}> + {ipAddress} + </div> + ))} </div> </div> ); diff --git a/examples/macros/fetchCSV.tsx b/examples/macros/fetchCSV.tsx index b06b1e03e..55a12bc42 100644 --- a/examples/macros/fetchCSV.tsx +++ b/examples/macros/fetchCSV.tsx @@ -36,12 +36,12 @@ export async function fetchCSV(callExpression) { rows = rows .slice(Math.max(limit, rows.length) - limit) .reverse() - .filter((columns) => columns.every(Boolean)); + .filter(columns => columns.every(Boolean)); const value = ( <array> - {rows.map((columns) => ( + {rows.map(columns => ( <array> - {columnIndices.map((columnIndex) => ( + {columnIndices.map(columnIndex => ( <string value={columns[columnIndex]} /> ))} </array> diff --git a/examples/macros/matchInFile.tsx b/examples/macros/matchInFile.tsx index bd1c92dea..e434d1dec 100644 --- a/examples/macros/matchInFile.tsx +++ b/examples/macros/matchInFile.tsx @@ -12,10 +12,10 @@ export function matchInFile(callExpression: BunAST.CallExpression) { <array> {file .split("\n") - .map((line) => line.match(matcher)) + .map(line => line.match(matcher)) .filter(Boolean) .reverse() - .map((line) => ( + .map(line => ( <string value={line[0]} /> ))} </array> diff --git a/examples/macros/mystery-box.ts b/examples/macros/mystery-box.ts index 6583c9868..c686f82c3 100644 --- a/examples/macros/mystery-box.ts +++ b/examples/macros/mystery-box.ts @@ -6,10 +6,7 @@ export function mysteryBox(callExpression) { const count: number = parseInt(countString, 10); // validate - if (!(count >= 1 && count <= 1000)) - return new Error( - `Argument ${countString} is expected to be between 1 and 1000`, - ); + if (!(count >= 1 && count <= 1000)) return new Error(`Argument ${countString} is expected to be between 1 and 1000`); // return a value return (Math.random() * count) | 0; diff --git a/examples/openInEditor.js b/examples/openInEditor.js index 59282c098..3c67f02c2 100644 --- a/examples/openInEditor.js +++ b/examples/openInEditor.js @@ -3,8 +3,7 @@ import { parse } from "querystring"; export default { fetch(req) { - if (req.url === "/favicon.ico") - return new Response("nooo dont open favicon in editor", { status: 404 }); + if (req.url === "/favicon.ico") return new Response("nooo dont open favicon in editor", { status: 404 }); var pathname = req.url.substring(1); const q = pathname.indexOf("?"); diff --git a/examples/react-fast-refresh-test/src/main.tsx b/examples/react-fast-refresh-test/src/main.tsx index 1a9948fe8..928d64b72 100644 --- a/examples/react-fast-refresh-test/src/main.tsx +++ b/examples/react-fast-refresh-test/src/main.tsx @@ -6,8 +6,7 @@ export const Main = ({ productName }) => { <header> <div className="Title">CSS HMR Stress Test</div> <p className="Description"> - This page visually tests how quickly a bundler can update CSS over Hot - Module Reloading. + This page visually tests how quickly a bundler can update CSS over Hot Module Reloading. </p> </header> <main className="main"> @@ -21,9 +20,7 @@ export const Main = ({ productName }) => { <div className="ProgressBar-container"> <div className="ProgressBar"></div> </div> - <div className="SectionLabel"> - The progress bar should move from left to right smoothly. - </div> + <div className="SectionLabel">The progress bar should move from left to right smoothly.</div> </section> <section> @@ -44,15 +41,11 @@ export const Main = ({ productName }) => { <div className="Spinner"></div> </div> </div> - <div className="SectionLabel"> - The spinners should rotate & change color smoothly. - </div> + <div className="SectionLabel">The spinners should rotate & change color smoothly.</div> </section> </main> <footer> - <div className="SectionLabel FooterLabel"> - There are no CSS animations on this page. - </div> + <div className="SectionLabel FooterLabel">There are no CSS animations on this page.</div> <div className="Bundler-container"> <div className="Bundler">{productName}</div> diff --git a/examples/react-file-system-router/index.tsx b/examples/react-file-system-router/index.tsx index a784c393b..62d1151d5 100644 --- a/examples/react-file-system-router/index.tsx +++ b/examples/react-file-system-router/index.tsx @@ -1,24 +1,20 @@ // A simple way to connect FileSystemRouter to Bun#serve // run with `bun run index.tsx` -import { renderToReadableStream } from 'react-dom/server' -import { FileSystemRouter } from 'bun' +import { renderToReadableStream } from "react-dom/server"; +import { FileSystemRouter } from "bun"; export default { port: 3000, async fetch(request: Request) { const router = new FileSystemRouter({ dir: process.cwd() + "/pages", - style: "nextjs" - }) + style: "nextjs", + }); - const route = router.match(request) + const route = router.match(request); - const { default: Root } = await import(route.filePath) - return new Response( - await renderToReadableStream( - <Root {...route.params} /> - ) - ) - } -}
\ No newline at end of file + const { default: Root } = await import(route.filePath); + return new Response(await renderToReadableStream(<Root {...route.params} />)); + }, +}; diff --git a/examples/react-file-system-router/pages/index.tsx b/examples/react-file-system-router/pages/index.tsx index b2d9e735e..f1fe2a2e1 100644 --- a/examples/react-file-system-router/pages/index.tsx +++ b/examples/react-file-system-router/pages/index.tsx @@ -7,11 +7,11 @@ export default () => ( </head> <body> <h1> - <a href="/one" >one</a> + <a href="/one">one</a> </h1> <h1> - <a href="/two" >two</a> + <a href="/two">two</a> </h1> </body> </html> -)
\ No newline at end of file +); diff --git a/examples/react-file-system-router/pages/one.tsx b/examples/react-file-system-router/pages/one.tsx index 68c9fb4b9..b65a32f6c 100644 --- a/examples/react-file-system-router/pages/one.tsx +++ b/examples/react-file-system-router/pages/one.tsx @@ -9,4 +9,4 @@ export default () => ( <p>one</p> </body> </html> -)
\ No newline at end of file +); diff --git a/examples/react-file-system-router/pages/two.tsx b/examples/react-file-system-router/pages/two.tsx index 09be9fa5c..c3d8beef9 100644 --- a/examples/react-file-system-router/pages/two.tsx +++ b/examples/react-file-system-router/pages/two.tsx @@ -9,4 +9,4 @@ export default () => ( <p>two</p> </body> </html> -)
\ No newline at end of file +); diff --git a/examples/sha.js b/examples/sha.js index 0155aecc4..bb7382a03 100644 --- a/examples/sha.js +++ b/examples/sha.js @@ -1,20 +1,9 @@ -import { - SHA1, - SHA256, - SHA512, - SHA384, - SHA512_256, - MD5, - MD4, - RIPEMD160, - sha, -} from "bun"; +import { SHA1, SHA256, SHA512, SHA384, SHA512_256, MD5, MD4, RIPEMD160, sha } from "bun"; const input = "Hello World"; const [first, second] = input.split(" "); -const log = (name, ...args) => - console.log(`${name}:`.padStart("SHA512_256: ".length), ...args); +const log = (name, ...args) => console.log(`${name}:`.padStart("SHA512_256: ".length), ...args); console.log(""); // This is SHA512-256: diff --git a/examples/spawn.ts b/examples/spawn.ts index d72a118b9..520fa60ff 100644 --- a/examples/spawn.ts +++ b/examples/spawn.ts @@ -7,9 +7,7 @@ const repo = process.argv.at(3) || "TheoBr/vercel-vite-demo"; const target = basename(repo) + "-main"; console.log("Downloading", repo, "to", "/tmp/" + target); -const archive = await fetch( - `https://github.com/${repo}/archive/refs/heads/main.tar.gz`, -); +const archive = await fetch(`https://github.com/${repo}/archive/refs/heads/main.tar.gz`); // remove the directory if it already exists locally rmSync("/tmp/" + target, { recursive: true, force: true }); |