Skip to content

Commit

Permalink
Change how you construct a readable byte stream
Browse files Browse the repository at this point in the history
Instead of `byob: true`, it's now `type: "bytes"`. Partially fixes #294.
  • Loading branch information
tyoshino authored and domenic committed Apr 8, 2016
1 parent 0c2d4cb commit 3a6caaa
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 80 deletions.
28 changes: 14 additions & 14 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,10 @@ Instances of {{ReadableStream}} are created with the internal slots described in
state via the passed <code>controller</code> object. This is an example of the
<a href="https://blog.domenic.me/the-revealing-constructor-pattern/">revealing constructor pattern</a>.

If the <code>underlyingSource</code> object contains a truthy property <code>byob</code>, this <a>readable stream</a> is
a <a>readable byte stream</a>, and can successfully vend <a>BYOB readers</a>. In that case, the passed
<code>controller</code> object will be an instance of {{ReadableByteStreamController}}. Otherwise, it will be an instance of
{{ReadableStreamDefaultController}}.
If the <code>underlyingSource</code> object contains a property <code>type</code> set to <code>"bytes"</code>, this
<a>readable stream</a> is a <a>readable byte stream</a>, and can successfully vend <a>BYOB readers</a>. In that case,
the passed <code>controller</code> object will be an instance of {{ReadableByteStreamController}}. Otherwise, it will
be an instance of {{ReadableStreamDefaultController}}.

For <a>readable byte streams</a>, <code>underlyingSource</code> can also contain a property
<code>autoAllocateChunkSize</code>, which can be set to a positive integer to enable the auto-allocation feature for
Expand All @@ -447,16 +447,16 @@ Instances of {{ReadableStream}} are created with the internal slots described in
1. Set *this*@[[reader]] and *this*@[[storedError]] to *undefined*.
1. Set *this*@[[disturbed]] to *false*.
1. Set *this*@[[readableStreamController]] to *undefined*.
1. Let _byob_ be ToBoolean(GetV(_underlyingSource_, `"byob"`)).
1. ReturnIfAbrupt(_byob_).
1. If _byob_ is *true*,
1. If _highWaterMark_ is *undefined*,
1. Let _highWaterMark_ be 0.
1. Let _type_ be GetV(_underlyingSource_, `"type"`).
1. ReturnIfAbrupt(_type_).
1. Let _typeString_ be ToString(_type_).
1. If _typeString_ is `"bytes"`,
1. If _highWaterMark_ is *undefined*, let _highWaterMark_ be 0.
1. Set *this*@[[readableStreamController]] to Construct(`<a idl>ReadableByteStreamController</a>`, «*this*, _underlyingSource_, _highWaterMark_»).
1. Otherwise,
1. If _highWaterMark_ is *undefined*,
1. Let _highWaterMark_ be 1.
1. Otherwise, if _type_ is *undefined*,
1. If _highWaterMark_ is *undefined*, let _highWaterMark_ be 1.
1. Set *this*@[[readableStreamController]] to Construct(`<a idl>ReadableStreamDefaultController</a>`, «*this*, _underlyingSource_, _size_, _highWaterMark_»).
1. Otherwise, throw a *RangeError* exception.
</emu-alg>

<h4 id="rs-prototype">Properties of the {{ReadableStream}} Prototype</h4>
Expand Down Expand Up @@ -3149,7 +3149,7 @@ BYOB reader, to move the data from the stream's internal queue to the developer-
const socket = createHypotheticalSelect2Socket(host, port);

return new ReadableStream({
byob: true,
type: "bytes",

start(controller) {
socket.setTCPWindowSize(Math.max(0, controller.desiredSize));
Expand Down Expand Up @@ -3256,7 +3256,7 @@ size of 1024, it attempts to fill the developer-supplied buffer, allowing full c
let position = 0;

return new ReadableStream({
byob: true,
type: "bytes",

start() {
return fs.open(filename, "r").then(result => {
Expand Down
9 changes: 6 additions & 3 deletions reference-implementation/lib/readable-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ export default class ReadableStream {
// Initialize to undefined first because the constructor of the controller checks this
// variable to validate the caller.
this._readableStreamController = undefined;
const byob = underlyingSource['byob'];
if (byob === true) {
const type = underlyingSource.type;
const typeString = String(type);
if (typeString === 'bytes') {
if (highWaterMark === undefined) {
highWaterMark = 0;
}
this._readableStreamController = new ReadableByteStreamController(this, underlyingSource, highWaterMark);
} else {
} else if (type === undefined) {
if (highWaterMark === undefined) {
highWaterMark = 1;
}
this._readableStreamController = new ReadableStreamDefaultController(this, underlyingSource, size, highWaterMark);
} else {
throw new RangeError('Invalid type is specified');
}
}

Expand Down

0 comments on commit 3a6caaa

Please sign in to comment.