Skip to content

Commit b0a93e2

Browse files
Fully implement bindings; update deps (#16)
* Add dependency on node-event-emitter * Fully implement net bindings * Make tests compile * Add changelog entry
1 parent ea853ca commit b0a93e2

File tree

14 files changed

+879
-885
lines changed

14 files changed

+879
-885
lines changed

CHANGELOG.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,48 @@ Notable changes to this project are documented in this file. The format is based
66

77
Breaking changes:
88
- Bumped `node-buffer` and `node-fs` to v9.0.0 and latest release (#12 by @JordanMartinez)
9+
- Update event handling API to use EventHandle-style API (#16 by @JordanMartinez)
10+
11+
Before:
12+
```purs
13+
foo = do
14+
Socket.onClose socket \b -> doSomething
15+
```
16+
17+
After:
18+
```purs
19+
foo = do
20+
socket # on_ Socket.closeH \b -> doSomething
21+
```
22+
- Renamed `isIP` to `isIP'` so that `isIP` returns an ADT
23+
24+
Previously, `Node.Net.isIP` returned an integer that could be one of two values,
25+
4 or 6. This function was renamed to `isIP'`, so that `isIP` could return
26+
an algebraic data type value representing that 4 or 6.
27+
- Distinguish between an IPC and TCP socket/server via phantom type (#16 by @JordanMartinez)
28+
29+
Some functions (e.g. `Node.Net.Server.address`) return different values depending on whether
30+
the provided server is an IPC or TCP server. Similarly, some functions
31+
only work on IPC sockets/server rather than TCP ones.
32+
33+
Rather than forcing the end-user to handle a case that's not possible,
34+
a phantom type was added to `Socket` and `Server` to indicate which
35+
kind of socket/server it is.
36+
37+
If a function was named `foo` before and it worked differently depending on
38+
whether the socket/server was IPC or TCP, it is now suffixed with `Ipc` or `Tcp`
39+
to distinguish which it works on.
940

1041
New features:
42+
- Added bindings for the `BlockList` class (#16 by @JordanMartinez)
43+
- Added bindings for the `SocketAddress` class (#16 by @JordanMartinez)
1144

1245
Bugfixes:
1346

1447
Other improvements:
1548
- Bump CI node to v18 (#12 by @JordanMartinez)
1649
- Enforce formatting in CI via `purs-tidy` (#12 by @JordanMartinez)
50+
- Updated all FFI to use uncurried functions (#16 by @JordanMartinez)
1751

1852
## [v4.0.0](https://github.com/purescript-node/purescript-node-net/releases/tag/v4.0.0) - 2022-04-29
1953

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"purescript-nullable": "^6.0.0",
2323
"purescript-options": "^7.0.0",
2424
"purescript-prelude": "^6.0.0",
25-
"purescript-transformers": "^6.0.0"
25+
"purescript-transformers": "^6.0.0",
26+
"purescript-node-event-emitter": "https://github.com/purescript-node/purescript-node-event-emitter.git#^3.0.0"
2627
},
2728
"devDependencies": {
2829
"purescript-console": "^6.0.0"

src/Node/Net.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
import net from "net";
2-
export const isIP = net.isIP;
3-
export const isIPv4 = net.isIPv4;
4-
export const isIPv6 = net.isIPv6;
1+
export { isIP as isIPImpl, isIPv4, isIPv6 } from "net";

src/Node/Net.purs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
module Node.Net
22
( isIP
3+
, isIP'
34
, isIPv4
45
, isIPv6
56
) where
67

8+
import Data.Maybe (Maybe(..))
9+
import Node.Net.Types (IpFamily(..))
10+
11+
isIP :: String -> Maybe IpFamily
12+
isIP s = case isIP' s of
13+
4 -> Just IPv4
14+
6 -> Just IPv6
15+
_ -> Nothing
16+
717
-- | Returns `4` if the `String` is a valid IPv4 address, `6` if the `String`
818
-- | is a valid IPv6 address, and `0` otherwise.
9-
foreign import isIP :: String -> Int
19+
isIP' :: String -> Int
20+
isIP' = isIPImpl
21+
22+
foreign import isIPImpl :: String -> Int
1023

1124
-- | Returns `true` if the `String` is a valid IPv4 address,
1225
-- | and `false` otherwise.

src/Node/Net/BlockList.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const addAddressImpl = (bl, addr, ty) => bl.addAddress(addr, ty);
2+
export const addRangeImpl = (bl, start, end, ty) => bl.addRange(start, end, ty);
3+
export const addSubnetImpl = (bl, net, prefix, ty) => bl.addSubnet(net, prefix, ty);
4+
export const checkImpl = (bl, addr, ty) => bl.check(addr, ty);
5+
export const rulesImpl = (bl) => bl.rules;

src/Node/Net/BlockList.purs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
module Node.Net.BlockList
2+
( addAddressAddr
3+
, addAddressStr
4+
, addRangeStrStr
5+
, addRangeStrAddr
6+
, addRangeAddrStr
7+
, addRangeAddrAddr
8+
, addSubnetStr
9+
, addSubnetAddr
10+
, checkStr
11+
, checkAddr
12+
, rules
13+
) where
14+
15+
import Prelude
16+
17+
import Effect (Effect)
18+
import Effect.Uncurried (EffectFn1, EffectFn3, EffectFn4, runEffectFn1, runEffectFn3, runEffectFn4)
19+
import Node.Net.Types (BlockList, IpFamily, SocketAddress, toNodeIpFamily)
20+
21+
foreign import addAddressImpl :: forall a. EffectFn3 (BlockList) a (String) (Unit)
22+
23+
addAddressAddr :: forall ipFamily. BlockList -> SocketAddress ipFamily -> IpFamily -> Effect Unit
24+
addAddressAddr bl a ty = runEffectFn3 addAddressImpl bl a (toNodeIpFamily ty)
25+
26+
addAddressStr :: BlockList -> String -> IpFamily -> Effect Unit
27+
addAddressStr bl a ty = runEffectFn3 addAddressImpl bl a (toNodeIpFamily ty)
28+
29+
foreign import addRangeImpl :: forall a b. EffectFn4 (BlockList) a b (IpFamily) (Unit)
30+
31+
addRangeStrStr :: BlockList -> String -> String -> IpFamily -> Effect Unit
32+
addRangeStrStr bl start end ty = runEffectFn4 addRangeImpl bl start end ty
33+
34+
addRangeStrAddr :: forall ipFamily. BlockList -> String -> SocketAddress ipFamily -> IpFamily -> Effect Unit
35+
addRangeStrAddr bl start end ty = runEffectFn4 addRangeImpl bl start end ty
36+
37+
addRangeAddrStr :: forall ipFamily. BlockList -> String -> SocketAddress ipFamily -> IpFamily -> Effect Unit
38+
addRangeAddrStr bl start end ty = runEffectFn4 addRangeImpl bl start end ty
39+
40+
addRangeAddrAddr :: forall ipFamilyStart ipFamilyEnd. BlockList -> SocketAddress ipFamilyStart -> SocketAddress ipFamilyEnd -> IpFamily -> Effect Unit
41+
addRangeAddrAddr bl start end ty = runEffectFn4 addRangeImpl bl start end ty
42+
43+
foreign import addSubnetImpl :: forall a. EffectFn4 (BlockList) a (Int) (IpFamily) (Unit)
44+
45+
addSubnetStr :: BlockList -> String -> Int -> IpFamily -> Effect Unit
46+
addSubnetStr bl net prefix ty = runEffectFn4 addSubnetImpl bl net prefix ty
47+
48+
addSubnetAddr :: forall ipFamily. BlockList -> SocketAddress ipFamily -> Int -> IpFamily -> Effect Unit
49+
addSubnetAddr bl net prefix ty = runEffectFn4 addSubnetImpl bl net prefix ty
50+
51+
foreign import checkImpl :: forall a. EffectFn3 (BlockList) a (IpFamily) (Boolean)
52+
53+
checkStr :: BlockList -> String -> IpFamily -> Effect Boolean
54+
checkStr bl addresss ty = runEffectFn3 checkImpl bl addresss ty
55+
56+
checkAddr :: forall ipFamily. BlockList -> SocketAddress ipFamily -> IpFamily -> Effect Boolean
57+
checkAddr bl addresss ty = runEffectFn3 checkImpl bl addresss ty
58+
59+
rules :: BlockList -> Effect (Array String)
60+
rules bl = runEffectFn1 rulesImpl bl
61+
62+
foreign import rulesImpl :: EffectFn1 (BlockList) ((Array String))

src/Node/Net/Server.js

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
1-
import net from "net";
2-
3-
export function addressImpl(server) {
4-
return server.address();
5-
}
6-
7-
export function closeImpl(server, callback) {
8-
server.close(callback);
9-
}
10-
11-
export const createServerImpl = net.createServer;
12-
13-
export function getConnectionsImpl(server, callback) {
14-
server.getConnections(callback);
15-
}
16-
17-
export function listenImpl(server, options, callback) {
18-
server.listen(options, callback);
19-
}
20-
21-
export function listeningImpl(socket) {
22-
return socket.listening;
23-
}
24-
25-
export function onImpl(event, server, callback) {
26-
server.on(event, callback);
27-
}
1+
import net from "node:net";
2+
3+
export const newServerImpl = () => new net.Server();
4+
export const newServerOptionsImpl = (o) => new net.Server(o);
5+
6+
export const addressTcpImpl = (s) => s.address();
7+
export const addressIpcImpl = (s) => s.address();
8+
export const closeImpl = (s) => s.close();
9+
export const getConnectionsImpl = (s, cb) => s.getConnections(cb);
10+
export const listenImpl = (s, o) => s.listen(o);
11+
export const listeningImpl = (s) => s.listening;
12+
export const maxConnectionsImpl = (s) => s.maxConnections;
13+
export const refImpl = (s) => s.ref();
14+
export const unrefImpl = (s) => s.unref();

0 commit comments

Comments
 (0)