Skip to content

Commit be0a346

Browse files
Allow all options in read/write streams interface (#77)
* Allow all options in read/write streams interface * Fix eslint issue
1 parent 0020e02 commit be0a346

File tree

4 files changed

+151
-112
lines changed

4 files changed

+151
-112
lines changed

.eslintrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"parserOptions": {
3-
"ecmaVersion": 6,
3+
"ecmaVersion": 9,
44
"sourceType": "module"
55
},
66
"extends": "eslint:recommended",

CHANGELOG.md

+18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ New features:
2222
getGid :: Stats -> Number
2323
getGid s = Stats.gid s
2424
```
25+
- Update `[fd]createReadStream`/`[fd]createWriteStream` to allow more options (#77 by @JordanMartinez)
26+
27+
| Removes... | ...in favor of |
28+
| - | - |
29+
| `createReadStreamWith` | `createReadStream'` |
30+
| `fdCreateReadStreamWith` | `fdCreateReadStream'` |
31+
| `createWriteStreamWith` | `createWriteStream'` |
32+
| `fdCreateWriteStreamWith` | `fdCreateWriteStream'` |
33+
34+
In the new APIs, all options are exposed and may require converting
35+
PureScript values to JavaScript ones.
36+
```purs
37+
filePath # createWriteStream'
38+
{ flags: fileFlagsToNode R
39+
, encoding: encodingToNode UTF8
40+
, mode: permsToInt Perms.all
41+
}
42+
```
2543

2644
Bugfixes:
2745

src/Node/FS/Stream.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
export {
2-
createReadStream as createReadStreamImpl,
3-
createWriteStream as createWriteStreamImpl
4-
} from "fs";
1+
import fs from "node:fs";
2+
3+
export const createReadStreamImpl = (path) => fs.createReadStream(path);
4+
export const createReadStreamOptsImpl = (path, opts) => fs.createReadStream(path, opts);
5+
export const fdCreateReadStreamImpl = (fd) => fs.createReadStream(null, { fd });
6+
export const fdCreateReadStreamOptsImpl = (fd, opts) => fs.createReadStream(null, { ...opts, fd});
7+
8+
export const createWriteStreamImpl = (path) => fs.createWriteStream(path);
9+
export const createWriteStreamOptsImpl = (path, opts) => fs.createWriteStream(path, opts);
10+
export const fdCreateWriteStreamImpl = (fd) => fs.createWriteStream(null, { fd });
11+
export const fdCreateWriteStreamOptsImpl = (fd, opts) => fs.createWriteStream(null, { ...opts, fd});

src/Node/FS/Stream.purs

+121-107
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,152 @@
11
module Node.FS.Stream
22
( createWriteStream
3-
, fdCreateWriteStream
43
, WriteStreamOptions
5-
, defaultWriteStreamOptions
6-
, createWriteStreamWith
7-
, fdCreateWriteStreamWith
4+
, createWriteStream'
5+
, fdCreateWriteStream
6+
, fdCreateWriteStream'
87
, createReadStream
9-
, fdCreateReadStream
108
, ReadStreamOptions
11-
, defaultReadStreamOptions
12-
, createReadStreamWith
13-
, fdCreateReadStreamWith
9+
, createReadStream'
10+
, fdCreateReadStream
11+
, fdCreateReadStream'
1412
) where
1513

16-
import Prelude
17-
18-
import Data.Nullable (Nullable, notNull, null)
1914
import Effect (Effect)
20-
import Effect.Uncurried (EffectFn2, runEffectFn2)
15+
import Effect.Uncurried (EffectFn1, EffectFn2, runEffectFn1, runEffectFn2)
2116
import Node.FS (FileDescriptor)
22-
import Node.FS.Constants (FileFlags(..), fileFlagsToNode)
23-
import Node.FS.Perms (Perms)
24-
import Node.FS.Perms as Perms
2517
import Node.Path (FilePath)
2618
import Node.Stream (Readable, Writable)
27-
28-
foreign import createReadStreamImpl :: forall opts. EffectFn2 (Nullable FilePath) { | opts } (Readable ())
29-
foreign import createWriteStreamImpl :: forall opts. EffectFn2 (Nullable FilePath) { | opts } (Writable ())
30-
31-
readWrite :: Perms
32-
readWrite = Perms.mkPerms rw rw rw
33-
where
34-
rw = Perms.read + Perms.write
19+
import Prim.Row as Row
3520

3621
-- | Create a Writable stream which writes data to the specified file, using
3722
-- | the default options.
38-
createWriteStream
39-
:: FilePath
23+
createWriteStream :: FilePath -> Effect (Writable ())
24+
createWriteStream f = runEffectFn1 createWriteStreamImpl f
25+
26+
foreign import createWriteStreamImpl :: EffectFn1 (FilePath) (Writable ())
27+
28+
type WriteStreamOptions =
29+
( flags :: String
30+
, encoding :: String
31+
, mode :: Int
32+
, autoClose :: Boolean
33+
, emitClose :: Boolean
34+
, start :: Int
35+
)
36+
37+
-- | Create a Writable stream which writes data to the specified file.
38+
-- | Unused options should not be specified. Some options
39+
-- | (e.g. `flags`, `encoding`, and `mode`) should convert their
40+
-- | PureScript values to the corresponding JavaScript ones:
41+
-- | ```
42+
-- | filePath # createWriteStream'
43+
-- | { flags: fileFlagsToNode R
44+
-- | , encoding: encodingToNode UTF8
45+
-- | , mode: permsToInt Perms.all
46+
-- | }
47+
-- | ```
48+
createWriteStream'
49+
:: forall r trash
50+
. Row.Union r trash WriteStreamOptions
51+
=> FilePath
52+
-> { | r }
4053
-> Effect (Writable ())
41-
createWriteStream = createWriteStreamWith defaultWriteStreamOptions
54+
createWriteStream' f opts = runEffectFn2 createWriteStreamOptsImpl f opts
55+
56+
foreign import createWriteStreamOptsImpl :: forall r. EffectFn2 (FilePath) ({ | r }) ((Writable ()))
4257

4358
-- | Create a Writable stream which writes data to the specified file
4459
-- | descriptor, using the default options.
45-
fdCreateWriteStream
46-
:: FileDescriptor
60+
fdCreateWriteStream :: FileDescriptor -> Effect (Writable ())
61+
fdCreateWriteStream f = runEffectFn1 fdCreateWriteStreamImpl f
62+
63+
foreign import fdCreateWriteStreamImpl :: EffectFn1 (FileDescriptor) (Writable ())
64+
65+
-- | Create a Writable stream which writes data to the specified file descriptor.
66+
-- | Unused options should not be specified. Some options
67+
-- | (e.g. `flags`, `encoding`, and `mode`) should convert their
68+
-- | PureScript values to the corresponding JavaScript ones:
69+
-- | ```
70+
-- | filePath # fdCreateWriteStream'
71+
-- | { flags: fileFlagsToNode R
72+
-- | , encoding: encodingToNode UTF8
73+
-- | , mode: permsToInt Perms.all
74+
-- | }
75+
-- | ```
76+
fdCreateWriteStream'
77+
:: forall r trash
78+
. Row.Union r trash WriteStreamOptions
79+
=> FileDescriptor
80+
-> { | r }
4781
-> Effect (Writable ())
48-
fdCreateWriteStream = fdCreateWriteStreamWith defaultWriteStreamOptions
82+
fdCreateWriteStream' f opts = runEffectFn2 fdCreateWriteStreamOptsImpl f opts
4983

50-
type WriteStreamOptions =
51-
{ flags :: FileFlags
52-
, perms :: Perms
53-
}
54-
55-
defaultWriteStreamOptions :: WriteStreamOptions
56-
defaultWriteStreamOptions =
57-
{ flags: W
58-
, perms: readWrite
59-
}
60-
61-
-- | Like `createWriteStream`, but allows you to pass options.
62-
createWriteStreamWith
63-
:: WriteStreamOptions
64-
-> FilePath
65-
-> Effect (Writable ())
66-
createWriteStreamWith opts file = runEffectFn2
67-
createWriteStreamImpl
68-
(notNull file)
69-
{ mode: Perms.permsToInt opts.perms
70-
, flags: fileFlagsToNode opts.flags
71-
}
72-
73-
-- | Like `fdCreateWriteStream`, but allows you to pass options.
74-
fdCreateWriteStreamWith
75-
:: WriteStreamOptions
76-
-> FileDescriptor
77-
-> Effect (Writable ())
78-
fdCreateWriteStreamWith opts fd = runEffectFn2
79-
createWriteStreamImpl
80-
null
81-
{ fd
82-
, mode: Perms.permsToInt opts.perms
83-
, flags: fileFlagsToNode opts.flags
84-
}
84+
foreign import fdCreateWriteStreamOptsImpl :: forall r. EffectFn2 (FileDescriptor) ({ | r }) (Writable ())
8585

8686
-- | Create a Readable stream which reads data to the specified file, using
8787
-- | the default options.
88-
createReadStream
89-
:: FilePath
90-
-> Effect (Readable ())
91-
createReadStream = createReadStreamWith defaultReadStreamOptions
88+
createReadStream :: FilePath -> Effect (Readable ())
89+
createReadStream p = runEffectFn1 createReadStreamImpl p
9290

93-
-- | Create a Readable stream which reads data to the specified file
94-
-- | descriptor, using the default options.
95-
fdCreateReadStream
96-
:: FileDescriptor
97-
-> Effect (Readable ())
98-
fdCreateReadStream = fdCreateReadStreamWith defaultReadStreamOptions
91+
foreign import createReadStreamImpl :: EffectFn1 (FilePath) (Readable ())
9992

10093
type ReadStreamOptions =
101-
{ flags :: FileFlags
102-
, perms :: Perms
94+
( flags :: String
95+
, encoding :: String
96+
, mode :: Int
10397
, autoClose :: Boolean
104-
}
105-
106-
defaultReadStreamOptions :: ReadStreamOptions
107-
defaultReadStreamOptions =
108-
{ flags: R
109-
, perms: readWrite
110-
, autoClose: true
111-
}
98+
, emitClose :: Boolean
99+
, start :: Int
100+
, end :: Int
101+
, highWaterMark :: Int
102+
)
112103

113104
-- | Create a Readable stream which reads data from the specified file.
114-
createReadStreamWith
115-
:: ReadStreamOptions
116-
-> FilePath
105+
-- | Unused options should not be specified. Some options
106+
-- | (e.g. `flags`, `encoding`, and `mode`) should convert their
107+
-- | PureScript values to the corresponding JavaScript ones:
108+
-- | ```
109+
-- | filePath # createReadStream'
110+
-- | { flags: fileFlagsToNode R
111+
-- | , encoding: encodingToNode UTF8
112+
-- | , mode: permsToInt Perms.all
113+
-- | }
114+
-- | ```
115+
createReadStream'
116+
:: forall r trash
117+
. Row.Union r trash ReadStreamOptions
118+
=> FilePath
119+
-> { | r }
117120
-> Effect (Readable ())
118-
createReadStreamWith opts file = runEffectFn2
119-
createReadStreamImpl
120-
(notNull file)
121-
{ mode: Perms.permsToInt opts.perms
122-
, flags: fileFlagsToNode opts.flags
123-
, autoClose: opts.autoClose
124-
}
125-
126-
-- | Create a Readable stream which reads data from the specified file descriptor.
127-
fdCreateReadStreamWith
128-
:: ReadStreamOptions
129-
-> FileDescriptor
121+
createReadStream' path opts = runEffectFn2 createReadStreamOptsImpl path opts
122+
123+
foreign import createReadStreamOptsImpl :: forall r. EffectFn2 (FilePath) ({ | r }) ((Readable ()))
124+
125+
-- | Create a Readable stream which reads data to the specified file
126+
-- | descriptor, using the default options.
127+
fdCreateReadStream :: FileDescriptor -> Effect (Readable ())
128+
fdCreateReadStream f = runEffectFn1 fdCreateReadStreamImpl f
129+
130+
foreign import fdCreateReadStreamImpl :: EffectFn1 (FileDescriptor) (Readable ())
131+
132+
-- | Create a Readable stream which reads data to the specified file descriptor.
133+
-- | Unused options should not be specified. Some options
134+
-- | (e.g. `flags`, `encoding`, and `mode`) should convert their
135+
-- | PureScript values to the corresponding JavaScript ones:
136+
-- | ```
137+
-- | filePath # fdCreateReadStream'
138+
-- | { flags: fileFlagsToNode R
139+
-- | , encoding: encodingToNode UTF8
140+
-- | , mode: permsToInt Perms.all
141+
-- | }
142+
-- | ```
143+
fdCreateReadStream'
144+
:: forall r trash
145+
. Row.Union r trash ReadStreamOptions
146+
=> FileDescriptor
147+
-> { | r }
130148
-> Effect (Readable ())
131-
fdCreateReadStreamWith opts fd = runEffectFn2
132-
createReadStreamImpl
133-
null
134-
{ fd
135-
, mode: Perms.permsToInt opts.perms
136-
, flags: fileFlagsToNode opts.flags
137-
, autoClose: opts.autoClose
138-
}
149+
fdCreateReadStream' f opts = runEffectFn2 fdCreateReadStreamOptsImpl f opts
150+
151+
foreign import fdCreateReadStreamOptsImpl :: forall r. EffectFn2 (FileDescriptor) ({ | r }) ((Readable ()))
152+

0 commit comments

Comments
 (0)