Skip to content

Commit 8701988

Browse files
user-agent (#13)
1 parent e187c12 commit 8701988

File tree

9 files changed

+646
-6308
lines changed

9 files changed

+646
-6308
lines changed

.circleci/config.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ commands:
7272
jobs:
7373
test:
7474
docker:
75-
- image: circleci/node:14-stretch
75+
- image: circleci/node:16-stretch
7676
steps:
7777
- checkout
7878
- node/install:
7979
node-version: << pipeline.parameters.node-version >>
8080
- run:
8181
name: Audit Dependencies
82-
command: npm audit --audit-level=high
82+
command: npm audit --production --audit-level=high
8383
- node/install-packages:
8484
cache-path: ./node_modules
8585
override-ci-command: npm install
@@ -88,7 +88,7 @@ jobs:
8888
command: npm test
8989
build:
9090
docker:
91-
- image: circleci/node:14-stretch
91+
- image: circleci/node:16-stretch
9292
user: root
9393
steps:
9494
- checkout

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 3.1.5 (August 26, 2022)
2+
3+
* Update Sailor version to 2.6.29
4+
* Get rid of vulnerabilities in dependencies
5+
* Update component-commons-library version to 3.0.2
6+
17
## 3.1.4 (May 06, 2022)
28
* Fix memory leak on `Emit Batch` behavior for `Read CSV attachment` action
39

component.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"title": "CSV",
33
"description": "A comma-separated values (CSV) file stores tabular data (numbers and text) in plain-text form",
44
"docsUrl": "https://github.com/elasticio/csv-component",
5-
"version": "3.1.4",
5+
"version": "3.1.5",
66
"actions": {
77
"read_action": {
88
"main": "./lib/actions/read.js",
@@ -85,4 +85,4 @@
8585
"dynamicMetadata": true
8686
}
8787
}
88-
}
88+
}

lib/actions/read.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const { messages } = require('elasticio-node')
66
const stream = require('stream')
77
const util = require('util')
88
const papa = require('papaparse')
9+
const { getUserAgent } = require('../util');
910

1011
const pipeline = util.promisify(stream.pipeline);
11-
const attachmentProcessor = new AttachmentProcessor()
1212

1313
// transform array to obj, for example:
1414
// ['aa', 'bb', 'cc'] => {column0: 'aa', column1: 'bb', column2: 'cc'}
@@ -72,6 +72,7 @@ async function readCSV(msg, cfg) {
7272
let dataStream
7373
const parseStream = papa.parse(papa.NODE_STREAM_INPUT, parseOptions)
7474

75+
const attachmentProcessor = new AttachmentProcessor(getUserAgent(), msg.id);
7576
try {
7677
dataStream = await attachmentProcessor.getAttachment(body.url, 'stream')
7778
this.logger.info('File received, trying to parse CSV')

lib/actions/write.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@ no-param-reassign */
44
const { AttachmentProcessor } = require('@elastic.io/component-commons-library')
55
const { messages } = require('elasticio-node')
66
const papa = require('papaparse')
7+
const { Readable } = require('stream');
8+
const { getUserAgent } = require('../util');
9+
10+
const formStream = (data) => {
11+
const stream = new Readable();
12+
stream.push(data);
13+
stream.push(null);
14+
return stream;
15+
}
716

817
const TIMEOUT_BETWEEN_EVENTS = process.env.TIMEOUT_BETWEEN_EVENTS || 10000; // 10s;
918

1019
const rawData = []
1120
let timeout
1221

13-
async function proceedData(data, cfg) {
14-
let csvString
22+
async function proceedData(data, msg, cfg) {
23+
let csvString;
1524
const delimiter = cfg.separator ? cfg.separator : ','
1625

1726
const unparseOptions = {
@@ -29,30 +38,31 @@ async function proceedData(data, cfg) {
2938
return filtered
3039
})
3140
return result
32-
})
41+
});
3342
csvString = papa.unparse({
3443
fields,
3544
data: orderedData
36-
}, unparseOptions)
45+
}, unparseOptions);
3746
} else {
38-
csvString = papa.unparse(data, unparseOptions)
47+
csvString = papa.unparse(data, unparseOptions);
3948
}
49+
const getAttachment = () => formStream(csvString);
4050

4151
if (!cfg.uploadToAttachment) {
4252
await this.emit('data', messages.newMessageWithBody({ csvString }))
4353
this.logger.info(`Complete, memory used: ${process.memoryUsage().heapUsed / 1024 / 1024} Mb`)
4454
return
4555
}
4656

47-
const attachmentProcessor = new AttachmentProcessor()
48-
let uploadResult
57+
const attachmentProcessor = new AttachmentProcessor(getUserAgent(), msg.id)
58+
let attachmentId;
4959
try {
50-
uploadResult = await attachmentProcessor.uploadAttachment(csvString, 'stream')
60+
attachmentId = await attachmentProcessor.uploadAttachment(getAttachment)
5161
} catch (err) {
5262
this.logger.error(`Upload attachment failed: ${err}`)
5363
this.emit('error', `Upload attachment failed: ${err}`)
5464
}
55-
const attachmentUrl = `${uploadResult.config.url}${uploadResult.data.objectId}?storage_type=maester`;
65+
const attachmentUrl = attachmentProcessor.getMaesterAttachmentUrlById(attachmentId)
5666
const body = {
5767
attachmentUrl,
5868
type: '.csv',
@@ -91,13 +101,13 @@ async function writeCSV(msg, cfg) {
91101
// if not array - create array from all fn calls and send data to proceedData
92102
if (Array.isArray(body.items)) {
93103
this.logger.info('input metadata is array. Proceed with data ')
94-
await proceedData.call(this, body.items, cfg)
104+
await proceedData.call(this, body.items, msg, cfg)
95105
} else {
96106
rawData.push(body.items)
97107
if (timeout) clearTimeout(timeout)
98108
timeout = setTimeout(() => {
99109
this.logger.info(`input metadata is object. Array creation (wait up to ${TIMEOUT_BETWEEN_EVENTS}ms for more records)`)
100-
proceedData.call(this, rawData, cfg)
110+
proceedData.call(this, rawData, msg, cfg)
101111
}, TIMEOUT_BETWEEN_EVENTS)
102112
}
103113
}

lib/util.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
const papa = require('papaparse');
2+
const packageJson = require('../package.json');
3+
const compJson = require('../component.json');
4+
5+
function getUserAgent() {
6+
const { name: compName } = packageJson;
7+
const { version: compVersion } = compJson;
8+
const libVersion = packageJson.dependencies['@elastic.io/component-commons-library'];
9+
return `${compName}/${compVersion} component-commons-library/${libVersion}`;
10+
}
211

312
function getItemsProperties(cfg) {
413
const properties = {};
@@ -65,5 +74,6 @@ function getOut(cfg) {
6574
return out;
6675
}
6776

77+
module.exports.getUserAgent = getUserAgent;
6878
module.exports.getItemsProperties = getItemsProperties;
6979
module.exports.getOut = getOut;

0 commit comments

Comments
 (0)