This repository has been archived by the owner on Aug 4, 2023. It is now read-only.
/
Internal.purs
130 lines (116 loc) · 3.5 KB
/
Internal.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
-- | Maybe the stuff in here should be moved into the
-- | [__Node.Stream__](https://pursuit.purescript.org/packages/purescript-node-streams/docs/Node.Stream)
-- | module?
module Node.Stream.Aff.Internal
( onceDrain
, onceEnd
, onceClose
, onceError
, onceReadable
, readable
, writable
, push
, newReadable
, newReadableStringUTF8
, newStreamPassThrough
) where
import Prelude
import Data.Nullable (Nullable, notNull, null)
import Effect (Effect)
import Effect.Class (class MonadEffect, liftEffect)
import Effect.Exception (Error)
import Node.Buffer (Buffer)
import Node.Buffer as Buffer
import Node.Encoding as Encoding
import Node.Stream (Readable, Stream, Writable, Duplex)
-- | Listen for one `readable` event, call the callback, then remove
-- | the event listener.
-- |
-- | Returns an effect for removing the event listener before the event
-- | is raised.
foreign import onceReadable
:: forall r
. Readable r
-> Effect Unit
-> Effect (Effect Unit)
-- | Listen for one `end` event, call the callback, then remove
-- | the event listener.
-- |
-- | Returns an effect for removing the event listener before the event
-- | is raised.
foreign import onceEnd
:: forall r
. Readable r
-> Effect Unit
-> Effect (Effect Unit)
-- | Listen for one `close` event, call the callback, then remove
-- | the event listener.
-- |
-- | Returns an effect for removing the event listener before the event
-- | is raised.
foreign import onceClose
:: forall s
. Stream s
-> Effect Unit
-> Effect (Effect Unit)
-- | Listen for one `drain` event, call the callback, then remove
-- | the event listener.
-- |
-- | Returns an effect for removing the event listener before the event
-- | is raised.
foreign import onceDrain
:: forall w
. Writable w
-> Effect Unit
-> Effect (Effect Unit)
-- | Listen for one `error` event, call the callback, then remove
-- | the event listener.
-- |
-- | Returns an effect for removing the event listener before the event
-- | is raised.
foreign import onceError
:: forall r
. Stream r
-> (Error -> Effect Unit)
-> Effect (Effect Unit)
-- | The [`readable.readable`](https://nodejs.org/api/stream.html#readablereadable)
-- | property of a stream.
-- |
-- | > Is true if it is safe to call `readable.read()`, which means the stream
-- | > has not been destroyed or emitted `'error'` or `'end'`.
foreign import readable
:: forall r
. Readable r
-> Effect Boolean
foreign import writable
:: forall w
. Writable w
-> Effect Boolean
-- | [`readable.push(chunk[, encoding])`](https://nodejs.org/api/stream.html#readablepushchunk-encoding)
foreign import push
:: forall r
. Readable r
-> Nullable Buffer
-> Effect Boolean
-- | `new stream.Readable()`
foreign import newReadable
:: forall r
. Effect (Readable r)
-- | Construct a UTF-8 `Readable` from a `String`.
newReadableStringUTF8
:: forall r m
. MonadEffect m
=> String
-> m (Readable r)
newReadableStringUTF8 strng = liftEffect do
rstream <- newReadable
_ <- push rstream =<< (notNull <$> Buffer.fromString strng Encoding.UTF8)
_ <- push rstream null -- the end of the stream
pure rstream
-- | “A trivial implementation of a `Transform` stream that simply passes the
-- | input bytes across to the output.”
-- |
-- | [__Class: `stream.PassThrough`__](https://nodejs.org/api/stream.html#class-streampassthrough)
newStreamPassThrough :: forall m. MonadEffect m => m Duplex
newStreamPassThrough = liftEffect newStreamPassThroughImpl
foreign import newStreamPassThroughImpl :: Effect Duplex