-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
126 lines (114 loc) · 3.34 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
const puppeteer = require("puppeteer");
const looksSame = require("looks-same");
const debugInMem = {};
const debugStart = (message, id) => {
const now = Date.now();
debugInMem[id] = { start: now, message };
process.stderr.write(`[START] ${message}\n`);
};
const debugEnd = (id) => {
if (debugInMem[id] === void 0) {
return;
}
const now = Date.now();
debugInMem[id].end = now;
const duration = now - debugInMem[id].start;
process.stderr.write(`[END] (${duration}ms) ${debugInMem[id].message}\n`);
};
const debugSummary = () => {
const eventTimes = Object.values(debugInMem).reduce(
(prev, { start, end }) => {
prev.push(start, end);
return prev;
},
[]
);
const min = Math.min(...eventTimes);
const max = Math.max(...eventTimes);
process.stderr.write(
`[SUMMARY] (${max - min}ms) for ${eventTimes.length / 2} events.\n`
);
};
/**
*
* @param {Buffer} reference Referred image buffer
* @param {Buffer} current Current image buffer
* @return {Promise<Buffer>} Diff image buffer
*/
const generateDiff = (reference, current, options) => {
const { color } = options;
return new Promise((resolve, reject) => {
looksSame.createDiff(
{
reference,
current,
highlightColor: color,
},
(error, screenDiff) => {
if (error) {
reject(
`Diff generation error. Perhaps the specified color "${color}" is invalid.`
);
} else {
resolve(screenDiff);
}
}
);
});
};
/**
*
* @param {number} duration
*/
const sleep = (duration) =>
new Promise((resolve) => setTimeout(resolve, duration));
/**
*
* @param {string} url Target url for screenshot
* @param {{duration: number, verbose: boolean, width?: number }} options
* @return {buffer} screenshot image buffer
*/
const screenshot = async (url, options) => {
const { delay, width } = options;
const browser = await puppeteer.launch();
const page = await browser.newPage();
width !== void 0 && (await page.setViewport({ width, height: 1 }));
await page.goto(url);
await sleep(delay);
const screenshot = await page.screenshot({ fullPage: true });
await browser.close();
return screenshot;
};
/**
*
* @param {string} referenceUrl referenced url
* @param {string?} currentUrl current url
* @param {object} options { color: string, duration: number, width?: number, verbose?: bool, reference?: bool, current?: bool }
* @return {buffer} diff image buffer
*/
const w3diff = async (referenceUrl, currentUrl, options) => {
const delay = Math.max(0, options.delay || 0);
const width =
typeof options.width === "number" ? Math.max(1, options.width) : void 0;
const verbose = !!options.verbose;
verbose && debugStart(`Taking screenshot(s) URL.`, 1);
const [referenceImage, currentImage] = await Promise.all([
referenceUrl && screenshot(referenceUrl, { delay, width }),
currentUrl && screenshot(currentUrl, { delay, width }),
]);
verbose && debugEnd(1);
let output;
if (!currentUrl) {
output = referenceImage;
} else {
const { color } = options;
verbose && debugStart(`Creating a diff image.`, 2);
output = await generateDiff(referenceImage, currentImage, {
color,
});
verbose && debugEnd(2);
}
verbose && debugSummary();
return output;
};
module.exports = w3diff;