@opencode/plugin-browser 2.2.5 → 2.2.6

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.
@@ -0,0 +1,84 @@
1
+ export * as BrowserTunnelMachine from "./tunnel-machine.js";
2
+ export declare const MAX_TUNNELS = 64;
3
+ export type Entry =
4
+ /** The slot is claimed before the first `await`, so concurrent opens cannot pass the cap. */
5
+ {
6
+ readonly status: "connecting";
7
+ } | {
8
+ readonly status: "open";
9
+ readonly reading: boolean;
10
+ readonly writing: boolean;
11
+ /** The desktop has read EOF. */
12
+ readonly readEnded: boolean;
13
+ /** The desktop's final write flushed. */
14
+ readonly writeEnded: boolean;
15
+ /** The socket's first error; the next read reports it. */
16
+ readonly error: Error | undefined;
17
+ };
18
+ export type State = {
19
+ readonly status: "active";
20
+ readonly entries: ReadonlyMap<string, Entry>;
21
+ } | {
22
+ readonly status: "disposed";
23
+ };
24
+ export type ReadOutcome = "chunk" | "eof" | "error" | "interrupted";
25
+ export type WriteOutcome = "ok" | "error" | "interrupted";
26
+ export type Event = {
27
+ readonly type: "open.started";
28
+ readonly id: string;
29
+ } | {
30
+ readonly type: "open.connected";
31
+ readonly id: string;
32
+ } | {
33
+ readonly type: "open.failed";
34
+ readonly id: string;
35
+ } | {
36
+ readonly type: "socket.errored";
37
+ readonly id: string;
38
+ readonly error: Error;
39
+ } | {
40
+ readonly type: "socket.closed";
41
+ readonly id: string;
42
+ } | {
43
+ readonly type: "read.started";
44
+ readonly id: string;
45
+ } | {
46
+ readonly type: "read.settled";
47
+ readonly id: string;
48
+ readonly outcome: ReadOutcome;
49
+ readonly socketClosed: boolean;
50
+ } | {
51
+ readonly type: "write.started";
52
+ readonly id: string;
53
+ } | {
54
+ readonly type: "write.settled";
55
+ readonly id: string;
56
+ readonly end: boolean;
57
+ readonly outcome: WriteOutcome;
58
+ readonly socketClosed: boolean;
59
+ } | {
60
+ readonly type: "tunnel.closed";
61
+ readonly id: string;
62
+ } | {
63
+ readonly type: "disposed";
64
+ };
65
+ export type SocketEffect = {
66
+ readonly type: "destroy-socket";
67
+ readonly id: string;
68
+ };
69
+ export type Transition = {
70
+ readonly state: State;
71
+ readonly effects: readonly SocketEffect[];
72
+ };
73
+ export declare const initial: () => State;
74
+ export declare function transition(state: State, event: Event): Transition;
75
+ /** Why `open` cannot claim a slot, if it cannot. */
76
+ export declare function openError(state: State): Error | undefined;
77
+ /** Whether `id` is connected and still in the pool. */
78
+ export declare function isOpen(state: State, id: string): boolean;
79
+ /** Why `read` cannot start on `id`, if it cannot. */
80
+ export declare function readError(state: State, id: string): Error | undefined;
81
+ /** Why `write` cannot start on `id`, if it cannot. */
82
+ export declare function writeError(state: State, id: string): Error | undefined;
83
+ /** The socket error recorded for `id`. */
84
+ export declare function socketError(state: State, id: string): Error | undefined;
@@ -0,0 +1,127 @@
1
+ export * as BrowserTunnelMachine from "./tunnel-machine.js";
2
+ // The server end of one desktop attachment's TCP tunnels. A tunnel leaves the pool when the desktop
3
+ // closes it or once both directions are finished, so a lost `tunnel.close` cannot hold a slot forever.
4
+ export const MAX_TUNNELS = 64;
5
+ export const initial = () => ({ status: "active", entries: new Map() });
6
+ const DISPOSED = { status: "disposed" };
7
+ export function transition(state, event) {
8
+ if (state.status === "disposed") {
9
+ // A connect that lands after disposal still owns a socket.
10
+ if (event.type === "open.connected" || event.type === "open.failed")
11
+ return destroy(state, event.id);
12
+ return unchanged(state);
13
+ }
14
+ if (event.type === "disposed")
15
+ return { state: DISPOSED, effects: Array.from(state.entries.keys(), (id) => ({ type: "destroy-socket", id })) };
16
+ const entry = state.entries.get(event.id);
17
+ switch (event.type) {
18
+ case "open.started":
19
+ if (entry || state.entries.size >= MAX_TUNNELS)
20
+ return unchanged(state);
21
+ return { state: put(state, event.id, { status: "connecting" }), effects: [] };
22
+ case "open.connected":
23
+ // Closed while connecting: the socket has no slot to return to.
24
+ if (!entry)
25
+ return destroy(state, event.id);
26
+ if (entry.status !== "connecting")
27
+ return unchanged(state);
28
+ return {
29
+ state: put(state, event.id, {
30
+ status: "open",
31
+ reading: false,
32
+ writing: false,
33
+ readEnded: false,
34
+ writeEnded: false,
35
+ error: undefined,
36
+ }),
37
+ effects: [],
38
+ };
39
+ case "open.failed":
40
+ case "tunnel.closed":
41
+ return entry ? evict(state, event.id) : unchanged(state);
42
+ }
43
+ if (entry?.status !== "open")
44
+ return unchanged(state);
45
+ switch (event.type) {
46
+ case "socket.errored":
47
+ if (entry.error)
48
+ return unchanged(state);
49
+ return { state: put(state, event.id, { ...entry, error: event.error }), effects: [] };
50
+ case "socket.closed":
51
+ if (entry.reading || entry.writing || !entry.readEnded)
52
+ return unchanged(state);
53
+ return evict(state, event.id);
54
+ case "read.started":
55
+ if (entry.reading)
56
+ return unchanged(state);
57
+ return { state: put(state, event.id, { ...entry, reading: true }), effects: [] };
58
+ case "read.settled": {
59
+ if (!entry.reading)
60
+ return unchanged(state);
61
+ const readEnded = entry.readEnded || event.outcome === "eof";
62
+ if (event.outcome === "error" || (readEnded && (entry.writeEnded || event.socketClosed)))
63
+ return evict(state, event.id);
64
+ return { state: put(state, event.id, { ...entry, reading: false, readEnded }), effects: [] };
65
+ }
66
+ case "write.started":
67
+ if (entry.writing || entry.writeEnded || entry.error)
68
+ return unchanged(state);
69
+ return { state: put(state, event.id, { ...entry, writing: true }), effects: [] };
70
+ case "write.settled": {
71
+ if (!entry.writing)
72
+ return unchanged(state);
73
+ const writeEnded = entry.writeEnded || (event.outcome === "ok" && event.end);
74
+ if (event.outcome === "interrupted" || (entry.readEnded && (writeEnded || event.socketClosed)))
75
+ return evict(state, event.id);
76
+ return { state: put(state, event.id, { ...entry, writing: false, writeEnded }), effects: [] };
77
+ }
78
+ }
79
+ }
80
+ /** Why `open` cannot claim a slot, if it cannot. */
81
+ export function openError(state) {
82
+ if (state.status === "disposed")
83
+ return new Error("Browser attachment is closed.");
84
+ if (state.entries.size >= MAX_TUNNELS)
85
+ return new Error(`Browser attachment has reached its ${MAX_TUNNELS}-connection limit.`);
86
+ }
87
+ /** Whether `id` is connected and still in the pool. */
88
+ export function isOpen(state, id) {
89
+ return state.status === "active" && state.entries.get(id)?.status === "open";
90
+ }
91
+ /** Why `read` cannot start on `id`, if it cannot. */
92
+ export function readError(state, id) {
93
+ const entry = state.status === "active" ? state.entries.get(id) : undefined;
94
+ if (entry?.status !== "open")
95
+ return new Error("Browser tunnel is closed or unknown.");
96
+ if (entry.reading)
97
+ return new Error("Only one read may be pending per browser tunnel.");
98
+ }
99
+ /** Why `write` cannot start on `id`, if it cannot. */
100
+ export function writeError(state, id) {
101
+ const entry = state.status === "active" ? state.entries.get(id) : undefined;
102
+ if (entry?.status !== "open" || entry.writeEnded)
103
+ return new Error("Browser tunnel is not writable.");
104
+ if (entry.error)
105
+ return entry.error;
106
+ if (entry.writing)
107
+ return new Error("Only one write may be pending per browser tunnel.");
108
+ }
109
+ /** The socket error recorded for `id`. */
110
+ export function socketError(state, id) {
111
+ const entry = state.status === "active" ? state.entries.get(id) : undefined;
112
+ return entry?.status === "open" ? entry.error : undefined;
113
+ }
114
+ function put(state, id, entry) {
115
+ return { status: "active", entries: new Map(state.entries).set(id, entry) };
116
+ }
117
+ function evict(state, id) {
118
+ const entries = new Map(state.entries);
119
+ entries.delete(id);
120
+ return { state: { status: "active", entries }, effects: [{ type: "destroy-socket", id }] };
121
+ }
122
+ function destroy(state, id) {
123
+ return { state, effects: [{ type: "destroy-socket", id }] };
124
+ }
125
+ function unchanged(state) {
126
+ return { state, effects: [] };
127
+ }
package/dist/tunnel.js CHANGED
@@ -1,125 +1,146 @@
1
1
  export * as BrowserTunnel from "./tunnel.js";
2
- import { Effect } from "effect";
2
+ import { Effect, Exit } from "effect";
3
3
  import { Browser } from "./rpc.js";
4
+ import { BrowserTunnelMachine } from "./tunnel-machine.js";
4
5
  // One instance belongs to one desktop attachment. Socket buffers provide
5
6
  // backpressure; reads never collect an unbounded stream in application memory.
6
7
  export function make() {
8
+ let state = BrowserTunnelMachine.initial();
7
9
  const sockets = new Map();
8
- let disposed = false;
9
- const close = (id) => Effect.sync(() => {
10
- sockets.get(id)?.socket.destroy();
11
- sockets.delete(id);
12
- });
10
+ const dispatch = (event) => {
11
+ const next = BrowserTunnelMachine.transition(state, event);
12
+ state = next.state;
13
+ next.effects.forEach((effect) => {
14
+ sockets.get(effect.id)?.destroy();
15
+ sockets.delete(effect.id);
16
+ });
17
+ };
13
18
  return {
14
19
  open: Effect.fn("BrowserTunnel.open")(function* (target) {
15
- const { createConnection } = yield* Effect.promise(() => import("node:net"));
16
- if (disposed)
17
- return yield* Effect.fail(new Error("Browser attachment is closed."));
18
- if (sockets.size >= 64)
19
- return yield* Effect.fail(new Error("Browser attachment has reached its 64-connection limit."));
20
- const socket = yield* Effect.try({
21
- try: () => createConnection({ ...target, allowHalfOpen: true }),
22
- catch: (cause) => (cause instanceof Error ? cause : new Error(String(cause))),
23
- });
20
+ const rejected = BrowserTunnelMachine.openError(state);
21
+ if (rejected)
22
+ return yield* Effect.fail(rejected);
24
23
  const id = crypto.randomUUID();
25
- const entry = { socket, reading: false, error: undefined };
26
- socket.on("error", (error) => {
27
- entry.error = error;
28
- });
29
- sockets.set(id, entry);
30
- yield* Effect.callback((resume) => {
31
- const connected = () => {
32
- cleanup();
33
- socket.setNoDelay(true);
34
- resume(Effect.void);
35
- };
36
- const failed = (error) => {
37
- cleanup();
38
- resume(Effect.fail(error));
39
- };
40
- const closed = () => failed(entry.error ?? new Error("Browser tunnel closed while connecting."));
41
- const cleanup = () => {
42
- socket.off("connect", connected);
43
- socket.off("error", failed);
44
- socket.off("close", closed);
45
- };
46
- socket.once("connect", connected);
47
- socket.once("error", failed);
48
- socket.once("close", closed);
49
- if (socket.destroyed)
50
- closed();
51
- if (!socket.destroyed && !socket.connecting)
52
- connected();
53
- return Effect.sync(cleanup);
54
- }).pipe(Effect.timeoutOrElse({
55
- duration: "10 seconds",
56
- orElse: () => Effect.fail(new Error("Browser tunnel target connection timed out.")),
57
- }), Effect.onError(() => close(id)));
58
- return id;
24
+ dispatch({ type: "open.started", id });
25
+ return yield* Effect.gen(function* () {
26
+ const { createConnection } = yield* Effect.promise(() => import("node:net"));
27
+ const socket = yield* Effect.try({
28
+ try: () => createConnection({ ...target, allowHalfOpen: true }),
29
+ catch: (cause) => (cause instanceof Error ? cause : new Error(String(cause))),
30
+ });
31
+ sockets.set(id, socket);
32
+ socket.on("error", (error) => dispatch({ type: "socket.errored", id, error }));
33
+ socket.once("close", () => dispatch({ type: "socket.closed", id }));
34
+ yield* Effect.callback((resume) => {
35
+ const connected = () => {
36
+ cleanup();
37
+ socket.setNoDelay(true);
38
+ resume(Effect.void);
39
+ };
40
+ const failed = (error) => {
41
+ cleanup();
42
+ resume(Effect.fail(error));
43
+ };
44
+ const closed = () => failed(new Error("Browser tunnel closed while connecting."));
45
+ const cleanup = () => {
46
+ socket.off("connect", connected);
47
+ socket.off("error", failed);
48
+ socket.off("close", closed);
49
+ };
50
+ socket.once("connect", connected);
51
+ socket.once("error", failed);
52
+ socket.once("close", closed);
53
+ if (socket.destroyed)
54
+ closed();
55
+ if (!socket.destroyed && !socket.connecting)
56
+ connected();
57
+ return Effect.sync(cleanup);
58
+ }).pipe(Effect.timeoutOrElse({
59
+ duration: "10 seconds",
60
+ orElse: () => Effect.fail(new Error("Browser tunnel target connection timed out.")),
61
+ }));
62
+ dispatch({ type: "open.connected", id });
63
+ // Disposal or `close(id)` can win the race with the connect.
64
+ if (!BrowserTunnelMachine.isOpen(state, id))
65
+ return yield* Effect.fail(new Error("Browser attachment is closed."));
66
+ return id;
67
+ }).pipe(Effect.onError(() => Effect.sync(() => dispatch({ type: "open.failed", id }))));
59
68
  }),
60
69
  read: Effect.fn("BrowserTunnel.read")(function* (id) {
61
- const entry = sockets.get(id);
62
- if (!entry)
63
- return yield* Effect.fail(new Error("Browser tunnel is closed or unknown."));
64
- if (entry.reading)
65
- return yield* Effect.fail(new Error("Only one read may be pending per browser tunnel."));
66
- entry.reading = true;
70
+ const socket = sockets.get(id);
71
+ const rejected = BrowserTunnelMachine.readError(state, id);
72
+ if (rejected || !socket)
73
+ return yield* Effect.fail(rejected ?? new Error("Browser tunnel is closed or unknown."));
74
+ dispatch({ type: "read.started", id });
75
+ let outcome = "interrupted";
67
76
  return yield* Effect.callback((resume) => {
68
- const done = (value) => {
77
+ const done = (value, result) => {
69
78
  cleanup();
79
+ outcome = result;
70
80
  resume(value);
71
81
  };
72
82
  const pull = () => {
73
- if (entry.error)
74
- return done(Effect.fail(entry.error));
75
- const size = Math.min(entry.socket.readableLength, Browser.TUNNEL_CHUNK_BYTES);
83
+ const error = BrowserTunnelMachine.socketError(state, id);
84
+ if (error)
85
+ return done(Effect.fail(error), "error");
86
+ const size = Math.min(socket.readableLength, Browser.TUNNEL_CHUNK_BYTES);
76
87
  if (size > 0) {
77
- const data = entry.socket.read(size);
78
- return done(Effect.succeed({ data, eof: false }));
88
+ const data = socket.read(size);
89
+ return done(Effect.succeed({ data, eof: false }), "chunk");
79
90
  }
80
- if (entry.socket.readableEnded || entry.socket.destroyed)
81
- done(Effect.succeed({ data: new Uint8Array(), eof: true }));
91
+ if (socket.readableEnded || socket.destroyed)
92
+ done(Effect.succeed({ data: new Uint8Array(), eof: true }), "eof");
82
93
  };
83
94
  const cleanup = () => {
84
- entry.reading = false;
85
- entry.socket.off("readable", pull);
86
- entry.socket.off("end", pull);
87
- entry.socket.off("error", pull);
88
- entry.socket.off("close", pull);
95
+ socket.off("readable", pull);
96
+ socket.off("end", pull);
97
+ socket.off("error", pull);
98
+ socket.off("close", pull);
89
99
  };
90
- entry.socket.on("readable", pull);
91
- entry.socket.on("end", pull);
92
- entry.socket.on("error", pull);
93
- entry.socket.on("close", pull);
100
+ socket.on("readable", pull);
101
+ socket.on("end", pull);
102
+ socket.on("error", pull);
103
+ socket.on("close", pull);
94
104
  pull();
95
105
  return Effect.sync(cleanup);
96
- });
106
+ }).pipe(Effect.ensuring(Effect.sync(() => dispatch({ type: "read.settled", id, outcome, socketClosed: socket.destroyed }))));
97
107
  }),
98
108
  write: Effect.fn("BrowserTunnel.write")(function* (id, data, end = false) {
99
- const entry = sockets.get(id);
100
- if (!entry || entry.socket.destroyed || entry.socket.writableEnded)
101
- return yield* Effect.fail(new Error("Browser tunnel is not writable."));
109
+ const socket = sockets.get(id);
110
+ const rejected = BrowserTunnelMachine.writeError(state, id);
111
+ if (rejected || !socket || socket.destroyed || socket.writableEnded)
112
+ return yield* Effect.fail(rejected ?? new Error("Browser tunnel is not writable."));
113
+ dispatch({ type: "write.started", id });
102
114
  yield* Effect.callback((resume) => {
103
115
  const done = (error) => {
104
- entry.socket.off("error", failed);
116
+ cleanup();
105
117
  resume(error ? Effect.fail(error) : Effect.void);
106
118
  };
107
119
  const failed = (error) => done(error);
108
- entry.socket.once("error", failed);
120
+ // `destroy()` without an error emits neither "finish" nor "error", so a pending end would never settle.
121
+ const closed = () => done(BrowserTunnelMachine.socketError(state, id) ?? new Error("Browser tunnel closed while writing."));
122
+ const cleanup = () => {
123
+ socket.off("error", failed);
124
+ socket.off("close", closed);
125
+ };
126
+ socket.once("error", failed);
127
+ socket.once("close", closed);
109
128
  if (end)
110
- entry.socket.end(data, () => done());
129
+ socket.end(data, done);
111
130
  if (!end)
112
- entry.socket.write(data, done);
113
- return Effect.sync(() => {
114
- entry.socket.off("error", failed);
115
- });
116
- }).pipe(Effect.onInterrupt(() => close(id)));
131
+ socket.write(data, done);
132
+ return Effect.sync(cleanup);
133
+ }).pipe(Effect.onExit((exit) => Effect.sync(() => dispatch({
134
+ type: "write.settled",
135
+ id,
136
+ end,
137
+ outcome: Exit.isSuccess(exit) ? "ok" : Exit.hasInterrupts(exit) ? "interrupted" : "error",
138
+ socketClosed: socket.destroyed,
139
+ }))));
117
140
  }),
118
- close,
141
+ close: (id) => Effect.sync(() => dispatch({ type: "tunnel.closed", id })),
119
142
  dispose() {
120
- disposed = true;
121
- sockets.forEach((entry) => entry.socket.destroy());
122
- sockets.clear();
143
+ dispatch({ type: "disposed" });
123
144
  },
124
145
  };
125
146
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@opencode/plugin-browser",
4
- "version": "2.2.5",
4
+ "version": "2.2.6",
5
5
  "description": "OpenCode's desktop browser plugin",
6
6
  "type": "module",
7
7
  "license": "MIT",
@@ -38,9 +38,9 @@
38
38
  "test": "bun test"
39
39
  },
40
40
  "dependencies": {
41
- "@opencode/plugin": "2.2.5",
42
- "@opencode/schema": "2.2.5",
43
- "effect": "4.0.6"
41
+ "@opencode/plugin": "2.2.6",
42
+ "@opencode/schema": "2.2.6",
43
+ "effect": "4.1.0"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@tsconfig/bun": "1.0.9",