-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update dependencies * Create new JSON to XML action * Add help links
- Loading branch information
Showing
10 changed files
with
1,841 additions
and
1,619 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,69 @@ | ||
/* eslint no-invalid-this: 0 no-console: 0 */ | ||
const eioUtils = require('elasticio-node').messages; | ||
const { AttachmentProcessor } = require('@elastic.io/component-commons-library'); | ||
const { messages } = require('elasticio-node'); | ||
const xml2js = require('xml2js'); | ||
const _ = require('lodash'); | ||
|
||
const ERROR = 'Prop name is invalid for XML tag'; | ||
const MB_TO_BYTES = 1024 * 1024; | ||
const MAX_FILE_SIZE = process.env.MAX_FILE_SIZE * MB_TO_BYTES || 10 * MB_TO_BYTES; | ||
|
||
/** | ||
* Checks whether property name is valid | ||
* @param {String} key - propName | ||
* @returns {Boolean} - valid prop or not | ||
*/ | ||
const propNameIsInvalid = (key) => /^\d/.test(key); | ||
module.exports.process = async function process(msg, cfg) { | ||
const { input } = msg.body; | ||
const { uploadToAttachment, excludeXmlHeader, headerStandalone } = cfg; | ||
|
||
/** | ||
* Checks whether object contains properties | ||
* that startsWith number | ||
* @see https://github.com/elasticio/xml-component/issues/1 | ||
* @param {Object|Number|String} value | ||
* @param {String} key | ||
*/ | ||
function validateJsonPropNames(value, key) { | ||
if (propNameIsInvalid(key)) { | ||
const message = 'Can\'t create XML element from prop that starts with digit.' | ||
+ 'See XML naming rules https://www.w3schools.com/xml/xml_elements.asp'; | ||
throw new Error(`${ERROR}: ${key}. ${message}`); | ||
} | ||
|
||
if (!_.isPlainObject(value)) { | ||
return; | ||
} | ||
this.logger.info('Message received.'); | ||
|
||
Object.keys(value).forEach((prop) => { | ||
validateJsonPropNames(value[prop], prop); | ||
}); | ||
} | ||
|
||
/** | ||
* This method will be called from elastic.io platform providing following data | ||
* | ||
* @param msg incoming message object that contains ``body`` with payload | ||
* @param cfg configuration that is account information and configuration field values | ||
*/ | ||
function processAction(msg, cfg) { | ||
this.logger.debug('Action started, message=%j cfg=%j', msg, cfg); | ||
const options = { | ||
trim: false, | ||
normalize: false, | ||
explicitArray: false, | ||
normalizeTags: false, | ||
attrkey: '_attr', | ||
tagNameProcessors: [ | ||
(name) => name.replace(':', '-'), | ||
], | ||
explicitRoot: false, | ||
xmldec: { | ||
standalone: headerStandalone, | ||
encoding: 'UTF-8', | ||
}, | ||
headless: excludeXmlHeader, | ||
}; | ||
const builder = new xml2js.Builder(options); | ||
|
||
const jsonToTransform = msg.body; | ||
// Check to make sure that input has at most one key | ||
// https://github.com/Leonidas-from-XIV/node-xml2js/issues/564 | ||
if (!_.isPlainObject(input) || Object.keys(input).length !== 1) { | ||
throw new Error('Input must be an object with exactly one key.'); | ||
} | ||
|
||
validateJsonPropNames(jsonToTransform); | ||
const xmlString = builder.buildObject(input); | ||
|
||
const result = builder.buildObject(jsonToTransform); | ||
this.logger.debug('Successfully converted body to XML result=%s', result); | ||
return eioUtils.newMessageWithBody({ | ||
xmlString: result, | ||
}); | ||
} | ||
if (!uploadToAttachment) { | ||
this.logger.info('Sending XML data in message.'); | ||
await this.emit('data', messages.newMessageWithBody({ | ||
xmlString, | ||
})); | ||
return; | ||
} | ||
|
||
const attachmentSize = Buffer.byteLength(xmlString); | ||
if (attachmentSize > MAX_FILE_SIZE) { | ||
throw new Error(`XML data is ${attachmentSize} bytes, and is too large to upload as an attachment. Max attachment size is ${MAX_FILE_SIZE} bytes`); | ||
} | ||
this.logger.info(`Will create XML attachment of size ${attachmentSize} byte(s)`); | ||
|
||
module.exports.process = processAction; | ||
const attachmentProcessor = new AttachmentProcessor(); | ||
const uploadResult = await attachmentProcessor.uploadAttachment(xmlString); | ||
const attachmentUrl = uploadResult.config.url; | ||
this.logger.info(`Successfully created attachment at ${attachmentUrl}`); | ||
|
||
const outboundMessage = messages.newEmptyMessage(); | ||
outboundMessage.attachments = { | ||
'jsonToXml.xml': { | ||
url: attachmentUrl, | ||
size: attachmentSize, | ||
}, | ||
}; | ||
outboundMessage.body = { | ||
attachmentUrl, | ||
attachmentSize, | ||
}; | ||
await this.emit('data', outboundMessage); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* eslint no-invalid-this: 0 no-console: 0 */ | ||
const eioUtils = require('elasticio-node').messages; | ||
const xml2js = require('xml2js'); | ||
const _ = require('lodash'); | ||
|
||
const ERROR = 'Prop name is invalid for XML tag'; | ||
|
||
/** | ||
* Checks whether property name is valid | ||
* @param {String} key - propName | ||
* @returns {Boolean} - valid prop or not | ||
*/ | ||
const propNameIsInvalid = (key) => /^\d/.test(key); | ||
|
||
/** | ||
* Checks whether object contains properties | ||
* that startsWith number | ||
* @see https://github.com/elasticio/xml-component/issues/1 | ||
* @param {Object|Number|String} value | ||
* @param {String} key | ||
*/ | ||
function validateJsonPropNames(value, key) { | ||
if (propNameIsInvalid(key)) { | ||
const message = 'Can\'t create XML element from prop that starts with digit.' | ||
+ 'See XML naming rules https://www.w3schools.com/xml/xml_elements.asp'; | ||
throw new Error(`${ERROR}: ${key}. ${message}`); | ||
} | ||
|
||
if (!_.isPlainObject(value)) { | ||
return; | ||
} | ||
|
||
Object.keys(value).forEach((prop) => { | ||
validateJsonPropNames(value[prop], prop); | ||
}); | ||
} | ||
|
||
/** | ||
* This method will be called from elastic.io platform providing following data | ||
* | ||
* @param msg incoming message object that contains ``body`` with payload | ||
* @param cfg configuration that is account information and configuration field values | ||
*/ | ||
function processAction(msg, cfg) { | ||
this.logger.debug('Action started, message=%j cfg=%j', msg, cfg); | ||
const options = { | ||
trim: false, | ||
normalize: false, | ||
explicitArray: false, | ||
normalizeTags: false, | ||
attrkey: '_attr', | ||
tagNameProcessors: [ | ||
(name) => name.replace(':', '-'), | ||
], | ||
}; | ||
const builder = new xml2js.Builder(options); | ||
|
||
const jsonToTransform = msg.body; | ||
|
||
validateJsonPropNames(jsonToTransform); | ||
|
||
const result = builder.buildObject(jsonToTransform); | ||
this.logger.debug('Successfully converted body to XML result=%s', result); | ||
return eioUtils.newMessageWithBody({ | ||
xmlString: result, | ||
}); | ||
} | ||
|
||
module.exports.process = processAction; |
File renamed without changes.
Oops, something went wrong.