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 necessary (@aduh95) and
check that the data was actually written in the
unit test

Refs: nodejs#35922
Refs: nodejs#35240
  • Loading branch information
mmomtchev committed Dec 2, 2020
1 parent 6662fb3 commit 7713743
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const {
PromiseResolve,
Symbol,
Uint8Array,
PromisePrototypeThen,
ReflectApply
} = primordials;

const {
Expand Down
8 changes: 5 additions & 3 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const {
ObjectSetPrototypeOf,
ReflectApply,
Symbol,
PromisePrototypeThen,
FunctionPrototypeBind
} = primordials;

const {
Expand Down Expand Up @@ -82,17 +84,17 @@ const FileHandleOperations = (handle) => {
},
read: (fd, buf, offset, length, pos, cb) => {
PromisePrototypeThen(handle.read(buf, offset, length, pos),
(r) => cb(null, r.bytesRead, r.buffer),
(r) => cb(null, r.bytesRead, r.buffer),
(err) => cb(err, 0, buf));
},
write: (fd, buf, offset, length, pos, cb) => {
PromisePrototypeThen(handle.write(buf, offset, length, pos),
(r) => cb(null, r.bytesWritten, r.buffer),
(r) => cb(null, r.bytesWritten, r.buffer),
(err) => cb(err, 0, buf));
},
writev: (fd, buffers, pos, cb) => {
PromisePrototypeThen(handle.writev(buffers, pos),
(r) => cb(null, r.bytesWritten, r.buffers),
(r) => cb(null, r.bytesWritten, r.buffers),
(err) => cb(err, 0, buffers));
}
};
Expand Down
6 changes: 5 additions & 1 deletion test/parallel/test-fs-write-stream-file-handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const common = require('../common');
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const tmpdir = require('../common/tmpdir');
const file = path.join(tmpdir.path, '/write_stream_filehandle_test.txt');
const input = 'hello world';
Expand All @@ -13,5 +14,8 @@ fs.promises.open(file, 'w+').then((handle) => {
const stream = fs.createWriteStream(null, { fd: handle });

stream.end(input);
stream.on('close', common.mustCall());
stream.on('close', common.mustCall(() => {
const output = fs.readFileSync(file, 'utf-8');
assert.strictEqual(output, input);
}));
});

0 comments on commit 7713743

Please sign in to comment.