@player-devtools/client-flipper 0.14.3--canary.24.1132 → 0.15.1--canary.29.1254

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.
@@ -11,7 +11,6 @@ import {
11
11
  FlipperServerState
12
12
  } from "flipper-server-client";
13
13
  import { spawn } from "child_process";
14
- import open from "open";
15
14
  import * as net from "net";
16
15
  import * as fs from "fs";
17
16
  import * as os from "os";
@@ -21,7 +20,6 @@ if (typeof globalThis.WebSocket === "undefined") {
21
20
  globalThis.WebSocket = WsWebSocket;
22
21
  }
23
22
  var PLUGIN_API = "player-ui-devtools";
24
- var PLUGIN_NPM_NAME = "flipper-plugin-player-ui-devtools";
25
23
  var log = (...args) => {
26
24
  console.error(...args);
27
25
  };
@@ -86,6 +84,13 @@ var FlipperRefcount = class {
86
84
  return null;
87
85
  }
88
86
  }
87
+ /**
88
+ * Read the current refcount state without mutating it. Still goes through
89
+ * `withLock` so a concurrent acquire/release can't be observed mid-write.
90
+ */
91
+ peek() {
92
+ return this.withLock(() => this.read());
93
+ }
89
94
  write(state) {
90
95
  fs.writeFileSync(this.file, JSON.stringify(state));
91
96
  }
@@ -151,12 +156,14 @@ var FlipperServerTransport = class {
151
156
  * plugin — these are the clients we send outbound messages to.
152
157
  */
153
158
  this.activeClientIds = /* @__PURE__ */ new Set();
159
+ /** Resolved connection target, set at the top of `connect()`. */
160
+ this.host = "localhost";
161
+ this.port = 52342;
154
162
  /**
155
- * Client IDs currently connected to flipper-server, regardless of whether
156
- * the devtools plugin has been activated for them. Used by `enablePlugin`'s
157
- * "all connected clients" convenience.
163
+ * Whether this instance started the shared daemon (vs. attaching to one
164
+ * another instance started). Only an owner may consider restarting it.
158
165
  */
159
- this.connectedClientIds = /* @__PURE__ */ new Set();
166
+ this.owns = false;
160
167
  this.sendMessage = async (message) => {
161
168
  if (!this.server) return;
162
169
  const payload = {
@@ -187,15 +194,23 @@ var FlipperServerTransport = class {
187
194
  this.removeListener = (callback) => {
188
195
  this.listeners.delete(callback);
189
196
  };
197
+ /**
198
+ * In-flight `close()` promise, if one is currently running on this
199
+ * instance. Guards `refcount.release()` and `killAndWait()` against being
200
+ * invoked twice for a single logical close — e.g. `restart()`'s internal
201
+ * `close()` racing an external `MCPServer.stop()` -> `transport.close()`.
202
+ */
203
+ this.closing = null;
190
204
  }
191
205
  async connect() {
192
- const host = this.options.host ?? "localhost";
193
- const port = this.options.port ?? 52342;
206
+ const host = this.host = this.options.host ?? "localhost";
207
+ const port = this.port = this.options.port ?? 52342;
194
208
  const { shouldStart, commit } = this.refcount.acquire();
209
+ this.owns = shouldStart;
195
210
  if (shouldStart) {
196
211
  log("[FlipperServerTransport] Starting flipper-server...");
197
212
  const serverScript = __require.resolve("flipper-server/server.js");
198
- const child = spawn(process.execPath, [serverScript, "--open=false"], {
213
+ const child = spawn(process.execPath, [serverScript, "--open=true"], {
199
214
  stdio: ["ignore", "ignore", "inherit"],
200
215
  detached: true
201
216
  });
@@ -206,22 +221,18 @@ var FlipperServerTransport = class {
206
221
  );
207
222
  });
208
223
  child.unref();
209
- await waitForPort(host, port);
224
+ try {
225
+ await waitForPort(host, port);
226
+ } catch (err) {
227
+ await this.killAndWait(child.pid);
228
+ throw err;
229
+ }
210
230
  commit(child.pid);
211
231
  log("[FlipperServerTransport] flipper-server ready.");
212
232
  } else {
213
233
  await waitForPort(host, port);
214
234
  log("[FlipperServerTransport] Attached to flipper-server.");
215
235
  }
216
- if (this.options.open) {
217
- const url = this.options.url ?? `http://localhost:${port}`;
218
- log(`[FlipperServerTransport] Opening ${url}`);
219
- try {
220
- await open(url);
221
- } catch (err) {
222
- console.warn("[FlipperServerTransport] Failed to open UI:", err);
223
- }
224
- }
225
236
  const { getAuthToken } = (
226
237
  // eslint-disable-next-line @typescript-eslint/no-require-imports
227
238
  __require("flipper-server/lib/app-connectivity/certificate-exchange/certificate-utils")
@@ -245,20 +256,10 @@ var FlipperServerTransport = class {
245
256
  await this.server.connect();
246
257
  this.server.on("client-connected", (info) => {
247
258
  log("[FlipperServerTransport] client-connected:", JSON.stringify(info));
248
- this.connectedClientIds.add(info.id);
249
- if (this.options.autoEnablePlugin) {
250
- this.enablePlugin(info.id).catch((err) => {
251
- console.warn(
252
- `[FlipperServerTransport] Failed to auto-enable plugin for client ${info.id}:`,
253
- err
254
- );
255
- });
256
- }
257
259
  });
258
260
  this.server.on("client-disconnected", ({ id }) => {
259
261
  log("[FlipperServerTransport] client-disconnected:", id);
260
262
  this.activeClientIds.delete(id);
261
- this.connectedClientIds.delete(id);
262
263
  });
263
264
  this.server.on("client-message", ({ id, message }) => {
264
265
  let parsed;
@@ -280,93 +281,132 @@ var FlipperServerTransport = class {
280
281
  }
281
282
  });
282
283
  }
283
- /**
284
- * Ensures `flipper-plugin-player-ui-devtools` is installed on the attached
285
- * flipper-server, installing the published npm package if it's missing.
286
- *
287
- * This is Flipper's own documented plugin-install RPC (the same
288
- * `exec(...)` commands the desktop UI's "Install Plugin" button calls) —
289
- * not a filesystem workaround. It's opt-in: callers that manage plugin
290
- * installation themselves (or run against a flipper-server that already
291
- * has it) can skip calling this.
292
- */
293
- async ensurePluginInstalled() {
294
- if (!this.server) {
295
- throw new Error("FlipperServerTransport is not connected");
296
- }
297
- const installed = await this.server.exec("plugins-get-installed-plugins");
298
- const existing = installed.find((p) => p.name === PLUGIN_NPM_NAME);
299
- if (existing) {
300
- log(
301
- `[FlipperServerTransport] ${PLUGIN_NPM_NAME}@${existing.version} already installed.`
302
- );
303
- return existing;
304
- }
305
- log(`[FlipperServerTransport] Installing ${PLUGIN_NPM_NAME} from npm...`);
306
- const details = await this.server.exec(
307
- "plugins-install-from-npm",
308
- PLUGIN_NPM_NAME
309
- );
310
- log(
311
- `[FlipperServerTransport] Installed ${PLUGIN_NPM_NAME}@${details.version}.`
312
- );
313
- return details;
314
- }
315
- /**
316
- * Activates the devtools plugin for a connected client by sending the
317
- * `init` handshake `js-flipper`'s device SDK requires before it will open
318
- * a live plugin connection (`onConnect`/`FlipperConnection`) and start
319
- * relaying `client-message`s for our api. Nothing in flipper-server itself
320
- * sends this automatically for a non-background plugin like ours unless a
321
- * full Flipper desktop app is attached and its tab is selected — this
322
- * method lets a caller trigger the same handshake directly, without
323
- * needing a desktop UI at all.
324
- *
325
- * Pass a specific `clientId`, or omit it to enable for every client
326
- * currently connected to flipper-server.
327
- */
328
- async enablePlugin(clientId) {
329
- await this.sendLifecycleMessage("init", clientId);
330
- }
331
- /** Symmetric counterpart to `enablePlugin` — releases the plugin connection without disconnecting the client from flipper-server. */
332
- async disablePlugin(clientId) {
333
- await this.sendLifecycleMessage("deinit", clientId);
334
- }
335
- async sendLifecycleMessage(method, clientId) {
336
- if (!this.server) {
337
- throw new Error("FlipperServerTransport is not connected");
338
- }
339
- const targets = clientId ? [clientId] : [...this.connectedClientIds];
340
- const payload = {
341
- method,
342
- params: { plugin: PLUGIN_API }
343
- };
344
- await Promise.all(
345
- targets.map(
346
- (id) => this.server.exec("client-request-response", id, payload).catch(
347
- (err) => {
348
- console.warn(
349
- `[FlipperServerTransport] Failed to ${method} plugin for client ${id}:`,
350
- err
351
- );
352
- }
353
- )
354
- )
355
- );
356
- }
357
284
  async close() {
285
+ if (this.closing) return this.closing;
286
+ this.closing = this.doClose().finally(() => {
287
+ this.closing = null;
288
+ });
289
+ return this.closing;
290
+ }
291
+ async doClose() {
358
292
  this.listeners.clear();
359
293
  this.activeClientIds.clear();
360
- this.connectedClientIds.clear();
361
294
  this.server?.close();
362
295
  this.server = null;
363
296
  const pidToKill = this.refcount.release();
364
297
  if (pidToKill !== null) {
298
+ await this.killAndWait(pidToKill);
299
+ }
300
+ }
301
+ /**
302
+ * Send SIGTERM and wait for the process to actually exit (or a bounded
303
+ * timeout) before resolving, so callers — notably `restart()` — can rely on
304
+ * the port being free once `close()` returns instead of racing a new child
305
+ * for it.
306
+ */
307
+ killAndWait(pid, timeoutMs = 5e3) {
308
+ return new Promise((resolve) => {
365
309
  try {
366
- process.kill(pidToKill);
367
- log("[FlipperServerTransport] Shut down flipper-server.");
310
+ process.kill(pid);
368
311
  } catch {
312
+ resolve();
313
+ return;
369
314
  }
315
+ const deadline = Date.now() + timeoutMs;
316
+ const poll = () => {
317
+ if (!this.isPidAlive(pid)) {
318
+ log("[FlipperServerTransport] Shut down flipper-server.");
319
+ resolve();
320
+ return;
321
+ }
322
+ if (Date.now() >= deadline) {
323
+ log(
324
+ "[FlipperServerTransport] Timed out waiting for flipper-server to exit."
325
+ );
326
+ resolve();
327
+ return;
328
+ }
329
+ setTimeout(poll, 100);
330
+ };
331
+ poll();
332
+ });
333
+ }
334
+ isPidAlive(pid) {
335
+ try {
336
+ process.kill(pid, 0);
337
+ return true;
338
+ } catch {
339
+ return false;
340
+ }
341
+ }
342
+ /** Read-only snapshot of this transport's connection to the shared daemon. */
343
+ getDiagnostics() {
344
+ const state = this.refcount.peek();
345
+ return {
346
+ connected: this.server !== null,
347
+ host: this.host,
348
+ port: this.port,
349
+ owns: this.owns,
350
+ refs: state?.refs ?? null,
351
+ activeClientIds: [...this.activeClientIds]
352
+ };
353
+ }
354
+ /**
355
+ * Restart the shared daemon. Only ever acts when this instance both owns
356
+ * the daemon (started it) and is its sole remaining consumer — otherwise
357
+ * this soft-errors without touching the daemon, since killing it out from
358
+ * under other attached instances would break their connections.
359
+ */
360
+ async restart() {
361
+ if (!this.owns) {
362
+ return {
363
+ ok: false,
364
+ reason: "this instance does not own the flipper-server daemon"
365
+ };
366
+ }
367
+ const state = this.refcount.peek();
368
+ if (state?.refs !== 1) {
369
+ return {
370
+ ok: false,
371
+ reason: "other consumers are attached to this daemon"
372
+ };
373
+ }
374
+ const recheck = this.refcount.peek();
375
+ if (recheck?.refs !== 1) {
376
+ return {
377
+ ok: false,
378
+ reason: "other consumers attached to this daemon just before restart"
379
+ };
380
+ }
381
+ try {
382
+ await this.close();
383
+ await this.connect();
384
+ } catch (err) {
385
+ this.owns = false;
386
+ return {
387
+ ok: false,
388
+ reason: err instanceof Error ? err.message : String(err)
389
+ };
390
+ }
391
+ return { ok: true, host: this.host, port: this.port };
392
+ }
393
+ /** Whether the devtools Flipper plugin is installed, per the live daemon. */
394
+ async getPluginInstallStatus() {
395
+ if (!this.server) return { installed: false, reason: "not connected" };
396
+ try {
397
+ const plugins = await this.server.exec(
398
+ "plugins-get-installed-plugins"
399
+ );
400
+ const plugin = plugins.find(
401
+ (candidate) => candidate.id === PLUGIN_API || candidate.name === PLUGIN_API
402
+ );
403
+ if (!plugin) return { installed: false };
404
+ return plugin.version ? { installed: true, version: plugin.version } : { installed: true };
405
+ } catch (err) {
406
+ return {
407
+ installed: false,
408
+ reason: err instanceof Error ? err.message : String(err)
409
+ };
370
410
  }
371
411
  }
372
412
  };
package/dist/index.mjs CHANGED
@@ -11,7 +11,6 @@ import {
11
11
  FlipperServerState
12
12
  } from "flipper-server-client";
13
13
  import { spawn } from "child_process";
14
- import open from "open";
15
14
  import * as net from "net";
16
15
  import * as fs from "fs";
17
16
  import * as os from "os";
@@ -21,7 +20,6 @@ if (typeof globalThis.WebSocket === "undefined") {
21
20
  globalThis.WebSocket = WsWebSocket;
22
21
  }
23
22
  var PLUGIN_API = "player-ui-devtools";
24
- var PLUGIN_NPM_NAME = "flipper-plugin-player-ui-devtools";
25
23
  var log = (...args) => {
26
24
  console.error(...args);
27
25
  };
@@ -86,6 +84,13 @@ var FlipperRefcount = class {
86
84
  return null;
87
85
  }
88
86
  }
87
+ /**
88
+ * Read the current refcount state without mutating it. Still goes through
89
+ * `withLock` so a concurrent acquire/release can't be observed mid-write.
90
+ */
91
+ peek() {
92
+ return this.withLock(() => this.read());
93
+ }
89
94
  write(state) {
90
95
  fs.writeFileSync(this.file, JSON.stringify(state));
91
96
  }
@@ -151,12 +156,14 @@ var FlipperServerTransport = class {
151
156
  * plugin — these are the clients we send outbound messages to.
152
157
  */
153
158
  this.activeClientIds = /* @__PURE__ */ new Set();
159
+ /** Resolved connection target, set at the top of `connect()`. */
160
+ this.host = "localhost";
161
+ this.port = 52342;
154
162
  /**
155
- * Client IDs currently connected to flipper-server, regardless of whether
156
- * the devtools plugin has been activated for them. Used by `enablePlugin`'s
157
- * "all connected clients" convenience.
163
+ * Whether this instance started the shared daemon (vs. attaching to one
164
+ * another instance started). Only an owner may consider restarting it.
158
165
  */
159
- this.connectedClientIds = /* @__PURE__ */ new Set();
166
+ this.owns = false;
160
167
  this.sendMessage = async (message) => {
161
168
  if (!this.server) return;
162
169
  const payload = {
@@ -187,15 +194,23 @@ var FlipperServerTransport = class {
187
194
  this.removeListener = (callback) => {
188
195
  this.listeners.delete(callback);
189
196
  };
197
+ /**
198
+ * In-flight `close()` promise, if one is currently running on this
199
+ * instance. Guards `refcount.release()` and `killAndWait()` against being
200
+ * invoked twice for a single logical close — e.g. `restart()`'s internal
201
+ * `close()` racing an external `MCPServer.stop()` -> `transport.close()`.
202
+ */
203
+ this.closing = null;
190
204
  }
191
205
  async connect() {
192
- const host = this.options.host ?? "localhost";
193
- const port = this.options.port ?? 52342;
206
+ const host = this.host = this.options.host ?? "localhost";
207
+ const port = this.port = this.options.port ?? 52342;
194
208
  const { shouldStart, commit } = this.refcount.acquire();
209
+ this.owns = shouldStart;
195
210
  if (shouldStart) {
196
211
  log("[FlipperServerTransport] Starting flipper-server...");
197
212
  const serverScript = __require.resolve("flipper-server/server.js");
198
- const child = spawn(process.execPath, [serverScript, "--open=false"], {
213
+ const child = spawn(process.execPath, [serverScript, "--open=true"], {
199
214
  stdio: ["ignore", "ignore", "inherit"],
200
215
  detached: true
201
216
  });
@@ -206,22 +221,18 @@ var FlipperServerTransport = class {
206
221
  );
207
222
  });
208
223
  child.unref();
209
- await waitForPort(host, port);
224
+ try {
225
+ await waitForPort(host, port);
226
+ } catch (err) {
227
+ await this.killAndWait(child.pid);
228
+ throw err;
229
+ }
210
230
  commit(child.pid);
211
231
  log("[FlipperServerTransport] flipper-server ready.");
212
232
  } else {
213
233
  await waitForPort(host, port);
214
234
  log("[FlipperServerTransport] Attached to flipper-server.");
215
235
  }
216
- if (this.options.open) {
217
- const url = this.options.url ?? `http://localhost:${port}`;
218
- log(`[FlipperServerTransport] Opening ${url}`);
219
- try {
220
- await open(url);
221
- } catch (err) {
222
- console.warn("[FlipperServerTransport] Failed to open UI:", err);
223
- }
224
- }
225
236
  const { getAuthToken } = (
226
237
  // eslint-disable-next-line @typescript-eslint/no-require-imports
227
238
  __require("flipper-server/lib/app-connectivity/certificate-exchange/certificate-utils")
@@ -245,20 +256,10 @@ var FlipperServerTransport = class {
245
256
  await this.server.connect();
246
257
  this.server.on("client-connected", (info) => {
247
258
  log("[FlipperServerTransport] client-connected:", JSON.stringify(info));
248
- this.connectedClientIds.add(info.id);
249
- if (this.options.autoEnablePlugin) {
250
- this.enablePlugin(info.id).catch((err) => {
251
- console.warn(
252
- `[FlipperServerTransport] Failed to auto-enable plugin for client ${info.id}:`,
253
- err
254
- );
255
- });
256
- }
257
259
  });
258
260
  this.server.on("client-disconnected", ({ id }) => {
259
261
  log("[FlipperServerTransport] client-disconnected:", id);
260
262
  this.activeClientIds.delete(id);
261
- this.connectedClientIds.delete(id);
262
263
  });
263
264
  this.server.on("client-message", ({ id, message }) => {
264
265
  let parsed;
@@ -280,93 +281,132 @@ var FlipperServerTransport = class {
280
281
  }
281
282
  });
282
283
  }
283
- /**
284
- * Ensures `flipper-plugin-player-ui-devtools` is installed on the attached
285
- * flipper-server, installing the published npm package if it's missing.
286
- *
287
- * This is Flipper's own documented plugin-install RPC (the same
288
- * `exec(...)` commands the desktop UI's "Install Plugin" button calls) —
289
- * not a filesystem workaround. It's opt-in: callers that manage plugin
290
- * installation themselves (or run against a flipper-server that already
291
- * has it) can skip calling this.
292
- */
293
- async ensurePluginInstalled() {
294
- if (!this.server) {
295
- throw new Error("FlipperServerTransport is not connected");
296
- }
297
- const installed = await this.server.exec("plugins-get-installed-plugins");
298
- const existing = installed.find((p) => p.name === PLUGIN_NPM_NAME);
299
- if (existing) {
300
- log(
301
- `[FlipperServerTransport] ${PLUGIN_NPM_NAME}@${existing.version} already installed.`
302
- );
303
- return existing;
304
- }
305
- log(`[FlipperServerTransport] Installing ${PLUGIN_NPM_NAME} from npm...`);
306
- const details = await this.server.exec(
307
- "plugins-install-from-npm",
308
- PLUGIN_NPM_NAME
309
- );
310
- log(
311
- `[FlipperServerTransport] Installed ${PLUGIN_NPM_NAME}@${details.version}.`
312
- );
313
- return details;
314
- }
315
- /**
316
- * Activates the devtools plugin for a connected client by sending the
317
- * `init` handshake `js-flipper`'s device SDK requires before it will open
318
- * a live plugin connection (`onConnect`/`FlipperConnection`) and start
319
- * relaying `client-message`s for our api. Nothing in flipper-server itself
320
- * sends this automatically for a non-background plugin like ours unless a
321
- * full Flipper desktop app is attached and its tab is selected — this
322
- * method lets a caller trigger the same handshake directly, without
323
- * needing a desktop UI at all.
324
- *
325
- * Pass a specific `clientId`, or omit it to enable for every client
326
- * currently connected to flipper-server.
327
- */
328
- async enablePlugin(clientId) {
329
- await this.sendLifecycleMessage("init", clientId);
330
- }
331
- /** Symmetric counterpart to `enablePlugin` — releases the plugin connection without disconnecting the client from flipper-server. */
332
- async disablePlugin(clientId) {
333
- await this.sendLifecycleMessage("deinit", clientId);
334
- }
335
- async sendLifecycleMessage(method, clientId) {
336
- if (!this.server) {
337
- throw new Error("FlipperServerTransport is not connected");
338
- }
339
- const targets = clientId ? [clientId] : [...this.connectedClientIds];
340
- const payload = {
341
- method,
342
- params: { plugin: PLUGIN_API }
343
- };
344
- await Promise.all(
345
- targets.map(
346
- (id) => this.server.exec("client-request-response", id, payload).catch(
347
- (err) => {
348
- console.warn(
349
- `[FlipperServerTransport] Failed to ${method} plugin for client ${id}:`,
350
- err
351
- );
352
- }
353
- )
354
- )
355
- );
356
- }
357
284
  async close() {
285
+ if (this.closing) return this.closing;
286
+ this.closing = this.doClose().finally(() => {
287
+ this.closing = null;
288
+ });
289
+ return this.closing;
290
+ }
291
+ async doClose() {
358
292
  this.listeners.clear();
359
293
  this.activeClientIds.clear();
360
- this.connectedClientIds.clear();
361
294
  this.server?.close();
362
295
  this.server = null;
363
296
  const pidToKill = this.refcount.release();
364
297
  if (pidToKill !== null) {
298
+ await this.killAndWait(pidToKill);
299
+ }
300
+ }
301
+ /**
302
+ * Send SIGTERM and wait for the process to actually exit (or a bounded
303
+ * timeout) before resolving, so callers — notably `restart()` — can rely on
304
+ * the port being free once `close()` returns instead of racing a new child
305
+ * for it.
306
+ */
307
+ killAndWait(pid, timeoutMs = 5e3) {
308
+ return new Promise((resolve) => {
365
309
  try {
366
- process.kill(pidToKill);
367
- log("[FlipperServerTransport] Shut down flipper-server.");
310
+ process.kill(pid);
368
311
  } catch {
312
+ resolve();
313
+ return;
369
314
  }
315
+ const deadline = Date.now() + timeoutMs;
316
+ const poll = () => {
317
+ if (!this.isPidAlive(pid)) {
318
+ log("[FlipperServerTransport] Shut down flipper-server.");
319
+ resolve();
320
+ return;
321
+ }
322
+ if (Date.now() >= deadline) {
323
+ log(
324
+ "[FlipperServerTransport] Timed out waiting for flipper-server to exit."
325
+ );
326
+ resolve();
327
+ return;
328
+ }
329
+ setTimeout(poll, 100);
330
+ };
331
+ poll();
332
+ });
333
+ }
334
+ isPidAlive(pid) {
335
+ try {
336
+ process.kill(pid, 0);
337
+ return true;
338
+ } catch {
339
+ return false;
340
+ }
341
+ }
342
+ /** Read-only snapshot of this transport's connection to the shared daemon. */
343
+ getDiagnostics() {
344
+ const state = this.refcount.peek();
345
+ return {
346
+ connected: this.server !== null,
347
+ host: this.host,
348
+ port: this.port,
349
+ owns: this.owns,
350
+ refs: state?.refs ?? null,
351
+ activeClientIds: [...this.activeClientIds]
352
+ };
353
+ }
354
+ /**
355
+ * Restart the shared daemon. Only ever acts when this instance both owns
356
+ * the daemon (started it) and is its sole remaining consumer — otherwise
357
+ * this soft-errors without touching the daemon, since killing it out from
358
+ * under other attached instances would break their connections.
359
+ */
360
+ async restart() {
361
+ if (!this.owns) {
362
+ return {
363
+ ok: false,
364
+ reason: "this instance does not own the flipper-server daemon"
365
+ };
366
+ }
367
+ const state = this.refcount.peek();
368
+ if (state?.refs !== 1) {
369
+ return {
370
+ ok: false,
371
+ reason: "other consumers are attached to this daemon"
372
+ };
373
+ }
374
+ const recheck = this.refcount.peek();
375
+ if (recheck?.refs !== 1) {
376
+ return {
377
+ ok: false,
378
+ reason: "other consumers attached to this daemon just before restart"
379
+ };
380
+ }
381
+ try {
382
+ await this.close();
383
+ await this.connect();
384
+ } catch (err) {
385
+ this.owns = false;
386
+ return {
387
+ ok: false,
388
+ reason: err instanceof Error ? err.message : String(err)
389
+ };
390
+ }
391
+ return { ok: true, host: this.host, port: this.port };
392
+ }
393
+ /** Whether the devtools Flipper plugin is installed, per the live daemon. */
394
+ async getPluginInstallStatus() {
395
+ if (!this.server) return { installed: false, reason: "not connected" };
396
+ try {
397
+ const plugins = await this.server.exec(
398
+ "plugins-get-installed-plugins"
399
+ );
400
+ const plugin = plugins.find(
401
+ (candidate) => candidate.id === PLUGIN_API || candidate.name === PLUGIN_API
402
+ );
403
+ if (!plugin) return { installed: false };
404
+ return plugin.version ? { installed: true, version: plugin.version } : { installed: true };
405
+ } catch (err) {
406
+ return {
407
+ installed: false,
408
+ reason: err instanceof Error ? err.message : String(err)
409
+ };
370
410
  }
371
411
  }
372
412
  };