@xiboplayer/utils 0.2.0 → 0.3.1
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/README.md +21 -35
- package/package.json +1 -1
- package/src/index.js +2 -0
- package/src/logger.js +8 -7
- package/src/logger.test.js +2 -2
package/README.md
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
# @xiboplayer/utils
|
|
1
|
+
# @xiboplayer/utils
|
|
2
2
|
|
|
3
|
-
**Shared utilities for all
|
|
3
|
+
**Shared utilities for all XiboPlayer SDK packages.**
|
|
4
4
|
|
|
5
5
|
## Overview
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Foundation utilities used across the SDK:
|
|
8
8
|
|
|
9
|
-
- **Logger**
|
|
10
|
-
- **EventEmitter**
|
|
11
|
-
- **
|
|
12
|
-
- **
|
|
9
|
+
- **Logger** — structured logging with configurable levels (`DEBUG`, `INFO`, `WARNING`, `ERROR`) and per-module tags
|
|
10
|
+
- **EventEmitter** — lightweight pub/sub event system
|
|
11
|
+
- **fetchWithRetry** — HTTP fetch with exponential backoff, jitter, and configurable retries
|
|
12
|
+
- **CMS REST client** — JSON API client with ETag caching
|
|
13
|
+
- **Config** — hardware key management and IndexedDB-backed configuration
|
|
13
14
|
|
|
14
15
|
## Installation
|
|
15
16
|
|
|
@@ -20,42 +21,27 @@ npm install @xiboplayer/utils
|
|
|
20
21
|
## Usage
|
|
21
22
|
|
|
22
23
|
```javascript
|
|
23
|
-
import {
|
|
24
|
+
import { createLogger, EventEmitter, fetchWithRetry } from '@xiboplayer/utils';
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
log.
|
|
26
|
+
const log = createLogger('my-module');
|
|
27
|
+
log.info('Starting...');
|
|
28
|
+
log.debug('Detailed info');
|
|
28
29
|
|
|
29
|
-
// EventEmitter
|
|
30
30
|
const emitter = new EventEmitter();
|
|
31
31
|
emitter.on('event', (data) => console.log(data));
|
|
32
|
-
emitter.emit('event', { foo: 'bar' });
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
## API Reference
|
|
36
|
-
|
|
37
|
-
### Logger
|
|
38
|
-
|
|
39
|
-
```javascript
|
|
40
|
-
logger.debug(message, data)
|
|
41
|
-
logger.info(message, data)
|
|
42
|
-
logger.warn(message, data)
|
|
43
|
-
logger.error(message, data)
|
|
44
|
-
```
|
|
45
32
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
```javascript
|
|
49
|
-
emitter.on(event, callback)
|
|
50
|
-
emitter.once(event, callback)
|
|
51
|
-
emitter.off(event, callback)
|
|
52
|
-
emitter.emit(event, data)
|
|
33
|
+
const response = await fetchWithRetry(url, { retries: 3 });
|
|
53
34
|
```
|
|
54
35
|
|
|
55
|
-
##
|
|
36
|
+
## Exports
|
|
56
37
|
|
|
57
|
-
|
|
38
|
+
| Export | Description |
|
|
39
|
+
|--------|-------------|
|
|
40
|
+
| `createLogger(tag)` | Create a tagged logger instance |
|
|
41
|
+
| `EventEmitter` | Pub/sub event emitter |
|
|
42
|
+
| `fetchWithRetry(url, opts)` | Fetch with exponential backoff |
|
|
43
|
+
| `Config` | Hardware key and IndexedDB config management |
|
|
58
44
|
|
|
59
45
|
---
|
|
60
46
|
|
|
61
|
-
**
|
|
47
|
+
**Part of the [XiboPlayer SDK](https://github.com/xibo-players/xiboplayer)** | [MCP Server](https://github.com/xibo-players/xiboplayer/tree/main/mcp-server) for AI-assisted development
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
// @xiboplayer/utils - Shared utilities
|
|
2
|
+
import pkg from '../package.json' with { type: 'json' };
|
|
3
|
+
export const VERSION = pkg.version;
|
|
2
4
|
export { createLogger, setLogLevel, getLogLevel, isDebug, applyCmsLogLevel, mapCmsLogLevel, registerLogSink, unregisterLogSink, LOG_LEVELS } from './logger.js';
|
|
3
5
|
export { EventEmitter } from './event-emitter.js';
|
|
4
6
|
export { config } from './config.js';
|
package/src/logger.js
CHANGED
|
@@ -7,7 +7,10 @@
|
|
|
7
7
|
* 1. URL param ?logLevel=DEBUG
|
|
8
8
|
* 2. localStorage xibo_log_level
|
|
9
9
|
* 3. CMS setting via RegisterDisplay (call applyCmsLogLevel())
|
|
10
|
-
* 4. Default:
|
|
10
|
+
* 4. Default: WARNING (production-safe)
|
|
11
|
+
*
|
|
12
|
+
* For development, pass ?logLevel=DEBUG in the URL.
|
|
13
|
+
* Electron's --dev flag does this automatically.
|
|
11
14
|
*
|
|
12
15
|
* Loggers created without an explicit level are REACTIVE — they follow
|
|
13
16
|
* the global level at call time, so setLogLevel() affects all of them.
|
|
@@ -93,7 +96,7 @@ class Logger {
|
|
|
93
96
|
|
|
94
97
|
// Global log level configuration
|
|
95
98
|
const globalConfig = {
|
|
96
|
-
level: LOG_LEVELS.
|
|
99
|
+
level: LOG_LEVELS.WARNING, // Default: WARNING (production-safe)
|
|
97
100
|
|
|
98
101
|
setGlobalLevel(level) {
|
|
99
102
|
if (typeof level === 'string') {
|
|
@@ -114,6 +117,8 @@ const globalConfig = {
|
|
|
114
117
|
let hasLocalOverride = false;
|
|
115
118
|
|
|
116
119
|
// Set global level from environment or localStorage
|
|
120
|
+
// Default: WARNING (production-safe). Use ?logLevel=DEBUG for development,
|
|
121
|
+
// or let the CMS override via applyCmsLogLevel().
|
|
117
122
|
if (typeof window !== 'undefined') {
|
|
118
123
|
const urlParams = new URLSearchParams(window.location.search);
|
|
119
124
|
const urlLevel = urlParams.get('logLevel');
|
|
@@ -125,12 +130,8 @@ if (typeof window !== 'undefined') {
|
|
|
125
130
|
} else if (storageLevel) {
|
|
126
131
|
globalConfig.setGlobalLevel(storageLevel);
|
|
127
132
|
hasLocalOverride = true;
|
|
128
|
-
} else if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
|
|
129
|
-
// Development mode - debug logging
|
|
130
|
-
globalConfig.setGlobalLevel('DEBUG');
|
|
131
133
|
} else {
|
|
132
|
-
|
|
133
|
-
globalConfig.setGlobalLevel('INFO');
|
|
134
|
+
globalConfig.setGlobalLevel('WARNING');
|
|
134
135
|
}
|
|
135
136
|
}
|
|
136
137
|
|
package/src/logger.test.js
CHANGED
|
@@ -33,13 +33,13 @@ describe('Logger', () => {
|
|
|
33
33
|
});
|
|
34
34
|
|
|
35
35
|
describe('Logger Creation', () => {
|
|
36
|
-
it('should create logger with default
|
|
36
|
+
it('should create logger with default WARNING level', () => {
|
|
37
37
|
const logger = createLogger('TestModule');
|
|
38
38
|
|
|
39
39
|
expect(logger.name).toBe('TestModule');
|
|
40
40
|
// When no explicit level is given, logger follows global level (useGlobal=true)
|
|
41
41
|
// so logger.level is undefined — check getEffectiveLevel() instead
|
|
42
|
-
expect(logger.getEffectiveLevel()).toBeLessThanOrEqual(LOG_LEVELS.
|
|
42
|
+
expect(logger.getEffectiveLevel()).toBeLessThanOrEqual(LOG_LEVELS.WARNING);
|
|
43
43
|
});
|
|
44
44
|
|
|
45
45
|
it('should create logger with custom level', () => {
|