Skip to content

Commit

Permalink
refactor(core): Remove request libraries from cli package (#3803)
Browse files Browse the repository at this point in the history
* ➖ Remove request libraries

* ♻️ Refactor requests and remove unused imports

* ⚡ Fix loaded workflow gets parsed twice

* ⚡ Fix remote workflow is parsed twice as json

* ⚡ Fix workflowData assignment when data is fetched

* ⚡ Fix move workflow request and assignment into try/catch block
  • Loading branch information
brianinoa authored Aug 2, 2022
1 parent 7e578b7 commit 2cab8e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
2 changes: 0 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
39 changes: 17 additions & 22 deletions packages/cli/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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, ...
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -171,7 +164,6 @@ import {
isUserManagementEnabled,
} from './UserManagement/UserManagementHelper';
import { loadPublicApiVersions } from './PublicApi';
import { SharedWorkflow } from './databases/entities/SharedWorkflow';

require('body-parser-xml')(bodyParser);

Expand Down Expand Up @@ -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<IWorkflowResponse>(req.query.url as string);
workflowData = data;
} catch (error) {
throw new ResponseHelper.ResponseError(
`The URL does not point to valid JSON file!`,
Expand Down Expand Up @@ -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<AxiosRequestConfig>);

// 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
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
Expand All @@ -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;

Expand Down Expand Up @@ -1940,7 +1935,7 @@ class App {
filterToAdd = { [key]: value };
}

Object.assign(findOptions.where, filterToAdd);
Object.assign(findOptions.where!, filterToAdd);
});

const rangeQuery: string[] = [];
Expand All @@ -1966,7 +1961,7 @@ class App {
}

if (rangeQuery.length) {
Object.assign(findOptions.where, {
Object.assign(findOptions.where!, {
id: Raw(() => rangeQuery.join(' and '), rangeQueryParams),
});
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2cab8e7

Please sign in to comment.