diff --git a/bower.json b/bower.json index a3a68f6..b6ffa51 100644 --- a/bower.json +++ b/bower.json @@ -12,15 +12,15 @@ "url": "git://github.com/purescript-node/purescript-node-streams.git" }, "devDependencies": { - "purescript-console": "^3.0.0", - "purescript-assert": "^3.0.0", - "purescript-partial": "^1.2.0" + "purescript-console": "^4.1.0", + "purescript-assert": "^4.0.0", + "purescript-partial": "^2.0.0" }, "dependencies": { - "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" + "purescript-effect": "^2.0.0", + "purescript-node-buffer": "^5.0.0", + "purescript-prelude": "^4.0.0", + "purescript-either": "^4.0.0", + "purescript-exceptions": "^4.0.0" } } diff --git a/example/Gzip.purs b/example/Gzip.purs index edd8784..e4e52f6 100644 --- a/example/Gzip.purs +++ b/example/Gzip.purs @@ -2,17 +2,15 @@ module Gzip where import Prelude -import Node.Stream +import Effect (Effect) +import Node.Stream (Duplex, Readable, Writable, pipe) -import Control.Monad.Eff -import Control.Monad.Eff.Console -foreign import data GZIP :: Effect - -foreign import gzip :: forall eff. Eff (gzip :: GZIP | eff) (Duplex (gzip :: GZIP | eff)) -foreign import stdin :: forall eff. Readable () (console :: CONSOLE | eff) -foreign import stdout :: forall eff. Writable () (console :: CONSOLE | eff) +foreign import gzip :: Effect Duplex +foreign import stdin :: Readable () +foreign import stdout :: Writable () +main :: Effect (Writable ()) main = do z <- gzip _ <- stdin `pipe` z diff --git a/package.json b/package.json index d6f87cb..9d9e694 100644 --- a/package.json +++ b/package.json @@ -7,10 +7,10 @@ }, "devDependencies": { "jscs": "^3.0.7", - "jshint": "^2.9.4", - "pulp": "^11.0.0", - "purescript-psa": "^0.5.0", - "rimraf": "^2.6.1", - "stream-buffers": "^3.0.1" + "jshint": "^2.9.5", + "pulp": "^12.2.0", + "purescript-psa": "^0.6.0", + "rimraf": "^2.6.2", + "stream-buffers": "^3.0.2" } } diff --git a/src/Node/Stream.purs b/src/Node/Stream.purs index 6c92fd8..2483a3b 100644 --- a/src/Node/Stream.purs +++ b/src/Node/Stream.purs @@ -36,9 +36,8 @@ module Node.Stream import Prelude -import Control.Monad.Eff (Eff, kind Effect) -import Control.Monad.Eff.Exception (throw, EXCEPTION(), Error()) -import Control.Monad.Eff.Unsafe (unsafeCoerceEff) +import Effect (Effect) +import Effect.Exception (throw, Error()) import Data.Either (Either(..)) import Data.Maybe (Maybe(..), fromMaybe) import Node.Buffer (Buffer()) @@ -51,7 +50,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 :: # Type -> # Effect -> Type +foreign import data Stream :: # Type -> Type -- | A phantom type associated with _readable streams_. data Read @@ -84,10 +83,10 @@ readChunk = readChunkImpl Left Right -- | Listen for `data` events, returning data in a Buffer. Note that this will fail -- | if `setEncoding` has been called on the stream. onData - :: forall w eff - . Readable w (exception :: EXCEPTION | eff) - -> (Buffer -> Eff (exception :: EXCEPTION | eff) Unit) - -> Eff (exception :: EXCEPTION | eff) Unit + :: forall w + . Readable w + -> (Buffer -> Effect Unit) + -> Effect Unit onData r cb = onDataEither r (cb <=< fromEither) where @@ -99,10 +98,10 @@ onData r cb = pure buf read - :: forall w eff - . Readable w (exception :: EXCEPTION | eff) + :: forall w + . Readable w -> Maybe Int - -> Eff (exception :: EXCEPTION | eff) (Maybe Buffer) + -> Effect (Maybe Buffer) read r size = do v <- readEither r size case v of @@ -111,67 +110,67 @@ read r size = do Just (Right b) -> pure (Just b) readString - :: forall w eff - . Readable w (exception :: EXCEPTION | eff) + :: forall w + . Readable w -> Maybe Int -> Encoding - -> Eff (exception :: EXCEPTION | eff) (Maybe String) + -> Effect (Maybe String) readString r size enc = do v <- readEither r size case v of Nothing -> pure Nothing Just (Left _) -> throw "Stream encoding should not be set" - Just (Right buf) -> Just <$> (unsafeCoerceEff $ Buffer.toString enc buf) + Just (Right buf) -> Just <$> Buffer.toString enc buf readEither - :: forall w eff - . Readable w eff + :: forall w + . Readable w -> Maybe Int - -> Eff eff (Maybe (Either String Buffer)) + -> Effect (Maybe (Either String Buffer)) readEither r size = readImpl readChunk Nothing Just r (fromMaybe undefined size) foreign import readImpl - :: forall r eff + :: forall r . (Chunk -> Either String Buffer) -> (forall a. Maybe a) -> (forall a. a -> Maybe a) - -> Readable r eff + -> Readable r -> Int - -> Eff eff (Maybe (Either String Buffer)) + -> Effect (Maybe (Either String Buffer)) -- | Listen for `data` events, returning data in a String, which will be -- | decoded using the given encoding. Note that this will fail if `setEncoding` -- | has been called on the stream. onDataString - :: forall w eff - . Readable w (exception :: EXCEPTION | eff) + :: forall w + . Readable w -> Encoding - -> (String -> Eff (exception :: EXCEPTION | eff) Unit) - -> Eff (exception :: EXCEPTION | eff) Unit -onDataString r enc cb = onData r (cb <=< unsafeCoerceEff <<< Buffer.toString enc) + -> (String -> Effect Unit) + -> Effect Unit +onDataString r enc cb = onData r (cb <=< 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 (exception :: EXCEPTION | eff) - -> (Either String Buffer -> Eff (exception :: EXCEPTION | eff) Unit) - -> Eff (exception :: EXCEPTION | eff) Unit + :: forall r + . Readable r + -> (Either String Buffer -> Effect Unit) + -> Effect Unit onDataEither r cb = onDataEitherImpl readChunk r cb foreign import onDataEitherImpl - :: forall r eff + :: forall r . (Chunk -> Either String Buffer) - -> Readable r eff - -> (Either String Buffer -> Eff eff Unit) - -> Eff eff Unit + -> Readable r + -> (Either String Buffer -> Effect Unit) + -> Effect Unit foreign import setEncodingImpl - :: forall w eff - . Readable w eff + :: forall w + . Readable w -> String - -> Eff eff Unit + -> Effect Unit -- | Set the encoding used to read chunks as strings from the stream. This -- | function may be useful when you are passing a readable stream to some other @@ -180,113 +179,113 @@ foreign import setEncodingImpl -- | Where possible, you should try to use `onDataString` instead of this -- | function. setEncoding - :: forall w eff - . Readable w eff + :: forall w + . Readable w -> Encoding - -> Eff eff Unit + -> Effect Unit setEncoding r enc = setEncodingImpl r (show enc) -- | Listen for `readable` events. foreign import onReadable - :: forall w eff - . Readable w eff - -> Eff eff Unit - -> Eff eff Unit + :: forall w + . Readable w + -> Effect Unit + -> Effect Unit -- | Listen for `end` events. foreign import onEnd - :: forall w eff - . Readable w eff - -> Eff eff Unit - -> Eff eff Unit + :: forall w + . Readable w + -> Effect Unit + -> Effect Unit -- | Listen for `finish` events. foreign import onFinish - :: forall w eff - . Writable w eff - -> Eff eff Unit - -> Eff eff Unit + :: forall w + . Writable w + -> Effect Unit + -> Effect Unit -- | Listen for `close` events. foreign import onClose - :: forall w eff - . Stream w eff - -> Eff eff Unit - -> Eff eff Unit + :: forall w + . Stream w + -> Effect Unit + -> Effect Unit -- | Listen for `error` events. foreign import onError - :: forall w eff - . Stream w eff - -> (Error -> Eff eff Unit) - -> Eff eff Unit + :: forall w + . Stream w + -> (Error -> Effect Unit) + -> Effect Unit -- | Resume reading from the stream. -foreign import resume :: forall w eff. Readable w eff -> Eff eff Unit +foreign import resume :: forall w. Readable w -> Effect Unit -- | Pause reading from the stream. -foreign import pause :: forall w eff. Readable w eff -> Eff eff Unit +foreign import pause :: forall w. Readable w -> Effect Unit -- | Check whether or not a stream is paused for reading. -foreign import isPaused :: forall w eff. Readable w eff -> Eff eff Boolean +foreign import isPaused :: forall w. Readable w -> Effect Boolean -- | Read chunks from a readable stream and write them to a writable stream. foreign import pipe - :: forall r w eff - . Readable w eff - -> Writable r eff - -> Eff eff (Writable r eff) + :: forall r w + . Readable w + -> Writable r + -> Effect (Writable r) -- | Detach a Writable stream previously attached using `pipe`. foreign import unpipe - :: forall r w eff - . Readable w eff - -> Writable r eff - -> Eff eff Unit + :: forall r w + . Readable w + -> Writable r + -> Effect Unit -- | Detach all Writable streams previously attached using `pipe`. foreign import unpipeAll - :: forall w eff - . Readable w eff - -> Eff eff Unit + :: forall w + . Readable w + -> Effect Unit -- | Write a Buffer to a writable stream. foreign import write - :: forall r eff - . Writable r eff + :: forall r + . Writable r -> Buffer - -> Eff eff Unit - -> Eff eff Boolean + -> Effect Unit + -> Effect Boolean foreign import writeStringImpl - :: forall r eff - . Writable r eff + :: forall r + . Writable r -> String -> String - -> Eff eff Unit - -> Eff eff Boolean + -> Effect Unit + -> Effect Boolean -- | Write a string in the specified encoding to a writable stream. writeString - :: forall r eff - . Writable r eff + :: forall r + . Writable r -> Encoding -> String - -> Eff eff Unit - -> Eff eff Boolean + -> Effect Unit + -> Effect Boolean writeString w enc = writeStringImpl w (show enc) -- | Force buffering of writes. -foreign import cork :: forall r eff. Writable r eff -> Eff eff Unit +foreign import cork :: forall r. Writable r -> Effect Unit -- | Flush buffered data. -foreign import uncork :: forall r eff. Writable r eff -> Eff eff Unit +foreign import uncork :: forall r. Writable r -> Effect Unit foreign import setDefaultEncodingImpl - :: forall r eff - . Writable r eff + :: forall r + . Writable r -> String - -> Eff eff Unit + -> Effect Unit -- | Set the default encoding used to write strings to the stream. This function -- | is useful when you are passing a writable stream to some other JavaScript @@ -294,32 +293,32 @@ foreign import setDefaultEncodingImpl -- | effect on the behaviour of the `writeString` function (because that -- | function ensures that the encoding is always supplied explicitly). setDefaultEncoding - :: forall r eff - . Writable r eff + :: forall r + . Writable r -> Encoding - -> Eff eff Unit + -> Effect Unit setDefaultEncoding r enc = setDefaultEncodingImpl r (show enc) -- | End writing data to the stream. foreign import end - :: forall r eff - . Writable r eff - -> Eff eff Unit - -> Eff eff Unit + :: forall r + . Writable r + -> Effect Unit + -> Effect Unit -- | Destroy the stream. It will release any internal resources. -- -- Added in node 8.0. foreign import destroy - :: forall r eff - . Stream r eff - -> Eff eff Unit + :: forall r + . Stream r + -> Effect Unit -- | Destroy the stream and emit 'error'. -- -- Added in node 8.0. foreign import destroyWithError - :: forall r eff - . Stream r eff + :: forall r + . Stream r -> Error - -> Eff eff Unit + -> Effect Unit diff --git a/test/Main.purs b/test/Main.purs index dcb9f1d..75d219e 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -1,44 +1,34 @@ module Test.Main where import Prelude -import Control.Monad.Eff (Eff, kind Effect) -import Control.Monad.Eff.Console (CONSOLE, log) -import Control.Monad.Eff.Exception (EXCEPTION) -import Node.Encoding (Encoding(..)) -import Node.Stream (Duplex, Readable, Writable, onDataString, end, writeString, pipe, onDataEither, onData, setEncoding, setDefaultEncoding, read, onReadable, readString) -import Test.Assert (ASSERT, assert, assert') -import Node.Buffer as Buffer + import Data.Either (Either(..)) import Data.Maybe (Maybe(..), fromJust, isNothing, isJust) +import Effect (Effect) +import Effect.Console (log) +import Node.Buffer as Buffer +import Node.Encoding (Encoding(..)) +import Node.Stream (Duplex, Readable, Writable, onDataString, end, writeString, pipe, onDataEither, onData, setEncoding, setDefaultEncoding, read, onReadable, readString) import Partial.Unsafe (unsafePartial) +import Test.Assert (assert, assert') -assertEqual :: forall e a. Show a => Eq a => a -> a -> Eff (assert :: ASSERT | e) Unit +assertEqual :: forall a. Show a => Eq a => a -> a -> Effect Unit assertEqual x y = assert' (show x <> " did not equal " <> show y) (x == y) -foreign import data STREAM_BUFFER :: Effect -foreign import writableStreamBuffer :: forall eff. Eff (sb :: STREAM_BUFFER | eff) (Writable () (sb :: STREAM_BUFFER | eff)) +foreign import writableStreamBuffer :: Effect (Writable ()) -foreign import getContentsAsString :: forall r eff. Writable r (sb :: STREAM_BUFFER | eff) -> Eff (sb :: STREAM_BUFFER | eff) String +foreign import getContentsAsString :: forall r. Writable r -> Effect String -foreign import readableStreamBuffer :: forall eff. Eff (sb :: STREAM_BUFFER | eff) (Readable () (sb :: STREAM_BUFFER | eff)) +foreign import readableStreamBuffer :: Effect (Readable ()) -foreign import putImpl :: forall r eff. String -> String -> Readable r (sb :: STREAM_BUFFER | eff) -> Eff (sb :: STREAM_BUFFER | eff) Unit +foreign import putImpl :: forall r. String -> String -> Readable r -> Effect Unit -put :: forall r eff. String -> Encoding -> Readable r (sb :: STREAM_BUFFER | eff) -> Eff (sb :: STREAM_BUFFER | eff) Unit +put :: forall r. String -> Encoding -> Readable r -> Effect Unit put str enc = putImpl str (show enc) -main - :: forall eff - . Eff ( console :: CONSOLE - , sb :: STREAM_BUFFER - , assert :: ASSERT - , exception :: EXCEPTION - , buffer :: Buffer.BUFFER - , stream :: PASS_THROUGH - , gzip :: GZIP - | eff ) Boolean +main :: Effect Boolean main = do log "setDefaultEncoding should not affect writing" _ <- testSetDefaultEncoding @@ -55,13 +45,7 @@ main = do testString :: String testString = "üöß💡" -testReads - :: forall eff - . Eff ( stream :: PASS_THROUGH - , exception :: EXCEPTION - , buffer :: Buffer.BUFFER - , assert :: ASSERT - | eff ) Boolean +testReads :: Effect Boolean testReads = do _ <- testReadString testReadBuf @@ -96,11 +80,7 @@ testReads = do writeString sIn UTF8 testString do pure unit -testSetDefaultEncoding - :: forall eff - . Eff ( sb :: STREAM_BUFFER - , assert :: ASSERT - | eff ) Boolean +testSetDefaultEncoding :: Effect Boolean testSetDefaultEncoding = do w1 <- writableStreamBuffer _ <- check w1 @@ -115,13 +95,7 @@ testSetDefaultEncoding = do c <- getContentsAsString w assertEqual testString c -testSetEncoding - :: forall eff - . Eff ( sb :: STREAM_BUFFER - , exception :: EXCEPTION - , buffer :: Buffer.BUFFER - , assert :: ASSERT - | eff ) Unit +testSetEncoding :: Effect Unit testSetEncoding = do check UTF8 check UTF16LE @@ -140,14 +114,7 @@ testSetEncoding = do _ <- assertEqual <$> Buffer.toString enc buf <*> pure testString assertEqual str testString -testPipe - :: forall eff - . Eff ( stream :: PASS_THROUGH - , gzip :: GZIP - , exception :: EXCEPTION - , assert :: ASSERT - , console :: CONSOLE - | eff ) Boolean +testPipe :: Effect Boolean testPipe = do sIn <- passThrough sOut <- passThrough @@ -166,12 +133,10 @@ testPipe = do onDataString sOut UTF8 \str -> do assertEqual str testString -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 createGzip :: Effect Duplex +foreign import createGunzip :: Effect Duplex -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)) +foreign import passThrough :: Effect Duplex