Repository navigation
Expand file tree
/
Copy pathproxy-machine.ts
More file actions
55 lines (38 loc) · 1.82 KB
/
Copy pathproxy-machine.ts
File metadata and controls
55 lines (38 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
export * as BrowserProxyMachine from "./proxy-machine.js"
// The desktop proxy's listener and the read side of each tunnel socket it hands to Chromium.
export type State = { readonly status: "open" } | { readonly status: "closed" }
export type Event = { readonly type: "close" }
/** `shutdown` aborts pending connects and destroys every tunnel and client socket, once. */
export type ProxyEffect = "shutdown"
export type Transition = { readonly state: State; readonly effects: readonly ProxyEffect[] }
export const initial: State = { status: "open" }
const CLOSED: State = { status: "closed" }
export function transition(state: State, event: Event): Transition {
if (state.status === "closed") return { state, effects: [] }
switch (event.type) {
case "close":
return { state: CLOSED, effects: ["shutdown"] }
}
}
/**
* Whether a client connection or a tunnel that just opened may join the proxy. One that arrives
* after `close` is destroyed instead, since shutdown already ran.
*/
export function admits(state: State) {
return state.status === "open"
}
/**
* One tunnel socket's read side: at most one transport read in flight, and no reads after EOF or
* destruction.
*/
export type ReadState = "idle" | "reading" | "ended" | "destroyed"
/** Chromium pulled, a transport read answered with data or EOF, or the socket was destroyed. */
export type ReadEvent = "pull" | "chunk" | "eof" | "destroyed"
/** Returns `state` itself when the event does not apply, so callers compare to see whether to act. */
export function transitionRead(state: ReadState, event: ReadEvent): ReadState {
if (state === "destroyed") return state
if (event === "destroyed") return "destroyed"
if (event === "pull") return state === "idle" ? "reading" : state
if (state !== "reading") return state
return event === "eof" ? "ended" : "idle"
}