Skip to content

Commit ecf1053

Browse files
committed
stream: cleanup a few minor details in stream/iter
1 parent 84df861 commit ecf1053

6 files changed

Lines changed: 24 additions & 46 deletions

File tree

lib/internal/streams/iter/broadcast.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ function isBroadcastable(value) {
727727
}
728728

729729
const Broadcast = {
730+
__proto__: null,
730731
from(input, options) {
731732
if (isBroadcastable(input)) {
732733
const bc = input[broadcastProtocol](options);

lib/internal/streams/iter/from.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ const FROM_BATCH_SIZE = 128;
5757
* @returns {boolean}
5858
*/
5959
function isPrimitiveChunk(value) {
60-
if (typeof value === 'string') return true;
61-
if (isArrayBuffer(value)) return true;
62-
if (ArrayBufferIsView(value)) return true;
63-
return false;
60+
return typeof value === 'string' || isArrayBuffer(value) || ArrayBufferIsView(value);
6461
}
6562

6663
/**

lib/internal/streams/iter/share.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ function isSyncShareable(value) {
647647
}
648648

649649
const Share = {
650+
__proto__: null,
650651
from(input, options) {
651652
if (isShareable(input)) {
652653
const result = input[shareProtocol](options);
@@ -665,6 +666,7 @@ const Share = {
665666
};
666667

667668
const SyncShare = {
669+
__proto__: null,
668670
fromSync(input, options) {
669671
if (isSyncShareable(input)) {
670672
const result = input[shareSyncProtocol](options);

lib/internal/streams/iter/transform.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
// New Streams API - Compression / Decompression Transforms
3+
// Compression / Decompression Transforms
44
//
55
// Creates bare native zlib handles via internalBinding('zlib'), bypassing
66
// the stream.Transform / ZlibBase / EventEmitter machinery entirely.

lib/internal/streams/iter/types.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
'use strict';
22

3-
// New Streams API - Protocol Symbols
4-
//
5-
// These symbols allow objects to participate in streaming.
6-
// Using Symbol.for() allows third-party code to implement protocols
7-
// without importing these symbols directly.
8-
93
const {
104
SymbolFor,
115
} = primordials;

lib/internal/streams/iter/utils.js

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
'use strict';
22

3-
// New Streams API - Utility Functions
4-
53
const {
6-
Array,
74
ArrayPrototypeSlice,
5+
TypedArrayPrototypeGetBuffer,
86
TypedArrayPrototypeGetByteLength,
9-
TypedArrayPrototypeSet,
7+
TypedArrayPrototypeGetByteOffset,
108
Uint8Array,
119
} = primordials;
1210

1311
const { TextEncoder } = require('internal/encoding');
1412
const {
1513
codes: {
1614
ERR_INVALID_ARG_TYPE,
17-
ERR_INVALID_ARG_VALUE,
1815
},
1916
} = require('internal/errors');
17+
18+
const { Buffer } = require('buffer');
19+
2020
const { isUint8Array } = require('internal/util/types');
2121

22+
const { validateOneOf } = require('internal/validators');
23+
2224
// Shared TextEncoder instance for string conversion.
2325
const encoder = new TextEncoder();
2426

@@ -55,6 +57,7 @@ function toUint8Array(chunk) {
5557
* @returns {boolean}
5658
*/
5759
function allUint8Array(chunks) {
60+
// Ok, well, kind of. This is more a check for "no strings"...
5861
for (let i = 0; i < chunks.length; i++) {
5962
if (typeof chunks[i] === 'string') return false;
6063
}
@@ -67,28 +70,11 @@ function allUint8Array(chunks) {
6770
* @returns {Uint8Array}
6871
*/
6972
function concatBytes(chunks) {
70-
if (chunks.length === 0) {
71-
return new Uint8Array(0);
72-
}
73-
if (chunks.length === 1) {
74-
return chunks[0];
75-
}
76-
77-
const len = chunks.length;
78-
const lengths = new Array(len);
79-
let total = 0;
80-
for (let i = 0; i < len; i++) {
81-
const l = TypedArrayPrototypeGetByteLength(chunks[i]);
82-
lengths[i] = l;
83-
total += l;
84-
}
85-
const result = new Uint8Array(total);
86-
let offset = 0;
87-
for (let i = 0; i < len; i++) {
88-
TypedArrayPrototypeSet(result, chunks[i], offset);
89-
offset += lengths[i];
90-
}
91-
return result;
73+
const buf = Buffer.concat(chunks);
74+
return new Uint8Array(
75+
TypedArrayPrototypeGetBuffer(buf),
76+
TypedArrayPrototypeGetByteOffset(buf),
77+
TypedArrayPrototypeGetByteLength(buf));
9278
}
9379

9480
/**
@@ -154,14 +140,12 @@ function parsePullArgs(args) {
154140
* @param {string} value
155141
*/
156142
function validateBackpressure(value) {
157-
if (value !== 'strict' &&
158-
value !== 'block' &&
159-
value !== 'drop-oldest' &&
160-
value !== 'drop-newest') {
161-
throw new ERR_INVALID_ARG_VALUE(
162-
'options.backpressure', value,
163-
'must be "strict", "block", "drop-oldest", or "drop-newest"');
164-
}
143+
validateOneOf(value, 'options.backpressure', [
144+
'strict',
145+
'block',
146+
'drop-oldest',
147+
'drop-newest',
148+
]);
165149
}
166150

167151
module.exports = {

0 commit comments

Comments
 (0)