@yaks/workerd 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 (74) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +273 -2
  3. package/_dist/cache.d.ts +67 -0
  4. package/_dist/cache.d.ts.map +1 -0
  5. package/_dist/compose.d.ts +69 -0
  6. package/_dist/compose.d.ts.map +1 -0
  7. package/_dist/deferred.d.ts +10 -0
  8. package/_dist/deferred.d.ts.map +1 -0
  9. package/_dist/door.d.ts +36 -0
  10. package/_dist/door.d.ts.map +1 -0
  11. package/_dist/fetch.d.ts +27 -0
  12. package/_dist/fetch.d.ts.map +1 -0
  13. package/_dist/gen.d.ts +65 -0
  14. package/_dist/gen.d.ts.map +1 -0
  15. package/_dist/mod.d.ts +53 -0
  16. package/_dist/mod.d.ts.map +1 -0
  17. package/_dist/plugin.d.ts +15 -0
  18. package/_dist/plugin.d.ts.map +1 -0
  19. package/_dist/purge.d.ts +104 -0
  20. package/_dist/purge.d.ts.map +1 -0
  21. package/_dist/rate.d.ts +65 -0
  22. package/_dist/rate.d.ts.map +1 -0
  23. package/_dist/stub.d.ts +20 -0
  24. package/_dist/stub.d.ts.map +1 -0
  25. package/_dist/upgrade.d.ts +25 -0
  26. package/_dist/upgrade.d.ts.map +1 -0
  27. package/_dist/version.d.ts +2 -0
  28. package/_dist/version.d.ts.map +1 -0
  29. package/_dist/worker.d.ts +40 -0
  30. package/_dist/worker.d.ts.map +1 -0
  31. package/cache.js +199 -0
  32. package/cache.js.map +1 -0
  33. package/cache.ts +254 -0
  34. package/compose.js +126 -0
  35. package/compose.js.map +1 -0
  36. package/compose.ts +236 -0
  37. package/deferred.js +39 -0
  38. package/deferred.js.map +1 -0
  39. package/deferred.ts +64 -0
  40. package/deno.json +1 -0
  41. package/door.js +64 -0
  42. package/door.js.map +1 -0
  43. package/door.ts +89 -0
  44. package/fetch.js +228 -0
  45. package/fetch.js.map +1 -0
  46. package/fetch.ts +231 -0
  47. package/gen.js +153 -0
  48. package/gen.js.map +1 -0
  49. package/gen.ts +189 -0
  50. package/mod.js +53 -0
  51. package/mod.js.map +1 -0
  52. package/mod.ts +69 -0
  53. package/package.json +52 -4
  54. package/plugin.js +16 -0
  55. package/plugin.js.map +1 -0
  56. package/plugin.ts +19 -0
  57. package/purge.js +200 -0
  58. package/purge.js.map +1 -0
  59. package/purge.ts +245 -0
  60. package/rate.js +84 -0
  61. package/rate.js.map +1 -0
  62. package/rate.ts +110 -0
  63. package/stub.js +23 -0
  64. package/stub.js.map +1 -0
  65. package/stub.ts +42 -0
  66. package/upgrade.js +82 -0
  67. package/upgrade.js.map +1 -0
  68. package/upgrade.ts +96 -0
  69. package/version.js +24 -0
  70. package/version.js.map +1 -0
  71. package/version.ts +30 -0
  72. package/worker.js +62 -0
  73. package/worker.js.map +1 -0
  74. package/worker.ts +87 -0
@@ -0,0 +1,36 @@
1
+ import type { Authenticate } from "@yaks/api";
2
+ import type { Actor } from "@yaks/graph";
3
+ /** Every cookie on a request, by name. An absent or malformed header is an
4
+ * empty set, never a throw. */ export declare let cookies: (request: Request) => Record<string, string>;
5
+ /** The `authorization: Bearer …` token on a request, or null. The scheme is
6
+ * matched case-insensitively, as HTTP requires. */ export declare let bearer: (request: Request) => string | null;
7
+ /** How a Worker's authentication is configured. */ export type Door = {
8
+ /** turn a credential into the actor making the request — `by` whoever holds
9
+ * it, and `via` whatever it arrived through; `null` for a token this
10
+ * application does not accept */ verify: (token: string, request: Request) => Actor | null | Promise<Actor | null>;
11
+ /** the cookie a session token lives in (default: read only the bearer) */ cookie?: string;
12
+ /** answer a request with no verified identity with a 401 (default: it is
13
+ * served, and its writes are stored unattributed) */ required?: boolean;
14
+ };
15
+ /**
16
+ * Request authentication for a Worker: read the credential a request carries —
17
+ * the named cookie first, then a bearer token — and pass it to `verify`. The
18
+ * result is an
19
+ * {@link https://jsr.io/@yaks/api/doc/~/Authenticate | Authenticate} for
20
+ * `api()`, so it runs on reads, writes and socket upgrades alike.
21
+ *
22
+ * ```ts
23
+ * declare let memberFor: (token: string) => { by: string } | null
24
+ *
25
+ * let authenticate = door({
26
+ * cookie: 'shop_session',
27
+ * verify: (token) => memberFor(token),
28
+ * required: true,
29
+ * })
30
+ * ```
31
+ *
32
+ * With `required`, a request carrying no credential — or one `verify` rejects —
33
+ * gets a 401 response. Without it, the request is served and its writes are stored
34
+ * with no actor on them.
35
+ */ export declare let door: (o: Door) => Authenticate;
36
+ //# sourceMappingURL=door.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"door.d.ts","sources":["../door.ts"],"names":[],"mappings":"AAUA,cAAc,YAAY,yBAA8B;AAExD,cAAc,KAAK,2BAAgC;AAYnD;6BAC6B,GAC7B,OAAO,YAAI,UAAW,SAAS,YAAU,OAAO,MAAM,EAAE,MAAM,EAS7D;AAED;iDACiD,GACjD,OAAO,YAAI,SAAU,SAAS,YAAU,MAAM,GAAG,IAAI,CAKpD;AAED,iDAAiD,GACjD,YAAY;EACV;;iCAE+B,GAC/B,SACE,OAAO,MAAM,EACb,SAAS,YACN,QAAQ,IAAI,GAAG,QAAQ,QAAQ,IAAI;EACxC,wEAAwE,GACxE,SAAS,MAAM;EACf;qDACmD,GACnD,WAAW,OAAO;;AAGpB;;;;;;;;;;;;;;;;;;;;CAoBC,GACD,OAAO,YAAI,OAAQ,GAAG,SAAO,aAK5B"}
@@ -0,0 +1,27 @@
1
+ import { type Clock } from "@yaks/fp";
2
+ /** A service binding's fetch door. */ export type Bound = {
3
+ fetch(req: Request, init?: Pick<RequestInit, 'signal'>): Promise<Response>;
4
+ };
5
+ /** How many ms the caller of a hop still waits for its answer. */ export declare const TIME_LEFT: 'x-yak-time-left';
6
+ /** A door whose every request says how long its caller still waits, until
7
+ * `end` (ms since the epoch), so the Worker behind it can give up first and
8
+ * say what it waited on. A resend says what is left when it goes. */ export declare let budgeted: (door: Bound, end: number, time?: Clock) => Bound;
9
+ /** The ms the caller of `req` still waits, when its hop said ({@link budgeted}). */ export declare let timeLeft: (req: Request) => number | undefined;
10
+ /** Answer by a wall-clock deadline. An answer that arrives later has no
11
+ * reader. A response that arrives in time keeps its own body lifetime. */ export declare let answerBy: (run: (signal: AbortSignal) => Promise<Response>, ms: number, expired: (elapsed: number) => Response, time?: Clock) => Promise<Response>;
12
+ /** Stop waiting and abort the forwarded request when its caller leaves or the
13
+ * deadline expires. The work decides whether headers or a body end the wait. */ export declare let before: <T>(req: Request, ms: number, timeout: Error, run: (req: Request) => Promise<T>, time?: Clock) => Promise<T>;
14
+ /** Send an unanswered read once more, aborting the lost hop. Writes and
15
+ * upgrades keep their one send, because replaying one can repeat its act. */ export declare let resending: (door: Bound, req: Request, time?: Clock, patience?: number) => Promise<Response>;
16
+ type Read = (signal: AbortSignal) => Promise<Response | null>;
17
+ type Answer = {
18
+ primary: boolean;
19
+ response: Response;
20
+ };
21
+ /** Give the primary read a head start. A pass starts the fallback immediately.
22
+ * After the grace period, either route can answer. A primary that lost may
23
+ * finish filling its cache while its response body is discarded. */ export declare let readWithFallback: (read: Read, fallback: (signal: AbortSignal) => Promise<Response>, grace: number, signal: AbortSignal, retain: (work: Promise<void>) => void, time?: Clock) => Promise<Answer>;
24
+ /** Give a read one more route to an answer after its grace period. The first
25
+ * answer wins, even if it is the original read finishing during the retry.
26
+ * Cancel the other hop and discard its response if the binding still sends it. */ export declare let hedged: (door: Bound, req: Request, grace: number, time?: Clock) => Promise<Response>;
27
+ //# sourceMappingURL=fetch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch.d.ts","sources":["../fetch.ts"],"names":[],"mappings":"AAGA,SAAS,KAAK,KAAK,wBAA6C;AAEhE,oCAAoC,GACpC,YAAY;EACV,MAAM,KAAK,SAAS,OAAO,KAAK,aAAa,YAAY,QAAQ;;AAMnE,gEAAgE,GAChE,OAAO,cAAM,WAAY,kBAAiB;AAE1C;;mEAEmE,GACnE,OAAO,YAAI,WACT,MAAM,OACN,KAAK,MAAM,EACX,OAAM,UACL,MAMD;AAIF,kFAAkF,GAClF,OAAO,YAAI,WAAY,KAAK,YAAU,MAAM,GAAG,SAAS,CAGvD;AAED;wEACwE,GACxE,OAAO,YAAI,WACT,MAAM,QAAQ,gBAAgB,QAAQ,WACtC,IAAI,MAAM,EACV,UAAU,SAAS,MAAM,KAAK,UAC9B,OAAM,UACL,QAAQ,UAwBV;AAED;8EAC8E,GAC9E,OAAO,YAAI,SAAgB,GACzB,KAAK,SACL,IAAI,MAAM,EACV,SAAS,OACT,MAAM,KAAK,YAAY,QAAQ,IAC/B,OAAM,UACL,QAAQ,GAoBV;AAED;2EAC2E,GAC3E,OAAO,YAAI,YACT,MAAM,OACN,KAAK,SACL,OAAM,OACN,sBACC,QAAQ,UAwBV;KAEI,QAAQ,QAAQ,gBAAgB,QAAQ,WAAW,IAAI;KACvD;EAAW,SAAS,OAAO;EAAE,UAAU;;AAE5C;;kEAEkE,GAClE,OAAO,YAAI,mBACT,MAAM,MACN,WAAW,QAAQ,gBAAgB,QAAQ,WAC3C,OAAO,MAAM,EACb,QAAQ,aACR,SAAS,MAAM,QAAQ,IAAI,MAAM,IAAI,EACrC,OAAM,UACL,QAAQ,QAkCV;AAED;;gFAEgF,GAChF,OAAO,YAAI,SACT,MAAM,OACN,KAAK,SACL,OAAO,MAAM,EACb,OAAM,UACL,QAAQ,UAsCV"}
package/_dist/gen.d.ts ADDED
@@ -0,0 +1,65 @@
1
+ /**
2
+ * `@yaks/workerd/gen` — the modules a Worker imports its plugins from: the
3
+ * entry's config and its plugins' `./worker` facets ({@link written}), and
4
+ * the facets its stores load ({@link loader}). A Worker resolves no module at run
5
+ * time: its bundler needs each import written out, so a build step writes
6
+ * them from the config, and the config itself as data, so the entry loads
7
+ * nothing the config was built from. The entry hands both to `compose`
8
+ * (@yaks/workerd).
9
+ *
10
+ * ```ts
11
+ * import { written } from '@yaks/workerd/gen'
12
+ * import { assert } from '@std/assert'
13
+ *
14
+ * let text = written(
15
+ * { plugins: ['@yaks/workerd', 'shop'] },
16
+ * { shop: './shop-worker.ts' },
17
+ * )
18
+ * assert(text.includes(`import * as worker1 from "./shop-worker.ts"`))
19
+ * ```
20
+ *
21
+ * @module
22
+ */ import type { FacetName } from "@yaks/host";
23
+ import { type Config } from "@yaks/host/config";
24
+ /** The module of `config`'s plugins, exported as `config`, and their
25
+ * `./worker` facets, exported as `facets` by plugin. What else the config
26
+ * says (its `stores`) is read by the code that serves it, so the entry
27
+ * evaluates none of it. A plugin `local` names is imported from
28
+ * that path, relative to the module; any other from its package's `./worker`
29
+ * subpath. */ export declare let written: (config: Config, local?: Record<string, string>) => string;
30
+ /** A package's `package.json`, as far as its exports. */ export type Manifest = {
31
+ name?: string;
32
+ exports?: string | Record<string, string>;
33
+ };
34
+ /** The facets a store imports: a graph and its effects. HTTP and long-running
35
+ * services belong to their concrete hosts, so they never enter its table. */ export declare let STORE: FacetName[];
36
+ /** Each `<plugin>/<facet>` the `configs` take of `facets`, once: a facet its
37
+ * package exports, and its plugin entry selects (`facets`, every one when
38
+ * unsaid). A plugin with no manifest is refused. */ export declare let imports: (configs: Config[], manifests: Manifest[], facets?: FacetName[]) => string[];
39
+ /** The `facet` of each plugin `config` names, as `<plugin>/<facet>`; a plugin
40
+ * exporting none is refused. */ export declare let named: (config: Config, manifests: Manifest[], facet: string) => string[];
41
+ /** A table of facets imported with the Worker rather than on first use,
42
+ * exported as `name` by plugin. */ export type Eager = {
43
+ name: string;
44
+ /** each facet's type, and the type it names with the module it is from */ type: string;
45
+ using: [name: string, from: string];
46
+ /** each facet, as `<plugin>/<facet>` */ specifiers: string[];
47
+ /** what the table holds, said above it */ says: string;
48
+ };
49
+ /**
50
+ * The module a store's facets load from (`Load`, @yaks/host): each
51
+ * `./vocab` imported with the Worker, as `vocabFacets`, since a store reads
52
+ * its words as it opens; each `eager` table the same way; every other facet
53
+ * imported on its first use. A specifier `local` names is imported from that
54
+ * path, relative to the module.
55
+ *
56
+ * ```ts
57
+ * import { loader } from '@yaks/workerd/gen'
58
+ * import { assert } from '@std/assert'
59
+ *
60
+ * let text = loader(['shop/vocab', 'shop/effects'])
61
+ * assert(text.includes(`import * as eager0_0 from "shop/vocab"`))
62
+ * assert(text.includes(`"shop/effects": () => import("shop/effects")`))
63
+ * ```
64
+ */ export declare let loader: (specifiers: string[], local?: Record<string, string>, eager?: Eager[]) => string;
65
+ //# sourceMappingURL=gen.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gen.d.ts","sources":["../gen.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;CAqBC,GACD,cAAc,SAAS,0BAA+B;AACtD,SAAS,KAAK,MAAM,iCAAsD;AAE1E;;;;;YAKY,GACZ,OAAO,YAAI,UACT,QAAQ,QACR,QAAO,OAAO,MAAM,EAAE,MAAM,MAC3B,MAAM,CAmBR;AAED,uDAAuD,GACvD,YAAY;EACV,OAAO,MAAM;EACb,UAAU,MAAM,GAAG,OAAO,MAAM,EAAE,MAAM;;AAG1C;2EAC2E,GAC3E,OAAO,YAAI,OAAO,YAAoD;AAMtE;;kDAEkD,GAClD,OAAO,YAAI,UACT,SAAS,UACT,WAAW,YACX,SAAQ,gBACP,MAAM,GAYR;AAED;8BAC8B,GAC9B,OAAO,YAAI,QACT,QAAQ,QACR,WAAW,YACX,OAAO,MAAM,KACZ,MAAM,GAML;AAEJ;iCACiC,GACjC,YAAY;EACV,MAAM,MAAM;EACZ,wEAAwE,GACxE,MAAM,MAAM;EACZ,QAAQ,MAAM,MAAM,EAAE,MAAM,MAAM;EAClC,sCAAsC,GACtC,YAAY,MAAM;EAClB,wCAAwC,GACxC,MAAM,MAAM;;AAOd;;;;;;;;;;;;;;;CAeC,GACD,OAAO,YAAI,SACT,YAAY,MAAM,IAClB,QAAO,OAAO,MAAM,EAAE,MAAM,GAC5B,QAAO,YACN,MAAM,CA+CR"}
package/_dist/mod.d.ts ADDED
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @yaks/workerd — the Cloudflare Workers adapter: a Worker made from a plugin
3
+ * config ({@link compose}), and {@link https://jsr.io/@yaks/api | @yaks/api}
4
+ * served from a Worker.
5
+ *
6
+ * `compose(config, facets)` makes a Worker module from the plugins a config
7
+ * names, each through its `./worker` facet: the routes it answers, the
8
+ * letters and queue batches it handles, the Durable Object and entrypoint
9
+ * classes it defines, loaded on their first call, and the boundary every
10
+ * invocation enters.
11
+ *
12
+ * `@yaks/api` is a plain `Request` → `Response` handler that knows nothing
13
+ * about the runtime it is served from. Three things are specific to Cloudflare,
14
+ * and they are all this package contains:
15
+ *
16
+ * - **{@link workerUpgrade}** — a socket, created the way Cloudflare creates
17
+ * one: a `WebSocketPair`, the server half accepted, the client half returned
18
+ * on a 101. It is the implementation of the `upgrade` callback `/ws` needs.
19
+ * - **{@link worker}** — the `fetch` entrypoint a Worker exports. Bindings
20
+ * arrive with the request, so the graph is built from `env` on the first
21
+ * request and cached for the isolate.
22
+ * - **{@link door}** — the identity of the caller, read off a Worker request:
23
+ * a session cookie or a bearer token, passed to the application's own
24
+ * `verify` function.
25
+ *
26
+ * A graph that lives in a Durable Object is served the same way, one hop
27
+ * further on: the Worker {@link forward}s the request to the object, and the
28
+ * object runs `api()` over its own storage.
29
+ *
30
+ * ```ts
31
+ * import type { Graph } from '@yaks/graph'
32
+ * import { door, worker } from '@yaks/workerd'
33
+ *
34
+ * declare let shopGraph: (db: unknown) => Graph
35
+ * declare let memberFor: (token: string) => { by: string } | null
36
+ *
37
+ * export default worker({
38
+ * api: (env: { DB: unknown }) => ({
39
+ * graph: shopGraph(env.DB),
40
+ * authenticate: door({ cookie: 'shop_session', verify: memberFor }),
41
+ * }),
42
+ * })
43
+ * ```
44
+ *
45
+ * @module
46
+ */ export { type Accepting, workerUpgrade } from "./upgrade.js";
47
+ export { bearer, cookies, type Door, door } from "./door.js";
48
+ export { type Context, type Env, type Options, type Worker, worker } from "./worker.js";
49
+ export { forward, type Namespace, type Stub } from "./stub.js";
50
+ export { compose, type Composed, type Deferred, type Entry, type Handling, type WorkerFacet, type WorkerRoute, type Wrap } from "./compose.js";
51
+ export { type Around, deferred, type Made } from "./deferred.js";
52
+ export { VERSION } from "./version.js";
53
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sources":["../mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6CC,GAED,SAAS,KAAK,SAAS,EAAE,aAAa,uBAAsB;AAC5D,SAAS,MAAM,EAAE,OAAO,EAAE,KAAK,IAAI,EAAE,IAAI,oBAAmB;AAC5D,SACE,KAAK,OAAO,EACZ,KAAK,GAAG,EACR,KAAK,OAAO,EACZ,KAAK,MAAM,EACX,MAAM,sBACY;AACpB,SAAS,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,IAAI,oBAAmB;AAC9D,SACE,OAAO,EACP,KAAK,QAAQ,EACb,KAAK,QAAQ,EACb,KAAK,KAAK,EACV,KAAK,QAAQ,EACb,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,IAAI,uBACU;AACrB,SAAS,KAAK,MAAM,EAAE,QAAQ,EAAE,KAAK,IAAI,wBAAuB;AAChE,SAAS,OAAO,uBAAsB"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * `@yaks/workerd/worker` — this package's own `./worker` facet: the route
3
+ * that says which deploy a Worker runs ({@link version}). Name it first in a
4
+ * Worker's config, so it answers before any other plugin's routes:
5
+ *
6
+ * ```json
7
+ * { "use": "@yaks/workerd", "with": { "name": "shop", "version": { "binding": "CF_VERSION_METADATA" } } }
8
+ * ```
9
+ *
10
+ * @module
11
+ */ import type { Route } from "@yaks/api";
12
+ import type { Options } from "@yaks/host/config";
13
+ export { VERSION } from "./version.js";
14
+ /** The routes this facet adds: the deploy's version. */ export declare let routes: (options: Options) => Route[];
15
+ //# sourceMappingURL=plugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.d.ts","sources":["../plugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;CAUC,GACD,cAAc,KAAK,yBAA8B;AACjD,cAAc,OAAO,iCAAsC;AAG3D,SAAS,OAAO,uBAAsB;AAEtC,sDAAsD,GACtD,OAAO,YAAI,SAAU,SAAS,YAAU,QAA6B"}
@@ -0,0 +1,104 @@
1
+ /**
2
+ * `@yaks/workerd/purge` — taking answers out of Cloudflare's cache by their
3
+ * tags (the `cache-tag` they were kept with).
4
+ *
5
+ * A purge reaches the entrypoint that called it and no other
6
+ * (developers.cloudflare.com/workers/cache/purge/), so the entrypoint behind
7
+ * the cache answers a `purge(tags)` RPC itself, which never goes through its
8
+ * cache (`@yaks/workerd/cache` `purging`), and every purge is sent there.
9
+ *
10
+ * Cloudflare holds a whole account to its free tier's purge rate whatever the
11
+ * plan: a bucket of 25, a token back every twelve seconds, shared by every
12
+ * Worker on the account (a refusal is error 1134). So every purge goes
13
+ * through one Durable Object, the {@link Purger}, which keeps the account's
14
+ * bucket: a purge waits for a token rather than spend a refusal, and every tag
15
+ * owed while it waits rides with it, up to 100 a purge.
16
+ *
17
+ * An acknowledged purge has not reached every cache yet. Cloudflare
18
+ * acknowledges one in about 60 ms, and it is gone from most colos within
19
+ * 200 ms of that, but in about one purge in twenty a colo answers the page a
20
+ * second or two longer: four times in about ninety deploys measured from one
21
+ * machine (T-124483), and once in 21 purges watched from a colo in each of
22
+ * nine regions, where two colos sharing an upper tier answered it 2.4 s after
23
+ * it was sent (T-150618). Cloudflare says nothing of when a purge has spread,
24
+ * so a purge that must be gone everywhere before its caller answers waits
25
+ * until {@link SPREAD} past when it was sent (T-124694). Any other purge's
26
+ * answer keeps its own short lifetime as the bound.
27
+ *
28
+ * A purge refused past the last try rejects. One that must land is its
29
+ * caller's to keep as stored work and purge again until it does (yaks.app's
30
+ * directory, workers/yak facets/pages-effects.ts).
31
+ *
32
+ * @module
33
+ */ import { Durable, type State } from "@yaks/durable-object/object";
34
+ /** What Cloudflare answers a purge with. */ export type Purged = {
35
+ success: boolean;
36
+ errors: {
37
+ code: number;
38
+ message: string;
39
+ }[];
40
+ };
41
+ /** How long past when it was sent a purge waits to have reached every colo,
42
+ * where its caller asks it to: past the longest seen (T-150618). */ export declare let SPREAD: number;
43
+ /** An entrypoint behind Cloudflare's cache, as far as purging asks of it. */ export type Purges = {
44
+ purge(tags: string[]): Promise<Purged>;
45
+ };
46
+ /** A purge refused, and why. */ export declare class Unpurged extends Error {
47
+ }
48
+ type Quota = {
49
+ take(): Promise<void>;
50
+ limited(): void;
51
+ };
52
+ /**
53
+ * Purges for one cached entrypoint: `purge(tags)` resolves once those tags
54
+ * are gone from the cache, sharing a purge with every tag owed beside them,
55
+ * or with `spread`, once they are gone from every colo's: `SPREAD` past when
56
+ * that purge was sent, its acknowledgement inside the wait. One purge is in
57
+ * flight at a time, each spends a token of the bucket, and with none left the
58
+ * next waits for one.
59
+ *
60
+ * ```ts
61
+ * import { purger } from '@yaks/workerd/purge'
62
+ * import { assertEquals } from '@std/assert'
63
+ * let sent: string[][] = []
64
+ * let started = Promise.withResolvers<void>()
65
+ * let slow = Promise.withResolvers<void>()
66
+ * let site = {
67
+ * purge: async (tags: string[]) => {
68
+ * sent.push(tags)
69
+ * started.resolve()
70
+ * await slow.promise
71
+ * return { success: true, errors: [] }
72
+ * },
73
+ * }
74
+ * let purge = purger(site)
75
+ * let first = purge(['app:a'])
76
+ * await started.promise
77
+ * let then = [purge(['app:b']), purge(['app:c', 'app:b'])]
78
+ * slow.resolve()
79
+ * await Promise.all([first, ...then])
80
+ * assertEquals(sent, [['app:a'], ['app:b', 'app:c']])
81
+ * ```
82
+ */ export declare let purger: (site: Purges, wait?: (ms: number) => Promise<void>, now?: () => number, account?: Quota) => (tags: string[], spread?: boolean) => Promise<void>;
83
+ /**
84
+ * The one Durable Object every purge goes through: it holds no graph and no
85
+ * storage, only the account's bucket in memory, which a new instance starts
86
+ * full. It purges through `site`, the binding of the entrypoint whose cache
87
+ * it empties. `purge` is its entry, held open while it waits for a token. A
88
+ * purge refused is its caller's to hear and report; the object says it on
89
+ * the console.
90
+ */ export declare class Purger extends Durable {
91
+ constructor(ctx: State, site?: Partial<Purges>, extra?: Partial<Purges>);
92
+ purge(tags: string[], spread?: boolean): Promise<void>;
93
+ }
94
+ /** The {@link Purger}'s binding. */ export type Purgers = {
95
+ idFromName(name: string): unknown;
96
+ get(id: unknown): {
97
+ purge(tags: string[], spread?: boolean): Promise<void>;
98
+ };
99
+ };
100
+ /** Purge `tags` from the cache through the {@link Purger} bound as
101
+ * `purgers`, and with `spread`, wait until they are gone from every colo's.
102
+ * Where there is no cache (a test run, `wrangler dev`) none is bound, so
103
+ * there is nothing to do. */ export declare let purge: (purgers: Purgers | undefined, tags: string[], spread?: boolean) => Promise<void>;
104
+ //# sourceMappingURL=purge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"purge.d.ts","sources":["../purge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgCC,GACD,SAAS,OAAO,EAAE,KAAK,KAAK,2CAAgD;AAE5E,0CAA0C,GAC1C,YAAY;EACV,SAAS,OAAO;EAChB;IAAU,MAAM,MAAM;IAAE,SAAS,MAAM;;;AAGzC;kEACkE,GAClE,OAAO,YAAI,eAAc;AAEzB,2EAA2E,GAC3E,YAAY;EAAW,MAAM,MAAM,MAAM,KAAK,QAAQ;;AAiBtD,8BAA8B,GAC9B,OAAO,cAAM,iBAAiB;AAAO;KA2BhC;EAAU,QAAQ,QAAQ,IAAI;EAAG,WAAW,IAAI;;AAqBrD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA8BC,GACD,OAAO,YAAI,SACT,MAAM,QACN,QAAO,IAAI,MAAM,KAAK,QAAQ,IAAI,GAClC,YAAW,MAAM,EACjB,UAAS,WACP,MAAM,MAAM,IAAI,SAAS,OAAO,KAAK,QAAQ,IAAI,EA+CpD;AAED;;;;;;;CAOC,GACD,OAAO,cAAM,eAAe;EAE1B,YAAY,KAAK,KAAK,EAAE,OAAO,QAAQ,OAAO,EAAE,QAAQ,QAAQ,OAAO;EAevE,MAAM,MAAM,MAAM,EAAE,EAAE,gBAAc,GAAG,QAAQ,IAAI;AAGrD;AAEA,kCAAkC,GAClC,YAAY;EACV,WAAW,MAAM,MAAM,GAAG,OAAO;EACjC,IAAI,IAAI,OAAO;IAAK,MAAM,MAAM,MAAM,IAAI,SAAS,OAAO,GAAG,QAAQ,IAAI;;;AAG3E;;;2BAG2B,GAC3B,OAAO,YAAI,QACT,SAAS,UAAU,SAAS,EAC5B,MAAM,MAAM,IACZ,qBACC,QAAQ,IAAI,EAGd"}
@@ -0,0 +1,65 @@
1
+ /**
2
+ * `@yaks/workerd/rate` — per-source ceilings on the doors anybody can knock
3
+ * on without signing in. Each door has its own Workers Rate Limiting binding
4
+ * (wrangler.toml `[[ratelimits]]`, where the numbers live), keyed by the
5
+ * address Cloudflare saw the request come from.
6
+ *
7
+ * A counter a stranger can spend for everybody is the thing never to build:
8
+ * one loop would silence every other stranger.
9
+ *
10
+ * The binding is a fixed window counted per Cloudflare location, so it is a
11
+ * brake, not an exact ledger. That is what these doors want: a person clicks
12
+ * a few times a minute and a loop thousands.
13
+ *
14
+ * A request with no `cf-connecting-ip` did not come in from the internet (a
15
+ * Worker's own subrequest, an in-process test) and is not limited; Cloudflare
16
+ * sets the header on every request that reaches the edge and a client cannot
17
+ * choose it. A limiter that throws is not a refusal either: a broken counter
18
+ * never locks a person out, and the caller hears what broke.
19
+ *
20
+ * ```ts
21
+ * import { source, within } from '@yaks/workerd/rate'
22
+ * import { assertEquals } from '@std/assert'
23
+ * let req = new Request('https://x.test/', {
24
+ * headers: { 'cf-connecting-ip': '2001:db8::1' },
25
+ * })
26
+ * let full = { limit: () => Promise.resolve({ success: false }) }
27
+ * assertEquals(await within(full, source(req), () => {}), false)
28
+ * ```
29
+ *
30
+ * @module
31
+ */ /** The binding's shape (@cloudflare/workers-types `RateLimit`), declared
32
+ * here so no Cloudflare type name leaks into what is given one. */ export type Limiter = {
33
+ limit(options: {
34
+ key: string;
35
+ }): Promise<{
36
+ success: boolean;
37
+ }>;
38
+ };
39
+ /** The address a request came from, or '' when it came from inside. */ export declare let source: (req: Request) => string;
40
+ /** The network an address belongs to, which is what a door counts: an IPv4
41
+ * address as it is, an IPv6 address its /48, the first three groups written
42
+ * out. A subscriber is handed a whole /64, /56 or /48 and can walk every
43
+ * address in it, so counting a smaller prefix counts one source many times.
44
+ *
45
+ * ```ts
46
+ * import { network } from '@yaks/workerd/rate'
47
+ * import { assertEquals } from '@std/assert'
48
+ * assertEquals(network('203.0.113.9'), '203.0.113.9')
49
+ * assertEquals(network('2001:DB8:0:ff::7'), '2001:db8:0')
50
+ * assertEquals(network('::1'), '0:0:0')
51
+ * ```
52
+ */ export declare let network: (ip: string) => string;
53
+ /** The network a request came from (`network`), the key a door's limiter
54
+ * counts, or '' when it came from inside. */ export declare let place: (req: Request) => string;
55
+ /** Who a visitor at a door is: their person, else the address they came
56
+ * from, else (a request from inside) one stranger. */ export declare let visitorKey: (who: {
57
+ person: string | null;
58
+ }, req: Request) => string;
59
+ /** May this source go on through this door? A door with no limiter, or a
60
+ * request from inside (no `key`), may. A limiter that throws lets it through,
61
+ * and `failed` hears what it threw. */ export declare let within: (lim: Limiter | undefined, key: string, failed: (error: unknown) => void) => Promise<boolean>;
62
+ /** Every binding's period is a minute, so a minute is when to come back. */ export declare let RETRY: number;
63
+ /** The refusal a door answers when a source is over its rate, for a door that
64
+ * answers HTTP rather than a tool result. */ export declare let tooMany: (said?: string) => Response;
65
+ //# sourceMappingURL=rate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rate.d.ts","sources":["../rate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA8BC,GAED;iEACiE,GACjE,YAAY;EACV,MAAM;IAAW,KAAK,MAAM;MAAK;IAAU,SAAS,OAAO;;;AAG7D,qEAAqE,GACrE,OAAO,YAAI,SAAU,KAAK,YAAU,MAAM,CACC;AAE3C;;;;;;;;;;;;CAYC,GACD,OAAO,YAAI,UAAW,IAAI,MAAM,KAAG,MAAM,CAWxC;AAED;2CAC2C,GAC3C,OAAO,YAAI,QAAS,KAAK,YAAU,MAAM,CAAwB;AAEjE;oDACoD,GACpD,OAAO,YAAI,aACT;EAAO,QAAQ,MAAM,GAAG,IAAI;GAC5B,KAAK,YACJ,MAAM,CAA6C;AAEtD;;qCAEqC,GACrC,OAAO,YAAI,SACT,KAAK,UAAU,SAAS,EACxB,KAAK,MAAM,EACX,SAAS,OAAO,OAAO,KAAK,IAAI,KAC/B,QAAQ,OAAO,EAQjB;AAED,0EAA0E,GAC1E,OAAO,YAAI,cAAU;AAErB;2CAC2C,GAC3C,OAAO,YAAI,UAAW,kBAA8C,SAQhE"}
@@ -0,0 +1,20 @@
1
+ /** A Durable Object stub: one object, reachable by fetch. */ export type Stub = {
2
+ /** send a request to the object and return its response */ fetch(request: Request): Promise<Response>;
3
+ };
4
+ /** A Durable Object namespace binding — the part of it a router uses. */ export type Namespace = {
5
+ /** the id of the object with this name, derived the same way every time */ idFromName(name: string): unknown;
6
+ /** the stub for an id */ get(id: unknown): Stub;
7
+ };
8
+ /**
9
+ * Forward a request to the Durable Object holding the named graph. The name is
10
+ * the graph's identity — a shop's subdomain, a customer id, the first path
11
+ * segment — and the same name always reaches the same object.
12
+ *
13
+ * ```ts
14
+ * export default {
15
+ * fetch: (request: Request, env: { GRAPHS: Namespace }) =>
16
+ * forward(env.GRAPHS, new URL(request.url).hostname.split('.')[0], request),
17
+ * }
18
+ * ```
19
+ */ export declare let forward: (ns: Namespace, name: string, request: Request) => Promise<Response>;
20
+ //# sourceMappingURL=stub.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stub.d.ts","sources":["../stub.ts"],"names":[],"mappings":"AAWA,2DAA2D,GAC3D,YAAY;EACV,yDAAyD,GACzD,MAAM,SAAS,UAAU,QAAQ;;AAGnC,uEAAuE,GACvE,YAAY;EACV,yEAAyE,GACzE,WAAW,MAAM,MAAM,GAAG,OAAO;EACjC,uBAAuB,GACvB,IAAI,IAAI,OAAO,GAAG;;AAGpB;;;;;;;;;;;CAWC,GACD,OAAO,YAAI,UACT,IAAI,WACJ,MAAM,MAAM,EACZ,SAAS,YACR,QAAQ,UAAuD"}
@@ -0,0 +1,25 @@
1
+ import type { Socket, Upgrade } from "@yaks/api";
2
+ /** A Workers socket: the half a Worker keeps has to be accepted before it
3
+ * carries frames. */ type Frame = string | Uint8Array;
4
+ export type Accepting = Socket<Frame> & {
5
+ /** start handling frames on this half of the pair */ accept: () => void;
6
+ };
7
+ /**
8
+ * Cloudflare's WebSocket upgrade, as an
9
+ * {@link https://jsr.io/@yaks/api/doc/~/Upgrade | Upgrade}: create a
10
+ * `WebSocketPair`, accept the server half, and answer 101 with the client
11
+ * half. Pass it to `api()` on a Worker, or let {@link worker} pass it for you.
12
+ *
13
+ * ```ts ignore
14
+ * import { api } from '@yaks/api'
15
+ * import { workerUpgrade } from '@yaks/workerd'
16
+ *
17
+ * let handler = api({ graph, upgrade: workerUpgrade })
18
+ * ```
19
+ *
20
+ * It throws outside the Workers runtime, where `WebSocketPair` does not exist.
21
+ */ export declare let workerUpgrade: Upgrade<Frame>;
22
+ /** The answer again under other headers, carrying its WebSocket if present. */ export declare let headed: (res: Response, headers: Headers) => Response;
23
+ /** Forward the socket so the next HTTP hop can generate its own handshake.
24
+ * A container's outbound proxy appends its headers to the ones it gets. */ export declare let rehandshake: (response: Response) => Response;
25
+ //# sourceMappingURL=upgrade.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upgrade.d.ts","sources":["../upgrade.ts"],"names":[],"mappings":"AAUA,cAAc,MAAM,EAAE,OAAO,yBAA8B;AAE3D;mBACmB,QACd,QAAQ,MAAM,GAAG;AAEtB,YAAY,YAAY,OAAO;EAC7B,mDAAmD,GACnD,cAAc,IAAI;;AAwBpB;;;;;;;;;;;;;;CAcC,GACD,OAAO,YAAI,eAAe,QAAQ,OAUjC;AAED,6EAA6E,GAC7E,OAAO,YAAI,SAAU,KAAK,UAAU,SAAS,YAAU,SActD;AAED;yEACyE,GACzE,OAAO,YAAI,cAAe,UAAU,aAAW,SAO9C"}
@@ -0,0 +1,2 @@
1
+ /** The path a Worker says its deploy at. */ export declare let VERSION: string;
2
+ //# sourceMappingURL=version.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"version.d.ts","sources":["../version.ts"],"names":[],"mappings":"AAOA,0CAA0C,GAC1C,OAAO,YAAI,gBAAyB"}
@@ -0,0 +1,40 @@
1
+ import { type Options as Api } from "@yaks/api";
2
+ /** A Worker's bindings, as this package reads them: whatever `wrangler.toml`
3
+ * declared, under the names it gave them. */ export type Env = Record<string, unknown>;
4
+ /** The Workers execution context — the third argument to `fetch`, kept opaque
5
+ * because this package only passes it along. */ export type Context = {
6
+ /** keep the Worker alive until a promise settles */ waitUntil: (promise: Promise<unknown>) => void;
7
+ };
8
+ /** A Cloudflare Worker's default export: its `fetch` entrypoint. */ export type Worker<E extends Env = Env> = {
9
+ /** answer one request, given this Worker's bindings */ fetch: (request: Request, env: E, ctx?: Context) => Promise<Response>;
10
+ };
11
+ /** How a Worker is built: what the api is, for a given set of bindings. */ export type Options<E extends Env = Env> = {
12
+ /** the api this Worker serves — its graph, and the authentication callback
13
+ * that identifies the caller — built from the bindings the request arrived
14
+ * with */ api: (env: E) => Api | Promise<Api>;
15
+ };
16
+ /**
17
+ * The Worker export for a graph: build the api from the Worker's bindings once
18
+ * per isolate, connect Cloudflare's socket upgrade to it, and serve every
19
+ * request from it.
20
+ *
21
+ * ```ts
22
+ * import type { Authenticate } from '@yaks/api'
23
+ * import type { Graph } from '@yaks/graph'
24
+ * import { worker } from '@yaks/workerd'
25
+ *
26
+ * declare let shopGraph: (db: unknown) => Graph
27
+ * declare let authenticate: Authenticate
28
+ *
29
+ * export default worker({
30
+ * api: (env) => ({ graph: shopGraph(env.DB), authenticate }),
31
+ * })
32
+ * ```
33
+ *
34
+ * The routes are [@yaks/api](https://jsr.io/@yaks/api)'s: `POST /apply`,
35
+ * `GET|POST /query`, and `/ws`. An `upgrade` given in the options is used as
36
+ * is; when none is given, {@link workerUpgrade} is used. A graph that lives in
37
+ * a Durable Object is served differently — the Worker {@link forward}s to the
38
+ * object, and the object is what calls this.
39
+ */ export declare let worker: <E extends Env = Env>(opts: Options<E>) => Worker<E>;
40
+ //# sourceMappingURL=worker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker.d.ts","sources":["../worker.ts"],"names":[],"mappings":"AAWA,SAA4B,KAAK,WAAW,GAAG,yBAAsC;AAGrF;2CAC2C,GAC3C,YAAY,MAAM,OAAO,MAAM,EAAE,OAAO;AAExC;8CAC8C,GAC9C,YAAY;EACV,kDAAkD,GAClD,YAAY,SAAS,QAAQ,OAAO,MAAM,IAAI;;AAGhD,kEAAkE,GAClE,YAAY,OAAO,UAAU,MAAM;EACjC,qDAAqD,GACrD,QAAQ,SAAS,SAAS,KAAK,GAAG,MAAM,YAAY,QAAQ;;AAG9D,yEAAyE,GACzE,YAAY,QAAQ,UAAU,MAAM;EAClC;;UAEQ,GACR,MAAM,KAAK,MAAM,MAAM,QAAQ;;AAGjC;;;;;;;;;;;;;;;;;;;;;;;CAuBC,GACD,OAAO,YAAI,SAAU,UAAU,MAAM,KAAK,MAAM,QAAQ,OAAK,OAAO,GAuBnE"}