@yaks/api 0.0.0-stage → 0.2.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +956 -2
  3. package/_dist/actor.d.ts +59 -0
  4. package/_dist/actor.d.ts.map +1 -0
  5. package/_dist/address.d.ts +33 -0
  6. package/_dist/address.d.ts.map +1 -0
  7. package/_dist/asset.d.ts +15 -0
  8. package/_dist/asset.d.ts.map +1 -0
  9. package/_dist/batches.d.ts +4 -0
  10. package/_dist/batches.d.ts.map +1 -0
  11. package/_dist/body.d.ts +7 -0
  12. package/_dist/body.d.ts.map +1 -0
  13. package/_dist/deno.d.ts +38 -0
  14. package/_dist/deno.d.ts.map +1 -0
  15. package/_dist/doors.d.ts +134 -0
  16. package/_dist/doors.d.ts.map +1 -0
  17. package/_dist/graph.d.ts +17 -0
  18. package/_dist/graph.d.ts.map +1 -0
  19. package/_dist/headers.d.ts +5 -0
  20. package/_dist/headers.d.ts.map +1 -0
  21. package/_dist/log.d.ts +253 -0
  22. package/_dist/log.d.ts.map +1 -0
  23. package/_dist/mod.d.ts +71 -0
  24. package/_dist/mod.d.ts.map +1 -0
  25. package/_dist/publish.d.ts +7 -0
  26. package/_dist/publish.d.ts.map +1 -0
  27. package/_dist/reached.d.ts +145 -0
  28. package/_dist/reached.d.ts.map +1 -0
  29. package/_dist/receipts.d.ts +153 -0
  30. package/_dist/receipts.d.ts.map +1 -0
  31. package/_dist/refuse.d.ts +51 -0
  32. package/_dist/refuse.d.ts.map +1 -0
  33. package/_dist/relay.d.ts +3 -0
  34. package/_dist/relay.d.ts.map +1 -0
  35. package/_dist/request.d.ts +79 -0
  36. package/_dist/request.d.ts.map +1 -0
  37. package/_dist/requests.d.ts +7 -0
  38. package/_dist/requests.d.ts.map +1 -0
  39. package/_dist/route.d.ts +71 -0
  40. package/_dist/route.d.ts.map +1 -0
  41. package/_dist/routed.d.ts +33 -0
  42. package/_dist/routed.d.ts.map +1 -0
  43. package/_dist/routes.d.ts +46 -0
  44. package/_dist/routes.d.ts.map +1 -0
  45. package/_dist/save.d.ts +6 -0
  46. package/_dist/save.d.ts.map +1 -0
  47. package/_dist/socket.d.ts +73 -0
  48. package/_dist/socket.d.ts.map +1 -0
  49. package/_dist/subs.d.ts +134 -0
  50. package/_dist/subs.d.ts.map +1 -0
  51. package/_dist/timing.d.ts +8 -0
  52. package/_dist/timing.d.ts.map +1 -0
  53. package/_dist/tools.d.ts +80 -0
  54. package/_dist/tools.d.ts.map +1 -0
  55. package/_dist/vocab.d.ts +6 -0
  56. package/_dist/vocab.d.ts.map +1 -0
  57. package/access.js +32 -0
  58. package/access.js.map +1 -0
  59. package/access.ts +42 -0
  60. package/actor.js +75 -0
  61. package/actor.js.map +1 -0
  62. package/actor.ts +106 -0
  63. package/address.js +42 -0
  64. package/address.js.map +1 -0
  65. package/address.ts +49 -0
  66. package/admission.js +71 -0
  67. package/admission.js.map +1 -0
  68. package/admission.ts +64 -0
  69. package/asset.js +37 -0
  70. package/asset.js.map +1 -0
  71. package/asset.ts +47 -0
  72. package/batches.js +92 -0
  73. package/batches.js.map +1 -0
  74. package/batches.ts +96 -0
  75. package/body.js +51 -0
  76. package/body.js.map +1 -0
  77. package/body.ts +82 -0
  78. package/deno.js +88 -0
  79. package/deno.js.map +1 -0
  80. package/deno.json +1 -0
  81. package/deno.ts +150 -0
  82. package/doors.js +435 -0
  83. package/doors.js.map +1 -0
  84. package/doors.ts +570 -0
  85. package/graph.js +12 -0
  86. package/graph.js.map +1 -0
  87. package/graph.ts +23 -0
  88. package/headers.js +5 -0
  89. package/headers.js.map +1 -0
  90. package/headers.ts +9 -0
  91. package/interest.js +224 -0
  92. package/interest.js.map +1 -0
  93. package/interest.ts +264 -0
  94. package/log.js +1011 -0
  95. package/log.js.map +1 -0
  96. package/log.ts +1035 -0
  97. package/mod.js +69 -0
  98. package/mod.js.map +1 -0
  99. package/mod.ts +142 -0
  100. package/package.json +75 -4
  101. package/peer_query.js +164 -0
  102. package/peer_query.js.map +1 -0
  103. package/peer_query.ts +158 -0
  104. package/publish.js +7 -0
  105. package/publish.js.map +1 -0
  106. package/publish.ts +16 -0
  107. package/reached.js +456 -0
  108. package/reached.js.map +1 -0
  109. package/reached.ts +549 -0
  110. package/receipts.js +543 -0
  111. package/receipts.js.map +1 -0
  112. package/receipts.ts +749 -0
  113. package/refuse.js +120 -0
  114. package/refuse.js.map +1 -0
  115. package/refuse.ts +179 -0
  116. package/relay.js +249 -0
  117. package/relay.js.map +1 -0
  118. package/relay.ts +284 -0
  119. package/request.js +218 -0
  120. package/request.js.map +1 -0
  121. package/request.ts +235 -0
  122. package/requests.js +250 -0
  123. package/requests.js.map +1 -0
  124. package/requests.ts +257 -0
  125. package/route.js +139 -0
  126. package/route.js.map +1 -0
  127. package/route.ts +228 -0
  128. package/routed.js +25 -0
  129. package/routed.js.map +1 -0
  130. package/routed.ts +53 -0
  131. package/routes.js +154 -0
  132. package/routes.js.map +1 -0
  133. package/routes.ts +201 -0
  134. package/save.js +341 -0
  135. package/save.js.map +1 -0
  136. package/save.ts +393 -0
  137. package/socket.js +398 -0
  138. package/socket.js.map +1 -0
  139. package/socket.ts +496 -0
  140. package/subs.js +2295 -0
  141. package/subs.js.map +1 -0
  142. package/subs.ts +2979 -0
  143. package/timing.js +17 -0
  144. package/timing.js.map +1 -0
  145. package/timing.ts +26 -0
  146. package/tools.js +251 -0
  147. package/tools.js.map +1 -0
  148. package/tools.ts +315 -0
  149. package/vocab.js +18 -0
  150. package/vocab.js.map +1 -0
  151. package/vocab.json +93 -0
  152. package/vocab.ts +18 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route.d.ts","sources":["../route.ts"],"names":[],"mappings":"AASA,cAAc,KAAK,EAAE,QAAQ,2BAAgC;AAI7D,SAAkB,KAAK,QAAQ,EAAO,KAAK,SAAS,qBAAoB;AAGxE,SAAS,KAAK,MAAM,uBAA8B;AAClD,SAAiB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+ /** A web-standard request handler: a `Request` in, a `Response` out. */ export type Handler = (request: Request) => Response | Promise<Response>;
2
+ /** One path served alongside this package's three endpoints — a plugin's
3
+ * route. `path` is matched exactly, or ends in `*` to match a prefix, which
4
+ * is what content-addressed bytes (`/blob/<sha>`) need; `method` is the HTTP
5
+ * method, or `*` for any. Whoever mounts the routes decides which one wins a
6
+ * conflict: this package serves `/apply`, `/query` and `/ws` and claims
7
+ * nothing else. */ export type Route = {
8
+ method: string;
9
+ path: string;
10
+ /** record the route pattern instead of a path carrying credentials */ sensitive?: boolean;
11
+ handle: Handler;
12
+ /** What a person reaching this path finds, said beside its address when a
13
+ * server starts listening: the few doors somebody starting one opens. */ says?: string;
14
+ };
15
+ /** Whether a route matches this request's method and path. A GET route
16
+ * matches a HEAD too, which is a GET answered without its body (RFC 9110
17
+ * §9.3.2): how a page asks a bundle's etag without fetching it. */ export declare let routed: (route: Route, method: string, path: string) => boolean;
18
+ /** The path a request names to doors that answer under `under` as well as at
19
+ * the root: its own path with `under` taken off its front where it starts
20
+ * there, so `/api/query` is `/query` to doors under `/api`, as a page's
21
+ * `./api/` finds them.
22
+ *
23
+ * ```ts
24
+ * import { pathOf } from '@yaks/api/routed'
25
+ * import { equal } from '@yaks/testing'
26
+ * let at = (path: string) => new Request(`http://box${path}`)
27
+ * equal(pathOf(at('/api/query'), '/api'), '/query')
28
+ * equal(pathOf(at('/query'), '/api'), '/query')
29
+ * equal(pathOf(at('/apiary'), '/api'), '/apiary')
30
+ * equal(pathOf(at('/api/query')), '/api/query')
31
+ * ```
32
+ */ export declare let pathOf: (request: Request, under?: string) => string;
33
+ //# sourceMappingURL=routed.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"routed.d.ts","sources":["../routed.ts"],"names":[],"mappings":"AAIA,sEAAsE,GACtE,YAAY,WAAW,SAAS,YAAY,WAAW,QAAQ;AAE/D;;;;;iBAKiB,GACjB,YAAY;EACV,QAAQ,MAAM;EACd,MAAM,MAAM;EACZ,oEAAoE,GACpE,YAAY,OAAO;EACnB,QAAQ;EACR;yEACuE,GACvE,OAAO,MAAM;;AAGf;;iEAEiE,GACjE,OAAO,YAAI,SAAU,OAAO,OAAO,QAAQ,MAAM,EAAE,MAAM,MAAM,KAAG,OAAO,CAKhD;AAEzB;;;;;;;;;;;;;;CAcC,GACD,OAAO,YAAI,SAAU,SAAS,SAAS,QAAQ,MAAM,KAAG,MAAM,CAG7D"}
@@ -0,0 +1,46 @@
1
+ import type { Bundle, Graph, News, Storage } from "@yaks/graph";
2
+ import type { Authenticate } from "./actor.js";
3
+ import { type Filter, type Handler, type Route } from "./route.js";
4
+ import type { Timer } from "./relay.js";
5
+ import { type Upgrade } from "./socket.js";
6
+ /** What this facet reads off the host it is composing into: the graph its
7
+ * endpoints answer over, who that host says is calling, every route the
8
+ * listed plugins contributed, every filter they put in front, and the commits
9
+ * other hosts make to the same store, which `/ws` subscribers are told of as
10
+ * well as this graph's own. */ export type Hosting = {
11
+ graph: Graph;
12
+ storage?: Pick<Storage, 'revision'>;
13
+ stopping?: AbortSignal;
14
+ who: Authenticate;
15
+ routes: Route[];
16
+ filters?: Filter[];
17
+ feed?: (each: (news: News) => void | Promise<void>) => () => void;
18
+ /** Host telemetry, supplied with the failing request bundle. */ report?: (error: unknown, context: {
19
+ request: Bundle;
20
+ during: {
21
+ request: string;
22
+ };
23
+ }) => void | Promise<void>;
24
+ };
25
+ /**
26
+ * The host's one request handler: each plugin's route, and this package's
27
+ * three endpoints, behind every plugin's filter. A filter that throws answers
28
+ * the request with that refusal, and nothing past it runs: a `/ws` upgrade it
29
+ * refuses (a `Refused`) opens, and answers each subscription with it. An
30
+ * answer at 500 or over, from a route, a filter or a door, goes to
31
+ * `host.report` with a `request` bundle named by its route (./request.ts
32
+ * `served`).
33
+ *
34
+ * The route that names the path most closely wins, whichever plugin listed it:
35
+ * an exact path over a prefix, a longer prefix over a shorter, and plugin order
36
+ * between equals. `/apply`, `/query` and `/ws` are exact paths, so a plugin's
37
+ * catch-all (`/*`) answers only what nothing else claims. A request no route
38
+ * claimed goes to the doors, which refuse anything they do not serve.
39
+ */ export declare let handler: (host: Hosting, options?: {
40
+ /** Time between store revision checks, in ms (default 1000). */ every?: number;
41
+ /** Schedule a revision check and return its cancellation function. */ timer?: Timer;
42
+ /** How a `/ws` request becomes a socket (default: Deno's). */ upgrade?: Upgrade;
43
+ /** A path the doors answer under as well as at the root (`/api`, where a
44
+ * page's `./api/` client finds its store). */ under?: string;
45
+ }) => Handler;
46
+ //# sourceMappingURL=routes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"routes.d.ts","sources":["../routes.ts"],"names":[],"mappings":"AAcA,cAAc,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,2BAAgC;AAC1E,cAAc,YAAY,qBAAoB;AAC9C,SAGE,KAAK,MAAM,EACX,KAAK,OAAO,EAEZ,KAAK,KAAK,qBAEO;AAGnB,cAAc,KAAK,qBAAoB;AAEvC,SAAmB,KAAK,OAAO,sBAAqB;AAmBpD;;;;6BAI6B,GAC7B,YAAY;EACV,OAAO;EACP,UAAU,KAAK,SAAS;EACxB,WAAW;EACX,KAAK;EACL,QAAQ;EACR,UAAU;EACV,QAAQ,OAAO,MAAM,SAAS,IAAI,GAAG,QAAQ,IAAI,YAAY,IAAI;EACjE,8DAA8D,GAC9D,UACE,OAAO,OAAO,EACd;IAAW,SAAS;IAAQ;MAAU,SAAS,MAAM;;QAClD,IAAI,GAAG,QAAQ,IAAI;;AAQ1B;;;;;;;;;;;;;;CAcC,GACD,OAAO,YAAI,UAAW,MAAM,SAAS;EACnC,8DAA8D,GAC9D,QAAQ,MAAM;EACd,oEAAoE,GACpE,QAAQ;EACR,4DAA4D,GAC5D,UAAU;EACV;8CAC4C,GAC5C,QAAQ,MAAM;MACP,QAsGR"}
@@ -0,0 +1,6 @@
1
+ import type { Actor } from "@yaks/graph";
2
+ /** The receiving door's identity and vocabulary versions, never client claims. */ export type PeerWriter = {
3
+ actor?: Actor | null;
4
+ speaks?: Record<string, number>;
5
+ };
6
+ //# sourceMappingURL=save.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"save.d.ts","sources":["../save.ts"],"names":[],"mappings":"AAIA,cAAc,KAAK,2BAAqD;AAQxE,gFAAgF,GAChF,YAAY;EACV,QAAQ,QAAQ,IAAI;EACpB,SAAS,OAAO,MAAM,EAAE,MAAM"}
@@ -0,0 +1,73 @@
1
+ import type { ReadOpts } from "@yaks/graph";
2
+ import type { Frame, Listening, Sink } from "./subs.js";
3
+ import type { PeerWriter } from "./save.js";
4
+ /** The part of a WebSocket this package uses: the standard `WebSocket`
5
+ * satisfies it, and so does a Cloudflare Worker's server-side half. */ export type Socket<Frame = string> = {
6
+ /** `0` connecting, `1` open — frames sent before it opens are queued */ readyState: number;
7
+ /** bytes waiting inside the WebSocket transport */ bufferedAmount?: number;
8
+ /** send one frame, already serialized */ send(data: Frame): void;
9
+ /** stop a peer that keeps sending beyond its relay allowance */ close?(code?: number, reason?: string): void;
10
+ /** listen for `open`, `message` and `close` */ addEventListener(type: string, listener: (event: Event & {
11
+ data?: unknown;
12
+ }) => void): void;
13
+ };
14
+ /**
15
+ * How the runtime turns a request into a WebSocket. This is the one thing
16
+ * about serving that no web standard covers, so the application supplies it:
17
+ * the Deno default is
18
+ * {@link https://jsr.io/@yaks/api/doc/~/denoUpgrade | denoUpgrade}, and on
19
+ * Cloudflare Workers you build one from `WebSocketPair` (see the README).
20
+ */ export type Upgrade<Frame = string> = (request: Request) => {
21
+ socket: Socket<Frame>;
22
+ response: Response;
23
+ };
24
+ /** Decode one incoming frame before anyone dispatches it. A hibernating socket
25
+ * can use the decoded shape to route its own work without parsing it again. */ export type Incoming = {
26
+ error: unknown;
27
+ id?: string;
28
+ } | {
29
+ value: unknown;
30
+ payload?: Uint8Array;
31
+ };
32
+ export declare let decode: (data: unknown) => Incoming;
33
+ /** Answer a finite client clock token directly, outside subscription/ack queues.
34
+ * `received` is epoch milliseconds captured at the socket's message boundary;
35
+ * `now` supplies epoch milliseconds immediately before sending the reply.
36
+ * Returns false for messages that are not valid clock requests. */ export declare let clockReply: (socket: Pick<Socket, 'send'>, value: unknown, received: number, now?: () => number) => boolean;
37
+ /** A replaceable frame queue for a socket, including hibernatable sockets
38
+ * whose incoming messages are delivered by a Durable Object method. */ export declare let queue: (socket: Pick<Socket, 'send' | 'bufferedAmount'>, timer?: (fn: () => void, ms: number) => void, ready?: () => boolean, resume?: {
39
+ owed?: string;
40
+ sent?: (frames: Frame[], token: string) => void;
41
+ acked?: (owed?: string) => void;
42
+ fits?: (frames: Frame[]) => boolean;
43
+ }) => {
44
+ send: Sink;
45
+ flush: () => void;
46
+ close: () => void;
47
+ enable: (frames?: boolean) => void;
48
+ ack: (token: string) => void;
49
+ };
50
+ /** A {@link Sink} that writes frames to a socket, queueing them until it
51
+ * opens. */ export declare let sink: (socket: Socket, timer?: (fn: () => void, ms: number) => void) => Sink;
52
+ /**
53
+ * One message from a client, dispatched: `{subscribe, id}` opens a
54
+ * subscription (a query string, or `true` for the raw feed of every committed
55
+ * transaction), `{unsubscribe}` closes one, and `{relay: [...]}` forwards
56
+ * `sync: peers` components to the other subscribers. `{tab}` is a page saying
57
+ * whether it is in view (@yaks/client), for a host that keeps where people are
58
+ * to read before this; here it is nothing. Anything else is refused under its
59
+ * own id.
60
+ *
61
+ * A relay is the only write that crosses this connection, and it leaves the
62
+ * connection it arrived on is what holds it. A component declaring `save`
63
+ * also stores periodic snapshots through the graph as this connection's writer
64
+ * — which is precisely why it cannot go through `/apply`, a separate request
65
+ * with no connection to name.
66
+ */ export declare let receive: (subs: Listening, to: Sink, data: unknown, now?: () => number, input?: Incoming, opts?: ReadOpts, writer?: PeerWriter) => void | 'close' | Promise<void>;
67
+ /**
68
+ * Connect a socket to a subscription registry: its messages become
69
+ * subscriptions, and closing it drops them all. Returns the sink its frames
70
+ * go to, which is also the key its subscriptions are held under. Without a
71
+ * `now`, relay pace reads the registry's ({@link Listening}).
72
+ */ export declare let attach: (subs: Listening, socket: Socket, timer?: (fn: () => void, ms: number) => void, now?: () => number, opts?: ReadOpts, writer?: PeerWriter) => Sink;
73
+ //# sourceMappingURL=socket.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"socket.d.ts","sources":["../socket.ts"],"names":[],"mappings":"AAgBA,cAAsB,QAAQ,2BAAgC;AAI9D,cAAc,KAAK,EAAE,SAAS,EAAE,IAAI,oBAAmB;AACvD,cAAc,UAAU,oBAAmB;AAE3C;qEACqE,GACrE,YAAY,OAAO,QAAQ,MAAM;EAC/B,sEAAsE,GACtE,YAAY,MAAM;EAClB,iDAAiD,GACjD,iBAAiB,MAAM;EACvB,uCAAuC,GACvC,KAAK,MAAM,QAAQ,IAAI;EACvB,8DAA8D,GAC9D,OAAO,OAAO,MAAM,EAAE,SAAS,MAAM,GAAG,IAAI;EAC5C,6CAA6C,GAC7C,iBACE,MAAM,MAAM,EACZ,WAAW,OAAO;IAAU,OAAO,OAAO;QAAO,IAAI,GACpD,IAAI;;AAGT;;;;;;CAMC,GACD,YAAY,QAAQ,QAAQ,MAAM,KAChC,SAAS;EACJ,QAAQ,OAAO;EAAQ,UAAU;;AAcxC;6EAC6E,GAC7E,YAAY;EACN,OAAO,OAAO;EAAE,KAAK,MAAM;;EAC3B,OAAO,OAAO;EAAE,UAAU;;AAsChC,OAAO,YAAI,SAAU,MAAM,OAAO,KAAG,SA0BpC;AAED;;;iEAGiE,GACjE,OAAO,YAAI,aACT,QAAQ,KAAK,QAAQ,SACrB,OAAO,OAAO,EACd,UAAU,MAAM,EAChB,YAAW,MAAM,KAChB,OAAO,CAMT;AAiBD;qEACqE,GACrE,OAAO,YAAI,QACT,QAAQ,KAAK,QAAQ,SAAS,mBAC9B,SAAQ,UAAU,IAAI,EAAE,IAAI,MAAM,KAAK,IAAI,EAG3C,cAAa,OAAO,EACpB;EACE,OAAO,MAAM;EACb,QAAQ,QAAQ,SAAS,OAAO,MAAM,KAAK,IAAI;EAC/C,SAAS,OAAO,MAAM,KAAK,IAAI;EAC/B,QAAQ,QAAQ,YAAY,OAAO;;EAGrC,MAAM;EACN,aAAa,IAAI;EACjB,aAAa,IAAI;EACjB,SAAS,SAAS,OAAO,KAAK,IAAI;EAClC,MAAM,OAAO,MAAM,KAAK,IAAI;EA4G7B;AAED;UACU,GACV,OAAO,YAAI,OACT,QAAQ,QACR,SAAS,UAAU,IAAI,EAAE,IAAI,MAAM,KAAK,IAAI,KAC3C,KAKF;AAED;;;;;;;;;;;;;;CAcC,GACD,OAAO,YAAI,UACT,MAAM,WACN,IAAI,MACJ,MAAM,OAAO,EACb,YAAY,MAAM,EAClB,QAAO,UACP,OAAO,UACP,SAAS,eACR,IAAI,GAAG,UAAU,QAAQ,IAAI,EAwB/B;AAwED;;;;;CAKC,GACD,OAAO,YAAI,SACT,MAAM,WACN,QAAQ,QACR,SAAS,UAAU,IAAI,EAAE,IAAI,MAAM,KAAK,IAAI,EAC5C,YAAY,MAAM,EAClB,OAAO,UACP,SAAS,eACR,KAwDF"}
@@ -0,0 +1,134 @@
1
+ import type { Bundle, Eid, Graph, News, ReadOpts, Reads, Taken } from "@yaks/graph";
2
+ import type { TransientFrame } from "@yaks/graph";
3
+ import type { Coverage, Reduced } from "@yaks/graph";
4
+ import { type Refusal } from "./refuse.js";
5
+ import { type Timer } from "./relay.js";
6
+ import { type PeerWriter } from "./save.js";
7
+ /** Which revision of the store a frame's rows were read at: one number, or,
8
+ * from a door over several stores, each store's under its name (@yaks/reach).
9
+ * A store's revision only moves forward, across restarts too, so of two
10
+ * frames about one row the one with the higher revision is the newer. */ export type Revision = number | Record<string, number>;
11
+ /**
12
+ * One push to one subscriber. `bundles` are whole entities that are now in
13
+ * the set — for the raw feed (`subscribe: true`), the committed transaction
14
+ * as it was applied, one bundle per entity, exactly as its writer's `/apply`
15
+ * response read. `gone` names the entities that left the set, whether they
16
+ * were deleted or merely stopped matching. `refused` replaces both when the
17
+ * subscription could not be opened.
18
+ */ export type Frame = {
19
+ /** Peer values held by this connection need to be sent again after wake. */ resay?: true;
20
+ /** the token a socket subscriber echoes after applying this frame */ ack?: string;
21
+ /** a whole answer replacing the subscriber's previous membership and order */ reset?: boolean;
22
+ transient?: TransientFrame[];
23
+ transientReset?: Eid[];
24
+ /** the subscription this frame answers */ id: string;
25
+ /** the store's revision when the frame left: its rows are no newer */ revision?: Revision;
26
+ /** the revision of the subscription's previous frame, absent on its first:
27
+ * a subscriber that last heard another has missed one */ since?: Revision;
28
+ /** the entities now in the set (whole), or the composed transaction for a
29
+ * raw feed */ bundles?: Bundle[];
30
+ /** what each of `bundles` covers, where a `.fields` projection narrowed it:
31
+ * a property covered and left out is absent, one not covered was not read */ coverage?: Record<Eid, Coverage>;
32
+ /** the entities a `.fields` projection's paths reach from the set, each
33
+ * narrowed to what was read off it: carried along, never in the set */ peers?: Bundle[];
34
+ /** what each of `peers` covers */ peerCoverage?: Record<Eid, Coverage>;
35
+ /** entities no path reaches any more */ peerGone?: Eid[];
36
+ /** entities that left the set — deleted, or no longer matching */ gone?: Eid[];
37
+ /**
38
+ * `sync: peers` components being relayed: a cursor, a caret, a presence
39
+ * dot. Components declaring `save` also retain a stored snapshot. A value cleared by its writer — or by
40
+ * that writer's connection closing, or by its own duration running out —
41
+ * arrives as the component set to `null`.
42
+ */ relay?: Bundle[];
43
+ /** why the subscription was refused, when it was */ refused?: Refusal;
44
+ } & Partial<Reduced>;
45
+ /** Where a subscriber's frames go. One sink per client — the socket layer
46
+ * makes one per connection, and the registry keys subscriptions by it. */ export type Sink = (frame: Frame) => void;
47
+ /** What a subscriber asks for: a query string, or `true` for the raw feed of
48
+ * every committed transaction. */ export type Ask = string | true;
49
+ /** Saved watches recovered together after their owner lost its memory, each
50
+ * with its opener: the read it asks as and the writer its socket holds. */ export type Opening = {
51
+ sink: Sink;
52
+ id: string;
53
+ query: Ask;
54
+ opts?: ReadOpts;
55
+ writer?: PeerWriter;
56
+ };
57
+ /** The subscription registry: what the socket layer talks to, and what an
58
+ * application can drive directly. */ export type Subs = {
59
+ /** Vocabulary for decoding binary relay properties at the JSON door. */ vocab?: import("@yaks/vocab").Vocab;
60
+ /** Runtime activity target, shared with the graph even through read overlays. */ activity?: object;
61
+ /** Read bundles using the graph's query and read-option contract, with held
62
+ * peer values unless `durable: true` selects storage alone. */ read: Graph['read'];
63
+ /** Observe changes to held peer values after registry work has released.
64
+ * Observers may read the registry or write the graph. */ observe: (fn: (bundles: Bundle[]) => void | Promise<unknown>) => () => void;
65
+ /** Answer one query from storage and the peer values held right now. */ snapshot: Reads<Bundle[] | Reduced | Promise<Bundle[] | Reduced>>;
66
+ /** open (or replace) a subscription and send its current set, as `opts`
67
+ * reads; `writer` is who opened it, for a registry that vouches its opener
68
+ * onward ({@link import('./reached.ts').reached}) */ open: (sink: Sink, id: string, query: Ask, opts?: ReadOpts, writer?: PeerWriter) => void | Promise<void>;
69
+ /** Reopen saved watches. A cold peer-only recovery may register proven
70
+ * static durable interests without repeating the page's held snapshot. */ restore: (openings: Opening[], deferStatic?: boolean) => void | Promise<void>;
71
+ /** Answer deferred static watches on an explicit read/handshake. */ resume?: () => void | Promise<void>;
72
+ /** Stop observation and cancel registry reads; admitted saves keep running. */ stop: () => void;
73
+ /** close one subscription */ close: (sink: Sink, id: string) => void | Promise<void>;
74
+ /** close every subscription a sink holds — a client went away */ drop: (sink: Sink) => void | Promise<void>;
75
+ /** a transaction committed: push what changed to whoever is watching */ commit: (applied: Bundle[]) => void | Promise<void>;
76
+ /** what other hosts committed, as a feed hands it: pushed as one pass, and
77
+ * every answer read again first when the feed missed some */ hear: (news: News) => void | Promise<void>;
78
+ /** Reconcile query answers when the store's revision changed outside this
79
+ * registry. The host decides when to check; opening also checks before
80
+ * reusing a retained answer. */ check: () => void | Promise<void>;
81
+ /**
82
+ * `sync: peers` components from one sink: forwarded to everyone else
83
+ * watching those entities, and held under this sink until it closes
84
+ * (relay.ts). Components declaring `save` also keep snapshots through apply.
85
+ * Queries that read peer
86
+ * components update their membership against the value now held.
87
+ */ relay: (sink: Sink, bundles: Bundle[], writer?: PeerWriter) => void | Promise<void>;
88
+ /** Socket inputs share one short fan-out batch across writers. Held values
89
+ * and ownership change immediately; the returned promise follows delivery. */ enqueue?: (sink: Sink, bundles: Bundle[], writer?: PeerWriter) => void | Promise<void>;
90
+ /** The vocabulary's cadence for a peer component, if it declares one. */ pace?: (comp: string) => number | null;
91
+ /** The query deciding when a peer component is saved. */ save?: (comp: string) => string | null;
92
+ /** The registry's clock, as {@link subscriptions} was given it: the time a
93
+ * socket serving it reads (relay pace, clock replies) and how it sets its
94
+ * timers (frame queues). Unset, the runtime's. */ now?: () => number;
95
+ timer?: Timer;
96
+ /** The keys one sink's relayed values are held under — small enough to
97
+ * store somewhere that outlives this process's memory. */ relaying: (sink: Sink) => string[];
98
+ /** Take those keys back after such a loss, so a close still clears them. */ relayed: (sink: Sink, keys: string[]) => void | Promise<void>;
99
+ };
100
+ /** What a socket asks of a registry (./socket.ts `attach`): to open, close
101
+ * and drop subscriptions and take relayed values. A {@link Subs} is one, and
102
+ * so is a registry over graphs it can only ask (./reached.ts). */ export type Listening = Pick<Subs, 'open' | 'close' | 'drop' | 'relay'> & Partial<Pick<Subs, 'activity' | 'vocab' | 'enqueue' | 'pace' | 'now'>>;
103
+ /** A listener that can resume the interests of a hibernating socket. */ export type RecoverableListening = Listening & Pick<Subs, 'restore' | 'resume' | 'relaying' | 'relayed' | 'timer' | 'save'>;
104
+ /**
105
+ * A subscription registry over a graph. It registers an `effect` hook on that
106
+ * graph, so every transaction that commits — through this API or not —
107
+ * reaches whoever is watching. Build one per graph;
108
+ * {@link https://jsr.io/@yaks/api | api()} makes one when you do not pass
109
+ * your own.
110
+ *
111
+ * ```ts ignore
112
+ * let subs = subscriptions(graph)
113
+ * subs.open(sink, 'cheap', '.book&.book.price<20')
114
+ * ```
115
+ */ export declare let subscriptions: (graph: Graph, opts?: {
116
+ /** The composed graph when `graph` is only its read overlay. */ activity?: object;
117
+ /** A store token that changes for commits through other graph instances. */ revision?: () => unknown;
118
+ /** A host may budget and page reads of a subscription's stored rows. */ readWindow?: (query: Taken, read: (query: Taken) => Bundle[] | Promise<Bundle[]>, signal?: AbortSignal) => Bundle[] | Promise<Bundle[]>;
119
+ /** Give other requests a turn between inherited snapshot reads. */ yieldRecovery?: () => Promise<void>;
120
+ /** A query may depend on entities outside its result (for example a computed
121
+ * session status depends on transcript entries). Returning true refreshes
122
+ * that subscription after this commit. It does not subscribe to those rows. */ invalidate?: (query: string, applied: Bundle[]) => boolean;
123
+ /** How every timer of the registry is set: a `durable: "5s"` relayed
124
+ * value's expiry, a pending save's retry, a relay batch's flush and the
125
+ * frame queue of a socket serving it (default: setTimeout). */ timer?: Timer;
126
+ /** The registry's time: what pending save queries and the relay pace of a
127
+ * socket serving it read (default: Date.now). */ now?: () => number;
128
+ /** How many closed answers stand at once for a page to ask again (default
129
+ * 256). */ keep?: number;
130
+ /** Told of every subscription the registry refuses and closes, with its
131
+ * query: the caller's own refusals too, which `fault` leaves unreported,
132
+ * for a host that knows whose query it was to judge. */ refused?: (error: unknown, query: string) => void;
133
+ }) => Subs;
134
+ //# sourceMappingURL=subs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subs.d.ts","sources":["../subs.ts"],"names":[],"mappings":"AAmCA,cACE,MAAM,EACN,GAAG,EACH,KAAK,EACL,IAAI,EAEJ,QAAQ,EACR,KAAK,EACL,KAAK,2BACwB;AAK/B,cAAc,cAAc,2BAAgC;AAG5D,cAEE,QAAQ,EAIR,OAAO,2BACsB;AA0B/B,SAAgB,KAAK,OAAO,sBAA8B;AAC1D,SAAwC,KAAK,KAAK,qBAAoB;AACtE,SAAS,KAAK,UAAU,oBAA2B;AAEnD;;;uEAGuE,GACvE,YAAY,WAAW,MAAM,GAAG,OAAO,MAAM,EAAE,MAAM;AAErD;;;;;;;CAOC,GACD,YAAY;EACV,0EAA0E,GAC1E,QAAQ,IAAI;EACZ,mEAAmE,GACnE,MAAM,MAA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D,GAC7D,QAAQ;EACR;iDAC+C,GAC/C,YAAY,MAAM;EAClB;WACS,GACT,OAAO,MAAM;EACb;;wDAEsD,GACtD,WAAW,OAAO,OAAO,EAAE,OAAO,MAAM,KAAK,IAAI;MAC1C,KAy1ER"}
@@ -0,0 +1,8 @@
1
+ /** A `fetch` that prints one line per response:
2
+ *
3
+ * POST /mcp 200 door;dur=12, hops;dur=3, total;dur=41
4
+ *
5
+ * The numbers are the server's own `Server-Timing` header (yaks.app writes it
6
+ * in workers/yak/timing.ts), printed exactly as they arrived, so this line and
7
+ * a `curl -i` agree. A server that sends no such header still gets a line. */ export declare let timed: (say: (line: string) => void, go?: (request: Request) => Response | Promise<Response>) => any;
8
+ //# sourceMappingURL=timing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timing.d.ts","sources":["../timing.ts"],"names":[],"mappings":"AAIA;;;;;;4EAM4E,GAC5E,OAAO,YAAI,QACT,MAAM,MAAM,MAAM,KAAK,IAAI,EAC3B,MAAK,SAAS,YAAY,WAAW,QAAQ,kBAY9C"}
@@ -0,0 +1,80 @@
1
+ import type { Runs } from "@yaks/graph/tools";
2
+ import type { Runner } from "@yaks/tools";
3
+ import { type Listen } from "./deno.js";
4
+ import type { Handler, Route } from "./route.js";
5
+ export { HOSTNAME, PORT, served } from "./address.js";
6
+ /** Seconds a stopping server lets requests finish before cancelling reads
7
+ * and cutting streamed answers. Admitted writes drain even after this grace
8
+ * expires. */ export declare let GRACE: number;
9
+ /** What this tool needs from the host that composed it: the request handler to
10
+ * answer with, the duties it takes over while it is up, and the config
11
+ * that said where to listen. */ export type Serving = {
12
+ config: {
13
+ db?: string;
14
+ port?: number;
15
+ hostname?: string;
16
+ };
17
+ /** what this host answers with. Optional because a host has one only where
18
+ * a plugin built one, and ./routes.ts is that plugin — so a `Serving` put
19
+ * together by hand may carry none. */ handler?: Handler;
20
+ /** the plugins' routes: each that `says` what it is is told on listening */ routes?: Pick<Route, 'path' | 'says'>[];
21
+ runner: Runner;
22
+ duties: (signal?: AbortSignal) => Promise<void>;
23
+ /** close for the processes on this machine that ended without closing
24
+ * (@yaks/cli host.ts `bury`); a host that tracks no processes has none */ bury?: () => Promise<unknown>;
25
+ stopping: AbortSignal;
26
+ /** warm the host (@yaks/host `Host.warm`): a server taking over a port
27
+ * (`share`) warms before it listens, any other once it listens, and every
28
+ * server before it says it is ready; one that fails ends the server */ warm?: () => Promise<void>;
29
+ };
30
+ /** `handler`, serving the graph file `db`, for a request whose {@link DB}
31
+ * header names the file it means: the file this server serves is answered
32
+ * with the header said back, which is how a client knows the answer came from
33
+ * its graph, and any other is answered 421 Misdirected Request, with nothing
34
+ * run. A request naming none is answered as it always was.
35
+ *
36
+ * ```ts
37
+ * import { assertEquals } from '@std/assert'
38
+ * import { DB } from '@yaks/api/address'
39
+ * import { addressed } from '@yaks/api/tools'
40
+ *
41
+ * let handle = addressed(() => new Response('ok'), '/g.db')
42
+ * let asked = (db?: string) =>
43
+ * handle(new Request('http://x/', { headers: db ? { [DB]: db } : {} }))
44
+ * let mine = await asked('/g.db')
45
+ * assertEquals([mine.status, mine.headers.get(DB)], [200, '/g.db'])
46
+ * assertEquals((await asked()).headers.get(DB), null)
47
+ * assertEquals((await asked('/copy.db')).status, 421)
48
+ * ```
49
+ */ export declare let addressed: (handler: Handler, db?: string) => Handler;
50
+ /** What a server says once it listens at `at`: the address and the graph,
51
+ * then each door a route says something about, at `origin`, where a client on
52
+ * this machine reaches it.
53
+ *
54
+ * ```ts
55
+ * import { assertEquals } from '@std/assert'
56
+ * import { listening } from '@yaks/api/tools'
57
+ *
58
+ * assertEquals(
59
+ * listening('http://0.0.0.0:80', 'http://127.0.0.1:80', {
60
+ * config: { db: 'g.db' },
61
+ * routes: [{ path: '/', says: 'the canvas' }, { path: '/ws' }],
62
+ * }),
63
+ * 'serve — http://0.0.0.0:80 · g.db\n' +
64
+ * ' http://127.0.0.1:80/ the canvas\n' +
65
+ * 'Ctrl-C stops it',
66
+ * )
67
+ * ```
68
+ */ export declare let listening: (at: string, origin: string, host: Pick<Serving, 'config' | 'routes'>) => string;
69
+ /** An additional transport into the same handler, with trusted headers set
70
+ * by its listener rather than accepted from HTTP input. */ export type Endpoint = {
71
+ port: number;
72
+ hostname?: string;
73
+ headers?: Record<string, string>;
74
+ };
75
+ /** A handler whose ingress sets these headers, replacing caller input. */ export declare let entering: (handler: Handler, headers?: Record<string, string>) => Handler;
76
+ /** The implementation of the tool ./vocab.json declares. */ export declare let runs: (host: Serving, options?: {
77
+ token?: string;
78
+ listeners?: Endpoint[];
79
+ }, listen?: Listen) => Runs;
80
+ //# sourceMappingURL=tools.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tools.d.ts","sources":["../tools.ts"],"names":[],"mappings":"AA2CA,cAAc,IAAI,iCAAsC;AACxD,cAAc,MAAM,2BAAgC;AAEpD,SAGE,KAAK,MAAM,oBAGK;AAGlB,cAAc,OAAO,EAAE,KAAK,qBAAoB;AAEhD,SAAS,QAAQ,EAAE,IAAI,EAAE,MAAM,uBAAsB;AAErD;;YAEY,GACZ,OAAO,YAAI,cAAU;AAErB;;8BAE8B,GAC9B,YAAY;EACV;IAAU,KAAK,MAAM;IAAE,OAAO,MAAM;IAAE,WAAW,MAAM;;EACvD;;sCAEoC,GACpC,UAAU;EACV,0EAA0E,GAC1E,SAAS,KAAK,OAAO,SAAS;EAC9B,QAAQ;EACR,SAAS,SAAS,gBAAgB,QAAQ,IAAI;EAC9C;0EACwE,GACxE,aAAa,QAAQ,OAAO;EAC5B,UAAU;EACV;;uEAEqE,GACrE,aAAa,QAAQ,IAAI;;AAK3B;;;;;;;;;;;;;;;;;;;CAmBC,GACD,OAAO,YAAI,YACR,SAAS,SAAS,KAAK,MAAM,KAAG,QAehC;AAEH;;;;;;;;;;;;;;;;;;CAkBC,GACD,OAAO,YAAI,YACT,IAAI,MAAM,EACV,QAAQ,MAAM,EACd,MAAM,KAAK,SAAS,WAAW,cAC9B,MAAM,CASR;AAED;yDACyD,GACzD,YAAY;EACV,MAAM,MAAM;EACZ,WAAW,MAAM;EACjB,UAAU,OAAO,MAAM,EAAE,MAAM;;AAGjC,wEAAwE,GACxE,OAAO,YAAI,WACR,SAAS,SAAS,UAAS,OAAO,MAAM,EAAE,MAAM,MAAS,QAOzD;AAEH,0DAA0D,GAC1D,OAAO,YAAI,OACT,MAAM,SACN;EAAW,QAAQ,MAAM;EAAE,YAAY;GACvC,SAAQ,WACP,KAiID"}
@@ -0,0 +1,6 @@
1
+ import type { VocabDoc } from "@yaks/vocab";
2
+ /** The components and the tool this plugin declares. */ export declare let apiDoc: VocabDoc;
3
+ /** What this package is: its manifest's description, the one place it is
4
+ * written, which a host stamps on each document here. */ export declare let description: string;
5
+ /** Every document this plugin declares. */ export declare let docs: VocabDoc[];
6
+ //# sourceMappingURL=vocab.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab.d.ts","sources":["../vocab.ts"],"names":[],"mappings":"AAKA,cAAc,QAAQ,2BAAgC;AAItD,sDAAsD,GACtD,OAAO,YAAI,QAAQ,SAAc;AAEjC;uDACuD,GACvD,OAAO,YAAI,aAAa,MAAM,CAAuB;AAErD,yCAAyC,GACzC,OAAO,YAAI,MAAM,WAAqB"}
package/access.js ADDED
@@ -0,0 +1,32 @@
1
+ // Access to a machine's HTTP listener. Only its transport can identify a local
2
+ // caller; an HTTP Host, forwarded header or session id cannot authorize one.
3
+ let loopback = (host)=>{
4
+ if (host == '::1' || host == '0:0:0:0:0:0:0:1') return true;
5
+ let ip = host.replace(/^::ffff:/i, '').split('.');
6
+ return ip.length == 4 && ip[0] == '127' && ip.every((n)=>/^\d{1,3}$/.test(n) && Number(n) <= 255);
7
+ };
8
+ // Inspect every byte of the expected credential, including on a mismatch.
9
+ let matches = (expected, supplied)=>{
10
+ let enc = new TextEncoder();
11
+ let a = enc.encode(expected), b = enc.encode(supplied);
12
+ let diff = a.length ^ b.length;
13
+ for(let i = 0; i < a.length; i++)diff |= a[i] ^ (b[i] ?? 0);
14
+ return diff == 0;
15
+ };
16
+ /** Admit loopback peers, or a bearer token read afresh for each remote request.
17
+ * Missing transport information fails closed, including when HTTP headers
18
+ * claim the request is local. This wraps every route before dispatch. */ export let secured = (handler, token)=>(request, peer)=>{
19
+ if (peer?.remoteAddr && loopback(peer.remoteAddr.hostname)) {
20
+ return handler(request);
21
+ }
22
+ let expected = token();
23
+ let supplied = /^Bearer ([^\s]+)$/i.exec(request.headers.get('authorization') ?? '')?.[1];
24
+ if (typeof expected == 'string' && expected.length && supplied && matches(expected, supplied)) return handler(request);
25
+ return new Response('a bearer credential is required for remote access', {
26
+ status: 401,
27
+ headers: {
28
+ 'www-authenticate': 'Bearer'
29
+ }
30
+ });
31
+ };
32
+ //# sourceMappingURL=access.js.map
package/access.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"access.js","sources":["./access.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,6EAA6E;AAI7E,IAAI,WAAW,CAAC;EACd,IAAI,QAAQ,SAAS,QAAQ,mBAAmB,OAAO;EACvD,IAAI,KAAK,KAAK,OAAO,CAAC,aAAa,IAAI,KAAK,CAAC;EAC7C,OAAO,GAAG,MAAM,IAAI,KAAK,EAAE,CAAC,EAAE,IAAI,SAChC,GAAG,KAAK,CAAC,CAAC,IAAM,YAAY,IAAI,CAAC,MAAM,OAAO,MAAM;AACxD;AAEA,0EAA0E;AAC1E,IAAI,UAAU,CAAC,UAAkB;EAC/B,IAAI,MAAM,IAAI;EACd,IAAI,IAAI,IAAI,MAAM,CAAC,WAAW,IAAI,IAAI,MAAM,CAAC;EAC7C,IAAI,OAAO,EAAE,MAAM,GAAG,EAAE,MAAM;EAC9B,IAAK,IAAI,IAAI,GAAG,IAAI,EAAE,MAAM,EAAE,IAAK,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC;EAC5D,OAAO,QAAQ;AACjB;AAEA;;uEAEuE,GACvE,OAAO,IAAI,UACT,CAAC,SAAkB,QAAmC,CAAC,SAAS;IAC9D,IAAI,MAAM,cAAc,SAAS,KAAK,UAAU,CAAC,QAAQ,GAAG;MAC1D,OAAO,QAAQ;IACjB;IACA,IAAI,WAAW;IACf,IAAI,WAAW,qBAAqB,IAAI,CACtC,QAAQ,OAAO,CAAC,GAAG,CAAC,oBAAoB,KACvC,CAAC,EAAE;IACN,IACE,OAAO,YAAY,YAAY,SAAS,MAAM,IAAI,YAClD,QAAQ,UAAU,WAClB,OAAO,QAAQ;IACjB,OAAO,IAAI,SAAS,qDAAqD;MACvE,QAAQ;MACR,SAAS;QAAE,oBAAoB;MAAS;IAC1C;EACF,EAAC"}
package/access.ts ADDED
@@ -0,0 +1,42 @@
1
+ // Access to a machine's HTTP listener. Only its transport can identify a local
2
+ // caller; an HTTP Host, forwarded header or session id cannot authorize one.
3
+ import type { Incoming } from './deno.ts'
4
+ import type { Handler } from './route.ts'
5
+
6
+ let loopback = (host: string) => {
7
+ if (host == '::1' || host == '0:0:0:0:0:0:0:1') return true
8
+ let ip = host.replace(/^::ffff:/i, '').split('.')
9
+ return ip.length == 4 && ip[0] == '127' &&
10
+ ip.every((n) => /^\d{1,3}$/.test(n) && Number(n) <= 255)
11
+ }
12
+
13
+ // Inspect every byte of the expected credential, including on a mismatch.
14
+ let matches = (expected: string, supplied: string) => {
15
+ let enc = new TextEncoder()
16
+ let a = enc.encode(expected), b = enc.encode(supplied)
17
+ let diff = a.length ^ b.length
18
+ for (let i = 0; i < a.length; i++) diff |= a[i] ^ (b[i] ?? 0)
19
+ return diff == 0
20
+ }
21
+
22
+ /** Admit loopback peers, or a bearer token read afresh for each remote request.
23
+ * Missing transport information fails closed, including when HTTP headers
24
+ * claim the request is local. This wraps every route before dispatch. */
25
+ export let secured =
26
+ (handler: Handler, token: () => unknown): Incoming => (request, peer) => {
27
+ if (peer?.remoteAddr && loopback(peer.remoteAddr.hostname)) {
28
+ return handler(request)
29
+ }
30
+ let expected = token()
31
+ let supplied = /^Bearer ([^\s]+)$/i.exec(
32
+ request.headers.get('authorization') ?? '',
33
+ )?.[1]
34
+ if (
35
+ typeof expected == 'string' && expected.length && supplied &&
36
+ matches(expected, supplied)
37
+ ) return handler(request)
38
+ return new Response('a bearer credential is required for remote access', {
39
+ status: 401,
40
+ headers: { 'www-authenticate': 'Bearer' },
41
+ })
42
+ }
package/actor.js ADDED
@@ -0,0 +1,75 @@
1
+ // Who is writing. A client can put anything in the JSON it posts — including
2
+ // whose name is on it — so the request handler is the only place that can
3
+ // know. Every array of bundles that arrives here has its `$actor` component
4
+ // discarded and replaced by the identity the application's `authenticate`
5
+ // returned for this request. A client that sends `$actor: { by: 'someone
6
+ // else' }` is not refused; it is simply overwritten, because there is nothing
7
+ // to argue about: the graph stamps what reached it, and what reaches it is
8
+ // what this handler decided.
9
+ //
10
+ // An unauthenticated request writes with no actor at all — the bundles are
11
+ // stored unattributed rather than attributed to a guess. An application that
12
+ // would rather refuse throws `Unauthorized` from its `authenticate`.
13
+ // The bundles signed by this handler: every bundle's `$actor` replaced by
14
+ // this writer, or removed when there is none. What a client sent is never
15
+ // kept. It is defined in @yaks/graph beside `land` — signing a write is a
16
+ // fact about a graph, not about HTTP — and re-exported here, where `/apply`
17
+ // reaches for it.
18
+ export { signed } from "@yaks/graph/provenance";
19
+ /** The header a request names the run behind it in: the fleet's own name
20
+ * for the instrument behind a write, never a credential. */ export let VIA = 'x-via';
21
+ /**
22
+ * Callers named by another graph's door: a request naming a run in `x-via` is
23
+ * whoever the door at `url` says it is (its `GET /who`, read for its `by` and
24
+ * `via` alone), so a graph that keeps no sessions of its own, such as the
25
+ * box's tracker, signs a session's writes as the graph that keeps them does.
26
+ * A request naming none is nobody here.
27
+ *
28
+ * ```ts
29
+ * import { api, asking } from '@yaks/api'
30
+ * import { graph } from '@yaks/graph'
31
+ * import { ram } from '@yaks/ram'
32
+ * import { loadVocab } from '@yaks/vocab'
33
+ * import { assertEquals } from '@std/assert'
34
+ *
35
+ * const vocab = loadVocab({ $defs: {} })
36
+ * const box = api({
37
+ * graph: graph({ vocab, storage: ram(vocab) }),
38
+ * authenticate: (r) => ({ by: 'agent', via: r.headers.get('x-via')! }),
39
+ * })
40
+ * const who = asking('http://box.test', async (url, init) =>
41
+ * box(new Request(url, init)))
42
+ * const request = new Request('http://tracker.test/apply', {
43
+ * headers: { 'x-via': 'run' },
44
+ * })
45
+ * assertEquals(await who(request), { by: 'agent', via: 'run' })
46
+ * assertEquals(await who(new Request('http://tracker.test/apply')), null)
47
+ * ```
48
+ */ export let asking = (url, go = fetch)=>{
49
+ let at = `${url.replace(/\/$/, '')}/who`;
50
+ return async (request)=>{
51
+ let via = request.headers.get(VIA);
52
+ if (!via) return null;
53
+ let response = await go(at, {
54
+ headers: {
55
+ [VIA]: via
56
+ }
57
+ });
58
+ if (!response.ok) {
59
+ await response.body?.cancel();
60
+ throw new Error(`${at} answered ${response.status}`);
61
+ }
62
+ // The door may say more of its caller than who they are (route.ts
63
+ // `Standing`); only who they are signs a write here.
64
+ let said = await response.json();
65
+ return said?.by || said?.via ? {
66
+ ...said.by ? {
67
+ by: said.by
68
+ } : {},
69
+ ...said.via ? {
70
+ via: said.via
71
+ } : {}
72
+ } : null;
73
+ };
74
+ };
75
+ //# sourceMappingURL=actor.js.map
package/actor.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"actor.js","sources":["./actor.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,0EAA0E;AAC1E,4EAA4E;AAC5E,0EAA0E;AAC1E,yEAAyE;AACzE,8EAA8E;AAC9E,2EAA2E;AAC3E,6BAA6B;AAC7B,EAAE;AACF,2EAA2E;AAC3E,6EAA6E;AAC7E,qEAAqE;AAmCrE,0EAA0E;AAC1E,0EAA0E;AAC1E,0EAA0E;AAC1E,4EAA4E;AAC5E,kBAAkB;AAClB,SAAS,MAAM,sCAA2C;AAE1D;0DAC0D,GAC1D,OAAO,IAAI,MAAM,QAAO;AAExB;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2BC,GACD,OAAO,IAAI,SAAS,CAAC,KAAa,KAAmB,KAAK;EACxD,IAAI,KAAK,GAAG,IAAI,OAAO,CAAC,OAAO,IAAI,IAAI,CAAC;EACxC,OAAO,OAAO;IACZ,IAAI,MAAM,QAAQ,OAAO,CAAC,GAAG,CAAC;IAC9B,IAAI,CAAC,KAAK,OAAO;IACjB,IAAI,WAAW,MAAM,GAAG,IAAI;MAAE,SAAS;QAAE,CAAC,IAAI,EAAE;MAAI;IAAE;IACtD,IAAI,CAAC,SAAS,EAAE,EAAE;MAChB,MAAM,SAAS,IAAI,EAAE;MACrB,MAAM,IAAI,MAAM,GAAG,GAAG,UAAU,EAAE,SAAS,MAAM,EAAE;IACrD;IACA,kEAAkE;IAClE,qDAAqD;IACrD,IAAI,OAAO,MAAM,SAAS,IAAI;IAC9B,OAAO,MAAM,MAAM,MAAM,MACrB;MACA,GAAG,KAAK,EAAE,GAAG;QAAE,IAAI,KAAK,EAAE;MAAC,IAAI,CAAC,CAAC;MACjC,GAAG,KAAK,GAAG,GAAG;QAAE,KAAK,KAAK,GAAG;MAAC,IAAI,CAAC,CAAC;IACtC,IACE;EACN;AACF,EAAC"}