Skip to content

Commit fd223e3

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

File tree

2 files changed

+88
-8
lines changed

2 files changed

+88
-8
lines changed

src/Node/FS/Async.purs

+47-5
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ module Node.FS.Async
2525
, exists
2626
, fdOpen
2727
, fdRead
28+
, fdReadLarge
2829
, fdNext
2930
, fdWrite
31+
, fdWriteLarge
3032
, fdAppend
3133
, fdClose
3234
) where
@@ -44,7 +46,7 @@ import Data.Function.Uncurried (Fn2, Fn6, Fn4, Fn3,
4446
import Data.Maybe (Maybe(..))
4547
import Data.Nullable (Nullable, toNullable)
4648
import Node.Buffer (Buffer(), BUFFER(), size)
47-
import Data.Int (round)
49+
import Data.Int (round, toNumber)
4850
import Node.Encoding (Encoding)
4951
import Node.FS (FS, FileDescriptor, ByteCount, FilePosition, BufferLength,
5052
BufferOffset, FileMode, FileFlags, SymlinkType,
@@ -85,12 +87,13 @@ fs ::
8587
, appendFile :: forall a opts. Fn4 FilePath a { | opts } (JSCallback Unit) Unit
8688
, exists :: forall a. Fn2 FilePath (Boolean -> a) Unit
8789
, 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
90+
, read :: Fn6 FileDescriptor Buffer BufferOffset BufferLength (Nullable Number) (JSCallback ByteCount) Unit
91+
, write :: Fn6 FileDescriptor Buffer BufferOffset BufferLength (Nullable Number) (JSCallback ByteCount) Unit
9092
, close :: Fn2 FileDescriptor (JSCallback Unit) Unit
9193
}
9294
fs = unsafeRequireFS
9395

96+
9497
-- | Type synonym for callback functions.
9598
type Callback eff a = Either Error a -> Eff (fs :: FS | eff) Unit
9699

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

315318
-- | Read from a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_read_fd_buffer_offset_length_position_callback)
316319
-- | for details.
320+
-- |
321+
-- | The use of an Int as FilePosition restricts this API to reading
322+
-- | files < 2GB. The call is retained to not break existing code.
323+
-- | fdReadLarge does not have this restriction.
317324
fdRead :: forall eff.
318325
FileDescriptor
319326
-> Buffer
@@ -322,7 +329,22 @@ fdRead :: forall eff.
322329
-> Maybe FilePosition
323330
-> Callback (buffer :: BUFFER | eff) ByteCount
324331
-> 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)
332+
fdRead fd buff off len pos cb =
333+
fdReadLarge fd buff off len (map toNumber pos) cb
334+
335+
-- | Read from a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_read_fd_buffer_offset_length_position_callback)
336+
-- | for details.
337+
-- |
338+
-- | This version takes the file position as a Number. It can read any file Node can.
339+
fdReadLarge :: forall eff.
340+
FileDescriptor
341+
-> Buffer
342+
-> BufferOffset
343+
-> BufferLength
344+
-> Maybe Number
345+
-> Callback (buffer :: BUFFER | eff) ByteCount
346+
-> Eff (buffer :: BUFFER, fs :: FS | eff) Unit
347+
fdReadLarge fd buff off len pos cb = mkEff $ \_ -> runFn6 fs.read fd buff off len (toNullable pos) (handleCallback cb)
326348

327349
-- | Convenience function to fill the whole buffer from the current
328350
-- | file position.
@@ -337,6 +359,10 @@ fdNext fd buff cb = do
337359

338360
-- | Write to a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_write_fd_buffer_offset_length_position_callback)
339361
-- | for details.
362+
-- |
363+
-- | The use of an Int as FilePosition restricts this API to writing
364+
-- | files < 2GB. The call is retained to not break existing code.
365+
-- | fdWriteLarge does not have this restriction.
340366
fdWrite :: forall eff.
341367
FileDescriptor
342368
-> Buffer
@@ -345,7 +371,23 @@ fdWrite :: forall eff.
345371
-> Maybe FilePosition
346372
-> Callback (buffer :: BUFFER | eff) ByteCount
347373
-> 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)
374+
fdWrite fd buff off len pos cb =
375+
fdWriteLarge fd buff off len (map toNumber pos) cb
376+
377+
-- | Write to a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_write_fd_buffer_offset_length_position_callback)
378+
-- | for details.
379+
-- |
380+
-- | This version takes file position as a Number. It can write any file Node can.
381+
fdWriteLarge :: forall eff.
382+
FileDescriptor
383+
-> Buffer
384+
-> BufferOffset
385+
-> BufferLength
386+
-> Maybe Number
387+
-> Callback (buffer :: BUFFER | eff) ByteCount
388+
-> Eff (buffer :: BUFFER, fs :: FS | eff) Unit
389+
fdWriteLarge fd buff off len pos cb = mkEff $ \_ -> runFn6 fs.write fd buff off len (toNullable pos) (handleCallback cb)
390+
349391

350392
-- | Convenience function to append the whole buffer to the current
351393
-- | file position.

src/Node/FS/Sync.purs

+41-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ module Node.FS.Sync
2424
, exists
2525
, fdOpen
2626
, fdRead
27+
, fdReadLarge
2728
, fdNext
2829
, fdWrite
30+
, fdWriteLarge
2931
, fdAppend
3032
, fdFlush
3133
, fdClose
@@ -40,7 +42,7 @@ import Data.DateTime.Instant (fromDateTime, unInstant)
4042
import Data.Function.Uncurried (Fn1, Fn5, Fn3, Fn2,
4143
runFn1, runFn5, runFn3, runFn2)
4244
import Data.Nullable (Nullable(), toNullable)
43-
import Data.Int (round)
45+
import Data.Int (round, toNumber)
4446
import Data.Maybe (Maybe(..))
4547
import Node.Buffer (Buffer(), BUFFER(), size)
4648
import Node.Encoding (Encoding)
@@ -73,8 +75,8 @@ fs ::
7375
, appendFileSync :: forall a opts. Fn3 FilePath a { | opts } Unit
7476
, existsSync :: FilePath -> Boolean
7577
, 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
78+
, readSync :: Fn5 FileDescriptor Buffer BufferOffset BufferLength (Nullable Number) ByteCount
79+
, writeSync :: Fn5 FileDescriptor Buffer BufferOffset BufferLength (Nullable Number) ByteCount
7880
, fsyncSync :: Fn1 FileDescriptor Unit
7981
, closeSync :: Fn1 FileDescriptor Unit
8082
}
@@ -276,6 +278,10 @@ fdOpen file flags mode = mkEff $ \_ ->
276278

277279
-- | Read from a file synchronously. See the [Node documentation](http://nodejs.org/api/fs.html#fs_fs_readsync_fd_buffer_offset_length_position)
278280
-- | for details.
281+
-- |
282+
-- | The use of an Int as FilePosition restricts this API to reading
283+
-- | files < 2GB. The call is retained to not break existing code.
284+
-- | fdReadLarge does not have this restriction.
279285
fdRead :: forall eff.
280286
FileDescriptor
281287
-> Buffer
@@ -284,6 +290,20 @@ fdRead :: forall eff.
284290
-> Maybe FilePosition
285291
-> Eff (buffer :: BUFFER, exception :: EXCEPTION, fs :: FS | eff) ByteCount
286292
fdRead fd buff off len pos =
293+
fdReadLarge fd buff off len (map toNumber pos)
294+
295+
-- | Read from a file synchronously. See the [Node documentation](http://nodejs.org/api/fs.html#fs_fs_readsync_fd_buffer_offset_length_position)
296+
-- | for details.
297+
-- |
298+
-- | This version takes the file position as a Number. It can read any file Node can.
299+
fdReadLarge :: forall eff.
300+
FileDescriptor
301+
-> Buffer
302+
-> BufferOffset
303+
-> BufferLength
304+
-> Maybe Number
305+
-> Eff (buffer :: BUFFER, exception :: EXCEPTION, fs :: FS | eff) ByteCount
306+
fdReadLarge fd buff off len pos =
287307
mkEff $ \_ -> runFn5 fs.readSync fd buff off len (toNullable pos)
288308

289309
-- | Convenience function to fill the whole buffer from the current
@@ -298,6 +318,10 @@ fdNext fd buff = do
298318

299319
-- | Write to a file synchronously. See the [Node documentation](http://nodejs.org/api/fs.html#fs_fs_writesync_fd_buffer_offset_length_position)
300320
-- | for details.
321+
-- |
322+
-- | The use of an Int as FilePosition restricts this API to writing
323+
-- | files < 2GB. The call is retained to not break existing code.
324+
-- | fdWriteLarge does not have this restriction.
301325
fdWrite :: forall eff.
302326
FileDescriptor
303327
-> Buffer
@@ -306,6 +330,20 @@ fdWrite :: forall eff.
306330
-> Maybe FilePosition
307331
-> Eff (buffer :: BUFFER, exception :: EXCEPTION, fs :: FS | eff) ByteCount
308332
fdWrite fd buff off len pos =
333+
fdWriteLarge fd buff off len (map toNumber pos)
334+
335+
-- | Write to a file synchronously. See the [Node documentation](http://nodejs.org/api/fs.html#fs_fs_writesync_fd_buffer_offset_length_position)
336+
-- | for details.
337+
-- |
338+
-- | This version takes the file position as a Number. It can write any file Node can.
339+
fdWriteLarge :: forall eff.
340+
FileDescriptor
341+
-> Buffer
342+
-> BufferOffset
343+
-> BufferLength
344+
-> Maybe Number
345+
-> Eff (buffer :: BUFFER, exception :: EXCEPTION, fs :: FS | eff) ByteCount
346+
fdWriteLarge fd buff off len pos =
309347
mkEff $ \_ -> runFn5 fs.writeSync fd buff off len (toNullable pos)
310348

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

0 commit comments

Comments
 (0)