Compare commits

..

19 Commits

Author SHA1 Message Date
Don Isaac
b4950b15a2 Merge branch 'main' of https://github.com/oven-sh/bun into don/test/toml 2025-03-06 16:56:41 -08:00
Don Isaac
31ecf12b5b sort files alphabetically 2025-03-06 16:56:39 -08:00
Don Isaac
cdb46b9a28 update tesets 2025-03-05 20:14:23 -08:00
Don Isaac
9b39881b0f undo sni changes 2025-03-05 20:09:58 -08:00
Don Isaac
81ee014d04 '-' is a valid key 2025-03-05 20:09:04 -08:00
Don Isaac
46704a9b32 cleanup 2025-03-05 19:57:39 -08:00
Don Isaac
eab6c52588 re-generate snapshot 2025-03-05 18:44:02 -08:00
Don Isaac
daf3528c38 fix pct logic 2025-03-05 18:42:14 -08:00
Don Isaac
40c227f227 Merge branch 'main' into don/test/toml 2025-03-05 16:47:39 -08:00
DonIsaac
a633380e96 bun run clang-format 2025-03-06 00:43:43 +00:00
DonIsaac
9b463b4ae3 bun run zig-format 2025-03-06 00:43:21 +00:00
Don Isaac
1ce94ad9ad fix string-encoded index keys 2025-03-05 16:42:18 -08:00
Don Isaac
c1b15c6f54 Merge branch 'main' of https://github.com/oven-sh/bun into don/test/toml 2025-03-05 15:39:03 -08:00
Don Isaac
f6038498b0 read files as utf8 2025-03-05 14:22:01 -08:00
Don Isaac
db02d23b72 Merge branch 'main' of https://github.com/oven-sh/bun into don/test/toml 2025-03-05 14:19:47 -08:00
Don Isaac
746f63664e Merge branch 'main' of https://github.com/oven-sh/bun into don/test/toml 2025-03-04 14:24:28 -08:00
Don Isaac
92c779f0d4 add import cases 2025-03-03 17:30:03 -08:00
Don Isaac
1123a3e521 move tests to integration 2025-03-03 17:03:31 -08:00
Don Isaac
36f6b048ae test(bundler): comprehensive TOML testing 2025-03-03 16:31:27 -08:00
2361 changed files with 33995 additions and 84952 deletions

View File

@@ -1,12 +1,12 @@
ARG LLVM_VERSION="19"
ARG REPORTED_LLVM_VERSION="19.1.7"
ARG LLVM_VERSION="18"
ARG REPORTED_LLVM_VERSION="18.1.8"
ARG OLD_BUN_VERSION="1.1.38"
ARG DEFAULT_CFLAGS="-mno-omit-leaf-frame-pointer -fno-omit-frame-pointer -ffunction-sections -fdata-sections -faddrsig -fno-unwind-tables -fno-asynchronous-unwind-tables"
ARG DEFAULT_CXXFLAGS="-flto=full -fwhole-program-vtables -fforce-emit-vtables"
ARG BUILDKITE_AGENT_TAGS="queue=linux,os=linux,arch=${TARGETARCH}"
FROM --platform=$BUILDPLATFORM ubuntu:20.04 as base-arm64
FROM --platform=$BUILDPLATFORM ubuntu:20.04 as base-amd64
FROM --platform=$BUILDPLATFORM ubuntu:18.04 as base-amd64
FROM base-$TARGETARCH as base
ARG LLVM_VERSION

View File

@@ -107,9 +107,9 @@ const buildPlatforms = [
{ os: "linux", arch: "aarch64", distro: "amazonlinux", release: "2023", features: ["docker"] },
{ os: "linux", arch: "x64", distro: "amazonlinux", release: "2023", features: ["docker"] },
{ os: "linux", arch: "x64", baseline: true, distro: "amazonlinux", release: "2023", features: ["docker"] },
{ os: "linux", arch: "aarch64", abi: "musl", distro: "alpine", release: "3.21" },
{ os: "linux", arch: "x64", abi: "musl", distro: "alpine", release: "3.21" },
{ os: "linux", arch: "x64", abi: "musl", baseline: true, distro: "alpine", release: "3.21" },
{ os: "linux", arch: "aarch64", abi: "musl", distro: "alpine", release: "3.20" },
{ os: "linux", arch: "x64", abi: "musl", distro: "alpine", release: "3.20" },
{ os: "linux", arch: "x64", abi: "musl", baseline: true, distro: "alpine", release: "3.20" },
{ os: "windows", arch: "x64", release: "2019" },
{ os: "windows", arch: "x64", baseline: true, release: "2019" },
];
@@ -134,9 +134,9 @@ const testPlatforms = [
{ os: "linux", arch: "x64", baseline: true, distro: "ubuntu", release: "24.04", tier: "latest" },
{ os: "linux", arch: "x64", baseline: true, distro: "ubuntu", release: "22.04", tier: "previous" },
{ os: "linux", arch: "x64", baseline: true, distro: "ubuntu", release: "20.04", tier: "oldest" },
{ os: "linux", arch: "aarch64", abi: "musl", distro: "alpine", release: "3.21", tier: "latest" },
{ os: "linux", arch: "x64", abi: "musl", distro: "alpine", release: "3.21", tier: "latest" },
{ os: "linux", arch: "x64", abi: "musl", baseline: true, distro: "alpine", release: "3.21", tier: "latest" },
{ os: "linux", arch: "aarch64", abi: "musl", distro: "alpine", release: "3.20", tier: "latest" },
{ os: "linux", arch: "x64", abi: "musl", distro: "alpine", release: "3.20", tier: "latest" },
{ os: "linux", arch: "x64", abi: "musl", baseline: true, distro: "alpine", release: "3.20", tier: "latest" },
{ os: "windows", arch: "x64", release: "2019", tier: "oldest" },
{ os: "windows", arch: "x64", release: "2019", baseline: true, tier: "oldest" },
];

View File

@@ -201,8 +201,6 @@ function create_release() {
local artifacts=(
bun-darwin-aarch64.zip
bun-darwin-aarch64-profile.zip
bun-darwin-x64.zip
bun-darwin-x64-profile.zip
bun-linux-aarch64.zip
bun-linux-aarch64-profile.zip
bun-linux-x64.zip

View File

@@ -11,8 +11,8 @@ on:
env:
BUN_VERSION: "1.2.0"
LLVM_VERSION: "19.1.7"
LLVM_VERSION_MAJOR: "19"
LLVM_VERSION: "18.1.8"
LLVM_VERSION_MAJOR: "18"
jobs:
clang-format:

View File

@@ -11,8 +11,8 @@ on:
env:
BUN_VERSION: "1.2.0"
LLVM_VERSION: "19.1.7"
LLVM_VERSION_MAJOR: "19"
LLVM_VERSION: "18.1.8"
LLVM_VERSION_MAJOR: "18"
jobs:
clang-tidy:

View File

@@ -3,6 +3,7 @@ name: Lint
permissions:
contents: read
env:
LLVM_VERSION: 16
BUN_VERSION: "1.2.0"
on:

View File

@@ -89,6 +89,4 @@ jobs:
Updates c-ares to version ${{ steps.check-version.outputs.tag }}
Compare: https://github.com/c-ares/c-ares/compare/${{ steps.check-version.outputs.current }}...${{ steps.check-version.outputs.latest }}
Auto-updated by [this workflow](https://github.com/oven-sh/bun/actions/workflows/update-cares.yml)

View File

@@ -89,6 +89,4 @@ jobs:
Updates libarchive to version ${{ steps.check-version.outputs.tag }}
Compare: https://github.com/libarchive/libarchive/compare/${{ steps.check-version.outputs.current }}...${{ steps.check-version.outputs.latest }}
Auto-updated by [this workflow](https://github.com/oven-sh/bun/actions/workflows/update-libarchive.yml)

View File

@@ -89,6 +89,4 @@ jobs:
Updates libdeflate to version ${{ steps.check-version.outputs.tag }}
Compare: https://github.com/ebiggers/libdeflate/compare/${{ steps.check-version.outputs.current }}...${{ steps.check-version.outputs.latest }}
Auto-updated by [this workflow](https://github.com/oven-sh/bun/actions/workflows/update-libdeflate.yml)

View File

@@ -89,6 +89,4 @@ jobs:
Updates lolhtml to version ${{ steps.check-version.outputs.tag }}
Compare: https://github.com/cloudflare/lol-html/compare/${{ steps.check-version.outputs.current }}...${{ steps.check-version.outputs.latest }}
Auto-updated by [this workflow](https://github.com/oven-sh/bun/actions/workflows/update-lolhtml.yml)

View File

@@ -89,6 +89,4 @@ jobs:
Updates lshpack to version ${{ steps.check-version.outputs.tag }}
Compare: https://github.com/litespeedtech/ls-hpack/compare/${{ steps.check-version.outputs.current }}...${{ steps.check-version.outputs.latest }}
Auto-updated by [this workflow](https://github.com/oven-sh/bun/actions/workflows/update-lshpack.yml)

View File

@@ -106,6 +106,4 @@ jobs:
Updates SQLite to version ${{ steps.check-version.outputs.latest }}
Compare: https://sqlite.org/src/vdiff?from=${{ steps.check-version.outputs.current }}&to=${{ steps.check-version.outputs.latest }}
Auto-updated by [this workflow](https://github.com/oven-sh/bun/actions/workflows/update-sqlite3.yml)

View File

@@ -1,16 +1,4 @@
# Tell LLDB what to do when the debugged process receives SIGPWR: pass it through to the process
# (-p), but do not stop the process (-s) or notify the user (-n).
#
# JSC's garbage collector sends this signal (as configured by Bun WebKit in
# Thread::initializePlatformThreading() in ThreadingPOSIX.cpp) to the JS thread to suspend or resume
# it. So stopping the process would just create noise when debugging any long-running script.
process handle -p true -s false -n false SIGPWR
# command script import vendor/zig/tools/lldb_pretty_printers.py
command script import vendor/WebKit/Tools/lldb/lldb_webkit.py
command script import misctools/lldb/lldb_pretty_printers.py
type category enable zig.lang
type category enable zig.std
command script import misctools/lldb/lldb_webkit.py
command script delete btjs
command alias btjs p {printf("gathering btjs trace...\n");printf("%s\n", (char*)dumpBtjsTrace())}
# type summary add --summary-string "${var} | inner=${var[0-30]}, source=${var[33-64]}, tag=${var[31-32]}" "unsigned long"

47
.vscode/launch.json generated vendored
View File

@@ -22,6 +22,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -37,6 +38,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -58,6 +60,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -73,6 +76,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -88,6 +92,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -103,6 +108,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -119,6 +125,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
"serverReadyAction": {
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
@@ -140,6 +147,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
"serverReadyAction": {
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
@@ -161,6 +169,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -179,6 +188,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -193,6 +203,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -210,6 +221,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -224,6 +236,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -240,6 +253,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
"serverReadyAction": {
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
@@ -261,6 +275,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
"serverReadyAction": {
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
@@ -282,6 +297,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -297,6 +313,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -312,6 +329,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -327,6 +345,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -342,6 +361,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -358,6 +378,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
"serverReadyAction": {
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
@@ -379,6 +400,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
"serverReadyAction": {
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
@@ -399,6 +421,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
// bun test [*]
{
@@ -414,6 +437,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -428,6 +452,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -443,6 +468,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
"serverReadyAction": {
"pattern": "https://debug.bun.sh/#localhost:([0-9]+)/",
"uriFormat": "https://debug.bun.sh/#ws://localhost:%s/",
@@ -462,6 +488,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
{
"type": "lldb",
@@ -476,6 +503,7 @@
},
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
// Windows: bun test [file]
{
@@ -1101,24 +1129,7 @@
],
"console": "internalConsole",
// Don't pause when the GC runs while the debugger is open.
},
{
"type": "bun",
"name": "[JS] bun test [file]",
"runtime": "${workspaceFolder}/build/debug/bun-debug",
"runtimeArgs": ["test", "${file}"],
"cwd": "${workspaceFolder}",
"env": {
"BUN_DEBUG_QUIET_LOGS": "1",
"BUN_GARBAGE_COLLECTOR_LEVEL": "2",
},
},
{
"type": "midas-rr",
"request": "attach",
"name": "rr",
"trace": "Off",
"setupCommands": ["handle SIGPWR nostop noprint pass"],
"postRunCommands": ["command source '${workspaceFolder}/misctools/lldb/lldb_commands'"],
},
],
"inputs": [

View File

@@ -80,7 +80,7 @@ $ sudo zypper install clang18 lld18 llvm18
{% /codetabs %}
If none of the above solutions apply, you will have to install it [manually](https://github.com/llvm/llvm-project/releases/tag/llvmorg-19.1.7).
If none of the above solutions apply, you will have to install it [manually](https://github.com/llvm/llvm-project/releases/tag/llvmorg-18.1.8).
Make sure Clang/LLVM 18 is in your path:
@@ -205,30 +205,18 @@ WebKit is not cloned by default (to save time and disk space). To clone and buil
# Clone WebKit into ./vendor/WebKit
$ git clone https://github.com/oven-sh/WebKit vendor/WebKit
# Check out the commit hash specified in `set(WEBKIT_VERSION <commit_hash>)` in cmake/tools/SetupWebKit.cmake
$ git -C vendor/WebKit checkout <commit_hash>
# Make a debug build of JSC. This will output build artifacts in ./vendor/WebKit/WebKitBuild/Debug
# Optionally, you can use `make jsc` for a release build
$ make jsc-debug && rm vendor/WebKit/WebKitBuild/Debug/JavaScriptCore/DerivedSources/inspector/InspectorProtocolObjects.h
$ make jsc-debug
# Build bun with the local JSC build
$ bun run build:local
```
Using `bun run build:local` will build Bun in the `./build/debug-local` directory (instead of `./build/debug`), you'll have to change a couple of places to use this new directory:
- The first line in [`src/js/builtins.d.ts`](/src/js/builtins.d.ts)
- The `CompilationDatabase` line in [`.clangd` config](/.clangd) should be `CompilationDatabase: build/debug-local`
- In [`build.zig`](/build.zig), the `codegen_path` option should be `build/debug-local/codegen` (instead of `build/debug/codegen`)
- In [`.vscode/launch.json`](/.vscode/launch.json), many configurations use `./build/debug/`, change them as you see fit
Note that the WebKit folder, including build artifacts, is 8GB+ in size.
If you are using a JSC debug build and using VScode, make sure to run the `C/C++: Select a Configuration` command to configure intellisense to find the debug headers.
Note that if you change make changes to our [WebKit fork](https://github.com/oven-sh/WebKit), you will also have to change [`SetupWebKit.cmake`](/cmake/tools/SetupWebKit.cmake) to point to the commit hash.
## Troubleshooting
### 'span' file not found on Ubuntu

2
LATEST
View File

@@ -1 +1 @@
1.2.5
1.2.4

View File

@@ -91,9 +91,9 @@ ZIG ?= $(shell which zig 2>/dev/null || echo -e "error: Missing zig. Please make
# This is easier to happen than you'd expect.
# Using realpath here causes issues because clang uses clang++ as a symlink
# so if that's resolved, it won't build for C++
REAL_CC = $(shell which clang-19 2>/dev/null || which clang 2>/dev/null)
REAL_CXX = $(shell which clang++-19 2>/dev/null || which clang++ 2>/dev/null)
CLANG_FORMAT = $(shell which clang-format-19 2>/dev/null || which clang-format 2>/dev/null)
REAL_CC = $(shell which clang-18 2>/dev/null || which clang 2>/dev/null)
REAL_CXX = $(shell which clang++-18 2>/dev/null || which clang++ 2>/dev/null)
CLANG_FORMAT = $(shell which clang-format-18 2>/dev/null || which clang-format 2>/dev/null)
CC = $(REAL_CC)
CXX = $(REAL_CXX)
@@ -117,14 +117,14 @@ CC_WITH_CCACHE = $(CCACHE_PATH) $(CC)
ifeq ($(OS_NAME),darwin)
# Find LLVM
ifeq ($(wildcard $(LLVM_PREFIX)),)
LLVM_PREFIX = $(shell brew --prefix llvm@19)
LLVM_PREFIX = $(shell brew --prefix llvm@18)
endif
ifeq ($(wildcard $(LLVM_PREFIX)),)
LLVM_PREFIX = $(shell brew --prefix llvm)
endif
ifeq ($(wildcard $(LLVM_PREFIX)),)
# This is kinda ugly, but I can't find a better way to error :(
LLVM_PREFIX = $(shell echo -e "error: Unable to find llvm. Please run 'brew install llvm@19' or set LLVM_PREFIX=/path/to/llvm")
LLVM_PREFIX = $(shell echo -e "error: Unable to find llvm. Please run 'brew install llvm@18' or set LLVM_PREFIX=/path/to/llvm")
endif
LDFLAGS += -L$(LLVM_PREFIX)/lib
@@ -164,7 +164,7 @@ CMAKE_FLAGS_WITHOUT_RELEASE = -DCMAKE_C_COMPILER=$(CC) \
-DCMAKE_OSX_DEPLOYMENT_TARGET=$(MIN_MACOS_VERSION) \
$(CMAKE_CXX_COMPILER_LAUNCHER_FLAG) \
-DCMAKE_AR=$(AR) \
-DCMAKE_RANLIB=$(which llvm-19-ranlib 2>/dev/null || which llvm-ranlib 2>/dev/null) \
-DCMAKE_RANLIB=$(which llvm-18-ranlib 2>/dev/null || which llvm-ranlib 2>/dev/null) \
-DCMAKE_CXX_STANDARD=20 \
-DCMAKE_C_STANDARD=17 \
-DCMAKE_CXX_STANDARD_REQUIRED=ON \
@@ -191,7 +191,7 @@ endif
ifeq ($(OS_NAME),linux)
LIBICONV_PATH =
AR = $(shell which llvm-ar-19 2>/dev/null || which llvm-ar 2>/dev/null || which ar 2>/dev/null)
AR = $(shell which llvm-ar-18 2>/dev/null || which llvm-ar 2>/dev/null || which ar 2>/dev/null)
endif
OPTIMIZATION_LEVEL=-O3 $(MARCH_NATIVE)
@@ -255,7 +255,7 @@ DEFAULT_LINKER_FLAGS= -pthread -ldl
endif
ifeq ($(OS_NAME),darwin)
_MIMALLOC_OBJECT_FILE = 0
JSC_BUILD_STEPS += jsc-build-mac
JSC_BUILD_STEPS += jsc-build-mac jsc-copy-headers
JSC_BUILD_STEPS_DEBUG += jsc-build-mac-debug
_MIMALLOC_FILE = libmimalloc.a
_MIMALLOC_INPUT_PATH = libmimalloc.a
@@ -286,7 +286,7 @@ STRIP=/usr/bin/strip
endif
ifeq ($(OS_NAME),linux)
STRIP=$(shell which llvm-strip 2>/dev/null || which llvm-strip-19 2>/dev/null || which strip 2>/dev/null || echo "Missing strip")
STRIP=$(shell which llvm-strip 2>/dev/null || which llvm-strip-18 2>/dev/null || which strip 2>/dev/null || echo "Missing strip")
endif
@@ -674,7 +674,7 @@ endif
.PHONY: assert-deps
assert-deps:
@echo "Checking if the required utilities are available..."
@if [ $(CLANG_VERSION) -lt "19" ]; then echo -e "ERROR: clang version >=19 required, found: $(CLANG_VERSION). Install with:\n\n $(POSIX_PKG_MANAGER) install llvm@19"; exit 1; fi
@if [ $(CLANG_VERSION) -lt "18" ]; then echo -e "ERROR: clang version >=18 required, found: $(CLANG_VERSION). Install with:\n\n $(POSIX_PKG_MANAGER) install llvm@18"; exit 1; fi
@cmake --version >/dev/null 2>&1 || (echo -e "ERROR: cmake is required."; exit 1)
@$(PYTHON) --version >/dev/null 2>&1 || (echo -e "ERROR: python is required."; exit 1)
@$(ESBUILD) --version >/dev/null 2>&1 || (echo -e "ERROR: esbuild is required."; exit 1)
@@ -924,7 +924,7 @@ bun-codesign-release-local-debug:
.PHONY: jsc
jsc: jsc-build
jsc: jsc-build jsc-copy-headers jsc-bindings
.PHONY: jsc-debug
jsc-debug: jsc-build-debug
.PHONY: jsc-build

View File

@@ -1,53 +0,0 @@
import crypto from "node:crypto";
import { bench, run } from "../runner.mjs";
// Pre-generate DH params to avoid including setup in benchmarks
const dhSize = 1024; // Reduced from 2048 for faster testing
const dh = crypto.createDiffieHellman(dhSize);
const dhPrime = dh.getPrime();
const dhGenerator = dh.getGenerator();
// Classical Diffie-Hellman
bench("DH - generateKeys", () => {
const alice = crypto.createDiffieHellman(dhPrime, dhGenerator);
return alice.generateKeys();
});
bench("DH - computeSecret", () => {
// Setup
const alice = crypto.createDiffieHellman(dhPrime, dhGenerator);
const aliceKey = alice.generateKeys();
const bob = crypto.createDiffieHellman(dhPrime, dhGenerator);
const bobKey = bob.generateKeys();
// Benchmark just the secret computation
return alice.computeSecret(bobKey);
});
// ECDH with prime256v1 (P-256)
bench("ECDH-P256 - generateKeys", () => {
const ecdh = crypto.createECDH("prime256v1");
return ecdh.generateKeys();
});
bench("ECDH-P256 - computeSecret", () => {
// Setup
const alice = crypto.createECDH("prime256v1");
const aliceKey = alice.generateKeys();
const bob = crypto.createECDH("prime256v1");
const bobKey = bob.generateKeys();
// Benchmark just the secret computation
return alice.computeSecret(bobKey);
});
// ECDH with secp384r1 (P-384)
bench("ECDH-P384 - computeSecret", () => {
const alice = crypto.createECDH("secp384r1");
const aliceKey = alice.generateKeys();
const bob = crypto.createECDH("secp384r1");
const bobKey = bob.generateKeys();
return alice.computeSecret(bobKey);
});
await run();

View File

@@ -1,44 +0,0 @@
import crypto from "node:crypto";
import { bench, run } from "../runner.mjs";
function generateTestKeyPairs() {
const curves = crypto.getCurves();
const keys = {};
for (const curve of curves) {
const ecdh = crypto.createECDH(curve);
ecdh.generateKeys();
keys[curve] = {
compressed: ecdh.getPublicKey("hex", "compressed"),
uncompressed: ecdh.getPublicKey("hex", "uncompressed"),
instance: ecdh,
};
}
return keys;
}
const testKeys = generateTestKeyPairs();
bench("ECDH key format - P256 compressed to uncompressed", () => {
const publicKey = testKeys["prime256v1"].compressed;
return crypto.ECDH.convertKey(publicKey, "prime256v1", "hex", "hex", "uncompressed");
});
bench("ECDH key format - P256 uncompressed to compressed", () => {
const publicKey = testKeys["prime256v1"].uncompressed;
return crypto.ECDH.convertKey(publicKey, "prime256v1", "hex", "hex", "compressed");
});
bench("ECDH key format - P384 compressed to uncompressed", () => {
const publicKey = testKeys["secp384r1"].compressed;
return crypto.ECDH.convertKey(publicKey, "secp384r1", "hex", "hex", "uncompressed");
});
bench("ECDH key format - P384 uncompressed to compressed", () => {
const publicKey = testKeys["secp384r1"].uncompressed;
return crypto.ECDH.convertKey(publicKey, "secp384r1", "hex", "hex", "compressed");
});
await run();

View File

@@ -1,50 +0,0 @@
import crypto from "node:crypto";
import { bench, run } from "../runner.mjs";
// Sample keys with different lengths
const keys = {
short: "secret",
long: "this-is-a-much-longer-secret-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
};
// Test parameters
const salts = ["", "salt"];
const infos = ["", "info"];
const hashes = ["sha256", "sha512"];
const sizes = [10, 1024];
// Benchmark sync HKDF
for (const hash of hashes) {
for (const keyName of Object.keys(keys)) {
const key = keys[keyName];
for (const size of sizes) {
bench(`hkdfSync ${hash} ${keyName}-key ${size} bytes`, () => {
return crypto.hkdfSync(hash, key, "salt", "info", size);
});
}
}
}
// Benchmark different combinations of salt and info
for (const salt of salts) {
for (const info of infos) {
bench(`hkdfSync sha256 with ${salt ? "salt" : "no-salt"} and ${info ? "info" : "no-info"}`, () => {
return crypto.hkdfSync("sha256", "secret", salt, info, 64);
});
}
}
// Benchmark async HKDF (using promises for cleaner benchmark)
// Note: async benchmarks in Mitata require returning a Promise
for (const hash of hashes) {
bench(`hkdf ${hash} async`, async () => {
return new Promise((resolve, reject) => {
crypto.hkdf(hash, "secret", "salt", "info", 64, (err, derivedKey) => {
if (err) reject(err);
else resolve(derivedKey);
});
});
});
}
await run();

View File

@@ -1,43 +0,0 @@
import { checkPrime, checkPrimeSync, generatePrime, generatePrimeSync } from "node:crypto";
import { bench, run } from "../runner.mjs";
const prime512 = generatePrimeSync(512);
const prime2048 = generatePrimeSync(2048);
bench("checkPrimeSync 512", () => {
return checkPrimeSync(prime512);
});
bench("checkPrimeSync 2048", () => {
return checkPrimeSync(prime2048);
});
bench("checkPrime 512", async () => {
const promises = Array.from({ length: 10 }, () => new Promise(resolve => checkPrime(prime512, resolve)));
await Promise.all(promises);
});
bench("checkPrime 2048", async () => {
const promises = Array.from({ length: 10 }, () => new Promise(resolve => checkPrime(prime2048, resolve)));
await Promise.all(promises);
});
bench("generatePrimeSync 512", () => {
return generatePrimeSync(512);
});
bench("generatePrimeSync 2048", () => {
return generatePrimeSync(2048);
});
bench("generatePrime 512", async () => {
const promises = Array.from({ length: 10 }, () => new Promise(resolve => generatePrime(512, resolve)));
await Promise.all(promises);
});
bench("generatePrime 2048", async () => {
const promises = Array.from({ length: 10 }, () => new Promise(resolve => generatePrime(2048, resolve)));
await Promise.all(promises);
});
await run();

View File

@@ -1,50 +0,0 @@
import crypto from "crypto";
import { bench, run } from "../runner.mjs";
bench("randomInt - sync", () => {
crypto.randomInt(1000);
});
bench("randomInt - async", async () => {
const { promise, resolve } = Promise.withResolvers();
crypto.randomInt(1000, () => {
resolve();
});
await promise;
});
bench("randonBytes - 32", () => {
crypto.randomBytes(32);
});
bench("randomBytes - 256", () => {
crypto.randomBytes(256);
});
const buf = Buffer.alloc(256);
bench("randomFill - 32", async () => {
const { promise, resolve } = Promise.withResolvers();
crypto.randomFill(buf, 0, 32, () => {
resolve();
});
await promise;
});
bench("randomFill - 256", async () => {
const { promise, resolve } = Promise.withResolvers();
crypto.randomFill(buf, 0, 256, () => {
resolve();
});
await promise;
});
bench("randomFillSync - 32", () => {
crypto.randomFillSync(buf, 0, 32);
});
bench("randomFillSync - 256", () => {
crypto.randomFillSync(buf, 0, 256);
});
await run();

View File

@@ -12,7 +12,6 @@
"eventemitter3": "^5.0.0",
"execa": "^8.0.1",
"fast-glob": "3.3.1",
"fastify": "^5.0.0",
"fdir": "^6.1.0",
"mitata": "^1.0.25",
"react": "^18.3.1",

View File

@@ -1,13 +0,0 @@
import express from "express";
const app = express();
const port = 3000;
var i = 0;
app.get("/", (req, res) => {
res.send("Hello World!" + i++);
});
app.listen(port, () => {
console.log(`Express app listening at http://localhost:${port}`);
});

View File

@@ -1,20 +0,0 @@
import Fastify from "fastify";
const fastify = Fastify({
logger: false,
});
fastify.get("/", async (request, reply) => {
return { hello: "world" };
});
const start = async () => {
try {
await fastify.listen({ port: 3000 });
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();

View File

@@ -19,17 +19,17 @@ const OperatingSystem = @import("src/env.zig").OperatingSystem;
const pathRel = fs.path.relative;
/// Do not rename this constant. It is scanned by some scripts to determine which zig version to install.
const recommended_zig_version = "0.14.0";
const recommended_zig_version = "0.14.0-dev.2987+183bb8b08";
comptime {
if (!std.mem.eql(u8, builtin.zig_version_string, recommended_zig_version)) {
@compileError(
"" ++
"Bun requires Zig version " ++ recommended_zig_version ++ ", but you have " ++
builtin.zig_version_string ++ ". This is automatically configured via Bun's " ++
"CMake setup. You likely meant to run `bun run build`. If you are trying to " ++
"upgrade the Zig compiler, edit ZIG_COMMIT in cmake/tools/SetupZig.cmake or " ++
"comment this error out.",
"Bun requires Zig version " ++ recommended_zig_version ++ " (found " ++
builtin.zig_version_string ++ "). This is " ++
"automatically configured via Bun's CMake setup. You likely meant to run " ++
"`bun setup`. If you are trying to upgrade the Zig compiler, " ++
"run `./scripts/download-zig.sh master` or comment this message out.",
);
}
}
@@ -319,21 +319,7 @@ pub fn build(b: *Build) !void {
.{ .os = .linux, .arch = .aarch64 },
.{ .os = .linux, .arch = .x86_64, .musl = true },
.{ .os = .linux, .arch = .aarch64, .musl = true },
}, &.{ .Debug, .ReleaseFast });
}
// zig build check-all-debug
{
const step = b.step("check-all-debug", "Check for semantic analysis errors on all supported platforms in debug mode");
addMultiCheck(b, step, build_options, &.{
.{ .os = .windows, .arch = .x86_64 },
.{ .os = .mac, .arch = .x86_64 },
.{ .os = .mac, .arch = .aarch64 },
.{ .os = .linux, .arch = .x86_64 },
.{ .os = .linux, .arch = .aarch64 },
.{ .os = .linux, .arch = .x86_64, .musl = true },
.{ .os = .linux, .arch = .aarch64, .musl = true },
}, &.{.Debug});
});
}
// zig build check-windows
@@ -341,21 +327,21 @@ pub fn build(b: *Build) !void {
const step = b.step("check-windows", "Check for semantic analysis errors on Windows");
addMultiCheck(b, step, build_options, &.{
.{ .os = .windows, .arch = .x86_64 },
}, &.{ .Debug, .ReleaseFast });
});
}
{
const step = b.step("check-macos", "Check for semantic analysis errors on Windows");
addMultiCheck(b, step, build_options, &.{
.{ .os = .mac, .arch = .x86_64 },
.{ .os = .mac, .arch = .aarch64 },
}, &.{ .Debug, .ReleaseFast });
});
}
{
const step = b.step("check-linux", "Check for semantic analysis errors on Windows");
addMultiCheck(b, step, build_options, &.{
.{ .os = .linux, .arch = .x86_64 },
.{ .os = .linux, .arch = .aarch64 },
}, &.{ .Debug, .ReleaseFast });
});
}
// zig build translate-c-headers
@@ -383,10 +369,9 @@ pub fn addMultiCheck(
parent_step: *Step,
root_build_options: BunBuildOptions,
to_check: []const struct { os: OperatingSystem, arch: Arch, musl: bool = false },
optimize: []const std.builtin.OptimizeMode,
) void {
for (to_check) |check| {
for (optimize) |mode| {
for ([_]std.builtin.Mode{ .Debug, .ReleaseFast }) |mode| {
const check_target = b.resolveTargetQuery(.{
.os_tag = OperatingSystem.stdOSTag(check.os),
.cpu_arch = check.arch,

View File

@@ -2,7 +2,3 @@
# https://github.com/oven-sh/bun/issues/16289
[test]
preload = ["./test/js/node/harness.ts", "./test/preload.ts"]
[install]
# Node.js never auto-installs modules.
auto = "disable"

View File

@@ -419,15 +419,7 @@ function(register_command)
list(APPEND CMD_EFFECTIVE_OUTPUTS ${artifact})
if(BUILDKITE)
file(RELATIVE_PATH filename ${BUILD_PATH} ${artifact})
if(filename STREQUAL "libbun-profile.a")
# libbun-profile.a is now over 5gb in size, compress it first
list(APPEND CMD_COMMANDS COMMAND ${CMAKE_COMMAND} -E chdir ${BUILD_PATH} rm -r ${BUILD_PATH}/codegen)
list(APPEND CMD_COMMANDS COMMAND ${CMAKE_COMMAND} -E chdir ${BUILD_PATH} rm -r ${CACHE_PATH})
list(APPEND CMD_COMMANDS COMMAND ${CMAKE_COMMAND} -E chdir ${BUILD_PATH} gzip -6 libbun-profile.a)
list(APPEND CMD_COMMANDS COMMAND ${CMAKE_COMMAND} -E chdir ${BUILD_PATH} buildkite-agent artifact upload libbun-profile.a.gz)
else()
list(APPEND CMD_COMMANDS COMMAND ${CMAKE_COMMAND} -E chdir ${BUILD_PATH} buildkite-agent artifact upload ${filename})
endif()
list(APPEND CMD_COMMANDS COMMAND ${CMAKE_COMMAND} -E chdir ${BUILD_PATH} buildkite-agent artifact upload ${filename})
endif()
endforeach()

View File

@@ -92,6 +92,10 @@ else()
optionx(ENABLE_ASAN BOOL "If ASAN support should be enabled" DEFAULT OFF)
endif()
# enabling ASAN roughly doubles `zig build` times so we disable it by default, even in debug builds.
# It may optionally be enabled this way.
optionx(ENABLE_ZIG_ASAN BOOL "If ASAN support should be enabled when compiling Zig code" DEFAULT OFF)
optionx(ENABLE_PRETTIER BOOL "If prettier should be ran" DEFAULT OFF)
if(USE_VALGRIND AND NOT USE_BASELINE)

View File

@@ -590,6 +590,7 @@ register_command(
-Doptimize=${ZIG_OPTIMIZE}
-Dcpu=${ZIG_CPU}
-Denable_logs=$<IF:$<BOOL:${ENABLE_LOGS}>,true,false>
-Denable_asan=$<IF:$<BOOL:${ENABLE_ZIG_ASAN}>,true,false>
-Dversion=${VERSION}
-Dreported_nodejs_version=${NODEJS_VERSION}
-Dcanary=${CANARY_REVISION}
@@ -738,7 +739,7 @@ endif()
# --- C/C++ Properties ---
set_target_properties(${bun} PROPERTIES
CXX_STANDARD 23
CXX_STANDARD 20
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS YES
CXX_VISIBILITY_PRESET hidden
@@ -747,18 +748,6 @@ set_target_properties(${bun} PROPERTIES
VISIBILITY_INLINES_HIDDEN YES
)
if (NOT WIN32)
# Enable precompiled headers
# Only enable in these scenarios:
# 1. NOT in CI, OR
# 2. In CI AND BUN_CPP_ONLY is enabled
if(NOT CI OR (CI AND BUN_CPP_ONLY))
target_precompile_headers(${bun} PRIVATE
"$<$<COMPILE_LANGUAGE:CXX>:${CWD}/src/bun.js/bindings/root.h>"
)
endif()
endif()
# --- C/C++ Includes ---
if(WIN32)
@@ -913,10 +902,6 @@ if(NOT WIN32)
-Werror
)
endif()
else()
target_compile_options(${bun} PUBLIC
-Wno-nullability-completeness
)
endif()
# --- Linker options ---
@@ -959,17 +944,28 @@ endif()
if(LINUX)
if(NOT ABI STREQUAL "musl")
target_link_options(${bun} PUBLIC
-Wl,--wrap=exp
-Wl,--wrap=expf
-Wl,--wrap=fcntl64
-Wl,--wrap=log
-Wl,--wrap=log2
-Wl,--wrap=log2f
-Wl,--wrap=logf
-Wl,--wrap=pow
-Wl,--wrap=powf
)
# on arm64
if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm|ARM|arm64|ARM64|aarch64|AARCH64")
target_link_options(${bun} PUBLIC
-Wl,--wrap=exp
-Wl,--wrap=expf
-Wl,--wrap=fcntl64
-Wl,--wrap=log
-Wl,--wrap=log2
-Wl,--wrap=log2f
-Wl,--wrap=logf
-Wl,--wrap=pow
-Wl,--wrap=powf
)
else()
target_link_options(${bun} PUBLIC
-Wl,--wrap=exp
-Wl,--wrap=expf
-Wl,--wrap=log2f
-Wl,--wrap=logf
-Wl,--wrap=powf
)
endif()
endif()
if(NOT ABI STREQUAL "musl")

View File

@@ -4,7 +4,7 @@ register_repository(
REPOSITORY
cloudflare/lol-html
COMMIT
67f1d4ffd6b74db7e053fb129dcce620193c180d
4f8becea13a0021c8b71abd2dcc5899384973b66
)
set(LOLHTML_CWD ${VENDOR_PATH}/lolhtml/c-api)

View File

@@ -120,9 +120,6 @@ foreach(i RANGE ${BUILDKITE_JOBS_MAX_INDEX})
endif()
if(BUILDKITE)
if(BUILDKITE_ARTIFACT_PATH STREQUAL "libbun-profile.a")
set(BUILDKITE_ARTIFACT_PATH libbun-profile.a.gz)
endif()
set(BUILDKITE_DOWNLOAD_COMMAND buildkite-agent artifact download ${BUILDKITE_ARTIFACT_PATH} . --build ${BUILDKITE_BUILD_UUID} --step ${BUILDKITE_JOB_ID})
else()
set(BUILDKITE_DOWNLOAD_COMMAND curl -L -o ${BUILDKITE_ARTIFACT_PATH} ${BUILDKITE_ARTIFACTS_URL}/${BUILDKITE_ARTIFACT_ID})
@@ -138,20 +135,6 @@ foreach(i RANGE ${BUILDKITE_JOBS_MAX_INDEX})
OUTPUT
${BUILD_PATH}/${BUILDKITE_ARTIFACT_PATH}
)
if(BUILDKITE_ARTIFACT_PATH STREQUAL "libbun-profile.a.gz")
add_custom_command(
COMMENT
"Unpacking libbun-profile.a.gz"
VERBATIM COMMAND
gunzip libbun-profile.a.gz
WORKING_DIRECTORY
${BUILD_PATH}
OUTPUT
${BUILD_PATH}/libbun-profile.a
DEPENDS
${BUILD_PATH}/libbun-profile.a.gz
)
endif()
endforeach()
list(APPEND BUILDKITE_JOBS_MATCH ${BUILDKITE_JOB_NAME})

View File

@@ -36,8 +36,7 @@ endif()
string(REPLACE "\n" ";" GIT_CHANGED_SOURCES "${GIT_DIFF}")
if(CI)
set(GIT_CHANGED_SOURCES "${GIT_CHANGED_SOURCES}")
message(STATUS "Set GIT_CHANGED_SOURCES: ${GIT_CHANGED_SOURCES}")
setx(GIT_CHANGED_SOURCES ${GIT_CHANGED_SOURCES})
endif()
list(TRANSFORM GIT_CHANGED_SOURCES PREPEND ${CWD}/)

View File

@@ -12,7 +12,7 @@ if(NOT ENABLE_LLVM)
return()
endif()
set(DEFAULT_LLVM_VERSION "19.1.7")
set(DEFAULT_LLVM_VERSION "18.1.8")
optionx(LLVM_VERSION STRING "The version of LLVM to use" DEFAULT ${DEFAULT_LLVM_VERSION})

View File

@@ -2,7 +2,7 @@ option(WEBKIT_VERSION "The version of WebKit to use")
option(WEBKIT_LOCAL "If a local version of WebKit should be used instead of downloading")
if(NOT WEBKIT_VERSION)
set(WEBKIT_VERSION 91bf2baced1b1309c7e05f19177c97fefec20976)
set(WEBKIT_VERSION 0d7d28b5907f628331d4a944c3436856447fbf64)
endif()
string(SUBSTRING ${WEBKIT_VERSION} 0 16 WEBKIT_VERSION_PREFIX)

View File

@@ -20,7 +20,7 @@ else()
unsupported(CMAKE_SYSTEM_NAME)
endif()
set(ZIG_COMMIT "cd1995944508e4c946deb75bd70947d302e0db37")
set(ZIG_COMMIT "bb9d6ab2c0bbbf20cc24dad03e88f3b3ffdb7de7")
optionx(ZIG_TARGET STRING "The zig target to use" DEFAULT ${DEFAULT_ZIG_TARGET})
if(CMAKE_BUILD_TYPE STREQUAL "Release")

View File

@@ -12,3 +12,5 @@ Alternatively, use `process.dlopen`:
let mod = { exports: {} };
process.dlopen(mod, "./my-node-module.node");
```
Bun polyfills the [`detect-libc`](https://npmjs.com/package/detect-libc) package, which is used by many Node-API modules to detect which `.node` binding to `require`.

View File

@@ -715,7 +715,7 @@ await S3Client.delete("my-file.txt", credentials);
await S3Client.unlink("my-file.txt", credentials);
```
## `s3://` protocol
## s3:// protocol
To make it easier to use the same code for local files and S3 files, the `s3://` protocol is supported in `fetch` and `Bun.file()`.

View File

@@ -77,16 +77,6 @@ console.log(text); // "const input = "hello world".repeat(400); ..."
---
- `ReadableStream`
- Use a readable stream as input.
---
- `Blob`
- Use a blob as input.
---
- `number`
- Read from the file with a given file descriptor.
@@ -139,13 +129,13 @@ Configure the output stream by passing one of the following values to `stdout/st
---
- `"ignore"`
- Discard the output.
- `Bun.file()`
- Write to the specified file.
---
- `Bun.file()`
- Write to the specified file.
- `null`
- Write to `/dev/null`.
---
@@ -184,8 +174,7 @@ const proc = Bun.spawn(["bun", "--version"]);
proc.kill();
proc.killed; // true
proc.kill(15); // specify a signal code
proc.kill("SIGTERM"); // specify a signal name
proc.kill(); // specify an exit code
```
The parent `bun` process will not terminate until all child processes have exited. Use `proc.unref()` to detach the child process from the parent.
@@ -195,64 +184,6 @@ const proc = Bun.spawn(["bun", "--version"]);
proc.unref();
```
## Resource usage
You can get information about the process's resource usage after it has exited:
```ts
const proc = Bun.spawn(["bun", "--version"]);
await proc.exited;
const usage = proc.resourceUsage();
console.log(`Max memory used: ${usage.maxRSS} bytes`);
console.log(`CPU time (user): ${usage.cpuTime.user} µs`);
console.log(`CPU time (system): ${usage.cpuTime.system} µs`);
```
## Using AbortSignal
You can abort a subprocess using an `AbortSignal`:
```ts
const controller = new AbortController();
const { signal } = controller;
const proc = Bun.spawn({
cmd: ["sleep", "100"],
signal,
});
// Later, to abort the process:
controller.abort();
```
## Using timeout and killSignal
You can set a timeout for a subprocess to automatically terminate after a specific duration:
```ts
// Kill the process after 5 seconds
const proc = Bun.spawn({
cmd: ["sleep", "10"],
timeout: 5000, // 5 seconds in milliseconds
});
await proc.exited; // Will resolve after 5 seconds
```
By default, timed-out processes are killed with the `SIGTERM` signal. You can specify a different signal with the `killSignal` option:
```ts
// Kill the process with SIGKILL after 5 seconds
const proc = Bun.spawn({
cmd: ["sleep", "10"],
timeout: 5000,
killSignal: "SIGKILL", // Can be string name or signal number
});
```
The `killSignal` option also controls which signal is sent when an AbortSignal is aborted.
## Inter-process communication (IPC)
Bun supports direct inter-process communication channel between two `bun` processes. To receive messages from a spawned Bun subprocess, specify an `ipc` handler.
@@ -302,17 +233,11 @@ process.send("Hello from child as string");
process.send({ message: "Hello from child as object" });
```
The `serialization` option controls the underlying communication format between the two processes:
The `ipcMode` option controls the underlying communication format between the two processes:
- `advanced`: (default) Messages are serialized using the JSC `serialize` API, which supports cloning [everything `structuredClone` supports](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm). This does not support transferring ownership of objects.
- `json`: Messages are serialized using `JSON.stringify` and `JSON.parse`, which does not support as many object types as `advanced` does.
To disconnect the IPC channel from the parent process, call:
```ts
childProc.disconnect();
```
### IPC between Bun & Node.js
To use IPC between a `bun` process and a Node.js process, set `serialization: "json"` in `Bun.spawn`. This is because Node.js and Bun use different JavaScript engines with different object serialization formats.
@@ -385,7 +310,7 @@ spawnSync echo hi 1.47 ms/iter (1.14 ms … 2.64 ms) 1.57 ms 2.37 ms
## Reference
A reference of the Spawn API and types are shown below. The real types have complex generics to strongly type the `Subprocess` streams with the options passed to `Bun.spawn` and `Bun.spawnSync`. For full details, find these types as defined [bun.d.ts](https://github.com/oven-sh/bun/blob/main/packages/bun-types/bun.d.ts).
A simple reference of the Spawn API and types are shown below. The real types have complex generics to strongly type the `Subprocess` streams with the options passed to `Bun.spawn` and `Bun.spawnSync`. For full details, find these types as defined [bun.d.ts](https://github.com/oven-sh/bun/blob/main/packages/bun-types/bun.d.ts).
```ts
interface Bun {
@@ -404,25 +329,16 @@ interface Bun {
namespace SpawnOptions {
interface OptionsObject {
cwd?: string;
env?: Record<string, string | undefined>;
stdio?: [Writable, Readable, Readable];
stdin?: Writable;
stdout?: Readable;
stderr?: Readable;
onExit?(
subprocess: Subprocess,
env?: Record<string, string>;
stdin?: SpawnOptions.Readable;
stdout?: SpawnOptions.Writable;
stderr?: SpawnOptions.Writable;
onExit?: (
proc: Subprocess,
exitCode: number | null,
signalCode: number | null,
error?: ErrorLike,
): void | Promise<void>;
ipc?(message: any, subprocess: Subprocess): void;
serialization?: "json" | "advanced";
windowsHide?: boolean;
windowsVerbatimArguments?: boolean;
argv0?: string;
signal?: AbortSignal;
timeout?: number;
killSignal?: string | number;
signalCode: string | null,
error: Error | null,
) => void;
}
type Readable =
@@ -450,62 +366,39 @@ namespace SpawnOptions {
| Request;
}
interface Subprocess extends AsyncDisposable {
readonly stdin: FileSink | number | undefined;
readonly stdout: ReadableStream<Uint8Array> | number | undefined;
readonly stderr: ReadableStream<Uint8Array> | number | undefined;
readonly readable: ReadableStream<Uint8Array> | number | undefined;
interface Subprocess<Stdin, Stdout, Stderr> {
readonly pid: number;
// the exact stream types here are derived from the generic parameters
readonly stdin: number | ReadableStream | FileSink | undefined;
readonly stdout: number | ReadableStream | undefined;
readonly stderr: number | ReadableStream | undefined;
readonly exited: Promise<number>;
readonly exitCode: number | null;
readonly signalCode: NodeJS.Signals | null;
readonly exitCode: number | undefined;
readonly signalCode: Signal | null;
readonly killed: boolean;
kill(exitCode?: number | NodeJS.Signals): void;
ref(): void;
unref(): void;
send(message: any): void;
disconnect(): void;
resourceUsage(): ResourceUsage | undefined;
kill(code?: number): void;
}
interface SyncSubprocess {
stdout: Buffer | undefined;
stderr: Buffer | undefined;
exitCode: number;
success: boolean;
resourceUsage: ResourceUsage;
signalCode?: string;
exitedDueToTimeout?: true;
pid: number;
interface SyncSubprocess<Stdout, Stderr> {
readonly pid: number;
readonly success: boolean;
// the exact buffer types here are derived from the generic parameters
readonly stdout: Buffer | undefined;
readonly stderr: Buffer | undefined;
}
interface ResourceUsage {
contextSwitches: {
voluntary: number;
involuntary: number;
};
type ReadableSubprocess = Subprocess<any, "pipe", "pipe">;
type WritableSubprocess = Subprocess<"pipe", any, any>;
type PipedSubprocess = Subprocess<"pipe", "pipe", "pipe">;
type NullSubprocess = Subprocess<null, null, null>;
cpuTime: {
user: number;
system: number;
total: number;
};
maxRSS: number;
messages: {
sent: number;
received: number;
};
ops: {
in: number;
out: number;
};
shmSize: number;
signalCount: number;
swapCount: number;
}
type ReadableSyncSubprocess = SyncSubprocess<"pipe", "pipe">;
type NullSyncSubprocess = SyncSubprocess<null, null>;
type Signal =
| "SIGABRT"

View File

@@ -11,7 +11,7 @@ Bun.listen({
socket: {
data(socket, data) {}, // message received from client
open(socket) {}, // socket opened
close(socket, error) {}, // socket closed
close(socket) {}, // socket closed
drain(socket) {}, // socket ready for more data
error(socket, error) {}, // error handler
},
@@ -30,7 +30,7 @@ Bun.listen({
open(socket) {},
data(socket, data) {},
drain(socket) {},
close(socket, error) {},
close(socket) {},
error(socket, error) {},
},
});
@@ -122,7 +122,7 @@ const socket = await Bun.connect({
socket: {
data(socket, data) {},
open(socket) {},
close(socket, error) {},
close(socket) {},
drain(socket) {},
error(socket, error) {},

File diff suppressed because it is too large Load Diff

View File

@@ -1,145 +0,0 @@
# CSS Modules
Bun's bundler also supports bundling [CSS modules](https://css-tricks.com/css-modules-part-1-need/) in addition to [regular CSS](/docs/bundler/css) with support for the following features:
- Automatically detecting CSS module files (`.module.css`) with zero configuration
- Composition (`composes` property)
- Importing CSS modules into JSX/TSX
- Warnings/errors for invalid usages of CSS modules
A CSS module is a CSS file (with the `.module.css` extension) where are all class names and animations are scoped to the file. This helps you avoid class name collisions as CSS declarations are globally scoped by default.
Under the hood, Bun's bundler transforms locally scoped class names into unique identifiers.
## Getting started
Create a CSS file with the `.module.css` extension:
```css
/* styles.module.css */
.button {
color: red;
}
/* other-styles.module.css */
.button {
color: blue;
}
```
You can then import this file, for example into a TSX file:
```tsx
import styles from "./styles.module.css";
import otherStyles from "./other-styles.module.css";
export default function App() {
return (
<>
<button className={styles.button}>Red button!</button>
<button className={otherStyles.button}>Blue button!</button>
</>
);
}
```
The `styles` object from importing the CSS module file will be an object with all class names as keys and
their unique identifiers as values:
```tsx
import styles from "./styles.module.css";
import otherStyles from "./other-styles.module.css";
console.log(styles);
console.log(otherStyles);
```
This will output:
```ts
{
button: "button_123";
}
{
button: "button_456";
}
```
As you can see, the class names are unique to each file, avoiding any collisions!
### Composition
CSS modules allow you to _compose_ class selectors together. This lets you reuse style rules across multiple classes.
For example:
```css
/* styles.module.css */
.button {
composes: background;
color: red;
}
.background {
background-color: blue;
}
```
Would be the same as writing:
```css
.button {
background-color: blue;
color: red;
}
.background {
background-color: blue;
}
```
{% callout %}
There are a couple rules to keep in mind when using `composes`:
- A `composes` property must come before any regular CSS properties or declarations
- You can only use `composes` on a **simple selector with a single class name**:
```css
#button {
/* Invalid! `#button` is not a class selector */
composes: background;
}
.button,
.button-secondary {
/* Invalid! `.button, .button-secondary` is not a simple selector */
composes: background;
}
```
{% /callout %}
### Composing from a separate CSS module file
You can also compose from a separate CSS module file:
```css
/* background.module.css */
.background {
background-color: blue;
}
/* styles.module.css */
.button {
composes: background from "./background.module.css";
color: red;
}
```
{% callout %}
When composing classes from separate files, be sure that they do not contain the same properties.
The CSS module spec says that composing classes from separate files with conflicting properties is
undefined behavior, meaning that the output may differ and be unreliable.
{% /callout %}

View File

@@ -1,234 +0,0 @@
Hot Module Replacement (HMR) allows you to update modules in a running
application without needing a full page reload. This preserves the application
state and improves the development experience.
HMR is enabled by default when using Bun's full-stack development server.
## `import.meta.hot` API Reference
Bun implements a client-side HMR API modeled after [Vite's `import.meta.hot` API](https://vitejs.dev/guide/api-hmr.html). It can be checked for with `if (import.meta.hot)`, tree-shaking it in production
```ts
if (import.meta.hot) {
// HMR APIs are available.
}
```
However, **this check is often not needed** as Bun will dead-code-eliminate
calls to all of the HMR APIs in production builds.
```ts
// This entire function call will be removed in production!
import.meta.hot.dispose(() => {
console.log("dispose");
});
```
For this to work, Bun forces these APIs to be called without indirection. That means the following do not work:
```ts#invalid-hmr-usage.ts
// INVALID: Assigning `hot` to a variable
const hot = import.meta.hot;
hot.accept();
// INVALID: Assigning `import.meta` to a variable
const meta = import.meta;
meta.hot.accept();
console.log(meta.hot.data);
// INVALID: Passing to a function
doSomething(import.meta.hot.dispose);
// OK: The full phrase "import.meta.hot.<API>" must be called directly:
import.meta.hot.accept();
// OK: `data` can be passed to functions:
doSomething(import.meta.hot.data);
```
{% callout %}
**Note** — The HMR API is still a work in progress. Some features are missing. HMR can be disabled in `Bun.serve` by setting the `development` option to `{ hmr: false }`.
{% endcallout %}
| | Method | Notes |
| --- | ------------------ | --------------------------------------------------------------------- |
| ✅ | `hot.accept()` | Indicate that a hot update can be replaced gracefully. |
| ✅ | `hot.data` | Persist data between module evaluations. |
| ✅ | `hot.dispose()` | Add a callback function to run when a module is about to be replaced. |
| ❌ | `hot.invalidate()` | |
| ✅ | `hot.on()` | Attach an event listener |
| ✅ | `hot.off()` | Remove an event listener from `on`. |
| ❌ | `hot.send()` | |
| 🚧 | `hot.prune()` | **NOTE**: Callback is currently never called. |
| ✅ | `hot.decline()` | No-op to match Vite's `import.meta.hot` |
### `import.meta.hot.accept()`
The `accept()` method indicates that a module can be hot-replaced. When called
without arguments, it indicates that this module can be replaced simply by
re-evaluating the file. After a hot update, importers of this module will be
automatically patched.
```ts#index.ts
import { getCount } from "./foo.ts";
console.log("count is ", getCount());
import.meta.hot.accept();
export function getNegativeCount() {
return -getCount();
}
```
This creates a hot-reloading boundary for all of the files that `index.ts`
imports. That means whenever `foo.ts` or any of its dependencies are saved, the
update will bubble up to `index.ts` will re-evaluate. Files that import
`index.ts` will then be patched to import the new version of
`getNegativeCount()`. If only `index.ts` is updated, only the one file will be
re-evaluated, and the counter in `foo.ts` is reused.
This may be used in combination with `import.meta.hot.data` to transfer state
from the previous module to the new one.
When no modules call `import.meta.hot.accept()` (and there isn't React Fast
Refresh or a plugin calling it for you), the page will reload when the file
updates, and a console warning shows which files were invalidated. This warning
is safe to ignore if it makes more sense to rely on full page reloads.
#### With callback
When provided one callback, `import.meta.hot.accept` will function how it does
in Vite. Instead of patching the importers of this module, it will call the
callback with the new module.
```ts
export const count = 0;
import.meta.hot.accept(newModule => {
if (newModule) {
// newModule is undefined when SyntaxError happened
console.log("updated: count is now ", newModule.count);
}
});
```
Prefer using `import.meta.hot.accept()` without an argument as it usually makes your code easier to understand.
#### Accepting other modules
```ts
import { count } from "./foo";
import.meta.hot.accept("./foo", () => {
if (!newModule) return;
console.log("updated: count is now ", count);
});
```
Indicates that a dependency's module can be accepted. When the dependency is updated, the callback will be called with the new module.
#### With multiple dependencies
```ts
import.meta.hot.accept(["./foo", "./bar"], newModules => {
// newModules is an array where each item corresponds to the updated module
// or undefined if that module had a syntax error
});
```
Indicates that multiple dependencies' modules can be accepted. This variant accepts an array of dependencies, where the callback will receive the updated modules, and `undefined` for any that had errors.
### `import.meta.hot.data`
`import.meta.hot.data` maintains state between module instances during hot
replacement, enabling data transfer from previous to new versions. When
`import.meta.hot.data` is written into, Bun will also mark this module as
capable of self-accepting (equivalent of calling `import.meta.hot.accept()`).
```ts
import { createRoot } from "react-dom/client";
import { App } from "./app";
const root = import.meta.hot.data.root ??= createRoot(elem);
root.render(<App />); // re-use an existing root
```
In production, `data` is inlined to be `{}`, meaning it cannot be used as a state holder.
The above pattern is recommended for stateful modules because Bun knows it can minify `{}.prop ??= value` into `value` in production.
### `import.meta.hot.dispose()`
Attaches an on-dispose callback. This is called:
- Just before the module is replaced with another copy (before the next is loaded)
- After the module is detached (removing all imports to this module, see `import.meta.hot.prune()`)
```ts
const sideEffect = setupSideEffect();
import.meta.hot.dispose(() => {
sideEffect.cleanup();
});
```
This callback is not called on route navigation or when the browser tab closes.
Returning a promise will delay module replacement until the module is disposed.
All dispose callbacks are called in parallel.
### `import.meta.hot.prune()`
Attaches an on-prune callback. This is called when all imports to this module
are removed, but the module was previously loaded.
This can be used to clean up resources that were created when the module was
loaded. Unlike `import.meta.hot.dispose()`, this pairs much better with `accept`
and `data` to manage stateful resources. A full example managing a `WebSocket`:
```ts
import { something } from "./something";
// Initialize or re-use a WebSocket connection
export const ws = (import.meta.hot.data.ws ??= new WebSocket(location.origin));
// If the module's import is removed, clean up the WebSocket connection.
import.meta.hot.prune(() => {
ws.close();
});
```
If `dispose` was used instead, the WebSocket would close and re-open on every
hot update. Both versions of the code will prevent page reloads when imported
files are updated.
### `import.meta.hot.on()` and `off()`
`on()` and `off()` are used to listen for events from the HMR runtime. Event names are prefixed with a prefix so that plugins do not conflict with each other.
```ts
import.meta.hot.on("bun:beforeUpdate", () => {
console.log("before a hot update");
});
```
When a file is replaced, all of its event listeners are automatically removed.
A list of all built-in events:
| Event | Emitted when |
| ---------------------- | ----------------------------------------------------------------------------------------------- |
| `bun:beforeUpdate` | before a hot update is applied. |
| `bun:afterUpdate` | after a hot update is applied. |
| `bun:beforeFullReload` | before a full page reload happens. |
| `bun:beforePrune` | before prune callbacks are called. |
| `bun:invalidate` | when a module is invalidated with `import.meta.hot.invalidate()` |
| `bun:error` | when a build or runtime error occurs |
| `bun:ws:disconnect` | when the HMR WebSocket connection is lost. This can indicate the development server is offline. |
| `bun:ws:connect` | when the HMR WebSocket connects or re-connects. |
For compatibility with Vite, the above events are also available via `vite:*` prefix instead of `bun:*`.

View File

@@ -82,11 +82,6 @@ The `--dry-run` flag can be used to simulate the publish process without actuall
$ bun publish --dry-run
```
### `--gzip-level`
Specify the level of gzip compression to use when packing the package. Only applies to `bun publish` without a tarball path argument. Values range from `0` to `9` (default is `9`).
{% bunCLIUsage command="publish" /%}
### `--auth-type`
If you have 2FA enabled for your npm account, `bun publish` will prompt you for a one-time password. This can be done through a browser or the CLI. The `--auth-type` flag can be used to tell the npm registry which method you prefer. The possible values are `web` and `legacy`, with `web` being the default.
@@ -107,6 +102,7 @@ Provide a one-time password directly to the CLI. If the password is valid, this
$ bun publish --otp 123456
```
{% callout %}
**Note** - `bun publish` respects the `NPM_CONFIG_TOKEN` environment variable which can be used when publishing in github actions or automated workflows.
{% /callout %}
### `--gzip-level`
Specify the level of gzip compression to use when packing the package. Only applies to `bun publish` without a tarball path argument. Values range from `0` to `9` (default is `9`).
{% bunCLIUsage command="publish" /%}

View File

@@ -215,19 +215,12 @@ export default {
page("bundler", "`Bun.build`", {
description: "Bundle code for consumption in the browser with Bun's native bundler.",
}),
page("bundler/html", "HTML & static sites", {
page("bundler/html", "Bundle frontend & static sites", {
description: `Zero-config HTML bundler for single-page apps and multi-page apps. Automatic bundling, TailwindCSS plugins, TypeScript, JSX, React support, and incredibly fast builds`,
}),
page("bundler/css", "CSS", {
description: `Production ready CSS bundler with support for modern CSS features, CSS modules, and more.`,
}),
page("bundler/fullstack", "Fullstack Dev Server", {
description: "Serve your frontend and backend from the same app with Bun's dev server.",
}),
page("bundler/hmr", "Hot reloading", {
description: `Update modules in a running application without reloading the page using import.meta.hot`,
}),
page("bundler/loaders", "Loaders", {
description: "Bun's built-in loaders for the bundler and runtime",
}),

View File

@@ -60,7 +60,7 @@ Visual Studio can be installed graphically using the wizard or through WinGet:
After Visual Studio, you need the following:
- LLVM 19.1.7
- LLVM 18.1.8
- Go
- Rust
- NASM
@@ -81,7 +81,7 @@ After Visual Studio, you need the following:
> irm https://get.scoop.sh | iex
> scoop install nodejs-lts go rust nasm ruby perl ccache
# scoop seems to be buggy if you install llvm and the rest at the same time
> scoop install llvm@19.1.7
> scoop install llvm@18.1.8
```
{% /codetabs %}

View File

@@ -174,7 +174,7 @@ Some methods are not optimized yet.
### [`node:test`](https://nodejs.org/api/test.html)
🟡 Partly implemented. Missing mocks, snapshots, timers. Use [`bun:test`](https://bun.sh/docs/cli/test) instead.
🔴 Not implemented. Use [`bun:test`](https://bun.sh/docs/cli/test) instead.
### [`node:trace_events`](https://nodejs.org/api/tracing.html)
@@ -346,7 +346,7 @@ The table below lists all globals implemented by Node.js and Bun's current compa
### [`process`](https://nodejs.org/api/process.html)
🟡 Mostly implemented. `process.binding` (internal Node.js bindings some packages rely on) is partially implemented. `process.title` is currently a no-op on macOS & Linux. `getActiveResourcesInfo` `setActiveResourcesInfo`, `getActiveResources` and `setSourceMapsEnabled` are stubs. Newer APIs like `process.loadEnvFile` and `process.getBuiltinModule` are not implemented yet.
🟡 Mostly implemented. `process.binding` (internal Node.js bindings some packages rely on) is partially implemented. `process.title` is a currently a no-op on macOS & Linux. `getActiveResourcesInfo` `setActiveResourcesInfo`, `getActiveResources` and `setSourceMapsEnabled` are stubs. Newer APIs like `process.loadEnvFile` and `process.getBuiltinModule` are not implemented yet.
### [`queueMicrotask()`](https://developer.mozilla.org/en-US/docs/Web/API/queueMicrotask)

View File

@@ -329,7 +329,7 @@ await Bun.build({
{% callout %}
**NOTE**: Plugin lifecycle callbacks (`onStart()`, `onResolve()`, etc.) do not have the ability to modify the `build.config` object in the `setup()` function. If you want to mutate `build.config`, you must do so directly in the `setup()` function:
**NOTE**: Plugin lifcycle callbacks (`onStart()`, `onResolve()`, etc.) do not have the ability to modify the `build.config` object in the `setup()` function. If you want to mutate `build.config`, you must do so directly in the `setup()` function:
```ts
await Bun.build({
@@ -400,7 +400,7 @@ type Loader = "js" | "jsx" | "ts" | "tsx" | "css" | "json" | "toml" | "object";
### Namespaces
`onLoad` and `onResolve` accept an optional `namespace` string. What is a namespace?
`onLoad` and `onResolve` accept an optional `namespace` string. What is a namespaace?
Every module has a namespace. Namespaces are used to prefix the import in transpiled code; for instance, a loader with a `filter: /\.yaml$/` and `namespace: "yaml:"` will transform an import from `./myfile.yaml` into `yaml:./myfile.yaml`.

View File

@@ -0,0 +1,13 @@
# Tell LLDB what to do when the debugged process receives SIGPWR: pass it through to the process
# (-p), but do not stop the process (-s) or notify the user (-n).
#
# JSC's garbage collector sends this signal (as configured by Bun WebKit in
# Thread::initializePlatformThreading() in ThreadingPOSIX.cpp) to the JS thread to suspend or resume
# it. So stopping the process would just create noise when debugging any long-running script.
process handle -p true -s false -n false SIGPWR
command script import misctools/lldb/lldb_pretty_printers.py
type category enable zig.lang
type category enable zig.std
command script import misctools/lldb/lldb_webkit.py

View File

@@ -329,7 +329,7 @@ def btjs(debugger, command, result, internal_dict):
addressFormat = '#0{width}x'.format(width=target.GetAddressByteSize() * 2 + 2)
process = target.GetProcess()
thread = process.GetSelectedThread()
jscModule = target.module["JavaScriptCore"] or target.module["bun"] or target.module["bun-debug"]
jscModule = target.module["JavaScriptCore"]
if jscModule.FindSymbol("JSC::CallFrame::describeFrame").GetSize() or jscModule.FindSymbol("_ZN3JSC9CallFrame13describeFrameEv").GetSize():
annotateJSFrames = True

View File

@@ -1,7 +1,7 @@
{
"private": true,
"name": "bun",
"version": "1.2.6",
"version": "1.2.5",
"workspaces": [
"./packages/bun-types"
],
@@ -32,6 +32,7 @@
"scripts": {
"build": "bun run build:debug",
"build:debug": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Debug -B build/debug",
"build:asan": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Debug -DENABLE_ZIG_ASAN=ON -B build/debug",
"build:valgrind": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Debug -DENABLE_BASELINE=ON -ENABLE_VALGRIND=ON -B build/debug-valgrind",
"build:release": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Release -B build/release",
"build:ci": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_VERBOSE_MAKEFILE=ON -DCI=true -B build/release-ci --verbose --fresh",

View File

@@ -8,7 +8,7 @@ The official [Svelte](https://svelte.dev/) plugin for [Bun](https://bun.sh/).
## Installation
```sh
$ bun add -D bun-plugin-svelte
bun add -D bun-plugin-svelte
```
## Dev Server Usage
@@ -16,25 +16,52 @@ $ bun add -D bun-plugin-svelte
`bun-plugin-svelte` integrates with Bun's [Fullstack Dev Server](https://bun.sh/docs/bundler/fullstack), giving you
HMR when developing your Svelte app.
Start by registering it in your [bunfig.toml](https://bun.sh/docs/runtime/bunfig):
```toml
[serve.static]
plugins = ["bun-plugin-svelte"]
```html
<!-- index.html -->
<html>
<head>
<script type="module" src="./index.ts"></script>
</head>
<body>
<div id="root"></div>
</body>
</html>
```
Then start your dev server:
```ts
// index.ts
```
$ bun index.html
import { mount, unmount } from "svelte";
import App from "./App.svelte";
// mount the application entrypoint to the DOM
const root = document.getElementById("root")!;
const app = mount(App, { target: root });
```
See the [example](https://github.com/oven-sh/bun/tree/main/packages/bun-plugin-svelte/example) for a complete example.
```svelte
<!-- App.svelte -->
<script lang="ts">
// out-of-the-box typescript support
let name: string = "Bun";
</script>
<main class="app">
<h1>Cookin up apps with {name}</h1>
</main>
<style>
h1 {
color: #ff3e00;
text-align: center;
font-size: 2em;
}
</style>
```
## Bundler Usage
`bun-plugin-svelte` lets you bundle Svelte components with [`Bun.build`](https://bun.sh/docs/bundler).
```ts
// build.ts
// to use: bun run build.ts
@@ -43,7 +70,7 @@ import { SveltePlugin } from "bun-plugin-svelte"; // NOTE: not published to npm
Bun.build({
entrypoints: ["src/index.ts"],
outdir: "dist",
target: "browser",
target: "browser", // use "bun" or "node" to use Svelte components server-side
sourcemap: true, // sourcemaps not yet supported
plugins: [
SveltePlugin({
@@ -57,13 +84,3 @@ Bun.build({
`bun-plugin-svelte` does not yet support server-side imports (e.g. for SSR).
This will be added in the near future.
## Not Yet Supported
Support for these features will be added in the near future
- Server-side imports/rendering
- Source maps
- CSS extensions (e.g. tailwind) in `<style>` blocks
- TypeScript-specific features (e.g. enums and namespaces). If you're using
TypeScript 5.8, consider enabling [`--erasableSyntaxOnly`](https://devblogs.microsoft.com/typescript/announcing-typescript-5-8-beta/#the---erasablesyntaxonly-option)

View File

@@ -1,311 +0,0 @@
<script lang="ts">
import FeatureCard from "./FeatureCard.svelte";
const links = [
{ text: "Bun Documentation", url: "https://bun.sh/docs" },
{ text: "Svelte Documentation", url: "https://svelte.dev/docs" },
{ text: "GitHub", url: "https://github.com/oven-sh/bun/tree/main/packages/bun-plugin-svelte" },
];
</script>
<main>
<div class="hero">
<div class="logo-container">
<a href="https://bun.sh" class="bun-logo">
<img
src="https://github.com/user-attachments/assets/50282090-adfd-4ddb-9e27-c30753c6b161"
alt="Bun Logo"
height="42"
/>
</a>
</div>
<h1><span class="highlight">bun-plugin-svelte</span></h1>
<p class="tagline">The official Svelte plugin for <a href="https://bun.sh" target="_blank">Bun</a></p>
<div class="cta-buttons">
<a href="https://bun.sh/docs/bundler/html" class="button primary">🚀 Get Started</a>
<a href="https://github.com/oven-sh/bun/tree/main/packages/bun-plugin-svelte/example" class="button secondary"
>👀 View Examples</a
>
</div>
</div>
<section class="usage">
<h2>🏃‍➡️ Quick Start</h2>
<div class="flex-grid">
<div>
<h3>1. Install from <a href="https://npmjs.com/package/bun-plugin-svelte" target="_blank">NPM</a></h3>
<pre><code class="language-bash">bun add -D bun-plugin-svelte</code></pre>
</div>
<div>
<h3>2. Add it to your <a href="https://bun.sh/docs/runtime/bunfig" target="_blank">bunfig.toml</a></h3>
<pre><code class="language-toml">
[serve.static]
plugins = ["bun-plugin-svelte"];
</code></pre>
</div>
</div>
</section>
<section class="features">
<h2>✨ Features</h2>
<div class="feature-grid">
<FeatureCard title="🔥 HMR Support" link="https://bun.sh/docs/bundler/html">
Integrates with Bun's Fullstack Dev Server for hot module replacement
</FeatureCard>
<FeatureCard title="📦 Bundling" link="https://bun.sh/docs/bundler">
Bundle Svelte components with <a href="https://bun.sh/docs/bundler">Bun.build</a>
</FeatureCard>
</div>
<section class="resources">
<h2>📖 Resources</h2>
<ul class="resource-links">
{#each links as link}
<li><a href={link.url} target="_blank" rel="noopener noreferrer">{link.text}</a></li>
{/each}
</ul>
</section>
<footer>
<p>Made with ❤️ by the Bun team</p>
</footer>
</section>
</main>
<style>
:global(body) {
margin: 0;
padding: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans",
"Helvetica Neue", sans-serif;
background-color: #f9f9f9;
color: #333;
}
:global(a) {
color: #ff3e00;
text-decoration: none;
position: relative;
}
:global(a::after) {
content: "";
position: absolute;
width: 100%;
height: 1px;
bottom: 0;
left: 0;
background-color: #ff3e00;
transform: scaleX(0);
transform-origin: bottom right;
transition: transform 0.3s ease-out;
}
:global(a:hover::after) {
transform: scaleX(1);
transform-origin: bottom left;
}
:global(a:visited) {
color: #ff3e00;
}
:global(pre > code.hljs) {
padding: 0;
}
main {
max-width: 1200px;
margin: 0 auto;
padding: 2rem;
}
.hero {
text-align: center;
padding: 3rem 1rem;
margin-bottom: 2rem;
display: flex;
flex-direction: column;
}
.logo-container {
margin-bottom: 1.5rem;
margin: auto 25%;
}
.bun-logo {
display: block;
transition: transform 0.3s ease;
}
.bun-logo:hover {
transform: scale(1.05);
}
.bun-logo img {
max-width: 33vw;
height: auto;
}
h1 {
font-size: 2.5rem;
margin-bottom: 0.5rem;
}
.highlight {
color: #ff3e00;
font-weight: bold;
}
/* Don't apply the underline effect to buttons and resource links */
.button::after,
.resource-links li a::after,
.bun-logo::after {
display: none;
}
.tagline {
font-size: 1.2rem;
color: #666;
margin-bottom: 2rem;
}
.cta-buttons {
display: flex;
justify-content: center;
gap: 1rem;
margin-bottom: 2rem;
}
.button {
display: inline-block;
padding: 0.8rem 1.5rem;
border-radius: 4px;
text-decoration: none;
font-weight: 600;
transition: all 0.2s ease;
}
.button.primary {
background-color: #ff3e00;
color: white;
box-shadow: 0 2px 10px rgba(255, 62, 0, 0.2);
}
.button.primary:hover {
background-color: #e63600;
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(255, 62, 0, 0.3);
}
.button.secondary {
background-color: #f0f0f0;
color: #333;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.button.secondary:hover {
background-color: #e6e6e6;
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
section {
margin-bottom: 3rem;
padding: 2rem;
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 15px rgba(0, 0, 0, 0.05);
}
h2 {
font-size: 1.8rem;
margin-bottom: 1.5rem;
color: #333;
border-bottom: 2px solid #f0f0f0;
padding-bottom: 0.5rem;
}
.flex-grid {
display: flex;
gap: 1.5rem;
}
.flex-grid > div {
flex: 1;
}
.feature-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 1.5rem;
margin-bottom: 2rem;
}
pre {
background-color: #f5f5f5;
padding: 1rem;
border-radius: 4px;
overflow-x: auto;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
font-size: 0.9rem;
line-height: 1.5;
}
code {
color: #333;
}
.resource-links {
list-style: none;
padding: 0;
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.resource-links li a {
display: inline-block;
padding: 0.5rem 1rem;
background-color: #f5f5f5;
color: #333;
text-decoration: none;
border-radius: 4px;
transition: all 0.2s ease;
}
.resource-links li a:hover {
background-color: #ff3e00;
color: white;
transform: translateY(-2px);
}
footer {
text-align: center;
padding: 2rem 0;
color: #666;
font-size: 0.9rem;
}
@media (max-width: 768px) {
.feature-grid {
grid-template-columns: 1fr;
}
.cta-buttons {
flex-direction: column;
align-items: center;
}
.button {
width: 100%;
max-width: 300px;
margin-bottom: 0.5rem;
text-align: center;
}
.resource-links {
flex-direction: column;
}
}
</style>

View File

@@ -1,28 +0,0 @@
<script lang="ts">
let { title, link, children } = $props();
</script>
<div class="feature-card">
<h3>
<a href={link}>
{title}
</a>
</h3>
<p>
{@render children()}
</p>
</div>
<style>
.feature-card {
padding: 1rem;
border-radius: 0.5rem;
background-color: #fff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
transition: transform 0.3s ease;
}
.feature-card h3 {
margin-bottom: 0.5rem;
}
</style>

View File

@@ -1,2 +0,0 @@
[serve.static]
plugins = ["bun-plugin-svelte"]

View File

@@ -1,25 +0,0 @@
<html>
<head>
<script type="module" src="./index.ts"></script>
<link rel="prefetch" href="https://bun.sh/docs/bundler/plugins" />
<link rel="preconnect" href="https://bun.sh" />
<link rel="preconnect" href="https://github.com" />
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/styles/default.min.css"
/>
<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/highlight.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/languages/toml.min.js"></script>
<script>
hljs.highlightAll();
</script>
</head>
<body>
<div id="root"></div>
</body>
</html>

View File

@@ -1,29 +0,0 @@
import { mount, unmount } from "svelte";
import App from "./App.svelte";
declare global {
var didMount: boolean | undefined;
var hljs: any;
}
let app: Record<string, any> | undefined;
// mount the application entrypoint to the DOM on first load. On subsequent hot
// updates, the app will be unmounted and re-mounted via the accept handler.
const root = document.getElementById("root")!;
if (!globalThis.didMount) {
app = mount(App, { target: root });
}
globalThis.didMount = true;
if (import.meta.hot) {
import.meta.hot.accept(async () => {
// avoid unmounting twice when another update gets accepted while outros are playing
if (!app) return;
const prevApp = app;
app = undefined;
await unmount(prevApp, { outro: true });
app = mount(App, { target: root });
});
}

View File

@@ -1,14 +1,6 @@
{
"name": "bun-plugin-svelte",
"version": "0.0.5",
"description": "Official Svelte plugin for Bun",
"repository": {
"type": "git",
"url": "https://github.com/oven-sh/bun",
"directory": "packages/bun-plugin-svelte"
},
"homepage": "https://bun.sh",
"license": "MIT",
"version": "0.0.1",
"type": "module",
"module": "src/index.ts",
"index": "src/index.ts",
@@ -16,18 +8,16 @@
".": "./src/index.ts"
},
"scripts": {
"example": "bun --config=./example/bunfig.toml example/index.html",
"lint": "oxlint .",
"fmt": "prettier --write .",
"check:types": "tsc --noEmit",
"build:types": "tsc --emitDeclarationOnly --declaration --declarationDir ./dist"
},
"devDependencies": {
"bun-types": "canary",
"svelte": "^5.20.4",
"@threlte/core": "8.0.1"
"svelte": "^5.20.4"
},
"peerDependencies": {
"typescript": "^5",
"svelte": "^5"
},
"files": [

View File

@@ -1,13 +1,7 @@
import type { BunPlugin, BuildConfig, OnLoadResult } from "bun";
import { basename } from "node:path";
import { compile, compileModule } from "svelte/compiler";
import {
getBaseCompileOptions,
validateOptions,
type SvelteOptions,
hash,
getBaseModuleCompileOptions,
} from "./options";
import { getBaseCompileOptions, validateOptions, type SvelteOptions, hash } from "./options";
const kEmptyObject = Object.create(null);
const virtualNamespace = "bun-svelte";
@@ -29,52 +23,38 @@ function SveltePlugin(options: SvelteOptions = kEmptyObject as SvelteOptions): B
return {
name: "bun-plugin-svelte",
setup(builder) {
// resolve "svelte" export conditions
//
// FIXME: the dev server does not currently respect bundler configs; it
// just passes a fake one to plugins and then never uses it. we need to to
// update it to ~not~ do this.
if (builder?.config) {
var conditions = builder.config.conditions ?? [];
if (typeof conditions === "string") {
conditions = [conditions];
}
conditions.push("svelte");
builder.config.conditions = conditions;
}
const { config = kEmptyObject as Partial<BuildConfig> } = builder;
const baseCompileOptions = getBaseCompileOptions(options ?? (kEmptyObject as Partial<SvelteOptions>), config);
const baseModuleCompileOptions = getBaseModuleCompileOptions(
options ?? (kEmptyObject as Partial<SvelteOptions>),
config,
);
const ts = new Bun.Transpiler({
loader: "ts",
target: config.target,
});
builder
.onLoad({ filter: /\.svelte$/ }, async function onLoadSvelte(args) {
.onLoad({ filter: /\.svelte(?:\.[tj]s)?$/ }, async args => {
const { path } = args;
var isModule = false;
switch (path.substring(path.length - 2)) {
case "js":
case "ts":
isModule = true;
break;
}
const sourceText = await Bun.file(path).text();
const side =
args && "side" in args // "side" only passed when run from dev server
? (args as { side: "client" | "server" }).side
: "server";
const hmr = Boolean((args as { hmr?: boolean })["hmr"] ?? process.env.NODE_ENV !== "production");
const generate = baseCompileOptions.generate ?? side;
const hmr = Boolean((args as { hmr?: boolean })["hmr"] ?? process.env.NODE_ENV !== "production");
const result = compile(sourceText, {
const compileFn = isModule ? compileModule : compile;
const result = compileFn(sourceText, {
...baseCompileOptions,
generate,
filename: args.path,
hmr,
});
var { js, css } = result;
if (css?.code && generate != "server") {
const uid = `${basename(path)}-${hash(path)}-style`.replaceAll(`"`, `'`);
@@ -85,37 +65,11 @@ function SveltePlugin(options: SvelteOptions = kEmptyObject as SvelteOptions): B
return {
contents: result.js.code,
loader: "ts",
loader: "js",
} satisfies OnLoadResult;
// TODO: allow plugins to return multiple results.
// TODO: support layered sourcemaps
})
.onLoad({ filter: /\.svelte.[tj]s$/ }, async function onLoadSvelteModule(args) {
const { path } = args;
const side =
args && "side" in args // "side" only passed when run from dev server
? (args as { side: "client" | "server" }).side
: "server";
const generate = baseModuleCompileOptions.generate ?? side;
var sourceText = await Bun.file(path).text();
if (path.endsWith(".ts")) {
sourceText = await ts.transform(sourceText);
}
const result = compileModule(sourceText, {
...baseModuleCompileOptions,
generate,
filename: args.path,
});
// NOTE: we assume js/ts modules won't have CSS blocks in them, so no
// virtual modules get created.
return {
contents: result.js.code,
loader: "js",
};
})
.onResolve({ filter: /^bun-svelte:/ }, args => {
return {
path: args.path,

View File

@@ -1,6 +1,6 @@
import { strict as assert } from "node:assert";
import { type BuildConfig } from "bun";
import type { CompileOptions, ModuleCompileOptions } from "svelte/compiler";
import type { BuildConfig } from "bun";
import type { CompileOptions } from "svelte/compiler";
export interface SvelteOptions {
/**
@@ -44,8 +44,8 @@ export function validateOptions(options: unknown): asserts options is SvelteOpti
* @internal
*/
export function getBaseCompileOptions(pluginOptions: SvelteOptions, config: Partial<BuildConfig>): CompileOptions {
let { development = false } = pluginOptions;
const { minify = false } = config;
let { forceSide, development = false } = pluginOptions;
const { minify = false, target } = config;
const shouldMinify = Boolean(minify);
const {
@@ -60,38 +60,6 @@ export function getBaseCompileOptions(pluginOptions: SvelteOptions, config: Part
identifiers: shouldMinify,
};
const generate = generateSide(pluginOptions, config);
return {
css: "external",
generate,
preserveWhitespace: !minifyWhitespace,
preserveComments: !shouldMinify,
dev: development,
cssHash({ css }) {
// same prime number seed used by svelte/compiler.
// TODO: ensure this provides enough entropy
return `svelte-${hash(css)}`;
},
};
}
export function getBaseModuleCompileOptions(
pluginOptions: SvelteOptions,
config: Partial<BuildConfig>,
): ModuleCompileOptions {
const { development = false } = pluginOptions;
const generate = generateSide(pluginOptions, config);
return {
dev: development,
generate,
};
}
function generateSide(pluginOptions: SvelteOptions, config: Partial<BuildConfig>) {
let { forceSide } = pluginOptions;
const { target } = config;
if (forceSide == null && typeof target === "string") {
switch (target) {
case "browser":
@@ -105,7 +73,19 @@ function generateSide(pluginOptions: SvelteOptions, config: Partial<BuildConfig>
// warn? throw?
}
}
return forceSide;
return {
css: "external",
generate: forceSide,
preserveWhitespace: !minifyWhitespace,
preserveComments: !shouldMinify,
dev: development,
cssHash({ css }) {
// same prime number seed used by svelte/compiler.
// TODO: ensure this provides enough entropy
return `svelte-${hash(css)}`;
},
};
}
export const hash = (content: string): string => Bun.hash(content, 5381).toString(36);

View File

@@ -1,17 +0,0 @@
<script>
import { Canvas } from "@threlte/core";
let name = "Bun";
</script>
<main class="app">
<h1>Cookin up apps with {name}</h1>
<Canvas />
</main>
<style>
h1 {
color: #ff3e00;
text-align: center;
font-size: 2em;
}
</style>

View File

@@ -1,15 +0,0 @@
class Todo {
title: string | undefined = $state();
done: boolean = $state(false);
createdAt: Date = $state(new Date());
constructor(title: string) {
this.title = title;
}
public toggle(): void {
this.done = !this.done;
}
}
module.exports = Todo;

View File

@@ -1,13 +0,0 @@
export class Todo {
title: string | undefined = $state();
done: boolean = $state(false);
createdAt: Date = $state(new Date());
constructor(title: string) {
this.title = title;
}
public toggle(): void {
this.done = !this.done;
}
}

View File

@@ -1,25 +0,0 @@
<script lang="ts">
const Todo = require("./todo-cjs.svelte.ts");
let name = "World";
let todo: Todo = $state(new Todo("Hello World!"));
</script>
<main class="app">
<h1>Hello {todo.title}!</h1>
<!-- clicking calls toggle -->
<input type="checkbox" bind:checked={todo.done} />
<button onclick={todo.toggle}>Toggle</button>
</main>
<style>
h1 {
color: #ff3e00;
text-align: center;
font-size: 2em;
font-weight: 100;
}
.app {
box-sizing: border-box;
}
</style>

View File

@@ -1,25 +0,0 @@
<script lang="ts">
import { Todo } from "./todo.svelte";
let name = "World";
let todo: Todo = $state(new Todo("Hello World!"));
</script>
<main class="app">
<h1>Hello {todo.title}!</h1>
<!-- clicking calls toggle -->
<input type="checkbox" bind:checked={todo.done} />
<button onclick={todo.toggle}>Toggle</button>
</main>
<style>
h1 {
color: #ff3e00;
text-align: center;
font-size: 2em;
font-weight: 100;
}
.app {
box-sizing: border-box;
}
</style>

View File

@@ -4,7 +4,6 @@ import fs from "node:fs";
import os from "node:os";
import { render } from "svelte/server";
import { SveltePlugin } from "../src";
import type { BuildOutput } from "bun";
const fixturePath = (...segs: string[]) => path.join(import.meta.dirname, "fixtures", ...segs);
@@ -33,55 +32,6 @@ it("hello world component", async () => {
expect(res.success).toBeTrue();
});
describe("when importing `.svelte.ts` files with ESM", () => {
let res: BuildOutput;
beforeAll(async () => {
res = await Bun.build({
entrypoints: [fixturePath("with-modules.svelte")],
outdir,
plugins: [SveltePlugin()],
});
});
it("builds successfully", () => {
expect(res.success).toBeTrue();
});
it(`handles "svelte" export condition`, async () => {
const res = await Bun.build({
entrypoints: [fixturePath("svelte-export-condition.svelte")],
outdir,
plugins: [SveltePlugin()],
});
expect(res.success).toBeTrue();
});
});
describe("when importing `.svelte.ts` files with CJS", () => {
let res: BuildOutput;
beforeAll(async () => {
res = await Bun.build({
entrypoints: [fixturePath("with-cjs.svelte")],
outdir,
plugins: [SveltePlugin()],
});
});
it("builds successfully", () => {
expect(res.success).toBeTrue();
});
it("does not double-wrap the module with function(module, exports, __filename, __dirname)", async () => {
const ts = res.outputs.find(output => output.loader === "ts");
expect(ts).toBeDefined();
const code = await ts!.text();
expect(code).toContain("require_todo_cjs_svelte");
expect(code).toContain("var require_todo_cjs_svelte = __commonJS((exports, module) => {\n");
});
});
describe("Bun.build", () => {
it.each(["node", "bun"] as const)('Generates server-side code when targeting "node" or "bun"', async target => {
const res = await Bun.build({

View File

@@ -1,3 +1,74 @@
declare class _ShellError extends Error implements ShellOutput {
readonly stdout: Buffer;
readonly stderr: Buffer;
readonly exitCode: number;
/**
* Read from stdout as a string
*
* @param encoding - The encoding to use when decoding the output
* @returns Stdout as a string with the given encoding
* @example
*
* ## Read as UTF-8 string
*
* ```ts
* const output = await $`echo hello`;
* console.log(output.text()); // "hello\n"
* ```
*
* ## Read as base64 string
*
* ```ts
* const output = await $`echo ${atob("hello")}`;
* console.log(output.text("base64")); // "hello\n"
* ```
*
*/
text(encoding?: BufferEncoding): string;
/**
* Read from stdout as a JSON object
*
* @returns Stdout as a JSON object
* @example
*
* ```ts
* const output = await $`echo '{"hello": 123}'`;
* console.log(output.json()); // { hello: 123 }
* ```
*
*/
json(): any;
/**
* Read from stdout as an ArrayBuffer
*
* @returns Stdout as an ArrayBuffer
* @example
*
* ```ts
* const output = await $`echo hello`;
* console.log(output.arrayBuffer()); // ArrayBuffer { byteLength: 6 }
* ```
*/
arrayBuffer(): ArrayBuffer;
/**
* Read from stdout as a Blob
*
* @returns Stdout as a blob
* @example
* ```ts
* const output = await $`echo hello`;
* console.log(output.blob()); // Blob { size: 6, type: "" }
* ```
*/
blob(): Blob;
bytes(): Uint8Array;
}
/**
* Bun.js runtime APIs
*
@@ -114,77 +185,6 @@ declare module "bun" {
| SpawnOptions.Writable
| ReadableStream;
class ShellError extends Error implements ShellOutput {
readonly stdout: Buffer;
readonly stderr: Buffer;
readonly exitCode: number;
/**
* Read from stdout as a string
*
* @param encoding - The encoding to use when decoding the output
* @returns Stdout as a string with the given encoding
* @example
*
* ## Read as UTF-8 string
*
* ```ts
* const output = await $`echo hello`;
* console.log(output.text()); // "hello\n"
* ```
*
* ## Read as base64 string
*
* ```ts
* const output = await $`echo ${atob("hello")}`;
* console.log(output.text("base64")); // "hello\n"
* ```
*
*/
text(encoding?: BufferEncoding): string;
/**
* Read from stdout as a JSON object
*
* @returns Stdout as a JSON object
* @example
*
* ```ts
* const output = await $`echo '{"hello": 123}'`;
* console.log(output.json()); // { hello: 123 }
* ```
*
*/
json(): any;
/**
* Read from stdout as an ArrayBuffer
*
* @returns Stdout as an ArrayBuffer
* @example
*
* ```ts
* const output = await $`echo hello`;
* console.log(output.arrayBuffer()); // ArrayBuffer { byteLength: 6 }
* ```
*/
arrayBuffer(): ArrayBuffer;
/**
* Read from stdout as a Blob
*
* @returns Stdout as a blob
* @example
* ```ts
* const output = await $`echo hello`;
* console.log(output.blob()); // Blob { size: 6, type: "" }
* ```
*/
blob(): Blob;
bytes(): Uint8Array;
}
class ShellPromise extends Promise<ShellOutput> {
get stdin(): WritableStream;
/**
@@ -304,12 +304,12 @@ declare module "bun" {
new (): Shell;
}
type ShellError = _ShellError;
export interface Shell {
(strings: TemplateStringsArray, ...expressions: ShellExpression[]): ShellPromise;
readonly Shell: ShellConstructor;
readonly ShellError: typeof ShellError;
readonly ShellPromise: typeof ShellPromise;
readonly ShellError: typeof _ShellError;
/**
* Perform bash-like brace expansion on the given pattern.
@@ -362,6 +362,9 @@ declare module "bun" {
* Configure whether or not the shell should throw an exception on non-zero exit codes.
*/
throws(shouldThrow: boolean): this;
readonly ShellPromise: typeof ShellPromise;
readonly Shell: ShellConstructor;
}
export interface ShellOutput {
@@ -2306,68 +2309,10 @@ declare module "bun" {
*/
interface SavepointSQL extends SQL {}
type CSRFAlgorithm = "blake2b256" | "blake2b512" | "sha256" | "sha384" | "sha512" | "sha512-256";
interface CSRFGenerateOptions {
/**
* The number of milliseconds until the token expires. 0 means the token never expires.
* @default 24 * 60 * 60 * 1000 (24 hours)
*/
expiresIn?: number;
/**
* The encoding of the token.
* @default "base64url"
*/
encoding?: "base64" | "base64url" | "hex";
/**
* The algorithm to use for the token.
* @default "sha256"
*/
algorithm?: CSRFAlgorithm;
}
interface CSRFVerifyOptions {
/**
* The secret to use for the token. If not provided, a random default secret will be generated in memory and used.
*/
secret?: string;
/**
* The encoding of the token.
* @default "base64url"
*/
encoding?: "base64" | "base64url" | "hex";
/**
* The algorithm to use for the token.
* @default "sha256"
*/
algorithm?: CSRFAlgorithm;
/**
* The number of milliseconds until the token expires. 0 means the token never expires.
* @default 24 * 60 * 60 * 1000 (24 hours)
*/
maxAge?: number;
}
interface CSRF {
/**
* Generate a CSRF token.
* @param secret The secret to use for the token. If not provided, a random default secret will be generated in memory and used.
* @param options The options for the token.
* @returns The generated token.
*/
generate(secret?: string, options?: CSRFGenerateOptions): string;
/**
* Verify a CSRF token.
* @param token The token to verify.
* @param options The options for the token.
* @returns True if the token is valid, false otherwise.
*/
verify(token: string, options?: CSRFVerifyOptions): boolean;
}
var sql: SQL;
var postgres: SQL;
var SQL: SQL;
var CSRF: CSRF;
/**
* This lets you use macros as regular imports
* @example
@@ -2712,7 +2657,7 @@ declare module "bun" {
loader?: { [k in string]: Loader };
/**
* Specifies if and how to generate source maps.
*
*
* - `"none"` - No source maps are generated
* - `"linked"` - A separate `*.ext.map` file is generated alongside each
* `*.ext` file. A `//# sourceMappingURL` comment is added to the output
@@ -2720,11 +2665,11 @@ declare module "bun" {
* - `"inline"` - an inline source map is appended to the output file.
* - `"external"` - Generate a separate source map file for each input file.
* No `//# sourceMappingURL` comment is added to the output file.
*
*
* `true` and `false` are aliasees for `"inline"` and `"none"`, respectively.
*
*
* @default "none"
*
*
* @see {@link outdir} required for `"linked"` maps
* @see {@link publicPath} to customize the base url of linked source maps
*/
@@ -2759,10 +2704,10 @@ declare module "bun" {
env?: "inline" | "disable" | `${string}*`;
/**
* Whether to enable minification.
*
*
* Use `true`/`false` to enable/disable all minification options. Alternatively,
* you can pass an object for granular control over certain minifications.
*
*
* @default false
*/
minify?:
@@ -3760,7 +3705,6 @@ declare module "bun" {
interface BunRequest<T extends string = string> extends Request {
params: RouterTypes.ExtractRouteParams<T>;
readonly cookies: CookieMap;
}
interface GenericServeOptions {
@@ -4321,7 +4265,17 @@ declare module "bun" {
* Passing other options such as `port` or `hostname` won't do anything.
*/
reload<T, R extends { [K in keyof R]: RouterTypes.RouteValue<K & string> }>(
options: ServeFunctionOptions<T, R> & {
options: (
| (Omit<ServeOptions, "fetch"> & {
routes: R;
fetch?: (this: Server, request: Request, server: Server) => Response | Promise<Response>;
})
| (Omit<ServeOptions, "routes"> & {
routes?: never;
fetch: (this: Server, request: Request, server: Server) => Response | Promise<Response>;
})
| WebSocketServeOptions<T>
) & {
/**
* @deprecated Use `routes` instead in new code. This will continue to work for awhile though.
*/
@@ -4699,7 +4653,17 @@ declare module "bun" {
@param options.routes - Route definitions mapping paths to handlers
*/
function serve<T, R extends { [K in keyof R]: RouterTypes.RouteValue<K & string> }>(
options: ServeFunctionOptions<T, R> & {
options: (
| (DistributedOmit<Serve, "fetch"> & {
routes: R;
fetch?: (this: Server, request: Request, server: Server) => Response | Promise<Response>;
})
| (DistributedOmit<Serve, "routes"> & {
routes?: never;
fetch: (this: Server, request: Request, server: Server) => Response | Promise<Response>;
})
| WebSocketServeOptions<T>
) & {
/**
* @deprecated Use `routes` instead in new code. This will continue to work for a while though.
*/
@@ -4707,32 +4671,6 @@ declare module "bun" {
},
): Server;
type ServeFunctionOptions<T, R extends { [K in keyof R]: RouterTypes.RouteValue<K & string> }> =
| (DistributedOmit<Exclude<Serve<T>, WebSocketServeOptions<T>>, "fetch"> & {
routes: R;
fetch?: (this: Server, request: Request, server: Server) => Response | Promise<Response>;
})
| (DistributedOmit<Exclude<Serve<T>, WebSocketServeOptions<T>>, "routes"> & {
routes?: never;
fetch: (this: Server, request: Request, server: Server) => Response | Promise<Response>;
})
| (WebSocketServeOptions<T> & {
routes: R;
fetch?: (
this: Server,
request: Request,
server: Server,
) => Response | Promise<Response | void | undefined> | void | undefined;
})
| (WebSocketServeOptions<T> & {
routes?: never;
fetch: (
this: Server,
request: Request,
server: Server,
) => Response | Promise<Response | void | undefined> | void | undefined;
});
/**
* [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) powered by the fastest system calls available for operating on files.
*
@@ -6301,7 +6239,7 @@ declare module "bun" {
* @param socket
*/
open?(socket: Socket<Data>): void | Promise<void>;
close?(socket: Socket<Data>, error?: Error): void | Promise<void>;
close?(socket: Socket<Data>): void | Promise<void>;
error?(socket: Socket<Data>, error: Error): void | Promise<void>;
data?(socket: Socket<Data>, data: BinaryTypeList[DataBinaryType]): void | Promise<void>;
drain?(socket: Socket<Data>): void | Promise<void>;
@@ -6700,8 +6638,7 @@ declare module "bun" {
* This is useful for aborting a subprocess when some other part of the
* program is aborted, such as a `fetch` response.
*
* If the signal is aborted, the process will be killed with the signal
* specified by `killSignal` (defaults to SIGTERM).
* Internally, this works by calling `subprocess.kill(1)`.
*
* @example
* ```ts
@@ -6720,41 +6657,6 @@ declare module "bun" {
* ```
*/
signal?: AbortSignal;
/**
* The maximum amount of time the process is allowed to run in milliseconds.
*
* If the timeout is reached, the process will be killed with the signal
* specified by `killSignal` (defaults to SIGTERM).
*
* @example
* ```ts
* // Kill the process after 5 seconds
* const subprocess = Bun.spawn({
* cmd: ["sleep", "10"],
* timeout: 5000,
* });
* await subprocess.exited; // Will resolve after 5 seconds
* ```
*/
timeout?: number;
/**
* The signal to use when killing the process after a timeout or when the AbortSignal is aborted.
*
* @default "SIGTERM" (signal 15)
*
* @example
* ```ts
* // Kill the process with SIGKILL after 5 seconds
* const subprocess = Bun.spawn({
* cmd: ["sleep", "10"],
* timeout: 5000,
* killSignal: "SIGKILL",
* });
* ```
*/
killSignal?: string | number;
}
type OptionsToSubprocess<Opts extends OptionsObject> =
@@ -7000,8 +6902,6 @@ declare module "bun" {
resourceUsage: ResourceUsage;
signalCode?: string;
exitedDueToTimeout?: true;
pid: number;
}
/**
@@ -7529,86 +7429,4 @@ declare module "bun" {
| [pkg: string, info: BunLockFilePackageInfo, bunTag: string]
/** root */
| [pkg: string, info: Pick<BunLockFileBasePackageInfo, "bin" | "binDir">];
interface CookieInit {
name?: string;
value?: string;
domain?: string;
path?: string;
expires?: number | Date;
secure?: boolean;
sameSite?: CookieSameSite;
httpOnly?: boolean;
partitioned?: boolean;
maxAge?: number;
}
interface CookieStoreDeleteOptions {
name: string;
domain?: string | null;
path?: string;
}
interface CookieStoreGetOptions {
name?: string;
url?: string;
}
type CookieSameSite = "strict" | "lax" | "none";
class Cookie {
constructor(name: string, value: string, options?: CookieInit);
constructor(cookieString: string);
constructor(cookieObject?: CookieInit);
name: string;
value: string;
domain?: string;
path: string;
expires?: number;
secure: boolean;
sameSite: CookieSameSite;
partitioned: boolean;
maxAge?: number;
httpOnly: boolean;
isExpired(): boolean;
toString(): string;
toJSON(): CookieInit;
static parse(cookieString: string): Cookie;
static from(name: string, value: string, options?: CookieInit): Cookie;
static serialize(...cookies: Cookie[]): string;
}
class CookieMap implements Iterable<[string, Cookie]> {
constructor(init?: string[][] | Record<string, string> | string);
get(name: string): Cookie | null;
get(options?: CookieStoreGetOptions): Cookie | null;
getAll(name: string): Cookie[];
getAll(options?: CookieStoreGetOptions): Cookie[];
has(name: string, value?: string): boolean;
set(name: string, value: string): void;
set(options: CookieInit): void;
delete(name: string): void;
delete(options: CookieStoreDeleteOptions): void;
toString(): string;
toJSON(): Record<string, ReturnType<Cookie["toJSON"]>>;
readonly size: number;
entries(): IterableIterator<[string, Cookie]>;
keys(): IterableIterator<string>;
values(): IterableIterator<Cookie>;
forEach(callback: (value: Cookie, key: string, map: CookieMap) => void, thisArg?: any): void;
[Symbol.iterator](): IterableIterator<[string, Cookie]>;
}
}

View File

@@ -1,192 +1,24 @@
export {};
declare global {
namespace Bun {
type HMREventNames =
| "bun:ready"
| "bun:beforeUpdate"
| "bun:afterUpdate"
| "bun:beforeFullReload"
| "bun:beforePrune"
| "bun:invalidate"
| "bun:error"
| "bun:ws:disconnect"
| "bun:ws:connect";
/**
* The event names for the dev server
*/
type HMREvent = `bun:${HMREventNames}` | (string & {});
}
interface ImportMeta {
/**
* Hot module replacement APIs. This value is `undefined` in production and
* can be used in an `if` statement to check if HMR APIs are available
* Hot module replacement
*
* ```ts
* if (import.meta.hot) {
* // HMR APIs are available
* }
* ```
*
* However, this check is usually not needed as Bun will dead-code-eliminate
* calls to all of the HMR APIs in production builds.
*
* https://bun.sh/docs/bundler/hmr
* https://bun.sh/docs/bundler/fullstack
*/
hot: {
/**
* `import.meta.hot.data` maintains state between module instances during
* hot replacement, enabling data transfer from previous to new versions.
* When `import.meta.hot.data` is written to, Bun will mark this module as
* capable of self-accepting (equivalent of calling `accept()`).
* import.meta.hot.data maintains state between module instances during hot replacement, enabling data transfer from previous to new versions.
*
* @example
* ```ts
* const root = import.meta.hot.data.root ??= createRoot(elem);
* root.render(<App />); // re-use an existing root
* import.meta.hot.data = {
* bun: 'is cool',
* };
* ```
*
* In production, `data` is inlined to be `{}`. This is handy because Bun
* knows it can minify `{}.prop ??= value` into `value` in production.
*/
data: any;
/**
* Indicate that this module can be replaced simply by re-evaluating the
* file. After a hot update, importers of this module will be
* automatically patched.
*
* When `import.meta.hot.accept` is not used, the page will reload when
* the file updates, and a console message shows which files were checked.
*
* @example
* ```ts
* import { getCount } from "./foo";
*
* console.log("count is ", getCount());
*
* import.meta.hot.accept();
* ```
*/
accept(): void;
/**
* Indicate that this module can be replaced by evaluating the new module,
* and then calling the callback with the new module. In this mode, the
* importers do not get patched. This is to match Vite, which is unable
* to patch their import statements. Prefer using `import.meta.hot.accept()`
* without an argument as it usually makes your code easier to understand.
*
* When `import.meta.hot.accept` is not used, the page will reload when
* the file updates, and a console message shows which files were checked.
*
* @example
* ```ts
* export const count = 0;
*
* import.meta.hot.accept((newModule) => {
* if (newModule) {
* // newModule is undefined when SyntaxError happened
* console.log('updated: count is now ', newModule.count)
* }
* });
* ```
*
* In production, calls to this are dead-code-eliminated.
*/
accept(cb: (newModule: any | undefined) => void): void;
/**
* Indicate that a dependency's module can be accepted. When the dependency
* is updated, the callback will be called with the new module.
*
* When `import.meta.hot.accept` is not used, the page will reload when
* the file updates, and a console message shows which files were checked.
*
* @example
* ```ts
* import.meta.hot.accept('./foo', (newModule) => {
* if (newModule) {
* // newModule is undefined when SyntaxError happened
* console.log('updated: count is now ', newModule.count)
* }
* });
* ```
*/
accept(specifier: string, callback: (newModule: any) => void): void;
/**
* Indicate that a dependency's module can be accepted. This variant
* accepts an array of dependencies, where the callback will receive
* the one updated module, and `undefined` for the rest.
*
* When `import.meta.hot.accept` is not used, the page will reload when
* the file updates, and a console message shows which files were checked.
*/
accept(specifiers: string[], callback: (newModules: (any | undefined)[]) => void): void;
/**
* Attach an on-dispose callback. This is called:
* - Just before the module is replaced with another copy (before the next is loaded)
* - After the module is detached (removing all imports to this module)
*
* This callback is not called on route navigation or when the browser tab closes.
*
* Returning a promise will delay module replacement until the module is
* disposed. All dispose callbacks are called in parallel.
*/
dispose(cb: (data: any) => void | Promise<void>): void;
/**
* No-op
* @deprecated
*/
decline(): void;
// NOTE TO CONTRIBUTORS ////////////////////////////////////////
// Callback is currently never called for `.prune()` //
// so the types are commented out until we support it. //
////////////////////////////////////////////////////////////////
// /**
// * Attach a callback that is called when the module is removed from the module graph.
// *
// * This can be used to clean up resources that were created when the module was loaded.
// * Unlike `import.meta.hot.dispose()`, this pairs much better with `accept` and `data` to manage stateful resources.
// *
// * @example
// * ```ts
// * export const ws = (import.meta.hot.data.ws ??= new WebSocket(location.origin));
// *
// * import.meta.hot.prune(() => {
// * ws.close();
// * });
// * ```
// */
// prune(callback: () => void): void;
/**
* Listen for an event from the dev server
*
* For compatibility with Vite, event names are also available via vite:* prefix instead of bun:*.
*
* https://bun.sh/docs/bundler/hmr#import-meta-hot-on-and-off
* @param event The event to listen to
* @param callback The callback to call when the event is emitted
*/
on(event: Bun.HMREvent, callback: () => void): void;
/**
* Stop listening for an event from the dev server
*
* For compatibility with Vite, event names are also available via vite:* prefix instead of bun:*.
*
* https://bun.sh/docs/bundler/hmr#import-meta-hot-on-and-off
* @param event The event to stop listening to
* @param callback The callback to stop listening to
*/
off(event: Bun.HMREvent, callback: () => void): void;
};
}
}

View File

@@ -76,17 +76,6 @@ declare global {
revision: string;
reallyExit(code?: number): never;
dlopen(module: { exports: any }, filename: string, flags?: number): void;
_exiting: boolean;
noDeprecation: boolean;
binding(m: string): object;
binding(m: "constants"): {
os: typeof import("node:os").constants;
fs: typeof import("node:fs").constants;
crypto: typeof import("node:crypto").constants;
zlib: typeof import("node:zlib").constants;
trace: typeof import("node:trace").constants;
};
}
}
@@ -1776,71 +1765,6 @@ declare global {
*/
bytes(): Promise<Uint8Array>;
}
var Blob: typeof Blob;
interface Uint8Array {
/**
* Convert the Uint8Array to a base64 encoded string
* @returns The base64 encoded string representation of the Uint8Array
*/
toBase64(options?: { alphabet?: "base64" | "base64url"; omitPadding?: boolean }): string;
/**
* Set the contents of the Uint8Array from a base64 encoded string
* @param base64 The base64 encoded string to decode into the array
* @param offset Optional starting index to begin setting the decoded bytes (default: 0)
*/
setFromBase64(
base64: string,
offset?: number,
): {
/**
* The number of bytes read from the base64 string
*/
read: number;
/**
* The number of bytes written to the Uint8Array
* Will never be greater than the `.byteLength` of this Uint8Array
*/
written: number;
};
/**
* Convert the Uint8Array to a hex encoded string
* @returns The hex encoded string representation of the Uint8Array
*/
toHex(): string;
/**
* Set the contents of the Uint8Array from a hex encoded string
* @param hex The hex encoded string to decode into the array. The string must have
* an even number of characters, be valid hexadecimal characters and contain no whitespace.
*/
setFromHex(hex: string): {
/**
* The number of bytes read from the hex string
*/
read: number;
/**
* The number of bytes written to the Uint8Array
* Will never be greater than the `.byteLength` of this Uint8Array
*/
written: number;
};
}
interface Uint8ArrayConstructor {
/**
* Create a new Uint8Array from a base64 encoded string
* @param base64 The base64 encoded string to convert to a Uint8Array
* @returns A new Uint8Array containing the decoded data
*/
fromBase64(
base64: string,
options?: {
alphabet?: "base64" | "base64url";
lastChunkHandling?: "loose" | "strict" | "stop-before-partial";
},
): Uint8Array;
}
}

View File

@@ -477,79 +477,6 @@ declare module "bun:sqlite" {
*/
static deserialize(serialized: NodeJS.TypedArray | ArrayBufferLike, isReadOnly?: boolean): Database;
/**
* Load a serialized SQLite3 database. This version enables you to specify
* additional options such as `strict` to put the database into strict mode.
*
* Internally, this calls `sqlite3_deserialize`.
*
* @param serialized Data to load
* @returns `Database` instance
*
* @example
* ```ts
* test("supports serialize/deserialize", () => {
* const db = Database.open(":memory:");
* db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)");
* db.exec('INSERT INTO test (name) VALUES ("Hello")');
* db.exec('INSERT INTO test (name) VALUES ("World")');
*
* const input = db.serialize();
* const db2 = Database.deserialize(input, { strict: true });
*
* const stmt = db2.prepare("SELECT * FROM test");
* expect(JSON.stringify(stmt.get())).toBe(
* JSON.stringify({
* id: 1,
* name: "Hello",
* }),
* );
*
* expect(JSON.stringify(stmt.all())).toBe(
* JSON.stringify([
* {
* id: 1,
* name: "Hello",
* },
* {
* id: 2,
* name: "World",
* },
* ]),
* );
* db2.exec("insert into test (name) values ($foo)", { foo: "baz" });
* expect(JSON.stringify(stmt.all())).toBe(
* JSON.stringify([
* {
* id: 1,
* name: "Hello",
* },
* {
* id: 2,
* name: "World",
* },
* {
* id: 3,
* name: "baz",
* },
* ]),
* );
*
* const db3 = Database.deserialize(input, { readonly: true, strict: true });
* try {
* db3.exec("insert into test (name) values ($foo)", { foo: "baz" });
* throw new Error("Expected error");
* } catch (e) {
* expect(e.message).toBe("attempt to write a readonly database");
* }
* });
* ```
*/
static deserialize(
serialized: NodeJS.TypedArray | ArrayBufferLike,
options?: { readonly?: boolean; strict?: boolean; safeIntegers?: boolean },
): Database;
/**
* See `sqlite3_file_control` for more information.
* @link https://www.sqlite.org/c3ref/file_control.html

View File

@@ -165,14 +165,8 @@ declare module "bun:test" {
* @param label the label for the tests
* @param fn the function that defines the tests
*/
interface FunctionLike {
readonly name: string;
}
export interface Describe {
(fn: () => void): void;
(label: number | string | Function | FunctionLike, fn: () => void): void;
(label: string, fn: () => void): void;
/**
* Skips all other tests, except this group of tests.
*

View File

@@ -72,7 +72,7 @@ void us_socket_context_close(int ssl, struct us_socket_context_t *context) {
while (ls) {
struct us_listen_socket_t *nextLS = (struct us_listen_socket_t *) ls->s.next;
us_listen_socket_close(ssl, ls);
ls = nextLS;
}
@@ -310,7 +310,7 @@ struct us_bun_verify_error_t us_socket_verify_error(int ssl, struct us_socket_t
}
#endif
return (struct us_bun_verify_error_t) { .error = 0, .code = NULL, .reason = NULL };
return (struct us_bun_verify_error_t) { .error = 0, .code = NULL, .reason = NULL };
}
void us_internal_socket_context_free(int ssl, struct us_socket_context_t *context) {
@@ -337,7 +337,7 @@ void us_socket_context_ref(int ssl, struct us_socket_context_t *context) {
}
void us_socket_context_unref(int ssl, struct us_socket_context_t *context) {
uint32_t ref_count = context->ref_count;
context->ref_count--;
context->ref_count--;
if (ref_count == 1) {
us_internal_socket_context_free(ssl, context);
}
@@ -520,7 +520,7 @@ void *us_socket_context_connect(int ssl, struct us_socket_context_t *context, co
}
struct us_connecting_socket_t *c = us_calloc(1, sizeof(struct us_connecting_socket_t) + socket_ext_size);
c->socket_ext_size = socket_ext_size;
c->socket_ext_size = socket_ext_size;
c->options = options;
c->ssl = ssl > 0;
c->timeout = 255;
@@ -641,9 +641,9 @@ void us_internal_socket_after_open(struct us_socket_t *s, int error) {
/* Emit error, close without emitting on_close */
/* There are two possible states here:
1. It's a us_connecting_socket_t*. DNS resolution failed, or a connection failed.
2. It's a us_socket_t*
/* There are two possible states here:
1. It's a us_connecting_socket_t*. DNS resolution failed, or a connection failed.
2. It's a us_socket_t*
We differentiate between these two cases by checking if the connect_state is null.
*/
@@ -887,7 +887,7 @@ void us_socket_context_on_connect_error(int ssl, struct us_socket_context_t *con
return;
}
#endif
context->on_connect_error = on_connect_error;
}
@@ -898,7 +898,7 @@ void us_socket_context_on_socket_connect_error(int ssl, struct us_socket_context
return;
}
#endif
context->on_socket_connect_error = on_connect_error;
}

View File

@@ -215,4 +215,4 @@ extern "C" {
#endif
#endif
#endif

View File

@@ -270,6 +270,7 @@ void us_loop_run_bun_tick(struct us_loop_t *loop, const struct timespec* timeout
/* Fetch ready polls */
#ifdef LIBUS_USE_EPOLL
loop->num_ready_polls = bun_epoll_pwait2(loop->fd, loop->ready_polls, 1024, timeout);
#else
do {

View File

@@ -22,6 +22,7 @@
#include <string.h>
#include <stdint.h>
#include <errno.h>
#ifndef WIN32
#include <fcntl.h>
#endif
@@ -167,17 +168,13 @@ void us_connecting_socket_close(int ssl, struct us_connecting_socket_t *c) {
if (!c->pending_resolve_callback) {
us_connecting_socket_free(ssl, c);
}
}
}
struct us_socket_t *us_socket_close(int ssl, struct us_socket_t *s, int code, void *reason) {
if(ssl) {
return (struct us_socket_t *)us_internal_ssl_socket_close((struct us_internal_ssl_socket_t *) s, code, reason);
}
if (!us_socket_is_closed(0, s)) {
/* make sure the context is alive until the callback ends */
us_socket_context_ref(ssl, s->context);
if (s->low_prio_state == 1) {
/* Unlink this socket from the low-priority queue */
if (!s->prev) s->context->loop->data.low_prio_head = s->next;
@@ -189,6 +186,7 @@ struct us_socket_t *us_socket_close(int ssl, struct us_socket_t *s, int code, vo
s->next = 0;
s->low_prio_state = 0;
us_socket_context_unref(ssl, s->context);
} else {
us_internal_socket_context_unlink_socket(ssl, s->context, s);
}
@@ -209,27 +207,18 @@ struct us_socket_t *us_socket_close(int ssl, struct us_socket_t *s, int code, vo
bsd_close_socket(us_poll_fd((struct us_poll_t *) s));
/* Any socket with prev = context is marked as closed */
s->prev = (struct us_socket_t *) s->context;
/* mark it as closed and call the callback */
struct us_socket_t *res = s;
if (!(us_internal_poll_type(&s->p) & POLL_TYPE_SEMI_SOCKET)) {
res = s->context->on_close(s, code, reason);
}
/* Link this socket to the close-list and let it be deleted after this iteration */
s->next = s->context->loop->data.closed_head;
s->context->loop->data.closed_head = s;
/* unref the context after the callback ends */
us_socket_context_unref(ssl, s->context);
/* Any socket with prev = context is marked as closed */
s->prev = (struct us_socket_t *) s->context;
/* preserve the return value from on_close if its called */
return res;
if (!(us_internal_poll_type(&s->p) & POLL_TYPE_SEMI_SOCKET)) {
return s->context->on_close(s, code, reason);
}
}
return s;
}
@@ -446,18 +435,18 @@ int us_connecting_socket_get_error(int ssl, struct us_connecting_socket_t *c) {
return c->error;
}
/*
/*
Note: this assumes that the socket is non-TLS and will be adopted and wrapped with a new TLS context
context ext will not be copied to the new context, new context will contain us_wrapped_socket_context_t on ext
*/
struct us_socket_t *us_socket_wrap_with_tls(int ssl, struct us_socket_t *s, struct us_bun_socket_context_options_t options, struct us_socket_events_t events, int socket_ext_size) {
// only accepts non-TLS sockets
if (ssl) {
return NULL;
return NULL;
}
return(struct us_socket_t *) us_internal_ssl_socket_wrap_with_tls(s, options, events, socket_ext_size);
}
}
// if a TLS socket calls this, it will start SSL call open event and TLS handshake if required
// will have no effect if the socket is closed or is not TLS
@@ -514,14 +503,9 @@ void us_socket_nodelay(struct us_socket_t *s, int enabled) {
}
}
/// Returns 0 on success. Returned error values depend on the platform.
/// - on posix, returns `errno`
/// - on windows, when libuv is used, returns a UV err code
/// - on windows, LIBUS_USE_LIBUV is set, returns `WSAGetLastError()`
/// - on windows, otherwise returns result of `WSAGetLastError`
int us_socket_keepalive(us_socket_r s, int enabled, unsigned int delay){
if (!us_socket_is_shut_down(0, s)) {
return bsd_socket_keepalive(us_poll_fd((struct us_poll_t *) s), enabled, delay);
bsd_socket_keepalive(us_poll_fd((struct us_poll_t *) s), enabled, delay);
}
return 0;
}
@@ -560,4 +544,4 @@ void us_socket_resume(int ssl, struct us_socket_t *s) {
}
// we are readable and writable so we resume everything
us_poll_change(&s->p, s->context->loop, LIBUS_SOCKET_READABLE | LIBUS_SOCKET_WRITABLE);
}
}

View File

@@ -189,10 +189,6 @@ public:
* This function should probably be optimized a lot in future releases,
* it could be O(1) with a hash map of fullnames and their counts. */
unsigned int numSubscribers(std::string_view topic) {
if (!topicTree) {
return 0;
}
Topic *t = topicTree->lookupTopic(topic);
if (t) {
return (unsigned int) t->size();
@@ -412,14 +408,14 @@ public:
webSocketContext->getExt()->messageHandler = std::move(behavior.message);
webSocketContext->getExt()->drainHandler = std::move(behavior.drain);
webSocketContext->getExt()->subscriptionHandler = std::move(behavior.subscription);
webSocketContext->getExt()->closeHandler = [closeHandler = std::move(behavior.close)](WebSocket<SSL, true, UserData> *ws, int code, std::string_view message) mutable {
webSocketContext->getExt()->closeHandler = std::move([closeHandler = std::move(behavior.close)](WebSocket<SSL, true, UserData> *ws, int code, std::string_view message) mutable {
if (closeHandler) {
closeHandler(ws, code, message);
}
/* Destruct user data after returning from close handler */
((UserData *) ws->getUserData())->~UserData();
};
});
webSocketContext->getExt()->pingHandler = std::move(behavior.ping);
webSocketContext->getExt()->pongHandler = std::move(behavior.pong);
@@ -432,8 +428,8 @@ public:
webSocketContext->getExt()->maxLifetime = behavior.maxLifetime;
webSocketContext->getExt()->compression = behavior.compression;
/* Calculate idleTimeoutComponents */
webSocketContext->getExt()->calculateIdleTimeoutComponents(behavior.idleTimeout);
/* Calculate idleTimeoutCompnents */
webSocketContext->getExt()->calculateIdleTimeoutCompnents(behavior.idleTimeout);
httpContext->onHttp("GET", pattern, [webSocketContext, behavior = std::move(behavior)](auto *res, auto *req) mutable {
@@ -610,20 +606,11 @@ public:
return std::move(*this);
}
void setOnClose(HttpContextData<SSL>::OnSocketClosedCallback onClose) {
httpContext->getSocketContextData()->onSocketClosed = onClose;
}
TemplatedApp &&run() {
uWS::run();
return std::move(*this);
}
TemplatedApp &&setUsingCustomExpectHandler(bool value) {
httpContext->getSocketContextData()->usingCustomExpectHandler = value;
return std::move(*this);
}
};
typedef TemplatedApp<false> App;

View File

@@ -30,9 +30,6 @@
#include <iostream>
#include "libusockets.h"
#include "bun-usockets/src/internal/internal.h"
#include "LoopData.h"
#include "AsyncSocketData.h"
@@ -57,6 +54,28 @@ struct AsyncSocket {
template <typename, typename> friend struct TopicTree;
template <bool> friend struct HttpResponse;
private:
/* Helper, do not use directly (todo: move to uSockets or de-crazify) */
void throttle_helper(int toggle) {
/* These should be exposed by uSockets */
static thread_local int us_events[2] = {0, 0};
struct us_poll_t *p = (struct us_poll_t *) this;
struct us_loop_t *loop = us_socket_context_loop(SSL, us_socket_context(SSL, (us_socket_t *) this));
if (toggle) {
/* Pause */
int events = us_poll_events(p);
if (events) {
us_events[getBufferedAmount() ? 1 : 0] = events;
}
us_poll_change(p, loop, 0);
} else {
/* Resume */
int events = us_events[getBufferedAmount() ? 1 : 0];
us_poll_change(p, loop, events);
}
}
public:
/* Returns SSL pointer or FD as pointer */
@@ -86,13 +105,13 @@ public:
/* Experimental pause */
us_socket_t *pause() {
us_socket_pause(SSL, (us_socket_t *) this);
throttle_helper(1);
return (us_socket_t *) this;
}
/* Experimental resume */
us_socket_t *resume() {
us_socket_resume(SSL, (us_socket_t *) this);
throttle_helper(0);
return (us_socket_t *) this;
}

View File

@@ -81,7 +81,7 @@ struct AsyncSocketData {
}
/* Or empty */
/* Or emppty */
AsyncSocketData() = default;
};

View File

@@ -43,10 +43,10 @@ private:
HttpContext() = delete;
/* Maximum delay allowed until an HTTP connection is terminated due to outstanding request or rejected data (slow loris protection) */
static constexpr int HTTP_IDLE_TIMEOUT_S = 10;
static const int HTTP_IDLE_TIMEOUT_S = 10;
/* Minimum allowed receive throughput per second (clients uploading less than 16kB/sec get dropped) */
static constexpr int HTTP_RECEIVE_THROUGHPUT_BYTES = 16 * 1024;
static const int HTTP_RECEIVE_THROUGHPUT_BYTES = 16 * 1024;
us_socket_context_t *getSocketContext() {
return (us_socket_context_t *) this;
@@ -115,8 +115,9 @@ private:
us_socket_context_on_close(SSL, getSocketContext(), [](us_socket_t *s, int /*code*/, void */*reason*/) {
((AsyncSocket<SSL> *)s)->uncorkWithoutSending();
/* Get socket ext */
auto *httpResponseData = reinterpret_cast<HttpResponseData<SSL> *>(us_socket_ext(SSL, s));
HttpResponseData<SSL> *httpResponseData = (HttpResponseData<SSL> *) us_socket_ext(SSL, s);
/* Call filter */
HttpContextData<SSL> *httpContextData = getSocketContextDataS(s);
@@ -129,9 +130,6 @@ private:
httpResponseData->onAborted((HttpResponse<SSL> *)s, httpResponseData->userData);
}
if (httpResponseData->socketData && httpContextData->onSocketClosed) {
httpContextData->onSocketClosed(httpResponseData->socketData, SSL, s);
}
/* Destruct socket ext */
httpResponseData->~HttpResponseData<SSL>();
@@ -173,7 +171,7 @@ private:
proxyParser = &httpResponseData->proxyParser;
#endif
/* The return value is entirely up to us to interpret. The HttpParser cares only for whether the returned value is DIFFERENT from passed user */
/* The return value is entirely up to us to interpret. The HttpParser only care for whether the returned value is DIFFERENT or not from passed user */
auto [err, returnedSocket] = httpResponseData->consumePostPadded(data, (unsigned int) length, s, proxyParser, [httpContextData](void *s, HttpRequest *httpRequest) -> void * {
/* For every request we reset the timeout and hang until user makes action */
/* Warning: if we are in shutdown state, resetting the timer is a security issue! */
@@ -184,7 +182,7 @@ private:
httpResponseData->offset = 0;
/* Are we not ready for another request yet? Terminate the connection.
* Important for denying async pipelining until, if ever, we want to support it.
* Important for denying async pipelining until, if ever, we want to suppot it.
* Otherwise requests can get mixed up on the same connection. We still support sync pipelining. */
if (httpResponseData->state & HttpResponseData<SSL>::HTTP_RESPONSE_PENDING) {
us_socket_close(SSL, (us_socket_t *) s, 0, nullptr);
@@ -199,8 +197,6 @@ private:
httpResponseData->state |= HttpResponseData<SSL>::HTTP_CONNECTION_CLOSE;
}
httpResponseData->fromAncientRequest = httpRequest->isAncient();
/* Select the router based on SNI (only possible for SSL) */
auto *selectedRouter = &httpContextData->router;
if constexpr (SSL) {
@@ -362,8 +358,9 @@ private:
/* Handle HTTP write out (note: SSL_read may trigger this spuriously, the app need to handle spurious calls) */
us_socket_context_on_writable(SSL, getSocketContext(), [](us_socket_t *s) {
auto *asyncSocket = reinterpret_cast<AsyncSocket<SSL> *>(s);
auto *httpResponseData = reinterpret_cast<HttpResponseData<SSL> *>(asyncSocket->getAsyncSocketData());
AsyncSocket<SSL> *asyncSocket = (AsyncSocket<SSL> *) s;
HttpResponseData<SSL> *httpResponseData = (HttpResponseData<SSL> *) asyncSocket->getAsyncSocketData();
/* Ask the developer to write data and return success (true) or failure (false), OR skip sending anything and return success (true). */
if (httpResponseData->onWritable) {
@@ -372,7 +369,7 @@ private:
/* We expect the developer to return whether or not write was successful (true).
* If write was never called, the developer should still return true so that we may drain. */
bool success = httpResponseData->callOnWritable(reinterpret_cast<HttpResponse<SSL> *>(asyncSocket), httpResponseData->offset);
bool success = httpResponseData->callOnWritable((HttpResponse<SSL> *)asyncSocket, httpResponseData->offset);
/* The developer indicated that their onWritable failed. */
if (!success) {
@@ -399,26 +396,28 @@ private:
}
/* Expect another writable event, or another request within the timeout */
reinterpret_cast<HttpResponse<SSL> *>(s)->resetTimeout();
((HttpResponse<SSL> *) s)->resetTimeout();
return s;
});
/* Handle FIN, HTTP does not support half-closed sockets, so simply close */
us_socket_context_on_end(SSL, getSocketContext(), [](us_socket_t *s) {
auto *asyncSocket = reinterpret_cast<AsyncSocket<SSL> *>(s);
asyncSocket->uncorkWithoutSending();
((AsyncSocket<SSL> *)s)->uncorkWithoutSending();
/* We do not care for half closed sockets */
AsyncSocket<SSL> *asyncSocket = (AsyncSocket<SSL> *) s;
return asyncSocket->close();
});
/* Handle socket timeouts, simply close them so to not confuse client with FIN */
us_socket_context_on_timeout(SSL, getSocketContext(), [](us_socket_t *s) {
/* Force close rather than gracefully shutdown and risk confusing the client with a complete download */
AsyncSocket<SSL> *asyncSocket = reinterpret_cast<AsyncSocket<SSL> *>(s);
// Node.js by default closes the connection but they emit the timeout event before that
HttpResponseData<SSL> *httpResponseData = reinterpret_cast<HttpResponseData<SSL> *>(asyncSocket->getAsyncSocketData());
AsyncSocket<SSL> *asyncSocket = (AsyncSocket<SSL> *) s;
// Node.js by default sclose the connection but they emit the timeout event before that
HttpResponseData<SSL> *httpResponseData = (HttpResponseData<SSL> *) asyncSocket->getAsyncSocketData();
if (httpResponseData->onTimeout) {
httpResponseData->onTimeout((HttpResponse<SSL> *)s, httpResponseData->userData);
@@ -494,20 +493,16 @@ public:
}
}
const bool &customContinue = httpContextData->usingCustomExpectHandler;
httpContextData->currentRouter->add(methods, pattern, [handler = std::move(handler), parameterOffsets = std::move(parameterOffsets), &customContinue](auto *r) mutable {
httpContextData->currentRouter->add(methods, pattern, [handler = std::move(handler), parameterOffsets = std::move(parameterOffsets)](auto *r) mutable {
auto user = r->getUserData();
user.httpRequest->setYield(false);
user.httpRequest->setParameters(r->getParameters());
user.httpRequest->setParameterOffsets(&parameterOffsets);
if (!customContinue) {
/* Middleware? Automatically respond to expectations */
std::string_view expect = user.httpRequest->getHeader("expect");
if (expect.length() && expect == "100-continue") {
user.httpResponse->writeContinue();
}
/* Middleware? Automatically respond to expectations */
std::string_view expect = user.httpRequest->getHeader("expect");
if (expect.length() && expect == "100-continue") {
user.httpResponse->writeContinue();
}
handler(user.httpResponse, user.httpRequest);

View File

@@ -34,7 +34,6 @@ struct alignas(16) HttpContextData {
template <bool> friend struct TemplatedApp;
private:
std::vector<MoveOnlyFunction<void(HttpResponse<SSL> *, int)>> filterHandlers;
using OnSocketClosedCallback = void (*)(void* userData, int is_ssl, struct us_socket_t *rawSocket);
MoveOnlyFunction<void(const char *hostname)> missingServerNameHandler;
@@ -51,10 +50,6 @@ private:
void *upgradedWebSocket = nullptr;
bool isParsingHttp = false;
bool rejectUnauthorized = false;
bool usingCustomExpectHandler = false;
/* Used to simulate Node.js socket events. */
OnSocketClosedCallback onSocketClosed = nullptr;
// TODO: SNI
void clearRoutes() {

View File

@@ -239,7 +239,7 @@ namespace uWS
}
return unsignedIntegerValue;
}
static inline uint64_t hasLess(uint64_t x, uint64_t n) {
return (((x)-~0ULL/255*(n))&~(x)&~0ULL/255*128);
}
@@ -283,7 +283,7 @@ namespace uWS
}
return false;
}
static inline void *consumeFieldName(char *p) {
/* Best case fast path (particularly useful with clang) */
while (true) {
@@ -323,14 +323,14 @@ namespace uWS
uint64_t http;
__builtin_memcpy(&http, data, sizeof(uint64_t));
uint32_t first_four_bytes = http & static_cast<uint32_t>(0xFFFFFFFF);
// check if any of the first four bytes are > non-ascii
if ((first_four_bytes & 0x80808080) != 0) [[unlikely]] {
return 0;
}
first_four_bytes |= 0x20202020; // Lowercase the first four bytes
static constexpr char http_lowercase_bytes[4] = {'h', 't', 't', 'p'};
static constexpr uint32_t http_lowercase_bytes_int = __builtin_bit_cast(uint32_t, http_lowercase_bytes);
if (first_four_bytes == http_lowercase_bytes_int) [[likely]] {
@@ -343,7 +343,7 @@ namespace uWS
static constexpr char S_colon_slash_slash[4] = {'S', ':', '/', '/'};
static constexpr uint32_t S_colon_slash_slash_int = __builtin_bit_cast(uint32_t, S_colon_slash_slash);
// Extract the last four bytes from the uint64_t
const uint32_t last_four_bytes = (http >> 32) & static_cast<uint32_t>(0xFFFFFFFF);
return (last_four_bytes == s_colon_slash_slash_int) || (last_four_bytes == S_colon_slash_slash_int);
@@ -361,7 +361,7 @@ namespace uWS
if (&data[1] == end) [[unlikely]] {
return nullptr;
}
if (data[0] == 32 && (__builtin_expect(data[1] == '/', 1) || isHTTPorHTTPSPrefixForProxies(data + 1, end) == 1)) [[likely]] {
header.key = {start, (size_t) (data - start)};
data++;
@@ -536,7 +536,7 @@ namespace uWS
while (headers->value.length() && headers->value.front() < 33) {
headers->value.remove_prefix(1);
}
headers++;
/* We definitely have at least one header (or request line), so check if we are done */
@@ -598,7 +598,7 @@ namespace uWS
for (HttpRequest::Header *h = req->headers; (++h)->key.length(); ) {
req->bf.add(h->key);
}
/* Break if no host header (but we can have empty string which is different from nullptr) */
if (!req->getHeader("host").data()) {
return {HTTP_ERROR_400_BAD_REQUEST, FULLPTR};
@@ -611,12 +611,11 @@ namespace uWS
* ought to be handled as an error. */
std::string_view transferEncodingString = req->getHeader("transfer-encoding");
std::string_view contentLengthString = req->getHeader("content-length");
auto transferEncodingStringLen = transferEncodingString.length();
auto contentLengthStringLen = contentLengthString.length();
if (transferEncodingStringLen && contentLengthStringLen) {
/* Returning fullptr is the same as calling the errorHandler */
/* We could be smart and set an error in the context along with this, to indicate what
/* We could be smart and set an error in the context along with this, to indicate what
* http error response we might want to return */
return {HTTP_ERROR_400_BAD_REQUEST, FULLPTR};
}
@@ -624,7 +623,7 @@ namespace uWS
/* Parse query */
const char *querySeparatorPtr = (const char *) memchr(req->headers->value.data(), '?', req->headers->value.length());
req->querySeparator = (unsigned int) ((querySeparatorPtr ? querySeparatorPtr : req->headers->value.data() + req->headers->value.length()) - req->headers->value.data());
// lets check if content len is valid before calling requestHandler
if(contentLengthStringLen) {
remainingStreamingBytes = toUnsignedInteger(contentLengthString);
@@ -634,14 +633,6 @@ namespace uWS
}
}
// lets check if content len is valid before calling requestHandler
if(contentLengthStringLen) {
remainingStreamingBytes = toUnsignedInteger(contentLengthString);
if (remainingStreamingBytes == UINT64_MAX) {
/* Parser error */
return {HTTP_ERROR_400_BAD_REQUEST, FULLPTR};
}
}
/* If returned socket is not what we put in we need
* to break here as we either have upgraded to
* WebSockets or otherwise closed the socket. */
@@ -663,7 +654,7 @@ namespace uWS
if (transferEncodingStringLen) {
/* If a proxy sent us the transfer-encoding header that 100% means it must be chunked or else the proxy is
* not RFC 9112 compliant. Therefore it is always better to assume this is the case, since that entirely eliminates
* not RFC 9112 compliant. Therefore it is always better to assume this is the case, since that entirely eliminates
* all forms of transfer-encoding obfuscation tricks. We just rely on the header. */
/* RFC 9112 6.3
@@ -692,6 +683,7 @@ namespace uWS
consumedTotal += consumed;
}
} else if (contentLengthStringLen) {
if constexpr (!ConsumeMinimally) {
unsigned int emittable = (unsigned int) std::min<uint64_t>(remainingStreamingBytes, length);
dataHandler(user, std::string_view(data, emittable), emittable == remainingStreamingBytes);

View File

@@ -81,12 +81,8 @@ public:
/* Called only once per request */
void writeMark() {
if (getHttpResponseData()->state & HttpResponseData<SSL>::HTTP_WROTE_DATE_HEADER) {
return;
}
/* Date is always written */
writeHeader("Date", std::string_view(((LoopData *) us_loop_ext(us_socket_context_loop(SSL, (us_socket_context(SSL, (us_socket_t *) this)))))->date, 29));
getHttpResponseData()->state |= HttpResponseData<SSL>::HTTP_WROTE_DATE_HEADER;
}
/* Returns true on success, indicating that it might be feasible to write more data.
@@ -117,8 +113,7 @@ public:
httpResponseData->state |= HttpResponseData<SSL>::HTTP_CONNECTION_CLOSE;
}
/* if write was called and there was previously no Content-Length header set */
if (httpResponseData->state & HttpResponseData<SSL>::HTTP_WRITE_CALLED && !(httpResponseData->state & HttpResponseData<SSL>::HTTP_WROTE_CONTENT_LENGTH_HEADER) && !httpResponseData->fromAncientRequest) {
if (httpResponseData->state & HttpResponseData<SSL>::HTTP_WRITE_CALLED) {
/* We do not have tryWrite-like functionalities, so ignore optional in this path */
@@ -150,8 +145,6 @@ public:
}
}
}
} else {
this->uncork();
}
/* tryEnd can never fail when in chunked mode, since we do not have tryWrite (yet), only write */
@@ -159,7 +152,7 @@ public:
return true;
} else {
/* Write content-length on first call */
if (!(httpResponseData->state & (HttpResponseData<SSL>::HTTP_END_CALLED))) {
if (!(httpResponseData->state & HttpResponseData<SSL>::HTTP_END_CALLED)) {
/* Write mark, this propagates to WebSockets too */
writeMark();
@@ -169,8 +162,7 @@ public:
Super::write("Content-Length: ", 16);
writeUnsigned64(totalSize);
Super::write("\r\n\r\n", 4);
httpResponseData->state |= HttpResponseData<SSL>::HTTP_WROTE_CONTENT_LENGTH_HEADER;
} else if (!(httpResponseData->state & (HttpResponseData<SSL>::HTTP_WRITE_CALLED))) {
} else {
Super::write("\r\n", 2);
}
@@ -215,8 +207,6 @@ public:
}
}
}
} else {
this->uncork();
}
}
@@ -241,7 +231,7 @@ public:
/* Manually upgrade to WebSocket. Typically called in upgrade handler. Immediately calls open handler.
* NOTE: Will invalidate 'this' as socket might change location in memory. Throw away after use. */
template <typename UserData>
us_socket_t *upgrade(UserData &&userData, std::string_view secWebSocketKey, std::string_view secWebSocketProtocol,
void upgrade(UserData &&userData, std::string_view secWebSocketKey, std::string_view secWebSocketProtocol,
std::string_view secWebSocketExtensions,
struct us_socket_context_t *webSocketContext) {
@@ -323,8 +313,8 @@ public:
bool wasCorked = Super::isCorked();
/* Adopting a socket invalidates it, do not rely on it directly to carry any data */
us_socket_t *usSocket = us_socket_context_adopt_socket(SSL, (us_socket_context_t *) webSocketContext, (us_socket_t *) this, sizeof(WebSocketData) + sizeof(UserData));
WebSocket<SSL, true, UserData> *webSocket = (WebSocket<SSL, true, UserData> *) usSocket;
WebSocket<SSL, true, UserData> *webSocket = (WebSocket<SSL, true, UserData> *) us_socket_context_adopt_socket(SSL,
(us_socket_context_t *) webSocketContext, (us_socket_t *) this, sizeof(WebSocketData) + sizeof(UserData));
/* For whatever reason we were corked, update cork to the new socket */
if (wasCorked) {
@@ -354,8 +344,6 @@ public:
if (webSocketContextData->openHandler) {
webSocketContextData->openHandler(webSocket);
}
return usSocket;
}
/* Immediately terminate this Http response */
@@ -439,7 +427,7 @@ public:
/* End the response with an optional data chunk. Always starts a timeout. */
void end(std::string_view data = {}, bool closeConnection = false) {
internalEnd(data, data.length(), false, !(this->getHttpResponseData()->state & HttpResponseData<SSL>::HTTP_WROTE_CONTENT_LENGTH_HEADER), closeConnection);
internalEnd(data, data.length(), false, true, closeConnection);
}
/* Try and end the response. Returns [true, true] on success.
@@ -453,12 +441,12 @@ public:
bool sendTerminatingChunk(bool closeConnection = false) {
writeStatus(HTTP_200_OK);
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
if (!(httpResponseData->state & (HttpResponseData<SSL>::HTTP_WRITE_CALLED | HttpResponseData<SSL>::HTTP_WROTE_CONTENT_LENGTH_HEADER))) {
if (!(httpResponseData->state & HttpResponseData<SSL>::HTTP_WRITE_CALLED)) {
/* Write mark on first call to write */
writeMark();
writeHeader("Transfer-Encoding", "chunked");
httpResponseData->state |= HttpResponseData<SSL>::HTTP_WRITE_CALLED;
httpResponseData->state |= HttpResponseData<SSL>::HTTP_WRITE_CALLED;
}
/* This will be sent always when state is HTTP_WRITE_CALLED inside internalEnd, so no need to write the terminating 0 chunk here */
@@ -468,46 +456,33 @@ public:
}
/* Write parts of the response in chunking fashion. Starts timeout if failed. */
bool write(std::string_view data, size_t *writtenPtr = nullptr) {
bool write(std::string_view data) {
writeStatus(HTTP_200_OK);
/* Do not allow sending 0 chunks, they mark end of response */
if (data.empty()) {
if (writtenPtr) {
*writtenPtr = 0;
}
/* If you called us, then according to you it was fine to call us so it's fine to still call us */
return true;
}
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
if (!(httpResponseData->state & HttpResponseData<SSL>::HTTP_WROTE_CONTENT_LENGTH_HEADER) && !httpResponseData->fromAncientRequest) {
if (!(httpResponseData->state & HttpResponseData<SSL>::HTTP_WRITE_CALLED)) {
/* Write mark on first call to write */
writeMark();
writeHeader("Transfer-Encoding", "chunked");
httpResponseData->state |= HttpResponseData<SSL>::HTTP_WRITE_CALLED;
}
Super::write("\r\n", 2);
writeUnsignedHex((unsigned int) data.length());
Super::write("\r\n", 2);
} else if (!(httpResponseData->state & HttpResponseData<SSL>::HTTP_WRITE_CALLED)) {
if (!(httpResponseData->state & HttpResponseData<SSL>::HTTP_WRITE_CALLED)) {
/* Write mark on first call to write */
writeMark();
Super::write("\r\n", 2);
writeHeader("Transfer-Encoding", "chunked");
httpResponseData->state |= HttpResponseData<SSL>::HTTP_WRITE_CALLED;
}
Super::write("\r\n", 2);
writeUnsignedHex((unsigned int) data.length());
Super::write("\r\n", 2);
auto [written, failed] = Super::write(data.data(), (int) data.length());
/* Reset timeout on each sended chunk */
this->resetTimeout();
if (writtenPtr) {
*writtenPtr = written;
}
/* If we did not fail the write, accept more */
return !failed;
}
@@ -540,7 +515,7 @@ public:
Super::cork();
handler();
/* The only way we could possibly have changed the corked socket during handler call, would be if
/* The only way we could possibly have changed the corked socket during handler call, would be if
* the HTTP socket was upgraded to WebSocket and caused a realloc. Because of this we cannot use "this"
* from here downwards. The corking is done with corkUnchecked() in upgrade. It steals cork. */
auto *newCorkedSocket = loopData->getCorkedSocket();
@@ -607,7 +582,7 @@ public:
/* Attach handler for aborted HTTP request */
HttpResponse *onAborted(void* userData, HttpResponseData<SSL>::OnAbortedCallback handler) {
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
httpResponseData->userData = userData;
httpResponseData->onAborted = handler;
return this;
@@ -615,7 +590,7 @@ public:
HttpResponse *onTimeout(void* userData, HttpResponseData<SSL>::OnTimeoutCallback handler) {
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
httpResponseData->userData = userData;
httpResponseData->onTimeout = handler;
return this;
@@ -645,7 +620,7 @@ public:
return this;
}
/* Attach a read handler for data sent. Will be called with FIN set true if last segment. */
void onData(void* userData, HttpResponseData<SSL>::OnDataCallback handler) {
void onData(void* userData, HttpResponseData<SSL>::OnDataCallback handler) {
HttpResponseData<SSL> *data = getHttpResponseData();
data->userData = userData;
data->inStream = handler;
@@ -654,17 +629,6 @@ public:
data->received_bytes_per_timeout = 0;
}
void* getSocketData() {
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
return httpResponseData->socketData;
}
void setSocketData(void* socketData) {
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
httpResponseData->socketData = socketData;
}
void setWriteOffset(uint64_t offset) {
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();

View File

@@ -36,7 +36,7 @@ struct HttpResponseData : AsyncSocketData<SSL>, HttpParser {
using OnAbortedCallback = void (*)(uWS::HttpResponse<SSL>*, void*);
using OnTimeoutCallback = void (*)(uWS::HttpResponse<SSL>*, void*);
using OnDataCallback = void (*)(uWS::HttpResponse<SSL>* response, const char* chunk, size_t chunk_length, bool, void*);
/* When we are done with a response we mark it like so */
void markDone() {
onAborted = nullptr;
@@ -53,7 +53,7 @@ struct HttpResponseData : AsyncSocketData<SSL>, HttpParser {
}
/* Caller of onWritable. It is possible onWritable calls markDone so we need to borrow it. */
bool callOnWritable(uWS::HttpResponse<SSL>* response, uint64_t offset) {
bool callOnWritable( uWS::HttpResponse<SSL>* response, uint64_t offset) {
/* Borrow real onWritable */
auto* borrowedOnWritable = std::move(onWritable);
@@ -77,14 +77,11 @@ struct HttpResponseData : AsyncSocketData<SSL>, HttpParser {
HTTP_WRITE_CALLED = 2, // used
HTTP_END_CALLED = 4, // used
HTTP_RESPONSE_PENDING = 8, // used
HTTP_CONNECTION_CLOSE = 16, // used
HTTP_WROTE_CONTENT_LENGTH_HEADER = 32, // used
HTTP_WROTE_DATE_HEADER = 64, // used
HTTP_CONNECTION_CLOSE = 16 // used
};
/* Shared context pointer */
void* userData = nullptr;
void* socketData = nullptr;
/* Per socket event handlers */
OnWritableCallback onWritable = nullptr;
@@ -100,7 +97,6 @@ struct HttpResponseData : AsyncSocketData<SSL>, HttpParser {
/* Current state (content-length sent, status sent, write called, etc */
uint8_t state = 0;
uint8_t idleTimeout = 10; // default HTTP_TIMEOUT 10 seconds
bool fromAncientRequest = false;
#ifdef UWS_WITH_PROXY
ProxyParser proxyParser;

View File

@@ -24,7 +24,6 @@
#include "LoopData.h"
#include <libusockets.h>
#include <iostream>
#include "AsyncSocket.h"
extern "C" int bun_is_exiting();
@@ -53,15 +52,6 @@ private:
for (auto &p : loopData->preHandlers) {
p.second((Loop *) loop);
}
void *corkedSocket = loopData->getCorkedSocket();
if (corkedSocket) {
if (loopData->isCorkedSSL()) {
((uWS::AsyncSocket<true> *) corkedSocket)->uncork();
} else {
((uWS::AsyncSocket<false> *) corkedSocket)->uncork();
}
}
}
static void postCb(us_loop_t *loop) {
@@ -158,10 +148,6 @@ public:
getLazyLoop().loop = nullptr;
}
static LoopData* data(struct us_loop_t *loop) {
return (LoopData *) us_loop_ext(loop);
}
void addPostHandler(void *key, MoveOnlyFunction<void(Loop *)> &&handler) {
LoopData *loopData = (LoopData *) us_loop_ext((us_loop_t *) this);

View File

@@ -63,7 +63,6 @@ public:
}
delete [] corkBuffer;
}
void* getCorkedSocket() {
return this->corkedSocket;
}

View File

@@ -82,14 +82,7 @@ namespace ofats {
namespace any_detail {
template <std::size_t Len, std::size_t Align>
class my_aligned_storage_t {
private:
alignas(Align) std::byte t_buff[Len];
};
using buffer = my_aligned_storage_t<sizeof(void*) * 2, alignof(void*)>;
using buffer = std::aligned_storage_t<sizeof(void*) * 2, alignof(void*)>;
template <class T>
inline constexpr bool is_small_object_v =

View File

@@ -115,7 +115,7 @@ public:
char header[10];
int header_length = (int) protocol::formatMessage<isServer>(header, "", 0, opCode, message.length(), compress, fin);
int written = us_socket_write2(0, (struct us_socket_t *)this, header, header_length, message.data(), (int) message.length());
if (written != header_length + (int) message.length()) {
/* Buffer up backpressure */
if (written > header_length) {
@@ -289,7 +289,7 @@ public:
);
WebSocketData *webSocketData = (WebSocketData *) us_socket_ext(SSL, (us_socket_t *) this);
if (!webSocketData->subscriber) { return false; }
/* Cannot return numSubscribers as this is only for this particular websocket context */

View File

@@ -82,7 +82,7 @@ public:
std::pair<unsigned short, unsigned short> idleTimeoutComponents;
/* This is run once on start-up */
void calculateIdleTimeoutComponents(unsigned short idleTimeout) {
void calculateIdleTimeoutCompnents(unsigned short idleTimeout) {
unsigned short margin = 4;
/* 4, 8 or 16 seconds margin based on idleTimeout */
while ((int) idleTimeout - margin * 2 >= margin * 2 && margin < 16) {

View File

@@ -345,7 +345,7 @@ function Install-Rust {
function Install-Llvm {
Install-Package llvm `
-Command clang-cl `
-Version "19.1.7"
-Version "18.1.8"
Add-To-Path "$env:ProgramFiles\LLVM\bin"
}

View File

@@ -1,5 +1,5 @@
#!/bin/sh
# Version: 10
# Version: 9
# A script that installs the dependencies needed to build and test Bun.
# This should work on macOS and Linux with a POSIX shell.
@@ -897,7 +897,7 @@ install_build_essentials() {
}
llvm_version_exact() {
print "19.1.7"
print "18.1.8"
}
llvm_version() {
@@ -915,12 +915,14 @@ install_llvm() {
install_packages "llvm@$(llvm_version)"
;;
apk)
# alpine doesn't have a lld19 package on 3.21 atm so use bare one for now
install_packages \
"llvm$(llvm_version)" \
"clang$(llvm_version)" \
"scudo-malloc" \
"lld"
--repository "http://dl-cdn.alpinelinux.org/alpine/edge/main"
install_packages \
"lld$(llvm_version)" \
--repository "http://dl-cdn.alpinelinux.org/alpine/edge/community"
;;
esac
}
@@ -964,7 +966,7 @@ install_gcc() {
;;
esac
llvm_v="19"
llvm_v="18"
append_to_profile "export CC=clang-${llvm_v}"
append_to_profile "export CXX=clang++-${llvm_v}"
@@ -1130,35 +1132,6 @@ install_tailscale() {
esac
}
install_fuse_python() {
# only linux needs this
case "$pm" in
apk)
# Build and install from source (https://github.com/libfuse/python-fuse/blob/master/INSTALL)
install_packages \
python3-dev \
fuse-dev \
pkgconf \
py3-setuptools
python_fuse_version="1.0.9"
python_fuse_tarball=$(download_file "https://github.com/libfuse/python-fuse/archive/refs/tags/v$python_fuse_version.tar.gz")
python_fuse_tmpdir="$(dirname "$python_fuse_tarball")"
execute tar -xzf "$python_fuse_tarball" -C "$python_fuse_tmpdir"
execute sh -c "cd '$python_fuse_tmpdir/python-fuse-$python_fuse_version' && python setup.py build"
execute_sudo sh -c "cd '$python_fuse_tmpdir/python-fuse-$python_fuse_version' && python setup.py install"
# For Alpine we also need to make sure the kernel module is automatically loaded
execute_sudo sh -c "echo fuse >> /etc/modules-load.d/fuse.conf"
# Check that it was actually installed
execute python -c 'import fuse'
;;
apt | dnf | yum)
install_packages python3-fuse
;;
esac
}
create_buildkite_user() {
if ! [ "$ci" = "1" ] || ! [ "$os" = "linux" ]; then
return
@@ -1350,7 +1323,6 @@ main() {
install_common_software
install_build_essentials
install_chromium
install_fuse_python
clean_system
}

View File

@@ -256,15 +256,13 @@ async function runTests() {
for (const testPath of tests) {
const absoluteTestPath = join(testsPath, testPath);
const title = relative(cwd, absoluteTestPath).replaceAll(sep, "/");
if (isNodeTest(testPath)) {
const testContent = readFileSync(absoluteTestPath, "utf-8");
const runWithBunTest =
title.includes("needs-test") || testContent.includes("bun:test") || testContent.includes("node:test");
if (isNodeParallelTest(testPath)) {
const runWithBunTest = title.includes("needs-test") || readFileSync(absoluteTestPath, "utf-8").includes('bun:test');
const subcommand = runWithBunTest ? "test" : "run";
await runTest(title, async () => {
const { ok, error, stdout } = await spawnBun(execPath, {
cwd: cwd,
args: [subcommand, "--config=" + join(import.meta.dirname, "../bunfig.node-test.toml"), absoluteTestPath],
args: [subcommand, "--config=./bunfig.node-test.toml", absoluteTestPath],
timeout: getNodeParallelTestTimeout(title),
env: {
FORCE_COLOR: "0",
@@ -872,26 +870,19 @@ function isJavaScriptTest(path) {
}
/**
* @param {string} path
* @param {string} testPath
* @returns {boolean}
*/
function isNodeTest(path) {
// Do not run node tests on macOS x64 in CI
// TODO: Unclear why we decided to do this?
if (isCI && isMacOS && isX64) {
return false;
}
const unixPath = path.replaceAll(sep, "/");
return unixPath.includes("js/node/test/parallel/") || unixPath.includes("js/node/test/sequential/");
function isNodeParallelTest(testPath) {
return testPath.replaceAll(sep, "/").includes("js/node/test/parallel/");
}
/**
* @param {string} path
* @param {string} testPath
* @returns {boolean}
*/
function isClusterTest(path) {
const unixPath = path.replaceAll(sep, "/");
return unixPath.includes("js/node/cluster/test-") && unixPath.endsWith(".ts");
function isNodeSequentialTest(testPath) {
return testPath.replaceAll(sep, "/").includes("js/node/test/sequential/");
}
/**
@@ -899,17 +890,21 @@ function isClusterTest(path) {
* @returns {boolean}
*/
function isTest(path) {
return isNodeTest(path) || isClusterTest(path) ? true : isTestStrict(path);
if (isNodeParallelTest(path) && targetDoesRunNodeTests()) return true;
if (isNodeSequentialTest(path) && targetDoesRunNodeTests()) return true;
if (path.replaceAll(sep, "/").startsWith("js/node/cluster/test-") && path.endsWith(".ts")) return true;
return isTestStrict(path);
}
/**
* @param {string} path
* @returns {boolean}
*/
function isTestStrict(path) {
return isJavaScript(path) && /\.test|spec\./.test(basename(path));
}
function targetDoesRunNodeTests() {
if (isMacOS && isX64) return false;
return true;
}
/**
* @param {string} path
* @returns {boolean}

View File

@@ -466,7 +466,11 @@ pub const StandaloneModuleGraph = struct {
return output_bytes;
}
const page_size = std.heap.page_size_max;
const page_size = if (Environment.isLinux and Environment.isAarch64)
// some linux distros do 64 KB pages on aarch64
64 * 1024
else
std.mem.page_size;
pub const InjectOptions = struct {
windows_hide_console: bool = false,

View File

@@ -74,17 +74,16 @@ pub fn allocator(scope: *AllocationScope) Allocator {
const vtable: Allocator.VTable = .{
.alloc = alloc,
.resize = resize,
.remap = remap,
.free = free,
};
fn alloc(ctx: *anyopaque, len: usize, alignment: std.mem.Alignment, ret_addr: usize) ?[*]u8 {
fn alloc(ctx: *anyopaque, len: usize, ptr_align: u8, ret_addr: usize) ?[*]u8 {
const scope: *AllocationScope = @ptrCast(@alignCast(ctx));
scope.state.mutex.lock();
defer scope.state.mutex.unlock();
scope.state.allocations.ensureUnusedCapacity(scope.parent, 1) catch
return null;
const result = scope.parent.vtable.alloc(scope.parent.ptr, len, alignment, ret_addr) orelse
const result = scope.parent.vtable.alloc(scope.parent.ptr, len, ptr_align, ret_addr) orelse
return null;
const trace = StoredTrace.capture(ret_addr);
scope.state.allocations.putAssumeCapacityNoClobber(result, .{
@@ -95,17 +94,12 @@ fn alloc(ctx: *anyopaque, len: usize, alignment: std.mem.Alignment, ret_addr: us
return result;
}
fn resize(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) bool {
fn resize(ctx: *anyopaque, buf: []u8, buf_align: u8, new_len: usize, ret_addr: usize) bool {
const scope: *AllocationScope = @ptrCast(@alignCast(ctx));
return scope.parent.vtable.resize(scope.parent.ptr, buf, alignment, new_len, ret_addr);
return scope.parent.vtable.resize(scope.parent.ptr, buf, buf_align, new_len, ret_addr);
}
fn remap(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, new_len: usize, ret_addr: usize) ?[*]u8 {
const scope: *AllocationScope = @ptrCast(@alignCast(ctx));
return scope.parent.vtable.remap(scope.parent.ptr, buf, alignment, new_len, ret_addr);
}
fn free(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, ret_addr: usize) void {
fn free(ctx: *anyopaque, buf: []u8, buf_align: u8, ret_addr: usize) void {
const scope: *AllocationScope = @ptrCast(@alignCast(ctx));
scope.state.mutex.lock();
defer scope.state.mutex.unlock();
@@ -143,7 +137,7 @@ fn free(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, ret_addr: usiz
// sanitizer does not catch the invalid free.
}
scope.parent.vtable.free(scope.parent.ptr, buf, alignment, ret_addr);
scope.parent.vtable.free(scope.parent.ptr, buf, buf_align, ret_addr);
// If asan did not catch the free, panic now.
if (invalid) @panic("Invalid free");

View File

@@ -33,7 +33,7 @@ pub fn free(this: *const NullableAllocator, bytes: []const u8) void {
if (this.get()) |allocator| {
if (bun.String.isWTFAllocator(allocator)) {
// workaround for https://github.com/ziglang/zig/issues/4298
bun.String.StringImplAllocator.free(allocator.ptr, @constCast(bytes), .fromByteUnits(1), 0);
bun.String.StringImplAllocator.free(allocator.ptr, @constCast(bytes), 0, 0);
return;
}

View File

@@ -62,15 +62,19 @@ pub const LinuxMemFdAllocator = struct {
}
const AllocatorInterface = struct {
fn alloc(_: *anyopaque, _: usize, _: std.mem.Alignment, _: usize) ?[*]u8 {
fn alloc(_: *anyopaque, _: usize, _: u8, _: usize) ?[*]u8 {
// it should perform no allocations or resizes
return null;
}
fn resize(_: *anyopaque, _: []u8, _: u8, _: usize, _: usize) bool {
return false;
}
fn free(
ptr: *anyopaque,
buf: []u8,
_: std.mem.Alignment,
_: u8,
_: usize,
) void {
var this: *LinuxMemFdAllocator = @alignCast(@ptrCast(ptr));
@@ -82,8 +86,7 @@ pub const LinuxMemFdAllocator = struct {
pub const VTable = &std.mem.Allocator.VTable{
.alloc = &AllocatorInterface.alloc,
.resize = &std.mem.Allocator.noResize,
.remap = &std.mem.Allocator.noRemap,
.resize = &resize,
.free = &free,
};
};
@@ -92,7 +95,7 @@ pub const LinuxMemFdAllocator = struct {
var size = len;
// size rounded up to nearest page
size = std.mem.alignForward(usize, size, std.heap.pageSize());
size += (size + std.mem.page_size - 1) & std.mem.page_size;
var flags_mut = flags;
flags_mut.TYPE = .SHARED;

View File

@@ -4,10 +4,9 @@ const std = @import("std");
/// Single allocation only.
///
pub const MaxHeapAllocator = struct {
array_list: std.ArrayListAligned(u8, @alignOf(std.c.max_align_t)),
array_list: std.ArrayList(u8),
fn alloc(ptr: *anyopaque, len: usize, alignment: std.mem.Alignment, _: usize) ?[*]u8 {
bun.assert(alignment.toByteUnits() <= @alignOf(std.c.max_align_t));
fn alloc(ptr: *anyopaque, len: usize, _: u8, _: usize) ?[*]u8 {
var this = bun.cast(*MaxHeapAllocator, ptr);
this.array_list.items.len = 0;
this.array_list.ensureTotalCapacity(len) catch return null;
@@ -15,7 +14,7 @@ pub const MaxHeapAllocator = struct {
return this.array_list.items.ptr;
}
fn resize(_: *anyopaque, buf: []u8, _: std.mem.Alignment, new_len: usize, _: usize) bool {
fn resize(_: *anyopaque, buf: []u8, _: u8, new_len: usize, _: usize) bool {
_ = new_len;
_ = buf;
@panic("not implemented");
@@ -24,7 +23,7 @@ pub const MaxHeapAllocator = struct {
fn free(
_: *anyopaque,
_: []u8,
_: std.mem.Alignment,
_: u8,
_: usize,
) void {}
@@ -40,10 +39,9 @@ pub const MaxHeapAllocator = struct {
.alloc = &alloc,
.free = &free,
.resize = &resize,
.remap = &std.mem.Allocator.noRemap,
};
pub fn init(this: *MaxHeapAllocator, allocator: std.mem.Allocator) std.mem.Allocator {
this.array_list = .init(allocator);
this.array_list = std.ArrayList(u8).init(allocator);
return std.mem.Allocator{
.ptr = this,

View File

@@ -12,7 +12,7 @@ const Environment = @import("../env.zig");
fn mimalloc_free(
_: *anyopaque,
buf: []u8,
alignment: mem.Alignment,
buf_align: u8,
_: usize,
) void {
if (comptime Environment.enable_logs)
@@ -23,8 +23,8 @@ fn mimalloc_free(
// let's only enable it in debug mode
if (comptime Environment.isDebug) {
assert(mimalloc.mi_is_in_heap_region(buf.ptr));
if (mimalloc.canUseAlignedAlloc(buf.len, alignment.toByteUnits()))
mimalloc.mi_free_size_aligned(buf.ptr, buf.len, alignment.toByteUnits())
if (mimalloc.canUseAlignedAlloc(buf.len, buf_align))
mimalloc.mi_free_size_aligned(buf.ptr, buf.len, buf_align)
else
mimalloc.mi_free_size(buf.ptr, buf.len);
} else {
@@ -35,12 +35,12 @@ fn mimalloc_free(
const CAllocator = struct {
pub const supports_posix_memalign = true;
fn alignedAlloc(len: usize, alignment: mem.Alignment) ?[*]u8 {
fn alignedAlloc(len: usize, alignment: usize) ?[*]u8 {
if (comptime Environment.enable_logs)
log("mi_alloc({d}, {d})", .{ len, alignment.toByteUnits() });
log("mi_alloc({d}, {d})", .{ len, alignment });
const ptr: ?*anyopaque = if (mimalloc.canUseAlignedAlloc(len, alignment.toByteUnits()))
mimalloc.mi_malloc_aligned(len, alignment.toByteUnits())
const ptr: ?*anyopaque = if (mimalloc.canUseAlignedAlloc(len, alignment))
mimalloc.mi_malloc_aligned(len, alignment)
else
mimalloc.mi_malloc(len);
@@ -60,11 +60,16 @@ const CAllocator = struct {
return mimalloc.mi_malloc_size(ptr);
}
fn alloc(_: *anyopaque, len: usize, alignment: mem.Alignment, _: usize) ?[*]u8 {
fn alloc(_: *anyopaque, len: usize, log2_align: u8, _: usize) ?[*]u8 {
if (comptime FeatureFlags.alignment_tweak) {
return alignedAlloc(len, log2_align);
}
const alignment = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_align));
return alignedAlloc(len, alignment);
}
fn resize(_: *anyopaque, buf: []u8, _: mem.Alignment, new_len: usize, _: usize) bool {
fn resize(_: *anyopaque, buf: []u8, _: u8, new_len: usize, _: usize) bool {
if (new_len <= buf.len) {
return true;
}
@@ -88,18 +93,17 @@ pub const c_allocator = Allocator{
const c_allocator_vtable = &Allocator.VTable{
.alloc = &CAllocator.alloc,
.resize = &CAllocator.resize,
.remap = &std.mem.Allocator.noRemap,
.free = &CAllocator.free,
};
const ZAllocator = struct {
pub const supports_posix_memalign = true;
fn alignedAlloc(len: usize, alignment: mem.Alignment) ?[*]u8 {
fn alignedAlloc(len: usize, alignment: usize) ?[*]u8 {
log("ZAllocator.alignedAlloc: {d}\n", .{len});
const ptr = if (mimalloc.canUseAlignedAlloc(len, alignment.toByteUnits()))
mimalloc.mi_zalloc_aligned(len, alignment.toByteUnits())
const ptr = if (mimalloc.canUseAlignedAlloc(len, alignment))
mimalloc.mi_zalloc_aligned(len, alignment)
else
mimalloc.mi_zalloc(len);
@@ -119,11 +123,11 @@ const ZAllocator = struct {
return mimalloc.mi_malloc_size(ptr);
}
fn alloc(_: *anyopaque, len: usize, alignment: mem.Alignment, _: usize) ?[*]u8 {
return alignedAlloc(len, alignment);
fn alloc(_: *anyopaque, len: usize, ptr_align: u8, _: usize) ?[*]u8 {
return alignedAlloc(len, ptr_align);
}
fn resize(_: *anyopaque, buf: []u8, _: mem.Alignment, new_len: usize, _: usize) bool {
fn resize(_: *anyopaque, buf: []u8, _: u8, new_len: usize, _: usize) bool {
if (new_len <= buf.len) {
return true;
}
@@ -146,7 +150,6 @@ pub const z_allocator = Allocator{
const z_allocator_vtable = Allocator.VTable{
.alloc = &ZAllocator.alloc,
.resize = &ZAllocator.resize,
.remap = &std.mem.Allocator.noRemap,
.free = &ZAllocator.free,
};
const HugeAllocator = struct {

View File

@@ -209,11 +209,11 @@ pub const Arena = struct {
}
pub const supports_posix_memalign = true;
fn alignedAlloc(heap: *mimalloc.Heap, len: usize, alignment: mem.Alignment) ?[*]u8 {
fn alignedAlloc(heap: *mimalloc.Heap, len: usize, alignment: usize) ?[*]u8 {
log("Malloc: {d}\n", .{len});
const ptr: ?*anyopaque = if (mimalloc.canUseAlignedAlloc(len, alignment.toByteUnits()))
mimalloc.mi_heap_malloc_aligned(heap, len, alignment.toByteUnits())
const ptr: ?*anyopaque = if (mimalloc.canUseAlignedAlloc(len, alignment))
mimalloc.mi_heap_malloc_aligned(heap, len, alignment)
else
mimalloc.mi_heap_malloc(heap, len);
@@ -234,10 +234,15 @@ pub const Arena = struct {
return mimalloc.mi_malloc_usable_size(ptr);
}
fn alloc(arena: *anyopaque, len: usize, alignment: mem.Alignment, _: usize) ?[*]u8 {
fn alloc(arena: *anyopaque, len: usize, log2_align: u8, _: usize) ?[*]u8 {
const this = bun.cast(*mimalloc.Heap, arena);
// if (comptime Environment.isDebug)
// ArenaRegistry.assert(.{ .heap = this });
if (comptime FeatureFlags.alignment_tweak) {
return alignedAlloc(this, len, log2_align);
}
const alignment = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_align));
return alignedAlloc(
this,
@@ -246,7 +251,7 @@ pub const Arena = struct {
);
}
fn resize(_: *anyopaque, buf: []u8, _: mem.Alignment, new_len: usize, _: usize) bool {
fn resize(_: *anyopaque, buf: []u8, _: u8, new_len: usize, _: usize) bool {
if (new_len <= buf.len) {
return true;
}
@@ -262,7 +267,7 @@ pub const Arena = struct {
fn free(
_: *anyopaque,
buf: []u8,
alignment: mem.Alignment,
buf_align: u8,
_: usize,
) void {
// mi_free_size internally just asserts the size
@@ -270,8 +275,8 @@ pub const Arena = struct {
// but its good to have that assertion
if (comptime Environment.isDebug) {
assert(mimalloc.mi_is_in_heap_region(buf.ptr));
if (mimalloc.canUseAlignedAlloc(buf.len, alignment.toByteUnits()))
mimalloc.mi_free_size_aligned(buf.ptr, buf.len, alignment.toByteUnits())
if (mimalloc.canUseAlignedAlloc(buf.len, buf_align))
mimalloc.mi_free_size_aligned(buf.ptr, buf.len, buf_align)
else
mimalloc.mi_free_size(buf.ptr, buf.len);
} else {
@@ -283,6 +288,5 @@ pub const Arena = struct {
const c_allocator_vtable = Allocator.VTable{
.alloc = &Arena.alloc,
.resize = &Arena.resize,
.remap = &std.mem.Allocator.noRemap,
.free = &Arena.free,
};

View File

@@ -128,8 +128,6 @@ pub const Features = struct {
pub var process_dlopen: usize = 0;
pub var postgres_connections: usize = 0;
pub var s3: usize = 0;
pub var csrf_verify: usize = 0;
pub var csrf_generate: usize = 0;
comptime {
@export(&napi_module_register, .{ .name = "Bun__napi_module_register_count" });

View File

@@ -187,7 +187,7 @@ pub const FilePoll = struct {
pub fn unregister(this: *FilePoll, loop: *Loop) bool {
_ = loop;
// TODO(@paperclover): This cast is extremely suspicious. At best, `fd` is
// TODO(@paperdave): This cast is extremely suspicious. At best, `fd` is
// the wrong type (it should be a uv handle), at worst this code is a
// crash due to invalid memory access.
uv.uv_unref(@ptrFromInt(@intFromEnum(this.fd)));

Some files were not shown because too many files have changed in this diff Show More