|
1 | 1 | module Node.FS.Stream |
2 | 2 | ( createWriteStream |
3 | | - , fdCreateWriteStream |
4 | 3 | , WriteStreamOptions |
5 | | - , defaultWriteStreamOptions |
6 | | - , createWriteStreamWith |
7 | | - , fdCreateWriteStreamWith |
| 4 | + , createWriteStream' |
| 5 | + , fdCreateWriteStream |
| 6 | + , fdCreateWriteStream' |
8 | 7 | , createReadStream |
9 | | - , fdCreateReadStream |
10 | 8 | , ReadStreamOptions |
11 | | - , defaultReadStreamOptions |
12 | | - , createReadStreamWith |
13 | | - , fdCreateReadStreamWith |
| 9 | + , createReadStream' |
| 10 | + , fdCreateReadStream |
| 11 | + , fdCreateReadStream' |
14 | 12 | ) where |
15 | 13 |
|
16 | | -import Prelude |
17 | | - |
18 | | -import Data.Nullable (Nullable, notNull, null) |
19 | 14 | import Effect (Effect) |
20 | | -import Effect.Uncurried (EffectFn2, runEffectFn2) |
| 15 | +import Effect.Uncurried (EffectFn1, EffectFn2, runEffectFn1, runEffectFn2) |
21 | 16 | import Node.FS (FileDescriptor) |
22 | | -import Node.FS.Constants (FileFlags(..), fileFlagsToNode) |
23 | | -import Node.FS.Perms (Perms) |
24 | | -import Node.FS.Perms as Perms |
25 | 17 | import Node.Path (FilePath) |
26 | 18 | import Node.Stream (Readable, Writable) |
27 | | - |
28 | | -foreign import createReadStreamImpl :: forall opts. EffectFn2 (Nullable FilePath) { | opts } (Readable ()) |
29 | | -foreign import createWriteStreamImpl :: forall opts. EffectFn2 (Nullable FilePath) { | opts } (Writable ()) |
30 | | - |
31 | | -readWrite :: Perms |
32 | | -readWrite = Perms.mkPerms rw rw rw |
33 | | - where |
34 | | - rw = Perms.read + Perms.write |
| 19 | +import Prim.Row as Row |
35 | 20 |
|
36 | 21 | -- | Create a Writable stream which writes data to the specified file, using |
37 | 22 | -- | the default options. |
38 | | -createWriteStream |
39 | | - :: FilePath |
| 23 | +createWriteStream :: FilePath -> Effect (Writable ()) |
| 24 | +createWriteStream f = runEffectFn1 createWriteStreamImpl f |
| 25 | + |
| 26 | +foreign import createWriteStreamImpl :: EffectFn1 (FilePath) (Writable ()) |
| 27 | + |
| 28 | +type WriteStreamOptions = |
| 29 | + ( flags :: String |
| 30 | + , encoding :: String |
| 31 | + , mode :: Int |
| 32 | + , autoClose :: Boolean |
| 33 | + , emitClose :: Boolean |
| 34 | + , start :: Int |
| 35 | + ) |
| 36 | + |
| 37 | +-- | Create a Writable stream which writes data to the specified file. |
| 38 | +-- | Unused options should not be specified. Some options |
| 39 | +-- | (e.g. `flags`, `encoding`, and `mode`) should convert their |
| 40 | +-- | PureScript values to the corresponding JavaScript ones: |
| 41 | +-- | ``` |
| 42 | +-- | filePath # createWriteStream' |
| 43 | +-- | { flags: fileFlagsToNode R |
| 44 | +-- | , encoding: encodingToNode UTF8 |
| 45 | +-- | , mode: permsToInt Perms.all |
| 46 | +-- | } |
| 47 | +-- | ``` |
| 48 | +createWriteStream' |
| 49 | + :: forall r trash |
| 50 | + . Row.Union r trash WriteStreamOptions |
| 51 | + => FilePath |
| 52 | + -> { | r } |
40 | 53 | -> Effect (Writable ()) |
41 | | -createWriteStream = createWriteStreamWith defaultWriteStreamOptions |
| 54 | +createWriteStream' f opts = runEffectFn2 createWriteStreamOptsImpl f opts |
| 55 | + |
| 56 | +foreign import createWriteStreamOptsImpl :: forall r. EffectFn2 (FilePath) ({ | r }) ((Writable ())) |
42 | 57 |
|
43 | 58 | -- | Create a Writable stream which writes data to the specified file |
44 | 59 | -- | descriptor, using the default options. |
45 | | -fdCreateWriteStream |
46 | | - :: FileDescriptor |
| 60 | +fdCreateWriteStream :: FileDescriptor -> Effect (Writable ()) |
| 61 | +fdCreateWriteStream f = runEffectFn1 fdCreateWriteStreamImpl f |
| 62 | + |
| 63 | +foreign import fdCreateWriteStreamImpl :: EffectFn1 (FileDescriptor) (Writable ()) |
| 64 | + |
| 65 | +-- | Create a Writable stream which writes data to the specified file descriptor. |
| 66 | +-- | Unused options should not be specified. Some options |
| 67 | +-- | (e.g. `flags`, `encoding`, and `mode`) should convert their |
| 68 | +-- | PureScript values to the corresponding JavaScript ones: |
| 69 | +-- | ``` |
| 70 | +-- | filePath # fdCreateWriteStream' |
| 71 | +-- | { flags: fileFlagsToNode R |
| 72 | +-- | , encoding: encodingToNode UTF8 |
| 73 | +-- | , mode: permsToInt Perms.all |
| 74 | +-- | } |
| 75 | +-- | ``` |
| 76 | +fdCreateWriteStream' |
| 77 | + :: forall r trash |
| 78 | + . Row.Union r trash WriteStreamOptions |
| 79 | + => FileDescriptor |
| 80 | + -> { | r } |
47 | 81 | -> Effect (Writable ()) |
48 | | -fdCreateWriteStream = fdCreateWriteStreamWith defaultWriteStreamOptions |
| 82 | +fdCreateWriteStream' f opts = runEffectFn2 fdCreateWriteStreamOptsImpl f opts |
49 | 83 |
|
50 | | -type WriteStreamOptions = |
51 | | - { flags :: FileFlags |
52 | | - , perms :: Perms |
53 | | - } |
54 | | - |
55 | | -defaultWriteStreamOptions :: WriteStreamOptions |
56 | | -defaultWriteStreamOptions = |
57 | | - { flags: W |
58 | | - , perms: readWrite |
59 | | - } |
60 | | - |
61 | | --- | Like `createWriteStream`, but allows you to pass options. |
62 | | -createWriteStreamWith |
63 | | - :: WriteStreamOptions |
64 | | - -> FilePath |
65 | | - -> Effect (Writable ()) |
66 | | -createWriteStreamWith opts file = runEffectFn2 |
67 | | - createWriteStreamImpl |
68 | | - (notNull file) |
69 | | - { mode: Perms.permsToInt opts.perms |
70 | | - , flags: fileFlagsToNode opts.flags |
71 | | - } |
72 | | - |
73 | | --- | Like `fdCreateWriteStream`, but allows you to pass options. |
74 | | -fdCreateWriteStreamWith |
75 | | - :: WriteStreamOptions |
76 | | - -> FileDescriptor |
77 | | - -> Effect (Writable ()) |
78 | | -fdCreateWriteStreamWith opts fd = runEffectFn2 |
79 | | - createWriteStreamImpl |
80 | | - null |
81 | | - { fd |
82 | | - , mode: Perms.permsToInt opts.perms |
83 | | - , flags: fileFlagsToNode opts.flags |
84 | | - } |
| 84 | +foreign import fdCreateWriteStreamOptsImpl :: forall r. EffectFn2 (FileDescriptor) ({ | r }) (Writable ()) |
85 | 85 |
|
86 | 86 | -- | Create a Readable stream which reads data to the specified file, using |
87 | 87 | -- | the default options. |
88 | | -createReadStream |
89 | | - :: FilePath |
90 | | - -> Effect (Readable ()) |
91 | | -createReadStream = createReadStreamWith defaultReadStreamOptions |
| 88 | +createReadStream :: FilePath -> Effect (Readable ()) |
| 89 | +createReadStream p = runEffectFn1 createReadStreamImpl p |
92 | 90 |
|
93 | | --- | Create a Readable stream which reads data to the specified file |
94 | | --- | descriptor, using the default options. |
95 | | -fdCreateReadStream |
96 | | - :: FileDescriptor |
97 | | - -> Effect (Readable ()) |
98 | | -fdCreateReadStream = fdCreateReadStreamWith defaultReadStreamOptions |
| 91 | +foreign import createReadStreamImpl :: EffectFn1 (FilePath) (Readable ()) |
99 | 92 |
|
100 | 93 | type ReadStreamOptions = |
101 | | - { flags :: FileFlags |
102 | | - , perms :: Perms |
| 94 | + ( flags :: String |
| 95 | + , encoding :: String |
| 96 | + , mode :: Int |
103 | 97 | , autoClose :: Boolean |
104 | | - } |
105 | | - |
106 | | -defaultReadStreamOptions :: ReadStreamOptions |
107 | | -defaultReadStreamOptions = |
108 | | - { flags: R |
109 | | - , perms: readWrite |
110 | | - , autoClose: true |
111 | | - } |
| 98 | + , emitClose :: Boolean |
| 99 | + , start :: Int |
| 100 | + , end :: Int |
| 101 | + , highWaterMark :: Int |
| 102 | + ) |
112 | 103 |
|
113 | 104 | -- | Create a Readable stream which reads data from the specified file. |
114 | | -createReadStreamWith |
115 | | - :: ReadStreamOptions |
116 | | - -> FilePath |
| 105 | +-- | Unused options should not be specified. Some options |
| 106 | +-- | (e.g. `flags`, `encoding`, and `mode`) should convert their |
| 107 | +-- | PureScript values to the corresponding JavaScript ones: |
| 108 | +-- | ``` |
| 109 | +-- | filePath # createReadStream' |
| 110 | +-- | { flags: fileFlagsToNode R |
| 111 | +-- | , encoding: encodingToNode UTF8 |
| 112 | +-- | , mode: permsToInt Perms.all |
| 113 | +-- | } |
| 114 | +-- | ``` |
| 115 | +createReadStream' |
| 116 | + :: forall r trash |
| 117 | + . Row.Union r trash ReadStreamOptions |
| 118 | + => FilePath |
| 119 | + -> { | r } |
117 | 120 | -> Effect (Readable ()) |
118 | | -createReadStreamWith opts file = runEffectFn2 |
119 | | - createReadStreamImpl |
120 | | - (notNull file) |
121 | | - { mode: Perms.permsToInt opts.perms |
122 | | - , flags: fileFlagsToNode opts.flags |
123 | | - , autoClose: opts.autoClose |
124 | | - } |
125 | | - |
126 | | --- | Create a Readable stream which reads data from the specified file descriptor. |
127 | | -fdCreateReadStreamWith |
128 | | - :: ReadStreamOptions |
129 | | - -> FileDescriptor |
| 121 | +createReadStream' path opts = runEffectFn2 createReadStreamOptsImpl path opts |
| 122 | + |
| 123 | +foreign import createReadStreamOptsImpl :: forall r. EffectFn2 (FilePath) ({ | r }) ((Readable ())) |
| 124 | + |
| 125 | +-- | Create a Readable stream which reads data to the specified file |
| 126 | +-- | descriptor, using the default options. |
| 127 | +fdCreateReadStream :: FileDescriptor -> Effect (Readable ()) |
| 128 | +fdCreateReadStream f = runEffectFn1 fdCreateReadStreamImpl f |
| 129 | + |
| 130 | +foreign import fdCreateReadStreamImpl :: EffectFn1 (FileDescriptor) (Readable ()) |
| 131 | + |
| 132 | +-- | Create a Readable stream which reads data to the specified file descriptor. |
| 133 | +-- | Unused options should not be specified. Some options |
| 134 | +-- | (e.g. `flags`, `encoding`, and `mode`) should convert their |
| 135 | +-- | PureScript values to the corresponding JavaScript ones: |
| 136 | +-- | ``` |
| 137 | +-- | filePath # fdCreateReadStream' |
| 138 | +-- | { flags: fileFlagsToNode R |
| 139 | +-- | , encoding: encodingToNode UTF8 |
| 140 | +-- | , mode: permsToInt Perms.all |
| 141 | +-- | } |
| 142 | +-- | ``` |
| 143 | +fdCreateReadStream' |
| 144 | + :: forall r trash |
| 145 | + . Row.Union r trash ReadStreamOptions |
| 146 | + => FileDescriptor |
| 147 | + -> { | r } |
130 | 148 | -> Effect (Readable ()) |
131 | | -fdCreateReadStreamWith opts fd = runEffectFn2 |
132 | | - createReadStreamImpl |
133 | | - null |
134 | | - { fd |
135 | | - , mode: Perms.permsToInt opts.perms |
136 | | - , flags: fileFlagsToNode opts.flags |
137 | | - , autoClose: opts.autoClose |
138 | | - } |
| 149 | +fdCreateReadStream' f opts = runEffectFn2 fdCreateReadStreamOptsImpl f opts |
| 150 | + |
| 151 | +foreign import fdCreateReadStreamOptsImpl :: forall r. EffectFn2 (FileDescriptor) ({ | r }) ((Readable ())) |
| 152 | + |
0 commit comments