gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain10f8a8c8Merge branch 'code-order' into mainJukeboxmain/plugins/http1/server.hl

3.4 KB

  1. \* hl:http1 — module exports beside the class surface (NativeHttpServer,
  2. NativeWebSocketServer, Response, WsClient in their own files). *\
  3. \* Bind a plain HTTP/1.1 listener; answers the request event source. *\
  4. listen(
  5. Number port \\ the port to bind
  6. ) {
  7. return __native("http1.listen", port)
  8. }
  9. \* The full-featured constructor: interface, TLS pair, worker count. *\
  10. create_server(
  11. Number port \\ the port to bind
  12. String host = '0.0.0.0' \\ the interface
  13. String cert = null \\ PEM certificate path — with key, serve https
  14. String key = null \\ PEM key path
  15. Number threads = 4 \\ I/O worker threads
  16. Number maxBody = 20971520 \\ largest request body read, bytes; over it: 413
  17. Boolean wsCheckOrigin = false \\ 403 a WebSocket upgrade with a foreign Origin
  18. String wsOrigins = "" \\ extra allowed origins, comma-separated
  19. ) {
  20. return __native("http1.create_server", port, host, cert, key, threads, maxBody, wsCheckOrigin, wsOrigins)
  21. }
  22. \* A cookie-grade random token: n lowercase hex characters read from the
  23. KERNEL's CSPRNG, never a seeded PRNG — a session id is the only thing
  24. between a stranger and someone else's session (hl:math's random() is a
  25. clock-seeded xoshiro whose state a handful of outputs reveal). Lives here
  26. because the session cookie is an HTTP artifact. *\
  27. randomToken(
  28. Number n \\ how many hex characters
  29. ) {
  30. return __native("http1.random_token", n)
  31. }
  32. \* SERVER-SENT EVENTS (ticket #144) — the push channel, imported by name like any
  33. other export: import { sseStart, sseSend } from 'hl:http1'. A static or a method
  34. calls them; the browser build of a file that imports them binds each to a refusal
  35. that throws only if the browser CALLS it (hl:http1 ships no browser half).
  36. let conn = sseStart(req.respond) \\ the GET is now a text/event-stream
  37. sseSend(conn, JSON.stringify(row)) \\ one `data:` frame to that client
  38. A stream lives until its client goes: hl:http1 reaps a closed one at once (FIN) or
  39. at the next ":" heartbeat (every 5 s), and a push to it answers false. Plain HTTP
  40. only — on hl:http1's own TLS, sseStart answers null (end TLS in a proxy). *\
  41. \* Turn this request into a Server-Sent Events stream: writes the 200
  42. text/event-stream head and keeps the socket open. Answers the CONNECTION ID the
  43. other sse calls address, or null when the request was already answered (or is TLS). *\
  44. sseStart(
  45. Hybrid respond \\ the request's respond handle: req.respond
  46. ) {
  47. return respond.call("sse_start")
  48. }
  49. \* One frame to one stream: `data: <text>` and the blank line. A text with
  50. newlines continues the frame (a line after the first is its own field, e.g.
  51. 'id: 42'). Answers true when it was written, false when that stream is gone. *\
  52. sseSend(
  53. Number conn \\ the id sseStart answered
  54. String text \\ the frame's data, usually JSON.stringify(...)
  55. ) {
  56. return __native("http1.sse_send", conn, text) == 1
  57. }
  58. \* The same frame to every open stream; answers how many got it. *\
  59. sseBroadcast(
  60. String text \\ the frame's data
  61. ) {
  62. return __native("http1.sse_broadcast", text)
  63. }
  64. \* Is the stream still open? Asks the registry, pushes nothing. *\
  65. sseAlive(
  66. Number conn \\ the id sseStart answered
  67. ) {
  68. return __native("http1.sse_alive", conn) == 1
  69. }
  70. \* Close one stream; answers true if closed, false when already gone. *\
  71. sseClose(
  72. Number conn \\ the id sseStart answered
  73. ) {
  74. return __native("http1.sse_close", conn) == 1
  75. }
  76. \* How many streams are open now. *\
  77. sseCount() {
  78. return __native("http1.sse_count")
  79. }

Branches

Latest commits

  • 10f8a8c8Merge branch 'code-order' into mainJukebox
  • 204fc828Merge branch 'day72' into mainJukebox
  • 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre