@zerogpu/browser-extension-sdk 0.2.2 → 0.2.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.
@@ -1,5 +1,5 @@
1
- const SDK_VERSION = typeof "0.2.2+0a17606" === 'string' && "0.2.2+0a17606".length > 0
2
- ? "0.2.2+0a17606"
1
+ const SDK_VERSION = typeof "0.2.3+38ce04b" === 'string' && "0.2.3+38ce04b".length > 0
2
+ ? "0.2.3+38ce04b"
3
3
  : '0.0.0-dev';
4
4
 
5
5
  const ORCHESTRATOR_BASE_URL = typeof "https://devices.zerogpu.ai" === 'string' && "https://devices.zerogpu.ai".length > 0
@@ -5245,6 +5245,86 @@ const initZeroGpuSdk = async (options) => {
5245
5245
  return handle;
5246
5246
  };
5247
5247
 
5248
+ /**
5249
+ * ZeroGPU "extension kit" — end-user consent store.
5250
+ *
5251
+ * An end user must be able to stop the SDK on their own device, and have that
5252
+ * stick across browser restarts. That decision lives in `chrome.storage.local`
5253
+ * rather than the offscreen document's `localStorage`, because the service
5254
+ * worker has to read it *before* deciding whether to create the offscreen
5255
+ * document at all — and it has no access to that document's storage.
5256
+ *
5257
+ * Enforcement is entirely client-side: opting out stops the device connecting.
5258
+ * Nothing is deregistered server-side.
5259
+ */
5260
+ /** Single `chrome.storage.local` key holding the whole record. */
5261
+ const ZEROGPU_CONSENT_KEY = 'zerogpu:consent';
5262
+ /**
5263
+ * Opted IN until told otherwise, which preserves the behaviour every already
5264
+ * shipped extension has today: adding this module must not silently stop
5265
+ * existing installs. The hosted settings page is what turns it off.
5266
+ */
5267
+ const DEFAULT_CONSENT = {
5268
+ optedIn: true,
5269
+ updatedAt: null,
5270
+ source: 'default'
5271
+ };
5272
+ const getLocalStorageArea = () => typeof chrome !== 'undefined' ? chrome?.storage?.local : undefined;
5273
+ /**
5274
+ * Coerce whatever is in storage into a usable record. Anything unexpected —
5275
+ * a partial write, a value from a future version, a corrupted entry — falls
5276
+ * back to the default rather than throwing: a consent read must never be the
5277
+ * reason the SDK fails to start.
5278
+ */
5279
+ const normalize = (raw) => {
5280
+ if (!raw || typeof raw !== 'object')
5281
+ return DEFAULT_CONSENT;
5282
+ const value = raw;
5283
+ if (typeof value.optedIn !== 'boolean')
5284
+ return DEFAULT_CONSENT;
5285
+ return {
5286
+ optedIn: value.optedIn,
5287
+ updatedAt: typeof value.updatedAt === 'string' ? value.updatedAt : null,
5288
+ source: value.source === 'extension' || value.source === 'settings-page' || value.source === 'default'
5289
+ ? value.source
5290
+ : 'default'
5291
+ };
5292
+ };
5293
+ /** Read the stored decision, or the default when nothing has been stored. */
5294
+ const getZeroGpuConsent = async () => {
5295
+ const area = getLocalStorageArea();
5296
+ if (!area)
5297
+ return DEFAULT_CONSENT;
5298
+ try {
5299
+ const stored = await area.get(ZEROGPU_CONSENT_KEY);
5300
+ return normalize(stored?.[ZEROGPU_CONSENT_KEY]);
5301
+ }
5302
+ catch {
5303
+ // A blocked or full storage area must not keep the SDK from starting.
5304
+ return DEFAULT_CONSENT;
5305
+ }
5306
+ };
5307
+ /**
5308
+ * Subscribe to consent changes from any extension context (the hosted settings
5309
+ * page writing through the service worker, the options page, another window).
5310
+ * Returns an unsubscribe function.
5311
+ */
5312
+ const onZeroGpuConsentChanged = (listener) => {
5313
+ const onChanged = typeof chrome !== 'undefined' ? chrome?.storage?.onChanged : undefined;
5314
+ if (!onChanged?.addListener)
5315
+ return () => { };
5316
+ const handler = (changes, areaName) => {
5317
+ if (areaName !== 'local')
5318
+ return;
5319
+ const change = changes?.[ZEROGPU_CONSENT_KEY];
5320
+ if (!change)
5321
+ return;
5322
+ listener(normalize(change.newValue));
5323
+ };
5324
+ onChanged.addListener(handler);
5325
+ return () => onChanged.removeListener?.(handler);
5326
+ };
5327
+
5248
5328
  /**
5249
5329
  * ZeroGPU "extension kit" — offscreen host helper.
5250
5330
  *
@@ -5362,12 +5442,42 @@ const buildOverrides = (options) => {
5362
5442
  }
5363
5443
  return overrides;
5364
5444
  };
5445
+ /**
5446
+ * Subscribed once per document. Holding the unsubscribe also guards against
5447
+ * stacking a listener — and against a recursion loop — when init runs again.
5448
+ */
5449
+ let consentResumeWatch = null;
5450
+ /**
5451
+ * Resume when the user opts back in. The service worker's
5452
+ * `ensureZeroGpuOffscreen` returns early once a document exists, so it will not
5453
+ * re-run init for a document that is already open — which is precisely the
5454
+ * state the early return below leaves behind. Without this watch such a
5455
+ * document stays idle until it is torn down and recreated.
5456
+ */
5457
+ const watchConsentForResume = (options) => {
5458
+ if (consentResumeWatch)
5459
+ return;
5460
+ consentResumeWatch = onZeroGpuConsentChanged((consent) => {
5461
+ if (consent.optedIn)
5462
+ void initZeroGpuOffscreen(options);
5463
+ });
5464
+ };
5365
5465
  /**
5366
5466
  * Pre-load the vendored model runtimes (so the SDK never reaches the CDN) and
5367
5467
  * initialize the ZeroGPU SDK. Safe to call more than once; the SDK ignores
5368
5468
  * repeated init.
5369
5469
  */
5370
5470
  const initZeroGpuOffscreen = async (options = {}) => {
5471
+ // Installed before the opted-out return below, so a document parked by that
5472
+ // return can still come back to life.
5473
+ watchConsentForResume(options);
5474
+ // Belt-and-braces. The service-worker bridge already refuses to create this
5475
+ // document while the user is opted out, but a host that creates it by some
5476
+ // other path must not start the SDK either. Loading the model runtimes below
5477
+ // is expensive, so this check comes before any of it.
5478
+ if (!(await getZeroGpuConsent()).optedIn) {
5479
+ return;
5480
+ }
5371
5481
  const assetsBaseUrl = resolveAssetsBaseUrl(options.assetsBaseUrl);
5372
5482
  // Point the JS-tag firer at the hosted runner (unless disabled) before
5373
5483
  // inference starts.