Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
image.toBuffer(async (err, data, info) => {
if (err) {
reject(err);
}
// Pass PNG images through PNGQuant as Sharp is not good at compressing them.
// See https://github.com/lovell/sharp/issues/478
if ( info.format === 'png' ) {
if ( enableTracing ) {
var mainSegment = AWSXRay.getSegment();
var segment = mainSegment.addNewSubsegment( 'imagemin-pngquant' );
}
data = await imageminPngquant()( data );
if ( enableTracing ) {
segment.close();
}
// Make sure we update the size in the info, to reflect the new
// size after lossless-compression.
info.size = data.length;
}
callback && callback(null, data, info);
resolve({ data, info });
(async () => {
try {
await init();
console.log('worker processing queue');
while (true) {
try {
let msg = await consumer.receive();
if (!msg) continue;
console.log('message received: ', msg);
let json = JSON.parse(msg);
let res = await db.updateVote(json);
console.log('message saved: %j', res);
let segment = xray.getSegment();
if (segment)
xray.getSegment().close();
} catch (err) {
console.log(err);
}
}
} catch (err) {
console.log(err);
} finally {
try {
await quit();
console.log('worker stopped processing queue');
} catch (err) {
console.log(err);
getSegment: () => {
if (traceId !== process.env._X_AMZN_TRACE_ID) {
traceId = process.env._X_AMZN_TRACE_ID
segment = process.env.TRACE ? AWSXray.getSegment() : null
}
return {
addNewSubsegment: subsegmentName => {
const subsegment = process.env.TRACE
? segment.addNewSubsegment(subsegmentName)
: null
const prevSegment = segment
segment = subsegment
return {
addAnnotation: (annotationName, data) => {
if (process.env.TRACE) {
subsegment.addAnnotation(annotationName, data)
}
},
constructor() {
super();
this.segment = AWSXRay.getSegment().addNewSubsegment( 'vandium' );
}
axios.interceptors.request.use(function (config) {
var parent = xray.getSegment();
var subsegment = parent.addNewSubsegment(config.baseURL + config.url.substr(1));
subsegment.namespace = 'remote';
let root = parent.segment ? parent.segment : parent;
let header = 'Root=' + root.trace_id + ';Parent=' + subsegment.id + ';Sampled=' + (!root.notTraced ? '1' : '0');
config.headers.get={ 'x-amzn-trace-id': header };
config.headers.post={ 'x-amzn-trace-id': header };
xray.setSegment(subsegment);
return config;
}, function (error) {
var subsegment = xray.getSegment().addNewSubsegment("Intercept request error");
get currentTraceContext(): TraceContext | undefined {
if (this.rootTraceContext === undefined) {
return;
}
const traceContext = { ...this.rootTraceContext };
const datadogContext = this.tracerWrapper.traceContext();
if (datadogContext) {
logDebug(`set trace context from dd-trace with parent ${datadogContext.parentID}`);
return datadogContext;
}
try {
const xraySegment = getSegment();
const value = convertToAPMParentID(xraySegment.id);
if (value !== undefined) {
logDebug(`set trace context from xray with parent ${value} from segment`);
traceContext.parentID = value;
}
} catch (error) {
logError("couldn't retrieve segment from xray", { innerError: error });
}
return traceContext;
}
export function readTraceContextFromXray() {
try {
const segment = getSegment();
const traceHeader = {
parentID: segment.id,
sampled: segment.notTraced ? 0 : 1,
traceID: segment.trace_id,
};
return convertTraceContext(traceHeader);
} catch (error) {
logError("couldn't read xray trace header", { innerError: error });
}
return undefined;
}
const initPerformanceTracer = resolve => {
let segment = process.env.TRACE ? AWSXray.getSegment() : null
let traceId = process.env._X_AMZN_TRACE_ID
Object.defineProperty(resolve, 'performanceTracer', {
value: {
getSegment: () => {
if (traceId !== process.env._X_AMZN_TRACE_ID) {
traceId = process.env._X_AMZN_TRACE_ID
segment = process.env.TRACE ? AWSXray.getSegment() : null
}
return {
addNewSubsegment: subsegmentName => {
const subsegment = process.env.TRACE
? segment.addNewSubsegment(subsegmentName)
: null
const prevSegment = segment
}, function (error) {
var subsegment = xray.getSegment().addNewSubsegment("Intercept request error");
subsegment.close(error);
return Promise.reject(error);
});
}, function (error) {
var subsegment = xray.getSegment();
subsegment.close(error);
return Promise.reject(error);
});
};