Skip to content

update for purescript 0.11 #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
"url": "git://github.com/purescript-node/purescript-node-streams.git"
},
"devDependencies": {
"purescript-console": "^2.0.0",
"purescript-assert": "^2.0.0",
"purescript-partial": "^1.1.2"
"purescript-console": "^3.0.0",
"purescript-assert": "^3.0.0",
"purescript-partial": "^1.2.0"
},
"dependencies": {
"purescript-eff": "^2.0.0",
"purescript-node-buffer": "^2.0.0",
"purescript-prelude": "^2.1.0",
"purescript-either": "^2.0.0",
"purescript-exceptions": "^2.0.0"
"purescript-eff": "^3.0.0",
"purescript-node-buffer": "^3.0.0",
"purescript-prelude": "^3.0.0",
"purescript-either": "^3.0.0",
"purescript-exceptions": "^3.0.0"
}
}
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"private": true,
"scripts": {
"clean": "rimraf output && rimraf .pulp-cache",
"build": "jshint src && jscs src && pulp build --censor-lib --strict",
"build": "jshint src && jscs src && pulp build -- --censor-lib --strict",
"test": "pulp test"
},
"devDependencies": {
"jscs": "^3.0.7",
"jshint": "^2.9.3",
"pulp": "^9.0.1",
"purescript-psa": "^0.3.9",
"rimraf": "^2.5.4",
"jshint": "^2.9.4",
"pulp": "^11.0.0",
"purescript-psa": "^0.5.0",
"rimraf": "^2.6.1",
"stream-buffers": "^3.0.1"
}
}
32 changes: 16 additions & 16 deletions src/Node/Stream.purs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Node.Stream

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff (Eff, kind Effect)
import Control.Monad.Eff.Exception (throw, EXCEPTION(), Error())
import Control.Monad.Eff.Unsafe (unsafeCoerceEff)
import Data.Either (Either(..))
Expand All @@ -47,7 +47,7 @@ import Node.Encoding (Encoding)
-- |
-- | - Whether reading and/or writing from/to the stream are allowed.
-- | - Effects associated with reading/writing from/to this stream.
foreign import data Stream :: # * -> # ! -> *
foreign import data Stream :: # Type -> # Effect -> Type

-- | A phantom type associated with _readable streams_.
data Read
Expand All @@ -66,7 +66,7 @@ type Duplex = Stream (read :: Read, write :: Write)

foreign import undefined :: forall a. a

foreign import data Chunk :: *
foreign import data Chunk :: Type

foreign import readChunkImpl
:: (forall l r. l -> Either l r)
Expand All @@ -81,9 +81,9 @@ readChunk = readChunkImpl Left Right
-- | if `setEncoding` has been called on the stream.
onData
:: forall w eff
. Readable w (err :: EXCEPTION | eff)
-> (Buffer -> Eff (err :: EXCEPTION | eff) Unit)
-> Eff (err :: EXCEPTION | eff) Unit
. Readable w (exception :: EXCEPTION | eff)
-> (Buffer -> Eff (exception :: EXCEPTION | eff) Unit)
-> Eff (exception :: EXCEPTION | eff) Unit
onData r cb =
onDataEither r (cb <=< fromEither)
where
Expand All @@ -96,9 +96,9 @@ onData r cb =

read
:: forall w eff
. Readable w (err :: EXCEPTION | eff)
. Readable w (exception :: EXCEPTION | eff)
-> Maybe Int
-> Eff (err :: EXCEPTION | eff) (Maybe Buffer)
-> Eff (exception :: EXCEPTION | eff) (Maybe Buffer)
read r size = do
v <- readEither r size
case v of
Expand All @@ -108,10 +108,10 @@ read r size = do

readString
:: forall w eff
. Readable w (err :: EXCEPTION | eff)
. Readable w (exception :: EXCEPTION | eff)
-> Maybe Int
-> Encoding
-> Eff (err :: EXCEPTION | eff) (Maybe String)
-> Eff (exception :: EXCEPTION | eff) (Maybe String)
readString r size enc = do
v <- readEither r size
case v of
Expand Down Expand Up @@ -140,20 +140,20 @@ foreign import readImpl
-- | has been called on the stream.
onDataString
:: forall w eff
. Readable w (err :: EXCEPTION | eff)
. Readable w (exception :: EXCEPTION | eff)
-> Encoding
-> (String -> Eff (err :: EXCEPTION | eff) Unit)
-> Eff (err :: EXCEPTION | eff) Unit
-> (String -> Eff (exception :: EXCEPTION | eff) Unit)
-> Eff (exception :: EXCEPTION | eff) Unit
onDataString r enc cb = onData r (cb <=< unsafeCoerceEff <<< Buffer.toString enc)

-- | Listen for `data` events, returning data in an `Either String Buffer`. This
-- | function is provided for the (hopefully rare) case that `setEncoding` has
-- | been called on the stream.
onDataEither
:: forall r eff
. Readable r (err :: EXCEPTION | eff)
-> (Either String Buffer -> Eff (err :: EXCEPTION | eff) Unit)
-> Eff (err :: EXCEPTION | eff) Unit
. Readable r (exception :: EXCEPTION | eff)
-> (Either String Buffer -> Eff (exception :: EXCEPTION | eff) Unit)
-> Eff (exception :: EXCEPTION | eff) Unit
onDataEither r cb = onDataEitherImpl readChunk r cb

foreign import onDataEitherImpl
Expand Down
36 changes: 18 additions & 18 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Test.Main where

import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff (Eff, kind Effect)
import Control.Monad.Eff.Console (CONSOLE, log)
import Control.Monad.Eff.Exception (EXCEPTION)
import Node.Encoding (Encoding(..))
Expand All @@ -12,11 +12,11 @@ import Data.Either (Either(..))
import Data.Maybe (Maybe(..), fromJust, isNothing, isJust)
import Partial.Unsafe (unsafePartial)

assertEqual :: forall e a. (Show a, Eq a) => a -> a -> Eff (assert :: ASSERT | e) Unit
assertEqual :: forall e a. Show a => Eq a => a -> a -> Eff (assert :: ASSERT | e) Unit
assertEqual x y =
assert' (show x <> " did not equal " <> show y) (x == y)

foreign import data STREAM_BUFFER :: !
foreign import data STREAM_BUFFER :: Effect

foreign import writableStreamBuffer :: forall eff. Eff (sb :: STREAM_BUFFER | eff) (Writable () (sb :: STREAM_BUFFER | eff))

Expand All @@ -34,20 +34,20 @@ main
. Eff ( console :: CONSOLE
, sb :: STREAM_BUFFER
, assert :: ASSERT
, err :: EXCEPTION
, exception :: EXCEPTION
, buffer :: Buffer.BUFFER
, stream :: PASS_THROUGH
, gzip :: GZIP
| eff ) Boolean
main = do
log "setDefaultEncoding should not affect writing"
testSetDefaultEncoding
_ <- testSetDefaultEncoding

log "setEncoding should not affect reading"
testSetEncoding

log "test pipe"
testPipe
_ <- testPipe

log "test manual reads"
testReads
Expand All @@ -58,12 +58,12 @@ testString = "üöß💡"
testReads
:: forall eff
. Eff ( stream :: PASS_THROUGH
, err :: EXCEPTION
, exception :: EXCEPTION
, buffer :: Buffer.BUFFER
, assert :: ASSERT
| eff ) Boolean
testReads = do
testReadString
_ <- testReadString
testReadBuf

where
Expand All @@ -89,7 +89,7 @@ testReads = do
onReadable sIn do
buf <- read sIn Nothing
assert (isJust buf)
assertEqual <$> (Buffer.toString UTF8 (unsafePartial (fromJust buf)))
_ <- assertEqual <$> (Buffer.toString UTF8 (unsafePartial (fromJust buf)))
<*> pure testString
pure unit

Expand All @@ -103,7 +103,7 @@ testSetDefaultEncoding
| eff ) Boolean
testSetDefaultEncoding = do
w1 <- writableStreamBuffer
check w1
_ <- check w1

w2 <- writableStreamBuffer
setDefaultEncoding w2 UCS2
Expand All @@ -118,7 +118,7 @@ testSetDefaultEncoding = do
testSetEncoding
:: forall eff
. Eff ( sb :: STREAM_BUFFER
, err :: EXCEPTION
, exception :: EXCEPTION
, buffer :: Buffer.BUFFER
, assert :: ASSERT
| eff ) Unit
Expand All @@ -137,14 +137,14 @@ testSetEncoding = do

onData r1 \buf -> unsafePartial do
onDataEither r2 \(Left str) -> do
assertEqual <$> Buffer.toString enc buf <*> pure testString
_ <- assertEqual <$> Buffer.toString enc buf <*> pure testString
assertEqual str testString

testPipe
:: forall eff
. Eff ( stream :: PASS_THROUGH
, gzip :: GZIP
, err :: EXCEPTION
, exception :: EXCEPTION
, assert :: ASSERT
, console :: CONSOLE
| eff ) Boolean
Expand All @@ -155,23 +155,23 @@ testPipe = do
unzip <- createGunzip

log "pipe 1"
sIn `pipe` zip
_ <- sIn `pipe` zip
log "pipe 2"
zip `pipe` unzip
_ <- zip `pipe` unzip
log "pipe 3"
unzip `pipe` sOut
_ <- unzip `pipe` sOut

writeString sIn UTF8 testString do
end sIn do
onDataString sOut UTF8 \str -> do
assertEqual str testString

foreign import data GZIP :: !
foreign import data GZIP :: Effect

foreign import createGzip :: forall eff. Eff (gzip :: GZIP | eff) (Duplex (gzip :: GZIP | eff))
foreign import createGunzip :: forall eff. Eff (gzip :: GZIP | eff) (Duplex (gzip :: GZIP | eff))

foreign import data PASS_THROUGH :: !
foreign import data PASS_THROUGH :: Effect

-- | Create a PassThrough stream, which simply writes its input to its output.
foreign import passThrough :: forall eff. Eff (stream :: PASS_THROUGH | eff) (Duplex (stream :: PASS_THROUGH | eff))