diff options
author | 2022-11-09 15:40:40 -0800 | |
---|---|---|
committer | 2022-11-09 15:40:40 -0800 | |
commit | f7f1b604443c030afe29d1059b90f72c69afe081 (patch) | |
tree | 8f2397447b2a84dab02850007264b72cc565f5d6 /examples | |
parent | da257336b0b70df8c31da647496899cf70670000 (diff) | |
download | bun-f7f1b604443c030afe29d1059b90f72c69afe081.tar.gz bun-f7f1b604443c030afe29d1059b90f72c69afe081.tar.zst bun-f7f1b604443c030afe29d1059b90f72c69afe081.zip |
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>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/bun-hot-websockets.js | 2 | ||||
-rw-r--r-- | examples/cat.ts | 4 | ||||
-rw-r--r-- | examples/html-rewriter.ts | 9 | ||||
-rw-r--r-- | examples/http-file.ts | 2 | ||||
-rw-r--r-- | examples/http-request-body.ts | 3 | ||||
-rw-r--r-- | examples/lambda.ts | 24 | ||||
-rwxr-xr-x | examples/macros/bun.lockb | bin | 0 -> 5816 bytes | |||
-rw-r--r-- | examples/macros/components/covid19.tsx | 2 | ||||
-rw-r--r-- | examples/macros/components/example.jsx | 2 | ||||
-rw-r--r-- | examples/macros/mystery-box.ts | 9 | ||||
-rwxr-xr-x | examples/react-fast-refresh-test/bun.lockb | bin | 0 -> 352211 bytes | |||
-rw-r--r-- | examples/react-fast-refresh-test/tsconfig.json | 18 | ||||
-rw-r--r-- | examples/spawn.ts | 15 | ||||
-rw-r--r-- | examples/tsconfig.json | 8 |
14 files changed, 51 insertions, 47 deletions
diff --git a/examples/bun-hot-websockets.js b/examples/bun-hot-websockets.js index 1656d2eb5..7cdaf0f02 100644 --- a/examples/bun-hot-websockets.js +++ b/examples/bun-hot-websockets.js @@ -83,7 +83,7 @@ export default { headers: { "Content-Type": "text/html; charset=utf-8", }, - } + }, ); }, }; diff --git a/examples/cat.ts b/examples/cat.ts index a7d60b347..01a059ad0 100644 --- a/examples/cat.ts +++ b/examples/cat.ts @@ -1,6 +1,6 @@ import { resolve } from "path"; const { write, stdout, file } = Bun; -const { argv } = process; +import { argv } from "process"; -const path = resolve(argv.at(-1)); +const path = resolve(argv.at(-1)!); await write(stdout, file(path)); diff --git a/examples/html-rewriter.ts b/examples/html-rewriter.ts index a2a9d03fa..877b957b7 100644 --- a/examples/html-rewriter.ts +++ b/examples/html-rewriter.ts @@ -1,4 +1,5 @@ // Start a fast HTTP server from a function + Bun.serve({ async fetch(req) { const { pathname } = new URL(req.url); @@ -9,21 +10,21 @@ Bun.serve({ "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() + req.clone(), ); return new HTMLRewriter() .on("a[href]", { - element(element: Element) { + element(element) { element.setAttribute( "href", - "https://www.youtube.com/watch?v=dQw4w9WgXcQ" + "https://www.youtube.com/watch?v=dQw4w9WgXcQ", ); }, }) diff --git a/examples/http-file.ts b/examples/http-file.ts index 130104534..fdf47f949 100644 --- a/examples/http-file.ts +++ b/examples/http-file.ts @@ -1,4 +1,4 @@ -import { file } from "bun"; +import { file, serve } from "bun"; serve({ fetch(req: Request) { diff --git a/examples/http-request-body.ts b/examples/http-request-body.ts index 6ce356ce0..b9a454889 100644 --- a/examples/http-request-body.ts +++ b/examples/http-request-body.ts @@ -6,11 +6,12 @@ serve({ const body = req.body; const writer = Bun.file(`upload.${Date.now()}.txt`).writer(); - for await (const chunk of body) { + for await (const chunk of body!) { writer.write(chunk); } const wrote = await writer.end(); + // @ts-ignore return Response.json({ wrote, type: req.headers.get("Content-Type") }); }, }); diff --git a/examples/lambda.ts b/examples/lambda.ts index ab2d5bb2b..da1a7d9ee 100644 --- a/examples/lambda.ts +++ b/examples/lambda.ts @@ -35,7 +35,7 @@ var Handler; try { Handler = await import(sourcefile); -} catch (e) { +} catch (e: any) { console.error("Error loading sourcefile:", e); try { await fetch( @@ -51,7 +51,7 @@ try { errorType: e.name, stackTrace: e?.stack?.split("\n") ?? [], }), - } + }, ); } catch (e2) { console.error("Error sending error to runtime:", e2); @@ -92,7 +92,7 @@ export default { errorType: e.name, stackTrace: e?.stack?.split("\n") ?? [], }), - } + }, ); } catch (e2) { console.error("Error sending error to runtime:", e2); @@ -109,7 +109,7 @@ if ("baseURI" in Handler.default) { var baseURL; try { baseURL = new URL(baseURLString); -} catch (e) { +} catch (e: any) { console.error("Error parsing baseURI:", e); try { await fetch( @@ -125,7 +125,7 @@ try { errorType: e.name, stackTrace: e?.stack?.split("\n") || [], }), - } + }, ); } catch (e2) { console.error("Error sending error to runtime:", e2); @@ -147,7 +147,7 @@ async function runHandler(response: Response) { }); // we are done with the Response object here // allow it to be GC'd - response = undefined; + (response as any) = undefined; var result: Response; try { @@ -155,10 +155,10 @@ async function runHandler(response: Response) { console.time(`[${traceID}] Run ${request.url}`); } result = handlerFunction(request, {}); - if (result && result.then) { + if (result && (result as any).then) { await result; } - } catch (e1) { + } catch (e1: any) { if (typeof process.env.VERBOSE !== "undefined") { console.error(`[${traceID}] Error running handler:`, e1); } @@ -172,7 +172,7 @@ async function runHandler(response: Response) { errorType: e1.name, stackTrace: e1?.stack?.split("\n") ?? [], }), - } + }, ).finally(noop); return; } finally { @@ -191,7 +191,7 @@ async function runHandler(response: Response) { errorType: "ExpectedResponseObject", stackTrace: [], }), - } + }, ); return; } @@ -202,9 +202,9 @@ async function runHandler(response: Response) { method: "POST", headers: result.headers, body: await result.blob(), - } + }, ); - result = undefined; + (result as any) = undefined; } while (true) { diff --git a/examples/macros/bun.lockb b/examples/macros/bun.lockb Binary files differnew file mode 100755 index 000000000..2e6de7391 --- /dev/null +++ b/examples/macros/bun.lockb diff --git a/examples/macros/components/covid19.tsx b/examples/macros/components/covid19.tsx index 52fee2ff3..330661c82 100644 --- a/examples/macros/components/covid19.tsx +++ b/examples/macros/components/covid19.tsx @@ -6,7 +6,7 @@ export const Covid19 = () => { { last: 100, columns: ["New_cases", "Date_reported", "Country"], - } + }, ); return ( diff --git a/examples/macros/components/example.jsx b/examples/macros/components/example.jsx index ad80ce9e1..84d2d92e9 100644 --- a/examples/macros/components/example.jsx +++ b/examples/macros/components/example.jsx @@ -10,7 +10,7 @@ export const IPAddresses = () => ( <div className="Line" key={index}> {ipAddress} </div> - ) + ), )} </div> </div> diff --git a/examples/macros/mystery-box.ts b/examples/macros/mystery-box.ts index 4dff5d003..6583c9868 100644 --- a/examples/macros/mystery-box.ts +++ b/examples/macros/mystery-box.ts @@ -1,13 +1,16 @@ export function mysteryBox(callExpression) { console.log(callExpression.log); // get arguments - const [ countNode ] = callExpression.arguments; + const [countNode] = callExpression.arguments; const countString: string = countNode.get(); 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; + return (Math.random() * count) | 0; } diff --git a/examples/react-fast-refresh-test/bun.lockb b/examples/react-fast-refresh-test/bun.lockb Binary files differnew file mode 100755 index 000000000..a3cf88405 --- /dev/null +++ b/examples/react-fast-refresh-test/bun.lockb diff --git a/examples/react-fast-refresh-test/tsconfig.json b/examples/react-fast-refresh-test/tsconfig.json index 401ede344..d543b6472 100644 --- a/examples/react-fast-refresh-test/tsconfig.json +++ b/examples/react-fast-refresh-test/tsconfig.json @@ -1,7 +1,11 @@ { "compilerOptions": { "target": "esnext", - "lib": ["dom", "dom.iterable", "esnext"], + "lib": [ + "dom", + "dom.iterable", + "esnext" + ], "allowJs": true, "skipLibCheck": true, "strict": false, @@ -14,6 +18,12 @@ "isolatedModules": true, "jsx": "preserve" }, - "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"], - "exclude": ["node_modules"] -} + "include": [ + "next-env.d.ts", + "**/*.ts", + "**/*.tsx" + ], + "exclude": [ + "node_modules" + ] +}
\ No newline at end of file diff --git a/examples/spawn.ts b/examples/spawn.ts index a590a94f3..d72a118b9 100644 --- a/examples/spawn.ts +++ b/examples/spawn.ts @@ -8,7 +8,7 @@ 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` + `https://github.com/${repo}/archive/refs/heads/main.tar.gz`, ); // remove the directory if it already exists locally @@ -29,14 +29,11 @@ await tar.exited; if (!which("vercel")) { console.log("Installing vercel..."); - const installer = spawn( - { cmd: ["bun", "install", "-g", "vercel"] }, - { - stderr: "inherit", - stdout: "inherit", - stdin: "inherit", - } - ); + const installer = spawn(["bun", "install", "-g", "vercel"], { + stderr: "inherit", + stdout: "inherit", + stdin: "inherit", + }); await installer.exited; if (!which("vercel")) { diff --git a/examples/tsconfig.json b/examples/tsconfig.json deleted file mode 100644 index 404c353e6..000000000 --- a/examples/tsconfig.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "compilerOptions": { - "lib": ["ESNext"], - "module": "esnext", - "target": "esnext", - "typeRoots": ["../types"] - } -} |