Skip to content

Commit d2dd38d

Browse files
author
Juergen Gmeiner
committed
additional api using Number purescript-node#37
1 parent d18a01f commit d2dd38d

File tree

2 files changed

+136
-33
lines changed

2 files changed

+136
-33
lines changed

src/Node/FS/Async.purs

+70-17
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,35 @@ module Node.FS.Async
2525
, exists
2626
, fdOpen
2727
, fdRead
28+
, fdReadLarge
2829
, fdNext
2930
, fdWrite
31+
, fdWriteLarge
3032
, fdAppend
3133
, fdClose
3234
) where
3335

3436
import Prelude
37+
3538
import Control.Monad.Eff (Eff, runPure)
36-
import Control.Monad.Eff.Unsafe (unsafeCoerceEff)
3739
import Control.Monad.Eff.Exception (Error)
40+
import Control.Monad.Eff.Unsafe (unsafeCoerceEff)
3841
import Data.DateTime (DateTime)
39-
import Data.Time.Duration (Milliseconds(..))
4042
import Data.DateTime.Instant (fromDateTime, unInstant)
4143
import Data.Either (Either(..))
42-
import Data.Function.Uncurried (Fn2, Fn6, Fn4, Fn3,
43-
runFn2, runFn6, runFn4, runFn3)
44-
import Data.Maybe (Maybe(..))
44+
import Data.Function.Uncurried (Fn2, Fn6, Fn4, Fn3, runFn2, runFn6, runFn4, runFn3)
45+
import Data.Int (fromNumber, round, toNumber)
46+
import Data.Maybe (Maybe(..), fromJust)
4547
import Data.Nullable (Nullable, toNullable)
46-
import Node.Buffer (Buffer(), BUFFER(), size)
47-
import Data.Int (round)
48+
import Data.Time.Duration (Milliseconds(..))
49+
import Node.Buffer (Buffer, BUFFER, size)
4850
import Node.Encoding (Encoding)
49-
import Node.FS (FS, FileDescriptor, ByteCount, FilePosition, BufferLength,
50-
BufferOffset, FileMode, FileFlags, SymlinkType,
51-
fileFlagsToNode, symlinkTypeToNode)
52-
import Node.FS.Stats (StatsObj, Stats(..))
53-
import Node.Path (FilePath())
54-
import Node.FS.Perms (Perms, permsToString, all, mkPerms)
51+
import Node.FS (FS, FileDescriptor, ByteCount, FilePosition, BufferLength, BufferOffset, FileMode, FileFlags, SymlinkType, fileFlagsToNode, symlinkTypeToNode)
5552
import Node.FS.Internal (mkEff, unsafeRequireFS)
53+
import Node.FS.Perms (Perms, permsToString, all, mkPerms)
54+
import Node.FS.Stats (StatsObj, Stats(..))
55+
import Node.Path (FilePath)
56+
import Partial.Unsafe (unsafePartial)
5657

5758
type JSCallback a = Fn2 (Nullable Error) a Unit
5859

@@ -85,12 +86,13 @@ fs ::
8586
, appendFile :: forall a opts. Fn4 FilePath a { | opts } (JSCallback Unit) Unit
8687
, exists :: forall a. Fn2 FilePath (Boolean -> a) Unit
8788
, open :: Fn4 FilePath String (Nullable FileMode) (JSCallback FileDescriptor) Unit
88-
, read :: Fn6 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition) (JSCallback ByteCount) Unit
89-
, write :: Fn6 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition) (JSCallback ByteCount) Unit
89+
, read :: Fn6 FileDescriptor Buffer Number Number (Nullable Number) (JSCallback Number) Unit
90+
, write :: Fn6 FileDescriptor Buffer Number Number (Nullable Number) (JSCallback Number) Unit
9091
, close :: Fn2 FileDescriptor (JSCallback Unit) Unit
9192
}
9293
fs = unsafeRequireFS
9394

95+
9496
-- | Type synonym for callback functions.
9597
type Callback eff a = Either Error a -> Eff (fs :: FS | eff) Unit
9698

@@ -314,6 +316,10 @@ fdOpen file flags mode cb = mkEff $ \_ -> runFn4 fs.open file (fileFlagsToNode f
314316

315317
-- | Read from a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_read_fd_buffer_offset_length_position_callback)
316318
-- | for details.
319+
-- |
320+
-- | The use of an Int as FilePosition restricts this API to reading
321+
-- | files < 2GB. The call is retained to not break existing code.
322+
-- | fdReadLarge does not have this restriction.
317323
fdRead :: forall eff.
318324
FileDescriptor
319325
-> Buffer
@@ -322,7 +328,28 @@ fdRead :: forall eff.
322328
-> Maybe FilePosition
323329
-> Callback (buffer :: BUFFER | eff) ByteCount
324330
-> Eff (buffer :: BUFFER, fs :: FS | eff) Unit
325-
fdRead fd buff off len pos cb = mkEff $ \_ -> runFn6 fs.read fd buff off len (toNullable pos) (handleCallback cb)
331+
fdRead fd buff off len pos cb =
332+
let
333+
off' = toNumber off
334+
len' = toNumber len
335+
pos' = map toNumber pos
336+
cb' x = cb (unsafePartial fromJust <<< fromNumber <$> x)
337+
in
338+
fdReadLarge fd buff off' len' pos' cb'
339+
340+
-- | Read from a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_read_fd_buffer_offset_length_position_callback)
341+
-- | for details.
342+
-- |
343+
-- | This version takes and returns Number. It can read any file Node can.
344+
fdReadLarge :: forall eff.
345+
FileDescriptor
346+
-> Buffer
347+
-> Number
348+
-> Number
349+
-> Maybe Number
350+
-> Callback (buffer :: BUFFER | eff) Number
351+
-> Eff (buffer :: BUFFER, fs :: FS | eff) Unit
352+
fdReadLarge fd buff off len pos cb = mkEff $ \_ -> runFn6 fs.read fd buff off len (toNullable pos) (handleCallback cb)
326353

327354
-- | Convenience function to fill the whole buffer from the current
328355
-- | file position.
@@ -337,6 +364,10 @@ fdNext fd buff cb = do
337364

338365
-- | Write to a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_write_fd_buffer_offset_length_position_callback)
339366
-- | for details.
367+
-- |
368+
-- | The use of an Int as FilePosition restricts this API to writing
369+
-- | files < 2GB. The call is retained to not break existing code.
370+
-- | fdWriteLarge does not have this restriction.
340371
fdWrite :: forall eff.
341372
FileDescriptor
342373
-> Buffer
@@ -345,7 +376,29 @@ fdWrite :: forall eff.
345376
-> Maybe FilePosition
346377
-> Callback (buffer :: BUFFER | eff) ByteCount
347378
-> Eff (buffer :: BUFFER, fs :: FS | eff) Unit
348-
fdWrite fd buff off len pos cb = mkEff $ \_ -> runFn6 fs.write fd buff off len (toNullable pos) (handleCallback cb)
379+
fdWrite fd buff off len pos cb =
380+
let
381+
off' = toNumber off
382+
len' = toNumber len
383+
pos' = map toNumber pos
384+
cb' x = cb (unsafePartial fromJust <<< fromNumber <$> x)
385+
in
386+
fdWriteLarge fd buff off' len' pos' cb'
387+
388+
-- | Write to a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_write_fd_buffer_offset_length_position_callback)
389+
-- | for details.
390+
-- |
391+
-- | This version takes and returns Number. It can write any file Node can.
392+
fdWriteLarge :: forall eff.
393+
FileDescriptor
394+
-> Buffer
395+
-> Number
396+
-> Number
397+
-> Maybe Number
398+
-> Callback (buffer :: BUFFER | eff) Number
399+
-> Eff (buffer :: BUFFER, fs :: FS | eff) Unit
400+
fdWriteLarge fd buff off len pos cb = mkEff $ \_ -> runFn6 fs.write fd buff off len (toNullable pos) (handleCallback cb)
401+
349402

350403
-- | Convenience function to append the whole buffer to the current
351404
-- | file position.

src/Node/FS/Sync.purs

+66-16
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,34 @@ module Node.FS.Sync
2424
, exists
2525
, fdOpen
2626
, fdRead
27+
, fdReadLarge
2728
, fdNext
2829
, fdWrite
30+
, fdWriteLarge
2931
, fdAppend
3032
, fdFlush
3133
, fdClose
3234
) where
3335

3436
import Prelude
37+
3538
import Control.Monad.Eff (Eff)
3639
import Control.Monad.Eff.Exception (EXCEPTION)
3740
import Data.DateTime (DateTime)
38-
import Data.Time.Duration (Milliseconds(..))
3941
import Data.DateTime.Instant (fromDateTime, unInstant)
40-
import Data.Function.Uncurried (Fn1, Fn5, Fn3, Fn2,
41-
runFn1, runFn5, runFn3, runFn2)
42-
import Data.Nullable (Nullable(), toNullable)
43-
import Data.Int (round)
44-
import Data.Maybe (Maybe(..))
45-
import Node.Buffer (Buffer(), BUFFER(), size)
42+
import Data.Function.Uncurried (Fn1, Fn5, Fn3, Fn2, runFn1, runFn5, runFn3, runFn2)
43+
import Data.Int (fromNumber, round, toNumber)
44+
import Data.Maybe (Maybe(..), fromJust)
45+
import Data.Nullable (Nullable, toNullable)
46+
import Data.Time.Duration (Milliseconds(..))
47+
import Node.Buffer (Buffer, BUFFER, size)
4648
import Node.Encoding (Encoding)
47-
48-
import Node.FS (FS, FileDescriptor, ByteCount, FilePosition, BufferLength,
49-
BufferOffset, FileMode, FileFlags, SymlinkType,
50-
fileFlagsToNode, symlinkTypeToNode)
51-
import Node.FS.Stats (StatsObj, Stats(..))
52-
import Node.Path (FilePath())
53-
import Node.FS.Perms (Perms, permsToString, all, mkPerms)
49+
import Node.FS (FS, FileDescriptor, ByteCount, FilePosition, BufferLength, BufferOffset, FileMode, FileFlags, SymlinkType, fileFlagsToNode, symlinkTypeToNode)
5450
import Node.FS.Internal (mkEff, unsafeRequireFS)
51+
import Node.FS.Perms (Perms, permsToString, all, mkPerms)
52+
import Node.FS.Stats (StatsObj, Stats(..))
53+
import Node.Path (FilePath)
54+
import Partial.Unsafe (unsafePartial)
5555

5656
fs ::
5757
{ renameSync :: Fn2 FilePath FilePath Unit
@@ -73,8 +73,8 @@ fs ::
7373
, appendFileSync :: forall a opts. Fn3 FilePath a { | opts } Unit
7474
, existsSync :: FilePath -> Boolean
7575
, openSync :: Fn3 FilePath String (Nullable FileMode) FileDescriptor
76-
, readSync :: Fn5 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition) ByteCount
77-
, writeSync :: Fn5 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition) ByteCount
76+
, readSync :: Fn5 FileDescriptor Buffer Number Number (Nullable Number) Number
77+
, writeSync :: Fn5 FileDescriptor Buffer Number Number (Nullable Number) Number
7878
, fsyncSync :: Fn1 FileDescriptor Unit
7979
, closeSync :: Fn1 FileDescriptor Unit
8080
}
@@ -276,6 +276,10 @@ fdOpen file flags mode = mkEff $ \_ ->
276276

277277
-- | Read from a file synchronously. See the [Node documentation](http://nodejs.org/api/fs.html#fs_fs_readsync_fd_buffer_offset_length_position)
278278
-- | for details.
279+
-- |
280+
-- | The use of an Int as FilePosition restricts this API to reading
281+
-- | files < 2GB. The call is retained to not break existing code.
282+
-- | fdReadLarge does not have this restriction.
279283
fdRead :: forall eff.
280284
FileDescriptor
281285
-> Buffer
@@ -284,6 +288,27 @@ fdRead :: forall eff.
284288
-> Maybe FilePosition
285289
-> Eff (buffer :: BUFFER, exception :: EXCEPTION, fs :: FS | eff) ByteCount
286290
fdRead fd buff off len pos =
291+
let
292+
off' = toNumber off
293+
len' = toNumber len
294+
pos' = map toNumber pos
295+
in
296+
map
297+
(unsafePartial fromJust <<< fromNumber)
298+
(fdReadLarge fd buff off' len' pos')
299+
300+
-- | Read from a file synchronously. See the [Node documentation](http://nodejs.org/api/fs.html#fs_fs_readsync_fd_buffer_offset_length_position)
301+
-- | for details.
302+
-- |
303+
-- | This version takes and returns Number. It can read any file Node can.
304+
fdReadLarge :: forall eff.
305+
FileDescriptor
306+
-> Buffer
307+
-> Number
308+
-> Number
309+
-> Maybe Number
310+
-> Eff (buffer :: BUFFER, exception :: EXCEPTION, fs :: FS | eff) Number
311+
fdReadLarge fd buff off len pos =
287312
mkEff $ \_ -> runFn5 fs.readSync fd buff off len (toNullable pos)
288313

289314
-- | Convenience function to fill the whole buffer from the current
@@ -298,6 +323,10 @@ fdNext fd buff = do
298323

299324
-- | Write to a file synchronously. See the [Node documentation](http://nodejs.org/api/fs.html#fs_fs_writesync_fd_buffer_offset_length_position)
300325
-- | for details.
326+
-- |
327+
-- | The use of an Int as FilePosition restricts this API to writing
328+
-- | files < 2GB. The call is retained to not break existing code.
329+
-- | fdWriteLarge does not have this restriction.
301330
fdWrite :: forall eff.
302331
FileDescriptor
303332
-> Buffer
@@ -306,6 +335,27 @@ fdWrite :: forall eff.
306335
-> Maybe FilePosition
307336
-> Eff (buffer :: BUFFER, exception :: EXCEPTION, fs :: FS | eff) ByteCount
308337
fdWrite fd buff off len pos =
338+
let
339+
off' = toNumber off
340+
len' = toNumber len
341+
pos' = map toNumber pos
342+
in
343+
map
344+
(unsafePartial fromJust <<< fromNumber)
345+
(fdWriteLarge fd buff off' len' pos')
346+
347+
-- | Write to a file synchronously. See the [Node documentation](http://nodejs.org/api/fs.html#fs_fs_writesync_fd_buffer_offset_length_position)
348+
-- | for details.
349+
-- |
350+
-- | This version takes and returns Number. It can write any file Node can.
351+
fdWriteLarge :: forall eff.
352+
FileDescriptor
353+
-> Buffer
354+
-> Number
355+
-> Number
356+
-> Maybe Number
357+
-> Eff (buffer :: BUFFER, exception :: EXCEPTION, fs :: FS | eff) Number
358+
fdWriteLarge fd buff off len pos =
309359
mkEff $ \_ -> runFn5 fs.writeSync fd buff off len (toNullable pos)
310360

311361
-- | Convenience function to append the whole buffer to the current

0 commit comments

Comments
 (0)