Skip to content

Use EventEmitter/EventHandle; fully-implement #14

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

Closed
wants to merge 3 commits into from
Closed
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
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