先上结论:官网的方法行不通
curl https://bun.sh/install | bash
- Bun:是一个现代JavaScript运行时,专注于性能与开发者体验。它不仅是一个快速的JavaScript执行环境,还提供了构建、测试和调试JavaScript和TypeScript代码的工具。Bun支持Windows、Linux和macOS操作系统,但在Windows桌面环境下安装时可能需要通过WSL(Windows Subsystem for Linux)来实现。
Bun is a fast JavaScript
runtime
Develop, test, run, and bundle JavaScript & TypeScript projects—all with Bun. Bun is an all-in-one JavaScript runtime & toolkit designed for speed, complete with a bundler, test runner, and Node.js-compatible package manager.
安装Bun的时候颇受了一些周折
按照官网安装bun
curl -fsSL https://bun.sh/install | bash
安装bun 失败,主要是github有问题。
尝试手工npm 安装bun
sudo npm install -g bun
安装成了。
Bun使用案例
使用起来也很方便,比如server
服务器
const server = Bun.serve({
port: 3000,
fetch(request) {
return new Response("Welcome to Bun!");
},
});
console.log(`Listening on localhost:${server.port}`);
读写文件
const file = Bun.file(import.meta.dir + '/package.json'); // BunFile
const pkg = await file.json(); // BunFile extends Blob
pkg.name = 'my-package';
pkg.version = '1.0.0';
await Bun.write(file, JSON.stringify(pkg, null, 2));
流读取
const response = await fetch("https://bun.sh");
await Bun.readableStreamToArrayBuffer(response.body); // => ArrayBuffer
await Bun.readableStreamToBlob(response.body); // => Blob
await Bun.readableStreamToJSON(response.body); // => object
await Bun.readableStreamToText(response.body); // => string
await Bun.readableStreamToArray(response.body); // => unknown[]
调用函数
import { dlopen, FFIType, suffix } from "bun:ffi";
// `suffix` is either "dylib", "so", or "dll" depending on the platform
const path = `libsqlite3.${suffix}`;
const {
symbols: {
sqlite3_libversion, // the function to call
},
} = dlopen(path, {
sqlite3_libversion: {
args: [], // no arguments
returns: FFIType.cstring, // returns a string
},
});
console.log(`SQLite 3 version: ${sqlite3_libversion()}`);