Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return new Promise((resolve) => {
// Publish the message body as an AWS X-Ray annotation.
// This allows us to trace the message processing through AWS X-Ray.
AWSXRay.captureAsyncFunc(topicName, (subsegment0) => {
subsegment = subsegment0;
try {
const msg = JSON.parse(buffer.toString());
const body = msg.body || msg;
if (!body) {
console.log('awsGetTopic', 'no_body');
return resolve('no_body');
}
for (const key of Object.keys(body)) {
// Log only scalar values.
const val = body[key];
if (val === null || val === undefined) continue;
if (typeof val === 'object') continue;
subsegment.addAnnotation(key, val);
}
} catch (error) {
exports.handler = async (event, callback) => {
callback.callbackWaitsForEmptyEventLoop = false;
/** @type {ChromdaOptions} */
const options = normalizeEvent(event);
// validate URL before wasting time waiting for Chrome to start
new URL(options?.url);
const browser = await puppetshot;
await AWSXRay.captureAsyncFunc("navigate", async segment => {
segment.addAnnotation("url", options.url);
options.puppeteer?.navigation &&
segment.addAnnotation(
"options",
JSON.stringify(options.puppeteer?.navigation)
);
await browser.navigate(options.url, options.puppeteer?.navigation);
segment.close();
});
options.puppeteer?.viewport &&
(await browser.resizeViewport(options.puppeteer.viewport));
options.styles &&
(await Promise.all([
options.styles.map(style => browser.overrideStyles(style))
if (username !== "" && message !== "")
{
var id = Math.floor(new Date() / 1000);
var params = {
TableName: table,
Item: {
"id": id,
"message": message,
"username": username
}
};
console.log("Adding a new message to DynamoDB: " + message);
AWSXRay.captureAsyncFunc("## Writing to DynamoDB", function(subsegment) {
docClient.put(params, function(err, data) {
if (err) {
console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
responseBody.message = "Unable to add item. Error JSON:", JSON.stringify(err, null, 2);
response.statusCode = 503;
} else {
console.log("Added item:", JSON.stringify(data, null, 2));
responseBody.message = "Added item:", JSON.stringify(data, null, 2);
}
response.body = JSON.stringify(responseBody);
console.log("response: " + JSON.stringify(response))
callback(null, response);
subsegment.close();
return new Promise(function (resolve, reject) {
AWSXRay.captureAsyncFunc(name, (segment) => {
func(segment).then((result) => {
segment.close();
resolve(result);
}, (error) => {
segment.close(error);
reject(error);
});
});
});
}
return new Promise((resolve, reject) => {
try {
XRay.captureAsyncFunc(segmentName, (subSegment) => {
try {
if (!subSegment) {
return promiseFactory()
.then(val => resolve(val))
.catch(err => reject(err));
} else {
addMetadata(subSegment, metadata);
addAnnotations(subSegment, annotations);
promiseFactory(subSegment)
.then(val => {
resolve(val);
subSegment.close();
})
.catch(err => {
reject(err);
});
options.puppeteer?.viewport &&
(await browser.resizeViewport(options.puppeteer.viewport));
options.styles &&
(await Promise.all([
options.styles.map(style => browser.overrideStyles(style))
]));
options.exclude?.length && (await browser.excludeElements(options.exclude));
const imageType = options.puppeteer?.screenshot?.type || "png";
const capture = options.capture || "viewport";
const buffer = await AWSXRay.captureAsyncFunc("screenshot", async segment => {
segment.addAnnotation("capture", capture);
/** @type {Buffer} */
let buffer;
switch (capture) {
case "element":
buffer = await browser.elementScreenshot(
options.selector,
options.puppeteer?.screenshot
);
break;
case "page":
buffer = await browser.pageScreenshot(options.puppeteer?.screenshot);
break;
case "viewport":