Skip to content

Commit

Permalink
stream: support FileHandle for Read/WriteStream
Browse files Browse the repository at this point in the history
Use primordials where applicable (@aduh95)

Co-authored-by: Antoine du Hamel <[email protected]>
  • Loading branch information
mmomtchev and aduh95 committed Dec 2, 2020
1 parent 4a841ac commit 6662fb3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ for (const op of ['on', 'emit', 'once', 'off', 'addListener',
'prependListener', 'removeListener', 'removeAllListeners',
'listeners', 'rawListeners'])
FileHandle.prototype[op] = function(...args) {
this[kEmitter][op].apply(this[kEmitter], args);
ReflectApply(this[kEmitter][op], this[kEmitter], args);
};

async function fsCall(fn, handle, ...args) {
Expand Down
25 changes: 12 additions & 13 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,23 @@ const FileHandleOperations = (handle) => {
},
close: (fd, cb) => {
handle[kUnref]();
handle.close()
.then(() => cb())
.catch((err) => cb(err));
PromisePrototypeThen(handle.close(),
() => cb(), cb);
},
read: (fd, buf, offset, length, pos, cb) => {
handle.read(buf, offset, length, pos)
.then((r) => cb(null, r.bytesRead, r.buffer))
.catch((err) => cb(err, 0, buf));
PromisePrototypeThen(handle.read(buf, offset, length, pos),
(r) => cb(null, r.bytesRead, r.buffer),
(err) => cb(err, 0, buf));
},
write: (fd, buf, offset, length, pos, cb) => {
handle.read(buf, offset, length, pos)
.then((r) => cb(null, r.bytesWritten, r.buffer))
.catch((err) => cb(err, 0, buf));
PromisePrototypeThen(handle.write(buf, offset, length, pos),
(r) => cb(null, r.bytesWritten, r.buffer),
(err) => cb(err, 0, buf));
},
writev: (fd, buffers, pos, cb) => {
handle.writev(buffers, pos)
.then((r) => cb(null, r.bytesWritten, r.buffers))
.catch((err) => cb(err, 0, buffers));
PromisePrototypeThen(handle.writev(buffers, pos),
(r) => cb(null, r.bytesWritten, r.buffers),
(err) => cb(err, 0, buffers));
}
};
};
Expand Down Expand Up @@ -125,7 +124,7 @@ function importFd(stream, options) {
stream.fd = options.fd.fd;
stream[kFs] = FileHandleOperations(stream[kHandle]);
stream[kHandle][kRef]();
options.fd.on('close', stream.close.bind(stream));
options.fd.on('close', FunctionPrototypeBind(stream.close, stream));
}
// When fd is a raw descriptor, we must keep our fingers crossed
// that the descriptor won't get closed, or worse, replaced with
Expand Down

0 comments on commit 6662fb3

Please sign in to comment.