gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commita544b261a544b261ident#28: README names the new vendored Hybrielmrea544b261/plugins/http1/native_web_socket_server.hl

4.4 KB

  1. \* hl:http1 — the native WebSocket server. Subclass it (`inherit
  2. NativeWebSocketServer`) and answer the declared events below; each
  3. connected peer arrives as a WsClient (send/ping/close). AUTO-LISTEN: the
  4. file root IS the constructor and ends with listen(), so
  5. `new NativeWebSocketServer(port = 8888)` serves immediately. *\
  6. /* Mission 064 (decision D9): the h1 plugin performs the RFC 6455 Upgrade/101
  7. handshake and framing (shared core plugins/http/ws_common.zig); this
  8. wrapper maps the native event stream onto on/emit, mirroring
  9. NativeHttpServer. A SUBCLASS does not inherit this run body and calls
  10. listen() from its own root. */
  11. \* A peer completed the upgrade and is connected. *\
  12. emit connect(
  13. WsClient client \\ the new peer — send/ping/close
  14. )
  15. \* A complete text (or binary) message arrived from a peer. *\
  16. emit message(
  17. WsClient client \\ who sent it
  18. String data \\ the message payload
  19. )
  20. \* The peer answered a client.ping(). *\
  21. emit pong(
  22. WsClient client \\ who answered
  23. )
  24. \* A connection ended — the peer is already unregistered. *\
  25. emit close(
  26. WsClient client \\ who left
  27. )
  28. \* OPTIONAL: plain HTTP answered on the same port, exactly as
  29. NativeHttpServer's request. *\
  30. emit request(
  31. Hybrid req \\ method, path, headers, body, bytes, params, respond
  32. )
  33. Number port = 8080 \\ the port listen() binds
  34. String host = "0.0.0.0" \\ the interface to bind
  35. Number threads = 4 \\ I/O worker threads
  36. Number maxBody = 20971520 \\ largest request body read, bytes (default 20 MiB); over it: 413, connection closed
  37. Boolean wsCheckOrigin = false \\ refuse an upgrade whose Origin is foreign (not the Host, not in wsOrigins) with 403 before the 101; no Origin = allowed
  38. String wsOrigins = "" \\ extra allowed origins, comma-separated
  39. Boolean listening = false \\ true after a successful bind
  40. Hybrid clients = {} \\ live WsClients by connection id
  41. Number #srcId = 0
  42. Number #wsSrcId = 0
  43. \* Bind the configured port and start the WebSocket engine (idempotent). *\
  44. listen() {
  45. if (listening == true) {
  46. return null
  47. }
  48. srv = __native("http1.create_server", port, host, "", "", threads, maxBody, wsCheckOrigin, wsOrigins)
  49. if (srv == null) {
  50. // A REFUSAL, NOT PROSE (mission 286) — same reason as NativeHttpServer's.
  51. hlError("hl:http1: listen() failed: could not bind port " + port + " on " + host + " — the port is already taken. The line just above, 'bind() failed on port " + port + "', names the process holding it: stop that process, or start this app on another port (--port N, or HL_PORT=N)")
  52. }
  53. wsev = __native("http1.ws_events")
  54. listening = true
  55. #srcId = __native("eventloop.register", srv, "request")
  56. #wsSrcId = __native("eventloop.register", wsev, "wsEvent")
  57. }
  58. \* One text frame to EVERY connected peer; answers how many received it. *\
  59. broadcast(
  60. String text \\ the frame to send
  61. ) {
  62. return __native("http1.ws_broadcast", text)
  63. }
  64. \* Stop this server: both event sources retired, workers joined, listener
  65. closed — the port is free again. Answers false when not listening. *\
  66. close() {
  67. if (listening == false) {
  68. return false
  69. }
  70. listening = false
  71. __native("eventloop.unregister", #srcId)
  72. __native("eventloop.unregister", #wsSrcId)
  73. return __native("http1.close", port)
  74. }
  75. // Raw native events ({ kind, id, data, binary, code }) re-emitted as the
  76. // documented surface — emit is the universal primitive.
  77. on wsEvent(ev) {
  78. key = "c" + ev.id
  79. if (ev.kind == "connect") {
  80. client = new WsClient(ev.id, ev.cookie, ev.host != '' ? ev.host : null, headerHash(ev.headers), ev.ip != '' ? ev.ip : null, ev.secure) // the upgrade's cookie, host and headers ride with the connection (creator, 2026-09-12; tickets #74, #105)
  81. clients[key] = client
  82. emit connect(client)
  83. }
  84. if (ev.kind == "message") {
  85. client = clients[key]
  86. if (client != null) {
  87. emit message(client, ev.data)
  88. }
  89. }
  90. if (ev.kind == "pong") {
  91. client = clients[key]
  92. if (client != null) {
  93. emit pong(client)
  94. }
  95. }
  96. if (ev.kind == "close") {
  97. client = clients[key]
  98. clients[key] = null
  99. if (client != null) {
  100. emit close(client)
  101. }
  102. }
  103. }
  104. // The upgrade's headers arrive as `name: value` lines (names already lowercase);
  105. // a connection is handed them as a hash (ticket #105).
  106. headerHash(lines) {
  107. let nl = "
  108. "
  109. let out = {}
  110. if (lines == null || lines == '') { return out }
  111. for (line of lines.split(nl)) {
  112. let colon = line.indexOf(': ')
  113. if (colon > 0) { out[line.slice(0, colon)] = line.slice(colon + 2) }
  114. }
  115. return out
  116. }
  117. // Run body: bind the configured port (idempotent, like NativeHttpServer).
  118. listen()

Branches

Latest commits

  • a544b261ident#28: README names the new vendored Hybrielmre
  • 716860b0ident#28: gates follow Hybriel ac1ed9da (lazy sessions, meta attribute order, JSON key order, default X-Frame-Options switched off for the embed page)mre
  • 889e00fbident#28: Hybriel master ac1ed9da (sessions reload a newer file, a replayed reconnect carries the server session); a startup fault exits 1 (on Error)mre
  • f5a55f8aident#11: apps can ask for nickname, firstname and lastname (fields list on /api/exchange, new POST /api/profile); empty fields come back null; gate checks, READMEmre
  • d8be66ebident: report and log line for the widget cache and frame height changemre
  • 2068f61aident#31: browsers revalidate the widget scripts on every load; the sign-in frame is exactly as tall as the current step (the frame page reports its height, selector.js accepts it from ident only)mre
  • a22d2daaident#31: the sign-in frame is no taller than the tallest step needs (plain CSS height per width), gate check for itmre
  • a77d1b22ident#31: the welcome form keeps editId a plain value (browser gate), README, wider rows in the framemre
  • 5c481bfdident#31: sign in to ident inside the selector's panel (email, code, identity in an iframe; a new address goes to ident in the same tab and comes back)mre
  • 061abfe2ident: the apps gate waits until the test app's login button stops moving before it clicks (the page grows after load and the click missed about every second run)mre
  • 45f7e4daident: the user menu's Sign out waits for the server before it reloads (the apps gate failed because the session could still be signed in)mre
  • f07658c7ident: home page shows the account once (account bar removed, the user menu carries email, Sign out, Sign out everywhere); wildcard origins https://*.domain for apps on many subdomains; gates follow (WIP: gates not re-run after the last edits)mre
  • f2c43bbbtheme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre
  • 6d4f996esessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre
  • 029eb5e5deploy: security gate (tests/security.mjs)mre
  • d3c065a8security audit (SI): uploadMax 0, Secure __Host- cookie behind https, headers on function routes/files, page CSP+referrer meta, app/identity caps, tests/security.mjs, reportmre
  • 5561a0cbreport I4-mobilemre
  • 4ca2ed20404 page in the shell (viewport, status 404, way home); phone: account actions live in the user menu so the open menu covers nothing; mobile gate; signoutall waits for a cold startmre
  • 9baad745tests: browser gate waits for the reloaded document (offline reload race under CPU throttle); HL_CPU_THROTTLE knobmre
  • b96796bdtests: wait for hydration before real clicks after a navigation (H2)mre