当前位置: 首页 > news >正文

bun一个现代JavaScript运行时

先上结论:官网的方法行不通

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'); // BunFileconst 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 argumentsreturns: FFIType.cstring, // returns a string},
});console.log(`SQLite 3 version: ${sqlite3_libversion()}`);


http://www.mrgr.cn/news/20421.html

相关文章:

  • 2024 年高教社杯全国大学生数学建模竞赛题目-A 题 “板凳龙” 闹元宵
  • 详解Python输出
  • 【2024数模国赛赛题思路公开】国赛C题思路丨附可运行代码丨无偿自提
  • 前端黑科技:使用 JavaScript 实现网页扫码功能
  • X 射线测厚仪 -开启高精度厚度检测之门
  • vue+ThreeJS:从0 到1 搭建开发环境
  • 多智能体强化学习:citylearn城市建筑能量优化和需求响应
  • iOS——strong和copy的底层实现
  • PHP一站式解决方案高级房产系统小程序源码
  • 2024高教社杯全国大学生数学建模竞赛B题保姆级分析完整思路+代码+数据教学
  • 2024数学建模国赛选题建议及初步思路来啦!
  • 2024年高教社杯全国大学生数学建模竞赛B题思路(2024数学建模国赛B题思路)
  • 老师如何发布月考成绩,学生如何查询成绩?
  • 最值求解 | 管理类联考数学专项
  • v$session_longops监控 PDB clone 进度
  • 【大模型实战篇】大模型显存资源计算以及GPU如何选择
  • 高压喷雾车的功能与应用_鼎跃安全
  • 计算机网络(运输层)
  • 听说金融界学历都硕士以上了?你还不来社科院与杜兰大学金融管理硕士项目提升吗
  • Python精选200Tips:71-80