diff options
author | 2022-11-22 21:25:26 -0800 | |
---|---|---|
committer | 2022-11-22 21:25:26 -0800 | |
commit | 4f41c3fb40a7cd23a48c1a888353059a69693fa8 (patch) | |
tree | 03d543e4471514c4e87ffbc371d5c60015339793 /src | |
parent | b249ed7257777f2d9b5fe18ba09694d75f0d0a90 (diff) | |
download | bun-4f41c3fb40a7cd23a48c1a888353059a69693fa8.tar.gz bun-4f41c3fb40a7cd23a48c1a888353059a69693fa8.tar.zst bun-4f41c3fb40a7cd23a48c1a888353059a69693fa8.zip |
[FileSystemRouter] Fix failing tests
Diffstat (limited to 'src')
-rw-r--r-- | src/bun.js/api/filesystem_router.zig | 5 | ||||
-rw-r--r-- | src/bun.js/bindings/bindings.zig | 4 | ||||
-rw-r--r-- | src/global.zig | 11 | ||||
-rw-r--r-- | src/string_immutable.zig | 29 | ||||
-rw-r--r-- | src/url.zig | 13 |
5 files changed, 52 insertions, 10 deletions
diff --git a/src/bun.js/api/filesystem_router.zig b/src/bun.js/api/filesystem_router.zig index 96c094b5c..f43a30d4e 100644 --- a/src/bun.js/api/filesystem_router.zig +++ b/src/bun.js/api/filesystem_router.zig @@ -290,6 +290,7 @@ pub const FileSystemRouter = struct { .arena = arena, .allocator = allocator, }; + router.config.dir = fs_router.base_dir.?.slice(); fs_router.base_dir.?.ref(); return fs_router; } @@ -701,8 +702,10 @@ pub const MatchedRoute = struct { this: *MatchedRoute, globalThis: *JSC.JSGlobalObject, ) callconv(.C) JSC.JSValue { - if (this.route.query_string.len == 0) { + if (this.route.query_string.len == 0 and this.route.params.len == 0) { return JSValue.createEmptyObject(globalThis, 0); + } else if (this.route.query_string.len == 0) { + return this.getParams(globalThis); } if (this.query_string_map == null) { diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index c88e0ba8f..b769625ec 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -236,8 +236,8 @@ pub const ZigString = extern struct { } pub fn cloneWithTrailingSlash(this: Slice, allocator: std.mem.Allocator) !Slice { - var duped = try std.fmt.allocPrintZ(allocator, "{s}/", .{strings.withoutTrailingSlash(this.slice())}); - return Slice{ .allocator = NullableAllocator.init(allocator), .ptr = duped.ptr, .len = @truncate(u32, duped.len) }; + var buf = try strings.cloneNormalizingSeparators(allocator, this.slice()); + return Slice{ .allocator = NullableAllocator.init(allocator), .ptr = buf.ptr, .len = @truncate(u32, buf.len) }; } pub fn cloneZ(this: Slice, allocator: std.mem.Allocator) !Slice { diff --git a/src/global.zig b/src/global.zig index c75a72891..32ef4214f 100644 --- a/src/global.zig +++ b/src/global.zig @@ -413,3 +413,14 @@ pub const Mimalloc = @import("./allocators/mimalloc.zig"); pub fn isSliceInBuffer(slice: []const u8, buffer: []const u8) bool { return slice.len > 0 and @ptrToInt(buffer.ptr) <= @ptrToInt(slice.ptr) and ((@ptrToInt(slice.ptr) + slice.len) <= (@ptrToInt(buffer.ptr) + buffer.len)); } + +pub fn rangeOfSliceInBuffer(slice: []const u8, buffer: []const u8) ?[2]u32 { + if (!isSliceInBuffer(slice, buffer)) return null; + const r = [_]u32{ + @truncate(u32, @ptrToInt(slice.ptr) -| @ptrToInt(buffer.ptr)), + @truncate(u32, slice.len), + }; + if (comptime Environment.allow_assert) + std.debug.assert(strings.eqlLong(slice, buffer[r[0]..][0..r[1]], false)); + return r; +} diff --git a/src/string_immutable.zig b/src/string_immutable.zig index 654080f8a..5acc5befd 100644 --- a/src/string_immutable.zig +++ b/src/string_immutable.zig @@ -3762,3 +3762,32 @@ pub fn isIPAddress(input: []const u8) bool { return false; } } + +pub fn cloneNormalizingSeparators( + allocator: std.mem.Allocator, + input: []const u8, +) ![]u8 { + // remove duplicate slashes in the file path + var base = withoutTrailingSlash(input); + var tokenized = std.mem.tokenize(u8, base, std.fs.path.sep_str); + var buf = try allocator.alloc(u8, base.len + 2); + std.debug.assert(base.len > 0); + if (base[0] == std.fs.path.sep) { + buf[0] = std.fs.path.sep; + } + var remain = buf[@as(usize, @boolToInt(base[0] == std.fs.path.sep))..]; + + while (tokenized.next()) |token| { + if (token.len == 0) continue; + std.mem.copy(u8, remain, token); + remain[token.len..][0] = std.fs.path.sep; + remain = remain[token.len + 1 ..]; + } + if ((remain.ptr - 1) != buf.ptr and (remain.ptr - 1)[0] != std.fs.path.sep) { + remain[0] = std.fs.path.sep; + remain = remain[1..]; + } + remain[0] = 0; + + return buf[0 .. @ptrToInt(remain.ptr) - @ptrToInt(buf.ptr)]; +} diff --git a/src/url.zig b/src/url.zig index 97ef74125..26c606a4d 100644 --- a/src/url.zig +++ b/src/url.zig @@ -859,15 +859,14 @@ pub const CombinedScanner = struct { fn stringPointerFromStrings(parent: string, in: string) Api.StringPointer { if (in.len == 0 or parent.len == 0) return Api.StringPointer{}; - if (bun.isSliceInBuffer(in, parent)) { - const offset = @minimum( - @maximum(@ptrToInt(in.ptr), @ptrToInt(parent.ptr)) - @minimum(@ptrToInt(in.ptr), @ptrToInt(parent.ptr)), - @minimum(in.len, parent.len), - ); - - return Api.StringPointer{ .offset = @truncate(u32, offset), .length = @truncate(u32, in.len) }; + if (bun.rangeOfSliceInBuffer(in, parent)) |range| { + return Api.StringPointer{ .offset = range[0], .length = range[1] }; } else { if (strings.indexOf(parent, in)) |i| { + if (comptime Environment.allow_assert) { + std.debug.assert(strings.eqlLong(parent[i..][0..in.len], in, false)); + } + return Api.StringPointer{ .offset = @truncate(u32, i), .length = @truncate(u32, in.len), |