Skip to content

Commit 8015a72

Browse files
committed
fixup! stream: refactor to use more primordials
1 parent 7c4befc commit 8015a72

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
maybeReadMore(this, this._readableState);
@@ -874,7 +874,7 @@ Readable.prototype.unpipe = function(dest) {
874874
// Set up data events if they are asked for
875875
// Ensure readable listeners eventually get something.
876876
Readable.prototype.on = function(ev, fn) {
877-
const res = ReflectApply(Stream.prototype.on, this, [ev, fn]);
877+
const res = FunctionPrototypeCall(Stream.prototype.on, this, ev, fn);
878878
const state = this._readableState;
879879

880880
if (ev === 'data') {
@@ -904,7 +904,8 @@ Readable.prototype.on = function(ev, fn) {
904904
Readable.prototype.addListener = Readable.prototype.on;
905905

906906
Readable.prototype.removeListener = function(ev, fn) {
907-
const res = ReflectApply(Stream.prototype.removeListener, this, [ev, fn]);
907+
const res = FunctionPrototypeCall(Stream.prototype.removeListener, this,
908+
ev, fn);
908909

909910
if (ev === 'readable') {
910911
// 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,14 +29,11 @@ const {
2929
ArrayPrototypePush,
3030
ArrayPrototypeSlice,
3131
ArrayPrototypeSplice,
32-
FunctionPrototype,
33-
FunctionPrototypeBind,
3432
FunctionPrototypeCall,
3533
FunctionPrototypeSymbolHasInstance,
3634
ObjectDefineProperty,
3735
ObjectDefineProperties,
3836
ObjectSetPrototypeOf,
39-
ReflectApply,
4037
StringPrototypeToLowerCase,
4138
Symbol,
4239
SymbolHasInstance,
@@ -76,7 +73,7 @@ const { errorOrDestroy } = destroyImpl;
7673
ObjectSetPrototypeOf(Writable.prototype, Stream.prototype);
7774
ObjectSetPrototypeOf(Writable, Stream);
7875

79-
const nop = FunctionPrototype;
76+
function nop() {}
8077

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

@@ -153,7 +150,7 @@ function WritableState(options, stream, isDuplex) {
153150
this.bufferProcessing = false;
154151

155152
// The callback that's passed to _write(chunk, cb).
156-
this.onwrite = FunctionPrototypeBind(onwrite, undefined, stream);
153+
this.onwrite = onwrite.bind(undefined, stream);
157154

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

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

369376
if (state.writing || state.corked || state.errored || !state.constructed) {
370-
ArrayPrototypePush(state.buffered, { chunk, encoding, callback });
377+
state.buffered.push({ chunk, encoding, callback });
371378
if (state.allBuffers && encoding !== 'buffer') {
372379
state.allBuffers = false;
373380
}
@@ -841,7 +848,7 @@ Writable.prototype.destroy = function(err, cb) {
841848
process.nextTick(errorBuffer, state);
842849
}
843850

844-
ReflectApply(destroy, this, [err, cb]);
851+
FunctionPrototypeCall(destroy, this, err, cb);
845852
return this;
846853
};
847854

0 commit comments

Comments
 (0)