aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/api/server.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-08-28 04:39:16 -0700
committerGravatar GitHub <noreply@github.com> 2023-08-28 04:39:16 -0700
commite2a17344dc543c9c652cfe2b14cd2709dd6cfd22 (patch)
treefe93965d39886494aee12dca71bdcf2a991d806f /src/bun.js/api/server.zig
parentefe987e8d12e824dde840b56cbb704feabe26ed1 (diff)
downloadbun-e2a17344dc543c9c652cfe2b14cd2709dd6cfd22.tar.gz
bun-e2a17344dc543c9c652cfe2b14cd2709dd6cfd22.tar.zst
bun-e2a17344dc543c9c652cfe2b14cd2709dd6cfd22.zip
just kernel32 things (#4354)
* just kernel32 things * more * Update linux_c.zig * Update windows_c.zig * Add workaround Workaround https://github.com/ziglang/zig/issues/16980 * Rename http.zig to bun_dev_http_server.zig * Rename usages * more * more * more * thanks tigerbeetle * Rename `JSC.Node.Syscall` -> `bun.sys` * more * woops * more! * hmm * it says there are only 37 errors, but that's not true * populate argv * it says 32 errors! * 24 errors * fix regular build * 12 left! * Still 12 left! * more * 2 errors left... * 1 more error * Add link to Tigerbeetle * Fix the remainign error * Fix test timeout * Update syscall.zig --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/bun.js/api/server.zig')
-rw-r--r--src/bun.js/api/server.zig45
1 files changed, 24 insertions, 21 deletions
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,