@alloy-js/core 0.23.0-dev.1 → 0.23.0-dev.11
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/CHANGELOG.md +0 -22
- package/dist/devtools/index.html +80 -0
- package/dist/src/binder.d.ts +2 -0
- package/dist/src/binder.d.ts.map +1 -1
- package/dist/src/binder.js +60 -12
- package/dist/src/binder.js.map +1 -1
- package/dist/src/components/AccessExpression.d.ts +78 -0
- package/dist/src/components/AccessExpression.d.ts.map +1 -0
- package/dist/src/components/AccessExpression.js +218 -0
- package/dist/src/components/AccessExpression.js.map +1 -0
- package/dist/src/components/AccessExpression.test.d.ts +2 -0
- package/dist/src/components/AccessExpression.test.d.ts.map +1 -0
- package/dist/src/components/AccessExpression.test.js +137 -0
- package/dist/src/components/AccessExpression.test.js.map +1 -0
- package/dist/src/components/AppendFile.d.ts.map +1 -1
- package/dist/src/components/AppendFile.js +14 -3
- package/dist/src/components/AppendFile.js.map +1 -1
- package/dist/src/components/Block.js +1 -1
- package/dist/src/components/Block.js.map +1 -1
- package/dist/src/components/Declaration.d.ts.map +1 -1
- package/dist/src/components/Declaration.js +2 -1
- package/dist/src/components/Declaration.js.map +1 -1
- package/dist/src/components/For.d.ts.map +1 -1
- package/dist/src/components/For.js +1 -1
- package/dist/src/components/For.js.map +1 -1
- package/dist/src/components/List.d.ts.map +1 -1
- package/dist/src/components/List.js +1 -1
- package/dist/src/components/List.js.map +1 -1
- package/dist/src/components/Prose.js +2 -2
- package/dist/src/components/Prose.js.map +1 -1
- package/dist/src/components/Scope.d.ts.map +1 -1
- package/dist/src/components/Scope.js +6 -1
- package/dist/src/components/Scope.js.map +1 -1
- package/dist/src/components/SourceDirectory.d.ts.map +1 -1
- package/dist/src/components/SourceDirectory.js +1 -2
- package/dist/src/components/SourceDirectory.js.map +1 -1
- package/dist/src/components/Switch.d.ts.map +1 -1
- package/dist/src/components/Switch.js +1 -1
- package/dist/src/components/Switch.js.map +1 -1
- package/dist/src/components/TemplateFile.d.ts.map +1 -1
- package/dist/src/components/TemplateFile.js +18 -3
- package/dist/src/components/TemplateFile.js.map +1 -1
- package/dist/src/components/index.d.ts +1 -0
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +1 -0
- package/dist/src/components/index.js.map +1 -1
- package/dist/src/content-slot.d.ts.map +1 -1
- package/dist/src/content-slot.js +7 -6
- package/dist/src/content-slot.js.map +1 -1
- package/dist/src/context.d.ts.map +1 -1
- package/dist/src/context.js +10 -3
- package/dist/src/context.js.map +1 -1
- package/dist/src/debug/cli.d.ts +6 -0
- package/dist/src/debug/cli.d.ts.map +1 -0
- package/dist/src/{debug.js → debug/cli.js} +79 -82
- package/dist/src/debug/cli.js.map +1 -0
- package/dist/src/debug/diagnostics.test.d.ts +2 -0
- package/dist/src/debug/diagnostics.test.d.ts.map +1 -0
- package/dist/src/debug/diagnostics.test.js +46 -0
- package/dist/src/debug/diagnostics.test.js.map +1 -0
- package/dist/src/debug/effects.d.ts +81 -0
- package/dist/src/debug/effects.d.ts.map +1 -0
- package/dist/src/debug/effects.js +358 -0
- package/dist/src/debug/effects.js.map +1 -0
- package/dist/src/debug/effects.test.d.ts +2 -0
- package/dist/src/debug/effects.test.d.ts.map +1 -0
- package/dist/src/debug/effects.test.js +256 -0
- package/dist/src/debug/effects.test.js.map +1 -0
- package/dist/src/debug/files.d.ts +14 -0
- package/dist/src/debug/files.d.ts.map +1 -0
- package/dist/src/debug/files.js +29 -0
- package/dist/src/debug/files.js.map +1 -0
- package/dist/src/debug/files.test.d.ts +2 -0
- package/dist/src/debug/files.test.d.ts.map +1 -0
- package/dist/src/debug/files.test.js +66 -0
- package/dist/src/debug/files.test.js.map +1 -0
- package/dist/src/debug/index.d.ts +63 -0
- package/dist/src/debug/index.d.ts.map +1 -0
- package/dist/src/debug/index.js +71 -0
- package/dist/src/debug/index.js.map +1 -0
- package/dist/src/debug/message-format.test.d.ts +2 -0
- package/dist/src/debug/message-format.test.d.ts.map +1 -0
- package/dist/src/debug/message-format.test.js +700 -0
- package/dist/src/debug/message-format.test.js.map +1 -0
- package/dist/src/debug/render-tree-orphans.test.d.ts +2 -0
- package/dist/src/debug/render-tree-orphans.test.d.ts.map +1 -0
- package/dist/src/debug/render-tree-orphans.test.js +297 -0
- package/dist/src/debug/render-tree-orphans.test.js.map +1 -0
- package/dist/src/debug/render.d.ts +57 -0
- package/dist/src/debug/render.d.ts.map +1 -0
- package/dist/src/debug/render.js +472 -0
- package/dist/src/debug/render.js.map +1 -0
- package/dist/src/debug/render.test.d.ts +2 -0
- package/dist/src/debug/render.test.d.ts.map +1 -0
- package/dist/src/debug/render.test.js +291 -0
- package/dist/src/debug/render.test.js.map +1 -0
- package/dist/src/debug/serialize.d.ts +9 -0
- package/dist/src/debug/serialize.d.ts.map +1 -0
- package/dist/src/debug/serialize.js +70 -0
- package/dist/src/debug/serialize.js.map +1 -0
- package/dist/src/debug/symbols.d.ts +16 -0
- package/dist/src/debug/symbols.d.ts.map +1 -0
- package/dist/src/debug/symbols.js +196 -0
- package/dist/src/debug/symbols.js.map +1 -0
- package/dist/src/debug/symbols.test.d.ts +2 -0
- package/dist/src/debug/symbols.test.d.ts.map +1 -0
- package/dist/src/debug/symbols.test.js +93 -0
- package/dist/src/debug/symbols.test.js.map +1 -0
- package/dist/src/debug/trace-writer.d.ts +55 -0
- package/dist/src/debug/trace-writer.d.ts.map +1 -0
- package/dist/src/debug/trace-writer.js +658 -0
- package/dist/src/debug/trace-writer.js.map +1 -0
- package/dist/src/debug/trace.d.ts +342 -0
- package/dist/src/debug/trace.d.ts.map +1 -0
- package/dist/src/debug/trace.js +446 -0
- package/dist/src/debug/trace.js.map +1 -0
- package/dist/src/devtools/devtools-protocol.d.ts +389 -0
- package/dist/src/devtools/devtools-protocol.d.ts.map +1 -0
- package/dist/src/devtools/devtools-protocol.js +2 -0
- package/dist/src/devtools/devtools-protocol.js.map +1 -0
- package/dist/src/devtools/devtools-server.browser.d.ts +23 -0
- package/dist/src/devtools/devtools-server.browser.d.ts.map +1 -0
- package/dist/src/devtools/devtools-server.browser.js +33 -0
- package/dist/src/devtools/devtools-server.browser.js.map +1 -0
- package/dist/src/devtools/devtools-server.d.ts +66 -0
- package/dist/src/devtools/devtools-server.d.ts.map +1 -0
- package/dist/src/devtools/devtools-server.js +444 -0
- package/dist/src/devtools/devtools-server.js.map +1 -0
- package/dist/src/devtools/devtools-transport.d.ts +23 -0
- package/dist/src/devtools/devtools-transport.d.ts.map +1 -0
- package/dist/src/devtools/devtools-transport.js +114 -0
- package/dist/src/devtools/devtools-transport.js.map +1 -0
- package/dist/src/devtools-entry.browser.d.ts +4 -0
- package/dist/src/devtools-entry.browser.d.ts.map +1 -0
- package/dist/src/devtools-entry.browser.js +2 -0
- package/dist/src/devtools-entry.browser.js.map +1 -0
- package/dist/src/devtools-entry.d.ts +4 -0
- package/dist/src/devtools-entry.d.ts.map +1 -0
- package/dist/src/devtools-entry.js +2 -0
- package/dist/src/devtools-entry.js.map +1 -0
- package/dist/src/diagnostics.d.ts +34 -0
- package/dist/src/diagnostics.d.ts.map +1 -0
- package/dist/src/diagnostics.js +89 -0
- package/dist/src/diagnostics.js.map +1 -0
- package/dist/src/index.d.ts +3 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/print-hook.d.ts +14 -0
- package/dist/src/print-hook.d.ts.map +1 -0
- package/dist/src/print-hook.js +10 -0
- package/dist/src/print-hook.js.map +1 -0
- package/dist/src/reactive-union-set.d.ts.map +1 -1
- package/dist/src/reactive-union-set.js +28 -3
- package/dist/src/reactive-union-set.js.map +1 -1
- package/dist/src/reactivity.d.ts +60 -7
- package/dist/src/reactivity.d.ts.map +1 -1
- package/dist/src/reactivity.js +308 -39
- package/dist/src/reactivity.js.map +1 -1
- package/dist/src/render-stack.d.ts +18 -1
- package/dist/src/render-stack.d.ts.map +1 -1
- package/dist/src/render-stack.js +61 -1
- package/dist/src/render-stack.js.map +1 -1
- package/dist/src/render.d.ts +8 -15
- package/dist/src/render.d.ts.map +1 -1
- package/dist/src/render.js +424 -109
- package/dist/src/render.js.map +1 -1
- package/dist/src/resource.d.ts.map +1 -1
- package/dist/src/resource.js +5 -0
- package/dist/src/resource.js.map +1 -1
- package/dist/src/scheduler.d.ts +13 -0
- package/dist/src/scheduler.d.ts.map +1 -1
- package/dist/src/scheduler.js +150 -13
- package/dist/src/scheduler.js.map +1 -1
- package/dist/src/symbols/basic-symbol.d.ts.map +1 -1
- package/dist/src/symbols/basic-symbol.js +6 -1
- package/dist/src/symbols/basic-symbol.js.map +1 -1
- package/dist/src/symbols/decl.d.ts.map +1 -1
- package/dist/src/symbols/decl.js +5 -1
- package/dist/src/symbols/decl.js.map +1 -1
- package/dist/src/symbols/output-scope.d.ts +2 -1
- package/dist/src/symbols/output-scope.d.ts.map +1 -1
- package/dist/src/symbols/output-scope.js +13 -8
- package/dist/src/symbols/output-scope.js.map +1 -1
- package/dist/src/symbols/output-symbol.d.ts +1 -0
- package/dist/src/symbols/output-symbol.d.ts.map +1 -1
- package/dist/src/symbols/output-symbol.js +25 -8
- package/dist/src/symbols/output-symbol.js.map +1 -1
- package/dist/src/symbols/symbol-flow.d.ts.map +1 -1
- package/dist/src/symbols/symbol-flow.js +24 -8
- package/dist/src/symbols/symbol-flow.js.map +1 -1
- package/dist/src/symbols/symbol-slot.d.ts.map +1 -1
- package/dist/src/symbols/symbol-slot.js +15 -0
- package/dist/src/symbols/symbol-slot.js.map +1 -1
- package/dist/src/symbols/symbol-slot.test.d.ts +2 -0
- package/dist/src/symbols/symbol-slot.test.d.ts.map +1 -0
- package/dist/src/symbols/symbol-slot.test.js +35 -0
- package/dist/src/symbols/symbol-slot.test.js.map +1 -0
- package/dist/src/symbols/symbol-table.d.ts.map +1 -1
- package/dist/src/symbols/symbol-table.js +6 -5
- package/dist/src/symbols/symbol-table.js.map +1 -1
- package/dist/src/trace.d.ts +2 -0
- package/dist/src/trace.d.ts.map +1 -0
- package/dist/src/trace.js +2 -0
- package/dist/src/trace.js.map +1 -0
- package/dist/src/tracer.d.ts +2 -228
- package/dist/src/tracer.d.ts.map +1 -1
- package/dist/src/tracer.js +5 -298
- package/dist/src/tracer.js.map +1 -1
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +17 -9
- package/dist/src/utils.js.map +1 -1
- package/dist/test/components/append-file.test.d.ts.map +1 -1
- package/dist/test/components/append-file.test.js +18 -10
- package/dist/test/components/append-file.test.js.map +1 -1
- package/dist/test/components/template-file.test.d.ts.map +1 -1
- package/dist/test/components/template-file.test.js +6 -4
- package/dist/test/components/template-file.test.js.map +1 -1
- package/dist/test/lazy-isempty.test.d.ts +2 -0
- package/dist/test/lazy-isempty.test.d.ts.map +1 -0
- package/dist/test/lazy-isempty.test.js +89 -0
- package/dist/test/lazy-isempty.test.js.map +1 -0
- package/dist/test/reactive-union-set-disposers.test.d.ts +2 -0
- package/dist/test/reactive-union-set-disposers.test.d.ts.map +1 -0
- package/dist/test/reactive-union-set-disposers.test.js +98 -0
- package/dist/test/reactive-union-set-disposers.test.js.map +1 -0
- package/dist/test/reactivity/shallow-reactive.test.d.ts +2 -0
- package/dist/test/reactivity/shallow-reactive.test.d.ts.map +1 -0
- package/dist/test/reactivity/shallow-reactive.test.js +52 -0
- package/dist/test/reactivity/shallow-reactive.test.js.map +1 -0
- package/dist/test/rendering/basic.test.js +3 -0
- package/dist/test/rendering/basic.test.js.map +1 -1
- package/dist/test/rendering/print-render-stack.test.d.ts.map +1 -1
- package/dist/test/rendering/print-render-stack.test.js +91 -98
- package/dist/test/rendering/print-render-stack.test.js.map +1 -1
- package/dist/test/scheduler-extended.test.d.ts +2 -0
- package/dist/test/scheduler-extended.test.d.ts.map +1 -0
- package/dist/test/scheduler-extended.test.js +96 -0
- package/dist/test/scheduler-extended.test.js.map +1 -0
- package/dist/test/scheduler.test.d.ts +2 -0
- package/dist/test/scheduler.test.d.ts.map +1 -0
- package/dist/test/scheduler.test.js +46 -0
- package/dist/test/scheduler.test.js.map +1 -0
- package/dist/testing/create-test-wrapper.d.ts +1 -1
- package/dist/testing/create-test-wrapper.d.ts.map +1 -1
- package/dist/testing/create-test-wrapper.js +1 -1
- package/dist/testing/create-test-wrapper.js.map +1 -1
- package/dist/testing/devtools-utils.d.ts +35 -0
- package/dist/testing/devtools-utils.d.ts.map +1 -0
- package/dist/testing/devtools-utils.js +162 -0
- package/dist/testing/devtools-utils.js.map +1 -0
- package/dist/testing/extend-expect.d.ts.map +1 -1
- package/dist/testing/extend-expect.js +63 -1
- package/dist/testing/extend-expect.js.map +1 -1
- package/dist/testing/render.d.ts +2 -2
- package/dist/testing/render.d.ts.map +1 -1
- package/dist/testing/render.js +2 -2
- package/dist/testing/render.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +21 -7
- package/scripts/copy-devtools-ui.mjs +26 -0
- package/src/binder.ts +117 -53
- package/src/components/AccessExpression.test.tsx +132 -0
- package/src/components/AccessExpression.tsx +344 -0
- package/src/components/AppendFile.tsx +14 -9
- package/src/components/Block.tsx +1 -1
- package/src/components/Declaration.tsx +2 -1
- package/src/components/For.tsx +14 -10
- package/src/components/List.tsx +7 -4
- package/src/components/Prose.tsx +1 -1
- package/src/components/Scope.tsx +6 -1
- package/src/components/SourceDirectory.tsx +1 -2
- package/src/components/Switch.tsx +11 -7
- package/src/components/TemplateFile.tsx +18 -9
- package/src/components/index.tsx +1 -0
- package/src/content-slot.tsx +7 -7
- package/src/context.ts +17 -6
- package/src/{debug.ts → debug/cli.ts} +114 -125
- package/src/debug/diagnostics.test.tsx +56 -0
- package/src/debug/effects.test.tsx +301 -0
- package/src/debug/effects.ts +531 -0
- package/src/debug/files.test.tsx +76 -0
- package/src/debug/files.ts +40 -0
- package/src/debug/index.ts +132 -0
- package/src/debug/message-format.test.tsx +759 -0
- package/src/debug/render-tree-orphans.test.tsx +344 -0
- package/src/debug/render.test.tsx +357 -0
- package/src/debug/render.ts +698 -0
- package/src/debug/serialize.ts +85 -0
- package/src/debug/symbols.test.tsx +105 -0
- package/src/debug/symbols.ts +322 -0
- package/src/debug/trace-writer.ts +969 -0
- package/src/debug/trace.ts +309 -0
- package/src/devtools/devtools-protocol.ts +497 -0
- package/src/devtools/devtools-server.browser.ts +62 -0
- package/src/devtools/devtools-server.ts +468 -0
- package/src/devtools/devtools-transport.ts +154 -0
- package/src/devtools-entry.browser.ts +48 -0
- package/src/devtools-entry.ts +48 -0
- package/src/diagnostics.ts +150 -0
- package/src/index.ts +2 -7
- package/src/print-hook.ts +22 -0
- package/src/reactive-union-set.ts +85 -44
- package/src/reactivity.ts +396 -58
- package/src/render-stack.ts +73 -1
- package/src/render.ts +544 -161
- package/src/resource.ts +28 -19
- package/src/scheduler.ts +209 -14
- package/src/symbols/basic-symbol.ts +6 -1
- package/src/symbols/decl.ts +5 -1
- package/src/symbols/output-scope.ts +21 -13
- package/src/symbols/output-symbol.ts +34 -14
- package/src/symbols/symbol-flow.ts +76 -39
- package/src/symbols/symbol-slot.test.tsx +41 -0
- package/src/symbols/symbol-slot.tsx +47 -20
- package/src/symbols/symbol-table.ts +6 -10
- package/src/trace.ts +1 -0
- package/src/tracer.ts +13 -242
- package/src/utils.tsx +31 -21
- package/temp/api.json +5700 -3015
- package/test/components/append-file.test.tsx +36 -29
- package/test/components/template-file.test.tsx +11 -11
- package/test/lazy-isempty.test.tsx +106 -0
- package/test/reactive-union-set-disposers.test.tsx +112 -0
- package/test/reactivity/shallow-reactive.test.tsx +56 -0
- package/test/rendering/basic.test.tsx +4 -0
- package/test/rendering/print-render-stack.test.tsx +52 -43
- package/test/scheduler-extended.test.tsx +122 -0
- package/test/scheduler.test.tsx +50 -0
- package/testing/create-test-wrapper.tsx +1 -1
- package/testing/devtools-utils.ts +245 -0
- package/testing/extend-expect.ts +89 -0
- package/testing/render.ts +2 -2
- package/testing/vitest.d.ts +9 -0
- package/dist/src/debug.d.ts +0 -14
- package/dist/src/debug.d.ts.map +0 -1
- package/dist/src/debug.js.map +0 -1
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Devtools session manager.
|
|
3
|
+
*
|
|
4
|
+
* Manages the lifecycle of a devtools session: enable, connect, broadcast
|
|
5
|
+
* messages, and reset. Transport details (HTTP/WebSocket) are delegated
|
|
6
|
+
* to devtools-transport.ts.
|
|
7
|
+
*/
|
|
8
|
+
import type { ChangeChannel, ChangeEvent } from "../debug/trace-writer.js";
|
|
9
|
+
import {
|
|
10
|
+
ALL_CHANNELS,
|
|
11
|
+
closeTrace,
|
|
12
|
+
initTrace,
|
|
13
|
+
isTraceEnabled,
|
|
14
|
+
queryChannel,
|
|
15
|
+
setChangeListener,
|
|
16
|
+
} from "../debug/trace-writer.js";
|
|
17
|
+
import {
|
|
18
|
+
createTransport,
|
|
19
|
+
getAlloyVersion,
|
|
20
|
+
type DevtoolsTransportState,
|
|
21
|
+
} from "./devtools-transport.js";
|
|
22
|
+
|
|
23
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
24
|
+
// Public types
|
|
25
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
26
|
+
|
|
27
|
+
export interface DevtoolsIncomingMessage {
|
|
28
|
+
type: string;
|
|
29
|
+
[key: string]: unknown;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface DevtoolsServerInfo {
|
|
33
|
+
port: number;
|
|
34
|
+
connected: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface EnableDevtoolsOptions {
|
|
38
|
+
/** Port to listen on. Use 0 for a random available port. Defaults to ALLOY_DEBUG_PORT or 8123. */
|
|
39
|
+
port?: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
43
|
+
// Session state
|
|
44
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
45
|
+
|
|
46
|
+
let transportState: DevtoolsTransportState | null = null;
|
|
47
|
+
let transportPromise: Promise<DevtoolsTransportState> | null = null;
|
|
48
|
+
const messageHandlers = new Set<(message: DevtoolsIncomingMessage) => void>();
|
|
49
|
+
let devtoolsExplicitlyEnabled = false;
|
|
50
|
+
let devtoolsInitialized = false;
|
|
51
|
+
let loggedDevtoolsLinks = false;
|
|
52
|
+
let waitingForConnection = false;
|
|
53
|
+
let configuredPort: number | undefined;
|
|
54
|
+
let tempDbPath: string | null = null;
|
|
55
|
+
let subscribedPromise: Promise<void> | null = null;
|
|
56
|
+
let resolveSubscribed: (() => void) | null = null;
|
|
57
|
+
|
|
58
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
59
|
+
// Per-client subscription state
|
|
60
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
61
|
+
|
|
62
|
+
interface ClientState {
|
|
63
|
+
subscriptions: Set<ChangeChannel>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const clientStates = new Map<any, ClientState>();
|
|
67
|
+
|
|
68
|
+
function eventToMessageType(event: ChangeEvent): string {
|
|
69
|
+
const map: Record<string, Record<string, string>> = {
|
|
70
|
+
render: {
|
|
71
|
+
added: "render:node_added",
|
|
72
|
+
updated: "render:node_updated",
|
|
73
|
+
removed: "render:node_removed",
|
|
74
|
+
reset: "render:reset",
|
|
75
|
+
},
|
|
76
|
+
effects: { added: "effect:added", updated: "effect:updated" },
|
|
77
|
+
refs: { added: "ref:added" },
|
|
78
|
+
edges: { added: `edge:${(event.data as any).edge_type ?? "track"}` },
|
|
79
|
+
symbols: {
|
|
80
|
+
added: "symbol:added",
|
|
81
|
+
updated: "symbol:updated",
|
|
82
|
+
removed: "symbol:removed",
|
|
83
|
+
},
|
|
84
|
+
scopes: {
|
|
85
|
+
added: "scope:added",
|
|
86
|
+
updated: "scope:updated",
|
|
87
|
+
removed: "scope:removed",
|
|
88
|
+
},
|
|
89
|
+
files: {
|
|
90
|
+
added: "file:added",
|
|
91
|
+
updated: "file:updated",
|
|
92
|
+
removed: "file:removed",
|
|
93
|
+
},
|
|
94
|
+
directories: { added: "directory:added", removed: "directory:removed" },
|
|
95
|
+
diagnostics: { added: "diagnostics:report" },
|
|
96
|
+
errors: { added: "render:error" },
|
|
97
|
+
lifecycle: { added: "effect:lifecycle" },
|
|
98
|
+
scheduler: { added: "scheduler:job" },
|
|
99
|
+
};
|
|
100
|
+
return (
|
|
101
|
+
map[event.channel]?.[event.action] ?? `${event.channel}:${event.action}`
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function channelToInitialMessageType(
|
|
106
|
+
channel: ChangeChannel,
|
|
107
|
+
row: Record<string, unknown>,
|
|
108
|
+
): string {
|
|
109
|
+
const map: Record<string, string> = {
|
|
110
|
+
render: "render:node_added",
|
|
111
|
+
effects: "effect:added",
|
|
112
|
+
refs: "ref:added",
|
|
113
|
+
symbols: "symbol:added",
|
|
114
|
+
scopes: "scope:added",
|
|
115
|
+
files: "file:added",
|
|
116
|
+
directories: "directory:added",
|
|
117
|
+
diagnostics: "diagnostics:report",
|
|
118
|
+
errors: "render:error",
|
|
119
|
+
lifecycle: "effect:lifecycle",
|
|
120
|
+
scheduler: "scheduler:job",
|
|
121
|
+
};
|
|
122
|
+
if (channel === "edges") {
|
|
123
|
+
return `edge:${(row.type as string) ?? "track"}`;
|
|
124
|
+
}
|
|
125
|
+
return map[channel] ?? `${channel}:added`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function sendInitialState(socket: any, channels: ChangeChannel[]): void {
|
|
129
|
+
if (!isTraceEnabled()) return;
|
|
130
|
+
for (const channel of channels) {
|
|
131
|
+
const rows = queryChannel(channel);
|
|
132
|
+
for (const row of rows) {
|
|
133
|
+
const msgType = channelToInitialMessageType(channel, row);
|
|
134
|
+
// Remap SQLite column names that collide with the message `type` field
|
|
135
|
+
if (channel === "effects" && row.type !== undefined) {
|
|
136
|
+
row.effect_type = row.type;
|
|
137
|
+
}
|
|
138
|
+
const msg = { ...row, type: msgType };
|
|
139
|
+
socket.send(JSON.stringify(msg));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
145
|
+
// Environment helpers
|
|
146
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
147
|
+
|
|
148
|
+
function isNodeEnvironment() {
|
|
149
|
+
return (
|
|
150
|
+
typeof process !== "undefined" &&
|
|
151
|
+
typeof process.versions === "object" &&
|
|
152
|
+
Boolean(process.versions?.node)
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function getCwd() {
|
|
157
|
+
if (!isNodeEnvironment()) return undefined;
|
|
158
|
+
try {
|
|
159
|
+
return process.cwd();
|
|
160
|
+
} catch {
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function resolveDebugPort() {
|
|
166
|
+
const raw = process.env.ALLOY_DEBUG_PORT;
|
|
167
|
+
if (!raw) return 8123;
|
|
168
|
+
const parsed = Number.parseInt(raw, 10);
|
|
169
|
+
if (Number.isNaN(parsed)) return 8123;
|
|
170
|
+
return parsed;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
174
|
+
// Query functions
|
|
175
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
176
|
+
|
|
177
|
+
/** Returns true when devtools are enabled (via env var or explicit call). */
|
|
178
|
+
export function isDevtoolsEnabled() {
|
|
179
|
+
if (!isNodeEnvironment()) return false;
|
|
180
|
+
return devtoolsExplicitlyEnabled || Boolean(process.env.ALLOY_DEBUG);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Returns true when a devtools client is currently connected. */
|
|
184
|
+
export function isDevtoolsConnected(): boolean {
|
|
185
|
+
return Boolean(transportState?.connected);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Get the current server info, or null if not running. */
|
|
189
|
+
export function getDevtoolsServerInfo(): DevtoolsServerInfo | null {
|
|
190
|
+
if (!transportState) return null;
|
|
191
|
+
return { port: transportState.port, connected: transportState.connected };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
195
|
+
// Temp SQLite for devtools
|
|
196
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
197
|
+
|
|
198
|
+
async function ensureSqliteForDevtools(): Promise<void> {
|
|
199
|
+
if (isTraceEnabled()) return;
|
|
200
|
+
const os = await import("node:os");
|
|
201
|
+
const path = await import("node:path");
|
|
202
|
+
tempDbPath = path.join(os.tmpdir(), `alloy-debug-${process.pid}.db`);
|
|
203
|
+
await initTrace(tempDbPath);
|
|
204
|
+
process.on("exit", () => {
|
|
205
|
+
if (!tempDbPath) return;
|
|
206
|
+
try {
|
|
207
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
208
|
+
const fs = require("node:fs");
|
|
209
|
+
fs.unlinkSync(tempDbPath);
|
|
210
|
+
try {
|
|
211
|
+
fs.unlinkSync(tempDbPath + "-wal");
|
|
212
|
+
} catch {
|
|
213
|
+
/* ignore */
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
fs.unlinkSync(tempDbPath + "-shm");
|
|
217
|
+
} catch {
|
|
218
|
+
/* ignore */
|
|
219
|
+
}
|
|
220
|
+
} catch {
|
|
221
|
+
/* ignore cleanup failures */
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
227
|
+
// Server lifecycle
|
|
228
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
229
|
+
|
|
230
|
+
async function ensureServer(): Promise<DevtoolsTransportState> {
|
|
231
|
+
if (transportState) return transportState;
|
|
232
|
+
if (!transportPromise) {
|
|
233
|
+
transportPromise = createTransport({
|
|
234
|
+
port: configuredPort ?? resolveDebugPort(),
|
|
235
|
+
onConnection(socket) {
|
|
236
|
+
// Start with no subscriptions — client must subscribe explicitly.
|
|
237
|
+
clientStates.set(socket, { subscriptions: new Set() });
|
|
238
|
+
if (!subscribedPromise) {
|
|
239
|
+
subscribedPromise = new Promise<void>((resolve) => {
|
|
240
|
+
resolveSubscribed = resolve;
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
if (waitingForConnection) {
|
|
244
|
+
waitingForConnection = false;
|
|
245
|
+
process.stdout.write(" Connected!\n");
|
|
246
|
+
}
|
|
247
|
+
socket.send(
|
|
248
|
+
JSON.stringify({
|
|
249
|
+
type: "debugger:info",
|
|
250
|
+
version: getAlloyVersion(),
|
|
251
|
+
cwd: getCwd(),
|
|
252
|
+
sourceMapEnabled: process.execArgv.includes("--enable-source-maps"),
|
|
253
|
+
}),
|
|
254
|
+
);
|
|
255
|
+
},
|
|
256
|
+
onMessage(raw, socket) {
|
|
257
|
+
const message = raw as DevtoolsIncomingMessage;
|
|
258
|
+
if (!message || !message.type) return;
|
|
259
|
+
|
|
260
|
+
if (message.type === "subscribe") {
|
|
261
|
+
const channels = (message as any).channels as string[];
|
|
262
|
+
const state = clientStates.get(socket);
|
|
263
|
+
if (state && channels && Array.isArray(channels)) {
|
|
264
|
+
const validChannels = channels.filter((ch) =>
|
|
265
|
+
ALL_CHANNELS.includes(ch as ChangeChannel),
|
|
266
|
+
) as ChangeChannel[];
|
|
267
|
+
state.subscriptions = new Set(validChannels);
|
|
268
|
+
sendInitialState(socket, validChannels);
|
|
269
|
+
}
|
|
270
|
+
resolveSubscribed?.();
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
if (message.type === "unsubscribe") {
|
|
274
|
+
const channels = (message as any).channels as string[];
|
|
275
|
+
const state = clientStates.get(socket);
|
|
276
|
+
if (state && channels) {
|
|
277
|
+
for (const ch of channels)
|
|
278
|
+
state.subscriptions.delete(ch as ChangeChannel);
|
|
279
|
+
}
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
for (const handler of messageHandlers) {
|
|
284
|
+
handler(message);
|
|
285
|
+
}
|
|
286
|
+
},
|
|
287
|
+
onDisconnect(socket) {
|
|
288
|
+
clientStates.delete(socket);
|
|
289
|
+
},
|
|
290
|
+
}).then((state) => {
|
|
291
|
+
transportState = state;
|
|
292
|
+
|
|
293
|
+
// Wire up the change notification bus to forward events to subscribed clients
|
|
294
|
+
setChangeListener((event: ChangeEvent) => {
|
|
295
|
+
if (!transportState) return;
|
|
296
|
+
|
|
297
|
+
// Lifecycle signals are broadcast to ALL connected clients
|
|
298
|
+
const signalType = (event.data as any)?._signal as string | undefined;
|
|
299
|
+
if (signalType) {
|
|
300
|
+
const payload = JSON.stringify({ type: signalType });
|
|
301
|
+
for (const client of transportState.clients) {
|
|
302
|
+
if (client.readyState === client.OPEN) {
|
|
303
|
+
client.send(payload);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const msgType = eventToMessageType(event);
|
|
310
|
+
const message = { ...event.data, type: msgType };
|
|
311
|
+
const payload = JSON.stringify(message);
|
|
312
|
+
|
|
313
|
+
for (const [client, clientState] of clientStates) {
|
|
314
|
+
if (client.readyState !== client.OPEN) continue;
|
|
315
|
+
if (clientState.subscriptions.has(event.channel)) {
|
|
316
|
+
client.send(payload);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
if (!loggedDevtoolsLinks) {
|
|
322
|
+
loggedDevtoolsLinks = true;
|
|
323
|
+
// eslint-disable-next-line no-console
|
|
324
|
+
console.log(`Alloy ${getAlloyVersion()}`);
|
|
325
|
+
// eslint-disable-next-line no-console
|
|
326
|
+
console.log(`➜ Debug UI: http://localhost:${state.port}/`);
|
|
327
|
+
// eslint-disable-next-line no-console
|
|
328
|
+
console.log(`➜ Websocket: ws://localhost:${state.port}/`);
|
|
329
|
+
// eslint-disable-next-line no-console
|
|
330
|
+
console.log("");
|
|
331
|
+
waitingForConnection = true;
|
|
332
|
+
process.stdout.write("Waiting for connection...");
|
|
333
|
+
}
|
|
334
|
+
return state;
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
return transportPromise;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
341
|
+
// Public API
|
|
342
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Wait for a devtools client to connect before proceeding.
|
|
346
|
+
*
|
|
347
|
+
* Starts the devtools server if not already running, and blocks until a
|
|
348
|
+
* devtools client connects via WebSocket.
|
|
349
|
+
*
|
|
350
|
+
* @example
|
|
351
|
+
* ```ts
|
|
352
|
+
* import { waitForDevtoolsConnection } from "@alloy-js/core/devtools";
|
|
353
|
+
* import { render } from "@alloy-js/core";
|
|
354
|
+
*
|
|
355
|
+
* await waitForDevtoolsConnection();
|
|
356
|
+
* const tree = render(<MyComponent />);
|
|
357
|
+
* ```
|
|
358
|
+
*/
|
|
359
|
+
export async function waitForDevtoolsConnection(): Promise<void> {
|
|
360
|
+
devtoolsExplicitlyEnabled = true;
|
|
361
|
+
const server = await ensureServer();
|
|
362
|
+
if (!server.connected) {
|
|
363
|
+
await server.ready;
|
|
364
|
+
}
|
|
365
|
+
// Wait for the client to send its initial subscribe message so that
|
|
366
|
+
// messages emitted during a synchronous render are not dropped.
|
|
367
|
+
if (subscribedPromise) {
|
|
368
|
+
await subscribedPromise;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Enable devtools and start the server, returning when the server is ready.
|
|
374
|
+
* Use this in tests to enable devtools before connecting a client.
|
|
375
|
+
*
|
|
376
|
+
* @returns Server info with port number
|
|
377
|
+
*/
|
|
378
|
+
export async function enableDevtools(
|
|
379
|
+
options?: EnableDevtoolsOptions,
|
|
380
|
+
): Promise<DevtoolsServerInfo> {
|
|
381
|
+
devtoolsExplicitlyEnabled = true;
|
|
382
|
+
devtoolsInitialized = true;
|
|
383
|
+
if (options?.port !== undefined) {
|
|
384
|
+
configuredPort = options.port;
|
|
385
|
+
}
|
|
386
|
+
await ensureSqliteForDevtools();
|
|
387
|
+
const server = await ensureServer();
|
|
388
|
+
return { port: server.port, connected: server.connected };
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Initialize devtools if ALLOY_DEBUG is set. Called lazily by render functions.
|
|
393
|
+
* Returns immediately if devtools are not enabled or already initialized.
|
|
394
|
+
*/
|
|
395
|
+
export async function initDevtoolsIfEnabled(): Promise<void> {
|
|
396
|
+
if (devtoolsInitialized) return;
|
|
397
|
+
if (!isDevtoolsEnabled()) return;
|
|
398
|
+
devtoolsInitialized = true;
|
|
399
|
+
await ensureSqliteForDevtools();
|
|
400
|
+
await waitForDevtoolsConnection();
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Start the devtools server and wait for a client connection.
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
* ```ts
|
|
408
|
+
* const serverInfo = await enableDevtoolsAndConnect({ port: 0 });
|
|
409
|
+
* // ... run your test ...
|
|
410
|
+
* await resetDevtoolsServerForTests();
|
|
411
|
+
* ```
|
|
412
|
+
*
|
|
413
|
+
* @returns Server info once a client has connected
|
|
414
|
+
*/
|
|
415
|
+
export async function enableDevtoolsAndConnect(
|
|
416
|
+
options?: EnableDevtoolsOptions,
|
|
417
|
+
): Promise<DevtoolsServerInfo> {
|
|
418
|
+
const info = await enableDevtools(options);
|
|
419
|
+
if (!info.connected) {
|
|
420
|
+
await transportState!.ready;
|
|
421
|
+
}
|
|
422
|
+
return { port: transportState!.port, connected: true };
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
426
|
+
// Messaging
|
|
427
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
428
|
+
|
|
429
|
+
/** Register a handler for incoming devtools messages. Returns an unsubscribe function. */
|
|
430
|
+
export function registerDevtoolsMessageHandler(
|
|
431
|
+
handler: (message: DevtoolsIncomingMessage) => void,
|
|
432
|
+
) {
|
|
433
|
+
messageHandlers.add(handler);
|
|
434
|
+
return () => messageHandlers.delete(handler);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** Throw if devtools are enabled but no client is connected (for sync render). */
|
|
438
|
+
export function assertDevtoolsConnectedForSyncRender() {
|
|
439
|
+
if (!isDevtoolsEnabled()) return;
|
|
440
|
+
if (!transportState || !transportState.connected) {
|
|
441
|
+
throw new Error(
|
|
442
|
+
"ALLOY_DEBUG is set but devtools are not connected. Use renderAsync or wait for the devtools client before rendering.",
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
448
|
+
// Test utilities
|
|
449
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
450
|
+
|
|
451
|
+
/** Reset all devtools state. For use in tests only. */
|
|
452
|
+
export async function resetDevtoolsServerForTests() {
|
|
453
|
+
setChangeListener(null);
|
|
454
|
+
clientStates.clear();
|
|
455
|
+
if (transportState) {
|
|
456
|
+
await transportState.close();
|
|
457
|
+
}
|
|
458
|
+
transportState = null;
|
|
459
|
+
transportPromise = null;
|
|
460
|
+
devtoolsExplicitlyEnabled = false;
|
|
461
|
+
devtoolsInitialized = false;
|
|
462
|
+
configuredPort = undefined;
|
|
463
|
+
loggedDevtoolsLinks = false;
|
|
464
|
+
subscribedPromise = null;
|
|
465
|
+
resolveSubscribed = null;
|
|
466
|
+
// Close the trace DB so each test starts fresh
|
|
467
|
+
closeTrace();
|
|
468
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket transport for the Alloy devtools server.
|
|
3
|
+
*
|
|
4
|
+
* Handles HTTP serving of the devtools UI and WebSocket connection management.
|
|
5
|
+
* This module is concerned only with the transport layer — session lifecycle
|
|
6
|
+
* is managed by devtools-server.ts.
|
|
7
|
+
*/
|
|
8
|
+
import { readFileSync } from "node:fs";
|
|
9
|
+
import {
|
|
10
|
+
createServer as createHttpServer,
|
|
11
|
+
type IncomingMessage,
|
|
12
|
+
type ServerResponse,
|
|
13
|
+
} from "node:http";
|
|
14
|
+
|
|
15
|
+
export interface DevtoolsTransportState {
|
|
16
|
+
port: number;
|
|
17
|
+
connected: boolean;
|
|
18
|
+
clients: Set<any>;
|
|
19
|
+
httpServer: ReturnType<typeof createHttpServer>;
|
|
20
|
+
ready: Promise<void>;
|
|
21
|
+
close(): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let cachedAlloyVersion: string | null = null;
|
|
25
|
+
|
|
26
|
+
/** Read the @alloy-js/core package version, cached after first call. */
|
|
27
|
+
export function getAlloyVersion(): string {
|
|
28
|
+
if (cachedAlloyVersion) return cachedAlloyVersion;
|
|
29
|
+
try {
|
|
30
|
+
const pkgUrl = new URL("../../../package.json", import.meta.url);
|
|
31
|
+
const pkg = JSON.parse(readFileSync(pkgUrl, "utf-8")) as {
|
|
32
|
+
version?: string;
|
|
33
|
+
};
|
|
34
|
+
cachedAlloyVersion = pkg.version ?? "0.0.0";
|
|
35
|
+
} catch {
|
|
36
|
+
cachedAlloyVersion = "0.0.0";
|
|
37
|
+
}
|
|
38
|
+
return cachedAlloyVersion;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Attempt to load the devtools UI HTML from known candidate paths. */
|
|
42
|
+
function loadDevtoolsUiHtml(): string | null {
|
|
43
|
+
const candidates = [
|
|
44
|
+
new URL("../../dist/devtools/index.html", import.meta.url),
|
|
45
|
+
new URL("../../devtools/index.html", import.meta.url),
|
|
46
|
+
new URL("../../../devtools/dist/index.html", import.meta.url),
|
|
47
|
+
];
|
|
48
|
+
for (const candidate of candidates) {
|
|
49
|
+
try {
|
|
50
|
+
return readFileSync(candidate, "utf-8");
|
|
51
|
+
} catch {
|
|
52
|
+
// try next
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface CreateTransportOptions {
|
|
59
|
+
port: number;
|
|
60
|
+
onConnection(socket: any): void;
|
|
61
|
+
onMessage(message: unknown, socket: any): void;
|
|
62
|
+
onDisconnect(socket: any): void;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Create the HTTP + WebSocket transport. Returns when the server is listening.
|
|
67
|
+
* Only accepts one concurrent WebSocket connection.
|
|
68
|
+
*/
|
|
69
|
+
export async function createTransport(
|
|
70
|
+
options: CreateTransportOptions,
|
|
71
|
+
): Promise<DevtoolsTransportState> {
|
|
72
|
+
const { WebSocketServer } = await import("ws");
|
|
73
|
+
const devtoolsUiHtml = loadDevtoolsUiHtml();
|
|
74
|
+
|
|
75
|
+
const httpServer = createHttpServer(
|
|
76
|
+
(req: IncomingMessage, res: ServerResponse) => {
|
|
77
|
+
const url = req.url ?? "/";
|
|
78
|
+
if (url !== "/" && url !== "/index.html") {
|
|
79
|
+
res.statusCode = 404;
|
|
80
|
+
res.end("Not Found");
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (!devtoolsUiHtml) {
|
|
84
|
+
res.statusCode = 404;
|
|
85
|
+
res.end("Not Found");
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
res.statusCode = 200;
|
|
89
|
+
res.setHeader("Content-Type", "text/html; charset=utf-8");
|
|
90
|
+
res.end(devtoolsUiHtml);
|
|
91
|
+
},
|
|
92
|
+
);
|
|
93
|
+
const wss = new WebSocketServer({ server: httpServer });
|
|
94
|
+
|
|
95
|
+
await new Promise<void>((resolve, reject) => {
|
|
96
|
+
httpServer.once("listening", resolve);
|
|
97
|
+
httpServer.once("error", reject);
|
|
98
|
+
httpServer.listen(options.port);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const address = httpServer.address();
|
|
102
|
+
const actualPort =
|
|
103
|
+
typeof address === "object" && address !== null ?
|
|
104
|
+
address.port
|
|
105
|
+
: options.port;
|
|
106
|
+
|
|
107
|
+
let resolveReady: (() => void) | undefined;
|
|
108
|
+
const ready = new Promise<void>((resolve) => {
|
|
109
|
+
resolveReady = resolve;
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const clients = new Set<any>();
|
|
113
|
+
const state: DevtoolsTransportState = {
|
|
114
|
+
port: actualPort,
|
|
115
|
+
connected: false,
|
|
116
|
+
clients,
|
|
117
|
+
httpServer,
|
|
118
|
+
ready,
|
|
119
|
+
close: async () => {
|
|
120
|
+
await new Promise<void>((resolve) => wss.close(() => resolve()));
|
|
121
|
+
await new Promise<void>((resolve) => httpServer.close(() => resolve()));
|
|
122
|
+
clients.clear();
|
|
123
|
+
state.connected = false;
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
wss.on("connection", (socket) => {
|
|
128
|
+
if (state.connected) {
|
|
129
|
+
socket.close(1000, "Another devtools client is already connected");
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
clients.add(socket);
|
|
134
|
+
state.connected = true;
|
|
135
|
+
resolveReady?.();
|
|
136
|
+
options.onConnection(socket);
|
|
137
|
+
|
|
138
|
+
socket.on("message", (data) => {
|
|
139
|
+
try {
|
|
140
|
+
options.onMessage(JSON.parse(String(data)), socket);
|
|
141
|
+
} catch {
|
|
142
|
+
// ignore malformed messages
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
socket.on("close", () => {
|
|
147
|
+
clients.delete(socket);
|
|
148
|
+
state.connected = clients.size > 0;
|
|
149
|
+
options.onDisconnect(socket);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
return state;
|
|
154
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
// Individual server→client message types
|
|
3
|
+
ClientToServerMessage,
|
|
4
|
+
DebuggerInfoMessage,
|
|
5
|
+
DiagnosticRow,
|
|
6
|
+
DiagnosticsReportMessage,
|
|
7
|
+
DirectoryAddedMessage,
|
|
8
|
+
DirectoryRemovedMessage,
|
|
9
|
+
EffectAddedMessage,
|
|
10
|
+
EffectEdgeUpdatedMessage,
|
|
11
|
+
EffectTrackMessage,
|
|
12
|
+
EffectTriggerMessage,
|
|
13
|
+
EffectUpdatedMessage,
|
|
14
|
+
FileAddedMessage,
|
|
15
|
+
FileRemovedMessage,
|
|
16
|
+
FileUpdatedMessage,
|
|
17
|
+
FlushJobsCompleteMessage,
|
|
18
|
+
RefAddedMessage,
|
|
19
|
+
RenderCompleteMessage,
|
|
20
|
+
RenderErrorMessage,
|
|
21
|
+
RenderNodeAddedMessage,
|
|
22
|
+
RenderNodeRemovedMessage,
|
|
23
|
+
RenderNodeUpdatedMessage,
|
|
24
|
+
RenderResetMessage,
|
|
25
|
+
// Individual client→server message types
|
|
26
|
+
RerenderBreakRequestMessage,
|
|
27
|
+
RerenderRequestMessage,
|
|
28
|
+
ScopeAddedMessage,
|
|
29
|
+
ScopeRemovedMessage,
|
|
30
|
+
ScopeUpdatedMessage,
|
|
31
|
+
ServerToClientMessage,
|
|
32
|
+
SourceLocation,
|
|
33
|
+
SubscribeMessage,
|
|
34
|
+
SymbolAddedMessage,
|
|
35
|
+
SymbolRemovedMessage,
|
|
36
|
+
SymbolUpdatedMessage,
|
|
37
|
+
UnsubscribeMessage,
|
|
38
|
+
} from "./devtools/devtools-protocol.js";
|
|
39
|
+
export {
|
|
40
|
+
enableDevtools,
|
|
41
|
+
enableDevtoolsAndConnect,
|
|
42
|
+
resetDevtoolsServerForTests,
|
|
43
|
+
waitForDevtoolsConnection,
|
|
44
|
+
} from "./devtools/devtools-server.browser.js";
|
|
45
|
+
export type {
|
|
46
|
+
DevtoolsServerInfo,
|
|
47
|
+
EnableDevtoolsOptions,
|
|
48
|
+
} from "./devtools/devtools-server.browser.js";
|