| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 837ca76 commit 6778261
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -641,15 +641,16 @@ then copying out the relevant bits. | |||
| 641 | 641 | const store = []; | |
| 642 | 642 | ||
| 643 | 643 | socket.on('readable', () => { | |
| 644 | - const data = socket.read(); | ||
| 644 | + let data; | ||
| 645 | + while (null !== (data = readable.read())) { | ||
| 646 | + // Allocate for retained data | ||
| 647 | + const sb = Buffer.allocUnsafeSlow(10); | ||
| 645 | 648 | ||
| 646 | - // Allocate for retained data | ||
| 647 | - const sb = Buffer.allocUnsafeSlow(10); | ||
| 649 | + // Copy the data into the new allocation | ||
| 650 | + data.copy(sb, 0, 0, 10); | ||
| 648 | 651 | ||
| 649 | - // Copy the data into the new allocation | ||
| 650 | - data.copy(sb, 0, 0, 10); | ||
| 651 | - | ||
| 652 | - store.push(sb); | ||
| 652 | + store.push(sb); | ||
| 653 | + } | ||
| 653 | 654 | }); | |
| 654 | 655 | ``` | |
| 655 | 656 | ||
@@ -2561,15 +2562,16 @@ un-pooled `Buffer` instance using `SlowBuffer` then copy out the relevant bits. | |||
| 2561 | 2562 | const store = []; | |
| 2562 | 2563 | ||
| 2563 | 2564 | socket.on('readable', () => { | |
| 2564 | - const data = socket.read(); | ||
| 2565 | + let data; | ||
| 2566 | + while (null !== (data = readable.read())) { | ||
| 2567 | + // Allocate for retained data | ||
| 2568 | + const sb = SlowBuffer(10); | ||
| 2565 | 2569 | ||
| 2566 | - // Allocate for retained data | ||
| 2567 | - const sb = SlowBuffer(10); | ||
| 2570 | + // Copy the data into the new allocation | ||
| 2571 | + data.copy(sb, 0, 0, 10); | ||
| 2568 | 2572 | ||
| 2569 | - // Copy the data into the new allocation | ||
| 2570 | - data.copy(sb, 0, 0, 10); | ||
| 2571 | - | ||
| 2572 | - store.push(sb); | ||
| 2573 | + store.push(sb); | ||
| 2574 | + } | ||
| 2573 | 2575 | }); | |
| 2574 | 2576 | ``` | |
| 2575 | 2577 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -200,9 +200,10 @@ const cipher = crypto.createCipheriv(algorithm, key, iv); | |||
| 200 | 200 | ||
| 201 | 201 | let encrypted = ''; | |
| 202 | 202 | cipher.on('readable', () => { | |
| 203 | - const data = cipher.read(); | ||
| 204 | - if (data) | ||
| 205 | - encrypted += data.toString('hex'); | ||
| 203 | + let chunk; | ||
| 204 | + while (null !== (chunk = cipher.read())) { | ||
| 205 | + encrypted += chunk.toString('hex'); | ||
| 206 | + } | ||
| 206 | 207 | }); | |
| 207 | 208 | cipher.on('end', () => { | |
| 208 | 209 | console.log(encrypted); | |
@@ -383,9 +384,9 @@ const decipher = crypto.createDecipheriv(algorithm, key, iv); | |||
| 383 | 384 | ||
| 384 | 385 | let decrypted = ''; | |
| 385 | 386 | decipher.on('readable', () => { | |
| 386 | - const data = decipher.read(); | ||
| 387 | - if (data) | ||
| 388 | - decrypted += data.toString('utf8'); | ||
| 387 | + while (null !== (chunk = decipher.read())) { | ||
| 388 | + decrypted += chunk.toString('utf8'); | ||
| 389 | + } | ||
| 389 | 390 | }); | |
| 390 | 391 | decipher.on('end', () => { | |
| 391 | 392 | console.log(decrypted); | |
@@ -941,6 +942,8 @@ const crypto = require('crypto'); | |||
| 941 | 942 | const hash = crypto.createHash('sha256'); | |
| 942 | 943 | ||
| 943 | 944 | hash.on('readable', () => { | |
| 945 | + // Only one element is going to be produced by the | ||
| 946 | + // hash stream. | ||
| 944 | 947 | const data = hash.read(); | |
| 945 | 948 | if (data) { | |
| 946 | 949 | console.log(data.toString('hex')); | |
@@ -1033,6 +1036,8 @@ const crypto = require('crypto'); | |||
| 1033 | 1036 | const hmac = crypto.createHmac('sha256', 'a secret'); | |
| 1034 | 1037 | ||
| 1035 | 1038 | hmac.on('readable', () => { | |
| 1039 | + // Only one element is going to be produced by the | ||
| 1040 | + // hash stream. | ||
| 1036 | 1041 | const data = hmac.read(); | |
| 1037 | 1042 | if (data) { | |
| 1038 | 1043 | console.log(data.toString('hex')); | |
@@ -1762,6 +1767,8 @@ const hash = crypto.createHash('sha256'); | |||
| 1762 | 1767 | ||
| 1763 | 1768 | const input = fs.createReadStream(filename); | |
| 1764 | 1769 | input.on('readable', () => { | |
| 1770 | + // Only one element is going to be produced by the | ||
| 1771 | + // hash stream. | ||
| 1765 | 1772 | const data = input.read(); | |
| 1766 | 1773 | if (data) | |
| 1767 | 1774 | hash.update(data); | |
@@ -1807,6 +1814,8 @@ const hmac = crypto.createHmac('sha256', 'a secret'); | |||
| 1807 | 1814 | ||
| 1808 | 1815 | const input = fs.createReadStream(filename); | |
| 1809 | 1816 | input.on('readable', () => { | |
| 1817 | + // Only one element is going to be produced by the | ||
| 1818 | + // hash stream. | ||
| 1810 | 1819 | const data = input.read(); | |
| 1811 | 1820 | if (data) | |
| 1812 | 1821 | hmac.update(data); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1011,6 +1011,10 @@ readable.on('readable', () => { | |||
| 1011 | 1011 | }); | |
| 1012 | 1012 | ``` | |
| 1013 | 1013 | ||
| 1014 | + Note that the `while` loop is necessary when processing data with | ||
| 1015 | + `readable.read()`. Only after `readable.read()` returns `null`, | ||
| 1016 | + [`'readable'`]() will be emitted. | ||
| 1017 | + | ||
| 1014 | 1018 | A `Readable` stream in object mode will always return a single item from | |
| 1015 | 1019 | a call to [`readable.read(size)`][stream-read], regardless of the value of the | |
| 1016 | 1020 | `size` argument. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments