@octane-xplat/devtools 0.0.0-nightly.g1b95ef7 → 0.0.1
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 +1 -3
- package/package.json +9 -86
- package/dist/app.d.ts +0 -28
- package/dist/app.js +0 -66
- package/dist/backend.d.ts +0 -31
- package/dist/backend.js +0 -182
- package/dist/client.d.ts +0 -49
- package/dist/client.js +0 -168
- package/dist/host/desktop.d.ts +0 -15
- package/dist/host/desktop.js +0 -133
- package/dist/host/panels.d.ts +0 -14
- package/dist/host/panels.js +0 -156
- package/dist/host/records.d.ts +0 -5
- package/dist/host/records.js +0 -46
- package/dist/host/server.d.ts +0 -8
- package/dist/host/server.js +0 -67
- package/dist/host/web.d.ts +0 -5
- package/dist/host/web.js +0 -74
- package/dist/macos/index.macos.d.ts +0 -3
- package/dist/macos/index.macos.js +0 -5
- package/dist/macos/servers.macos.d.ts +0 -4
- package/dist/macos/servers.macos.js +0 -57
- package/dist/macos/socket.macos.d.ts +0 -2
- package/dist/macos/socket.macos.js +0 -152
- package/dist/macos/ws-frames.d.ts +0 -24
- package/dist/macos/ws-frames.js +0 -205
- package/dist/node.d.ts +0 -12
- package/dist/node.js +0 -51
- package/dist/record.d.ts +0 -9
- package/dist/record.js +0 -38
- package/dist/types.d.ts +0 -128
- package/dist/types.js +0 -25
- package/src/app.ts +0 -105
- package/src/assets.d.ts +0 -2
- package/src/backend.ts +0 -278
- package/src/client.ts +0 -248
- package/src/desktop/Shell.macos.tsrx +0 -158
- package/src/desktop/Shell.ts +0 -16
- package/src/desktop/index.macos.ts +0 -7
- package/src/desktop/main.macos.ts +0 -130
- package/src/desktop/panels.d.ts +0 -10
- package/src/desktop/target.ts +0 -34
- package/src/host/desktop.ts +0 -176
- package/src/host/panels.ts +0 -193
- package/src/host/records.ts +0 -62
- package/src/host/server.ts +0 -92
- package/src/host/web.ts +0 -68
- package/src/macos/index.macos.ts +0 -7
- package/src/macos/servers.macos.ts +0 -83
- package/src/macos/socket.macos.ts +0 -218
- package/src/macos/ws-frames.ts +0 -249
- package/src/node.ts +0 -60
- package/src/panel/App.tsrx +0 -288
- package/src/panel/index.html +0 -12
- package/src/panel/index.ts +0 -24
- package/src/panel/main.tsrx +0 -2
- package/src/panel/main.web.tsrx +0 -89
- package/src/panel/model.ts +0 -216
- package/src/panel/panel.css +0 -189
- package/src/panel/plugin.ts +0 -65
- package/src/panel/styles.ts +0 -5
- package/src/panel/tsconfig.browser.json +0 -9
- package/src/record.ts +0 -66
- package/src/types.ts +0 -129
package/src/app.ts
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
// App-side devtools channel: declares a channel on the app's dev socket so
|
|
2
|
-
// the xplat dev hub routes its frames to the target's backend and connected
|
|
3
|
-
// devtools panels. Inert outside `xplat dev` — the Vite fallback bridge has
|
|
4
|
-
// no channels and reports 'unavailable'.
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
registerDevChannel,
|
|
8
|
-
type ChannelErrorCode,
|
|
9
|
-
type DevChannelState,
|
|
10
|
-
type DevChannelRegistration,
|
|
11
|
-
} from '@octane-xplat/platform/dev-actions'
|
|
12
|
-
|
|
13
|
-
export type { ChannelErrorCode, DevChannelState }
|
|
14
|
-
|
|
15
|
-
/** One routed channel message delivered to the app. */
|
|
16
|
-
export interface DevChannelMessage<In> {
|
|
17
|
-
from: 'backend' | 'panel'
|
|
18
|
-
panel?: string
|
|
19
|
-
data: In
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/** A routing failure the hub bounced back to this channel's sender. */
|
|
23
|
-
export interface DevChannelFailure {
|
|
24
|
-
code: ChannelErrorCode
|
|
25
|
-
detail: string
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** An app-side devtools channel. @internal */
|
|
29
|
-
export interface DevChannel<In, Out> {
|
|
30
|
-
readonly state: DevChannelState
|
|
31
|
-
/** False unless the app socket is live and the hub advertised 'channels'. */
|
|
32
|
-
send(to: 'backend' | 'panels', data: Out): boolean
|
|
33
|
-
subscribe(listener: (message: DevChannelMessage<In>) => void): () => void
|
|
34
|
-
onStateChange(listener: (state: DevChannelState) => void): () => void
|
|
35
|
-
onError(listener: (failure: DevChannelFailure) => void): () => void
|
|
36
|
-
close(): void
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Declare a devtools channel. `id` must match /^[a-z][a-z0-9-]{0,31}$/ and
|
|
41
|
-
* `version` a positive int32; the pair joins the app's registry announcement.
|
|
42
|
-
*/
|
|
43
|
-
export function openDevChannel<In = unknown, Out = unknown>(
|
|
44
|
-
id: string,
|
|
45
|
-
version: number,
|
|
46
|
-
): DevChannel<In, Out> {
|
|
47
|
-
const listeners = new Set<(message: DevChannelMessage<In>) => void>()
|
|
48
|
-
const stateListeners = new Set<(state: DevChannelState) => void>()
|
|
49
|
-
const errorListeners = new Set<(failure: DevChannelFailure) => void>()
|
|
50
|
-
let current: DevChannelState = 'connecting'
|
|
51
|
-
|
|
52
|
-
const registration: DevChannelRegistration = registerDevChannel(id, version, {
|
|
53
|
-
message(frame) {
|
|
54
|
-
// Channel payloads are typed by the channel's own protocol; `as In` is
|
|
55
|
-
// the documented boundary for this module.
|
|
56
|
-
for (const listener of listeners) {
|
|
57
|
-
listener({
|
|
58
|
-
from: frame.from,
|
|
59
|
-
...(frame.panel ? { panel: frame.panel } : {}),
|
|
60
|
-
data: frame.data as In,
|
|
61
|
-
})
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
state(state) {
|
|
65
|
-
current = state
|
|
66
|
-
for (const listener of stateListeners) {
|
|
67
|
-
listener(state)
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
error(failure) {
|
|
71
|
-
for (const listener of errorListeners) {
|
|
72
|
-
listener(failure)
|
|
73
|
-
}
|
|
74
|
-
},
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
let closed = false
|
|
78
|
-
|
|
79
|
-
return {
|
|
80
|
-
get state() {
|
|
81
|
-
return current
|
|
82
|
-
},
|
|
83
|
-
send(to, data) {
|
|
84
|
-
return !closed && registration.send(to, data)
|
|
85
|
-
},
|
|
86
|
-
subscribe(listener) {
|
|
87
|
-
listeners.add(listener)
|
|
88
|
-
return () => listeners.delete(listener)
|
|
89
|
-
},
|
|
90
|
-
onStateChange(listener) {
|
|
91
|
-
stateListeners.add(listener)
|
|
92
|
-
return () => stateListeners.delete(listener)
|
|
93
|
-
},
|
|
94
|
-
onError(listener) {
|
|
95
|
-
errorListeners.add(listener)
|
|
96
|
-
return () => errorListeners.delete(listener)
|
|
97
|
-
},
|
|
98
|
-
close() {
|
|
99
|
-
if (!closed) {
|
|
100
|
-
closed = true
|
|
101
|
-
registration.dispose()
|
|
102
|
-
}
|
|
103
|
-
},
|
|
104
|
-
}
|
|
105
|
-
}
|
package/src/assets.d.ts
DELETED
package/src/backend.ts
DELETED
|
@@ -1,278 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,248 +0,0 @@
|
|
|
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
|
-
}
|