Skip to content
This repository was archived by the owner on Aug 4, 2023. It is now read-only.

Commit 0e0a799

Browse files
committed
Add file descriptor functions. Fixes #7
1 parent 5510808 commit 0e0a799

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

docs/Node/FS/Aff.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,58 @@ exists :: forall eff. String -> Aff (fs :: FS | eff) Boolean
231231
Check to see if a file exists.
232232

233233

234+
#### `fdOpen`
235+
236+
``` purescript
237+
fdOpen :: forall eff. FilePath -> FileFlags -> Maybe FileMode -> Aff (fs :: FS | eff) FileDescriptor
238+
```
239+
240+
Open a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback)
241+
for details.
242+
243+
#### `fdRead`
244+
245+
``` purescript
246+
fdRead :: forall eff. FileDescriptor -> Buffer -> BufferOffset -> BufferLength -> Maybe FilePosition -> Aff (buffer :: BUFFER, fs :: FS | eff) ByteCount
247+
```
248+
249+
Read from a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_read_fd_buffer_offset_length_position_callback)
250+
for details.
251+
252+
#### `fdNext`
253+
254+
``` purescript
255+
fdNext :: forall eff. FileDescriptor -> Buffer -> Aff (buffer :: BUFFER, fs :: FS | eff) ByteCount
256+
```
257+
258+
Convenience function to fill the whole buffer from the current
259+
file position.
260+
261+
#### `fdWrite`
262+
263+
``` purescript
264+
fdWrite :: forall eff. FileDescriptor -> Buffer -> BufferOffset -> BufferLength -> Maybe FilePosition -> Aff (buffer :: BUFFER, fs :: FS | eff) ByteCount
265+
```
266+
267+
Write to a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_write_fd_buffer_offset_length_position_callback)
268+
for details.
269+
270+
#### `fdAppend`
271+
272+
``` purescript
273+
fdAppend :: forall eff. FileDescriptor -> Buffer -> Aff (buffer :: BUFFER, fs :: FS | eff) ByteCount
274+
```
275+
276+
Convenience function to append the whole buffer to the current
277+
file position.
278+
279+
#### `fdClose`
280+
281+
``` purescript
282+
fdClose :: forall eff. FileDescriptor -> Aff (fs :: FS | eff) Unit
283+
```
284+
285+
Close a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_close_fd_callback)
286+
for details.
287+
234288

src/Node/FS/Aff.purs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@ module Node.FS.Aff
2222
, appendFile
2323
, appendTextFile
2424
, exists
25+
, fdOpen
26+
, fdRead
27+
, fdNext
28+
, fdWrite
29+
, fdAppend
30+
, fdClose
2531
) where
2632

2733
import Prelude
2834

35+
import Data.Maybe (Maybe())
2936
import Node.Path (FilePath())
3037
import Node.FS.Perms (Perms())
3138
import Node.FS.Stats (Stats())
@@ -64,6 +71,16 @@ toAff3 :: forall eff a x y z.
6471
Aff (fs :: F.FS | eff) a
6572
toAff3 f a b c = toAff (f a b c)
6673

74+
toAff5 :: forall eff a w v x y z.
75+
(w -> v -> x -> y -> z -> A.Callback eff a -> Eff (fs :: F.FS | eff) Unit) ->
76+
w ->
77+
v ->
78+
x ->
79+
y ->
80+
z ->
81+
Aff (fs :: F.FS | eff) a
82+
toAff5 f a b c d e = toAff (f a b c d e)
83+
6784
-- |
6885
-- | Rename a file.
6986
-- |
@@ -244,3 +261,57 @@ appendTextFile = toAff3 A.appendTextFile
244261
exists :: forall eff. String
245262
-> Aff (fs :: F.FS | eff) Boolean
246263
exists file = makeAff \_ a -> A.exists file a
264+
265+
-- | Open a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback)
266+
-- | for details.
267+
fdOpen :: forall eff.
268+
FilePath
269+
-> F.FileFlags
270+
-> Maybe F.FileMode
271+
-> Aff (fs :: F.FS | eff) F.FileDescriptor
272+
fdOpen = toAff3 A.fdOpen
273+
274+
-- | Read from a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_read_fd_buffer_offset_length_position_callback)
275+
-- | for details.
276+
fdRead :: forall eff.
277+
F.FileDescriptor
278+
-> Buffer
279+
-> F.BufferOffset
280+
-> F.BufferLength
281+
-> Maybe F.FilePosition
282+
-> Aff (buffer :: BUFFER, fs :: F.FS | eff) F.ByteCount
283+
fdRead = toAff5 A.fdRead
284+
285+
-- | Convenience function to fill the whole buffer from the current
286+
-- | file position.
287+
fdNext :: forall eff.
288+
F.FileDescriptor
289+
-> Buffer
290+
-> Aff (buffer :: BUFFER, fs :: F.FS | eff) F.ByteCount
291+
fdNext = toAff2 A.fdNext
292+
293+
-- | Write to a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_write_fd_buffer_offset_length_position_callback)
294+
-- | for details.
295+
fdWrite :: forall eff.
296+
F.FileDescriptor
297+
-> Buffer
298+
-> F.BufferOffset
299+
-> F.BufferLength
300+
-> Maybe F.FilePosition
301+
-> Aff (buffer :: BUFFER, fs :: F.FS | eff) F.ByteCount
302+
fdWrite = toAff5 A.fdWrite
303+
304+
-- | Convenience function to append the whole buffer to the current
305+
-- | file position.
306+
fdAppend :: forall eff.
307+
F.FileDescriptor
308+
-> Buffer
309+
-> Aff (buffer :: BUFFER, fs :: F.FS | eff) F.ByteCount
310+
fdAppend = toAff2 A.fdAppend
311+
312+
-- | Close a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_close_fd_callback)
313+
-- | for details.
314+
fdClose :: forall eff.
315+
F.FileDescriptor
316+
-> Aff (fs :: F.FS | eff) Unit
317+
fdClose = toAff1 A.fdClose

0 commit comments

Comments
 (0)