Skip to content

Commit

Permalink
fix: set closed property correct on Node 18 (#836)
Browse files Browse the repository at this point in the history
* fix: setting closed flag on Node >= 18

* chore: use node 18 in CI

* address PR feedback + fix closing write stream
  • Loading branch information
dsokal committed May 17, 2022
1 parent 7e5f375 commit d1823e1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2
refs:
container: &container
docker:
- image: node:14
- image: node:18
working_directory: ~/repo
steps:
- &Versions
Expand Down
38 changes: 36 additions & 2 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2470,7 +2470,14 @@ FsReadStream.prototype.close = function(cb) {
return process.nextTick(() => this.emit('close'));
}

this.closed = true;
// Since Node 18, there is only a getter for '.closed'.
// The first branch mimics other setters from Readable.
// See https://github.com/nodejs/node/blob/v18.0.0/lib/internal/streams/readable.js#L1243
if (typeof this._readableState?.closed === 'boolean') {
this._readableState.closed = true;
} else {
this.closed = true;
}

this._vol.close(this.fd, er => {
if (er) this.emit('error', er);
Expand Down Expand Up @@ -2615,8 +2622,35 @@ FsWriteStream.prototype._writev = function(data, cb) {
if (this.pos !== undefined) this.pos += size;
};

FsWriteStream.prototype.close = function(cb) {
if (cb) this.once('close', cb);

if (this.closed || typeof this.fd !== 'number') {
if (typeof this.fd !== 'number') {
this.once('open', closeOnOpen);
return;
}
return process.nextTick(() => this.emit('close'));
}

// Since Node 18, there is only a getter for '.closed'.
// The first branch mimics other setters from Writable.
// See https://github.com/nodejs/node/blob/v18.0.0/lib/internal/streams/writable.js#L766
if (typeof this._writableState?.closed === 'boolean') {
this._writableState.closed = true;
} else {
this.closed = true;
}

this._vol.close(this.fd, er => {
if (er) this.emit('error', er);
else this.emit('close');
});

this.fd = null;
};

FsWriteStream.prototype._destroy = FsReadStream.prototype._destroy;
FsWriteStream.prototype.close = FsReadStream.prototype.close;

// There is no shutdown() for files.
FsWriteStream.prototype.destroySoon = FsWriteStream.prototype.end;
Expand Down

0 comments on commit d1823e1

Please sign in to comment.