@octane-xplat/devtools 0.0.0-nightly.g1b95ef7
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/README.md +5 -0
- package/dist/app.d.ts +28 -0
- package/dist/app.js +66 -0
- package/dist/backend.d.ts +31 -0
- package/dist/backend.js +182 -0
- package/dist/client.d.ts +49 -0
- package/dist/client.js +168 -0
- package/dist/host/desktop.d.ts +15 -0
- package/dist/host/desktop.js +133 -0
- package/dist/host/panels.d.ts +14 -0
- package/dist/host/panels.js +156 -0
- package/dist/host/records.d.ts +5 -0
- package/dist/host/records.js +46 -0
- package/dist/host/server.d.ts +8 -0
- package/dist/host/server.js +67 -0
- package/dist/host/web.d.ts +5 -0
- package/dist/host/web.js +74 -0
- package/dist/macos/index.macos.d.ts +3 -0
- package/dist/macos/index.macos.js +5 -0
- package/dist/macos/servers.macos.d.ts +4 -0
- package/dist/macos/servers.macos.js +57 -0
- package/dist/macos/socket.macos.d.ts +2 -0
- package/dist/macos/socket.macos.js +152 -0
- package/dist/macos/ws-frames.d.ts +24 -0
- package/dist/macos/ws-frames.js +205 -0
- package/dist/node.d.ts +12 -0
- package/dist/node.js +51 -0
- package/dist/record.d.ts +9 -0
- package/dist/record.js +38 -0
- package/dist/types.d.ts +128 -0
- package/dist/types.js +25 -0
- package/package.json +88 -0
- package/src/app.ts +105 -0
- package/src/assets.d.ts +2 -0
- package/src/backend.ts +278 -0
- package/src/client.ts +248 -0
- package/src/desktop/Shell.macos.tsrx +158 -0
- package/src/desktop/Shell.ts +16 -0
- package/src/desktop/index.macos.ts +7 -0
- package/src/desktop/main.macos.ts +130 -0
- package/src/desktop/panels.d.ts +10 -0
- package/src/desktop/target.ts +34 -0
- package/src/host/desktop.ts +176 -0
- package/src/host/panels.ts +193 -0
- package/src/host/records.ts +62 -0
- package/src/host/server.ts +92 -0
- package/src/host/web.ts +68 -0
- package/src/macos/index.macos.ts +7 -0
- package/src/macos/servers.macos.ts +83 -0
- package/src/macos/socket.macos.ts +218 -0
- package/src/macos/ws-frames.ts +249 -0
- package/src/node.ts +60 -0
- package/src/panel/App.tsrx +288 -0
- package/src/panel/index.html +12 -0
- package/src/panel/index.ts +24 -0
- package/src/panel/main.tsrx +2 -0
- package/src/panel/main.web.tsrx +89 -0
- package/src/panel/model.ts +216 -0
- package/src/panel/panel.css +189 -0
- package/src/panel/plugin.ts +65 -0
- package/src/panel/styles.ts +5 -0
- package/src/panel/tsconfig.browser.json +9 -0
- package/src/record.ts +66 -0
- package/src/types.ts +129 -0
package/src/backend.ts
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
// Node-side devtools backend: runs inside the per-target engine process
|
|
2
|
+
// (usually a Vite plugin) and authenticates to the Go dev hub with the
|
|
3
|
+
// XPLAT_DEV_BACKEND grant. Reconnects with bounded backoff on unexpected
|
|
4
|
+
// closes; a same-key 'replaced' close or close() ends it permanently.
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
devSocket,
|
|
8
|
+
parseHubFrame,
|
|
9
|
+
type ChannelError,
|
|
10
|
+
type DevSocket,
|
|
11
|
+
type SessionSnapshot,
|
|
12
|
+
} from './types.ts'
|
|
13
|
+
|
|
14
|
+
export type { ChannelError, SessionSnapshot } from './types.ts'
|
|
15
|
+
|
|
16
|
+
export interface DevBackendMessage<In> {
|
|
17
|
+
from: 'app' | 'panel'
|
|
18
|
+
session?: string
|
|
19
|
+
panel?: string
|
|
20
|
+
data: In
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface DevBackend<In, Out> {
|
|
24
|
+
readonly state: 'connecting' | 'live' | 'closed'
|
|
25
|
+
readonly targetId: string
|
|
26
|
+
sessions(): readonly SessionSnapshot[]
|
|
27
|
+
/** '*' reaches this target's live sessions that declared the channel. */
|
|
28
|
+
sendToApp(session: string | '*', data: Out): boolean
|
|
29
|
+
sendToPanels(data: Out, panel?: string): boolean
|
|
30
|
+
subscribe(
|
|
31
|
+
listener: (
|
|
32
|
+
message:
|
|
33
|
+
| { from: 'app'; session: string; data: In }
|
|
34
|
+
| { from: 'panel'; panel: string; data: In },
|
|
35
|
+
) => void,
|
|
36
|
+
): () => void
|
|
37
|
+
onSessions(listener: (sessions: readonly SessionSnapshot[]) => void): () => void
|
|
38
|
+
onError(listener: (error: ChannelError) => void): () => void
|
|
39
|
+
close(): void
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface BackendGrant {
|
|
43
|
+
protocol: string
|
|
44
|
+
version: number
|
|
45
|
+
socket: string
|
|
46
|
+
instanceId: string
|
|
47
|
+
targetId: string
|
|
48
|
+
token: string
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function parseBackendGrant(source: string, channel: string): BackendGrant {
|
|
52
|
+
if (source.length > 8192) {
|
|
53
|
+
throw new Error('Invalid devtools backend configuration')
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let grant: BackendGrant
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
grant = JSON.parse(source) as BackendGrant
|
|
60
|
+
} catch {
|
|
61
|
+
throw new Error('Invalid devtools backend JSON')
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// The macOS JSC host has no global `URL`; the grant only ever carries the
|
|
65
|
+
// strict loopback `/backend` socket shape, so validate it directly.
|
|
66
|
+
const endpoint = /^ws:\/\/127\.0\.0\.1:\d{1,5}\/backend$/.test(grant.socket)
|
|
67
|
+
|
|
68
|
+
if (
|
|
69
|
+
grant.protocol !== 'octane-devtools-backend' ||
|
|
70
|
+
grant.version !== 1 ||
|
|
71
|
+
!endpoint ||
|
|
72
|
+
!/^[a-f0-9]{32}$/.test(grant.instanceId) ||
|
|
73
|
+
!/^[a-f0-9]{64}$/.test(grant.token) ||
|
|
74
|
+
typeof grant.targetId !== 'string' ||
|
|
75
|
+
!grant.targetId ||
|
|
76
|
+
!/^[a-z][a-z0-9-]{0,31}$/.test(channel)
|
|
77
|
+
) {
|
|
78
|
+
throw new Error('Invalid devtools backend identity')
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return grant
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const RECONNECT_MS = [200, 500, 1000, 2000, 4000, 8000]
|
|
85
|
+
const REPLACED_CLOSE = 1008
|
|
86
|
+
|
|
87
|
+
// Node's `process.env`, reached through globalThis so host programs that sweep
|
|
88
|
+
// this source never need the full @types/node process declaration.
|
|
89
|
+
interface DevGlobalEnv {
|
|
90
|
+
process?: { env?: Record<string, string | undefined> }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function connectDevBackend<In = unknown, Out = unknown>(
|
|
94
|
+
channel: string,
|
|
95
|
+
options?: { env?: Record<string, string | undefined> },
|
|
96
|
+
): DevBackend<In, Out> | null {
|
|
97
|
+
const source = (options?.env ?? (globalThis as DevGlobalEnv).process?.env ?? {}).XPLAT_DEV_BACKEND
|
|
98
|
+
|
|
99
|
+
if (!source) {
|
|
100
|
+
return null
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const grant = parseBackendGrant(source, channel)
|
|
104
|
+
|
|
105
|
+
let state: 'connecting' | 'live' | 'closed' = 'connecting'
|
|
106
|
+
let socket: DevSocket | undefined
|
|
107
|
+
let snapshot: readonly SessionSnapshot[] = []
|
|
108
|
+
let attempts = 0
|
|
109
|
+
let reconnect: ReturnType<typeof setTimeout> | undefined
|
|
110
|
+
const listeners = new Set<
|
|
111
|
+
(
|
|
112
|
+
message:
|
|
113
|
+
| { from: 'app'; session: string; data: In }
|
|
114
|
+
| { from: 'panel'; panel: string; data: In },
|
|
115
|
+
) => void
|
|
116
|
+
>()
|
|
117
|
+
|
|
118
|
+
const sessionListeners = new Set<(sessions: readonly SessionSnapshot[]) => void>()
|
|
119
|
+
const errorListeners = new Set<(error: ChannelError) => void>()
|
|
120
|
+
|
|
121
|
+
const emitSessions = (sessions: SessionSnapshot[]) => {
|
|
122
|
+
snapshot = sessions
|
|
123
|
+
for (const listener of sessionListeners) {
|
|
124
|
+
listener(snapshot)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const connect = () => {
|
|
129
|
+
if (state === 'closed') {
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
state = 'connecting'
|
|
134
|
+
|
|
135
|
+
const next = devSocket(grant.socket)
|
|
136
|
+
|
|
137
|
+
if (!next) {
|
|
138
|
+
state = 'closed'
|
|
139
|
+
return
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
socket = next
|
|
143
|
+
next.onopen = () => {
|
|
144
|
+
if (socket !== next || state === 'closed') {
|
|
145
|
+
next.close()
|
|
146
|
+
return
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
next.send(
|
|
150
|
+
JSON.stringify({
|
|
151
|
+
protocol: 'octane-devtools-backend',
|
|
152
|
+
version: 1,
|
|
153
|
+
instanceId: grant.instanceId,
|
|
154
|
+
targetId: grant.targetId,
|
|
155
|
+
token: grant.token,
|
|
156
|
+
channel,
|
|
157
|
+
}),
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
next.onmessage = (event) => {
|
|
162
|
+
if (socket !== next || typeof event.data !== 'string') {
|
|
163
|
+
return
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const frame = parseHubFrame(event.data)
|
|
167
|
+
|
|
168
|
+
if (!frame) {
|
|
169
|
+
return
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (frame.type === 'ready' || frame.type === 'sessions') {
|
|
173
|
+
emitSessions(Array.isArray(frame.sessions) ? frame.sessions : [])
|
|
174
|
+
if (frame.type === 'ready') {
|
|
175
|
+
state = 'live'
|
|
176
|
+
attempts = 0
|
|
177
|
+
}
|
|
178
|
+
} else if (
|
|
179
|
+
frame.type === 'channel' &&
|
|
180
|
+
frame.from === 'app' &&
|
|
181
|
+
typeof frame.session === 'string'
|
|
182
|
+
) {
|
|
183
|
+
// Channel payloads are typed by the channel's own protocol, so the
|
|
184
|
+
// single `as In` here is the documented boundary for this module.
|
|
185
|
+
for (const listener of listeners) {
|
|
186
|
+
listener({ from: 'app', session: frame.session, data: frame.data as In })
|
|
187
|
+
}
|
|
188
|
+
} else if (
|
|
189
|
+
frame.type === 'channel' &&
|
|
190
|
+
frame.from === 'panel' &&
|
|
191
|
+
typeof frame.panel === 'string'
|
|
192
|
+
) {
|
|
193
|
+
for (const listener of listeners) {
|
|
194
|
+
listener({ from: 'panel', panel: frame.panel, data: frame.data as In })
|
|
195
|
+
}
|
|
196
|
+
} else if (frame.type === 'channel-error' && typeof frame.code === 'string') {
|
|
197
|
+
const failure: ChannelError = {
|
|
198
|
+
type: 'channel-error',
|
|
199
|
+
code: frame.code,
|
|
200
|
+
...(frame.channel ? { channel: frame.channel } : {}),
|
|
201
|
+
detail: typeof frame.detail === 'string' ? frame.detail : '',
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
for (const listener of errorListeners) {
|
|
205
|
+
listener(failure)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
next.onerror = () => {}
|
|
211
|
+
next.onclose = (event) => {
|
|
212
|
+
if (socket !== next) {
|
|
213
|
+
return
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
socket = undefined
|
|
217
|
+
if (state === 'closed' || event.code === REPLACED_CLOSE) {
|
|
218
|
+
state = 'closed'
|
|
219
|
+
return
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
reconnect = setTimeout(connect, RECONNECT_MS[Math.min(attempts++, RECONNECT_MS.length - 1)])
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
connect()
|
|
227
|
+
|
|
228
|
+
const send = (frame: Record<string, unknown>): boolean => {
|
|
229
|
+
if (state !== 'live' || socket?.readyState !== 1) {
|
|
230
|
+
return false
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
socket.send(JSON.stringify({ type: 'channel', ...frame }))
|
|
235
|
+
return true
|
|
236
|
+
} catch {
|
|
237
|
+
return false
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return {
|
|
242
|
+
get state() {
|
|
243
|
+
return state
|
|
244
|
+
},
|
|
245
|
+
get targetId() {
|
|
246
|
+
return grant.targetId
|
|
247
|
+
},
|
|
248
|
+
sessions: () => snapshot,
|
|
249
|
+
sendToApp: (session, data) => send({ to: 'app', session, data }),
|
|
250
|
+
sendToPanels: (data, panel) =>
|
|
251
|
+
send(panel === undefined ? { to: 'panels', data } : { to: 'panels', panel, data }),
|
|
252
|
+
subscribe(listener) {
|
|
253
|
+
listeners.add(listener)
|
|
254
|
+
return () => listeners.delete(listener)
|
|
255
|
+
},
|
|
256
|
+
onSessions(listener) {
|
|
257
|
+
sessionListeners.add(listener)
|
|
258
|
+
return () => sessionListeners.delete(listener)
|
|
259
|
+
},
|
|
260
|
+
onError(listener) {
|
|
261
|
+
errorListeners.add(listener)
|
|
262
|
+
return () => errorListeners.delete(listener)
|
|
263
|
+
},
|
|
264
|
+
close() {
|
|
265
|
+
if (state === 'closed') {
|
|
266
|
+
return
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
state = 'closed'
|
|
270
|
+
if (reconnect) {
|
|
271
|
+
clearTimeout(reconnect)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
socket?.close()
|
|
275
|
+
socket = undefined
|
|
276
|
+
},
|
|
277
|
+
}
|
|
278
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
// Devtools panel client: the devtools app authenticates one connection per
|
|
2
|
+
// discovery record with the per-server devtools token, receives a hub-assigned
|
|
3
|
+
// panel id, and routes channel frames to any session or backend.
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
devSocket,
|
|
7
|
+
parseHubFrame,
|
|
8
|
+
type BackendRef,
|
|
9
|
+
type ChannelError,
|
|
10
|
+
type DevSocket,
|
|
11
|
+
type SessionSnapshot,
|
|
12
|
+
} from './types.ts'
|
|
13
|
+
|
|
14
|
+
export type { BackendRef, ChannelError, SessionSnapshot } from './types.ts'
|
|
15
|
+
|
|
16
|
+
/** The fields a panel needs from the dev server's discovery record. */
|
|
17
|
+
export interface DevtoolsServerRecord {
|
|
18
|
+
endpoint: string
|
|
19
|
+
instanceId: string
|
|
20
|
+
devtoolsToken: string
|
|
21
|
+
appRoot: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface DevtoolsMessage {
|
|
25
|
+
channel: string
|
|
26
|
+
from: 'app' | 'backend'
|
|
27
|
+
session?: string
|
|
28
|
+
targetId?: string
|
|
29
|
+
data: unknown
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface DevtoolsConnection {
|
|
33
|
+
readonly state: 'connecting' | 'live' | 'closed'
|
|
34
|
+
/** Hub-assigned panel id, set by the `ready` frame. */
|
|
35
|
+
readonly panel: string | null
|
|
36
|
+
sessions(): readonly SessionSnapshot[]
|
|
37
|
+
backends(): readonly BackendRef[]
|
|
38
|
+
sendToApp(channel: string, session: string | '*', data: unknown): boolean
|
|
39
|
+
sendToBackend(channel: string, targetId: string, data: unknown): boolean
|
|
40
|
+
subscribe(
|
|
41
|
+
listener: (
|
|
42
|
+
message:
|
|
43
|
+
| { channel: string; from: 'app'; session: string; data: unknown }
|
|
44
|
+
| { channel: string; from: 'backend'; targetId: string; data: unknown },
|
|
45
|
+
) => void,
|
|
46
|
+
): () => void
|
|
47
|
+
onSessions(listener: (sessions: readonly SessionSnapshot[]) => void): () => void
|
|
48
|
+
onBackends(listener: (backends: readonly BackendRef[]) => void): () => void
|
|
49
|
+
onError(listener: (error: ChannelError) => void): () => void
|
|
50
|
+
/** Fires on every connection-state transition ('live' on the ready frame,
|
|
51
|
+
* 'closed' when the socket ends or the factory yields no socket). */
|
|
52
|
+
onState(listener: (state: 'connecting' | 'live' | 'closed') => void): () => void
|
|
53
|
+
close(): void
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface ConnectDevtoolsOptions {
|
|
57
|
+
/** Overrides the socket factory — the macOS JSC host has no global
|
|
58
|
+
* `WebSocket`, so native panels inject their transport here. */
|
|
59
|
+
socket?: (url: string) => DevSocket | undefined
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function connectDevtools(
|
|
63
|
+
record: DevtoolsServerRecord,
|
|
64
|
+
options: ConnectDevtoolsOptions = {},
|
|
65
|
+
): DevtoolsConnection {
|
|
66
|
+
// The macOS JSC host has no global `URL`; records only ever carry the
|
|
67
|
+
// strict loopback `/rpc` shape, so validate it directly.
|
|
68
|
+
const endpoint = /^http:\/\/(127\.0\.0\.1:\d{1,5})\/rpc$/.exec(record.endpoint)
|
|
69
|
+
|
|
70
|
+
if (
|
|
71
|
+
!endpoint ||
|
|
72
|
+
!/^[a-f0-9]{32}$/.test(record.instanceId) ||
|
|
73
|
+
!/^[a-f0-9]{64}$/.test(record.devtoolsToken)
|
|
74
|
+
) {
|
|
75
|
+
throw new Error('Invalid devtools server record')
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let state: 'connecting' | 'live' | 'closed' = 'connecting'
|
|
79
|
+
let panelId: string | null = null
|
|
80
|
+
let socket: DevSocket | undefined
|
|
81
|
+
let snapshot: readonly SessionSnapshot[] = []
|
|
82
|
+
let backendList: readonly BackendRef[] = []
|
|
83
|
+
const listeners = new Set<
|
|
84
|
+
(
|
|
85
|
+
message:
|
|
86
|
+
| { channel: string; from: 'app'; session: string; data: unknown }
|
|
87
|
+
| { channel: string; from: 'backend'; targetId: string; data: unknown },
|
|
88
|
+
) => void
|
|
89
|
+
>()
|
|
90
|
+
|
|
91
|
+
const sessionListeners = new Set<(sessions: readonly SessionSnapshot[]) => void>()
|
|
92
|
+
const backendListeners = new Set<(backends: readonly BackendRef[]) => void>()
|
|
93
|
+
const errorListeners = new Set<(error: ChannelError) => void>()
|
|
94
|
+
const stateListeners = new Set<(state: 'connecting' | 'live' | 'closed') => void>()
|
|
95
|
+
|
|
96
|
+
const setState = (next: 'connecting' | 'live' | 'closed') => {
|
|
97
|
+
if (state === next) {
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
state = next
|
|
102
|
+
for (const listener of stateListeners) {
|
|
103
|
+
listener(next)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const ws = `ws://${endpoint[1]}/devtools`
|
|
108
|
+
const next = (options.socket ?? devSocket)(ws)
|
|
109
|
+
|
|
110
|
+
if (!next) {
|
|
111
|
+
setState('closed')
|
|
112
|
+
} else {
|
|
113
|
+
socket = next
|
|
114
|
+
next.onopen = () => {
|
|
115
|
+
next.send(
|
|
116
|
+
JSON.stringify({
|
|
117
|
+
protocol: 'octane-devtools',
|
|
118
|
+
version: 1,
|
|
119
|
+
instanceId: record.instanceId,
|
|
120
|
+
token: record.devtoolsToken,
|
|
121
|
+
}),
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
next.onmessage = (event) => {
|
|
126
|
+
if (socket !== next || typeof event.data !== 'string') {
|
|
127
|
+
return
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const frame = parseHubFrame(event.data)
|
|
131
|
+
|
|
132
|
+
if (!frame) {
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (frame.type === 'ready') {
|
|
137
|
+
panelId = typeof frame.panel === 'string' ? frame.panel : null
|
|
138
|
+
snapshot = Array.isArray(frame.sessions) ? frame.sessions : []
|
|
139
|
+
backendList = Array.isArray(frame.backends) ? frame.backends : []
|
|
140
|
+
setState('live')
|
|
141
|
+
} else if (frame.type === 'sessions' && Array.isArray(frame.sessions)) {
|
|
142
|
+
snapshot = frame.sessions
|
|
143
|
+
for (const listener of sessionListeners) {
|
|
144
|
+
listener(snapshot)
|
|
145
|
+
}
|
|
146
|
+
} else if (frame.type === 'backends' && Array.isArray(frame.backends)) {
|
|
147
|
+
backendList = frame.backends
|
|
148
|
+
for (const listener of backendListeners) {
|
|
149
|
+
listener(backendList)
|
|
150
|
+
}
|
|
151
|
+
} else if (frame.type === 'channel' && typeof frame.channel === 'string') {
|
|
152
|
+
if (frame.from === 'app' && typeof frame.session === 'string') {
|
|
153
|
+
for (const listener of listeners) {
|
|
154
|
+
listener({
|
|
155
|
+
channel: frame.channel,
|
|
156
|
+
from: 'app',
|
|
157
|
+
session: frame.session,
|
|
158
|
+
data: frame.data,
|
|
159
|
+
})
|
|
160
|
+
}
|
|
161
|
+
} else if (frame.from === 'backend' && typeof frame.targetId === 'string') {
|
|
162
|
+
for (const listener of listeners) {
|
|
163
|
+
listener({
|
|
164
|
+
channel: frame.channel,
|
|
165
|
+
from: 'backend',
|
|
166
|
+
targetId: frame.targetId,
|
|
167
|
+
data: frame.data,
|
|
168
|
+
})
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
} else if (frame.type === 'channel-error' && typeof frame.code === 'string') {
|
|
172
|
+
const failure: ChannelError = {
|
|
173
|
+
type: 'channel-error',
|
|
174
|
+
code: frame.code,
|
|
175
|
+
...(frame.channel ? { channel: frame.channel } : {}),
|
|
176
|
+
detail: typeof frame.detail === 'string' ? frame.detail : '',
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
for (const listener of errorListeners) {
|
|
180
|
+
listener(failure)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
next.onerror = () => {}
|
|
186
|
+
next.onclose = () => {
|
|
187
|
+
if (socket === next) {
|
|
188
|
+
socket = undefined
|
|
189
|
+
setState('closed')
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const send = (frame: Record<string, unknown>): boolean => {
|
|
195
|
+
if (state !== 'live' || socket?.readyState !== 1) {
|
|
196
|
+
return false
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
try {
|
|
200
|
+
socket.send(JSON.stringify({ type: 'channel', ...frame }))
|
|
201
|
+
return true
|
|
202
|
+
} catch {
|
|
203
|
+
return false
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return {
|
|
208
|
+
get state() {
|
|
209
|
+
return state
|
|
210
|
+
},
|
|
211
|
+
get panel() {
|
|
212
|
+
return panelId
|
|
213
|
+
},
|
|
214
|
+
sessions: () => snapshot,
|
|
215
|
+
backends: () => backendList,
|
|
216
|
+
sendToApp: (channel, session, data) => send({ channel, to: 'app', session, data }),
|
|
217
|
+
sendToBackend: (channel, targetId, data) => send({ channel, to: 'backend', targetId, data }),
|
|
218
|
+
subscribe(listener) {
|
|
219
|
+
listeners.add(listener)
|
|
220
|
+
return () => listeners.delete(listener)
|
|
221
|
+
},
|
|
222
|
+
onSessions(listener) {
|
|
223
|
+
sessionListeners.add(listener)
|
|
224
|
+
return () => sessionListeners.delete(listener)
|
|
225
|
+
},
|
|
226
|
+
onBackends(listener) {
|
|
227
|
+
backendListeners.add(listener)
|
|
228
|
+
return () => backendListeners.delete(listener)
|
|
229
|
+
},
|
|
230
|
+
onError(listener) {
|
|
231
|
+
errorListeners.add(listener)
|
|
232
|
+
return () => errorListeners.delete(listener)
|
|
233
|
+
},
|
|
234
|
+
onState(listener) {
|
|
235
|
+
stateListeners.add(listener)
|
|
236
|
+
return () => stateListeners.delete(listener)
|
|
237
|
+
},
|
|
238
|
+
close() {
|
|
239
|
+
if (state === 'closed') {
|
|
240
|
+
return
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
setState('closed')
|
|
244
|
+
socket?.close()
|
|
245
|
+
socket = undefined
|
|
246
|
+
},
|
|
247
|
+
}
|
|
248
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/** @jsxImportSource @octane-xplat/jsx */
|
|
2
|
+
// Desktop shell — owns discovery-record polling and the single window's
|
|
3
|
+
// waiting/picking/targeted states. `xplat devtools` passes the app it was
|
|
4
|
+
// launched from as preferredRoot; the apps/devtools harness passes null.
|
|
5
|
+
import '../panel/styles.ts'
|
|
6
|
+
import { useEffect, useRef, useState } from 'octane'
|
|
7
|
+
import { Button, Center, HStack, Pressable, ScrollableArea, Text, VStack } from '@octane-xplat/ui'
|
|
8
|
+
import { listDevtoolsServers, nwDevSocket } from '../macos/index.macos.ts'
|
|
9
|
+
import type { ListedDevtoolsServer } from '../macos/index.macos.ts'
|
|
10
|
+
import { App } from '../panel/index.ts'
|
|
11
|
+
import { autoTarget } from './target.ts'
|
|
12
|
+
import type { DevtoolsShellProps } from './Shell.ts'
|
|
13
|
+
import panelPlugins from 'virtual:octane-devtools-panels'
|
|
14
|
+
|
|
15
|
+
export type { DevtoolsShellProps } from './Shell.ts'
|
|
16
|
+
|
|
17
|
+
const POLL_MS = 2000
|
|
18
|
+
|
|
19
|
+
function basename(path: string): string {
|
|
20
|
+
const parts = path.split('/').filter(Boolean)
|
|
21
|
+
|
|
22
|
+
return parts[parts.length - 1] ?? path
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function Shell(props: DevtoolsShellProps) @{
|
|
26
|
+
const [records, setRecords] = useState<ListedDevtoolsServer[]>([])
|
|
27
|
+
const [target, setTarget] = useState<ListedDevtoolsServer | null>(null)
|
|
28
|
+
// Mirrors of rendered state for the poll/selftest closures.
|
|
29
|
+
const recordsRef = useRef<ListedDevtoolsServer[]>([])
|
|
30
|
+
const targetRef = useRef<ListedDevtoolsServer | null>(null)
|
|
31
|
+
// Auto-targeting applies only until the first target is chosen — auto or
|
|
32
|
+
// by click — or Switch is pressed; after that the user owns selection.
|
|
33
|
+
const autoPick = useRef(true)
|
|
34
|
+
const [onTop, setOnTop] = useState(false)
|
|
35
|
+
|
|
36
|
+
const choose = (record: ListedDevtoolsServer | null) => {
|
|
37
|
+
autoPick.current = false
|
|
38
|
+
targetRef.current = record
|
|
39
|
+
setTarget(record)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
const scan = () => {
|
|
44
|
+
let next: ListedDevtoolsServer[]
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
next = listDevtoolsServers()
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.error('[devtools] record scan failed', error)
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
recordsRef.current = next
|
|
54
|
+
setRecords(next)
|
|
55
|
+
|
|
56
|
+
const current = targetRef.current
|
|
57
|
+
|
|
58
|
+
if (current) {
|
|
59
|
+
// Same key with a new instanceId is a server restart — reconnect the
|
|
60
|
+
// panel in place; a vanished record keeps App mounted on its
|
|
61
|
+
// disconnected state rather than bouncing to the picker.
|
|
62
|
+
const again = next.find((record) => record.key === current.key)
|
|
63
|
+
|
|
64
|
+
if (again && again.instanceId !== current.instanceId) {
|
|
65
|
+
targetRef.current = again
|
|
66
|
+
setTarget(again)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (autoPick.current) {
|
|
73
|
+
const chosen = autoTarget(next, props.preferredRoot)
|
|
74
|
+
|
|
75
|
+
if (chosen) {
|
|
76
|
+
autoPick.current = false
|
|
77
|
+
targetRef.current = chosen
|
|
78
|
+
setTarget(chosen)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
scan()
|
|
84
|
+
|
|
85
|
+
const timer = setInterval(scan, POLL_MS)
|
|
86
|
+
|
|
87
|
+
return () => clearInterval(timer)
|
|
88
|
+
}, [])
|
|
89
|
+
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
props.onTitle?.(target ? `devtools — ${basename(target.appRoot)}` : 'devtools')
|
|
92
|
+
}, [target])
|
|
93
|
+
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
if (props.selftest) {
|
|
96
|
+
props.selftest.pick = (index: number) => {
|
|
97
|
+
const record = recordsRef.current[index]
|
|
98
|
+
|
|
99
|
+
if (record) {
|
|
100
|
+
choose(record)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}, [])
|
|
105
|
+
|
|
106
|
+
<VStack className="dt-root">
|
|
107
|
+
@if (target !== null) {
|
|
108
|
+
const focused = target
|
|
109
|
+
|
|
110
|
+
<>
|
|
111
|
+
<HStack className="dt-target-bar">
|
|
112
|
+
<Text className="dt-target-name">{basename(focused.appRoot)}</Text>
|
|
113
|
+
<HStack className="dt-right">
|
|
114
|
+
@if (props.onKeepOnTop) {
|
|
115
|
+
<Button
|
|
116
|
+
label={onTop ? 'Unpin' : 'Keep on top'}
|
|
117
|
+
onPress={() => {
|
|
118
|
+
const next = !onTop
|
|
119
|
+
|
|
120
|
+
setOnTop(next)
|
|
121
|
+
props.onKeepOnTop?.(next)
|
|
122
|
+
}}
|
|
123
|
+
/>
|
|
124
|
+
}
|
|
125
|
+
<Button label="Switch…" onPress={() => choose(null)} />
|
|
126
|
+
</HStack>
|
|
127
|
+
</HStack>
|
|
128
|
+
<VStack className="dt-targeted">
|
|
129
|
+
<App
|
|
130
|
+
key={`${focused.key}:${focused.instanceId}`}
|
|
131
|
+
record={focused}
|
|
132
|
+
socket={nwDevSocket}
|
|
133
|
+
plugins={panelPlugins}
|
|
134
|
+
initialTab={props.initialTab}
|
|
135
|
+
/>
|
|
136
|
+
</VStack>
|
|
137
|
+
</>
|
|
138
|
+
} @else if (records.length === 0) {
|
|
139
|
+
<Center style={{ flex: 1 }}>
|
|
140
|
+
<Text className="dt-empty">No xplat dev servers running</Text>
|
|
141
|
+
</Center>
|
|
142
|
+
} @else {
|
|
143
|
+
<ScrollableArea className="dt-main">
|
|
144
|
+
<VStack className="dt-content">
|
|
145
|
+
<Text className={['dt-heading', 'dt-heading-first']}>Select a dev server</Text>
|
|
146
|
+
@for (const record of records; key `${record.key}:${record.instanceId}`) {
|
|
147
|
+
<Pressable className="dt-pick-row" onPress={() => choose(record)}>
|
|
148
|
+
<VStack>
|
|
149
|
+
<Text className="dt-pick-name">{basename(record.appRoot)}</Text>
|
|
150
|
+
<Text className="dt-pick-path">{record.appRoot}</Text>
|
|
151
|
+
</VStack>
|
|
152
|
+
</Pressable>
|
|
153
|
+
}
|
|
154
|
+
</VStack>
|
|
155
|
+
</ScrollableArea>
|
|
156
|
+
}
|
|
157
|
+
</VStack>
|
|
158
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Type contract for the desktop devtools shell — the AppKit implementation
|
|
2
|
+
// lives in Shell.macos.tsrx.
|
|
3
|
+
export interface DevtoolsShellProps {
|
|
4
|
+
/** The selected project root `xplat devtools` ran from; null in the harness. */
|
|
5
|
+
preferredRoot: string | null
|
|
6
|
+
/** Tab to show on mount (DEVTOOLS_SELFTEST_TAB). */
|
|
7
|
+
initialTab?: string
|
|
8
|
+
/** Toggle the main window's level (keep-on-top); absent → no toggle shown. */
|
|
9
|
+
onKeepOnTop?: (onTop: boolean) => void
|
|
10
|
+
/** Main-window title updates: `devtools` until a server is targeted. */
|
|
11
|
+
onTitle?: (title: string) => void
|
|
12
|
+
/** Dev-only selftest hook — receives the row-pick function on mount. */
|
|
13
|
+
selftest?: { pick?: (index: number) => void }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export declare function Shell(props: DevtoolsShellProps): unknown
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Devtools desktop app entry — `@octane-xplat/devtools/desktop`. Imported for
|
|
2
|
+
// its side effects: NSApplication startup and a single window whose Shell
|
|
3
|
+
// polls discovery records and picks (or asks for) the dev server to target.
|
|
4
|
+
// Built by apps/devtools (monorepo harness) and by the generated Vite config
|
|
5
|
+
// behind `xplat devtools`.
|
|
6
|
+
|
|
7
|
+
import './main.macos.ts'
|