Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
natefaubion committed Jan 14, 2020
0 parents commit e7fefe0
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/bower_components/
/node_modules/
/.pulp-cache/
/output/
/generated-docs/
/.psc-package/
/.psc*
/.purs*
/.psa*
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Awake Security

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# purescript-web-streams
25 changes: 25 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "purescript-web-streams",
"homepage": "https://github.com/purescript-web/purescript-web-streams",
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/purescript-web/purescript-web-streams.git"
},
"ignore": [
"**/.*",
"bower_components",
"node_modules",
"output",
"bower.json",
"package.json"
],
"dependencies": {
"purescript-prelude": "^4.0.0",
"purescript-effect": "^2.0.0",
"purescript-nullable": "^4.0.0",
"purescript-arraybuffer-types": "^2.0.0",
"purescript-tuples": "^5.0.0",
"purescript-web-promise": "https://github.com/purescript-web/purescript-web-promise.git#^1.0.0"
}
}
17 changes: 17 additions & 0 deletions src/Web/Streams/QueuingStrategy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
exports.new = function(options) {
return function() {
return new QueuingStrategy(options);
};
};

exports.byteLengthQueuingStrategy = function(options) {
return function() {
return new ByteLengthQueuingStrategy(options);
};
};

exports.countQueuingStrategy = function(options) {
return function() {
return new CountQueuingStrategy(options);
};
};
12 changes: 12 additions & 0 deletions src/Web/Streams/QueuingStrategy.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Web.Streams.QueuingStrategy where

import Data.ArrayBuffer.Types (Uint8Array)
import Effect (Effect)

foreign import data QueuingStrategy :: Type -> Type

foreign import new :: forall chunk. { size :: chunk -> Int, highWaterMark :: Int } -> Effect (QueuingStrategy chunk)

foreign import byteLengthQueuingStrategy :: { highWaterMark :: Int } -> Effect (QueuingStrategy Uint8Array)

foreign import countQueuingStrategy :: forall a. { highWaterMark :: Int } -> Effect (QueuingStrategy a)
26 changes: 26 additions & 0 deletions src/Web/Streams/ReadableStream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
exports._new = function(source, strategy) {
return new ReadableStream(source, strategy);
};

exports.cancel = function(stream) {
return function() {
return stream.cancel();
};
};

exports.locked = function(stream) {
return function() {
return stream.locked;
};
};

exports.getReader = function(stream) {
return function() {
return stream.getReader();
};
};

exports._tee = function(tuple, stream) {
var r = stream.tee();
return tuple(r[0])(r[1]);
};
37 changes: 37 additions & 0 deletions src/Web/Streams/ReadableStream.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Web.Streams.ReadableStream
( ReadableStream
, new
, cancel
, locked
, getReader
, tee
) where

import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toNullable)
import Data.Tuple (Tuple(..))
import Effect (Effect)
import Effect.Uncurried (EffectFn2, runEffectFn2)
import Prelude (Unit)
import Web.Promise (Promise)
import Web.Streams.QueuingStrategy (QueuingStrategy)
import Web.Streams.Reader (Reader)
import Web.Streams.Source (Source)

foreign import data ReadableStream :: Type -> Type

foreign import _new :: forall chunk. EffectFn2 (Source chunk) (Nullable (QueuingStrategy chunk)) (ReadableStream chunk)

foreign import cancel :: forall chunk. ReadableStream chunk -> Effect (Promise Unit)

foreign import locked :: forall chunk. ReadableStream chunk -> Effect Boolean

foreign import getReader :: forall chunk. ReadableStream chunk -> Effect (Reader chunk)

foreign import _tee :: forall chunk. EffectFn2 (forall a b. a -> b -> Tuple a b) (ReadableStream chunk) (Tuple (ReadableStream chunk) (ReadableStream chunk))

new :: forall chunk. Source chunk -> Maybe (QueuingStrategy chunk) -> Effect (ReadableStream chunk)
new source strategy = runEffectFn2 _new source (toNullable strategy)

tee :: forall chunk. ReadableStream chunk -> Effect (Tuple (ReadableStream chunk) (ReadableStream chunk))
tee = runEffectFn2 _tee Tuple
27 changes: 27 additions & 0 deletions src/Web/Streams/ReadableStreamController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
exports.enqueue = function(chunk) {
return function(controller) {
return function() {
return controller.enqueue(chunk);
};
};
};

exports.close = function(controller) {
return function() {
return controller.close();
};
};

exports.error = function(error) {
return function(controller) {
return function() {
return controller.error(error);
};
};
};

exports.desiredSize = function(controller) {
return function() {
return controller.desiredSize;
};
};
15 changes: 15 additions & 0 deletions src/Web/Streams/ReadableStreamController.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Web.Streams.ReadableStreamController where

import Effect (Effect)
import Error (Error)
import Prelude (Unit)

foreign import data ReadableStreamController :: Type -> Type

foreign import enqueue :: forall chunk. chunk -> ReadableStreamController chunk -> Effect Unit

foreign import close :: forall chunk. ReadableStreamController chunk -> Effect Unit

foreign import error :: forall chunk. Error -> ReadableStreamController chunk -> Effect Unit

foreign import desiredSize :: forall chunk. ReadableStreamController chunk -> Effect Int
8 changes: 8 additions & 0 deletions src/Web/Streams/Reader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exports._read = function(nothing, just, reader) {
return reader.read().then(function(res) {
if (res.done) {
return nothing;
}
return just(res.value);
});
};
13 changes: 13 additions & 0 deletions src/Web/Streams/Reader.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Web.Streams.Reader where

import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Uncurried (EffectFn3, runEffectFn3)
import Web.Promise (Promise)

foreign import data Reader :: Type -> Type

foreign import _read :: forall chunk. EffectFn3 (forall a. Maybe a) (forall a. a -> Maybe a) (Reader chunk) (Promise (Maybe chunk))

read :: forall chunk. Reader chunk -> Effect (Promise (Maybe chunk))
read = runEffectFn3 _read Nothing Just
18 changes: 18 additions & 0 deletions src/Web/Streams/Source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
exports._make = function(options) {
var newOptions = {
start: function(controller) {
return options.start(controller)();
}
};
if (options.pull) {
newOptions.pull = function(controller) {
return options.pull(controller)();
};
}
if (options.cancel) {
newOptions.cancel = function() {
return options.cancel();
};
}
return newOptions;
};
27 changes: 27 additions & 0 deletions src/Web/Streams/Source.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Web.Streams.Source
( Source
, make
) where

import Effect (Effect)
import Prelude (Unit)
import Prim.Row as Row
import Web.Promise (Promise)
import Web.Streams.ReadableStreamController (ReadableStreamController)

foreign import data Source :: Type -> Type

type Required chunk r =
( start :: ReadableStreamController chunk -> Effect (Promise Unit)
| r
)

type Optional chunk =
( pull :: ReadableStreamController chunk -> Effect (Promise Unit)
, cancel :: Effect (Promise Unit)
)

foreign import _make :: forall r chunk. { | r } -> Source chunk

make :: forall r rx chunk. Row.Union r rx (Optional chunk) => { | Required chunk r } -> Source chunk
make = _make

0 comments on commit e7fefe0

Please sign in to comment.