Skip to content

Add simpler reading methods to Blob interface. #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ spec: url
text: url; for:/
type: interface
text: URL
spec: fetch
type:interface
text:ReadableStream
</pre>

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

<pre class="idl">
<xmp class="idl">
[Constructor(optional sequence&lt;BlobPart> blobParts,
optional BlobPropertyBag options),
Exposed=(Window,Worker), Serializable]
Expand All @@ -228,6 +231,11 @@ interface Blob {
Blob slice(optional [Clamp] long long start,
optional [Clamp] long long end,
optional DOMString contentType);

// read from the Blob.
[NewObject] ReadableStream stream();
[NewObject] Promise<USVString> text();
[NewObject] Promise<ArrayBuffer> arrayBuffer();
};

enum EndingType { "transparent", "native" };
Expand All @@ -238,7 +246,7 @@ dictionary BlobPropertyBag {
};

typedef (BufferSource or Blob or USVString) BlobPart;
</pre>
</xmp>

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

### The {{Blob/stream()}} method ### {#stream-method-algo}

The <dfn method for=Blob>stream()</dfn> method, when invoked, must return
the result of calling [=get stream=] on the [=context object=].

### The {{Blob/text()}} method ### {#text-method-algo}

The <dfn method for=Blob>text()</dfn> method, when invoked, must run these steps:

1. Let |stream| be the result of calling [=get stream=] on the [=context object=].
1. Let |reader| be the result of [=get a reader|getting a reader=] from |stream|.
If that threw an exception, return a new promise rejected with that exception.
1. Let |promise| be the result of [=read all bytes|reading all bytes=] from |stream| with |reader|.
1. Return the result of transforming |promise| by a fulfillment handler that returns the result of
running [=UTF-8 decode=] on its first argument.

Note: This is different from the behavior of {{FileReader/readAsText()}} to align better
with the behavior of {{Body/text()|Fetch's text()}}. Specifically this method will always
use UTF-8 as encoding, while {{FileReader}} can use a different encoding depending on
the blob's type and passed in encoding name.

### The {{Blob/arrayBuffer()}} method ### {#arraybuffer-method-algo}

The <dfn method for=Blob>arrayBuffer()</dfn> method, when invoked, must run these steps:

1. Let |stream| be the result of calling [=get stream=] on the [=context object=].
1. Let |reader| be the result of [=get a reader|getting a reader=] from |stream|.
If that threw an exception, return a new promise rejected with that exception.
1. Let |promise| be the result of [=read all bytes|reading all bytes=] from |stream| with |reader|.
1. Return the result of transforming |promise| by a fulfillment handler that returns
a new {{ArrayBuffer}} whose contents are its first argument.

<!--
████████ ████ ██ ████████
██ ██ ██ ██
Expand Down