Skip to content

Fully implement bindings; update deps #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,48 @@ Notable changes to this project are documented in this file. The format is based

Breaking changes:
- Bumped `node-buffer` and `node-fs` to v9.0.0 and latest release (#12 by @JordanMartinez)
- Update event handling API to use EventHandle-style API (#16 by @JordanMartinez)

Before:
```purs
foo = do
Socket.onClose socket \b -> doSomething
```

After:
```purs
foo = do
socket # on_ Socket.closeH \b -> doSomething
```
- Renamed `isIP` to `isIP'` so that `isIP` returns an ADT

Previously, `Node.Net.isIP` returned an integer that could be one of two values,
4 or 6. This function was renamed to `isIP'`, so that `isIP` could return
an algebraic data type value representing that 4 or 6.
- Distinguish between an IPC and TCP socket/server via phantom type (#16 by @JordanMartinez)

Some functions (e.g. `Node.Net.Server.address`) return different values depending on whether
the provided server is an IPC or TCP server. Similarly, some functions
only work on IPC sockets/server rather than TCP ones.

Rather than forcing the end-user to handle a case that's not possible,
a phantom type was added to `Socket` and `Server` to indicate which
kind of socket/server it is.

If a function was named `foo` before and it worked differently depending on
whether the socket/server was IPC or TCP, it is now suffixed with `Ipc` or `Tcp`
to distinguish which it works on.

New features:
- Added bindings for the `BlockList` class (#16 by @JordanMartinez)
- Added bindings for the `SocketAddress` class (#16 by @JordanMartinez)

Bugfixes:

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

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

Expand Down
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"purescript-nullable": "^6.0.0",
"purescript-options": "^7.0.0",
"purescript-prelude": "^6.0.0",
"purescript-transformers": "^6.0.0"
"purescript-transformers": "^6.0.0",
"purescript-node-event-emitter": "https://github.com/purescript-node/purescript-node-event-emitter.git#^3.0.0"
},
"devDependencies": {
"purescript-console": "^6.0.0"
Expand Down
5 changes: 1 addition & 4 deletions src/Node/Net.js
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
import net from "net";
export const isIP = net.isIP;
export const isIPv4 = net.isIPv4;
export const isIPv6 = net.isIPv6;
export { isIP as isIPImpl, isIPv4, isIPv6 } from "net";
15 changes: 14 additions & 1 deletion src/Node/Net.purs
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
module Node.Net
( isIP
, isIP'
, isIPv4
, isIPv6
) where

import Data.Maybe (Maybe(..))
import Node.Net.Types (IpFamily(..))

isIP :: String -> Maybe IpFamily
isIP s = case isIP' s of
4 -> Just IPv4
6 -> Just IPv6
_ -> Nothing

-- | Returns `4` if the `String` is a valid IPv4 address, `6` if the `String`
-- | is a valid IPv6 address, and `0` otherwise.
foreign import isIP :: String -> Int
isIP' :: String -> Int
isIP' = isIPImpl

foreign import isIPImpl :: String -> Int

-- | Returns `true` if the `String` is a valid IPv4 address,
-- | and `false` otherwise.
Expand Down
5 changes: 5 additions & 0 deletions src/Node/Net/BlockList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const addAddressImpl = (bl, addr, ty) => bl.addAddress(addr, ty);
export const addRangeImpl = (bl, start, end, ty) => bl.addRange(start, end, ty);
export const addSubnetImpl = (bl, net, prefix, ty) => bl.addSubnet(net, prefix, ty);
export const checkImpl = (bl, addr, ty) => bl.check(addr, ty);
export const rulesImpl = (bl) => bl.rules;
62 changes: 62 additions & 0 deletions src/Node/Net/BlockList.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module Node.Net.BlockList
( addAddressAddr
, addAddressStr
, addRangeStrStr
, addRangeStrAddr
, addRangeAddrStr
, addRangeAddrAddr
, addSubnetStr
, addSubnetAddr
, checkStr
, checkAddr
, rules
) where

import Prelude

import Effect (Effect)
import Effect.Uncurried (EffectFn1, EffectFn3, EffectFn4, runEffectFn1, runEffectFn3, runEffectFn4)
import Node.Net.Types (BlockList, IpFamily, SocketAddress, toNodeIpFamily)

foreign import addAddressImpl :: forall a. EffectFn3 (BlockList) a (String) (Unit)

addAddressAddr :: forall ipFamily. BlockList -> SocketAddress ipFamily -> IpFamily -> Effect Unit
addAddressAddr bl a ty = runEffectFn3 addAddressImpl bl a (toNodeIpFamily ty)

addAddressStr :: BlockList -> String -> IpFamily -> Effect Unit
addAddressStr bl a ty = runEffectFn3 addAddressImpl bl a (toNodeIpFamily ty)

foreign import addRangeImpl :: forall a b. EffectFn4 (BlockList) a b (IpFamily) (Unit)

addRangeStrStr :: BlockList -> String -> String -> IpFamily -> Effect Unit
addRangeStrStr bl start end ty = runEffectFn4 addRangeImpl bl start end ty

addRangeStrAddr :: forall ipFamily. BlockList -> String -> SocketAddress ipFamily -> IpFamily -> Effect Unit
addRangeStrAddr bl start end ty = runEffectFn4 addRangeImpl bl start end ty

addRangeAddrStr :: forall ipFamily. BlockList -> String -> SocketAddress ipFamily -> IpFamily -> Effect Unit
addRangeAddrStr bl start end ty = runEffectFn4 addRangeImpl bl start end ty

addRangeAddrAddr :: forall ipFamilyStart ipFamilyEnd. BlockList -> SocketAddress ipFamilyStart -> SocketAddress ipFamilyEnd -> IpFamily -> Effect Unit
addRangeAddrAddr bl start end ty = runEffectFn4 addRangeImpl bl start end ty

foreign import addSubnetImpl :: forall a. EffectFn4 (BlockList) a (Int) (IpFamily) (Unit)

addSubnetStr :: BlockList -> String -> Int -> IpFamily -> Effect Unit
addSubnetStr bl net prefix ty = runEffectFn4 addSubnetImpl bl net prefix ty

addSubnetAddr :: forall ipFamily. BlockList -> SocketAddress ipFamily -> Int -> IpFamily -> Effect Unit
addSubnetAddr bl net prefix ty = runEffectFn4 addSubnetImpl bl net prefix ty

foreign import checkImpl :: forall a. EffectFn3 (BlockList) a (IpFamily) (Boolean)

checkStr :: BlockList -> String -> IpFamily -> Effect Boolean
checkStr bl addresss ty = runEffectFn3 checkImpl bl addresss ty

checkAddr :: forall ipFamily. BlockList -> SocketAddress ipFamily -> IpFamily -> Effect Boolean
checkAddr bl addresss ty = runEffectFn3 checkImpl bl addresss ty

rules :: BlockList -> Effect (Array String)
rules bl = runEffectFn1 rulesImpl bl

foreign import rulesImpl :: EffectFn1 (BlockList) ((Array String))
41 changes: 14 additions & 27 deletions src/Node/Net/Server.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
import net from "net";

export function addressImpl(server) {
return server.address();
}

export function closeImpl(server, callback) {
server.close(callback);
}

export const createServerImpl = net.createServer;

export function getConnectionsImpl(server, callback) {
server.getConnections(callback);
}

export function listenImpl(server, options, callback) {
server.listen(options, callback);
}

export function listeningImpl(socket) {
return socket.listening;
}

export function onImpl(event, server, callback) {
server.on(event, callback);
}
import net from "node:net";

export const newServerImpl = () => new net.Server();
export const newServerOptionsImpl = (o) => new net.Server(o);

export const addressTcpImpl = (s) => s.address();
export const addressIpcImpl = (s) => s.address();
export const closeImpl = (s) => s.close();
export const getConnectionsImpl = (s, cb) => s.getConnections(cb);
export const listenImpl = (s, o) => s.listen(o);
export const listeningImpl = (s) => s.listening;
export const maxConnectionsImpl = (s) => s.maxConnections;
export const refImpl = (s) => s.ref();
export const unrefImpl = (s) => s.unref();
Loading