@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/_dist/mod.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/api — an HTTP and WebSocket interface to a yaks graph, as a plain
|
|
3
|
+
* request handler that runs in any JavaScript runtime.
|
|
4
|
+
*
|
|
5
|
+
* Given a {@link https://jsr.io/@yaks/graph | @yaks/graph} `Graph`, this
|
|
6
|
+
* package serves the three endpoints a client needs and nothing more:
|
|
7
|
+
*
|
|
8
|
+
* - **`POST /apply`** — the request body is a JSON array of bundles, applied
|
|
9
|
+
* in one transaction; the response body is that array as applied.
|
|
10
|
+
* `?check=1` runs every phase and then rolls the transaction back, so a
|
|
11
|
+
* caller spreading one array over several graphs can ask each of them
|
|
12
|
+
* before any of them keeps it. `content-type: application/x-ndjson` is the
|
|
13
|
+
* same endpoint with one bundle per line, for an import too big to parse or
|
|
14
|
+
* commit whole ({@link pour}).
|
|
15
|
+
* - **`GET /query?q=…`** (or `POST /query`) — a query string in, the bundles
|
|
16
|
+
* it selects out.
|
|
17
|
+
* - **`/ws`** — subscriptions: a saved query whose result is pushed again
|
|
18
|
+
* whenever a committed transaction changes it.
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { api } from '@yaks/api'
|
|
22
|
+
* // Deno.serve(api({ graph, authenticate }))
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* ## Attribution is decided here, not by the client
|
|
26
|
+
* A client can put anything in the JSON it posts, including whose name is on
|
|
27
|
+
* it. So every array that arrives has its `$actor` component replaced by the
|
|
28
|
+
* identity {@link Authenticate} returned for that request. `authenticate`
|
|
29
|
+
* runs on every request, and throwing {@link Unauthorized} from it answers
|
|
30
|
+
* that request with a 401.
|
|
31
|
+
*
|
|
32
|
+
* ## Two callbacks the application supplies
|
|
33
|
+
* {@link Authenticate} names the writer. {@link Upgrade} turns a request into
|
|
34
|
+
* a WebSocket — the one step no web standard covers — and defaults to
|
|
35
|
+
* {@link denoUpgrade}; on Cloudflare Workers you pass one built on
|
|
36
|
+
* `WebSocketPair` (see the README). Everything else here is standard
|
|
37
|
+
* `Request`, `Response` and `WebSocket`, so the same handler serves on Deno,
|
|
38
|
+
* Node and a Worker.
|
|
39
|
+
*
|
|
40
|
+
* ## The WebSocket protocol
|
|
41
|
+
* ```text
|
|
42
|
+
* → { subscribe: "<query>" | true, id: "<id>" } open (true = every commit)
|
|
43
|
+
* → { unsubscribe: "<id>" } close
|
|
44
|
+
* → { relay: Bundle[] } forward `sync: peers` values
|
|
45
|
+
* ← { id, bundles: Bundle[], gone?: Eid[] } the set, then every change
|
|
46
|
+
* ← { id, relay: Bundle[] } peer values from a client
|
|
47
|
+
* ← { id, refused: { error, message, … } } the subscription was refused
|
|
48
|
+
* ```
|
|
49
|
+
* No durable write crosses the socket: bundles are applied with
|
|
50
|
+
* `POST /apply`, and the socket is how every connected client learns about
|
|
51
|
+
* them. A relayed value is the one exception: it is forwarded to the other
|
|
52
|
+
* subscribers; components declaring `save` also keep periodic snapshots and
|
|
53
|
+
* their last value on disconnect, as the authenticated writer.
|
|
54
|
+
*
|
|
55
|
+
* @module
|
|
56
|
+
*/ export { api, type Arrived, DOORS, type Filter, type Handler, type Offers, type Options, type Route, routed, type Standing, type Told, whoDoor } from "./route.js";
|
|
57
|
+
export { asking, type Authenticate, signed, VIA } from "./actor.js";
|
|
58
|
+
export type { PeerWriter } from "./save.js";
|
|
59
|
+
export type { Timer } from "./relay.js";
|
|
60
|
+
export { type Applier, applier, type Applying, ask, CHUNK, type ListQuery, pour, poured, type Trusted, type WriteContext, writeOptions } from "./doors.js";
|
|
61
|
+
export { published } from "./publish.js";
|
|
62
|
+
export { readBody, writeBytes, writeSize } from "./body.js";
|
|
63
|
+
export { applyBatches } from "./batches.js";
|
|
64
|
+
export { cutOf, reads } from "./requests.js";
|
|
65
|
+
export { fault, faults, FILED, json, type Refusal, refusal, refuse, type Reporter, type Tags, Unauthorized, waits } from "./refuse.js";
|
|
66
|
+
export { type Ask, type Frame, type Listening, type Opening, type RecoverableListening, type Revision, type Sink, type Subs, subscriptions } from "./subs.js";
|
|
67
|
+
export { attach, clockReply, decode, type Incoming, queue, receive, sink, type Socket, type Upgrade } from "./socket.js";
|
|
68
|
+
export { agent, type Answered, type Report, requested, served, type Watch } from "./request.js";
|
|
69
|
+
export { timed } from "./timing.js";
|
|
70
|
+
export { type Addr, denoListen, denoUpgrade, type Listen, type Listener } from "./deno.js";
|
|
71
|
+
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sources":["../mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAuDC,GAED,SACE,GAAG,EACH,KAAK,OAAO,EACZ,KAAK,EACL,KAAK,MAAM,EACX,KAAK,OAAO,EACZ,KAAK,MAAM,EACX,KAAK,OAAO,EACZ,KAAK,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|
+
import { type Vocab } from "@yaks/vocab";
|
|
3
|
+
/** The bundles a transport may send. A `sync: none` component belongs to
|
|
4
|
+
* this node alone; identities, pipeline metadata and every other component
|
|
5
|
+
* retain their values, including server-owned stamps and deletion markers.
|
|
6
|
+
* This projection never changes the graph's stored or applied bundles. */ export declare let published: (vocab: Vocab, bundles: Bundle[]) => Bundle[];
|
|
7
|
+
//# sourceMappingURL=publish.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"publish.d.ts","sources":["../publish.ts"],"names":[],"mappings":"AAAA,cAAc,MAAM,2BAAgC;AAEpD,SAAiB,KAAK,KAAK,2BAAgC;AAE3D;;;wEAGwE,GACxE,OAAO,YAAI,YAAa,OAAO,OAAO,SAAS,aAAW,SAOvD"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { Bundle, Graph, ReadOpts, Reduced } from "@yaks/graph";
|
|
2
|
+
import { type Clock } from "@yaks/fp";
|
|
3
|
+
import { type Tags } from "./refuse.js";
|
|
4
|
+
import type { Frame, Opening, RecoverableListening, Sink, Subs } from "./subs.js";
|
|
5
|
+
import type { PeerWriter } from "./save.js";
|
|
6
|
+
/** A registry over a reach, as {@link reached} makes one. */ export type Reached = {
|
|
7
|
+
/** Watch a line as `opts` and `writer` say who asks, and send its
|
|
8
|
+
* answer. */ open: (sink: Sink, id: string, line: string, opts?: ReadOpts, writer?: PeerWriter) => void;
|
|
9
|
+
/** Reopen saved watches. With `quiet`, each is held and not asked until
|
|
10
|
+
* {@link Reached.resume} or the next {@link Reached.hear}: a page that
|
|
11
|
+
* kept its answers through its host's sleep is not sent them again. */ restore: (openings: Opening[], quiet?: boolean) => void | Promise<void>;
|
|
12
|
+
/** Ask every watch held quiet. */ resume: () => void | Promise<void>;
|
|
13
|
+
close: (sink: Sink, id: string) => void;
|
|
14
|
+
drop: (sink: Sink) => void;
|
|
15
|
+
/** A member's graph moved: each watch selected by its line and read
|
|
16
|
+
* options is asked again. Omit `only` to refresh every watch. */ hear: (only?: (line: string, read?: ReadOpts) => boolean) => Promise<void>;
|
|
17
|
+
/** Whether any line is watched. */ watching: () => boolean;
|
|
18
|
+
};
|
|
19
|
+
type Read = (line: string, opts?: ReadOpts, writer?: PeerWriter) => Promise<Bundle[] | Reduced>;
|
|
20
|
+
type Reader = {
|
|
21
|
+
read: Read;
|
|
22
|
+
readBatch?: never;
|
|
23
|
+
} | {
|
|
24
|
+
read?: never;
|
|
25
|
+
/** Open a reader for one set of watches, so their reads can share work. */ readBatch: () => Read;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Subscriptions answered by `read`, the reach's one answer to a line, and
|
|
29
|
+
* asked again on each {@link Reached.hear}. A refusal is sent under its id
|
|
30
|
+
* and the subscription stays open: the next change asks again, and a member
|
|
31
|
+
* that came back answers.
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { reached } from '@yaks/api/reached'
|
|
35
|
+
* import { equal, until } from '@yaks/testing'
|
|
36
|
+
*
|
|
37
|
+
* let books = [{ entity: { eid: 'dune' }, book: {} }]
|
|
38
|
+
* let subs = reached({ read: () => Promise.resolve(books) })
|
|
39
|
+
* let frames: unknown[] = []
|
|
40
|
+
* subs.open((f) => frames.push(f), 'all', '.book')
|
|
41
|
+
* await until(() => frames.length == 1)
|
|
42
|
+
* books = [...books, { entity: { eid: 'emma' }, book: {} }]
|
|
43
|
+
* await subs.hear()
|
|
44
|
+
* equal(frames[1], { id: 'all', bundles: books, reset: true })
|
|
45
|
+
* ```
|
|
46
|
+
*/ export declare let reached: (opts: Reader & {
|
|
47
|
+
/** told of every answer refused, with its line, for a host that knows
|
|
48
|
+
* whose line it was */ refused?: (error: unknown, line: string) => void;
|
|
49
|
+
/** what a failure reported here is tagged with (./refuse.ts `fault`) */ tags?: Tags;
|
|
50
|
+
/** When a time-dependent answer next changes without a write. */ renew?: (answer: Bundle[] | Reduced) => number | undefined;
|
|
51
|
+
/** Schedule a renewal at an absolute time. Defaults to the clock's timer. */ renewAt?: (at: number, read: () => Promise<void>) => () => void;
|
|
52
|
+
clock?: Clock;
|
|
53
|
+
/** The local revision at which a derived read starts. */ revision?: () => number;
|
|
54
|
+
/** Give other requests a turn between watches asked one after another:
|
|
55
|
+
* those recovered after wake, and those a change asks again. */ yieldRecovery?: () => Promise<void>;
|
|
56
|
+
/** Watches whose rows signal an upstream change on every refresh. */ signals?: (line: string) => boolean;
|
|
57
|
+
}) => Reached;
|
|
58
|
+
/** A derived read that changes with committed inputs in a local graph: each
|
|
59
|
+
* commit asks again the lines `affects` says it moves, after the write that
|
|
60
|
+
* made it has answered. An entity the commit deleted reaches `affects` as it
|
|
61
|
+
* stood before, beside its `$delete`, so a deletion is judged by what it took
|
|
62
|
+
* away; one deleted by another (a cascade) reaches it bare.
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* import { assertEquals } from '@std/assert'
|
|
66
|
+
* import { graph } from '@yaks/graph'
|
|
67
|
+
* import { ram } from '@yaks/ram'
|
|
68
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
69
|
+
* let vocab = loadVocab({ $defs: { book: { component: true } } })
|
|
70
|
+
* let g = graph({ vocab, storage: ram(vocab) })
|
|
71
|
+
* await g.apply([{ entity: { eid: 'a' }, book: {} }])
|
|
72
|
+
* let seen: unknown[] = []
|
|
73
|
+
* let subs = computed(g, {
|
|
74
|
+
* affects: (bundles) => (seen.push(...bundles), false),
|
|
75
|
+
* read: async () => [],
|
|
76
|
+
* })
|
|
77
|
+
* subs.open(() => {}, 'all', '.book')
|
|
78
|
+
* await g.apply([{ entity: { eid: 'a' }, $delete: true }])
|
|
79
|
+
* await new Promise((done) => setTimeout(done, 10))
|
|
80
|
+
* assertEquals(seen, [{ entity: { eid: 'a' }, book: {}, $delete: true }])
|
|
81
|
+
* ```
|
|
82
|
+
*/ export declare let computed: (graph: Graph, opts: Parameters<typeof reached>[0] & {
|
|
83
|
+
affects: (bundles: Bundle[]) => Affected | Promise<Affected>;
|
|
84
|
+
hold?: (work: Promise<void>) => void;
|
|
85
|
+
}) => Reached;
|
|
86
|
+
type Affected = boolean | NonNullable<Parameters<Reached['hear']>[0]>;
|
|
87
|
+
/**
|
|
88
|
+
* One registry of two: the lines `borrows` names asked of `lent`, and every
|
|
89
|
+
* other subscription (its own lines, the raw feed) and every relayed value
|
|
90
|
+
* of `subs`, so a host's socket serves both as one ({@link Subs}). A watch
|
|
91
|
+
* replaced by a line the other answers moves to it.
|
|
92
|
+
*
|
|
93
|
+
* ```ts
|
|
94
|
+
* import { reached, beside } from '@yaks/api/reached'
|
|
95
|
+
* import { subscriptions } from '@yaks/api'
|
|
96
|
+
* import { graph } from '@yaks/graph'
|
|
97
|
+
* import { ram } from '@yaks/ram'
|
|
98
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
99
|
+
* import { equal, until } from '@yaks/testing'
|
|
100
|
+
*
|
|
101
|
+
* let vocab = loadVocab({ $defs: { book: { component: true, type: 'object' } } })
|
|
102
|
+
* let own = subscriptions(graph({ vocab, storage: ram(vocab) }))
|
|
103
|
+
* let lent = reached({
|
|
104
|
+
* read: () => Promise.resolve([{ entity: { eid: 'loan' }, loan: {} }]),
|
|
105
|
+
* })
|
|
106
|
+
* let both = beside(own, lent, (line) => line.includes('loan'))
|
|
107
|
+
* let frames: unknown[] = []
|
|
108
|
+
* both.open((f) => frames.push(f), 'loans', '.loan')
|
|
109
|
+
* await until(() => frames.length == 1)
|
|
110
|
+
* equal(lent.watching(), true)
|
|
111
|
+
* ```
|
|
112
|
+
*/ export declare let beside: (subs: Subs, lent: Reached, borrows: (line: string) => boolean) => Subs;
|
|
113
|
+
/** A frame a kept watch sends, before it is said under a subscription's id:
|
|
114
|
+
* its `refused` is the error itself. */ export type Kept = Omit<Frame, 'id' | 'refused'> & {
|
|
115
|
+
refused?: unknown;
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Subscriptions whose every line is a live watch another keeps, such as a
|
|
119
|
+
* reach's (@yaks/reach `watch`): what it sends goes out under the
|
|
120
|
+
* subscription's id, and closing the subscription stops it. It answers lines
|
|
121
|
+
* only, so the raw feed is refused and relayed values go nowhere.
|
|
122
|
+
*
|
|
123
|
+
* ```ts
|
|
124
|
+
* import { watched } from '@yaks/api/reached'
|
|
125
|
+
* import { equal } from '@yaks/testing'
|
|
126
|
+
*
|
|
127
|
+
* let stopped = 0
|
|
128
|
+
* let subs = watched({
|
|
129
|
+
* watch: (line, sink) => {
|
|
130
|
+
* sink({ bundles: [{ entity: { eid: line } }], reset: true })
|
|
131
|
+
* return () => stopped++
|
|
132
|
+
* },
|
|
133
|
+
* })
|
|
134
|
+
* let frames: unknown[] = []
|
|
135
|
+
* let to = (f: unknown) => void frames.push(f)
|
|
136
|
+
* subs.open(to, 'a', 'dune')
|
|
137
|
+
* subs.drop(to)
|
|
138
|
+
* equal(frames, [{ id: 'a', bundles: [{ entity: { eid: 'dune' } }], reset: true }])
|
|
139
|
+
* equal(stopped, 1)
|
|
140
|
+
* ```
|
|
141
|
+
*/ export declare let watched: (opts: {
|
|
142
|
+
/** watch a line as `read` says who asks; returns what stops it */ watch: (line: string, sink: (frame: Kept) => void, read?: ReadOpts) => () => void;
|
|
143
|
+
/** what a failure reported here is tagged with (./refuse.ts `fault`) */ tags?: Tags;
|
|
144
|
+
}) => RecoverableListening;
|
|
145
|
+
//# sourceMappingURL=reached.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reached.d.ts","sources":["../reached.ts"],"names":[],"mappings":"AAYA,cAAc,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,OAAO,2BAAgC;AAE9E,SAAgB,KAAK,KAAK,wBAAqC;AAC/D,SAAyB,KAAK,IAAI,sBAAqB;AACvD,cAEE,KAAK,EACL,OAAO,EACP,oBAAoB,EACpB,IAAI,EACJ,IAAI,oBACY;AAClB,cAAc,UAAU,oBAAmB;AAE3C,2DAA2D,GAC3D,YAAY;EACV;aACW,GACX,OACE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GAAG,YAAY,WAAW,QAAQ,SAAS;AAElE;;;;;;;;;;;;;;;;;;;;;;;;;CAyBC,GACD,OAAO,YAAI,SACT,MAAM,MACN,MAAM,SACN,UAAU,MAAM,MAAM,KAAK,OAAO,KACjC,KA6CF;AAED;sCACsC,GACtC,YAAY,OAAO,KAAK,OAAO,OAAO;EAAe,UAAU,OAAO;;AAEtE;;;;;;;;;;;;;;;;;;;;;;;;CAwBC,GACD,OAAO,YAAI,UAAW;EACpB,gEAAgE,GAChE,QACE,MAAM,MAAM,EACZ,OAAO,OAAO,SAAS,IAAI,EAC3B,OAAO,mBACE,IAAI;EACf,sEAAsE,GACtE,OAAO;MACL,qBAuCH"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { type Actor, type Bundle, type Graph, type Plugin } from "@yaks/graph";
|
|
2
|
+
import type { Driver } from "@yaks/sql";
|
|
3
|
+
import { type Receipt } from "./log.js";
|
|
4
|
+
/** API-owned graph hooks over the host's write log (`db`), also loaded by
|
|
5
|
+
* the box's graph facet: a keyed write's receipt is inserted in the
|
|
6
|
+
* transaction that commits it, and a resend is told what it was told. */ export declare let receipts: (db: Driver) => Plugin;
|
|
7
|
+
/** Apply with a receipt named by its writer and key (log.ts `receiptKey`),
|
|
8
|
+
* kept in the graph's registered write log in the write's own transaction.
|
|
9
|
+
* A standalone door may supply the graph's own connection as `db`.
|
|
10
|
+
* It is kept as every receipt under
|
|
11
|
+
* that key is: an outbox key's for good and bound to its exact body, since a
|
|
12
|
+
* durable browser outbox may replay after an arbitrary delay, and any other's
|
|
13
|
+
* for the resend window. A host with no log refuses a keyed write
|
|
14
|
+
* rather than apply it without the receipt that makes a resend safe. */ export declare let received: (graph: Graph, db: Driver | undefined, key: string, body: string, actor: Actor | null, bundles: Bundle[], options: Parameters<Graph['apply']>[1]) => Promise<Bundle[]>;
|
|
15
|
+
/** A host's log and the words its answers are kept in, read as each is
|
|
16
|
+
* needed: an object opens its storage again with each incarnation. */ export type Beside = {
|
|
17
|
+
readonly db: Driver;
|
|
18
|
+
readonly vocab: Graph['vocab'];
|
|
19
|
+
now: () => number;
|
|
20
|
+
};
|
|
21
|
+
/** What a commit lands: a kept write by its place in the log, a live keyed
|
|
22
|
+
* request by its receipt, or neither. */ export type Mark = number | Receipt | null;
|
|
23
|
+
/** A host's hook into its graph's commits, and the one apply it names a
|
|
24
|
+
* write for. */ export type Landing = {
|
|
25
|
+
plugin: Plugin;
|
|
26
|
+
during: <T>(mark: Mark, apply: () => T) => T;
|
|
27
|
+
};
|
|
28
|
+
/** The hook that completes a write in the transaction that commits its
|
|
29
|
+
* batch: a kept write leaves the log, or stays `applied` with its answer
|
|
30
|
+
* (`landed`), and a live keyed request's receipt is inserted (`received`). A
|
|
31
|
+
* resend cannot see one without the other. `during` names the write for the
|
|
32
|
+
* one synchronous `apply()` it wraps; every other batch (an effect's, a
|
|
33
|
+
* tick's, one applied after an await) finds none named.
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { equal } from '@yaks/testing'
|
|
37
|
+
* import { graph } from '@yaks/graph'
|
|
38
|
+
* import { ram } from '@yaks/ram'
|
|
39
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
40
|
+
* import { open } from '@yaks/sqlite/db'
|
|
41
|
+
* import { keep, next, raise } from './log.ts'
|
|
42
|
+
* import { landing } from './receipts.ts'
|
|
43
|
+
* let vocab = loadVocab({ $defs: { doc: { component: true, type: 'object',
|
|
44
|
+
* properties: { title: { type: 'string' } } } } })
|
|
45
|
+
* let db = open(':memory:')
|
|
46
|
+
* raise(db)
|
|
47
|
+
* let hook = landing({ db, vocab, now: Date.now })
|
|
48
|
+
* let g = graph({ vocab, storage: ram(vocab), plugins: [hook.plugin] })
|
|
49
|
+
* let sent = new Request('http://store/apply', { method: 'POST', body: '[]' })
|
|
50
|
+
* let seq = keep(db, sent, '[]')
|
|
51
|
+
* await hook.during(seq, () => g.apply([{ entity: { eid: 'a' }, doc: { title: 'A' } }]))
|
|
52
|
+
* equal(next(db), null)
|
|
53
|
+
* ```
|
|
54
|
+
*/ export declare let landing: (log: Beside) => Landing;
|
|
55
|
+
/** A refusal that asks to be sent again `retry.after` ms on. */ export type Halt = Error & {
|
|
56
|
+
retry: {
|
|
57
|
+
after: number;
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
/** What keeps an object from writing for a while, given as data: its row
|
|
61
|
+
* ceiling (@yaks/durable-object `bounded`), whose statements the log's own
|
|
62
|
+
* still take while it is stopped. */ export type Window = {
|
|
63
|
+
/** What a write would be refused with now, if anything. */ refusal: () => Halt | null;
|
|
64
|
+
/** Run statements a stopped object must still take. */ free: <T>(body: () => T) => T;
|
|
65
|
+
/** Whether a failure is the window's refusal. */ stops: (error: unknown) => error is Halt;
|
|
66
|
+
};
|
|
67
|
+
/** What a host lends its write log's replay ({@link replays}). */ export type Replayer = {
|
|
68
|
+
readonly db: Driver;
|
|
69
|
+
now: () => number;
|
|
70
|
+
readonly window: Window;
|
|
71
|
+
/** One kept write applied, `mark` naming it to {@link landing}. */ commit: (request: Request, mark: Mark) => Promise<Response>;
|
|
72
|
+
/** The object's alarm at `at`; `why` says what for. */ arm: (at: number, why?: string) => unknown;
|
|
73
|
+
/** A failure, where it happened. */ report: (error: unknown, request: string) => void;
|
|
74
|
+
/** A replay, or a read's wait on one, as a phase of the host's trace. */ stage?: (name: 'replay' | 'settle', work: () => Promise<void>) => Promise<void>;
|
|
75
|
+
/** How long until the alarm comes back for a write kept on a failure. */ retry?: number;
|
|
76
|
+
};
|
|
77
|
+
/** What a host lends its write log's door. */ export type Host = Beside & Replayer & {
|
|
78
|
+
/** The object brought up for a request: its refusal, if it can't be. */ ready: (request: Request) => Promise<Response | null>;
|
|
79
|
+
/** Every other door, for a write the log does not keep. */ serve: (request: Request) => Promise<Response>;
|
|
80
|
+
/** Today's authority to write, asked of a durable outbox write before its
|
|
81
|
+
* receipt is told: a cached acceptance is still a write answer. */ authorize: (request: Request) => Promise<void>;
|
|
82
|
+
/** Whether the log may keep a body, as said before decoding: one carrying
|
|
83
|
+
* a secret's value it may not. */ keeps: (told: string) => boolean;
|
|
84
|
+
/** The caller's own view of an answer. */ answer: (request: Request, response: Response) => Promise<Response>;
|
|
85
|
+
/** Why the object could not start, once it could not. */ refused: () => string | null;
|
|
86
|
+
/** Who sent a request, said by its headers alone: the log names a keyed
|
|
87
|
+
* write by its writer (`./log.ts` `receiptKey`) before the host may have
|
|
88
|
+
* started to ask anything more. */ writer: (request: Request) => Actor | null;
|
|
89
|
+
};
|
|
90
|
+
/** The replay of a host's write log ({@link replays}). */ export type Replays = {
|
|
91
|
+
/** The log, from its oldest waiting write, one replay at a time. */ drain: () => Promise<void>;
|
|
92
|
+
/** The log first, for a request that reads: unless the log itself failed,
|
|
93
|
+
* which the alarm owns, or a replay is already running. */ settle: () => Promise<unknown> | void;
|
|
94
|
+
/** A replay asked for anew, by the alarm or a person's retry: the log that
|
|
95
|
+
* failed last time may stand now. */ mend: () => Promise<void>;
|
|
96
|
+
};
|
|
97
|
+
/** A host's write door over its log ({@link door}). */ export type Door = Replays & {
|
|
98
|
+
/** The hook that lands each write in its batch's transaction: last among
|
|
99
|
+
* the graph's, so the answer a keyed write keeps is the batch as every
|
|
100
|
+
* other commit hook left it. */ plugin: Plugin;
|
|
101
|
+
/** A write in at `/apply`. */ write: (request: Request) => Promise<Response>;
|
|
102
|
+
/** A durable outbox write as its commit begins: today's authority asked
|
|
103
|
+
* first, then the request bound to its body. Only a matching durable
|
|
104
|
+
* receipt may stand in for a rehearsal (`check`), which is told what the
|
|
105
|
+
* write was told; a fresh rehearsal still runs every guard and never
|
|
106
|
+
* writes a receipt. */ bound: (request: Request, text: string, check?: boolean) => Promise<Request | Response>;
|
|
107
|
+
/** A batch's commit around the host's one synchronous `apply()`, the
|
|
108
|
+
* write it lands named to `plugin` for exactly that call: a live keyed
|
|
109
|
+
* write is looked for once more right before it, and one already in the
|
|
110
|
+
* log is told what that one was told. */ applying: <T>(request: Request, mark: Mark, apply: () => T) => T | Response;
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* The replay of a host's write log, for a host whose input comes in at a
|
|
114
|
+
* door of its own and is kept there (log.ts `keep`): a space's Builder keeps
|
|
115
|
+
* a person's lines so, and replays them one turn at a time.
|
|
116
|
+
*
|
|
117
|
+
* ```ts
|
|
118
|
+
* import { equal } from '@yaks/testing'
|
|
119
|
+
* import { open } from '@yaks/sqlite/db'
|
|
120
|
+
* import { keep, next, raise } from './log.ts'
|
|
121
|
+
* import { replays } from './receipts.ts'
|
|
122
|
+
* let db = open(':memory:')
|
|
123
|
+
* raise(db)
|
|
124
|
+
* let heard: string[] = []
|
|
125
|
+
* let log = replays({
|
|
126
|
+
* db,
|
|
127
|
+
* now: Date.now,
|
|
128
|
+
* arm: () => {},
|
|
129
|
+
* report: () => {},
|
|
130
|
+
* window: {
|
|
131
|
+
* refusal: () => null,
|
|
132
|
+
* free: (body) => body(),
|
|
133
|
+
* stops: (_e): _e is never => false,
|
|
134
|
+
* },
|
|
135
|
+
* commit: async (r) => (heard.push(await r.text()), new Response('[]')),
|
|
136
|
+
* })
|
|
137
|
+
* keep(db, new Request('http://builder/say', { method: 'POST' }), 'hello')
|
|
138
|
+
* await log.drain()
|
|
139
|
+
* equal([heard, next(db)], [['hello'], null])
|
|
140
|
+
* ```
|
|
141
|
+
*/ export declare let replays: (host: Replayer) => Replays;
|
|
142
|
+
/**
|
|
143
|
+
* A host's write door over its log. A live keyed write commits its mutation
|
|
144
|
+
* and successful receipt together, with no transient log stages. Input that
|
|
145
|
+
* cannot commit is kept and replayed in order by the one drain that runs at a
|
|
146
|
+
* time ({@link replays}). A write that finds the object stopped is kept and
|
|
147
|
+
* told when to look again, and the alarm armed for the window's end replays
|
|
148
|
+
* it. One that finds the object refusing to start, or the log itself
|
|
149
|
+
* failing, is answered 202 and waits in the log. A resend (its idempotency
|
|
150
|
+
* key already in the log) is the write it repeats: told what that one was
|
|
151
|
+
* told, or waiting beside it.
|
|
152
|
+
*/ export declare let door: (host: Host) => Door;
|
|
153
|
+
//# sourceMappingURL=receipts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"receipts.d.ts","sources":["../receipts.ts"],"names":[],"mappings":"AAsBA,SACE,KAAK,KAAK,EACV,KAAK,MAAM,EACX,KAAK,KAAK,EACV,KAAK,MAAM,2BAEkB;AAC/B,cAAc,MAAM,yBAA8B;AAIlD,SAmBE,KAAK,OAAO,mBAWG;AA4BjB;;uEAEuE,GACvE,OAAO,YAAI,WAAY,IAAI,WAAS,OAuCnC;AAED;;;;;;;sEAOsE,GACtE,OAAO,YAAI,WACT,OAAO,OACP,IAAI,SAAS,SAAS,EACtB,KAAK,MAAM,EACX,MAAM,MAAM,EACZ,OAAO,QAAQ,IAAI,EACnB,SAAS,UACT,SAAS,WAAW,MAAM,UAAU,OACnC,QAAQ,UA8BV;AAID;oEACoE,GACpE,YAAY;WACD,IAAI;WACJ,OAAO,MAAM;EACtB,WAAW,MAAM;;AAGnB;uCACuC,GACvC,YAAY,OAAO,MAAM,GAAG,UAAU,IAAI;AAE1C;cACc,GACd,YAAY;EACV,QAAQ;EACR,SAAS,GAAG,MAAM,MAAM,aAAa,MAAM;;AAG7C;;;;;;;;;;;;;;;;;;;;;;;;;;CA0BC,GACD,OAAO,YAAI,UAAW,KAAK,WAAS,QA6BnC;AAID,8DAA8D,GAC9D,YAAY,OAAO;EAAU;IAAS,OAAO,MAAM;;;AAEnD;;mCAEmC,GACnC,YAAY;EACV,yDAAyD,GACzD,eAAe,OAAO,IAAI;EAC1B,qDAAqD,GACrD,OAAO,GAAG,YAAY,MAAM;EAC5B,+CAA+C,GAC/C,QAAQ,OAAO,OAAO,KAAK,SAAS;;AAGtC,gEAAgE,GAChE,YAAY;WACD,IAAI;EACb,WAAW,MAAM;WACR,QAAQ;EACjB,iEAAiE,GACjE,SAAS,SAAS,SAAS,MAAM,SAAS,QAAQ;EAClD,qDAAqD,GACrD,MAAM,IAAI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
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type Stale } from "@yaks/graph/errors";
|
|
2
|
+
import { FILED } from "./headers.js";
|
|
3
|
+
/** A refusal, as a client reads it: the error's name, its message, and any
|
|
4
|
+
* fields the error carried (a {@link https://jsr.io/@yaks/graph | Stale}
|
|
5
|
+
* precondition's `eid`, `comp`, `prop` and `current`, say). */ export type Refusal = {
|
|
6
|
+
/** the error's name — `Refused`, `Stale`, `Unsupported`, `Unknown`,
|
|
7
|
+
* `Unauthorized`, `Denied` */ error: string;
|
|
8
|
+
/** what was wrong, in the error's own words */ message: string;
|
|
9
|
+
/** whatever else the error carried */ [detail: string]: unknown;
|
|
10
|
+
};
|
|
11
|
+
/** A graph precondition conflict carries the value the caller can retry on. */ export declare let staleRefusal: (code: number, body: unknown) => body is Refusal & Pick<Stale, 'eid' | 'comp' | 'prop' | 'current'>;
|
|
12
|
+
/** The request could not be authenticated. Throw this from an
|
|
13
|
+
* {@link https://jsr.io/@yaks/api/doc/~/Authenticate | Authenticate} to answer
|
|
14
|
+
* a request with a 401. */ export declare class Unauthorized extends Error {
|
|
15
|
+
/** @param message what the caller is missing (default: `not authenticated`) */ constructor(message?: string);
|
|
16
|
+
}
|
|
17
|
+
/** An error as the body a client reads: its name as `error`, its message, and
|
|
18
|
+
* every other field it carries. A refusal relayed from another door is said
|
|
19
|
+
* as that door said it. */ export declare let refusal: (err: unknown) => Refusal;
|
|
20
|
+
/** What a host adds to the failures a door of its reports (`{store: 'x'}`):
|
|
21
|
+
* the door's own words for where they happened, beside `where`. */ export type Tags = Record<string, string>;
|
|
22
|
+
/** A host's reporter ({@link faults}): a failure, where it was caught, and
|
|
23
|
+
* the tags the door that caught it was given (none: `{}`). */ export type Reporter = (err: unknown, where: string, tags: Tags) => void;
|
|
24
|
+
/** Report every server failure this package catches to `report`, with where
|
|
25
|
+
* it was caught and its door's tags: the host's own reporter, which tells its
|
|
26
|
+
* tracker. A failure answered to its caller and one with no caller at all (a
|
|
27
|
+
* socket's message, a subscription's refresh, a page body that did not build)
|
|
28
|
+
* go the same way. The newest installed is the one told; the function
|
|
29
|
+
* returned removes it. */ export declare let faults: (report: Reporter) => () => void;
|
|
30
|
+
/** The header on an answer whose failure was reported where it was caught:
|
|
31
|
+
* a caller that relays the answer files it no second time. */ export { FILED };
|
|
32
|
+
/** Whether a host's reporter hears this package's failures ({@link faults}). */ export declare let reporting: () => boolean;
|
|
33
|
+
/** A server failure, reported with where it happened and its door's tags to
|
|
34
|
+
* the host's reporter ({@link faults}), or said on the console while none is
|
|
35
|
+
* installed; a refusal (below 500) is the caller's and says nothing. A
|
|
36
|
+
* refusal relayed from another door is judged and reported as the error it
|
|
37
|
+
* names. */ export declare let fault: (err: unknown, where: string, tags?: Tags) => void;
|
|
38
|
+
export declare let watch: (request: Request) => void;
|
|
39
|
+
export declare let cause: (request: Request) => unknown;
|
|
40
|
+
/** A JSON response. */ export declare let json: (body: unknown, code?: number) => Response;
|
|
41
|
+
/** The seconds an error asks its caller to wait (`retry.after`, in
|
|
42
|
+
* milliseconds, as @yaks/effects reads it), for an answer's `retry-after`. */ export declare let waits: (err: unknown) => number | null;
|
|
43
|
+
/** A thrown error as its response: the refusal body, at its HTTP status, with
|
|
44
|
+
* a `retry-after` when the error asks for a wait (`retry.after`). A
|
|
45
|
+
* server failure is reported once: by the watch the request is answered under
|
|
46
|
+
* (`served`), with its request, or else here ({@link fault}), with the method
|
|
47
|
+
* and path when a request is supplied, never the query string, headers or
|
|
48
|
+
* body, and the `tags` of the door that answered it, and its answer says so
|
|
49
|
+
* ({@link FILED}) while a host's reporter hears it. An expected client
|
|
50
|
+
* refusal is not reported at all. */ export declare let refuse: (err: unknown, request?: Request, tags?: Tags) => Response;
|
|
51
|
+
//# sourceMappingURL=refuse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refuse.d.ts","sources":["../refuse.ts"],"names":[],"mappings":"AAaA,SAAS,KAAK,KAAK,kCAAuD;AAC1E,S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0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"relay.d.ts","sources":["../relay.ts"],"names":[],"mappings":"AAmCA;oBACoB,GACpB,YAAY,SAAS,UAAU,IAAI,EAAE,OAAO,MAAM,WAAW,IAAI"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { Bundle } from "@yaks/graph";
|
|
2
|
+
import { type Tags } from "./refuse.js";
|
|
3
|
+
import type { Handler } from "./route.js";
|
|
4
|
+
/** Where a request that broke goes: its `request` bundle, and the error when
|
|
5
|
+
* one was thrown. Whatever it does, it must not throw; one that does is said
|
|
6
|
+
* on the console, and the caller is answered all the same. */ export type Report = (request: Bundle, err?: unknown) => void;
|
|
7
|
+
/** How the server answered, as the `request` component records it. */ export type Answered = {
|
|
8
|
+
/** the HTTP status */ status: number;
|
|
9
|
+
/** milliseconds from arrival until the answer's head was ready */ ms: number;
|
|
10
|
+
/** the route that matched, in the server's own words (`/blob/*`) */ route?: string;
|
|
11
|
+
/** use the route pattern instead of a credential-bearing pathname */ redact?: boolean;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* A `User-Agent` reduced to what a person reading a failure wants: the
|
|
15
|
+
* browser, its major version and the operating system. Anything else is its
|
|
16
|
+
* first product token.
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { assertEquals } from '@std/assert'
|
|
20
|
+
* import { agent } from '@yaks/api'
|
|
21
|
+
*
|
|
22
|
+
* assertEquals(
|
|
23
|
+
* agent(
|
|
24
|
+
* 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 ' +
|
|
25
|
+
* '(KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
|
|
26
|
+
* ),
|
|
27
|
+
* 'Chrome 141 on macOS',
|
|
28
|
+
* )
|
|
29
|
+
* assertEquals(
|
|
30
|
+
* agent(
|
|
31
|
+
* 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) ' +
|
|
32
|
+
* 'AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 ' +
|
|
33
|
+
* 'Mobile/15E148 Safari/604.1',
|
|
34
|
+
* ),
|
|
35
|
+
* 'Safari 18 on iOS',
|
|
36
|
+
* )
|
|
37
|
+
* assertEquals(agent('curl/8.5.0'), 'curl 8.5.0')
|
|
38
|
+
* ```
|
|
39
|
+
*/ export declare let agent: (ua: string) => string;
|
|
40
|
+
/**
|
|
41
|
+
* The `request` bundle for one answered request, under the id its caller was
|
|
42
|
+
* answered with. The address keeps no query string.
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { assertEquals } from '@std/assert'
|
|
46
|
+
* import { requested } from '@yaks/api'
|
|
47
|
+
*
|
|
48
|
+
* let r = new Request('https://shop.test/blob/ab?token=x', { method: 'PUT' })
|
|
49
|
+
* assertEquals(requested('1f0c', r, { status: 503, ms: 12, route: '/blob/*' }), {
|
|
50
|
+
* entity: { eid: '1f0c' },
|
|
51
|
+
* request: {
|
|
52
|
+
* method: 'PUT',
|
|
53
|
+
* url: 'https://shop.test/blob/ab',
|
|
54
|
+
* route: '/blob/*',
|
|
55
|
+
* status: 503,
|
|
56
|
+
* ms: 12,
|
|
57
|
+
* },
|
|
58
|
+
* })
|
|
59
|
+
* ```
|
|
60
|
+
*/ export declare let requested: (id: string, request: Request, a: Answered) => Bundle;
|
|
61
|
+
/** What {@link served} is told by the host it serves: where a request that
|
|
62
|
+
* broke goes, and how to name the route that answered one. */ export type Watch = {
|
|
63
|
+
/** Exact graph identity, even when the handler reads through an overlay. */ graph?: object;
|
|
64
|
+
report?: Report;
|
|
65
|
+
route?: (request: Request) => string | undefined;
|
|
66
|
+
/** whether the pathname carries a credential and must be redacted */ redact?: (request: Request) => boolean;
|
|
67
|
+
/** What the host adds to each failure reported without a `report`
|
|
68
|
+
* (./refuse.ts `Tags`), read as one is. */ tags?: () => Tags;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* `handle`, watched. Each answer is timed; one at 500 or over, thrown or
|
|
72
|
+
* returned, is answered with an `x-request-id` and reported as a `request`
|
|
73
|
+
* bundle under that id, with the error thrown or the one `refuse` answered.
|
|
74
|
+
* A thrown error is answered as its refusal body. An answer that already
|
|
75
|
+
* carries an `x-request-id` was reported by whoever answered it, so a watched
|
|
76
|
+
* handler inside another is reported once; one reported to a host's reporter
|
|
77
|
+
* says so to its caller ({@link FILED}).
|
|
78
|
+
*/ export declare let served: (handle: Handler, o?: Watch) => Handler;
|
|
79
|
+
//# sourceMappingURL=request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.d.ts","sources":["../request.ts"],"names":[],"mappings":"AAeA,cAAc,MAAM,2BAAgC;AAGpD,SAOE,KAAK,IAAI,sBAES;AACpB,cAAc,OAAO,qBAAoB;AAGzC;;4DAE4D,GAC5D,YAAY,UAAU,SAAS,QAAQ,MAAM,OAAO,KAAK,IAAI;AAE7D,oEAAoE,GACpE,YAAY;EACV,oBAAoB,GACpB,QAAQ,MAAM;EACd,gEAAgE,GAChE,IAAI,MAAM;EACV,kEAAkE,GAClE,QAAQ,MAAM;EACd,mEAAmE,GACnE,SAAS,OAAO;;AAuBlB;;;;;;;;;;;;;;;;;;;;;;;;;;CA0BC,GACD,OAAO,YAAI,QAAS,IAAI,MAAM,KAAG,MAAM,CAQtC;AAED;;;;;;;;;;;;;;;;;;;;CAoBC,GACD,OAAO,YAAI,YACT,IAAI,MAAM,EACV,SAAS,SACT,GAAG,aACF,OAcF;AAWD;4DAC4D,GAC5D,YAAY;EACV,0EAA0E,GAC1E,QAAQ,MAAM;EACd,SAAS;EACT,SAAS,SAAS,YAAY,MAAM,GAAG,SAAS;EAChD,mEAAmE,GACnE,UAAU,SAAS,YAAY,OAAO;EACtC;2CACyC,GACzC,aAAa;;AAGf;;;;;;;;CAQC,GACD,OAAO,YAAI,SACR,QAAQ,SAAS,IAAG,UAAa,QAuDjC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** A request whose method is a write's but whose work only reads, such as an
|
|
2
|
+
* MCP call to a read-only tool: the cut at the end of a grace answers it 503,
|
|
3
|
+
* as it does a GET, and the drain no longer waits for it. */ export declare let reads: (request: Request) => void;
|
|
4
|
+
/** The cut at the end of a grace, for a write's request: what its admitted
|
|
5
|
+
* work stops waiting on, such as the extras an MCP call reads beside a kept
|
|
6
|
+
* answer, or a tool still running (@yaks/tools `Invocation.call`). */ export declare let cutOf: (request: Request) => AbortSignal | undefined;
|
|
7
|
+
//# sourceMappingURL=requests.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"requests.d.ts","sources":["../requests.ts"],"names":[],"mappings":"AAsBA;;2DAE2D,GAC3D,OAAO,YAAI,QAAS,SAAS,YAAU,IAAI,CAA4B;AAWvE;;oEAEoE,GACpE,OAAO,YAAI,QAAS,SAAS,YAAU,cAAc,SAAS,CAC3C"}
|
package/_dist/route.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { Actor, ReadOpts } from "@yaks/graph";
|
|
2
|
+
import { type Applying, type ListQuery } from "./doors.js";
|
|
3
|
+
import { type Report } from "./request.js";
|
|
4
|
+
import { type Upgrade } from "./socket.js";
|
|
5
|
+
import { type Subs } from "./subs.js";
|
|
6
|
+
import { type Handler } from "./routed.js";
|
|
7
|
+
export { type Handler, type Route, routed } from "./routed.js";
|
|
8
|
+
/** A check every request passes before any route or door answers it: a
|
|
9
|
+
* plugin's own word on which requests it lets in. It refuses one by throwing
|
|
10
|
+
* (a `Denied`, say, which answers 403), and anything it does not throw at goes
|
|
11
|
+
* on. */ export type Filter = (request: Request) => void | Promise<void>;
|
|
12
|
+
/** Who a request that reached the host through a plugin's link (a tunnel) is
|
|
13
|
+
* from: its actor, `null` for nobody, or `undefined` for a request that came
|
|
14
|
+
* another way. How a request arrived outranks what it says about itself, so a
|
|
15
|
+
* host asks this before its `authenticate`: a request through a link never
|
|
16
|
+
* names one of the host's own runs. */ export type Arrived = (request: Request) => Actor | null | undefined | Promise<Actor | null | undefined>;
|
|
17
|
+
/** The tools a request that reached the host through a plugin's link is
|
|
18
|
+
* offered, by name, or `undefined` for a request that came another way, which
|
|
19
|
+
* is offered every tool. */ export type Offers = (request: Request) => string[] | undefined;
|
|
20
|
+
/** What a request that reached the host through a plugin's link is told
|
|
21
|
+
* before anything else a door says to read, such as the text of the persona
|
|
22
|
+
* it writes as, or `undefined` for a request told nothing of its own. */ export type Told = (request: Request) => string | undefined | Promise<string | undefined>;
|
|
23
|
+
/** What a host says of a caller beside the actor `authenticate` named, which
|
|
24
|
+
* `/who` answers with it: whether they may read and write, say, or where they
|
|
25
|
+
* sign in. The host's own words; this package reads none of them. */ export type Standing = (request: Request, actor: Actor | null) => Record<string, unknown> | Promise<Record<string, unknown>>;
|
|
26
|
+
/** How a handler is built: the graph it serves, the callbacks the
|
|
27
|
+
* application supplies, and what its apply door is made of ({@link Applying}). */ export type Options = Applying & {
|
|
28
|
+
/** what `/who` says of a caller beside its actor (default: nothing) */ standing?: Standing;
|
|
29
|
+
/** Caller read context for queries, write replies and socket frames. */ read?: (request: Request) => ReadOpts | undefined | Promise<ReadOpts | undefined>;
|
|
30
|
+
/** Shape the published bundles of a query, including a live snapshot. */ list?: ListQuery;
|
|
31
|
+
/** the runtime's WebSocket upgrade (default: Deno's) */ upgrade?: Upgrade;
|
|
32
|
+
/** schedule socket relay batches (default: setTimeout) */ socketTimer?: (fn: () => void, ms: number) => void;
|
|
33
|
+
/** clock for socket relay admission (default: Date.now) */ socketNow?: () => number;
|
|
34
|
+
/** the subscription registry (default: a fresh one over `graph`) */ subs?: Subs;
|
|
35
|
+
/** where a request that broke goes, as its `request` bundle (default: the
|
|
36
|
+
* console) */ report?: Report;
|
|
37
|
+
/** a path the doors answer under as well as at the root (`/api`, where a
|
|
38
|
+
* page's `./api/` client finds its store) */ under?: string;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* `GET /who` alone, as {@link api} answers it: the actor `authenticate` names
|
|
42
|
+
* and what `standing` says beside it, for a host whose other doors are its
|
|
43
|
+
* own.
|
|
44
|
+
*
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { whoDoor } from '@yaks/api'
|
|
47
|
+
* import { assertEquals } from '@std/assert'
|
|
48
|
+
*
|
|
49
|
+
* let door = whoDoor({
|
|
50
|
+
* authenticate: () => ({ by: 'ada' }),
|
|
51
|
+
* standing: () => ({ writes: true }),
|
|
52
|
+
* })
|
|
53
|
+
* let answer = await door(new Request('http://notes.test/who'))
|
|
54
|
+
* assertEquals(await answer.json(), { by: 'ada', writes: true })
|
|
55
|
+
* ```
|
|
56
|
+
*/ export declare let whoDoor: (opts: Pick<Options, 'authenticate' | 'standing'>) => Handler;
|
|
57
|
+
/** The paths {@link api} answers itself. */ export declare let DOORS: string[];
|
|
58
|
+
/**
|
|
59
|
+
* Build the request handler for a graph: `POST /apply`, `GET|POST /query`,
|
|
60
|
+
* `/ws` for live subscriptions, `GET /vocab`, the vocabulary, and `GET /who`,
|
|
61
|
+
* the actor `authenticate` names for the request with what `standing` says
|
|
62
|
+
* beside it ({@link whoDoor}). Everything else is a 404, every thrown error
|
|
63
|
+
* becomes the refusal body it describes (see the README's Refusals), and an
|
|
64
|
+
* answer at 500 or over goes to `report` as a `request` bundle (./request.ts
|
|
65
|
+
* `served`).
|
|
66
|
+
*
|
|
67
|
+
* ```ts ignore
|
|
68
|
+
* Deno.serve(api({ graph, authenticate }))
|
|
69
|
+
* ```
|
|
70
|
+
*/ export declare let api: (opts: Options) => Handler;
|
|
71
|
+
//# sourceMappingURL=route.d.ts.map
|