Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow Uint8Arrays to be passed to FsWriteStream #842

Merged
merged 1 commit into from May 28, 2022

Conversation

MrMYHuang
Copy link
Contributor

From the Volume write function declaration, we can see the second argument has a type of Buffer or Uint8Array.

memfs/src/volume.ts

Lines 1211 to 1218 in 0587738

write(
fd: number,
buffer: Buffer | Uint8Array,
offset: number,
length: number,
position: number,
callback: (...args) => void,
);

However, FsWriteStream.prototype._write only allows a type of Buffer:

memfs/src/volume.ts

Lines 2569 to 2591 in 0587738

FsWriteStream.prototype._write = function(data, encoding, cb) {
if (!(data instanceof Buffer)) return this.emit('error', new Error('Invalid data'));
if (typeof this.fd !== 'number') {
return this.once('open', function() {
this._write(data, encoding, cb);
});
}
var self = this; // tslint:disable-line no-this-assignment
this._vol.write(this.fd, data, 0, data.length, this.pos, (er, bytes) => {
if (er) {
if (self.autoClose && self.destroy) {
self.destroy();
}
return cb(er);
}
self.bytesWritten += bytes;
cb();
});
if (this.pos !== undefined) this.pos += data.length;
};

This issue does cause an error of the combination: webpack 5 + node-downloader-helper + memfs.
node-downloader-helper and its dependencies can call FsWriteStream.prototype._write with a data of type Uint8Array.
(If anyone has an interest for an example, please try my project https://github.com/MrMYHuang/taa with memfs 3.4.3.)

After applying my changes, this issues is fixed.

@G-Rath G-Rath changed the title fix: FsWriteStream.prototype._write instanceof checks. fix: allow Uint8Arrays to be passed to FsWriteStream May 28, 2022
@G-Rath G-Rath merged commit 4398992 into streamich:master May 28, 2022
streamich pushed a commit that referenced this pull request May 28, 2022
## [3.4.4](v3.4.3...v3.4.4) (2022-05-28)

### Bug Fixes

* allow `Uint8Array`s to be passed to `FsWriteStream` ([#842](#842)) ([4398992](4398992))
@streamich
Copy link
Owner

🎉 This PR is included in version 3.4.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants