~/bend-docscommunity

src/io.bend relies on unsafe/foreign

raw source on the hub · import ezmcp@0.4.0.0/src/io.bend as Io

ezmcp/src/io: the effects. Every decision is src/web.bend's, src/line.bend's, src/server.bend's or src/client.bend's; this module reads, runs the tool runner, and writes, in order. It reaches ezhttp's socket effects, so no law file imports it.

Trusted, not proved: this module decides nothing and acts on the pure modules' answers in order. The HTTP handler answers Web.decide's reply, sent as Web.reply of it; for a tool call it runs the runner once with the name and arguments, then answers Web.ran of its result under the request's revision; for a batch it does each act in order (a reply taken as it is, a tool call run and its reply built with Server.ran under the request's revision, a notification or response taking none), then answers Web.many of Server.batch.reply of those replies. A serve _with a context hands that context, unchanged, to every runner call. Every HTTP serve is ezhttp's http.serve_io_with or http.serve_forever_with with one handler, the endpoint's: it takes the request as ezhttp's plain serves do (server.plain), answers http.handle_with of it, and hands the reply back as their text answer (server.text); http.serve and http.serve_forever are the _with serves with the context Unit and max 0; a serve _from is the _with serve whose handler decides with the server srv(ctx) reads from the context, on every request. The stdio loop starts at Server.latest(), reads stdin a chunk at a time, acts on every whole line of Line.cut in order under the revision the line before it left (Line.line's turn: a reply written as Line.frame, a tool call run then its reply written, a batch's acts done in order and Server.batch.reply of their replies written when there is one), keeps the rest and the revision for the next read, acts on the rest as the last line when a read is empty (stdin ended), and then returns; it writes nothing else to stdout, and a failed read is written to stderr. The client POSTs the message's JSON text with Client.headers, reads the reply to a request with Client.took, and judges the reply to a notification with Client.accepted. This is IO sequencing: effects are foreign, so the checker cannot run an IO program and no law can state what happens after an effect. Every decision it acts on is one the laws under laws/ prove.

Trusted, not proved: ezhttp (pinned in ez.lock.toml) parses HTTP/1.1 requests and answers one that does not parse 400 itself (ezhttp's laws/BadRequestAnswers400.bend); hands a _with handler the context and the request decoded as UTF-8 (ezhttp's laws/WithServesGetOctetsAndContext.bend); serves each connection on its own fork, at most max at once, 0 for no limit (trusted in ezhttp's src/server.bend); finds a header case-insensitively (ezhttp's laws/FieldLookupIgnoresCase.bend); POSTs with a timeout (ezhttp's laws/DeadlineIsTimeout.bend, trusted in its src/client.bend); and body_text decodes a response's body as UTF-8 (ezhttp's laws/OctetsDecodeAsUtf8.bend). Its socket effects are foreign C and JS that no law here reaches. ezmcp keeps nothing between requests (it issues no session), so connections served at once share only the context, which nothing changes.

12 imports
import Base
import ezjson@1.2.0.0/main.bend as Json
import ezjson@1.2.0.0/src/value.bend as Value
import ezhttp@0.23.0.0/main.bend as Ez
import ezhttp@0.23.0.0/src/http.bend as Http
import ezhttp@0.23.0.0/src/client.bend as EzClient
import ezhttp@0.23.0.0/src/server.bend as EzServer
import ./rpc.bend as Rpc
import ./server.bend as Server
import ./web.bend as Web
import ./line.bend as Line
import ./client.bend as Client

Definitions

def stdio.chunk source · line 310 · raw

U32

bytes one read of stdin asks for

def stdio.batch source · line 314 · raw

@replies:Maybe<&2, 0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json> -> IO(Unit)

a batch's replies: nothing when there are none, else one line

def client.took source · line 465 · raw

@res:0x7367809738fddd234b8d2bed772ad2cf/src/client.Response -> 0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Reply

what an ezhttp response to a request says

def client.ask source · line 474 · raw

@url:String -> @version:String -> @timeout_ms:U32 -> @msg:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Reply)

POST one request and read its reply

def client.sent source · line 485 · raw

@res:0x7367809738fddd234b8d2bed772ad2cf/src/client.Response -> Bool

def client.tell source · line 493 · raw

@url:String -> @version:String -> @timeout_ms:U32 -> @msg:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(Bool)

POST one notification; whether the server accepted it

Templates

template act.reply source · line 67 · raw

@-C:Data -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @+ctx:C -> @+rev:String -> @act:0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Act -> IO(Maybe<&2, 0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json>)

one element of a batch, under revision rev: its reply, after running its tool when it is a tool call; none for a notification or a response

template act.all source · line 88 · raw

@-C:Data -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @+ctx:C -> @+rev:String -> @acts:List<&2, 0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Act> -> IO(List<&2, Maybe<&2, 0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json>>)

every element of a batch, in order

template http.web source · line 109 · raw

@-C:Data -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @+ctx:C -> @wid:0x391cc0066bd18b3b1fbf0005e4f14c71/src/web.Web -> IO(0x7367809738fddd234b8d2bed772ad2cf/src/http.Reply)

act on the transport's decision: reply now, or run the tool and reply with its result, or run a batch's acts and reply with their replies

template http.handle_with source · line 127 · raw

@-C:Data -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @-srv:0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Server -> @-path:String -> @-origins:List<&2, String> -> @+ctx:C -> @req:0x7367809738fddd234b8d2bed772ad2cf/src/http.Request -> IO(0x7367809738fddd234b8d2bed772ad2cf/src/http.Reply)

one HTTP request to a server's endpoint at path

template http.answer source · line 147 · raw

@-C:Data -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @-srv:0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Server -> @-path:String -> @-origins:List<&2, String> -> @+ctx:C -> @req:0x7367809738fddd234b8d2bed772ad2cf/src/server.Req -> IO(0x7367809738fddd234b8d2bed772ad2cf/src/server.Answer)

the endpoint's answer to one request ezhttp read, as its plain serves take one: the request as text in, a text reply out

template http.serve_with source · line 162 · raw

@-C:Data -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @-srv:0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Server -> @-path:String -> @-origins:List<&2, String> -> @+ctx:C -> @addr:Pair(String, U32) -> @limit:U32 -> @+max:U32 -> IO(Unit)

serve limit connections on addr, (host, port), at most max at once (0: no limit), then stop

template http.answer_from source · line 203 · raw

@-C:Data -> @-srv:(@_:C -> 0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Server) -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @-path:String -> @-origins:List<&2, String> -> @+ctx:C -> @req:0x7367809738fddd234b8d2bed772ad2cf/src/server.Req -> IO(0x7367809738fddd234b8d2bed772ad2cf/src/server.Answer)

the endpoint's answer to one request ezhttp read, its server read from the context

template http.serve_from source · line 219 · raw

@-C:Data -> @-srv:(@_:C -> 0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Server) -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @-path:String -> @-origins:List<&2, String> -> @+ctx:C -> @addr:Pair(String, U32) -> @limit:U32 -> @+max:U32 -> IO(Unit)

serve limit connections on addr, (host, port), at most max at once (0: no limit), then stop

template http.handle source · line 255 · raw

@-run:(@_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @-srv:0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Server -> @-path:String -> @-origins:List<&2, String> -> @req:0x7367809738fddd234b8d2bed772ad2cf/src/http.Request -> IO(0x7367809738fddd234b8d2bed772ad2cf/src/http.Reply)

one HTTP request to a server's endpoint at path

template http.serve source · line 266 · raw

@-run:(@_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @-srv:0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Server -> @-path:String -> @-origins:List<&2, String> -> @host:String -> @port:U32 -> @limit:U32 -> IO(Unit)

serve limit connections, as many at once as arrive, then stop

template stdio.act source · line 324 · raw

@-C:Data -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @+ctx:C -> @+rev:String -> @act:0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Act -> IO(Unit)

act on one line's act, read by revision rev: write a reply as one line, or run the tool and write its reply, or run a batch's acts and write the array of their replies, or write nothing

template stdio.turn source · line 346 · raw

@-C:Data -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @+ctx:C -> @+rev:String -> @turn:0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Turn -> @next:(@_:String -> IO(String)) -> IO(String)

act on a turn read by revision rev, then go on with the revision after it

template stdio.lines source · line 360 · raw

@-C:Data -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @-srv:0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Server -> @+ctx:C -> @lines:List<&2, List<&2, U32>> -> @+rev:String -> IO(String)

every whole line, in order, each read by the revision the line before it left; answers the revision the last one left

template stdio.cut source · line 376 · raw

@-C:Data -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @-srv:0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Server -> @+ctx:C -> @rev:String -> @again:(@_:List<&2, U32> -> @_:String -> IO(Unit)) -> @cut:0x391cc0066bd18b3b1fbf0005e4f14c71/src/line.Cut -> IO(Unit)

act on a cut's whole lines, then read on with its rest

template stdio.chunked source · line 392 · raw

@-C:Data -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @-srv:0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Server -> @+ctx:C -> @rev:String -> @buf:List<&2, U32> -> @bytes:List<&2, U32> -> @again:(@_:List<&2, U32> -> @_:String -> IO(Unit)) -> @empty:Bool -> IO(Unit)

after a read: an empty one is the end of stdin, and the bytes after the last newline are the last message; any other is cut into lines

template stdio.got source · line 412 · raw

@-C:Data -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @-srv:0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Server -> @+ctx:C -> @rev:String -> @buf:List<&2, U32> -> @again:(@_:List<&2, U32> -> @_:String -> IO(Unit)) -> @res:Result<&1, &1, Pair(U32, String), List<&2, U32>> -> IO(Unit)

after one read: a failure is logged to stderr and ends the loop

Effects (foreign code)

effect ezmcp.stdin source · line 305 · raw

@max:U32 -> IO(Result<&1, &1, Pair(U32, String), List<&2, U32>>)

up to max bytes from file descriptor 0, as one read(2) returns them; empty at the end of input

Trusted, not proved: ezmcp.stdin(max) (src/effs/stdin.c, src/effs/stdin.js) answers the bytes one read(2) of file descriptor 0 returns, at most max, an empty list at the end of input, or Fail{(errno, message)}; it reads the descriptor itself, so it works whatever stdin is (a pipe, a file, a socket). Base's IO.write writes its text to stdout in order, and IO.print_err to stderr. They are foreign C and JS: ezmcp's own over read(2), and Bend's runtime (effs/write.c, effs/print_err.c), which no law reaches. Base has no stdin, and opening /dev/stdin fails with ENXIO when stdin is a socket, as it is for a server Node spawns.

foreign: src/effs/stdin.c, src/effs/stdin.js

Unsafe

unsafe http.serve_forever_with source · line 180 · raw

@-C:Data -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @-srv:0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Server -> @-path:String -> @-origins:List<&2, String> -> @+ctx:C -> @addr:Pair(String, U32) -> @+max:U32 -> IO(Unit)

serve on addr, (host, port), until the process is stopped, at most max connections at once (0: no limit)

unsafe http.serve_forever_from source · line 237 · raw

@-C:Data -> @-srv:(@_:C -> 0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Server) -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @-path:String -> @-origins:List<&2, String> -> @+ctx:C -> @addr:Pair(String, U32) -> @+max:U32 -> IO(Unit)

serve on addr, (host, port), until the process is stopped, at most max connections at once (0: no limit)

unsafe http.serve_forever source · line 279 · raw

@-run:(@_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @-srv:0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Server -> @-path:String -> @-origins:List<&2, String> -> @host:String -> @port:U32 -> IO(Unit)

serve until the process is stopped, as many connections at once as arrive

unsafe stdio.loop source · line 431 · raw

@-C:Data -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @-srv:0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Server -> @+ctx:C -> @buf:List<&2, U32> -> @rev:String -> IO(Unit)

read, act on every whole line, and read again until stdin ends; rev is the revision the next line is read by

unsafe stdio.serve_with source · line 446 · raw

@-C:Data -> @-run:(@_:C -> @_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @-srv:0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Server -> @+ctx:C -> IO(Unit)

serve newline-delimited JSON-RPC on stdin and stdout until stdin ends; a line is read by the latest revision until an initialize names another

unsafe stdio.serve source · line 456 · raw

@-run:(@_:String -> @_:0xa81e161008ecc2f297c0a3065cb9b188/src/value.Json -> IO(0x391cc0066bd18b3b1fbf0005e4f14c71/src/rpc.Out)) -> @-srv:0x391cc0066bd18b3b1fbf0005e4f14c71/src/server.Server -> IO(Unit)

serve newline-delimited JSON-RPC on stdin and stdout until stdin ends, with no context