Skip to content

Commit b0fa7ef

Browse files
Add simpler reading methods to Blob interface. (#117)
* Add simpler reading methods to Blob interface. This fixes #40
1 parent 795750f commit b0fa7ef

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

index.bs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ spec: url
6565
text: url; for:/
6666
type: interface
6767
text: URL
68+
spec: fetch
69+
type:interface
70+
text:ReadableStream
6871
</pre>
6972

7073
<pre class="anchors">
@@ -215,7 +218,7 @@ which must be initially set to the state of the underlying storage,
215218
if any such underlying storage exists.
216219
Further normative definition of <a>snapshot state</a> can be found for {{File}}s.
217220

218-
<pre class="idl">
221+
<xmp class="idl">
219222
[Constructor(optional sequence&lt;BlobPart> blobParts,
220223
optional BlobPropertyBag options),
221224
Exposed=(Window,Worker), Serializable]
@@ -228,6 +231,11 @@ interface Blob {
228231
Blob slice(optional [Clamp] long long start,
229232
optional [Clamp] long long end,
230233
optional DOMString contentType);
234+
235+
// read from the Blob.
236+
[NewObject] ReadableStream stream();
237+
[NewObject] Promise<USVString> text();
238+
[NewObject] Promise<ArrayBuffer> arrayBuffer();
231239
};
232240

233241
enum EndingType { "transparent", "native" };
@@ -238,7 +246,7 @@ dictionary BlobPropertyBag {
238246
};
239247

240248
typedef (BufferSource or Blob or USVString) BlobPart;
241-
</pre>
249+
</xmp>
242250

243251
{{Blob}} objects are [=serializable objects=]. Their [=serialization steps=],
244252
given |value| and |serialized|, are:
@@ -561,6 +569,38 @@ It must act as follows:
561569
</pre>
562570
</div>
563571

572+
### The {{Blob/stream()}} method ### {#stream-method-algo}
573+
574+
The <dfn method for=Blob>stream()</dfn> method, when invoked, must return
575+
the result of calling [=get stream=] on the [=context object=].
576+
577+
### The {{Blob/text()}} method ### {#text-method-algo}
578+
579+
The <dfn method for=Blob>text()</dfn> method, when invoked, must run these steps:
580+
581+
1. Let |stream| be the result of calling [=get stream=] on the [=context object=].
582+
1. Let |reader| be the result of [=get a reader|getting a reader=] from |stream|.
583+
If that threw an exception, return a new promise rejected with that exception.
584+
1. Let |promise| be the result of [=read all bytes|reading all bytes=] from |stream| with |reader|.
585+
1. Return the result of transforming |promise| by a fulfillment handler that returns the result of
586+
running [=UTF-8 decode=] on its first argument.
587+
588+
Note: This is different from the behavior of {{FileReader/readAsText()}} to align better
589+
with the behavior of {{Body/text()|Fetch's text()}}. Specifically this method will always
590+
use UTF-8 as encoding, while {{FileReader}} can use a different encoding depending on
591+
the blob's type and passed in encoding name.
592+
593+
### The {{Blob/arrayBuffer()}} method ### {#arraybuffer-method-algo}
594+
595+
The <dfn method for=Blob>arrayBuffer()</dfn> method, when invoked, must run these steps:
596+
597+
1. Let |stream| be the result of calling [=get stream=] on the [=context object=].
598+
1. Let |reader| be the result of [=get a reader|getting a reader=] from |stream|.
599+
If that threw an exception, return a new promise rejected with that exception.
600+
1. Let |promise| be the result of [=read all bytes|reading all bytes=] from |stream| with |reader|.
601+
1. Return the result of transforming |promise| by a fulfillment handler that returns
602+
a new {{ArrayBuffer}} whose contents are its first argument.
603+
564604
<!--
565605
████████ ████ ██ ████████
566606
██ ██ ██ ██

0 commit comments

Comments
 (0)