@xiboplayer/utils 0.5.0 → 0.5.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xiboplayer/utils",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "Shared utilities for Xibo Player packages",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -11,7 +11,7 @@
11
11
  "./config": "./src/config.js"
12
12
  },
13
13
  "dependencies": {
14
- "@xiboplayer/crypto": "0.5.0"
14
+ "@xiboplayer/crypto": "0.5.2"
15
15
  },
16
16
  "devDependencies": {
17
17
  "vitest": "^2.0.0"
package/src/config.js CHANGED
@@ -58,46 +58,44 @@ export class Config {
58
58
 
59
59
  // Try to load from localStorage
60
60
  const json = localStorage.getItem(STORAGE_KEY);
61
+ let config = {};
61
62
 
62
63
  if (json) {
63
64
  try {
64
- const config = JSON.parse(json);
65
-
66
- // CRITICAL: Hardware key must persist
67
- if (!config.hardwareKey || config.hardwareKey.length < 10) {
68
- console.error('[Config] CRITICAL: Invalid/missing hardwareKey in localStorage!');
69
- config.hardwareKey = this.generateStableHardwareKey();
70
- localStorage.setItem(STORAGE_KEY, JSON.stringify(config));
71
- this._backupHardwareKey(config.hardwareKey);
72
- } else {
73
- console.log('[Config] ✓ Loaded existing hardwareKey:', config.hardwareKey);
74
- }
75
-
76
- return config;
65
+ config = JSON.parse(json);
77
66
  } catch (e) {
78
- console.error('[Config] Failed to parse config from localStorage:', e);
79
- // Fall through to create new config
67
+ console.error('[Config] Failed to parse localStorage config:', e);
80
68
  }
81
69
  }
82
70
 
83
- // No config in localStorage - first time setup
84
- console.log('[Config] No config in localStorage - first time setup');
71
+ // ── Single validation gate (same path for fresh + pre-seeded) ──
72
+ let changed = false;
73
+
74
+ if (!config.hardwareKey || config.hardwareKey.length < 10) {
75
+ console.warn('[Config] Missing/invalid hardwareKey — generating');
76
+ config.hardwareKey = this.generateStableHardwareKey();
77
+ this._backupHardwareKey(config.hardwareKey);
78
+ changed = true;
79
+ } else {
80
+ console.log('[Config] ✓ Loaded existing hardwareKey:', config.hardwareKey);
81
+ }
82
+
83
+ if (!config.xmrChannel) {
84
+ console.warn('[Config] Missing xmrChannel — generating');
85
+ config.xmrChannel = this.generateXmrChannel();
86
+ changed = true;
87
+ }
85
88
 
86
- const newConfig = {
87
- cmsUrl: '',
88
- cmsKey: '',
89
- displayName: '',
90
- hardwareKey: this.generateStableHardwareKey(),
91
- xmrChannel: this.generateXmrChannel()
92
- };
89
+ // Ensure optional fields have defaults
90
+ config.cmsUrl = config.cmsUrl || '';
91
+ config.cmsKey = config.cmsKey || '';
92
+ config.displayName = config.displayName || '';
93
93
 
94
- // Save immediately
95
- localStorage.setItem(STORAGE_KEY, JSON.stringify(newConfig));
96
- this._backupHardwareKey(newConfig.hardwareKey);
97
- console.log('[Config] ✓ Saved new config to localStorage');
98
- console.log('[Config] Hardware key will persist across reloads:', newConfig.hardwareKey);
94
+ if (changed) {
95
+ localStorage.setItem(STORAGE_KEY, JSON.stringify(config));
96
+ }
99
97
 
100
- return newConfig;
98
+ return config;
101
99
  }
102
100
 
103
101
  /**
@@ -320,7 +318,14 @@ export class Config {
320
318
  }
321
319
  return this.data.hardwareKey;
322
320
  }
323
- get xmrChannel() { return this.data.xmrChannel; }
321
+ get xmrChannel() {
322
+ if (!this.data.xmrChannel) {
323
+ console.warn('[Config] xmrChannel missing at access time — generating');
324
+ this.data.xmrChannel = this.generateXmrChannel();
325
+ this.save();
326
+ }
327
+ return this.data.xmrChannel;
328
+ }
324
329
  get xmrPubKey() { return this.data.xmrPubKey || ''; }
325
330
  get xmrPrivKey() { return this.data.xmrPrivKey || ''; }
326
331
 
@@ -444,7 +444,8 @@ describe('Config', () => {
444
444
  config = new Config();
445
445
 
446
446
  expect(config.isConfigured()).toBe(false);
447
- expect(config.cmsUrl).toBeNull();
447
+ // null values are normalized to empty strings by the validation gate
448
+ expect(config.cmsUrl).toBe('');
448
449
  });
449
450
 
450
451
  it('should handle very long strings', () => {