@xiboplayer/utils 0.4.8 → 0.5.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.
- package/package.json +2 -2
- package/src/config.js +7 -7
- package/src/config.test.js +23 -23
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xiboplayer/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
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.
|
|
14
|
+
"@xiboplayer/crypto": "0.5.0"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
17
|
"vitest": "^2.0.0"
|
package/src/config.js
CHANGED
|
@@ -12,7 +12,7 @@ const HW_DB_VERSION = 1;
|
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Check for environment variable config (highest priority).
|
|
15
|
-
* Env vars:
|
|
15
|
+
* Env vars: CMS_URL, CMS_KEY, DISPLAY_NAME, HARDWARE_KEY, XMR_CHANNEL
|
|
16
16
|
* Returns config object if any env vars are set, null otherwise.
|
|
17
17
|
*/
|
|
18
18
|
function loadFromEnv() {
|
|
@@ -20,7 +20,7 @@ function loadFromEnv() {
|
|
|
20
20
|
const env = typeof process !== 'undefined' && process.env ? process.env : {};
|
|
21
21
|
|
|
22
22
|
const envConfig = {
|
|
23
|
-
|
|
23
|
+
cmsUrl: env.CMS_URL || '',
|
|
24
24
|
cmsKey: env.CMS_KEY || '',
|
|
25
25
|
displayName: env.DISPLAY_NAME || '',
|
|
26
26
|
hardwareKey: env.HARDWARE_KEY || '',
|
|
@@ -53,7 +53,7 @@ export class Config {
|
|
|
53
53
|
|
|
54
54
|
// Priority 2: localStorage (browser)
|
|
55
55
|
if (typeof localStorage === 'undefined') {
|
|
56
|
-
return {
|
|
56
|
+
return { cmsUrl: '', cmsKey: '', displayName: '', hardwareKey: '', xmrChannel: '' };
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
// Try to load from localStorage
|
|
@@ -84,7 +84,7 @@ export class Config {
|
|
|
84
84
|
console.log('[Config] No config in localStorage - first time setup');
|
|
85
85
|
|
|
86
86
|
const newConfig = {
|
|
87
|
-
|
|
87
|
+
cmsUrl: '',
|
|
88
88
|
cmsKey: '',
|
|
89
89
|
displayName: '',
|
|
90
90
|
hardwareKey: this.generateStableHardwareKey(),
|
|
@@ -188,7 +188,7 @@ export class Config {
|
|
|
188
188
|
}
|
|
189
189
|
|
|
190
190
|
isConfigured() {
|
|
191
|
-
return !!(this.data.
|
|
191
|
+
return !!(this.data.cmsUrl && this.data.cmsKey && this.data.displayName);
|
|
192
192
|
}
|
|
193
193
|
|
|
194
194
|
generateStableHardwareKey() {
|
|
@@ -302,8 +302,8 @@ export class Config {
|
|
|
302
302
|
return result.substring(0, 32);
|
|
303
303
|
}
|
|
304
304
|
|
|
305
|
-
get
|
|
306
|
-
set
|
|
305
|
+
get cmsUrl() { return this.data.cmsUrl; }
|
|
306
|
+
set cmsUrl(val) { this.data.cmsUrl = val; this.save(); }
|
|
307
307
|
|
|
308
308
|
get cmsKey() { return this.data.cmsKey; }
|
|
309
309
|
set cmsKey(val) { this.data.cmsKey = val; this.save(); }
|
package/src/config.test.js
CHANGED
|
@@ -76,7 +76,7 @@ describe('Config', () => {
|
|
|
76
76
|
config = new Config();
|
|
77
77
|
|
|
78
78
|
expect(config.data).toBeDefined();
|
|
79
|
-
expect(config.data.
|
|
79
|
+
expect(config.data.cmsUrl).toBe('');
|
|
80
80
|
expect(config.data.cmsKey).toBe('');
|
|
81
81
|
expect(config.data.displayName).toBe('');
|
|
82
82
|
expect(config.data.hardwareKey).toMatch(/^pwa-/);
|
|
@@ -100,7 +100,7 @@ describe('Config', () => {
|
|
|
100
100
|
|
|
101
101
|
it('should load existing config from localStorage', () => {
|
|
102
102
|
const existingConfig = {
|
|
103
|
-
|
|
103
|
+
cmsUrl: 'https://test.cms.com',
|
|
104
104
|
cmsKey: 'test-key',
|
|
105
105
|
displayName: 'Test Display',
|
|
106
106
|
hardwareKey: 'pwa-existinghardwarekey1234567',
|
|
@@ -116,7 +116,7 @@ describe('Config', () => {
|
|
|
116
116
|
|
|
117
117
|
it('should regenerate hardware key if invalid in stored config', () => {
|
|
118
118
|
const invalidConfig = {
|
|
119
|
-
|
|
119
|
+
cmsUrl: 'https://test.cms.com',
|
|
120
120
|
cmsKey: 'test-key',
|
|
121
121
|
displayName: 'Test Display',
|
|
122
122
|
hardwareKey: 'short', // Invalid: too short
|
|
@@ -298,20 +298,20 @@ describe('Config', () => {
|
|
|
298
298
|
config = new Config();
|
|
299
299
|
});
|
|
300
300
|
|
|
301
|
-
it('should get/set
|
|
302
|
-
expect(config.
|
|
301
|
+
it('should get/set cmsUrl', () => {
|
|
302
|
+
expect(config.cmsUrl).toBe('');
|
|
303
303
|
|
|
304
|
-
config.
|
|
304
|
+
config.cmsUrl = 'https://new.cms.com';
|
|
305
305
|
|
|
306
|
-
expect(config.
|
|
307
|
-
expect(config.data.
|
|
306
|
+
expect(config.cmsUrl).toBe('https://new.cms.com');
|
|
307
|
+
expect(config.data.cmsUrl).toBe('https://new.cms.com');
|
|
308
308
|
});
|
|
309
309
|
|
|
310
|
-
it('should save to localStorage when
|
|
311
|
-
config.
|
|
310
|
+
it('should save to localStorage when cmsUrl set', () => {
|
|
311
|
+
config.cmsUrl = 'https://test.com';
|
|
312
312
|
|
|
313
313
|
const stored = JSON.parse(mockLocalStorage.getItem('xibo_config'));
|
|
314
|
-
expect(stored.
|
|
314
|
+
expect(stored.cmsUrl).toBe('https://test.com');
|
|
315
315
|
});
|
|
316
316
|
|
|
317
317
|
it('should get/set cmsKey', () => {
|
|
@@ -351,8 +351,8 @@ describe('Config', () => {
|
|
|
351
351
|
expect(config.isConfigured()).toBe(false);
|
|
352
352
|
});
|
|
353
353
|
|
|
354
|
-
it('should return false when only
|
|
355
|
-
config.
|
|
354
|
+
it('should return false when only cmsUrl set', () => {
|
|
355
|
+
config.cmsUrl = 'https://test.com';
|
|
356
356
|
|
|
357
357
|
expect(config.isConfigured()).toBe(false);
|
|
358
358
|
});
|
|
@@ -370,7 +370,7 @@ describe('Config', () => {
|
|
|
370
370
|
});
|
|
371
371
|
|
|
372
372
|
it('should return true when all required fields set', () => {
|
|
373
|
-
config.
|
|
373
|
+
config.cmsUrl = 'https://test.com';
|
|
374
374
|
config.cmsKey = 'test-key';
|
|
375
375
|
config.displayName = 'Test Display';
|
|
376
376
|
|
|
@@ -384,21 +384,21 @@ describe('Config', () => {
|
|
|
384
384
|
});
|
|
385
385
|
|
|
386
386
|
it('should save current config to localStorage', () => {
|
|
387
|
-
config.data.
|
|
387
|
+
config.data.cmsUrl = 'https://manual.com';
|
|
388
388
|
config.data.cmsKey = 'manual-key';
|
|
389
389
|
|
|
390
390
|
config.save();
|
|
391
391
|
|
|
392
392
|
const stored = JSON.parse(mockLocalStorage.getItem('xibo_config'));
|
|
393
|
-
expect(stored.
|
|
393
|
+
expect(stored.cmsUrl).toBe('https://manual.com');
|
|
394
394
|
expect(stored.cmsKey).toBe('manual-key');
|
|
395
395
|
});
|
|
396
396
|
|
|
397
397
|
it('should auto-save when setters used', () => {
|
|
398
|
-
config.
|
|
398
|
+
config.cmsUrl = 'https://auto.com';
|
|
399
399
|
|
|
400
400
|
const stored = JSON.parse(mockLocalStorage.getItem('xibo_config'));
|
|
401
|
-
expect(stored.
|
|
401
|
+
expect(stored.cmsUrl).toBe('https://auto.com');
|
|
402
402
|
});
|
|
403
403
|
});
|
|
404
404
|
|
|
@@ -420,7 +420,7 @@ describe('Config', () => {
|
|
|
420
420
|
describe('Edge Cases', () => {
|
|
421
421
|
it('should handle missing hardwareKey in loaded config', () => {
|
|
422
422
|
mockLocalStorage.setItem('xibo_config', JSON.stringify({
|
|
423
|
-
|
|
423
|
+
cmsUrl: 'https://test.com',
|
|
424
424
|
cmsKey: 'test-key',
|
|
425
425
|
displayName: 'Test'
|
|
426
426
|
// hardwareKey missing
|
|
@@ -434,7 +434,7 @@ describe('Config', () => {
|
|
|
434
434
|
|
|
435
435
|
it('should handle null values in config', () => {
|
|
436
436
|
mockLocalStorage.setItem('xibo_config', JSON.stringify({
|
|
437
|
-
|
|
437
|
+
cmsUrl: null,
|
|
438
438
|
cmsKey: null,
|
|
439
439
|
displayName: null,
|
|
440
440
|
hardwareKey: 'pwa-1234567812344567890123456789',
|
|
@@ -444,7 +444,7 @@ describe('Config', () => {
|
|
|
444
444
|
config = new Config();
|
|
445
445
|
|
|
446
446
|
expect(config.isConfigured()).toBe(false);
|
|
447
|
-
expect(config.
|
|
447
|
+
expect(config.cmsUrl).toBeNull();
|
|
448
448
|
});
|
|
449
449
|
|
|
450
450
|
it('should handle very long strings', () => {
|
|
@@ -478,13 +478,13 @@ describe('Config', () => {
|
|
|
478
478
|
|
|
479
479
|
it('should persist configuration changes', () => {
|
|
480
480
|
const config1 = new Config();
|
|
481
|
-
config1.
|
|
481
|
+
config1.cmsUrl = 'https://persist.com';
|
|
482
482
|
config1.cmsKey = 'persist-key';
|
|
483
483
|
config1.displayName = 'Persist Display';
|
|
484
484
|
|
|
485
485
|
const config2 = new Config();
|
|
486
486
|
|
|
487
|
-
expect(config2.
|
|
487
|
+
expect(config2.cmsUrl).toBe('https://persist.com');
|
|
488
488
|
expect(config2.cmsKey).toBe('persist-key');
|
|
489
489
|
expect(config2.displayName).toBe('Persist Display');
|
|
490
490
|
});
|