aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/bun.js/api')
-rw-r--r--src/bun.js/api/JSBundler.zig4
-rw-r--r--src/bun.js/api/JSTranspiler.zig2
-rw-r--r--src/bun.js/api/bun.zig11
-rw-r--r--src/bun.js/api/bun/dns_resolver.zig15
-rw-r--r--src/bun.js/api/bun/spawn.zig2
-rw-r--r--src/bun.js/api/bun/subprocess.zig114
-rw-r--r--src/bun.js/api/ffi.zig6
-rw-r--r--src/bun.js/api/filesystem_router.zig2
-rw-r--r--src/bun.js/api/html_rewriter.zig8
-rw-r--r--src/bun.js/api/server.zig45
10 files changed, 117 insertions, 92 deletions
diff --git a/src/bun.js/api/JSBundler.zig b/src/bun.js/api/JSBundler.zig
index f5c3793ed..4a4d10f0f 100644
--- a/src/bun.js/api/JSBundler.zig
+++ b/src/bun.js/api/JSBundler.zig
@@ -1,6 +1,6 @@
const std = @import("std");
const Api = @import("../../api/schema.zig").Api;
-const http = @import("../../http.zig");
+const http = @import("../../bun_dev_http_server.zig");
const JavaScript = @import("../javascript.zig");
const QueryStringMap = @import("../../url.zig").QueryStringMap;
const CombinedScanner = @import("../../url.zig").CombinedScanner;
@@ -287,7 +287,7 @@ pub const JSBundler = struct {
defer dir.close();
var rootdir_buf: [bun.MAX_PATH_BYTES]u8 = undefined;
- this.rootdir.appendSliceExact(try bun.getFdPath(dir.fd, &rootdir_buf)) catch unreachable;
+ this.rootdir.appendSliceExact(try bun.getFdPath(bun.toFD(dir.fd), &rootdir_buf)) catch unreachable;
}
if (try config.getArray(globalThis, "external")) |externals| {
diff --git a/src/bun.js/api/JSTranspiler.zig b/src/bun.js/api/JSTranspiler.zig
index c31354712..8cec025eb 100644
--- a/src/bun.js/api/JSTranspiler.zig
+++ b/src/bun.js/api/JSTranspiler.zig
@@ -1,6 +1,6 @@
const std = @import("std");
const Api = @import("../../api/schema.zig").Api;
-const http = @import("../../http.zig");
+const http = @import("../../bun_dev_http_server.zig");
const JavaScript = @import("../javascript.zig");
const QueryStringMap = @import("../../url.zig").QueryStringMap;
const CombinedScanner = @import("../../url.zig").CombinedScanner;
diff --git a/src/bun.js/api/bun.zig b/src/bun.js/api/bun.zig
index 71b065993..cf984c81a 100644
--- a/src/bun.js/api/bun.zig
+++ b/src/bun.js/api/bun.zig
@@ -179,7 +179,7 @@ const ServerEntryPoint = bun.bundler.ServerEntryPoint;
const js_printer = bun.js_printer;
const js_parser = bun.js_parser;
const js_ast = bun.JSAst;
-const http = @import("../../http.zig");
+const http = @import("../../bun_dev_http_server.zig");
const NodeFallbackModules = @import("../../node_fallbacks.zig");
const ImportKind = ast.ImportKind;
const Analytics = @import("../../analytics/analytics_thread.zig");
@@ -2738,6 +2738,11 @@ pub fn mmapFile(
globalThis: *JSC.JSGlobalObject,
callframe: *JSC.CallFrame,
) callconv(.C) JSC.JSValue {
+ if (comptime Environment.isWindows) {
+ globalThis.throwTODO("mmapFile is not supported on Windows");
+ return JSC.JSValue.zero;
+ }
+
const arguments_ = callframe.arguments(2);
var args = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments_.slice());
defer args.deinit();
@@ -2789,7 +2794,7 @@ pub fn mmapFile(
flags |= std.os.MAP.SHARED;
}
- const map = switch (JSC.Node.Syscall.mmapFile(buf_z, flags, map_size, offset)) {
+ const map = switch (bun.sys.mmapFile(buf_z, flags, map_size, offset)) {
.result => |map| map,
.err => |err| {
@@ -2800,7 +2805,7 @@ pub fn mmapFile(
return JSC.C.JSObjectMakeTypedArrayWithBytesNoCopy(globalThis, JSC.C.JSTypedArrayType.kJSTypedArrayTypeUint8Array, @as(?*anyopaque, @ptrCast(map.ptr)), map.len, struct {
pub fn x(ptr: ?*anyopaque, size: ?*anyopaque) callconv(.C) void {
- _ = JSC.Node.Syscall.munmap(@as([*]align(std.mem.page_size) u8, @ptrCast(@alignCast(ptr)))[0..@intFromPtr(size)]);
+ _ = bun.sys.munmap(@as([*]align(std.mem.page_size) u8, @ptrCast(@alignCast(ptr)))[0..@intFromPtr(size)]);
}
}.x, @as(?*anyopaque, @ptrFromInt(map.len)), null).?.value();
}
diff --git a/src/bun.js/api/bun/dns_resolver.zig b/src/bun.js/api/bun/dns_resolver.zig
index 5c9be467f..1a370cd60 100644
--- a/src/bun.js/api/bun/dns_resolver.zig
+++ b/src/bun.js/api/bun/dns_resolver.zig
@@ -225,6 +225,11 @@ pub fn addressToJS(
address: std.net.Address,
globalThis: *JSC.JSGlobalObject,
) JSC.JSValue {
+ if (comptime Environment.isWindows) {
+ globalThis.throwTODO("TODO: windows");
+ return .zero;
+ }
+
return addressToString(allocator, address).toValueGC(globalThis);
}
@@ -823,6 +828,10 @@ pub const GetAddrInfoRequest = struct {
query: GetAddrInfo,
pub fn run(this: *@This()) void {
+ if (comptime Environment.isWindows) {
+ bun.todo(@src(), {});
+ return;
+ }
const query = this.query;
defer bun.default_allocator.free(bun.constStrToU8(query.name));
var hints = query.options.toLibC();
@@ -842,7 +851,7 @@ pub const GetAddrInfoRequest = struct {
if (hints) |*hint| hint else null,
&addrinfo,
);
- JSC.Node.Syscall.syslog("getaddrinfo({s}, {d}) = {d} ({any})", .{
+ bun.sys.syslog("getaddrinfo({s}, {d}) = {d} ({any})", .{
query.name,
port,
err,
@@ -1043,7 +1052,7 @@ pub const DNSLookup = struct {
const error_value = brk: {
if (err == .ESERVFAIL) {
- break :brk JSC.Node.Syscall.Error.fromCode(std.c.getErrno(-1), .getaddrinfo).toJSC(globalThis);
+ break :brk bun.sys.Error.fromCode(bun.C.getErrno(-1), .getaddrinfo).toJSC(globalThis);
}
const error_value = globalThis.createErrorInstance("DNS lookup failed: {s}", .{err.label()});
error_value.put(
@@ -1435,7 +1444,7 @@ pub const DNSResolver = struct {
var poll_entry = this.polls.getOrPut(fd) catch unreachable;
if (!poll_entry.found_existing) {
- poll_entry.value_ptr.* = JSC.FilePoll.init(vm, fd, .{}, DNSResolver, this);
+ poll_entry.value_ptr.* = JSC.FilePoll.init(vm, bun.toFD(fd), .{}, DNSResolver, this);
}
var poll = poll_entry.value_ptr.*.?;
diff --git a/src/bun.js/api/bun/spawn.zig b/src/bun.js/api/bun/spawn.zig
index be354c7f0..e5678ec44 100644
--- a/src/bun.js/api/bun/spawn.zig
+++ b/src/bun.js/api/bun/spawn.zig
@@ -206,7 +206,7 @@ pub const PosixSpawn = struct {
envp,
);
if (comptime bun.Environment.allow_assert)
- JSC.Node.Syscall.syslog("posix_spawn({s}) = {d} ({d})", .{
+ bun.sys.syslog("posix_spawn({s}) = {d} ({d})", .{
path,
rc,
pid,
diff --git a/src/bun.js/api/bun/subprocess.zig b/src/bun.js/api/bun/subprocess.zig
index f2e33cf78..77cf3886e 100644
--- a/src/bun.js/api/bun/subprocess.zig
+++ b/src/bun.js/api/bun/subprocess.zig
@@ -23,7 +23,7 @@ pub const Subprocess = struct {
pid: std.os.pid_t,
// on macOS, this is nothing
// on linux, it's a pidfd
- pidfd: std.os.fd_t = std.math.maxInt(std.os.fd_t),
+ pidfd: if (Environment.isLinux) bun.FileDescriptor else u0 = std.math.maxInt(if (Environment.isLinux) bun.FileDescriptor else u0),
stdin: Writable,
stdout: Readable,
@@ -36,7 +36,7 @@ pub const Subprocess = struct {
exit_code: ?u8 = null,
signal_code: ?SignalCode = null,
- waitpid_err: ?JSC.Node.Syscall.Error = null,
+ waitpid_err: ?bun.sys.Error = null,
has_waitpid_task: bool = false,
notification_task: JSC.AnyTask = undefined,
@@ -210,7 +210,7 @@ pub const Subprocess = struct {
};
}
- pub fn onClose(this: *Readable, _: ?JSC.Node.Syscall.Error) void {
+ pub fn onClose(this: *Readable, _: ?bun.sys.Error) void {
this.* = .closed;
}
@@ -221,7 +221,7 @@ pub const Subprocess = struct {
pub fn close(this: *Readable) void {
switch (this.*) {
.fd => |fd| {
- _ = JSC.Node.Syscall.close(fd);
+ _ = bun.sys.close(fd);
},
.pipe => {
this.pipe.done();
@@ -233,7 +233,7 @@ pub const Subprocess = struct {
pub fn finalize(this: *Readable) void {
switch (this.*) {
.fd => |fd| {
- _ = JSC.Node.Syscall.close(fd);
+ _ = bun.sys.close(fd);
},
.pipe => {
if (this.pipe == .stream and this.pipe.stream.ptr == .File) {
@@ -355,7 +355,7 @@ pub const Subprocess = struct {
if (comptime Environment.isLinux) {
// should this be handled differently?
// this effectively shouldn't happen
- if (this.pidfd == std.math.maxInt(std.os.fd_t)) {
+ if (this.pidfd == bun.invalid_fd) {
return .{ .result = {} };
}
@@ -366,16 +366,16 @@ pub const Subprocess = struct {
const errno = std.os.linux.getErrno(rc);
// if the process was already killed don't throw
if (errno != .SRCH)
- return .{ .err = JSC.Node.Syscall.Error.fromCode(errno, .kill) };
+ return .{ .err = bun.sys.Error.fromCode(errno, .kill) };
}
} else {
const err = std.c.kill(this.pid, sig);
if (err != 0) {
- const errno = std.c.getErrno(err);
+ const errno = bun.C.getErrno(err);
// if the process was already killed don't throw
if (errno != .SRCH)
- return .{ .err = JSC.Node.Syscall.Error.fromCode(errno, .kill) };
+ return .{ .err = bun.sys.Error.fromCode(errno, .kill) };
}
}
@@ -394,9 +394,9 @@ pub const Subprocess = struct {
const pidfd = this.pidfd;
- this.pidfd = std.math.maxInt(std.os.fd_t);
+ this.pidfd = bun.invalid_fd;
- if (pidfd != std.math.maxInt(std.os.fd_t)) {
+ if (pidfd != bun.invalid_fd) {
_ = std.os.close(pidfd);
}
}
@@ -492,7 +492,7 @@ pub const Subprocess = struct {
}
while (to_write.len > 0) {
- switch (JSC.Node.Syscall.write(this.fd, to_write)) {
+ switch (bun.sys.write(this.fd, to_write)) {
.err => |e| {
if (e.isRetry()) {
log("write({d}) retry", .{
@@ -546,7 +546,7 @@ pub const Subprocess = struct {
}
if (this.fd != bun.invalid_fd) {
- _ = JSC.Node.Syscall.close(this.fd);
+ _ = bun.sys.close(this.fd);
this.fd = bun.invalid_fd;
}
}
@@ -576,7 +576,7 @@ pub const Subprocess = struct {
pub const Status = union(enum) {
pending: void,
done: void,
- err: JSC.Node.Syscall.Error,
+ err: bun.sys.Error,
};
pub fn init(fd: bun.FileDescriptor) BufferedOutput {
@@ -619,7 +619,7 @@ pub const Subprocess = struct {
if (err == .Error) {
this.status = .{ .err = err.Error };
} else {
- this.status = .{ .err = JSC.Node.Syscall.Error.fromCode(.CANCELED, .read) };
+ this.status = .{ .err = bun.sys.Error.fromCode(.CANCELED, .read) };
}
this.fifo.close();
@@ -838,7 +838,7 @@ pub const Subprocess = struct {
// When the stream has closed we need to be notified to prevent a use-after-free
// We can test for this use-after-free by enabling hot module reloading on a file and then saving it twice
- pub fn onClose(this: *Writable, _: ?JSC.Node.Syscall.Error) void {
+ pub fn onClose(this: *Writable, _: ?bun.sys.Error) void {
this.* = .{
.ignore = {},
};
@@ -916,7 +916,7 @@ pub const Subprocess = struct {
_ = pipe_to_readable_stream.pipe.end(null);
},
.fd => |fd| {
- _ = JSC.Node.Syscall.close(fd);
+ _ = bun.sys.close(fd);
this.* = .{ .ignore = {} };
},
.buffered_input => {
@@ -934,7 +934,7 @@ pub const Subprocess = struct {
_ = pipe_to_readable_stream.pipe.end(null);
},
.fd => |fd| {
- _ = JSC.Node.Syscall.close(fd);
+ _ = bun.sys.close(fd);
this.* = .{ .ignore = {} };
},
.buffered_input => {
@@ -1035,6 +1035,11 @@ pub const Subprocess = struct {
secondaryArgsValue: ?JSValue,
comptime is_sync: bool,
) JSValue {
+ if (comptime Environment.isWindows) {
+ globalThis.throwTODO("spawn() is not yet implemented on Windows");
+ return .zero;
+ }
+
var arena = @import("root").bun.ArenaAllocator.init(bun.default_allocator);
defer arena.deinit();
var allocator = arena.allocator();
@@ -1204,7 +1209,7 @@ pub const Subprocess = struct {
if (stdio_val.jsType().isArray()) {
var stdio_iter = stdio_val.arrayIterator(globalThis);
stdio_iter.len = @min(stdio_iter.len, 3);
- var i: usize = 0;
+ var i: u32 = 0;
while (stdio_iter.next()) |value| : (i += 1) {
if (!extractStdio(globalThis, i, value, &stdio))
return JSC.JSValue.jsUndefined();
@@ -1216,17 +1221,17 @@ pub const Subprocess = struct {
}
} else {
if (args.get(globalThis, "stdin")) |value| {
- if (!extractStdio(globalThis, std.os.STDIN_FILENO, value, &stdio))
+ if (!extractStdio(globalThis, bun.STDIN_FD, value, &stdio))
return .zero;
}
if (args.get(globalThis, "stderr")) |value| {
- if (!extractStdio(globalThis, std.os.STDERR_FILENO, value, &stdio))
+ if (!extractStdio(globalThis, bun.STDERR_FD, value, &stdio))
return .zero;
}
if (args.get(globalThis, "stdout")) |value| {
- if (!extractStdio(globalThis, std.os.STDOUT_FILENO, value, &stdio))
+ if (!extractStdio(globalThis, bun.STDOUT_FD, value, &stdio))
return .zero;
}
}
@@ -1282,19 +1287,19 @@ pub const Subprocess = struct {
stdio[0].setUpChildIoPosixSpawn(
&actions,
stdin_pipe,
- std.os.STDIN_FILENO,
+ bun.STDIN_FD,
) catch |err| return globalThis.handleError(err, "in configuring child stdin");
stdio[1].setUpChildIoPosixSpawn(
&actions,
stdout_pipe,
- std.os.STDOUT_FILENO,
+ bun.STDOUT_FD,
) catch |err| return globalThis.handleError(err, "in configuring child stdout");
stdio[2].setUpChildIoPosixSpawn(
&actions,
stderr_pipe,
- std.os.STDERR_FILENO,
+ bun.STDERR_FD,
) catch |err| return globalThis.handleError(err, "in configuring child stderr");
actions.chdir(cwd) catch |err| return globalThis.handleError(err, "in chdir()");
@@ -1313,15 +1318,15 @@ pub const Subprocess = struct {
const pid = brk: {
defer {
if (stdio[0].isPiped()) {
- _ = JSC.Node.Syscall.close(stdin_pipe[0]);
+ _ = bun.sys.close(stdin_pipe[0]);
}
if (stdio[1].isPiped()) {
- _ = JSC.Node.Syscall.close(stdout_pipe[1]);
+ _ = bun.sys.close(stdout_pipe[1]);
}
if (stdio[2].isPiped()) {
- _ = JSC.Node.Syscall.close(stderr_pipe[1]);
+ _ = bun.sys.close(stderr_pipe[1]);
}
}
@@ -1332,8 +1337,8 @@ pub const Subprocess = struct {
};
const pidfd: std.os.fd_t = brk: {
- if (Environment.isMac) {
- break :brk @as(std.os.fd_t, @intCast(pid));
+ if (!Environment.isLinux) {
+ break :brk pid;
}
const kernel = @import("../../../analytics.zig").GenerateHeader.GeneratePlatform.kernelVersion();
@@ -1352,7 +1357,7 @@ pub const Subprocess = struct {
switch (std.os.linux.getErrno(fd)) {
.SUCCESS => break :brk @as(std.os.fd_t, @intCast(fd)),
else => |err| {
- globalThis.throwValue(JSC.Node.Syscall.Error.fromCode(err, .open).toJSC(globalThis));
+ globalThis.throwValue(bun.sys.Error.fromCode(err, .open).toJSC(globalThis));
var status: u32 = 0;
// ensure we don't leak the child process on error
_ = std.os.linux.waitpid(pid, &status, 0);
@@ -1370,14 +1375,14 @@ pub const Subprocess = struct {
subprocess.* = Subprocess{
.globalThis = globalThis,
.pid = pid,
- .pidfd = pidfd,
- .stdin = Writable.init(stdio[std.os.STDIN_FILENO], stdin_pipe[1], globalThis) catch {
+ .pidfd = @truncate(pidfd),
+ .stdin = Writable.init(stdio[bun.STDIN_FD], stdin_pipe[1], globalThis) catch {
globalThis.throw("out of memory", .{});
return .zero;
},
// stdout and stderr only uses allocator and default_max_buffer_size if they are pipes and not a array buffer
- .stdout = Readable.init(stdio[std.os.STDOUT_FILENO], stdout_pipe[0], jsc_vm.allocator, default_max_buffer_size),
- .stderr = Readable.init(stdio[std.os.STDERR_FILENO], stderr_pipe[0], jsc_vm.allocator, default_max_buffer_size),
+ .stdout = Readable.init(stdio[bun.STDOUT_FD], stdout_pipe[0], jsc_vm.allocator, default_max_buffer_size),
+ .stderr = Readable.init(stdio[bun.STDERR_FD], stderr_pipe[0], jsc_vm.allocator, default_max_buffer_size),
.on_exit_callback = if (on_exit_callback != .zero) JSC.Strong.create(on_exit_callback, globalThis) else .{},
.is_sync = is_sync,
};
@@ -1393,9 +1398,10 @@ pub const Subprocess = struct {
subprocess.this_jsvalue = out;
var send_exit_notification = false;
+ const watchfd = if (comptime Environment.isLinux) pidfd else pid;
if (comptime !is_sync) {
- var poll = JSC.FilePoll.init(jsc_vm, pidfd, .{}, Subprocess, subprocess);
+ var poll = JSC.FilePoll.init(jsc_vm, watchfd, .{}, Subprocess, subprocess);
subprocess.poll_ref = poll;
switch (subprocess.poll_ref.?.register(
jsc_vm.uws_event_loop.?,
@@ -1458,7 +1464,7 @@ pub const Subprocess = struct {
subprocess.closeIO(.stdin);
{
- var poll = JSC.FilePoll.init(jsc_vm, pidfd, .{}, Subprocess, subprocess);
+ var poll = JSC.FilePoll.init(jsc_vm, watchfd, .{}, Subprocess, subprocess);
subprocess.poll_ref = poll;
switch (subprocess.poll_ref.?.register(
jsc_vm.uws_event_loop.?,
@@ -1680,7 +1686,7 @@ pub const Subprocess = struct {
try actions.dup2(fd, std_fileno);
},
.path => |pathlike| {
- const flag = if (std_fileno == std.os.STDIN_FILENO) @as(u32, os.O.RDONLY) else @as(u32, std.os.O.WRONLY);
+ const flag = if (std_fileno == bun.STDIN_FD) @as(u32, os.O.RDONLY) else @as(u32, std.os.O.WRONLY);
try actions.open(std_fileno, pathlike.slice(), flag | std.os.O.CREAT, 0o664);
},
.inherit => {
@@ -1692,7 +1698,7 @@ pub const Subprocess = struct {
},
.ignore => {
- const flag = if (std_fileno == std.os.STDIN_FILENO) @as(u32, os.O.RDONLY) else @as(u32, std.os.O.WRONLY);
+ const flag = if (std_fileno == bun.STDIN_FD) @as(u32, os.O.RDONLY) else @as(u32, std.os.O.WRONLY);
try actions.openZ(std_fileno, "/dev/null", flag, 0o664);
},
}
@@ -1702,25 +1708,27 @@ pub const Subprocess = struct {
fn extractStdioBlob(
globalThis: *JSC.JSGlobalObject,
blob: JSC.WebCore.AnyBlob,
- i: usize,
+ i: u32,
stdio_array: []Stdio,
) bool {
+ const fd = bun.stdio(i);
+
if (blob.needsToReadFile()) {
if (blob.store()) |store| {
if (store.data.file.pathlike == .fd) {
- if (store.data.file.pathlike.fd == @as(bun.FileDescriptor, @intCast(i))) {
+ if (store.data.file.pathlike.fd == fd) {
stdio_array[i] = Stdio{ .inherit = {} };
} else {
- switch (@as(std.os.fd_t, @intCast(i))) {
- std.os.STDIN_FILENO => {
- if (i == std.os.STDERR_FILENO or i == std.os.STDOUT_FILENO) {
+ switch (bun.FDTag.get(i)) {
+ .stdin => {
+ if (i == 1 or i == 2) {
globalThis.throwInvalidArguments("stdin cannot be used for stdout or stderr", .{});
return false;
}
},
- std.os.STDOUT_FILENO, std.os.STDERR_FILENO => {
- if (i == std.os.STDIN_FILENO) {
+ .stdout, .stderr => {
+ if (i == 0) {
globalThis.throwInvalidArguments("stdout and stderr cannot be used for stdin", .{});
return false;
}
@@ -1745,7 +1753,7 @@ pub const Subprocess = struct {
fn extractStdio(
globalThis: *JSC.JSGlobalObject,
- i: usize,
+ i: u32,
value: JSValue,
stdio_array: []Stdio,
) bool {
@@ -1776,16 +1784,16 @@ pub const Subprocess = struct {
const fd = @as(bun.FileDescriptor, @intCast(fd_));
- switch (@as(std.os.fd_t, @intCast(i))) {
- std.os.STDIN_FILENO => {
- if (i == std.os.STDERR_FILENO or i == std.os.STDOUT_FILENO) {
+ switch (bun.FDTag.get(fd)) {
+ .stdin => {
+ if (i == bun.STDERR_FD or i == bun.STDOUT_FD) {
globalThis.throwInvalidArguments("stdin cannot be used for stdout or stderr", .{});
return false;
}
},
- std.os.STDOUT_FILENO, std.os.STDERR_FILENO => {
- if (i == std.os.STDIN_FILENO) {
+ .stdout, .stderr => {
+ if (i == bun.STDIN_FD) {
globalThis.throwInvalidArguments("stdout and stderr cannot be used for stdin", .{});
return false;
}
@@ -1806,7 +1814,7 @@ pub const Subprocess = struct {
return extractStdioBlob(globalThis, req.getBodyValue().useAsAnyBlob(), i, stdio_array);
} else if (JSC.WebCore.ReadableStream.fromJS(value, globalThis)) |req_const| {
var req = req_const;
- if (i == std.os.STDIN_FILENO) {
+ if (i == bun.STDIN_FD) {
if (req.toAnyBlob(globalThis)) |blob| {
return extractStdioBlob(globalThis, blob, i, stdio_array);
}
diff --git a/src/bun.js/api/ffi.zig b/src/bun.js/api/ffi.zig
index 1b265af11..097b66d35 100644
--- a/src/bun.js/api/ffi.zig
+++ b/src/bun.js/api/ffi.zig
@@ -25,7 +25,7 @@ const ServerEntryPoint = bun.bundler.ServerEntryPoint;
const js_printer = bun.js_printer;
const js_parser = bun.js_parser;
const js_ast = bun.JSAst;
-const http = @import("../../http.zig");
+const http = @import("../../bun_dev_http_server.zig");
const NodeFallbackModules = @import("../../node_fallbacks.zig");
const ImportKind = ast.ImportKind;
const Analytics = @import("../../analytics/analytics_thread.zig");
@@ -722,7 +722,7 @@ pub const FFI = struct {
var runtime_path = std.fs.path.join(default_allocator, &[_]string{ dir, "FFI.h" }) catch unreachable;
const file = std.fs.openFileAbsolute(runtime_path, .{}) catch @panic("Missing bun/src/bun.js/api/FFI.h.");
defer file.close();
- return file.readToEndAlloc(default_allocator, (file.stat() catch unreachable).size) catch unreachable;
+ return file.readToEndAlloc(default_allocator, file.getEndPos() catch unreachable) catch unreachable;
} else {
return FFI_HEADER;
}
@@ -937,7 +937,7 @@ pub const FFI = struct {
var ffi_wrapper = Bun__createFFICallbackFunction(js_context, js_function);
try this.printCallbackSourceCode(js_context, ffi_wrapper, &source_code_writer);
- if (comptime Environment.allow_assert) {
+ if (comptime Environment.allow_assert and Environment.isPosix) {
debug_write: {
const fd = std.os.open("/tmp/bun-ffi-callback-source.c", std.os.O.WRONLY | std.os.O.CREAT, 0o644) catch break :debug_write;
_ = std.os.write(fd, source_code.items) catch break :debug_write;
diff --git a/src/bun.js/api/filesystem_router.zig b/src/bun.js/api/filesystem_router.zig
index c15826c31..f33066298 100644
--- a/src/bun.js/api/filesystem_router.zig
+++ b/src/bun.js/api/filesystem_router.zig
@@ -1,6 +1,6 @@
const std = @import("std");
const Api = @import("../../api/schema.zig").Api;
-const http = @import("../../http.zig");
+const http = @import("../../bun_dev_http_server.zig");
const JavaScript = @import("../javascript.zig");
const QueryStringMap = @import("../../url.zig").QueryStringMap;
const CombinedScanner = @import("../../url.zig").CombinedScanner;
diff --git a/src/bun.js/api/html_rewriter.zig b/src/bun.js/api/html_rewriter.zig
index fd0b5dd9e..86c2c4b53 100644
--- a/src/bun.js/api/html_rewriter.zig
+++ b/src/bun.js/api/html_rewriter.zig
@@ -199,7 +199,7 @@ pub const HTMLRewriter = struct {
this.finalized = true;
}
- pub fn fail(this: *HTMLRewriterLoader, err: JSC.Node.Syscall.Error) void {
+ pub fn fail(this: *HTMLRewriterLoader, err: bun.sys.Error) void {
this.signal.close(err);
this.output.end(err);
this.failed = true;
@@ -213,7 +213,7 @@ pub const HTMLRewriter = struct {
pub fn writeToDestination(this: *HTMLRewriterLoader, bytes: []const u8) void {
if (this.backpressure.count > 0) {
this.backpressure.write(bytes) catch {
- this.fail(JSC.Node.Syscall.Error.oom);
+ this.fail(bun.sys.Error.oom);
this.finalize();
};
return;
@@ -287,9 +287,9 @@ pub const HTMLRewriter = struct {
return JSC.WebCore.Sink.init(this);
}
- fn writeBytes(this: *HTMLRewriterLoader, bytes: bun.ByteList, comptime deinit_: bool) ?JSC.Node.Syscall.Error {
+ fn writeBytes(this: *HTMLRewriterLoader, bytes: bun.ByteList, comptime deinit_: bool) ?bun.sys.Error {
this.rewriter.write(bytes.slice()) catch {
- return JSC.Node.Syscall.Error{
+ return bun.sys.Error{
.errno = 1,
// TODO: make this a union
.path = bun.default_allocator.dupe(u8, LOLHTML.HTMLString.lastError().slice()) catch unreachable,
diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig
index 01f06ebf2..ffe24f0f6 100644
--- a/src/bun.js/api/server.zig
+++ b/src/bun.js/api/server.zig
@@ -24,7 +24,7 @@ const ServerEntryPoint = bun.bundler.ServerEntryPoint;
const js_printer = bun.js_printer;
const js_parser = bun.js_parser;
const js_ast = bun.JSAst;
-const http = @import("../../http.zig");
+const http = @import("../../bun_dev_http_server.zig");
const NodeFallbackModules = @import("../../node_fallbacks.zig");
const ImportKind = ast.ImportKind;
const Analytics = @import("../../analytics/analytics_thread.zig");
@@ -80,7 +80,7 @@ const Blob = JSC.WebCore.Blob;
const BoringSSL = @import("root").bun.BoringSSL;
const Arena = @import("../../mimalloc_arena.zig").Arena;
const SendfileContext = struct {
- fd: i32,
+ fd: bun.FileDescriptor,
socket_fd: i32 = 0,
remain: Blob.SizeType = 0,
offset: Blob.SizeType = 0,
@@ -1182,7 +1182,8 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
defer_deinit_until_callback_completes: ?*bool = null,
// TODO: support builtin compression
- const can_sendfile = !ssl_enabled;
+ // TODO: Use TransmitFile on Windows
+ const can_sendfile = !ssl_enabled and !bun.Environment.isWindows;
pub inline fn isAsync(this: *const RequestContext) bool {
return this.defer_deinit_until_callback_completes == null;
@@ -1744,7 +1745,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
}
// use node syscall so that we don't segfault on BADF
if (this.sendfile.auto_close)
- _ = JSC.Node.Syscall.close(this.sendfile.fd);
+ _ = bun.sys.close(this.sendfile.fd);
this.sendfile = undefined;
this.finalize();
}
@@ -1837,8 +1838,9 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
return true;
}
- pub fn sendWritableBytesForBlob(this: *RequestContext, bytes_: []const u8, write_offset: c_ulong, resp: *App.Response) bool {
+ pub fn sendWritableBytesForBlob(this: *RequestContext, bytes_: []const u8, write_offset_: c_ulong, resp: *App.Response) bool {
std.debug.assert(this.resp == resp);
+ const write_offset: usize = write_offset_;
var bytes = bytes_[@min(bytes_.len, @as(usize, @truncate(write_offset)))..];
if (resp.tryEnd(bytes, bytes_.len, this.shouldCloseConnection())) {
@@ -1851,7 +1853,8 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
}
}
- pub fn sendWritableBytesForCompleteResponseBuffer(this: *RequestContext, bytes_: []const u8, write_offset: c_ulong, resp: *App.Response) bool {
+ pub fn sendWritableBytesForCompleteResponseBuffer(this: *RequestContext, bytes_: []const u8, write_offset_: c_ulong, resp: *App.Response) bool {
+ const write_offset: usize = write_offset_;
std.debug.assert(this.resp == resp);
var bytes = bytes_[@min(bytes_.len, @as(usize, @truncate(write_offset)))..];
@@ -1882,7 +1885,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
const auto_close = file.pathlike != .fd;
const fd = if (!auto_close)
file.pathlike.fd
- else switch (JSC.Node.Syscall.open(file.pathlike.path.sliceZ(&file_buf), std.os.O.RDONLY | std.os.O.NONBLOCK | std.os.O.CLOEXEC, 0)) {
+ else switch (bun.sys.open(file.pathlike.path.sliceZ(&file_buf), std.os.O.RDONLY | std.os.O.NONBLOCK | std.os.O.CLOEXEC, 0)) {
.result => |_fd| _fd,
.err => |err| return this.runErrorHandler(err.withPath(file.pathlike.path.slice()).toSystemError().toErrorInstance(
this.server.globalThis,
@@ -1890,27 +1893,27 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
};
// stat only blocks if the target is a file descriptor
- const stat: std.os.Stat = switch (JSC.Node.Syscall.fstat(fd)) {
+ const stat: bun.Stat = switch (bun.sys.fstat(fd)) {
.result => |result| result,
.err => |err| {
this.runErrorHandler(err.withPathLike(file.pathlike).toSystemError().toErrorInstance(
this.server.globalThis,
));
if (auto_close) {
- _ = JSC.Node.Syscall.close(fd);
+ _ = bun.sys.close(fd);
}
return;
},
};
if (Environment.isMac) {
- if (!std.os.S.ISREG(stat.mode)) {
+ if (!bun.isRegularFile(stat.mode)) {
if (auto_close) {
- _ = JSC.Node.Syscall.close(fd);
+ _ = bun.sys.close(fd);
}
- var err = JSC.Node.Syscall.Error{
- .errno = @as(JSC.Node.Syscall.Error.Int, @intCast(@intFromEnum(std.os.E.INVAL))),
+ var err = bun.sys.Error{
+ .errno = @as(bun.sys.Error.Int, @intCast(@intFromEnum(std.os.E.INVAL))),
.syscall = .sendfile,
};
var sys = err.withPathLike(file.pathlike).toSystemError();
@@ -1923,13 +1926,13 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
}
if (Environment.isLinux) {
- if (!(std.os.S.ISREG(stat.mode) or std.os.S.ISFIFO(stat.mode))) {
+ if (!(bun.isRegularFile(stat.mode) or std.os.S.ISFIFO(stat.mode))) {
if (auto_close) {
- _ = JSC.Node.Syscall.close(fd);
+ _ = bun.sys.close(fd);
}
- var err = JSC.Node.Syscall.Error{
- .errno = @as(JSC.Node.Syscall.Error.Int, @intCast(@intFromEnum(std.os.E.INVAL))),
+ var err = bun.sys.Error{
+ .errno = @as(bun.sys.Error.Int, @intCast(@intFromEnum(std.os.E.INVAL))),
.syscall = .sendfile,
};
var sys = err.withPathLike(file.pathlike).toSystemError();
@@ -1943,7 +1946,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
const original_size = this.blob.Blob.size;
const stat_size = @as(Blob.SizeType, @intCast(stat.size));
- this.blob.Blob.size = if (std.os.S.ISREG(stat.mode))
+ this.blob.Blob.size = if (bun.isRegularFile(stat.mode))
stat_size
else
@min(original_size, stat_size);
@@ -1961,12 +1964,12 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
// if we are sending only part of a file, include the content-range header
// only include content-range automatically when using a file path instead of an fd
// this is to better support manually controlling the behavior
- if (std.os.S.ISREG(stat.mode) and auto_close) {
+ if (bun.isRegularFile(stat.mode) and auto_close) {
this.flags.needs_content_range = (this.sendfile.remain -| this.sendfile.offset) != stat_size;
}
// we know the bounds when we are sending a regular file
- if (std.os.S.ISREG(stat.mode)) {
+ if (bun.isRegularFile(stat.mode)) {
this.sendfile.offset = @min(this.sendfile.offset, stat_size);
this.sendfile.remain = @min(@max(this.sendfile.remain, this.sendfile.offset), stat_size) -| this.sendfile.offset;
}
@@ -2035,7 +2038,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
// this is used by content-range
this.sendfile = .{
- .fd = @as(i32, @truncate(bun.invalid_fd)),
+ .fd = bun.invalid_fd,
.remain = @as(Blob.SizeType, @truncate(result.result.buf.len)),
.offset = this.blob.Blob.offset,
.auto_close = false,