@snap/valdi 1.0.7 → 1.0.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/.bootstrap/modules/{ios_android_bridge_module → polyglot_bridge_module}/BUILD.bazel.template +16 -1
- package/.bootstrap/modules/polyglot_bridge_module/web/Calculator.ts +11 -0
- package/.bootstrap/modules/polyglot_bridge_module/web/tsconfig.json +16 -0
- package/.bootstrap/modules/polyglot_view_module/BUILD.bazel.template +75 -0
- package/.bootstrap/modules/polyglot_view_module/android/PolyglotNativeView.kt.template +41 -0
- package/.bootstrap/modules/polyglot_view_module/android/PolyglotNativeViewAttributesBinder.kt.template +17 -0
- package/.bootstrap/modules/polyglot_view_module/ios/SCPolyglotView.h +8 -0
- package/.bootstrap/modules/polyglot_view_module/ios/SCPolyglotView.m +23 -0
- package/.bootstrap/modules/polyglot_view_module/macos/SCPolyglotView.h +8 -0
- package/.bootstrap/modules/polyglot_view_module/macos/SCPolyglotView.m +23 -0
- package/.bootstrap/modules/polyglot_view_module/src/PolyglotCustomView.tsx.template +23 -0
- package/.bootstrap/modules/polyglot_view_module/web/PolyglotWeb.ts.template +30 -0
- package/.bootstrap/modules/polyglot_view_module/web/tsconfig.json +16 -0
- package/.metadata/AGENTS.md.template +8 -274
- package/.metadata/skill-reference.tsx.template +8 -0
- package/.metadata/skill-tests-BUILD.bazel.template +21 -0
- package/.metadata/skill-tests-README.md.template +17 -0
- package/.metadata/skill.md.template +27 -0
- package/README.md +12 -3
- package/bundled-skills/CONTRIBUTING_SKILLS.md +74 -0
- package/bundled-skills/registry.json +21 -0
- package/bundled-skills/skills/valdi-android/skill.md +89 -0
- package/bundled-skills/skills/valdi-async/skill.md +246 -0
- package/bundled-skills/skills/valdi-async/tests/BUILD.bazel +19 -0
- package/bundled-skills/skills/valdi-async/tests/README.md +25 -0
- package/bundled-skills/skills/valdi-async/tests/src/reference.tsx +210 -0
- package/bundled-skills/skills/valdi-async/tests/tsconfig.json +1 -0
- package/bundled-skills/skills/valdi-bazel/skill.md +141 -0
- package/bundled-skills/skills/valdi-compiler/skill.md +108 -0
- package/bundled-skills/skills/valdi-component-tests/skill.md +430 -0
- package/bundled-skills/skills/valdi-component-tests/tests/BUILD.bazel +24 -0
- package/bundled-skills/skills/valdi-component-tests/tests/README.md +35 -0
- package/bundled-skills/skills/valdi-component-tests/tests/src/ProfileCard.tsx +38 -0
- package/bundled-skills/skills/valdi-component-tests/tests/test/ProfileCardTest.spec.tsx +107 -0
- package/bundled-skills/skills/valdi-component-tests/tests/tsconfig.json +1 -0
- package/bundled-skills/skills/valdi-cpp-runtime/skill.md +103 -0
- package/bundled-skills/skills/valdi-custom-view/skill.md +64 -0
- package/bundled-skills/skills/valdi-inspect/skill.md +199 -0
- package/bundled-skills/skills/valdi-ios/skill.md +93 -0
- package/bundled-skills/skills/valdi-migrate/skill.md +175 -0
- package/bundled-skills/skills/valdi-migrate/tests/BUILD.bazel +23 -0
- package/bundled-skills/skills/valdi-migrate/tests/README.md +70 -0
- package/bundled-skills/skills/valdi-migrate/tests/check_antipatterns.py +89 -0
- package/bundled-skills/skills/valdi-migrate/tests/compose_example.kt +94 -0
- package/bundled-skills/skills/valdi-migrate/tests/flutter_example.dart +129 -0
- package/bundled-skills/skills/valdi-migrate/tests/react_example.tsx +98 -0
- package/bundled-skills/skills/valdi-migrate/tests/src/expected_valdi.tsx +267 -0
- package/bundled-skills/skills/valdi-migrate/tests/tsconfig.json +3 -0
- package/bundled-skills/skills/valdi-overview/skill.md +63 -0
- package/bundled-skills/skills/valdi-perf/skill.md +197 -0
- package/bundled-skills/skills/valdi-perf/tests/BUILD.bazel +19 -0
- package/bundled-skills/skills/valdi-perf/tests/README.md +26 -0
- package/bundled-skills/skills/valdi-perf/tests/src/reference.tsx +202 -0
- package/bundled-skills/skills/valdi-perf/tests/tsconfig.json +1 -0
- package/bundled-skills/skills/valdi-polyglot-module/skill.md +124 -0
- package/bundled-skills/skills/valdi-profile/skill.md +126 -0
- package/bundled-skills/skills/valdi-setup/skill.md +129 -0
- package/bundled-skills/skills/valdi-testing/skill.md +65 -0
- package/bundled-skills/skills/valdi-tsx/skill.md +385 -0
- package/bundled-skills/skills/valdi-tsx/tests/BUILD.bazel +19 -0
- package/bundled-skills/skills/valdi-tsx/tests/README.md +29 -0
- package/bundled-skills/skills/valdi-tsx/tests/src/reference.tsx +173 -0
- package/bundled-skills/skills/valdi-tsx/tests/tsconfig.json +1 -0
- package/dist/commands/bootstrap.js +0 -7
- package/dist/commands/inspect.js +16 -0
- package/dist/commands/inspect_commands/contexts.js +55 -0
- package/dist/commands/inspect_commands/devices.js +53 -0
- package/dist/commands/inspect_commands/heap.js +59 -0
- package/dist/commands/inspect_commands/select.js +43 -0
- package/dist/commands/inspect_commands/snapshot.js +64 -0
- package/dist/commands/inspect_commands/status.js +68 -0
- package/dist/commands/inspect_commands/tree.js +86 -0
- package/dist/commands/newModule.js +10 -14
- package/dist/commands/profile.js +16 -0
- package/dist/commands/profile_commands/capture.js +106 -0
- package/dist/commands/skills.js +16 -0
- package/dist/commands/skills_commands/create.js +125 -0
- package/dist/commands/skills_commands/install.js +96 -0
- package/dist/commands/skills_commands/list.js +83 -0
- package/dist/commands/skills_commands/remove.js +41 -0
- package/dist/commands/skills_commands/update.js +59 -0
- package/dist/core/constants.js +0 -1
- package/dist/core/packageFiles.spec.js +29 -0
- package/dist/setup/versions.js +1 -1
- package/dist/utils/daemonClient.js +342 -0
- package/dist/utils/hermesClient.js +252 -0
- package/dist/utils/skillsAdapters.js +140 -0
- package/dist/utils/skillsRegistry.js +40 -0
- package/dist/utils/stringUtils.spec.js +1 -1
- package/package.json +20 -5
- package/.bazelignore +0 -2
- package/.bootstrap/modules/cpp_bridge_module/BUILD.bazel.template +0 -21
- package/.bootstrap/modules/cpp_bridge_module/cpp/CalculatorModuleFactory.cpp.template +0 -28
- package/.bootstrap/modules/ios_android_bridge_module/src/Calculator.d.ts +0 -4
- package/.bootstrap/modules/ios_android_view_module/BUILD.bazel.template +0 -41
- package/.bootstrap/modules/ios_android_view_module/android/CircleView.kt.template +0 -42
- package/.bootstrap/modules/ios_android_view_module/android/CircleViewAttributesBinder.kt.template +0 -60
- package/.bootstrap/modules/ios_android_view_module/ios/SCCCircleView.h +0 -6
- package/.bootstrap/modules/ios_android_view_module/ios/SCCCircleView.m +0 -82
- package/.bootstrap/modules/ios_android_view_module/src/Circle.tsx.template +0 -15
- package/.config/copyconfig.json +0 -10
- package/.editorconfig +0 -21
- package/.eslintignore +0 -1
- package/.eslintrc.js +0 -72
- package/.github/README.md +0 -164
- package/.metadata/.cursorrules.template +0 -60
- package/.prettierignore +0 -2
- package/.prettierrc.json +0 -7
- package/LINUX_COMPATIBILITY.md +0 -248
- package/prettier.config.js +0 -4
- package/scripts/sync-agents-md.sh +0 -44
- package/scripts/update-agents-templates.ts +0 -170
- package/spec/support/jasmine.json +0 -6
- package/src/commands/bootstrap.ts +0 -410
- package/src/commands/build.ts +0 -89
- package/src/commands/devSetup.ts +0 -17
- package/src/commands/doctor.ts +0 -1832
- package/src/commands/export.ts +0 -131
- package/src/commands/hotreload.ts +0 -108
- package/src/commands/install.ts +0 -93
- package/src/commands/lint.ts +0 -15
- package/src/commands/lint_commands/lintCheck.ts +0 -53
- package/src/commands/lint_commands/lintFormat.ts +0 -49
- package/src/commands/log.ts +0 -108
- package/src/commands/newModule.ts +0 -286
- package/src/commands/package.ts +0 -102
- package/src/commands/projectsync.ts +0 -488
- package/src/commands/test.ts +0 -68
- package/src/core/constants.ts +0 -98
- package/src/core/errors.ts +0 -10
- package/src/index.ts +0 -27
- package/src/setup/DevSetupHelper.ts +0 -290
- package/src/setup/linuxSetup.ts +0 -261
- package/src/setup/macOSSetup.ts +0 -156
- package/src/setup/setupEntryPoint.ts +0 -64
- package/src/setup/versions.ts +0 -8
- package/src/utils/ArgumentsResolver.ts +0 -23
- package/src/utils/BazelClient.ts +0 -285
- package/src/utils/Digraph.ts +0 -81
- package/src/utils/LoadingIndicator.ts +0 -43
- package/src/utils/applicationUtils.ts +0 -276
- package/src/utils/buildInfo.ts +0 -99
- package/src/utils/byString.ts +0 -3
- package/src/utils/cliUtils.ts +0 -112
- package/src/utils/copyBootstrapFiles.ts +0 -30
- package/src/utils/deviceUtils.ts +0 -76
- package/src/utils/errorUtils.ts +0 -63
- package/src/utils/fileUtils.ts +0 -257
- package/src/utils/githubUtils.ts +0 -102
- package/src/utils/jsonUtils.ts +0 -30
- package/src/utils/lintUtils.ts +0 -190
- package/src/utils/linuxDistro.spec.ts +0 -219
- package/src/utils/linuxDistro.ts +0 -326
- package/src/utils/logUtils.ts +0 -9
- package/src/utils/pathUtils.ts +0 -12
- package/src/utils/stringUtils.spec.ts +0 -95
- package/src/utils/stringUtils.ts +0 -103
- package/src/utils/tempDir.ts +0 -24
- package/src/utils/zipUtils.ts +0 -14
- package/test/ValdiSmokeTest.ts +0 -279
- package/test/helpers/AsyncHelpers.ts +0 -113
- package/test/helpers/CommandHelpers.ts +0 -37
- package/test/helpers/ProjectHelpers.ts +0 -153
- package/test/helpers/StreamHelpers.ts +0 -37
- package/test/helpers/TypeScriptClient.ts +0 -217
- package/tsconfig.dist.json +0 -8
- package/tsconfig.json +0 -42
- /package/.bootstrap/modules/{ios_android_bridge_module → polyglot_bridge_module}/android/CalculatorModuleFactory.kt.template +0 -0
- /package/.bootstrap/modules/{ios_android_bridge_module → polyglot_bridge_module}/ios/SCCCalculatorModuleFactory.m.template +0 -0
- /package/.bootstrap/modules/{cpp_bridge_module → polyglot_bridge_module}/src/Calculator.d.ts +0 -0
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DaemonConnection = exports.DaemonMsgType = exports.DEFAULT_PORT = exports.MOBILE_PORT = exports.STANDALONE_PORT = void 0;
|
|
4
|
+
exports.loadInspectConfig = loadInspectConfig;
|
|
5
|
+
exports.saveInspectConfig = saveInspectConfig;
|
|
6
|
+
exports.connectToDaemon = connectToDaemon;
|
|
7
|
+
exports.resolveContextId = resolveContextId;
|
|
8
|
+
exports.resolveClientId = resolveClientId;
|
|
9
|
+
const tslib_1 = require("tslib");
|
|
10
|
+
const net = tslib_1.__importStar(require("net"));
|
|
11
|
+
const os = tslib_1.__importStar(require("os"));
|
|
12
|
+
const path = tslib_1.__importStar(require("path"));
|
|
13
|
+
const fs = tslib_1.__importStar(require("fs"));
|
|
14
|
+
const errors_1 = require("../core/errors");
|
|
15
|
+
const cliUtils_1 = require("./cliUtils");
|
|
16
|
+
exports.STANDALONE_PORT = 13591;
|
|
17
|
+
exports.MOBILE_PORT = 13592;
|
|
18
|
+
exports.DEFAULT_PORT = exports.MOBILE_PORT;
|
|
19
|
+
var DaemonMsgType;
|
|
20
|
+
(function (DaemonMsgType) {
|
|
21
|
+
DaemonMsgType[DaemonMsgType["LIST_CONTEXTS_REQUEST"] = 2] = "LIST_CONTEXTS_REQUEST";
|
|
22
|
+
DaemonMsgType[DaemonMsgType["LIST_CONTEXTS_RESPONSE"] = -2] = "LIST_CONTEXTS_RESPONSE";
|
|
23
|
+
DaemonMsgType[DaemonMsgType["GET_CONTEXT_TREE_REQUEST"] = 3] = "GET_CONTEXT_TREE_REQUEST";
|
|
24
|
+
DaemonMsgType[DaemonMsgType["GET_CONTEXT_TREE_RESPONSE"] = -3] = "GET_CONTEXT_TREE_RESPONSE";
|
|
25
|
+
DaemonMsgType[DaemonMsgType["TAKE_ELEMENT_SNAPSHOT_REQUEST"] = 4] = "TAKE_ELEMENT_SNAPSHOT_REQUEST";
|
|
26
|
+
DaemonMsgType[DaemonMsgType["TAKE_ELEMENT_SNAPSHOT_RESPONSE"] = -4] = "TAKE_ELEMENT_SNAPSHOT_RESPONSE";
|
|
27
|
+
DaemonMsgType[DaemonMsgType["DUMP_HEAP_REQUEST"] = 5] = "DUMP_HEAP_REQUEST";
|
|
28
|
+
DaemonMsgType[DaemonMsgType["DUMP_HEAP_RESPONSE"] = -5] = "DUMP_HEAP_RESPONSE";
|
|
29
|
+
})(DaemonMsgType || (exports.DaemonMsgType = DaemonMsgType = {}));
|
|
30
|
+
const CONFIG_PATH = path.join(os.homedir(), '.valdi-inspect.json');
|
|
31
|
+
function loadInspectConfig() {
|
|
32
|
+
try {
|
|
33
|
+
return JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf8'));
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return {};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function saveInspectConfig(config) {
|
|
40
|
+
fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), 'utf8');
|
|
41
|
+
}
|
|
42
|
+
const MAGIC = Buffer.from([0x33, 0xc6, 0x00, 0x01]);
|
|
43
|
+
const HEADER_SIZE = 8;
|
|
44
|
+
function encodePacket(json) {
|
|
45
|
+
const payload = Buffer.from(JSON.stringify(json), 'utf8');
|
|
46
|
+
const header = Buffer.alloc(8);
|
|
47
|
+
MAGIC.copy(header, 0);
|
|
48
|
+
header.writeUInt32LE(payload.length, 4);
|
|
49
|
+
return Buffer.concat([header, payload]);
|
|
50
|
+
}
|
|
51
|
+
const DIRECT_CLIENT_ID = 1;
|
|
52
|
+
class DaemonConnection {
|
|
53
|
+
socket;
|
|
54
|
+
recvBuf = Buffer.alloc(0);
|
|
55
|
+
recvChunks = [];
|
|
56
|
+
recvLen = 0;
|
|
57
|
+
reqCounter = 0;
|
|
58
|
+
msgCounter = 0;
|
|
59
|
+
pendingRequests = new Map();
|
|
60
|
+
payloadListeners = new Map();
|
|
61
|
+
configureReady = null;
|
|
62
|
+
configureData = null;
|
|
63
|
+
constructor(socket) {
|
|
64
|
+
this.socket = socket;
|
|
65
|
+
socket.on('data', (data) => this.onData(data));
|
|
66
|
+
socket.on('close', () => this.rejectAllPending(new Error('Connection closed unexpectedly')));
|
|
67
|
+
socket.on('error', (err) => this.rejectAllPending(err));
|
|
68
|
+
}
|
|
69
|
+
rejectAllPending(err) {
|
|
70
|
+
this.configureReady?.reject(err);
|
|
71
|
+
this.configureReady = null;
|
|
72
|
+
for (const pending of this.pendingRequests.values())
|
|
73
|
+
pending.reject(err);
|
|
74
|
+
for (const listener of this.payloadListeners.values())
|
|
75
|
+
listener.reject(err);
|
|
76
|
+
this.pendingRequests.clear();
|
|
77
|
+
this.payloadListeners.clear();
|
|
78
|
+
}
|
|
79
|
+
onData(chunk) {
|
|
80
|
+
this.recvChunks.push(chunk);
|
|
81
|
+
this.recvLen += chunk.length;
|
|
82
|
+
this.drainBuffer();
|
|
83
|
+
}
|
|
84
|
+
drainBuffer() {
|
|
85
|
+
for (;;) {
|
|
86
|
+
if (this.recvLen < HEADER_SIZE)
|
|
87
|
+
break;
|
|
88
|
+
if (this.recvBuf.length < this.recvLen) {
|
|
89
|
+
this.recvBuf = Buffer.concat(this.recvChunks, this.recvLen);
|
|
90
|
+
this.recvChunks = [this.recvBuf];
|
|
91
|
+
}
|
|
92
|
+
if (this.recvBuf[0] !== 0x33 ||
|
|
93
|
+
this.recvBuf[1] !== 0xc6 ||
|
|
94
|
+
this.recvBuf[2] !== 0x00 ||
|
|
95
|
+
this.recvBuf[3] !== 0x01) {
|
|
96
|
+
this.socket.destroy(new Error('ValdiPacket: bad magic'));
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
const payloadLen = this.recvBuf.readUInt32LE(4);
|
|
100
|
+
if (this.recvLen < HEADER_SIZE + payloadLen)
|
|
101
|
+
break;
|
|
102
|
+
const raw = this.recvBuf.subarray(HEADER_SIZE, HEADER_SIZE + payloadLen).toString('utf8');
|
|
103
|
+
const consumed = HEADER_SIZE + payloadLen;
|
|
104
|
+
this.recvBuf = this.recvBuf.subarray(consumed);
|
|
105
|
+
this.recvLen -= consumed;
|
|
106
|
+
this.recvChunks = this.recvLen > 0 ? [this.recvBuf] : [];
|
|
107
|
+
try {
|
|
108
|
+
this.dispatchMessage(JSON.parse(raw));
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
dispatchMessage(msg) {
|
|
115
|
+
if (msg['request']) {
|
|
116
|
+
const req = msg['request'];
|
|
117
|
+
const reqId = req['request_id'];
|
|
118
|
+
const respKey = Object.keys(req).find((k) => k !== 'request_id');
|
|
119
|
+
if (respKey) {
|
|
120
|
+
this.socket.write(encodePacket({ response: { [respKey]: {}, request_id: reqId } }));
|
|
121
|
+
if (respKey === 'configure') {
|
|
122
|
+
this.configureData = req['configure'];
|
|
123
|
+
if (this.configureReady) {
|
|
124
|
+
const waiter = this.configureReady;
|
|
125
|
+
this.configureReady = null;
|
|
126
|
+
waiter.resolve(req);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
else if (respKey === 'forward_client_payload') {
|
|
130
|
+
const fcp = req['forward_client_payload'];
|
|
131
|
+
if (fcp) {
|
|
132
|
+
try {
|
|
133
|
+
const inner = JSON.parse(fcp['payload_string']);
|
|
134
|
+
const msgId = String(inner['requestId']);
|
|
135
|
+
const listener = this.payloadListeners.get(msgId);
|
|
136
|
+
if (listener) {
|
|
137
|
+
this.payloadListeners.delete(msgId);
|
|
138
|
+
listener.resolve(inner);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else if (msg['response']) {
|
|
148
|
+
const resp = msg['response'];
|
|
149
|
+
const reqId = resp['request_id'];
|
|
150
|
+
const pending = this.pendingRequests.get(reqId);
|
|
151
|
+
if (pending) {
|
|
152
|
+
this.pendingRequests.delete(reqId);
|
|
153
|
+
if (resp['error']) {
|
|
154
|
+
const errMsg = resp['error']['error_message'];
|
|
155
|
+
pending.reject(new Error(errMsg));
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
pending.resolve(resp);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
sendRequest(payload, timeoutMs = 5_000) {
|
|
164
|
+
const reqId = String(++this.reqCounter);
|
|
165
|
+
const envelope = { request: { ...payload, request_id: reqId } };
|
|
166
|
+
return new Promise((resolve, reject) => {
|
|
167
|
+
const timer = setTimeout(() => {
|
|
168
|
+
this.pendingRequests.delete(reqId);
|
|
169
|
+
reject(new errors_1.CliError(`Valdi daemon did not respond (port ${(this.socket.remotePort ?? '?')}).\n` +
|
|
170
|
+
`Is the hot-reloader actually running and connected?`));
|
|
171
|
+
}, timeoutMs);
|
|
172
|
+
this.pendingRequests.set(reqId, {
|
|
173
|
+
resolve: (v) => { clearTimeout(timer); resolve(v); },
|
|
174
|
+
reject: (e) => { clearTimeout(timer); reject(e); },
|
|
175
|
+
});
|
|
176
|
+
this.socket.write(encodePacket(envelope), (err) => {
|
|
177
|
+
if (err) {
|
|
178
|
+
clearTimeout(timer);
|
|
179
|
+
this.pendingRequests.delete(reqId);
|
|
180
|
+
reject(err);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
nextMsgId() {
|
|
186
|
+
return String(++this.msgCounter);
|
|
187
|
+
}
|
|
188
|
+
configure() {
|
|
189
|
+
if (this.configureData)
|
|
190
|
+
return Promise.resolve();
|
|
191
|
+
return new Promise((resolve, reject) => {
|
|
192
|
+
const timer = setTimeout(() => {
|
|
193
|
+
this.configureReady = null;
|
|
194
|
+
reject(new errors_1.CliError(`Valdi daemon did not respond (port ${this.socket.remotePort ?? '?'}).\n` +
|
|
195
|
+
`Is the hot-reloader actually running and connected?`));
|
|
196
|
+
}, 5_000);
|
|
197
|
+
this.configureReady = {
|
|
198
|
+
resolve: () => { clearTimeout(timer); resolve(); },
|
|
199
|
+
reject: (e) => { clearTimeout(timer); reject(e); },
|
|
200
|
+
};
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
listConnectedClients() {
|
|
204
|
+
if (this.configureData) {
|
|
205
|
+
const client = {
|
|
206
|
+
client_id: String(DIRECT_CLIENT_ID),
|
|
207
|
+
platform: String(this.configureData['platform'] ?? 'unknown'),
|
|
208
|
+
application_id: String(this.configureData['application_id'] ?? 'unknown'),
|
|
209
|
+
};
|
|
210
|
+
return Promise.resolve([client]);
|
|
211
|
+
}
|
|
212
|
+
return Promise.resolve([]);
|
|
213
|
+
}
|
|
214
|
+
async forwardAndWait(_clientId, msgType, body, timeoutMs = 15_000) {
|
|
215
|
+
const msgId = this.nextMsgId();
|
|
216
|
+
const payloadString = JSON.stringify({ type: msgType, requestId: msgId, body });
|
|
217
|
+
const resultPromise = new Promise((resolve, reject) => {
|
|
218
|
+
const timer = setTimeout(() => {
|
|
219
|
+
this.payloadListeners.delete(msgId);
|
|
220
|
+
reject(new Error('Timeout waiting for device response. Is the app running?'));
|
|
221
|
+
}, timeoutMs);
|
|
222
|
+
this.payloadListeners.set(msgId, {
|
|
223
|
+
resolve: (v) => { clearTimeout(timer); resolve(v); },
|
|
224
|
+
reject: (e) => { clearTimeout(timer); reject(e); },
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
this.socket.write(encodePacket({
|
|
228
|
+
event: {
|
|
229
|
+
payload_from_client: {
|
|
230
|
+
sender_client_id: DIRECT_CLIENT_ID,
|
|
231
|
+
payload_string: payloadString,
|
|
232
|
+
},
|
|
233
|
+
},
|
|
234
|
+
}), (err) => {
|
|
235
|
+
if (err) {
|
|
236
|
+
const listener = this.payloadListeners.get(msgId);
|
|
237
|
+
if (listener) {
|
|
238
|
+
this.payloadListeners.delete(msgId);
|
|
239
|
+
listener.reject(err);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
return resultPromise;
|
|
244
|
+
}
|
|
245
|
+
async listContexts(clientId) {
|
|
246
|
+
const resp = await this.forwardAndWait(clientId, DaemonMsgType.LIST_CONTEXTS_REQUEST, {});
|
|
247
|
+
return (resp['body'] ?? []);
|
|
248
|
+
}
|
|
249
|
+
async getContextTree(clientId, contextId) {
|
|
250
|
+
const resp = await this.forwardAndWait(clientId, DaemonMsgType.GET_CONTEXT_TREE_REQUEST, { id: contextId });
|
|
251
|
+
return resp['body'];
|
|
252
|
+
}
|
|
253
|
+
async takeSnapshot(clientId, elementId, contextId) {
|
|
254
|
+
const resp = await this.forwardAndWait(clientId, DaemonMsgType.TAKE_ELEMENT_SNAPSHOT_REQUEST, {
|
|
255
|
+
elementId: parseInt(elementId, 10),
|
|
256
|
+
contextId,
|
|
257
|
+
});
|
|
258
|
+
return resp['body'];
|
|
259
|
+
}
|
|
260
|
+
async dumpHeap(clientId, performGC = false) {
|
|
261
|
+
const resp = await this.forwardAndWait(clientId, DaemonMsgType.DUMP_HEAP_REQUEST, { performGC }, 60_000);
|
|
262
|
+
return resp['body'];
|
|
263
|
+
}
|
|
264
|
+
close() {
|
|
265
|
+
this.socket.destroy();
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
exports.DaemonConnection = DaemonConnection;
|
|
269
|
+
async function tryAdbForward(port) {
|
|
270
|
+
try {
|
|
271
|
+
await (0, cliUtils_1.runCliCommand)(`adb forward tcp:${port} tcp:${port}`);
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
async function connectToDaemon(port = exports.DEFAULT_PORT) {
|
|
277
|
+
await tryAdbForward(port);
|
|
278
|
+
return new Promise((resolve, reject) => {
|
|
279
|
+
const socket = net.createConnection({ port, host: '127.0.0.1' });
|
|
280
|
+
const timer = setTimeout(() => {
|
|
281
|
+
socket.destroy();
|
|
282
|
+
reject(new Error(`Timeout connecting to Valdi daemon on port ${port}. Is the hot-reloader running?`));
|
|
283
|
+
}, 5_000);
|
|
284
|
+
socket.once('connect', () => {
|
|
285
|
+
clearTimeout(timer);
|
|
286
|
+
resolve(new DaemonConnection(socket));
|
|
287
|
+
});
|
|
288
|
+
socket.once('error', (err) => {
|
|
289
|
+
clearTimeout(timer);
|
|
290
|
+
if (err.code === 'ECONNREFUSED') {
|
|
291
|
+
reject(new errors_1.CliError(`Valdi daemon not running on port ${port}.\n` +
|
|
292
|
+
`Start the hot-reloader (valdi hotreload) or pass --port to use a different port.`));
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
reject(err);
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
async function resolveContextId(conn, clientId, contextIdOverride) {
|
|
301
|
+
const contexts = await conn.listContexts(clientId);
|
|
302
|
+
if (contexts.length === 0) {
|
|
303
|
+
throw new errors_1.CliError('No contexts found on the connected device.');
|
|
304
|
+
}
|
|
305
|
+
if (contextIdOverride) {
|
|
306
|
+
if (!contexts.some((c) => c.id === contextIdOverride)) {
|
|
307
|
+
throw new errors_1.CliError(`Context "${contextIdOverride}" not found. Run "valdi inspect contexts" to see available contexts.`);
|
|
308
|
+
}
|
|
309
|
+
return contextIdOverride;
|
|
310
|
+
}
|
|
311
|
+
if (contexts.length === 1) {
|
|
312
|
+
return contexts[0].id;
|
|
313
|
+
}
|
|
314
|
+
return (0, cliUtils_1.getUserChoice)(contexts.map((c) => ({
|
|
315
|
+
name: `${c.rootComponentName} [${c.id}]`,
|
|
316
|
+
value: c.id,
|
|
317
|
+
})), 'Multiple contexts found. Select one:');
|
|
318
|
+
}
|
|
319
|
+
async function resolveClientId(conn, clientIdOverride) {
|
|
320
|
+
const clients = await conn.listConnectedClients();
|
|
321
|
+
if (clients.length === 0) {
|
|
322
|
+
throw new errors_1.CliError('No devices connected to the Valdi daemon.\n' +
|
|
323
|
+
'Make sure the Valdi app is running and connected to the hot-reloader.');
|
|
324
|
+
}
|
|
325
|
+
if (clientIdOverride) {
|
|
326
|
+
if (!clients.some((c) => c.client_id === clientIdOverride)) {
|
|
327
|
+
throw new errors_1.CliError(`Client "${clientIdOverride}" not found. Run "valdi inspect devices" to see connected clients.`);
|
|
328
|
+
}
|
|
329
|
+
return clientIdOverride;
|
|
330
|
+
}
|
|
331
|
+
const config = loadInspectConfig();
|
|
332
|
+
if (config.selectedClientId && clients.some((c) => c.client_id === config.selectedClientId)) {
|
|
333
|
+
return config.selectedClientId;
|
|
334
|
+
}
|
|
335
|
+
if (clients.length === 1) {
|
|
336
|
+
return clients[0].client_id;
|
|
337
|
+
}
|
|
338
|
+
return (0, cliUtils_1.getUserChoice)(clients.map((c) => ({
|
|
339
|
+
name: `${c.application_id} (${c.platform}) [${c.client_id}]`,
|
|
340
|
+
value: c.client_id,
|
|
341
|
+
})), 'Multiple devices connected. Select one:');
|
|
342
|
+
}
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HermesConnection = exports.NotHermesError = exports.HERMES_BUILD_FLAG = exports.HERMES_PORT = void 0;
|
|
4
|
+
exports.listHermesDevices = listHermesDevices;
|
|
5
|
+
exports.normalizeHermesProfile = normalizeHermesProfile;
|
|
6
|
+
const tslib_1 = require("tslib");
|
|
7
|
+
const net = tslib_1.__importStar(require("net"));
|
|
8
|
+
const crypto = tslib_1.__importStar(require("crypto"));
|
|
9
|
+
const http = tslib_1.__importStar(require("http"));
|
|
10
|
+
const errors_1 = require("../core/errors");
|
|
11
|
+
exports.HERMES_PORT = 13595;
|
|
12
|
+
exports.HERMES_BUILD_FLAG = '--@valdi//bzl/valdi:js_engine=hermes --@valdi//bzl/valdi:js_bytecode_format=hermes';
|
|
13
|
+
class NotHermesError extends errors_1.CliError {
|
|
14
|
+
constructor(port) {
|
|
15
|
+
super(`Hermes debug socket not found on port ${port}.\n\n` +
|
|
16
|
+
`CPU profiling requires:\n` +
|
|
17
|
+
` 1. A Hermes build (the default JS engine is QuickJS):\n` +
|
|
18
|
+
` valdi install android --bazel_args="${exports.HERMES_BUILD_FLAG}"\n` +
|
|
19
|
+
` valdi install ios --bazel_args="${exports.HERMES_BUILD_FLAG}"\n\n` +
|
|
20
|
+
` 2. The hot-reloader running (it establishes the debug tunnel on port ${port}):\n` +
|
|
21
|
+
` valdi hotreload android\n`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.NotHermesError = NotHermesError;
|
|
25
|
+
async function listHermesDevices(port) {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
const timer = setTimeout(() => {
|
|
28
|
+
req.destroy();
|
|
29
|
+
reject(new NotHermesError(port));
|
|
30
|
+
}, 3_000);
|
|
31
|
+
const req = http.get(`http://127.0.0.1:${port}/json`, (res) => {
|
|
32
|
+
let body = '';
|
|
33
|
+
res.on('data', (chunk) => { body += chunk; });
|
|
34
|
+
res.on('end', () => {
|
|
35
|
+
clearTimeout(timer);
|
|
36
|
+
try {
|
|
37
|
+
resolve(JSON.parse(body));
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
reject(new errors_1.CliError(`Invalid response from Hermes debug server on port ${port}`));
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
req.on('error', (err) => {
|
|
45
|
+
clearTimeout(timer);
|
|
46
|
+
if (err.code === 'ECONNREFUSED') {
|
|
47
|
+
reject(new NotHermesError(port));
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
reject(err);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
function encodeWsFrame(text) {
|
|
56
|
+
const payload = Buffer.from(text, 'utf8');
|
|
57
|
+
const mask = crypto.randomBytes(4);
|
|
58
|
+
const masked = Buffer.alloc(payload.length);
|
|
59
|
+
for (let i = 0; i < payload.length; i++) {
|
|
60
|
+
masked[i] = payload[i] ^ mask[i % 4];
|
|
61
|
+
}
|
|
62
|
+
const len = payload.length;
|
|
63
|
+
let header;
|
|
64
|
+
if (len < 126) {
|
|
65
|
+
header = Buffer.alloc(6);
|
|
66
|
+
header[0] = 0x81;
|
|
67
|
+
header[1] = 0x80 | len;
|
|
68
|
+
mask.copy(header, 2);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
header = Buffer.alloc(8);
|
|
72
|
+
header[0] = 0x81;
|
|
73
|
+
header[1] = 0x80 | 126;
|
|
74
|
+
header.writeUInt16BE(len, 2);
|
|
75
|
+
mask.copy(header, 4);
|
|
76
|
+
}
|
|
77
|
+
return Buffer.concat([header, masked]);
|
|
78
|
+
}
|
|
79
|
+
function normalizeHermesProfile(profile) {
|
|
80
|
+
const { samples = [], timeDeltas = [], startTime, endTime, nodes } = profile;
|
|
81
|
+
if (samples.length < 2 || timeDeltas.length < 2)
|
|
82
|
+
return profile;
|
|
83
|
+
const absTimes = [];
|
|
84
|
+
let t = startTime;
|
|
85
|
+
for (const d of timeDeltas) {
|
|
86
|
+
t += d;
|
|
87
|
+
absTimes.push(t);
|
|
88
|
+
}
|
|
89
|
+
const realStart = absTimes[1];
|
|
90
|
+
const realAbsTimes = absTimes.slice(1);
|
|
91
|
+
const realSamples = samples.slice(1);
|
|
92
|
+
const newDeltas = [0];
|
|
93
|
+
for (let i = 1; i < realAbsTimes.length; i++) {
|
|
94
|
+
newDeltas.push(realAbsTimes[i] - realAbsTimes[i - 1]);
|
|
95
|
+
}
|
|
96
|
+
return { nodes, startTime: realStart, endTime, samples: realSamples, timeDeltas: newDeltas };
|
|
97
|
+
}
|
|
98
|
+
class HermesConnection {
|
|
99
|
+
socket;
|
|
100
|
+
recvBuf = Buffer.alloc(0);
|
|
101
|
+
callId = 0;
|
|
102
|
+
pending = new Map();
|
|
103
|
+
constructor(socket) {
|
|
104
|
+
this.socket = socket;
|
|
105
|
+
socket.on('data', (chunk) => this.onData(chunk));
|
|
106
|
+
socket.on('close', () => {
|
|
107
|
+
this.rejectAll(new Error('Hermes debug WebSocket closed unexpectedly'));
|
|
108
|
+
});
|
|
109
|
+
socket.on('error', (err) => this.rejectAll(err));
|
|
110
|
+
}
|
|
111
|
+
static connect(port, deviceId) {
|
|
112
|
+
return new Promise((resolve, reject) => {
|
|
113
|
+
const socket = net.createConnection({ port, host: '127.0.0.1' });
|
|
114
|
+
const timer = setTimeout(() => {
|
|
115
|
+
socket.destroy();
|
|
116
|
+
reject(new errors_1.CliError(`Timeout connecting to Hermes debug socket on port ${port}.\n` +
|
|
117
|
+
`Make sure the Valdi app is running and the hot-reloader is active.`));
|
|
118
|
+
}, 5_000);
|
|
119
|
+
socket.once('error', (err) => {
|
|
120
|
+
clearTimeout(timer);
|
|
121
|
+
if (err.code === 'ECONNREFUSED') {
|
|
122
|
+
reject(new NotHermesError(port));
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
reject(err);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
socket.once('connect', () => {
|
|
129
|
+
const key = crypto.randomBytes(16).toString('base64');
|
|
130
|
+
const handshake = `GET /${deviceId} HTTP/1.1\r\n` +
|
|
131
|
+
`Host: localhost:${port}\r\n` +
|
|
132
|
+
`Upgrade: websocket\r\n` +
|
|
133
|
+
`Connection: Upgrade\r\n` +
|
|
134
|
+
`Sec-WebSocket-Key: ${key}\r\n` +
|
|
135
|
+
`Sec-WebSocket-Version: 13\r\n` +
|
|
136
|
+
`\r\n`;
|
|
137
|
+
socket.write(handshake);
|
|
138
|
+
let handshakeBuf = '';
|
|
139
|
+
const onHandshakeData = (chunk) => {
|
|
140
|
+
handshakeBuf += chunk.toString('utf8');
|
|
141
|
+
if (handshakeBuf.includes('\r\n\r\n')) {
|
|
142
|
+
socket.removeListener('data', onHandshakeData);
|
|
143
|
+
clearTimeout(timer);
|
|
144
|
+
if (!handshakeBuf.includes('101 Switching Protocols')) {
|
|
145
|
+
socket.destroy();
|
|
146
|
+
reject(new errors_1.CliError(`Hermes WebSocket handshake failed: ${handshakeBuf.slice(0, 100)}`));
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
resolve(new HermesConnection(socket));
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
socket.on('data', onHandshakeData);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
call(method, params = {}, timeoutMs = 10_000) {
|
|
157
|
+
const id = ++this.callId;
|
|
158
|
+
return new Promise((resolve, reject) => {
|
|
159
|
+
const timer = setTimeout(() => {
|
|
160
|
+
this.pending.delete(id);
|
|
161
|
+
reject(new Error(`Timeout waiting for response to ${method}`));
|
|
162
|
+
}, timeoutMs);
|
|
163
|
+
this.pending.set(id, {
|
|
164
|
+
resolve: (v) => { clearTimeout(timer); resolve(v); },
|
|
165
|
+
reject: (e) => { clearTimeout(timer); reject(e); },
|
|
166
|
+
});
|
|
167
|
+
this.socket.write(encodeWsFrame(JSON.stringify({ id, method, params })));
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
async startProfiling() {
|
|
171
|
+
await this.call('Profiler.start');
|
|
172
|
+
}
|
|
173
|
+
async stopProfiling() {
|
|
174
|
+
const result = await this.call('Profiler.stop', {}, 60_000);
|
|
175
|
+
return normalizeHermesProfile(result.profile);
|
|
176
|
+
}
|
|
177
|
+
close() {
|
|
178
|
+
this.socket.destroy();
|
|
179
|
+
}
|
|
180
|
+
rejectAll(err) {
|
|
181
|
+
for (const pending of this.pending.values())
|
|
182
|
+
pending.reject(err);
|
|
183
|
+
this.pending.clear();
|
|
184
|
+
}
|
|
185
|
+
onData(chunk) {
|
|
186
|
+
this.recvBuf = Buffer.concat([this.recvBuf, chunk]);
|
|
187
|
+
this.drainBuffer();
|
|
188
|
+
}
|
|
189
|
+
drainBuffer() {
|
|
190
|
+
for (;;) {
|
|
191
|
+
if (this.recvBuf.length < 2)
|
|
192
|
+
break;
|
|
193
|
+
const firstByte = this.recvBuf[0];
|
|
194
|
+
const secondByte = this.recvBuf[1];
|
|
195
|
+
const opcode = firstByte & 0x0f;
|
|
196
|
+
if (opcode === 0x08) {
|
|
197
|
+
this.rejectAll(new Error('Hermes debug WebSocket closed by server'));
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const isMasked = (secondByte & 0x80) !== 0;
|
|
201
|
+
let payloadLen = secondByte & 0x7f;
|
|
202
|
+
let offset = 2;
|
|
203
|
+
if (payloadLen === 126) {
|
|
204
|
+
if (this.recvBuf.length < 4)
|
|
205
|
+
break;
|
|
206
|
+
payloadLen = this.recvBuf.readUInt16BE(2);
|
|
207
|
+
offset = 4;
|
|
208
|
+
}
|
|
209
|
+
else if (payloadLen === 127) {
|
|
210
|
+
if (this.recvBuf.length < 10)
|
|
211
|
+
break;
|
|
212
|
+
payloadLen = this.recvBuf.readUInt32BE(6);
|
|
213
|
+
offset = 10;
|
|
214
|
+
}
|
|
215
|
+
const maskLen = isMasked ? 4 : 0;
|
|
216
|
+
const totalLen = offset + maskLen + payloadLen;
|
|
217
|
+
if (this.recvBuf.length < totalLen)
|
|
218
|
+
break;
|
|
219
|
+
if (opcode !== 0x01 && opcode !== 0x02) {
|
|
220
|
+
this.recvBuf = this.recvBuf.subarray(totalLen);
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
let payload = this.recvBuf.subarray(offset + maskLen, totalLen);
|
|
224
|
+
if (isMasked) {
|
|
225
|
+
const mask = this.recvBuf.subarray(offset, offset + 4);
|
|
226
|
+
payload = Buffer.from(payload.map((b, i) => b ^ mask[i % 4]));
|
|
227
|
+
}
|
|
228
|
+
this.recvBuf = this.recvBuf.subarray(totalLen);
|
|
229
|
+
try {
|
|
230
|
+
this.dispatchMessage(JSON.parse(payload.toString('utf8')));
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
dispatchMessage(msg) {
|
|
237
|
+
if (typeof msg['id'] !== 'number')
|
|
238
|
+
return;
|
|
239
|
+
const pending = this.pending.get(msg['id']);
|
|
240
|
+
if (!pending)
|
|
241
|
+
return;
|
|
242
|
+
this.pending.delete(msg['id']);
|
|
243
|
+
if (msg['error']) {
|
|
244
|
+
const err = msg['error'];
|
|
245
|
+
pending.reject(new Error(String(err['message'] ?? 'Unknown CDP error')));
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
pending.resolve(msg['result']);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
exports.HermesConnection = HermesConnection;
|