Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
cb = args.find(arg => typeof arg === 'function')
if (verb) opts.method = verb.toUpperCase()
if (uri && !opts.uri && !opts.url) opts.url = uri
const context = {
correlationId: correlator.getId(),
startTime: new Date(),
url: joinURL(lodash.get(opts, 'baseUrl', ''), opts.url),
method: opts.method,
description: opts.description,
service: opts.service
}
// Set headers and optional x-ray trace headers
lodash.set(opts, `headers.${CORRELATION_HEADER}`, context.correlationId)
if (clsSegment) {
const subSegment = opts.subSegment || new AWSXRay.Segment('_request_nbc', null, clsSegment.trace_id)
opts.headers['X-Amzn-Trace-Id'] = 'Root=' + clsSegment.trace_id + ';Parent=' + subSegment.id + ';Sampled=1'
}
opts.headers['Content-Type'] = opts.headers['Content-Type'] || 'application/json'
// Set up post response and error handling method
const transform = opts.transform || false
const handleError = opts.baseClientErrorHandler || 'new'
opts.transform = undefined
opts.baseClientErrorHandler = undefined
// start request
requestLogger.logRequestStart(context)
const call = method(opts, (err, response, body) => {
if (cb) cb(err, response, body)
if (err) {
reject(err)
const optionalPort = proxiedUrl.port ? ':' + proxiedUrl.port : ''
const finalUrl = proxiedUrl.protocol + '//' + proxiedUrl.hostname + optionalPort + proxiedUrl.pathname
const context = {
correlationId: opts.correlationId,
startTime: new Date(),
url: finalUrl,
method: opts.method,
description: opts.description,
service: opts.service
}
// Set headers and optional x-ray trace headers
_.set(opts, `headers.${CORRELATION_HEADER}`, context.correlationId)
if (clsSegment) {
const subSegment = opts.subSegment || new AWSXRay.Segment('_request_nbc', null, clsSegment.trace_id)
opts.headers['X-Amzn-Trace-Id'] = 'Root=' + clsSegment.trace_id + ';Parent=' + subSegment.id + ';Sampled=1'
}
opts.headers['Content-Type'] = opts.headers['Content-Type'] || 'application/json'
// start request
requestLogger.logRequestStart(context)
const startTime = new Date()
method(opts).then(response => {
logger.info('[%s] - %s to %s ended - total time %dms', opts.correlationId, opts.method, opts.url, new Date() - startTime)
resolve(response)
}).catch(err => {
logger.info('[%s] - %s to %s ended - total time %dms', opts.correlationId, opts.method, opts.url, new Date() - startTime)
logger.error(`Calling ${opts.service} threw exception -`, {
service: opts.service,
method: opts.method,
get: () => new AWSXRay.Segment('stub-segment'),
bindEmitter: () => { },
captureAsyncFunc: (name, callback) => callback(new AWSXRay.Segment('stub-subsegment')),
'@global': true
get: () => new AWSXRay.Segment('stub-segment'),
bindEmitter: () => {},
get: () => new AWSXRay.Segment('stub-segment'),
bindEmitter: () => {},
captureAsyncFunc: (name, callback) => callback(new AWSXRay.Segment('stub-subsegment')),
'@global': true
const getHeaders = function getHeaders (args, segmentData) {
const requestContext = getRequestContext(args.correlationId || '') || {}
let headers = {}
headers['Content-Type'] = 'application/json'
headers[CORRELATION_HEADER_NAME] = args.correlationId || ''
headers[FEATURE_FLAGS_HEADER_NAME] = (requestContext.features || []).toString()
if (segmentData.clsSegment) {
const subSegment = segmentData.subSegment || new AWSXRay.Segment('_request', null, segmentData.clsSegment.trace_id)
headers['X-Amzn-Trace-Id'] = 'Root=' + segmentData.clsSegment.trace_id + ';Parent=' + subSegment.id + ';Sampled=1'
}
_.merge(headers, args.headers)
return headers
}
/**
const getHeaders = function getHeaders (args, segmentData, url) {
let headers = {}
headers['Content-Type'] = 'application/json'
headers[CORRELATION_HEADER_NAME] = args.correlationId || ''
if (url) {
const port = (urlParse(url).port) ? ':' + urlParse(url).port : ''
headers['host'] = urlParse(url).hostname + port
}
if (segmentData.clsSegment) {
const subSegment = segmentData.subSegment || new AWSXRay.Segment('_request', null, segmentData.clsSegment.trace_id)
headers['X-Amzn-Trace-Id'] = [
'Root=',
segmentData.clsSegment.trace_id,
';Parent=',
subSegment.id,
';Sampled=1'
].join('')
}
_.merge(headers, args.headers)
return headers
}
/**
function deserializeUser (req, externalId, done) {
const segment = new AWSXRay.Segment('deserialize')
const namespace = getNamespace(clsXrayConfig.nameSpaceName)
namespace.run(() => {
namespace.set(clsXrayConfig.segmentKeyName, segment)
AWSXRay.captureAsyncFunc('auth_service_deserializeUser', function (subSegment) {
return userService.findByExternalId(externalId, req.headers[CORRELATION_HEADER] || '', subSegment)
.then((user) => {
segment.close()
done(null, user)
})
.catch(err => {
segment.close(err)
logger.info(`[${req.correlationId}]: Failed to retrieve user, '${externalId}', from adminusers with statuscode: ${err.errorCode}`)
done(err)
})
}, segment)
})