@npaw/forge-web 9.7.1 → 9.7.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/dist/index.js CHANGED
@@ -16,7 +16,9 @@ var CAPTURE_PLAN_BITS = {
16
16
  // kernel never sets them and they must never be reassigned so existing
17
17
  // bits never renumber.
18
18
  SECTIONS: 1 << 13,
19
- ATTRIBUTION: 1 << 14
19
+ ATTRIBUTION: 1 << 14,
20
+ /** The operator enabled a motion switch, so location may be observed. */
21
+ LOCATION: 1 << 15
20
22
  };
21
23
  function isCapturePlanEnabled(bits, bit) {
22
24
  return (bits & bit) !== 0;
@@ -3853,6 +3855,166 @@ function boundaryDiagnostic(error) {
3853
3855
  };
3854
3856
  }
3855
3857
 
3858
+ // src/connection-type.ts
3859
+ function connectionTypeFromNetworkType(type) {
3860
+ switch (type) {
3861
+ case "wifi":
3862
+ return "wifi";
3863
+ case "cellular":
3864
+ return "cellular";
3865
+ case "ethernet":
3866
+ return "ethernet";
3867
+ case "bluetooth":
3868
+ case "wimax":
3869
+ case "other":
3870
+ return "other";
3871
+ case "none":
3872
+ case "unknown":
3873
+ return "unknown";
3874
+ default:
3875
+ return "unknown";
3876
+ }
3877
+ }
3878
+ var ForgeConnectionTypeObserver = class {
3879
+ constructor(publish, connection = readNavigatorConnection()) {
3880
+ this.publish = publish;
3881
+ this.connection = connection;
3882
+ }
3883
+ /**
3884
+ * Publishes the current value and starts listening. Starting twice is a
3885
+ * no-op, so a host that re-initialises does not stack listeners.
3886
+ */
3887
+ start() {
3888
+ var _a;
3889
+ const connection = this.connection;
3890
+ if (!connection || this.listener) {
3891
+ return;
3892
+ }
3893
+ this.report(connectionTypeFromNetworkType(connection.type));
3894
+ const listener = () => {
3895
+ this.report(connectionTypeFromNetworkType(connection.type));
3896
+ };
3897
+ try {
3898
+ (_a = connection.addEventListener) == null ? void 0 : _a.call(connection, "change", listener);
3899
+ this.listener = listener;
3900
+ } catch (e) {
3901
+ }
3902
+ }
3903
+ /**
3904
+ * Stops listening and clears the published value, so a torn-down instance
3905
+ * cannot keep a stale class attached to later events.
3906
+ */
3907
+ stop() {
3908
+ var _a, _b;
3909
+ const listener = this.listener;
3910
+ this.listener = void 0;
3911
+ this.lastPublished = void 0;
3912
+ if (!listener) {
3913
+ return;
3914
+ }
3915
+ try {
3916
+ (_b = (_a = this.connection) == null ? void 0 : _a.removeEventListener) == null ? void 0 : _b.call(_a, "change", listener);
3917
+ } catch (e) {
3918
+ }
3919
+ this.publish(void 0);
3920
+ }
3921
+ /** Publishes only genuine changes; `change` also fires on speed estimates. */
3922
+ report(observed) {
3923
+ if (this.lastPublished === observed) {
3924
+ return;
3925
+ }
3926
+ this.lastPublished = observed;
3927
+ this.publish(observed);
3928
+ }
3929
+ };
3930
+ function readNavigatorConnection() {
3931
+ const navigatorRef = globalThis.navigator;
3932
+ return navigatorRef == null ? void 0 : navigatorRef.connection;
3933
+ }
3934
+
3935
+ // src/location.ts
3936
+ var ForgeLocationObserver = class {
3937
+ constructor(publish, environment = readGeolocationEnvironment()) {
3938
+ this.starting = false;
3939
+ /** Set by stop(), so a start() awaiting its permission query gives up. */
3940
+ this.stopped = false;
3941
+ this.publish = publish;
3942
+ this.environment = environment;
3943
+ }
3944
+ /**
3945
+ * Starts observing if - and only if - the permission is already `granted`.
3946
+ * Starting twice is a no-op.
3947
+ */
3948
+ async start() {
3949
+ const environment = this.environment;
3950
+ if (!environment || this.watchId !== void 0 || this.starting) {
3951
+ return;
3952
+ }
3953
+ this.starting = true;
3954
+ this.stopped = false;
3955
+ try {
3956
+ const state = await environment.queryPermission();
3957
+ if (state !== "granted") {
3958
+ return;
3959
+ }
3960
+ if (this.watchId !== void 0 || this.stopped) {
3961
+ return;
3962
+ }
3963
+ this.watchId = environment.watchPosition(
3964
+ (position) => this.publish(fixFromPosition(position)),
3965
+ () => {
3966
+ }
3967
+ );
3968
+ } catch (e) {
3969
+ } finally {
3970
+ this.starting = false;
3971
+ }
3972
+ }
3973
+ /** Stops observing. Safe to call when never started. */
3974
+ stop() {
3975
+ this.stopped = true;
3976
+ const watchId = this.watchId;
3977
+ this.watchId = void 0;
3978
+ if (watchId === void 0 || !this.environment) {
3979
+ return;
3980
+ }
3981
+ try {
3982
+ this.environment.clearWatch(watchId);
3983
+ } catch (e) {
3984
+ }
3985
+ }
3986
+ };
3987
+ function fixFromPosition(position) {
3988
+ return {
3989
+ latitude: position.coords.latitude,
3990
+ longitude: position.coords.longitude,
3991
+ accuracyMetres: position.coords.accuracy,
3992
+ timestampMs: position.timestamp
3993
+ };
3994
+ }
3995
+ function readGeolocationEnvironment() {
3996
+ const navigatorRef = globalThis.navigator;
3997
+ const geolocation = navigatorRef == null ? void 0 : navigatorRef.geolocation;
3998
+ const permissions = navigatorRef == null ? void 0 : navigatorRef.permissions;
3999
+ if (!geolocation || !(permissions == null ? void 0 : permissions.query)) {
4000
+ return void 0;
4001
+ }
4002
+ return {
4003
+ queryPermission: async () => {
4004
+ const status = await permissions.query({ name: "geolocation" });
4005
+ return status == null ? void 0 : status.state;
4006
+ },
4007
+ watchPosition: (onFix, onError) => geolocation.watchPosition(onFix, onError, {
4008
+ // Coarse and cheap: this answers "moving or not", which needs neither
4009
+ // navigation-grade accuracy nor its battery cost.
4010
+ enableHighAccuracy: false,
4011
+ maximumAge: 3e4,
4012
+ timeout: 6e4
4013
+ }),
4014
+ clearWatch: (id) => geolocation.clearWatch(id)
4015
+ };
4016
+ }
4017
+
3856
4018
  // src/video.ts
3857
4019
  var PlayerAdapter = class {
3858
4020
  attach(video) {
@@ -4449,6 +4611,7 @@ var _NPAWForge = class _NPAWForge {
4449
4611
  this.videos = /* @__PURE__ */ new Map();
4450
4612
  }
4451
4613
  static async create(options) {
4614
+ var _a, _b;
4452
4615
  const { bridge } = await ForgeRuntimeBridge.create(options);
4453
4616
  const forge = new _NPAWForge(bridge);
4454
4617
  if (options.startSessionOnInitialize) {
@@ -4456,9 +4619,23 @@ var _NPAWForge = class _NPAWForge {
4456
4619
  }
4457
4620
  forge.captureHandle = installCapture(forge, options.capture);
4458
4621
  forge.capturePlanUnsubscribe = bridge.onCapturePlan((plan) => {
4459
- var _a, _b;
4460
- (_b = (_a = forge.captureHandle) == null ? void 0 : _a.applyPlan) == null ? void 0 : _b.call(_a, plan);
4622
+ var _a2, _b2;
4623
+ (_b2 = (_a2 = forge.captureHandle) == null ? void 0 : _a2.applyPlan) == null ? void 0 : _b2.call(_a2, plan);
4461
4624
  });
4625
+ if ((_a = options.autoLocation) != null ? _a : true) {
4626
+ forge.locationObserver = new ForgeLocationObserver((fix) => {
4627
+ void forge.recordLocationFix(fix);
4628
+ });
4629
+ bridge.onCapturePlan((plan) => {
4630
+ forge.applyLocationPlan(plan);
4631
+ });
4632
+ }
4633
+ if ((_b = options.autoConnectionType) != null ? _b : true) {
4634
+ forge.connectionTypeObserver = new ForgeConnectionTypeObserver((connectionType) => {
4635
+ void forge.setAutoConnectionType(connectionType);
4636
+ });
4637
+ forge.connectionTypeObserver.start();
4638
+ }
4462
4639
  return forge;
4463
4640
  }
4464
4641
  onEvent(listener) {
@@ -4624,6 +4801,40 @@ var _NPAWForge = class _NPAWForge {
4624
4801
  setLogLevel(level) {
4625
4802
  return this.runtime.process({ type: "setLogLevel", level });
4626
4803
  }
4804
+ /**
4805
+ * Reports the browser's current connection class to the kernel. Called by the
4806
+ * automatic observer; a `connectionType` set through video metadata keeps
4807
+ * precedence over it.
4808
+ */
4809
+ setAutoConnectionType(connectionType) {
4810
+ return this.runtime.process({ type: "setAutoConnectionType", connectionType });
4811
+ }
4812
+ /** Forwards a location fix to the kernel, which owns every threshold. */
4813
+ recordLocationFix(fix) {
4814
+ return this.runtime.process({
4815
+ type: "recordLocationFix",
4816
+ latitude: fix.latitude,
4817
+ longitude: fix.longitude,
4818
+ accuracyM: fix.accuracyMetres,
4819
+ timestampMs: fix.timestampMs
4820
+ });
4821
+ }
4822
+ /**
4823
+ * Starts or stops location observation to match the capture plan. Nothing is
4824
+ * observed - not even the permission state - until an operator enables one
4825
+ * of the two motion switches.
4826
+ */
4827
+ applyLocationPlan(plan) {
4828
+ const observer = this.locationObserver;
4829
+ if (!observer) {
4830
+ return;
4831
+ }
4832
+ if ((plan.bits & CAPTURE_PLAN_BITS.LOCATION) !== 0) {
4833
+ void observer.start();
4834
+ } else {
4835
+ observer.stop();
4836
+ }
4837
+ }
4627
4838
  applicationForeground(foreground) {
4628
4839
  return this.runtime.process({
4629
4840
  type: "applicationForeground",
@@ -4677,7 +4888,7 @@ var _NPAWForge = class _NPAWForge {
4677
4888
  return (_b = video == null ? void 0 : video.removeAdAdapter()) != null ? _b : false;
4678
4889
  }
4679
4890
  async destroy() {
4680
- var _a, _b;
4891
+ var _a, _b, _c, _d;
4681
4892
  for (const video of this.videos.values()) {
4682
4893
  video.detach();
4683
4894
  }
@@ -4687,6 +4898,10 @@ var _NPAWForge = class _NPAWForge {
4687
4898
  this.capturePlanUnsubscribe = void 0;
4688
4899
  (_b = this.captureHandle) == null ? void 0 : _b.dispose();
4689
4900
  this.captureHandle = void 0;
4901
+ (_c = this.connectionTypeObserver) == null ? void 0 : _c.stop();
4902
+ this.connectionTypeObserver = void 0;
4903
+ (_d = this.locationObserver) == null ? void 0 : _d.stop();
4904
+ this.locationObserver = void 0;
4690
4905
  return this.runtime.destroy();
4691
4906
  }
4692
4907
  whenIdle() {