-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
568 lines (523 loc) · 15.6 KB
/
controller.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
const crypto = require('crypto');
const fs = require('fs');
const { mkdirp } = require('mkdirp');
const nodeFetch = require('node-fetch');
const imageSize = require('image-size');
const pAll = require('p-all');
const { RemoteBrowserTarget } = require('happo.io');
const createAssetPackage = require('./src/createAssetPackage');
const findCSSAssetUrls = require('./src/findCSSAssetUrls');
const makeRequest = require('happo.io/build/makeRequest').default;
const proxiedFetch = require('./src/fetch');
const loadHappoConfig = require('./src/loadHappoConfig');
const makeAbsolute = require('./src/makeAbsolute');
const makeExternalUrlsAbsolute = require('./src/makeExternalUrlsAbsolute');
const resolveEnvironment = require('./src/resolveEnvironment');
const convertBase64FileToReal = require('./src/convertBase64FileToReal');
const { HAPPO_E2E_PORT, HAPPO_DEBUG, HAPPO_ENABLED } = process.env;
function getUniqueUrls(urls) {
const seenKeys = new Set();
const result = [];
urls.forEach((url) => {
const key = [url.url, url.baseUrl].join('||');
if (!seenKeys.has(key)) {
result.push(url);
seenKeys.add(key);
}
});
return urls;
}
function ampersands(string) {
return string.replace(/&/g, '&');
}
async function downloadCSSContent(blocks) {
const actions = blocks.map((block) => async () => {
if (block.href) {
const absUrl = makeAbsolute(block.href, block.baseUrl);
if (HAPPO_DEBUG) {
console.log(`[HAPPO] Downloading CSS file from ${absUrl}`);
}
let res;
try {
res = await proxiedFetch(absUrl, { retryCount: 5 });
} catch (e) {
console.warn(
`[HAPPO] Failed to fetch CSS file from ${block.href} (using base URL ${block.baseUrl}). This might mean styles are missing in your Happo screenshots`,
);
return;
}
let text = await res.text();
if (HAPPO_DEBUG) {
console.log(
`[HAPPO] Done downloading CSS file from ${absUrl}. Got ${text.length} chars back.`,
);
}
if (!absUrl.startsWith(block.baseUrl)) {
text = makeExternalUrlsAbsolute(text, absUrl);
}
block.content = text;
block.assetsBaseUrl = absUrl.replace(/\/[^/]*$/, '/');
delete block.href;
}
});
await pAll(actions, { concurrency: 5 });
}
class Controller {
async init() {
this.snapshots = [];
this.allCssBlocks = [];
this.snapshotAssetUrls = [];
this.localSnapshots = [];
this.localSnapshotImages = {};
this.knownComponentVariants = {};
if (!(HAPPO_E2E_PORT || HAPPO_ENABLED)) {
console.log(
`
[HAPPO] Happo is disabled. Here's how to enable it:
- Use the \`happo-e2e\` wrapper.
- Set \`HAPPO_ENABLED=true\`.
Docs:
https://docs.happo.io/docs/cypress#usage-with-cypress-run
https://docs.happo.io/docs/cypress#usage-with-cypress-open
`.trim(),
);
return null;
}
if (HAPPO_DEBUG) {
console.log('[HAPPO] Running Controller.init');
}
this.happoConfig = await loadHappoConfig();
}
isActive() {
const result = !!this.happoConfig;
if (HAPPO_DEBUG) {
console.log('[HAPPO] Controller.isActive()?', result);
}
return result;
}
async uploadAssetsIfNeeded({ buffer, hash }) {
if (HAPPO_DEBUG) {
console.log(`[HAPPO] Checking if we need to upload assets`);
}
try {
// Check if the assets already exist. If so, we don't have to upload them.
const assetsDataRes = await makeRequest(
{
url: `${this.happoConfig.endpoint}/api/snap-requests/assets-data/${hash}`,
method: 'GET',
json: true,
},
{ ...this.happoConfig },
);
if (HAPPO_DEBUG) {
console.log(
`[HAPPO] Reusing existing assets at ${assetsDataRes.path} (previously uploaded on ${assetsDataRes.uploadedAt})`,
);
}
return assetsDataRes.path;
} catch (e) {
if (e.statusCode !== 404) {
throw e;
}
}
if (HAPPO_DEBUG) {
console.log(`[HAPPO] Uploading assets package`);
}
const assetsRes = await makeRequest(
{
url: `${this.happoConfig.endpoint}/api/snap-requests/assets/${hash}`,
method: 'POST',
json: true,
formData: {
payload: {
options: {
filename: 'payload.zip',
contentType: 'application/zip',
},
value: buffer,
},
},
},
{ ...this.happoConfig, maxTries: 3 },
);
if (HAPPO_DEBUG) {
console.log('[HAPPO] Done uploading assets package, got', assetsRes);
}
return assetsRes.path;
}
async finish() {
if (HAPPO_DEBUG) {
console.log('[HAPPO] Running Controller.finish');
}
if (this.localSnapshots.length) {
if (HAPPO_DEBUG) {
console.log(
`[HAPPO] Processing ${this.localSnapshots.length} local snapshots`,
);
}
await this.processSnapRequestIds([await this.uploadLocalSnapshots()]);
return null;
}
if (!this.snapshots.length) {
if (HAPPO_DEBUG) {
console.log('[HAPPO] No snapshots recorded');
}
return null;
}
this.dedupeSnapshots();
await downloadCSSContent(this.allCssBlocks);
const allUrls = [...this.snapshotAssetUrls];
this.allCssBlocks.forEach((block) => {
findCSSAssetUrls(block.content).forEach((url) =>
allUrls.push({ url, baseUrl: block.assetsBaseUrl || block.baseUrl }),
);
});
const uniqueUrls = getUniqueUrls(allUrls);
const { buffer, hash } = await createAssetPackage(uniqueUrls);
const assetsPath = await this.uploadAssetsIfNeeded({ buffer, hash });
const globalCSS = this.allCssBlocks.map((block) => ({
id: block.key,
conditional: true,
css: block.content,
}));
for (const url of uniqueUrls) {
if (/^\/_external\//.test(url.name) && url.name !== url.url) {
for (const block of globalCSS) {
block.css = block.css.split(url.url).join(url.name);
}
this.snapshots.forEach((snapshot) => {
snapshot.html = snapshot.html.split(url.url).join(url.name);
if (/&/.test(url.url)) {
// When URL has an ampersand, we need to make sure the html wasn't
// escaped so we replace again, this time with "&" replaced by
// "&"
snapshot.html = snapshot.html.split(ampersands(url.url)).join(url.name);
}
});
}
}
const allRequestIds = [];
for (const name of Object.keys(this.happoConfig.targets)) {
if (HAPPO_DEBUG) {
console.log(`[HAPPO] Sending snap-request(s) for target=${name}`);
}
const snapshotsForTarget = this.snapshots.filter(
({ targets }) => !targets || targets.includes(name),
);
if (!snapshotsForTarget.length) {
if (HAPPO_DEBUG) {
console.log(`[HAPPO] No snapshots recorded for target=${name}. Skipping.`);
}
continue;
}
const requestIds = await this.happoConfig.targets[name].execute({
targetName: name,
asyncResults: true,
endpoint: this.happoConfig.endpoint,
globalCSS,
assetsPackage: assetsPath,
snapPayloads: snapshotsForTarget,
apiKey: this.happoConfig.apiKey,
apiSecret: this.happoConfig.apiSecret,
});
if (HAPPO_DEBUG) {
console.log(
`[HAPPO] Snap-request(s) for target=${name} created with ID(s)=${requestIds.join(
',',
)}`,
);
}
allRequestIds.push(...requestIds);
}
await this.processSnapRequestIds(allRequestIds);
}
async registerSnapshot({
timestamp,
html,
assetUrls,
cssBlocks,
component,
variant,
targets: rawTargets,
htmlElementAttrs,
bodyElementAttrs,
}) {
if (!component) {
throw new Error('Missing `component`');
}
if (!variant) {
throw new Error('Missing `variant`');
}
if (HAPPO_DEBUG) {
console.log(`[HAPPO] Registering snapshot for ${component} > ${variant}`);
}
this.snapshotAssetUrls.push(...assetUrls);
const targets = this.handleDynamicTargets(rawTargets);
this.snapshots.push({
timestamp,
html,
component,
variant,
targets,
stylesheets: cssBlocks.map((b) => b.key),
htmlElementAttrs,
bodyElementAttrs,
});
cssBlocks.forEach((block) => {
if (this.allCssBlocks.some((b) => b.key === block.key)) {
return;
}
this.allCssBlocks.push(block);
});
}
async registerLocalSnapshot({
component,
variant: rawVariant,
targets,
target,
width,
height,
// One of path, buffer is required
path,
buffer,
}) {
const variant = this.dedupeVariant(component, rawVariant);
if (!width && !height && buffer) {
const dimensions = imageSize(buffer);
width = dimensions.width;
height = dimensions.height;
}
this.localSnapshots.push({
component,
variant,
targets,
target,
url: await this.uploadImage(path || buffer),
width,
height,
});
}
removeSnapshotsMadeBetween({ start, end }) {
if (HAPPO_DEBUG) {
console.log(
`[HAPPO] Removing snapshots made between ${new Date(
start,
)} and ${new Date(end)}`,
);
}
this.snapshots = this.snapshots.filter(({ timestamp }) => {
if (!timestamp) {
return true;
}
return timestamp < start || timestamp > end;
});
}
removeDuplicatesInTimeframe({ start, end }) {
if (HAPPO_DEBUG) {
console.log(
`[HAPPO] Removing duplicate snapshots made between ${new Date(
start,
)} and ${new Date(end)}`,
);
}
const seenSnapshots = {};
this.snapshots = this.snapshots.filter((snapshot) => {
const { timestamp, component, variant } = snapshot;
if (!timestamp) {
return true;
}
const id = [component, variant].join('-_|_-');
const inTimeframe = timestamp >= start && timestamp <= end;
if (inTimeframe) {
if (seenSnapshots[id]) {
// Found a duplicate made in the timeframe specified
if (HAPPO_DEBUG) {
console.log(
`[HAPPO] Found duplicate snapshot to remove: "${component}", "${variant}" at timestamp ${new Date(
timestamp,
)}`,
);
}
return false;
}
seenSnapshots[id] = true;
}
return true;
});
}
async processSnapRequestIds(allRequestIds) {
if (HAPPO_E2E_PORT) {
// We're running with `happo-cypress --`
const fetchRes = await nodeFetch(`http://localhost:${HAPPO_E2E_PORT}/`, {
method: 'POST',
body: allRequestIds.join('\n'),
});
if (!fetchRes.ok) {
throw new Error('Failed to communicate with happo-e2e server');
}
} else {
// We're not running with `happo-e2e --`. We'll create a report
// despite the fact that it might not contain all the snapshots. This is
// still helpful when running e.g. `cypress open` locally.
const { afterSha } = resolveEnvironment();
const reportResult = await makeRequest(
{
url: `${this.happoConfig.endpoint}/api/async-reports/${afterSha}`,
method: 'POST',
json: true,
body: {
requestIds: allRequestIds,
project: this.happoConfig.project,
},
},
{ ...this.happoConfig, maxTries: 3 },
);
console.log(`[HAPPO] ${reportResult.url}`);
// Reset the component variants so that we can run the test again while
// cypress is still open.
this.knownComponentVariants = {};
return null;
}
}
handleDynamicTargets(targets) {
const result = [];
if (typeof targets === 'undefined') {
// return non-dynamic targets from .happo.js
return Object.keys(this.happoConfig.targets).filter(
(targetName) => !this.happoConfig.targets[targetName].__dynamic,
);
}
for (const target of targets) {
if (typeof target === 'string') {
result.push(target);
}
if (
typeof target === 'object' &&
target.name &&
target.viewport &&
target.browser
) {
if (this.happoConfig.targets[target.name]) {
// already added
} else {
// add dynamic target
this.happoConfig.targets[target.name] = new RemoteBrowserTarget(
target.browser,
target,
);
this.happoConfig.targets[target.name].__dynamic = true;
}
result.push(target.name);
}
}
return result;
}
async uploadImage(pathOrBuffer) {
const pathToFile = Buffer.isBuffer(pathOrBuffer) ? undefined : pathOrBuffer;
if (HAPPO_DEBUG) {
console.log(`[HAPPO] Uploading image ${pathToFile || ''}`);
}
const buffer = pathToFile
? await fs.promises.readFile(pathToFile)
: pathOrBuffer;
const hash = crypto.createHash('md5').update(buffer).digest('hex');
const uploadUrlResult = await makeRequest(
{
url: `${this.happoConfig.endpoint}/api/images/${hash}/upload-url`,
method: 'GET',
json: true,
},
{ ...this.happoConfig, maxTries: 2 },
);
if (!uploadUrlResult.uploadUrl) {
// image has already been uploaded
if (HAPPO_DEBUG) {
console.log(
`[HAPPO] Image has already been uploaded: ${uploadUrlResult.url}`,
);
}
return uploadUrlResult.url;
}
const uploadResult = await makeRequest(
{
url: uploadUrlResult.uploadUrl,
method: 'POST',
json: true,
formData: {
file: {
options: {
filename: 'image.png',
contentType: 'image/png',
},
value: buffer,
},
},
},
{ ...this.happoConfig, maxTries: 2 },
);
if (HAPPO_DEBUG) {
console.log(`[HAPPO] Uploaded image: ${uploadUrlResult.url}`);
}
return uploadResult.url;
}
async uploadLocalSnapshots() {
const reportResult = await makeRequest(
{
url: `${this.happoConfig.endpoint}/api/snap-requests/with-results`,
method: 'POST',
json: true,
body: {
snaps: this.localSnapshots,
},
},
{ ...this.happoConfig, maxTries: 3 },
);
return reportResult.requestId;
}
dedupeVariant(component, variant) {
this.knownComponentVariants[component] =
this.knownComponentVariants[component] || {};
const comp = this.knownComponentVariants[component];
comp[variant] = comp[variant] || 0;
comp[variant]++;
if (comp[variant] === 1) {
return variant;
}
return `${variant}-${comp[variant]}`;
}
dedupeSnapshots() {
for (const snapshot of this.snapshots) {
snapshot.variant = this.dedupeVariant(snapshot.component, snapshot.variant);
}
}
async registerBase64ImageChunk({ base64Chunk, src, isFirst, isLast }) {
const filename = src.slice(1);
const filenameB64 = `${filename}.b64`;
if (isFirst) {
await mkdirp('.happo-tmp/_inlined');
await new Promise((resolve, reject) =>
fs.writeFile(filenameB64, base64Chunk, (e) => {
if (e) {
reject(e);
} else {
resolve();
}
}),
);
} else {
await new Promise((resolve, reject) =>
fs.appendFile(filenameB64, base64Chunk, (e) => {
if (e) {
reject(e);
} else {
resolve();
}
}),
);
}
if (isLast) {
await convertBase64FileToReal(filenameB64, filename);
}
}
}
module.exports = Controller;