From 2cab8e7779c82a7a9f83d7ef0a08f51b5810a5ce Mon Sep 17 00:00:00 2001 From: brianinoa <54530642+brianinoa@users.noreply.github.com> Date: Tue, 2 Aug 2022 16:43:31 +0200 Subject: [PATCH] refactor(core): Remove request libraries from cli package (#3803) * :heavy_minus_sign: Remove request libraries * :recycle: Refactor requests and remove unused imports * :zap: Fix loaded workflow gets parsed twice * :zap: Fix remote workflow is parsed twice as json * :zap: Fix workflowData assignment when data is fetched * :zap: Fix move workflow request and assignment into try/catch block --- packages/cli/package.json | 2 -- packages/cli/src/Server.ts | 39 +++++++++++++++++--------------------- 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/packages/cli/package.json b/packages/cli/package.json index 206c91c83e..d5ffbeddfc 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -81,7 +81,6 @@ "@types/parseurl": "^1.3.1", "@types/passport-jwt": "^3.0.6", "@types/psl": "^1.1.0", - "@types/request-promise-native": "~1.0.15", "@types/superagent": "4.1.13", "@types/supertest": "^2.0.11", "@types/uuid": "^8.3.2", @@ -159,7 +158,6 @@ "pg": "^8.3.0", "prom-client": "^13.1.0", "psl": "^1.8.0", - "request-promise-native": "^1.0.7", "shelljs": "^0.8.5", "sqlite3": "^5.0.2", "sse-channel": "^3.1.1", diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index f0830e9280..9a18f5d132 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -37,7 +37,6 @@ import _, { cloneDeep } from 'lodash'; import { dirname as pathDirname, join as pathJoin, resolve as pathResolve } from 'path'; import { FindManyOptions, - getConnection, getConnectionManager, In, IsNull, @@ -50,10 +49,8 @@ import cookieParser from 'cookie-parser'; import history from 'connect-history-api-fallback'; import os from 'os'; // eslint-disable-next-line import/no-extraneous-dependencies -import clientOAuth2 from 'client-oauth2'; import clientOAuth1, { RequestOptions } from 'oauth-1.0a'; -import csrf from 'csrf'; -import requestPromise, { OptionsWithUrl } from 'request-promise-native'; +import axios, { AxiosRequestConfig, AxiosPromise } from 'axios'; import { createHmac, randomBytes } from 'crypto'; // IMPORTANT! Do not switch to anther bcrypt library unless really necessary and // tested with all possible systems like Windows, Alpine on ARM, FreeBSD, ... @@ -88,11 +85,9 @@ import jwks from 'jwks-rsa'; // @ts-ignore import timezones from 'google-timezones-json'; import parseUrl from 'parseurl'; -import querystring from 'querystring'; import promClient, { Registry } from 'prom-client'; import * as Queue from './Queue'; import { - LoadNodesAndCredentials, ActiveExecutions, ActiveWorkflowRunner, CredentialsHelper, @@ -150,8 +145,6 @@ import { userManagementRouter } from './UserManagement'; import { resolveJwt } from './UserManagement/auth/jwt'; import { User } from './databases/entities/User'; import type { - AuthenticatedRequest, - CredentialRequest, ExecutionRequest, NodeParameterOptionsRequest, OAuthRequest, @@ -171,7 +164,6 @@ import { isUserManagementEnabled, } from './UserManagement/UserManagementHelper'; import { loadPublicApiVersions } from './PublicApi'; -import { SharedWorkflow } from './databases/entities/SharedWorkflow'; require('body-parser-xml')(bodyParser); @@ -793,11 +785,10 @@ class App { 400, ); } - const data = await requestPromise.get(req.query.url as string); - let workflowData: IWorkflowResponse | undefined; try { - workflowData = JSON.parse(data); + const { data } = await axios.get(req.query.url as string); + workflowData = data; } catch (error) { throw new ResponseHelper.ResponseError( `The URL does not point to valid JSON file!`, @@ -1714,11 +1705,13 @@ class App { // @ts-ignore options.headers = data; - const response = await requestPromise(options); + const { data: response } = await axios.request(options as Partial); // Response comes as x-www-form-urlencoded string so convert it to JSON - const responseJson = querystring.parse(response); + const paramsParser = new URLSearchParams(response); + + const responseJson = Object.fromEntries(paramsParser.entries()); const returnUri = `${_.get(oauthCredentials, 'authUrl')}?oauth_token=${ responseJson.oauth_token @@ -1813,10 +1806,10 @@ class App { timezone, ); - const options: OptionsWithUrl = { + const options: AxiosRequestConfig = { method: 'POST', url: _.get(oauthCredentials, 'accessTokenUrl') as string, - qs: { + params: { oauth_token, oauth_verifier, }, @@ -1825,7 +1818,7 @@ class App { let oauthToken; try { - oauthToken = await requestPromise(options); + oauthToken = await axios.request(options); } catch (error) { LoggerProxy.error('Unable to fetch tokens for OAuth1 callback', { userId: req.user?.id, @@ -1841,7 +1834,9 @@ class App { // Response comes as x-www-form-urlencoded string so convert it to JSON - const oauthTokenJson = querystring.parse(oauthToken); + const paramParser = new URLSearchParams(oauthToken.data); + + const oauthTokenJson = Object.fromEntries(paramParser.entries()); decryptedDataOriginal.oauthTokenData = oauthTokenJson; @@ -1940,7 +1935,7 @@ class App { filterToAdd = { [key]: value }; } - Object.assign(findOptions.where, filterToAdd); + Object.assign(findOptions.where!, filterToAdd); }); const rangeQuery: string[] = []; @@ -1966,7 +1961,7 @@ class App { } if (rangeQuery.length) { - Object.assign(findOptions.where, { + Object.assign(findOptions.where!, { id: Raw(() => rangeQuery.join(' and '), rangeQueryParams), }); } @@ -2288,10 +2283,10 @@ class App { if (req.query.filter) { const { workflowId } = JSON.parse(req.query.filter); if (workflowId && sharedWorkflowIds.includes(workflowId)) { - Object.assign(findOptions.where, { workflowId }); + Object.assign(findOptions.where!, { workflowId }); } } else { - Object.assign(findOptions.where, { workflowId: In(sharedWorkflowIds) }); + Object.assign(findOptions.where!, { workflowId: In(sharedWorkflowIds) }); } const executions = await Db.collections.Execution.find(findOptions);