Skip to content

Commit b93fda8

Browse files
committed
fixup! stream: refactor to use more primordials
1 parent 4381dd3 commit b93fda8

File tree

6 files changed

+28
-23
lines changed

6 files changed

+28
-23
lines changed

lib/internal/streams/buffer_list.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const {
55
SymbolIterator,
66
Uint8Array,
77
TypedArrayPrototypeSet,
8-
TypedArrayPrototypeSlice,
98
} = primordials;
109

1110
const { Buffer } = require('buffer');
@@ -81,11 +80,10 @@ module.exports = class BufferList {
8180
consume(n, hasStrings) {
8281
const data = this.head.data;
8382
if (n < data.length) {
84-
const slice = typeof data === 'string' ?
85-
StringPrototypeSlice :
86-
TypedArrayPrototypeSlice;
87-
this.head.data = slice(data, n);
88-
return slice(data, 0, n);
83+
// `slice` is the same for buffers and strings.
84+
const slice = data.slice(0, n);
85+
this.head.data = data.slice(n);
86+
return slice;
8987
}
9088
if (n === data.length) {
9189
// First chunk is a perfect match.
@@ -160,7 +158,7 @@ module.exports = class BufferList {
160158
new Uint8Array(buf.buffer, buf.byteOffset, n),
161159
retLen - n);
162160
this.head = p;
163-
p.data = TypedArrayPrototypeSlice(buf, n);
161+
p.data = buf.slice(n);
164162
}
165163
break;
166164
}

lib/internal/streams/duplex.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
'use strict';
2828

2929
const {
30-
FunctionPrototypeCall,
3130
ObjectDefineProperties,
3231
ObjectGetOwnPropertyDescriptor,
3332
ObjectKeys,
@@ -54,8 +53,8 @@ function Duplex(options) {
5453
if (!(this instanceof Duplex))
5554
return new Duplex(options);
5655

57-
FunctionPrototypeCall(Readable, this, options);
58-
FunctionPrototypeCall(Writable, this, options);
56+
Readable.call(this, options);
57+
Writable.call(this, options);
5958
this.allowHalfOpen = true;
6059

6160
if (options) {

lib/internal/streams/lazy_transform.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
'use strict';
55

66
const {
7+
FunctionPrototypeCall,
78
ObjectDefineProperties,
89
ObjectDefineProperty,
910
ObjectSetPrototypeOf,
10-
ReflectApply,
1111
} = primordials;
1212

1313
const stream = require('stream');
@@ -26,7 +26,7 @@ ObjectSetPrototypeOf(LazyTransform, stream.Transform);
2626

2727
function makeGetter(name) {
2828
return function() {
29-
ReflectApply(stream.Transform, this, [this._options]);
29+
FunctionPrototypeCall(stream.Transform, this, this._options);
3030
this._writableState.decodeStrings = false;
3131

3232
if (!this._options || !this._options.defaultEncoding) {

lib/internal/streams/readable.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ function Readable(options) {
207207
addAbortSignalNoValidate(options.signal, this);
208208
}
209209

210-
ReflectApply(Stream, this, [options]);
210+
FunctionPrototypeCall(Stream, this, options);
211211

212212
destroyImpl.construct(this, () => {
213213
if (this._readableState.needReadable) {
@@ -876,7 +876,7 @@ Readable.prototype.unpipe = function(dest) {
876876
// Set up data events if they are asked for
877877
// Ensure readable listeners eventually get something.
878878
Readable.prototype.on = function(ev, fn) {
879-
const res = ReflectApply(Stream.prototype.on, this, [ev, fn]);
879+
const res = FunctionPrototypeCall(Stream.prototype.on, this, ev, fn);
880880
const state = this._readableState;
881881

882882
if (ev === 'data') {
@@ -906,7 +906,8 @@ Readable.prototype.on = function(ev, fn) {
906906
Readable.prototype.addListener = Readable.prototype.on;
907907

908908
Readable.prototype.removeListener = function(ev, fn) {
909-
const res = ReflectApply(Stream.prototype.removeListener, this, [ev, fn]);
909+
const res = FunctionPrototypeCall(Stream.prototype.removeListener, this,
910+
ev, fn);
910911

911912
if (ev === 'readable') {
912913
// We need to check if there is someone still listening to

lib/internal/streams/transform.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function Transform(options) {
8383
if (!(this instanceof Transform))
8484
return new Transform(options);
8585

86-
FunctionPrototypeCall(Duplex, this, options);
86+
Duplex.call(this, options);
8787

8888
// We have implemented the _read method, and done the other things
8989
// that Readable wants before the first _read call, so unset the

lib/internal/streams/writable.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,12 @@ const {
2929
ArrayPrototypePush,
3030
ArrayPrototypeSlice,
3131
ArrayPrototypeSplice,
32-
FunctionPrototype,
3332
Error,
34-
FunctionPrototypeBind,
3533
FunctionPrototypeCall,
3634
FunctionPrototypeSymbolHasInstance,
3735
ObjectDefineProperty,
3836
ObjectDefineProperties,
3937
ObjectSetPrototypeOf,
40-
ReflectApply,
4138
StringPrototypeToLowerCase,
4239
Symbol,
4340
SymbolHasInstance,
@@ -77,7 +74,7 @@ const { errorOrDestroy } = destroyImpl;
7774
ObjectSetPrototypeOf(Writable.prototype, Stream.prototype);
7875
ObjectSetPrototypeOf(Writable, Stream);
7976

80-
const nop = FunctionPrototype;
77+
function nop() {}
8178

8279
const kOnFinished = Symbol('kOnFinished');
8380

@@ -154,7 +151,7 @@ function WritableState(options, stream, isDuplex) {
154151
this.bufferProcessing = false;
155152

156153
// The callback that's passed to _write(chunk, cb).
157-
this.onwrite = FunctionPrototypeBind(onwrite, undefined, stream);
154+
this.onwrite = onwrite.bind(undefined, stream);
158155

159156
// The callback that the user supplies to write(chunk, encoding, cb).
160157
this.writecb = null;
@@ -273,6 +270,16 @@ function Writable(options) {
273270
});
274271
}
275272

273+
ObjectDefineProperty(Writable, SymbolHasInstance, {
274+
value: function(object) {
275+
if (FunctionPrototypeSymbolHasInstance(this, object)) return true;
276+
if (this !== Writable) return false;
277+
278+
return object && object._writableState instanceof WritableState;
279+
},
280+
});
281+
282+
276283
// Otherwise people can pipe Writable streams, which is just wrong.
277284
Writable.prototype.pipe = function() {
278285
errorOrDestroy(this, new ERR_STREAM_CANNOT_PIPE());
@@ -372,7 +379,7 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) {
372379
state.needDrain = true;
373380

374381
if (state.writing || state.corked || state.errored || !state.constructed) {
375-
ArrayPrototypePush(state.buffered, { chunk, encoding, callback });
382+
state.buffered.push({ chunk, encoding, callback });
376383
if (state.allBuffers && encoding !== 'buffer') {
377384
state.allBuffers = false;
378385
}
@@ -855,7 +862,7 @@ Writable.prototype.destroy = function(err, cb) {
855862
process.nextTick(errorBuffer, state);
856863
}
857864

858-
ReflectApply(destroy, this, [err, cb]);
865+
FunctionPrototypeCall(destroy, this, err, cb);
859866
return this;
860867
};
861868

0 commit comments

Comments
 (0)