@npaw/forge-web 9.7.2 → 9.7.4

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.
Binary file
package/dist/index.cjs CHANGED
@@ -148,7 +148,12 @@ var CAPTURE_PLAN_BITS = {
148
148
  SECTIONS: 1 << 13,
149
149
  ATTRIBUTION: 1 << 14,
150
150
  /** The operator enabled a motion switch, so location may be observed. */
151
- LOCATION: 1 << 15
151
+ LOCATION: 1 << 15,
152
+ /**
153
+ * The operator enabled device insights (`di.en` with a non-empty family
154
+ * allowlist); the kernel will emit `deviceSampleRequest` effects.
155
+ */
156
+ DEVICE_INSIGHTS: 1 << 16
152
157
  };
153
158
  function isCapturePlanEnabled(bits, bit) {
154
159
  return (bits & bit) !== 0;
@@ -3144,6 +3149,104 @@ function randomUuid() {
3144
3149
  return `${random()}-${random().slice(0, 4)}-4${random().slice(0, 3)}-a${random().slice(0, 3)}-${random()}${random().slice(0, 4)}`;
3145
3150
  }
3146
3151
 
3152
+ // src/device-insights.ts
3153
+ var METRIC_FIELDS = [
3154
+ "cpuTimeMs",
3155
+ "cpuCores",
3156
+ "memUsedBytes",
3157
+ "memAvailBytes",
3158
+ "batteryLevelPct",
3159
+ "batteryCharging",
3160
+ "thermal",
3161
+ "diskFreeBytes"
3162
+ ];
3163
+ async function collectBrowserDeviceSample(families, environment = readBrowserDeviceInsightsEnvironment()) {
3164
+ const sample = {};
3165
+ if (families.includes("mem")) {
3166
+ const usedBytes = readUsedJsHeapBytes(environment);
3167
+ if (usedBytes !== void 0) {
3168
+ sample.memUsedBytes = usedBytes;
3169
+ }
3170
+ }
3171
+ if (families.includes("bat")) {
3172
+ const battery = await readBattery(environment);
3173
+ if (battery.levelPct !== void 0) {
3174
+ sample.batteryLevelPct = battery.levelPct;
3175
+ }
3176
+ if (battery.charging !== void 0) {
3177
+ sample.batteryCharging = battery.charging;
3178
+ }
3179
+ }
3180
+ return hasDeviceMetric(sample) ? sample : void 0;
3181
+ }
3182
+ function hasDeviceMetric(sample) {
3183
+ return METRIC_FIELDS.some((field) => sample[field] !== void 0);
3184
+ }
3185
+ function compactDeviceSample(sample) {
3186
+ const compact = {};
3187
+ if (sample.sampledAtMs !== void 0) {
3188
+ compact.sampledAtMs = sample.sampledAtMs;
3189
+ }
3190
+ for (const field of METRIC_FIELDS) {
3191
+ const value = sample[field];
3192
+ if (value !== void 0) {
3193
+ compact[field] = value;
3194
+ }
3195
+ }
3196
+ return compact;
3197
+ }
3198
+ function readUsedJsHeapBytes(environment) {
3199
+ var _a;
3200
+ if (!environment.memory) {
3201
+ return void 0;
3202
+ }
3203
+ let used;
3204
+ try {
3205
+ used = (_a = environment.memory()) == null ? void 0 : _a.usedJSHeapSize;
3206
+ } catch (e) {
3207
+ return void 0;
3208
+ }
3209
+ if (typeof used !== "number" || !Number.isFinite(used) || used <= 0) {
3210
+ return void 0;
3211
+ }
3212
+ return Math.floor(used);
3213
+ }
3214
+ async function readBattery(environment) {
3215
+ if (!environment.getBattery) {
3216
+ return {};
3217
+ }
3218
+ let manager;
3219
+ try {
3220
+ manager = await environment.getBattery();
3221
+ } catch (e) {
3222
+ return {};
3223
+ }
3224
+ if (!manager) {
3225
+ return {};
3226
+ }
3227
+ const result = {};
3228
+ const level = manager.level;
3229
+ if (typeof level === "number" && Number.isFinite(level) && level >= 0 && level <= 1) {
3230
+ result.levelPct = Math.round(level * 100);
3231
+ }
3232
+ if (typeof manager.charging === "boolean") {
3233
+ result.charging = manager.charging;
3234
+ }
3235
+ return result;
3236
+ }
3237
+ function readBrowserDeviceInsightsEnvironment() {
3238
+ const environment = {};
3239
+ const performanceRef = globalThis.performance;
3240
+ if (performanceRef && typeof performanceRef.memory === "object" && performanceRef.memory !== null) {
3241
+ environment.memory = () => performanceRef.memory;
3242
+ }
3243
+ const navigatorRef = globalThis.navigator;
3244
+ if (navigatorRef && typeof navigatorRef.getBattery === "function") {
3245
+ environment.getBattery = () => navigatorRef.getBattery();
3246
+ }
3247
+ return environment;
3248
+ }
3249
+
3147
3250
  // src/utils.ts
3148
3251
  function systemClock() {
3149
3252
  return {
@@ -3238,6 +3341,7 @@ var BrowserEffectExecutor = class {
3238
3341
  this.transport = options.transport;
3239
3342
  this.storage = options.storage;
3240
3343
  this.submitCompletion = options.submitCompletion;
3344
+ this.deviceSampleCollector = options.deviceSampleCollector;
3241
3345
  if (options.onEvent) {
3242
3346
  this.eventListeners.add(options.onEvent);
3243
3347
  }
@@ -3300,6 +3404,11 @@ var BrowserEffectExecutor = class {
3300
3404
  case "diagnostic":
3301
3405
  this.emitDiagnostic(effect);
3302
3406
  break;
3407
+ case "deviceSampleRequest":
3408
+ this.track(this.executeDeviceSampleRequest(effect.families));
3409
+ break;
3410
+ default:
3411
+ break;
3303
3412
  }
3304
3413
  }
3305
3414
  }
@@ -3376,6 +3485,31 @@ var BrowserEffectExecutor = class {
3376
3485
  });
3377
3486
  }
3378
3487
  }
3488
+ /**
3489
+ * Answers a kernel device sample request through the collector. Nothing is
3490
+ * sent when the collector has nothing to report (a browser without the
3491
+ * APIs, or a host that read nothing): the kernel would accept an all-absent
3492
+ * sample and store nothing from it, so sending it would only cost the WASM
3493
+ * round trip. A collector that throws is a host bug worth surfacing, so it
3494
+ * becomes a diagnostic rather than a silent skip.
3495
+ */
3496
+ async executeDeviceSampleRequest(families) {
3497
+ const collector = this.deviceSampleCollector;
3498
+ if (!collector) {
3499
+ return;
3500
+ }
3501
+ let sample;
3502
+ try {
3503
+ sample = await collector(families);
3504
+ } catch (error) {
3505
+ this.emitDiagnostic(diagnosticFromError(error, "device_sample_collector_failed"));
3506
+ return;
3507
+ }
3508
+ if (!sample || !hasDeviceMetric(sample)) {
3509
+ return;
3510
+ }
3511
+ this.submitCompletion({ type: "recordDeviceSample", sample: compactDeviceSample(sample) });
3512
+ }
3379
3513
  scheduleTimer(timer) {
3380
3514
  this.cancelTimer(timer.id);
3381
3515
  const startedAtMs = this.clock.nowMs();
@@ -3686,7 +3820,8 @@ var ForgeRuntimeBridge = class _ForgeRuntimeBridge {
3686
3820
  },
3687
3821
  onEvent: options.onEvent,
3688
3822
  onDiagnostic: options.onDiagnostic,
3689
- onLog: options.onLog
3823
+ onLog: options.onLog,
3824
+ deviceSampleCollector: options.deviceSampleCollector
3690
3825
  });
3691
3826
  }
3692
3827
  static async create(options) {
@@ -3714,7 +3849,8 @@ var ForgeRuntimeBridge = class _ForgeRuntimeBridge {
3714
3849
  transport: (_c = options.transport) != null ? _c : new BrowserForgeTransport(),
3715
3850
  onEvent: options.onEvent,
3716
3851
  onDiagnostic: options.onDiagnostic,
3717
- onLog: options.onLog
3852
+ onLog: options.onLog,
3853
+ deviceSampleCollector: deviceSampleCollector(options)
3718
3854
  });
3719
3855
  await bridge.process({ type: "start" });
3720
3856
  return { bridge, module: module2 };
@@ -3823,6 +3959,8 @@ var ForgeRuntimeBridge = class _ForgeRuntimeBridge {
3823
3959
  timemarkMs: this.nowMs()
3824
3960
  }
3825
3961
  } : command;
3962
+ case "recordDeviceSample":
3963
+ return command.sample.sampledAtMs === void 0 ? { ...command, sample: { ...command.sample, sampledAtMs: this.nowMs() } } : command;
3826
3964
  default:
3827
3965
  return command;
3828
3966
  }
@@ -3910,6 +4048,13 @@ var ForgeRuntimeBridge = class _ForgeRuntimeBridge {
3910
4048
  function isEffectCompletion(command) {
3911
4049
  return command.type === "httpCompleted" || command.type === "httpFailed" || command.type === "storageReadCompleted" || command.type === "storageWriteCompleted" || command.type === "storageDeleteCompleted";
3912
4050
  }
4051
+ function deviceSampleCollector(options) {
4052
+ var _a;
4053
+ if (options.deviceSampleCollector) {
4054
+ return options.deviceSampleCollector;
4055
+ }
4056
+ return ((_a = options.autoDeviceInsights) != null ? _a : true) ? (families) => collectBrowserDeviceSample(families) : void 0;
4057
+ }
3913
4058
  function runtimeConfig(options) {
3914
4059
  var _a, _b;
3915
4060
  const config = {
@@ -4949,6 +5094,20 @@ var _NPAWForge = class _NPAWForge {
4949
5094
  timestampMs: fix.timestampMs
4950
5095
  });
4951
5096
  }
5097
+ /**
5098
+ * Forwards a device runtime health sample to the kernel, for hosts that can
5099
+ * read more than a browser can (Electron, smart-TV device APIs). Set only the
5100
+ * fields you can read; an omitted field is an absent metric, never a
5101
+ * default. `sampledAtMs` defaults to now. The kernel owns the cadence, the
5102
+ * `dev_*` keys and the diff, and refuses the sample (`ignored_invalid_state`)
5103
+ * while an operator has device insights off. Pair with
5104
+ * `autoDeviceInsights: false` so the built-in browser reader does not
5105
+ * overwrite your values between pushes, or answer the kernel's own requests
5106
+ * with `deviceSampleCollector` instead.
5107
+ */
5108
+ recordDeviceSample(sample) {
5109
+ return this.runtime.process({ type: "recordDeviceSample", sample: compactDeviceSample(sample) });
5110
+ }
4952
5111
  /**
4953
5112
  * Starts or stops location observation to match the capture plan. Nothing is
4954
5113
  * observed - not even the permission state - until an operator enables one