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

stream: add FileHandle support to Read/WriteStream #35922

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
stream: Support FileHandle in Read/WriteStream
Include remarks by @addaleax and @aduh95
Move the double inheritance class generator to
the internal event_target
Restore deleted commas
Fix a slightly flaky test

Refs: #35922
  • Loading branch information
mmomtchev committed Dec 2, 2020
commit 7495c2490aa2ffc588eb5b85fe7be4645dc010e7
17 changes: 1 addition & 16 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const {
ErrorCaptureStackTrace,
MathMin,
NumberIsNaN,
Object,
ObjectCreate,
ObjectDefineProperty,
ObjectDefineProperties,
Expand All @@ -42,7 +41,7 @@ const {
String,
Symbol,
SymbolFor,
SymbolAsyncIterator
SymbolAsyncIterator,
mmomtchev marked this conversation as resolved.
Show resolved Hide resolved
} = primordials;
const kRejection = SymbolFor('nodejs.rejection');

Expand Down Expand Up @@ -897,17 +896,3 @@ function on(emitter, event, options) {
iterator.return();
}
}

const EventEmitterMixin = (Superclass) => {
class MixedEventEmitter extends Superclass {
constructor(...args) {
super(...args);
EventEmitter.call(this);
}
}
const protoProps = Object.getOwnPropertyDescriptors(EventEmitter.prototype);
delete protoProps.constructor;
Object.defineProperties(MixedEventEmitter.prototype, protoProps);
return MixedEventEmitter;
};
module.exports.EventEmitterMixin = EventEmitterMixin;
17 changes: 17 additions & 0 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
ObjectDefineProperties,
ObjectDefineProperty,
ObjectGetOwnPropertyDescriptor,
ObjectGetOwnPropertyDescriptors,
ReflectApply,
SafeMap,
String,
Expand All @@ -30,6 +31,7 @@ const { validateObject } = require('internal/validators');

const { customInspectSymbol } = require('internal/util');
const { inspect } = require('util');
const EventEmitter = require('events');

const kIsEventTarget = SymbolFor('nodejs.event_target');

Expand Down Expand Up @@ -646,8 +648,23 @@ function defineEventHandler(emitter, name) {
enumerable: true
});
}

const EventEmitterMixin = (Superclass) => {
class MixedEventEmitter extends Superclass {
constructor(...args) {
super(...args);
EventEmitter.call(this);
mmomtchev marked this conversation as resolved.
Show resolved Hide resolved
}
}
const protoProps = ObjectGetOwnPropertyDescriptors(EventEmitter.prototype);
delete protoProps.constructor;
ObjectDefineProperties(MixedEventEmitter.prototype, protoProps);
return MixedEventEmitter;
};

module.exports = {
Event,
EventEmitterMixin,
EventTarget,
NodeEventTarget,
defineEventHandler,
Expand Down
9 changes: 4 additions & 5 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const {
PromisePrototypeThen,
PromiseResolve,
Symbol,
Uint8Array
Uint8Array,
} = primordials;

const {
Expand Down Expand Up @@ -71,7 +71,7 @@ const {
} = require('internal/validators');
const pathModule = require('path');
const { promisify } = require('internal/util');
const EventEmitter = require('events');
const { EventEmitterMixin } = require('internal/event_target');

const kHandle = Symbol('kHandle');
const kFd = Symbol('kFd');
Expand All @@ -97,7 +97,7 @@ const lazyDOMException = hideStackFrames((message, name) => {
return new DOMException(message, name);
});

class FileHandle extends EventEmitter.EventEmitterMixin(JSTransferable) {
class FileHandle extends EventEmitterMixin(JSTransferable) {
constructor(filehandle) {
super();
this[kHandle] = filehandle;
Expand Down Expand Up @@ -229,7 +229,6 @@ class FileHandle extends EventEmitter.EventEmitterMixin(JSTransferable) {
[kDeserialize]({ handle }) {
this[kHandle] = handle;
this[kFd] = handle.fd;
EventEmitter.call(this);
}

[kRef]() {
Expand Down Expand Up @@ -727,5 +726,5 @@ module.exports = {

FileHandle,
kRef,
kUnref
kUnref,
};
2 changes: 1 addition & 1 deletion lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {
const {
ERR_INVALID_ARG_TYPE,
ERR_OUT_OF_RANGE,
ERR_METHOD_NOT_IMPLEMENTED
ERR_METHOD_NOT_IMPLEMENTED,
} = require('internal/errors').codes;
const { deprecate } = require('internal/util');
const { validateInteger } = require('internal/validators');
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-fs-read-stream-file-handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ fs.promises.open(file, 'r').then((handle) => {
name: 'Error',
message: 'The FileHandle with fs method is not implemented'
});
handle.close();
});