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