From a0082928b0f4d8095404e769534ef0e9d4d35a54 Mon Sep 17 00:00:00 2001 From: Vortex Date: Wed, 6 Mar 2024 08:08:33 -0600 Subject: [PATCH] Fixed File.readSync --- src/emulation/callbacks.ts | 11 ----------- src/emulation/sync.ts | 5 ----- src/file.ts | 17 +++++++++++------ 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/src/emulation/callbacks.ts b/src/emulation/callbacks.ts index 776f6ed91..cbd216c9d 100644 --- a/src/emulation/callbacks.ts +++ b/src/emulation/callbacks.ts @@ -146,11 +146,6 @@ open satisfies Omit; /** * Asynchronously reads the entire contents of a file. - * @example Usage example - * fs.readFile('/etc/passwd', function (err, data) { - * if (err) throw err; - * console.log(data); - * }); * @param filename * @param options * @option options encoding The string encoding for the file contents. Defaults to `null`. @@ -632,12 +627,6 @@ lutimes satisfies Omit; * Asynchronous `realpath`. The callback gets two arguments * `(err, resolvedPath)`. May use `process.cwd` to resolve relative paths. * - * @example Usage example - * fs.realpath('/etc/passwd', function (err, resolvedPath) { - * if (err) throw err; - * console.log(resolvedPath); - * }); - * * @param path * @param callback */ diff --git a/src/emulation/sync.ts b/src/emulation/sync.ts index ef5d37aab..154787b91 100644 --- a/src/emulation/sync.ts +++ b/src/emulation/sync.ts @@ -303,11 +303,6 @@ function _appendFileSync(fname: string, data: Uint8Array, flag: string, mode: nu * Asynchronously append data to a file, creating the file if it not yet * exists. * - * @example Usage example - * fs.appendFile('message.txt', 'data to append', function (err) { - * if (err) throw err; - * console.log('The "data to append" was appended to file!'); - * }); * @param filename * @param data * @param options diff --git a/src/file.ts b/src/file.ts index 6c1b45815..e3c69ed34 100644 --- a/src/file.ts +++ b/src/file.ts @@ -623,14 +623,19 @@ export abstract class PreloadFile extends File { throw new ApiError(ErrorCode.EPERM, 'File not opened with a readable mode.'); } position ??= this.position; - const endRead = position + length; - if (endRead > this.stats.size) { - length = this.stats.size - position; + let end = position + length; + if (end > this.stats.size) { + end = position + Math.max(this.stats.size - position, 0); } - buffer.set(this._buffer.slice(position, position + length), offset); this.stats.atimeMs = Date.now(); - this._position = position + length; - return this.buffer.length; + this._position = end; + const bytesRead = end - position; + if (bytesRead == 0) { + // No copy/read. Return immediatly for better performance + return bytesRead; + } + buffer.set(this._buffer.slice(position, end), offset); + return bytesRead; } /**