Skip to content

Commit fbc9952

Browse files
Merge pull request #27 from elasticio/sprint-32-update-sailor-261
update sailor to 2.6.1
2 parents b9f9806 + c2d4213 commit fbc9952

File tree

13 files changed

+1067
-1150
lines changed

13 files changed

+1067
-1150
lines changed

.eslintrc.js

Lines changed: 6 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,162 +1,7 @@
1-
'use strict';
2-
3-
const OFF = 'off';
4-
const ERROR = 'error';
5-
const WARN = 'warn';
6-
const ALWAYS = 'always';
7-
const NEVER = 'never';
8-
91
module.exports = {
10-
'parserOptions': {
11-
ecmaVersion: 2017
12-
},
13-
'env': {
14-
es6: true,
15-
node: true,
16-
mocha: true
17-
},
18-
'plugins': [
19-
'mocha'
20-
],
21-
'extends': 'airbnb-base',
22-
'rules': {
23-
'indent': [
24-
ERROR,
25-
4,
26-
{
27-
SwitchCase: 1
28-
}
29-
],
30-
'linebreak-style': ERROR,
31-
'quotes': [
32-
WARN,
33-
'single',
34-
{
35-
avoidEscape: true,
36-
allowTemplateLiterals: true
37-
}
38-
],
39-
'semi': [
40-
ERROR,
41-
ALWAYS
42-
],
43-
'func-names': ERROR,
44-
'no-empty': ERROR,
45-
'no-empty-function': ERROR,
46-
'brace-style': [
47-
ERROR,
48-
'1tbs',
49-
{ allowSingleLine: true }
50-
],
51-
'no-multiple-empty-lines': ERROR,
52-
'no-multi-spaces': ERROR,
53-
'one-var': [
54-
ERROR,
55-
NEVER
56-
],
57-
'quote-props': [
58-
WARN,
59-
'consistent-as-needed'
60-
],
61-
'key-spacing': ERROR,
62-
'space-unary-ops': [
63-
ERROR,
64-
{
65-
words: true,
66-
nonwords: false
67-
}
68-
],
69-
'no-spaced-func': ERROR,
70-
'space-before-function-paren': [
71-
ERROR,
72-
{
73-
anonymous: ALWAYS,
74-
named: NEVER
75-
}
76-
],
77-
'arrow-body-style': [
78-
WARN,
79-
'as-needed'
80-
],
81-
'array-bracket-spacing': ERROR,
82-
'space-in-parens': ERROR,
83-
'comma-dangle': ERROR,
84-
'no-trailing-spaces': ERROR,
85-
'yoda': ERROR,
86-
'max-len': [
87-
ERROR,
88-
120
89-
],
90-
'camelcase': [
91-
ERROR,
92-
{
93-
properties: 'never'
94-
}
95-
],
96-
'new-cap': [
97-
WARN,
98-
{
99-
capIsNewExceptions: ['Q']
100-
}
101-
],
102-
'comma-style': ERROR,
103-
'curly': ERROR,
104-
'object-curly-spacing': [
105-
WARN,
106-
ALWAYS
107-
],
108-
'object-curly-newline': [
109-
ERROR,
110-
{
111-
ObjectExpression: {
112-
minProperties: 1
113-
},
114-
ObjectPattern: {
115-
multiline: true,
116-
minProperties: 5
117-
}
118-
}
119-
],
120-
'object-property-newline': ERROR,
121-
'template-curly-spacing': ERROR,
122-
'dot-notation': ERROR,
123-
'dot-location': [
124-
ERROR,
125-
'property'
126-
],
127-
'func-style': [
128-
ERROR,
129-
'declaration',
130-
{
131-
allowArrowFunctions: true
132-
}
133-
],
134-
'eol-last': ERROR,
135-
'space-infix-ops': ERROR,
136-
'keyword-spacing': ERROR,
137-
'space-before-blocks': ERROR,
138-
'no-invalid-this': ERROR,
139-
'consistent-this': ERROR,
140-
'no-this-before-super': ERROR,
141-
'no-unreachable': ERROR,
142-
'no-sparse-arrays': ERROR,
143-
'array-callback-return': ERROR,
144-
'strict': [
145-
WARN,
146-
'global'
147-
],
148-
'eqeqeq': ERROR,
149-
'no-use-before-define': WARN,
150-
'no-undef': ERROR,
151-
'no-unused-vars': WARN,
152-
'no-mixed-spaces-and-tabs': ERROR,
153-
'operator-linebreak': [
154-
ERROR,
155-
'before'
156-
],
157-
'no-console': [
158-
OFF
159-
],
160-
"mocha/no-exclusive-tests": "error"
161-
}
162-
};
2+
'extends': 'airbnb-base',
3+
'env': {
4+
'mocha': true,
5+
'node': true,
6+
}
7+
};

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 1.2.0 (January 30, 2020)
2+
3+
* Update sailor version to 2.6.1
4+
* Refactor console.log to built in sailor logger
5+
* Change build type to docker
6+
17
## 1.1.1 (September 25, 2019)
28

39
* Upload attachments with component commons library

component.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"title": "XML",
33
"description": "Component to work with XML files",
4+
"buildType": "docker",
45
"actions": {
56
"xmlToJson": {
67
"title": "XML to JSON",

lib/actions/attachmentToJson.js

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-await-in-loop */
12
const sizeof = require('object-sizeof');
23
const { AttachmentProcessor } = require('@elastic.io/component-commons-library');
34
const { messages } = require('elasticio-node');
@@ -6,69 +7,70 @@ const xml2Json = require('../xml2Json.js');
67

78
const MAX_FILE_SIZE = 5242880; // 5 MiB
89

9-
function checkFileName(fileName, pattern) {
10-
if (fileName === undefined) {
11-
return false;
12-
}
10+
function checkFileName(self, fileName, pattern) {
11+
if (fileName === undefined) {
12+
return false;
13+
}
1314

14-
if (!pattern.test(fileName)) {
15-
console.log('%s does not match pattern: \'%s\'', fileName, pattern);
16-
return false;
17-
}
15+
if (!pattern.test(fileName)) {
16+
self.logger.debug('%s does not match pattern: \'%s\'', fileName, pattern);
17+
return false;
18+
}
1819

19-
if (fileName.split('.').pop() !== 'xml') {
20-
console.log('%s is not .xml file: ', fileName);
21-
return false;
22-
}
20+
if (fileName.split('.').pop() !== 'xml') {
21+
self.logger.debug('%s is not .xml file: ', fileName);
22+
return false;
23+
}
2324

24-
return true;
25+
return true;
2526
}
2627

2728
module.exports.process = async function processAction(msg, cfg) {
28-
const { attachments } = msg;
29-
const pattern = new RegExp(cfg !== undefined ? cfg.pattern || '(.xml)' : '(.xml)');
30-
let foundXML = false;
29+
const self = this;
30+
const { attachments } = msg;
31+
const pattern = new RegExp(cfg !== undefined ? cfg.pattern || '(.xml)' : '(.xml)');
32+
let foundXML = false;
3133

32-
console.log('Attachment to XML started');
33-
console.log('Found %s attachments', Object.keys(attachments || {}).length);
34+
self.logger.info('Attachment to XML started');
35+
self.logger.info('Found %s attachments', Object.keys(attachments || {}).length);
3436

35-
// eslint-disable-next-line no-restricted-syntax
36-
for (const key of Object.keys(attachments)) {
37-
const attachment = attachments[key];
38-
const fileName = key;
39-
let fileSize = attachment.size; // get file size based attachment object may not be define or be accurate
40-
console.log('Processing attachment=%s', fileName);
37+
// eslint-disable-next-line no-restricted-syntax
38+
for (const key of Object.keys(attachments)) {
39+
const attachment = attachments[key];
40+
const fileName = key;
41+
// get file size based attachment object may not be define or be accurate
42+
let fileSize = attachment.size;
43+
self.logger.info('Processing attachment=%s', fileName);
4144

42-
if (checkFileName(fileName, pattern)) {
43-
if (fileSize === undefined || fileSize < MAX_FILE_SIZE) {
44-
// eslint-disable-next-line no-await-in-loop
45-
const response = await new AttachmentProcessor().getAttachment(attachment.url, 'arraybuffer');
45+
if (checkFileName(self, fileName, pattern)) {
46+
if (fileSize === undefined || fileSize < MAX_FILE_SIZE) {
47+
// eslint-disable-next-line no-await-in-loop
48+
const response = await new AttachmentProcessor().getAttachment(attachment.url, 'arraybuffer');
4649

47-
if (response.status >= 400) {
48-
throw new Error(`Error in making request to ${attachment.url}
50+
if (response.status >= 400) {
51+
throw new Error(`Error in making request to ${attachment.url}
4952
Status code: ${response.status},
5053
Body: ${Buffer.from(response.data, 'binary').toString('base64')}`);
51-
}
54+
}
5255

53-
const responseBodyString = Buffer.from(response.data, 'binary').toString('utf-8');
54-
fileSize = sizeof(responseBodyString);
56+
const responseBodyString = Buffer.from(response.data, 'binary').toString('utf-8');
57+
fileSize = sizeof(responseBodyString);
5558

56-
if (fileSize < MAX_FILE_SIZE) {
57-
// eslint-disable-next-line no-await-in-loop
58-
const returnMsg = await xml2Json.process(responseBodyString);
59-
this.emit('data', messages.newMessageWithBody(returnMsg.body));
60-
foundXML = true;
61-
} else {
62-
throw new Error(`Attachment ${key} is too large to be processed my XML component.`
63-
+ ` File limit is: ${MAX_FILE_SIZE} byte, file given was: ${fileSize} byte.`);
64-
}
65-
} else {
66-
throw new Error(`Attachment ${key} is too large to be processed my XML component.`
67-
+ ` File limit is: ${MAX_FILE_SIZE} byte, file given was: ${fileSize} byte.`);
68-
}
59+
if (fileSize < MAX_FILE_SIZE) {
60+
const returnMsg = await xml2Json.process(this, responseBodyString);
61+
foundXML = true;
62+
await self.emit('data', messages.newMessageWithBody(returnMsg.body));
63+
} else {
64+
throw new Error(`Attachment ${key} is too large to be processed my XML component.`
65+
+ ` File limit is: ${MAX_FILE_SIZE} byte, file given was: ${fileSize} byte.`);
6966
}
67+
} else {
68+
throw new Error(`Attachment ${key} is too large to be processed my XML component.`
69+
+ ` File limit is: ${MAX_FILE_SIZE} byte, file given was: ${fileSize} byte.`);
70+
}
7071
}
71-
if (!foundXML) {
72-
console.log(`No XML files that match the pattern found with in attachments. Pattern: ${pattern}`);
73-
}
72+
}
73+
if (!foundXML) {
74+
self.logger.info(`No XML files that match the pattern found with in attachments. Pattern: ${pattern}`);
75+
}
7476
};

lib/actions/jsonToXml.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ const propNameIsInvalid = (key) => /^\d/.test(key);
2020
* @param {String} key
2121
*/
2222
function validateJsonPropNames(value, key) {
23-
if (propNameIsInvalid(key)) {
24-
const message = 'Can\'t create XML element from prop that starts with digit.'
25-
+ 'See XML naming rules https://www.w3schools.com/xml/xml_elements.asp';
26-
throw new Error(`${ERROR}: ${key}. ${message}`);
27-
}
23+
if (propNameIsInvalid(key)) {
24+
const message = 'Can\'t create XML element from prop that starts with digit.'
25+
+ 'See XML naming rules https://www.w3schools.com/xml/xml_elements.asp';
26+
throw new Error(`${ERROR}: ${key}. ${message}`);
27+
}
2828

29-
if (!_.isPlainObject(value)) {
30-
return;
31-
}
29+
if (!_.isPlainObject(value)) {
30+
return;
31+
}
3232

33-
Object.keys(value).forEach((prop) => {
34-
validateJsonPropNames(value[prop], prop);
35-
});
33+
Object.keys(value).forEach((prop) => {
34+
validateJsonPropNames(value[prop], prop);
35+
});
3636
}
3737

3838
/**
@@ -42,28 +42,28 @@ function validateJsonPropNames(value, key) {
4242
* @param cfg configuration that is account information and configuration field values
4343
*/
4444
function processAction(msg, cfg) {
45-
console.log('Action started, message=%j cfg=%j', msg, cfg);
46-
const options = {
47-
trim: false,
48-
normalize: false,
49-
explicitArray: false,
50-
normalizeTags: false,
51-
attrkey: '_attr',
52-
tagNameProcessors: [
53-
(name) => name.replace(':', '-'),
54-
],
55-
};
56-
const builder = new xml2js.Builder(options);
45+
this.logger.debug('Action started, message=%j cfg=%j', msg, cfg);
46+
const options = {
47+
trim: false,
48+
normalize: false,
49+
explicitArray: false,
50+
normalizeTags: false,
51+
attrkey: '_attr',
52+
tagNameProcessors: [
53+
(name) => name.replace(':', '-'),
54+
],
55+
};
56+
const builder = new xml2js.Builder(options);
5757

58-
const jsonToTransform = msg.body;
58+
const jsonToTransform = msg.body;
5959

60-
validateJsonPropNames(jsonToTransform);
60+
validateJsonPropNames(jsonToTransform);
6161

62-
const result = builder.buildObject(jsonToTransform);
63-
console.log('Successfully converted body to XML result=%s', result);
64-
return eioUtils.newMessageWithBody({
65-
xmlString: result,
66-
});
62+
const result = builder.buildObject(jsonToTransform);
63+
this.logger.debug('Successfully converted body to XML result=%s', result);
64+
return eioUtils.newMessageWithBody({
65+
xmlString: result,
66+
});
6767
}
6868

6969
module.exports.process = processAction;

0 commit comments

Comments
 (0)