| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d62ab3a commit 7d258db
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,36 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common.js'); | ||
| 3 | + const { Readable } = require('stream'); | ||
| 4 | + | ||
| 5 | + const bench = common.createBenchmark(main, { | ||
| 6 | + n: [1e6], | ||
| 7 | + kind: ['read', 'encoding'], | ||
| 8 | + }); | ||
| 9 | + const ABC = new Uint8Array([0x41, 0x42, 0x43]); | ||
| 10 | + | ||
| 11 | + function main({ n, kind }) { | ||
| 12 | + switch (kind) { | ||
| 13 | + case 'read': { | ||
| 14 | + bench.start(); | ||
| 15 | + const readable = new Readable({ | ||
| 16 | + read() {}, | ||
| 17 | + }); | ||
| 18 | + for (let i = 0; i < n; ++i) readable.push(ABC); | ||
| 19 | + bench.end(n); | ||
| 20 | + break; | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + case 'encoding': { | ||
| 24 | + bench.start(); | ||
| 25 | + const readable = new Readable({ | ||
| 26 | + read() {}, | ||
| 27 | + }); | ||
| 28 | + readable.setEncoding('utf8'); | ||
| 29 | + for (let i = 0; i < n; ++i) readable.push(ABC); | ||
| 30 | + bench.end(n); | ||
| 31 | + break; | ||
| 32 | + } | ||
| 33 | + default: | ||
| 34 | + throw new Error('Invalid kind'); | ||
| 35 | + } | ||
| 36 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,51 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common.js'); | ||
| 3 | + const { Writable } = require('stream'); | ||
| 4 | + | ||
| 5 | + const bench = common.createBenchmark(main, { | ||
| 6 | + n: [50e6], | ||
| 7 | + kind: ['write', 'object-mode', 'writev'], | ||
| 8 | + }); | ||
| 9 | + const ABC = new Uint8Array([0x41, 0x42, 0x43]); | ||
| 10 | + | ||
| 11 | + function main({ n, kind }) { | ||
| 12 | + switch (kind) { | ||
| 13 | + case 'write': { | ||
| 14 | + bench.start(); | ||
| 15 | + const wr = new Writable({ | ||
| 16 | + write(chunk, encoding, cb) { | ||
| 17 | + cb(); | ||
| 18 | + }, | ||
| 19 | + }); | ||
| 20 | + for (let i = 0; i < n; ++i) wr.write(ABC); | ||
| 21 | + bench.end(n); | ||
| 22 | + break; | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + case 'object-mode': { | ||
| 26 | + bench.start(); | ||
| 27 | + const wr = new Writable({ | ||
| 28 | + objectMode: true, | ||
| 29 | + write(chunk, encoding, cb) { | ||
| 30 | + cb(); | ||
| 31 | + }, | ||
| 32 | + }); | ||
| 33 | + for (let i = 0; i < n; ++i) wr.write(ABC); | ||
| 34 | + bench.end(n); | ||
| 35 | + break; | ||
| 36 | + } | ||
| 37 | + case 'writev': { | ||
| 38 | + bench.start(); | ||
| 39 | + const wr = new Writable({ | ||
| 40 | + writev(chunks, cb) { | ||
| 41 | + cb(); | ||
| 42 | + }, | ||
| 43 | + }); | ||
| 44 | + for (let i = 0; i < n; ++i) wr.write(ABC); | ||
| 45 | + bench.end(n); | ||
| 46 | + break; | ||
| 47 | + } | ||
| 48 | + default: | ||
| 49 | + throw new Error('Invalid kind'); | ||
| 50 | + } | ||
| 51 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -282,11 +282,19 @@ The `finished` API also provides a [callback version][stream-finished]. | |||
| 282 | 282 | ||
| 283 | 283 | ### Object mode | |
| 284 | 284 | ||
| 285 | - All streams created by Node.js APIs operate exclusively on strings and `Buffer` | ||
| 286 | - (or `Uint8Array`) objects. It is possible, however, for stream implementations | ||
| 287 | - to work with other types of JavaScript values (with the exception of `null`, | ||
| 288 | - which serves a special purpose within streams). Such streams are considered to | ||
| 289 | - operate in "object mode". | ||
| 285 | + All streams created by Node.js APIs operate exclusively on strings, {Buffer}, | ||
| 286 | + {TypedArray} and {DataView} objects: | ||
| 287 | + | ||
| 288 | + * `Strings` and `Buffers` are the most common types used with streams. | ||
| 289 | + * `TypedArray` and `DataView` lets you handle binary data with types like | ||
| 290 | + `Int32Array` or `Uint8Array`. When you write a TypedArray or DataView to a | ||
| 291 | + stream, Node.js processes | ||
| 292 | + the raw bytes. | ||
| 293 | + | ||
| 294 | + It is possible, however, for stream | ||
| 295 | + implementations to work with other types of JavaScript values (with the | ||
| 296 | + exception of `null`, which serves a special purpose within streams). | ||
| 297 | + Such streams are considered to operate in "object mode". | ||
| 290 | 298 | ||
| 291 | 299 | Stream instances are switched into object mode using the `objectMode` option | |
| 292 | 300 | when the stream is created. Attempting to switch an existing stream into | |
@@ -712,6 +720,9 @@ console.log(myStream.destroyed); // true | |||
| 712 | 720 | <!-- YAML | |
| 713 | 721 | added: v0.9.4 | |
| 714 | 722 | changes: | |
| 723 | + - version: REPLACEME | ||
| 724 | + pr-url: https://github.com/nodejs/node/pull/51866 | ||
| 725 | + description: The `chunk` argument can now be a `TypedArray` or `DataView` instance. | ||
| 715 | 726 | - version: v15.0.0 | |
| 716 | 727 | pr-url: https://github.com/nodejs/node/pull/34101 | |
| 717 | 728 | description: The `callback` is invoked before 'finish' or on error. | |
@@ -726,10 +737,10 @@ changes: | |||
| 726 | 737 | description: The `chunk` argument can now be a `Uint8Array` instance. | |
| 727 | 738 | --> | |
| 728 | 739 | ||
| 729 | - * `chunk` {string|Buffer|Uint8Array|any} Optional data to write. For streams | ||
| 730 | - not operating in object mode, `chunk` must be a string, `Buffer` or | ||
| 731 | - `Uint8Array`. For object mode streams, `chunk` may be any JavaScript value | ||
| 732 | - other than `null`. | ||
| 740 | + * `chunk` {string|Buffer|TypedArray|DataView|any} Optional data to write. For | ||
| 741 | + streams not operating in object mode, `chunk` must be a {string}, {Buffer}, | ||
| 742 | + {TypedArray} or {DataView}. For object mode streams, `chunk` may be any | ||
| 743 | + JavaScript value other than `null`. | ||
| 733 | 744 | * `encoding` {string} The encoding if `chunk` is a string | |
| 734 | 745 | * `callback` {Function} Callback for when the stream is finished. | |
| 735 | 746 | * Returns: {this} | |
@@ -926,6 +937,9 @@ Getter for the property `objectMode` of a given `Writable` stream. | |||
| 926 | 937 | <!-- YAML | |
| 927 | 938 | added: v0.9.4 | |
| 928 | 939 | changes: | |
| 940 | + - version: REPLACEME | ||
| 941 | + pr-url: https://github.com/nodejs/node/pull/51866 | ||
| 942 | + description: The `chunk` argument can now be a `TypedArray` or `DataView` instance. | ||
| 929 | 943 | - version: v8.0.0 | |
| 930 | 944 | pr-url: https://github.com/nodejs/node/pull/11608 | |
| 931 | 945 | description: The `chunk` argument can now be a `Uint8Array` instance. | |
@@ -935,10 +949,10 @@ changes: | |||
| 935 | 949 | considered invalid now, even in object mode. | |
| 936 | 950 | --> | |
| 937 | 951 | ||
| 938 | - * `chunk` {string|Buffer|Uint8Array|any} Optional data to write. For streams | ||
| 939 | - not operating in object mode, `chunk` must be a string, `Buffer` or | ||
| 940 | - `Uint8Array`. For object mode streams, `chunk` may be any JavaScript value | ||
| 941 | - other than `null`. | ||
| 952 | + * `chunk` {string|Buffer|TypedArray|DataView|any} Optional data to write. For | ||
| 953 | + streams not operating in object mode, `chunk` must be a {string}, {Buffer}, | ||
| 954 | + {TypedArray} or {DataView}. For object mode streams, `chunk` may be any | ||
| 955 | + JavaScript value other than `null`. | ||
| 942 | 956 | * `encoding` {string|null} The encoding, if `chunk` is a string. **Default:** `'utf8'` | |
| 943 | 957 | * `callback` {Function} Callback for when this chunk of data is flushed. | |
| 944 | 958 | * Returns: {boolean} `false` if the stream wishes for the calling code to | |
@@ -1763,15 +1777,18 @@ setTimeout(() => { | |||
| 1763 | 1777 | <!-- YAML | |
| 1764 | 1778 | added: v0.9.11 | |
| 1765 | 1779 | changes: | |
| 1780 | + - version: REPLACEME | ||
| 1781 | + pr-url: https://github.com/nodejs/node/pull/51866 | ||
| 1782 | + description: The `chunk` argument can now be a `TypedArray` or `DataView` instance. | ||
| 1766 | 1783 | - version: v8.0.0 | |
| 1767 | 1784 | pr-url: https://github.com/nodejs/node/pull/11608 | |
| 1768 | 1785 | description: The `chunk` argument can now be a `Uint8Array` instance. | |
| 1769 | 1786 | --> | |
| 1770 | 1787 | ||
| 1771 | - * `chunk` {Buffer|Uint8Array|string|null|any} Chunk of data to unshift onto the | ||
| 1772 | - read queue. For streams not operating in object mode, `chunk` must be a | ||
| 1773 | - string, `Buffer`, `Uint8Array`, or `null`. For object mode streams, `chunk` | ||
| 1774 | - may be any JavaScript value. | ||
| 1788 | + * `chunk` {Buffer|TypedArray|DataView|string|null|any} Chunk of data to unshift | ||
| 1789 | + onto the read queue. For streams not operating in object mode, `chunk` must | ||
| 1790 | + be a {string}, {Buffer}, {TypedArray}, {DataView} or `null`. | ||
| 1791 | + For object mode streams, `chunk` may be any JavaScript value. | ||
| 1775 | 1792 | * `encoding` {string} Encoding of string chunks. Must be a valid | |
| 1776 | 1793 | `Buffer` encoding, such as `'utf8'` or `'ascii'`. | |
| 1777 | 1794 | ||
@@ -3515,8 +3532,8 @@ changes: | |||
| 3515 | 3532 | **Default:** `'utf8'`. | |
| 3516 | 3533 | * `objectMode` {boolean} Whether or not the | |
| 3517 | 3534 | [`stream.write(anyObj)`][stream-write] is a valid operation. When set, | |
| 3518 | - it becomes possible to write JavaScript values other than string, | ||
| 3519 | - `Buffer` or `Uint8Array` if supported by the stream implementation. | ||
| 3535 | + it becomes possible to write JavaScript values other than string, {Buffer}, | ||
| 3536 | + {TypedArray} or {DataView} if supported by the stream implementation. | ||
| 3520 | 3537 | **Default:** `false`. | |
| 3521 | 3538 | * `emitClose` {boolean} Whether or not the stream should emit `'close'` | |
| 3522 | 3539 | after it has been destroyed. **Default:** `true`. | |
@@ -4068,22 +4085,25 @@ It can be overridden by child classes but it **must not** be called directly. | |||
| 4068 | 4085 | ||
| 4069 | 4086 | <!-- YAML | |
| 4070 | 4087 | changes: | |
| 4088 | + - version: REPLACEME | ||
| 4089 | + pr-url: https://github.com/nodejs/node/pull/51866 | ||
| 4090 | + description: The `chunk` argument can now be a `TypedArray` or `DataView` instance. | ||
| 4071 | 4091 | - version: v8.0.0 | |
| 4072 | 4092 | pr-url: https://github.com/nodejs/node/pull/11608 | |
| 4073 | 4093 | description: The `chunk` argument can now be a `Uint8Array` instance. | |
| 4074 | 4094 | --> | |
| 4075 | 4095 | ||
| 4076 | - * `chunk` {Buffer|Uint8Array|string|null|any} Chunk of data to push into the | ||
| 4077 | - read queue. For streams not operating in object mode, `chunk` must be a | ||
| 4078 | - string, `Buffer` or `Uint8Array`. For object mode streams, `chunk` may be | ||
| 4079 | - any JavaScript value. | ||
| 4096 | + * `chunk` {Buffer|TypedArray|DataView|string|null|any} Chunk of data to push | ||
| 4097 | + into the read queue. For streams not operating in object mode, `chunk` must | ||
| 4098 | + be a {string}, {Buffer}, {TypedArray} or {DataView}. For object mode streams, | ||
| 4099 | + `chunk` may be any JavaScript value. | ||
| 4080 | 4100 | * `encoding` {string} Encoding of string chunks. Must be a valid | |
| 4081 | 4101 | `Buffer` encoding, such as `'utf8'` or `'ascii'`. | |
| 4082 | 4102 | * Returns: {boolean} `true` if additional chunks of data may continue to be | |
| 4083 | 4103 | pushed; `false` otherwise. | |
| 4084 | 4104 | ||
| 4085 | - When `chunk` is a `Buffer`, `Uint8Array`, or `string`, the `chunk` of data will | ||
| 4086 | - be added to the internal queue for users of the stream to consume. | ||
| 4105 | + When `chunk` is a {Buffer}, {TypedArray}, {DataView} or {string}, the `chunk` | ||
| 4106 | + of data will be added to the internal queue for users of the stream to consume. | ||
| 4087 | 4107 | Passing `chunk` as `null` signals the end of the stream (EOF), after which no | |
| 4088 | 4108 | more data can be written. | |
| 4089 | 4109 | ||
@@ -4758,8 +4778,9 @@ situations within Node.js where this is done, particularly in the | |||
| 4758 | 4778 | ||
| 4759 | 4779 | Use of `readable.push('')` is not recommended. | |
| 4760 | 4780 | ||
| 4761 | - Pushing a zero-byte string, `Buffer`, or `Uint8Array` to a stream that is not in | ||
| 4762 | - object mode has an interesting side effect. Because it _is_ a call to | ||
| 4781 | + Pushing a zero-byte {string}, {Buffer}, {TypedArray} or {DataView} to a stream | ||
| 4782 | + that is not in object mode has an interesting side effect. | ||
| 4783 | + Because it _is_ a call to | ||
| 4763 | 4784 | [`readable.push()`][stream-push], the call will end the reading process. | |
| 4764 | 4785 | However, because the argument is an empty string, no data is added to the | |
| 4765 | 4786 | readable buffer so there is nothing for a user to consume. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -420,11 +420,11 @@ function readableAddChunkUnshiftByteMode(stream, state, chunk, encoding) { | |||
| 420 | 420 | chunk = Buffer.from(chunk, encoding); | |
| 421 | 421 | } | |
| 422 | 422 | } | |
| 423 | - } else if (Stream._isUint8Array(chunk)) { | ||
| 423 | + } else if (Stream._isArrayBufferView(chunk)) { | ||
| 424 | 424 | chunk = Stream._uint8ArrayToBuffer(chunk); | |
| 425 | 425 | } else if (chunk !== undefined && !(chunk instanceof Buffer)) { | |
| 426 | 426 | errorOrDestroy(stream, new ERR_INVALID_ARG_TYPE( | |
| 427 | - 'chunk', ['string', 'Buffer', 'Uint8Array'], chunk)); | ||
| 427 | + 'chunk', ['string', 'Buffer', 'TypedArray', 'DataView'], chunk)); | ||
| 428 | 428 | return false; | |
| 429 | 429 | } | |
| 430 | 430 | ||
@@ -473,12 +473,12 @@ function readableAddChunkPushByteMode(stream, state, chunk, encoding) { | |||
| 473 | 473 | } | |
| 474 | 474 | } else if (chunk instanceof Buffer) { | |
| 475 | 475 | encoding = ''; | |
| 476 | - } else if (Stream._isUint8Array(chunk)) { | ||
| 476 | + } else if (Stream._isArrayBufferView(chunk)) { | ||
| 477 | 477 | chunk = Stream._uint8ArrayToBuffer(chunk); | |
| 478 | 478 | encoding = ''; | |
| 479 | 479 | } else if (chunk !== undefined) { | |
| 480 | 480 | errorOrDestroy(stream, new ERR_INVALID_ARG_TYPE( | |
| 481 | - 'chunk', ['string', 'Buffer', 'Uint8Array'], chunk)); | ||
| 481 | + 'chunk', ['string', 'Buffer', 'TypedArray', 'DataView'], chunk)); | ||
| 482 | 482 | return false; | |
| 483 | 483 | } | |
| 484 | 484 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -467,12 +467,12 @@ function _write(stream, chunk, encoding, cb) { | |||
| 467 | 467 | } | |
| 468 | 468 | } else if (chunk instanceof Buffer) { | |
| 469 | 469 | encoding = 'buffer'; | |
| 470 | - } else if (Stream._isUint8Array(chunk)) { | ||
| 470 | + } else if (Stream._isArrayBufferView(chunk)) { | ||
| 471 | 471 | chunk = Stream._uint8ArrayToBuffer(chunk); | |
| 472 | 472 | encoding = 'buffer'; | |
| 473 | 473 | } else { | |
| 474 | 474 | throw new ERR_INVALID_ARG_TYPE( | |
| 475 | - 'chunk', ['string', 'Buffer', 'Uint8Array'], chunk); | ||
| 475 | + 'chunk', ['string', 'Buffer', 'TypedArray', 'DataView'], chunk); | ||
| 476 | 476 | } | |
| 477 | 477 | } | |
| 478 | 478 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -50,6 +50,7 @@ const internalBuffer = require('internal/buffer'); | |||
| 50 | 50 | ||
| 51 | 51 | const promises = require('stream/promises'); | |
| 52 | 52 | const utils = require('internal/streams/utils'); | |
| 53 | + const { isArrayBufferView, isUint8Array } = require('internal/util/types'); | ||
| 53 | 54 | ||
| 54 | 55 | const Stream = module.exports = require('internal/streams/legacy').Stream; | |
| 55 | 56 | ||
@@ -137,7 +138,8 @@ ObjectDefineProperty(eos, customPromisify, { | |||
| 137 | 138 | // Backwards-compat with node 0.4.x | |
| 138 | 139 | Stream.Stream = Stream; | |
| 139 | 140 | ||
| 140 | - Stream._isUint8Array = require('internal/util/types').isUint8Array; | ||
| 141 | + Stream._isArrayBufferView = isArrayBufferView; | ||
| 142 | + Stream._isUint8Array = isUint8Array; | ||
| 141 | 143 | Stream._uint8ArrayToBuffer = function _uint8ArrayToBuffer(chunk) { | |
| 142 | 144 | return new internalBuffer.FastBuffer(chunk.buffer, | |
| 143 | 145 | chunk.byteOffset, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,6 +34,6 @@ assert.throws(() => { | |||
| 34 | 34 | code: 'ERR_INVALID_ARG_TYPE', | |
| 35 | 35 | name: 'TypeError', | |
| 36 | 36 | message: 'The "chunk" argument must be of type string or an instance of ' + | |
| 37 | - `Buffer or Uint8Array.${common.invalidArgTypeHelper(value)}` | ||
| 37 | + `Buffer, TypedArray, or DataView.${common.invalidArgTypeHelper(value)}` | ||
| 38 | 38 | }); | |
| 39 | 39 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments