-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Passes most tests except blocked on jsdom/webidl2js#123 (comment) and jsdom/webidl2js#79
- Loading branch information
Showing
21 changed files
with
714 additions
and
871 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
dictionary QueuingStrategy { | ||
unrestricted double highWaterMark; | ||
QueuingStrategySize size; | ||
}; | ||
|
||
callback QueuingStrategySize = unrestricted double (optional any chunk); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
dictionary UnderlyingSink { | ||
WritableStreamStartCallback start; | ||
WritableStreamWriteCallback write; | ||
WritableStreamCloseCallback close; | ||
WritableStreamAbortCallback abort; | ||
any type; | ||
}; | ||
|
||
callback WritableStreamStartCallback = Promise<void> (WritableStreamDefaultController controller); | ||
callback WritableStreamWriteCallback = Promise<void> (WritableStreamDefaultController controller, optional any chunk); | ||
callback WritableStreamCloseCallback = Promise<void> (); | ||
callback WritableStreamAbortCallback = Promise<void> (optional any reason); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict'; | ||
const { promiseRejectedWith } = require('./webidl-helpers.js'); | ||
|
||
const { ExtractHighWaterMark, ExtractSizeAlgorithm } = require('./abstract-ops/queuing-strategy.js'); | ||
const aos = require('./abstract-ops/writable-streams.js'); | ||
|
||
const UnderlyingSink = require('../generated/UnderlyingSink.js'); | ||
|
||
exports.implementation = class WritableStreamImpl { | ||
constructor(globalObject, [underlyingSink, strategy]) { | ||
if (underlyingSink === undefined) { | ||
underlyingSink = null; | ||
} | ||
const underlyingSinkDict = UnderlyingSink.convert(underlyingSink); | ||
if ('type' in underlyingSinkDict) { | ||
throw new RangeError('Invalid type is specified'); | ||
} | ||
|
||
aos.InitializeWritableStream(this); | ||
|
||
const sizeAlgorithm = ExtractSizeAlgorithm(strategy); | ||
const highWaterMark = ExtractHighWaterMark(strategy, 1); | ||
|
||
aos.SetUpWritableStreamDefaultControllerFromUnderlyingSink( | ||
this, underlyingSink, underlyingSinkDict, highWaterMark, sizeAlgorithm | ||
); | ||
} | ||
|
||
get locked() { | ||
return aos.IsWritableStreamLocked(this); | ||
} | ||
|
||
abort(reason) { | ||
if (aos.IsWritableStreamLocked(this) === true) { | ||
return promiseRejectedWith(new TypeError('Cannot abort a stream that already has a writer')); | ||
} | ||
|
||
return aos.WritableStreamAbort(this, reason); | ||
} | ||
|
||
close() { | ||
if (aos.IsWritableStreamLocked(this) === true) { | ||
return promiseRejectedWith(new TypeError('Cannot close a stream that already has a writer')); | ||
} | ||
|
||
if (aos.WritableStreamCloseQueuedOrInFlight(this) === true) { | ||
return promiseRejectedWith(new TypeError('Cannot close an already-closing stream')); | ||
} | ||
|
||
return aos.WritableStreamClose(this); | ||
} | ||
|
||
getWriter() { | ||
return aos.AcquireWritableStreamDefaultWriter(this); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[Exposed=(Window,Worker,Worklet)] | ||
interface WritableStream { | ||
constructor(optional object underlyingSink, optional QueuingStrategy strategy = {}); | ||
|
||
readonly attribute boolean locked; | ||
|
||
Promise<void> abort(optional any reason); | ||
Promise<void> close(); | ||
WritableStreamDefaultWriter getWriter(); | ||
}; |
28 changes: 28 additions & 0 deletions
28
reference-implementation/lib/WritableStreamDefaultController-impl.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
const aos = require('./abstract-ops/writable-streams.js'); | ||
const { AbortSteps, ErrorSteps } = require('./abstract-ops/internal-methods.js'); | ||
const { ResetQueue } = require('./abstract-ops/queue-with-sizes.js'); | ||
|
||
exports.implementation = class WritableStreamDefaultControllerImpl { | ||
error(e) { | ||
const state = this._controlledWritableStream._state; | ||
|
||
if (state !== 'writable') { | ||
// The stream is closed, errored or will be soon. The sink can't do anything useful if it gets an error here, so | ||
// just treat it as a no-op. | ||
return; | ||
} | ||
|
||
aos.WritableStreamDefaultControllerError(this, e); | ||
} | ||
|
||
[AbortSteps](reason) { | ||
const result = this._abortAlgorithm(reason); | ||
aos.WritableStreamDefaultControllerClearAlgorithms(this); | ||
return result; | ||
} | ||
|
||
[ErrorSteps]() { | ||
ResetQueue(this); | ||
} | ||
}; |
4 changes: 4 additions & 0 deletions
4
reference-implementation/lib/WritableStreamDefaultController.webidl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[Exposed=(Window,Worker,Worklet)] | ||
interface WritableStreamDefaultController { | ||
void error(optional any e); | ||
}; |
74 changes: 74 additions & 0 deletions
74
reference-implementation/lib/WritableStreamDefaultWriter-impl.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
'use strict'; | ||
const assert = require('assert'); | ||
|
||
const { promiseRejectedWith } = require('./webidl-helpers.js'); | ||
|
||
const aos = require('./abstract-ops/writable-streams.js'); | ||
|
||
exports.implementation = class WritableStreamDefaultWriterImpl { | ||
constructor(globalObject, [stream]) { | ||
aos.SetUpWritableStreamDefaultWriter(this, stream); | ||
} | ||
|
||
get closed() { | ||
return this._closedPromise; | ||
} | ||
|
||
get desiredSize() { | ||
if (this._ownerWritableStream === undefined) { | ||
throw defaultWriterLockException('desiredSize'); | ||
} | ||
|
||
return aos.WritableStreamDefaultWriterGetDesiredSize(this); | ||
} | ||
|
||
get ready() { | ||
return this._readyPromise; | ||
} | ||
|
||
abort(reason) { | ||
if (this._ownerWritableStream === undefined) { | ||
return promiseRejectedWith(defaultWriterLockException('abort')); | ||
} | ||
|
||
return aos.WritableStreamDefaultWriterAbort(this, reason); | ||
} | ||
|
||
close() { | ||
const stream = this._ownerWritableStream; | ||
|
||
if (stream === undefined) { | ||
return promiseRejectedWith(defaultWriterLockException('close')); | ||
} | ||
|
||
if (aos.WritableStreamCloseQueuedOrInFlight(stream) === true) { | ||
return promiseRejectedWith(new TypeError('Cannot close an already-closing stream')); | ||
} | ||
|
||
return aos.WritableStreamDefaultWriterClose(this); | ||
} | ||
|
||
releaseLock() { | ||
const stream = this._ownerWritableStream; | ||
|
||
if (stream === undefined) { | ||
return; | ||
} | ||
|
||
assert(stream._writer !== undefined); | ||
|
||
aos.WritableStreamDefaultWriterRelease(this); | ||
} | ||
|
||
write(chunk) { | ||
if (this._ownerWritableStream === undefined) { | ||
return promiseRejectedWith(defaultWriterLockException('write to')); | ||
} | ||
|
||
return aos.WritableStreamDefaultWriterWrite(this, chunk); | ||
} | ||
}; | ||
|
||
function defaultWriterLockException(name) { | ||
return new TypeError('Cannot ' + name + ' a stream using a released writer'); | ||
} |
13 changes: 13 additions & 0 deletions
13
reference-implementation/lib/WritableStreamDefaultWriter.webidl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[Exposed=(Window,Worker,Worklet)] | ||
interface WritableStreamDefaultWriter { | ||
constructor(WritableStream stream); | ||
|
||
readonly attribute Promise<void> closed; | ||
readonly attribute unrestricted double? desiredSize; | ||
readonly attribute Promise<void> ready; | ||
|
||
Promise<void> abort(optional any reason); | ||
Promise<void> close(); | ||
void releaseLock(); | ||
Promise<void> write(optional any chunk); | ||
}; |
4 changes: 4 additions & 0 deletions
4
reference-implementation/lib/abstract-ops/internal-methods.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
'use strict'; | ||
|
||
exports.AbortSteps = Symbol('[[AbortSteps]]'); | ||
exports.ErrorSteps = Symbol('[[ErrorSteps]]'); |
17 changes: 17 additions & 0 deletions
17
reference-implementation/lib/abstract-ops/miscellaneous.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
exports.IsNonNegativeNumber = v => { | ||
if (typeof v !== 'number') { | ||
return false; | ||
} | ||
|
||
if (Number.isNaN(v)) { | ||
return false; | ||
} | ||
|
||
if (v < 0) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
reference-implementation/lib/abstract-ops/queuing-strategy.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
const { invoke } = require('../webidl-helpers.js'); | ||
|
||
exports.ExtractHighWaterMark = (strategy, defaultHWM) => { | ||
if (!('highWaterMark' in strategy)) { | ||
return defaultHWM; | ||
} | ||
|
||
const { highWaterMark } = strategy; | ||
if (Number.isNaN(highWaterMark) || highWaterMark < 0) { | ||
throw new RangeError('Invalid highWaterMark'); | ||
} | ||
|
||
return highWaterMark; | ||
}; | ||
|
||
exports.ExtractSizeAlgorithm = strategy => { | ||
if (!('size' in strategy)) { | ||
return () => 1; | ||
} | ||
|
||
return chunk => { | ||
// TODO: manual number conversion won't be necessary when https://github.com/jsdom/webidl2js/pull/123 lands. | ||
return Number(invoke(strategy.size, [chunk])); | ||
}; | ||
}; |
Oops, something went wrong.