Skip to content

Commit

Permalink
Slightly refactor Bun.file().text() control flow (#7535)
Browse files Browse the repository at this point in the history
* Clean up control flow in Bun.file().* file reader

* Preallocate large files on Linux

* remove `this.wrote` field

* push

* doesnt work yet

* Create if not exists

* Fix for macOS

* Update blob.zig

* Slightly clean up this test

---------

Co-authored-by: Jarred Sumner <[email protected]>
  • Loading branch information
Jarred-Sumner and Jarred-Sumner authored Dec 10, 2023
1 parent ce63023 commit 300d17f
Show file tree
Hide file tree
Showing 8 changed files with 623 additions and 190 deletions.
62 changes: 57 additions & 5 deletions packages/bun-types/bun.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ declare module "bun" {
*
* Use the fastest syscalls available to copy from `input` into `destination`.
*
* If `destination` exists, it must be a regular file or symlink to a file.
* If `destination` exists, it must be a regular file or symlink to a file. If `destination`'s directory does not exist, it will be created by default.
*
* @param destination The file or file path to write to
* @param input The data to copy into `destination`.
Expand All @@ -295,8 +295,14 @@ declare module "bun" {
destination: BunFile | PathLike,
input: Blob | TypedArray | ArrayBufferLike | string | BlobPart[],
options?: {
/** If writing to a PathLike, set the permissions of the file. */
mode?: number;
/**
* If `true`, create the parent directory if it doesn't exist. By default, this is `true`.
*
* If `false`, this will throw an error if the directory doesn't exist.
*
* @default true
*/
createPath?: boolean;
},
): Promise<number>;

Expand All @@ -311,7 +317,20 @@ declare module "bun" {
* @param input - `Response` object
* @returns A promise that resolves with the number of bytes written.
*/
export function write(destination: BunFile, input: Response): Promise<number>;
export function write(
destination: BunFile,
input: Response,
options?: {
/**
* If `true`, create the parent directory if it doesn't exist. By default, this is `true`.
*
* If `false`, this will throw an error if the directory doesn't exist.
*
* @default true
*/
createPath?: boolean;
},
): Promise<number>;

/**
*
Expand All @@ -328,6 +347,16 @@ declare module "bun" {
export function write(
destinationPath: PathLike,
input: Response,
options?: {
/**
* If `true`, create the parent directory if it doesn't exist. By default, this is `true`.
*
* If `false`, this will throw an error if the directory doesn't exist.
*
* @default true
*/
createPath?: boolean;
},
): Promise<number>;

/**
Expand All @@ -350,7 +379,20 @@ declare module "bun" {
* @returns A promise that resolves with the number of bytes written.
*/
// tslint:disable-next-line:unified-signatures
export function write(destination: BunFile, input: BunFile): Promise<number>;
export function write(
destination: BunFile,
input: BunFile,
options?: {
/**
* If `true`, create the parent directory if it doesn't exist. By default, this is `true`.
*
* If `false`, this will throw an error if the directory doesn't exist.
*
* @default true
*/
createPath?: boolean;
},
): Promise<number>;

/**
*
Expand All @@ -375,6 +417,16 @@ declare module "bun" {
export function write(
destinationPath: PathLike,
input: BunFile,
options?: {
/**
* If `true`, create the parent directory if it doesn't exist. By default, this is `true`.
*
* If `false`, this will throw an error if the directory doesn't exist.
*
* @default true
*/
createPath?: boolean;
},
): Promise<number>;

export interface SystemError extends Error {
Expand Down
8 changes: 8 additions & 0 deletions src/bun.js/node/node_fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1868,6 +1868,7 @@ pub const Arguments = struct {
/// A file mode. If a string is passed, it is parsed as an octal integer. If not specified
/// @default
mode: Mode = 0o777,
return_empty_string: bool = false,

pub fn deinit(this: Mkdir) void {
this.path.deinit();
Expand Down Expand Up @@ -4190,6 +4191,9 @@ pub const NodeFS = struct {
}
},
.result => {
if (args.return_empty_string) {
return Option{ .result = bun.String.empty };
}
return Option{
.result = if (args.path == .slice_with_underlying_string)
args.path.slice_with_underlying_string.underlying
Expand Down Expand Up @@ -4282,6 +4286,10 @@ pub const NodeFS = struct {
}
},
.result => {
if (args.return_empty_string) {
return Option{ .result = bun.String.empty };
}

return Option{
.result = if (first_match != std.math.maxInt(u16))
bun.String.create(working_mem[0..first_match])
Expand Down
Loading

0 comments on commit 300d17f

Please sign in to comment.