@npaw/forge-web 9.7.3 → 9.7.5

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/dist/index.js CHANGED
@@ -18,7 +18,12 @@ var CAPTURE_PLAN_BITS = {
18
18
  SECTIONS: 1 << 13,
19
19
  ATTRIBUTION: 1 << 14,
20
20
  /** The operator enabled a motion switch, so location may be observed. */
21
- LOCATION: 1 << 15
21
+ LOCATION: 1 << 15,
22
+ /**
23
+ * The operator enabled device insights (`di.en` with a non-empty family
24
+ * allowlist); the kernel will emit `deviceSampleRequest` effects.
25
+ */
26
+ DEVICE_INSIGHTS: 1 << 16
22
27
  };
23
28
  function isCapturePlanEnabled(bits, bit) {
24
29
  return (bits & bit) !== 0;
@@ -3014,6 +3019,104 @@ function randomUuid() {
3014
3019
  return `${random()}-${random().slice(0, 4)}-4${random().slice(0, 3)}-a${random().slice(0, 3)}-${random()}${random().slice(0, 4)}`;
3015
3020
  }
3016
3021
 
3022
+ // src/device-insights.ts
3023
+ var METRIC_FIELDS = [
3024
+ "cpuTimeMs",
3025
+ "cpuCores",
3026
+ "memUsedBytes",
3027
+ "memAvailBytes",
3028
+ "batteryLevelPct",
3029
+ "batteryCharging",
3030
+ "thermal",
3031
+ "diskFreeBytes"
3032
+ ];
3033
+ async function collectBrowserDeviceSample(families, environment = readBrowserDeviceInsightsEnvironment()) {
3034
+ const sample = {};
3035
+ if (families.includes("mem")) {
3036
+ const usedBytes = readUsedJsHeapBytes(environment);
3037
+ if (usedBytes !== void 0) {
3038
+ sample.memUsedBytes = usedBytes;
3039
+ }
3040
+ }
3041
+ if (families.includes("bat")) {
3042
+ const battery = await readBattery(environment);
3043
+ if (battery.levelPct !== void 0) {
3044
+ sample.batteryLevelPct = battery.levelPct;
3045
+ }
3046
+ if (battery.charging !== void 0) {
3047
+ sample.batteryCharging = battery.charging;
3048
+ }
3049
+ }
3050
+ return hasDeviceMetric(sample) ? sample : void 0;
3051
+ }
3052
+ function hasDeviceMetric(sample) {
3053
+ return METRIC_FIELDS.some((field) => sample[field] !== void 0);
3054
+ }
3055
+ function compactDeviceSample(sample) {
3056
+ const compact = {};
3057
+ if (sample.sampledAtMs !== void 0) {
3058
+ compact.sampledAtMs = sample.sampledAtMs;
3059
+ }
3060
+ for (const field of METRIC_FIELDS) {
3061
+ const value = sample[field];
3062
+ if (value !== void 0) {
3063
+ compact[field] = value;
3064
+ }
3065
+ }
3066
+ return compact;
3067
+ }
3068
+ function readUsedJsHeapBytes(environment) {
3069
+ var _a;
3070
+ if (!environment.memory) {
3071
+ return void 0;
3072
+ }
3073
+ let used;
3074
+ try {
3075
+ used = (_a = environment.memory()) == null ? void 0 : _a.usedJSHeapSize;
3076
+ } catch (e) {
3077
+ return void 0;
3078
+ }
3079
+ if (typeof used !== "number" || !Number.isFinite(used) || used <= 0) {
3080
+ return void 0;
3081
+ }
3082
+ return Math.floor(used);
3083
+ }
3084
+ async function readBattery(environment) {
3085
+ if (!environment.getBattery) {
3086
+ return {};
3087
+ }
3088
+ let manager;
3089
+ try {
3090
+ manager = await environment.getBattery();
3091
+ } catch (e) {
3092
+ return {};
3093
+ }
3094
+ if (!manager) {
3095
+ return {};
3096
+ }
3097
+ const result = {};
3098
+ const level = manager.level;
3099
+ if (typeof level === "number" && Number.isFinite(level) && level >= 0 && level <= 1) {
3100
+ result.levelPct = Math.round(level * 100);
3101
+ }
3102
+ if (typeof manager.charging === "boolean") {
3103
+ result.charging = manager.charging;
3104
+ }
3105
+ return result;
3106
+ }
3107
+ function readBrowserDeviceInsightsEnvironment() {
3108
+ const environment = {};
3109
+ const performanceRef = globalThis.performance;
3110
+ if (performanceRef && typeof performanceRef.memory === "object" && performanceRef.memory !== null) {
3111
+ environment.memory = () => performanceRef.memory;
3112
+ }
3113
+ const navigatorRef = globalThis.navigator;
3114
+ if (navigatorRef && typeof navigatorRef.getBattery === "function") {
3115
+ environment.getBattery = () => navigatorRef.getBattery();
3116
+ }
3117
+ return environment;
3118
+ }
3119
+
3017
3120
  // src/utils.ts
3018
3121
  function systemClock() {
3019
3122
  return {
@@ -3108,6 +3211,7 @@ var BrowserEffectExecutor = class {
3108
3211
  this.transport = options.transport;
3109
3212
  this.storage = options.storage;
3110
3213
  this.submitCompletion = options.submitCompletion;
3214
+ this.deviceSampleCollector = options.deviceSampleCollector;
3111
3215
  if (options.onEvent) {
3112
3216
  this.eventListeners.add(options.onEvent);
3113
3217
  }
@@ -3170,6 +3274,11 @@ var BrowserEffectExecutor = class {
3170
3274
  case "diagnostic":
3171
3275
  this.emitDiagnostic(effect);
3172
3276
  break;
3277
+ case "deviceSampleRequest":
3278
+ this.track(this.executeDeviceSampleRequest(effect.families));
3279
+ break;
3280
+ default:
3281
+ break;
3173
3282
  }
3174
3283
  }
3175
3284
  }
@@ -3246,6 +3355,31 @@ var BrowserEffectExecutor = class {
3246
3355
  });
3247
3356
  }
3248
3357
  }
3358
+ /**
3359
+ * Answers a kernel device sample request through the collector. Nothing is
3360
+ * sent when the collector has nothing to report (a browser without the
3361
+ * APIs, or a host that read nothing): the kernel would accept an all-absent
3362
+ * sample and store nothing from it, so sending it would only cost the WASM
3363
+ * round trip. A collector that throws is a host bug worth surfacing, so it
3364
+ * becomes a diagnostic rather than a silent skip.
3365
+ */
3366
+ async executeDeviceSampleRequest(families) {
3367
+ const collector = this.deviceSampleCollector;
3368
+ if (!collector) {
3369
+ return;
3370
+ }
3371
+ let sample;
3372
+ try {
3373
+ sample = await collector(families);
3374
+ } catch (error) {
3375
+ this.emitDiagnostic(diagnosticFromError(error, "device_sample_collector_failed"));
3376
+ return;
3377
+ }
3378
+ if (!sample || !hasDeviceMetric(sample)) {
3379
+ return;
3380
+ }
3381
+ this.submitCompletion({ type: "recordDeviceSample", sample: compactDeviceSample(sample) });
3382
+ }
3249
3383
  scheduleTimer(timer) {
3250
3384
  this.cancelTimer(timer.id);
3251
3385
  const startedAtMs = this.clock.nowMs();
@@ -3556,7 +3690,8 @@ var ForgeRuntimeBridge = class _ForgeRuntimeBridge {
3556
3690
  },
3557
3691
  onEvent: options.onEvent,
3558
3692
  onDiagnostic: options.onDiagnostic,
3559
- onLog: options.onLog
3693
+ onLog: options.onLog,
3694
+ deviceSampleCollector: options.deviceSampleCollector
3560
3695
  });
3561
3696
  }
3562
3697
  static async create(options) {
@@ -3584,7 +3719,8 @@ var ForgeRuntimeBridge = class _ForgeRuntimeBridge {
3584
3719
  transport: (_c = options.transport) != null ? _c : new BrowserForgeTransport(),
3585
3720
  onEvent: options.onEvent,
3586
3721
  onDiagnostic: options.onDiagnostic,
3587
- onLog: options.onLog
3722
+ onLog: options.onLog,
3723
+ deviceSampleCollector: deviceSampleCollector(options)
3588
3724
  });
3589
3725
  await bridge.process({ type: "start" });
3590
3726
  return { bridge, module };
@@ -3693,6 +3829,8 @@ var ForgeRuntimeBridge = class _ForgeRuntimeBridge {
3693
3829
  timemarkMs: this.nowMs()
3694
3830
  }
3695
3831
  } : command;
3832
+ case "recordDeviceSample":
3833
+ return command.sample.sampledAtMs === void 0 ? { ...command, sample: { ...command.sample, sampledAtMs: this.nowMs() } } : command;
3696
3834
  default:
3697
3835
  return command;
3698
3836
  }
@@ -3780,6 +3918,13 @@ var ForgeRuntimeBridge = class _ForgeRuntimeBridge {
3780
3918
  function isEffectCompletion(command) {
3781
3919
  return command.type === "httpCompleted" || command.type === "httpFailed" || command.type === "storageReadCompleted" || command.type === "storageWriteCompleted" || command.type === "storageDeleteCompleted";
3782
3920
  }
3921
+ function deviceSampleCollector(options) {
3922
+ var _a;
3923
+ if (options.deviceSampleCollector) {
3924
+ return options.deviceSampleCollector;
3925
+ }
3926
+ return ((_a = options.autoDeviceInsights) != null ? _a : true) ? (families) => collectBrowserDeviceSample(families) : void 0;
3927
+ }
3783
3928
  function runtimeConfig(options) {
3784
3929
  var _a, _b;
3785
3930
  const config = {
@@ -4819,6 +4964,20 @@ var _NPAWForge = class _NPAWForge {
4819
4964
  timestampMs: fix.timestampMs
4820
4965
  });
4821
4966
  }
4967
+ /**
4968
+ * Forwards a device runtime health sample to the kernel, for hosts that can
4969
+ * read more than a browser can (Electron, smart-TV device APIs). Set only the
4970
+ * fields you can read; an omitted field is an absent metric, never a
4971
+ * default. `sampledAtMs` defaults to now. The kernel owns the cadence, the
4972
+ * `dev_*` keys and the diff, and refuses the sample (`ignored_invalid_state`)
4973
+ * while an operator has device insights off. Pair with
4974
+ * `autoDeviceInsights: false` so the built-in browser reader does not
4975
+ * overwrite your values between pushes, or answer the kernel's own requests
4976
+ * with `deviceSampleCollector` instead.
4977
+ */
4978
+ recordDeviceSample(sample) {
4979
+ return this.runtime.process({ type: "recordDeviceSample", sample: compactDeviceSample(sample) });
4980
+ }
4822
4981
  /**
4823
4982
  * Starts or stops location observation to match the capture plan. Nothing is
4824
4983
  * observed - not even the permission state - until an operator enables one