@xiboplayer/utils
Shared utilities for all XiboPlayer SDK packages.
Overview
Foundation utilities used across the SDK:
- Logger -- structured logging with configurable levels (DEBUG, INFO, WARNING, ERROR, NONE) and per-module tags
- Log sinks -- pluggable log destinations (console, LogReporter for CMS submission)
- EventEmitter -- lightweight pub/sub event system
- fetchWithRetry -- HTTP fetch with exponential backoff, jitter, and configurable retries
- Config -- hardware key management, IndexedDB-backed configuration, CMS ID computation
- CMS REST API client -- 77-method JSON API client with ETag caching and JWT auth
- PLAYER_API -- configurable base path for media/widget/dependency URLs
Installation
npm install @xiboplayer/utils
Usage
Logger
import { createLogger, setLogLevel, applyCmsLogLevel, registerLogSink } from '@xiboplayer/utils';
const log = createLogger('my-module');
log.info('Starting...');
log.debug('Detailed info', { key: 'value' });
log.warn('Something unexpected');
log.error('Critical failure', error);
setLogLevel('DEBUG');
applyCmsLogLevel('audit');
registerLogSink({
log(level, tag, ...args) {
cmsReporter.log(level, args.join(' '), tag);
}
});
EventEmitter
import { EventEmitter } from '@xiboplayer/utils';
class MyClass extends EventEmitter {
doSomething() {
this.emit('done', { result: 42 });
}
}
const obj = new MyClass();
obj.on('done', (data) => console.log(data));
obj.once('done', (data) => console.log('First time only'));
obj.off('done', handler);
fetchWithRetry
import { fetchWithRetry } from '@xiboplayer/utils';
const response = await fetchWithRetry(url, {
retries: 3,
baseDelay: 2000,
headers: { 'Content-Type': 'application/json' },
method: 'POST',
body: JSON.stringify(data),
});
Config
import { config, computeCmsId } from '@xiboplayer/utils';
const cmsUrl = config.data.cmsUrl;
const hardwareKey = config.data.hardwareKey;
const cmsId = computeCmsId('https://cms.example.com', 'display-key');
CMS REST API client
import { CmsApiClient } from '@xiboplayer/utils';
const api = new CmsApiClient({
baseUrl: 'https://cms.example.com',
clientId: 'oauth-client-id',
clientSecret: 'oauth-client-secret',
});
const displays = await api.getDisplays();
const layouts = await api.getLayouts();
await api.authorizeDisplay(displayId);
PLAYER_API
import { PLAYER_API, setPlayerApi } from '@xiboplayer/utils';
console.log(PLAYER_API);
setPlayerApi('/custom/player/path');
Exports
createLogger(tag) | Create a tagged logger instance |
setLogLevel(level) | Set global log level |
getLogLevel() | Get current log level |
isDebug() | Check if DEBUG level is active |
applyCmsLogLevel(cmsLevel) | Map CMS log level to SDK level |
registerLogSink(sink) | Add custom log destination |
unregisterLogSink(sink) | Remove log destination |
LOG_LEVELS | Level constants: DEBUG, INFO, WARNING, ERROR, NONE |
EventEmitter | Pub/sub event emitter class |
fetchWithRetry(url, opts) | Fetch with exponential backoff |
config | Global config instance (localStorage/IndexedDB) |
extractPwaConfig(config) | Extract PWA-relevant keys from shell config |
computeCmsId(url, key) | FNV hash for CMS+display namespacing |
SHELL_ONLY_KEYS | Config keys not forwarded to PWA |
CmsApiClient | CMS REST API client (77 methods) |
CmsApiError | API error class with status code |
PLAYER_API | Media/widget base path (configurable) |
setPlayerApi(base) | Override PLAYER_API at runtime |
VERSION | Package version from package.json |
Dependencies
No external runtime dependencies.
xiboplayer.org · Part of the XiboPlayer SDK