@ohos-ports/whatwg-streams 0.1.1-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENCE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2014 Raynos.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # whatwg-streams
2
+
3
+ [![build status][1]][2] [![NPM version][3]][4] [![Coverage Status][5]][6] [![Davis Dependency status][9]][10]
4
+
5
+ [![browser support][11]][12]
6
+
7
+ An implementation of the whatwg/streams spec
8
+
9
+ ## Example
10
+
11
+ ```js
12
+ var whatwgStreams = require("whatwg-streams")
13
+
14
+ // TODO. Show example
15
+ ```
16
+
17
+ ## Installation
18
+
19
+ `npm install whatwg-streams`
20
+
21
+ ## Contributors
22
+
23
+ - Raynos
24
+
25
+ ## MIT Licenced
26
+
27
+ [1]: https://secure.travis-ci.org/Raynos/whatwg-streams.png
28
+ [2]: https://travis-ci.org/Raynos/whatwg-streams
29
+ [3]: https://badge.fury.io/js/whatwg-streams.png
30
+ [4]: https://badge.fury.io/js/whatwg-streams
31
+ [5]: https://coveralls.io/repos/Raynos/whatwg-streams/badge.png
32
+ [6]: https://coveralls.io/r/Raynos/whatwg-streams
33
+ [7]: https://gemnasium.com/Raynos/whatwg-streams.png
34
+ [8]: https://gemnasium.com/Raynos/whatwg-streams
35
+ [9]: https://david-dm.org/Raynos/whatwg-streams.png
36
+ [10]: https://david-dm.org/Raynos/whatwg-streams
37
+ [11]: https://ci.testling.com/Raynos/whatwg-streams.png
38
+ [12]: https://ci.testling.com/Raynos/whatwg-streams
@@ -0,0 +1,53 @@
1
+ var ReadableStreamState = require("./readable/stream-state.js")
2
+ var constructor = require("./readable/constructor.js")
3
+ var read = require("./readable/read.js")
4
+ var waitForReadable = require("./readable/wait-for-readable.js")
5
+ var abort = require("./readable/abort.js")
6
+
7
+ var defineProperty = Object.defineProperty
8
+
9
+ function BaseReadableStream(options) {
10
+ if (!(this instanceof BaseReadableStream)) {
11
+ return new BaseReadableStream(options)
12
+ }
13
+
14
+ this._state = new ReadableStreamState(options)
15
+
16
+ constructor(this._state, options.start)
17
+ }
18
+
19
+ var proto = BaseReadableStream.prototype
20
+
21
+ proto.read = function protoRead() {
22
+ return read(this._state)
23
+ }
24
+
25
+ proto.waitForReadable = function protoWaitForReadable() {
26
+ return waitForReadable(this._state)
27
+ }
28
+
29
+ defineProperty(proto, "readableState", {
30
+ configurable: true,
31
+ get: function getReadableState() {
32
+ return this._state.readableState
33
+ }
34
+ })
35
+
36
+ proto.abort = function protoAbort(reason) {
37
+ return abort(this._state, reason)
38
+ }
39
+
40
+ defineProperty(proto, "finished", {
41
+ configurable: true,
42
+ get: function getFinished() {
43
+ return this._state.finished
44
+ }
45
+ })
46
+
47
+ // TODO IMPLEMENT
48
+ proto.pipe = function protoPipe() {}
49
+
50
+ module.exports = BaseReadableStream
51
+
52
+
53
+
@@ -0,0 +1,52 @@
1
+ var WritableStreamState = require("./writable/stream-state.js")
2
+ var constructor = require("./writable/constructor.js")
3
+ var write = require("./writable/write.js")
4
+ var waitForWritable = require("./writable/wait-for-writable.js")
5
+ var close = require("./writable/close.js")
6
+ var dispose = require("./writable/dispose.js")
7
+
8
+ var defineProperty = Object.defineProperty
9
+
10
+ function BaseWritableStream(options) {
11
+ if (!(this instanceof BaseWritableStream)) {
12
+ return new BaseWritableStream(options)
13
+ }
14
+
15
+ this._state = new WritableStreamState(options)
16
+
17
+ constructor(this._state, options.start)
18
+ }
19
+
20
+ var proto = BaseWritableStream.prototype
21
+
22
+ proto.write = function protoWrite(chunk) {
23
+ return write(this._state, chunk)
24
+ }
25
+
26
+ proto.waitForWritable = function protoWaitForWritable() {
27
+ return waitForWritable(this._state)
28
+ }
29
+
30
+ defineProperty(proto, "writableState", {
31
+ configurable: true,
32
+ get: function getWritableState() {
33
+ return this._state.writableState
34
+ }
35
+ })
36
+
37
+ proto.close = function protoClose() {
38
+ return close(this._state)
39
+ }
40
+
41
+ proto.dispose = function protoDispose(reason) {
42
+ return dispose(this._state, reason)
43
+ }
44
+
45
+ defineProperty(proto, "closed", {
46
+ configurable: true,
47
+ get: function getClose() {
48
+ return this._state.closedPromise
49
+ }
50
+ })
51
+
52
+ module.exports = BaseWritableStream
package/index.js ADDED
@@ -0,0 +1,13 @@
1
+ // Main entry restored as part of the HarmonyOS migration: the 0.1.1 npm
2
+ // tarball (and the upstream v0.1.1 tag) ship no index.js even though
3
+ // package.json declares "main": "index".
4
+ //
5
+ // At v0.1.1 only the readable half of the package is complete:
6
+ // base-writable.js requires writable/write.js, writable/wait-for-writable.js,
7
+ // writable/close.js and writable/dispose.js, none of which exist in the
8
+ // published package or in the upstream v0.1.1 tag (writable/constructor.js is
9
+ // an empty function upstream). Requiring base-writable here would therefore
10
+ // throw MODULE_NOT_FOUND, so this entry only exports BaseReadableStream.
11
+ module.exports = {
12
+ BaseReadableStream: require("./base-readable.js")
13
+ }
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@ohos-ports/whatwg-streams",
3
+ "version": "0.1.1-beta.1",
4
+ "description": "An implementation of the whatwg/streams spec",
5
+ "keywords": [],
6
+ "author": "Raynos <raynos2@gmail.com>",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/ohos-ports/ohos-ports.git",
10
+ "directory": "ports/whatwg-streams/0.1.1"
11
+ },
12
+ "main": "index",
13
+ "homepage": "https://github.com/Raynos/whatwg-streams",
14
+ "contributors": [
15
+ {
16
+ "name": "Raynos"
17
+ }
18
+ ],
19
+ "bugs": {
20
+ "url": "https://github.com/ohos-ports/ohos-ports/issues",
21
+ "email": "raynos2@gmail.com"
22
+ },
23
+ "dependencies": {
24
+ "tape": "~2.3.2",
25
+ "bluebird": "~1.0.0"
26
+ },
27
+ "devDependencies": {
28
+ "tape": "~1.0.2"
29
+ },
30
+ "licenses": [
31
+ {
32
+ "type": "MIT",
33
+ "url": "http://github.com/Raynos/whatwg-streams/raw/master/LICENSE"
34
+ }
35
+ ],
36
+ "scripts": {
37
+ "test": "node ./test/index.js",
38
+ "start": "node ./index.js",
39
+ "watch": "nodemon -w ./index.js index.js",
40
+ "travis-test": "istanbul cover ./test/index.js && ((cat coverage/lcov.info | coveralls) || exit 0)",
41
+ "cover": "istanbul cover --report none --print detail ./test/index.js",
42
+ "view-cover": "istanbul report html && google-chrome ./coverage/index.html",
43
+ "test-browser": "testem-browser ./test/browser/index.js",
44
+ "testem": "testem-both -b=./test/browser/index.js"
45
+ },
46
+ "testling": {
47
+ "files": "test/index.js",
48
+ "browsers": [
49
+ "ie/8..latest",
50
+ "firefox/16..latest",
51
+ "firefox/nightly",
52
+ "chrome/22..latest",
53
+ "chrome/canary",
54
+ "opera/12..latest",
55
+ "opera/next",
56
+ "safari/5.1..latest",
57
+ "ipad/6.0..latest",
58
+ "iphone/6.0..latest",
59
+ "android-browser/4.2..latest"
60
+ ]
61
+ }
62
+ }
@@ -0,0 +1,27 @@
1
+ var Promise = require("bluebird")
2
+
3
+ module.exports = abort
4
+
5
+ function abort(streamState, reason) {
6
+ // NON STANDARD
7
+ if (streamState.readableState === "finished") {
8
+ throw new Error("cannot abort finished stream")
9
+ // NON STANDARD
10
+ } else if (streamState.readableState === "errored") {
11
+ throw new Error("cannot abort errored stream")
12
+ }
13
+
14
+ streamState.onAbort(reason)
15
+ streamState.finishedPromise._fulfill(undefined)
16
+ // should this be errored ?
17
+ streamState.readableState = "finished"
18
+
19
+ if (streamState.readableState === "waiting") {
20
+ // Question: should `reason` be cast to an `Error` object
21
+ // if it's a string
22
+ streamState.readablePromise._reject(reason)
23
+ } else if (streamState.readableState === "readable") {
24
+ streamState.readablePromise = Promise.rejected(reason)
25
+ streamState.buffer.length = 0
26
+ }
27
+ }
@@ -0,0 +1,27 @@
1
+ var push = require("./push.js")
2
+ var finish = require("./finish.js")
3
+ var error = require("./error.js")
4
+
5
+ module.exports = callPull
6
+
7
+ function callPull(streamState) {
8
+ if (streamState.pulling) {
9
+ return
10
+ }
11
+
12
+ if (streamState.started) {
13
+ streamState.onPull(
14
+ push.bind(null, streamState),
15
+ finish.bind(null, streamState),
16
+ error.bind(null, streamState)
17
+ )
18
+ } else {
19
+ streamState.startedPromise.then(function () {
20
+ streamState.onPull(
21
+ push.bind(null, streamState),
22
+ finish.bind(null, streamState),
23
+ error.bind(null, streamState)
24
+ )
25
+ })
26
+ }
27
+ }
@@ -0,0 +1,26 @@
1
+ var Promise = require("bluebird")
2
+
3
+ var error = require("./error.js")
4
+ var push = require("./push.js")
5
+ var finish = require("./finish.js")
6
+
7
+ module.exports = constructor
8
+
9
+ function constructor(streamState, start) {
10
+ var value = start(
11
+ push.bind(null, streamState),
12
+ finish.bind(null, streamState),
13
+ error.bind(null, streamState)
14
+ )
15
+
16
+ var startedPromise = streamState.startedPromise = Promise.cast(value)
17
+ startedPromise.then(onFulfill, onReject)
18
+
19
+ function onFulfill() {
20
+ streamState.started = true
21
+ }
22
+
23
+ function onReject(reason) {
24
+ error(streamState, reason)
25
+ }
26
+ }
@@ -0,0 +1,25 @@
1
+ var Promise = require("bluebird")
2
+
3
+ module.exports = error
4
+
5
+ function error(streamState, err) {
6
+ // NON STANDARD
7
+ if (streamState.readableState === "errored") {
8
+ throw new Error("cannot error() a stream in errored state")
9
+ // NON STANDARD
10
+ } else if (streamState.readableState === "finished") {
11
+ throw new Error("cannot error() a stream in finished state")
12
+ }
13
+
14
+ streamState.storedError = err
15
+ streamState.finishedPromise._reject(err)
16
+ streamState.readableState = "errored"
17
+
18
+ // Question: check err is instanceof Error ???
19
+ if (streamState.readableState === "waiting") {
20
+ streamState.readablePromise._reject(err)
21
+ } else if (streamState.readableState === "readable") {
22
+ streamState.buffer = []
23
+ streamState.readablePromise = Promise.rejected(err)
24
+ }
25
+ }
@@ -0,0 +1,20 @@
1
+ module.exports = finish
2
+
3
+ function finish(streamState) {
4
+ // NON STANDARD
5
+ if (streamState.readableState === "finished") {
6
+ throw new Error("cannot finish finished stream")
7
+ // NON STANDARD
8
+ } else if (streamState.readableState === "errored") {
9
+ throw new Error("cannot finish errored stream")
10
+ }
11
+
12
+ if (streamState.readableState === "waiting") {
13
+ streamState.readablePromise._reject(
14
+ new Error("stream has been completely read"))
15
+ streamState.finishedPromise._fulfill(undefined)
16
+ streamState.readableState = "finished"
17
+ } else if (streamState.readableState === "readable") {
18
+ streamState.draining = true
19
+ }
20
+ }
@@ -0,0 +1,23 @@
1
+ module.exports = push
2
+
3
+ function push(streamState, data) {
4
+ // NON STANDARD
5
+ if (streamState.readableState === "finished") {
6
+ throw new Error("cannot push into finished stream")
7
+ // NON STANDARD
8
+ } else if (streamState.readableState === "errored") {
9
+ throw new Error("cannot push into errored stream")
10
+ }
11
+
12
+ streamState.buffer.push(data)
13
+ streamState.pulling = false
14
+
15
+ if (streamState.readableState === "waiting") {
16
+ streamState.readablePromise._fulfill(undefined)
17
+ streamState.readableState = "readable"
18
+ }
19
+
20
+ // NON STANDARD
21
+ var needsMore = false
22
+ return needsMore
23
+ }
@@ -0,0 +1,34 @@
1
+ var assert = require("assert")
2
+ var Promise = require("bluebird")
3
+
4
+ var callPull = require("./call-pull.js");
5
+
6
+ module.exports = read
7
+
8
+ function read(streamState) {
9
+ if (streamState.readableState === "waiting") {
10
+ throw new Error("no data available yet")
11
+ } else if (streamState.readableState === "errored") {
12
+ throw streamState.storedError
13
+ } else if (streamState.readableState === "finished") {
14
+ throw new Error("stream has been completely read")
15
+ }
16
+
17
+ assert(streamState.buffer.length !== 0, "buffer is empty")
18
+ var data = streamState.buffer.shift()
19
+
20
+ if (streamState.buffer.length === 0) {
21
+ if (streamState.draining) {
22
+ streamState.finishedPromise._fulfill(undefined)
23
+ streamState.readablePromise =
24
+ Promise.rejected(new Error("stream has been completely read"))
25
+ streamState.readableState = "finished"
26
+ } else {
27
+ streamState.readableState = "waiting"
28
+ streamState.readablePromise = new Promise(function () {})
29
+ callPull(streamState)
30
+ }
31
+ }
32
+
33
+ return data
34
+ }
@@ -0,0 +1,17 @@
1
+ var Promise = require("bluebird")
2
+
3
+ module.exports = ReadableStreamState
4
+
5
+ function ReadableStreamState(options) {
6
+ this.buffer = []
7
+ this.started = false
8
+ this.draining = false
9
+ this.pulling = false
10
+ this.readableState = "waiting"
11
+ this.storedError = null
12
+ this.readablePromise = new Promise(function () {})
13
+ this.finishedPromise = new Promise(function () {})
14
+ this.startedPromise = new Promise(function () {})
15
+ this.onAbort = options.abort
16
+ this.onPull = options.pull
17
+ }
@@ -0,0 +1,11 @@
1
+ var callPull = require("./call-pull.js")
2
+
3
+ module.exports = waitForReadable
4
+
5
+ function waitForReadable(streamState) {
6
+ if (streamState.readableState === "waiting") {
7
+ callPull(streamState)
8
+ }
9
+
10
+ return streamState.readablePromise
11
+ }
@@ -0,0 +1,5 @@
1
+ module.exports = constructor
2
+
3
+ function constructor(streamState, start) {
4
+
5
+ }
@@ -0,0 +1,15 @@
1
+ var Promise = require("bluebird")
2
+
3
+ module.exports = WritableStreamState
4
+
5
+ function WritableStreamState(options) {
6
+ this.buffer = []
7
+ this.writableState = "waiting"
8
+ this.storedError = null
9
+ this.currentWritePromise = new Promise(function () {})
10
+ this.writablePromise = new Promise(function () {})
11
+ this.closedPromise = new Promise(function () {})
12
+ this.onWrite = options.write
13
+ this.onClose = options.close
14
+ this.onDispose = options.dispose || options.close
15
+ }