|
| 1 | +module Node.FS.Stream |
| 2 | + ( createWriteStream |
| 3 | + , fdCreateWriteStream |
| 4 | + , WriteStreamOptions() |
| 5 | + , defaultWriteStreamOptions |
| 6 | + , createWriteStreamWith |
| 7 | + , fdCreateWriteStreamWith |
| 8 | + , createReadStream |
| 9 | + , fdCreateReadStream |
| 10 | + , ReadStreamOptions() |
| 11 | + , defaultReadStreamOptions |
| 12 | + , createReadStreamWith |
| 13 | + , fdCreateReadStreamWith |
| 14 | + ) where |
| 15 | + |
| 16 | +import Prelude |
| 17 | +import Data.Maybe (Maybe(..)) |
| 18 | +import Data.Function |
| 19 | +import Data.Nullable |
| 20 | +import Control.Monad.Eff |
| 21 | +import Node.Stream (Readable(), Writable()) |
| 22 | +import Node.Path (FilePath()) |
| 23 | + |
| 24 | +import Node.FS |
| 25 | +import Node.FS.Perms (Perms()) |
| 26 | +import Node.FS.Perms as Perms |
| 27 | +import Node.FS.Internal |
| 28 | + |
| 29 | +fs :: |
| 30 | + { createReadStream :: forall eff opts. Fn2 (Nullable FilePath) { | opts } (Readable () (fs :: FS | eff)) |
| 31 | + , createWriteStream :: forall eff opts. Fn2 (Nullable FilePath) { | opts } (Writable () (fs :: FS | eff)) |
| 32 | + } |
| 33 | +fs = unsafeRequireFS |
| 34 | + |
| 35 | +readWrite :: Perms |
| 36 | +readWrite = Perms.mkPerms rw rw rw |
| 37 | + where |
| 38 | + rw = Perms.read + Perms.write |
| 39 | + |
| 40 | +null :: forall a. Nullable a |
| 41 | +null = toNullable Nothing |
| 42 | + |
| 43 | +nonnull :: forall a. a -> Nullable a |
| 44 | +nonnull = toNullable <<< Just |
| 45 | + |
| 46 | +-- | Create a Writable stream which writes data to the specified file, using |
| 47 | +-- | the default options. |
| 48 | +createWriteStream :: forall eff. |
| 49 | + FilePath |
| 50 | + -> Eff (fs :: FS | eff) (Writable () (fs :: FS | eff)) |
| 51 | +createWriteStream = createWriteStreamWith defaultWriteStreamOptions |
| 52 | + |
| 53 | +-- | Create a Writable stream which writes data to the specified file |
| 54 | +-- | descriptor, using the default options. |
| 55 | +fdCreateWriteStream :: forall eff. |
| 56 | + FileDescriptor |
| 57 | + -> Eff (fs :: FS | eff) (Writable () (fs :: FS | eff)) |
| 58 | +fdCreateWriteStream = fdCreateWriteStreamWith defaultWriteStreamOptions |
| 59 | + |
| 60 | +type WriteStreamOptions = |
| 61 | + { flags :: FileFlags |
| 62 | + , perms :: Perms |
| 63 | + } |
| 64 | + |
| 65 | +defaultWriteStreamOptions :: WriteStreamOptions |
| 66 | +defaultWriteStreamOptions = |
| 67 | + { flags: W |
| 68 | + , perms: readWrite |
| 69 | + } |
| 70 | + |
| 71 | +-- | Like `createWriteStream`, but allows you to pass options. |
| 72 | +createWriteStreamWith :: forall eff. |
| 73 | + WriteStreamOptions |
| 74 | + -> FilePath |
| 75 | + -> Eff (fs :: FS | eff) (Writable () (fs :: FS | eff)) |
| 76 | +createWriteStreamWith opts file = mkEff $ \_ -> runFn2 |
| 77 | + fs.createWriteStream (nonnull file) |
| 78 | + { mode: Perms.permsToInt opts.perms |
| 79 | + , flags: fileFlagsToNode opts.flags |
| 80 | + } |
| 81 | + |
| 82 | +-- | Like `fdCreateWriteStream`, but allows you to pass options. |
| 83 | +fdCreateWriteStreamWith :: forall eff. |
| 84 | + WriteStreamOptions |
| 85 | + -> FileDescriptor |
| 86 | + -> Eff (fs :: FS | eff) (Writable () (fs :: FS | eff)) |
| 87 | +fdCreateWriteStreamWith opts fd = mkEff $ \_ -> runFn2 |
| 88 | + fs.createWriteStream null |
| 89 | + { fd |
| 90 | + , mode: Perms.permsToInt opts.perms |
| 91 | + , flags: fileFlagsToNode opts.flags |
| 92 | + } |
| 93 | + |
| 94 | +-- | Create a Readable stream which reads data to the specified file, using |
| 95 | +-- | the default options. |
| 96 | +createReadStream :: forall eff. |
| 97 | + FilePath |
| 98 | + -> Eff (fs :: FS | eff) (Readable () (fs :: FS | eff)) |
| 99 | +createReadStream = createReadStreamWith defaultReadStreamOptions |
| 100 | + |
| 101 | +-- | Create a Readable stream which reads data to the specified file |
| 102 | +-- | descriptor, using the default options. |
| 103 | +fdCreateReadStream :: forall eff. |
| 104 | + FileDescriptor |
| 105 | + -> Eff (fs :: FS | eff) (Readable () (fs :: FS | eff)) |
| 106 | +fdCreateReadStream = fdCreateReadStreamWith defaultReadStreamOptions |
| 107 | + |
| 108 | +type ReadStreamOptions = |
| 109 | + { flags :: FileFlags |
| 110 | + , perms :: Perms |
| 111 | + , autoClose :: Boolean |
| 112 | + } |
| 113 | + |
| 114 | +defaultReadStreamOptions :: ReadStreamOptions |
| 115 | +defaultReadStreamOptions = |
| 116 | + { flags: R |
| 117 | + , perms: readWrite |
| 118 | + , autoClose: true |
| 119 | + } |
| 120 | + |
| 121 | +-- | Create a Readable stream which reads data from the specified file. |
| 122 | +createReadStreamWith :: forall eff. |
| 123 | + ReadStreamOptions |
| 124 | + -> FilePath |
| 125 | + -> Eff (fs :: FS | eff) (Readable () (fs :: FS | eff)) |
| 126 | +createReadStreamWith opts file = mkEff $ \_ -> runFn2 |
| 127 | + fs.createReadStream (nonnull file) |
| 128 | + { mode: Perms.permsToInt opts.perms |
| 129 | + , flags: fileFlagsToNode opts.flags |
| 130 | + , autoClose: opts.autoClose |
| 131 | + } |
| 132 | + |
| 133 | +-- | Create a Readable stream which reads data from the specified file descriptor. |
| 134 | +fdCreateReadStreamWith :: forall eff. |
| 135 | + ReadStreamOptions |
| 136 | + -> FileDescriptor |
| 137 | + -> Eff (fs :: FS | eff) (Readable () (fs :: FS | eff)) |
| 138 | +fdCreateReadStreamWith opts fd = mkEff $ \_ -> runFn2 |
| 139 | + fs.createReadStream null |
| 140 | + { fd |
| 141 | + , mode: Perms.permsToInt opts.perms |
| 142 | + , flags: fileFlagsToNode opts.flags |
| 143 | + , autoClose: opts.autoClose |
| 144 | + } |
0 commit comments