From 7ab4af9b2aa8ff03d01c9ff70410dcf1a20506db Mon Sep 17 00:00:00 2001 From: Roman Dorin Date: Mon, 7 Sep 2020 16:09:20 +0300 Subject: [PATCH 1/2] Fix exception, emit called without await Small eslint fixes --- lib/actions/bulk_cud.js | 4 ++-- lib/actions/createObject.js | 3 ++- lib/actions/lookup.js | 15 ++++++++------- lib/actions/lookupObject.js | 21 ++++++++++---------- lib/actions/lookupObjects.js | 26 +++++++++++++------------ lib/util.js | 6 +++--- package-lock.json | 37 ++++++++++++++++++++++++++++++------ 7 files changed, 71 insertions(+), 41 deletions(-) diff --git a/lib/actions/bulk_cud.js b/lib/actions/bulk_cud.js index 68f0bc6..b45d072 100644 --- a/lib/actions/bulk_cud.js +++ b/lib/actions/bulk_cud.js @@ -1,7 +1,7 @@ const { messages } = require('elasticio-node'); +const { Readable } = require('stream'); const util = require('../util'); -const { Readable } = require('stream'); const MetaLoader = require('../helpers/metaLoader'); const sfConnection = require('../helpers/sfConnection.js'); @@ -49,7 +49,7 @@ exports.process = async function bulkCUD(message, configuration) { timeout = DEFAULT_TIMEOUT; } - let result = await util.downloadAttachment(message.attachments[key].url); + const result = await util.downloadAttachment(message.attachments[key].url); const csvStream = new Readable(); csvStream.push(result); diff --git a/lib/actions/createObject.js b/lib/actions/createObject.js index 869fa10..6fc93e3 100644 --- a/lib/actions/createObject.js +++ b/lib/actions/createObject.js @@ -36,8 +36,9 @@ exports.process = async function createObject(message, configuration) { return messages.newMessageWithBody(message.body); } catch (err) { - return this.emit('error', err); + await this.emit('error', err); } + return undefined; }; exports.getMetaModel = function getMetaModel(cfg, cb) { diff --git a/lib/actions/lookup.js b/lib/actions/lookup.js index e4271e1..fb90c3f 100644 --- a/lib/actions/lookup.js +++ b/lib/actions/lookup.js @@ -57,26 +57,27 @@ module.exports.process = async function processAction(message, configuration) { .on('record', (record) => { res.push(record); }) - .on('end', () => { + .on('end', async () => { if (!res.length) { - this.emit('data', messages.newMessageWithBody({})); + await this.emit('data', messages.newMessageWithBody({})); } if (batchSize > 0) { while (res.length) { const result = res.splice(0, batchSize); this.logger.debug('emitting batch %j', { result }); - this.emit('data', messages.newMessageWithBody({ result })); + // eslint-disable-next-line no-await-in-loop + await this.emit('data', messages.newMessageWithBody({ result })); } } else { - res.forEach((record) => { + res.forEach(async (record) => { this.logger.debug('emitting record %j', record); - this.emit('data', messages.newMessageWithBody(record)); + await this.emit('data', messages.newMessageWithBody(record)); }); } }) - .on('error', (err) => { + .on('error', async (err) => { this.logger.error(err); - this.emit('error', err); + await this.emit('error', err); }) .execute({ autoFetch: true, maxFetch }); }; diff --git a/lib/actions/lookupObject.js b/lib/actions/lookupObject.js index b0b79e0..fcfa97d 100644 --- a/lib/actions/lookupObject.js +++ b/lib/actions/lookupObject.js @@ -58,12 +58,12 @@ module.exports.process = async function processAction(message, configuration) { if (!lookupValue) { if (allowCriteriaToBeOmitted) { - this.emit('data', messages.newMessageWithBody({})); + await this.emit('data', messages.newMessageWithBody({})); return; } const err = new Error('No unique criteria provided'); this.logger.error(err); - this.emit('error', err); + await this.emit('error', err); return; } @@ -80,7 +80,8 @@ module.exports.process = async function processAction(message, configuration) { this.logger.info('Cached response found!'); const response = lookupCache.getResponse(queryKey); // eslint-disable-next-line consistent-return - return this.emit('data', messages.newMessageWithBody(response)); + await this.emit('data', messages.newMessageWithBody(response)); + return; } // the query for the object and all its linked parent objects @@ -105,7 +106,7 @@ module.exports.process = async function processAction(message, configuration) { }, query); query = query.where(condition) - .on('error', (err) => { + .on('error', async (err) => { this.logger.error(err); if (err.message === 'Binary fields cannot be selected in join queries') { // eslint-disable-next-line no-param-reassign @@ -113,7 +114,7 @@ module.exports.process = async function processAction(message, configuration) { + 'Instead of querying objects with binary fields as linked objects ' + '(such as children Attachments), try querying them directly.'; } - this.emit('error', err); + await this.emit('error', err); }); query.on('record', (record) => { @@ -124,11 +125,11 @@ module.exports.process = async function processAction(message, configuration) { if (res.length === 0) { if (allowZeroResults) { lookupCache.addRequestResponsePair(queryKey, {}); - this.emit('data', messages.newMessageWithBody({})); + await this.emit('data', messages.newMessageWithBody({})); } else { const err = new Error('No objects found'); this.logger.error(err); - this.emit('error', err); + await this.emit('error', err); } } else if (res.length === 1) { try { @@ -143,15 +144,15 @@ module.exports.process = async function processAction(message, configuration) { lookupCache.addRequestResponsePair(queryKey, res[0]); this.logger.debug('emitting record %j', outputMessage); - this.emit('data', outputMessage); + await this.emit('data', outputMessage); } catch (err) { this.logger.error(err); - this.emit('error', err); + await this.emit('error', err); } } else { const err = new Error('More than one object found'); this.logger.error(err); - this.emit('error', err); + await this.emit('error', err); } }); diff --git a/lib/actions/lookupObjects.js b/lib/actions/lookupObjects.js index aa1fb7a..49a8b06 100644 --- a/lib/actions/lookupObjects.js +++ b/lib/actions/lookupObjects.js @@ -229,17 +229,19 @@ module.exports.process = async function processAction(message, configuration) { const responseArray = lookupCache.getResponse(queryKey); if (configuration.outputMethod === 'emitIndividually') { if (responseArray.length === 0) { - return this.emit('data', messages.newMessageWithBody({ results: [] })); + await this.emit('data', messages.newMessageWithBody({ results: [] })); + return; } for (let i = 0; i < responseArray.length; i += 1) { // eslint-disable-next-line no-await-in-loop await this.emit('data', messages.newMessageWithBody({ results: [responseArray[i]] })); } - return true; + return; } - return this.emit('data', messages.newMessageWithBody({ results: responseArray })); + await this.emit('data', messages.newMessageWithBody({ results: responseArray })); + return; } const records = []; @@ -250,20 +252,20 @@ module.exports.process = async function processAction(message, configuration) { .offset(offset) .limit(limit) .scanAll(configuration.includeDeleted) - .on('error', (err) => { + .on('error', async (err) => { const errExt = _.cloneDeep(err); errExt.message = `Salesforce returned an error: ${err.message}`; - this.emit('error', errExt); + await this.emit('error', errExt); }); if (configuration.outputMethod === 'emitIndividually') { - query.on('record', (record) => { + query.on('record', async (record) => { records.push(record); - this.emit('data', messages.newMessageWithBody({ results: [record] })); + await this.emit('data', messages.newMessageWithBody({ results: [record] })); }) - .on('end', () => { + .on('end', async () => { if (!query.totalFetched) { - this.emit('data', messages.newMessageWithBody({ results: [] })); + await this.emit('data', messages.newMessageWithBody({ results: [] })); } lookupCache.addRequestResponsePair(queryKey, records); @@ -273,13 +275,13 @@ module.exports.process = async function processAction(message, configuration) { query.on('record', (record) => { records.push(record); }) - .on('end', () => { + .on('end', async () => { lookupCache.addRequestResponsePair(queryKey, records); - this.emit('data', messages.newMessageWithBody({ results: records })); + await this.emit('data', messages.newMessageWithBody({ results: records })); this.logger.info(`Got ${query.totalFetched} records`); }); } this.logger.info('Sending the request to SalesForce...'); - return query.execute({ autoFetch: true, maxFetch: limit }); + query.execute({ autoFetch: true, maxFetch: limit }); }; diff --git a/lib/util.js b/lib/util.js index 01d2bfe..08259d9 100644 --- a/lib/util.js +++ b/lib/util.js @@ -15,13 +15,13 @@ function addRetryCountInterceptorToAxios(ax) { return Promise.reject(err); } config.currentRetryCount += 1; - return new Promise((resolve) => setTimeout(() => resolve(ax(config)), config.delay)); + return new Promise(resolve => setTimeout(() => resolve(ax(config)), config.delay)); }); } -module.exports.base64Encode = (value) => Buffer.from(value).toString('base64'); -module.exports.base64Decode = (value) => Buffer.from(value, 'base64').toString('utf-8'); +module.exports.base64Encode = value => Buffer.from(value).toString('base64'); +module.exports.base64Decode = value => Buffer.from(value, 'base64').toString('utf-8'); module.exports.createSignedUrl = async () => client.resources.storage.createSignedUrl(); module.exports.uploadAttachment = async (url, payload) => { const ax = axios.create(); diff --git a/package-lock.json b/package-lock.json index e79e640..4236475 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "salesforce-component", - "version": "1.2.3", + "version": "1.3.5", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -244,6 +244,14 @@ "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.8.0.tgz", "integrity": "sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==" }, + "axios": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz", + "integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==", + "requires": { + "follow-redirects": "1.5.10" + } + }, "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", @@ -604,8 +612,7 @@ "dotenv": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.0.0.tgz", - "integrity": "sha512-30xVGqjLjiUOArT4+M5q9sYdvuR4riM6yK9wMcas9Vbp6zZa+ocC9dp6QoftuhTPhFAiLK/0C5Ni2nou/Bk8lg==", - "dev": true + "integrity": "sha512-30xVGqjLjiUOArT4+M5q9sYdvuR4riM6yK9wMcas9Vbp6zZa+ocC9dp6QoftuhTPhFAiLK/0C5Ni2nou/Bk8lg==" }, "dtrace-provider": { "version": "0.8.8", @@ -1133,6 +1140,24 @@ "integrity": "sha512-a1hQMktqW9Nmqr5aktAux3JMNqaucxGcjtjWnZLHX7yyPCmlSV3M54nGYbqT8K+0GhF3NBgmJCc3ma+WOgX8Jg==", "dev": true }, + "follow-redirects": { + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", + "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", + "requires": { + "debug": "=3.1.0" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + } + } + }, "forEachAsync": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/forEachAsync/-/forEachAsync-2.2.1.tgz", @@ -1617,9 +1642,9 @@ } }, "lodash": { - "version": "4.17.11", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", - "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" + "version": "4.17.13", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.13.tgz", + "integrity": "sha512-vm3/XWXfWtRua0FkUyEHBZy8kCPjErNBT9fJx8Zvs+U6zjqPbTUOpkaoum3O5uiA8sm+yNMHXfYkTUHFoMxFNA==" }, "lodash.get": { "version": "4.4.2", From 9ee76b39e5ac94f7dac2f5893e4bc2afd360680e Mon Sep 17 00:00:00 2001 From: Roman Dorin Date: Tue, 8 Sep 2020 17:23:24 +0300 Subject: [PATCH 2/2] Replace forEach with for of --- lib/actions/lookup.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/actions/lookup.js b/lib/actions/lookup.js index fb90c3f..50e2585 100644 --- a/lib/actions/lookup.js +++ b/lib/actions/lookup.js @@ -69,10 +69,12 @@ module.exports.process = async function processAction(message, configuration) { await this.emit('data', messages.newMessageWithBody({ result })); } } else { - res.forEach(async (record) => { + // eslint-disable-next-line no-restricted-syntax + for (const record of res) { this.logger.debug('emitting record %j', record); + // eslint-disable-next-line no-await-in-loop await this.emit('data', messages.newMessageWithBody(record)); - }); + } } }) .on('error', async (err) => {