@yaks/tunnel 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 (50) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +319 -2
  3. package/_dist/cloudflare.d.ts +38 -0
  4. package/_dist/cloudflare.d.ts.map +1 -0
  5. package/_dist/effects.d.ts +37 -0
  6. package/_dist/effects.d.ts.map +1 -0
  7. package/_dist/filter.d.ts +68 -0
  8. package/_dist/filter.d.ts.map +1 -0
  9. package/_dist/gateway.d.ts +14 -0
  10. package/_dist/gateway.d.ts.map +1 -0
  11. package/_dist/hosted.d.ts +101 -0
  12. package/_dist/hosted.d.ts.map +1 -0
  13. package/_dist/mod.d.ts +5 -0
  14. package/_dist/mod.d.ts.map +1 -0
  15. package/_dist/routes.d.ts +31 -0
  16. package/_dist/routes.d.ts.map +1 -0
  17. package/_dist/service.d.ts +24 -0
  18. package/_dist/service.d.ts.map +1 -0
  19. package/_dist/vocab.d.ts +6 -0
  20. package/_dist/vocab.d.ts.map +1 -0
  21. package/cloudflare.js +170 -0
  22. package/cloudflare.js.map +1 -0
  23. package/cloudflare.ts +233 -0
  24. package/deno.json +1 -0
  25. package/effects.js +103 -0
  26. package/effects.js.map +1 -0
  27. package/effects.ts +143 -0
  28. package/filter.js +95 -0
  29. package/filter.js.map +1 -0
  30. package/filter.ts +128 -0
  31. package/gateway.js +37 -0
  32. package/gateway.js.map +1 -0
  33. package/gateway.ts +46 -0
  34. package/hosted.js +345 -0
  35. package/hosted.js.map +1 -0
  36. package/hosted.ts +496 -0
  37. package/mod.js +15 -0
  38. package/mod.js.map +1 -0
  39. package/mod.ts +35 -0
  40. package/package.json +45 -4
  41. package/routes.js +94 -0
  42. package/routes.js.map +1 -0
  43. package/routes.ts +144 -0
  44. package/service.js +119 -0
  45. package/service.js.map +1 -0
  46. package/service.ts +164 -0
  47. package/vocab.js +16 -0
  48. package/vocab.js.map +1 -0
  49. package/vocab.json +62 -0
  50. package/vocab.ts +17 -0
@@ -0,0 +1,68 @@
1
+ /** What a machine's owner opens to requests through the tunnel: the paths
2
+ * answered, the tools offered where a path runs them, and the persona written
3
+ * as. */ export type Opened = {
4
+ /** the paths answered, as URLPattern pathnames; none opens nothing */ routes?: string[];
5
+ /** the tools offered at a path that runs them, by name; none offers
6
+ * nothing */ tools?: string[];
7
+ /** the persona written as, by eid, number or name */ persona?: string;
8
+ /** the platform's id for the person the machine's own config names as its
9
+ * `person`: a request the platform says is that person's own act
10
+ * (./gateway.ts `BY`) writes as them. Without one no request does */ owner?: string;
11
+ };
12
+ /** What a machine's owner opens to one kind of caller on top of what every
13
+ * request through the tunnel gets, by the platform's word for the caller. */ export type Callers = Record<string, Opened>;
14
+ /** What the caller this request is from was opened, on top of what every
15
+ * request through the tunnel is: the entry for the platform's word on it, or
16
+ * nothing. */ export declare let named: (callers: Callers | undefined, req: Request) => Opened;
17
+ /** Whether the gateway marked this request: it came through the tunnel. */ export declare let marked: (req: Request) => boolean;
18
+ /** Whether the tunnel answers at `path`: `routes` are URLPattern pathnames
19
+ * (`/mail/inbound`, `/hooks/*`), and none opens nothing.
20
+ *
21
+ * ```ts
22
+ * import { assert } from '@std/assert'
23
+ * assert(opens(['/mail/inbound', '/hooks/*'], '/hooks/github'))
24
+ * assert(!opens(['/mail/inbound'], '/apply'))
25
+ * ```
26
+ */ export declare let opens: (routes: string[] | undefined, path: string) => boolean;
27
+ /** A marked request at a path the machine did not open. Its name is the
28
+ * policy refusal's, so a server answers it 403. */ export declare class Closed extends Error {
29
+ name: string;
30
+ }
31
+ /** The machine's word on the tunnel: a marked request passes only at a path
32
+ * opened to every request (`routes`) or to the caller the platform says it is
33
+ * from (`callers`), and an unmarked one always does.
34
+ *
35
+ * ```ts
36
+ * import { assertThrows } from '@std/assert'
37
+ * import { CALLER, HEADER } from './gateway.ts'
38
+ * let only = filter(['/mail/inbound'], { operator: { routes: ['/mcp'] } })
39
+ * let through = (path: string, caller = 'app') =>
40
+ * new Request(`http://box${path}`, {
41
+ * headers: { [HEADER]: '1', [CALLER]: caller },
42
+ * })
43
+ * only(new Request('http://box/apply'))
44
+ * only(through('/mail/inbound'))
45
+ * only(through('/mcp', 'operator'))
46
+ * assertThrows(() => only(through('/mcp')))
47
+ * assertThrows(() => only(through('/apply', 'operator')))
48
+ * ```
49
+ */ export declare let filter: (routes?: string[], callers?: Callers) => any;
50
+ /** The tools a request is offered, by name: for a marked request the ones the
51
+ * machine's owner named for every request (`tools`) and for the caller the
52
+ * platform says it is from (`callers`), and none names nothing; an unmarked
53
+ * request is not narrowed (`undefined`).
54
+ *
55
+ * ```ts
56
+ * import { assertEquals } from '@std/assert'
57
+ * import { CALLER, HEADER } from './gateway.ts'
58
+ * let tools = offered(['task_list'], { operator: { tools: ['task_new'] } })
59
+ * let through = (caller: string) =>
60
+ * new Request('http://box/mcp', {
61
+ * headers: { [HEADER]: '1', [CALLER]: caller },
62
+ * })
63
+ * assertEquals(tools(new Request('http://box/mcp')), undefined)
64
+ * assertEquals(tools(through('app')), ['task_list'])
65
+ * assertEquals(tools(through('operator')), ['task_list', 'task_new'])
66
+ * ```
67
+ */ export declare let offered: (tools?: string[], callers?: Callers) => any;
68
+ //# sourceMappingURL=filter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filter.d.ts","sources":["../filter.ts"],"names":[],"mappings":"AAeA;;OAEO,GACP,YAAY;EACV,oEAAoE,GACpE,SAAS,MAAM;EACf;aACW,GACX,QAAQ,MAAM;EACd,mDAAmD,GACnD,UAAU,MAAM;EAChB;;qEAEmE,GACnE,QAAQ,MAAM;;AAGhB;2EAC2E,GAC3E,YAAY,UAAU,OAAO,MAAM,EAAE;AAErC;;YAEY,GACZ,OAAO,YAAI,QAAS,SAAS,qBAAc,KAAK,YAAU,OAGzD;AAED,yEAAyE,GACzE,OAAO,YAAI,SAAU,KAAK,YAAU,OAAO,CAA2B;AAatE;;;;;;;;CAQC,GACD,OAAO,YAAI,QAAS,QAAQ,MAAM,gBAAS,MAAM,MAAM,KAAG,OAAO,CACM;AAEvE;iDACiD,GACjD,OAAO,cAAM,eAAe;EACjB,aAAe;AAC1B;AAEA;;;;;;;;;;;;;;;;;;CAkBC,GACD,OAAO,YAAI,SACR,SAAQ,MAAM,IAAS,UAAU,gBAQjC;AAEH;;;;;;;;;;;;;;;;;CAiBC,GACD,OAAO,YAAI,UACR,QAAO,MAAM,IAAS,UAAU,gBAEyC"}
@@ -0,0 +1,14 @@
1
+ /** The header the gateway marks every request with. */ export declare let HEADER: string;
2
+ /** The header the platform says who is asking in: its own word for the kind of
3
+ * caller, which only it sets. */ export declare let CALLER: string;
4
+ /** The header the platform names a person in, by its own id for them: the
5
+ * request is that person's own act (their words, their answer), where one
6
+ * without it is their assistant's. Only the platform says it, as it does
7
+ * `x-yak-caller`. The machine's end decides whose act it then is
8
+ * (./routes.ts). */ export declare let BY: string;
9
+ /** The header the platform names the person a request's conversation is
10
+ * held with, by its own id for them: whose assistant is asking, where
11
+ * `x-via` names the conversation. Only the platform says it. */ export declare let WITH: string;
12
+ /** The gateway's one module: the request as the platform passed it on, marked
13
+ * as the tunnel's, to the machine behind the VPC Service (`BOX`). */ export declare let GATEWAY: string;
14
+ //# sourceMappingURL=gateway.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gateway.d.ts","sources":["../gateway.ts"],"names":[],"mappings":"AAiBA,qDAAqD,GACrD,OAAO,YAAI,eAAuB;AAElC;+BAC+B,GAC/B,OAAO,YAAI,eAAuB;AAElC;;;;kBAIkB,GAClB,OAAO,YAAI,WAAe;AAE1B;;8DAE8D,GAC9D,OAAO,YAAI,aAAmB;AAE9B;mEACmE,GACnE,OAAO,YAAI,gBAOV"}
@@ -0,0 +1,101 @@
1
+ import { type Api, type Gateways } from "./cloudflare.js";
2
+ import type { Told } from "./effects.js";
3
+ /** A tunnel as its host records it on the entity it reaches a machine from
4
+ * (the `tunnel` component): its id, the VPC Service its gateway is bound to,
5
+ * and whether it was made elsewhere and only recorded here. */ export type Tunnel = {
6
+ id: string;
7
+ service: string;
8
+ adopted: boolean;
9
+ };
10
+ /** A space as this module reads it: its slug, and its tunnel or none. */ export type Held = {
11
+ slug: string;
12
+ tunnel: Tunnel | null;
13
+ tunnelTrashed?: string | null;
14
+ };
15
+ /** Who is asking a machine, as the platform tells it (`CALLER`): an `app`'s
16
+ * own worker; the owner's `operator`, in a conversation or a run of its own;
17
+ * or the owner's own `connector`, the assistant they signed in to the
18
+ * platform from. A machine's config opens paths, tools and a persona to each
19
+ * by this word. */ export type Caller = 'app' | 'operator' | 'connector';
20
+ /** Who asks a machine for its owner: their connector unless it says their
21
+ * Operator, which also names its space and the conversation it asks in
22
+ * (`via`), where there is one. A request that carries the owner's own words
23
+ * as they gave them, and nobody's reading of them, is their `own` act: the
24
+ * machine is told who they are (`BY`), and writes it as theirs. Only the host
25
+ * says so, of words it read itself. */ export type Calling = {
26
+ via?: () => Promise<string | undefined> | string | undefined;
27
+ } & {
28
+ own?: boolean;
29
+ } & ({
30
+ caller: 'connector';
31
+ } | {
32
+ caller: 'operator';
33
+ space: string;
34
+ });
35
+ /** One call through a space's gateway, as its host makes it: the machine's
36
+ * answer, null where the tunnel has no gateway, and a throw where the tunnel
37
+ * or the machine did not answer. */ export type Relay = (out: Request) => Promise<Response | null>;
38
+ /** The name of the gateway in front of the tunnel of the space `eid`, in the
39
+ * namespace the host uploads gateways to (./cloudflare.ts `gateways`). */ export declare let gatewayOf: (eid: string) => string;
40
+ /** Where an app's worker asks for its space's machine, under the app's own
41
+ * doors: `/tunneled/mail/inbound` is `/mail/inbound` on the machine. */ export declare let TUNNELED: string;
42
+ /** A refusal as this module answers one: its code, and the sentence it says. */ export type Ending = {
43
+ status: number;
44
+ code: string;
45
+ message: string;
46
+ };
47
+ /** A request as it goes on to a machine: `req`'s method, body and headers at
48
+ * `path` there, with the platform's own words taken off and its word on who
49
+ * is asking put on: the conversation it was made in (`via`), whose that is
50
+ * (`with`, only beside `via`) and whose own act it is (`by`). */ export declare let onward: (path: string, req: Request, caller: Caller, said?: {
51
+ via?: string;
52
+ with?: string;
53
+ by?: string;
54
+ }) => Request;
55
+ /** An app as this module reads it: its slug, and whether it is an installed
56
+ * copy of somebody else's. */ export type Reaching = {
57
+ slug: string;
58
+ installed?: unknown;
59
+ };
60
+ /** Why `app` may not reach the machine its space's tunnel goes to, or null
61
+ * where it may: only an app built in the space reaches it, since an installed
62
+ * copy's code was written elsewhere and a tunnel reaches into the owner's own
63
+ * machine, and only while the space has a tunnel. */ export declare let barred: (space: Held, app: Reaching) => Ending | null;
64
+ /** An app's own worker reaching the machine its space's tunnel goes to, at
65
+ * `path` under its doors, or null for a path not at `TUNNELED`. `itself` is
66
+ * whether the host knows the request for the app's own worker: nothing a
67
+ * visitor holds opens it. The request goes on as an `app`'s, and the machine
68
+ * decides what it answers. A throw from the relay is told to `report`. */ export declare let reach: (req: Request, path: string, at: {
69
+ space: Held;
70
+ app: Reaching;
71
+ itself: boolean;
72
+ }, relay: Relay, report?: (e: unknown) => void) => Promise<Response | null>;
73
+ /** Whether `asking` reaches the machine of the space `at` for a person seated
74
+ * there as `role`: its owner alone, and their Operator only from that space,
75
+ * since another space's members shape its instructions and memory. */ export declare let owners: (role: string | null | undefined, at: string, asking: Calling) => boolean;
76
+ /** The machine `space`'s tunnel goes to, as a `fetch` for its owner `person`,
77
+ * asked as `asking` says (whom the host has found `owners`). Only the path of
78
+ * what it is asked for is meant. Every ending is an answer: a space with no
79
+ * tunnel, a tunnel with no gateway and a machine that is off are the machine
80
+ * being unavailable to its owner. */ export declare let machine: (space: Held, person: string, asking: Calling, relay: Relay) => typeof fetch;
81
+ /** A space as the door finds it for whoever asks: its tunnel, and whether
82
+ * they own it or own the platform. */ export type Kept = Held & {
83
+ eid: string;
84
+ owner: boolean;
85
+ platform: boolean;
86
+ };
87
+ /** What the door reads and writes through its host. */ export type Keeping = {
88
+ /** the account tunnels are made in, or null until the host has its token */ account: Api | null;
89
+ /** the namespace gateways are uploaded to, or null until the host can */ gateways: Gateways | null;
90
+ /** who is asking, by the host's id for them, or null for nobody */ who: () => Promise<string | null>;
91
+ /** the space `slug`, as `person` stands in it, or null for none */ find: (slug: string, person: string | null) => Promise<Kept | null>;
92
+ /** the tunnel recorded on the space `eid`, or none */ write: (eid: string, row: Tunnel | null) => Promise<unknown>;
93
+ /** keep the tunnel and its shared names in the trash, or restore it */ trash: (eid: string, at: string | null) => Promise<unknown>;
94
+ /** ask its person to purge a trashed tunnel through their confirmation page */ purge: (space: Kept) => Promise<string>;
95
+ /** what the machine tells its owner `person`, taken in by the host: what
96
+ * it answers, or null where none of their spaces with a tunnel holds the
97
+ * conversation */ tell: (person: string, told: Told) => Promise<object | null>;
98
+ /** An authenticated machine reports that its tunnel registered. */ ready?: (person: string, tunnel: string) => Promise<void>;
99
+ };
100
+ /** The platform's door onto its spaces' tunnels, as the header says. */ export declare let door: (req: Request, host: Keeping) => Promise<Response>;
101
+ //# sourceMappingURL=hosted.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hosted.d.ts","sources":["../hosted.ts"],"names":[],"mappings":"AA2CA,SACE,KAAK,GAAG,EAGR,KAAK,QAAQ,0BAES;AACxB,cAAc,IAAI,uBAAsB;AAGxC;;6DAE6D,GAC7D,YAAY;EAAW,IAAI,MAAM;EAAE,SAAS,MAAM;EAAE,SAAS,OAAO;;AAEpE,uEAAuE,GACvE,YAAY;EACV,MAAM,MAAM;EACZ,QAAQ,SAAS,IAAI;EACrB,gBAAgB,MAAM,GAAG,IAAI;;AAG/B;;;;iBAIiB,GACjB,YAAY,SAAS,QAAQ,aAAa;AAE1C;;;;;qCAKqC,GACrC,YAAY;EACN,YAAY,QAAQ,MAAM,GAAG,SAAS,IAAI,MAAM,GAAG,SAAS;;EAC5D,MAAM,OAAO;;EACZ,QAAQ;;EAAkB,QAAQ;EAAY,OAAO,MAAM;;AAElE;;kCAEkC,GAClC,YAAY,SAAS,KAAK,YAAY,QAAQ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IAAI,KAAK,QAAQ,OAAO,IAAI;EAClE,oDAAoD,GACpD,QAAQ,KAAK,MAAM,EAAE,KAAK,SAAS,IAAI,KAAK,QAAQ,OAAO;EAC3D,qEAAqE,GACrE,QAAQ,KAAK,MAAM,EAAE,IAAI,MAAM,GAAG,IAAI,KAAK,QAAQ,OAAO;EAC1D,6EAA6E,GAC7E,QAAQ,OAAO,SAAS,QAAQ,MAAM;EACtC;;kBAEgB,GAChB,OAAO,QAAQ,MAAM,EAAE,MAAM,SAAS,QAAQ,MAAM,GAAG,IAAI;EAC3D,iEAAiE,GACjE,SAAS,QAAQ,MAAM,EAAE,QAAQ,MAAM,KAAK,QAAQ,IAAI;;AAY1D,sEAAsE,GACtE,OAAO,YAAI,OAAc,KAAK,SAAS,MAAM,YAAU,QAAQ,UAyC9D"}
package/_dist/mod.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export { type Api, connect, disconnect, type Gateways, gateways, type Made, type Pair, services, type Target, tunnels } from "./cloudflare.js";
2
+ export { type Callers, Closed, filter, marked, named, offered, type Opened, opens } from "./filter.js";
3
+ export { BY, CALLER, GATEWAY, HEADER, WITH } from "./gateway.js";
4
+ export { docs, tunnelDoc } from "./vocab.js";
5
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sources":["../mod.ts"],"names":[],"mappings":"AAWA,SACE,KAAK,GAAG,EACR,OAAO,EACP,UAAU,EACV,KAAK,QAAQ,EACb,QAAQ,EACR,KAAK,IAAI,EACT,KAAK,IAAI,EACT,QAAQ,EACR,KAAK,MAAM,EACX,OAAO,0BACe;AACxB,SACE,KAAK,OAAO,EACZ,MAAM,EACN,MAAM,EACN,MAAM,EACN,KAAK,EACL,OAAO,EACP,KAAK,MAAM,EACX,KAAK,sBACa;AACpB,SAAS,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,uBAAsB;AAChE,SAAS,IAAI,EAAE,SAAS,qBAAoB"}
@@ -0,0 +1,31 @@
1
+ import type { Arrived, Offers, Told } from "@yaks/api";
2
+ import type { Eid, Graph } from "@yaks/graph";
3
+ import { type Callers, type Opened } from "./filter.js";
4
+ /** What a config file can set for this facet: what every request through the
5
+ * tunnel is opened (without a persona it writes as nobody, through its
6
+ * session), and what each kind of caller is on top of that. */ export type Options = Opened & {
7
+ /** what one kind of caller is opened as well, by the platform's word for
8
+ * it: its paths and tools beside everyone's, and its own persona */ callers?: Callers;
9
+ };
10
+ /** What this facet reads off the host: the graph a caller's session is kept
11
+ * in, the person the machine's config names, and its own process. */ export type Hosting = {
12
+ graph: Pick<Graph, 'address' | 'get' | 'apply' | 'storage' | 'vocab'>;
13
+ config?: {
14
+ person?: string;
15
+ };
16
+ me?: Eid;
17
+ };
18
+ /** The session a tunnel's callers stand in is named within this door. */ export declare let DOOR: string;
19
+ /** The tunnel's filter, over the paths the config opens. */ export declare let filter: (_host: unknown, options?: Options) => (req: Request) => void;
20
+ /** Who a request through the tunnel is from: the persona the config names for
21
+ * its caller, or else for every request, through the session standing for the
22
+ * conversation its `x-via` names. One the platform says is the own act of the
23
+ * tunnel's `owner` is from the config's `person`, through this process. */ export declare let arrived: (host: Hosting, options?: Options) => Arrived;
24
+ /** The tools a request through the tunnel is offered: the config's, for every
25
+ * request and for its caller. */ export declare let offers: (_host: unknown, options?: Options) => Offers;
26
+ /** What a request through the tunnel is told before a door's own
27
+ * instructions: the text of the persona it writes as, composed as every
28
+ * persona here is (@yaks/persona `wear` and `voice`), and read again for each
29
+ * request that asks, so an edit to it reaches the next one. A request that is
30
+ * the owner's own act writes as no persona, and is told none. */ export declare let told: (host: Hosting, options?: Options) => Told;
31
+ //# sourceMappingURL=routes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"routes.d.ts","sources":["../routes.ts"],"names":[],"mappings":"AAsCA,cAAc,OAAO,EAAE,MAAM,EAAE,IAAI,yBAA8B;AAEjE,cAAc,GAAG,EAAE,KAAK,2BAAgC;AAGxD,SACE,KAAK,OAAO,EAMZ,KAAK,MAAM,sBACO;AAGpB;;6DAE6D,GAC7D,YAAY,UAAU;EACpB;oEACkE,GAClE,UAAU;;AAGZ;mEACmE,GACnE,YAAY;EACV,OAAO,KAAK,OAAO,YAAY,QAAQ,UAAU,YAAY;EAC7D;IAAW,SAAS,MAAM;;EAC1B,KAAK;;AAGP,uEAAuE,GACvE,OAAO,YAAI,aAAe;AAE1B,0DAA0D,GAC1D,OAAO,YAAI,SACT,OAAO,OAAO,EACd,UAAS,aACP,KAAK,YAAY,IAAI,CAAyC;AAclE;;;yEAGyE,GACzE,OAAO,YAAI,UAAW,MAAM,SAAS,UAAS,YAAe,QAuB5D;AAED;+BAC+B,GAC/B,OAAO,YAAI,SAAU,OAAO,OAAO,EAAE,UAAS,YAAe,OACpB;AAEzC;;;;+DAI+D,GAC/D,OAAO,YAAI,OAAQ,MAAM,SAAS,UAAS,YAAe,KAYzD"}
@@ -0,0 +1,24 @@
1
+ import { type Hosting, type Options as Platform } from "./effects.js";
2
+ /** What a config file can set for this plugin. */ export type Options = Platform & {
3
+ /** the tunnel's token; with none, there is nothing to run */ token?: string;
4
+ /** the `cloudflared` to run (default: the one on PATH) */ cloudflared?: string;
5
+ /** the first pause, in ms, before a failed connector is started again */ pause?: number;
6
+ /** how the connector is started; a test passes its own */ spawn?: Spawn;
7
+ };
8
+ /** A started connector: when it ends, and how to end it. */ export type Running = {
9
+ status: Promise<{
10
+ code: number;
11
+ }>;
12
+ kill: () => void;
13
+ logs?: ReadableStream<Uint8Array>;
14
+ };
15
+ /** Start `cmd` with `args` and `env` added to the environment. */ export type Spawn = (cmd: string, args: string[], env: Record<string, string>) => Running;
16
+ /** What the connector is started with: run the account-configured tunnel,
17
+ * saying only warnings and failures. */ export declare let ARGS: readonly [string, string, string, string, string];
18
+ /** The most the pause before a restart grows to. */ export declare let MOST: number;
19
+ /** `cloudflared` as a child of this process, its warnings on our stderr. */ export declare let spawned: Spawn;
20
+ /** Keep the tunnel up until the signal aborts; an already-aborted signal
21
+ * starts nothing. */ export declare let service: (host: (Hosting & {
22
+ report?: (error: unknown) => unknown;
23
+ }) | null, options?: Options, signal?: AbortSignal) => Promise<void>;
24
+ //# sourceMappingURL=service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service.d.ts","sources":["../service.ts"],"names":[],"mappings":"AAmBA,SACE,KAAK,OAAO,EACZ,KAAK,WAAW,QAAQ,uBAEL;AAErB,gDAAgD,GAChD,YAAY,UAAU;EACpB,2DAA2D,GAC3D,QAAQ,MAAM;EACd,wDAAwD,GACxD,cAAc,MAAM;EACpB,uEAAuE,GACvE,QAAQ,MAAM;EACd,wDAAwD,GACxD,QAAQ;;AAGV,0DAA0D,GAC1D,YAAY;EACV,QAAQ;IAAU,MAAM,MAAM;;EAC9B,YAAY,IAAI;EAChB,OAAO,eAAe;;AAGxB,gEAAgE,GAChE,YAAY,SACV,KAAK,MAAM,EACX,MAAM,MAAM,IACZ,KAAK,OAAO,MAAM,EAAE,MAAM,MACvB;AAEL;sCACsC,GACtC,OAAO,YAAI,wDAAiE;AAE5E,kDAAkD,GAClD,OAAO,YAAI,aAAa;AAExB,0EAA0E,GAC1E,OAAO,YAAI,SAAS,MAmBnB;AAoED;mBACmB,GACnB,OAAO,YAAI,UACT,OAAO;EAAY,UAAU,OAAO,OAAO,KAAK,OAAO;KAAM,IAAI,EACjE,UAAS,SACT,SAAQ,gBACP,QAAQ,IAAI,EAWZ"}
@@ -0,0 +1,6 @@
1
+ import type { VocabDoc } from "@yaks/vocab";
2
+ /** The tunnel vocabulary, in the form `loadVocab` accepts. */ export declare let tunnelDoc: VocabDoc;
3
+ /** What this package is: its manifest's description, the one place it is
4
+ * written, which a host stamps on each document here. */ export declare let description: string;
5
+ /** Every document this plugin declares. */ export declare let docs: VocabDoc[];
6
+ //# sourceMappingURL=vocab.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab.d.ts","sources":["../vocab.ts"],"names":[],"mappings":"AAIA,cAAc,QAAQ,2BAAgC;AAItD,4DAA4D,GAC5D,OAAO,YAAI,WAAW,SAA0B;AAEhD;uDACuD,GACvD,OAAO,YAAI,aAAa,MAAM,CAAuB;AAErD,yCAAyC,GACzC,OAAO,YAAI,MAAM,WAAwB"}
package/cloudflare.js ADDED
@@ -0,0 +1,170 @@
1
+ // The account side of a tunnel: the two Cloudflare resources that let a
2
+ // Worker reach a service on somebody's machine, and nothing else.
3
+ //
4
+ // A Cloudflare Tunnel is the machine's outbound connection: `cloudflared` runs
5
+ // there with the tunnel's token (./service.ts) and dials Cloudflare, so the
6
+ // machine needs no open port and no public address. A Workers VPC Service
7
+ // names one host and port behind that tunnel; a Worker bound to it reaches
8
+ // exactly that service and nothing else on the network, whatever URL it
9
+ // fetches (https://developers.cloudflare.com/workers-vpc/api/).
10
+ //
11
+ // A gateway is the third: the Worker in front of a tunnel (./gateway.ts), and
12
+ // the only thing bound to its VPC Service, so that nothing but the platform
13
+ // that uploaded it reaches the machine, and every request it passes on is
14
+ // marked as the tunnel's.
15
+ //
16
+ // The tunnel's token is a credential: whoever holds it can run the tunnel. So
17
+ // this module hands it back to its caller and never keeps or logs it; where it
18
+ // goes next is the caller's business (a vault, a sealed secret).
19
+ //
20
+ // The API token the tunnel and service calls are made with needs Cloudflare
21
+ // Tunnel Write and the Connectivity Directory Admin role
22
+ // (https://developers.cloudflare.com/workers-vpc/configuration/vpc-services/);
23
+ // the one a gateway is uploaded with needs Workers Scripts Edit and the
24
+ // Connectivity Directory Bind role.
25
+ import { GATEWAY } from "./gateway.js";
26
+ /** One call, and what it answered, or the sentence Cloudflare refused it
27
+ * with. Every reply is wrapped `{success, errors, result}`, so a failure is
28
+ * read out of the body and not only off the status. A body is JSON, or a
29
+ * form sent as the multipart it is. */ let call = async (api, method, path, body)=>{
30
+ let json = body !== undefined && !(body instanceof FormData);
31
+ let r = await (api.fetch ?? fetch)(`https://api.cloudflare.com/client/v4/accounts/${api.account}${path}`, {
32
+ method,
33
+ headers: {
34
+ authorization: `Bearer ${api.token}`,
35
+ ...json ? {
36
+ 'content-type': 'application/json'
37
+ } : {}
38
+ },
39
+ ...body === undefined ? {} : {
40
+ body: json ? JSON.stringify(body) : body
41
+ }
42
+ });
43
+ let text = await r.text();
44
+ let said = null;
45
+ try {
46
+ said = JSON.parse(text);
47
+ } catch {
48
+ // not JSON: the status and the text say it
49
+ }
50
+ if (r.ok && said?.success) return said.result;
51
+ let why = said?.errors?.map((e)=>e.message).filter(Boolean).join('; ');
52
+ throw new Error(`cloudflare: ${why || text.slice(0, 400) || r.status}`);
53
+ };
54
+ let field = (v, name)=>{
55
+ let got = v?.[name];
56
+ if (typeof got != 'string' || !got) {
57
+ throw new Error(`cloudflare: the answer named no ${name}`);
58
+ }
59
+ return got;
60
+ };
61
+ /** A tunnel's secret: 32 random bytes, base64, the size Cloudflare asks
62
+ * for. */ let secret = ()=>btoa(String.fromCharCode(...crypto.getRandomValues(new Uint8Array(32))));
63
+ /** The account's Cloudflare Tunnels, each run by `cloudflared` with its
64
+ * token and configured from the account rather than a file on the machine. */ export let tunnels = (api)=>({
65
+ /** A new tunnel, and the token to run it with. */ create: async (name)=>{
66
+ let r = await call(api, 'POST', '/cfd_tunnel', {
67
+ name,
68
+ config_src: 'cloudflare'
69
+ });
70
+ return {
71
+ id: field(r, 'id'),
72
+ token: field(r, 'token')
73
+ };
74
+ },
75
+ /** The token a tunnel runs with now. */ token: async (id)=>{
76
+ let r = await call(api, 'GET', `/cfd_tunnel/${id}/token`);
77
+ if (typeof r != 'string' || !r) {
78
+ throw new Error('cloudflare: the answer named no token');
79
+ }
80
+ return r;
81
+ },
82
+ /** A new token for the tunnel. The old one opens no new connection, and a
83
+ * connector already running keeps going until it restarts. */ rotate: async (id)=>field(await call(api, 'PATCH', `/cfd_tunnel/${id}`, {
84
+ tunnel_secret: secret()
85
+ }), 'token'),
86
+ /** The tunnel gone. One that is already gone is not a failure. */ remove: (id)=>gone(call(api, 'DELETE', `/cfd_tunnel/${id}`))
87
+ });
88
+ /** The account's Workers VPC Services, each one host and port behind one
89
+ * tunnel. */ export let services = (api)=>({
90
+ /** A new HTTP service behind a tunnel; answers its id. */ create: async (name, tunnel, to)=>field(await call(api, 'POST', '/connectivity/directory/services', {
91
+ type: 'http',
92
+ name,
93
+ http_port: to.port,
94
+ host: {
95
+ ipv4: to.host ?? '127.0.0.1',
96
+ network: {
97
+ tunnel_id: tunnel
98
+ }
99
+ }
100
+ }), 'service_id'),
101
+ /** The service gone. One that is already gone is not a failure. */ remove: (id)=>gone(call(api, 'DELETE', `/connectivity/directory/services/${id}`))
102
+ });
103
+ /** The module a gateway runs, by name, and the date its runtime is pinned
104
+ * to. */ export let MODULE = 'gateway.js';
105
+ let DATE = '2026-09-01';
106
+ /** The gateways in front of tunnels: one Worker per tunnel in a Workers for
107
+ * Platforms dispatch namespace, where only the platform that owns the
108
+ * namespace can call it (./gateway.ts). */ export let gateways = (api, namespace)=>{
109
+ let at = (name)=>`/workers/dispatch/namespaces/${namespace}/scripts/${name}`;
110
+ return {
111
+ /** The gateway `name` in front of the VPC Service `service`, made or
112
+ * made again. */ put: async (name, service)=>{
113
+ let body = new FormData();
114
+ let meta = {
115
+ main_module: MODULE,
116
+ compatibility_date: DATE,
117
+ bindings: [
118
+ {
119
+ type: 'vpc_service',
120
+ name: 'BOX',
121
+ service_id: service
122
+ }
123
+ ]
124
+ };
125
+ body.append('metadata', new Blob([
126
+ JSON.stringify(meta)
127
+ ], {
128
+ type: 'application/json'
129
+ }));
130
+ body.append(MODULE, new Blob([
131
+ GATEWAY
132
+ ], {
133
+ type: 'application/javascript+module'
134
+ }), MODULE);
135
+ await call(api, 'PUT', at(name), body);
136
+ },
137
+ /** The gateway gone. One that is already gone is not a failure. */ remove: (name)=>gone(call(api, 'DELETE', at(name)))
138
+ };
139
+ };
140
+ // A delete of something already deleted has done what was asked.
141
+ let gone = async (p)=>{
142
+ try {
143
+ await p;
144
+ } catch (e) {
145
+ if (!/not found|does not exist|404/i.test(String(e))) throw e;
146
+ }
147
+ };
148
+ /**
149
+ * A machine's pair made: a tunnel, and a VPC Service behind it on `to`. Answers
150
+ * both ids and the token. A service that cannot be made takes its tunnel
151
+ * with it, so a failure leaves nothing behind in the account.
152
+ */ export let connect = async (api, name, to)=>{
153
+ let made = await tunnels(api).create(name);
154
+ try {
155
+ let service = await services(api).create(name, made.id, to);
156
+ return {
157
+ tunnel: made.id,
158
+ service,
159
+ token: made.token
160
+ };
161
+ } catch (e) {
162
+ await tunnels(api).remove(made.id);
163
+ throw e;
164
+ }
165
+ };
166
+ /** A machine's pair removed: the service first, since it names the tunnel. */ export let disconnect = async (api, pair)=>{
167
+ await services(api).remove(pair.service);
168
+ await tunnels(api).remove(pair.tunnel);
169
+ };
170
+ //# sourceMappingURL=cloudflare.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cloudflare.js","sources":["./cloudflare.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,kEAAkE;AAClE,EAAE;AACF,+EAA+E;AAC/E,4EAA4E;AAC5E,0EAA0E;AAC1E,2EAA2E;AAC3E,wEAAwE;AACxE,gEAAgE;AAChE,EAAE;AACF,8EAA8E;AAC9E,4EAA4E;AAC5E,0EAA0E;AAC1E,0BAA0B;AAC1B,EAAE;AACF,8EAA8E;AAC9E,+EAA+E;AAC/E,iEAAiE;AACjE,EAAE;AACF,4EAA4E;AAC5E,yDAAyD;AACzD,+EAA+E;AAC/E,wEAAwE;AACxE,oCAAoC;AACpC,SAAS,OAAO,uBAAsB;AA0BtC;;;qCAGqC,GACrC,IAAI,OAAO,OACT,KACA,QACA,MACA;EAEA,IAAI,OAAO,SAAS,aAAa,CAAC,CAAC,gBAAgB,QAAQ;EAC3D,IAAI,IAAI,MAAM,CAAC,IAAI,KAAK,IAAI,KAAK,EAC/B,CAAC,8CAA8C,EAAE,IAAI,OAAO,GAAG,MAAM,EACrE;IACE;IACA,SAAS;MACP,eAAe,CAAC,OAAO,EAAE,IAAI,KAA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