fullscreen-engine 1.0.0 → 1.1.0

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,4 +1,4 @@
1
- /*! fullscreen-engine v1.0.0 | MIT */
1
+ /*! fullscreen-engine v1.1.0 | MIT */
2
2
  (function (global, factory) {
3
3
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
4
4
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
@@ -675,7 +675,7 @@
675
675
  );
676
676
  }
677
677
 
678
- function normalizeAutoRelock(val) {
678
+ function normalizeAutoRelock$1(val) {
679
679
  if (val === undefined || val === null || val === false) {
680
680
  return { enabled: false, delay: 50, maxRetries: 3 };
681
681
  }
@@ -694,9 +694,9 @@
694
694
  throw new TypeError('[fullscreen-engine] invalid autoRelock option: expected boolean or object');
695
695
  }
696
696
 
697
- function createFullscreen(userOptions = {}) {
697
+ function createFullscreen$1(userOptions = {}) {
698
698
  const triggers = normalizeTriggers(userOptions.trigger);
699
- const relockConfig = normalizeAutoRelock(userOptions.autoRelock);
699
+ const relockConfig = normalizeAutoRelock$1(userOptions.autoRelock);
700
700
 
701
701
  const fallbackOption = userOptions.fallback ?? 'none';
702
702
  if (fallbackOption !== 'css' && fallbackOption !== 'none') {
@@ -1173,12 +1173,518 @@
1173
1173
  };
1174
1174
  }
1175
1175
 
1176
+ /**
1177
+ * Electron renderer-side adapter.
1178
+ *
1179
+ * Provide this on the renderer via contextBridge (window.fsNative).
1180
+ */
1181
+
1182
+
1183
+ function normalizeAutoRelock(val) {
1184
+ if (val === undefined || val === null || val === false) {
1185
+ return { enabled: false, delay: 50, maxRetries: 3 };
1186
+ }
1187
+ if (val === true) {
1188
+ return { enabled: true, delay: 50, maxRetries: 3 };
1189
+ }
1190
+ if (typeof val === 'object') {
1191
+ const delayNum = Number(val.delay);
1192
+ const retriesNum = Number(val.maxRetries);
1193
+ return {
1194
+ enabled: val.enabled ?? true,
1195
+ delay: Number.isFinite(delayNum) ? Math.max(0, delayNum) : 50,
1196
+ maxRetries: Number.isFinite(retriesNum) ? Math.max(1, Math.trunc(retriesNum)) : 3
1197
+ };
1198
+ }
1199
+ throw new TypeError('[fullscreen-engine/electron] invalid autoRelock option: expected boolean or object');
1200
+ }
1201
+
1202
+ function createElectronFullscreen(bridge, opts = {}) {
1203
+ if (!bridge || typeof bridge.set !== 'function') {
1204
+ throw new Error('[fullscreen-engine/electron] bridge with .set() required');
1205
+ }
1206
+
1207
+ const relockConfig = normalizeAutoRelock(opts.autoRelock);
1208
+ const onError = typeof opts.onError === 'function' ? opts.onError : null;
1209
+ const emitter = new Emitter();
1210
+ let active = false;
1211
+ let destroyed = false;
1212
+ let unsubscribe = null;
1213
+ let exitLocked = !!opts.preventExit;
1214
+ let manualUnlocking = false;
1215
+ let relockTimer = null;
1216
+ let relockRetries = 0;
1217
+
1218
+ function emitError(err) {
1219
+ emitter.emit('error', err);
1220
+ if (onError) onError(err);
1221
+ }
1222
+
1223
+ function clearRelockTimer() {
1224
+ if (relockTimer !== null) {
1225
+ clearTimeout(relockTimer);
1226
+ relockTimer = null;
1227
+ }
1228
+ }
1229
+
1230
+ function getState() {
1231
+ return {
1232
+ active,
1233
+ source: active ? 'electron' : 'none',
1234
+ element: null,
1235
+ supported: true
1236
+ };
1237
+ }
1238
+
1239
+ function scheduleRelock() {
1240
+ if (destroyed || !relockConfig.enabled || manualUnlocking) return;
1241
+ if (relockRetries >= relockConfig.maxRetries) {
1242
+ emitError(new Error('[fullscreen-engine/electron] autoRelock failed after maxRetries'));
1243
+ return;
1244
+ }
1245
+ clearRelockTimer();
1246
+ relockTimer = setTimeout(() => {
1247
+ relockTimer = null;
1248
+ if (destroyed || !relockConfig.enabled || manualUnlocking || active) return;
1249
+ relockRetries += 1;
1250
+ request()
1251
+ .catch((err) => {
1252
+ emitError(err);
1253
+ if (!destroyed && relockConfig.enabled && !manualUnlocking && !active) {
1254
+ scheduleRelock();
1255
+ }
1256
+ });
1257
+ }, relockConfig.delay);
1258
+ }
1259
+
1260
+ function updateActive(next) {
1261
+ const boolNext = !!next;
1262
+ if (boolNext) {
1263
+ relockRetries = 0;
1264
+ clearRelockTimer();
1265
+ }
1266
+ if (boolNext === active) return;
1267
+ const wasActive = active;
1268
+ active = boolNext;
1269
+ const state = getState();
1270
+ emitter.emit('change', state);
1271
+ if (active) emitter.emit('enter', state);
1272
+ else {
1273
+ emitter.emit('exit', state);
1274
+ if (wasActive && relockConfig.enabled && !manualUnlocking) {
1275
+ scheduleRelock();
1276
+ }
1277
+ }
1278
+ }
1279
+
1280
+ if (typeof bridge.onChange === 'function') {
1281
+ unsubscribe = bridge.onChange((on) => {
1282
+ if (!destroyed) updateActive(on);
1283
+ });
1284
+ }
1285
+
1286
+ if (typeof bridge.get === 'function') {
1287
+ Promise.resolve(bridge.get())
1288
+ .then((on) => {
1289
+ if (!destroyed) updateActive(on);
1290
+ })
1291
+ .catch(() => {});
1292
+ }
1293
+
1294
+ async function request() {
1295
+ if (destroyed) throw new Error('[fullscreen-engine/electron] destroyed');
1296
+ manualUnlocking = false;
1297
+ const res = await bridge.set(true);
1298
+ if (res === false) {
1299
+ throw new Error('[fullscreen-engine/electron] bridge.set(true) returned false');
1300
+ }
1301
+ updateActive(true);
1302
+ }
1303
+
1304
+ async function exit() {
1305
+ if (destroyed) throw new Error('[fullscreen-engine/electron] destroyed');
1306
+ if (exitLocked) {
1307
+ const err = new Error('[fullscreen-engine/electron] Exit blocked: preventExit is active');
1308
+ emitError(err);
1309
+ throw err;
1310
+ }
1311
+ const res = await bridge.set(false);
1312
+ if (res === true) {
1313
+ throw new Error('[fullscreen-engine/electron] bridge.set(false) failed to exit');
1314
+ }
1315
+ updateActive(false);
1316
+ }
1317
+
1318
+ function unlock() {
1319
+ exitLocked = false;
1320
+ manualUnlocking = true;
1321
+ clearRelockTimer();
1322
+ relockRetries = 0;
1323
+ }
1324
+
1325
+ async function relock() {
1326
+ manualUnlocking = false;
1327
+ return request();
1328
+ }
1329
+
1330
+ return {
1331
+ request,
1332
+ exit,
1333
+ async toggle() {
1334
+ return active ? exit() : request();
1335
+ },
1336
+ unlock,
1337
+ relock,
1338
+ isActive() { return active; },
1339
+ isSupported() { return true; },
1340
+ getState,
1341
+ on(event, cb) { return emitter.on(event, cb); },
1342
+ once(event, cb) { return emitter.once(event, cb); },
1343
+ off(event, cb) { return emitter.off(event, cb); },
1344
+ destroy() {
1345
+ if (destroyed) return;
1346
+ destroyed = true;
1347
+ clearRelockTimer();
1348
+ if (typeof unsubscribe === 'function') unsubscribe();
1349
+ unsubscribe = null;
1350
+ active = false;
1351
+ emitter.clear();
1352
+ }
1353
+ };
1354
+ }
1355
+
1356
+ /**
1357
+ * True backend fullscreen control from Node.js.
1358
+ *
1359
+ * Uses Chrome DevTools Protocol (CDP) via Puppeteer to control the
1360
+ * browser window's fullscreen state. This works from a Node.js process.
1361
+ */
1362
+
1363
+ function createPuppeteerFullscreen(page, opts = {}) {
1364
+ if (!page || typeof page.createCDPSession !== 'function') {
1365
+ throw new Error('[fullscreen-engine/puppeteer] page with .createCDPSession() required');
1366
+ }
1367
+
1368
+ let client = null;
1369
+ let windowId = null;
1370
+ let initPromise = null;
1371
+ let active = false;
1372
+ let destroyed = false;
1373
+ let exitLocked = !!opts.preventExit;
1374
+
1375
+ async function ensure() {
1376
+ if (destroyed) throw new Error('[fullscreen-engine/puppeteer] destroyed');
1377
+ if (client && windowId !== null) return;
1378
+ if (!initPromise) {
1379
+ initPromise = (async () => {
1380
+ let session = null;
1381
+ try {
1382
+ session = await page.createCDPSession();
1383
+ const res = await session.send('Browser.getWindowForTarget');
1384
+ if (destroyed) {
1385
+ try { await session.detach(); } catch {}
1386
+ throw new Error('[fullscreen-engine/puppeteer] destroyed');
1387
+ }
1388
+ client = session;
1389
+ windowId = res.windowId;
1390
+ } catch (err) {
1391
+ if (session) {
1392
+ try { await session.detach(); } catch {}
1393
+ }
1394
+ client = null;
1395
+ windowId = null;
1396
+ throw err;
1397
+ } finally {
1398
+ initPromise = null;
1399
+ }
1400
+ })();
1401
+ }
1402
+ return initPromise;
1403
+ }
1404
+
1405
+ async function setState(state) {
1406
+ if (destroyed) throw new Error('[fullscreen-engine/puppeteer] destroyed');
1407
+ await ensure();
1408
+ await client.send('Browser.setWindowBounds', {
1409
+ windowId,
1410
+ bounds: { windowState: state } // 'fullscreen' | 'normal' | 'minimized' | 'maximized'
1411
+ });
1412
+ active = state === 'fullscreen';
1413
+ return active;
1414
+ }
1415
+
1416
+ return {
1417
+ async request() {
1418
+ if (destroyed) throw new Error('[fullscreen-engine/puppeteer] destroyed');
1419
+ if (active) return true;
1420
+ return setState('fullscreen');
1421
+ },
1422
+ async exit() {
1423
+ if (destroyed) throw new Error('[fullscreen-engine/puppeteer] destroyed');
1424
+ if (exitLocked) {
1425
+ throw new Error('[fullscreen-engine/puppeteer] Exit blocked: preventExit is active');
1426
+ }
1427
+ if (!active) {
1428
+ const current = await this.getWindowState();
1429
+ if (current !== 'fullscreen') return false;
1430
+ }
1431
+ return setState(opts.restoreState || 'normal');
1432
+ },
1433
+ async toggle() {
1434
+ if (destroyed) throw new Error('[fullscreen-engine/puppeteer] destroyed');
1435
+ const current = await this.getWindowState();
1436
+ return current === 'fullscreen' ? this.exit() : this.request();
1437
+ },
1438
+ unlock() {
1439
+ exitLocked = false;
1440
+ },
1441
+ async relock() {
1442
+ if (destroyed) throw new Error('[fullscreen-engine/puppeteer] destroyed');
1443
+ const current = await this.getWindowState();
1444
+ if (current === 'fullscreen') return true;
1445
+ return setState('fullscreen');
1446
+ },
1447
+ isActive() {
1448
+ return active;
1449
+ },
1450
+ isSupported() {
1451
+ return true;
1452
+ },
1453
+ async getWindowState() {
1454
+ if (destroyed) throw new Error('[fullscreen-engine/puppeteer] destroyed');
1455
+ await ensure();
1456
+ const res = await client.send('Browser.getWindowBounds', { windowId });
1457
+ const state = res.bounds.windowState;
1458
+ active = state === 'fullscreen';
1459
+ return state;
1460
+ },
1461
+ async destroy() {
1462
+ if (destroyed) return;
1463
+ destroyed = true;
1464
+ if (initPromise) {
1465
+ try { await initPromise; } catch {}
1466
+ }
1467
+ try { if (client) await client.detach(); } catch {}
1468
+ client = null;
1469
+ windowId = null;
1470
+ initPromise = null;
1471
+ active = false;
1472
+ }
1473
+ };
1474
+ }
1475
+
1476
+ /**
1477
+ * SSR / Node.js safe stub.
1478
+ *
1479
+ * Importing the main entry already works in Node (all guards are in place).
1480
+ * But if you explicitly need a no-op controller (e.g. for template
1481
+ * environments that can't tree-shake), use this:
1482
+ *
1483
+ * import { createFullscreen } from 'fullscreen-engine/adapters/ssr';
1484
+ */
1485
+
1486
+ function createFullscreen() {
1487
+ return {
1488
+ async request() { throw new Error('[fullscreen-engine/ssr] fullscreen unavailable in SSR'); },
1489
+ async exit() {},
1490
+ async toggle() { return this.request(); },
1491
+ isActive() { return false; },
1492
+ isSupported() { return false; },
1493
+ isFallback() { return false; },
1494
+ getState() { return { active: false, source: 'none', element: null, supported: false }; },
1495
+ on() { return () => {}; },
1496
+ once() { return () => {}; },
1497
+ off() {},
1498
+ destroy() {}
1499
+ };
1500
+ }
1501
+
1502
+ /**
1503
+ * Global configuration engine for fullscreen-engine.
1504
+ *
1505
+ * Pure isomorphic module (no Node.js built-in imports) safe for Browser,
1506
+ * Electron renderer, Puppeteer/Node, and SSR environments.
1507
+ */
1508
+
1509
+
1510
+ function isPlainObject(val) {
1511
+ return typeof val === 'object' && val !== null && !Array.isArray(val);
1512
+ }
1513
+
1514
+ function mergeAutoRelock(baseVal, nextVal) {
1515
+ if (nextVal === undefined) return baseVal;
1516
+ if (isPlainObject(baseVal) && isPlainObject(nextVal)) {
1517
+ return { ...baseVal, ...nextVal };
1518
+ }
1519
+ return nextVal;
1520
+ }
1521
+
1522
+ function mergeSection(base, ext) {
1523
+ if (!ext) return { ...base };
1524
+ const merged = { ...base, ...ext };
1525
+ if ('autoRelock' in base || 'autoRelock' in ext) {
1526
+ merged.autoRelock = mergeAutoRelock(base.autoRelock, ext.autoRelock);
1527
+ }
1528
+ return merged;
1529
+ }
1530
+
1531
+ function validateConfigShape(cfg, label = 'config') {
1532
+ if (!isPlainObject(cfg)) {
1533
+ throw new TypeError(`[fullscreen-engine/config] ${label} must be a plain object`);
1534
+ }
1535
+ if (cfg.mode !== undefined && (typeof cfg.mode !== 'string' || !(cfg.mode in PRESETS))) {
1536
+ throw new TypeError(`[fullscreen-engine/config] unknown mode/preset: ${String(cfg.mode)}`);
1537
+ }
1538
+ if (cfg.core !== undefined && !isPlainObject(cfg.core)) {
1539
+ throw new TypeError(`[fullscreen-engine/config] ${label}.core must be a plain object`);
1540
+ }
1541
+ if (cfg.adapters !== undefined) {
1542
+ if (!isPlainObject(cfg.adapters)) {
1543
+ throw new TypeError(`[fullscreen-engine/config] ${label}.adapters must be a plain object`);
1544
+ }
1545
+ for (const key of ['electron', 'puppeteer', 'ssr']) {
1546
+ if (cfg.adapters[key] !== undefined && !isPlainObject(cfg.adapters[key])) {
1547
+ throw new TypeError(
1548
+ `[fullscreen-engine/config] adapter "${key}" options must be a plain object`
1549
+ );
1550
+ }
1551
+ }
1552
+ }
1553
+ }
1554
+
1555
+ const PRESETS = Object.freeze({
1556
+ default: Object.freeze({
1557
+ core: Object.freeze({}),
1558
+ adapters: Object.freeze({
1559
+ electron: Object.freeze({}),
1560
+ puppeteer: Object.freeze({}),
1561
+ ssr: Object.freeze({ enabled: true })
1562
+ })
1563
+ }),
1564
+ kiosk: Object.freeze({
1565
+ core: Object.freeze({
1566
+ fallback: 'css',
1567
+ lockBody: true,
1568
+ autoRelock: true,
1569
+ preventExit: true,
1570
+ suppressOnManualExit: true
1571
+ }),
1572
+ adapters: Object.freeze({
1573
+ electron: Object.freeze({ autoRelock: true, preventExit: true }),
1574
+ puppeteer: Object.freeze({ preventExit: true }),
1575
+ ssr: Object.freeze({ enabled: true })
1576
+ })
1577
+ }),
1578
+ presentation: Object.freeze({
1579
+ core: Object.freeze({
1580
+ fallback: 'css',
1581
+ lockBody: true,
1582
+ trigger: 'manual',
1583
+ autoExit: false,
1584
+ suppressOnManualExit: true,
1585
+ autoRelock: false,
1586
+ preventExit: false
1587
+ }),
1588
+ adapters: Object.freeze({
1589
+ electron: Object.freeze({ autoRelock: false, preventExit: false }),
1590
+ puppeteer: Object.freeze({ restoreState: 'normal', preventExit: false }),
1591
+ ssr: Object.freeze({ enabled: true })
1592
+ })
1593
+ }),
1594
+ strict: Object.freeze({
1595
+ core: Object.freeze({
1596
+ fallback: 'css',
1597
+ lockBody: true,
1598
+ autoRelock: Object.freeze({ enabled: true, delay: 50, maxRetries: 5 }),
1599
+ preventExit: true,
1600
+ suppressOnManualExit: true
1601
+ }),
1602
+ adapters: Object.freeze({
1603
+ electron: Object.freeze({
1604
+ autoRelock: Object.freeze({ enabled: true, delay: 50, maxRetries: 5 }),
1605
+ preventExit: true
1606
+ }),
1607
+ puppeteer: Object.freeze({ preventExit: true }),
1608
+ ssr: Object.freeze({ enabled: false })
1609
+ })
1610
+ })
1611
+ });
1612
+
1613
+ function defineConfig(config = {}) {
1614
+ validateConfigShape(config, 'config');
1615
+ return config;
1616
+ }
1617
+
1618
+ function resolveConfig(config = {}, overrides = {}) {
1619
+ validateConfigShape(config, 'config');
1620
+ validateConfigShape(overrides, 'overrides');
1621
+
1622
+ const mode = overrides.mode ?? config.mode ?? 'default';
1623
+ const preset = PRESETS[mode];
1624
+
1625
+ const cfgAdapters = config.adapters || {};
1626
+ const ovAdapters = overrides.adapters || {};
1627
+
1628
+ return {
1629
+ mode,
1630
+ core: mergeSection(mergeSection(preset.core, config.core), overrides.core),
1631
+ adapters: {
1632
+ electron: mergeSection(
1633
+ mergeSection(preset.adapters.electron, cfgAdapters.electron),
1634
+ ovAdapters.electron
1635
+ ),
1636
+ puppeteer: mergeSection(
1637
+ mergeSection(preset.adapters.puppeteer, cfgAdapters.puppeteer),
1638
+ ovAdapters.puppeteer
1639
+ ),
1640
+ ssr: mergeSection(
1641
+ mergeSection(preset.adapters.ssr, cfgAdapters.ssr),
1642
+ ovAdapters.ssr
1643
+ )
1644
+ }
1645
+ };
1646
+ }
1647
+
1648
+ function initFullscreen(config = {}, context = {}) {
1649
+ if (!isPlainObject(context)) {
1650
+ throw new TypeError('[fullscreen-engine/config] context must be a plain object');
1651
+ }
1652
+
1653
+ const resolved = resolveConfig(config, context.overrides || {});
1654
+ if (context.target !== undefined) {
1655
+ resolved.core.target = context.target;
1656
+ }
1657
+
1658
+ if (context.bridge !== undefined || context.adapter === 'electron') {
1659
+ const bridge =
1660
+ context.bridge ?? (typeof window !== 'undefined' ? window.fsNative : null);
1661
+ return createElectronFullscreen(bridge, resolved.adapters.electron);
1662
+ }
1663
+
1664
+ if (context.page !== undefined || context.adapter === 'puppeteer') {
1665
+ return createPuppeteerFullscreen(context.page, resolved.adapters.puppeteer);
1666
+ }
1667
+
1668
+ if (context.adapter === 'ssr' || (isNode() && !isBrowser() && !isElectron())) {
1669
+ if (resolved.adapters.ssr.enabled === false) {
1670
+ throw new Error('[fullscreen-engine/config] SSR adapter is disabled by config');
1671
+ }
1672
+ return createFullscreen();
1673
+ }
1674
+
1675
+ return createFullscreen$1(resolved.core);
1676
+ }
1677
+
1176
1678
  exports.Emitter = Emitter;
1177
- exports.createFullscreen = createFullscreen;
1679
+ exports.PRESETS = PRESETS;
1680
+ exports.createFullscreen = createFullscreen$1;
1681
+ exports.defineConfig = defineConfig;
1178
1682
  exports.detectEnvironment = detectEnvironment;
1683
+ exports.initFullscreen = initFullscreen;
1179
1684
  exports.isBrowser = isBrowser;
1180
1685
  exports.isElectron = isElectron;
1181
1686
  exports.isNode = isNode;
1687
+ exports.resolveConfig = resolveConfig;
1182
1688
 
1183
1689
  }));
1184
1690
  //# sourceMappingURL=fullscreen.umd.cjs.map