on master help sign in
Simplify server.knot: inline headOf, flatten isValidHex, drop unused let bindings, add hasPrefix helper
commitb27998149e4abc8dea0bcee04e8449820eaebcb4 authoriko <mail@iko.soy> dateMon 27 Apr 2026, 12:50 (6 months ago) parent606037c
1 file changed+11−16
server.knot+11−16
server.knot+11−16
@@ -482,12 +482,10 @@ addServerToList = \listRef server -> atomic do
482482-- Remove `server` from the named list ref. Returns whether it was present.
483483removeServerFromList = \listRef server -> atomic do
484484 current <- *listRef
485− pruneRef listRef (\x -> x != server)
485+ set *listRef = removeWhere current (\x -> x == server)
486486 yield (elem current server)
487487
488−headOf = \items -> fold (\acc x -> case acc of
489− Nothing {} -> Just {value: x}
490− _ -> acc) Nothing {} items
488+headOf = \items -> fold (\_ x -> Just {value: x}) Nothing {} (take 1 items)
491489
492490-- Sort then take head: O(n log n) but no fold-with-Maybe boilerplate.
493491minBy = \items getKey -> headOf (sortBy getKey items)
@@ -560,18 +558,13 @@ tooLong : Text -> Int -> Bool
560558tooLong = \s maxLen -> count (chars s) > maxLen
561559
562560isValidHex : Text -> Bool
563−isValidHex = \s ->
564− let cs = chars s in
565− let n = count cs in
566− n > 0 && mod n 2 == 0 && all cs (\c -> contains c "0123456789abcdef")
561+isValidHex = \s -> let cs = chars s in count cs > 0 && mod (count cs) 2 == 0 && all cs (\c -> contains c "0123456789abcdef")
567562
568563normalizeHex : Text -> Text
569564normalizeHex = \s -> bytesToHex (bytesFromHex s)
570565
571566stripTrailingDot : Text -> Text
572−stripTrailingDot = \s ->
573− let n = count (chars s) in
574− if n > 0 && drop (n - 1) s == "." then stripTrailingDot (take (n - 1) s) else s
567+stripTrailingDot = \s -> if containsSuffix "." s then stripTrailingDot (take (count (chars s) - 1) s) else s
575568
576569normalizeServerName : Text -> Text
577570normalizeServerName = \s -> toLower (stripTrailingDot s)
@@ -581,6 +574,9 @@ serverHostN = normalizeServerName serverHost
581574isLocalServer : Text -> Bool
582575isLocalServer = \s -> s == serverHostN
583576
577+hasPrefix : Text -> Text -> Bool
578+hasPrefix = \pre s -> take (count (chars pre)) s == pre
579+
584580containsSuffix : Text -> Text -> Bool
585581containsSuffix = \suffix s ->
586582 let d = count (chars s) - count (chars suffix) in
@@ -592,8 +588,8 @@ isBadServerName : Text -> Bool
592588isBadServerName = \sn ->
593589 sn == "" || sn == "localhost"
594590 || not (all (chars sn) (\c -> contains c "0123456789abcdefghijklmnopqrstuvwxyz-.:"))
595− || any [".", ":", "[", "127.", "10.", "192.168.", "169.254.", "0.", "::"] (\pre -> take (count (chars pre)) sn == pre)
596− || any [".localhost", ".local", ".internal", ".arpa", ".onion"] (\suf -> containsSuffix suf sn)
591+ || any [".", ":", "[", "127.", "10.", "192.168.", "169.254.", "0.", "::"] (\p -> hasPrefix p sn)
592+ || any [".localhost", ".local", ".internal", ".arpa", ".onion"] (\s -> containsSuffix s sn)
597593
598594-- ============================================
599595-- Auth helpers
@@ -890,10 +886,9 @@ recordAcksAndDeliveries = \toAck t -> do
890886applyCancel = \msgs matches to mid t -> do
891887 set *messages = removeWhere msgs matches
892888 fwds <- *forwards
893− let fwdServers = collectPeers (filter fwds matches) (\f -> f.toServer)
894− pruneRef forwards (\f -> not (matches f))
889+ set *forwards = removeWhere fwds matches
895890 appendCapped cancelNotifications [{toKey: to, messageId: mid, cancelledAt: t}] 10000 (\c -> c.cancelledAt)
896− yield fwdServers
891+ yield (collectPeers (filter fwds matches) (\f -> f.toServer))
897892
898893sendBatchedGossip = \peers events hops -> forEach peers id (\server -> do
899894 t <- now