dsh-hello 0.1.1 → 0.1.3

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/lib/index.js CHANGED
@@ -4193,7 +4193,7 @@ var Config = Schema.object({
4193
4193
  pairingMaxAttempts: Schema.number().default(8),
4194
4194
  pairingCode: Schema.string(),
4195
4195
  logPairingCode: Schema.boolean().default(true),
4196
- interactionTarget: Schema.union(["phone-first", "browser-first", "browser-only"]).default("browser-first"),
4196
+ interactionTarget: Schema.union(["phone-first", "browser-first", "browser-only"]).default("browser-only"),
4197
4197
  approvalWaitTimeoutMs: Schema.number().default(2 * 60 * 1e3),
4198
4198
  questionWaitTimeoutMs: Schema.number().default(2 * 60 * 1e3),
4199
4199
  allowSessionWideGrants: Schema.boolean().default(true),
@@ -4234,7 +4234,7 @@ function normalizeConfig(input) {
4234
4234
  pairingCodeLength: raw.pairingCodeLength ?? 4,
4235
4235
  pairingMaxAttempts: raw.pairingMaxAttempts ?? 8,
4236
4236
  logPairingCode: raw.logPairingCode ?? true,
4237
- interactionTarget: raw.interactionTarget ?? "browser-first",
4237
+ interactionTarget: raw.interactionTarget ?? "browser-only",
4238
4238
  approvalWaitTimeoutMs: raw.approvalWaitTimeoutMs ?? 2 * 60 * 1e3,
4239
4239
  questionWaitTimeoutMs: raw.questionWaitTimeoutMs ?? 2 * 60 * 1e3,
4240
4240
  allowSessionWideGrants: raw.allowSessionWideGrants ?? true,
@@ -4830,6 +4830,7 @@ var InteractionBroker = class {
4830
4830
  this.hub = hub;
4831
4831
  this.push = push2;
4832
4832
  this.logger = logger;
4833
+ this.target = config.interactionTarget;
4833
4834
  }
4834
4835
  ctx;
4835
4836
  config;
@@ -4843,12 +4844,19 @@ var InteractionBroker = class {
4843
4844
  foreground = /* @__PURE__ */ new Map();
4844
4845
  /** Hands held interactions back if a foreground app dies without a false ping. */
4845
4846
  presenceSweep = setInterval(() => this.releasePhoneHolds(), 15e3);
4847
+ /** Runtime policy: the config default, replaceable from the admin page. */
4848
+ target;
4849
+ getTarget() {
4850
+ return this.target;
4851
+ }
4852
+ setTarget(target) {
4853
+ if (target === this.target) return;
4854
+ this.target = target;
4855
+ this.logger.info(`[dsh-hello] interaction target is now ${target}`);
4856
+ if (target === "browser-only") this.releasePhoneHolds(true);
4857
+ }
4846
4858
  register() {
4847
4859
  this.presenceSweep.unref?.();
4848
- if (this.config.interactionTarget === "browser-only") {
4849
- this.logger.info("[dsh-hello] interactionTarget browser-only: the app will not answer approvals or questions");
4850
- return;
4851
- }
4852
4860
  const listen = { global: true, prepend: true };
4853
4861
  this.ctx.on("approval/request", async (...args) => {
4854
4862
  const req = args[0];
@@ -5174,7 +5182,7 @@ var InteractionBroker = class {
5174
5182
  */
5175
5183
  shouldHoldForPhone() {
5176
5184
  return shouldHoldInteraction({
5177
- target: this.config.interactionTarget,
5185
+ target: this.target,
5178
5186
  browserLive: this.browserWins(),
5179
5187
  foregroundPhone: this.phoneWins(),
5180
5188
  appConnected: this.appCanAnswer()
@@ -5195,8 +5203,8 @@ var InteractionBroker = class {
5195
5203
  * The app left the foreground (or stopped heartbeating). Any interaction it
5196
5204
  * was holding goes back to dsh so a connected browser can render it now.
5197
5205
  */
5198
- releasePhoneHolds() {
5199
- if (this.hasForegroundPhone()) return;
5206
+ releasePhoneHolds(force = false) {
5207
+ if (!force && this.hasForegroundPhone()) return;
5200
5208
  for (const [id, pending] of [...this.questions]) {
5201
5209
  if (!pending.phoneHold) continue;
5202
5210
  clearTimeout(pending.timer);
@@ -6367,6 +6375,22 @@ function registerRoutes(router, deps) {
6367
6375
  sendJson(rc.res, 200, await adminData(deps, requestBase(rc) ?? requestOrigin(deps, rc) ?? ""));
6368
6376
  }
6369
6377
  });
6378
+ router.add({
6379
+ method: "POST",
6380
+ pattern: "/admin/interaction",
6381
+ auth: "public",
6382
+ handler: async (rc) => {
6383
+ if (!adminAuthorized(deps, rc)) throw new HttpError(401, "admin-unauthorized", "open this page from an authenticated dsh browser");
6384
+ const body = rc.body ?? {};
6385
+ const target = body.target;
6386
+ if (target !== "phone-first" && target !== "browser-first" && target !== "browser-only") {
6387
+ throw new HttpError(400, "bad-target", "target must be phone-first, browser-first, or browser-only");
6388
+ }
6389
+ if (deps.saveInteractionTarget === void 0) throw new HttpError(503, "interaction-unavailable", "this Host cannot change the interaction policy");
6390
+ await deps.saveInteractionTarget(target);
6391
+ sendJson(rc.res, 200, await adminData(deps, requestBase(rc) ?? requestOrigin(deps, rc) ?? ""));
6392
+ }
6393
+ });
6370
6394
  router.add({
6371
6395
  method: "POST",
6372
6396
  pattern: "/admin/lock",
@@ -7380,10 +7404,11 @@ function scheduleCode(result) {
7380
7404
  return typeof code === "string" ? code : void 0;
7381
7405
  }
7382
7406
  function features(deps) {
7407
+ const target = deps.interactionTarget?.() ?? deps.config.interactionTarget;
7383
7408
  return {
7384
- approvals: deps.config.interactionTarget !== "browser-only",
7385
- questions: deps.config.interactionTarget !== "browser-only",
7386
- interactionTarget: deps.config.interactionTarget,
7409
+ approvals: target !== "browser-only",
7410
+ questions: target !== "browser-only",
7411
+ interactionTarget: target,
7387
7412
  presence: true,
7388
7413
  push: deps.push.isConfigured(),
7389
7414
  debugRoutes: deps.config.enableDebugRoutes,
@@ -7463,6 +7488,7 @@ async function adminData(deps, originBase) {
7463
7488
  formatted: view.formatted,
7464
7489
  fixed: view.fixed,
7465
7490
  locked: deps.pairing.isLocked(),
7491
+ interactionTarget: deps.interactionTarget?.() ?? deps.config.interactionTarget,
7466
7492
  expiresAt: view.fixed ? null : view.expiresAt,
7467
7493
  origin,
7468
7494
  basePath: deps.config.basePath,
@@ -7520,28 +7546,65 @@ async function sendBinary(rc, file) {
7520
7546
  });
7521
7547
  }
7522
7548
  async function renderQr(text) {
7549
+ const candidates = [];
7523
7550
  try {
7524
7551
  const mod = await Promise.resolve().then(() => __toESM(require_main(), 1));
7525
- const qrcode = mod.default ?? mod;
7526
- return await new Promise((resolve4) => {
7527
- ;
7528
- qrcode.generate(text, { small: true }, resolve4);
7529
- });
7552
+ candidates.push(mod.default, mod.default?.default, mod);
7553
+ } catch {
7554
+ }
7555
+ try {
7556
+ const { createRequire: createRequire2 } = await import("node:module");
7557
+ candidates.push(createRequire2(import.meta.url)("qrcode-terminal"));
7530
7558
  } catch {
7531
- return null;
7532
7559
  }
7560
+ for (const candidate of candidates) {
7561
+ const generate = candidate?.generate;
7562
+ if (typeof generate !== "function") continue;
7563
+ try {
7564
+ return await new Promise((resolve4) => {
7565
+ ;
7566
+ candidate.generate(text, { small: true }, resolve4);
7567
+ });
7568
+ } catch {
7569
+ continue;
7570
+ }
7571
+ }
7572
+ return null;
7533
7573
  }
7534
- async function renderQrSvg(text) {
7574
+ async function loadQrSvgApi() {
7575
+ const isApi = (value) => typeof value === "object" && value !== null && typeof value.toString === "function" && value.toString !== Object.prototype.toString;
7535
7576
  try {
7536
7577
  const mod = await import("qrcode");
7537
- const qrcode = mod.default ?? mod;
7538
- return await qrcode.toString(text, {
7578
+ for (const candidate of [mod.default, mod.default?.default, mod]) {
7579
+ if (isApi(candidate)) return candidate;
7580
+ }
7581
+ } catch (error) {
7582
+ console.warn("[dsh-hello] could not import qrcode through ESM:", error);
7583
+ }
7584
+ try {
7585
+ const { createRequire: createRequire2 } = await import("node:module");
7586
+ const required = createRequire2(import.meta.url)("qrcode");
7587
+ if (isApi(required)) return required;
7588
+ } catch (error) {
7589
+ console.warn("[dsh-hello] could not require qrcode:", error);
7590
+ }
7591
+ return null;
7592
+ }
7593
+ async function renderQrSvg(text) {
7594
+ const qrcode = await loadQrSvgApi();
7595
+ if (qrcode === null) {
7596
+ console.warn("[dsh-hello] qrcode is unavailable; the admin page will not show a QR code");
7597
+ return null;
7598
+ }
7599
+ try {
7600
+ const svg = await qrcode.toString(text, {
7539
7601
  type: "svg",
7540
7602
  margin: 1,
7541
7603
  width: 320,
7542
7604
  errorCorrectionLevel: "M",
7543
7605
  color: { dark: "#0b0e14", light: "#ffffff" }
7544
7606
  });
7607
+ return typeof svg === "string" && svg.trimStart().startsWith("<svg") ? svg : null;
7545
7608
  } catch (error) {
7546
7609
  console.warn("[dsh-hello] QR SVG generation failed:", error);
7547
7610
  return null;
@@ -8222,6 +8285,23 @@ async function apply(ctx, rawConfig) {
8222
8285
  }
8223
8286
  const interactions = new InteractionBroker(host, config, hub, push2, logger);
8224
8287
  interactions.register();
8288
+ const interactionRecordKey = "dsh-hello/interactions";
8289
+ if (credentials !== void 0) {
8290
+ try {
8291
+ const record = await credentials.readRecord(interactionRecordKey);
8292
+ const target = record?.payload?.target;
8293
+ if (target === "phone-first" || target === "browser-first" || target === "browser-only") {
8294
+ interactions.setTarget(target);
8295
+ }
8296
+ } catch (error) {
8297
+ logger.warn(`[dsh-hello] could not load the interaction target: ${shortError(error)}`);
8298
+ }
8299
+ }
8300
+ const saveInteractionTarget = async (target) => {
8301
+ interactions.setTarget(target);
8302
+ if (credentials === void 0) return;
8303
+ await credentials.modifyRecord(interactionRecordKey, async () => ({ kind: "grant", payload: { target } }));
8304
+ };
8225
8305
  const sessions = createSessionFacade(host);
8226
8306
  const terminals = new TerminalManager(host, logger);
8227
8307
  const connection = readService(ctx, "connection");
@@ -8255,7 +8335,9 @@ async function apply(ctx, rawConfig) {
8255
8335
  logger,
8256
8336
  version,
8257
8337
  pluginVersion: PLUGIN_VERSION,
8258
- webUrl
8338
+ webUrl,
8339
+ saveInteractionTarget,
8340
+ interactionTarget: () => interactions.getTarget()
8259
8341
  });
8260
8342
  ctx.effect(() => {
8261
8343
  const dispose = webServer.register(router.hostRoute());