Skip to content

Commit

Permalink
improve readFileUtf8 binding
Browse files Browse the repository at this point in the history
  • Loading branch information
Caleb ツ Everett committed Sep 25, 2024
1 parent a6fbf84 commit a5eb15b
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions lib/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,28 +844,43 @@ Binding.prototype.readdir = function (
});
};

Binding.prototype.readFile = function (filepath, options, callback, ctx) {
markSyscall(ctx, 'readFile');

return maybeCallback(normalizeCallback(callback), ctx, this, function () {
filepath = deBuffer(filepath);
const item = this._system.getItem(filepath);

return item.getContent();
});
};
/**
* Read file as utf8 string.
* @param {string} name file to write.
* @param {number} flags Flags.
* @return {string} the file content.
*/
Binding.prototype.readFileUtf8 = function (name, flags) {
const fd = this.open(name, flags);
const descriptor = this.getDescriptorById(fd);

Binding.prototype.readFileSync = function (filepath, options, ctx) {
return this.readFile(filepath, options, undefined, ctx);
};
Binding.prototype.readFileUtf8 = function (filepath, options, ctx) {
return this.readFile(filepath, options, undefined, ctx).toString('utf-8');
if (!descriptor.isRead()) {
throw new FSError('EBADF');
}
const file = descriptor.getItem();
if (file instanceof Directory) {
throw new FSError('EISDIR');
}
if (!(file instanceof File)) {
// deleted or not a regular file
throw new FSError('EBADF');
}
const content = file.getContent();
return content.toString('utf8');
};

/**
* Write a utf8 string.
* @param {string} filepath file to write.
* @param {string} data data to write to filepath.
* @param {number} flags Flags.
* @param {number} mode Mode.
*/
Binding.prototype.writeFileUtf8 = function (filepath, data, flags, mode) {
const destFd = this.open(filepath, flags, mode);
this.writeBuffer(destFd, data, 0, data.length);
};

/**
* Create a directory.
* @param {string} pathname Path to new directory.
Expand Down

0 comments on commit a5eb15b

Please sign in to comment.