| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent eda91b6 commit 55f4844
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -593,6 +593,18 @@ added: | |||
| 593 | 593 | Returns a promise that fulfills with the contents of the `Blob` decoded as a | |
| 594 | 594 | UTF-8 string. | |
| 595 | 595 | ||
| 596 | + ### `blob.textStream()` | ||
| 597 | + | ||
| 598 | + <!-- YAML | ||
| 599 | + added: REPLACEME | ||
| 600 | + --> | ||
| 601 | + | ||
| 602 | + * Returns: {ReadableStream} | ||
| 603 | + | ||
| 604 | + Returns a new `ReadableStream` that allows the content of the `Blob` to be read | ||
| 605 | + as a stream of UTF-8 decoded strings. It is equivalent to piping | ||
| 606 | + [`blob.stream()`][] through a [`TextDecoderStream`][] set up with UTF-8. | ||
| 607 | + | ||
| 596 | 608 | ### `blob.type` | |
| 597 | 609 | ||
| 598 | 610 | <!-- YAML | |
@@ -5610,10 +5622,12 @@ introducing security vulnerabilities into an application. | |||
| 5610 | 5622 | [`String.prototype.indexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf | |
| 5611 | 5623 | [`String.prototype.lastIndexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf | |
| 5612 | 5624 | [`String.prototype.length`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length | |
| 5625 | + [`TextDecoderStream`]: webstreams.md#class-textdecoderstream | ||
| 5613 | 5626 | [`TypedArray.from()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from | |
| 5614 | 5627 | [`TypedArray.prototype.set()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set | |
| 5615 | 5628 | [`TypedArray.prototype.slice()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice | |
| 5616 | 5629 | [`TypedArray.prototype.subarray()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray | |
| 5630 | + [`blob.stream()`]: #blobstream | ||
| 5617 | 5631 | [`buf.buffer`]: #bufbuffer | |
| 5618 | 5632 | [`buf.compare()`]: #bufcomparetarget-targetstart-targetend-sourcestart-sourceend | |
| 5619 | 5633 | [`buf.entries()`]: #bufentries | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -85,6 +85,7 @@ const kNotCloneable = Symbol('kNotCloneable'); | |||
| 85 | 85 | const disallowedTypeCharacters = /[^\u{0020}-\u{007E}]/u; | |
| 86 | 86 | ||
| 87 | 87 | let ReadableStream; | |
| 88 | + let TextDecoderStream; | ||
| 88 | 89 | ||
| 89 | 90 | const enc = new TextEncoder(); | |
| 90 | 91 | let dec; | |
@@ -100,6 +101,13 @@ function lazyReadableStream(options) { | |||
| 100 | 101 | return new ReadableStream(options); | |
| 101 | 102 | } | |
| 102 | 103 | ||
| 104 | + function lazyTextDecoderStream() { | ||
| 105 | + // eslint-disable-next-line no-global-assign | ||
| 106 | + TextDecoderStream ??= | ||
| 107 | + require('internal/webstreams/encoding').TextDecoderStream; | ||
| 108 | + return new TextDecoderStream(); | ||
| 109 | + } | ||
| 110 | + | ||
| 103 | 111 | const { EOL } = require('internal/constants'); | |
| 104 | 112 | ||
| 105 | 113 | function isBlob(object) { | |
@@ -325,6 +333,17 @@ class Blob { | |||
| 325 | 333 | throw new ERR_INVALID_THIS('Blob'); | |
| 326 | 334 | return createBlobReaderStream(this[kHandle].getReader()); | |
| 327 | 335 | } | |
| 336 | + | ||
| 337 | + /** | ||
| 338 | + * @returns {ReadableStream} | ||
| 339 | + */ | ||
| 340 | + textStream() { | ||
| 341 | + if (!isBlob(this)) | ||
| 342 | + throw new ERR_INVALID_THIS('Blob'); | ||
| 343 | + const stream = createBlobReaderStream(this[kHandle].getReader()); | ||
| 344 | + const decoder = lazyTextDecoderStream(); | ||
| 345 | + return stream.pipeThrough(decoder); | ||
| 346 | + } | ||
| 328 | 347 | } | |
| 329 | 348 | ||
| 330 | 349 | function TransferableBlob(handle, length, type = '') { | |
@@ -358,6 +377,7 @@ ObjectDefineProperties(Blob.prototype, { | |||
| 358 | 377 | type: kEnumerableProperty, | |
| 359 | 378 | slice: kEnumerableProperty, | |
| 360 | 379 | stream: kEnumerableProperty, | |
| 380 | + textStream: kEnumerableProperty, | ||
| 361 | 381 | text: kEnumerableProperty, | |
| 362 | 382 | arrayBuffer: kEnumerableProperty, | |
| 363 | 383 | bytes: kEnumerableProperty, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,7 @@ | |||
| 3 | 3 | ||
| 4 | 4 | const common = require('../common'); | |
| 5 | 5 | const assert = require('assert'); | |
| 6 | - const { Blob } = require('buffer'); | ||
| 6 | + const { Blob, File } = require('buffer'); | ||
| 7 | 7 | const { inspect } = require('util'); | |
| 8 | 8 | const { EOL } = require('os'); | |
| 9 | 9 | const { kState } = require('internal/webstreams/util'); | |
@@ -524,3 +524,22 @@ assert.throws(() => new Blob({}), { | |||
| 524 | 524 | Blob.prototype.arrayBuffer = arrayBuffer; | |
| 525 | 525 | } | |
| 526 | 526 | })().then(common.mustCall()); | |
| 527 | + | ||
| 528 | + { | ||
| 529 | + assert.strictEqual(typeof Blob.prototype.textStream, 'function'); | ||
| 530 | + assert.strictEqual(typeof File.prototype.textStream, 'function'); | ||
| 531 | + assert.strictEqual(File.prototype.textStream, Blob.prototype.textStream); | ||
| 532 | + } | ||
| 533 | + | ||
| 534 | + (async () => { | ||
| 535 | + const smiley = Buffer.from('😀', 'utf8'); | ||
| 536 | + const blob = new Blob(['hello ', smiley.subarray(0, 2), smiley.subarray(2)]); | ||
| 537 | + const stream = blob.textStream(); | ||
| 538 | + assert.ok(stream instanceof ReadableStream); | ||
| 539 | + let result = ''; | ||
| 540 | + for await (const chunk of stream) { | ||
| 541 | + assert.strictEqual(typeof chunk, 'string'); | ||
| 542 | + result += chunk; | ||
| 543 | + } | ||
| 544 | + assert.strictEqual(result, 'hello 😀'); | ||
| 545 | + })().then(common.mustCall()); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments