tibber-api
Version:
Node.js module for connecting to Tibber API and extract data from your connected homes, including realtime data from Tibber Pulse.
53 lines • 2.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HeaderManager = void 0;
const Version_1 = require("../../Version");
class HeaderManager {
/**
* Constructor
* Create an instance of TibberBase class
* @param {IConfig} config Config object
* @see IConfig
*/
constructor(config) {
// Clone the config object to avoid changing the original object
this._config = JSON.parse(JSON.stringify(config));
}
/**
* Gets the User-Agent from apiEndpoint and ensures it is not changed once set.
*/
get userAgent() {
if (HeaderManager._userAgent === null) {
HeaderManager._userAgent = `${this.sanitizeUserAgent(this._config.apiEndpoint.userAgent)} ${HeaderManager.DEFAULT_USER_AGENT}`.trim();
}
return HeaderManager._userAgent;
}
/**
* Sanitize User-Agent string.
* - Remove all characters that are not allowed in User-Agent.
* - Limit the length to 255 characters.
* @param {string} userAgent User-Agent string to sanitize.
* @returns {string} Sanitized User-Agent string.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent}
* @see {@link https://tools.ietf.org/html/rfc7231#section-5.5.3}
* @see {@link https://tools.ietf.org/html/rfc7230#section-3.2.6}
* @see {@link https://tools.ietf.org/html/rfc7230#section-5.5}
* */
sanitizeUserAgent(userAgent) {
if (!userAgent) {
return '';
}
// Regex to match valid characters (printable ASCII excluding control characters)
const validCharsRegex = /[^\x20-\x7E]+/g;
// Remove invalid characters
let sanitized = userAgent.replace(validCharsRegex, '');
// Normalize excessive spaces
sanitized = sanitized.replace(/\s+/g, ' ').trim();
return sanitized.length > HeaderManager.USER_AGENT_MAX_LENGTH ? sanitized.substring(0, HeaderManager.USER_AGENT_MAX_LENGTH) : sanitized;
}
}
exports.HeaderManager = HeaderManager;
HeaderManager.DEFAULT_USER_AGENT = `bisand/tibber-api/${Version_1.version}`;
HeaderManager.USER_AGENT_MAX_LENGTH = 255 - HeaderManager.DEFAULT_USER_AGENT.length - 1;
HeaderManager._userAgent = null;
//# sourceMappingURL=HeaderManager.js.map