@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.
- package/LICENSE +202 -0
- package/README.md +319 -2
- package/_dist/cloudflare.d.ts +38 -0
- package/_dist/cloudflare.d.ts.map +1 -0
- package/_dist/effects.d.ts +37 -0
- package/_dist/effects.d.ts.map +1 -0
- package/_dist/filter.d.ts +68 -0
- package/_dist/filter.d.ts.map +1 -0
- package/_dist/gateway.d.ts +14 -0
- package/_dist/gateway.d.ts.map +1 -0
- package/_dist/hosted.d.ts +101 -0
- package/_dist/hosted.d.ts.map +1 -0
- package/_dist/mod.d.ts +5 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/routes.d.ts +31 -0
- package/_dist/routes.d.ts.map +1 -0
- package/_dist/service.d.ts +24 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/cloudflare.js +170 -0
- package/cloudflare.js.map +1 -0
- package/cloudflare.ts +233 -0
- package/deno.json +1 -0
- package/effects.js +103 -0
- package/effects.js.map +1 -0
- package/effects.ts +143 -0
- package/filter.js +95 -0
- package/filter.js.map +1 -0
- package/filter.ts +128 -0
- package/gateway.js +37 -0
- package/gateway.js.map +1 -0
- package/gateway.ts +46 -0
- package/hosted.js +345 -0
- package/hosted.js.map +1 -0
- package/hosted.ts +496 -0
- package/mod.js +15 -0
- package/mod.js.map +1 -0
- package/mod.ts +35 -0
- package/package.json +45 -4
- package/routes.js +94 -0
- package/routes.js.map +1 -0
- package/routes.ts +144 -0
- package/service.js +119 -0
- package/service.js.map +1 -0
- package/service.ts +164 -0
- package/vocab.js +16 -0
- package/vocab.js.map +1 -0
- package/vocab.json +62 -0
- package/vocab.ts +17 -0
package/hosted.ts
ADDED
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
// The hosting half, exported as `@yaks/tunnel/hosted`: what a platform that
|
|
2
|
+
// hosts spaces answers about the tunnels they have to machines, and how it
|
|
3
|
+
// passes a request through one. ./routes.ts is the machine's end; this is the
|
|
4
|
+
// other.
|
|
5
|
+
//
|
|
6
|
+
// The platform's door (`door`, at ./effects.ts `TELL`) answers a space's
|
|
7
|
+
// tunnel to its owner and to the platform's own owner, and changes it:
|
|
8
|
+
//
|
|
9
|
+
// GET ?space=ada the tunnel, or null
|
|
10
|
+
// POST space=ada do=connect port=N a new tunnel, service and gateway; the
|
|
11
|
+
// token once
|
|
12
|
+
// POST space=ada do=rotate the gateway put again, and a new token
|
|
13
|
+
// for a tunnel the platform made, once
|
|
14
|
+
// POST space=ada do=disconnect keep the tunnel in the trash
|
|
15
|
+
// POST space=ada do=restore restore the same tunnel
|
|
16
|
+
// POST space=ada do=purge ask its owner to confirm a permanent purge
|
|
17
|
+
// POST space=ada do=adopt tunnel=… service=… record a pair made elsewhere,
|
|
18
|
+
// with a gateway in front
|
|
19
|
+
// POST do=tell conversation=… key=… text=… [about=…] the machine telling
|
|
20
|
+
// its owner what changed there
|
|
21
|
+
// (./effects.ts)
|
|
22
|
+
//
|
|
23
|
+
// The space's owner connects, rotates and disconnects. Adopting is the
|
|
24
|
+
// platform owner's alone: the ids name resources in the platform's account,
|
|
25
|
+
// and a space that could write any service id could reach somebody else's
|
|
26
|
+
// machine. Disconnecting keeps the pair and its names in the trash and
|
|
27
|
+
// removes only its regenerable gateway. Restoring puts the gateway again.
|
|
28
|
+
// Purging an adopted pair forgets its ids; whoever made it removes it.
|
|
29
|
+
// A tunnel’s token is answered once and never kept.
|
|
30
|
+
//
|
|
31
|
+
// A request reaches a machine two ways: an app's own worker asking for it
|
|
32
|
+
// (`reach`), built in the space and never an installed copy, and the space's
|
|
33
|
+
// owner asking with no app between (`machine`), by their connector or their
|
|
34
|
+
// Operator, which reaches only its own space's machine (`owners`). Either way
|
|
35
|
+
// the request goes on to the space's gateway (./gateway.ts) with the
|
|
36
|
+
// platform's own words taken off and its word on who is asking put on
|
|
37
|
+
// (`onward`), and every way it can fail to arrive is an answer.
|
|
38
|
+
//
|
|
39
|
+
// What only the host can do comes in as data: who is asking and where they
|
|
40
|
+
// stand (`Keeping`), and the call that hands a request to a gateway
|
|
41
|
+
// (`Relay`), which is a Workers for Platforms dispatch binding on yaks.app.
|
|
42
|
+
import { VIA } from '@yaks/api'
|
|
43
|
+
import type { Eid } from 'jsr:@yaks/graph@0.2.36'
|
|
44
|
+
import {
|
|
45
|
+
type Api,
|
|
46
|
+
connect,
|
|
47
|
+
disconnect,
|
|
48
|
+
type Gateways,
|
|
49
|
+
tunnels,
|
|
50
|
+
} from './cloudflare.js'
|
|
51
|
+
import type { Told } from './effects.ts'
|
|
52
|
+
import { BY, CALLER, WITH } from './gateway.js'
|
|
53
|
+
|
|
54
|
+
/** A tunnel as its host records it on the entity it reaches a machine from
|
|
55
|
+
* (the `tunnel` component): its id, the VPC Service its gateway is bound to,
|
|
56
|
+
* and whether it was made elsewhere and only recorded here. */
|
|
57
|
+
export type Tunnel = { id: string; service: string; adopted: boolean }
|
|
58
|
+
|
|
59
|
+
/** A space as this module reads it: its slug, and its tunnel or none. */
|
|
60
|
+
export type Held = {
|
|
61
|
+
slug: string
|
|
62
|
+
tunnel: Tunnel | null
|
|
63
|
+
tunnelTrashed?: string | null
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Who is asking a machine, as the platform tells it (`CALLER`): an `app`'s
|
|
67
|
+
* own worker; the owner's `operator`, in a conversation or a run of its own;
|
|
68
|
+
* or the owner's own `connector`, the assistant they signed in to the
|
|
69
|
+
* platform from. A machine's config opens paths, tools and a persona to each
|
|
70
|
+
* by this word. */
|
|
71
|
+
export type Caller = 'app' | 'operator' | 'connector'
|
|
72
|
+
|
|
73
|
+
/** Who asks a machine for its owner: their connector unless it says their
|
|
74
|
+
* Operator, which also names its space and the conversation it asks in
|
|
75
|
+
* (`via`), where there is one. A request that carries the owner's own words
|
|
76
|
+
* as they gave them, and nobody's reading of them, is their `own` act: the
|
|
77
|
+
* machine is told who they are (`BY`), and writes it as theirs. Only the host
|
|
78
|
+
* says so, of words it read itself. */
|
|
79
|
+
export type Calling =
|
|
80
|
+
& { via?: () => Promise<string | undefined> | string | undefined }
|
|
81
|
+
& { own?: boolean }
|
|
82
|
+
& ({ caller: 'connector' } | { caller: 'operator'; space: string })
|
|
83
|
+
|
|
84
|
+
/** One call through a space's gateway, as its host makes it: the machine's
|
|
85
|
+
* answer, null where the tunnel has no gateway, and a throw where the tunnel
|
|
86
|
+
* or the machine did not answer. */
|
|
87
|
+
export type Relay = (out: Request) => Promise<Response | null>
|
|
88
|
+
|
|
89
|
+
/** The name of the gateway in front of the tunnel of the space `eid`, in the
|
|
90
|
+
* namespace the host uploads gateways to (./cloudflare.ts `gateways`). */
|
|
91
|
+
export let gatewayOf = (eid: string): string => `tunnel-${eid}`
|
|
92
|
+
|
|
93
|
+
/** Where an app's worker asks for its space's machine, under the app's own
|
|
94
|
+
* doors: `/tunneled/mail/inbound` is `/mail/inbound` on the machine. */
|
|
95
|
+
export let TUNNELED = '/tunneled/'
|
|
96
|
+
|
|
97
|
+
/** A refusal as this module answers one: its code, and the sentence it says. */
|
|
98
|
+
export type Ending = { status: number; code: string; message: string }
|
|
99
|
+
|
|
100
|
+
let ending = (e: Ending): Response =>
|
|
101
|
+
Response.json({ error: { code: e.code, message: e.message } }, {
|
|
102
|
+
status: e.status,
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
let refused = (status: number, code: string, message: string): Response =>
|
|
106
|
+
ending({ status, code, message })
|
|
107
|
+
|
|
108
|
+
// How a call that reached no machine ends, said the same to an app's worker
|
|
109
|
+
// and to the tunnel's owner.
|
|
110
|
+
let NO_TUNNEL = (space: Held): Ending => ({
|
|
111
|
+
status: 404,
|
|
112
|
+
code: 'no_tunnel',
|
|
113
|
+
message:
|
|
114
|
+
`${space.slug} has no tunnel to a machine; its owner makes one first`,
|
|
115
|
+
})
|
|
116
|
+
let NO_GATEWAY = (space: Held): Ending => ({
|
|
117
|
+
status: 503,
|
|
118
|
+
code: 'no_gateway',
|
|
119
|
+
message:
|
|
120
|
+
`${space.slug}'s tunnel has no gateway; its owner rotates the tunnel to make one`,
|
|
121
|
+
})
|
|
122
|
+
let UNREACHED = (space: Held): Ending => ({
|
|
123
|
+
status: 502,
|
|
124
|
+
code: 'unreached',
|
|
125
|
+
message: `the machine ${space.slug}'s tunnel goes to did not answer`,
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
// ── A request on its way to a machine ───────────────────────────────────────
|
|
129
|
+
|
|
130
|
+
// The platform's own words about a request (the grants, who is looking), none
|
|
131
|
+
// of which is the machine's business, and the words it says to the machine
|
|
132
|
+
// itself: no caller's request names the conversation it was made in, or who
|
|
133
|
+
// is asking.
|
|
134
|
+
let OURS = /^(cookie|x-via)$|^x-yak-/
|
|
135
|
+
|
|
136
|
+
/** A request as it goes on to a machine: `req`'s method, body and headers at
|
|
137
|
+
* `path` there, with the platform's own words taken off and its word on who
|
|
138
|
+
* is asking put on: the conversation it was made in (`via`), whose that is
|
|
139
|
+
* (`with`, only beside `via`) and whose own act it is (`by`). */
|
|
140
|
+
export let onward = (
|
|
141
|
+
path: string,
|
|
142
|
+
req: Request,
|
|
143
|
+
caller: Caller,
|
|
144
|
+
said: { via?: string; with?: string; by?: string } = {},
|
|
145
|
+
): Request => {
|
|
146
|
+
let headers = new Headers()
|
|
147
|
+
for (let [k, v] of req.headers) if (!OURS.test(k)) headers.set(k, v)
|
|
148
|
+
headers.set(CALLER, caller)
|
|
149
|
+
if (said.via) headers.set(VIA, said.via)
|
|
150
|
+
if (said.via && said.with) headers.set(WITH, said.with)
|
|
151
|
+
if (said.by) headers.set(BY, said.by)
|
|
152
|
+
return new Request(`http://machine${path}`, {
|
|
153
|
+
method: req.method,
|
|
154
|
+
headers,
|
|
155
|
+
body: req.body,
|
|
156
|
+
redirect: 'manual',
|
|
157
|
+
signal: req.signal,
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// `out` through the relay, or how it failed to arrive; a throw is told to
|
|
162
|
+
// `report` where there is one.
|
|
163
|
+
let relayed = async (
|
|
164
|
+
space: Held,
|
|
165
|
+
relay: Relay,
|
|
166
|
+
out: Request,
|
|
167
|
+
report?: (e: unknown) => void,
|
|
168
|
+
): Promise<Response> => {
|
|
169
|
+
try {
|
|
170
|
+
return await relay(out) ?? ending(NO_GATEWAY(space))
|
|
171
|
+
} catch (e) {
|
|
172
|
+
report?.(e)
|
|
173
|
+
return ending(UNREACHED(space))
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** An app as this module reads it: its slug, and whether it is an installed
|
|
178
|
+
* copy of somebody else's. */
|
|
179
|
+
export type Reaching = { slug: string; installed?: unknown }
|
|
180
|
+
|
|
181
|
+
/** Why `app` may not reach the machine its space's tunnel goes to, or null
|
|
182
|
+
* where it may: only an app built in the space reaches it, since an installed
|
|
183
|
+
* copy's code was written elsewhere and a tunnel reaches into the owner's own
|
|
184
|
+
* machine, and only while the space has a tunnel. */
|
|
185
|
+
export let barred = (space: Held, app: Reaching): Ending | null =>
|
|
186
|
+
app.installed
|
|
187
|
+
? {
|
|
188
|
+
status: 403,
|
|
189
|
+
code: 'installed',
|
|
190
|
+
message:
|
|
191
|
+
`${app.slug} is an installed copy, and only an app built in ${space.slug} reaches the machine its tunnel goes to`,
|
|
192
|
+
}
|
|
193
|
+
: !space.tunnel || space.tunnelTrashed
|
|
194
|
+
? NO_TUNNEL(space)
|
|
195
|
+
: null
|
|
196
|
+
|
|
197
|
+
/** An app's own worker reaching the machine its space's tunnel goes to, at
|
|
198
|
+
* `path` under its doors, or null for a path not at `TUNNELED`. `itself` is
|
|
199
|
+
* whether the host knows the request for the app's own worker: nothing a
|
|
200
|
+
* visitor holds opens it. The request goes on as an `app`'s, and the machine
|
|
201
|
+
* decides what it answers. A throw from the relay is told to `report`. */
|
|
202
|
+
export let reach = async (
|
|
203
|
+
req: Request,
|
|
204
|
+
path: string,
|
|
205
|
+
at: { space: Held; app: Reaching; itself: boolean },
|
|
206
|
+
relay: Relay,
|
|
207
|
+
report?: (e: unknown) => void,
|
|
208
|
+
): Promise<Response | null> => {
|
|
209
|
+
if (!path.startsWith(TUNNELED)) return null
|
|
210
|
+
if (!at.itself) {
|
|
211
|
+
return refused(
|
|
212
|
+
403,
|
|
213
|
+
'not_the_app',
|
|
214
|
+
"only the app's own worker reaches the machine its space's tunnel goes to",
|
|
215
|
+
)
|
|
216
|
+
}
|
|
217
|
+
let bar = barred(at.space, at.app)
|
|
218
|
+
if (bar) return ending(bar)
|
|
219
|
+
let rest = path.slice(TUNNELED.length - 1) + new URL(req.url).search
|
|
220
|
+
return await relayed(at.space, relay, onward(rest, req, 'app'), report)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Whether `asking` reaches the machine of the space `at` for a person seated
|
|
224
|
+
* there as `role`: its owner alone, and their Operator only from that space,
|
|
225
|
+
* since another space's members shape its instructions and memory. */
|
|
226
|
+
export let owners = (
|
|
227
|
+
role: string | null | undefined,
|
|
228
|
+
at: string,
|
|
229
|
+
asking: Calling,
|
|
230
|
+
): boolean =>
|
|
231
|
+
role == 'owner' && (asking.caller != 'operator' || asking.space == at)
|
|
232
|
+
|
|
233
|
+
/** The machine `space`'s tunnel goes to, as a `fetch` for its owner `person`,
|
|
234
|
+
* asked as `asking` says (whom the host has found `owners`). Only the path of
|
|
235
|
+
* what it is asked for is meant. Every ending is an answer: a space with no
|
|
236
|
+
* tunnel, a tunnel with no gateway and a machine that is off are the machine
|
|
237
|
+
* being unavailable to its owner. */
|
|
238
|
+
export let machine = (
|
|
239
|
+
space: Held,
|
|
240
|
+
person: string,
|
|
241
|
+
asking: Calling,
|
|
242
|
+
relay: Relay,
|
|
243
|
+
): typeof fetch =>
|
|
244
|
+
async (input, init) => {
|
|
245
|
+
if (!space.tunnel || space.tunnelTrashed) return ending(NO_TUNNEL(space))
|
|
246
|
+
let req = new Request(input, init)
|
|
247
|
+
let { pathname, search } = new URL(req.url)
|
|
248
|
+
let out = onward(pathname + search, req, asking.caller, {
|
|
249
|
+
via: await asking.via?.(),
|
|
250
|
+
with: person,
|
|
251
|
+
by: asking.own ? person : undefined,
|
|
252
|
+
})
|
|
253
|
+
return await relayed(space, relay, out)
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// ── The door ────────────────────────────────────────────────────────────────
|
|
257
|
+
|
|
258
|
+
/** A space as the door finds it for whoever asks: its tunnel, and whether
|
|
259
|
+
* they own it or own the platform. */
|
|
260
|
+
export type Kept = Held & { eid: string; owner: boolean; platform: boolean }
|
|
261
|
+
|
|
262
|
+
/** What the door reads and writes through its host. */
|
|
263
|
+
export type Keeping = {
|
|
264
|
+
/** the account tunnels are made in, or null until the host has its token */
|
|
265
|
+
account: Api | null
|
|
266
|
+
/** the namespace gateways are uploaded to, or null until the host can */
|
|
267
|
+
gateways: Gateways | null
|
|
268
|
+
/** who is asking, by the host's id for them, or null for nobody */
|
|
269
|
+
who: () => Promise<string | null>
|
|
270
|
+
/** the space `slug`, as `person` stands in it, or null for none */
|
|
271
|
+
find: (slug: string, person: string | null) => Promise<Kept | null>
|
|
272
|
+
/** the tunnel recorded on the space `eid`, or none */
|
|
273
|
+
write: (eid: string, row: Tunnel | null) => Promise<unknown>
|
|
274
|
+
/** keep the tunnel and its shared names in the trash, or restore it */
|
|
275
|
+
trash: (eid: string, at: string | null) => Promise<unknown>
|
|
276
|
+
/** ask its person to purge a trashed tunnel through their confirmation page */
|
|
277
|
+
purge: (space: Kept) => Promise<string>
|
|
278
|
+
/** what the machine tells its owner `person`, taken in by the host: what
|
|
279
|
+
* it answers, or null where none of their spaces with a tunnel holds the
|
|
280
|
+
* conversation */
|
|
281
|
+
tell: (person: string, told: Told) => Promise<object | null>
|
|
282
|
+
/** An authenticated machine reports that its tunnel registered. */
|
|
283
|
+
ready?: (person: string, tunnel: string) => Promise<void>
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// A Cloudflare id, as the API spells one: a uuid. An eid is spelled the same.
|
|
287
|
+
let UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
|
|
288
|
+
|
|
289
|
+
let NO_ACCOUNT = 'the platform has no account token to make tunnels with'
|
|
290
|
+
let NO_UPLOAD = "the platform has no token to upload a tunnel's gateway with"
|
|
291
|
+
|
|
292
|
+
let shown = (space: Held, tunnel: Tunnel | null, more = {}): Response =>
|
|
293
|
+
Response.json({ space: space.slug, tunnel, ...more })
|
|
294
|
+
|
|
295
|
+
/** The platform's door onto its spaces' tunnels, as the header says. */
|
|
296
|
+
export let door = async (req: Request, host: Keeping): Promise<Response> => {
|
|
297
|
+
if (req.method != 'GET' && req.method != 'POST') {
|
|
298
|
+
return refused(
|
|
299
|
+
405,
|
|
300
|
+
'method_not_allowed',
|
|
301
|
+
'get the tunnel, or post a change',
|
|
302
|
+
)
|
|
303
|
+
}
|
|
304
|
+
let form = req.method == 'POST' ? await req.formData() : null
|
|
305
|
+
let said = (k: string) =>
|
|
306
|
+
String(form?.get(k) ?? new URL(req.url).searchParams.get(k) ?? '')
|
|
307
|
+
let person = await host.who()
|
|
308
|
+
if (form && said('do') == 'tell') return await heard(host, person, said)
|
|
309
|
+
if (form && said('do') == 'ready') {
|
|
310
|
+
let tunnel = said('tunnel')
|
|
311
|
+
if (!person) {
|
|
312
|
+
return refused(403, 'not_owner', 'Sign in to report readiness.')
|
|
313
|
+
}
|
|
314
|
+
if (!UUID.test(tunnel)) {
|
|
315
|
+
return refused(400, 'bad_ids', 'Give the tunnel id.')
|
|
316
|
+
}
|
|
317
|
+
await host.ready?.(person, tunnel)
|
|
318
|
+
return new Response(null, { status: 204 })
|
|
319
|
+
}
|
|
320
|
+
let space = said('space') ? await host.find(said('space'), person) : null
|
|
321
|
+
if (!space) {
|
|
322
|
+
return refused(
|
|
323
|
+
404,
|
|
324
|
+
'no_space',
|
|
325
|
+
`no space is called ${said('space') || '""'}`,
|
|
326
|
+
)
|
|
327
|
+
}
|
|
328
|
+
if (!space.owner && !space.platform) {
|
|
329
|
+
return refused(403, 'not_owner', `${space.slug}'s machine is its owner's`)
|
|
330
|
+
}
|
|
331
|
+
if (req.method == 'GET') {
|
|
332
|
+
return shown(space, space.tunnelTrashed ? null : space.tunnel, {
|
|
333
|
+
...space.tunnelTrashed ? { trashed: space.tunnelTrashed } : {},
|
|
334
|
+
})
|
|
335
|
+
}
|
|
336
|
+
return await changed(host, space, said)
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// A knock from the machine (./effects.ts `platformTell`): only its owner
|
|
340
|
+
// tells it, by their own connection, and it names its conversation and
|
|
341
|
+
// itself, and says something. What it tells of rides with it where it is an
|
|
342
|
+
// eid.
|
|
343
|
+
let heard = async (
|
|
344
|
+
host: Keeping,
|
|
345
|
+
person: string | null,
|
|
346
|
+
said: (k: string) => string,
|
|
347
|
+
): Promise<Response> => {
|
|
348
|
+
if (!person) {
|
|
349
|
+
return refused(
|
|
350
|
+
403,
|
|
351
|
+
'not_owner',
|
|
352
|
+
"a machine tells only its owner's Operator",
|
|
353
|
+
)
|
|
354
|
+
}
|
|
355
|
+
let [conversation, key, text, about] = [
|
|
356
|
+
'conversation',
|
|
357
|
+
'key',
|
|
358
|
+
'text',
|
|
359
|
+
'about',
|
|
360
|
+
]
|
|
361
|
+
.map(said)
|
|
362
|
+
if (!conversation || !key || !text.trim()) {
|
|
363
|
+
return refused(
|
|
364
|
+
400,
|
|
365
|
+
'bad_tell',
|
|
366
|
+
'a knock names its conversation and itself, and says something',
|
|
367
|
+
)
|
|
368
|
+
}
|
|
369
|
+
let told: Told = {
|
|
370
|
+
conversation,
|
|
371
|
+
key: key as Eid,
|
|
372
|
+
text,
|
|
373
|
+
...UUID.test(about) ? { about: about as Eid } : {},
|
|
374
|
+
...['settled', 'failed', 'stopped', 'open'].includes(said('outcome'))
|
|
375
|
+
? { outcome: said('outcome') }
|
|
376
|
+
: {},
|
|
377
|
+
}
|
|
378
|
+
let answer = await host.tell(person, told)
|
|
379
|
+
return answer ? Response.json(answer) : refused(
|
|
380
|
+
404,
|
|
381
|
+
'no_conversation',
|
|
382
|
+
`no space of yours with a tunnel holds ${conversation}`,
|
|
383
|
+
)
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// A change to `space`'s tunnel, by its owner or the platform's.
|
|
387
|
+
let changed = async (
|
|
388
|
+
host: Keeping,
|
|
389
|
+
space: Kept,
|
|
390
|
+
said: (k: string) => string,
|
|
391
|
+
): Promise<Response> => {
|
|
392
|
+
let held = space.tunnel
|
|
393
|
+
let act = said('do')
|
|
394
|
+
let gateway = gatewayOf(space.eid)
|
|
395
|
+
if (act == 'adopt') {
|
|
396
|
+
if (!space.platform) {
|
|
397
|
+
return refused(
|
|
398
|
+
403,
|
|
399
|
+
'not_platform',
|
|
400
|
+
"adopting names the platform's own resources, so the platform's owner does it",
|
|
401
|
+
)
|
|
402
|
+
}
|
|
403
|
+
// Replacing either kind of pair would lose its saved identifiers.
|
|
404
|
+
if (
|
|
405
|
+
held && (!held.adopted || space.tunnelTrashed ||
|
|
406
|
+
held.id != said('tunnel') || held.service != said('service'))
|
|
407
|
+
) {
|
|
408
|
+
return refused(
|
|
409
|
+
409,
|
|
410
|
+
'has_tunnel',
|
|
411
|
+
`${space.slug} has a tunnel; restore it or confirm its purge before adopting another`,
|
|
412
|
+
)
|
|
413
|
+
}
|
|
414
|
+
let [id, service] = [said('tunnel'), said('service')]
|
|
415
|
+
if (!UUID.test(id) || !UUID.test(service)) {
|
|
416
|
+
return refused(400, 'bad_ids', 'tunnel and service are each a uuid')
|
|
417
|
+
}
|
|
418
|
+
if (!host.gateways) return refused(503, 'no_token', NO_UPLOAD)
|
|
419
|
+
await host.gateways.put(gateway, service)
|
|
420
|
+
let row = { id, service, adopted: true }
|
|
421
|
+
await host.write(space.eid, row)
|
|
422
|
+
return shown(space, row)
|
|
423
|
+
}
|
|
424
|
+
if (!space.owner) {
|
|
425
|
+
return refused(403, 'not_owner', `${space.slug}'s machine is its owner's`)
|
|
426
|
+
}
|
|
427
|
+
if (act == 'connect') {
|
|
428
|
+
if (held) {
|
|
429
|
+
return refused(409, 'has_tunnel', `${space.slug} has a tunnel already`)
|
|
430
|
+
}
|
|
431
|
+
let port = Number(said('port'))
|
|
432
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
433
|
+
return refused(400, 'bad_port', 'port is the one the machine serves on')
|
|
434
|
+
}
|
|
435
|
+
let { account, gateways } = host
|
|
436
|
+
if (!account) return refused(503, 'no_token', NO_ACCOUNT)
|
|
437
|
+
if (!gateways) return refused(503, 'no_token', NO_UPLOAD)
|
|
438
|
+
let made = await connect(account, `space-${space.eid}`, { port })
|
|
439
|
+
let row = { id: made.tunnel, service: made.service, adopted: false }
|
|
440
|
+
try {
|
|
441
|
+
await gateways.put(gateway, made.service)
|
|
442
|
+
await host.write(space.eid, row)
|
|
443
|
+
} catch (e) {
|
|
444
|
+
await gateways.remove(gateway)
|
|
445
|
+
await disconnect(account, made)
|
|
446
|
+
throw e
|
|
447
|
+
}
|
|
448
|
+
return shown(space, row, { token: made.token })
|
|
449
|
+
}
|
|
450
|
+
if (!['rotate', 'disconnect', 'restore', 'purge'].includes(act)) {
|
|
451
|
+
return refused(
|
|
452
|
+
400,
|
|
453
|
+
'bad_do',
|
|
454
|
+
'do is connect, rotate, disconnect, restore, purge, adopt or tell',
|
|
455
|
+
)
|
|
456
|
+
}
|
|
457
|
+
if (!held) return refused(404, 'no_tunnel', `${space.slug} has no tunnel`)
|
|
458
|
+
if (act == 'purge') {
|
|
459
|
+
if (!space.tunnelTrashed) {
|
|
460
|
+
return refused(
|
|
461
|
+
409,
|
|
462
|
+
'not_trashed',
|
|
463
|
+
'disconnect the tunnel into the trash first',
|
|
464
|
+
)
|
|
465
|
+
}
|
|
466
|
+
return shown(space, null, {
|
|
467
|
+
trashed: space.tunnelTrashed,
|
|
468
|
+
message: await host.purge(space),
|
|
469
|
+
})
|
|
470
|
+
}
|
|
471
|
+
if (space.tunnelTrashed && act == 'rotate') {
|
|
472
|
+
return refused(409, 'trashed', 'restore the tunnel from the trash first')
|
|
473
|
+
}
|
|
474
|
+
let { account, gateways } = host
|
|
475
|
+
if (!gateways) return refused(503, 'no_token', NO_UPLOAD)
|
|
476
|
+
if (act == 'rotate' && !held.adopted && !account) {
|
|
477
|
+
return refused(503, 'no_token', NO_ACCOUNT)
|
|
478
|
+
}
|
|
479
|
+
if (act == 'restore') {
|
|
480
|
+
await gateways.put(gateway, held.service)
|
|
481
|
+
await host.trash(space.eid, null)
|
|
482
|
+
return shown(space, held)
|
|
483
|
+
}
|
|
484
|
+
if (act == 'rotate') {
|
|
485
|
+
// An adopted tunnel is rotated where it was made; its gateway is ours.
|
|
486
|
+
let token = account && !held.adopted
|
|
487
|
+
? await tunnels(account).rotate(held.id)
|
|
488
|
+
: undefined
|
|
489
|
+
await gateways.put(gateway, held.service)
|
|
490
|
+
return shown(space, held, { token })
|
|
491
|
+
}
|
|
492
|
+
let at = space.tunnelTrashed ?? new Date().toISOString()
|
|
493
|
+
await host.trash(space.eid, at)
|
|
494
|
+
await gateways.remove(gateway)
|
|
495
|
+
return shown(space, null, { trashed: at })
|
|
496
|
+
}
|
package/mod.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// @yaks/tunnel: a machine reached from a hosted space through a Cloudflare
|
|
2
|
+
// Tunnel and a Workers VPC Service. The account calls that make and remove
|
|
3
|
+
// one, and the gateway in front of it, are ./cloudflare.ts; the tunnel's two
|
|
4
|
+
// ends are ./gateway.ts, the module the gateway runs, and ./filter.ts, the
|
|
5
|
+
// machine's check on what comes through; what the hosting platform answers
|
|
6
|
+
// about its spaces' tunnels and passes through one is ./hosted.ts; the
|
|
7
|
+
// filter as a machine's server
|
|
8
|
+
// takes it is ./routes.ts, and the connector it runs is ./service.ts, each its
|
|
9
|
+
// own subpath because each is one role's; the `tunnel` component that records
|
|
10
|
+
// an entity's pair is ./vocab.json.
|
|
11
|
+
export { connect, disconnect, gateways, services, tunnels } from "./cloudflare.js";
|
|
12
|
+
export { Closed, filter, marked, named, offered, opens } from "./filter.js";
|
|
13
|
+
export { BY, CALLER, GATEWAY, HEADER, WITH } from "./gateway.js";
|
|
14
|
+
export { docs, tunnelDoc } from "./vocab.js";
|
|
15
|
+
//# sourceMappingURL=mod.js.map
|
package/mod.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,2EAA2E;AAC3E,6EAA6E;AAC7E,2EAA2E;AAC3E,2EAA2E;AAC3E,uEAAuE;AACvE,+BAA+B;AAC/B,+EAA+E;AAC/E,8EAA8E;AAC9E,oCAAoC;AAEpC,SAEE,OAAO,EACP,UAAU,EAEV,QAAQ,EAGR,QAAQ,EAER,OAAO,0BACe;AACxB,SAEE,MAAM,EACN,MAAM,EACN,MAAM,EACN,KAAK,EACL,OAAO,EAEP,KAAK,sBACa;AACpB,SAAS,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,uBAAsB;AAChE,SAAS,IAAI,EAAE,SAAS,qBAAoB"}
|
package/mod.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// @yaks/tunnel: a machine reached from a hosted space through a Cloudflare
|
|
2
|
+
// Tunnel and a Workers VPC Service. The account calls that make and remove
|
|
3
|
+
// one, and the gateway in front of it, are ./cloudflare.ts; the tunnel's two
|
|
4
|
+
// ends are ./gateway.ts, the module the gateway runs, and ./filter.ts, the
|
|
5
|
+
// machine's check on what comes through; what the hosting platform answers
|
|
6
|
+
// about its spaces' tunnels and passes through one is ./hosted.ts; the
|
|
7
|
+
// filter as a machine's server
|
|
8
|
+
// takes it is ./routes.ts, and the connector it runs is ./service.ts, each its
|
|
9
|
+
// own subpath because each is one role's; the `tunnel` component that records
|
|
10
|
+
// an entity's pair is ./vocab.json.
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
type Api,
|
|
14
|
+
connect,
|
|
15
|
+
disconnect,
|
|
16
|
+
type Gateways,
|
|
17
|
+
gateways,
|
|
18
|
+
type Made,
|
|
19
|
+
type Pair,
|
|
20
|
+
services,
|
|
21
|
+
type Target,
|
|
22
|
+
tunnels,
|
|
23
|
+
} from './cloudflare.js'
|
|
24
|
+
export {
|
|
25
|
+
type Callers,
|
|
26
|
+
Closed,
|
|
27
|
+
filter,
|
|
28
|
+
marked,
|
|
29
|
+
named,
|
|
30
|
+
offered,
|
|
31
|
+
type Opened,
|
|
32
|
+
opens,
|
|
33
|
+
} from './filter.js'
|
|
34
|
+
export { BY, CALLER, GATEWAY, HEADER, WITH } from './gateway.js'
|
|
35
|
+
export { docs, tunnelDoc } from './vocab.js'
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaks/tunnel",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.36",
|
|
4
|
+
"homepage": "https://jsr.io/@yaks/tunnel",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./_dist/mod.d.ts",
|
|
9
|
+
"default": "./mod.js"
|
|
10
|
+
},
|
|
11
|
+
"./vocab": {
|
|
12
|
+
"types": "./_dist/vocab.d.ts",
|
|
13
|
+
"default": "./vocab.js"
|
|
14
|
+
},
|
|
15
|
+
"./routes": {
|
|
16
|
+
"types": "./_dist/routes.d.ts",
|
|
17
|
+
"default": "./routes.js"
|
|
18
|
+
},
|
|
19
|
+
"./hosted": {
|
|
20
|
+
"types": "./_dist/hosted.d.ts",
|
|
21
|
+
"default": "./hosted.js"
|
|
22
|
+
},
|
|
23
|
+
"./service": {
|
|
24
|
+
"types": "./_dist/service.d.ts",
|
|
25
|
+
"default": "./service.js"
|
|
26
|
+
},
|
|
27
|
+
"./effects": {
|
|
28
|
+
"types": "./_dist/effects.d.ts",
|
|
29
|
+
"default": "./effects.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"license": "Apache-2.0",
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@yaks/api": "0.2.36",
|
|
35
|
+
"@yaks/connections": "0.2.36",
|
|
36
|
+
"@yaks/effects": "0.2.36",
|
|
37
|
+
"@yaks/fp": "0.2.36",
|
|
38
|
+
"@yaks/graph": "0.2.36",
|
|
39
|
+
"@yaks/persona": "0.2.36",
|
|
40
|
+
"@yaks/process": "0.2.36",
|
|
41
|
+
"@yaks/secrets": "0.2.36",
|
|
42
|
+
"@yaks/session": "0.2.36",
|
|
43
|
+
"@yaks/vocab": "0.2.36"
|
|
44
|
+
},
|
|
45
|
+
"description": "Reaches a machine from a hosted space through a Cloudflare Tunnel and a Workers VPC Service: the account calls that make one, the hosting platform's door onto them and the requests it passes through one, the connector that runs it on the machine, the machine's word on what comes through: the paths it opened, the tools it offers and who a caller writes as, and what the machine tells the platform back.",
|
|
46
|
+
"repository": "https://github.com/yak-sh/yak.git"
|
|
47
|
+
}
|