aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/node/node_fs.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/bun.js/node/node_fs.zig')
-rw-r--r--src/bun.js/node/node_fs.zig71
1 files changed, 61 insertions, 10 deletions
diff --git a/src/bun.js/node/node_fs.zig b/src/bun.js/node/node_fs.zig
index f1a0e6a82..881d4a483 100644
--- a/src/bun.js/node/node_fs.zig
+++ b/src/bun.js/node/node_fs.zig
@@ -2342,6 +2342,11 @@ const Return = struct {
}
};
pub const ReadFile = JSC.Node.StringOrNodeBuffer;
+ pub const ReadFileWithOptions = union(enum) {
+ string: string,
+ buffer: JSC.Node.Buffer,
+ null_terminated: [:0]const u8,
+ };
pub const Readlink = StringOrBuffer;
pub const Realpath = StringOrBuffer;
pub const RealpathNative = Realpath;
@@ -3287,7 +3292,33 @@ pub const NodeFS = struct {
return Maybe(Return.Readdir).todo;
}
+
+ pub const StringType = enum {
+ default,
+ null_terminated,
+ };
+
pub fn readFile(this: *NodeFS, args: Arguments.ReadFile, comptime flavor: Flavor) Maybe(Return.ReadFile) {
+ const ret = readFileWithOptions(this, args, flavor, .default);
+ return switch (ret) {
+ .err => .{ .err = ret.err },
+ .result => switch (ret.result) {
+ .buffer => .{
+ .result = .{
+ .buffer = ret.result.buffer,
+ },
+ },
+ .string => .{
+ .result = .{
+ .string = ret.result.string,
+ },
+ },
+ else => unreachable,
+ },
+ };
+ }
+
+ pub fn readFileWithOptions(this: *NodeFS, args: Arguments.ReadFile, comptime flavor: Flavor, comptime string_type: StringType) Maybe(Return.ReadFileWithOptions) {
var path: [:0]const u8 = undefined;
switch (comptime flavor) {
.sync => {
@@ -3341,7 +3372,7 @@ pub const NodeFS = struct {
),
0,
),
- );
+ ) + if (comptime string_type == .null_terminated) 1 else 0;
var buf = std.ArrayList(u8).init(bun.default_allocator);
buf.ensureTotalCapacityPrecise(size + 16) catch unreachable;
@@ -3391,7 +3422,7 @@ pub const NodeFS = struct {
}
}
- buf.items.len = total;
+ buf.items.len = if (comptime string_type == .null_terminated) total + 1 else total;
if (total == 0) {
buf.deinit();
return switch (args.encoding) {
@@ -3400,10 +3431,20 @@ pub const NodeFS = struct {
.buffer = Buffer.empty,
},
},
- else => .{
- .result = .{
- .string = "",
- },
+ else => brk: {
+ if (comptime string_type == .default) {
+ break :brk .{
+ .result = .{
+ .string = "",
+ },
+ };
+ } else {
+ break :brk .{
+ .result = .{
+ .null_terminated = "",
+ },
+ };
+ }
},
};
}
@@ -3414,10 +3455,20 @@ pub const NodeFS = struct {
.buffer = Buffer.fromBytes(buf.items, bun.default_allocator, .Uint8Array),
},
},
- else => .{
- .result = .{
- .string = buf.items,
- },
+ else => brk: {
+ if (comptime string_type == .default) {
+ break :brk .{
+ .result = .{
+ .string = buf.items,
+ },
+ };
+ } else {
+ break :brk .{
+ .result = .{
+ .null_terminated = buf.toOwnedSliceSentinel(0) catch unreachable,
+ },
+ };
+ }
},
};
},