From 26bf46aea740147bbcf126a3d0f9d6fde033138f Mon Sep 17 00:00:00 2001 From: Marijn Kruisselbrink Date: Thu, 21 Feb 2019 16:43:20 -0800 Subject: [PATCH] Add simpler reading methods to Blob interface. This fixes #40 --- index.bs | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/index.bs b/index.bs index 0f28ebd..f48d53d 100644 --- a/index.bs +++ b/index.bs @@ -65,6 +65,9 @@ spec: url text: url; for:/ type: interface text: URL +spec: fetch + type:interface + text:ReadableStream
@@ -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 snapshot state can be found for {{File}}s.
 
-
+
 [Constructor(optional sequence<BlobPart> blobParts,
              optional BlobPropertyBag options),
  Exposed=(Window,Worker), Serializable]
@@ -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.
+  ReadableStream stream();
+  Promise<USVString> text();
+  Promise<ArrayBuffer> arrayBuffer();
 };
 
 enum EndingType { "transparent", "native" };
@@ -238,7 +246,7 @@ dictionary BlobPropertyBag {
 };
 
 typedef (BufferSource or Blob or USVString) BlobPart;
-</pre>
+
 
 {{Blob}} objects are [=serializable objects=]. Their [=serialization steps=],
 given |value| and |serialized|, are:
@@ -561,6 +569,59 @@ It must act as follows:
   
+

+The stream method

+ +The stream() method returns the contents +of the blob as a {{ReadableStream}}. + +It must return the result of [=getting a stream for a Blob=] for the [=context object=]. + +To get a stream for a {{Blob}} +|blob|, run the following steps: + +1. Let |stream| be the result of [=construct a ReadableStream object|constructing=] a + {{ReadableStream}} object. +1. Return |stream| and run the following steps [=in parallel=]: + 1. While not all bytes of |blob| have been read: + 1. Let |bytes| be the byte sequence that results from reading a [=chunk=] from |blob|. + 1. If a [=file read error=] occured while reading |bytes|, [$ReadableStream/error$] + |stream| with a [=failure reason=] and abort these steps. + 1. [=ReadableStream/Enqueue=] |bytes| into |stream|. + + Issue: We need to specify more concretely what reading from a Blob actually does, and what + possible errors can happen. + +

+The text method

+ +The text() method returns the contents +of the blob as a {{USVString}}. + +It must act as follows: + +1. Let |stream| be the the result of [=getting a stream for a Blob=] for the [=context object=]. +1. Let |reader| be the result of [=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 [=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. + +

+The arrayBuffer method

+ +The arrayBuffer() method returns the contents +of the blob as an {{ArrayBuffer}}. + +It must act as follows: + +1. Let |stream| be the the result of [=getting a stream for a Blob=] for the [=context object=]. +1. Let |reader| be the result of [=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 [=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. +