mirror of
https://github.com/oven-sh/bun
synced 2026-02-08 01:49:33 +00:00
Compare commits
123 Commits
kai/mimall
...
claude/nod
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7ccb1a5ebe | ||
|
|
1ae5bb5ca0 | ||
|
|
cff2c2690b | ||
|
|
d0272d4a98 | ||
|
|
48ebc15e63 | ||
|
|
2e8e7a000c | ||
|
|
c1584b8a35 | ||
|
|
a0f13ea5bb | ||
|
|
c2bd4095eb | ||
|
|
0a7313e66c | ||
|
|
83293ea50c | ||
|
|
de7c947161 | ||
|
|
033c977fea | ||
|
|
d957a81c0a | ||
|
|
0b98086c3d | ||
|
|
f6c5318560 | ||
|
|
ad1fa514ed | ||
|
|
24c43c8f4d | ||
|
|
d69eb3ca00 | ||
|
|
3f53add5f1 | ||
|
|
fcaff77ed7 | ||
|
|
25c61fcd5a | ||
|
|
d0b5f9b587 | ||
|
|
1400e05e11 | ||
|
|
c8e3a91602 | ||
|
|
2e5f7f10ae | ||
|
|
559c95ee2c | ||
|
|
262f8863cb | ||
|
|
05f5ea0070 | ||
|
|
46ce975175 | ||
|
|
fa4822f8b8 | ||
|
|
8881e671d4 | ||
|
|
97d55411de | ||
|
|
f247277375 | ||
|
|
b93468ca48 | ||
|
|
35e9f3d4a2 | ||
|
|
9142cdcb1a | ||
|
|
822445d922 | ||
|
|
a34e10db53 | ||
|
|
684f7ecd09 | ||
|
|
d189759576 | ||
|
|
e395dec309 | ||
|
|
ff6af0e2f7 | ||
|
|
1085908386 | ||
|
|
a56488f221 | ||
|
|
fe8f8242fd | ||
|
|
c69ed120e9 | ||
|
|
edea077947 | ||
|
|
669b34ff6c | ||
|
|
eb7727819a | ||
|
|
84604888e9 | ||
|
|
6286824e28 | ||
|
|
dcb51bda60 | ||
|
|
36e2870fc8 | ||
|
|
5ac0a9a95c | ||
|
|
448fad8213 | ||
|
|
0315c97e7b | ||
|
|
3545cca8cc | ||
|
|
b199333f17 | ||
|
|
c0ba7e9e34 | ||
|
|
d4e614da8e | ||
|
|
b96980a95d | ||
|
|
1dd5761daa | ||
|
|
196182f8ec | ||
|
|
a3fcfd3963 | ||
|
|
54b90213eb | ||
|
|
fd69af7356 | ||
|
|
3ed06e3ddf | ||
|
|
437e15bae5 | ||
|
|
300f486125 | ||
|
|
fe7dfbb615 | ||
|
|
26c0f324f8 | ||
|
|
00722626fa | ||
|
|
2d6c67ffc0 | ||
|
|
e577a965ac | ||
|
|
654d33620a | ||
|
|
b99bbe7ee4 | ||
|
|
ec2e2993f5 | ||
|
|
d3abdc489e | ||
|
|
7c45ed97de | ||
|
|
a7586212eb | ||
|
|
8c3278b50d | ||
|
|
8bc2959a52 | ||
|
|
d2b37a575f | ||
|
|
fe3cbce1f0 | ||
|
|
f718f4a312 | ||
|
|
c0eebd7523 | ||
|
|
85770596ca | ||
|
|
b6613beaa2 | ||
|
|
404ac7fe9d | ||
|
|
707fc4c3a2 | ||
|
|
8fad98ffdb | ||
|
|
75f0ac4395 | ||
|
|
c342453065 | ||
|
|
7717693c70 | ||
|
|
790e5d4a7e | ||
|
|
f99efe398d | ||
|
|
b2351bbb4e | ||
|
|
d7bf8210eb | ||
|
|
92b38fdf80 | ||
|
|
e3e8d15263 | ||
|
|
73fe9a4484 | ||
|
|
0e37dc4e78 | ||
|
|
cca10d4530 | ||
|
|
ecbf103bf5 | ||
|
|
efdbe3b54f | ||
|
|
97495a86fe | ||
|
|
5b972fa2b4 | ||
|
|
87a5fac697 | ||
|
|
ede4ba567b | ||
|
|
f1e9bfb856 | ||
|
|
d05f806297 | ||
|
|
b14cabfca4 | ||
|
|
9e71922c07 | ||
|
|
77de08b51b | ||
|
|
a200522683 | ||
|
|
178369d08f | ||
|
|
074baee2da | ||
|
|
e05ff19f65 | ||
|
|
dd7fd0a036 | ||
|
|
8aee3cb3eb | ||
|
|
f3e5848549 | ||
|
|
09ab0fee3a |
@@ -434,11 +434,17 @@ function getBuildEnv(target, options) {
|
||||
* @param {PipelineOptions} options
|
||||
* @returns {string}
|
||||
*/
|
||||
function getBuildCommand(target, options) {
|
||||
function getBuildCommand(target, options, label) {
|
||||
const { profile } = target;
|
||||
const buildProfile = profile || "release";
|
||||
|
||||
const label = profile || "release";
|
||||
return `bun run build:${label}`;
|
||||
if (target.os === "windows" && label === "build-bun") {
|
||||
// Only sign release builds, not canary builds (DigiCert charges per signature)
|
||||
const enableSigning = !options.canary ? " -DENABLE_WINDOWS_CODESIGNING=ON" : "";
|
||||
return `bun run build:${buildProfile}${enableSigning}`;
|
||||
}
|
||||
|
||||
return `bun run build:${buildProfile}`;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -534,7 +540,7 @@ function getLinkBunStep(platform, options) {
|
||||
BUN_LINK_ONLY: "ON",
|
||||
...getBuildEnv(platform, options),
|
||||
},
|
||||
command: `${getBuildCommand(platform, options)} --target bun`,
|
||||
command: `${getBuildCommand(platform, options, "build-bun")} --target bun`,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
464
.buildkite/scripts/sign-windows.ps1
Normal file
464
.buildkite/scripts/sign-windows.ps1
Normal file
@@ -0,0 +1,464 @@
|
||||
# Windows Code Signing Script for Bun
|
||||
# Uses DigiCert KeyLocker for Authenticode signing
|
||||
# Native PowerShell implementation - no path translation issues
|
||||
|
||||
param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$BunProfileExe,
|
||||
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$BunExe
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
$ProgressPreference = "SilentlyContinue"
|
||||
|
||||
# Logging functions
|
||||
function Log-Info {
|
||||
param([string]$Message)
|
||||
Write-Host "[INFO] $Message" -ForegroundColor Cyan
|
||||
}
|
||||
|
||||
function Log-Success {
|
||||
param([string]$Message)
|
||||
Write-Host "[SUCCESS] $Message" -ForegroundColor Green
|
||||
}
|
||||
|
||||
function Log-Error {
|
||||
param([string]$Message)
|
||||
Write-Host "[ERROR] $Message" -ForegroundColor Red
|
||||
}
|
||||
|
||||
function Log-Debug {
|
||||
param([string]$Message)
|
||||
if ($env:DEBUG -eq "true" -or $env:DEBUG -eq "1") {
|
||||
Write-Host "[DEBUG] $Message" -ForegroundColor Gray
|
||||
}
|
||||
}
|
||||
|
||||
# Load Visual Studio environment if not already loaded
|
||||
function Ensure-VSEnvironment {
|
||||
if ($null -eq $env:VSINSTALLDIR) {
|
||||
Log-Info "Loading Visual Studio environment..."
|
||||
|
||||
$vswhere = "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"
|
||||
if (!(Test-Path $vswhere)) {
|
||||
throw "Command not found: vswhere (did you install Visual Studio?)"
|
||||
}
|
||||
|
||||
$vsDir = & $vswhere -prerelease -latest -property installationPath
|
||||
if ($null -eq $vsDir) {
|
||||
$vsDir = Get-ChildItem -Path "C:\Program Files\Microsoft Visual Studio\2022" -Directory -ErrorAction SilentlyContinue
|
||||
if ($null -eq $vsDir) {
|
||||
throw "Visual Studio directory not found."
|
||||
}
|
||||
$vsDir = $vsDir.FullName
|
||||
}
|
||||
|
||||
Push-Location $vsDir
|
||||
try {
|
||||
$vsShell = Join-Path -Path $vsDir -ChildPath "Common7\Tools\Launch-VsDevShell.ps1"
|
||||
. $vsShell -Arch amd64 -HostArch amd64
|
||||
} finally {
|
||||
Pop-Location
|
||||
}
|
||||
|
||||
Log-Success "Visual Studio environment loaded"
|
||||
}
|
||||
|
||||
if ($env:VSCMD_ARG_TGT_ARCH -eq "x86") {
|
||||
throw "Visual Studio environment is targeting 32 bit, but only 64 bit is supported."
|
||||
}
|
||||
}
|
||||
|
||||
# Check for required environment variables
|
||||
function Check-Environment {
|
||||
Log-Info "Checking environment variables..."
|
||||
|
||||
$required = @{
|
||||
"SM_API_KEY" = $env:SM_API_KEY
|
||||
"SM_CLIENT_CERT_PASSWORD" = $env:SM_CLIENT_CERT_PASSWORD
|
||||
"SM_KEYPAIR_ALIAS" = $env:SM_KEYPAIR_ALIAS
|
||||
"SM_HOST" = $env:SM_HOST
|
||||
"SM_CLIENT_CERT_FILE" = $env:SM_CLIENT_CERT_FILE
|
||||
}
|
||||
|
||||
$missing = @()
|
||||
foreach ($key in $required.Keys) {
|
||||
if ([string]::IsNullOrEmpty($required[$key])) {
|
||||
$missing += $key
|
||||
} else {
|
||||
Log-Debug "$key is set (length: $($required[$key].Length))"
|
||||
}
|
||||
}
|
||||
|
||||
if ($missing.Count -gt 0) {
|
||||
throw "Missing required environment variables: $($missing -join ', ')"
|
||||
}
|
||||
|
||||
Log-Success "All required environment variables are present"
|
||||
}
|
||||
|
||||
# Setup certificate file
|
||||
function Setup-Certificate {
|
||||
Log-Info "Setting up certificate..."
|
||||
|
||||
# Always try to decode as base64 first
|
||||
# If it fails, then treat as file path
|
||||
try {
|
||||
Log-Info "Attempting to decode certificate as base64..."
|
||||
Log-Debug "Input string length: $($env:SM_CLIENT_CERT_FILE.Length) characters"
|
||||
|
||||
$tempCertPath = Join-Path $env:TEMP "digicert_cert_$(Get-Random).p12"
|
||||
|
||||
# Try to decode as base64
|
||||
$certBytes = [System.Convert]::FromBase64String($env:SM_CLIENT_CERT_FILE)
|
||||
[System.IO.File]::WriteAllBytes($tempCertPath, $certBytes)
|
||||
|
||||
# Validate the decoded certificate size
|
||||
$fileSize = (Get-Item $tempCertPath).Length
|
||||
if ($fileSize -lt 100) {
|
||||
throw "Decoded certificate too small: $fileSize bytes (expected >100 bytes)"
|
||||
}
|
||||
|
||||
# Update environment to point to file
|
||||
$env:SM_CLIENT_CERT_FILE = $tempCertPath
|
||||
|
||||
Log-Success "Certificate decoded and written to: $tempCertPath"
|
||||
Log-Debug "Decoded certificate file size: $fileSize bytes"
|
||||
|
||||
# Register cleanup
|
||||
$global:TEMP_CERT_PATH = $tempCertPath
|
||||
|
||||
} catch {
|
||||
# If base64 decode fails, check if it's a file path
|
||||
Log-Info "Base64 decode failed, checking if it's a file path..."
|
||||
Log-Debug "Decode error: $_"
|
||||
|
||||
if (Test-Path $env:SM_CLIENT_CERT_FILE) {
|
||||
$fileSize = (Get-Item $env:SM_CLIENT_CERT_FILE).Length
|
||||
|
||||
# Validate file size
|
||||
if ($fileSize -lt 100) {
|
||||
throw "Certificate file too small: $fileSize bytes at $env:SM_CLIENT_CERT_FILE (possibly corrupted)"
|
||||
}
|
||||
|
||||
Log-Info "Using certificate file: $env:SM_CLIENT_CERT_FILE"
|
||||
Log-Debug "Certificate file size: $fileSize bytes"
|
||||
} else {
|
||||
throw "SM_CLIENT_CERT_FILE is neither valid base64 nor an existing file: $env:SM_CLIENT_CERT_FILE"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Install DigiCert KeyLocker tools
|
||||
function Install-KeyLocker {
|
||||
Log-Info "Setting up DigiCert KeyLocker tools..."
|
||||
|
||||
# Define our controlled installation directory
|
||||
$installDir = "C:\BuildTools\DigiCert"
|
||||
$smctlPath = Join-Path $installDir "smctl.exe"
|
||||
|
||||
# Check if already installed in our controlled location
|
||||
if (Test-Path $smctlPath) {
|
||||
Log-Success "KeyLocker tools already installed at: $smctlPath"
|
||||
|
||||
# Add to PATH if not already there
|
||||
if ($env:PATH -notlike "*$installDir*") {
|
||||
$env:PATH = "$installDir;$env:PATH"
|
||||
Log-Info "Added to PATH: $installDir"
|
||||
}
|
||||
|
||||
return $smctlPath
|
||||
}
|
||||
|
||||
Log-Info "Installing KeyLocker tools to: $installDir"
|
||||
|
||||
# Create the installation directory if it doesn't exist
|
||||
if (!(Test-Path $installDir)) {
|
||||
Log-Info "Creating installation directory: $installDir"
|
||||
try {
|
||||
New-Item -ItemType Directory -Path $installDir -Force | Out-Null
|
||||
Log-Success "Created directory: $installDir"
|
||||
} catch {
|
||||
throw "Failed to create directory $installDir : $_"
|
||||
}
|
||||
}
|
||||
|
||||
# Download MSI installer
|
||||
$msiUrl = "https://bun-ci-assets.bun.sh/Keylockertools-windows-x64.msi"
|
||||
$msiPath = Join-Path $env:TEMP "Keylockertools-windows-x64.msi"
|
||||
|
||||
Log-Info "Downloading MSI from: $msiUrl"
|
||||
Log-Info "Downloading to: $msiPath"
|
||||
|
||||
try {
|
||||
# Remove existing MSI if present
|
||||
if (Test-Path $msiPath) {
|
||||
Remove-Item $msiPath -Force
|
||||
Log-Debug "Removed existing MSI file"
|
||||
}
|
||||
|
||||
# Download with progress tracking
|
||||
$webClient = New-Object System.Net.WebClient
|
||||
$webClient.DownloadFile($msiUrl, $msiPath)
|
||||
|
||||
if (!(Test-Path $msiPath)) {
|
||||
throw "MSI download failed - file not found"
|
||||
}
|
||||
|
||||
$fileSize = (Get-Item $msiPath).Length
|
||||
Log-Success "MSI downloaded successfully (size: $fileSize bytes)"
|
||||
|
||||
} catch {
|
||||
throw "Failed to download MSI: $_"
|
||||
}
|
||||
|
||||
# Install MSI
|
||||
Log-Info "Installing MSI..."
|
||||
Log-Debug "MSI path: $msiPath"
|
||||
Log-Debug "File exists: $(Test-Path $msiPath)"
|
||||
Log-Debug "File size: $((Get-Item $msiPath).Length) bytes"
|
||||
|
||||
# Check if running as administrator
|
||||
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
|
||||
Log-Info "Running as administrator: $isAdmin"
|
||||
|
||||
# Install MSI silently to our controlled directory
|
||||
$arguments = @(
|
||||
"/i", "`"$msiPath`"",
|
||||
"/quiet",
|
||||
"/norestart",
|
||||
"TARGETDIR=`"$installDir`"",
|
||||
"INSTALLDIR=`"$installDir`"",
|
||||
"ACCEPT_EULA=1",
|
||||
"ADDLOCAL=ALL"
|
||||
)
|
||||
|
||||
Log-Debug "Running: msiexec.exe $($arguments -join ' ')"
|
||||
Log-Info "Installing to: $installDir"
|
||||
|
||||
$process = Start-Process -FilePath "msiexec.exe" -ArgumentList $arguments -Wait -PassThru -NoNewWindow
|
||||
|
||||
if ($process.ExitCode -ne 0) {
|
||||
Log-Error "MSI installation failed with exit code: $($process.ExitCode)"
|
||||
|
||||
# Try to get error details from event log
|
||||
try {
|
||||
$events = Get-WinEvent -LogName "Application" -MaxEvents 10 |
|
||||
Where-Object { $_.ProviderName -eq "MsiInstaller" -and $_.TimeCreated -gt (Get-Date).AddMinutes(-1) }
|
||||
|
||||
foreach ($event in $events) {
|
||||
Log-Debug "MSI Event: $($event.Message)"
|
||||
}
|
||||
} catch {
|
||||
Log-Debug "Could not retrieve MSI installation events"
|
||||
}
|
||||
|
||||
throw "MSI installation failed with exit code: $($process.ExitCode)"
|
||||
}
|
||||
|
||||
Log-Success "MSI installation completed"
|
||||
|
||||
# Wait for installation to complete
|
||||
Start-Sleep -Seconds 2
|
||||
|
||||
# Verify smctl.exe exists in our controlled location
|
||||
if (Test-Path $smctlPath) {
|
||||
Log-Success "KeyLocker tools installed successfully at: $smctlPath"
|
||||
|
||||
# Add to PATH
|
||||
$env:PATH = "$installDir;$env:PATH"
|
||||
Log-Info "Added to PATH: $installDir"
|
||||
|
||||
return $smctlPath
|
||||
}
|
||||
|
||||
# If not in our expected location, check if it installed somewhere in the directory
|
||||
$found = Get-ChildItem -Path $installDir -Filter "smctl.exe" -Recurse -ErrorAction SilentlyContinue |
|
||||
Select-Object -First 1
|
||||
|
||||
if ($found) {
|
||||
Log-Success "Found smctl.exe at: $($found.FullName)"
|
||||
$smctlDir = $found.DirectoryName
|
||||
$env:PATH = "$smctlDir;$env:PATH"
|
||||
return $found.FullName
|
||||
}
|
||||
|
||||
throw "KeyLocker tools installation succeeded but smctl.exe not found in $installDir"
|
||||
}
|
||||
|
||||
# Configure KeyLocker
|
||||
function Configure-KeyLocker {
|
||||
param([string]$SmctlPath)
|
||||
|
||||
Log-Info "Configuring KeyLocker..."
|
||||
|
||||
# Verify smctl is accessible
|
||||
try {
|
||||
$version = & $SmctlPath --version 2>&1
|
||||
Log-Debug "smctl version: $version"
|
||||
} catch {
|
||||
throw "Failed to run smctl: $_"
|
||||
}
|
||||
|
||||
# Configure KeyLocker credentials and environment
|
||||
Log-Info "Configuring KeyLocker credentials..."
|
||||
|
||||
try {
|
||||
# Save credentials (API key and password)
|
||||
Log-Info "Saving credentials to OS store..."
|
||||
$saveOutput = & $SmctlPath credentials save $env:SM_API_KEY $env:SM_CLIENT_CERT_PASSWORD 2>&1 | Out-String
|
||||
Log-Debug "Credentials save output: $saveOutput"
|
||||
|
||||
if ($saveOutput -like "*Credentials saved*") {
|
||||
Log-Success "Credentials saved successfully"
|
||||
}
|
||||
|
||||
# Set environment variables for smctl
|
||||
Log-Info "Setting KeyLocker environment variables..."
|
||||
$env:SM_HOST = $env:SM_HOST # Already set, but ensure it's available
|
||||
$env:SM_API_KEY = $env:SM_API_KEY # Already set
|
||||
$env:SM_CLIENT_CERT_FILE = $env:SM_CLIENT_CERT_FILE # Path to decoded cert file
|
||||
Log-Debug "SM_HOST: $env:SM_HOST"
|
||||
Log-Debug "SM_CLIENT_CERT_FILE: $env:SM_CLIENT_CERT_FILE"
|
||||
|
||||
# Run health check
|
||||
Log-Info "Running KeyLocker health check..."
|
||||
$healthOutput = & $SmctlPath healthcheck 2>&1 | Out-String
|
||||
Log-Debug "Health check output: $healthOutput"
|
||||
|
||||
if ($healthOutput -like "*Healthy*" -or $healthOutput -like "*SUCCESS*" -or $LASTEXITCODE -eq 0) {
|
||||
Log-Success "KeyLocker health check passed"
|
||||
} else {
|
||||
Log-Error "Health check failed: $healthOutput"
|
||||
# Don't throw here, sometimes healthcheck is flaky but signing still works
|
||||
}
|
||||
|
||||
# Sync certificates to Windows certificate store
|
||||
Log-Info "Syncing certificates to Windows store..."
|
||||
$syncOutput = & $SmctlPath windows certsync 2>&1 | Out-String
|
||||
Log-Debug "Certificate sync output: $syncOutput"
|
||||
|
||||
if ($syncOutput -like "*success*" -or $syncOutput -like "*synced*" -or $LASTEXITCODE -eq 0) {
|
||||
Log-Success "Certificates synced to Windows store"
|
||||
} else {
|
||||
Log-Info "Certificate sync output: $syncOutput"
|
||||
}
|
||||
|
||||
} catch {
|
||||
throw "Failed to configure KeyLocker: $_"
|
||||
}
|
||||
}
|
||||
|
||||
# Sign an executable
|
||||
function Sign-Executable {
|
||||
param(
|
||||
[string]$ExePath,
|
||||
[string]$SmctlPath
|
||||
)
|
||||
|
||||
if (!(Test-Path $ExePath)) {
|
||||
throw "Executable not found: $ExePath"
|
||||
}
|
||||
|
||||
$fileName = Split-Path $ExePath -Leaf
|
||||
Log-Info "Signing $fileName..."
|
||||
Log-Debug "Full path: $ExePath"
|
||||
Log-Debug "File size: $((Get-Item $ExePath).Length) bytes"
|
||||
|
||||
# Check if already signed
|
||||
$existingSig = Get-AuthenticodeSignature $ExePath
|
||||
if ($existingSig.Status -eq "Valid") {
|
||||
Log-Info "$fileName is already signed by: $($existingSig.SignerCertificate.Subject)"
|
||||
Log-Info "Skipping re-signing"
|
||||
return
|
||||
}
|
||||
|
||||
# Sign the executable using smctl
|
||||
try {
|
||||
# smctl sign command with keypair-alias
|
||||
$signArgs = @(
|
||||
"sign",
|
||||
"--keypair-alias", $env:SM_KEYPAIR_ALIAS,
|
||||
"--input", $ExePath,
|
||||
"--verbose"
|
||||
)
|
||||
|
||||
Log-Debug "Running: $SmctlPath $($signArgs -join ' ')"
|
||||
|
||||
$signOutput = & $SmctlPath $signArgs 2>&1 | Out-String
|
||||
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Log-Error "Signing output: $signOutput"
|
||||
throw "Signing failed with exit code: $LASTEXITCODE"
|
||||
}
|
||||
|
||||
Log-Debug "Signing output: $signOutput"
|
||||
Log-Success "Signing command completed"
|
||||
|
||||
} catch {
|
||||
throw "Failed to sign $fileName : $_"
|
||||
}
|
||||
|
||||
# Verify signature
|
||||
$newSig = Get-AuthenticodeSignature $ExePath
|
||||
|
||||
if ($newSig.Status -eq "Valid") {
|
||||
Log-Success "$fileName signed successfully"
|
||||
Log-Info "Signed by: $($newSig.SignerCertificate.Subject)"
|
||||
Log-Info "Thumbprint: $($newSig.SignerCertificate.Thumbprint)"
|
||||
Log-Info "Valid from: $($newSig.SignerCertificate.NotBefore) to $($newSig.SignerCertificate.NotAfter)"
|
||||
} else {
|
||||
throw "$fileName signature verification failed: $($newSig.Status) - $($newSig.StatusMessage)"
|
||||
}
|
||||
}
|
||||
|
||||
# Cleanup function
|
||||
function Cleanup {
|
||||
if ($global:TEMP_CERT_PATH -and (Test-Path $global:TEMP_CERT_PATH)) {
|
||||
try {
|
||||
Remove-Item $global:TEMP_CERT_PATH -Force
|
||||
Log-Info "Cleaned up temporary certificate"
|
||||
} catch {
|
||||
Log-Error "Failed to cleanup temporary certificate: $_"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Main execution
|
||||
try {
|
||||
Write-Host "========================================" -ForegroundColor Cyan
|
||||
Write-Host " Windows Code Signing for Bun" -ForegroundColor Cyan
|
||||
Write-Host "========================================" -ForegroundColor Cyan
|
||||
|
||||
# Ensure we're in a VS environment
|
||||
Ensure-VSEnvironment
|
||||
|
||||
# Check environment variables
|
||||
Check-Environment
|
||||
|
||||
# Setup certificate
|
||||
Setup-Certificate
|
||||
|
||||
# Install and configure KeyLocker
|
||||
$smctlPath = Install-KeyLocker
|
||||
Configure-KeyLocker -SmctlPath $smctlPath
|
||||
|
||||
# Sign both executables
|
||||
Sign-Executable -ExePath $BunProfileExe -SmctlPath $smctlPath
|
||||
Sign-Executable -ExePath $BunExe -SmctlPath $smctlPath
|
||||
|
||||
Write-Host "========================================" -ForegroundColor Green
|
||||
Write-Host " Code signing completed successfully!" -ForegroundColor Green
|
||||
Write-Host "========================================" -ForegroundColor Green
|
||||
|
||||
exit 0
|
||||
|
||||
} catch {
|
||||
Log-Error "Code signing failed: $_"
|
||||
exit 1
|
||||
|
||||
} finally {
|
||||
Cleanup
|
||||
}
|
||||
4
.github/workflows/auto-label-claude-prs.yml
vendored
4
.github/workflows/auto-label-claude-prs.yml
vendored
@@ -6,7 +6,7 @@ on:
|
||||
|
||||
jobs:
|
||||
auto-label:
|
||||
if: github.event.pull_request.user.login == 'robobun'
|
||||
if: github.event.pull_request.user.login == 'robobun' || contains(github.event.pull_request.body, '🤖 Generated with')
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
@@ -21,4 +21,4 @@ jobs:
|
||||
repo: context.repo.repo,
|
||||
issue_number: context.issue.number,
|
||||
labels: ['claude']
|
||||
});
|
||||
});
|
||||
|
||||
23
.github/workflows/format.yml
vendored
23
.github/workflows/format.yml
vendored
@@ -8,10 +8,8 @@ on:
|
||||
workflow_dispatch:
|
||||
pull_request:
|
||||
merge_group:
|
||||
push:
|
||||
branches: ["main"]
|
||||
env:
|
||||
BUN_VERSION: "1.2.11"
|
||||
BUN_VERSION: "1.2.20"
|
||||
LLVM_VERSION: "19.1.7"
|
||||
LLVM_VERSION_MAJOR: "19"
|
||||
|
||||
@@ -37,13 +35,14 @@ jobs:
|
||||
- name: Setup Dependencies
|
||||
run: |
|
||||
bun install
|
||||
bun scripts/glob-sources.mjs
|
||||
- name: Format Code
|
||||
run: |
|
||||
# Start prettier in background with prefixed output
|
||||
echo "::group::Prettier"
|
||||
(bun run prettier 2>&1 | sed 's/^/[prettier] /' || echo "[prettier] Failed with exit code $?") &
|
||||
PRETTIER_PID=$!
|
||||
|
||||
|
||||
# Start clang-format installation and formatting in background with prefixed output
|
||||
echo "::group::Clang-format"
|
||||
(
|
||||
@@ -56,13 +55,13 @@ jobs:
|
||||
LLVM_VERSION_MAJOR=${{ env.LLVM_VERSION_MAJOR }} ./scripts/run-clang-format.sh format 2>&1 | sed 's/^/[clang-format] /'
|
||||
) &
|
||||
CLANG_PID=$!
|
||||
|
||||
|
||||
# Setup Zig in temp directory and run zig fmt in background with prefixed output
|
||||
echo "::group::Zig fmt"
|
||||
(
|
||||
ZIG_TEMP=$(mktemp -d)
|
||||
echo "[zig] Downloading Zig (musl build)..."
|
||||
wget -q -O "$ZIG_TEMP/zig.zip" https://github.com/oven-sh/zig/releases/download/autobuild-d1a4e0b0ddc75f37c6a090b97eef0cbb6335556e/bootstrap-x86_64-linux-musl.zip
|
||||
wget -q -O "$ZIG_TEMP/zig.zip" https://github.com/oven-sh/zig/releases/download/autobuild-e0b7c318f318196c5f81fdf3423816a7b5bb3112/bootstrap-x86_64-linux-musl.zip
|
||||
unzip -q -d "$ZIG_TEMP" "$ZIG_TEMP/zig.zip"
|
||||
export PATH="$ZIG_TEMP/bootstrap-x86_64-linux-musl:$PATH"
|
||||
echo "[zig] Running zig fmt..."
|
||||
@@ -72,36 +71,36 @@ jobs:
|
||||
rm -rf "$ZIG_TEMP"
|
||||
) &
|
||||
ZIG_PID=$!
|
||||
|
||||
|
||||
# Wait for all formatting tasks to complete
|
||||
echo ""
|
||||
echo "Running formatters in parallel..."
|
||||
FAILED=0
|
||||
|
||||
|
||||
if ! wait $PRETTIER_PID; then
|
||||
echo "::error::Prettier failed"
|
||||
FAILED=1
|
||||
fi
|
||||
echo "::endgroup::"
|
||||
|
||||
|
||||
if ! wait $CLANG_PID; then
|
||||
echo "::error::Clang-format failed"
|
||||
FAILED=1
|
||||
fi
|
||||
echo "::endgroup::"
|
||||
|
||||
|
||||
if ! wait $ZIG_PID; then
|
||||
echo "::error::Zig fmt failed"
|
||||
FAILED=1
|
||||
fi
|
||||
echo "::endgroup::"
|
||||
|
||||
|
||||
# Exit with error if any formatter failed
|
||||
if [ $FAILED -eq 1 ]; then
|
||||
echo "::error::One or more formatters failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
echo "✅ All formatters completed successfully"
|
||||
- name: Ban Words
|
||||
run: |
|
||||
|
||||
41
.github/workflows/glob-sources.yml
vendored
41
.github/workflows/glob-sources.yml
vendored
@@ -1,41 +0,0 @@
|
||||
name: Glob Sources
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
on:
|
||||
workflow_call:
|
||||
workflow_dispatch:
|
||||
pull_request:
|
||||
|
||||
env:
|
||||
BUN_VERSION: "1.2.11"
|
||||
|
||||
jobs:
|
||||
glob-sources:
|
||||
name: Glob Sources
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Configure Git
|
||||
run: |
|
||||
git config --global core.autocrlf true
|
||||
git config --global core.ignorecase true
|
||||
git config --global core.precomposeUnicode true
|
||||
- name: Setup Bun
|
||||
uses: ./.github/actions/setup-bun
|
||||
with:
|
||||
bun-version: ${{ env.BUN_VERSION }}
|
||||
- name: Setup Dependencies
|
||||
run: |
|
||||
bun install
|
||||
- name: Glob sources
|
||||
run: bun scripts/glob-sources.mjs
|
||||
- name: Commit
|
||||
uses: stefanzweifel/git-auto-commit-action@v5
|
||||
with:
|
||||
commit_message: "`bun scripts/glob-sources.mjs`"
|
||||
|
||||
41
.github/workflows/labeled.yml
vendored
41
.github/workflows/labeled.yml
vendored
@@ -5,6 +5,8 @@ env:
|
||||
on:
|
||||
issues:
|
||||
types: [labeled]
|
||||
pull_request_target:
|
||||
types: [labeled, opened, reopened, synchronize, unlabeled]
|
||||
|
||||
jobs:
|
||||
# on-bug:
|
||||
@@ -43,9 +45,46 @@ jobs:
|
||||
# token: ${{ secrets.GITHUB_TOKEN }}
|
||||
# issue-number: ${{ github.event.issue.number }}
|
||||
# labels: ${{ steps.add-labels.outputs.labels }}
|
||||
on-slop:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name == 'pull_request_target' && contains(github.event.pull_request.labels.*.name, 'slop')
|
||||
permissions:
|
||||
issues: write
|
||||
pull-requests: write
|
||||
contents: write
|
||||
steps:
|
||||
- name: Update PR title and body for slop and close
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
const pr = await github.rest.pulls.get({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
pull_number: context.issue.number
|
||||
});
|
||||
|
||||
await github.rest.pulls.update({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
pull_number: context.issue.number,
|
||||
title: 'ai slop',
|
||||
body: 'This PR has been marked as AI slop and the description has been updated to avoid confusion or misleading reviewers.\n\nMany AI PRs are fine, but sometimes they submit a PR too early, fail to test if the problem is real, fail to reproduce the problem, or fail to test that the problem is fixed. If you think this PR is not AI slop, please leave a comment.',
|
||||
state: 'closed'
|
||||
});
|
||||
|
||||
// Delete the branch if it's from a fork or if it's not a protected branch
|
||||
try {
|
||||
await github.rest.git.deleteRef({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
ref: `heads/${pr.data.head.ref}`
|
||||
});
|
||||
} catch (error) {
|
||||
console.log('Could not delete branch:', error.message);
|
||||
}
|
||||
on-labeled:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event.label.name == 'crash' || github.event.label.name == 'needs repro'
|
||||
if: github.event_name == 'issues' && (github.event.label.name == 'crash' || github.event.label.name == 'needs repro')
|
||||
permissions:
|
||||
issues: write
|
||||
steps:
|
||||
|
||||
@@ -223,8 +223,8 @@ $ git clone https://github.com/oven-sh/WebKit vendor/WebKit
|
||||
$ 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
|
||||
# Optionally, you can use `bun run jsc:build` for a release build
|
||||
$ bun run jsc:build:debug && rm vendor/WebKit/WebKitBuild/Debug/JavaScriptCore/DerivedSources/inspector/InspectorProtocolObjects.h
|
||||
|
||||
# After an initial run of `make jsc-debug`, you can rebuild JSC with:
|
||||
$ cmake --build vendor/WebKit/WebKitBuild/Debug --target jsc && rm vendor/WebKit/WebKitBuild/Debug/JavaScriptCore/DerivedSources/inspector/InspectorProtocolObjects.h
|
||||
|
||||
176
STATUS.md
Normal file
176
STATUS.md
Normal file
@@ -0,0 +1,176 @@
|
||||
# Node.js SQLite API Implementation Status
|
||||
|
||||
## Overview
|
||||
|
||||
This document tracks the implementation of `node:sqlite` support in Bun to match the Node.js SQLite API. The implementation follows Bun's architectural patterns using JavaScriptCore (JSC) bindings and native modules.
|
||||
|
||||
## ✅ Actually Working Stuff
|
||||
|
||||
### 1. Module Loading & Constructor Export ✅ (Finally!)
|
||||
- **Module Loading**: `require('node:sqlite')` works without crashing
|
||||
- **Constructor Export**: `new sqlite.DatabaseSync()` actually works now
|
||||
- **Class Architecture**: Proper JSC class structure with Prototype/Constructor/Instance pattern
|
||||
- **Build System**: Compiles successfully (though took way too many iterations)
|
||||
|
||||
### 2. JSC Integration ✅
|
||||
- **LazyClassStructure Pattern**: Applied X509Certificate pattern correctly after several failed attempts
|
||||
- **Memory Management**: Proper ISO subspaces and garbage collection hooks
|
||||
- **Module Registration**: Added to builtin module registry and enum generation
|
||||
- **Static Properties**: Removed assertion conflicts by NOT using HasStaticPropertyTable
|
||||
|
||||
## 🤷♂️ What We Actually Have
|
||||
|
||||
### The Good News
|
||||
- The module loads
|
||||
- The constructor can be instantiated
|
||||
- No more "assertion failed" crashes during startup
|
||||
- All the scaffolding is in place
|
||||
- Follows Bun's architectural patterns properly
|
||||
|
||||
### The Reality Check
|
||||
- **Zero SQLite functionality**: All methods return `undefined`
|
||||
- **No database operations**: Can't open, read, write, or query anything
|
||||
- **Placeholder methods**: `open()`, `close()`, `exec()`, `prepare()` do absolutely nothing
|
||||
- **No error handling**: Will probably explode if you try to do real work
|
||||
- **StatementSync**: Completely unimplemented beyond the constructor
|
||||
|
||||
## 🔍 The Brutal Truth About What We Accomplished
|
||||
|
||||
### What Took Forever (Constructor Export Issue)
|
||||
- **3+ iterations** trying different JSC patterns
|
||||
- **Multiple assertion failures** from HasStaticPropertyTable misconfigurations
|
||||
- **Hours debugging** LazyClassStructure timing issues
|
||||
- **Final solution**: Literally just follow the X509Certificate pattern exactly
|
||||
- **Key insight**: Don't try to be clever, copy what works
|
||||
|
||||
### Files That Actually Matter
|
||||
- `JSNodeSQLiteDatabaseSyncPrototype.{h,cpp}` - Object prototype (mostly empty)
|
||||
- `JSNodeSQLiteDatabaseSyncConstructor.{h,cpp}` - Function prototype (works!)
|
||||
- `JSNodeSQLiteDatabaseSync.{h,cpp}` - Main class (has SQLite* member, does nothing with it)
|
||||
- `NodeSQLiteModule.h` - Native module exports (uses LazyClassStructure correctly)
|
||||
- `isBuiltinModule.cpp` - Module registry (needed for `require()` to work)
|
||||
|
||||
### What We Learned The Hard Way
|
||||
1. **JSC is picky**: Structure flags must match exactly what you declare
|
||||
2. **Timing matters**: LazyClassStructure can't be accessed during certain init phases
|
||||
3. **Copy existing patterns**: Don't reinvent, just follow X509Certificate exactly
|
||||
4. **Assertions are your friend**: When JSC crashes, it's usually a structure mismatch
|
||||
|
||||
## ⚠️ Current Status: "It Compiles and Runs"
|
||||
|
||||
### What Works Right Now
|
||||
```javascript
|
||||
const sqlite = require('node:sqlite'); // ✅ Loads
|
||||
const db = new sqlite.DatabaseSync(); // ✅ Creates object
|
||||
console.log(typeof db.open); // ✅ "function"
|
||||
db.open(); // ✅ Returns undefined, does nothing
|
||||
```
|
||||
|
||||
### What Definitely Doesn't Work
|
||||
```javascript
|
||||
db.open('my.db'); // ❌ Ignores filename, does nothing
|
||||
const stmt = db.prepare('SELECT 1'); // ❌ Returns undefined instead of statement
|
||||
stmt.get(); // ❌ stmt is undefined, will crash
|
||||
```
|
||||
|
||||
## 🎯 What Actually Needs To Happen Next
|
||||
|
||||
### The Real Work (Implementing SQLite)
|
||||
1. **DatabaseSync.open(filename)**: Actually call `sqlite3_open()`
|
||||
2. **DatabaseSync.exec(sql)**: Actually call `sqlite3_exec()`
|
||||
3. **DatabaseSync.prepare(sql)**: Return a real StatementSync object
|
||||
4. **StatementSync methods**: `run()`, `get()`, `all()`, `iterate()` - none exist
|
||||
5. **Error handling**: Map SQLite errors to JavaScript exceptions
|
||||
6. **Parameter binding**: Support `?` placeholders in SQL
|
||||
7. **Result handling**: Convert SQLite results to JavaScript objects
|
||||
|
||||
### Testing Reality Check
|
||||
- **No real tests**: Just "does it load without crashing"
|
||||
- **Node.js compatibility**: Probably fails every single test
|
||||
- **Edge cases**: Haven't even thought about them yet
|
||||
- **Memory leaks**: Probably has them since we don't close SQLite handles
|
||||
|
||||
## 📊 Honest Assessment
|
||||
|
||||
### Completion Percentage: ~15%
|
||||
- ✅ **Architecture (15%)**: JSC classes, module loading, build system
|
||||
- ❌ **Functionality (0%)**: No actual SQLite operations
|
||||
- ❌ **Testing (0%)**: No meaningful test coverage
|
||||
- ❌ **Compatibility (0%)**: Doesn't match Node.js behavior yet
|
||||
|
||||
### Time Spent vs Value
|
||||
- **90% of time**: Fighting JSC assertion failures and class structure issues
|
||||
- **10% of time**: Actual SQLite functionality (which doesn't work)
|
||||
- **Result**: A very well-architected module that does absolutely nothing
|
||||
|
||||
## 🔧 Development Commands
|
||||
|
||||
```bash
|
||||
# Build (takes ~5 minutes, be patient)
|
||||
bun bd
|
||||
|
||||
# Test what actually works (module loading)
|
||||
/workspace/bun/build/debug/bun-debug -e "
|
||||
const sqlite = require('node:sqlite');
|
||||
console.log('Module loaded:', Object.keys(sqlite));
|
||||
const db = new sqlite.DatabaseSync();
|
||||
console.log('Constructor works:', typeof db);
|
||||
"
|
||||
|
||||
# Test what doesn't work (everything else)
|
||||
/workspace/bun/build/debug/bun-debug -e "
|
||||
const sqlite = require('node:sqlite');
|
||||
const db = new sqlite.DatabaseSync();
|
||||
db.open('test.db'); // Does nothing
|
||||
console.log('Opened database... not really');
|
||||
"
|
||||
```
|
||||
|
||||
## 🤔 Lessons Learned
|
||||
|
||||
### Technical Insights
|
||||
1. **JSC patterns are rigid**: Follow existing examples exactly, don't improvise
|
||||
2. **LazyClassStructure is powerful**: But only when used correctly
|
||||
3. **Build system complexity**: Small changes require understanding the entire pipeline
|
||||
4. **Debugging is hard**: JSC assertion failures are cryptic but usually structure-related
|
||||
|
||||
### Development Philosophy
|
||||
1. **Get it working first**: Architecture is worthless if it doesn't run
|
||||
2. **Copy successful patterns**: X509Certificate saved the day
|
||||
3. **Incremental progress**: Module loading → Constructor → Methods → Functionality
|
||||
4. **Honest documentation**: Better to admit what doesn't work than pretend it does
|
||||
|
||||
## 🎯 Next Steps (For Someone Brave Enough)
|
||||
|
||||
### Immediate (Actually Implement SQLite)
|
||||
1. Fill in the `JSNodeSQLiteDatabaseSync::open()` method with real `sqlite3_open()` calls
|
||||
2. Implement `exec()` with proper SQL execution and result handling
|
||||
3. Create real `StatementSync` objects instead of returning undefined
|
||||
4. Add basic error handling so it doesn't crash on invalid SQL
|
||||
|
||||
### Short Term (Make It Usable)
|
||||
1. Parameter binding for prepared statements
|
||||
2. Result set handling for SELECT queries
|
||||
3. Transaction support (begin/commit/rollback)
|
||||
4. Basic Node.js compatibility testing
|
||||
|
||||
### Long Term (Production Ready)
|
||||
1. Full Node.js sqlite test suite compatibility
|
||||
2. Performance optimization
|
||||
3. Memory leak prevention
|
||||
4. Edge case handling
|
||||
|
||||
## 🏁 Bottom Line
|
||||
|
||||
We have successfully implemented **the hard part** (JSC integration and module architecture) and **none of the easy part** (actual SQLite functionality). It's a solid foundation that does absolutely nothing useful yet.
|
||||
|
||||
The good news: Adding SQLite functionality should be straightforward now that the class structure is working. The bad news: That's still like 85% of the actual work.
|
||||
|
||||
But hey, at least it doesn't crash anymore! 🎉
|
||||
|
||||
---
|
||||
|
||||
*Status updated 2025-08-06 after implementing proper JSC class architecture*
|
||||
*Previous status: "Constructor export assertion failures"*
|
||||
*Current status: "Constructor works, SQLite functionality doesn't exist"*
|
||||
*Next milestone: "Make it actually do something with databases"*
|
||||
116
bench/postMessage/postMessage-object.mjs
Normal file
116
bench/postMessage/postMessage-object.mjs
Normal file
@@ -0,0 +1,116 @@
|
||||
// Benchmark for object fast path optimization in postMessage with Workers
|
||||
|
||||
import { bench, run } from "mitata";
|
||||
import { Worker } from "node:worker_threads";
|
||||
|
||||
const extraProperties = {
|
||||
a: "a!",
|
||||
b: "b!",
|
||||
"second": "c!",
|
||||
bool: true,
|
||||
nully: null,
|
||||
undef: undefined,
|
||||
int: 0,
|
||||
double: 1.234,
|
||||
falsy: false,
|
||||
};
|
||||
|
||||
const objects = {
|
||||
small: { property: "Hello world", ...extraProperties },
|
||||
medium: {
|
||||
property: Buffer.alloc("Hello World!!!".length * 1024, "Hello World!!!").toString(),
|
||||
...extraProperties,
|
||||
},
|
||||
large: {
|
||||
property: Buffer.alloc("Hello World!!!".length * 1024 * 256, "Hello World!!!").toString(),
|
||||
...extraProperties,
|
||||
},
|
||||
};
|
||||
|
||||
let worker;
|
||||
let receivedCount = new Int32Array(new SharedArrayBuffer(4));
|
||||
let sentCount = 0;
|
||||
|
||||
function createWorker() {
|
||||
const workerCode = `
|
||||
import { parentPort, workerData } from "node:worker_threads";
|
||||
|
||||
let int = workerData;
|
||||
|
||||
parentPort?.on("message", data => {
|
||||
switch (data.property.length) {
|
||||
case ${objects.small.property.length}:
|
||||
case ${objects.medium.property.length}:
|
||||
case ${objects.large.property.length}: {
|
||||
if (
|
||||
data.a === "a!" &&
|
||||
data.b === "b!" &&
|
||||
data.second === "c!" &&
|
||||
data.bool === true &&
|
||||
data.nully === null &&
|
||||
data.undef === undefined &&
|
||||
data.int === 0 &&
|
||||
data.double === 1.234 &&
|
||||
data.falsy === false) {
|
||||
Atomics.add(int, 0, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
default: {
|
||||
throw new Error("Invalid data object: " + JSON.stringify(data));
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
`;
|
||||
|
||||
worker = new Worker(workerCode, { eval: true, workerData: receivedCount });
|
||||
|
||||
worker.on("message", confirmationId => {});
|
||||
|
||||
worker.on("error", error => {
|
||||
console.error("Worker error:", error);
|
||||
});
|
||||
}
|
||||
|
||||
// Initialize worker before running benchmarks
|
||||
createWorker();
|
||||
|
||||
function fmt(int) {
|
||||
if (int < 1000) {
|
||||
return `${int} chars`;
|
||||
}
|
||||
|
||||
if (int < 100000) {
|
||||
return `${(int / 1024) | 0} KB`;
|
||||
}
|
||||
|
||||
return `${(int / 1024 / 1024) | 0} MB`;
|
||||
}
|
||||
|
||||
// Benchmark postMessage with pure strings (uses fast path)
|
||||
bench("postMessage({ prop: " + fmt(objects.small.property.length) + " string, ...9 more props })", async () => {
|
||||
sentCount++;
|
||||
worker.postMessage(objects.small);
|
||||
});
|
||||
|
||||
bench("postMessage({ prop: " + fmt(objects.medium.property.length) + " string, ...9 more props })", async () => {
|
||||
sentCount++;
|
||||
worker.postMessage(objects.medium);
|
||||
});
|
||||
|
||||
bench("postMessage({ prop: " + fmt(objects.large.property.length) + " string, ...9 more props })", async () => {
|
||||
sentCount++;
|
||||
worker.postMessage(objects.large);
|
||||
});
|
||||
|
||||
await run();
|
||||
|
||||
await new Promise(resolve => setTimeout(resolve, 5000));
|
||||
|
||||
if (receivedCount[0] !== sentCount) {
|
||||
throw new Error("Expected " + receivedCount[0] + " to equal " + sentCount);
|
||||
}
|
||||
|
||||
// Cleanup worker
|
||||
worker?.terminate();
|
||||
Binary file not shown.
58
bench/postgres/mysql.mjs
Normal file
58
bench/postgres/mysql.mjs
Normal file
@@ -0,0 +1,58 @@
|
||||
const isBun = typeof globalThis?.Bun?.sql !== "undefined";
|
||||
let conn;
|
||||
let sql;
|
||||
import * as mariadb from "mariadb";
|
||||
import * as mysql2 from "mysql2/promise";
|
||||
let useMYSQL2 = false;
|
||||
if (process.argv.includes("--mysql2")) {
|
||||
useMYSQL2 = true;
|
||||
}
|
||||
if (isBun) {
|
||||
sql = new Bun.SQL({
|
||||
adapter: "mysql",
|
||||
database: "test",
|
||||
username: "root",
|
||||
});
|
||||
} else {
|
||||
const pool = (useMYSQL2 ? mysql2 : mariadb).createPool({
|
||||
// Add your MariaDB connection details here
|
||||
user: "root",
|
||||
database: "test",
|
||||
});
|
||||
conn = await pool.getConnection();
|
||||
}
|
||||
|
||||
if (isBun) {
|
||||
// Initialize the benchmark table (equivalent to initFct)
|
||||
await sql`DROP TABLE IF EXISTS test100`;
|
||||
await sql`CREATE TABLE test100 (i1 int,i2 int,i3 int,i4 int,i5 int,i6 int,i7 int,i8 int,i9 int,i10 int,i11 int,i12 int,i13 int,i14 int,i15 int,i16 int,i17 int,i18 int,i19 int,i20 int,i21 int,i22 int,i23 int,i24 int,i25 int,i26 int,i27 int,i28 int,i29 int,i30 int,i31 int,i32 int,i33 int,i34 int,i35 int,i36 int,i37 int,i38 int,i39 int,i40 int,i41 int,i42 int,i43 int,i44 int,i45 int,i46 int,i47 int,i48 int,i49 int,i50 int,i51 int,i52 int,i53 int,i54 int,i55 int,i56 int,i57 int,i58 int,i59 int,i60 int,i61 int,i62 int,i63 int,i64 int,i65 int,i66 int,i67 int,i68 int,i69 int,i70 int,i71 int,i72 int,i73 int,i74 int,i75 int,i76 int,i77 int,i78 int,i79 int,i80 int,i81 int,i82 int,i83 int,i84 int,i85 int,i86 int,i87 int,i88 int,i89 int,i90 int,i91 int,i92 int,i93 int,i94 int,i95 int,i96 int,i97 int,i98 int,i99 int,i100 int)`;
|
||||
await sql`INSERT INTO test100 value (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100)`;
|
||||
} else {
|
||||
// Initialize the benchmark table (equivalent to initFct)
|
||||
await conn.query("DROP TABLE IF EXISTS test100");
|
||||
await conn.query(
|
||||
"CREATE TABLE test100 (i1 int,i2 int,i3 int,i4 int,i5 int,i6 int,i7 int,i8 int,i9 int,i10 int,i11 int,i12 int,i13 int,i14 int,i15 int,i16 int,i17 int,i18 int,i19 int,i20 int,i21 int,i22 int,i23 int,i24 int,i25 int,i26 int,i27 int,i28 int,i29 int,i30 int,i31 int,i32 int,i33 int,i34 int,i35 int,i36 int,i37 int,i38 int,i39 int,i40 int,i41 int,i42 int,i43 int,i44 int,i45 int,i46 int,i47 int,i48 int,i49 int,i50 int,i51 int,i52 int,i53 int,i54 int,i55 int,i56 int,i57 int,i58 int,i59 int,i60 int,i61 int,i62 int,i63 int,i64 int,i65 int,i66 int,i67 int,i68 int,i69 int,i70 int,i71 int,i72 int,i73 int,i74 int,i75 int,i76 int,i77 int,i78 int,i79 int,i80 int,i81 int,i82 int,i83 int,i84 int,i85 int,i86 int,i87 int,i88 int,i89 int,i90 int,i91 int,i92 int,i93 int,i94 int,i95 int,i96 int,i97 int,i98 int,i99 int,i100 int)",
|
||||
);
|
||||
await conn.query(
|
||||
"INSERT INTO test100 value (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100)",
|
||||
);
|
||||
}
|
||||
// Run the benchmark (equivalent to benchFct)
|
||||
const type = isBun ? "Bun.SQL" : useMYSQL2 ? "mysql2" : "mariadb";
|
||||
console.time(type);
|
||||
let promises = [];
|
||||
|
||||
for (let i = 0; i < 100_000; i++) {
|
||||
if (isBun) {
|
||||
promises.push(sql`select * FROM test100`);
|
||||
} else {
|
||||
promises.push(conn.query("select * FROM test100"));
|
||||
}
|
||||
}
|
||||
await Promise.all(promises);
|
||||
console.timeEnd(type);
|
||||
|
||||
// Clean up connection
|
||||
if (!isBun && conn.release) {
|
||||
conn.release();
|
||||
}
|
||||
@@ -9,6 +9,8 @@
|
||||
"typescript": "^5.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"mariadb": "^3.4.5",
|
||||
"mysql2": "^3.14.3",
|
||||
"postgres": "^3.4.7"
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,9 @@ const scenarios = [
|
||||
{ alg: "sha1", digest: "base64" },
|
||||
{ alg: "sha256", digest: "hex" },
|
||||
{ alg: "sha256", digest: "base64" },
|
||||
{ alg: "blake2b512", digest: "hex" },
|
||||
{ alg: "sha512-224", digest: "hex" },
|
||||
{ alg: "sha512-256", digest: "hex" },
|
||||
];
|
||||
|
||||
for (const { alg, digest } of scenarios) {
|
||||
@@ -23,6 +26,10 @@ for (const { alg, digest } of scenarios) {
|
||||
bench(`${alg}-${digest} (Bun.CryptoHasher)`, () => {
|
||||
new Bun.CryptoHasher(alg).update(data).digest(digest);
|
||||
});
|
||||
|
||||
bench(`${alg}-${digest} (Bun.CryptoHasher.hash)`, () => {
|
||||
return Bun.CryptoHasher.hash(alg, data, digest);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
19
bench/yaml/bun.lock
Normal file
19
bench/yaml/bun.lock
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"lockfileVersion": 1,
|
||||
"workspaces": {
|
||||
"": {
|
||||
"name": "yaml-benchmark",
|
||||
"dependencies": {
|
||||
"js-yaml": "^4.1.0",
|
||||
"yaml": "^2.8.1",
|
||||
},
|
||||
},
|
||||
},
|
||||
"packages": {
|
||||
"argparse": ["argparse@2.0.1", "", {}, "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q=="],
|
||||
|
||||
"js-yaml": ["js-yaml@4.1.0", "", { "dependencies": { "argparse": "^2.0.1" }, "bin": { "js-yaml": "bin/js-yaml.js" } }, "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA=="],
|
||||
|
||||
"yaml": ["yaml@2.8.1", "", { "bin": { "yaml": "bin.mjs" } }, "sha512-lcYcMxX2PO9XMGvAJkJ3OsNMw+/7FKes7/hgerGUYWIoWu5j/+YQqcZr5JnPZWzOsEBgMbSbiSTn/dv/69Mkpw=="],
|
||||
}
|
||||
}
|
||||
8
bench/yaml/package.json
Normal file
8
bench/yaml/package.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"name": "yaml-benchmark",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"js-yaml": "^4.1.0",
|
||||
"yaml": "^2.8.1"
|
||||
}
|
||||
}
|
||||
368
bench/yaml/yaml-parse.mjs
Normal file
368
bench/yaml/yaml-parse.mjs
Normal file
@@ -0,0 +1,368 @@
|
||||
import { bench, group, run } from "../runner.mjs";
|
||||
import jsYaml from "js-yaml";
|
||||
import yaml from "yaml";
|
||||
|
||||
// Small YAML document
|
||||
const smallYaml = `
|
||||
name: John Doe
|
||||
age: 30
|
||||
email: john@example.com
|
||||
active: true
|
||||
`;
|
||||
|
||||
// Medium YAML document with nested structures
|
||||
const mediumYaml = `
|
||||
company: Acme Corp
|
||||
employees:
|
||||
- name: John Doe
|
||||
age: 30
|
||||
position: Developer
|
||||
skills:
|
||||
- JavaScript
|
||||
- TypeScript
|
||||
- Node.js
|
||||
- name: Jane Smith
|
||||
age: 28
|
||||
position: Designer
|
||||
skills:
|
||||
- Figma
|
||||
- Photoshop
|
||||
- Illustrator
|
||||
- name: Bob Johnson
|
||||
age: 35
|
||||
position: Manager
|
||||
skills:
|
||||
- Leadership
|
||||
- Communication
|
||||
- Planning
|
||||
settings:
|
||||
database:
|
||||
host: localhost
|
||||
port: 5432
|
||||
name: mydb
|
||||
cache:
|
||||
enabled: true
|
||||
ttl: 3600
|
||||
`;
|
||||
|
||||
// Large YAML document with complex structures
|
||||
const largeYaml = `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: nginx-deployment
|
||||
labels:
|
||||
app: nginx
|
||||
spec:
|
||||
replicas: 3
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx:1.14.2
|
||||
ports:
|
||||
- containerPort: 80
|
||||
env:
|
||||
- name: ENV_VAR_1
|
||||
value: "value1"
|
||||
- name: ENV_VAR_2
|
||||
value: "value2"
|
||||
volumeMounts:
|
||||
- name: config
|
||||
mountPath: /etc/nginx
|
||||
resources:
|
||||
limits:
|
||||
cpu: "1"
|
||||
memory: "1Gi"
|
||||
requests:
|
||||
cpu: "0.5"
|
||||
memory: "512Mi"
|
||||
volumes:
|
||||
- name: config
|
||||
configMap:
|
||||
name: nginx-config
|
||||
items:
|
||||
- key: nginx.conf
|
||||
path: nginx.conf
|
||||
- key: mime.types
|
||||
path: mime.types
|
||||
nodeSelector:
|
||||
disktype: ssd
|
||||
tolerations:
|
||||
- key: "key1"
|
||||
operator: "Equal"
|
||||
value: "value1"
|
||||
effect: "NoSchedule"
|
||||
- key: "key2"
|
||||
operator: "Exists"
|
||||
effect: "NoExecute"
|
||||
affinity:
|
||||
nodeAffinity:
|
||||
requiredDuringSchedulingIgnoredDuringExecution:
|
||||
nodeSelectorTerms:
|
||||
- matchExpressions:
|
||||
- key: kubernetes.io/e2e-az-name
|
||||
operator: In
|
||||
values:
|
||||
- e2e-az1
|
||||
- e2e-az2
|
||||
podAntiAffinity:
|
||||
preferredDuringSchedulingIgnoredDuringExecution:
|
||||
- weight: 100
|
||||
podAffinityTerm:
|
||||
labelSelector:
|
||||
matchExpressions:
|
||||
- key: app
|
||||
operator: In
|
||||
values:
|
||||
- web-store
|
||||
topologyKey: kubernetes.io/hostname
|
||||
`;
|
||||
|
||||
// YAML with anchors and references
|
||||
const yamlWithAnchors = `
|
||||
defaults: &defaults
|
||||
adapter: postgresql
|
||||
host: localhost
|
||||
port: 5432
|
||||
|
||||
development:
|
||||
<<: *defaults
|
||||
database: dev_db
|
||||
|
||||
test:
|
||||
<<: *defaults
|
||||
database: test_db
|
||||
|
||||
production:
|
||||
<<: *defaults
|
||||
database: prod_db
|
||||
host: prod.example.com
|
||||
`;
|
||||
|
||||
// Array of items
|
||||
const arrayYaml = `
|
||||
- id: 1
|
||||
name: Item 1
|
||||
price: 10.99
|
||||
tags: [electronics, gadgets]
|
||||
- id: 2
|
||||
name: Item 2
|
||||
price: 25.50
|
||||
tags: [books, education]
|
||||
- id: 3
|
||||
name: Item 3
|
||||
price: 5.00
|
||||
tags: [food, snacks]
|
||||
- id: 4
|
||||
name: Item 4
|
||||
price: 100.00
|
||||
tags: [electronics, computers]
|
||||
- id: 5
|
||||
name: Item 5
|
||||
price: 15.75
|
||||
tags: [clothing, accessories]
|
||||
`;
|
||||
|
||||
// Multiline strings
|
||||
const multilineYaml = `
|
||||
description: |
|
||||
This is a multiline string
|
||||
that preserves line breaks
|
||||
and indentation.
|
||||
|
||||
It can contain multiple paragraphs
|
||||
and special characters: !@#$%^&*()
|
||||
|
||||
folded: >
|
||||
This is a folded string
|
||||
where line breaks are converted
|
||||
to spaces unless there are
|
||||
|
||||
empty lines like above.
|
||||
plain: This is a plain string
|
||||
quoted: "This is a quoted string with \\"escapes\\""
|
||||
literal: 'This is a literal string with ''quotes'''
|
||||
`;
|
||||
|
||||
// Numbers and special values
|
||||
const numbersYaml = `
|
||||
integer: 42
|
||||
negative: -17
|
||||
float: 3.14159
|
||||
scientific: 1.23e-4
|
||||
infinity: .inf
|
||||
negativeInfinity: -.inf
|
||||
notANumber: .nan
|
||||
octal: 0o755
|
||||
hex: 0xFF
|
||||
binary: 0b1010
|
||||
`;
|
||||
|
||||
// Dates and timestamps
|
||||
const datesYaml = `
|
||||
date: 2024-01-15
|
||||
datetime: 2024-01-15T10:30:00Z
|
||||
timestamp: 2024-01-15 10:30:00.123456789 -05:00
|
||||
canonical: 2024-01-15T10:30:00.123456789Z
|
||||
`;
|
||||
|
||||
// Parse benchmarks
|
||||
group("parse small YAML", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.parse", () => {
|
||||
globalThis.result = Bun.YAML.parse(smallYaml);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.load", () => {
|
||||
globalThis.result = jsYaml.load(smallYaml);
|
||||
});
|
||||
|
||||
bench("yaml.parse", () => {
|
||||
globalThis.result = yaml.parse(smallYaml);
|
||||
});
|
||||
});
|
||||
|
||||
group("parse medium YAML", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.parse", () => {
|
||||
globalThis.result = Bun.YAML.parse(mediumYaml);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.load", () => {
|
||||
globalThis.result = jsYaml.load(mediumYaml);
|
||||
});
|
||||
|
||||
bench("yaml.parse", () => {
|
||||
globalThis.result = yaml.parse(mediumYaml);
|
||||
});
|
||||
});
|
||||
|
||||
group("parse large YAML", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.parse", () => {
|
||||
globalThis.result = Bun.YAML.parse(largeYaml);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.load", () => {
|
||||
globalThis.result = jsYaml.load(largeYaml);
|
||||
});
|
||||
|
||||
bench("yaml.parse", () => {
|
||||
globalThis.result = yaml.parse(largeYaml);
|
||||
});
|
||||
});
|
||||
|
||||
group("parse YAML with anchors", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.parse", () => {
|
||||
globalThis.result = Bun.YAML.parse(yamlWithAnchors);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.load", () => {
|
||||
globalThis.result = jsYaml.load(yamlWithAnchors);
|
||||
});
|
||||
|
||||
bench("yaml.parse", () => {
|
||||
globalThis.result = yaml.parse(yamlWithAnchors);
|
||||
});
|
||||
});
|
||||
|
||||
group("parse YAML array", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.parse", () => {
|
||||
globalThis.result = Bun.YAML.parse(arrayYaml);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.load", () => {
|
||||
globalThis.result = jsYaml.load(arrayYaml);
|
||||
});
|
||||
|
||||
bench("yaml.parse", () => {
|
||||
globalThis.result = yaml.parse(arrayYaml);
|
||||
});
|
||||
});
|
||||
|
||||
group("parse YAML with multiline strings", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.parse", () => {
|
||||
globalThis.result = Bun.YAML.parse(multilineYaml);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.load", () => {
|
||||
globalThis.result = jsYaml.load(multilineYaml);
|
||||
});
|
||||
|
||||
bench("yaml.parse", () => {
|
||||
globalThis.result = yaml.parse(multilineYaml);
|
||||
});
|
||||
});
|
||||
|
||||
group("parse YAML with numbers", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.parse", () => {
|
||||
globalThis.result = Bun.YAML.parse(numbersYaml);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.load", () => {
|
||||
globalThis.result = jsYaml.load(numbersYaml);
|
||||
});
|
||||
|
||||
bench("yaml.parse", () => {
|
||||
globalThis.result = yaml.parse(numbersYaml);
|
||||
});
|
||||
});
|
||||
|
||||
group("parse YAML with dates", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.parse", () => {
|
||||
globalThis.result = Bun.YAML.parse(datesYaml);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.load", () => {
|
||||
globalThis.result = jsYaml.load(datesYaml);
|
||||
});
|
||||
|
||||
bench("yaml.parse", () => {
|
||||
globalThis.result = yaml.parse(datesYaml);
|
||||
});
|
||||
});
|
||||
|
||||
// // Stringify benchmarks
|
||||
// const smallObjJs = jsYaml.load(smallYaml);
|
||||
// const mediumObjJs = jsYaml.load(mediumYaml);
|
||||
// const largeObjJs = jsYaml.load(largeYaml);
|
||||
|
||||
// group("stringify small object", () => {
|
||||
// bench("js-yaml.dump", () => {
|
||||
// globalThis.result = jsYaml.dump(smallObjJs);
|
||||
// });
|
||||
// });
|
||||
|
||||
// group("stringify medium object", () => {
|
||||
// bench("js-yaml.dump", () => {
|
||||
// globalThis.result = jsYaml.dump(mediumObjJs);
|
||||
// });
|
||||
// });
|
||||
|
||||
// group("stringify large object", () => {
|
||||
// bench("js-yaml.dump", () => {
|
||||
// globalThis.result = jsYaml.dump(largeObjJs);
|
||||
// });
|
||||
// });
|
||||
|
||||
await run();
|
||||
407
bench/yaml/yaml-stringify.mjs
Normal file
407
bench/yaml/yaml-stringify.mjs
Normal file
@@ -0,0 +1,407 @@
|
||||
import { bench, group, run } from "../runner.mjs";
|
||||
import jsYaml from "js-yaml";
|
||||
import yaml from "yaml";
|
||||
|
||||
// Small object
|
||||
const smallObject = {
|
||||
name: "John Doe",
|
||||
age: 30,
|
||||
email: "john@example.com",
|
||||
active: true,
|
||||
};
|
||||
|
||||
// Medium object with nested structures
|
||||
const mediumObject = {
|
||||
company: "Acme Corp",
|
||||
employees: [
|
||||
{
|
||||
name: "John Doe",
|
||||
age: 30,
|
||||
position: "Developer",
|
||||
skills: ["JavaScript", "TypeScript", "Node.js"],
|
||||
},
|
||||
{
|
||||
name: "Jane Smith",
|
||||
age: 28,
|
||||
position: "Designer",
|
||||
skills: ["Figma", "Photoshop", "Illustrator"],
|
||||
},
|
||||
{
|
||||
name: "Bob Johnson",
|
||||
age: 35,
|
||||
position: "Manager",
|
||||
skills: ["Leadership", "Communication", "Planning"],
|
||||
},
|
||||
],
|
||||
settings: {
|
||||
database: {
|
||||
host: "localhost",
|
||||
port: 5432,
|
||||
name: "mydb",
|
||||
},
|
||||
cache: {
|
||||
enabled: true,
|
||||
ttl: 3600,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
// Large object with complex structures
|
||||
const largeObject = {
|
||||
apiVersion: "apps/v1",
|
||||
kind: "Deployment",
|
||||
metadata: {
|
||||
name: "nginx-deployment",
|
||||
labels: {
|
||||
app: "nginx",
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
replicas: 3,
|
||||
selector: {
|
||||
matchLabels: {
|
||||
app: "nginx",
|
||||
},
|
||||
},
|
||||
template: {
|
||||
metadata: {
|
||||
labels: {
|
||||
app: "nginx",
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
containers: [
|
||||
{
|
||||
name: "nginx",
|
||||
image: "nginx:1.14.2",
|
||||
ports: [
|
||||
{
|
||||
containerPort: 80,
|
||||
},
|
||||
],
|
||||
env: [
|
||||
{
|
||||
name: "ENV_VAR_1",
|
||||
value: "value1",
|
||||
},
|
||||
{
|
||||
name: "ENV_VAR_2",
|
||||
value: "value2",
|
||||
},
|
||||
],
|
||||
volumeMounts: [
|
||||
{
|
||||
name: "config",
|
||||
mountPath: "/etc/nginx",
|
||||
},
|
||||
],
|
||||
resources: {
|
||||
limits: {
|
||||
cpu: "1",
|
||||
memory: "1Gi",
|
||||
},
|
||||
requests: {
|
||||
cpu: "0.5",
|
||||
memory: "512Mi",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
volumes: [
|
||||
{
|
||||
name: "config",
|
||||
configMap: {
|
||||
name: "nginx-config",
|
||||
items: [
|
||||
{
|
||||
key: "nginx.conf",
|
||||
path: "nginx.conf",
|
||||
},
|
||||
{
|
||||
key: "mime.types",
|
||||
path: "mime.types",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
nodeSelector: {
|
||||
disktype: "ssd",
|
||||
},
|
||||
tolerations: [
|
||||
{
|
||||
key: "key1",
|
||||
operator: "Equal",
|
||||
value: "value1",
|
||||
effect: "NoSchedule",
|
||||
},
|
||||
{
|
||||
key: "key2",
|
||||
operator: "Exists",
|
||||
effect: "NoExecute",
|
||||
},
|
||||
],
|
||||
affinity: {
|
||||
nodeAffinity: {
|
||||
requiredDuringSchedulingIgnoredDuringExecution: {
|
||||
nodeSelectorTerms: [
|
||||
{
|
||||
matchExpressions: [
|
||||
{
|
||||
key: "kubernetes.io/e2e-az-name",
|
||||
operator: "In",
|
||||
values: ["e2e-az1", "e2e-az2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
podAntiAffinity: {
|
||||
preferredDuringSchedulingIgnoredDuringExecution: [
|
||||
{
|
||||
weight: 100,
|
||||
podAffinityTerm: {
|
||||
labelSelector: {
|
||||
matchExpressions: [
|
||||
{
|
||||
key: "app",
|
||||
operator: "In",
|
||||
values: ["web-store"],
|
||||
},
|
||||
],
|
||||
},
|
||||
topologyKey: "kubernetes.io/hostname",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
// Object with anchors and references (after resolution)
|
||||
const objectWithAnchors = {
|
||||
defaults: {
|
||||
adapter: "postgresql",
|
||||
host: "localhost",
|
||||
port: 5432,
|
||||
},
|
||||
development: {
|
||||
adapter: "postgresql",
|
||||
host: "localhost",
|
||||
port: 5432,
|
||||
database: "dev_db",
|
||||
},
|
||||
test: {
|
||||
adapter: "postgresql",
|
||||
host: "localhost",
|
||||
port: 5432,
|
||||
database: "test_db",
|
||||
},
|
||||
production: {
|
||||
adapter: "postgresql",
|
||||
host: "prod.example.com",
|
||||
port: 5432,
|
||||
database: "prod_db",
|
||||
},
|
||||
};
|
||||
|
||||
// Array of items
|
||||
const arrayObject = [
|
||||
{
|
||||
id: 1,
|
||||
name: "Item 1",
|
||||
price: 10.99,
|
||||
tags: ["electronics", "gadgets"],
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: "Item 2",
|
||||
price: 25.5,
|
||||
tags: ["books", "education"],
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: "Item 3",
|
||||
price: 5.0,
|
||||
tags: ["food", "snacks"],
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
name: "Item 4",
|
||||
price: 100.0,
|
||||
tags: ["electronics", "computers"],
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
name: "Item 5",
|
||||
price: 15.75,
|
||||
tags: ["clothing", "accessories"],
|
||||
},
|
||||
];
|
||||
|
||||
// Multiline strings
|
||||
const multilineObject = {
|
||||
description:
|
||||
"This is a multiline string\nthat preserves line breaks\nand indentation.\n\nIt can contain multiple paragraphs\nand special characters: !@#$%^&*()\n",
|
||||
folded: "This is a folded string where line breaks are converted to spaces unless there are\nempty lines like above.",
|
||||
plain: "This is a plain string",
|
||||
quoted: 'This is a quoted string with "escapes"',
|
||||
literal: "This is a literal string with 'quotes'",
|
||||
};
|
||||
|
||||
// Numbers and special values
|
||||
const numbersObject = {
|
||||
integer: 42,
|
||||
negative: -17,
|
||||
float: 3.14159,
|
||||
scientific: 0.000123,
|
||||
infinity: Infinity,
|
||||
negativeInfinity: -Infinity,
|
||||
notANumber: NaN,
|
||||
octal: 493, // 0o755
|
||||
hex: 255, // 0xFF
|
||||
binary: 10, // 0b1010
|
||||
};
|
||||
|
||||
// Dates and timestamps
|
||||
const datesObject = {
|
||||
date: new Date("2024-01-15"),
|
||||
datetime: new Date("2024-01-15T10:30:00Z"),
|
||||
timestamp: new Date("2024-01-15T15:30:00.123456789Z"), // Adjusted for UTC-5
|
||||
canonical: new Date("2024-01-15T10:30:00.123456789Z"),
|
||||
};
|
||||
|
||||
// Stringify benchmarks
|
||||
group("stringify small object", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.stringify", () => {
|
||||
return Bun.YAML.stringify(smallObject);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.dump", () => {
|
||||
return jsYaml.dump(smallObject);
|
||||
});
|
||||
|
||||
bench("yaml.stringify", () => {
|
||||
return yaml.stringify(smallObject);
|
||||
});
|
||||
});
|
||||
|
||||
group("stringify medium object", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.stringify", () => {
|
||||
return Bun.YAML.stringify(mediumObject);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.dump", () => {
|
||||
return jsYaml.dump(mediumObject);
|
||||
});
|
||||
|
||||
bench("yaml.stringify", () => {
|
||||
return yaml.stringify(mediumObject);
|
||||
});
|
||||
});
|
||||
|
||||
group("stringify large object", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.stringify", () => {
|
||||
return Bun.YAML.stringify(largeObject);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.dump", () => {
|
||||
return jsYaml.dump(largeObject);
|
||||
});
|
||||
|
||||
bench("yaml.stringify", () => {
|
||||
return yaml.stringify(largeObject);
|
||||
});
|
||||
});
|
||||
|
||||
group("stringify object with anchors", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.stringify", () => {
|
||||
return Bun.YAML.stringify(objectWithAnchors);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.dump", () => {
|
||||
return jsYaml.dump(objectWithAnchors);
|
||||
});
|
||||
|
||||
bench("yaml.stringify", () => {
|
||||
return yaml.stringify(objectWithAnchors);
|
||||
});
|
||||
});
|
||||
|
||||
group("stringify array", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.stringify", () => {
|
||||
return Bun.YAML.stringify(arrayObject);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.dump", () => {
|
||||
return jsYaml.dump(arrayObject);
|
||||
});
|
||||
|
||||
bench("yaml.stringify", () => {
|
||||
return yaml.stringify(arrayObject);
|
||||
});
|
||||
});
|
||||
|
||||
group("stringify object with multiline strings", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.stringify", () => {
|
||||
return Bun.YAML.stringify(multilineObject);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.dump", () => {
|
||||
return jsYaml.dump(multilineObject);
|
||||
});
|
||||
|
||||
bench("yaml.stringify", () => {
|
||||
return yaml.stringify(multilineObject);
|
||||
});
|
||||
});
|
||||
|
||||
group("stringify object with numbers", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.stringify", () => {
|
||||
return Bun.YAML.stringify(numbersObject);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.dump", () => {
|
||||
return jsYaml.dump(numbersObject);
|
||||
});
|
||||
|
||||
bench("yaml.stringify", () => {
|
||||
return yaml.stringify(numbersObject);
|
||||
});
|
||||
});
|
||||
|
||||
group("stringify object with dates", () => {
|
||||
if (typeof Bun !== "undefined" && Bun.YAML) {
|
||||
bench("Bun.YAML.stringify", () => {
|
||||
return Bun.YAML.stringify(datesObject);
|
||||
});
|
||||
}
|
||||
|
||||
bench("js-yaml.dump", () => {
|
||||
return jsYaml.dump(datesObject);
|
||||
});
|
||||
|
||||
bench("yaml.stringify", () => {
|
||||
return yaml.stringify(datesObject);
|
||||
});
|
||||
});
|
||||
|
||||
await run();
|
||||
2
bun.lock
2
bun.lock
@@ -40,8 +40,8 @@
|
||||
},
|
||||
},
|
||||
"overrides": {
|
||||
"bun-types": "workspace:packages/bun-types",
|
||||
"@types/bun": "workspace:packages/@types/bun",
|
||||
"bun-types": "workspace:packages/bun-types",
|
||||
},
|
||||
"packages": {
|
||||
"@esbuild/aix-ppc64": ["@esbuild/aix-ppc64@0.21.5", "", { "os": "aix", "cpu": "ppc64" }, "sha512-1SDgH6ZSPTlggy1yI6+Dbkiz8xzpHJEVAlF/AM1tHPLsf5STom9rwtjE4hKAF20FfXXNTFqEYXyJNWh1GiZedQ=="],
|
||||
|
||||
@@ -57,6 +57,23 @@ else()
|
||||
message(FATAL_ERROR "Unsupported architecture: ${CMAKE_SYSTEM_PROCESSOR}")
|
||||
endif()
|
||||
|
||||
# Windows Code Signing Option
|
||||
if(WIN32)
|
||||
optionx(ENABLE_WINDOWS_CODESIGNING BOOL "Enable Windows code signing with DigiCert KeyLocker" DEFAULT OFF)
|
||||
|
||||
if(ENABLE_WINDOWS_CODESIGNING)
|
||||
message(STATUS "Windows code signing: ENABLED")
|
||||
|
||||
# Check for required environment variables
|
||||
if(NOT DEFINED ENV{SM_API_KEY})
|
||||
message(WARNING "SM_API_KEY not set - code signing may fail")
|
||||
endif()
|
||||
if(NOT DEFINED ENV{SM_CLIENT_CERT_FILE})
|
||||
message(WARNING "SM_CLIENT_CERT_FILE not set - code signing may fail")
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(LINUX)
|
||||
if(EXISTS "/etc/alpine-release")
|
||||
set(DEFAULT_ABI "musl")
|
||||
|
||||
@@ -87,6 +87,7 @@ src/bun.js/bindings/JSNodePerformanceHooksHistogramConstructor.cpp
|
||||
src/bun.js/bindings/JSNodePerformanceHooksHistogramPrototype.cpp
|
||||
src/bun.js/bindings/JSPropertyIterator.cpp
|
||||
src/bun.js/bindings/JSS3File.cpp
|
||||
src/bun.js/bindings/JSSecrets.cpp
|
||||
src/bun.js/bindings/JSSocketAddressDTO.cpp
|
||||
src/bun.js/bindings/JSStringDecoder.cpp
|
||||
src/bun.js/bindings/JSWrappingFunction.cpp
|
||||
@@ -94,6 +95,7 @@ src/bun.js/bindings/JSX509Certificate.cpp
|
||||
src/bun.js/bindings/JSX509CertificateConstructor.cpp
|
||||
src/bun.js/bindings/JSX509CertificatePrototype.cpp
|
||||
src/bun.js/bindings/linux_perf_tracing.cpp
|
||||
src/bun.js/bindings/MarkedArgumentBufferBinding.cpp
|
||||
src/bun.js/bindings/MarkingConstraint.cpp
|
||||
src/bun.js/bindings/ModuleLoader.cpp
|
||||
src/bun.js/bindings/napi_external.cpp
|
||||
@@ -188,11 +190,22 @@ src/bun.js/bindings/ProcessIdentifier.cpp
|
||||
src/bun.js/bindings/RegularExpression.cpp
|
||||
src/bun.js/bindings/S3Error.cpp
|
||||
src/bun.js/bindings/ScriptExecutionContext.cpp
|
||||
src/bun.js/bindings/SecretsDarwin.cpp
|
||||
src/bun.js/bindings/SecretsLinux.cpp
|
||||
src/bun.js/bindings/SecretsWindows.cpp
|
||||
src/bun.js/bindings/Serialization.cpp
|
||||
src/bun.js/bindings/ServerRouteList.cpp
|
||||
src/bun.js/bindings/spawn.cpp
|
||||
src/bun.js/bindings/SQLClient.cpp
|
||||
src/bun.js/bindings/sqlite/JSNodeSQLiteDatabaseSync.cpp
|
||||
src/bun.js/bindings/sqlite/JSNodeSQLiteDatabaseSyncConstructor.cpp
|
||||
src/bun.js/bindings/sqlite/JSNodeSQLiteDatabaseSyncPrototype.cpp
|
||||
src/bun.js/bindings/sqlite/JSNodeSQLiteStatementSync.cpp
|
||||
src/bun.js/bindings/sqlite/JSNodeSQLiteStatementSyncConstructor.cpp
|
||||
src/bun.js/bindings/sqlite/JSNodeSQLiteStatementSyncPrototype.cpp
|
||||
src/bun.js/bindings/sqlite/JSSQLStatement.cpp
|
||||
src/bun.js/bindings/sqlite/sqlite_init.cpp
|
||||
src/bun.js/bindings/StringBuilderBinding.cpp
|
||||
src/bun.js/bindings/stripANSI.cpp
|
||||
src/bun.js/bindings/Strong.cpp
|
||||
src/bun.js/bindings/TextCodec.cpp
|
||||
@@ -491,6 +504,7 @@ src/bun.js/bindings/ZigGeneratedCode.cpp
|
||||
src/bun.js/bindings/ZigGlobalObject.cpp
|
||||
src/bun.js/bindings/ZigSourceProvider.cpp
|
||||
src/bun.js/modules/NodeModuleModule.cpp
|
||||
src/bun.js/modules/NodeSQLiteModule.cpp
|
||||
src/bun.js/modules/NodeTTYModule.cpp
|
||||
src/bun.js/modules/NodeUtilTypesModule.cpp
|
||||
src/bun.js/modules/ObjectModule.cpp
|
||||
|
||||
@@ -66,11 +66,11 @@ src/js/internal/primordials.js
|
||||
src/js/internal/promisify.ts
|
||||
src/js/internal/shared.ts
|
||||
src/js/internal/sql/errors.ts
|
||||
src/js/internal/sql/mysql.ts
|
||||
src/js/internal/sql/postgres.ts
|
||||
src/js/internal/sql/query.ts
|
||||
src/js/internal/sql/shared.ts
|
||||
src/js/internal/sql/sqlite.ts
|
||||
src/js/internal/sql/utils.ts
|
||||
src/js/internal/stream.promises.ts
|
||||
src/js/internal/stream.ts
|
||||
src/js/internal/streams/add-abort-signal.ts
|
||||
@@ -97,6 +97,7 @@ src/js/internal/tls.ts
|
||||
src/js/internal/tty.ts
|
||||
src/js/internal/url.ts
|
||||
src/js/internal/util/colors.ts
|
||||
src/js/internal/util/deprecate.ts
|
||||
src/js/internal/util/inspect.d.ts
|
||||
src/js/internal/util/inspect.js
|
||||
src/js/internal/util/mime.ts
|
||||
|
||||
@@ -6,7 +6,6 @@ src/bun.js/api/Glob.classes.ts
|
||||
src/bun.js/api/h2.classes.ts
|
||||
src/bun.js/api/html_rewriter.classes.ts
|
||||
src/bun.js/api/JSBundler.classes.ts
|
||||
src/bun.js/api/postgres.classes.ts
|
||||
src/bun.js/api/ResumableSink.classes.ts
|
||||
src/bun.js/api/S3Client.classes.ts
|
||||
src/bun.js/api/S3Stat.classes.ts
|
||||
@@ -15,6 +14,7 @@ src/bun.js/api/Shell.classes.ts
|
||||
src/bun.js/api/ShellArgs.classes.ts
|
||||
src/bun.js/api/sockets.classes.ts
|
||||
src/bun.js/api/sourcemap.classes.ts
|
||||
src/bun.js/api/sql.classes.ts
|
||||
src/bun.js/api/streams.classes.ts
|
||||
src/bun.js/api/valkey.classes.ts
|
||||
src/bun.js/api/zlib.classes.ts
|
||||
|
||||
@@ -64,6 +64,7 @@ src/async/windows_event_loop.zig
|
||||
src/bake.zig
|
||||
src/bake/DevServer.zig
|
||||
src/bake/DevServer/Assets.zig
|
||||
src/bake/DevServer/DevAllocator.zig
|
||||
src/bake/DevServer/DirectoryWatchStore.zig
|
||||
src/bake/DevServer/ErrorReportRequest.zig
|
||||
src/bake/DevServer/HmrSocket.zig
|
||||
@@ -143,6 +144,7 @@ src/bun.js/api/Timer/TimerObjectInternals.zig
|
||||
src/bun.js/api/Timer/WTFTimer.zig
|
||||
src/bun.js/api/TOMLObject.zig
|
||||
src/bun.js/api/UnsafeObject.zig
|
||||
src/bun.js/api/YAMLObject.zig
|
||||
src/bun.js/bindgen_test.zig
|
||||
src/bun.js/bindings/AbortSignal.zig
|
||||
src/bun.js/bindings/AnyPromise.zig
|
||||
@@ -184,10 +186,12 @@ src/bun.js/bindings/JSPromiseRejectionOperation.zig
|
||||
src/bun.js/bindings/JSPropertyIterator.zig
|
||||
src/bun.js/bindings/JSRef.zig
|
||||
src/bun.js/bindings/JSRuntimeType.zig
|
||||
src/bun.js/bindings/JSSecrets.zig
|
||||
src/bun.js/bindings/JSString.zig
|
||||
src/bun.js/bindings/JSType.zig
|
||||
src/bun.js/bindings/JSUint8Array.zig
|
||||
src/bun.js/bindings/JSValue.zig
|
||||
src/bun.js/bindings/MarkedArgumentBuffer.zig
|
||||
src/bun.js/bindings/NodeModuleModule.zig
|
||||
src/bun.js/bindings/RegularExpression.zig
|
||||
src/bun.js/bindings/ResolvedSource.zig
|
||||
@@ -196,6 +200,7 @@ src/bun.js/bindings/sizes.zig
|
||||
src/bun.js/bindings/SourceProvider.zig
|
||||
src/bun.js/bindings/SourceType.zig
|
||||
src/bun.js/bindings/static_export.zig
|
||||
src/bun.js/bindings/StringBuilder.zig
|
||||
src/bun.js/bindings/SystemError.zig
|
||||
src/bun.js/bindings/TextCodec.zig
|
||||
src/bun.js/bindings/URL.zig
|
||||
@@ -412,6 +417,7 @@ src/bundler/DeferredBatchTask.zig
|
||||
src/bundler/entry_points.zig
|
||||
src/bundler/Graph.zig
|
||||
src/bundler/HTMLImportManifest.zig
|
||||
src/bundler/IndexStringMap.zig
|
||||
src/bundler/linker_context/computeChunks.zig
|
||||
src/bundler/linker_context/computeCrossChunkDependencies.zig
|
||||
src/bundler/linker_context/convertStmtsForChunk.zig
|
||||
@@ -438,6 +444,7 @@ src/bundler/linker_context/writeOutputFilesToDisk.zig
|
||||
src/bundler/LinkerContext.zig
|
||||
src/bundler/LinkerGraph.zig
|
||||
src/bundler/ParseTask.zig
|
||||
src/bundler/PathToSourceIndexMap.zig
|
||||
src/bundler/ServerComponentParseTask.zig
|
||||
src/bundler/ThreadPool.zig
|
||||
src/bunfig.zig
|
||||
@@ -486,6 +493,7 @@ src/codegen/process_windows_translate_c.zig
|
||||
src/collections.zig
|
||||
src/collections/baby_list.zig
|
||||
src/collections/bit_set.zig
|
||||
src/collections/BoundedArray.zig
|
||||
src/collections/hive_array.zig
|
||||
src/collections/multi_array_list.zig
|
||||
src/compile_target.zig
|
||||
@@ -494,7 +502,6 @@ src/copy_file.zig
|
||||
src/crash_handler.zig
|
||||
src/create/SourceFileProjectGenerator.zig
|
||||
src/csrf.zig
|
||||
src/css_scanner.zig
|
||||
src/css/compat.zig
|
||||
src/css/context.zig
|
||||
src/css/css_internals.zig
|
||||
@@ -646,6 +653,7 @@ src/glob.zig
|
||||
src/glob/GlobWalker.zig
|
||||
src/glob/match.zig
|
||||
src/Global.zig
|
||||
src/handle_oom.zig
|
||||
src/heap_breakdown.zig
|
||||
src/highway.zig
|
||||
src/hmac.zig
|
||||
@@ -659,6 +667,7 @@ src/http/ETag.zig
|
||||
src/http/FetchRedirect.zig
|
||||
src/http/HeaderBuilder.zig
|
||||
src/http/Headers.zig
|
||||
src/http/HeaderValueIterator.zig
|
||||
src/http/HTTPCertError.zig
|
||||
src/http/HTTPContext.zig
|
||||
src/http/HTTPRequestBody.zig
|
||||
@@ -730,6 +739,7 @@ src/install/PackageManager/patchPackage.zig
|
||||
src/install/PackageManager/processDependencyList.zig
|
||||
src/install/PackageManager/ProgressStrings.zig
|
||||
src/install/PackageManager/runTasks.zig
|
||||
src/install/PackageManager/security_scanner.zig
|
||||
src/install/PackageManager/updatePackageJSONAndInstall.zig
|
||||
src/install/PackageManager/UpdateRequest.zig
|
||||
src/install/PackageManager/WorkspacePackageJSONCache.zig
|
||||
@@ -748,6 +758,7 @@ src/interchange.zig
|
||||
src/interchange/json.zig
|
||||
src/interchange/toml.zig
|
||||
src/interchange/toml/lexer.zig
|
||||
src/interchange/yaml.zig
|
||||
src/io/heap.zig
|
||||
src/io/io.zig
|
||||
src/io/MaxBuf.zig
|
||||
@@ -794,7 +805,9 @@ src/ptr/CowSlice.zig
|
||||
src/ptr/meta.zig
|
||||
src/ptr/owned.zig
|
||||
src/ptr/owned/maybe.zig
|
||||
src/ptr/owned/scoped.zig
|
||||
src/ptr/ref_count.zig
|
||||
src/ptr/shared.zig
|
||||
src/ptr/tagged_pointer.zig
|
||||
src/ptr/weak_ptr.zig
|
||||
src/renamer.zig
|
||||
@@ -882,30 +895,63 @@ src/sourcemap/JSSourceMap.zig
|
||||
src/sourcemap/LineOffsetTable.zig
|
||||
src/sourcemap/sourcemap.zig
|
||||
src/sourcemap/VLQ.zig
|
||||
src/sql/mysql.zig
|
||||
src/sql/mysql/AuthMethod.zig
|
||||
src/sql/mysql/Capabilities.zig
|
||||
src/sql/mysql/ConnectionState.zig
|
||||
src/sql/mysql/MySQLConnection.zig
|
||||
src/sql/mysql/MySQLContext.zig
|
||||
src/sql/mysql/MySQLQuery.zig
|
||||
src/sql/mysql/MySQLRequest.zig
|
||||
src/sql/mysql/MySQLStatement.zig
|
||||
src/sql/mysql/MySQLTypes.zig
|
||||
src/sql/mysql/protocol/AnyMySQLError.zig
|
||||
src/sql/mysql/protocol/Auth.zig
|
||||
src/sql/mysql/protocol/AuthSwitchRequest.zig
|
||||
src/sql/mysql/protocol/AuthSwitchResponse.zig
|
||||
src/sql/mysql/protocol/CharacterSet.zig
|
||||
src/sql/mysql/protocol/ColumnDefinition41.zig
|
||||
src/sql/mysql/protocol/CommandType.zig
|
||||
src/sql/mysql/protocol/DecodeBinaryValue.zig
|
||||
src/sql/mysql/protocol/EncodeInt.zig
|
||||
src/sql/mysql/protocol/EOFPacket.zig
|
||||
src/sql/mysql/protocol/ErrorPacket.zig
|
||||
src/sql/mysql/protocol/HandshakeResponse41.zig
|
||||
src/sql/mysql/protocol/HandshakeV10.zig
|
||||
src/sql/mysql/protocol/LocalInfileRequest.zig
|
||||
src/sql/mysql/protocol/NewReader.zig
|
||||
src/sql/mysql/protocol/NewWriter.zig
|
||||
src/sql/mysql/protocol/OKPacket.zig
|
||||
src/sql/mysql/protocol/PacketHeader.zig
|
||||
src/sql/mysql/protocol/PacketType.zig
|
||||
src/sql/mysql/protocol/PreparedStatement.zig
|
||||
src/sql/mysql/protocol/Query.zig
|
||||
src/sql/mysql/protocol/ResultSet.zig
|
||||
src/sql/mysql/protocol/ResultSetHeader.zig
|
||||
src/sql/mysql/protocol/Signature.zig
|
||||
src/sql/mysql/protocol/StackReader.zig
|
||||
src/sql/mysql/protocol/StmtPrepareOKPacket.zig
|
||||
src/sql/mysql/SSLMode.zig
|
||||
src/sql/mysql/StatusFlags.zig
|
||||
src/sql/mysql/TLSStatus.zig
|
||||
src/sql/postgres.zig
|
||||
src/sql/postgres/AnyPostgresError.zig
|
||||
src/sql/postgres/AuthenticationState.zig
|
||||
src/sql/postgres/CommandTag.zig
|
||||
src/sql/postgres/ConnectionFlags.zig
|
||||
src/sql/postgres/Data.zig
|
||||
src/sql/postgres/DataCell.zig
|
||||
src/sql/postgres/DebugSocketMonitorReader.zig
|
||||
src/sql/postgres/DebugSocketMonitorWriter.zig
|
||||
src/sql/postgres/ObjectIterator.zig
|
||||
src/sql/postgres/PostgresCachedStructure.zig
|
||||
src/sql/postgres/PostgresProtocol.zig
|
||||
src/sql/postgres/PostgresRequest.zig
|
||||
src/sql/postgres/PostgresSQLConnection.zig
|
||||
src/sql/postgres/PostgresSQLContext.zig
|
||||
src/sql/postgres/PostgresSQLQuery.zig
|
||||
src/sql/postgres/PostgresSQLQueryResultMode.zig
|
||||
src/sql/postgres/PostgresSQLStatement.zig
|
||||
src/sql/postgres/PostgresTypes.zig
|
||||
src/sql/postgres/protocol/ArrayList.zig
|
||||
src/sql/postgres/protocol/Authentication.zig
|
||||
src/sql/postgres/protocol/BackendKeyData.zig
|
||||
src/sql/postgres/protocol/Close.zig
|
||||
src/sql/postgres/protocol/ColumnIdentifier.zig
|
||||
src/sql/postgres/protocol/CommandComplete.zig
|
||||
src/sql/postgres/protocol/CopyData.zig
|
||||
src/sql/postgres/protocol/CopyFail.zig
|
||||
@@ -938,7 +984,6 @@ src/sql/postgres/protocol/StartupMessage.zig
|
||||
src/sql/postgres/protocol/TransactionStatusIndicator.zig
|
||||
src/sql/postgres/protocol/WriteWrap.zig
|
||||
src/sql/postgres/protocol/zHelpers.zig
|
||||
src/sql/postgres/QueryBindingIterator.zig
|
||||
src/sql/postgres/SASL.zig
|
||||
src/sql/postgres/Signature.zig
|
||||
src/sql/postgres/SocketMonitor.zig
|
||||
@@ -953,6 +998,14 @@ src/sql/postgres/types/json.zig
|
||||
src/sql/postgres/types/numeric.zig
|
||||
src/sql/postgres/types/PostgresString.zig
|
||||
src/sql/postgres/types/Tag.zig
|
||||
src/sql/shared/CachedStructure.zig
|
||||
src/sql/shared/ColumnIdentifier.zig
|
||||
src/sql/shared/ConnectionFlags.zig
|
||||
src/sql/shared/Data.zig
|
||||
src/sql/shared/ObjectIterator.zig
|
||||
src/sql/shared/QueryBindingIterator.zig
|
||||
src/sql/shared/SQLDataCell.zig
|
||||
src/sql/shared/SQLQueryResultMode.zig
|
||||
src/StandaloneModuleGraph.zig
|
||||
src/StaticHashMap.zig
|
||||
src/string.zig
|
||||
|
||||
@@ -1205,6 +1205,7 @@ if(NOT BUN_CPP_ONLY)
|
||||
endif()
|
||||
|
||||
if(bunStrip)
|
||||
# First, strip bun-profile.exe to create bun.exe
|
||||
register_command(
|
||||
TARGET
|
||||
${bun}
|
||||
@@ -1225,6 +1226,48 @@ if(NOT BUN_CPP_ONLY)
|
||||
OUTPUTS
|
||||
${BUILD_PATH}/${bunStripExe}
|
||||
)
|
||||
|
||||
# Then sign both executables on Windows
|
||||
if(WIN32 AND ENABLE_WINDOWS_CODESIGNING)
|
||||
set(SIGN_SCRIPT "${CMAKE_SOURCE_DIR}/.buildkite/scripts/sign-windows.ps1")
|
||||
|
||||
# Verify signing script exists
|
||||
if(NOT EXISTS "${SIGN_SCRIPT}")
|
||||
message(FATAL_ERROR "Windows signing script not found: ${SIGN_SCRIPT}")
|
||||
endif()
|
||||
|
||||
# Use PowerShell for Windows code signing (native Windows, no path issues)
|
||||
find_program(POWERSHELL_EXECUTABLE
|
||||
NAMES pwsh.exe powershell.exe
|
||||
PATHS
|
||||
"C:/Program Files/PowerShell/7"
|
||||
"C:/Program Files (x86)/PowerShell/7"
|
||||
"C:/Windows/System32/WindowsPowerShell/v1.0"
|
||||
DOC "Path to PowerShell executable"
|
||||
)
|
||||
|
||||
if(NOT POWERSHELL_EXECUTABLE)
|
||||
set(POWERSHELL_EXECUTABLE "powershell.exe")
|
||||
endif()
|
||||
|
||||
message(STATUS "Using PowerShell executable: ${POWERSHELL_EXECUTABLE}")
|
||||
|
||||
# Sign both bun-profile.exe and bun.exe after stripping
|
||||
register_command(
|
||||
TARGET
|
||||
${bun}
|
||||
TARGET_PHASE
|
||||
POST_BUILD
|
||||
COMMENT
|
||||
"Code signing bun-profile.exe and bun.exe with DigiCert KeyLocker"
|
||||
COMMAND
|
||||
"${POWERSHELL_EXECUTABLE}" "-NoProfile" "-ExecutionPolicy" "Bypass" "-File" "${SIGN_SCRIPT}" "-BunProfileExe" "${BUILD_PATH}/${bunExe}" "-BunExe" "${BUILD_PATH}/${bunStripExe}"
|
||||
CWD
|
||||
${CMAKE_SOURCE_DIR}
|
||||
SOURCES
|
||||
${BUILD_PATH}/${bunStripExe}
|
||||
)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# somehow on some Linux systems we need to disable ASLR for ASAN-instrumented binaries to run
|
||||
|
||||
@@ -4,7 +4,7 @@ register_repository(
|
||||
REPOSITORY
|
||||
oven-sh/mimalloc
|
||||
COMMIT
|
||||
c90e3981edcf6d75c8243e3d323e4300d700ebc6
|
||||
1beadf9651a7bfdec6b5367c380ecc3fe1c40d1a
|
||||
)
|
||||
|
||||
set(MIMALLOC_CMAKE_ARGS
|
||||
@@ -14,7 +14,7 @@ set(MIMALLOC_CMAKE_ARGS
|
||||
-DMI_BUILD_TESTS=OFF
|
||||
-DMI_USE_CXX=ON
|
||||
-DMI_SKIP_COLLECT_ON_EXIT=ON
|
||||
|
||||
|
||||
# ```
|
||||
# ❯ mimalloc_allow_large_os_pages=0 BUN_PORT=3004 mem bun http-hello.js
|
||||
# Started development server: http://localhost:3004
|
||||
@@ -51,7 +51,7 @@ if(ENABLE_ASAN)
|
||||
list(APPEND MIMALLOC_CMAKE_ARGS -DMI_DEBUG_UBSAN=ON)
|
||||
elseif(APPLE OR LINUX)
|
||||
if(APPLE)
|
||||
list(APPEND MIMALLOC_CMAKE_ARGS -DMI_OVERRIDE=OFF)
|
||||
list(APPEND MIMALLOC_CMAKE_ARGS -DMI_OVERRIDE=OFF)
|
||||
list(APPEND MIMALLOC_CMAKE_ARGS -DMI_OSX_ZONE=OFF)
|
||||
list(APPEND MIMALLOC_CMAKE_ARGS -DMI_OSX_INTERPOSE=OFF)
|
||||
else()
|
||||
|
||||
@@ -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 53385bda2d2270223ac66f7b021a4aec3dd6df75)
|
||||
set(WEBKIT_VERSION f474428677de1fafaf13bb3b9a050fe3504dda25)
|
||||
endif()
|
||||
|
||||
string(SUBSTRING ${WEBKIT_VERSION} 0 16 WEBKIT_VERSION_PREFIX)
|
||||
|
||||
@@ -20,7 +20,7 @@ else()
|
||||
unsupported(CMAKE_SYSTEM_NAME)
|
||||
endif()
|
||||
|
||||
set(ZIG_COMMIT "edc6229b1fafb1701a25fb4e17114cc756991546")
|
||||
set(ZIG_COMMIT "e0b7c318f318196c5f81fdf3423816a7b5bb3112")
|
||||
optionx(ZIG_TARGET STRING "The zig target to use" DEFAULT ${DEFAULT_ZIG_TARGET})
|
||||
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||
|
||||
319
docs/api/secrets.md
Normal file
319
docs/api/secrets.md
Normal file
@@ -0,0 +1,319 @@
|
||||
Store and retrieve sensitive credentials securely using the operating system's native credential storage APIs.
|
||||
|
||||
**Experimental:** This API is new and experimental. It may change in the future.
|
||||
|
||||
```typescript
|
||||
import { secrets } from "bun";
|
||||
|
||||
const githubToken = await secrets.get({
|
||||
service: "my-cli-tool",
|
||||
name: "github-token",
|
||||
});
|
||||
|
||||
if (!githubToken) {
|
||||
const response = await fetch("https://api.github.com/name", {
|
||||
headers: { "Authorization": `token ${githubToken}` },
|
||||
});
|
||||
console.log("Please enter your GitHub token");
|
||||
} else {
|
||||
await secrets.set({
|
||||
service: "my-cli-tool",
|
||||
name: "github-token",
|
||||
value: prompt("Please enter your GitHub token"),
|
||||
});
|
||||
console.log("GitHub token stored");
|
||||
}
|
||||
```
|
||||
|
||||
## Overview
|
||||
|
||||
`Bun.secrets` provides a cross-platform API for managing sensitive credentials that CLI tools and development applications typically store in plaintext files like `~/.npmrc`, `~/.aws/credentials`, or `.env` files. It uses:
|
||||
|
||||
- **macOS**: Keychain Services
|
||||
- **Linux**: libsecret (GNOME Keyring, KWallet, etc.)
|
||||
- **Windows**: Windows Credential Manager
|
||||
|
||||
All operations are asynchronous and non-blocking, running on Bun's threadpool.
|
||||
|
||||
Note: in the future, we may add an additional `provider` option to make this better for production deployment secrets, but today this API is mostly useful for local development tools.
|
||||
|
||||
## API
|
||||
|
||||
### `Bun.secrets.get(options)`
|
||||
|
||||
Retrieve a stored credential.
|
||||
|
||||
```typescript
|
||||
import { secrets } from "bun";
|
||||
|
||||
const password = await Bun.secrets.get({
|
||||
service: "my-app",
|
||||
name: "alice@example.com",
|
||||
});
|
||||
// Returns: string | null
|
||||
|
||||
// Or if you prefer without an object
|
||||
const password = await Bun.secrets.get("my-app", "alice@example.com");
|
||||
```
|
||||
|
||||
**Parameters:**
|
||||
|
||||
- `options.service` (string, required) - The service or application name
|
||||
- `options.name` (string, required) - The username or account identifier
|
||||
|
||||
**Returns:**
|
||||
|
||||
- `Promise<string | null>` - The stored password, or `null` if not found
|
||||
|
||||
### `Bun.secrets.set(options, value)`
|
||||
|
||||
Store or update a credential.
|
||||
|
||||
```typescript
|
||||
import { secrets } from "bun";
|
||||
|
||||
await secrets.set({
|
||||
service: "my-app",
|
||||
name: "alice@example.com",
|
||||
value: "super-secret-password",
|
||||
});
|
||||
```
|
||||
|
||||
**Parameters:**
|
||||
|
||||
- `options.service` (string, required) - The service or application name
|
||||
- `options.name` (string, required) - The username or account identifier
|
||||
- `value` (string, required) - The password or secret to store
|
||||
|
||||
**Notes:**
|
||||
|
||||
- If a credential already exists for the given service/name combination, it will be replaced
|
||||
- The stored value is encrypted by the operating system
|
||||
|
||||
### `Bun.secrets.delete(options)`
|
||||
|
||||
Delete a stored credential.
|
||||
|
||||
```typescript
|
||||
const deleted = await Bun.secrets.delete({
|
||||
service: "my-app",
|
||||
name: "alice@example.com",
|
||||
value: "super-secret-password",
|
||||
});
|
||||
// Returns: boolean
|
||||
```
|
||||
|
||||
**Parameters:**
|
||||
|
||||
- `options.service` (string, required) - The service or application name
|
||||
- `options.name` (string, required) - The username or account identifier
|
||||
|
||||
**Returns:**
|
||||
|
||||
- `Promise<boolean>` - `true` if a credential was deleted, `false` if not found
|
||||
|
||||
## Examples
|
||||
|
||||
### Storing CLI Tool Credentials
|
||||
|
||||
```javascript
|
||||
// Store GitHub CLI token (instead of ~/.config/gh/hosts.yml)
|
||||
await Bun.secrets.set({
|
||||
service: "my-app.com",
|
||||
name: "github-token",
|
||||
value: "ghp_xxxxxxxxxxxxxxxxxxxx",
|
||||
});
|
||||
|
||||
// Or if you prefer without an object
|
||||
await Bun.secrets.set("my-app.com", "github-token", "ghp_xxxxxxxxxxxxxxxxxxxx");
|
||||
|
||||
// Store npm registry token (instead of ~/.npmrc)
|
||||
await Bun.secrets.set({
|
||||
service: "npm-registry",
|
||||
name: "https://registry.npmjs.org",
|
||||
value: "npm_xxxxxxxxxxxxxxxxxxxx",
|
||||
});
|
||||
|
||||
// Retrieve for API calls
|
||||
const token = await Bun.secrets.get({
|
||||
service: "gh-cli",
|
||||
name: "github.com",
|
||||
});
|
||||
|
||||
if (token) {
|
||||
const response = await fetch("https://api.github.com/name", {
|
||||
headers: {
|
||||
"Authorization": `token ${token}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
### Migrating from Plaintext Config Files
|
||||
|
||||
```javascript
|
||||
// Instead of storing in ~/.aws/credentials
|
||||
await Bun.secrets.set({
|
||||
service: "aws-cli",
|
||||
name: "AWS_SECRET_ACCESS_KEY",
|
||||
value: process.env.AWS_SECRET_ACCESS_KEY,
|
||||
});
|
||||
|
||||
// Instead of .env files with sensitive data
|
||||
await Bun.secrets.set({
|
||||
service: "my-app",
|
||||
name: "api-key",
|
||||
value: "sk_live_xxxxxxxxxxxxxxxxxxxx",
|
||||
});
|
||||
|
||||
// Load at runtime
|
||||
const apiKey =
|
||||
(await Bun.secrets.get({
|
||||
service: "my-app",
|
||||
name: "api-key",
|
||||
})) || process.env.API_KEY; // Fallback for CI/production
|
||||
```
|
||||
|
||||
### Error Handling
|
||||
|
||||
```javascript
|
||||
try {
|
||||
await Bun.secrets.set({
|
||||
service: "my-app",
|
||||
name: "alice",
|
||||
value: "password123",
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Failed to store credential:", error.message);
|
||||
}
|
||||
|
||||
// Check if a credential exists
|
||||
const password = await Bun.secrets.get({
|
||||
service: "my-app",
|
||||
name: "alice",
|
||||
});
|
||||
|
||||
if (password === null) {
|
||||
console.log("No credential found");
|
||||
}
|
||||
```
|
||||
|
||||
### Updating Credentials
|
||||
|
||||
```javascript
|
||||
// Initial password
|
||||
await Bun.secrets.set({
|
||||
service: "email-server",
|
||||
name: "admin@example.com",
|
||||
value: "old-password",
|
||||
});
|
||||
|
||||
// Update to new password
|
||||
await Bun.secrets.set({
|
||||
service: "email-server",
|
||||
name: "admin@example.com",
|
||||
value: "new-password",
|
||||
});
|
||||
|
||||
// The old password is replaced
|
||||
```
|
||||
|
||||
## Platform Behavior
|
||||
|
||||
### macOS (Keychain)
|
||||
|
||||
- Credentials are stored in the name's login keychain
|
||||
- The keychain may prompt for access permission on first use
|
||||
- Credentials persist across system restarts
|
||||
- Accessible by the name who stored them
|
||||
|
||||
### Linux (libsecret)
|
||||
|
||||
- Requires a secret service daemon (GNOME Keyring, KWallet, etc.)
|
||||
- Credentials are stored in the default collection
|
||||
- May prompt for unlock if the keyring is locked
|
||||
- The secret service must be running
|
||||
|
||||
### Windows (Credential Manager)
|
||||
|
||||
- Credentials are stored in Windows Credential Manager
|
||||
- Visible in Control Panel → Credential Manager → Windows Credentials
|
||||
- Persist with `CRED_PERSIST_ENTERPRISE` flag so it's scoped per user
|
||||
- Encrypted using Windows Data Protection API
|
||||
|
||||
## Security Considerations
|
||||
|
||||
1. **Encryption**: Credentials are encrypted by the operating system's credential manager
|
||||
2. **Access Control**: Only the name who stored the credential can retrieve it
|
||||
3. **No Plain Text**: Passwords are never stored in plain text
|
||||
4. **Memory Safety**: Bun zeros out password memory after use
|
||||
5. **Process Isolation**: Credentials are isolated per name account
|
||||
|
||||
## Limitations
|
||||
|
||||
- Maximum password length varies by platform (typically 2048-4096 bytes)
|
||||
- Service and name names should be reasonable lengths (< 256 characters)
|
||||
- Some special characters may need escaping depending on the platform
|
||||
- Requires appropriate system services:
|
||||
- Linux: Secret service daemon must be running
|
||||
- macOS: Keychain Access must be available
|
||||
- Windows: Credential Manager service must be enabled
|
||||
|
||||
## Comparison with Environment Variables
|
||||
|
||||
Unlike environment variables, `Bun.secrets`:
|
||||
|
||||
- ✅ Encrypts credentials at rest (thanks to the operating system)
|
||||
- ✅ Avoids exposing secrets in process memory dumps (memory is zeroed after its no longer needed)
|
||||
- ✅ Survives application restarts
|
||||
- ✅ Can be updated without restarting the application
|
||||
- ✅ Provides name-level access control
|
||||
- ❌ Requires OS credential service
|
||||
- ❌ Not very useful for deployment secrets (use environment variables in production)
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Use descriptive service names**: Match the tool or application name
|
||||
If you're building a CLI for external use, you probably should use a UTI (Uniform Type Identifier) for the service name.
|
||||
|
||||
```javascript
|
||||
// Good - matches the actual tool
|
||||
{ service: "com.docker.hub", name: "username" }
|
||||
{ service: "com.vercel.cli", name: "team-name" }
|
||||
|
||||
// Avoid - too generic
|
||||
{ service: "api", name: "key" }
|
||||
```
|
||||
|
||||
2. **Credentials-only**: Don't store application configuration in this API
|
||||
This API is slow, you probably still need to use a config file for some things.
|
||||
|
||||
3. **Use for local development tools**:
|
||||
- ✅ CLI tools (gh, npm, docker, kubectl)
|
||||
- ✅ Local development servers
|
||||
- ✅ Personal API keys for testing
|
||||
- ❌ Production servers (use proper secret management)
|
||||
|
||||
## TypeScript
|
||||
|
||||
```typescript
|
||||
namespace Bun {
|
||||
interface SecretsOptions {
|
||||
service: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
interface Secrets {
|
||||
get(options: SecretsOptions): Promise<string | null>;
|
||||
set(options: SecretsOptions, value: string): Promise<void>;
|
||||
delete(options: SecretsOptions): Promise<boolean>;
|
||||
}
|
||||
|
||||
const secrets: Secrets;
|
||||
}
|
||||
```
|
||||
|
||||
## See Also
|
||||
|
||||
- [Environment Variables](./env.md) - For deployment configuration
|
||||
- [Bun.password](./password.md) - For password hashing and verification
|
||||
389
docs/api/sql.md
389
docs/api/sql.md
@@ -1,4 +1,4 @@
|
||||
Bun provides native bindings for working with SQL databases through a unified Promise-based API that supports both PostgreSQL and SQLite. The interface is designed to be simple and performant, using tagged template literals for queries and offering features like connection pooling, transactions, and prepared statements.
|
||||
Bun provides native bindings for working with SQL databases through a unified Promise-based API that supports PostgreSQL, MySQL, and SQLite. The interface is designed to be simple and performant, using tagged template literals for queries and offering features like connection pooling, transactions, and prepared statements.
|
||||
|
||||
```ts
|
||||
import { sql, SQL } from "bun";
|
||||
@@ -10,9 +10,16 @@ const users = await sql`
|
||||
LIMIT ${10}
|
||||
`;
|
||||
|
||||
// With a a SQLite db
|
||||
// With MySQL
|
||||
const mysql = new SQL("mysql://user:pass@localhost:3306/mydb");
|
||||
const mysqlResults = await mysql`
|
||||
SELECT * FROM users
|
||||
WHERE active = ${true}
|
||||
`;
|
||||
|
||||
// With SQLite
|
||||
const sqlite = new SQL("sqlite://myapp.db");
|
||||
const results = await sqlite`
|
||||
const sqliteResults = await sqlite`
|
||||
SELECT * FROM users
|
||||
WHERE active = ${1}
|
||||
`;
|
||||
@@ -52,7 +59,7 @@ Bun.SQL provides a unified API for multiple database systems:
|
||||
|
||||
PostgreSQL is used when:
|
||||
|
||||
- The connection string doesn't match SQLite patterns (it's the fallback adapter)
|
||||
- The connection string doesn't match SQLite or MySQL patterns (it's the fallback adapter)
|
||||
- The connection string explicitly uses `postgres://` or `postgresql://` protocols
|
||||
- No connection string is provided and environment variables point to PostgreSQL
|
||||
|
||||
@@ -66,9 +73,82 @@ const pg = new SQL("postgres://user:pass@localhost:5432/mydb");
|
||||
await pg`SELECT ...`;
|
||||
```
|
||||
|
||||
### MySQL
|
||||
|
||||
MySQL support is built into Bun.SQL, providing the same tagged template literal interface with full compatibility for MySQL 5.7+ and MySQL 8.0+:
|
||||
|
||||
```ts
|
||||
import { SQL } from "bun";
|
||||
|
||||
// MySQL connection
|
||||
const mysql = new SQL("mysql://user:password@localhost:3306/database");
|
||||
const mysql2 = new SQL("mysql2://user:password@localhost:3306/database"); // mysql2 protocol also works
|
||||
|
||||
// Using options object
|
||||
const mysql3 = new SQL({
|
||||
adapter: "mysql",
|
||||
hostname: "localhost",
|
||||
port: 3306,
|
||||
database: "myapp",
|
||||
username: "dbuser",
|
||||
password: "secretpass",
|
||||
});
|
||||
|
||||
// Works with parameters - automatically uses prepared statements
|
||||
const users = await mysql`SELECT * FROM users WHERE id = ${userId}`;
|
||||
|
||||
// Transactions work the same as PostgreSQL
|
||||
await mysql.begin(async tx => {
|
||||
await tx`INSERT INTO users (name) VALUES (${"Alice"})`;
|
||||
await tx`UPDATE accounts SET balance = balance - 100 WHERE user_id = ${userId}`;
|
||||
});
|
||||
|
||||
// Bulk inserts
|
||||
const newUsers = [
|
||||
{ name: "Alice", email: "alice@example.com" },
|
||||
{ name: "Bob", email: "bob@example.com" },
|
||||
];
|
||||
await mysql`INSERT INTO users ${mysql(newUsers)}`;
|
||||
```
|
||||
|
||||
{% details summary="MySQL Connection String Formats" %}
|
||||
|
||||
MySQL accepts various URL formats for connection strings:
|
||||
|
||||
```ts
|
||||
// Standard mysql:// protocol
|
||||
new SQL("mysql://user:pass@localhost:3306/database");
|
||||
new SQL("mysql://user:pass@localhost/database"); // Default port 3306
|
||||
|
||||
// mysql2:// protocol (compatibility with mysql2 npm package)
|
||||
new SQL("mysql2://user:pass@localhost:3306/database");
|
||||
|
||||
// With query parameters
|
||||
new SQL("mysql://user:pass@localhost/db?ssl=true");
|
||||
|
||||
// Unix socket connection
|
||||
new SQL("mysql://user:pass@/database?socket=/var/run/mysqld/mysqld.sock");
|
||||
```
|
||||
|
||||
{% /details %}
|
||||
|
||||
{% details summary="MySQL-Specific Features" %}
|
||||
|
||||
MySQL databases support:
|
||||
|
||||
- **Prepared statements**: Automatically created for parameterized queries with statement caching
|
||||
- **Binary protocol**: For better performance with prepared statements and accurate type handling
|
||||
- **Multiple result sets**: Support for stored procedures returning multiple result sets
|
||||
- **Authentication plugins**: Support for mysql_native_password, caching_sha2_password (MySQL 8.0 default), and sha256_password
|
||||
- **SSL/TLS connections**: Configurable SSL modes similar to PostgreSQL
|
||||
- **Connection attributes**: Client information sent to server for monitoring
|
||||
- **Query pipelining**: Execute multiple prepared statements without waiting for responses
|
||||
|
||||
{% /details %}
|
||||
|
||||
### SQLite
|
||||
|
||||
SQLite support is now built into Bun.SQL, providing the same tagged template literal interface as PostgreSQL:
|
||||
SQLite support is built into Bun.SQL, providing the same tagged template literal interface:
|
||||
|
||||
```ts
|
||||
import { SQL } from "bun";
|
||||
@@ -90,8 +170,7 @@ const db2 = new SQL({
|
||||
const db3 = new SQL("myapp.db", { adapter: "sqlite" });
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>SQLite Connection String Formats</summary>
|
||||
{% details summary="SQLite Connection String Formats" %}
|
||||
|
||||
SQLite accepts various URL formats for connection strings:
|
||||
|
||||
@@ -122,10 +201,9 @@ new SQL("sqlite://data.db?mode=rwc"); // Read-write-create mode (default)
|
||||
|
||||
**Note:** Simple filenames without a protocol (like `"myapp.db"`) require explicitly specifying `{ adapter: "sqlite" }` to avoid ambiguity with PostgreSQL.
|
||||
|
||||
</details>
|
||||
{% /details %}
|
||||
|
||||
<details>
|
||||
<summary>SQLite-Specific Options</summary>
|
||||
{% details summary="SQLite-Specific Options" %}
|
||||
|
||||
SQLite databases support additional configuration options:
|
||||
|
||||
@@ -151,7 +229,7 @@ Query parameters in the URL are parsed to set these options:
|
||||
- `?mode=rw` → `readonly: false, create: false`
|
||||
- `?mode=rwc` → `readonly: false, create: true` (default)
|
||||
|
||||
</details>
|
||||
{% /details %}
|
||||
|
||||
### Inserting data
|
||||
|
||||
@@ -364,7 +442,24 @@ await query;
|
||||
|
||||
### Automatic Database Detection
|
||||
|
||||
When using `Bun.sql()` without arguments or `new SQL()` with a connection string, the adapter is automatically detected based on the URL format. SQLite becomes the default adapter in these cases:
|
||||
When using `Bun.sql()` without arguments or `new SQL()` with a connection string, the adapter is automatically detected based on the URL format:
|
||||
|
||||
#### MySQL Auto-Detection
|
||||
|
||||
MySQL is automatically selected when the connection string matches these patterns:
|
||||
|
||||
- `mysql://...` - MySQL protocol URLs
|
||||
- `mysql2://...` - MySQL2 protocol URLs (compatibility alias)
|
||||
|
||||
```ts
|
||||
// These all use MySQL automatically (no adapter needed)
|
||||
const sql1 = new SQL("mysql://user:pass@localhost/mydb");
|
||||
const sql2 = new SQL("mysql2://user:pass@localhost:3306/mydb");
|
||||
|
||||
// Works with DATABASE_URL environment variable
|
||||
DATABASE_URL="mysql://user:pass@localhost/mydb" bun run app.js
|
||||
DATABASE_URL="mysql2://user:pass@localhost:3306/mydb" bun run app.js
|
||||
```
|
||||
|
||||
#### SQLite Auto-Detection
|
||||
|
||||
@@ -390,17 +485,42 @@ DATABASE_URL="file://./data/app.db" bun run app.js
|
||||
|
||||
#### PostgreSQL Auto-Detection
|
||||
|
||||
PostgreSQL is the default for all other connection strings:
|
||||
PostgreSQL is the default for connection strings that don't match MySQL or SQLite patterns:
|
||||
|
||||
```bash
|
||||
# PostgreSQL is detected for these patterns
|
||||
DATABASE_URL="postgres://user:pass@localhost:5432/mydb" bun run app.js
|
||||
DATABASE_URL="postgresql://user:pass@localhost:5432/mydb" bun run app.js
|
||||
|
||||
# Or any URL that doesn't match SQLite patterns
|
||||
# Or any URL that doesn't match MySQL or SQLite patterns
|
||||
DATABASE_URL="localhost:5432/mydb" bun run app.js
|
||||
```
|
||||
|
||||
### MySQL Environment Variables
|
||||
|
||||
MySQL connections can be configured via environment variables:
|
||||
|
||||
```bash
|
||||
# Primary connection URL (checked first)
|
||||
MYSQL_URL="mysql://user:pass@localhost:3306/mydb"
|
||||
|
||||
# Alternative: DATABASE_URL with MySQL protocol
|
||||
DATABASE_URL="mysql://user:pass@localhost:3306/mydb"
|
||||
DATABASE_URL="mysql2://user:pass@localhost:3306/mydb"
|
||||
```
|
||||
|
||||
If no connection URL is provided, MySQL checks these individual parameters:
|
||||
|
||||
| Environment Variable | Default Value | Description |
|
||||
| ------------------------ | ------------- | -------------------------------- |
|
||||
| `MYSQL_HOST` | `localhost` | Database host |
|
||||
| `MYSQL_PORT` | `3306` | Database port |
|
||||
| `MYSQL_USER` | `root` | Database user |
|
||||
| `MYSQL_PASSWORD` | (empty) | Database password |
|
||||
| `MYSQL_DATABASE` | `mysql` | Database name |
|
||||
| `MYSQL_URL` | (empty) | Primary connection URL for MySQL |
|
||||
| `TLS_MYSQL_DATABASE_URL` | (empty) | SSL/TLS-enabled connection URL |
|
||||
|
||||
### PostgreSQL Environment Variables
|
||||
|
||||
The following environment variables can be used to define the PostgreSQL connection:
|
||||
@@ -458,6 +578,54 @@ The `--sql-preconnect` flag will automatically establish a PostgreSQL connection
|
||||
|
||||
You can configure your database connection manually by passing options to the SQL constructor. Options vary depending on the database adapter:
|
||||
|
||||
### MySQL Options
|
||||
|
||||
```ts
|
||||
import { SQL } from "bun";
|
||||
|
||||
const db = new SQL({
|
||||
// Required for MySQL when using options object
|
||||
adapter: "mysql",
|
||||
|
||||
// Connection details
|
||||
hostname: "localhost",
|
||||
port: 3306,
|
||||
database: "myapp",
|
||||
username: "dbuser",
|
||||
password: "secretpass",
|
||||
|
||||
// Unix socket connection (alternative to hostname/port)
|
||||
// socket: "/var/run/mysqld/mysqld.sock",
|
||||
|
||||
// Connection pool settings
|
||||
max: 20, // Maximum connections in pool (default: 10)
|
||||
idleTimeout: 30, // Close idle connections after 30s
|
||||
maxLifetime: 0, // Connection lifetime in seconds (0 = forever)
|
||||
connectionTimeout: 30, // Timeout when establishing new connections
|
||||
|
||||
// SSL/TLS options
|
||||
ssl: "prefer", // or "disable", "require", "verify-ca", "verify-full"
|
||||
// tls: {
|
||||
// rejectUnauthorized: true,
|
||||
// ca: "path/to/ca.pem",
|
||||
// key: "path/to/key.pem",
|
||||
// cert: "path/to/cert.pem",
|
||||
// },
|
||||
|
||||
// Callbacks
|
||||
onconnect: client => {
|
||||
console.log("Connected to MySQL");
|
||||
},
|
||||
onclose: (client, err) => {
|
||||
if (err) {
|
||||
console.error("MySQL connection error:", err);
|
||||
} else {
|
||||
console.log("MySQL connection closed");
|
||||
}
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
### PostgreSQL Options
|
||||
|
||||
```ts
|
||||
@@ -532,15 +700,14 @@ const db = new SQL({
|
||||
});
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>SQLite Connection Notes</summary>
|
||||
{% details summary="SQLite Connection Notes" %}
|
||||
|
||||
- **Connection Pooling**: SQLite doesn't use connection pooling as it's a file-based database. Each `SQL` instance represents a single connection.
|
||||
- **Transactions**: SQLite supports nested transactions through savepoints, similar to PostgreSQL.
|
||||
- **Concurrent Access**: SQLite handles concurrent access through file locking. Use WAL mode for better concurrency.
|
||||
- **Memory Databases**: Using `:memory:` creates a temporary database that exists only for the connection lifetime.
|
||||
|
||||
</details>
|
||||
{% /details %}
|
||||
|
||||
## Dynamic passwords
|
||||
|
||||
@@ -838,6 +1005,8 @@ try {
|
||||
}
|
||||
```
|
||||
|
||||
{% details summary="PostgreSQL-Specific Error Codes" %}
|
||||
|
||||
### PostgreSQL Connection Errors
|
||||
|
||||
| Connection Errors | Description |
|
||||
@@ -903,12 +1072,13 @@ try {
|
||||
| `ERR_POSTGRES_UNSAFE_TRANSACTION` | Unsafe transaction operation detected |
|
||||
| `ERR_POSTGRES_INVALID_TRANSACTION_STATE` | Invalid transaction state |
|
||||
|
||||
{% /details %}
|
||||
|
||||
### SQLite-Specific Errors
|
||||
|
||||
SQLite errors provide error codes and numbers that correspond to SQLite's standard error codes:
|
||||
|
||||
<details>
|
||||
<summary>Common SQLite Error Codes</summary>
|
||||
{% details summary="Common SQLite Error Codes" %}
|
||||
|
||||
| Error Code | errno | Description |
|
||||
| ------------------- | ----- | ---------------------------------------------------- |
|
||||
@@ -945,7 +1115,7 @@ try {
|
||||
}
|
||||
```
|
||||
|
||||
</details>
|
||||
{% /details %}
|
||||
|
||||
## Numbers and BigInt
|
||||
|
||||
@@ -979,11 +1149,106 @@ console.log(typeof x, x); // "bigint" 9223372036854777n
|
||||
There's still some things we haven't finished yet.
|
||||
|
||||
- Connection preloading via `--db-preconnect` Bun CLI flag
|
||||
- MySQL support: [we're working on it](https://github.com/oven-sh/bun/pull/15274)
|
||||
- Column name transforms (e.g. `snake_case` to `camelCase`). This is mostly blocked on a unicode-aware implementation of changing the case in C++ using WebKit's `WTF::String`.
|
||||
- Column type transforms
|
||||
|
||||
### Postgres-specific features
|
||||
## Database-Specific Features
|
||||
|
||||
#### Authentication Methods
|
||||
|
||||
MySQL supports multiple authentication plugins that are automatically negotiated:
|
||||
|
||||
- **`mysql_native_password`** - Traditional MySQL authentication, widely compatible
|
||||
- **`caching_sha2_password`** - Default in MySQL 8.0+, more secure with RSA key exchange
|
||||
- **`sha256_password`** - SHA-256 based authentication
|
||||
|
||||
The client automatically handles authentication plugin switching when requested by the server, including secure password exchange over non-SSL connections.
|
||||
|
||||
#### Prepared Statements & Performance
|
||||
|
||||
MySQL uses server-side prepared statements for all parameterized queries:
|
||||
|
||||
```ts
|
||||
// This automatically creates a prepared statement on the server
|
||||
const user = await mysql`SELECT * FROM users WHERE id = ${userId}`;
|
||||
|
||||
// Prepared statements are cached and reused for identical queries
|
||||
for (const id of userIds) {
|
||||
// Same prepared statement is reused
|
||||
await mysql`SELECT * FROM users WHERE id = ${id}`;
|
||||
}
|
||||
|
||||
// Query pipelining - multiple statements sent without waiting
|
||||
const [users, orders, products] = await Promise.all([
|
||||
mysql`SELECT * FROM users WHERE active = ${true}`,
|
||||
mysql`SELECT * FROM orders WHERE status = ${"pending"}`,
|
||||
mysql`SELECT * FROM products WHERE in_stock = ${true}`,
|
||||
]);
|
||||
```
|
||||
|
||||
#### Multiple Result Sets
|
||||
|
||||
MySQL can return multiple result sets from multi-statement queries:
|
||||
|
||||
```ts
|
||||
const mysql = new SQL("mysql://user:pass@localhost/mydb");
|
||||
|
||||
// Multi-statement queries with simple() method
|
||||
const multiResults = await mysql`
|
||||
SELECT * FROM users WHERE id = 1;
|
||||
SELECT * FROM orders WHERE user_id = 1;
|
||||
`.simple();
|
||||
```
|
||||
|
||||
#### Character Sets & Collations
|
||||
|
||||
Bun.SQL automatically uses `utf8mb4` character set for MySQL connections, ensuring full Unicode support including emojis. This is the recommended character set for modern MySQL applications.
|
||||
|
||||
#### Connection Attributes
|
||||
|
||||
Bun automatically sends client information to MySQL for better monitoring:
|
||||
|
||||
```ts
|
||||
// These attributes are sent automatically:
|
||||
// _client_name: "Bun"
|
||||
// _client_version: <bun version>
|
||||
// You can see these in MySQL's performance_schema.session_connect_attrs
|
||||
```
|
||||
|
||||
#### Type Handling
|
||||
|
||||
MySQL types are automatically converted to JavaScript types:
|
||||
|
||||
| MySQL Type | JavaScript Type | Notes |
|
||||
| --------------------------------------- | ------------------------ | ---------------------------------------------------------------------------------------------------- |
|
||||
| INT, TINYINT, MEDIUMINT | number | Within safe integer range |
|
||||
| BIGINT | string, number or BigInt | If the value fits in i32/u32 size will be number otherwise string or BigInt Based on `bigint` option |
|
||||
| DECIMAL, NUMERIC | string | To preserve precision |
|
||||
| FLOAT, DOUBLE | number | |
|
||||
| DATE | Date | JavaScript Date object |
|
||||
| DATETIME, TIMESTAMP | Date | With timezone handling |
|
||||
| TIME | number | Total of microseconds |
|
||||
| YEAR | number | |
|
||||
| CHAR, VARCHAR, VARSTRING, STRING | string | |
|
||||
| TINY TEXT, MEDIUM TEXT, TEXT, LONG TEXT | string | |
|
||||
| TINY BLOB, MEDIUM BLOB, BLOG, LONG BLOB | string | BLOB Types are alias for TEXT types |
|
||||
| JSON | object/array | Automatically parsed |
|
||||
| BIT(1) | boolean | BIT(1) in MySQL |
|
||||
| GEOMETRY | string | Geometry data |
|
||||
|
||||
#### Differences from PostgreSQL
|
||||
|
||||
While the API is unified, there are some behavioral differences:
|
||||
|
||||
1. **Parameter placeholders**: MySQL uses `?` internally but Bun converts `$1, $2` style automatically
|
||||
2. **RETURNING clause**: MySQL doesn't support RETURNING; use `result.lastInsertRowid` or a separate SELECT
|
||||
3. **Array types**: MySQL doesn't have native array types like PostgreSQL
|
||||
|
||||
### MySQL-Specific Features
|
||||
|
||||
We haven't implemented `LOAD DATA INFILE` support yet
|
||||
|
||||
### PostgreSQL-Specific Features
|
||||
|
||||
We haven't implemented these yet:
|
||||
|
||||
@@ -998,13 +1263,89 @@ We also haven't implemented some of the more uncommon features like:
|
||||
- Point & PostGIS types
|
||||
- All the multi-dimensional integer array types (only a couple of the types are supported)
|
||||
|
||||
## Common Patterns & Best Practices
|
||||
|
||||
### Working with MySQL Result Sets
|
||||
|
||||
```ts
|
||||
// Getting insert ID after INSERT
|
||||
const result = await mysql`INSERT INTO users (name) VALUES (${"Alice"})`;
|
||||
console.log(result.lastInsertRowid); // MySQL's LAST_INSERT_ID()
|
||||
|
||||
// Handling affected rows
|
||||
const updated =
|
||||
await mysql`UPDATE users SET active = ${false} WHERE age < ${18}`;
|
||||
console.log(updated.affectedRows); // Number of rows updated
|
||||
|
||||
// Using MySQL-specific functions
|
||||
const now = await mysql`SELECT NOW() as current_time`;
|
||||
const uuid = await mysql`SELECT UUID() as id`;
|
||||
```
|
||||
|
||||
### MySQL Error Handling
|
||||
|
||||
```ts
|
||||
try {
|
||||
await mysql`INSERT INTO users (email) VALUES (${"duplicate@email.com"})`;
|
||||
} catch (error) {
|
||||
if (error.code === "ER_DUP_ENTRY") {
|
||||
console.log("Duplicate entry detected");
|
||||
} else if (error.code === "ER_ACCESS_DENIED_ERROR") {
|
||||
console.log("Access denied");
|
||||
} else if (error.code === "ER_BAD_DB_ERROR") {
|
||||
console.log("Database does not exist");
|
||||
}
|
||||
// MySQL error codes are compatible with mysql/mysql2 packages
|
||||
}
|
||||
```
|
||||
|
||||
### Performance Tips for MySQL
|
||||
|
||||
1. **Use connection pooling**: Set appropriate `max` pool size based on your workload
|
||||
2. **Enable prepared statements**: They're enabled by default and improve performance
|
||||
3. **Use transactions for bulk operations**: Group related queries in transactions
|
||||
4. **Index properly**: MySQL relies heavily on indexes for query performance
|
||||
5. **Use `utf8mb4` charset**: It's set by default and handles all Unicode characters
|
||||
|
||||
## Frequently Asked Questions
|
||||
|
||||
> Why is this `Bun.sql` and not `Bun.postgres`?
|
||||
|
||||
The plan is to add more database drivers in the future.
|
||||
The plan was to add more database drivers in the future. Now with MySQL support added, this unified API supports PostgreSQL, MySQL, and SQLite.
|
||||
|
||||
> Why not just use an existing library?
|
||||
> How do I know which database adapter is being used?
|
||||
|
||||
The adapter is automatically detected from the connection string:
|
||||
|
||||
- URLs starting with `mysql://` or `mysql2://` use MySQL
|
||||
- URLs matching SQLite patterns (`:memory:`, `sqlite://`, `file://`) use SQLite
|
||||
- Everything else defaults to PostgreSQL
|
||||
|
||||
> Are MySQL stored procedures supported?
|
||||
|
||||
Yes, stored procedures are fully supported including OUT parameters and multiple result sets:
|
||||
|
||||
```ts
|
||||
// Call stored procedure
|
||||
const results = await mysql`CALL GetUserStats(${userId}, @total_orders)`;
|
||||
|
||||
// Get OUT parameter
|
||||
const outParam = await mysql`SELECT @total_orders as total`;
|
||||
```
|
||||
|
||||
> Can I use MySQL-specific SQL syntax?
|
||||
|
||||
Yes, you can use any MySQL-specific syntax:
|
||||
|
||||
```ts
|
||||
// MySQL-specific syntax works fine
|
||||
await mysql`SET @user_id = ${userId}`;
|
||||
await mysql`SHOW TABLES`;
|
||||
await mysql`DESCRIBE users`;
|
||||
await mysql`EXPLAIN SELECT * FROM users WHERE id = ${id}`;
|
||||
```
|
||||
|
||||
## Why not just use an existing library?
|
||||
|
||||
npm packages like postgres.js, pg, and node-postgres can be used in Bun too. They're great options.
|
||||
|
||||
|
||||
@@ -122,6 +122,59 @@ Messages are automatically enqueued until the worker is ready, so there is no ne
|
||||
|
||||
To send messages, use [`worker.postMessage`](https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage) and [`self.postMessage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage). This leverages the [HTML Structured Clone Algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm).
|
||||
|
||||
### Performance optimizations
|
||||
|
||||
Bun includes optimized fast paths for `postMessage` to dramatically improve performance for common data types:
|
||||
|
||||
**String fast path** - When posting pure string values, Bun bypasses the structured clone algorithm entirely, achieving significant performance gains with no serialization overhead.
|
||||
|
||||
**Simple object fast path** - For plain objects containing only primitive values (strings, numbers, booleans, null, undefined), Bun uses an optimized serialization path that stores properties directly without full structured cloning.
|
||||
|
||||
The simple object fast path activates when the object:
|
||||
|
||||
- Is a plain object with no prototype chain modifications
|
||||
- Contains only enumerable, configurable data properties
|
||||
- Has no indexed properties or getter/setter methods
|
||||
- All property values are primitives or strings
|
||||
|
||||
With these fast paths, Bun's `postMessage` performs **2-241x faster** because the message length no longer has a meaningful impact on performance.
|
||||
|
||||
**Bun (with fast paths):**
|
||||
|
||||
```
|
||||
postMessage({ prop: 11 chars string, ...9 more props }) - 648ns
|
||||
postMessage({ prop: 14 KB string, ...9 more props }) - 719ns
|
||||
postMessage({ prop: 3 MB string, ...9 more props }) - 1.26µs
|
||||
```
|
||||
|
||||
**Node.js v24.6.0 (for comparison):**
|
||||
|
||||
```
|
||||
postMessage({ prop: 11 chars string, ...9 more props }) - 1.19µs
|
||||
postMessage({ prop: 14 KB string, ...9 more props }) - 2.69µs
|
||||
postMessage({ prop: 3 MB string, ...9 more props }) - 304µs
|
||||
```
|
||||
|
||||
```js
|
||||
// String fast path - optimized
|
||||
postMessage("Hello, worker!");
|
||||
|
||||
// Simple object fast path - optimized
|
||||
postMessage({
|
||||
message: "Hello",
|
||||
count: 42,
|
||||
enabled: true,
|
||||
data: null,
|
||||
});
|
||||
|
||||
// Complex objects still work but use standard structured clone
|
||||
postMessage({
|
||||
nested: { deep: { object: true } },
|
||||
date: new Date(),
|
||||
buffer: new ArrayBuffer(8),
|
||||
});
|
||||
```
|
||||
|
||||
```js
|
||||
// On the worker thread, `postMessage` is automatically "routed" to the parent thread.
|
||||
postMessage({ hello: "world" });
|
||||
|
||||
459
docs/api/yaml.md
Normal file
459
docs/api/yaml.md
Normal file
@@ -0,0 +1,459 @@
|
||||
In Bun, YAML is a first-class citizen alongside JSON and TOML.
|
||||
|
||||
Bun provides built-in support for YAML files through both runtime APIs and bundler integration. You can
|
||||
|
||||
- Parse YAML strings with `Bun.YAML.parse`
|
||||
- import & require YAML files as modules at runtime (including hot reloading & watch mode support)
|
||||
- import & require YAML files in frontend apps via bun's bundler
|
||||
|
||||
## Conformance
|
||||
|
||||
Bun's YAML parser currently passes over 90% of the official YAML test suite. While we're actively working on reaching 100% conformance, the current implementation covers the vast majority of real-world use cases. The parser is written in Zig for optimal performance and is continuously being improved.
|
||||
|
||||
## Runtime API
|
||||
|
||||
### `Bun.YAML.parse()`
|
||||
|
||||
Parse a YAML string into a JavaScript object.
|
||||
|
||||
```ts
|
||||
import { YAML } from "bun";
|
||||
const text = `
|
||||
name: John Doe
|
||||
age: 30
|
||||
email: john@example.com
|
||||
hobbies:
|
||||
- reading
|
||||
- coding
|
||||
- hiking
|
||||
`;
|
||||
|
||||
const data = YAML.parse(text);
|
||||
console.log(data);
|
||||
// {
|
||||
// name: "John Doe",
|
||||
// age: 30,
|
||||
// email: "john@example.com",
|
||||
// hobbies: ["reading", "coding", "hiking"]
|
||||
// }
|
||||
```
|
||||
|
||||
#### Multi-document YAML
|
||||
|
||||
When parsing YAML with multiple documents (separated by `---`), `Bun.YAML.parse()` returns an array:
|
||||
|
||||
```ts
|
||||
const multiDoc = `
|
||||
---
|
||||
name: Document 1
|
||||
---
|
||||
name: Document 2
|
||||
---
|
||||
name: Document 3
|
||||
`;
|
||||
|
||||
const docs = Bun.YAML.parse(multiDoc);
|
||||
console.log(docs);
|
||||
// [
|
||||
// { name: "Document 1" },
|
||||
// { name: "Document 2" },
|
||||
// { name: "Document 3" }
|
||||
// ]
|
||||
```
|
||||
|
||||
#### Supported YAML Features
|
||||
|
||||
Bun's YAML parser supports the full YAML 1.2 specification, including:
|
||||
|
||||
- **Scalars**: strings, numbers, booleans, null values
|
||||
- **Collections**: sequences (arrays) and mappings (objects)
|
||||
- **Anchors and Aliases**: reusable nodes with `&` and `*`
|
||||
- **Tags**: type hints like `!!str`, `!!int`, `!!float`, `!!bool`, `!!null`
|
||||
- **Multi-line strings**: literal (`|`) and folded (`>`) scalars
|
||||
- **Comments**: using `#`
|
||||
- **Directives**: `%YAML` and `%TAG`
|
||||
|
||||
```ts
|
||||
const yaml = `
|
||||
# Employee record
|
||||
employee: &emp
|
||||
name: Jane Smith
|
||||
department: Engineering
|
||||
skills:
|
||||
- JavaScript
|
||||
- TypeScript
|
||||
- React
|
||||
|
||||
manager: *emp # Reference to employee
|
||||
|
||||
config: !!str 123 # Explicit string type
|
||||
|
||||
description: |
|
||||
This is a multi-line
|
||||
literal string that preserves
|
||||
line breaks and spacing.
|
||||
|
||||
summary: >
|
||||
This is a folded string
|
||||
that joins lines with spaces
|
||||
unless there are blank lines.
|
||||
`;
|
||||
|
||||
const data = Bun.YAML.parse(yaml);
|
||||
```
|
||||
|
||||
#### Error Handling
|
||||
|
||||
`Bun.YAML.parse()` throws a `SyntaxError` if the YAML is invalid:
|
||||
|
||||
```ts
|
||||
try {
|
||||
Bun.YAML.parse("invalid: yaml: content:");
|
||||
} catch (error) {
|
||||
console.error("Failed to parse YAML:", error.message);
|
||||
}
|
||||
```
|
||||
|
||||
## Module Import
|
||||
|
||||
### ES Modules
|
||||
|
||||
You can import YAML files directly as ES modules. The YAML content is parsed and made available as both default and named exports:
|
||||
|
||||
```yaml#config.yaml
|
||||
database:
|
||||
host: localhost
|
||||
port: 5432
|
||||
name: myapp
|
||||
|
||||
redis:
|
||||
host: localhost
|
||||
port: 6379
|
||||
|
||||
features:
|
||||
auth: true
|
||||
rateLimit: true
|
||||
analytics: false
|
||||
```
|
||||
|
||||
#### Default Import
|
||||
|
||||
```ts#app.ts
|
||||
import config from "./config.yaml";
|
||||
|
||||
console.log(config.database.host); // "localhost"
|
||||
console.log(config.redis.port); // 6379
|
||||
```
|
||||
|
||||
#### Named Imports
|
||||
|
||||
You can destructure top-level YAML properties as named imports:
|
||||
|
||||
```ts
|
||||
import { database, redis, features } from "./config.yaml";
|
||||
|
||||
console.log(database.host); // "localhost"
|
||||
console.log(redis.port); // 6379
|
||||
console.log(features.auth); // true
|
||||
```
|
||||
|
||||
Or combine both:
|
||||
|
||||
```ts
|
||||
import config, { database, features } from "./config.yaml";
|
||||
|
||||
// Use the full config object
|
||||
console.log(config);
|
||||
|
||||
// Or use specific parts
|
||||
if (features.rateLimit) {
|
||||
setupRateLimiting(database);
|
||||
}
|
||||
```
|
||||
|
||||
### CommonJS
|
||||
|
||||
YAML files can also be required in CommonJS:
|
||||
|
||||
```js
|
||||
const config = require("./config.yaml");
|
||||
console.log(config.database.name); // "myapp"
|
||||
|
||||
// Destructuring also works
|
||||
const { database, redis } = require("./config.yaml");
|
||||
console.log(database.port); // 5432
|
||||
```
|
||||
|
||||
## Hot Reloading with YAML
|
||||
|
||||
One of the most powerful features of Bun's YAML support is hot reloading. When you run your application with `bun --hot`, changes to YAML files are automatically detected and reloaded without closing connections
|
||||
|
||||
### Configuration Hot Reloading
|
||||
|
||||
```yaml#config.yaml
|
||||
server:
|
||||
port: 3000
|
||||
host: localhost
|
||||
|
||||
features:
|
||||
debug: true
|
||||
verbose: false
|
||||
```
|
||||
|
||||
```ts#server.ts
|
||||
import { server, features } from "./config.yaml";
|
||||
|
||||
console.log(`Starting server on ${server.host}:${server.port}`);
|
||||
|
||||
if (features.debug) {
|
||||
console.log("Debug mode enabled");
|
||||
}
|
||||
|
||||
// Your server code here
|
||||
Bun.serve({
|
||||
port: server.port,
|
||||
hostname: server.host,
|
||||
fetch(req) {
|
||||
if (features.verbose) {
|
||||
console.log(`${req.method} ${req.url}`);
|
||||
}
|
||||
return new Response("Hello World");
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
Run with hot reloading:
|
||||
|
||||
```bash
|
||||
bun --hot server.ts
|
||||
```
|
||||
|
||||
Now when you modify `config.yaml`, the changes are immediately reflected in your running application. This is perfect for:
|
||||
|
||||
- Adjusting configuration during development
|
||||
- Testing different settings without restarts
|
||||
- Live debugging with configuration changes
|
||||
- Feature flag toggling
|
||||
|
||||
## Configuration Management
|
||||
|
||||
### Environment-Based Configuration
|
||||
|
||||
YAML excels at managing configuration across different environments:
|
||||
|
||||
```yaml#config.yaml
|
||||
defaults: &defaults
|
||||
timeout: 5000
|
||||
retries: 3
|
||||
cache:
|
||||
enabled: true
|
||||
ttl: 3600
|
||||
|
||||
development:
|
||||
<<: *defaults
|
||||
api:
|
||||
url: http://localhost:4000
|
||||
key: dev_key_12345
|
||||
logging:
|
||||
level: debug
|
||||
pretty: true
|
||||
|
||||
staging:
|
||||
<<: *defaults
|
||||
api:
|
||||
url: https://staging-api.example.com
|
||||
key: ${STAGING_API_KEY}
|
||||
logging:
|
||||
level: info
|
||||
pretty: false
|
||||
|
||||
production:
|
||||
<<: *defaults
|
||||
api:
|
||||
url: https://api.example.com
|
||||
key: ${PROD_API_KEY}
|
||||
cache:
|
||||
enabled: true
|
||||
ttl: 86400
|
||||
logging:
|
||||
level: error
|
||||
pretty: false
|
||||
```
|
||||
|
||||
```ts#app.ts
|
||||
import configs from "./config.yaml";
|
||||
|
||||
const env = process.env.NODE_ENV || "development";
|
||||
const config = configs[env];
|
||||
|
||||
// Environment variables in YAML values can be interpolated
|
||||
function interpolateEnvVars(obj: any): any {
|
||||
if (typeof obj === "string") {
|
||||
return obj.replace(/\${(\w+)}/g, (_, key) => process.env[key] || "");
|
||||
}
|
||||
if (typeof obj === "object") {
|
||||
for (const key in obj) {
|
||||
obj[key] = interpolateEnvVars(obj[key]);
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
export default interpolateEnvVars(config);
|
||||
```
|
||||
|
||||
### Feature Flags Configuration
|
||||
|
||||
```yaml#features.yaml
|
||||
features:
|
||||
newDashboard:
|
||||
enabled: true
|
||||
rolloutPercentage: 50
|
||||
allowedUsers:
|
||||
- admin@example.com
|
||||
- beta@example.com
|
||||
|
||||
experimentalAPI:
|
||||
enabled: false
|
||||
endpoints:
|
||||
- /api/v2/experimental
|
||||
- /api/v2/beta
|
||||
|
||||
darkMode:
|
||||
enabled: true
|
||||
default: auto # auto, light, dark
|
||||
```
|
||||
|
||||
```ts#feature-flags.ts
|
||||
import { features } from "./features.yaml";
|
||||
|
||||
export function isFeatureEnabled(
|
||||
featureName: string,
|
||||
userEmail?: string,
|
||||
): boolean {
|
||||
const feature = features[featureName];
|
||||
|
||||
if (!feature?.enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check rollout percentage
|
||||
if (feature.rolloutPercentage < 100) {
|
||||
const hash = hashCode(userEmail || "anonymous");
|
||||
if (hash % 100 >= feature.rolloutPercentage) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Check allowed users
|
||||
if (feature.allowedUsers && userEmail) {
|
||||
return feature.allowedUsers.includes(userEmail);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Use with hot reloading to toggle features in real-time
|
||||
if (isFeatureEnabled("newDashboard", user.email)) {
|
||||
renderNewDashboard();
|
||||
} else {
|
||||
renderLegacyDashboard();
|
||||
}
|
||||
```
|
||||
|
||||
### Database Configuration
|
||||
|
||||
```yaml#database.yaml
|
||||
connections:
|
||||
primary:
|
||||
type: postgres
|
||||
host: ${DB_HOST:-localhost}
|
||||
port: ${DB_PORT:-5432}
|
||||
database: ${DB_NAME:-myapp}
|
||||
username: ${DB_USER:-postgres}
|
||||
password: ${DB_PASS}
|
||||
pool:
|
||||
min: 2
|
||||
max: 10
|
||||
idleTimeout: 30000
|
||||
|
||||
cache:
|
||||
type: redis
|
||||
host: ${REDIS_HOST:-localhost}
|
||||
port: ${REDIS_PORT:-6379}
|
||||
password: ${REDIS_PASS}
|
||||
db: 0
|
||||
|
||||
analytics:
|
||||
type: clickhouse
|
||||
host: ${ANALYTICS_HOST:-localhost}
|
||||
port: 8123
|
||||
database: analytics
|
||||
|
||||
migrations:
|
||||
autoRun: ${AUTO_MIGRATE:-false}
|
||||
directory: ./migrations
|
||||
|
||||
seeds:
|
||||
enabled: ${SEED_DB:-false}
|
||||
directory: ./seeds
|
||||
```
|
||||
|
||||
```ts#db.ts
|
||||
import { connections, migrations } from "./database.yaml";
|
||||
import { createConnection } from "./database-driver";
|
||||
|
||||
// Parse environment variables with defaults
|
||||
function parseConfig(config: any) {
|
||||
return JSON.parse(
|
||||
JSON.stringify(config).replace(
|
||||
/\${([^:-]+)(?::([^}]+))?}/g,
|
||||
(_, key, defaultValue) => process.env[key] || defaultValue || "",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
const dbConfig = parseConfig(connections);
|
||||
|
||||
export const db = await createConnection(dbConfig.primary);
|
||||
export const cache = await createConnection(dbConfig.cache);
|
||||
export const analytics = await createConnection(dbConfig.analytics);
|
||||
|
||||
// Auto-run migrations if configured
|
||||
if (parseConfig(migrations).autoRun === "true") {
|
||||
await runMigrations(db, migrations.directory);
|
||||
}
|
||||
```
|
||||
|
||||
### Bundler Integration
|
||||
|
||||
When you import YAML files in your application and bundle it with Bun, the YAML is parsed at build time and included as a JavaScript module:
|
||||
|
||||
```bash
|
||||
bun build app.ts --outdir=dist
|
||||
```
|
||||
|
||||
This means:
|
||||
|
||||
- Zero runtime YAML parsing overhead in production
|
||||
- Smaller bundle sizes
|
||||
- Tree-shaking support for unused configuration (named imports)
|
||||
|
||||
### Dynamic Imports
|
||||
|
||||
YAML files can be dynamically imported, useful for loading configuration on demand:
|
||||
|
||||
```ts#Load configuration based on environment
|
||||
const env = process.env.NODE_ENV || "development";
|
||||
const config = await import(`./configs/${env}.yaml`);
|
||||
|
||||
// Load user-specific settings
|
||||
async function loadUserSettings(userId: string) {
|
||||
try {
|
||||
const settings = await import(`./users/${userId}/settings.yaml`);
|
||||
return settings.default;
|
||||
} catch {
|
||||
return await import("./users/default-settings.yaml");
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -408,16 +408,119 @@ $ bun build --compile --asset-naming="[name].[ext]" ./index.ts
|
||||
|
||||
To trim down the size of the executable a little, pass `--minify` to `bun build --compile`. This uses Bun's minifier to reduce the code size. Overall though, Bun's binary is still way too big and we need to make it smaller.
|
||||
|
||||
## Using Bun.build() API
|
||||
|
||||
You can also generate standalone executables using the `Bun.build()` JavaScript API. This is useful when you need programmatic control over the build process.
|
||||
|
||||
### Basic usage
|
||||
|
||||
```js
|
||||
await Bun.build({
|
||||
entrypoints: ["./app.ts"],
|
||||
outdir: "./dist",
|
||||
compile: {
|
||||
target: "bun-windows-x64",
|
||||
outfile: "myapp.exe",
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
### Windows metadata with Bun.build()
|
||||
|
||||
When targeting Windows, you can specify metadata through the `windows` object:
|
||||
|
||||
```js
|
||||
await Bun.build({
|
||||
entrypoints: ["./app.ts"],
|
||||
outdir: "./dist",
|
||||
compile: {
|
||||
target: "bun-windows-x64",
|
||||
outfile: "myapp.exe",
|
||||
windows: {
|
||||
title: "My Application",
|
||||
publisher: "My Company Inc",
|
||||
version: "1.2.3.4",
|
||||
description: "A powerful application built with Bun",
|
||||
copyright: "© 2024 My Company Inc",
|
||||
hideConsole: false, // Set to true for GUI applications
|
||||
icon: "./icon.ico", // Path to icon file
|
||||
},
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
### Cross-compilation with Bun.build()
|
||||
|
||||
You can cross-compile for different platforms:
|
||||
|
||||
```js
|
||||
// Build for multiple platforms
|
||||
const platforms = [
|
||||
{ target: "bun-windows-x64", outfile: "app-windows.exe" },
|
||||
{ target: "bun-linux-x64", outfile: "app-linux" },
|
||||
{ target: "bun-darwin-arm64", outfile: "app-macos" },
|
||||
];
|
||||
|
||||
for (const platform of platforms) {
|
||||
await Bun.build({
|
||||
entrypoints: ["./app.ts"],
|
||||
outdir: "./dist",
|
||||
compile: platform,
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
## Windows-specific flags
|
||||
|
||||
When compiling a standalone executable on Windows, there are two platform-specific options that can be used to customize metadata on the generated `.exe` file:
|
||||
When compiling a standalone executable for Windows, there are several platform-specific options that can be used to customize the generated `.exe` file:
|
||||
|
||||
- `--windows-icon=path/to/icon.ico` to customize the executable file icon.
|
||||
- `--windows-hide-console` to disable the background terminal, which can be used for applications that do not need a TTY.
|
||||
### Visual customization
|
||||
|
||||
- `--windows-icon=path/to/icon.ico` - Set the executable file icon
|
||||
- `--windows-hide-console` - Disable the background terminal window (useful for GUI applications)
|
||||
|
||||
### Metadata customization
|
||||
|
||||
You can embed version information and other metadata into your Windows executable:
|
||||
|
||||
- `--windows-title <STR>` - Set the product name (appears in file properties)
|
||||
- `--windows-publisher <STR>` - Set the company name
|
||||
- `--windows-version <STR>` - Set the version number (e.g. "1.2.3.4")
|
||||
- `--windows-description <STR>` - Set the file description
|
||||
- `--windows-copyright <STR>` - Set the copyright information
|
||||
|
||||
#### Example with all metadata flags:
|
||||
|
||||
```sh
|
||||
bun build --compile ./app.ts \
|
||||
--outfile myapp.exe \
|
||||
--windows-title "My Application" \
|
||||
--windows-publisher "My Company Inc" \
|
||||
--windows-version "1.2.3.4" \
|
||||
--windows-description "A powerful application built with Bun" \
|
||||
--windows-copyright "© 2024 My Company Inc"
|
||||
```
|
||||
|
||||
This metadata will be visible in Windows Explorer when viewing the file properties:
|
||||
|
||||
1. Right-click the executable in Windows Explorer
|
||||
2. Select "Properties"
|
||||
3. Go to the "Details" tab
|
||||
|
||||
#### Version string format
|
||||
|
||||
The `--windows-version` flag accepts version strings in the following formats:
|
||||
|
||||
- `"1"` - Will be normalized to "1.0.0.0"
|
||||
- `"1.2"` - Will be normalized to "1.2.0.0"
|
||||
- `"1.2.3"` - Will be normalized to "1.2.3.0"
|
||||
- `"1.2.3.4"` - Full version format
|
||||
|
||||
Each version component must be a number between 0 and 65535.
|
||||
|
||||
{% callout %}
|
||||
|
||||
These flags currently cannot be used when cross-compiling because they depend on Windows APIs.
|
||||
These flags currently cannot be used when cross-compiling because they depend on Windows APIs. They are only available when building on Windows itself.
|
||||
|
||||
{% /callout %}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Bun's fast native bundler is now in beta. It can be used via the `bun build` CLI command or the `Bun.build()` JavaScript API.
|
||||
Bun's fast native bundler can be used via the `bun build` CLI command or the `Bun.build()` JavaScript API.
|
||||
|
||||
{% codetabs group="a" %}
|
||||
|
||||
@@ -1259,6 +1259,33 @@ $ bun build ./index.tsx --outdir ./out --drop=console --drop=debugger --drop=any
|
||||
|
||||
{% /codetabs %}
|
||||
|
||||
### `throw`
|
||||
|
||||
Controls error handling behavior when the build fails. When set to `true` (default), the returned promise rejects with an `AggregateError`. When set to `false`, the promise resolves with a `BuildOutput` object where `success` is `false`.
|
||||
|
||||
```ts#JavaScript
|
||||
// Default behavior: throws on error
|
||||
try {
|
||||
await Bun.build({
|
||||
entrypoints: ['./index.tsx'],
|
||||
throw: true, // default
|
||||
});
|
||||
} catch (error) {
|
||||
// Handle AggregateError
|
||||
console.error("Build failed:", error);
|
||||
}
|
||||
|
||||
// Alternative: handle errors via success property
|
||||
const result = await Bun.build({
|
||||
entrypoints: ['./index.tsx'],
|
||||
throw: false,
|
||||
});
|
||||
|
||||
if (!result.success) {
|
||||
console.error("Build failed with errors:", result.logs);
|
||||
}
|
||||
```
|
||||
|
||||
## Outputs
|
||||
|
||||
The `Bun.build` function returns a `Promise<BuildOutput>`, defined as:
|
||||
@@ -1569,8 +1596,7 @@ interface BuildConfig {
|
||||
* When set to `true`, the returned promise rejects with an AggregateError when a build failure happens.
|
||||
* When set to `false`, the `success` property of the returned object will be `false` when a build failure happens.
|
||||
*
|
||||
* This defaults to `false` in Bun 1.1 and will change to `true` in Bun 1.2
|
||||
* as most usage of `Bun.build` forgets to check for errors.
|
||||
* This defaults to `true`.
|
||||
*/
|
||||
throw?: boolean;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
The Bun bundler implements a set of default loaders out of the box. As a rule of thumb, the bundler and the runtime both support the same set of file types out of the box.
|
||||
|
||||
`.js` `.cjs` `.mjs` `.mts` `.cts` `.ts` `.tsx` `.jsx` `.toml` `.json` `.txt` `.wasm` `.node` `.html`
|
||||
`.js` `.cjs` `.mjs` `.mts` `.cts` `.ts` `.tsx` `.jsx` `.toml` `.json` `.yaml` `.yml` `.txt` `.wasm` `.node` `.html`
|
||||
|
||||
Bun uses the file extension to determine which built-in _loader_ should be used to parse the file. Every loader has a name, such as `js`, `tsx`, or `json`. These names are used when building [plugins](https://bun.com/docs/bundler/plugins) that extend Bun with custom loaders.
|
||||
|
||||
@@ -121,6 +121,55 @@ export default {
|
||||
|
||||
{% /codetabs %}
|
||||
|
||||
### `yaml`
|
||||
|
||||
**YAML loader**. Default for `.yaml` and `.yml`.
|
||||
|
||||
YAML files can be directly imported. Bun will parse them with its fast native YAML parser.
|
||||
|
||||
```ts
|
||||
import config from "./config.yaml";
|
||||
config.database.host; // => "localhost"
|
||||
|
||||
// via import attribute:
|
||||
// import myCustomYAML from './my.config' with {type: "yaml"};
|
||||
```
|
||||
|
||||
During bundling, the parsed YAML is inlined into the bundle as a JavaScript object.
|
||||
|
||||
```ts
|
||||
var config = {
|
||||
database: {
|
||||
host: "localhost",
|
||||
port: 5432,
|
||||
},
|
||||
// ...other fields
|
||||
};
|
||||
config.database.host;
|
||||
```
|
||||
|
||||
If a `.yaml` or `.yml` file is passed as an entrypoint, it will be converted to a `.js` module that `export default`s the parsed object.
|
||||
|
||||
{% codetabs %}
|
||||
|
||||
```yaml#Input
|
||||
name: John Doe
|
||||
age: 35
|
||||
email: johndoe@example.com
|
||||
```
|
||||
|
||||
```js#Output
|
||||
export default {
|
||||
name: "John Doe",
|
||||
age: 35,
|
||||
email: "johndoe@example.com"
|
||||
}
|
||||
```
|
||||
|
||||
{% /codetabs %}
|
||||
|
||||
For more details on YAML support including the runtime API `Bun.YAML.parse()`, see the [YAML API documentation](/docs/api/yaml).
|
||||
|
||||
### `text`
|
||||
|
||||
**Text loader**. Default for `.txt`.
|
||||
|
||||
@@ -9,6 +9,7 @@ Plugins can register callbacks to be run at various points in the lifecycle of a
|
||||
- [`onStart()`](#onstart): Run once the bundler has started a bundle
|
||||
- [`onResolve()`](#onresolve): Run before a module is resolved
|
||||
- [`onLoad()`](#onload): Run before a module is loaded.
|
||||
- [`onEnd()`](#onend): Run after the bundle has completed
|
||||
- [`onBeforeParse()`](#onbeforeparse): Run zero-copy native addons in the parser thread before a file is parsed.
|
||||
|
||||
### Reference
|
||||
@@ -18,6 +19,7 @@ A rough overview of the types (please refer to Bun's `bun.d.ts` for the full typ
|
||||
```ts
|
||||
type PluginBuilder = {
|
||||
onStart(callback: () => void): void;
|
||||
onEnd(callback: (result: BuildOutput) => void | Promise<void>): void;
|
||||
onResolve: (
|
||||
args: { filter: RegExp; namespace?: string },
|
||||
callback: (args: { path: string; importer: string }) => {
|
||||
@@ -285,6 +287,53 @@ plugin({
|
||||
|
||||
Note that the `.defer()` function currently has the limitation that it can only be called once per `onLoad` callback.
|
||||
|
||||
### `onEnd`
|
||||
|
||||
```ts
|
||||
onEnd(callback: (result: BuildOutput) => void | Promise<void>): void;
|
||||
```
|
||||
|
||||
Registers a callback to be run when the bundler completes a bundle (whether successful or not).
|
||||
|
||||
The callback receives the `BuildOutput` object containing:
|
||||
|
||||
- `success`: boolean indicating if the build succeeded
|
||||
- `outputs`: array of generated build artifacts
|
||||
- `logs`: array of build messages (warnings, errors, etc.)
|
||||
|
||||
This is useful for post-processing, cleanup, notifications, or custom error handling.
|
||||
|
||||
```ts
|
||||
await Bun.build({
|
||||
entrypoints: ["./index.ts"],
|
||||
outdir: "./out",
|
||||
plugins: [
|
||||
{
|
||||
name: "onEnd example",
|
||||
setup(build) {
|
||||
build.onEnd(result => {
|
||||
if (result.success) {
|
||||
console.log(
|
||||
`✅ Build succeeded with ${result.outputs.length} outputs`,
|
||||
);
|
||||
} else {
|
||||
console.error(`❌ Build failed with ${result.logs.length} errors`);
|
||||
}
|
||||
});
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
The `onEnd` callbacks are called:
|
||||
|
||||
- **Before** the build promise resolves or rejects
|
||||
- **After** all bundling is complete
|
||||
- **In the order** they were registered
|
||||
|
||||
Multiple plugins can register `onEnd` callbacks, and they will all be called sequentially. If an `onEnd` callback returns a promise, the build will wait for it to resolve before continuing.
|
||||
|
||||
## Native plugins
|
||||
|
||||
One of the reasons why Bun's bundler is so fast is that it is written in native code and leverages multi-threading to load and parse modules in parallel.
|
||||
|
||||
@@ -245,8 +245,8 @@ In Bun's CLI, simple boolean flags like `--minify` do not accept an argument. Ot
|
||||
---
|
||||
|
||||
- `--jsx-side-effects`
|
||||
- n/a
|
||||
- JSX is always assumed to be side-effect-free
|
||||
- `--jsx-side-effects`
|
||||
- Controls whether JSX expressions are marked as `/* @__PURE__ */` for dead code elimination. Default is `false` (JSX marked as pure).
|
||||
|
||||
---
|
||||
|
||||
@@ -617,7 +617,7 @@ In Bun's CLI, simple boolean flags like `--minify` do not accept an argument. Ot
|
||||
|
||||
- `jsxSideEffects`
|
||||
- `jsxSideEffects`
|
||||
- Not supported in JS API, configure in `tsconfig.json`
|
||||
- Controls whether JSX expressions are marked as pure for dead code elimination
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -230,16 +230,15 @@ $ bun install --backend copyfile
|
||||
|
||||
**`symlink`** is typically only used for `file:` dependencies (and eventually `link:`) internally. To prevent infinite loops, it skips symlinking the `node_modules` folder.
|
||||
|
||||
If you install with `--backend=symlink`, Node.js won't resolve node_modules of dependencies unless each dependency has its own node_modules folder or you pass `--preserve-symlinks` to `node`. See [Node.js documentation on `--preserve-symlinks`](https://nodejs.org/api/cli.html#--preserve-symlinks).
|
||||
If you install with `--backend=symlink`, Node.js won't resolve node_modules of dependencies unless each dependency has its own node_modules folder or you pass `--preserve-symlinks` to `node` or `bun`. See [Node.js documentation on `--preserve-symlinks`](https://nodejs.org/api/cli.html#--preserve-symlinks).
|
||||
|
||||
```bash
|
||||
$ rm -rf node_modules
|
||||
$ bun install --backend symlink
|
||||
$ bun --preserve-symlinks ./my-file.js
|
||||
$ node --preserve-symlinks ./my-file.js # https://nodejs.org/api/cli.html#--preserve-symlinks
|
||||
```
|
||||
|
||||
Bun's runtime does not currently expose an equivalent of `--preserve-symlinks`, though the code for it does exist.
|
||||
|
||||
## npm registry metadata
|
||||
|
||||
bun uses a binary format for caching NPM registry responses. This loads much faster than JSON and tends to be smaller on disk.
|
||||
|
||||
@@ -8,6 +8,14 @@ The `bun` CLI contains a Node.js-compatible package manager designed to be a dra
|
||||
|
||||
{% /callout %}
|
||||
|
||||
{% callout %}
|
||||
|
||||
**💾 Disk efficient** — Bun install stores all packages in a global cache (`~/.bun/install/cache/`) and creates hardlinks (Linux) or copy-on-write clones (macOS) to `node_modules`. This means duplicate packages across projects point to the same underlying data, taking up virtually no extra disk space.
|
||||
|
||||
For more details, see [Package manager > Global cache](https://bun.com/docs/install/cache).
|
||||
|
||||
{% /callout %}
|
||||
|
||||
{% details summary="For Linux users" %}
|
||||
The recommended minimum Linux Kernel version is 5.6. If you're on Linux kernel 5.1 - 5.5, `bun install` will work, but HTTP requests will be slow due to a lack of support for io_uring's `connect()` operation.
|
||||
|
||||
@@ -207,6 +215,12 @@ Isolated installs create a central package store in `node_modules/.bun/` with sy
|
||||
|
||||
For complete documentation on isolated installs, refer to [Package manager > Isolated installs](https://bun.com/docs/install/isolated).
|
||||
|
||||
## Disk efficiency
|
||||
|
||||
Bun uses a global cache at `~/.bun/install/cache/` to minimize disk usage. Packages are stored once and linked to `node_modules` using hardlinks (Linux/Windows) or copy-on-write (macOS), so duplicate packages across projects don't consume additional disk space.
|
||||
|
||||
For complete documentation refer to [Package manager > Global cache](https://bun.com/docs/install/cache).
|
||||
|
||||
## Configuration
|
||||
|
||||
The default behavior of `bun install` can be configured in `bunfig.toml`. The default values are shown below.
|
||||
|
||||
4
docs/guides/deployment/index.json
Normal file
4
docs/guides/deployment/index.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"name": "Deployment",
|
||||
"description": "A collection of guides for deploying Bun to providers"
|
||||
}
|
||||
157
docs/guides/deployment/railway.md
Normal file
157
docs/guides/deployment/railway.md
Normal file
@@ -0,0 +1,157 @@
|
||||
---
|
||||
name: Deploy a Bun application on Railway
|
||||
description: Deploy Bun applications to Railway with this step-by-step guide covering CLI and dashboard methods, optional PostgreSQL setup, and automatic SSL configuration.
|
||||
---
|
||||
|
||||
Railway is an infrastructure platform where you can provision infrastructure, develop with that infrastructure locally, and then deploy to the cloud. It enables instant deployments from GitHub with zero configuration, automatic SSL, and built-in database provisioning.
|
||||
|
||||
This guide walks through deploying a Bun application with a PostgreSQL database (optional), which is exactly what the template below provides.
|
||||
|
||||
You can either follow this guide step-by-step or simply deploy the pre-configured template with one click:
|
||||
|
||||
{% raw %}
|
||||
|
||||
<a href="https://railway.com/deploy/bun-react-postgres?referralCode=Bun&utm_medium=integration&utm_source=template&utm_campaign=bun" target="_blank">
|
||||
<img src="https://railway.com/button.svg" alt="Deploy on Railway" />
|
||||
</a>
|
||||
|
||||
{% /raw %}
|
||||
|
||||
---
|
||||
|
||||
**Prerequisites**:
|
||||
|
||||
- A Bun application ready for deployment
|
||||
- A [Railway account](https://railway.app/)
|
||||
- Railway CLI (for CLI deployment method)
|
||||
- A GitHub account (for Dashboard deployment method)
|
||||
|
||||
---
|
||||
|
||||
## Method 1: Deploy via CLI
|
||||
|
||||
---
|
||||
|
||||
#### Step 1
|
||||
|
||||
Ensure sure you have the Railway CLI installed.
|
||||
|
||||
```bash
|
||||
bun install -g @railway/cli
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
#### Step 2
|
||||
|
||||
Log into your Railway account.
|
||||
|
||||
```bash
|
||||
railway login
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
#### Step 3
|
||||
|
||||
After successfully authenticating, initialize a new project.
|
||||
|
||||
```bash
|
||||
# Initialize project
|
||||
bun-react-postgres$ railway init
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
#### Step 4
|
||||
|
||||
After initializing the project, add a new database and service.
|
||||
|
||||
> **Note:** Step 4 is only necessary if your application uses a database. If you don't need PostgreSQL, skip to Step 5.
|
||||
|
||||
```bash
|
||||
# Add PostgreSQL database. Make sure to add this first!
|
||||
bun-react-postgres$ railway add --database postgres
|
||||
|
||||
# Add your application service.
|
||||
bun-react-postgres$ railway add --service bun-react-db --variables DATABASE_URL=\${{Postgres.DATABASE_URL}}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
#### Step 5
|
||||
|
||||
After the services have been created and connected, deploy the application to Railway. By default, services are only accessible within Railway's private network. To make your app publicly accessible, you need to generate a public domain.
|
||||
|
||||
```bash
|
||||
# Deploy your application
|
||||
bun-nextjs-starter$ railway up
|
||||
|
||||
# Generate public domain
|
||||
bun-nextjs-starter$ railway domain
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Method 2: Deploy via Dashboard
|
||||
|
||||
---
|
||||
|
||||
#### Step 1
|
||||
|
||||
Create a new project
|
||||
|
||||
1. Go to [Railway Dashboard](http://railway.com/dashboard?utm_medium=integration&utm_source=docs&utm_campaign=bun)
|
||||
2. Click **"+ New"** → **"GitHub repo"**
|
||||
3. Choose your repository
|
||||
|
||||
---
|
||||
|
||||
#### Step 2
|
||||
|
||||
Add a PostgreSQL database, and connect this database to the service
|
||||
|
||||
> **Note:** Step 2 is only necessary if your application uses a database. If you don't need PostgreSQL, skip to Step 3.
|
||||
|
||||
1. Click **"+ New"** → **"Database"** → **"Add PostgreSQL"**
|
||||
2. After the database has been created, select your service (not the database)
|
||||
3. Go to **"Variables"** tab
|
||||
4. Click **"+ New Variable"** → **"Add Reference"**
|
||||
5. Select `DATABASE_URL` from postgres
|
||||
|
||||
---
|
||||
|
||||
#### Step 3
|
||||
|
||||
Generate a public domain
|
||||
|
||||
1. Select your service
|
||||
2. Go to **"Settings"** tab
|
||||
3. Under **"Networking"**, click **"Generate Domain"**
|
||||
|
||||
---
|
||||
|
||||
Your app is now live! Railway auto-deploys on every GitHub push.
|
||||
|
||||
---
|
||||
|
||||
## Configuration (Optional)
|
||||
|
||||
---
|
||||
|
||||
By default, Railway uses [Nixpacks](https://docs.railway.com/guides/build-configuration#nixpacks-options) to automatically detect and build your Bun application with zero configuration.
|
||||
|
||||
However, using the [Railpack](https://docs.railway.com/guides/build-configuration#railpack) application builder provides better Bun support, and will always support the latest version of Bun. The pre-configured templates use Railpack by default.
|
||||
|
||||
To enable Railpack in a custom project, add the following to your `railway.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"$schema": "https://railway.com/railway.schema.json",
|
||||
"build": {
|
||||
"builder": "RAILPACK"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
For more build configuration settings, check out the [Railway documentation](https://docs.railway.com/guides/build-configuration).
|
||||
76
docs/guides/runtime/import-yaml.md
Normal file
76
docs/guides/runtime/import-yaml.md
Normal file
@@ -0,0 +1,76 @@
|
||||
---
|
||||
name: Import a YAML file
|
||||
---
|
||||
|
||||
Bun natively supports `.yaml` and `.yml` imports.
|
||||
|
||||
```yaml#config.yaml
|
||||
database:
|
||||
host: localhost
|
||||
port: 5432
|
||||
name: myapp
|
||||
|
||||
server:
|
||||
port: 3000
|
||||
timeout: 30
|
||||
|
||||
features:
|
||||
auth: true
|
||||
rateLimit: true
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
Import the file like any other source file.
|
||||
|
||||
```ts
|
||||
import config from "./config.yaml";
|
||||
|
||||
config.database.host; // => "localhost"
|
||||
config.server.port; // => 3000
|
||||
config.features.auth; // => true
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
You can also use named imports to destructure top-level properties:
|
||||
|
||||
```ts
|
||||
import { database, server, features } from "./config.yaml";
|
||||
|
||||
console.log(database.name); // => "myapp"
|
||||
console.log(server.timeout); // => 30
|
||||
console.log(features.rateLimit); // => true
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
Bun also supports [Import Attributes](https://github.com/tc39/proposal-import-attributes) syntax:
|
||||
|
||||
```ts
|
||||
import config from "./config.yaml" with { type: "yaml" };
|
||||
|
||||
config.database.port; // => 5432
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
For parsing YAML strings at runtime, use `Bun.YAML.parse()`:
|
||||
|
||||
```ts
|
||||
const yamlString = `
|
||||
name: John Doe
|
||||
age: 30
|
||||
hobbies:
|
||||
- reading
|
||||
- coding
|
||||
`;
|
||||
|
||||
const data = Bun.YAML.parse(yamlString);
|
||||
console.log(data.name); // => "John Doe"
|
||||
console.log(data.hobbies); // => ["reading", "coding"]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
See [Docs > API > YAML](https://bun.com/docs/api/yaml) for complete documentation on YAML support in Bun.
|
||||
@@ -17,7 +17,7 @@ Bun reads the following files automatically (listed in order of increasing prece
|
||||
|
||||
- `.env`
|
||||
- `.env.production`, `.env.development`, `.env.test` (depending on value of `NODE_ENV`)
|
||||
- `.env.local`
|
||||
- `.env.local` (not loaded when `NODE_ENV=test`)
|
||||
|
||||
```txt#.env
|
||||
FOO=hello
|
||||
|
||||
@@ -35,7 +35,7 @@ Add this directive to _just one file_ in your project, such as:
|
||||
- Any single `.ts` file that TypeScript includes in your compilation
|
||||
|
||||
```ts
|
||||
/// <reference types="bun/test-globals" />
|
||||
/// <reference types="bun-types/test-globals" />
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
@@ -48,12 +48,12 @@ This behavior is configurable with the `--backend` flag, which is respected by a
|
||||
- **`copyfile`**: The fallback used when any of the above fail. It is the slowest option. On macOS, it uses `fcopyfile()`; on Linux it uses `copy_file_range()`.
|
||||
- **`symlink`**: Currently used only `file:` (and eventually `link:`) dependencies. To prevent infinite loops, it skips symlinking the `node_modules` folder.
|
||||
|
||||
If you install with `--backend=symlink`, Node.js won't resolve node_modules of dependencies unless each dependency has its own `node_modules` folder or you pass `--preserve-symlinks` to `node`. See [Node.js documentation on `--preserve-symlinks`](https://nodejs.org/api/cli.html#--preserve-symlinks).
|
||||
If you install with `--backend=symlink`, Node.js won't resolve node_modules of dependencies unless each dependency has its own `node_modules` folder or you pass `--preserve-symlinks` to `node` or `bun`. See [Node.js documentation on `--preserve-symlinks`](https://nodejs.org/api/cli.html#--preserve-symlinks).
|
||||
|
||||
```bash
|
||||
$ bun install --backend symlink
|
||||
$ node --preserve-symlinks ./foo.js
|
||||
$ bun --preserve-symlinks ./foo.js
|
||||
```
|
||||
|
||||
Bun's runtime does not currently expose an equivalent of `--preserve-symlinks`.
|
||||
{% /details %}
|
||||
|
||||
81
docs/install/security-scanner-api.md
Normal file
81
docs/install/security-scanner-api.md
Normal file
@@ -0,0 +1,81 @@
|
||||
Bun's package manager can scan packages for security vulnerabilities before installation, helping protect your applications from supply chain attacks and known vulnerabilities.
|
||||
|
||||
## Quick Start
|
||||
|
||||
Configure a security scanner in your `bunfig.toml`:
|
||||
|
||||
```toml
|
||||
[install.security]
|
||||
scanner = "@acme/bun-security-scanner"
|
||||
```
|
||||
|
||||
When configured, Bun will:
|
||||
|
||||
- Scan all packages before installation
|
||||
- Display security warnings and advisories
|
||||
- Cancel installation if critical vulnerabilities are found
|
||||
- Automatically disable auto-install for security
|
||||
|
||||
## How It Works
|
||||
|
||||
Security scanners analyze packages during `bun install`, `bun add`, and other package operations. They can detect:
|
||||
|
||||
- Known security vulnerabilities (CVEs)
|
||||
- Malicious packages
|
||||
- License compliance issues
|
||||
- ...and more!
|
||||
|
||||
### Security Levels
|
||||
|
||||
Scanners report issues at two severity levels:
|
||||
|
||||
- **`fatal`** - Installation stops immediately, exits with non-zero code
|
||||
- **`warn`** - In interactive terminals, prompts to continue; in CI, exits immediately
|
||||
|
||||
## Using Pre-built Scanners
|
||||
|
||||
Many security companies publish Bun security scanners as npm packages that you can install and use immediately.
|
||||
|
||||
### Installing a Scanner
|
||||
|
||||
Install a security scanner from npm:
|
||||
|
||||
```bash
|
||||
$ bun add -d @acme/bun-security-scanner
|
||||
```
|
||||
|
||||
> **Note:** Consult your security scanner's documentation for their specific package name and installation instructions. Most scanners will be installed with `bun add`.
|
||||
|
||||
### Configuring the Scanner
|
||||
|
||||
After installation, configure it in your `bunfig.toml`:
|
||||
|
||||
```toml
|
||||
[install.security]
|
||||
scanner = "@acme/bun-security-scanner"
|
||||
```
|
||||
|
||||
### Enterprise Configuration
|
||||
|
||||
Some enterprise scanners might support authentication and/or configuration through environment variables:
|
||||
|
||||
```bash
|
||||
# This might go in ~/.bashrc, for example
|
||||
export SECURITY_API_KEY="your-api-key"
|
||||
|
||||
# The scanner will now use these credentials automatically
|
||||
bun install
|
||||
```
|
||||
|
||||
Consult your security scanner's documentation to learn which environment variables to set and if any additional configuration is required.
|
||||
|
||||
### Authoring your own scanner
|
||||
|
||||
For a complete example with tests and CI setup, see the official template:
|
||||
[github.com/oven-sh/security-scanner-template](https://github.com/oven-sh/security-scanner-template)
|
||||
|
||||
## Related
|
||||
|
||||
- [Configuration (bunfig.toml)](/docs/runtime/bunfig#install-security-scanner)
|
||||
- [Package Manager](/docs/install)
|
||||
- [Security Scanner Template](https://github.com/oven-sh/security-scanner-template)
|
||||
@@ -219,6 +219,9 @@ export default {
|
||||
page("install/npmrc", ".npmrc support", {
|
||||
description: "Bun supports loading some configuration options from .npmrc",
|
||||
}),
|
||||
page("install/security-scanner-api", "Security Scanner API", {
|
||||
description: "Scan your project for vulnerabilities with Bun's security scanner API.",
|
||||
}),
|
||||
// page("install/utilities", "Utilities", {
|
||||
// description: "Use `bun pm` to introspect your global module cache or project dependency tree.",
|
||||
// }),
|
||||
@@ -383,6 +386,9 @@ export default {
|
||||
page("api/spawn", "Child processes", {
|
||||
description: `Spawn sync and async child processes with easily configurable input and output streams.`,
|
||||
}), // "`Bun.spawn`"),
|
||||
page("api/yaml", "YAML", {
|
||||
description: `Bun.YAML.parse(string) lets you parse YAML files in JavaScript`,
|
||||
}), // "`Bun.spawn`"),
|
||||
page("api/html-rewriter", "HTMLRewriter", {
|
||||
description: `Parse and transform HTML with Bun's native HTMLRewriter API, inspired by Cloudflare Workers.`,
|
||||
}), // "`HTMLRewriter`"),
|
||||
|
||||
@@ -195,12 +195,12 @@ Click the link in the right column to jump to the associated documentation.
|
||||
---
|
||||
|
||||
- Parsing & Formatting
|
||||
- [`Bun.semver`](https://bun.com/docs/api/semver), `Bun.TOML.parse`, [`Bun.color`](https://bun.com/docs/api/color)
|
||||
- [`Bun.semver`](https://bun.com/docs/api/semver), `Bun.TOML.parse`, [`Bun.YAML.parse`](https://bun.com/docs/api/yaml), [`Bun.color`](https://bun.com/docs/api/color)
|
||||
|
||||
---
|
||||
|
||||
- Low-level / Internals
|
||||
- `Bun.mmap`, `Bun.gc`, `Bun.generateHeapSnapshot`, [`bun:jsc`](https://bun.com/docs/api/bun-jsc)
|
||||
- `Bun.mmap`, `Bun.gc`, `Bun.generateHeapSnapshot`, [`bun:jsc`](https://bun.com/reference/bun/jsc)
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -94,6 +94,7 @@ Bun supports the following loaders:
|
||||
- `file`
|
||||
- `json`
|
||||
- `toml`
|
||||
- `yaml`
|
||||
- `wasm`
|
||||
- `napi`
|
||||
- `base64`
|
||||
@@ -496,6 +497,32 @@ Whether to generate a non-Bun lockfile alongside `bun.lock`. (A `bun.lock` will
|
||||
print = "yarn"
|
||||
```
|
||||
|
||||
### `install.security.scanner`
|
||||
|
||||
Configure a security scanner to scan packages for vulnerabilities before installation.
|
||||
|
||||
First, install a security scanner from npm:
|
||||
|
||||
```bash
|
||||
$ bun add -d @acme/bun-security-scanner
|
||||
```
|
||||
|
||||
Then configure it in your `bunfig.toml`:
|
||||
|
||||
```toml
|
||||
[install.security]
|
||||
scanner = "@acme/bun-security-scanner"
|
||||
```
|
||||
|
||||
When a security scanner is configured:
|
||||
|
||||
- Auto-install is automatically disabled for security
|
||||
- Packages are scanned before installation
|
||||
- Installation is cancelled if fatal issues are found
|
||||
- Security warnings are displayed during installation
|
||||
|
||||
Learn more about [using and writing security scanners](/docs/install/security).
|
||||
|
||||
### `install.linker`
|
||||
|
||||
Configure the default linker strategy. Default `"hoisted"`.
|
||||
|
||||
@@ -8,6 +8,10 @@ Bun reads the following files automatically (listed in order of increasing prece
|
||||
- `.env.production`, `.env.development`, `.env.test` (depending on value of `NODE_ENV`)
|
||||
- `.env.local`
|
||||
|
||||
{% callout %}
|
||||
**Note:** When `NODE_ENV=test`, `.env.local` is **not** loaded. This ensures consistent test environments across different executions by preventing local overrides during testing. This behavior matches popular frameworks like [Next.js](https://nextjs.org/docs/pages/guides/environment-variables#test-environment-variables) and [Create React App](https://create-react-app.dev/docs/adding-custom-environment-variables/#what-other-env-files-can-be-used).
|
||||
{% /callout %}
|
||||
|
||||
```txt#.env
|
||||
FOO=hello
|
||||
BAR=world
|
||||
|
||||
@@ -92,15 +92,18 @@ every file before execution. Its transpiler can directly run TypeScript and JSX
|
||||
|
||||
## JSX
|
||||
|
||||
## JSON and TOML
|
||||
## JSON, TOML, and YAML
|
||||
|
||||
Source files can import a `*.json` or `*.toml` file to load its contents as a plain old JavaScript object.
|
||||
Source files can import `*.json`, `*.toml`, or `*.yaml` files to load their contents as plain JavaScript objects.
|
||||
|
||||
```ts
|
||||
import pkg from "./package.json";
|
||||
import bunfig from "./bunfig.toml";
|
||||
import config from "./config.yaml";
|
||||
```
|
||||
|
||||
See the [YAML API documentation](/docs/api/yaml) for more details on YAML support.
|
||||
|
||||
## WASI
|
||||
|
||||
{% callout %}
|
||||
|
||||
@@ -246,6 +246,65 @@ The module from which the component factory function (`createElement`, `jsx`, `j
|
||||
|
||||
{% /table %}
|
||||
|
||||
### `jsxSideEffects`
|
||||
|
||||
By default, Bun marks JSX expressions as `/* @__PURE__ */` so they can be removed during bundling if they are unused (known as "dead code elimination" or "tree shaking"). Set `jsxSideEffects` to `true` to prevent this behavior.
|
||||
|
||||
{% table %}
|
||||
|
||||
- Compiler options
|
||||
- Transpiled output
|
||||
|
||||
---
|
||||
|
||||
- ```jsonc
|
||||
{
|
||||
"jsx": "react",
|
||||
// jsxSideEffects is false by default
|
||||
}
|
||||
```
|
||||
|
||||
- ```tsx
|
||||
// JSX expressions are marked as pure
|
||||
/* @__PURE__ */ React.createElement("div", null, "Hello");
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
- ```jsonc
|
||||
{
|
||||
"jsx": "react",
|
||||
"jsxSideEffects": true,
|
||||
}
|
||||
```
|
||||
|
||||
- ```tsx
|
||||
// JSX expressions are not marked as pure
|
||||
React.createElement("div", null, "Hello");
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
- ```jsonc
|
||||
{
|
||||
"jsx": "react-jsx",
|
||||
"jsxSideEffects": true,
|
||||
}
|
||||
```
|
||||
|
||||
- ```tsx
|
||||
// Automatic runtime also respects jsxSideEffects
|
||||
jsx("div", { children: "Hello" });
|
||||
```
|
||||
|
||||
{% /table %}
|
||||
|
||||
This option is also available as a CLI flag:
|
||||
|
||||
```bash
|
||||
$ bun build --jsx-side-effects
|
||||
```
|
||||
|
||||
### JSX pragma
|
||||
|
||||
All of these values can be set on a per-file basis using _pragmas_. A pragma is a special comment that sets a compiler option in a particular file.
|
||||
|
||||
@@ -52,15 +52,18 @@ Hello world!
|
||||
|
||||
{% /codetabs %}
|
||||
|
||||
## JSON and TOML
|
||||
## JSON, TOML, and YAML
|
||||
|
||||
JSON and TOML files can be directly imported from a source file. The contents will be loaded and returned as a JavaScript object.
|
||||
JSON, TOML, and YAML files can be directly imported from a source file. The contents will be loaded and returned as a JavaScript object.
|
||||
|
||||
```ts
|
||||
import pkg from "./package.json";
|
||||
import data from "./data.toml";
|
||||
import config from "./config.yaml";
|
||||
```
|
||||
|
||||
For more details on YAML support, see the [YAML API documentation](/docs/api/yaml).
|
||||
|
||||
## WASI
|
||||
|
||||
{% callout %}
|
||||
|
||||
@@ -12,6 +12,8 @@ test("NODE_ENV is set to test", () => {
|
||||
});
|
||||
```
|
||||
|
||||
When `NODE_ENV` is set to `"test"`, Bun will not load `.env.local` files. This ensures consistent test environments across different executions by preventing local overrides during testing. Instead, use `.env.test` for test-specific environment variables, which should be committed to your repository for consistency across all developers and CI environments.
|
||||
|
||||
#### `$TZ` environment variable
|
||||
|
||||
By default, all `bun test` runs use UTC (`Etc/UTC`) as the time zone unless overridden by the `TZ` environment variable. This ensures consistent date and time behavior across different development environments.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"private": true,
|
||||
"name": "bun",
|
||||
"version": "1.2.21",
|
||||
"version": "1.2.22",
|
||||
"workspaces": [
|
||||
"./packages/bun-types",
|
||||
"./packages/@types/bun"
|
||||
|
||||
@@ -18,9 +18,11 @@ typedef enum {
|
||||
BUN_LOADER_BASE64 = 10,
|
||||
BUN_LOADER_DATAURL = 11,
|
||||
BUN_LOADER_TEXT = 12,
|
||||
BUN_LOADER_HTML = 17,
|
||||
BUN_LOADER_YAML = 18,
|
||||
} BunLoader;
|
||||
|
||||
const BunLoader BUN_LOADER_MAX = BUN_LOADER_TEXT;
|
||||
const BunLoader BUN_LOADER_MAX = BUN_LOADER_YAML;
|
||||
|
||||
typedef struct BunLogOptions {
|
||||
size_t __struct_size;
|
||||
|
||||
378
packages/bun-types/bun.d.ts
vendored
378
packages/bun-types/bun.d.ts
vendored
@@ -619,6 +619,65 @@ declare module "bun" {
|
||||
export function parse(input: string): object;
|
||||
}
|
||||
|
||||
/**
|
||||
* YAML related APIs
|
||||
*/
|
||||
namespace YAML {
|
||||
/**
|
||||
* Parse a YAML string into a JavaScript value
|
||||
*
|
||||
* @category Utilities
|
||||
*
|
||||
* @param input The YAML string to parse
|
||||
* @returns A JavaScript value
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { YAML } from "bun";
|
||||
*
|
||||
* console.log(YAML.parse("123")) // 123
|
||||
* console.log(YAML.parse("123")) // null
|
||||
* console.log(YAML.parse("false")) // false
|
||||
* console.log(YAML.parse("abc")) // "abc"
|
||||
* console.log(YAML.parse("- abc")) // [ "abc" ]
|
||||
* console.log(YAML.parse("abc: def")) // { "abc": "def" }
|
||||
* ```
|
||||
*/
|
||||
export function parse(input: string): unknown;
|
||||
|
||||
/**
|
||||
* Convert a JavaScript value into a YAML string. Strings are double quoted if they contain keywords, non-printable or
|
||||
* escaped characters, or if a YAML parser would parse them as numbers. Anchors and aliases are inferred from objects, allowing cycles.
|
||||
*
|
||||
* @category Utilities
|
||||
*
|
||||
* @param input The JavaScript value to stringify.
|
||||
* @param replacer Currently not supported.
|
||||
* @param space A number for how many spaces each level of indentation gets, or a string used as indentation. The number is clamped between 0 and 10, and the first 10 characters of the string are used.
|
||||
* @returns A string containing the YAML document.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { YAML } from "bun";
|
||||
*
|
||||
* const input = {
|
||||
* abc: "def"
|
||||
* };
|
||||
* console.log(YAML.stringify(input));
|
||||
* // # output
|
||||
* // abc: def
|
||||
*
|
||||
* const cycle = {};
|
||||
* cycle.obj = cycle;
|
||||
* console.log(YAML.stringify(cycle));
|
||||
* // # output
|
||||
* // &root
|
||||
* // obj:
|
||||
* // *root
|
||||
*/
|
||||
export function stringify(input: unknown, replacer?: undefined | null, space?: string | number): string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronously resolve a `moduleId` as though it were imported from `parent`
|
||||
*
|
||||
@@ -1628,7 +1687,7 @@ declare module "bun" {
|
||||
kind: ImportKind;
|
||||
}
|
||||
|
||||
namespace _BunBuildInterface {
|
||||
namespace Build {
|
||||
type Architecture = "x64" | "arm64";
|
||||
type Libc = "glibc" | "musl";
|
||||
type SIMD = "baseline" | "modern";
|
||||
@@ -1641,6 +1700,7 @@ declare module "bun" {
|
||||
| `bun-windows-x64-${SIMD}`
|
||||
| `bun-linux-x64-${SIMD}-${Libc}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see [Bun.build API docs](https://bun.com/docs/bundler#api)
|
||||
*/
|
||||
@@ -1813,9 +1873,10 @@ declare module "bun" {
|
||||
drop?: string[];
|
||||
|
||||
/**
|
||||
* When set to `true`, the returned promise rejects with an AggregateError when a build failure happens.
|
||||
* When set to `false`, the `success` property of the returned object will be `false` when a build failure happens.
|
||||
* This defaults to `true`.
|
||||
* - When set to `true`, the returned promise rejects with an AggregateError when a build failure happens.
|
||||
* - When set to `false`, returns a {@link BuildOutput} with `{success: false}`
|
||||
*
|
||||
* @default true
|
||||
*/
|
||||
throw?: boolean;
|
||||
|
||||
@@ -1836,7 +1897,7 @@ declare module "bun" {
|
||||
}
|
||||
|
||||
interface CompileBuildOptions {
|
||||
target?: _BunBuildInterface.Target;
|
||||
target?: Bun.Build.Target;
|
||||
execArgv?: string[];
|
||||
executablePath?: string;
|
||||
outfile?: string;
|
||||
@@ -1844,6 +1905,10 @@ declare module "bun" {
|
||||
hideConsole?: boolean;
|
||||
icon?: string;
|
||||
title?: string;
|
||||
publisher?: string;
|
||||
version?: string;
|
||||
description?: string;
|
||||
copyright?: string;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1874,7 +1939,7 @@ declare module "bun" {
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
compile: boolean | _BunBuildInterface.Target | CompileBuildOptions;
|
||||
compile: boolean | Bun.Build.Target | CompileBuildOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2120,6 +2185,287 @@ declare module "bun" {
|
||||
): string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Securely store and retrieve sensitive credentials using the operating system's native credential storage.
|
||||
*
|
||||
* Uses platform-specific secure storage:
|
||||
* - **macOS**: Keychain Services
|
||||
* - **Linux**: libsecret (GNOME Keyring, KWallet, etc.)
|
||||
* - **Windows**: Windows Credential Manager
|
||||
*
|
||||
* @category Security
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { secrets } from "bun";
|
||||
*
|
||||
* // Store a credential
|
||||
* await secrets.set({
|
||||
* service: "my-cli-tool",
|
||||
* name: "github-token",
|
||||
* value: "ghp_xxxxxxxxxxxxxxxxxxxx"
|
||||
* });
|
||||
*
|
||||
* // Retrieve a credential
|
||||
* const token = await secrets.get({
|
||||
* service: "my-cli-tool",
|
||||
* name: "github-token"
|
||||
* });
|
||||
*
|
||||
* if (token) {
|
||||
* console.log("Token found:", token);
|
||||
* } else {
|
||||
* console.log("Token not found");
|
||||
* }
|
||||
*
|
||||
* // Delete a credential
|
||||
* const deleted = await secrets.delete({
|
||||
* service: "my-cli-tool",
|
||||
* name: "github-token"
|
||||
* });
|
||||
* console.log("Deleted:", deleted); // true if deleted, false if not found
|
||||
* ```
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* // Replace plaintext config files
|
||||
* import { secrets } from "bun";
|
||||
*
|
||||
* // Instead of storing in ~/.npmrc
|
||||
* await secrets.set({
|
||||
* service: "npm-registry",
|
||||
* name: "https://registry.npmjs.org",
|
||||
* value: "npm_xxxxxxxxxxxxxxxxxxxx"
|
||||
* });
|
||||
*
|
||||
* // Instead of storing in ~/.aws/credentials
|
||||
* await secrets.set({
|
||||
* service: "aws-cli",
|
||||
* name: "default",
|
||||
* value: process.env.AWS_SECRET_ACCESS_KEY
|
||||
* });
|
||||
*
|
||||
* // Load at runtime with fallback
|
||||
* const apiKey = await secrets.get({
|
||||
* service: "my-app",
|
||||
* name: "api-key"
|
||||
* }) || process.env.API_KEY;
|
||||
* ```
|
||||
*/
|
||||
const secrets: {
|
||||
/**
|
||||
* Retrieve a stored credential from the operating system's secure storage.
|
||||
*
|
||||
* @param options - The service and name identifying the credential
|
||||
* @returns The stored credential value, or null if not found
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const password = await Bun.secrets.get({
|
||||
* service: "my-database",
|
||||
* name: "admin"
|
||||
* });
|
||||
*
|
||||
* if (password) {
|
||||
* await connectToDatabase(password);
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* // Check multiple possible locations
|
||||
* const token =
|
||||
* await Bun.secrets.get({ service: "github", name: "token" }) ||
|
||||
* await Bun.secrets.get({ service: "gh-cli", name: "github.com" }) ||
|
||||
* process.env.GITHUB_TOKEN;
|
||||
* ```
|
||||
*/
|
||||
get(options: {
|
||||
/**
|
||||
* The service or application name.
|
||||
*
|
||||
* Use a unique identifier for your application to avoid conflicts.
|
||||
* Consider using reverse domain notation for production apps (e.g., "com.example.myapp").
|
||||
*/
|
||||
service: string;
|
||||
|
||||
/**
|
||||
* The account name, username, or resource identifier.
|
||||
*
|
||||
* This identifies the specific credential within the service.
|
||||
* Common patterns include usernames, email addresses, or resource URLs.
|
||||
*/
|
||||
name: string;
|
||||
}): Promise<string | null>;
|
||||
|
||||
/**
|
||||
* Store or update a credential in the operating system's secure storage.
|
||||
*
|
||||
* If a credential already exists for the given service/name combination, it will be replaced.
|
||||
* The credential is encrypted by the operating system and only accessible to the current user.
|
||||
*
|
||||
* @param options - The service and name identifying the credential
|
||||
* @param value - The secret value to store (e.g., password, API key, token)
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* // Store an API key
|
||||
* await Bun.secrets.set({
|
||||
* service: "openai-api",
|
||||
* name: "production",
|
||||
* value: "sk-proj-xxxxxxxxxxxxxxxxxxxx"
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* // Update an existing credential
|
||||
* const newPassword = generateSecurePassword();
|
||||
* await Bun.secrets.set({
|
||||
* service: "email-server",
|
||||
* name: "admin@example.com",
|
||||
* value: newPassword
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* // Store credentials from environment variables
|
||||
* if (process.env.DATABASE_PASSWORD) {
|
||||
* await Bun.secrets.set({
|
||||
* service: "postgres",
|
||||
* name: "production",
|
||||
* value: process.env.DATABASE_PASSWORD
|
||||
* });
|
||||
* delete process.env.DATABASE_PASSWORD; // Remove from memory
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* // Delete a credential using empty string (equivalent to delete())
|
||||
* await Bun.secrets.set({
|
||||
* service: "my-service",
|
||||
* name: "api-key",
|
||||
* value: "" // Empty string deletes the credential
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* // Store credential with unrestricted access for CI environments
|
||||
* await Bun.secrets.set({
|
||||
* service: "github-actions",
|
||||
* name: "deploy-token",
|
||||
* value: process.env.DEPLOY_TOKEN,
|
||||
* allowUnrestrictedAccess: true // Allows access without user interaction on macOS
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
set(options: {
|
||||
/**
|
||||
* The service or application name.
|
||||
*
|
||||
* Use a unique identifier for your application to avoid conflicts.
|
||||
* Consider using reverse domain notation for production apps (e.g., "com.example.myapp").
|
||||
*/
|
||||
service: string;
|
||||
|
||||
/**
|
||||
* The account name, username, or resource identifier.
|
||||
*
|
||||
* This identifies the specific credential within the service.
|
||||
* Common patterns include usernames, email addresses, or resource URLs.
|
||||
*/
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* The secret value to store.
|
||||
*
|
||||
* This should be a sensitive credential like a password, API key, or token.
|
||||
* The value is encrypted by the operating system before storage.
|
||||
*
|
||||
* Note: To delete a credential, use the delete() method or pass an empty string.
|
||||
* An empty string value will delete the credential if it exists.
|
||||
*/
|
||||
value: string;
|
||||
|
||||
/**
|
||||
* Allow unrestricted access to stored credentials on macOS.
|
||||
*
|
||||
* When true, allows all applications to access this keychain item without user interaction.
|
||||
* This is useful for CI environments but reduces security.
|
||||
*
|
||||
* @default false
|
||||
* @platform macOS - Only affects macOS keychain behavior. Ignored on other platforms.
|
||||
*/
|
||||
allowUnrestrictedAccess?: boolean;
|
||||
}): Promise<void>;
|
||||
|
||||
/**
|
||||
* Delete a stored credential from the operating system's secure storage.
|
||||
*
|
||||
* @param options - The service and name identifying the credential
|
||||
* @returns true if a credential was deleted, false if not found
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* // Delete a single credential
|
||||
* const deleted = await Bun.secrets.delete({
|
||||
* service: "my-app",
|
||||
* name: "api-key"
|
||||
* });
|
||||
*
|
||||
* if (deleted) {
|
||||
* console.log("Credential removed successfully");
|
||||
* } else {
|
||||
* console.log("Credential was not found");
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* // Clean up multiple credentials
|
||||
* const services = ["github", "npm", "docker"];
|
||||
* for (const service of services) {
|
||||
* await Bun.secrets.delete({
|
||||
* service,
|
||||
* name: "token"
|
||||
* });
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* // Clean up on uninstall
|
||||
* if (process.argv.includes("--uninstall")) {
|
||||
* const deleted = await Bun.secrets.delete({
|
||||
* service: "my-cli-tool",
|
||||
* name: "config"
|
||||
* });
|
||||
* process.exit(deleted ? 0 : 1);
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
delete(options: {
|
||||
/**
|
||||
* The service or application name.
|
||||
*
|
||||
* Use a unique identifier for your application to avoid conflicts.
|
||||
* Consider using reverse domain notation for production apps (e.g., "com.example.myapp").
|
||||
*/
|
||||
service: string;
|
||||
|
||||
/**
|
||||
* The account name, username, or resource identifier.
|
||||
*
|
||||
* This identifies the specific credential within the service.
|
||||
* Common patterns include usernames, email addresses, or resource URLs.
|
||||
*/
|
||||
name: string;
|
||||
}): Promise<boolean>;
|
||||
};
|
||||
|
||||
/**
|
||||
* A build artifact represents a file that was generated by the bundler @see {@link Bun.build}
|
||||
*
|
||||
@@ -5199,6 +5545,7 @@ declare module "bun" {
|
||||
type OnLoadResult = OnLoadResultSourceCode | OnLoadResultObject | undefined | void;
|
||||
type OnLoadCallback = (args: OnLoadArgs) => OnLoadResult | Promise<OnLoadResult>;
|
||||
type OnStartCallback = () => void | Promise<void>;
|
||||
type OnEndCallback = (result: BuildOutput) => void | Promise<void>;
|
||||
|
||||
interface OnResolveArgs {
|
||||
/**
|
||||
@@ -5276,6 +5623,25 @@ declare module "bun" {
|
||||
* @returns `this` for method chaining
|
||||
*/
|
||||
onStart(callback: OnStartCallback): this;
|
||||
/**
|
||||
* Register a callback which will be invoked when bundling ends. This is
|
||||
* called after all modules have been bundled and the build is complete.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const plugin: Bun.BunPlugin = {
|
||||
* name: "my-plugin",
|
||||
* setup(builder) {
|
||||
* builder.onEnd((result) => {
|
||||
* console.log("bundle just finished!!", result);
|
||||
* });
|
||||
* },
|
||||
* };
|
||||
* ```
|
||||
*
|
||||
* @returns `this` for method chaining
|
||||
*/
|
||||
onEnd(callback: OnEndCallback): this;
|
||||
onBeforeParse(
|
||||
constraints: PluginConstraints,
|
||||
callback: {
|
||||
|
||||
4
packages/bun-types/experimental.d.ts
vendored
4
packages/bun-types/experimental.d.ts
vendored
@@ -191,7 +191,9 @@ declare module "bun" {
|
||||
* };
|
||||
* ```
|
||||
*/
|
||||
export type SSGPage<Params extends SSGParamsLike = SSGParamsLike> = React.ComponentType<SSGPageProps<Params>>;
|
||||
export type SSGPage<Params extends SSGParamsLike = SSGParamsLike> = import("react").ComponentType<
|
||||
SSGPageProps<Params>
|
||||
>;
|
||||
|
||||
/**
|
||||
* getStaticPaths is Bun's implementation of SSG (Static Site Generation) path determination.
|
||||
|
||||
10
packages/bun-types/extensions.d.ts
vendored
10
packages/bun-types/extensions.d.ts
vendored
@@ -8,6 +8,16 @@ declare module "*.toml" {
|
||||
export = contents;
|
||||
}
|
||||
|
||||
declare module "*.yaml" {
|
||||
var contents: any;
|
||||
export = contents;
|
||||
}
|
||||
|
||||
declare module "*.yml" {
|
||||
var contents: any;
|
||||
export = contents;
|
||||
}
|
||||
|
||||
declare module "*.jsonc" {
|
||||
var contents: any;
|
||||
export = contents;
|
||||
|
||||
1
packages/bun-types/index.d.ts
vendored
1
packages/bun-types/index.d.ts
vendored
@@ -22,6 +22,7 @@
|
||||
/// <reference path="./shell.d.ts" />
|
||||
/// <reference path="./experimental.d.ts" />
|
||||
/// <reference path="./sql.d.ts" />
|
||||
/// <reference path="./security.d.ts" />
|
||||
|
||||
/// <reference path="./bun.ns.d.ts" />
|
||||
|
||||
|
||||
90
packages/bun-types/overrides.d.ts
vendored
90
packages/bun-types/overrides.d.ts
vendored
@@ -174,6 +174,96 @@ declare global {
|
||||
UV_ENODATA: number;
|
||||
UV_EUNATCH: number;
|
||||
};
|
||||
binding(m: "http_parser"): {
|
||||
methods: [
|
||||
"DELETE",
|
||||
"GET",
|
||||
"HEAD",
|
||||
"POST",
|
||||
"PUT",
|
||||
"CONNECT",
|
||||
"OPTIONS",
|
||||
"TRACE",
|
||||
"COPY",
|
||||
"LOCK",
|
||||
"MKCOL",
|
||||
"MOVE",
|
||||
"PROPFIND",
|
||||
"PROPPATCH",
|
||||
"SEARCH",
|
||||
"UNLOCK",
|
||||
"BIND",
|
||||
"REBIND",
|
||||
"UNBIND",
|
||||
"ACL",
|
||||
"REPORT",
|
||||
"MKACTIVITY",
|
||||
"CHECKOUT",
|
||||
"MERGE",
|
||||
"M - SEARCH",
|
||||
"NOTIFY",
|
||||
"SUBSCRIBE",
|
||||
"UNSUBSCRIBE",
|
||||
"PATCH",
|
||||
"PURGE",
|
||||
"MKCALENDAR",
|
||||
"LINK",
|
||||
"UNLINK",
|
||||
"SOURCE",
|
||||
"QUERY",
|
||||
];
|
||||
allMethods: [
|
||||
"DELETE",
|
||||
"GET",
|
||||
"HEAD",
|
||||
"POST",
|
||||
"PUT",
|
||||
"CONNECT",
|
||||
"OPTIONS",
|
||||
"TRACE",
|
||||
"COPY",
|
||||
"LOCK",
|
||||
"MKCOL",
|
||||
"MOVE",
|
||||
"PROPFIND",
|
||||
"PROPPATCH",
|
||||
"SEARCH",
|
||||
"UNLOCK",
|
||||
"BIND",
|
||||
"REBIND",
|
||||
"UNBIND",
|
||||
"ACL",
|
||||
"REPORT",
|
||||
"MKACTIVITY",
|
||||
"CHECKOUT",
|
||||
"MERGE",
|
||||
"M - SEARCH",
|
||||
"NOTIFY",
|
||||
"SUBSCRIBE",
|
||||
"UNSUBSCRIBE",
|
||||
"PATCH",
|
||||
"PURGE",
|
||||
"MKCALENDAR",
|
||||
"LINK",
|
||||
"UNLINK",
|
||||
"SOURCE",
|
||||
"PRI",
|
||||
"DESCRIBE",
|
||||
"ANNOUNCE",
|
||||
"SETUP",
|
||||
"PLAY",
|
||||
"PAUSE",
|
||||
"TEARDOWN",
|
||||
"GET_PARAMETER",
|
||||
"SET_PARAMETER",
|
||||
"REDIRECT",
|
||||
"RECORD",
|
||||
"FLUSH",
|
||||
"QUERY",
|
||||
];
|
||||
HTTPParser: unknown;
|
||||
ConnectionsList: unknown;
|
||||
};
|
||||
binding(m: string): object;
|
||||
}
|
||||
|
||||
|
||||
101
packages/bun-types/security.d.ts
vendored
Normal file
101
packages/bun-types/security.d.ts
vendored
Normal file
@@ -0,0 +1,101 @@
|
||||
declare module "bun" {
|
||||
/**
|
||||
* `bun install` security related declarations
|
||||
*/
|
||||
export namespace Security {
|
||||
export interface Package {
|
||||
/**
|
||||
* The name of the package
|
||||
*/
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* The resolved version to be installed that matches the requested range.
|
||||
*
|
||||
* This is the exact version string, **not** a range.
|
||||
*/
|
||||
version: string;
|
||||
|
||||
/**
|
||||
* The URL of the tgz of this package that Bun will download
|
||||
*/
|
||||
tarball: string;
|
||||
|
||||
/**
|
||||
* The range that was requested by the command
|
||||
*
|
||||
* This could be a tag like `beta` or a semver range like `>=4.0.0`
|
||||
*/
|
||||
requestedRange: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Advisory represents the result of a security scan result of a package
|
||||
*/
|
||||
export interface Advisory {
|
||||
/**
|
||||
* Level represents the degree of danger for a security advisory
|
||||
*
|
||||
* Bun behaves differently depending on the values returned from the
|
||||
* {@link Scanner.scan `scan()`} hook:
|
||||
*
|
||||
* > In any case, Bun *always* pretty prints *all* the advisories,
|
||||
* > but...
|
||||
* >
|
||||
* > → if any **fatal**, Bun will immediately cancel the installation
|
||||
* > and quit with a non-zero exit code
|
||||
* >
|
||||
* > → else if any **warn**, Bun will either ask the user if they'd like
|
||||
* > to continue with the install if in a TTY environment, or
|
||||
* > immediately exit if not.
|
||||
*/
|
||||
level: "fatal" | "warn";
|
||||
|
||||
/**
|
||||
* The name of the package attempting to be installed.
|
||||
*/
|
||||
package: string;
|
||||
|
||||
/**
|
||||
* If available, this is a url linking to a CVE or report online so
|
||||
* users can learn more about the advisory.
|
||||
*/
|
||||
url: string | null;
|
||||
|
||||
/**
|
||||
* If available, this is a brief description of the advisory that Bun
|
||||
* will print to the user.
|
||||
*/
|
||||
description: string | null;
|
||||
}
|
||||
|
||||
export interface Scanner {
|
||||
/**
|
||||
* This is the version of the scanner implementation. It may change in
|
||||
* future versions, so we will use this version to discriminate between
|
||||
* such versions. It's entirely possible this API changes in the future
|
||||
* so much that version 1 would no longer be supported.
|
||||
*
|
||||
* The version is required because third-party scanner package versions
|
||||
* are inherently unrelated to Bun versions
|
||||
*/
|
||||
version: "1";
|
||||
|
||||
/**
|
||||
* Perform an advisory check when a user ran `bun add <package>
|
||||
* [...packages]` or other related/similar commands.
|
||||
*
|
||||
* If this function throws an error, Bun will immediately stop the
|
||||
* install process and print the error to the user.
|
||||
*
|
||||
* @param info An object containing an array of packages to be added.
|
||||
* The package array will contain all proposed dependencies, including
|
||||
* transitive ones. More simply, that means it will include dependencies
|
||||
* of the packages the user wants to add.
|
||||
*
|
||||
* @returns A list of advisories.
|
||||
*/
|
||||
scan: (info: { packages: Package[] }) => Promise<Advisory[]>;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
packages/bun-types/shell.d.ts
vendored
2
packages/bun-types/shell.d.ts
vendored
@@ -211,7 +211,7 @@ declare module "bun" {
|
||||
* try {
|
||||
* const result = await $`exit 1`;
|
||||
* } catch (error) {
|
||||
* if (error instanceof ShellError) {
|
||||
* if (error instanceof $.ShellError) {
|
||||
* console.log(error.exitCode); // 1
|
||||
* }
|
||||
* }
|
||||
|
||||
26
packages/bun-types/sql.d.ts
vendored
26
packages/bun-types/sql.d.ts
vendored
@@ -82,6 +82,13 @@ declare module "bun" {
|
||||
);
|
||||
}
|
||||
|
||||
class MySQLError extends SQLError {
|
||||
public readonly code: string;
|
||||
public readonly errno: number | undefined;
|
||||
public readonly sqlState: string | undefined;
|
||||
constructor(message: string, options: { code: string; errno: number | undefined; sqlState: string | undefined });
|
||||
}
|
||||
|
||||
class SQLiteError extends SQLError {
|
||||
public readonly code: string;
|
||||
public readonly errno: number;
|
||||
@@ -128,7 +135,7 @@ declare module "bun" {
|
||||
onclose?: ((err: Error | null) => void) | undefined;
|
||||
}
|
||||
|
||||
interface PostgresOptions {
|
||||
interface PostgresOrMySQLOptions {
|
||||
/**
|
||||
* Connection URL (can be string or URL object)
|
||||
*/
|
||||
@@ -196,7 +203,7 @@ declare module "bun" {
|
||||
* Database adapter/driver to use
|
||||
* @default "postgres"
|
||||
*/
|
||||
adapter?: "postgres";
|
||||
adapter?: "postgres" | "mysql" | "mariadb";
|
||||
|
||||
/**
|
||||
* Maximum time in seconds to wait for connection to become available
|
||||
@@ -265,14 +272,11 @@ declare module "bun" {
|
||||
*/
|
||||
ssl?: TLSOptions | boolean | undefined;
|
||||
|
||||
// `.path` is currently unsupported in Bun, the implementation is
|
||||
// incomplete.
|
||||
//
|
||||
// /**
|
||||
// * Unix domain socket path for connection
|
||||
// * @default ""
|
||||
// */
|
||||
// path?: string | undefined;
|
||||
/**
|
||||
* Unix domain socket path for connection
|
||||
* @default undefined
|
||||
*/
|
||||
path?: string | undefined;
|
||||
|
||||
/**
|
||||
* Callback executed when a connection attempt completes
|
||||
@@ -332,7 +336,7 @@ declare module "bun" {
|
||||
* };
|
||||
* ```
|
||||
*/
|
||||
type Options = SQLiteOptions | PostgresOptions;
|
||||
type Options = SQLiteOptions | PostgresOrMySQLOptions;
|
||||
|
||||
/**
|
||||
* Represents a SQL query that can be executed, with additional control
|
||||
|
||||
5
packages/bun-types/test-globals.d.ts
vendored
5
packages/bun-types/test-globals.d.ts
vendored
@@ -3,7 +3,7 @@
|
||||
// This file gets loaded by developers including the following triple slash directive:
|
||||
//
|
||||
// ```ts
|
||||
// /// <reference types="bun/test-globals" />
|
||||
// /// <reference types="bun-types/test-globals" />
|
||||
// ```
|
||||
|
||||
declare var test: typeof import("bun:test").test;
|
||||
@@ -19,3 +19,6 @@ declare var setDefaultTimeout: typeof import("bun:test").setDefaultTimeout;
|
||||
declare var mock: typeof import("bun:test").mock;
|
||||
declare var spyOn: typeof import("bun:test").spyOn;
|
||||
declare var jest: typeof import("bun:test").jest;
|
||||
declare var xit: typeof import("bun:test").xit;
|
||||
declare var xtest: typeof import("bun:test").xtest;
|
||||
declare var xdescribe: typeof import("bun:test").xdescribe;
|
||||
|
||||
51
packages/bun-types/test.d.ts
vendored
51
packages/bun-types/test.d.ts
vendored
@@ -152,11 +152,41 @@ declare module "bun:test" {
|
||||
type SpiedSetter<T> = JestMock.SpiedSetter<T>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a spy on an object property or method
|
||||
*/
|
||||
export function spyOn<T extends object, K extends keyof T>(
|
||||
obj: T,
|
||||
methodOrPropertyValue: K,
|
||||
): Mock<Extract<T[K], (...args: any[]) => any>>;
|
||||
|
||||
/**
|
||||
* Vitest-compatible mocking utilities
|
||||
* Provides Vitest-style mocking API for easier migration from Vitest to Bun
|
||||
*/
|
||||
export const vi: {
|
||||
/**
|
||||
* Create a mock function
|
||||
*/
|
||||
fn: typeof jest.fn;
|
||||
/**
|
||||
* Create a spy on an object property or method
|
||||
*/
|
||||
spyOn: typeof spyOn;
|
||||
/**
|
||||
* Mock a module
|
||||
*/
|
||||
module: typeof mock.module;
|
||||
/**
|
||||
* Restore all mocks to their original implementation
|
||||
*/
|
||||
restoreAllMocks: typeof jest.restoreAllMocks;
|
||||
/**
|
||||
* Clear all mock state (calls, results, etc.) without restoring original implementation
|
||||
*/
|
||||
clearAllMocks: typeof jest.clearAllMocks;
|
||||
};
|
||||
|
||||
interface FunctionLike {
|
||||
readonly name: string;
|
||||
}
|
||||
@@ -262,6 +292,15 @@ declare module "bun:test" {
|
||||
* @param fn the function that defines the tests
|
||||
*/
|
||||
export const describe: Describe;
|
||||
/**
|
||||
* Skips a group of related tests.
|
||||
*
|
||||
* This is equivalent to calling `describe.skip()`.
|
||||
*
|
||||
* @param label the label for the tests
|
||||
* @param fn the function that defines the tests
|
||||
*/
|
||||
export const xdescribe: Describe;
|
||||
/**
|
||||
* Runs a function, once, before all the tests.
|
||||
*
|
||||
@@ -515,7 +554,17 @@ declare module "bun:test" {
|
||||
* @param fn the test function
|
||||
*/
|
||||
export const test: Test;
|
||||
export { test as it };
|
||||
export { test as it, xtest as xit };
|
||||
|
||||
/**
|
||||
* Skips a test.
|
||||
*
|
||||
* This is equivalent to calling `test.skip()`.
|
||||
*
|
||||
* @param label the label for the test
|
||||
* @param fn the test function
|
||||
*/
|
||||
export const xtest: Test;
|
||||
|
||||
/**
|
||||
* Asserts that a value matches some criteria.
|
||||
|
||||
@@ -5,7 +5,9 @@ import { chmodSync, cpSync, existsSync, mkdirSync, readFileSync } from "node:fs"
|
||||
import { basename, join, relative, resolve } from "node:path";
|
||||
import {
|
||||
formatAnnotationToHtml,
|
||||
getSecret,
|
||||
isCI,
|
||||
isWindows,
|
||||
parseAnnotations,
|
||||
printEnvironment,
|
||||
reportAnnotationToBuildKite,
|
||||
@@ -214,14 +216,47 @@ function parseOptions(args, flags = []) {
|
||||
async function spawn(command, args, options, label) {
|
||||
const effectiveArgs = args.filter(Boolean);
|
||||
const description = [command, ...effectiveArgs].map(arg => (arg.includes(" ") ? JSON.stringify(arg) : arg)).join(" ");
|
||||
let env = options?.env;
|
||||
|
||||
console.log("$", description);
|
||||
|
||||
label ??= basename(command);
|
||||
|
||||
const pipe = process.env.CI === "true";
|
||||
|
||||
if (isBuildkite()) {
|
||||
if (process.env.BUN_LINK_ONLY && isWindows) {
|
||||
env ||= options?.env || { ...process.env };
|
||||
|
||||
// Pass signing secrets directly to the build process
|
||||
// The PowerShell signing script will handle certificate decoding
|
||||
env.SM_CLIENT_CERT_PASSWORD = getSecret("SM_CLIENT_CERT_PASSWORD", {
|
||||
redact: true,
|
||||
required: true,
|
||||
});
|
||||
env.SM_CLIENT_CERT_FILE = getSecret("SM_CLIENT_CERT_FILE", {
|
||||
redact: true,
|
||||
required: true,
|
||||
});
|
||||
env.SM_API_KEY = getSecret("SM_API_KEY", {
|
||||
redact: true,
|
||||
required: true,
|
||||
});
|
||||
env.SM_KEYPAIR_ALIAS = getSecret("SM_KEYPAIR_ALIAS", {
|
||||
redact: true,
|
||||
required: true,
|
||||
});
|
||||
env.SM_HOST = getSecret("SM_HOST", {
|
||||
redact: true,
|
||||
required: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const subprocess = nodeSpawn(command, effectiveArgs, {
|
||||
stdio: pipe ? "pipe" : "inherit",
|
||||
...options,
|
||||
env,
|
||||
});
|
||||
|
||||
let killedManually = false;
|
||||
|
||||
@@ -40,7 +40,25 @@ if ($args.Count -gt 0) {
|
||||
$commandArgs = @($args[1..($args.Count - 1)] | % {$_})
|
||||
}
|
||||
|
||||
Write-Host "$ $command $commandArgs"
|
||||
# Don't print the full command as it may contain sensitive information like certificates
|
||||
# Just show the command name and basic info
|
||||
$displayArgs = @()
|
||||
foreach ($arg in $commandArgs) {
|
||||
if ($arg -match "^-") {
|
||||
# Include flags
|
||||
$displayArgs += $arg
|
||||
} elseif ($arg -match "\.(mjs|js|ts|cmake|zig|cpp|c|h|exe)$") {
|
||||
# Include file names
|
||||
$displayArgs += $arg
|
||||
} elseif ($arg.Length -gt 100) {
|
||||
# Truncate long arguments (likely certificates or encoded data)
|
||||
$displayArgs += "[REDACTED]"
|
||||
} else {
|
||||
$displayArgs += $arg
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "$ $command $displayArgs"
|
||||
& $command $commandArgs
|
||||
exit $LASTEXITCODE
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ pub fn onHTMLParseError(this: *HTMLScanner, message: []const u8) void {
|
||||
this.source,
|
||||
logger.Loc.Empty,
|
||||
message,
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err);
|
||||
}
|
||||
|
||||
pub fn onTag(this: *HTMLScanner, _: *lol.Element, path: []const u8, url_attribute: []const u8, kind: ImportKind) void {
|
||||
@@ -222,7 +222,7 @@ pub fn HTMLProcessor(
|
||||
var builder = lol.HTMLRewriter.Builder.init();
|
||||
defer builder.deinit();
|
||||
|
||||
var selectors: std.BoundedArray(*lol.HTMLSelector, tag_handlers.len + if (visit_document_tags) 3 else 0) = .{};
|
||||
var selectors: bun.BoundedArray(*lol.HTMLSelector, tag_handlers.len + if (visit_document_tags) 3 else 0) = .{};
|
||||
defer for (selectors.slice()) |selector| {
|
||||
selector.deinit();
|
||||
};
|
||||
|
||||
@@ -248,7 +248,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
};
|
||||
|
||||
const source_files = serialized.sourceFileNames();
|
||||
const slices = bun.default_allocator.alloc(?[]u8, source_files.len * 2) catch bun.outOfMemory();
|
||||
const slices = bun.handleOom(bun.default_allocator.alloc(?[]u8, source_files.len * 2));
|
||||
|
||||
const file_names: [][]const u8 = @ptrCast(slices[0..source_files.len]);
|
||||
const decompressed_contents_slice = slices[source_files.len..][0..source_files.len];
|
||||
@@ -492,9 +492,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
|
||||
const page_size = std.heap.page_size_max;
|
||||
|
||||
pub const InjectOptions = struct {
|
||||
windows_hide_console: bool = false,
|
||||
};
|
||||
pub const InjectOptions = bun.options.WindowsOptions;
|
||||
|
||||
pub const CompileResult = union(enum) {
|
||||
success: void,
|
||||
@@ -515,7 +513,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
var buf: bun.PathBuffer = undefined;
|
||||
var zname: [:0]const u8 = bun.span(bun.fs.FileSystem.instance.tmpname("bun-build", &buf, @as(u64, @bitCast(std.time.milliTimestamp()))) catch |err| {
|
||||
Output.prettyErrorln("<r><red>error<r><d>:<r> failed to get temporary file name: {s}", .{@errorName(err)});
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
});
|
||||
|
||||
const cleanup = struct {
|
||||
@@ -545,7 +543,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
|
||||
bun.copyFile(in, out).unwrap() catch |err| {
|
||||
Output.prettyErrorln("<r><red>error<r><d>:<r> failed to copy bun executable into temporary file: {s}", .{@errorName(err)});
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
};
|
||||
const file = bun.sys.openFileAtWindows(
|
||||
bun.invalid_fd,
|
||||
@@ -557,7 +555,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
},
|
||||
).unwrap() catch |e| {
|
||||
Output.prettyErrorln("<r><red>error<r><d>:<r> failed to open temporary file to copy bun into\n{}", .{e});
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
};
|
||||
|
||||
break :brk file;
|
||||
@@ -600,7 +598,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
std.fs.path.sep_str,
|
||||
zname,
|
||||
&.{0},
|
||||
}) catch bun.outOfMemory();
|
||||
}) catch |e| bun.handleOom(e);
|
||||
zname = zname_z[0..zname_z.len -| 1 :0];
|
||||
continue;
|
||||
}
|
||||
@@ -611,7 +609,8 @@ pub const StandaloneModuleGraph = struct {
|
||||
}
|
||||
|
||||
Output.prettyErrorln("<r><red>error<r><d>:<r> failed to open temporary file to copy bun into\n{}", .{err});
|
||||
Global.exit(1);
|
||||
// No fd to cleanup yet, just return error
|
||||
return bun.invalid_fd;
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -633,7 +632,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
|
||||
Output.prettyErrorln("<r><red>error<r><d>:<r> failed to open bun executable to copy from as read-only\n{}", .{err});
|
||||
cleanup(zname, fd);
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -645,7 +644,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
bun.copyFile(self_fd, fd).unwrap() catch |err| {
|
||||
Output.prettyErrorln("<r><red>error<r><d>:<r> failed to copy bun executable into temporary file: {s}", .{@errorName(err)});
|
||||
cleanup(zname, fd);
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
};
|
||||
|
||||
break :brk fd;
|
||||
@@ -657,18 +656,18 @@ pub const StandaloneModuleGraph = struct {
|
||||
if (input_result.err) |err| {
|
||||
Output.prettyErrorln("Error reading standalone module graph: {}", .{err});
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
}
|
||||
var macho_file = bun.macho.MachoFile.init(bun.default_allocator, input_result.bytes.items, bytes.len) catch |err| {
|
||||
Output.prettyErrorln("Error initializing standalone module graph: {}", .{err});
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
};
|
||||
defer macho_file.deinit();
|
||||
macho_file.writeSection(bytes) catch |err| {
|
||||
Output.prettyErrorln("Error writing standalone module graph: {}", .{err});
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
};
|
||||
input_result.bytes.deinit();
|
||||
|
||||
@@ -676,7 +675,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
.err => |err| {
|
||||
Output.prettyErrorln("Error seeking to start of temporary file: {}", .{err});
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
@@ -684,19 +683,19 @@ pub const StandaloneModuleGraph = struct {
|
||||
var file = bun.sys.File{ .handle = cloned_executable_fd };
|
||||
const writer = file.writer();
|
||||
const BufferedWriter = std.io.BufferedWriter(512 * 1024, @TypeOf(writer));
|
||||
var buffered_writer = bun.default_allocator.create(BufferedWriter) catch bun.outOfMemory();
|
||||
var buffered_writer = bun.handleOom(bun.default_allocator.create(BufferedWriter));
|
||||
buffered_writer.* = .{
|
||||
.unbuffered_writer = writer,
|
||||
};
|
||||
macho_file.buildAndSign(buffered_writer.writer()) catch |err| {
|
||||
Output.prettyErrorln("Error writing standalone module graph: {}", .{err});
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
};
|
||||
buffered_writer.flush() catch |err| {
|
||||
Output.prettyErrorln("Error flushing standalone module graph: {}", .{err});
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
};
|
||||
if (comptime !Environment.isWindows) {
|
||||
_ = bun.c.fchmod(cloned_executable_fd.native(), 0o777);
|
||||
@@ -708,18 +707,18 @@ pub const StandaloneModuleGraph = struct {
|
||||
if (input_result.err) |err| {
|
||||
Output.prettyErrorln("Error reading standalone module graph: {}", .{err});
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
}
|
||||
var pe_file = bun.pe.PEFile.init(bun.default_allocator, input_result.bytes.items) catch |err| {
|
||||
Output.prettyErrorln("Error initializing PE file: {}", .{err});
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
};
|
||||
defer pe_file.deinit();
|
||||
pe_file.addBunSection(bytes) catch |err| {
|
||||
Output.prettyErrorln("Error adding Bun section to PE file: {}", .{err});
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
};
|
||||
input_result.bytes.deinit();
|
||||
|
||||
@@ -727,7 +726,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
.err => |err| {
|
||||
Output.prettyErrorln("Error seeking to start of temporary file: {}", .{err});
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
@@ -737,7 +736,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
pe_file.write(writer) catch |err| {
|
||||
Output.prettyErrorln("Error writing PE file: {}", .{err});
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
};
|
||||
// Set executable permissions when running on POSIX hosts, even for Windows targets
|
||||
if (comptime !Environment.isWindows) {
|
||||
@@ -751,7 +750,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
total_byte_count = bytes.len + 8 + (Syscall.setFileOffsetToEndWindows(cloned_executable_fd).unwrap() catch |err| {
|
||||
Output.prettyErrorln("<r><red>error<r><d>:<r> failed to seek to end of temporary file\n{}", .{err});
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
});
|
||||
} else {
|
||||
const seek_position = @as(u64, @intCast(brk: {
|
||||
@@ -760,7 +759,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
.err => |err| {
|
||||
Output.prettyErrorln("{}", .{err});
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -787,7 +786,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
},
|
||||
);
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
@@ -800,8 +799,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
.err => |err| {
|
||||
Output.prettyErrorln("<r><red>error<r><d>:<r> failed to write to temporary file\n{}", .{err});
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
|
||||
Global.exit(1);
|
||||
return bun.invalid_fd;
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -816,12 +814,42 @@ pub const StandaloneModuleGraph = struct {
|
||||
},
|
||||
}
|
||||
|
||||
if (Environment.isWindows and inject_options.windows_hide_console) {
|
||||
if (Environment.isWindows and inject_options.hide_console) {
|
||||
bun.windows.editWin32BinarySubsystem(.{ .handle = cloned_executable_fd }, .windows_gui) catch |err| {
|
||||
Output.err(err, "failed to disable console on executable", .{});
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
return bun.invalid_fd;
|
||||
};
|
||||
}
|
||||
|
||||
Global.exit(1);
|
||||
// Set Windows icon and/or metadata if any options are provided (single operation)
|
||||
if (Environment.isWindows and (inject_options.icon != null or
|
||||
inject_options.title != null or
|
||||
inject_options.publisher != null or
|
||||
inject_options.version != null or
|
||||
inject_options.description != null or
|
||||
inject_options.copyright != null))
|
||||
{
|
||||
var zname_buf: bun.OSPathBuffer = undefined;
|
||||
const zname_w = bun.strings.toWPathNormalized(&zname_buf, zname) catch |err| {
|
||||
Output.err(err, "failed to resolve executable path", .{});
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
return bun.invalid_fd;
|
||||
};
|
||||
|
||||
// Single call to set all Windows metadata at once
|
||||
bun.windows.rescle.setWindowsMetadata(
|
||||
zname_w.ptr,
|
||||
inject_options.icon,
|
||||
inject_options.title,
|
||||
inject_options.publisher,
|
||||
inject_options.version,
|
||||
inject_options.description,
|
||||
inject_options.copyright,
|
||||
) catch |err| {
|
||||
Output.err(err, "failed to set Windows metadata on executable", .{});
|
||||
cleanup(zname, cloned_executable_fd);
|
||||
return bun.invalid_fd;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -872,7 +900,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
Output.errGeneric("Failed to download {}: {s}", .{ target.*, @errorName(err) });
|
||||
},
|
||||
}
|
||||
Global.exit(1);
|
||||
return error.DownloadFailed;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -888,8 +916,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
outfile: []const u8,
|
||||
env: *bun.DotEnv.Loader,
|
||||
output_format: bun.options.Format,
|
||||
windows_hide_console: bool,
|
||||
windows_icon: ?[]const u8,
|
||||
windows_options: bun.options.WindowsOptions,
|
||||
compile_exec_argv: []const u8,
|
||||
self_exe_path: ?[]const u8,
|
||||
) !CompileResult {
|
||||
@@ -902,7 +929,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
var free_self_exe = false;
|
||||
const self_exe = if (self_exe_path) |path| brk: {
|
||||
free_self_exe = true;
|
||||
break :brk allocator.dupeZ(u8, path) catch bun.outOfMemory();
|
||||
break :brk bun.handleOom(allocator.dupeZ(u8, path));
|
||||
} else if (target.isDefault())
|
||||
bun.selfExePath() catch |err| {
|
||||
return CompileResult.fail(std.fmt.allocPrint(allocator, "failed to get self executable path: {s}", .{@errorName(err)}) catch "failed to get self executable path");
|
||||
@@ -931,7 +958,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
}
|
||||
|
||||
free_self_exe = true;
|
||||
break :blk allocator.dupeZ(u8, dest_z) catch bun.outOfMemory();
|
||||
break :blk bun.handleOom(allocator.dupeZ(u8, dest_z));
|
||||
};
|
||||
|
||||
defer if (free_self_exe) {
|
||||
@@ -941,7 +968,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
var fd = inject(
|
||||
bytes,
|
||||
self_exe,
|
||||
.{ .windows_hide_console = windows_hide_console },
|
||||
windows_options,
|
||||
target,
|
||||
);
|
||||
defer if (fd != bun.invalid_fd) fd.close();
|
||||
@@ -974,11 +1001,41 @@ pub const StandaloneModuleGraph = struct {
|
||||
fd.close();
|
||||
fd = bun.invalid_fd;
|
||||
|
||||
if (windows_icon) |icon_utf8| {
|
||||
var icon_buf: bun.OSPathBuffer = undefined;
|
||||
const icon = bun.strings.toWPathNormalized(&icon_buf, icon_utf8);
|
||||
bun.windows.rescle.setIcon(outfile_slice, icon) catch |err| {
|
||||
Output.debug("Warning: Failed to set Windows icon for executable: {s}", .{@errorName(err)});
|
||||
// Set Windows icon and/or metadata using unified function
|
||||
if (windows_options.icon != null or
|
||||
windows_options.title != null or
|
||||
windows_options.publisher != null or
|
||||
windows_options.version != null or
|
||||
windows_options.description != null or
|
||||
windows_options.copyright != null)
|
||||
{
|
||||
// Need to get the full path to the executable
|
||||
var full_path_buf: bun.OSPathBuffer = undefined;
|
||||
const full_path = brk: {
|
||||
// Get the directory path
|
||||
var dir_buf: bun.PathBuffer = undefined;
|
||||
const dir_path = bun.getFdPath(bun.FD.fromStdDir(root_dir), &dir_buf) catch |err| {
|
||||
return CompileResult.fail(std.fmt.allocPrint(allocator, "Failed to get directory path: {s}", .{@errorName(err)}) catch "Failed to get directory path");
|
||||
};
|
||||
|
||||
// Join with the outfile name
|
||||
const full_path_str = bun.path.joinAbsString(dir_path, &[_][]const u8{outfile}, .auto);
|
||||
const full_path_w = bun.strings.toWPathNormalized(&full_path_buf, full_path_str);
|
||||
const buf_u16 = bun.reinterpretSlice(u16, &full_path_buf);
|
||||
buf_u16[full_path_w.len] = 0;
|
||||
break :brk buf_u16[0..full_path_w.len :0];
|
||||
};
|
||||
|
||||
bun.windows.rescle.setWindowsMetadata(
|
||||
full_path.ptr,
|
||||
windows_options.icon,
|
||||
windows_options.title,
|
||||
windows_options.publisher,
|
||||
windows_options.version,
|
||||
windows_options.description,
|
||||
windows_options.copyright,
|
||||
) catch |err| {
|
||||
return CompileResult.fail(std.fmt.allocPrint(allocator, "Failed to set Windows metadata: {s}", .{@errorName(err)}) catch "Failed to set Windows metadata");
|
||||
};
|
||||
}
|
||||
return .success;
|
||||
@@ -1301,7 +1358,7 @@ pub const StandaloneModuleGraph = struct {
|
||||
const compressed_file = compressed_codes[@intCast(index)].slice(this.map.bytes);
|
||||
const size = bun.zstd.getDecompressedSize(compressed_file);
|
||||
|
||||
const bytes = bun.default_allocator.alloc(u8, size) catch bun.outOfMemory();
|
||||
const bytes = bun.handleOom(bun.default_allocator.alloc(u8, size));
|
||||
const result = bun.zstd.decompress(bytes, compressed_file);
|
||||
|
||||
if (result == .err) {
|
||||
@@ -1421,7 +1478,6 @@ const w = std.os.windows;
|
||||
|
||||
const bun = @import("bun");
|
||||
const Environment = bun.Environment;
|
||||
const Global = bun.Global;
|
||||
const Output = bun.Output;
|
||||
const SourceMap = bun.sourcemap;
|
||||
const StringPointer = bun.StringPointer;
|
||||
|
||||
@@ -322,7 +322,7 @@ fn appendFileAssumeCapacity(
|
||||
const watchlist_id = this.watchlist.len;
|
||||
|
||||
const file_path_: string = if (comptime clone_file_path)
|
||||
bun.asByteSlice(this.allocator.dupeZ(u8, file_path) catch bun.outOfMemory())
|
||||
bun.asByteSlice(bun.handleOom(this.allocator.dupeZ(u8, file_path)))
|
||||
else
|
||||
file_path;
|
||||
|
||||
@@ -409,7 +409,7 @@ fn appendDirectoryAssumeCapacity(
|
||||
};
|
||||
|
||||
const file_path_: string = if (comptime clone_file_path)
|
||||
bun.asByteSlice(this.allocator.dupeZ(u8, file_path) catch bun.outOfMemory())
|
||||
bun.asByteSlice(bun.handleOom(this.allocator.dupeZ(u8, file_path)))
|
||||
else
|
||||
file_path;
|
||||
|
||||
@@ -529,7 +529,7 @@ pub fn appendFileMaybeLock(
|
||||
}
|
||||
}
|
||||
}
|
||||
this.watchlist.ensureUnusedCapacity(this.allocator, 1 + @as(usize, @intCast(@intFromBool(parent_watch_item == null)))) catch bun.outOfMemory();
|
||||
bun.handleOom(this.watchlist.ensureUnusedCapacity(this.allocator, 1 + @as(usize, @intCast(@intFromBool(parent_watch_item == null)))));
|
||||
|
||||
if (autowatch_parent_dir) {
|
||||
parent_watch_item = parent_watch_item orelse switch (this.appendDirectoryAssumeCapacity(dir_fd, parent_dir, parent_dir_hash, clone_file_path)) {
|
||||
@@ -595,7 +595,7 @@ pub fn addDirectory(
|
||||
return .{ .result = @truncate(idx) };
|
||||
}
|
||||
|
||||
this.watchlist.ensureUnusedCapacity(this.allocator, 1) catch bun.outOfMemory();
|
||||
bun.handleOom(this.watchlist.ensureUnusedCapacity(this.allocator, 1));
|
||||
|
||||
return this.appendDirectoryAssumeCapacity(fd, file_path, hash, clone_file_path);
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ pub fn BSSList(comptime ValueType: type, comptime _count: anytype) type {
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator) *Self {
|
||||
if (!loaded) {
|
||||
instance = bun.default_allocator.create(Self) catch bun.outOfMemory();
|
||||
instance = bun.handleOom(bun.default_allocator.create(Self));
|
||||
// Avoid struct initialization syntax.
|
||||
// This makes Bun start about 1ms faster.
|
||||
// https://github.com/ziglang/zig/issues/24313
|
||||
@@ -330,7 +330,7 @@ pub fn BSSStringList(comptime _count: usize, comptime _item_length: usize) type
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator) *Self {
|
||||
if (!loaded) {
|
||||
instance = bun.default_allocator.create(Self) catch bun.outOfMemory();
|
||||
instance = bun.handleOom(bun.default_allocator.create(Self));
|
||||
// Avoid struct initialization syntax.
|
||||
// This makes Bun start about 1ms faster.
|
||||
// https://github.com/ziglang/zig/issues/24313
|
||||
@@ -513,7 +513,7 @@ pub fn BSSMap(comptime ValueType: type, comptime count: anytype, comptime store_
|
||||
// Avoid struct initialization syntax.
|
||||
// This makes Bun start about 1ms faster.
|
||||
// https://github.com/ziglang/zig/issues/24313
|
||||
instance = bun.default_allocator.create(Self) catch bun.outOfMemory();
|
||||
instance = bun.handleOom(bun.default_allocator.create(Self));
|
||||
instance.index = IndexMap{};
|
||||
instance.allocator = allocator;
|
||||
instance.overflow_list.zero();
|
||||
@@ -666,7 +666,7 @@ pub fn BSSMap(comptime ValueType: type, comptime count: anytype, comptime store_
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator) *Self {
|
||||
if (!instance_loaded) {
|
||||
instance = bun.default_allocator.create(Self) catch bun.outOfMemory();
|
||||
instance = bun.handleOom(bun.default_allocator.create(Self));
|
||||
// Avoid struct initialization syntax.
|
||||
// This makes Bun start about 1ms faster.
|
||||
// https://github.com/ziglang/zig/issues/24313
|
||||
|
||||
@@ -1,19 +1,24 @@
|
||||
//! AllocationScope wraps another allocator, providing leak and invalid free assertions.
|
||||
//! It also allows measuring how much memory a scope has allocated.
|
||||
//!
|
||||
//! AllocationScope is conceptually a pointer, so it can be moved without invalidating allocations.
|
||||
//! Therefore, it isn't necessary to pass an AllocationScope by pointer.
|
||||
|
||||
const AllocationScope = @This();
|
||||
const Self = @This();
|
||||
|
||||
pub const enabled = bun.Environment.enableAllocScopes;
|
||||
|
||||
parent: Allocator,
|
||||
state: if (enabled) struct {
|
||||
internal_state: if (enabled) *State else Allocator,
|
||||
|
||||
const State = struct {
|
||||
parent: Allocator,
|
||||
mutex: bun.Mutex,
|
||||
total_memory_allocated: usize,
|
||||
allocations: std.AutoHashMapUnmanaged([*]const u8, Allocation),
|
||||
frees: std.AutoArrayHashMapUnmanaged([*]const u8, Free),
|
||||
/// Once `frees` fills up, entries are overwritten from start to end.
|
||||
free_overwrite_index: std.math.IntFittingRange(0, max_free_tracking + 1),
|
||||
} else void,
|
||||
};
|
||||
|
||||
pub const max_free_tracking = 2048 - 1;
|
||||
|
||||
@@ -36,55 +41,72 @@ pub const Extra = union(enum) {
|
||||
const RefCountDebugData = @import("../ptr/ref_count.zig").DebugData;
|
||||
};
|
||||
|
||||
pub fn init(parent: Allocator) AllocationScope {
|
||||
return if (comptime enabled)
|
||||
.{
|
||||
.parent = parent,
|
||||
.state = .{
|
||||
.total_memory_allocated = 0,
|
||||
.allocations = .empty,
|
||||
.frees = .empty,
|
||||
.free_overwrite_index = 0,
|
||||
.mutex = .{},
|
||||
},
|
||||
}
|
||||
pub fn init(parent_alloc: Allocator) Self {
|
||||
const state = if (comptime enabled)
|
||||
bun.new(State, .{
|
||||
.parent = parent_alloc,
|
||||
.total_memory_allocated = 0,
|
||||
.allocations = .empty,
|
||||
.frees = .empty,
|
||||
.free_overwrite_index = 0,
|
||||
.mutex = .{},
|
||||
})
|
||||
else
|
||||
.{ .parent = parent, .state = {} };
|
||||
parent_alloc;
|
||||
return .{ .internal_state = state };
|
||||
}
|
||||
|
||||
pub fn deinit(scope: *AllocationScope) void {
|
||||
if (comptime enabled) {
|
||||
scope.state.mutex.lock();
|
||||
defer scope.state.allocations.deinit(scope.parent);
|
||||
const count = scope.state.allocations.count();
|
||||
if (count == 0) return;
|
||||
Output.errGeneric("Allocation scope leaked {d} allocations ({})", .{
|
||||
count,
|
||||
bun.fmt.size(scope.state.total_memory_allocated, .{}),
|
||||
});
|
||||
var it = scope.state.allocations.iterator();
|
||||
var n: usize = 0;
|
||||
while (it.next()) |entry| {
|
||||
Output.prettyErrorln("- {any}, len {d}, at:", .{ entry.key_ptr.*, entry.value_ptr.len });
|
||||
bun.crash_handler.dumpStackTrace(entry.value_ptr.allocated_at.trace(), trace_limits);
|
||||
pub fn deinit(scope: Self) void {
|
||||
if (comptime !enabled) return;
|
||||
|
||||
switch (entry.value_ptr.extra) {
|
||||
.none => {},
|
||||
inline else => |t| t.onAllocationLeak(@constCast(entry.key_ptr.*[0..entry.value_ptr.len])),
|
||||
}
|
||||
const state = scope.internal_state;
|
||||
state.mutex.lock();
|
||||
defer bun.destroy(state);
|
||||
defer state.allocations.deinit(state.parent);
|
||||
const count = state.allocations.count();
|
||||
if (count == 0) return;
|
||||
Output.errGeneric("Allocation scope leaked {d} allocations ({})", .{
|
||||
count,
|
||||
bun.fmt.size(state.total_memory_allocated, .{}),
|
||||
});
|
||||
var it = state.allocations.iterator();
|
||||
var n: usize = 0;
|
||||
while (it.next()) |entry| {
|
||||
Output.prettyErrorln("- {any}, len {d}, at:", .{ entry.key_ptr.*, entry.value_ptr.len });
|
||||
bun.crash_handler.dumpStackTrace(entry.value_ptr.allocated_at.trace(), trace_limits);
|
||||
|
||||
n += 1;
|
||||
if (n >= 8) {
|
||||
Output.prettyErrorln("(only showing first 10 leaks)", .{});
|
||||
break;
|
||||
}
|
||||
switch (entry.value_ptr.extra) {
|
||||
.none => {},
|
||||
inline else => |t| t.onAllocationLeak(@constCast(entry.key_ptr.*[0..entry.value_ptr.len])),
|
||||
}
|
||||
|
||||
n += 1;
|
||||
if (n >= 8) {
|
||||
Output.prettyErrorln("(only showing first 10 leaks)", .{});
|
||||
break;
|
||||
}
|
||||
Output.panic("Allocation scope leaked {}", .{bun.fmt.size(scope.state.total_memory_allocated, .{})});
|
||||
}
|
||||
Output.panic("Allocation scope leaked {}", .{bun.fmt.size(state.total_memory_allocated, .{})});
|
||||
}
|
||||
|
||||
pub fn allocator(scope: *AllocationScope) Allocator {
|
||||
return if (comptime enabled) .{ .ptr = scope, .vtable = &vtable } else scope.parent;
|
||||
pub fn allocator(scope: Self) Allocator {
|
||||
const state = scope.internal_state;
|
||||
return if (comptime enabled) .{ .ptr = state, .vtable = &vtable } else state;
|
||||
}
|
||||
|
||||
pub fn parent(scope: Self) Allocator {
|
||||
const state = scope.internal_state;
|
||||
return if (comptime enabled) state.parent else state;
|
||||
}
|
||||
|
||||
pub fn total(self: Self) usize {
|
||||
if (comptime !enabled) @compileError("AllocationScope must be enabled");
|
||||
return self.internal_state.total_memory_allocated;
|
||||
}
|
||||
|
||||
pub fn numAllocations(self: Self) usize {
|
||||
if (comptime !enabled) @compileError("AllocationScope must be enabled");
|
||||
return self.internal_state.allocations.count();
|
||||
}
|
||||
|
||||
const vtable: Allocator.VTable = .{
|
||||
@@ -107,60 +129,61 @@ pub const free_trace_limits: bun.crash_handler.WriteStackTraceLimits = .{
|
||||
};
|
||||
|
||||
fn alloc(ctx: *anyopaque, len: usize, alignment: std.mem.Alignment, 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
|
||||
const state: *State = @ptrCast(@alignCast(ctx));
|
||||
|
||||
state.mutex.lock();
|
||||
defer state.mutex.unlock();
|
||||
state.allocations.ensureUnusedCapacity(state.parent, 1) catch
|
||||
return null;
|
||||
const result = scope.parent.vtable.alloc(scope.parent.ptr, len, alignment, ret_addr) orelse
|
||||
const result = state.parent.vtable.alloc(state.parent.ptr, len, alignment, ret_addr) orelse
|
||||
return null;
|
||||
scope.trackAllocationAssumeCapacity(result[0..len], ret_addr, .none);
|
||||
trackAllocationAssumeCapacity(state, result[0..len], ret_addr, .none);
|
||||
return result;
|
||||
}
|
||||
|
||||
fn trackAllocationAssumeCapacity(scope: *AllocationScope, buf: []const u8, ret_addr: usize, extra: Extra) void {
|
||||
fn trackAllocationAssumeCapacity(state: *State, buf: []const u8, ret_addr: usize, extra: Extra) void {
|
||||
const trace = StoredTrace.capture(ret_addr);
|
||||
scope.state.allocations.putAssumeCapacityNoClobber(buf.ptr, .{
|
||||
state.allocations.putAssumeCapacityNoClobber(buf.ptr, .{
|
||||
.allocated_at = trace,
|
||||
.len = buf.len,
|
||||
.extra = extra,
|
||||
});
|
||||
scope.state.total_memory_allocated += buf.len;
|
||||
state.total_memory_allocated += buf.len;
|
||||
}
|
||||
|
||||
fn free(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, ret_addr: usize) void {
|
||||
const scope: *AllocationScope = @ptrCast(@alignCast(ctx));
|
||||
scope.state.mutex.lock();
|
||||
defer scope.state.mutex.unlock();
|
||||
const invalid = scope.trackFreeAssumeLocked(buf, ret_addr);
|
||||
const state: *State = @ptrCast(@alignCast(ctx));
|
||||
state.mutex.lock();
|
||||
defer state.mutex.unlock();
|
||||
const invalid = trackFreeAssumeLocked(state, buf, ret_addr);
|
||||
|
||||
scope.parent.vtable.free(scope.parent.ptr, buf, alignment, ret_addr);
|
||||
state.parent.vtable.free(state.parent.ptr, buf, alignment, ret_addr);
|
||||
|
||||
// If asan did not catch the free, panic now.
|
||||
if (invalid) @panic("Invalid free");
|
||||
}
|
||||
|
||||
fn trackFreeAssumeLocked(scope: *AllocationScope, buf: []const u8, ret_addr: usize) bool {
|
||||
if (scope.state.allocations.fetchRemove(buf.ptr)) |entry| {
|
||||
scope.state.total_memory_allocated -= entry.value.len;
|
||||
fn trackFreeAssumeLocked(state: *State, buf: []const u8, ret_addr: usize) bool {
|
||||
if (state.allocations.fetchRemove(buf.ptr)) |entry| {
|
||||
state.total_memory_allocated -= entry.value.len;
|
||||
|
||||
free_entry: {
|
||||
scope.state.frees.put(scope.parent, buf.ptr, .{
|
||||
state.frees.put(state.parent, buf.ptr, .{
|
||||
.allocated_at = entry.value.allocated_at,
|
||||
.freed_at = StoredTrace.capture(ret_addr),
|
||||
}) catch break :free_entry;
|
||||
// Store a limited amount of free entries
|
||||
if (scope.state.frees.count() >= max_free_tracking) {
|
||||
const i = scope.state.free_overwrite_index;
|
||||
scope.state.free_overwrite_index = @mod(scope.state.free_overwrite_index + 1, max_free_tracking);
|
||||
scope.state.frees.swapRemoveAt(i);
|
||||
if (state.frees.count() >= max_free_tracking) {
|
||||
const i = state.free_overwrite_index;
|
||||
state.free_overwrite_index = @mod(state.free_overwrite_index + 1, max_free_tracking);
|
||||
state.frees.swapRemoveAt(i);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
bun.Output.errGeneric("Invalid free, pointer {any}, len {d}", .{ buf.ptr, buf.len });
|
||||
|
||||
if (scope.state.frees.get(buf.ptr)) |free_entry_const| {
|
||||
if (state.frees.get(buf.ptr)) |free_entry_const| {
|
||||
var free_entry = free_entry_const;
|
||||
bun.Output.printErrorln("Pointer allocated here:", .{});
|
||||
bun.crash_handler.dumpStackTrace(free_entry.allocated_at.trace(), trace_limits);
|
||||
@@ -176,27 +199,29 @@ fn trackFreeAssumeLocked(scope: *AllocationScope, buf: []const u8, ret_addr: usi
|
||||
}
|
||||
}
|
||||
|
||||
pub fn assertOwned(scope: *AllocationScope, ptr: anytype) void {
|
||||
pub fn assertOwned(scope: Self, ptr: anytype) void {
|
||||
if (comptime !enabled) return;
|
||||
const cast_ptr: [*]const u8 = @ptrCast(switch (@typeInfo(@TypeOf(ptr)).pointer.size) {
|
||||
.c, .one, .many => ptr,
|
||||
.slice => if (ptr.len > 0) ptr.ptr else return,
|
||||
});
|
||||
scope.state.mutex.lock();
|
||||
defer scope.state.mutex.unlock();
|
||||
_ = scope.state.allocations.getPtr(cast_ptr) orelse
|
||||
const state = scope.internal_state;
|
||||
state.mutex.lock();
|
||||
defer state.mutex.unlock();
|
||||
_ = state.allocations.getPtr(cast_ptr) orelse
|
||||
@panic("this pointer was not owned by the allocation scope");
|
||||
}
|
||||
|
||||
pub fn assertUnowned(scope: *AllocationScope, ptr: anytype) void {
|
||||
pub fn assertUnowned(scope: Self, ptr: anytype) void {
|
||||
if (comptime !enabled) return;
|
||||
const cast_ptr: [*]const u8 = @ptrCast(switch (@typeInfo(@TypeOf(ptr)).pointer.size) {
|
||||
.c, .one, .many => ptr,
|
||||
.slice => if (ptr.len > 0) ptr.ptr else return,
|
||||
});
|
||||
scope.state.mutex.lock();
|
||||
defer scope.state.mutex.unlock();
|
||||
if (scope.state.allocations.getPtr(cast_ptr)) |owned| {
|
||||
const state = scope.internal_state;
|
||||
state.mutex.lock();
|
||||
defer state.mutex.unlock();
|
||||
if (state.allocations.getPtr(cast_ptr)) |owned| {
|
||||
Output.warn("Owned pointer allocated here:");
|
||||
bun.crash_handler.dumpStackTrace(owned.allocated_at.trace(), trace_limits, trace_limits);
|
||||
}
|
||||
@@ -205,17 +230,18 @@ pub fn assertUnowned(scope: *AllocationScope, ptr: anytype) void {
|
||||
|
||||
/// Track an arbitrary pointer. Extra data can be stored in the allocation,
|
||||
/// which will be printed when a leak is detected.
|
||||
pub fn trackExternalAllocation(scope: *AllocationScope, ptr: []const u8, ret_addr: ?usize, extra: Extra) void {
|
||||
pub fn trackExternalAllocation(scope: Self, ptr: []const u8, ret_addr: ?usize, extra: Extra) void {
|
||||
if (comptime !enabled) return;
|
||||
scope.state.mutex.lock();
|
||||
defer scope.state.mutex.unlock();
|
||||
scope.state.allocations.ensureUnusedCapacity(scope.parent, 1) catch bun.outOfMemory();
|
||||
trackAllocationAssumeCapacity(scope, ptr, ptr.len, ret_addr orelse @returnAddress(), extra);
|
||||
const state = scope.internal_state;
|
||||
state.mutex.lock();
|
||||
defer state.mutex.unlock();
|
||||
bun.handleOom(state.allocations.ensureUnusedCapacity(state.parent, 1));
|
||||
trackAllocationAssumeCapacity(state, ptr, ptr.len, ret_addr orelse @returnAddress(), extra);
|
||||
}
|
||||
|
||||
/// Call when the pointer from `trackExternalAllocation` is freed.
|
||||
/// Returns true if the free was invalid.
|
||||
pub fn trackExternalFree(scope: *AllocationScope, slice: anytype, ret_addr: ?usize) bool {
|
||||
pub fn trackExternalFree(scope: Self, slice: anytype, ret_addr: ?usize) bool {
|
||||
if (comptime !enabled) return false;
|
||||
const ptr: []const u8 = switch (@typeInfo(@TypeOf(slice))) {
|
||||
.pointer => |p| switch (p.size) {
|
||||
@@ -231,23 +257,25 @@ pub fn trackExternalFree(scope: *AllocationScope, slice: anytype, ret_addr: ?usi
|
||||
};
|
||||
// Empty slice usually means invalid pointer
|
||||
if (ptr.len == 0) return false;
|
||||
scope.state.mutex.lock();
|
||||
defer scope.state.mutex.unlock();
|
||||
return trackFreeAssumeLocked(scope, ptr, ret_addr orelse @returnAddress());
|
||||
const state = scope.internal_state;
|
||||
state.mutex.lock();
|
||||
defer state.mutex.unlock();
|
||||
return trackFreeAssumeLocked(state, ptr, ret_addr orelse @returnAddress());
|
||||
}
|
||||
|
||||
pub fn setPointerExtra(scope: *AllocationScope, ptr: *anyopaque, extra: Extra) void {
|
||||
pub fn setPointerExtra(scope: Self, ptr: *anyopaque, extra: Extra) void {
|
||||
if (comptime !enabled) return;
|
||||
scope.state.mutex.lock();
|
||||
defer scope.state.mutex.unlock();
|
||||
const allocation = scope.state.allocations.getPtr(ptr) orelse
|
||||
const state = scope.internal_state;
|
||||
state.mutex.lock();
|
||||
defer state.mutex.unlock();
|
||||
const allocation = state.allocations.getPtr(ptr) orelse
|
||||
@panic("Pointer not owned by allocation scope");
|
||||
allocation.extra = extra;
|
||||
}
|
||||
|
||||
pub inline fn downcast(a: Allocator) ?*AllocationScope {
|
||||
pub inline fn downcast(a: Allocator) ?Self {
|
||||
return if (enabled and a.vtable == &vtable)
|
||||
@ptrCast(@alignCast(a.ptr))
|
||||
.{ .internal_state = @ptrCast(@alignCast(a.ptr)) }
|
||||
else
|
||||
null;
|
||||
}
|
||||
|
||||
@@ -112,6 +112,7 @@ pub const Features = struct {
|
||||
pub var unsupported_uv_function: usize = 0;
|
||||
pub var exited: usize = 0;
|
||||
pub var yarn_migration: usize = 0;
|
||||
pub var yaml_parse: usize = 0;
|
||||
|
||||
comptime {
|
||||
@export(&napi_module_register, .{ .name = "Bun__napi_module_register_count" });
|
||||
|
||||
74
src/api/schema.d.ts
generated
vendored
74
src/api/schema.d.ts
generated
vendored
@@ -21,46 +21,58 @@ export const enum Loader {
|
||||
css = 5,
|
||||
file = 6,
|
||||
json = 7,
|
||||
toml = 8,
|
||||
wasm = 9,
|
||||
napi = 10,
|
||||
base64 = 11,
|
||||
dataurl = 12,
|
||||
text = 13,
|
||||
sqlite = 14,
|
||||
html = 15,
|
||||
jsonc = 8,
|
||||
toml = 9,
|
||||
wasm = 10,
|
||||
napi = 11,
|
||||
base64 = 12,
|
||||
dataurl = 13,
|
||||
text = 14,
|
||||
bunsh = 15,
|
||||
sqlite = 16,
|
||||
sqlite_embedded = 17,
|
||||
html = 18,
|
||||
yaml = 19,
|
||||
}
|
||||
export const LoaderKeys: {
|
||||
1: "jsx";
|
||||
jsx: "jsx";
|
||||
2: "js";
|
||||
js: "js";
|
||||
3: "ts";
|
||||
ts: "ts";
|
||||
4: "tsx";
|
||||
tsx: "tsx";
|
||||
5: "css";
|
||||
css: "css";
|
||||
6: "file";
|
||||
file: "file";
|
||||
7: "json";
|
||||
json: "json";
|
||||
8: "toml";
|
||||
toml: "toml";
|
||||
9: "wasm";
|
||||
wasm: "wasm";
|
||||
10: "napi";
|
||||
napi: "napi";
|
||||
11: "base64";
|
||||
base64: "base64";
|
||||
12: "dataurl";
|
||||
dataurl: "dataurl";
|
||||
13: "text";
|
||||
text: "text";
|
||||
14: "sqlite";
|
||||
sqlite: "sqlite";
|
||||
15: "html";
|
||||
"html": "html";
|
||||
8: "jsonc";
|
||||
9: "toml";
|
||||
10: "wasm";
|
||||
11: "napi";
|
||||
12: "base64";
|
||||
13: "dataurl";
|
||||
14: "text";
|
||||
15: "bunsh";
|
||||
16: "sqlite";
|
||||
17: "sqlite_embedded";
|
||||
18: "html";
|
||||
19: "yaml";
|
||||
jsx: 1;
|
||||
js: 2;
|
||||
ts: 3;
|
||||
tsx: 4;
|
||||
css: 5;
|
||||
file: 6;
|
||||
json: 7;
|
||||
jsonc: 8;
|
||||
toml: 9;
|
||||
wasm: 10;
|
||||
napi: 11;
|
||||
base64: 12;
|
||||
dataurl: 13;
|
||||
text: 14;
|
||||
bunsh: 15;
|
||||
sqlite: 16;
|
||||
sqlite_embedded: 17;
|
||||
html: 18;
|
||||
yaml: 19;
|
||||
};
|
||||
export const enum FrameworkEntryPointType {
|
||||
client = 1,
|
||||
|
||||
122
src/api/schema.js
generated
122
src/api/schema.js
generated
@@ -1,34 +1,42 @@
|
||||
const Loader = {
|
||||
"1": 1,
|
||||
"2": 2,
|
||||
"3": 3,
|
||||
"4": 4,
|
||||
"5": 5,
|
||||
"6": 6,
|
||||
"7": 7,
|
||||
"8": 8,
|
||||
"9": 9,
|
||||
"10": 10,
|
||||
"11": 11,
|
||||
"12": 12,
|
||||
"13": 13,
|
||||
"14": 14,
|
||||
"15": 15,
|
||||
"jsx": 1,
|
||||
"js": 2,
|
||||
"ts": 3,
|
||||
"tsx": 4,
|
||||
"css": 5,
|
||||
"file": 6,
|
||||
"json": 7,
|
||||
"toml": 8,
|
||||
"wasm": 9,
|
||||
"napi": 10,
|
||||
"base64": 11,
|
||||
"dataurl": 12,
|
||||
"text": 13,
|
||||
"sqlite": 14,
|
||||
"html": 15,
|
||||
"1": "jsx",
|
||||
"2": "js",
|
||||
"3": "ts",
|
||||
"4": "tsx",
|
||||
"5": "css",
|
||||
"6": "file",
|
||||
"7": "json",
|
||||
"8": "jsonc",
|
||||
"9": "toml",
|
||||
"10": "wasm",
|
||||
"11": "napi",
|
||||
"12": "base64",
|
||||
"13": "dataurl",
|
||||
"14": "text",
|
||||
"15": "bunsh",
|
||||
"16": "sqlite",
|
||||
"17": "sqlite_embedded",
|
||||
"18": "html",
|
||||
"19": "yaml",
|
||||
jsx: 1,
|
||||
js: 2,
|
||||
ts: 3,
|
||||
tsx: 4,
|
||||
css: 5,
|
||||
file: 6,
|
||||
json: 7,
|
||||
jsonc: 8,
|
||||
toml: 9,
|
||||
wasm: 10,
|
||||
napi: 11,
|
||||
base64: 12,
|
||||
dataurl: 13,
|
||||
text: 14,
|
||||
bunsh: 15,
|
||||
sqlite: 16,
|
||||
sqlite_embedded: 17,
|
||||
html: 18,
|
||||
yaml: 19,
|
||||
};
|
||||
const LoaderKeys = {
|
||||
"1": "jsx",
|
||||
@@ -38,29 +46,37 @@ const LoaderKeys = {
|
||||
"5": "css",
|
||||
"6": "file",
|
||||
"7": "json",
|
||||
"8": "toml",
|
||||
"9": "wasm",
|
||||
"10": "napi",
|
||||
"11": "base64",
|
||||
"12": "dataurl",
|
||||
"13": "text",
|
||||
"14": "sqlite",
|
||||
"15": "html",
|
||||
"jsx": "jsx",
|
||||
"js": "js",
|
||||
"ts": "ts",
|
||||
"tsx": "tsx",
|
||||
"css": "css",
|
||||
"file": "file",
|
||||
"json": "json",
|
||||
"toml": "toml",
|
||||
"wasm": "wasm",
|
||||
"napi": "napi",
|
||||
"base64": "base64",
|
||||
"dataurl": "dataurl",
|
||||
"text": "text",
|
||||
"sqlite": "sqlite",
|
||||
"html": "html",
|
||||
"8": "jsonc",
|
||||
"9": "toml",
|
||||
"10": "wasm",
|
||||
"11": "napi",
|
||||
"12": "base64",
|
||||
"13": "dataurl",
|
||||
"14": "text",
|
||||
"15": "bunsh",
|
||||
"16": "sqlite",
|
||||
"17": "sqlite_embedded",
|
||||
"18": "html",
|
||||
"19": "yaml",
|
||||
jsx: "jsx",
|
||||
js: "js",
|
||||
ts: "ts",
|
||||
tsx: "tsx",
|
||||
css: "css",
|
||||
file: "file",
|
||||
json: "json",
|
||||
jsonc: "jsonc",
|
||||
toml: "toml",
|
||||
wasm: "wasm",
|
||||
napi: "napi",
|
||||
base64: "base64",
|
||||
dataurl: "dataurl",
|
||||
text: "text",
|
||||
bunsh: "bunsh",
|
||||
sqlite: "sqlite",
|
||||
sqlite_embedded: "sqlite_embedded",
|
||||
html: "html",
|
||||
yaml: "yaml",
|
||||
};
|
||||
const FrameworkEntryPointType = {
|
||||
"1": 1,
|
||||
|
||||
@@ -322,22 +322,26 @@ pub const FileWriter = Writer(std.fs.File);
|
||||
|
||||
pub const api = struct {
|
||||
pub const Loader = enum(u8) {
|
||||
_none,
|
||||
jsx,
|
||||
js,
|
||||
ts,
|
||||
tsx,
|
||||
css,
|
||||
file,
|
||||
json,
|
||||
toml,
|
||||
wasm,
|
||||
napi,
|
||||
base64,
|
||||
dataurl,
|
||||
text,
|
||||
sqlite,
|
||||
html,
|
||||
_none = 255,
|
||||
jsx = 1,
|
||||
js = 2,
|
||||
ts = 3,
|
||||
tsx = 4,
|
||||
css = 5,
|
||||
file = 6,
|
||||
json = 7,
|
||||
jsonc = 8,
|
||||
toml = 9,
|
||||
wasm = 10,
|
||||
napi = 11,
|
||||
base64 = 12,
|
||||
dataurl = 13,
|
||||
text = 14,
|
||||
bunsh = 15,
|
||||
sqlite = 16,
|
||||
sqlite_embedded = 17,
|
||||
html = 18,
|
||||
yaml = 19,
|
||||
_,
|
||||
|
||||
pub fn jsonStringify(self: @This(), writer: anytype) !void {
|
||||
@@ -795,6 +799,9 @@ pub const api = struct {
|
||||
/// import_source
|
||||
import_source: []const u8,
|
||||
|
||||
/// side_effects
|
||||
side_effects: bool = false,
|
||||
|
||||
pub fn decode(reader: anytype) anyerror!Jsx {
|
||||
var this = std.mem.zeroes(Jsx);
|
||||
|
||||
@@ -803,6 +810,7 @@ pub const api = struct {
|
||||
this.fragment = try reader.readValue([]const u8);
|
||||
this.development = try reader.readValue(bool);
|
||||
this.import_source = try reader.readValue([]const u8);
|
||||
this.side_effects = try reader.readValue(bool);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -812,6 +820,7 @@ pub const api = struct {
|
||||
try writer.writeValue(@TypeOf(this.fragment), this.fragment);
|
||||
try writer.writeInt(@as(u8, @intFromBool(this.development)));
|
||||
try writer.writeValue(@TypeOf(this.import_source), this.import_source);
|
||||
try writer.writeInt(@as(u8, @intFromBool(this.side_effects)));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -2816,7 +2825,7 @@ pub const api = struct {
|
||||
token: []const u8,
|
||||
|
||||
pub fn dupe(this: NpmRegistry, allocator: std.mem.Allocator) NpmRegistry {
|
||||
const buf = allocator.alloc(u8, this.url.len + this.username.len + this.password.len + this.token.len) catch bun.outOfMemory();
|
||||
const buf = bun.handleOom(allocator.alloc(u8, this.url.len + this.username.len + this.password.len + this.token.len));
|
||||
|
||||
var out: NpmRegistry = .{
|
||||
.url = "",
|
||||
@@ -3041,6 +3050,8 @@ pub const api = struct {
|
||||
|
||||
node_linker: ?bun.install.PackageManager.Options.NodeLinker = null,
|
||||
|
||||
security_scanner: ?[]const u8 = null,
|
||||
|
||||
pub fn decode(reader: anytype) anyerror!BunInstall {
|
||||
var this = std.mem.zeroes(BunInstall);
|
||||
|
||||
|
||||
@@ -193,7 +193,7 @@ pub fn addUrlForCss(
|
||||
const encode_len = bun.base64.encodeLen(contents);
|
||||
const data_url_prefix_len = "data:".len + mime_type.len + ";base64,".len;
|
||||
const total_buffer_len = data_url_prefix_len + encode_len;
|
||||
var encoded = allocator.alloc(u8, total_buffer_len) catch bun.outOfMemory();
|
||||
var encoded = bun.handleOom(allocator.alloc(u8, total_buffer_len));
|
||||
_ = std.fmt.bufPrint(encoded[0..data_url_prefix_len], "data:{s};base64,", .{mime_type}) catch unreachable;
|
||||
const len = bun.base64.encode(encoded[data_url_prefix_len..], contents);
|
||||
break :url_for_css encoded[0 .. data_url_prefix_len + len];
|
||||
|
||||
@@ -434,7 +434,7 @@ pub const Number = struct {
|
||||
|
||||
if (Environment.isNative) {
|
||||
var buf: [124]u8 = undefined;
|
||||
return allocator.dupe(u8, bun.fmt.FormatDouble.dtoa(&buf, value)) catch bun.outOfMemory();
|
||||
return bun.handleOom(allocator.dupe(u8, bun.fmt.FormatDouble.dtoa(&buf, value)));
|
||||
} else {
|
||||
// do not attempt to implement the spec here, it would be error prone.
|
||||
}
|
||||
@@ -909,7 +909,7 @@ pub const String = struct {
|
||||
return if (bun.strings.isAllASCII(utf8))
|
||||
init(utf8)
|
||||
else
|
||||
init(bun.strings.toUTF16AllocForReal(allocator, utf8, false, false) catch bun.outOfMemory());
|
||||
init(bun.handleOom(bun.strings.toUTF16AllocForReal(allocator, utf8, false, false)));
|
||||
}
|
||||
|
||||
pub fn slice8(this: *const String) []const u8 {
|
||||
@@ -924,11 +924,11 @@ pub const String = struct {
|
||||
|
||||
pub fn resolveRopeIfNeeded(this: *String, allocator: std.mem.Allocator) void {
|
||||
if (this.next == null or !this.isUTF8()) return;
|
||||
var bytes = std.ArrayList(u8).initCapacity(allocator, this.rope_len) catch bun.outOfMemory();
|
||||
var bytes = bun.handleOom(std.ArrayList(u8).initCapacity(allocator, this.rope_len));
|
||||
bytes.appendSliceAssumeCapacity(this.data);
|
||||
var str = this.next;
|
||||
while (str) |part| {
|
||||
bytes.appendSlice(part.data) catch bun.outOfMemory();
|
||||
bun.handleOom(bytes.appendSlice(part.data));
|
||||
str = part.next;
|
||||
}
|
||||
this.data = bytes.items;
|
||||
@@ -937,7 +937,7 @@ pub const String = struct {
|
||||
|
||||
pub fn slice(this: *String, allocator: std.mem.Allocator) []const u8 {
|
||||
this.resolveRopeIfNeeded(allocator);
|
||||
return this.string(allocator) catch bun.outOfMemory();
|
||||
return bun.handleOom(this.string(allocator));
|
||||
}
|
||||
|
||||
pub var empty = String{};
|
||||
|
||||
@@ -474,7 +474,7 @@ pub inline fn isString(expr: *const Expr) bool {
|
||||
|
||||
pub inline fn asString(expr: *const Expr, allocator: std.mem.Allocator) ?string {
|
||||
switch (expr.data) {
|
||||
.e_string => |str| return str.string(allocator) catch bun.outOfMemory(),
|
||||
.e_string => |str| return bun.handleOom(str.string(allocator)),
|
||||
else => return null,
|
||||
}
|
||||
}
|
||||
@@ -3072,9 +3072,9 @@ pub const Data = union(Tag) {
|
||||
.e_null => jsc.JSValue.null,
|
||||
.e_undefined => .js_undefined,
|
||||
.e_boolean => |boolean| if (boolean.value)
|
||||
jsc.JSValue.true
|
||||
.true
|
||||
else
|
||||
jsc.JSValue.false,
|
||||
.false,
|
||||
.e_number => |e| e.toJS(),
|
||||
// .e_big_int => |e| e.toJS(ctx, exception),
|
||||
|
||||
|
||||
@@ -217,7 +217,7 @@ pub fn scan(
|
||||
result.* = alias;
|
||||
}
|
||||
strings.sortDesc(sorted);
|
||||
p.named_imports.ensureUnusedCapacity(p.allocator, sorted.len) catch bun.outOfMemory();
|
||||
bun.handleOom(p.named_imports.ensureUnusedCapacity(p.allocator, sorted.len));
|
||||
|
||||
// Create named imports for these property accesses. This will
|
||||
// cause missing imports to generate useful warnings.
|
||||
@@ -236,7 +236,7 @@ pub fn scan(
|
||||
.namespace_ref = namespace_ref,
|
||||
.import_record_index = st.import_record_index,
|
||||
},
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err);
|
||||
|
||||
const name: LocRef = item;
|
||||
const name_ref = name.ref.?;
|
||||
@@ -262,7 +262,7 @@ pub fn scan(
|
||||
p.named_imports.ensureUnusedCapacity(
|
||||
p.allocator,
|
||||
st.items.len + @as(usize, @intFromBool(st.default_name != null)) + @as(usize, @intFromBool(st.star_name_loc != null)),
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err);
|
||||
|
||||
if (st.star_name_loc) |loc| {
|
||||
record.contains_import_star = true;
|
||||
|
||||
@@ -78,7 +78,7 @@ pub fn NewStore(comptime types: []const type, comptime count: usize) type {
|
||||
pub fn init() *Store {
|
||||
log("init", .{});
|
||||
// Avoid initializing the entire struct.
|
||||
const prealloc = backing_allocator.create(PreAlloc) catch bun.outOfMemory();
|
||||
const prealloc = bun.handleOom(backing_allocator.create(PreAlloc));
|
||||
prealloc.zero();
|
||||
|
||||
return &prealloc.metadata;
|
||||
|
||||
115
src/ast/P.zig
115
src/ast/P.zig
@@ -565,7 +565,7 @@ pub fn NewParser_(
|
||||
|
||||
pub fn transposeRequire(noalias p: *P, arg: Expr, state: *const TransposeState) Expr {
|
||||
if (!p.options.features.allow_runtime) {
|
||||
const args = p.allocator.alloc(Expr, 1) catch bun.outOfMemory();
|
||||
const args = bun.handleOom(p.allocator.alloc(Expr, 1));
|
||||
args[0] = arg;
|
||||
return p.newExpr(
|
||||
E.Call{
|
||||
@@ -607,8 +607,8 @@ pub fn NewParser_(
|
||||
|
||||
// Note that this symbol may be completely removed later.
|
||||
var path_name = fs.PathName.init(path.text);
|
||||
const name = path_name.nonUniqueNameString(p.allocator) catch bun.outOfMemory();
|
||||
const namespace_ref = p.newSymbol(.other, name) catch bun.outOfMemory();
|
||||
const name = bun.handleOom(path_name.nonUniqueNameString(p.allocator));
|
||||
const namespace_ref = bun.handleOom(p.newSymbol(.other, name));
|
||||
|
||||
p.imports_to_convert_from_require.append(p.allocator, .{
|
||||
.namespace = .{
|
||||
@@ -616,8 +616,8 @@ pub fn NewParser_(
|
||||
.loc = arg.loc,
|
||||
},
|
||||
.import_record_id = import_record_index,
|
||||
}) catch bun.outOfMemory();
|
||||
p.import_items_for_namespace.put(p.allocator, namespace_ref, ImportItemForNamespaceMap.init(p.allocator)) catch bun.outOfMemory();
|
||||
}) catch |err| bun.handleOom(err);
|
||||
bun.handleOom(p.import_items_for_namespace.put(p.allocator, namespace_ref, ImportItemForNamespaceMap.init(p.allocator)));
|
||||
p.recordUsage(namespace_ref);
|
||||
|
||||
if (!state.is_require_immediately_assigned_to_decl) {
|
||||
@@ -1994,6 +1994,9 @@ pub fn NewParser_(
|
||||
p.jest.afterEach = try p.declareCommonJSSymbol(.unbound, "afterEach");
|
||||
p.jest.beforeAll = try p.declareCommonJSSymbol(.unbound, "beforeAll");
|
||||
p.jest.afterAll = try p.declareCommonJSSymbol(.unbound, "afterAll");
|
||||
p.jest.xit = try p.declareCommonJSSymbol(.unbound, "xit");
|
||||
p.jest.xtest = try p.declareCommonJSSymbol(.unbound, "xtest");
|
||||
p.jest.xdescribe = try p.declareCommonJSSymbol(.unbound, "xdescribe");
|
||||
}
|
||||
|
||||
if (p.options.features.react_fast_refresh) {
|
||||
@@ -2019,7 +2022,7 @@ pub fn NewParser_(
|
||||
fn ensureRequireSymbol(p: *P) void {
|
||||
if (p.runtime_imports.__require != null) return;
|
||||
const static_symbol = generatedSymbolName("__require");
|
||||
p.runtime_imports.__require = declareSymbolMaybeGenerated(p, .other, logger.Loc.Empty, static_symbol, true) catch bun.outOfMemory();
|
||||
p.runtime_imports.__require = bun.handleOom(declareSymbolMaybeGenerated(p, .other, logger.Loc.Empty, static_symbol, true));
|
||||
p.runtime_imports.put("__require", p.runtime_imports.__require.?);
|
||||
}
|
||||
|
||||
@@ -2232,8 +2235,8 @@ pub fn NewParser_(
|
||||
{
|
||||
p.log.level = .verbose;
|
||||
|
||||
p.log.addDebugFmt(p.source, loc, p.allocator, "Expected this scope (.{s})", .{@tagName(kind)}) catch bun.outOfMemory();
|
||||
p.log.addDebugFmt(p.source, order.loc, p.allocator, "Found this scope (.{s})", .{@tagName(order.scope.kind)}) catch bun.outOfMemory();
|
||||
bun.handleOom(p.log.addDebugFmt(p.source, loc, p.allocator, "Expected this scope (.{s})", .{@tagName(kind)}));
|
||||
bun.handleOom(p.log.addDebugFmt(p.source, order.loc, p.allocator, "Found this scope (.{s})", .{@tagName(order.scope.kind)}));
|
||||
|
||||
p.panic("Scope mismatch while visiting", .{});
|
||||
}
|
||||
@@ -2280,8 +2283,8 @@ pub fn NewParser_(
|
||||
if (p.scopes_in_order.items[last_i]) |prev_scope| {
|
||||
if (prev_scope.loc.start >= loc.start) {
|
||||
p.log.level = .verbose;
|
||||
p.log.addDebugFmt(p.source, prev_scope.loc, p.allocator, "Previous Scope", .{}) catch bun.outOfMemory();
|
||||
p.log.addDebugFmt(p.source, loc, p.allocator, "Next Scope", .{}) catch bun.outOfMemory();
|
||||
bun.handleOom(p.log.addDebugFmt(p.source, prev_scope.loc, p.allocator, "Previous Scope", .{}));
|
||||
bun.handleOom(p.log.addDebugFmt(p.source, loc, p.allocator, "Next Scope", .{}));
|
||||
p.panic("Scope location {d} must be greater than {d}", .{ loc.start, prev_scope.loc.start });
|
||||
}
|
||||
}
|
||||
@@ -2956,7 +2959,7 @@ pub fn NewParser_(
|
||||
scope: js_ast.TSNamespaceScope,
|
||||
};
|
||||
|
||||
var pair = p.allocator.create(Pair) catch bun.outOfMemory();
|
||||
var pair = bun.handleOom(p.allocator.create(Pair));
|
||||
pair.map = .{};
|
||||
pair.scope = .{
|
||||
.exported_members = &pair.map,
|
||||
@@ -3328,7 +3331,7 @@ pub fn NewParser_(
|
||||
p.allocator,
|
||||
"panic here",
|
||||
.{},
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err);
|
||||
}
|
||||
|
||||
p.log.level = .verbose;
|
||||
@@ -4529,9 +4532,9 @@ pub fn NewParser_(
|
||||
if ((symbol.kind == .ts_namespace or symbol.kind == .ts_enum) and
|
||||
!p.emitted_namespace_vars.contains(name_ref))
|
||||
{
|
||||
p.emitted_namespace_vars.putNoClobber(allocator, name_ref, {}) catch bun.outOfMemory();
|
||||
bun.handleOom(p.emitted_namespace_vars.putNoClobber(allocator, name_ref, {}));
|
||||
|
||||
var decls = allocator.alloc(G.Decl, 1) catch bun.outOfMemory();
|
||||
var decls = bun.handleOom(allocator.alloc(G.Decl, 1));
|
||||
decls[0] = G.Decl{ .binding = p.b(B.Identifier{ .ref = name_ref }, name_loc) };
|
||||
|
||||
if (p.enclosing_namespace_arg_ref == null) {
|
||||
@@ -4542,7 +4545,7 @@ pub fn NewParser_(
|
||||
.decls = G.Decl.List.init(decls),
|
||||
.is_export = is_export,
|
||||
}, stmt_loc),
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err);
|
||||
} else {
|
||||
// Nested namespace: "let"
|
||||
stmts.append(
|
||||
@@ -4550,7 +4553,7 @@ pub fn NewParser_(
|
||||
.kind = .k_let,
|
||||
.decls = G.Decl.List.init(decls),
|
||||
}, stmt_loc),
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4589,10 +4592,10 @@ pub fn NewParser_(
|
||||
}, name_loc);
|
||||
};
|
||||
|
||||
var func_args = allocator.alloc(G.Arg, 1) catch bun.outOfMemory();
|
||||
var func_args = bun.handleOom(allocator.alloc(G.Arg, 1));
|
||||
func_args[0] = .{ .binding = p.b(B.Identifier{ .ref = arg_ref }, name_loc) };
|
||||
|
||||
var args_list = allocator.alloc(ExprNodeIndex, 1) catch bun.outOfMemory();
|
||||
var args_list = bun.handleOom(allocator.alloc(ExprNodeIndex, 1));
|
||||
args_list[0] = arg_expr;
|
||||
|
||||
// TODO: if unsupported features includes arrow functions
|
||||
@@ -5463,15 +5466,15 @@ pub fn NewParser_(
|
||||
pub fn generateTempRefWithScope(p: *P, default_name: ?string, scope: *Scope) Ref {
|
||||
const name = (if (p.willUseRenamer()) default_name else null) orelse brk: {
|
||||
p.temp_ref_count += 1;
|
||||
break :brk std.fmt.allocPrint(p.allocator, "__bun_temp_ref_{x}$", .{p.temp_ref_count}) catch bun.outOfMemory();
|
||||
break :brk bun.handleOom(std.fmt.allocPrint(p.allocator, "__bun_temp_ref_{x}$", .{p.temp_ref_count}));
|
||||
};
|
||||
const ref = p.newSymbol(.other, name) catch bun.outOfMemory();
|
||||
const ref = bun.handleOom(p.newSymbol(.other, name));
|
||||
|
||||
p.temp_refs_to_declare.append(p.allocator, .{
|
||||
.ref = ref,
|
||||
}) catch bun.outOfMemory();
|
||||
}) catch |err| bun.handleOom(err);
|
||||
|
||||
scope.generated.append(p.allocator, &.{ref}) catch bun.outOfMemory();
|
||||
bun.handleOom(scope.generated.append(p.allocator, &.{ref}));
|
||||
|
||||
return ref;
|
||||
}
|
||||
@@ -5557,7 +5560,7 @@ pub fn NewParser_(
|
||||
if (decl.value) |*decl_value| {
|
||||
const value_loc = decl_value.loc;
|
||||
p.recordUsage(ctx.stack_ref);
|
||||
const args = p.allocator.alloc(Expr, 3) catch bun.outOfMemory();
|
||||
const args = bun.handleOom(p.allocator.alloc(Expr, 3));
|
||||
args[0] = Expr{
|
||||
.data = .{ .e_identifier = .{ .ref = ctx.stack_ref } },
|
||||
.loc = stmt.loc,
|
||||
@@ -5591,14 +5594,14 @@ pub fn NewParser_(
|
||||
switch (stmt.data) {
|
||||
.s_directive, .s_import, .s_export_from, .s_export_star => {
|
||||
// These can't go in a try/catch block
|
||||
result.append(stmt) catch bun.outOfMemory();
|
||||
bun.handleOom(result.append(stmt));
|
||||
continue;
|
||||
},
|
||||
|
||||
.s_class => {
|
||||
if (stmt.data.s_class.is_export) {
|
||||
// can't go in try/catch; hoist out
|
||||
result.append(stmt) catch bun.outOfMemory();
|
||||
bun.handleOom(result.append(stmt));
|
||||
continue;
|
||||
}
|
||||
},
|
||||
@@ -5609,14 +5612,14 @@ pub fn NewParser_(
|
||||
|
||||
.s_export_clause => |data| {
|
||||
// Merge export clauses together
|
||||
exports.appendSlice(data.items) catch bun.outOfMemory();
|
||||
bun.handleOom(exports.appendSlice(data.items));
|
||||
continue;
|
||||
},
|
||||
|
||||
.s_function => {
|
||||
if (should_hoist_fns) {
|
||||
// Hoist function declarations for cross-file ESM references
|
||||
result.append(stmt) catch bun.outOfMemory();
|
||||
bun.handleOom(result.append(stmt));
|
||||
continue;
|
||||
}
|
||||
},
|
||||
@@ -5635,7 +5638,7 @@ pub fn NewParser_(
|
||||
},
|
||||
.alias = p.symbols.items[identifier.ref.inner_index].original_name,
|
||||
.alias_loc = decl.binding.loc,
|
||||
}) catch bun.outOfMemory();
|
||||
}) catch |err| bun.handleOom(err);
|
||||
local.kind = .k_var;
|
||||
}
|
||||
}
|
||||
@@ -5666,12 +5669,12 @@ pub fn NewParser_(
|
||||
caught_ref,
|
||||
err_ref,
|
||||
has_err_ref,
|
||||
}) catch bun.outOfMemory();
|
||||
}) catch |err| bun.handleOom(err);
|
||||
p.declared_symbols.ensureUnusedCapacity(
|
||||
p.allocator,
|
||||
// 5 to include the _promise decl later on:
|
||||
if (ctx.has_await_using) 5 else 4,
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err);
|
||||
p.declared_symbols.appendAssumeCapacity(.{ .is_top_level = is_top_level, .ref = ctx.stack_ref });
|
||||
p.declared_symbols.appendAssumeCapacity(.{ .is_top_level = is_top_level, .ref = caught_ref });
|
||||
p.declared_symbols.appendAssumeCapacity(.{ .is_top_level = is_top_level, .ref = err_ref });
|
||||
@@ -5682,7 +5685,7 @@ pub fn NewParser_(
|
||||
p.recordUsage(ctx.stack_ref);
|
||||
p.recordUsage(err_ref);
|
||||
p.recordUsage(has_err_ref);
|
||||
const args = p.allocator.alloc(Expr, 3) catch bun.outOfMemory();
|
||||
const args = bun.handleOom(p.allocator.alloc(Expr, 3));
|
||||
args[0] = Expr{
|
||||
.data = .{ .e_identifier = .{ .ref = ctx.stack_ref } },
|
||||
.loc = loc,
|
||||
@@ -5701,7 +5704,7 @@ pub fn NewParser_(
|
||||
const finally_stmts = finally: {
|
||||
if (ctx.has_await_using) {
|
||||
const promise_ref = p.generateTempRef("_promise");
|
||||
scope.generated.append(p.allocator, &.{promise_ref}) catch bun.outOfMemory();
|
||||
bun.handleOom(scope.generated.append(p.allocator, &.{promise_ref}));
|
||||
p.declared_symbols.appendAssumeCapacity(.{ .is_top_level = is_top_level, .ref = promise_ref });
|
||||
|
||||
const promise_ref_expr = p.newExpr(E.Identifier{ .ref = promise_ref }, loc);
|
||||
@@ -5711,10 +5714,10 @@ pub fn NewParser_(
|
||||
}, loc);
|
||||
p.recordUsage(promise_ref);
|
||||
|
||||
const statements = p.allocator.alloc(Stmt, 2) catch bun.outOfMemory();
|
||||
const statements = bun.handleOom(p.allocator.alloc(Stmt, 2));
|
||||
statements[0] = p.s(S.Local{
|
||||
.decls = decls: {
|
||||
const decls = p.allocator.alloc(Decl, 1) catch bun.outOfMemory();
|
||||
const decls = bun.handleOom(p.allocator.alloc(Decl, 1));
|
||||
decls[0] = .{
|
||||
.binding = p.b(B.Identifier{ .ref = promise_ref }, loc),
|
||||
.value = call_dispose,
|
||||
@@ -5739,7 +5742,7 @@ pub fn NewParser_(
|
||||
|
||||
break :finally statements;
|
||||
} else {
|
||||
const single = p.allocator.alloc(Stmt, 1) catch bun.outOfMemory();
|
||||
const single = bun.handleOom(p.allocator.alloc(Stmt, 1));
|
||||
single[0] = p.s(S.SExpr{ .value = call_dispose }, call_dispose.loc);
|
||||
break :finally single;
|
||||
}
|
||||
@@ -5747,10 +5750,10 @@ pub fn NewParser_(
|
||||
|
||||
// Wrap everything in a try/catch/finally block
|
||||
p.recordUsage(caught_ref);
|
||||
result.ensureUnusedCapacity(2 + @as(usize, @intFromBool(exports.items.len > 0))) catch bun.outOfMemory();
|
||||
bun.handleOom(result.ensureUnusedCapacity(2 + @as(usize, @intFromBool(exports.items.len > 0))));
|
||||
result.appendAssumeCapacity(p.s(S.Local{
|
||||
.decls = decls: {
|
||||
const decls = p.allocator.alloc(Decl, 1) catch bun.outOfMemory();
|
||||
const decls = bun.handleOom(p.allocator.alloc(Decl, 1));
|
||||
decls[0] = .{
|
||||
.binding = p.b(B.Identifier{ .ref = ctx.stack_ref }, loc),
|
||||
.value = p.newExpr(E.Array{}, loc),
|
||||
@@ -5765,10 +5768,10 @@ pub fn NewParser_(
|
||||
.catch_ = .{
|
||||
.binding = p.b(B.Identifier{ .ref = caught_ref }, loc),
|
||||
.body = catch_body: {
|
||||
const statements = p.allocator.alloc(Stmt, 1) catch bun.outOfMemory();
|
||||
const statements = bun.handleOom(p.allocator.alloc(Stmt, 1));
|
||||
statements[0] = p.s(S.Local{
|
||||
.decls = decls: {
|
||||
const decls = p.allocator.alloc(Decl, 2) catch bun.outOfMemory();
|
||||
const decls = bun.handleOom(p.allocator.alloc(Decl, 2));
|
||||
decls[0] = .{
|
||||
.binding = p.b(B.Identifier{ .ref = err_ref }, loc),
|
||||
.value = p.newExpr(E.Identifier{ .ref = caught_ref }, loc),
|
||||
@@ -5824,7 +5827,7 @@ pub fn NewParser_(
|
||||
},
|
||||
.e_array => |arr| for (arr.items.slice()) |*item| {
|
||||
if (item.data != .e_string) {
|
||||
p.log.addError(p.source, item.loc, import_meta_hot_accept_err) catch bun.outOfMemory();
|
||||
bun.handleOom(p.log.addError(p.source, item.loc, import_meta_hot_accept_err));
|
||||
continue;
|
||||
}
|
||||
item.data = p.rewriteImportMetaHotAcceptString(item.data.e_string, item.loc) orelse
|
||||
@@ -5837,7 +5840,7 @@ pub fn NewParser_(
|
||||
}
|
||||
|
||||
fn rewriteImportMetaHotAcceptString(p: *P, str: *E.String, loc: logger.Loc) ?Expr.Data {
|
||||
str.toUTF8(p.allocator) catch bun.outOfMemory();
|
||||
bun.handleOom(str.toUTF8(p.allocator));
|
||||
const specifier = str.data;
|
||||
|
||||
const import_record_index = for (p.import_records.items, 0..) |import_record, i| {
|
||||
@@ -5845,7 +5848,7 @@ pub fn NewParser_(
|
||||
break i;
|
||||
}
|
||||
} else {
|
||||
p.log.addError(p.source, loc, import_meta_hot_accept_err) catch bun.outOfMemory();
|
||||
bun.handleOom(p.log.addError(p.source, loc, import_meta_hot_accept_err));
|
||||
return null;
|
||||
};
|
||||
|
||||
@@ -5917,7 +5920,7 @@ pub fn NewParser_(
|
||||
val,
|
||||
module_path,
|
||||
p.newExpr(E.String{ .data = original_name }, logger.Loc.Empty),
|
||||
}) catch bun.outOfMemory(),
|
||||
}) catch |err| bun.handleOom(err),
|
||||
}, logger.Loc.Empty);
|
||||
}
|
||||
|
||||
@@ -5949,7 +5952,7 @@ pub fn NewParser_(
|
||||
p.declared_symbols.append(p.allocator, .{
|
||||
.is_top_level = true,
|
||||
.ref = ctx_storage.*.?.signature_cb,
|
||||
}) catch bun.outOfMemory();
|
||||
}) catch |err| bun.handleOom(err);
|
||||
|
||||
break :init &(ctx_storage.*.?);
|
||||
};
|
||||
@@ -5972,7 +5975,7 @@ pub fn NewParser_(
|
||||
.e_import_identifier,
|
||||
.e_commonjs_export_identifier,
|
||||
=> |id, tag| {
|
||||
const gop = ctx.user_hooks.getOrPut(p.allocator, id.ref) catch bun.outOfMemory();
|
||||
const gop = bun.handleOom(ctx.user_hooks.getOrPut(p.allocator, id.ref));
|
||||
if (!gop.found_existing) {
|
||||
gop.value_ptr.* = .{
|
||||
.data = @unionInit(Expr.Data, @tagName(tag), id),
|
||||
@@ -5995,7 +5998,7 @@ pub fn NewParser_(
|
||||
// re-allocated entirely to fit. Only one slot of new capacity
|
||||
// is used since we know this statement list is not going to be
|
||||
// appended to afterwards; This function is a post-visit handler.
|
||||
const new_stmts = p.allocator.alloc(Stmt, stmts.items.len + 1) catch bun.outOfMemory();
|
||||
const new_stmts = bun.handleOom(p.allocator.alloc(Stmt, stmts.items.len + 1));
|
||||
@memcpy(new_stmts[1..], stmts.items);
|
||||
stmts.deinit();
|
||||
stmts.* = ListManaged(Stmt).fromOwnedSlice(p.allocator, new_stmts);
|
||||
@@ -6023,14 +6026,14 @@ pub fn NewParser_(
|
||||
.value = p.newExpr(E.Call{
|
||||
.target = Expr.initIdentifier(p.react_refresh.create_signature_ref, loc),
|
||||
}, loc),
|
||||
}}) catch bun.outOfMemory() }, loc);
|
||||
}}) catch |err| bun.handleOom(err) }, loc);
|
||||
}
|
||||
|
||||
pub fn getReactRefreshHookSignalInit(p: *P, ctx: *ReactRefresh.HookContext, function_with_hook_calls: Expr) Expr {
|
||||
const loc = logger.Loc.Empty;
|
||||
|
||||
const final = ctx.hasher.final();
|
||||
const hash_data = p.allocator.alloc(u8, comptime bun.base64.encodeLenFromSize(@sizeOf(@TypeOf(final)))) catch bun.outOfMemory();
|
||||
const hash_data = bun.handleOom(p.allocator.alloc(u8, comptime bun.base64.encodeLenFromSize(@sizeOf(@TypeOf(final)))));
|
||||
bun.assert(bun.base64.encode(hash_data, std.mem.asBytes(&final)) == hash_data.len);
|
||||
|
||||
const have_custom_hooks = ctx.user_hooks.count() > 0;
|
||||
@@ -6041,7 +6044,7 @@ pub fn NewParser_(
|
||||
2 +
|
||||
@as(usize, @intFromBool(have_force_arg)) +
|
||||
@as(usize, @intFromBool(have_custom_hooks)),
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err);
|
||||
|
||||
args[0] = function_with_hook_calls;
|
||||
args[1] = p.newExpr(E.String{ .data = hash_data }, loc);
|
||||
@@ -6056,7 +6059,7 @@ pub fn NewParser_(
|
||||
p.s(S.Return{ .value = p.newExpr(E.Array{
|
||||
.items = ExprNodeList.init(ctx.user_hooks.values()),
|
||||
}, loc) }, loc),
|
||||
}) catch bun.outOfMemory(),
|
||||
}) catch |err| bun.handleOom(err),
|
||||
.loc = loc,
|
||||
},
|
||||
.prefer_expr = true,
|
||||
@@ -6195,7 +6198,7 @@ pub fn NewParser_(
|
||||
// })
|
||||
//
|
||||
// which is then called in `evaluateCommonJSModuleOnce`
|
||||
var args = allocator.alloc(Arg, 5 + @as(usize, @intFromBool(p.has_import_meta))) catch bun.outOfMemory();
|
||||
var args = bun.handleOom(allocator.alloc(Arg, 5 + @as(usize, @intFromBool(p.has_import_meta))));
|
||||
args[0..5].* = .{
|
||||
Arg{ .binding = p.b(B.Identifier{ .ref = p.exports_ref }, logger.Loc.Empty) },
|
||||
Arg{ .binding = p.b(B.Identifier{ .ref = p.require_ref }, logger.Loc.Empty) },
|
||||
@@ -6204,7 +6207,7 @@ pub fn NewParser_(
|
||||
Arg{ .binding = p.b(B.Identifier{ .ref = p.dirname_ref }, logger.Loc.Empty) },
|
||||
};
|
||||
if (p.has_import_meta) {
|
||||
p.import_meta_ref = p.newSymbol(.other, "$Bun_import_meta") catch bun.outOfMemory();
|
||||
p.import_meta_ref = bun.handleOom(p.newSymbol(.other, "$Bun_import_meta"));
|
||||
args[5] = Arg{ .binding = p.b(B.Identifier{ .ref = p.import_meta_ref }, logger.Loc.Empty) };
|
||||
}
|
||||
|
||||
@@ -6220,7 +6223,7 @@ pub fn NewParser_(
|
||||
|
||||
total_stmts_count += @as(usize, @intCast(@intFromBool(preserve_strict_mode)));
|
||||
|
||||
const stmts_to_copy = allocator.alloc(Stmt, total_stmts_count) catch bun.outOfMemory();
|
||||
const stmts_to_copy = bun.handleOom(allocator.alloc(Stmt, total_stmts_count));
|
||||
{
|
||||
var remaining_stmts = stmts_to_copy;
|
||||
if (preserve_strict_mode) {
|
||||
@@ -6254,7 +6257,7 @@ pub fn NewParser_(
|
||||
logger.Loc.Empty,
|
||||
);
|
||||
|
||||
var top_level_stmts = p.allocator.alloc(Stmt, 1) catch bun.outOfMemory();
|
||||
var top_level_stmts = bun.handleOom(p.allocator.alloc(Stmt, 1));
|
||||
top_level_stmts[0] = p.s(
|
||||
S.SExpr{
|
||||
.value = wrapper,
|
||||
@@ -6334,8 +6337,8 @@ pub fn NewParser_(
|
||||
p.allocator,
|
||||
"require_{any}",
|
||||
.{p.source.fmtIdentifier()},
|
||||
) catch bun.outOfMemory(),
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err),
|
||||
) catch |err| bun.handleOom(err);
|
||||
}
|
||||
|
||||
break :brk Ref.None;
|
||||
|
||||
@@ -446,7 +446,7 @@ pub const Parser = struct {
|
||||
if (p.options.bundle) {
|
||||
// The bundler requires a part for generated module wrappers. This
|
||||
// part must be at the start as it is referred to by index.
|
||||
before.append(js_ast.Part{}) catch bun.outOfMemory();
|
||||
bun.handleOom(before.append(js_ast.Part{}));
|
||||
}
|
||||
|
||||
// --inspect-brk
|
||||
@@ -460,7 +460,7 @@ pub const Parser = struct {
|
||||
js_ast.Part{
|
||||
.stmts = debugger_stmts,
|
||||
},
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err);
|
||||
}
|
||||
|
||||
// When "using" declarations appear at the top level, we change all TDZ
|
||||
@@ -713,7 +713,7 @@ pub const Parser = struct {
|
||||
var import_part_stmts = remaining_stmts[0..1];
|
||||
remaining_stmts = remaining_stmts[1..];
|
||||
|
||||
p.module_scope.generated.push(p.allocator, deferred_import.namespace.ref.?) catch bun.outOfMemory();
|
||||
bun.handleOom(p.module_scope.generated.push(p.allocator, deferred_import.namespace.ref.?));
|
||||
|
||||
import_part_stmts[0] = Stmt.alloc(
|
||||
S.Import,
|
||||
|
||||
@@ -347,7 +347,7 @@ pub const SideEffects = enum(u1) {
|
||||
const stack_bottom = stack.items.len;
|
||||
defer stack.shrinkRetainingCapacity(stack_bottom);
|
||||
|
||||
stack.append(.{ .bin = expr.data.e_binary }) catch bun.outOfMemory();
|
||||
bun.handleOom(stack.append(.{ .bin = expr.data.e_binary }));
|
||||
|
||||
// Build stack up of expressions
|
||||
var left: Expr = expr.data.e_binary.left;
|
||||
@@ -357,7 +357,7 @@ pub const SideEffects = enum(u1) {
|
||||
.bin_strict_ne,
|
||||
.bin_comma,
|
||||
=> {
|
||||
stack.append(.{ .bin = left_bin }) catch bun.outOfMemory();
|
||||
bun.handleOom(stack.append(.{ .bin = left_bin }));
|
||||
left = left_bin.left;
|
||||
},
|
||||
else => break,
|
||||
|
||||
@@ -182,7 +182,7 @@ pub fn foldStringAddition(l: Expr, r: Expr, allocator: std.mem.Allocator, kind:
|
||||
allocator,
|
||||
E.TemplatePart,
|
||||
&.{ left.parts, right.parts },
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err);
|
||||
return lhs;
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -459,12 +459,12 @@ pub fn AstMaybe(
|
||||
p.allocator,
|
||||
id.ref,
|
||||
.{},
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err);
|
||||
const inner_use = gop.value_ptr.getOrPutValue(
|
||||
p.allocator,
|
||||
name,
|
||||
.{},
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err);
|
||||
inner_use.value_ptr.count_estimate += 1;
|
||||
}
|
||||
},
|
||||
@@ -572,8 +572,8 @@ pub fn AstMaybe(
|
||||
p.allocator,
|
||||
"import.meta.hot.{s} does not exist",
|
||||
.{name},
|
||||
) catch bun.outOfMemory(),
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err),
|
||||
) catch |err| bun.handleOom(err);
|
||||
return .{ .data = .e_undefined, .loc = loc };
|
||||
}
|
||||
},
|
||||
|
||||
@@ -347,7 +347,7 @@ pub fn ParseProperty(
|
||||
// Handle invalid identifiers in property names
|
||||
// https://github.com/oven-sh/bun/issues/12039
|
||||
if (p.lexer.token == .t_syntax_error) {
|
||||
p.log.addRangeErrorFmt(p.source, name_range, p.allocator, "Unexpected {}", .{bun.fmt.quote(name)}) catch bun.outOfMemory();
|
||||
bun.handleOom(p.log.addRangeErrorFmt(p.source, name_range, p.allocator, "Unexpected {}", .{bun.fmt.quote(name)}));
|
||||
return error.SyntaxError;
|
||||
}
|
||||
|
||||
|
||||
@@ -376,8 +376,8 @@ pub fn ParseStmt(
|
||||
.{
|
||||
path_name.fmtIdentifier(),
|
||||
},
|
||||
) catch bun.outOfMemory(),
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err),
|
||||
) catch |err| bun.handleOom(err);
|
||||
|
||||
if (comptime track_symbol_usage_during_parse_pass) {
|
||||
// In the scan pass, we need _some_ way of knowing *not* to mark as unused
|
||||
|
||||
@@ -210,7 +210,7 @@ pub fn ParseTypescript(
|
||||
p.popScope();
|
||||
|
||||
if (!opts.is_typescript_declare) {
|
||||
name.ref = p.declareSymbol(.ts_namespace, name_loc, name_text) catch bun.outOfMemory();
|
||||
name.ref = bun.handleOom(p.declareSymbol(.ts_namespace, name_loc, name_text));
|
||||
try p.ref_to_ts_namespace_member.put(p.allocator, name.ref.?, ns_member_data);
|
||||
}
|
||||
|
||||
@@ -288,7 +288,7 @@ pub fn ParseTypescript(
|
||||
name.ref = try p.declareSymbol(.ts_enum, name_loc, name_text);
|
||||
_ = try p.pushScopeForParsePass(.entry, loc);
|
||||
p.current_scope.ts_namespace = ts_namespace;
|
||||
p.ref_to_ts_namespace_member.putNoClobber(p.allocator, name.ref.?, enum_member_data) catch bun.outOfMemory();
|
||||
bun.handleOom(p.ref_to_ts_namespace_member.putNoClobber(p.allocator, name.ref.?, enum_member_data));
|
||||
}
|
||||
|
||||
try p.lexer.expect(.t_open_brace);
|
||||
@@ -329,7 +329,7 @@ pub fn ParseTypescript(
|
||||
exported_members.put(p.allocator, value.name, .{
|
||||
.loc = value.loc,
|
||||
.data = .enum_property,
|
||||
}) catch bun.outOfMemory();
|
||||
}) catch |err| bun.handleOom(err);
|
||||
|
||||
if (p.lexer.token != .t_comma and p.lexer.token != .t_semicolon) {
|
||||
break;
|
||||
@@ -376,7 +376,7 @@ pub fn ParseTypescript(
|
||||
} else {
|
||||
arg_ref = p.declareSymbol(.hoisted, name_loc, name_text) catch unreachable;
|
||||
}
|
||||
p.ref_to_ts_namespace_member.put(p.allocator, arg_ref, enum_member_data) catch bun.outOfMemory();
|
||||
bun.handleOom(p.ref_to_ts_namespace_member.put(p.allocator, arg_ref, enum_member_data));
|
||||
ts_namespace.arg_ref = arg_ref;
|
||||
|
||||
p.popScope();
|
||||
@@ -406,7 +406,7 @@ pub fn ParseTypescript(
|
||||
if (i != null) count += 1;
|
||||
}
|
||||
|
||||
const items = p.allocator.alloc(ScopeOrder, count) catch bun.outOfMemory();
|
||||
const items = bun.handleOom(p.allocator.alloc(ScopeOrder, count));
|
||||
var i: usize = 0;
|
||||
for (p.scopes_in_order.items[scope_index..]) |item| {
|
||||
items[i] = item orelse continue;
|
||||
@@ -414,7 +414,7 @@ pub fn ParseTypescript(
|
||||
}
|
||||
break :scope_order_clone items;
|
||||
},
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err);
|
||||
|
||||
return p.s(S.Enum{
|
||||
.name = name,
|
||||
|
||||
@@ -860,11 +860,11 @@ pub fn Visit(
|
||||
// Merge the two identifiers back into a single one
|
||||
p.symbols.items[hoisted_ref.innerIndex()].link = name_ref;
|
||||
}
|
||||
non_fn_stmts.append(stmt) catch bun.outOfMemory();
|
||||
bun.handleOom(non_fn_stmts.append(stmt));
|
||||
continue;
|
||||
}
|
||||
|
||||
const gpe = fn_stmts.getOrPut(name_ref) catch bun.outOfMemory();
|
||||
const gpe = bun.handleOom(fn_stmts.getOrPut(name_ref));
|
||||
var index = gpe.value_ptr.*;
|
||||
if (!gpe.found_existing) {
|
||||
index = @as(u32, @intCast(let_decls.items.len));
|
||||
@@ -889,7 +889,7 @@ pub fn Visit(
|
||||
},
|
||||
data.func.name.?.loc,
|
||||
),
|
||||
}) catch bun.outOfMemory();
|
||||
}) catch |err| bun.handleOom(err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -257,7 +257,7 @@ pub fn VisitExpr(
|
||||
.target = if (runtime == .classic) target else p.jsxImport(.createElement, expr.loc),
|
||||
.args = ExprNodeList.init(args[0..i]),
|
||||
// Enable tree shaking
|
||||
.can_be_unwrapped_if_unused = if (!p.options.ignore_dce_annotations) .if_unused else .never,
|
||||
.can_be_unwrapped_if_unused = if (!p.options.ignore_dce_annotations and !p.options.jsx.side_effects) .if_unused else .never,
|
||||
.close_paren_loc = e_.close_tag_loc,
|
||||
}, expr.loc);
|
||||
}
|
||||
@@ -311,12 +311,12 @@ pub fn VisitExpr(
|
||||
.items = e_.children,
|
||||
.is_single_line = e_.children.len < 2,
|
||||
}, e_.close_tag_loc),
|
||||
}) catch bun.outOfMemory();
|
||||
}) catch |err| bun.handleOom(err);
|
||||
} else if (e_.children.len == 1) {
|
||||
props.append(allocator, G.Property{
|
||||
.key = children_key,
|
||||
.value = e_.children.ptr[0],
|
||||
}) catch bun.outOfMemory();
|
||||
}) catch |err| bun.handleOom(err);
|
||||
}
|
||||
|
||||
// Either:
|
||||
@@ -362,7 +362,7 @@ pub fn VisitExpr(
|
||||
.target = p.jsxImportAutomatic(expr.loc, is_static_jsx),
|
||||
.args = ExprNodeList.init(args),
|
||||
// Enable tree shaking
|
||||
.can_be_unwrapped_if_unused = if (!p.options.ignore_dce_annotations) .if_unused else .never,
|
||||
.can_be_unwrapped_if_unused = if (!p.options.ignore_dce_annotations and !p.options.jsx.side_effects) .if_unused else .never,
|
||||
.was_jsx_element = true,
|
||||
.close_paren_loc = e_.close_tag_loc,
|
||||
}, expr.loc);
|
||||
@@ -490,7 +490,7 @@ pub fn VisitExpr(
|
||||
// Note that we only append to the stack (and therefore allocate memory
|
||||
// on the heap) when there are nested binary expressions. A single binary
|
||||
// expression doesn't add anything to the stack.
|
||||
p.binary_expression_stack.append(v) catch bun.outOfMemory();
|
||||
bun.handleOom(p.binary_expression_stack.append(v));
|
||||
v = BinaryExpressionVisitor{
|
||||
.e = left_binary.?,
|
||||
.loc = left.loc,
|
||||
@@ -1449,8 +1449,8 @@ pub fn VisitExpr(
|
||||
p.allocator,
|
||||
"\"useState\" is not available in a server component. If you need interactivity, consider converting part of this to a Client Component (by adding `\"use client\";` to the top of the file).",
|
||||
.{},
|
||||
) catch bun.outOfMemory(),
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err),
|
||||
) catch |err| bun.handleOom(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1542,7 +1542,7 @@ pub fn VisitExpr(
|
||||
|
||||
if (react_hook_data) |*hook| try_mark_hook: {
|
||||
const stmts = p.nearest_stmt_list orelse break :try_mark_hook;
|
||||
stmts.append(p.getReactRefreshHookSignalDecl(hook.signature_cb)) catch bun.outOfMemory();
|
||||
bun.handleOom(stmts.append(p.getReactRefreshHookSignalDecl(hook.signature_cb)));
|
||||
|
||||
p.handleReactRefreshPostVisitFunctionBody(&stmts_list, hook);
|
||||
e_.body.stmts = stmts_list.items;
|
||||
@@ -1568,7 +1568,7 @@ pub fn VisitExpr(
|
||||
|
||||
if (react_hook_data) |*hook| try_mark_hook: {
|
||||
const stmts = p.nearest_stmt_list orelse break :try_mark_hook;
|
||||
stmts.append(p.getReactRefreshHookSignalDecl(hook.signature_cb)) catch bun.outOfMemory();
|
||||
bun.handleOom(stmts.append(p.getReactRefreshHookSignalDecl(hook.signature_cb)));
|
||||
final_expr = p.getReactRefreshHookSignalInit(hook, expr);
|
||||
}
|
||||
|
||||
|
||||
@@ -287,7 +287,7 @@ pub fn VisitStmt(
|
||||
if (p.current_scope.parent == null and p.will_wrap_module_in_try_catch_for_using) {
|
||||
try stmts.ensureUnusedCapacity(2);
|
||||
|
||||
const decls = p.allocator.alloc(G.Decl, 1) catch bun.outOfMemory();
|
||||
const decls = bun.handleOom(p.allocator.alloc(G.Decl, 1));
|
||||
decls[0] = .{
|
||||
.binding = p.b(B.Identifier{ .ref = data.default_name.ref.? }, data.default_name.loc),
|
||||
.value = data.value.expr,
|
||||
@@ -295,7 +295,7 @@ pub fn VisitStmt(
|
||||
stmts.appendAssumeCapacity(p.s(S.Local{
|
||||
.decls = G.Decl.List.init(decls),
|
||||
}, stmt.loc));
|
||||
const items = p.allocator.alloc(js_ast.ClauseItem, 1) catch bun.outOfMemory();
|
||||
const items = bun.handleOom(p.allocator.alloc(js_ast.ClauseItem, 1));
|
||||
items[0] = js_ast.ClauseItem{
|
||||
.alias = "default",
|
||||
.alias_loc = data.default_name.loc,
|
||||
@@ -343,7 +343,7 @@ pub fn VisitStmt(
|
||||
}
|
||||
|
||||
if (react_hook_data) |*hook| {
|
||||
stmts.append(p.getReactRefreshHookSignalDecl(hook.signature_cb)) catch bun.outOfMemory();
|
||||
bun.handleOom(stmts.append(p.getReactRefreshHookSignalDecl(hook.signature_cb)));
|
||||
|
||||
data.value = .{
|
||||
.expr = p.getReactRefreshHookSignalInit(hook, p.newExpr(
|
||||
@@ -402,7 +402,7 @@ pub fn VisitStmt(
|
||||
.value = data.value.expr,
|
||||
},
|
||||
}),
|
||||
}, stmt.loc)) catch bun.outOfMemory();
|
||||
}, stmt.loc)) catch |err| bun.handleOom(err);
|
||||
|
||||
data.value = .{ .expr = .initIdentifier(ref_to_use, stmt.loc) };
|
||||
|
||||
@@ -515,7 +515,7 @@ pub fn VisitStmt(
|
||||
data.func.flags.remove(.is_export);
|
||||
|
||||
const enclosing_namespace_arg_ref = p.enclosing_namespace_arg_ref orelse bun.outOfMemory();
|
||||
stmts.ensureUnusedCapacity(3) catch bun.outOfMemory();
|
||||
bun.handleOom(stmts.ensureUnusedCapacity(3));
|
||||
stmts.appendAssumeCapacity(stmt.*);
|
||||
stmts.appendAssumeCapacity(Stmt.assign(
|
||||
p.newExpr(E.Dot{
|
||||
@@ -547,7 +547,7 @@ pub fn VisitStmt(
|
||||
}}),
|
||||
}, stmt.loc));
|
||||
} else {
|
||||
stmts.append(stmt.*) catch bun.outOfMemory();
|
||||
bun.handleOom(stmts.append(stmt.*));
|
||||
}
|
||||
} else if (mark_as_dead) {
|
||||
if (p.options.features.replace_exports.getPtr(original_name)) |replacement| {
|
||||
@@ -1200,7 +1200,7 @@ pub fn VisitStmt(
|
||||
const first = p.s(S.Local{
|
||||
.kind = init2.kind,
|
||||
.decls = bindings: {
|
||||
const decls = p.allocator.alloc(G.Decl, 1) catch bun.outOfMemory();
|
||||
const decls = bun.handleOom(p.allocator.alloc(G.Decl, 1));
|
||||
decls[0] = .{
|
||||
.binding = p.b(B.Identifier{ .ref = id.ref }, loc),
|
||||
.value = p.newExpr(E.Identifier{ .ref = temp_ref }, loc),
|
||||
@@ -1210,7 +1210,7 @@ pub fn VisitStmt(
|
||||
}, loc);
|
||||
|
||||
const length = if (data.body.data == .s_block) data.body.data.s_block.stmts.len else 1;
|
||||
const statements = p.allocator.alloc(Stmt, 1 + length) catch bun.outOfMemory();
|
||||
const statements = bun.handleOom(p.allocator.alloc(Stmt, 1 + length));
|
||||
statements[0] = first;
|
||||
if (data.body.data == .s_block) {
|
||||
@memcpy(statements[1..], data.body.data.s_block.stmts);
|
||||
@@ -1315,10 +1315,10 @@ pub fn VisitStmt(
|
||||
try p.top_level_enums.append(p.allocator, data.name.ref.?);
|
||||
}
|
||||
|
||||
p.recordDeclaredSymbol(data.name.ref.?) catch bun.outOfMemory();
|
||||
p.pushScopeForVisitPass(.entry, stmt.loc) catch bun.outOfMemory();
|
||||
bun.handleOom(p.recordDeclaredSymbol(data.name.ref.?));
|
||||
bun.handleOom(p.pushScopeForVisitPass(.entry, stmt.loc));
|
||||
defer p.popScope();
|
||||
p.recordDeclaredSymbol(data.arg) catch bun.outOfMemory();
|
||||
bun.handleOom(p.recordDeclaredSymbol(data.arg));
|
||||
|
||||
const allocator = p.allocator;
|
||||
// Scan ahead for any variables inside this namespace. This must be done
|
||||
@@ -1327,7 +1327,7 @@ pub fn VisitStmt(
|
||||
// We need to convert the uses into property accesses on the namespace.
|
||||
for (data.values) |value| {
|
||||
if (value.ref.isValid()) {
|
||||
p.is_exported_inside_namespace.put(allocator, value.ref, data.arg) catch bun.outOfMemory();
|
||||
bun.handleOom(p.is_exported_inside_namespace.put(allocator, value.ref, data.arg));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1336,7 +1336,7 @@ pub fn VisitStmt(
|
||||
// without initializers are initialized to undefined.
|
||||
var next_numeric_value: ?f64 = 0.0;
|
||||
|
||||
var value_exprs = ListManaged(Expr).initCapacity(allocator, data.values.len) catch bun.outOfMemory();
|
||||
var value_exprs = bun.handleOom(ListManaged(Expr).initCapacity(allocator, data.values.len));
|
||||
|
||||
var all_values_are_pure = true;
|
||||
|
||||
@@ -1373,7 +1373,7 @@ pub fn VisitStmt(
|
||||
p.allocator,
|
||||
value.ref,
|
||||
.{ .enum_number = num.value },
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err);
|
||||
|
||||
next_numeric_value = num.value + 1.0;
|
||||
},
|
||||
@@ -1386,7 +1386,7 @@ pub fn VisitStmt(
|
||||
p.allocator,
|
||||
value.ref,
|
||||
.{ .enum_string = str },
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err);
|
||||
},
|
||||
else => {
|
||||
if (visited.knownPrimitive() == .string) {
|
||||
@@ -1409,7 +1409,7 @@ pub fn VisitStmt(
|
||||
p.allocator,
|
||||
value.ref,
|
||||
.{ .enum_number = num },
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err);
|
||||
} else {
|
||||
value.value = p.newExpr(E.Undefined{}, value.loc);
|
||||
}
|
||||
@@ -1451,7 +1451,7 @@ pub fn VisitStmt(
|
||||
|
||||
// String-valued enums do not form a two-way map
|
||||
if (has_string_value) {
|
||||
value_exprs.append(assign_target) catch bun.outOfMemory();
|
||||
bun.handleOom(value_exprs.append(assign_target));
|
||||
} else {
|
||||
// "Enum[assignTarget] = 'Name'"
|
||||
value_exprs.append(
|
||||
@@ -1465,7 +1465,7 @@ pub fn VisitStmt(
|
||||
}, value.loc),
|
||||
name_as_e_string.?,
|
||||
),
|
||||
) catch bun.outOfMemory();
|
||||
) catch |err| bun.handleOom(err);
|
||||
p.recordUsage(data.arg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ pub const StringRefList = struct {
|
||||
pub const empty: StringRefList = .{ .strings = .{} };
|
||||
|
||||
pub fn track(al: *StringRefList, str: ZigString.Slice) []const u8 {
|
||||
al.strings.append(bun.default_allocator, str) catch bun.outOfMemory();
|
||||
bun.handleOom(al.strings.append(bun.default_allocator, str));
|
||||
return str.slice();
|
||||
}
|
||||
|
||||
@@ -261,7 +261,7 @@ pub const Framework = struct {
|
||||
.{ .code = bun.runtimeEmbedFile(.src, "bake/bun-framework-react/client.tsx") },
|
||||
.{ .code = bun.runtimeEmbedFile(.src, "bake/bun-framework-react/server.tsx") },
|
||||
.{ .code = bun.runtimeEmbedFile(.src, "bake/bun-framework-react/ssr.tsx") },
|
||||
}) catch bun.outOfMemory(),
|
||||
}) catch |err| bun.handleOom(err),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -41,7 +41,7 @@ pub fn replacePath(
|
||||
) !EntryIndex {
|
||||
assert(assets.owner().magic == .valid);
|
||||
defer assert(assets.files.count() == assets.refs.items.len);
|
||||
const alloc = assets.owner().allocator;
|
||||
const alloc = assets.owner().allocator();
|
||||
debug.log("replacePath {} {} - {s}/{s} ({s})", .{
|
||||
bun.fmt.quote(abs_path),
|
||||
content_hash,
|
||||
@@ -100,9 +100,9 @@ pub fn replacePath(
|
||||
/// means there is already data here.
|
||||
pub fn putOrIncrementRefCount(assets: *Assets, content_hash: u64, ref_count: u32) !?**StaticRoute {
|
||||
defer assert(assets.files.count() == assets.refs.items.len);
|
||||
const file_index_gop = try assets.files.getOrPut(assets.owner().allocator, content_hash);
|
||||
const file_index_gop = try assets.files.getOrPut(assets.owner().allocator(), content_hash);
|
||||
if (!file_index_gop.found_existing) {
|
||||
try assets.refs.append(assets.owner().allocator, ref_count);
|
||||
try assets.refs.append(assets.owner().allocator(), ref_count);
|
||||
return file_index_gop.value_ptr;
|
||||
} else {
|
||||
assets.refs.items[file_index_gop.index] += ref_count;
|
||||
|
||||
19
src/bake/DevServer/DevAllocator.zig
Normal file
19
src/bake/DevServer/DevAllocator.zig
Normal file
@@ -0,0 +1,19 @@
|
||||
const Self = @This();
|
||||
|
||||
maybe_scope: if (AllocationScope.enabled) AllocationScope else void,
|
||||
|
||||
pub fn get(self: Self) Allocator {
|
||||
return if (comptime AllocationScope.enabled)
|
||||
self.maybe_scope.allocator()
|
||||
else
|
||||
bun.default_allocator;
|
||||
}
|
||||
|
||||
pub fn scope(self: Self) ?AllocationScope {
|
||||
return if (comptime AllocationScope.enabled) self.maybe_scope else null;
|
||||
}
|
||||
|
||||
const bun = @import("bun");
|
||||
const std = @import("std");
|
||||
const AllocationScope = bun.allocators.AllocationScope;
|
||||
const Allocator = std.mem.Allocator;
|
||||
@@ -47,6 +47,7 @@ pub fn trackResolutionFailure(store: *DirectoryWatchStore, import_source: []cons
|
||||
.json,
|
||||
.jsonc,
|
||||
.toml,
|
||||
.yaml,
|
||||
.wasm,
|
||||
.napi,
|
||||
.base64,
|
||||
@@ -100,14 +101,14 @@ fn insert(
|
||||
});
|
||||
|
||||
if (store.dependencies_free_list.items.len == 0)
|
||||
try store.dependencies.ensureUnusedCapacity(dev.allocator, 1);
|
||||
try store.dependencies.ensureUnusedCapacity(dev.allocator(), 1);
|
||||
|
||||
const gop = try store.watches.getOrPut(dev.allocator, bun.strings.withoutTrailingSlashWindowsPath(dir_name_to_watch));
|
||||
const gop = try store.watches.getOrPut(dev.allocator(), bun.strings.withoutTrailingSlashWindowsPath(dir_name_to_watch));
|
||||
const specifier_cloned = if (specifier[0] == '.' or std.fs.path.isAbsolute(specifier))
|
||||
try dev.allocator.dupe(u8, specifier)
|
||||
try dev.allocator().dupe(u8, specifier)
|
||||
else
|
||||
try std.fmt.allocPrint(dev.allocator, "./{s}", .{specifier});
|
||||
errdefer dev.allocator.free(specifier_cloned);
|
||||
try std.fmt.allocPrint(dev.allocator(), "./{s}", .{specifier});
|
||||
errdefer dev.allocator().free(specifier_cloned);
|
||||
|
||||
if (gop.found_existing) {
|
||||
const dep = store.appendDepAssumeCapacity(.{
|
||||
@@ -163,8 +164,8 @@ fn insert(
|
||||
if (owned_fd) "from dir cache" else "owned fd",
|
||||
});
|
||||
|
||||
const dir_name = try dev.allocator.dupe(u8, dir_name_to_watch);
|
||||
errdefer dev.allocator.free(dir_name);
|
||||
const dir_name = try dev.allocator().dupe(u8, dir_name_to_watch);
|
||||
errdefer dev.allocator().free(dir_name);
|
||||
|
||||
gop.key_ptr.* = bun.strings.withoutTrailingSlashWindowsPath(dir_name);
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ body: uws.BodyReaderMixin(@This(), "body", runWithBody, finalize),
|
||||
pub fn run(dev: *DevServer, _: *Request, resp: anytype) void {
|
||||
const ctx = bun.new(ErrorReportRequest, .{
|
||||
.dev = dev,
|
||||
.body = .init(dev.allocator),
|
||||
.body = .init(dev.allocator()),
|
||||
});
|
||||
ctx.dev.server.?.onPendingRequest();
|
||||
ctx.body.readBody(resp);
|
||||
@@ -41,8 +41,8 @@ pub fn runWithBody(ctx: *ErrorReportRequest, body: []const u8, r: AnyResponse) !
|
||||
var s = std.io.fixedBufferStream(body);
|
||||
const reader = s.reader();
|
||||
|
||||
var sfa_general = std.heap.stackFallback(65536, ctx.dev.allocator);
|
||||
var sfa_sourcemap = std.heap.stackFallback(65536, ctx.dev.allocator);
|
||||
var sfa_general = std.heap.stackFallback(65536, ctx.dev.allocator());
|
||||
var sfa_sourcemap = std.heap.stackFallback(65536, ctx.dev.allocator());
|
||||
const temp_alloc = sfa_general.get();
|
||||
var arena = std.heap.ArenaAllocator.init(temp_alloc);
|
||||
defer arena.deinit();
|
||||
@@ -169,8 +169,8 @@ pub fn runWithBody(ctx: *ErrorReportRequest, body: []const u8, r: AnyResponse) !
|
||||
|
||||
if (runtime_lines == null) {
|
||||
const file = result.entry_files.get(@intCast(index - 1));
|
||||
if (file != .empty) {
|
||||
const json_encoded_source_code = file.ref.data.quotedContents();
|
||||
if (file.get()) |source_map| {
|
||||
const json_encoded_source_code = source_map.quotedContents();
|
||||
// First line of interest is two above the target line.
|
||||
const target_line = @as(usize, @intCast(frame.position.line.zeroBased()));
|
||||
first_line_of_interest = target_line -| 2;
|
||||
@@ -238,7 +238,7 @@ pub fn runWithBody(ctx: *ErrorReportRequest, body: []const u8, r: AnyResponse) !
|
||||
) catch {},
|
||||
}
|
||||
|
||||
var out: std.ArrayList(u8) = .init(ctx.dev.allocator);
|
||||
var out: std.ArrayList(u8) = .init(ctx.dev.allocator());
|
||||
errdefer out.deinit();
|
||||
const w = out.writer();
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user