summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpolwex <polwex@sortug.com>2025-10-31 22:59:42 +0700
committerpolwex <polwex@sortug.com>2025-10-31 22:59:42 +0700
commitf019da2e338733dd88d3a8efe60f260d74b9a7ad (patch)
treeecccfab6926dd89c3ec0597653cca6e30c62dc41
parent54fb3abb81621bd2a5fafbdd9b96a8be8464878e (diff)
fixes to iris request flow with threads
-rw-r--r--.gitignore2
-rw-r--r--app/app/nostrill.hoon17
-rw-r--r--app/lib/nostr/client.hoon20
-rw-r--r--app/lib/nostr/req.hoon4
-rw-r--r--app/lib/nostrill.hoon4
-rw-r--r--app/lib/websockets.hoon29
-rw-r--r--app/ted/ws.hoon48
-rw-r--r--arvo/iris.hoon35
8 files changed, 114 insertions, 45 deletions
diff --git a/.gitignore b/.gitignore
index 0523acd..aece2ac 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,5 @@ AGENTS.md
CLAUDE.md
NOTES.md
run.sh
+pier
+urbit
diff --git a/app/app/nostrill.hoon b/app/app/nostrill.hoon
index c8ed796..9b3b974 100644
--- a/app/app/nostrill.hoon
+++ b/app/app/nostrill.hoon
@@ -60,13 +60,15 @@
handle-comms
%json on-ui
%handle-http-request handle-shim
- %websocket-client-message handle-relay-ws
+ %websocket-client-message handle-relay-ws
%websocket-handshake handle-ws-handshake
%websocket-server-message handle-ws-msg
==
+$ ws-msg [@ud websocket-message:eyre]
++ handle-relay-ws
+ ^- (quip card:agent:gall agent:gall)
=/ msg !<(ws-msg vase)
+ ~& handle-relay-ws=msg
=/ m=websocket-message:eyre +.msg
?~ message.m `this
=/ =octs u.message.m
@@ -76,6 +78,7 @@
=^ cards state (handle-ws:mutan relay-url u.urelay-msg)
[cards this]
++ handle-ws-handshake
+ ^- (quip card:agent:gall agent:gall)
=/ order !<([@ inbound-request:eyre] vase)
~& >> nostrill-ws-handshake=order
=/ url url.request.order
@@ -89,6 +92,7 @@
?: ok (accept-handshake:ws -.order) (refuse-handshake:ws -.order)
:: we behave like a Server here, mind you. messages from clients, not relays
++ handle-ws-msg
+ ^- (quip card:agent:gall agent:gall)
=/ order !<([wid=@ =path msg=websocket-message:eyre] vase)
:: ~& opcode=op=opcode.msg.order :: 0 for continuation, 1 for text, 2 for binary, 9 for ping 0xa for pong
=/ msg message.msg.order
@@ -102,9 +106,11 @@
==
::
++ handle-ui-ws
+ ^- (quip card:agent:gall agent:gall)
=/ cs (ui-ws-res:lib -.order wsdata)
[cs this]
++ handle-nostr-client-ws
+ ^- (quip card:agent:gall agent:gall)
=/ jsonm (de:json:html wsdata)
?~ jsonm `this
=/ client-msg (parse-client-msg:nreq u.jsonm)
@@ -129,6 +135,7 @@
[cs this]
:: DEPRECATED
++ handle-shim
+ ^- (quip card:agent:gall agent:gall)
=/ order !<(order:web vase)
~& request.req.order
?: .=(url.request.req.order '/nostr-shim')
@@ -224,6 +231,10 @@
=/ =task:iris [%websocket-connect dap.bowl 'ws://localhost:8888']
:~ [%pass /iris-test %arvo %i task]
==
+ %wsted
+ =/ relay-url 'ws://localhost:8888'
+ =^ cs state (test-connection:nclient relay-url)
+ [cs this]
%irisf
:_ this
=/ inc-subs ~(tap by sup.bowl)
@@ -241,6 +252,10 @@
=/ =task:iris [%websocket-event +.noun %message 1 `(as-octs:mimes:html 'heyhey')]
:~ [%pass /iris-test2 %arvo %i task]
==
+ %iriss
+ =/ res (check-connected:ws 'ws://localhost:8888' bowl)
+ ~& res
+ `this
%wtf
=/ lol=(unit @) ~
=/ l ~| "wtf" (need lol)
diff --git a/app/lib/nostr/client.hoon b/app/lib/nostr/client.hoon
index 908b360..41c3080 100644
--- a/app/lib/nostr/client.hoon
+++ b/app/lib/nostr/client.hoon
@@ -1,5 +1,5 @@
/- sur=nostrill, nsur=nostr
-/+ js=json-nostr, sr=sortug, nostr-keys, constants, server
+/+ js=json-nostr, sr=sortug, nostr-keys, constants, server, ws=websockets
/= web /web/router
|_ [=state:sur =bowl:gall]
@@ -88,14 +88,30 @@
=/ =filter:nsur [~ ~ `kinds `tags ~ ~ ~]
(send-req ~[filter])
-:: TODO URGENT
+::
+++ test-connection
+ |= relay-url=@t
+ =/ kinds (silt ~[1])
+ =/ since (sub now.bowl ~h1)
+ =/ =filter:nsur [~ ~ `kinds ~ `since ~ ~]
+ =/ sub-id (gen-sub-id:nostr-keys eny.bowl)
+ =/ req=client-msg:nsur [%req sub-id ~[filter]]
+ :- :~ (send relay-url req) == state
+
++ send
|= [relay-url=@t req=client-msg:nsur] ^- card:agent:gall
+ ~& >>> send=relay-url
=/ req-body=json (req:en:js req)
=/ octs (json-to-octs:server req-body)
=/ wev=websocket-event:eyre [%message 1 `octs]
+ =/ conn (check-connected:ws relay-url bowl)
+ ~& >>> send-client-conn=conn
+ ?~ conn :: if no ws connection we start a thread which will connect first, then send the message
=/ pat /to-nostr-relay
[%pass (weld /ws pat) %arvo %k %fard dap.bowl %ws %noun !>([relay-url wev])]
+ ::
+ (give-ws-payload-client:ws id.u.conn [1 `octs])
+
:: ++ send-http
:: |= req=http-req:shim:nsur
diff --git a/app/lib/nostr/req.hoon b/app/lib/nostr/req.hoon
index e53159c..89cd262 100644
--- a/app/lib/nostr/req.hoon
+++ b/app/lib/nostr/req.hoon
@@ -14,5 +14,7 @@
=/ jon (relay-msg:en:js msg)
=/ octs (json-to-octs:server jon)
=/ res-event=websocket-event:eyre [%message 1 `octs]
- (give-ws-payload:ws wid res-event)
+ :~
+ (give-ws-payload-server:ws wid res-event)
+ ==
--
diff --git a/app/lib/nostrill.hoon b/app/lib/nostrill.hoon
index b9d8f88..d46d090 100644
--- a/app/lib/nostrill.hoon
+++ b/app/lib/nostrill.hoon
@@ -34,7 +34,9 @@
=/ resmsg (cat 3 msg (cat 3 msg msg))
=/ octs (as-octs:mimes:html resmsg)
=/ res-event=websocket-event:eyre [%message 1 `octs]
- (give-ws-payload:ws wid res-event)
+ :~ (give-ws-payload-server:ws wid res-event)
+ ==
+
::
++ cards
diff --git a/app/lib/websockets.hoon b/app/lib/websockets.hoon
index 4d1f952..98ea25b 100644
--- a/app/lib/websockets.hoon
+++ b/app/lib/websockets.hoon
@@ -1,17 +1,34 @@
|%
- ++ give-ws-payload
+ ++ give-ws-payload-client
+ |= [wid=@ msg=websocket-message:eyre]
+ ^- card:agent:gall
+ =/ =cage
+ [%message !>(msg)]
+ =/ wsid (scot %ud wid)
+ [%give %fact ~[/websocket-client/[wsid]] cage]
+
+ ++ give-ws-payload-server
|= [wid=@ event=websocket-event:eyre]
- ^- (list card:agent:gall)
+ ^- card:agent:gall
=/ =cage
[%websocket-response !>([wid event])]
=/ wsid (scot %ud wid)
- :~ [%give %fact ~[/websocket-server/[wsid]] cage]
- ==
+ [%give %fact ~[/websocket-server/[wsid]] cage]
+
++ accept-handshake |= wid=@
=/ response [%accept ~]
- (give-ws-payload wid response)
+ :~
+ (give-ws-payload-server wid response)
+ ==
++ refuse-handshake |= wid=@
=/ response [%reject ~]
- (give-ws-payload wid response)
+ :~
+ (give-ws-payload-server wid response)
+ ==
+ ++ check-connected
+ |= [url=@t =bowl:gall] ^- (unit websocket-connection:iris)
+ =/ scry-path=path /(scot %p our.bowl)/ws/(scot %da now.bowl)/url/[url]
+ =/ conn .^((unit websocket-connection:iris) %ix scry-path)
+ conn
--
diff --git a/app/ted/ws.hoon b/app/ted/ws.hoon
index d721832..ebb7926 100644
--- a/app/ted/ws.hoon
+++ b/app/ted/ws.hoon
@@ -9,14 +9,6 @@
=/ [url=@t wev=websocket-event:eyre] !<([@t websocket-event:eyre] arg)
~& > url=url
~& > req=wev
-=/ url 'ws://localhost:8888'
-::
-::
-::
-
-
-:: ;< ~ bind:m (send-request:strandio [%'GET' 'http://localhost:8888/test' ~ ~])
-::
;< =bowl:spider bind:m get-bowl:strandio
=/ desk q.byk.bowl
=/ =task:iris [%websocket-connect desk url]
@@ -24,22 +16,28 @@
;< ~ bind:m (send-raw-card:strandio card)
;< res=(pair wire sign-arvo) bind:m take-sign-arvo:strandio
~& > res=res
-:: :: confirm connection was established
-:: ?. ?=([%iris %websocket-response id=@ud websocket-event:eyre] q.res)
-:: (strand-fail:strand %bad-sign ~)
-:: ?. ?=(%accept +>+<.q.res)
-:: (strand-fail:strand %bad-sign ~)
+:: confirm connection was established
+?. ?=([%iris %websocket-response id=@ud websocket-event:eyre] q.res)
+ (strand-fail:strand %bad-sign ~)
+~& > ted-ws-res=+>.q.res
+?. ?=(%accept +>+<.q.res)
+ (pure:m !>([%ng '']))
+ :: (strand-fail:strand %bad-sign ~)
:: :: ~& ws-handshake=[id.q.res url.q.res]
-:: :: ?. ?=([%iris %websocket-handshake id=@ud url=@t] q.res)
-:: :: (strand-fail:strand %bad-sign ~)
-:: :: ~& ws-handshake=[id.q.res url.q.res]
-:: =/ wid id.+.q.res
-:: =/ =task:iris [%websocket-event wid wev]
-:: =/ =card:agent:gall [%pass /ws-req-2 %arvo %i task]
-:: ;< ~ bind:m (send-raw-card:strandio card)
-:: ;< res=(pair wire sign-arvo) bind:m take-sign-arvo:strandio
-:: ?. ?=(%iris -.q.res)
-:: (strand-fail:strand %bad-sign ~)
-:: =/ g=gift:iris +.q.res
-(pure:m !>('done'))
+:: TODO this might fail if the subscription is not set yet
+~& >>> "sleeping"
+;< ~ bind:m (sleep:strandio ~s3)
+~& >>> "slept"
+
+=/ subwire=path /websocket-server/(scot %ud id.q.res)
+=/ =cage [%websocket-response !>(+>.q.res)]
+=/ gf=gift:agent:gall [%fact :~(subwire) cage]
+=/ =card:agent:gall [%give gf]
+~& >> ws-ted-ok-sending-msg=id.q.res
+;< ~ bind:m (send-raw-card:strandio card)
+;< res2=(pair wire sign-arvo) bind:m take-sign-arvo:strandio
+?. ?=([%iris %websocket-response id=@ud %message wm=websocket-message:eyre] q.res2)
+ (strand-fail:strand %bad-sign ~)
+=/ wm=websocket-message:eyre +>+>.q.res2
+(pure:m !>([%ok id.q.res]))
diff --git a/arvo/iris.hoon b/arvo/iris.hoon
index 41a4160..7b618b2 100644
--- a/arvo/iris.hoon
+++ b/arvo/iris.hoon
@@ -322,7 +322,7 @@
:- [outbound-duct.state %give %websocket-handshake id url]~
state
::
- :: incoming websockets event
+ :: incoming websockets event to be sent BY VERE NOT USERSPACE
++ ws-event
|= [wid=@ud event=websocket-event:eyre]
~& iris-ws-event=[wid event duct]
@@ -336,13 +336,11 @@
=. wc wc(status %accepted)
=. sockets.state (~(put by sockets.state) wid wc)
:_ state
- :: :~ (watch-agent wid app.wc)
- :: ==
- ~
+ :~ (watch-agent wid app.wc)
+ ==
%message :_ state
- :: :~ (poke-agent [wid +.event] app.wc)
- :: ==
- ~
+ :~ (poke-agent [wid +.event] app.wc)
+ ==
%reject (cleanup-ws wid)
%disconnect (cleanup-ws wid)
@@ -517,7 +515,7 @@
++ load
|= old=axle
:: |= old=*
- :: ^+ ..^$
+ ^+ ..^$
::
~! %loading
..^$(ax old)
@@ -533,8 +531,27 @@
|= [lyc=gang pov=path car=term bem=beam]
^- (unit (unit cage))
~& >> iris-scry=[lyc=lyc pov=pov car=car bem=bem syd=q.bem]
+ :: TODO this is obviously insecure so should be deprecated soon
?. ?=(%x car) [~ ~]
- ?: ?=(%ws q.bem) ``noun+!>(sockets.state.ax)
+ =/ caller +<.pov
+ ?: ?=(%ws q.bem)
+ ?+ s.bem ~
+ ~ ``noun+!>(sockets.state.ax)
+ [%id @ ~]
+ =/ wid (slav %ud i.s.bem)
+ =/ socket (~(got by sockets.state.ax) wid)
+ ?. .=(app.socket caller) ~
+ ``noun+!>(socket)
+ [%url @ ~]
+ =/ sockets ~(tap by sockets.state.ax)
+ :: pass a (unit websocket-connection)
+ :- ~ :- ~ :- %noun !>
+ |- ?~ sockets ~
+ =/ socket=websocket-connection:iris q.i.sockets
+ ?. .=(app.socket caller) $(sockets t.sockets)
+ ?: .=(url.socket i.t.s.bem) `socket
+ $(sockets t.sockets)
+ ==
=* ren car
=* why=shop &/p.bem
=* syd q.bem