aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-12-18 20:03:00 -0800
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-12-18 20:03:00 -0800
commite2d6c692c4d6e6efef2a3ac1229b80764736a0e2 (patch)
tree2b4452655cca7cfa78f764dc3a7c61dd04359103
parent501fab0befb92ac5d35048b959a11befaa3e3e5e (diff)
downloadbun-e2d6c692c4d6e6efef2a3ac1229b80764736a0e2.tar.gz
bun-e2d6c692c4d6e6efef2a3ac1229b80764736a0e2.tar.zst
bun-e2d6c692c4d6e6efef2a3ac1229b80764736a0e2.zip
Use new http in `bun upgrade`
-rw-r--r--build.zig7
-rw-r--r--src/cli/upgrade_command.zig47
-rw-r--r--src/http_client_async.zig36
-rw-r--r--src/install/install.zig5
-rw-r--r--src/javascript/jsc/bindings/headers-cpp.h2
-rw-r--r--src/javascript/jsc/bindings/headers.h2
-rw-r--r--src/libarchive/libarchive.zig1
-rw-r--r--src/which_npm_client.zig93
8 files changed, 64 insertions, 129 deletions
diff --git a/build.zig b/build.zig
index 2398ca85e..2746dd338 100644
--- a/build.zig
+++ b/build.zig
@@ -435,13 +435,6 @@ pub fn build(b: *std.build.Builder) !void {
defer headers_step.dependOn(&headers_obj.step);
try configureObjectStep(headers_obj, target, exe.main_pkg_path.?);
}
-
- {
- const headers_step = b.step("hop-obj", "Build hop (object files)");
- var headers_obj: *std.build.LibExeObjStep = b.addObject("hop", "misctools/hop.zig");
- defer headers_step.dependOn(&headers_obj.step);
- try configureObjectStep(headers_obj, target, exe.main_pkg_path.?);
- }
} else {
b.default_step.dependOn(&exe.step);
}
diff --git a/src/cli/upgrade_command.zig b/src/cli/upgrade_command.zig
index f2a12c145..61000023f 100644
--- a/src/cli/upgrade_command.zig
+++ b/src/cli/upgrade_command.zig
@@ -21,18 +21,18 @@ const bundler = @import("../bundler.zig");
const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle;
const fs = @import("../fs.zig");
const URL = @import("../query_string_map.zig").URL;
-const HTTPClient = @import("../http_client.zig");
+const HTTP = @import("http");
const ParseJSON = @import("../json_parser.zig").ParseJSON;
const Archive = @import("../libarchive/libarchive.zig").Archive;
const Zlib = @import("../zlib.zig");
const JSPrinter = @import("../js_printer.zig");
const DotEnv = @import("../env_loader.zig");
-const NPMClient = @import("../which_npm_client.zig").NPMClient;
const which = @import("../which.zig").which;
const clap = @import("clap");
const Lock = @import("../lock.zig").Lock;
const Headers = @import("../javascript/jsc/webcore/response.zig").Headers;
const CopyFile = @import("../copy_file.zig");
+const NetworkThread = @import("network_thread");
pub var initialized_store = false;
pub fn initializeStore() void {
@@ -145,18 +145,6 @@ pub const UpgradeCommand = struct {
),
);
- var client = HTTPClient.init(
- allocator,
- .GET,
- api_url,
- header_entries,
- headers_buf,
- );
- client.timeout = timeout;
- if (!silent) {
- client.progress_node = progress;
- }
-
if (env_loader.map.get("GITHUB_ACCESS_TOKEN")) |access_token| {
if (access_token.len > 0) {
headers_buf = try std.fmt.allocPrint(allocator, default_github_headers ++ "Access-TokenBearer {s}", .{access_token});
@@ -177,7 +165,13 @@ pub const UpgradeCommand = struct {
}
var metadata_body = try MutableString.init(allocator, 2048);
- var response = try client.send("", &metadata_body);
+ var request_body = try MutableString.init(allocator, 0);
+
+ // ensure very stable memory address
+ var async_http: *HTTP.AsyncHTTP = allocator.create(HTTP.AsyncHTTP) catch unreachable;
+ async_http.* = try HTTP.AsyncHTTP.init(allocator, .GET, api_url, header_entries, headers_buf, &metadata_body, &request_body, 60 * std.time.ns_per_min);
+ if (!silent) async_http.client.progress_node = progress;
+ const response = try async_http.sendSync();
switch (response.status_code) {
404 => return error.HTTP404,
@@ -326,6 +320,8 @@ pub const UpgradeCommand = struct {
const exe_subpath = Version.folder_name ++ std.fs.path.sep_str ++ "bun";
pub fn exec(ctx: Command.Context) !void {
+ try NetworkThread.init();
+
var filesystem = try fs.FileSystem.init1(ctx.allocator, null);
var env_loader: DotEnv.Loader = brk: {
var map = try ctx.allocator.create(DotEnv.Map);
@@ -375,21 +371,24 @@ pub const UpgradeCommand = struct {
var refresher = std.Progress{};
var progress = try refresher.start("Downloading", version.size);
refresher.refresh();
+ var async_http = ctx.allocator.create(HTTP.AsyncHTTP) catch unreachable;
+ var zip_file_buffer = try ctx.allocator.create(MutableString);
+ zip_file_buffer.* = try MutableString.init(ctx.allocator, @maximum(version.size, 1024));
+ var request_buffer = try MutableString.init(ctx.allocator, 0);
- var client = HTTPClient.init(
+ async_http.* = try HTTP.AsyncHTTP.init(
ctx.allocator,
.GET,
URL.parse(version.zip_url),
.{},
"",
+ zip_file_buffer,
+ &request_buffer,
+ timeout,
);
- client.timeout = timeout;
- client.progress_node = progress;
- var zip_file_buffer = try MutableString.init(ctx.allocator, @maximum(version.size, 1024));
- var response = try client.send(
- "",
- &zip_file_buffer,
- );
+ async_http.client.timeout = timeout;
+ async_http.client.progress_node = progress;
+ const response = try async_http.sendSync();
switch (response.status_code) {
404 => return error.HTTP404,
@@ -400,7 +399,7 @@ pub const UpgradeCommand = struct {
else => return error.HTTPError,
}
- var bytes = zip_file_buffer.toOwnedSliceLeaky();
+ const bytes = zip_file_buffer.toOwnedSliceLeaky();
progress.end();
refresher.refresh();
diff --git a/src/http_client_async.zig b/src/http_client_async.zig
index 684e4bc2c..370308d08 100644
--- a/src/http_client_async.zig
+++ b/src/http_client_async.zig
@@ -200,6 +200,11 @@ pub const HeaderBuilder = struct {
pub const HTTPChannel = @import("./sync.zig").Channel(*AsyncHTTP, .{ .Static = 1000 });
+// 32 pointers much cheaper than 1000 pointers
+const SingleHTTPChannel = @import("./sync.zig").Channel(*AsyncHTTP, .{ .Static = 32 });
+var send_sync_channel: SingleHTTPChannel = undefined;
+var send_sync_channel_loaded: bool = false;
+
pub const HTTPChannelContext = struct {
http: AsyncHTTP = undefined,
channel: *HTTPChannel,
@@ -287,6 +292,37 @@ pub const AsyncHTTP = struct {
batch.push(ThreadPool.Batch.from(&sender.task));
}
+ fn sendSyncCallback(this: *AsyncHTTP, sender: *HTTPSender) void {
+ send_sync_channel.writeItem(this) catch unreachable;
+ sender.release();
+ }
+
+ pub fn sendSync(this: *AsyncHTTP) anyerror!picohttp.Response {
+ if (!send_sync_channel_loaded) {
+ send_sync_channel_loaded = true;
+ send_sync_channel = SingleHTTPChannel.init();
+ }
+
+ this.callback = sendSyncCallback;
+ var batch = NetworkThread.Batch{};
+ this.schedule(this.allocator, &batch);
+ NetworkThread.global.pool.schedule(batch);
+ while (true) {
+ var async_http: *AsyncHTTP = (send_sync_channel.tryReadItem() catch unreachable) orelse {
+ std.atomic.spinLoopHint();
+ std.time.sleep(std.time.ns_per_us * 100);
+ continue;
+ };
+ if (async_http.err) |err| {
+ return err;
+ }
+
+ return async_http.response.?;
+ }
+
+ unreachable;
+ }
+
var http_sender_head: std.atomic.Atomic(?*HTTPSender) = std.atomic.Atomic(?*HTTPSender).init(null);
pub const HTTPSender = struct {
diff --git a/src/install/install.zig b/src/install/install.zig
index ab3bddb59..c46e9784d 100644
--- a/src/install/install.zig
+++ b/src/install/install.zig
@@ -3155,7 +3155,6 @@ const PackageInstall = struct {
});
pub const linux = BackendSupport.initDefault(false, .{
- .io_uring = true,
.hardlink = true,
.copyfile = true,
});
@@ -5443,10 +5442,6 @@ pub const PackageManager = struct {
break :brk PackageInstall.Method.hardlink;
} else if (strings.eqlComptime(backend_, "copyfile")) {
break :brk PackageInstall.Method.copyfile;
- } else if (strings.eqlComptime(backend_, "io_uring")) {
- break :brk PackageInstall.Method.io_uring;
- } else if (strings.eqlComptime(backend_, "copy_file_range")) {
- break :brk PackageInstall.Method.copy_file_range;
}
}
break :brk null;
diff --git a/src/javascript/jsc/bindings/headers-cpp.h b/src/javascript/jsc/bindings/headers-cpp.h
index ec3a8cb2c..c57f02fe9 100644
--- a/src/javascript/jsc/bindings/headers-cpp.h
+++ b/src/javascript/jsc/bindings/headers-cpp.h
@@ -1,4 +1,4 @@
-//-- AUTOGENERATED FILE -- 1639731411
+//-- AUTOGENERATED FILE -- 1639884060
// clang-format off
#pragma once
diff --git a/src/javascript/jsc/bindings/headers.h b/src/javascript/jsc/bindings/headers.h
index 3633c7ca1..95c6b78ad 100644
--- a/src/javascript/jsc/bindings/headers.h
+++ b/src/javascript/jsc/bindings/headers.h
@@ -1,5 +1,5 @@
// clang-format: off
-//-- AUTOGENERATED FILE -- 1639731411
+//-- AUTOGENERATED FILE -- 1639884060
#pragma once
#include <stddef.h>
diff --git a/src/libarchive/libarchive.zig b/src/libarchive/libarchive.zig
index 4e1751e80..94342c1e9 100644
--- a/src/libarchive/libarchive.zig
+++ b/src/libarchive/libarchive.zig
@@ -3,7 +3,6 @@
const lib = @import("./libarchive-bindings.zig");
usingnamespace @import("../global.zig");
const std = @import("std");
-const Hop = @import("../hop/hop.zig").Library;
const struct_archive = lib.struct_archive;
pub const Seek = enum(c_int) {
set = std.os.SEEK_SET,
diff --git a/src/which_npm_client.zig b/src/which_npm_client.zig
index 56dd752c1..aa90622ca 100644
--- a/src/which_npm_client.zig
+++ b/src/which_npm_client.zig
@@ -1,6 +1,5 @@
usingnamespace @import("./global.zig");
-const which = @import("./which.zig").which;
const std = @import("std");
pub const NPMClient = struct {
@@ -8,96 +7,10 @@ pub const NPMClient = struct {
tag: Tag,
pub const Tag = enum {
- npm,
- yarn,
- pnpm,
+ bun,
};
- // This check adds around 150ms
- // so...if we do do this, we should do it in a separate thread
- pub fn isYarnBerry(allocator: *std.mem.Allocator, cwd_dir: string, yarn_path: string) bool {
- var args = [_]string{ yarn_path, "--version" };
- var term = std.ChildProcess.exec(.{
- .argv = &args,
- .allocator = allocator,
- .cwd = if (cwd_dir.len > 1) std.mem.trimRight(u8, cwd_dir, "/") else cwd_dir,
- }) catch return true;
- defer allocator.free(term.stderr);
- defer allocator.free(term.stdout);
-
- if (term.stdout.len == 0) return true;
- return term.stdout[0] != '1';
- }
-
- pub fn detect(allocator: *std.mem.Allocator, realpath_buf: *[std.fs.MAX_PATH_BYTES]u8, PATH: string, cwd: string, comptime allow_yarn: bool) !?NPMClient {
-
- // We say:
- // - pnpm if it exists, is the default. its most esoteric, so if you have it installed, you prob want it.
- // - yarn if it exists and it is yarn 1, its the default (yarn 2 or later is not supported)
- // - else npm
-
- const out_path = brk: {
- var path_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
-
- const path: [:0]const u8 = if (comptime allow_yarn)
- which(
- &path_buf,
- PATH,
- cwd,
- "pnpm",
- ) orelse which(
- &path_buf,
- PATH,
- cwd,
- "yarn",
- ) orelse which(
- &path_buf,
- PATH,
- cwd,
- "npm",
- ) orelse ""
- else
- which(
- &path_buf,
- PATH,
- cwd,
- "pnpm",
- ) orelse which(
- &path_buf,
- PATH,
- cwd,
- "npm",
- ) orelse "";
-
- std.mem.copy(u8, realpath_buf, std.mem.span(path));
- // It's important we don't resolve the symlink
- // That breaks volta.
- break :brk realpath_buf[0..path.len];
- };
-
- const basename = std.fs.path.basename(std.mem.span(out_path));
- if (basename.len == 0) return null;
-
- // if (comptime allow_yarn) {
- // if (std.mem.indexOf(u8, basename, "yarn") != null) {
- // if (isYarnBerry(allocator, cwd, out_path)) {
- // return try detect(allocator, realpath_buf, PATH, cwd, false);
- // }
- // }
- // }
-
- if (strings.contains(basename, "pnpm")) {
- return NPMClient{ .bin = out_path, .tag = .pnpm };
- }
-
- if (strings.contains(basename, "yarn")) {
- return NPMClient{ .bin = out_path, .tag = .yarn };
- }
-
- if (strings.contains(basename, "npm")) {
- return NPMClient{ .bin = out_path, .tag = .npm };
- }
-
- return null;
+ pub fn detect(allocator: *std.mem.Allocator, realpath_buf: *[std.fs.MAX_PATH_BYTES]u8, PATH: string, cwd: string, comptime allow_yarn: bool) !NPMClient {
+
}
};