UNPKG

trade360-nodejs-sdk

Version:
106 lines 4.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TransportMessageHeaders = void 0; class TransportMessageHeaders { static createFromProperties(properties) { if (!properties) { throw new Error('Properties parameter cannot be null or undefined'); } const headers = new TransportMessageHeaders(); headers.messageGuid = this.getRequiredProperty(properties, this.MESSAGE_GUID_KEY); headers.messageType = this.getRequiredProperty(properties, this.MESSAGE_TYPE_KEY); headers.timestampInMs = this.getRequiredProperty(properties, this.TIMESTAMP_IN_MS_KEY); headers.messageSequence = this.getRequiredProperty(properties, this.MESSAGE_SEQUENCE_KEY, false); headers.fixtureId = this.getRequiredProperty(properties, this.FIXTURE_ID_KEY, false); return headers; } static getRequiredProperty(properties, key, required = true) { this.validateKey(key); if (!Object.prototype.hasOwnProperty.call(properties, key)) { return this.handleMissingProperty(key, required); } // Security: Safe property access with additional validation const value = this.safePropertyAccess(properties, key); const stringValue = this.convertToString(value, key); return this.validateFinalValue(stringValue, key, required); } static safePropertyAccess(properties, key) { // Additional security check: Verify property descriptor is safe const descriptor = Object.getOwnPropertyDescriptor(properties, key); if (!descriptor) { return undefined; } // Ensure the property is enumerable and has a simple value (not a getter) if (!descriptor.enumerable || typeof descriptor.get === 'function') { throw new Error(`Header '${key}' has unsafe property descriptor`); } // Safe access: we've validated the key and property descriptor return descriptor.value; } static validateKey(key) { if (!this.ALLOWED_KEYS.has(key)) { throw new Error(`Invalid property key: '${key}'. Only predefined header keys are allowed.`); } } static handleMissingProperty(key, required) { if (required) { throw new Error(`Header '${key}' is missing, null, or empty in message properties object.`); } return ''; } static extractValidValue(value) { // Handle null and undefined - these are valid for optional properties if (value === null || value === undefined) { return value; // Return as-is for proper handling in validation } const isValidType = typeof value === 'string' || Buffer.isBuffer(value) || typeof value === 'number' || typeof value === 'boolean'; if (!isValidType) { return Symbol('invalid'); // Use unique symbol to signal invalid type } return value; } static convertToString(value, key) { // Handle undefined (missing property) if (value === undefined) { return ''; } // Handle null values - these should be treated as missing for validation if (value === null) { return ''; } // Handle invalid type marker if (typeof value === 'symbol') { throw new Error(`Header '${key}' contains invalid data type. Only primitive values are allowed.`); } if (Buffer.isBuffer(value)) { return value.toString('utf8'); } if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') { return String(value); } throw new Error(`Header '${key}' contains invalid data type. Only primitive values are allowed.`); } static validateFinalValue(stringValue, key, required) { if (required && (!stringValue || stringValue.trim() === '')) { throw new Error(`Header '${key}' is missing, null, or empty in message properties object.`); } return stringValue || ''; } } exports.TransportMessageHeaders = TransportMessageHeaders; TransportMessageHeaders.MESSAGE_GUID_KEY = 'MessageGuid'; TransportMessageHeaders.MESSAGE_TYPE_KEY = 'MessageType'; TransportMessageHeaders.TIMESTAMP_IN_MS_KEY = 'timestamp_in_ms'; TransportMessageHeaders.MESSAGE_SEQUENCE_KEY = 'MessageSequence'; TransportMessageHeaders.FIXTURE_ID_KEY = 'FixtureId'; TransportMessageHeaders.ALLOWED_KEYS = new Set([ TransportMessageHeaders.MESSAGE_GUID_KEY, TransportMessageHeaders.MESSAGE_TYPE_KEY, TransportMessageHeaders.TIMESTAMP_IN_MS_KEY, TransportMessageHeaders.MESSAGE_SEQUENCE_KEY, TransportMessageHeaders.FIXTURE_ID_KEY, ]); //# sourceMappingURL=transport-message-headers.js.map