Skip to content

Commit e7fefe0

Browse files
committed
Initial commit
0 parents  commit e7fefe0

14 files changed

+256
-0
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/bower_components/
2+
/node_modules/
3+
/.pulp-cache/
4+
/output/
5+
/generated-docs/
6+
/.psc-package/
7+
/.psc*
8+
/.purs*
9+
/.psa*

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Awake Security
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# purescript-web-streams

bower.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "purescript-web-streams",
3+
"homepage": "https://github.com/purescript-web/purescript-web-streams",
4+
"license": "MIT",
5+
"repository": {
6+
"type": "git",
7+
"url": "git://github.com/purescript-web/purescript-web-streams.git"
8+
},
9+
"ignore": [
10+
"**/.*",
11+
"bower_components",
12+
"node_modules",
13+
"output",
14+
"bower.json",
15+
"package.json"
16+
],
17+
"dependencies": {
18+
"purescript-prelude": "^4.0.0",
19+
"purescript-effect": "^2.0.0",
20+
"purescript-nullable": "^4.0.0",
21+
"purescript-arraybuffer-types": "^2.0.0",
22+
"purescript-tuples": "^5.0.0",
23+
"purescript-web-promise": "https://github.com/purescript-web/purescript-web-promise.git#^1.0.0"
24+
}
25+
}

src/Web/Streams/QueuingStrategy.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
exports.new = function(options) {
2+
return function() {
3+
return new QueuingStrategy(options);
4+
};
5+
};
6+
7+
exports.byteLengthQueuingStrategy = function(options) {
8+
return function() {
9+
return new ByteLengthQueuingStrategy(options);
10+
};
11+
};
12+
13+
exports.countQueuingStrategy = function(options) {
14+
return function() {
15+
return new CountQueuingStrategy(options);
16+
};
17+
};

src/Web/Streams/QueuingStrategy.purs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Web.Streams.QueuingStrategy where
2+
3+
import Data.ArrayBuffer.Types (Uint8Array)
4+
import Effect (Effect)
5+
6+
foreign import data QueuingStrategy :: Type -> Type
7+
8+
foreign import new :: forall chunk. { size :: chunk -> Int, highWaterMark :: Int } -> Effect (QueuingStrategy chunk)
9+
10+
foreign import byteLengthQueuingStrategy :: { highWaterMark :: Int } -> Effect (QueuingStrategy Uint8Array)
11+
12+
foreign import countQueuingStrategy :: forall a. { highWaterMark :: Int } -> Effect (QueuingStrategy a)

src/Web/Streams/ReadableStream.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
exports._new = function(source, strategy) {
2+
return new ReadableStream(source, strategy);
3+
};
4+
5+
exports.cancel = function(stream) {
6+
return function() {
7+
return stream.cancel();
8+
};
9+
};
10+
11+
exports.locked = function(stream) {
12+
return function() {
13+
return stream.locked;
14+
};
15+
};
16+
17+
exports.getReader = function(stream) {
18+
return function() {
19+
return stream.getReader();
20+
};
21+
};
22+
23+
exports._tee = function(tuple, stream) {
24+
var r = stream.tee();
25+
return tuple(r[0])(r[1]);
26+
};

src/Web/Streams/ReadableStream.purs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module Web.Streams.ReadableStream
2+
( ReadableStream
3+
, new
4+
, cancel
5+
, locked
6+
, getReader
7+
, tee
8+
) where
9+
10+
import Data.Maybe (Maybe)
11+
import Data.Nullable (Nullable, toNullable)
12+
import Data.Tuple (Tuple(..))
13+
import Effect (Effect)
14+
import Effect.Uncurried (EffectFn2, runEffectFn2)
15+
import Prelude (Unit)
16+
import Web.Promise (Promise)
17+
import Web.Streams.QueuingStrategy (QueuingStrategy)
18+
import Web.Streams.Reader (Reader)
19+
import Web.Streams.Source (Source)
20+
21+
foreign import data ReadableStream :: Type -> Type
22+
23+
foreign import _new :: forall chunk. EffectFn2 (Source chunk) (Nullable (QueuingStrategy chunk)) (ReadableStream chunk)
24+
25+
foreign import cancel :: forall chunk. ReadableStream chunk -> Effect (Promise Unit)
26+
27+
foreign import locked :: forall chunk. ReadableStream chunk -> Effect Boolean
28+
29+
foreign import getReader :: forall chunk. ReadableStream chunk -> Effect (Reader chunk)
30+
31+
foreign import _tee :: forall chunk. EffectFn2 (forall a b. a -> b -> Tuple a b) (ReadableStream chunk) (Tuple (ReadableStream chunk) (ReadableStream chunk))
32+
33+
new :: forall chunk. Source chunk -> Maybe (QueuingStrategy chunk) -> Effect (ReadableStream chunk)
34+
new source strategy = runEffectFn2 _new source (toNullable strategy)
35+
36+
tee :: forall chunk. ReadableStream chunk -> Effect (Tuple (ReadableStream chunk) (ReadableStream chunk))
37+
tee = runEffectFn2 _tee Tuple
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
exports.enqueue = function(chunk) {
2+
return function(controller) {
3+
return function() {
4+
return controller.enqueue(chunk);
5+
};
6+
};
7+
};
8+
9+
exports.close = function(controller) {
10+
return function() {
11+
return controller.close();
12+
};
13+
};
14+
15+
exports.error = function(error) {
16+
return function(controller) {
17+
return function() {
18+
return controller.error(error);
19+
};
20+
};
21+
};
22+
23+
exports.desiredSize = function(controller) {
24+
return function() {
25+
return controller.desiredSize;
26+
};
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Web.Streams.ReadableStreamController where
2+
3+
import Effect (Effect)
4+
import Error (Error)
5+
import Prelude (Unit)
6+
7+
foreign import data ReadableStreamController :: Type -> Type
8+
9+
foreign import enqueue :: forall chunk. chunk -> ReadableStreamController chunk -> Effect Unit
10+
11+
foreign import close :: forall chunk. ReadableStreamController chunk -> Effect Unit
12+
13+
foreign import error :: forall chunk. Error -> ReadableStreamController chunk -> Effect Unit
14+
15+
foreign import desiredSize :: forall chunk. ReadableStreamController chunk -> Effect Int

src/Web/Streams/Reader.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
exports._read = function(nothing, just, reader) {
2+
return reader.read().then(function(res) {
3+
if (res.done) {
4+
return nothing;
5+
}
6+
return just(res.value);
7+
});
8+
};

src/Web/Streams/Reader.purs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Web.Streams.Reader where
2+
3+
import Data.Maybe (Maybe(..))
4+
import Effect (Effect)
5+
import Effect.Uncurried (EffectFn3, runEffectFn3)
6+
import Web.Promise (Promise)
7+
8+
foreign import data Reader :: Type -> Type
9+
10+
foreign import _read :: forall chunk. EffectFn3 (forall a. Maybe a) (forall a. a -> Maybe a) (Reader chunk) (Promise (Maybe chunk))
11+
12+
read :: forall chunk. Reader chunk -> Effect (Promise (Maybe chunk))
13+
read = runEffectFn3 _read Nothing Just

src/Web/Streams/Source.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
exports._make = function(options) {
2+
var newOptions = {
3+
start: function(controller) {
4+
return options.start(controller)();
5+
}
6+
};
7+
if (options.pull) {
8+
newOptions.pull = function(controller) {
9+
return options.pull(controller)();
10+
};
11+
}
12+
if (options.cancel) {
13+
newOptions.cancel = function() {
14+
return options.cancel();
15+
};
16+
}
17+
return newOptions;
18+
};

src/Web/Streams/Source.purs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module Web.Streams.Source
2+
( Source
3+
, make
4+
) where
5+
6+
import Effect (Effect)
7+
import Prelude (Unit)
8+
import Prim.Row as Row
9+
import Web.Promise (Promise)
10+
import Web.Streams.ReadableStreamController (ReadableStreamController)
11+
12+
foreign import data Source :: Type -> Type
13+
14+
type Required chunk r =
15+
( start :: ReadableStreamController chunk -> Effect (Promise Unit)
16+
| r
17+
)
18+
19+
type Optional chunk =
20+
( pull :: ReadableStreamController chunk -> Effect (Promise Unit)
21+
, cancel :: Effect (Promise Unit)
22+
)
23+
24+
foreign import _make :: forall r chunk. { | r } -> Source chunk
25+
26+
make :: forall r rx chunk. Row.Union r rx (Optional chunk) => { | Required chunk r } -> Source chunk
27+
make = _make

0 commit comments

Comments
 (0)