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