Skip to content

Commit

Permalink
Fixed File.readSync
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Mar 6, 2024
1 parent d503a46 commit a008292
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
11 changes: 0 additions & 11 deletions src/emulation/callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,6 @@ open satisfies Omit<typeof Node.open, '__promisify__'>;

/**
* 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`.
Expand Down Expand Up @@ -632,12 +627,6 @@ lutimes satisfies Omit<typeof Node.lutimes, '__promisify__'>;
* 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
*/
Expand Down
5 changes: 0 additions & 5 deletions src/emulation/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 11 additions & 6 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,14 +623,19 @@ export abstract class PreloadFile<T extends FileSystem> 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;
}

/**
Expand Down

0 comments on commit a008292

Please sign in to comment.