MyGit

bun-v0.0.79

oven-sh/bun

版本发布时间: 2022-05-04 23:23:25

oven-sh/bun最新发布版本:bun-v1.1.38(2024-11-29 19:55:48)

To upgrade:

bun upgrade
If you run into any issues with upgrading

Try running this:

curl https://bun.sh/install | bash

Highlights

The next large project for bun is a production bundler Tracking issue

New Contributors

bun:ffi

The "bun:ffi" core module lets you efficiently call native libraries from JavaScript. It works with languages that support the C ABI (Zig, Rust, C/C++, C#, Nim, Kotlin, etc).

Get the locally-installed SQLite version number:

import { dlopen, CString, ptr, suffix, FFIType } from "bun:ffi";

const sqlite3Path = process.env.SQLITE3_PATH || `libsqlite3.${suffix}`;
const {
  symbols: { sqlite3_libversion },
} = dlopen(sqlite3Path, {
  sqlite3_libversion: {
    returns: "cstring",
  },
});
console.log("SQLite version", sqlite3_libversion());

FFI is really exciting because there is no runtime-specific code. You don't have to write a Bun FFI module (that isn't a thing). Use JavaScript to write bindings to native libraries installed with homebrew, with your linux distro's package manager or elsewhere. You can also write bindings to your own native code.

FFI has a reputation of being slower than runtime-specific APIs like napi – but that's not true for bun:ffi.

image

Bun embeds a small C compiler that generates code on-demand and converts types between JavaScript & native code inline. A lot of overhead in native libraries comes from function calls that validate & convert types, so moving that to just-in-time compiled C using engine-specific implementation details makes that faster. Those C functions are called directly – there is no extra wrapper in the native code side of things.

Some bun:ffi usecases:

Later (not yet), bun:ffi will be integrated with bun's bundler and that will enable things like:

Buffer

A lot of Node.js' Buffer module is now implemented natively in Bun.js, but it's not complete yet.

Here is a comparison of how long various functions take.

image

Replace & eliminate exports with Bun.Transpiler

For code transpiled with Bun.Transpiler, you can now remove and/or replace exports with a different value.

const transpiler = new Bun.Transpiler({
  exports: {
    replace: {
      // Next.js does this
      getStaticProps: ["__N_SSG", true],
    },
    eliminate: ["localVarToRemove"],
  },
  treeShaking: true,
  trimUnusedImports: true,
});

const code = `
import fs from "fs";
export var localVarToRemove = fs.readFileSync("/etc/passwd");
import * as CSV from "my-csv-parser";

export function getStaticProps() {
  return {
    props: { rows: CSV.parse(fs.readFileSync("./users-list.csv", "utf8")) },
  };
}

export function Page({ rows }) {
  return (
    <div>
      <h1>My page</h1>
      <p>
        <a href="/about">About</a>
      </p>
      <p>
        <a href="/users">Users</a>
      </p>
      <div>
        {rows.map((columns, index) => (
          <span key={index}>{columns.join(" | ")} </span>
        ))}
      </div>
    </div>
  );
}
`;

console.log(transpiler.transformSync(code));

Which outputs (this is the automatic react transform)

export var __N_SSG = true;

export function Page({ rows }) {
  return jsxDEV("div", {
    children: [
      jsxDEV("h1", {
        children: "My page"
      }, undefined, false, undefined, this),
      jsxDEV("p", {
        children: jsxDEV("a", {
          href: "/about",
          children: "About"
        }, undefined, false, undefined, this)
      }, undefined, false, undefined, this),
      jsxDEV("p", {
        children: jsxDEV("a", {
          href: "/users",
          children: "Users"
        }, undefined, false, undefined, this)
      }, undefined, false, undefined, this),
      jsxDEV("div", {
        children: rows.map((columns, index) => jsxDEV("span", {
          children: [
            columns.join(" | "),
            " "
          ]
        }, index, true, undefined, this))
      }, undefined, false, undefined, this)
    ]
  }, undefined, true, undefined, this);
}

More new stuff

Hashing functions powered by BoringSSL:

import {
  SHA1,
  MD5,
  MD4,
  SHA224,
  SHA512,
  SHA384,
  SHA256,
  SHA512_256,
} from "bun";

// hash the string and return as a Uint8Array
SHA1.hash("123456");
MD5.hash("123456");
MD4.hash("123456");
SHA224.hash("123456");
SHA512.hash("123456");
SHA384.hash("123456");
SHA256.hash("123456");
SHA512_256.hash("123456");

// output as a hex string
SHA1.hash(new Uint8Array(42), "hex");
MD5.hash(new Uint8Array(42), "hex");
MD4.hash(new Uint8Array(42), "hex");
SHA224.hash(new Uint8Array(42), "hex");
SHA512.hash(new Uint8Array(42), "hex");
SHA384.hash(new Uint8Array(42), "hex");
SHA256.hash(new Uint8Array(42), "hex");
SHA512_256.hash(new Uint8Array(42), "hex");

// incrementally update the hashing function value and convert it at the end to a hex string
// similar to node's API in require('crypto')
// this is not wired up yet to bun's "crypto" polyfill, but it really should be
new SHA1().update(new Uint8Array(42)).digest("hex");
new MD5().update(new Uint8Array(42)).digest("hex");
new MD4().update(new Uint8Array(42)).digest("hex");
new SHA224().update(new Uint8Array(42)).digest("hex");
new SHA512().update(new Uint8Array(42)).digest("hex");
new SHA384().update(new Uint8Array(42)).digest("hex");
new SHA256().update(new Uint8Array(42)).digest("hex");
new SHA512_256().update(new Uint8Array(42)).digest("hex");

Reliability improvements

相关地址:原始地址 下载(tar) 下载(zip)

1、 bun-darwin-aarch64.dSYM.tar.gz 15.11MB

2、 bun-darwin-aarch64.zip 11.67MB

3、 bun-darwin-x64.dSYM.tar.gz 16.43MB

4、 bun-darwin-x64.zip 13.84MB

5、 bun-linux-x64.zip 25.73MB

查看:2022-05-04发行的版本