@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.
- package/LICENSE +202 -0
- package/README.md +956 -2
- package/_dist/actor.d.ts +59 -0
- package/_dist/actor.d.ts.map +1 -0
- package/_dist/address.d.ts +33 -0
- package/_dist/address.d.ts.map +1 -0
- package/_dist/asset.d.ts +15 -0
- package/_dist/asset.d.ts.map +1 -0
- package/_dist/batches.d.ts +4 -0
- package/_dist/batches.d.ts.map +1 -0
- package/_dist/body.d.ts +7 -0
- package/_dist/body.d.ts.map +1 -0
- package/_dist/deno.d.ts +38 -0
- package/_dist/deno.d.ts.map +1 -0
- package/_dist/doors.d.ts +134 -0
- package/_dist/doors.d.ts.map +1 -0
- package/_dist/graph.d.ts +17 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/headers.d.ts +5 -0
- package/_dist/headers.d.ts.map +1 -0
- package/_dist/log.d.ts +253 -0
- package/_dist/log.d.ts.map +1 -0
- package/_dist/mod.d.ts +71 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/publish.d.ts +7 -0
- package/_dist/publish.d.ts.map +1 -0
- package/_dist/reached.d.ts +145 -0
- package/_dist/reached.d.ts.map +1 -0
- package/_dist/receipts.d.ts +153 -0
- package/_dist/receipts.d.ts.map +1 -0
- package/_dist/refuse.d.ts +51 -0
- package/_dist/refuse.d.ts.map +1 -0
- package/_dist/relay.d.ts +3 -0
- package/_dist/relay.d.ts.map +1 -0
- package/_dist/request.d.ts +79 -0
- package/_dist/request.d.ts.map +1 -0
- package/_dist/requests.d.ts +7 -0
- package/_dist/requests.d.ts.map +1 -0
- package/_dist/route.d.ts +71 -0
- package/_dist/route.d.ts.map +1 -0
- package/_dist/routed.d.ts +33 -0
- package/_dist/routed.d.ts.map +1 -0
- package/_dist/routes.d.ts +46 -0
- package/_dist/routes.d.ts.map +1 -0
- package/_dist/save.d.ts +6 -0
- package/_dist/save.d.ts.map +1 -0
- package/_dist/socket.d.ts +73 -0
- package/_dist/socket.d.ts.map +1 -0
- package/_dist/subs.d.ts +134 -0
- package/_dist/subs.d.ts.map +1 -0
- package/_dist/timing.d.ts +8 -0
- package/_dist/timing.d.ts.map +1 -0
- package/_dist/tools.d.ts +80 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/access.js +32 -0
- package/access.js.map +1 -0
- package/access.ts +42 -0
- package/actor.js +75 -0
- package/actor.js.map +1 -0
- package/actor.ts +106 -0
- package/address.js +42 -0
- package/address.js.map +1 -0
- package/address.ts +49 -0
- package/admission.js +71 -0
- package/admission.js.map +1 -0
- package/admission.ts +64 -0
- package/asset.js +37 -0
- package/asset.js.map +1 -0
- package/asset.ts +47 -0
- package/batches.js +92 -0
- package/batches.js.map +1 -0
- package/batches.ts +96 -0
- package/body.js +51 -0
- package/body.js.map +1 -0
- package/body.ts +82 -0
- package/deno.js +88 -0
- package/deno.js.map +1 -0
- package/deno.json +1 -0
- package/deno.ts +150 -0
- package/doors.js +435 -0
- package/doors.js.map +1 -0
- package/doors.ts +570 -0
- package/graph.js +12 -0
- package/graph.js.map +1 -0
- package/graph.ts +23 -0
- package/headers.js +5 -0
- package/headers.js.map +1 -0
- package/headers.ts +9 -0
- package/interest.js +224 -0
- package/interest.js.map +1 -0
- package/interest.ts +264 -0
- package/log.js +1011 -0
- package/log.js.map +1 -0
- package/log.ts +1035 -0
- package/mod.js +69 -0
- package/mod.js.map +1 -0
- package/mod.ts +142 -0
- package/package.json +75 -4
- package/peer_query.js +164 -0
- package/peer_query.js.map +1 -0
- package/peer_query.ts +158 -0
- package/publish.js +7 -0
- package/publish.js.map +1 -0
- package/publish.ts +16 -0
- package/reached.js +456 -0
- package/reached.js.map +1 -0
- package/reached.ts +549 -0
- package/receipts.js +543 -0
- package/receipts.js.map +1 -0
- package/receipts.ts +749 -0
- package/refuse.js +120 -0
- package/refuse.js.map +1 -0
- package/refuse.ts +179 -0
- package/relay.js +249 -0
- package/relay.js.map +1 -0
- package/relay.ts +284 -0
- package/request.js +218 -0
- package/request.js.map +1 -0
- package/request.ts +235 -0
- package/requests.js +250 -0
- package/requests.js.map +1 -0
- package/requests.ts +257 -0
- package/route.js +139 -0
- package/route.js.map +1 -0
- package/route.ts +228 -0
- package/routed.js +25 -0
- package/routed.js.map +1 -0
- package/routed.ts +53 -0
- package/routes.js +154 -0
- package/routes.js.map +1 -0
- package/routes.ts +201 -0
- package/save.js +341 -0
- package/save.js.map +1 -0
- package/save.ts +393 -0
- package/socket.js +398 -0
- package/socket.js.map +1 -0
- package/socket.ts +496 -0
- package/subs.js +2295 -0
- package/subs.js.map +1 -0
- package/subs.ts +2979 -0
- package/timing.js +17 -0
- package/timing.js.map +1 -0
- package/timing.ts +26 -0
- package/tools.js +251 -0
- package/tools.js.map +1 -0
- package/tools.ts +315 -0
- package/vocab.js +18 -0
- package/vocab.js.map +1 -0
- package/vocab.json +93 -0
- package/vocab.ts +18 -0
package/routed.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// One route, and whether a request takes it: no graph, no socket and no
|
|
2
|
+
// subscription, so a router that only matches paths (@yaks/workerd's) loads
|
|
3
|
+
// nothing else of this package. Exported as `@yaks/api/routed`.
|
|
4
|
+
|
|
5
|
+
/** A web-standard request handler: a `Request` in, a `Response` out. */
|
|
6
|
+
export type Handler = (request: Request) => Response | Promise<Response>
|
|
7
|
+
|
|
8
|
+
/** One path served alongside this package's three endpoints — a plugin's
|
|
9
|
+
* route. `path` is matched exactly, or ends in `*` to match a prefix, which
|
|
10
|
+
* is what content-addressed bytes (`/blob/<sha>`) need; `method` is the HTTP
|
|
11
|
+
* method, or `*` for any. Whoever mounts the routes decides which one wins a
|
|
12
|
+
* conflict: this package serves `/apply`, `/query` and `/ws` and claims
|
|
13
|
+
* nothing else. */
|
|
14
|
+
export type Route = {
|
|
15
|
+
method: string
|
|
16
|
+
path: string
|
|
17
|
+
/** record the route pattern instead of a path carrying credentials */
|
|
18
|
+
sensitive?: boolean
|
|
19
|
+
handle: Handler
|
|
20
|
+
/** What a person reaching this path finds, said beside its address when a
|
|
21
|
+
* server starts listening: the few doors somebody starting one opens. */
|
|
22
|
+
says?: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Whether a route matches this request's method and path. A GET route
|
|
26
|
+
* matches a HEAD too, which is a GET answered without its body (RFC 9110
|
|
27
|
+
* §9.3.2): how a page asks a bundle's etag without fetching it. */
|
|
28
|
+
export let routed = (route: Route, method: string, path: string): boolean =>
|
|
29
|
+
(route.method == '*' || route.method == method ||
|
|
30
|
+
route.method == 'GET' && method == 'HEAD') &&
|
|
31
|
+
(route.path.endsWith('*')
|
|
32
|
+
? path.startsWith(route.path.slice(0, -1))
|
|
33
|
+
: route.path == path)
|
|
34
|
+
|
|
35
|
+
/** The path a request names to doors that answer under `under` as well as at
|
|
36
|
+
* the root: its own path with `under` taken off its front where it starts
|
|
37
|
+
* there, so `/api/query` is `/query` to doors under `/api`, as a page's
|
|
38
|
+
* `./api/` finds them.
|
|
39
|
+
*
|
|
40
|
+
* ```ts
|
|
41
|
+
* import { pathOf } from '@yaks/api/routed'
|
|
42
|
+
* import { equal } from '@yaks/testing'
|
|
43
|
+
* let at = (path: string) => new Request(`http://box${path}`)
|
|
44
|
+
* equal(pathOf(at('/api/query'), '/api'), '/query')
|
|
45
|
+
* equal(pathOf(at('/query'), '/api'), '/query')
|
|
46
|
+
* equal(pathOf(at('/apiary'), '/api'), '/apiary')
|
|
47
|
+
* equal(pathOf(at('/api/query')), '/api/query')
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export let pathOf = (request: Request, under?: string): string => {
|
|
51
|
+
let path = new URL(request.url).pathname
|
|
52
|
+
return under && path.startsWith(`${under}/`) ? path.slice(under.length) : path
|
|
53
|
+
}
|
package/routes.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// The routes facet, exported as `@yaks/api/routes`: this package is what
|
|
2
|
+
// makes a host answer HTTP at all.
|
|
3
|
+
//
|
|
4
|
+
// A host does not serve requests because it was composed. It serves them
|
|
5
|
+
// because its config names this package among its plugins, and this is the one
|
|
6
|
+
// module that turns routes into a handler: every listed plugin's own routes
|
|
7
|
+
// (`host.routes`), with `/apply`, `/query` and `/ws` behind them, and every
|
|
8
|
+
// plugin's filter (`host.filters`) in front of all of it. A config
|
|
9
|
+
// that does not name this package composes a host with no handler, no `serve`
|
|
10
|
+
// verb, and no reason to ask the other plugins for routes nobody would answer.
|
|
11
|
+
//
|
|
12
|
+
// It is handed the host once the graph is open and the routes are gathered,
|
|
13
|
+
// unlike `authenticate` beside it, which is asked for before anything is open.
|
|
14
|
+
import { api, DOORS, pathOf, routed } from "./route.js";
|
|
15
|
+
import { served } from "./request.js";
|
|
16
|
+
import { subscriptions } from "./subs.js";
|
|
17
|
+
import { fault } from "./refuse.js";
|
|
18
|
+
import { refusing } from "./socket.js";
|
|
19
|
+
import { denoUpgrade } from "./deno.js";
|
|
20
|
+
import { Refused } from "@yaks/graph/errors";
|
|
21
|
+
import { asking } from "@yaks/graph/asker";
|
|
22
|
+
// The page a request came from: the one that sent it, or the one a
|
|
23
|
+
// navigation asks for. Its path only; a query string can carry a token.
|
|
24
|
+
let pageOf = (request)=>{
|
|
25
|
+
let from = request.headers.get('referer');
|
|
26
|
+
if (from) return URL.parse(from)?.pathname;
|
|
27
|
+
if (request.headers.get('sec-fetch-mode') == 'navigate') {
|
|
28
|
+
return new URL(request.url).pathname;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
let socketing = (request, under)=>pathOf(request, under) == '/ws' && (request.headers.get('upgrade') ?? '').toLowerCase() == 'websocket';
|
|
32
|
+
// How closely a route names a path: an exact path over any prefix, and a
|
|
33
|
+
// longer prefix over a shorter one.
|
|
34
|
+
let exact = (r)=>!r.path.endsWith('*');
|
|
35
|
+
let reach = (r)=>exact(r) ? Infinity : r.path.length;
|
|
36
|
+
/**
|
|
37
|
+
* The host's one request handler: each plugin's route, and this package's
|
|
38
|
+
* three endpoints, behind every plugin's filter. A filter that throws answers
|
|
39
|
+
* the request with that refusal, and nothing past it runs: a `/ws` upgrade it
|
|
40
|
+
* refuses (a `Refused`) opens, and answers each subscription with it. An
|
|
41
|
+
* answer at 500 or over, from a route, a filter or a door, goes to
|
|
42
|
+
* `host.report` with a `request` bundle named by its route (./request.ts
|
|
43
|
+
* `served`).
|
|
44
|
+
*
|
|
45
|
+
* The route that names the path most closely wins, whichever plugin listed it:
|
|
46
|
+
* an exact path over a prefix, a longer prefix over a shorter, and plugin order
|
|
47
|
+
* between equals. `/apply`, `/query` and `/ws` are exact paths, so a plugin's
|
|
48
|
+
* catch-all (`/*`) answers only what nothing else claims. A request no route
|
|
49
|
+
* claimed goes to the doors, which refuse anything they do not serve.
|
|
50
|
+
*/ export let handler = (host, options = {})=>{
|
|
51
|
+
let under = options.under;
|
|
52
|
+
let upgrade = options.upgrade ?? denoUpgrade;
|
|
53
|
+
let routes = host.routes;
|
|
54
|
+
let graph = host.graph;
|
|
55
|
+
// One registry, fed twice: its own graph's `effect` phase for what this host
|
|
56
|
+
// commits, and the host's feed for what every other process or thread
|
|
57
|
+
// commits to the same store — a `yak` command beside `yak serve`, the effect
|
|
58
|
+
// pool's thread — which that phase never runs for.
|
|
59
|
+
// A commit feed already carries every commit, this process's and others',
|
|
60
|
+
// as precise patches. The revision is for a store without one: it moves on
|
|
61
|
+
// any write, so polling it beside a feed would reset every query each time.
|
|
62
|
+
let revision = host.feed ? undefined : graph.storage.revision ?? host.storage?.revision;
|
|
63
|
+
let subs = subscriptions(graph, {
|
|
64
|
+
revision
|
|
65
|
+
});
|
|
66
|
+
let stopFeed = host.feed?.(subs.hear);
|
|
67
|
+
let cancel = ()=>{};
|
|
68
|
+
let stopped = host.stopping?.aborted ?? false;
|
|
69
|
+
let stop = ()=>{
|
|
70
|
+
stopped = true;
|
|
71
|
+
cancel();
|
|
72
|
+
stopFeed?.();
|
|
73
|
+
subs.stop();
|
|
74
|
+
};
|
|
75
|
+
if (stopped) stop();
|
|
76
|
+
else host.stopping?.addEventListener('abort', stop, {
|
|
77
|
+
once: true
|
|
78
|
+
});
|
|
79
|
+
if (revision && !host.stopping?.aborted) {
|
|
80
|
+
let timer = options.timer ?? ((fn, ms)=>{
|
|
81
|
+
let id = setTimeout(fn, ms);
|
|
82
|
+
return ()=>clearTimeout(id);
|
|
83
|
+
});
|
|
84
|
+
let look = async ()=>{
|
|
85
|
+
try {
|
|
86
|
+
await subs.check();
|
|
87
|
+
} catch (error) {
|
|
88
|
+
fault(error, 'store revision');
|
|
89
|
+
}
|
|
90
|
+
if (!stopped) cancel = timer(look, options.every ?? 1000);
|
|
91
|
+
};
|
|
92
|
+
cancel = timer(look, options.every ?? 1000);
|
|
93
|
+
}
|
|
94
|
+
let report = host.report ? (request, error)=>{
|
|
95
|
+
void host.report(error ?? 'HTTP request failed', {
|
|
96
|
+
request,
|
|
97
|
+
during: {
|
|
98
|
+
request: request.entity.eid
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
} : undefined;
|
|
102
|
+
let door = api({
|
|
103
|
+
graph,
|
|
104
|
+
authenticate: host.who,
|
|
105
|
+
subs,
|
|
106
|
+
report,
|
|
107
|
+
upgrade,
|
|
108
|
+
under
|
|
109
|
+
});
|
|
110
|
+
let claimed = (request)=>{
|
|
111
|
+
let path = new URL(request.url).pathname;
|
|
112
|
+
let route = routes.filter((r)=>routed(r, request.method, path)).reduce((best, r)=>best && reach(best) >= reach(r) ? best : r, undefined);
|
|
113
|
+
return route && (exact(route) || !DOORS.includes(pathOf(request, under))) ? route : undefined;
|
|
114
|
+
};
|
|
115
|
+
let filters = host.filters ?? [];
|
|
116
|
+
let routeOf = (request)=>{
|
|
117
|
+
let path = pathOf(request, under);
|
|
118
|
+
return claimed(request)?.path ?? (DOORS.includes(path) ? path : undefined);
|
|
119
|
+
};
|
|
120
|
+
// Whatever a request starts, a read on another thread too, answers the
|
|
121
|
+
// route it came through and the page that sent it (@yaks/graph/asker).
|
|
122
|
+
let asker = (request)=>{
|
|
123
|
+
let door = routeOf(request);
|
|
124
|
+
let page = pageOf(request);
|
|
125
|
+
return door ? {
|
|
126
|
+
door,
|
|
127
|
+
...page && {
|
|
128
|
+
page
|
|
129
|
+
}
|
|
130
|
+
} : undefined;
|
|
131
|
+
};
|
|
132
|
+
let answer = async (request)=>{
|
|
133
|
+
try {
|
|
134
|
+
for (let f of filters)await f(request);
|
|
135
|
+
} catch (error) {
|
|
136
|
+
// A socket hears its refusal on the socket, where its client reads it.
|
|
137
|
+
if (!(error instanceof Refused) || !socketing(request, under)) throw error;
|
|
138
|
+
let { socket, response } = upgrade(request);
|
|
139
|
+
refusing(socket, error);
|
|
140
|
+
return response;
|
|
141
|
+
}
|
|
142
|
+
let route = claimed(request);
|
|
143
|
+
if (!route) return door(request);
|
|
144
|
+
let answered = await route.handle(request);
|
|
145
|
+
return request.method == 'HEAD' ? new Response(null, answered) : answered;
|
|
146
|
+
};
|
|
147
|
+
return served((request)=>asking(asker(request), ()=>answer(request)), {
|
|
148
|
+
graph: host.graph,
|
|
149
|
+
report,
|
|
150
|
+
route: routeOf,
|
|
151
|
+
redact: (request)=>claimed(request)?.sensitive === true
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
//# sourceMappingURL=routes.js.map
|
package/routes.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routes.js","sources":["./routes.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,mCAAmC;AACnC,EAAE;AACF,yEAAyE;AACzE,+EAA+E;AAC/E,4EAA4E;AAC5E,4EAA4E;AAC5E,mEAAmE;AACnE,8EAA8E;AAC9E,+EAA+E;AAC/E,EAAE;AACF,4EAA4E;AAC5E,+EAA+E;AAI/E,SACE,GAAG,EACH,KAAK,EAGL,MAAM,EAEN,MAAM,qBACW;AACnB,SAAsB,MAAM,uBAA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AAI,UAAU,CAAC;IACb,IAAI,OAAO,OAAO,SAAS;IAC3B,OAAO,QAAQ,UAAU,QAAQ,CAAC,MAAM,QAAQ,CAAC,QAAQ,OAAO,SAAS;EAC3E;EACA,uEAAuE;EACvE,uEAAuE;EACvE,IAAI,QAAQ,CAAC;IACX,IAAI,OAAO,QAAQ;IACnB,IAAI,OAAO,OAAO;IAClB,OAAO,OAAO;MAAE;MAAM,GAAI,QAAQ;QAAE;MAAK,CAAC;IAAE,IAAI;EAClD;EACA,IAAI,SAAS,OAAO;IAClB,IAAI;MACF,KAAK,IAAI,KAAK,QAAS,MAAM,EAAE;IACjC,EAAE,OAAO,OAAO;MACd,uEAAuE;MACvE,IAAI,CAAC,CAAC,iBAAiB,OAAO,KAAK,CAAC,UAAU,SAAS,QAAQ,MAAM;MACrE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,QAAQ;MACnC,SAAS,QAAQ;MACjB,OAAO;IACT;IACA,IAAI,QAAQ,QAAQ;IACpB,IAAI,CAAC,OAAO,OAAO,KAAK;IACxB,IAAI,WAAW,MAAM,MAAM,MAAM,CAAC;IAClC,OAAO,QAAQ,MAAM,IAAI,SAAS,IAAI,SAAS,MAAM,YAAY;EACnE;EACA,OAAO,OAAO,CAAC,UAAY,OAAO,MAAM,UAAU,IAAM,OAAO,WAAW;IACxE,OAAO,KAAK,KAAK;IACjB;IACA,OAAO;IACP,QAAQ,CAAC,UAAY,QAAQ,UAAU,cAAc;EACvD;AACF,EAAC"}
|
package/routes.ts
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// The routes facet, exported as `@yaks/api/routes`: this package is what
|
|
2
|
+
// makes a host answer HTTP at all.
|
|
3
|
+
//
|
|
4
|
+
// A host does not serve requests because it was composed. It serves them
|
|
5
|
+
// because its config names this package among its plugins, and this is the one
|
|
6
|
+
// module that turns routes into a handler: every listed plugin's own routes
|
|
7
|
+
// (`host.routes`), with `/apply`, `/query` and `/ws` behind them, and every
|
|
8
|
+
// plugin's filter (`host.filters`) in front of all of it. A config
|
|
9
|
+
// that does not name this package composes a host with no handler, no `serve`
|
|
10
|
+
// verb, and no reason to ask the other plugins for routes nobody would answer.
|
|
11
|
+
//
|
|
12
|
+
// It is handed the host once the graph is open and the routes are gathered,
|
|
13
|
+
// unlike `authenticate` beside it, which is asked for before anything is open.
|
|
14
|
+
|
|
15
|
+
import type { Bundle, Graph, News, Storage } from 'jsr:@yaks/graph@0.2.36'
|
|
16
|
+
import type { Authenticate } from './actor.ts'
|
|
17
|
+
import {
|
|
18
|
+
api,
|
|
19
|
+
DOORS,
|
|
20
|
+
type Filter,
|
|
21
|
+
type Handler,
|
|
22
|
+
pathOf,
|
|
23
|
+
type Route,
|
|
24
|
+
routed,
|
|
25
|
+
} from './route.js'
|
|
26
|
+
import { type Report, served } from './request.js'
|
|
27
|
+
import { subscriptions } from './subs.js'
|
|
28
|
+
import type { Timer } from './relay.ts'
|
|
29
|
+
import { fault } from './refuse.js'
|
|
30
|
+
import { refusing, type Upgrade } from './socket.js'
|
|
31
|
+
import { denoUpgrade } from './deno.js'
|
|
32
|
+
import { Refused } from '@yaks/graph/errors'
|
|
33
|
+
import { type Asker, asking } from '@yaks/graph/asker'
|
|
34
|
+
|
|
35
|
+
// The page a request came from: the one that sent it, or the one a
|
|
36
|
+
// navigation asks for. Its path only; a query string can carry a token.
|
|
37
|
+
let pageOf = (request: Request): string | undefined => {
|
|
38
|
+
let from = request.headers.get('referer')
|
|
39
|
+
if (from) return URL.parse(from)?.pathname
|
|
40
|
+
if (request.headers.get('sec-fetch-mode') == 'navigate') {
|
|
41
|
+
return new URL(request.url).pathname
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let socketing = (request: Request, under?: string) =>
|
|
46
|
+
pathOf(request, under) == '/ws' &&
|
|
47
|
+
(request.headers.get('upgrade') ?? '').toLowerCase() == 'websocket'
|
|
48
|
+
|
|
49
|
+
/** What this facet reads off the host it is composing into: the graph its
|
|
50
|
+
* endpoints answer over, who that host says is calling, every route the
|
|
51
|
+
* listed plugins contributed, every filter they put in front, and the commits
|
|
52
|
+
* other hosts make to the same store, which `/ws` subscribers are told of as
|
|
53
|
+
* well as this graph's own. */
|
|
54
|
+
export type Hosting = {
|
|
55
|
+
graph: Graph
|
|
56
|
+
storage?: Pick<Storage, 'revision'>
|
|
57
|
+
stopping?: AbortSignal
|
|
58
|
+
who: Authenticate
|
|
59
|
+
routes: Route[]
|
|
60
|
+
filters?: Filter[]
|
|
61
|
+
feed?: (each: (news: News) => void | Promise<void>) => () => void
|
|
62
|
+
/** Host telemetry, supplied with the failing request bundle. */
|
|
63
|
+
report?: (
|
|
64
|
+
error: unknown,
|
|
65
|
+
context: { request: Bundle; during: { request: string } },
|
|
66
|
+
) => void | Promise<void>
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// How closely a route names a path: an exact path over any prefix, and a
|
|
70
|
+
// longer prefix over a shorter one.
|
|
71
|
+
let exact = (r: Route) => !r.path.endsWith('*')
|
|
72
|
+
let reach = (r: Route) => exact(r) ? Infinity : r.path.length
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The host's one request handler: each plugin's route, and this package's
|
|
76
|
+
* three endpoints, behind every plugin's filter. A filter that throws answers
|
|
77
|
+
* the request with that refusal, and nothing past it runs: a `/ws` upgrade it
|
|
78
|
+
* refuses (a `Refused`) opens, and answers each subscription with it. An
|
|
79
|
+
* answer at 500 or over, from a route, a filter or a door, goes to
|
|
80
|
+
* `host.report` with a `request` bundle named by its route (./request.ts
|
|
81
|
+
* `served`).
|
|
82
|
+
*
|
|
83
|
+
* The route that names the path most closely wins, whichever plugin listed it:
|
|
84
|
+
* an exact path over a prefix, a longer prefix over a shorter, and plugin order
|
|
85
|
+
* between equals. `/apply`, `/query` and `/ws` are exact paths, so a plugin's
|
|
86
|
+
* catch-all (`/*`) answers only what nothing else claims. A request no route
|
|
87
|
+
* claimed goes to the doors, which refuse anything they do not serve.
|
|
88
|
+
*/
|
|
89
|
+
export let handler = (host: Hosting, options: {
|
|
90
|
+
/** Time between store revision checks, in ms (default 1000). */
|
|
91
|
+
every?: number
|
|
92
|
+
/** Schedule a revision check and return its cancellation function. */
|
|
93
|
+
timer?: Timer
|
|
94
|
+
/** How a `/ws` request becomes a socket (default: Deno's). */
|
|
95
|
+
upgrade?: Upgrade
|
|
96
|
+
/** A path the doors answer under as well as at the root (`/api`, where a
|
|
97
|
+
* page's `./api/` client finds its store). */
|
|
98
|
+
under?: string
|
|
99
|
+
} = {}): Handler => {
|
|
100
|
+
let under = options.under
|
|
101
|
+
let upgrade = options.upgrade ?? denoUpgrade
|
|
102
|
+
let routes = host.routes
|
|
103
|
+
let graph = host.graph
|
|
104
|
+
// One registry, fed twice: its own graph's `effect` phase for what this host
|
|
105
|
+
// commits, and the host's feed for what every other process or thread
|
|
106
|
+
// commits to the same store — a `yak` command beside `yak serve`, the effect
|
|
107
|
+
// pool's thread — which that phase never runs for.
|
|
108
|
+
// A commit feed already carries every commit, this process's and others',
|
|
109
|
+
// as precise patches. The revision is for a store without one: it moves on
|
|
110
|
+
// any write, so polling it beside a feed would reset every query each time.
|
|
111
|
+
let revision = host.feed
|
|
112
|
+
? undefined
|
|
113
|
+
: graph.storage.revision ?? host.storage?.revision
|
|
114
|
+
let subs = subscriptions(graph, { revision })
|
|
115
|
+
let stopFeed = host.feed?.(subs.hear)
|
|
116
|
+
let cancel = () => {}
|
|
117
|
+
let stopped = host.stopping?.aborted ?? false
|
|
118
|
+
let stop = () => {
|
|
119
|
+
stopped = true
|
|
120
|
+
cancel()
|
|
121
|
+
stopFeed?.()
|
|
122
|
+
subs.stop()
|
|
123
|
+
}
|
|
124
|
+
if (stopped) stop()
|
|
125
|
+
else host.stopping?.addEventListener('abort', stop, { once: true })
|
|
126
|
+
if (revision && !host.stopping?.aborted) {
|
|
127
|
+
let timer = options.timer ?? ((fn, ms) => {
|
|
128
|
+
let id = setTimeout(fn, ms)
|
|
129
|
+
return () => clearTimeout(id)
|
|
130
|
+
})
|
|
131
|
+
let look = async () => {
|
|
132
|
+
try {
|
|
133
|
+
await subs.check()
|
|
134
|
+
} catch (error) {
|
|
135
|
+
fault(error, 'store revision')
|
|
136
|
+
}
|
|
137
|
+
if (!stopped) cancel = timer(look, options.every ?? 1000)
|
|
138
|
+
}
|
|
139
|
+
cancel = timer(look, options.every ?? 1000)
|
|
140
|
+
}
|
|
141
|
+
let report: Report | undefined = host.report
|
|
142
|
+
? (request, error) => {
|
|
143
|
+
void host.report!(error ?? 'HTTP request failed', {
|
|
144
|
+
request,
|
|
145
|
+
during: { request: request.entity.eid },
|
|
146
|
+
})
|
|
147
|
+
}
|
|
148
|
+
: undefined
|
|
149
|
+
let door = api({
|
|
150
|
+
graph,
|
|
151
|
+
authenticate: host.who,
|
|
152
|
+
subs,
|
|
153
|
+
report,
|
|
154
|
+
upgrade,
|
|
155
|
+
under,
|
|
156
|
+
})
|
|
157
|
+
let claimed = (request: Request): Route | undefined => {
|
|
158
|
+
let path = new URL(request.url).pathname
|
|
159
|
+
let route = routes.filter((r) => routed(r, request.method, path))
|
|
160
|
+
.reduce<Route | undefined>(
|
|
161
|
+
(best, r) => best && reach(best) >= reach(r) ? best : r,
|
|
162
|
+
undefined,
|
|
163
|
+
)
|
|
164
|
+
return route && (exact(route) || !DOORS.includes(pathOf(request, under)))
|
|
165
|
+
? route
|
|
166
|
+
: undefined
|
|
167
|
+
}
|
|
168
|
+
let filters = host.filters ?? []
|
|
169
|
+
let routeOf = (request: Request) => {
|
|
170
|
+
let path = pathOf(request, under)
|
|
171
|
+
return claimed(request)?.path ?? (DOORS.includes(path) ? path : undefined)
|
|
172
|
+
}
|
|
173
|
+
// Whatever a request starts, a read on another thread too, answers the
|
|
174
|
+
// route it came through and the page that sent it (@yaks/graph/asker).
|
|
175
|
+
let asker = (request: Request): Asker | undefined => {
|
|
176
|
+
let door = routeOf(request)
|
|
177
|
+
let page = pageOf(request)
|
|
178
|
+
return door ? { door, ...(page && { page }) } : undefined
|
|
179
|
+
}
|
|
180
|
+
let answer = async (request: Request): Promise<Response> => {
|
|
181
|
+
try {
|
|
182
|
+
for (let f of filters) await f(request)
|
|
183
|
+
} catch (error) {
|
|
184
|
+
// A socket hears its refusal on the socket, where its client reads it.
|
|
185
|
+
if (!(error instanceof Refused) || !socketing(request, under)) throw error
|
|
186
|
+
let { socket, response } = upgrade(request)
|
|
187
|
+
refusing(socket, error)
|
|
188
|
+
return response
|
|
189
|
+
}
|
|
190
|
+
let route = claimed(request)
|
|
191
|
+
if (!route) return door(request)
|
|
192
|
+
let answered = await route.handle(request)
|
|
193
|
+
return request.method == 'HEAD' ? new Response(null, answered) : answered
|
|
194
|
+
}
|
|
195
|
+
return served((request) => asking(asker(request), () => answer(request)), {
|
|
196
|
+
graph: host.graph,
|
|
197
|
+
report,
|
|
198
|
+
route: routeOf,
|
|
199
|
+
redact: (request) => claimed(request)?.sensitive === true,
|
|
200
|
+
})
|
|
201
|
+
}
|