-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
stream: add FileHandle support to Read/WriteStream #35922
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
Changes from all commits
72873cf
51640a5
4a841ac
6662fb3
7713743
d43eb37
e6eba34
7495c24
be4dc0b
b9c8218
bb8f483
9506ec8
cb0c20c
96d0198
7095132
ea37dd7
403a7aa
c267bc1
68f98fb
8d0a717
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,21 +2,25 @@ | |||||||||||||||||
|
|
||||||||||||||||||
| const { | ||||||||||||||||||
| Array, | ||||||||||||||||||
| FunctionPrototypeBind, | ||||||||||||||||||
| MathMin, | ||||||||||||||||||
| ObjectDefineProperty, | ||||||||||||||||||
| ObjectSetPrototypeOf, | ||||||||||||||||||
| PromisePrototypeThen, | ||||||||||||||||||
| ReflectApply, | ||||||||||||||||||
| Symbol, | ||||||||||||||||||
| } = primordials; | ||||||||||||||||||
|
|
||||||||||||||||||
| const { | ||||||||||||||||||
| ERR_INVALID_ARG_TYPE, | ||||||||||||||||||
| ERR_OUT_OF_RANGE | ||||||||||||||||||
| ERR_OUT_OF_RANGE, | ||||||||||||||||||
| ERR_METHOD_NOT_IMPLEMENTED, | ||||||||||||||||||
| } = require('internal/errors').codes; | ||||||||||||||||||
| const { deprecate } = require('internal/util'); | ||||||||||||||||||
| const { validateInteger } = require('internal/validators'); | ||||||||||||||||||
| const { errorOrDestroy } = require('internal/streams/destroy'); | ||||||||||||||||||
| const fs = require('fs'); | ||||||||||||||||||
| const { kRef, kUnref, FileHandle } = require('internal/fs/promises'); | ||||||||||||||||||
| const { Buffer } = require('buffer'); | ||||||||||||||||||
| const { | ||||||||||||||||||
| copyObject, | ||||||||||||||||||
|
|
@@ -28,6 +32,7 @@ const kIoDone = Symbol('kIoDone'); | |||||||||||||||||
| const kIsPerformingIO = Symbol('kIsPerformingIO'); | ||||||||||||||||||
|
|
||||||||||||||||||
| const kFs = Symbol('kFs'); | ||||||||||||||||||
| const kHandle = Symbol('kHandle'); | ||||||||||||||||||
|
|
||||||||||||||||||
| function _construct(callback) { | ||||||||||||||||||
| const stream = this; | ||||||||||||||||||
|
|
@@ -66,6 +71,35 @@ function _construct(callback) { | |||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // This generates an fs operations structure for a FileHandle | ||||||||||||||||||
| const FileHandleOperations = (handle) => { | ||||||||||||||||||
| return { | ||||||||||||||||||
| open: (path, flags, mode, cb) => { | ||||||||||||||||||
| throw new ERR_METHOD_NOT_IMPLEMENTED('open()'); | ||||||||||||||||||
| }, | ||||||||||||||||||
| close: (fd, cb) => { | ||||||||||||||||||
| handle[kUnref](); | ||||||||||||||||||
| PromisePrototypeThen(handle.close(), | ||||||||||||||||||
| () => cb(), cb); | ||||||||||||||||||
| }, | ||||||||||||||||||
| read: (fd, buf, offset, length, pos, cb) => { | ||||||||||||||||||
| 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) => { | ||||||||||||||||||
| PromisePrototypeThen(handle.write(buf, offset, length, pos), | ||||||||||||||||||
| (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), | ||||||||||||||||||
| (err) => cb(err, 0, buffers)); | ||||||||||||||||||
| } | ||||||||||||||||||
| }; | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| function close(stream, err, cb) { | ||||||||||||||||||
| if (!stream.fd) { | ||||||||||||||||||
| // TODO(ronag) | ||||||||||||||||||
|
|
@@ -80,6 +114,32 @@ function close(stream, err, cb) { | |||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| function importFd(stream, options) { | ||||||||||||||||||
| stream.fd = null; | ||||||||||||||||||
| if (options.fd) { | ||||||||||||||||||
| if (typeof options.fd === 'number') { | ||||||||||||||||||
| // When fd is a raw descriptor, we must keep our fingers crossed | ||||||||||||||||||
| // that the descriptor won't get closed, or worse, replaced with | ||||||||||||||||||
| // another one | ||||||||||||||||||
| // https://github.com/nodejs/node/issues/35862 | ||||||||||||||||||
| stream.fd = options.fd; | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
| } else if (typeof options.fd === 'object' && | ||||||||||||||||||
| options.fd instanceof FileHandle) { | ||||||||||||||||||
| // When fd is a FileHandle we can listen for 'close' events | ||||||||||||||||||
| if (options.fs) | ||||||||||||||||||
| // FileHandle is not supported with custom fs operations | ||||||||||||||||||
| throw new ERR_METHOD_NOT_IMPLEMENTED('FileHandle with fs'); | ||||||||||||||||||
| stream[kHandle] = options.fd; | ||||||||||||||||||
| stream.fd = options.fd.fd; | ||||||||||||||||||
| stream[kFs] = FileHandleOperations(stream[kHandle]); | ||||||||||||||||||
| stream[kHandle][kRef](); | ||||||||||||||||||
|
Comment on lines
132
to
135
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
| options.fd.on('close', FunctionPrototypeBind(stream.close, stream)); | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
| } else | ||||||||||||||||||
| throw ERR_INVALID_ARG_TYPE('options.fd', | ||||||||||||||||||
| ['number', 'FileHandle'], options.fd); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| function ReadStream(path, options) { | ||||||||||||||||||
| if (!(this instanceof ReadStream)) | ||||||||||||||||||
| return new ReadStream(path, options); | ||||||||||||||||||
|
|
@@ -115,10 +175,11 @@ function ReadStream(path, options) { | |||||||||||||||||
|
|
||||||||||||||||||
| // Path will be ignored when fd is specified, so it can be falsy | ||||||||||||||||||
| this.path = toPathIfFileURL(path); | ||||||||||||||||||
| this.fd = options.fd === undefined ? null : options.fd; | ||||||||||||||||||
| this.flags = options.flags === undefined ? 'r' : options.flags; | ||||||||||||||||||
| this.mode = options.mode === undefined ? 0o666 : options.mode; | ||||||||||||||||||
|
|
||||||||||||||||||
| importFd(this, options); | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that would not help a future debugging to have some properties defined in the different fashion than the others. What doo you think about creating a new internal class
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took extra care to be as compatible as possible, no internal properties have different meaning, ie
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| this.start = options.start; | ||||||||||||||||||
| this.end = options.end; | ||||||||||||||||||
| this.pos = undefined; | ||||||||||||||||||
|
|
@@ -287,10 +348,11 @@ function WriteStream(path, options) { | |||||||||||||||||
|
|
||||||||||||||||||
| // Path will be ignored when fd is specified, so it can be falsy | ||||||||||||||||||
| this.path = toPathIfFileURL(path); | ||||||||||||||||||
| this.fd = options.fd === undefined ? null : options.fd; | ||||||||||||||||||
| this.flags = options.flags === undefined ? 'w' : options.flags; | ||||||||||||||||||
| this.mode = options.mode === undefined ? 0o666 : options.mode; | ||||||||||||||||||
|
|
||||||||||||||||||
| importFd(this, options); | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand what would this accomplish, you would still see
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes you would.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand correctly (
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hum, I don't get what you mean, my objection is not about
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most functions that accept a stream as a first argument do change its state. They are in fact member-like functions of both classes.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, my objection remains (I still think a parent class would be preferable), but I'm not blocking this PR from landing. I'll open a follow up PR to propose my changes once this one has landed. |
||||||||||||||||||
|
|
||||||||||||||||||
| this.start = options.start; | ||||||||||||||||||
| this.pos = undefined; | ||||||||||||||||||
| this.bytesWritten = 0; | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| 'use strict'; | ||
| const common = require('../common'); | ||
| const fs = require('fs'); | ||
| const assert = require('assert'); | ||
| const path = require('path'); | ||
| const tmpdir = require('../common/tmpdir'); | ||
| const file = path.join(tmpdir.path, 'read_stream_filehandle_worker.txt'); | ||
| const input = 'hello world'; | ||
| const { Worker, isMainThread, workerData } = require('worker_threads'); | ||
|
|
||
| if (isMainThread || !workerData) { | ||
| tmpdir.refresh(); | ||
| fs.writeFileSync(file, input); | ||
|
|
||
| fs.promises.open(file, 'r').then((handle) => { | ||
| handle.on('close', common.mustNotCall()); | ||
| new Worker(__filename, { | ||
| workerData: { handle }, | ||
| transferList: [handle] | ||
| }); | ||
| }); | ||
| fs.promises.open(file, 'r').then((handle) => { | ||
| fs.createReadStream(null, { fd: handle }); | ||
| assert.throws(() => { | ||
| new Worker(__filename, { | ||
| workerData: { handle }, | ||
| transferList: [handle] | ||
| }); | ||
| }, { | ||
| code: 25, | ||
| }); | ||
| }); | ||
| } else { | ||
| let output = ''; | ||
|
|
||
| const handle = workerData.handle; | ||
| handle.on('close', common.mustCall()); | ||
| const stream = fs.createReadStream(null, { fd: handle }); | ||
|
|
||
| stream.on('data', common.mustCallAtLeast((data) => { | ||
| output += data; | ||
| })); | ||
|
|
||
| stream.on('end', common.mustCall(() => { | ||
| handle.close(); | ||
| assert.strictEqual(output, input); | ||
| })); | ||
|
|
||
| stream.on('close', common.mustCall()); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.