-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
197 lines (167 loc) · 4.36 KB
/
index.js
File metadata and controls
197 lines (167 loc) · 4.36 KB
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
var fs = require('fs-extra');
var path = require('path');
var childProcess = require('child_process');
var phantomjs = require('phantomjs');
var binPath = phantomjs.path;
var PNG = require('pngjs').PNG;
var pixelmatch = require('pixelmatch');
module.exports = differ;
function differ (cfg) {
getScreenshots(cfg, onScreenshotsReady);
};
// получаем снимки
function getScreenshots (cfg, cb) {
if (!cfg.what[0]._done) {
var item = cfg.what[0];
// получаем снимок
getScreenshot({
url: item.url
}, function (err, stdout, stderr) {
// сохраняем снимок
savePng64Sync(stdout, cfg.where, item.name +'.tmp');
cfg.each(item);
item._done = 'true';
cfg.what.push(item);
cfg.what.splice(0, 1);
getScreenshots(cfg, cb);
});
}
else {
// удаляем служебное поле
cfg.what.map(function (item) {
delete item._done;
return item;
});
cb(cfg);
};
};
// по готовности снимков
function onScreenshotsReady (cfg, result) {
if (!result) {
result = [];
};
if (!cfg.what[0]._done) {
var item = cfg.what[0];
var itemPath = {
tmp: path.join(
cfg.where,
item.name +'.tmp.png'
),
ref: path.join(
cfg.where,
item.name +'.png'
),
diff: path.join(
cfg.where,
item.name +'.diff.png'
)
};
var isReference = checkFileSync(itemPath.ref);
if (!isReference) {
fs.copySync(
itemPath.tmp,
itemPath.ref
);
result.push({
item: item,
path: itemPath,
type: 'new'
});
item._done = 'true';
cfg.what.push(item);
cfg.what.splice(0, 1);
onScreenshotsReady(cfg, result);
}
else {
// читаем изображения
var img1 = fs.createReadStream(itemPath.tmp).pipe(new PNG()).on('parsed', doneReading);
var img2 = fs.createReadStream(itemPath.ref).pipe(new PNG()).on('parsed', doneReading),
filesRead = 0;
// по окончанию чтения
function doneReading() {
if (++filesRead < 2) return;
var diff = new PNG({width: img1.width, height: img1.height});
var diffPixels = pixelmatch(img1.data, img2.data, diff.data, img1.width, img1.height, {threshold: 0.1});
var diffStream = fs.createWriteStream(itemPath.diff);
diffStream
.on('open', function () {
diff.pack().pipe(diffStream);
})
.on('finish', function () {
// если изображения отличаются
if (diffPixels > 0) {
result.push({
item: item,
diffPixels: diffPixels,
path: itemPath,
type: 'change'
});
}
else {
result.push({
item: item,
path: itemPath,
type: 'equal'
});
};
item._done = 'true';
cfg.what.push(item);
cfg.what.splice(0, 1);
onScreenshotsReady(cfg, result);
});
};
};
}
else {
// удаляем служебное поле
result.map(function (item) {
delete item.item._done;
return item;
});
// отправляем результат
cfg.cb(result, cfg);
};
};
// проверка существования файла
function checkFileSync (filePath) {
try {
stats = fs.lstatSync(filePath);
}
catch (e) {
return false;
};
if (stats) {
return true;
};
};
/**
* Получить снимок документа по адресу
* @param {object} cfg настройка функции
* @param {Function} cb вызывается после получения снимка
*/
function getScreenshot (cfg, cb) {
// настраиваем скрипт фантома
var childArgs = [
path.join(
__dirname,
'phantomjs-script.js'
),
JSON.stringify(cfg)
];
// стартуем процесс фантома
childProcess.execFile(binPath, childArgs, {
maxBuffer: 3 * 1024 * 1024
}, cb);
};
/**
* сохранение закодированного изображения синхронно
* @param {string} data закодированные данные
* @param {string} path куда сохранять файл
*/
function savePng64Sync (data, filePath, fileName) {
var buffer = new Buffer(data, 'base64');
fs.outputFileSync(path.join(
filePath,
fileName +'.png'
), buffer);
};