@@ -39,67 +39,61 @@ import Prelude
39
39
import Data.DateTime (DateTime )
40
40
import Data.DateTime.Instant (fromDateTime , unInstant )
41
41
import Data.Either (Either (..))
42
- import Data.Function.Uncurried (Fn2 , Fn6 , Fn4 , Fn3 , runFn2 , runFn6 , runFn4 , runFn3 )
43
42
import Data.Int (round )
44
43
import Data.Maybe (Maybe (..))
45
- import Data.Nullable (Nullable , toNullable )
44
+ import Data.Nullable (Nullable , toMaybe , toNullable )
46
45
import Data.Time.Duration (Milliseconds (..))
47
46
import Effect (Effect )
48
47
import Effect.Exception (Error )
48
+ import Effect.Uncurried (EffectFn2 , EffectFn3 , EffectFn4 , EffectFn6 , mkEffectFn2 , runEffectFn2 , runEffectFn3 , runEffectFn4 , runEffectFn6 )
49
49
import Node.Buffer (Buffer , size )
50
50
import Node.Encoding (Encoding )
51
51
import Node.FS (FileDescriptor , ByteCount , FilePosition , BufferLength , BufferOffset , FileMode , FileFlags , SymlinkType , fileFlagsToNode , symlinkTypeToNode )
52
- import Node.FS.Internal (mkEffect )
53
52
import Node.FS.Perms (Perms , permsToString , all , mkPerms )
54
53
import Node.FS.Stats (StatsObj , Stats (..))
55
54
import Node.Path (FilePath )
56
55
57
- type JSCallback a = Fn2 (Nullable Error ) a Unit
58
-
59
- foreign import handleCallbackImpl ::
60
- forall a . Fn3 (Error -> Either Error a )
61
- (a -> Either Error a )
62
- (Callback a )
63
- (JSCallback a )
64
-
65
- handleCallback :: forall a . (Callback a ) -> JSCallback a
66
- handleCallback cb = runFn3 handleCallbackImpl Left Right cb
56
+ type JSCallback a = EffectFn2 (Nullable Error ) a Unit
67
57
58
+ handleCallback :: forall a . Callback a -> JSCallback a
59
+ handleCallback cb = mkEffectFn2 \err a -> case toMaybe err of
60
+ Nothing -> cb (Right a)
61
+ Just err' -> cb (Left err')
68
62
69
63
-- | Type synonym for callback functions.
70
64
type Callback a = Either Error a -> Effect Unit
71
65
72
- foreign import renameImpl :: Fn3 FilePath FilePath (JSCallback Unit ) Unit
73
- foreign import truncateImpl :: Fn3 FilePath Int (JSCallback Unit ) Unit
74
- foreign import chownImpl :: Fn4 FilePath Int Int (JSCallback Unit ) Unit
75
- foreign import chmodImpl :: Fn3 FilePath String (JSCallback Unit ) Unit
76
- foreign import statImpl :: Fn2 FilePath (JSCallback StatsObj ) Unit
77
- foreign import lstatImpl :: Fn2 FilePath (JSCallback StatsObj ) Unit
78
- foreign import linkImpl :: Fn3 FilePath FilePath (JSCallback Unit ) Unit
79
- foreign import symlinkImpl :: Fn4 FilePath FilePath String (JSCallback Unit ) Unit
80
- foreign import readlinkImpl :: Fn2 FilePath (JSCallback FilePath ) Unit
81
- foreign import realpathImpl :: forall cache . Fn3 FilePath { | cache } (JSCallback FilePath ) Unit
82
- foreign import unlinkImpl :: Fn2 FilePath (JSCallback Unit ) Unit
83
- foreign import rmdirImpl :: Fn3 FilePath { maxRetries :: Int , retryDelay :: Int } (JSCallback Unit ) Unit
84
- foreign import rmImpl :: Fn3 FilePath { force :: Boolean , maxRetries :: Int , recursive :: Boolean , retryDelay :: Int } (JSCallback Unit ) Unit
85
- foreign import mkdirImpl :: Fn3 FilePath { recursive :: Boolean , mode :: String } (JSCallback Unit ) Unit
86
- foreign import readdirImpl :: Fn2 FilePath (JSCallback (Array FilePath )) Unit
87
- foreign import utimesImpl :: Fn4 FilePath Int Int (JSCallback Unit ) Unit
88
- foreign import readFileImpl :: forall a opts . Fn3 FilePath { | opts } (JSCallback a ) Unit
89
- foreign import writeFileImpl :: forall a opts . Fn4 FilePath a { | opts } (JSCallback Unit ) Unit
90
- foreign import appendFileImpl :: forall a opts . Fn4 FilePath a { | opts } (JSCallback Unit ) Unit
91
- foreign import openImpl :: Fn4 FilePath String (Nullable FileMode ) (JSCallback FileDescriptor ) Unit
92
- foreign import readImpl :: Fn6 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition ) (JSCallback ByteCount ) Unit
93
- foreign import writeImpl :: Fn6 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition ) (JSCallback ByteCount ) Unit
94
- foreign import closeImpl :: Fn2 FileDescriptor (JSCallback Unit ) Unit
66
+ foreign import renameImpl :: EffectFn3 FilePath FilePath (JSCallback Unit ) Unit
67
+ foreign import truncateImpl :: EffectFn3 FilePath Int (JSCallback Unit ) Unit
68
+ foreign import chownImpl :: EffectFn4 FilePath Int Int (JSCallback Unit ) Unit
69
+ foreign import chmodImpl :: EffectFn3 FilePath String (JSCallback Unit ) Unit
70
+ foreign import statImpl :: EffectFn2 FilePath (JSCallback StatsObj ) Unit
71
+ foreign import lstatImpl :: EffectFn2 FilePath (JSCallback StatsObj ) Unit
72
+ foreign import linkImpl :: EffectFn3 FilePath FilePath (JSCallback Unit ) Unit
73
+ foreign import symlinkImpl :: EffectFn4 FilePath FilePath String (JSCallback Unit ) Unit
74
+ foreign import readlinkImpl :: EffectFn2 FilePath (JSCallback FilePath ) Unit
75
+ foreign import realpathImpl :: forall cache . EffectFn3 FilePath { | cache } (JSCallback FilePath ) Unit
76
+ foreign import unlinkImpl :: EffectFn2 FilePath (JSCallback Unit ) Unit
77
+ foreign import rmdirImpl :: EffectFn3 FilePath { maxRetries :: Int , retryDelay :: Int } (JSCallback Unit ) Unit
78
+ foreign import rmImpl :: EffectFn3 FilePath { force :: Boolean , maxRetries :: Int , recursive :: Boolean , retryDelay :: Int } (JSCallback Unit ) Unit
79
+ foreign import mkdirImpl :: EffectFn3 FilePath { recursive :: Boolean , mode :: String } (JSCallback Unit ) Unit
80
+ foreign import readdirImpl :: EffectFn2 FilePath (JSCallback (Array FilePath )) Unit
81
+ foreign import utimesImpl :: EffectFn4 FilePath Int Int (JSCallback Unit ) Unit
82
+ foreign import readFileImpl :: forall a opts . EffectFn3 FilePath { | opts } (JSCallback a ) Unit
83
+ foreign import writeFileImpl :: forall a opts . EffectFn4 FilePath a { | opts } (JSCallback Unit ) Unit
84
+ foreign import appendFileImpl :: forall a opts . EffectFn4 FilePath a { | opts } (JSCallback Unit ) Unit
85
+ foreign import openImpl :: EffectFn4 FilePath String (Nullable FileMode ) (JSCallback FileDescriptor ) Unit
86
+ foreign import readImpl :: EffectFn6 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition ) (JSCallback ByteCount ) Unit
87
+ foreign import writeImpl :: EffectFn6 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition ) (JSCallback ByteCount ) Unit
88
+ foreign import closeImpl :: EffectFn2 FileDescriptor (JSCallback Unit ) Unit
95
89
96
90
97
91
-- | Renames a file.
98
92
rename :: FilePath
99
93
-> FilePath
100
94
-> Callback Unit
101
95
-> Effect Unit
102
- rename oldFile newFile cb = mkEffect $ \_ -> runFn3
96
+ rename oldFile newFile cb = runEffectFn3
103
97
renameImpl oldFile newFile (handleCallback cb)
104
98
105
99
-- | Truncates a file to the specified length.
@@ -108,7 +102,7 @@ truncate :: FilePath
108
102
-> Callback Unit
109
103
-> Effect Unit
110
104
111
- truncate file len cb = mkEffect $ \_ -> runFn3
105
+ truncate file len cb = runEffectFn3
112
106
truncateImpl file len (handleCallback cb)
113
107
114
108
-- | Changes the ownership of a file.
@@ -118,7 +112,7 @@ chown :: FilePath
118
112
-> Callback Unit
119
113
-> Effect Unit
120
114
121
- chown file uid gid cb = mkEffect $ \_ -> runFn4
115
+ chown file uid gid cb = runEffectFn4
122
116
chownImpl file uid gid (handleCallback cb)
123
117
124
118
-- | Changes the permissions of a file.
@@ -127,15 +121,15 @@ chmod :: FilePath
127
121
-> Callback Unit
128
122
-> Effect Unit
129
123
130
- chmod file perms cb = mkEffect $ \_ -> runFn3
124
+ chmod file perms cb = runEffectFn3
131
125
chmodImpl file (permsToString perms) (handleCallback cb)
132
126
133
127
-- | Gets file statistics.
134
128
stat :: FilePath
135
129
-> Callback Stats
136
130
-> Effect Unit
137
131
138
- stat file cb = mkEffect $ \_ -> runFn2
132
+ stat file cb = runEffectFn2
139
133
statImpl file (handleCallback $ cb <<< map Stats )
140
134
141
135
-- | Gets file or symlink statistics. `lstat` is identical to `stat`, except
@@ -144,7 +138,7 @@ stat file cb = mkEffect $ \_ -> runFn2
144
138
lstat :: FilePath
145
139
-> Callback Stats
146
140
-> Effect Unit
147
- lstat file cb = mkEffect $ \_ -> runFn2
141
+ lstat file cb = runEffectFn2
148
142
lstatImpl file (handleCallback $ cb <<< map Stats )
149
143
150
144
-- | Creates a link to an existing file.
@@ -153,7 +147,7 @@ link :: FilePath
153
147
-> Callback Unit
154
148
-> Effect Unit
155
149
156
- link src dst cb = mkEffect $ \_ -> runFn3
150
+ link src dst cb = runEffectFn3
157
151
linkImpl src dst (handleCallback cb)
158
152
159
153
-- | Creates a symlink.
@@ -163,23 +157,23 @@ symlink :: FilePath
163
157
-> Callback Unit
164
158
-> Effect Unit
165
159
166
- symlink src dest ty cb = mkEffect $ \_ -> runFn4
160
+ symlink src dest ty cb = runEffectFn4
167
161
symlinkImpl src dest (symlinkTypeToNode ty) (handleCallback cb)
168
162
169
163
-- | Reads the value of a symlink.
170
164
readlink :: FilePath
171
165
-> Callback FilePath
172
166
-> Effect Unit
173
167
174
- readlink path cb = mkEffect $ \_ -> runFn2
168
+ readlink path cb = runEffectFn2
175
169
readlinkImpl path (handleCallback cb)
176
170
177
171
-- | Find the canonicalized absolute location for a path.
178
172
realpath :: FilePath
179
173
-> Callback FilePath
180
174
-> Effect Unit
181
175
182
- realpath path cb = mkEffect $ \_ -> runFn3
176
+ realpath path cb = runEffectFn3
183
177
realpathImpl path {} (handleCallback cb)
184
178
185
179
-- | Find the canonicalized absolute location for a path using a cache object
@@ -189,15 +183,15 @@ realpath' :: forall cache. FilePath
189
183
-> Callback FilePath
190
184
-> Effect Unit
191
185
192
- realpath' path cache cb = mkEffect $ \_ -> runFn3
186
+ realpath' path cache cb = runEffectFn3
193
187
realpathImpl path cache (handleCallback cb)
194
188
195
189
-- | Deletes a file.
196
190
unlink :: FilePath
197
191
-> Callback Unit
198
192
-> Effect Unit
199
193
200
- unlink file cb = mkEffect $ \_ -> runFn2
194
+ unlink file cb = runEffectFn2
201
195
unlinkImpl file (handleCallback cb)
202
196
203
197
-- | Deletes a directory.
@@ -211,7 +205,7 @@ rmdir' :: FilePath
211
205
-> { maxRetries :: Int , retryDelay :: Int }
212
206
-> Callback Unit
213
207
-> Effect Unit
214
- rmdir' path opts cb = mkEffect $ \_ -> runFn3
208
+ rmdir' path opts cb = runEffectFn3
215
209
rmdirImpl path opts (handleCallback cb)
216
210
217
211
-- | Deletes a file or directory.
@@ -225,7 +219,7 @@ rm' :: FilePath
225
219
-> { force :: Boolean , maxRetries :: Int , recursive :: Boolean , retryDelay :: Int }
226
220
-> Callback Unit
227
221
-> Effect Unit
228
- rm' path opts cb = mkEffect $ \_ -> runFn3
222
+ rm' path opts cb = runEffectFn3
229
223
rmImpl path opts (handleCallback cb)
230
224
231
225
@@ -241,15 +235,15 @@ mkdir'
241
235
-> { recursive :: Boolean , mode :: Perms }
242
236
-> Callback Unit
243
237
-> Effect Unit
244
- mkdir' file { recursive, mode: perms } cb = mkEffect $ \_ -> runFn3
238
+ mkdir' file { recursive, mode: perms } cb = runEffectFn3
245
239
mkdirImpl file { recursive, mode: permsToString perms } (handleCallback cb)
246
240
247
241
-- | Reads the contents of a directory.
248
242
readdir :: FilePath
249
243
-> Callback (Array FilePath )
250
244
-> Effect Unit
251
245
252
- readdir file cb = mkEffect $ \_ -> runFn2
246
+ readdir file cb = runEffectFn2
253
247
readdirImpl file (handleCallback cb)
254
248
255
249
-- | Sets the accessed and modified times for the specified file.
@@ -259,7 +253,7 @@ utimes :: FilePath
259
253
-> Callback Unit
260
254
-> Effect Unit
261
255
262
- utimes file atime mtime cb = mkEffect $ \_ -> runFn4
256
+ utimes file atime mtime cb = runEffectFn4
263
257
utimesImpl file
264
258
(fromDate atime)
265
259
(fromDate mtime)
@@ -274,7 +268,7 @@ readFile :: FilePath
274
268
-> Callback Buffer
275
269
-> Effect Unit
276
270
277
- readFile file cb = mkEffect $ \_ -> runFn3
271
+ readFile file cb = runEffectFn3
278
272
readFileImpl file {} (handleCallback cb)
279
273
280
274
-- | Reads the entire contents of a text file with the specified encoding.
@@ -283,7 +277,7 @@ readTextFile :: Encoding
283
277
-> Callback String
284
278
-> Effect Unit
285
279
286
- readTextFile encoding file cb = mkEffect $ \_ -> runFn3
280
+ readTextFile encoding file cb = runEffectFn3
287
281
readFileImpl file { encoding: show encoding } (handleCallback cb)
288
282
289
283
-- | Writes a buffer to a file.
@@ -292,7 +286,7 @@ writeFile :: FilePath
292
286
-> Callback Unit
293
287
-> Effect Unit
294
288
295
- writeFile file buff cb = mkEffect $ \_ -> runFn4
289
+ writeFile file buff cb = runEffectFn4
296
290
writeFileImpl file buff {} (handleCallback cb)
297
291
298
292
-- | Writes text to a file using the specified encoding.
@@ -302,7 +296,7 @@ writeTextFile :: Encoding
302
296
-> Callback Unit
303
297
-> Effect Unit
304
298
305
- writeTextFile encoding file buff cb = mkEffect $ \_ -> runFn4
299
+ writeTextFile encoding file buff cb = runEffectFn4
306
300
writeFileImpl file buff { encoding: show encoding } (handleCallback cb)
307
301
308
302
-- | Appends the contents of a buffer to a file.
@@ -311,7 +305,7 @@ appendFile :: FilePath
311
305
-> Callback Unit
312
306
-> Effect Unit
313
307
314
- appendFile file buff cb = mkEffect $ \_ -> runFn4
308
+ appendFile file buff cb = runEffectFn4
315
309
appendFileImpl file buff {} (handleCallback cb)
316
310
317
311
-- | Appends text to a file using the specified encoding.
@@ -321,7 +315,7 @@ appendTextFile :: Encoding
321
315
-> Callback Unit
322
316
-> Effect Unit
323
317
324
- appendTextFile encoding file buff cb = mkEffect $ \_ -> runFn4
318
+ appendTextFile encoding file buff cb = runEffectFn4
325
319
appendFileImpl file buff { encoding: show encoding } (handleCallback cb)
326
320
327
321
@@ -332,7 +326,7 @@ fdOpen :: FilePath
332
326
-> Maybe FileMode
333
327
-> Callback FileDescriptor
334
328
-> Effect Unit
335
- fdOpen file flags mode cb = mkEffect $ \_ -> runFn4 openImpl file (fileFlagsToNode flags) (toNullable mode) (handleCallback cb)
329
+ fdOpen file flags mode cb = runEffectFn4 openImpl file (fileFlagsToNode flags) (toNullable mode) (handleCallback cb)
336
330
337
331
-- | Read from a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_read_fd_buffer_offset_length_position_callback)
338
332
-- | for details.
@@ -343,7 +337,7 @@ fdRead :: FileDescriptor
343
337
-> Maybe FilePosition
344
338
-> Callback ByteCount
345
339
-> Effect Unit
346
- fdRead fd buff off len pos cb = mkEffect $ \_ -> runFn6 readImpl fd buff off len (toNullable pos) (handleCallback cb)
340
+ fdRead fd buff off len pos cb = runEffectFn6 readImpl fd buff off len (toNullable pos) (handleCallback cb)
347
341
348
342
-- | Convenience function to fill the whole buffer from the current
349
343
-- | file position.
@@ -364,7 +358,7 @@ fdWrite :: FileDescriptor
364
358
-> Maybe FilePosition
365
359
-> Callback ByteCount
366
360
-> Effect Unit
367
- fdWrite fd buff off len pos cb = mkEffect $ \_ -> runFn6 writeImpl fd buff off len (toNullable pos) (handleCallback cb)
361
+ fdWrite fd buff off len pos cb = runEffectFn6 writeImpl fd buff off len (toNullable pos) (handleCallback cb)
368
362
369
363
-- | Convenience function to append the whole buffer to the current
370
364
-- | file position.
@@ -381,4 +375,4 @@ fdAppend fd buff cb = do
381
375
fdClose :: FileDescriptor
382
376
-> Callback Unit
383
377
-> Effect Unit
384
- fdClose fd cb = mkEffect $ \_ -> runFn2 closeImpl fd (handleCallback cb)
378
+ fdClose fd cb = runEffectFn2 closeImpl fd (handleCallback cb)
0 commit comments