Skip to content
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": "^3.0.0",
"purescript-assert": "^3.0.0",
"purescript-partial": "^1.2.0"
"purescript-console": "#compiler/0.12",
"purescript-assert": "#compiler/0.12",
"purescript-partial": "#compiler/0.12"
},
"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": "#compiler/0.12",
"purescript-node-buffer": "justinwoo/purescript-node-buffer#compiler/0.12",
"purescript-prelude": "#compiler/0.12",
"purescript-either": "#compiler/0.12",
"purescript-exceptions": "#compiler/0.12"
}
}
5 changes: 2 additions & 3 deletions example/Gzip.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import Prelude

import Node.Stream

import Control.Monad.Eff
import Control.Monad.Eff.Console
import Effect
import Effect.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)
Expand Down
207 changes: 103 additions & 104 deletions src/Node/Stream.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -180,146 +179,146 @@ 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
-- | library, which already expects a default encoding to be set. It has no
-- | 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
Loading