@@ -4,14 +4,15 @@ const mem = std.mem;
44const fs = std .fs ;
55const json = std .json ;
66const http = std .http ;
7- const utils = @import ( "utils.zig" );
7+
88const c = @import ("colors.zig" );
9+ const utils = @import ("utils.zig" );
910
1011pub fn getInstalledVersions (allocator : mem.Allocator , config_dir : []const u8 ) ! std. ArrayList ([]const u8 ) {
1112 const versions_dir_path = try getVersionsDir (allocator , config_dir );
1213 defer allocator .free (versions_dir_path );
1314
14- var result = std .ArrayList ([]const u8 ). init ( allocator ) ;
15+ var result : std .ArrayList ([]const u8 ) = .empty ;
1516
1617 var versions_dir = fs .openDirAbsolute (versions_dir_path , .{ .iterate = true }) catch | err | switch (err ) {
1718 error .FileNotFound = > return result ,
@@ -30,7 +31,7 @@ pub fn getInstalledVersions(allocator: mem.Allocator, config_dir: []const u8) !s
3031 defer allocator .free (bin );
3132
3233 if (try utils .file_exists (bin )) {
33- try result .append (version );
34+ try result .append (allocator , version );
3435 } else {
3536 allocator .free (version );
3637 }
@@ -90,13 +91,17 @@ pub fn detectProjectVersion(allocator: mem.Allocator, is_debug: bool) !?[]const
9091pub fn getLatestLocalVersion (allocator : mem.Allocator , is_debug : bool , config_dir : []const u8 ) ! ? []const u8 {
9192 if (is_debug ) std .debug .print ("Getting latest local version...\n " , .{});
9293
93- const installed_versions = try getInstalledVersions (allocator , config_dir );
94- if (is_debug ) std .debug .print ("{d} versions: {s}\n " , .{ installed_versions .items .len , installed_versions .items });
94+ var installed_versions = try getInstalledVersions (allocator , config_dir );
95+ if (is_debug ) {
96+ const versions_str = try utils .stringifyStringList (allocator , installed_versions .items );
97+ defer allocator .free (versions_str );
98+ std .debug .print ("{d} versions: {s}\n " , .{ installed_versions .items .len , versions_str });
99+ }
95100 defer {
96101 for (installed_versions .items ) | item | {
97102 allocator .free (item );
98103 }
99- installed_versions .deinit ();
104+ installed_versions .deinit (allocator );
100105 }
101106
102107 if (installed_versions .items .len == 0 ) {
@@ -120,19 +125,20 @@ pub fn getLatestRemoteVersion(allocator: mem.Allocator, is_debug: bool) ![]const
120125 const uri = try std .Uri .parse ("https://ungh.cc/repos/oven-sh/bun/releases/latest" );
121126 const buf = try allocator .alloc (u8 , 1024 * 1024 * 4 );
122127 defer allocator .free (buf );
123- var req = try client .open (
124- .GET ,
125- uri ,
126- .{ . server_header_buffer = buf , . keep_alive = false , . extra_headers = &[_ ]http.Header {accept_header } },
127- );
128+ var req = try client .request ( .GET , uri , .{
129+ .keep_alive = false ,
130+ . headers = .{ . accept_encoding = .omit } ,
131+ .extra_headers = &[_ ]http.Header {accept_header },
132+ } );
128133 defer req .deinit ();
129134
130- try req .send ();
131- try req .finish ();
132- try req .wait ();
135+ try req .sendBodiless ();
133136
134- var rdr = req .reader ();
135- const body = try rdr .readAllAlloc (allocator , 1024 * 1024 * 4 );
137+ var redirect_buffer : [8 * 1024 ]u8 = undefined ;
138+ var response = try req .receiveHead (& redirect_buffer );
139+ var transfer_buffer : [4 * 1024 ]u8 = undefined ;
140+ const reader = response .reader (& transfer_buffer );
141+ const body = try reader .allocRemaining (allocator , .limited (1024 * 1024 * 4 ));
136142 defer allocator .free (body );
137143 if (is_debug ) std .debug .print ("Latest release: {s}\n " , .{body });
138144
@@ -238,25 +244,29 @@ fn confirmInstallation(version: []const u8) !void {
238244 }
239245 }
240246
241- const stdout = std .io .getStdOut ().writer ();
247+ var stdout_buf : [1024 ]u8 = undefined ;
248+ var stdout = std .fs .File .stdout ().writer (& stdout_buf );
242249
243250 // Check if stdin is a TTY (interactive)
244- const is_interactive = std .io .getStdIn ().isTty ();
251+ const stdin_file = std .fs .File .stdin ();
252+ const is_interactive = stdin_file .isTty ();
245253
246254 if (is_interactive ) {
247- const stdin = std .io .getStdIn ().reader ();
255+ var stdin_buf : [1024 ]u8 = undefined ;
256+ var stdin = stdin_file .reader (& stdin_buf );
248257
249- try stdout .print ("{s}Bun v{s} is not installed. Do you want to install it? [y/N]{s} " , .{ c .yellow , version , c .reset });
250- var buffer : [ 2 ] u8 = undefined ;
251- const user_input = stdin .readUntilDelimiterOrEof ( & buffer , '\n ' ) catch | err | switch (err ) {
258+ try stdout .interface . print ("{s}Bun v{s} is not installed. Do you want to install it? [y/N]{s} " , .{ c .yellow , version , c .reset });
259+ try stdout . interface . flush () ;
260+ const user_input = stdin .interface . takeDelimiterExclusive ( '\n ' ) catch | err | switch (err ) {
252261 error .StreamTooLong = > "N" ,
262+ error .EndOfStream = > "N" ,
253263 else = > return err ,
254- } orelse "N" ;
264+ };
255265
256266 if (mem .eql (u8 , user_input , "y" )) return ;
257267 } else {
258268 // Non-interactive mode, just display message and abort
259- try stdout .print ("{s}Bun v{s} is not installed. Run in an interactive terminal to install or set BUNV_AUTO_INSTALL=1.{s}\n " , .{ c .yellow , version , c .reset });
269+ try stdout .interface . print ("{s}Bun v{s} is not installed. Run in an interactive terminal to install or set BUNV_AUTO_INSTALL=1.{s}\n " , .{ c .yellow , version , c .reset });
260270 }
261271
262272 std .debug .print ("Installation aborted by user\n " , .{});
0 commit comments