Skip to content

Commit

Permalink
Merge pull request #6 from fxhash/feat/all-the-logs
Browse files Browse the repository at this point in the history
add all the logs for debugging
  • Loading branch information
louisholley authored Jan 9, 2024
2 parents b834871 + e7d6385 commit 752f4fa
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ const sleep = (time) => new Promise(resolve => {
const waitPreview = (triggerMode, page, delay) => new Promise(async (resolve) => {
let resolved = false
if (triggerMode === "DELAY") {
console.log("waiting for delay:", delay)
await sleep(delay)
resolve()
}
else if (triggerMode === "FN_TRIGGER") {
console.log("waiting for function trigger...")
Promise.race([
// add event listener and wait for event to fire before returning
page.evaluate(function () {
Expand Down Expand Up @@ -127,6 +129,16 @@ const main = async () => {
try {
let { url, mode, trigger: triggerMode, delay, resX, resY, selector, features } = program.opts()

console.log("running capture with params:", {
url,
mode,
resX,
resY,
triggerMode,
delay,
selector,
})

// default parameter for triggerMode
if (typeof triggerMode === "undefined") {
triggerMode = "DELAY"
Expand Down Expand Up @@ -164,6 +176,8 @@ const main = async () => {
}
}

console.log("bootstrapping chromium...")

const browser = await puppeteer.launch({
headless: true,
args: [
Expand All @@ -175,6 +189,8 @@ const main = async () => {
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH
})

console.log("configuring page...")

// browse to the page
const viewportSettings = {
deviceScaleFactor: 1,
Expand All @@ -193,10 +209,12 @@ const main = async () => {
// try to reach the page
let response
try {
console.log("navigating to: ", url)
response = await page.goto(url, {
timeout: 200000,
waitUntil: "domcontentloaded"
})
console.log(`navigated to URL with response status: ${response.status()}`);
}
catch (err) {
if (err && err.name && err.name === "TimeoutError") {
Expand All @@ -221,6 +239,7 @@ const main = async () => {
}
else if (mode === "CANVAS") {
await waitPreview(triggerMode, page, delay)
console.log("converting canvas to PNG with selector:", selector)
// get the base64 image from the CANVAS targetted
const base64 = await page.$eval(selector, (el) => {
if (!el || el.tagName !== "CANVAS") return null
Expand All @@ -246,6 +265,7 @@ const main = async () => {


// EXTRACT FEATURES
console.log("extracting features...")
// find $fxhashFeatures in the window object
let rawFeatures = null
try {
Expand Down Expand Up @@ -283,6 +303,8 @@ const main = async () => {
// the base key path
const baseKey = process.env.AWS_BATCH_JOB_ID

console.log("uploading capture to S3...");

// upload the preview PNG
await client.send(new PutObjectCommand({
Bucket: process.env.AWS_S3_BUCKET,
Expand All @@ -299,6 +321,8 @@ const main = async () => {
ContentType: "application/json",
}))

console.log("successfully uploaded capture to S3");

// it's a success, we write success to cloud watch
console.log(`Successfully processed ${url}`)
process.exit(0)
Expand Down

0 comments on commit 752f4fa

Please sign in to comment.