-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
338 lines (299 loc) · 9.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
const { Command } = require("commander")
const fs = require("fs")
const puppeteer = require("puppeteer-core")
const sharp = require("sharp")
const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3")
//
// DEFINITIONS
//
const DELAY_MIN = 0
const DELAY_MAX = 300000
// the different capture modes
const CAPTURE_MODES = [
"CANVAS",
"VIEWPORT",
]
// the different trigger modes
const TRIGGER_MODES = [
"DELAY",
"FN_TRIGGER",
]
// possible output errors
const ERRORS = {
UNKNOWN: "UNKNOWN",
HTTP_ERROR: "HTTP_ERROR",
MISSING_PARAMETERS: "MISSING_PARAMETERS",
INVALID_TRIGGER_PARAMETERS: "INVALID_TRIGGER_PARAMETERS",
INVALID_PARAMETERS: "INVALID_PARAMETERS",
UNSUPPORTED_URL: "UNSUPPORTED_URL",
CANVAS_CAPTURE_FAILED: "CANVAS_CAPTURE_FAILED",
TIMEOUT: "TIMEOUT",
EXTRACT_FEATURES_FAILED: "EXTRACT_FEATURES_FAILED",
}
//
// UTILITY FUNCTIONS
//
// sleep X milliseconds
const sleep = (time) => new Promise(resolve => {
setTimeout(resolve, time)
})
// generic function which resolves once the waiting conditions to take a preview
// are met (delay, programmatic trigger)
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 () {
return new Promise(function (resolve, reject) {
window.addEventListener("fxhash-preview", function () {
resolve() // resolves when the event fires
})
})
}),
sleep(DELAY_MAX)
]).then(resolve)
}
})
// given a trigger mode and an optionnal delay, returns true of false depending on the
// validity of the trigger input settings
function isTriggerValid(triggerMode, delay) {
if (!TRIGGER_MODES.includes(triggerMode)) {
return false
}
if (triggerMode === "DELAY") {
// delay must be defined if trigger mode is delay
return typeof delay !== undefined && !isNaN(delay) && delay >= DELAY_MIN && delay <= DELAY_MAX
}
else if (triggerMode === "FN_TRIGGER") {
// fn trigger doesn't need any param
return true
}
}
// process the raw features extracted into attributes
function processRawTokenFeatures(rawFeatures) {
const features = []
// first check if features are an object
if (typeof rawFeatures !== "object" || Array.isArray(rawFeatures) || !rawFeatures) {
throw null
}
// go through each property and process it
for (const name in rawFeatures) {
// chack if propery is accepted type
if (!(typeof rawFeatures[name] === "boolean" || typeof rawFeatures[name] === "string" || typeof rawFeatures[name] === "number")) {
continue
}
// all good, the feature can be added safely
features.push({
name,
value: rawFeatures[name]
})
}
return features
}
// process the command line arguments
const program = new Command()
program
.requiredOption('--url <url>', 'The URL of the resource to fetch')
.requiredOption('--mode <mode>', 'The mode of the capture')
.option('--trigger <trigger>', 'The trigger mode of the capture (DELAY, FN_TRIGGER)')
.option('--delay <delay>', 'The delay before the capture is taken')
.option('--resX <resX>', 'The width of the viewport, in case of mode VIEWPORT')
.option('--resY <resY>', 'The height of the viewport, in case of mode VIEWPORT')
.option('--selector <selector>', 'The CSS selector to target the CANVAS, in case of a capture')
program.parse(process.argv)
const main = async () => {
// global definitions
let capture,
captureName,
features = []
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"
}
//
// Checking parameters validity
//
// general parameters
if (!url || !mode) {
throw ERRORS.MISSING_PARAMETERS
}
if (!CAPTURE_MODES.includes(mode)) {
throw ERRORS.INVALID_PARAMETERS
}
// parameters based on selected mode
if (mode === "VIEWPORT") {
if (!resX || !resY) {
throw ERRORS.MISSING_PARAMETERS
}
resX = Math.round(resX)
resY = Math.round(resY)
if (isNaN(resX) || isNaN(resY) || resX < 256 || resX > 2048 || resY < 256 || resY > 2048) {
throw ERRORS.INVALID_PARAMETERS
}
if (delay < DELAY_MIN || delay > DELAY_MAX) {
throw ERRORS.INVALID_PARAMETERS
}
}
else if (mode === "CANVAS") {
if (!selector) {
throw ERRORS.INVALID_PARAMETERS
}
}
console.log("bootstrapping chromium...")
const browser = await puppeteer.launch({
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--use-gl=egl'
],
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH
})
console.log("configuring page...")
// browse to the page
const viewportSettings = {
deviceScaleFactor: 1,
}
if (mode === "VIEWPORT") {
viewportSettings.width = resX
viewportSettings.height = resY
}
else {
viewportSettings.width = 800
viewportSettings.height = 800
}
let page = await browser.newPage()
await page.setViewport(viewportSettings)
// 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) {
console.log(err)
if (err && err.name && err.name === "TimeoutError") {
throw ERRORS.TIMEOUT
}
else {
throw ERRORS.UNKNOWN
}
}
// if the response is not 200 (success), we want to throw
if (response.status() !== 200) {
throw ERRORS.HTTP_ERROR
}
try {
// based on the capture mode use different capture strategies
if (mode === "VIEWPORT") {
await waitPreview(triggerMode, page, delay)
// we simply take a capture of the viewport
capture = await page.screenshot()
}
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
return el.toDataURL()
})
if (!base64) throw null
const pureBase64 = base64.replace(/^data:image\/png;base64,/, "")
capture = Buffer.from(pureBase64, "base64")
}
// if the capture is too big, we want to reduce its size
// ! we don't need to reduce the size anymore since S3 is used to store the images
// if (capture.byteLength > 10*1024*1024) {
// capture = await sharp(capture)
// .resize(1024, 1024, { fit: "inside" })
// .jpeg({ quality: 100 })
// .toBuffer()
// }
}
catch (err) {
console.log(err)
throw ERRORS.CANVAS_CAPTURE_FAILED
}
// EXTRACT FEATURES
console.log("extracting features...")
// find $fxhashFeatures in the window object
let rawFeatures = null
try {
const extractedFeatures = await page.evaluate(
() => {
// v3 syntax
if (window.$fx?._features) return JSON.stringify(window.$fx._features)
// deprecated syntax
return JSON.stringify(window.$fxhashFeatures)
}
)
rawFeatures = (extractedFeatures && JSON.parse(extractedFeatures)) || null
}
catch {
throw ERRORS.EXTRACT_FEATURES_FAILED
}
// turn raw features into attributed
try {
features = processRawTokenFeatures(rawFeatures)
}
catch { }
// if features are still undefined, we assume that there are none
features = features || []
// call for the close of the browser, but don't wait for it
browser.close()
// create the S3 client
const client = new S3Client({
region: process.env.AWS_S3_REGION,
})
// 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,
Key: `${baseKey}/preview.png`,
Body: capture,
ContentType: "image/png",
}))
// upload the features object to a JSON file
await client.send(new PutObjectCommand({
Bucket: process.env.AWS_S3_BUCKET,
Key: `${baseKey}/features.json`,
Body: JSON.stringify(features),
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)
}
catch (error) {
console.error(error)
process.exit(1)
}
}
main()