Skip to content

Main/node6 - DON'T MERGE - ONLY FOR COMMENTS #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["env"]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
coverage/
.DS_Store
.flowconfig
dist
7 changes: 7 additions & 0 deletions babel-inplace.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

DIR=$1
TEMPDIR=$(mktemp -d /tmp/babel-in-place.XXXXXXX)
babel --copy-files --presets=@babel/preset-env,@babel/preset-react "$DIR" --out-dir "$TEMPDIR"
rm -rf "$DIR"
mv "$TEMPDIR" "$DIR"
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { CIVersion } = require('../../enums/CIVersion');
const { V1CredentialsIntelligenceProtocol } = require('./V1CredentialsIntelligenceProtocol');
const { MultistepSSOCredentialsIntelligenceProtocol } = require('./MultistepSSOCredentialsIntelligenceProtocol');
const { V2CredentialsIntelligenceProtocol } = require('./V2CredentialsIntelligenceProtocol');

const { ObjectValues } = require('../../pxutil');
class CredentialsIntelligenceProtocolFactory {
static Create(protocolVersion) {
switch (protocolVersion) {
Expand All @@ -13,7 +13,7 @@ class CredentialsIntelligenceProtocolFactory {
case CIVersion.MULTISTEP_SSO:
return new MultistepSSOCredentialsIntelligenceProtocol();
default:
throw new Error(`Unknown CI protocol version '${protocolVersion}', acceptable versions are ${Object.values(CIVersion)}`);
throw new Error(`Unknown CI protocol version '${protocolVersion}', acceptable versions are ${ObjectValues(CIVersion)}`);
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions lib/pxutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,19 @@ function extractGraphqlOperationName(query, operationName) {
}

function isValidGraphqlOperationType(operationType) {
return Object.values(GraphqlOperationType).includes(operationType);
return ObjectValues(GraphqlOperationType).includes(operationType);
}

function isEmailAddress(str) {
return EMAIL_ADDRESS_REGEX.test(str);
}

// Using this method because Object.values() is only supported
// in later version of NodeJS
function ObjectValues(obj) {
return Object.keys(obj).map(k => obj[k]);
}

module.exports = {
formatHeaders,
filterSensitiveHeaders,
Expand All @@ -343,5 +349,6 @@ module.exports = {
isReqInMonitorMode,
getTokenObject,
getGraphqlData,
isEmailAddress
isEmailAddress,
ObjectValues
};
31 changes: 21 additions & 10 deletions lib/request.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const p = require('agent-phin').unpromisified;
const https = require('https');
const http = require('http');
const httpsKeepAliveAgent = new https.Agent({ keepAlive: true });

const axios = require('axios');

exports.get = (options, config, cb) => {
options.method = 'GET';
Expand All @@ -16,11 +14,24 @@ exports.post = (options, config, cb) => {
return makeRequest(options, config, cb);
};

function makeRequest(options, config, cb) {
if (options.url && options.url.startsWith('https://')) {
options.agent = config.agent || httpsKeepAliveAgent;
} else {
options.agent = new http.Agent();
async function makeRequest(options, config, cb) {
try {
const response = await axios.request(options);
cb(null, convertResponse(response));
} catch (e) {
if (e.response) {
cb(null, convertResponse(e.response));
} else {
cb(e, null);
}
}
p(options, cb);
}

function convertResponse(response) {
return {
...response,
statusCode: response.status,
// Check about data type.
body: response.data && (typeof response.data === 'object' ? JSON.stringify(response.data) : response.data.toString())
};
}
Loading