forked from nymo/gulp-tinypng-extended
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
396 lines (327 loc) · 13.4 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
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
var test = process.env.NODE_ENV == 'test',
through = require('through2'),
throughParallel = require('through2-concurrent'),
gutil = require('gulp-util'),
chalk = gutil.colors,
request = require('requestretry'),
path = require('path'),
util = require('util'),
fs = require('fs'),
crypto = require('crypto'),
minimatch = require('minimatch');
var PLUGIN_NAME = 'gulp-tinypng-extended',
PluginError = gutil.PluginError;
/**
* TinyPNG class
* @todo Move into own library
*/
function TinyPNG(opt, obj) {
var self = this;
this.conf = {
token: null,
options: {
key: '',
sigFile: false,
log: false,
force: false,
ignore: false,
sameDest: false,
summarize: false,
parallel: true,
parallelMax: 5,
keepOriginal: true,
keepMetadata: false,
retryAttempts: 10,
retryDelay: 10000
}
};
this.stats = {
total: {
in: 0,
out: 0
},
compressed: 0,
skipped: 0,
retries: 0,
retried: []
};
this.init = function(opt) {
if(typeof opt !== 'object') opt = { key: opt };
opt = util._extend(this.conf.options, opt);
if(!opt.force) opt.force = gutil.env.force || false; // force match glob
if(!opt.ignore) opt.ignore = gutil.env.ignore || false; // ignore match glob
if(opt.summarise) opt.summarize = true; // chin chin, old chap!
this.conf.options = opt; // export opts
if(opt.key)
this.conf.token = new Buffer('api:' + opt.key).toString('base64'); // prep key
this.hash = new this.hasher(opt.sigFile).populate(); // init hasher class
return this;
};
this.stream = function() {
var self = this,
opt = this.conf.options;
return (opt.parallel ? throughParallel : through).obj({maxConcurrency: opt.parallelMax}, function(file, enc, cb) {
if(self.utils.glob(file, opt.ignore)) return cb();
if(file.isNull()) {
return cb();
}
if(file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams not supported'));
return cb();
}
if(file.isBuffer()) {
var hash = null;
if(opt.sigFile && !self.utils.glob(file, opt.force)) {
var result = self.hash.compare(file);
hash = result.hash;
if(result.match) {
self.utils.log('[skipping] ' + chalk.green('✔ ') + file.relative);
self.stats.skipped++;
return cb();
}
}
self.request(file).get(function(err, tinyFile) {
if(err) {
this.emit('error', new PluginError(PLUGIN_NAME, err));
return cb();
}
self.utils.log('[compressing] ' + chalk.green('✔ ') + file.relative + chalk.gray(' (done)'));
self.stats.compressed++;
self.stats.total.in += file.contents.toString().length;
self.stats.total.out += tinyFile.contents.toString().length;
if(opt.sigFile) {
var curr = {
file: file,
hash: hash
};
if(opt.sameDest) {
curr.file = tinyFile;
curr.hash = self.hash.calc(tinyFile);
}
self.hash.update(curr.file, curr.hash);
}
if (opt.keepOriginal === false) {
fs.writeFile(file.path, tinyFile.contents, (err) => {
if(err)
throw err;
});
} else {
this.push(tinyFile);
}
return cb();
}.bind(this)); // maintain stream context
}
})
.on('error', function(err) {
console.log(err.message);
self.stats.skipped++;
self.utils.log(err.message);
})
.on('end', function() {
if(opt.sigFile) {
// write sigs after complete or but also when error occured in order to keep track of already compressed files
self.hash.write();
}
if(opt.summarize) {
var stats = self.stats,
info = util.format('Skipped: %s image%s, Retries: %s, Compressed: %s image%s, Savings: %s (ratio: %s)',
stats.skipped,
stats.skipped == 1 ? '' : 's',
stats.retries,
stats.compressed,
stats.compressed == 1 ? '' : 's',
(self.utils.prettySize(stats.total.in - stats.total.out)),
(stats.total.in ? Math.round(stats.total.out / stats.total.in * 10000) / 10000 : 0)
);
self.utils.log(info, true);
if(stats.retries > 0) {
self.utils.log('Retry Attempts:', true);
stats.retried.forEach(function(item) {
self.utils.log(item.file + ': ' + item.attempts + ' attempts', true);
});
}
}
});
};
this.request = function(file, cb) {
var self = this;
return {
file: file,
upload: function(cb) {
var file = this.file;
//do not process empty files
if(file.contents <= 0) {
err = new Error('Error: Empty or broken images could not be send ' + file.relative);
return cb(err);
}
request.post({
url: 'https://tinypng.com/web/shrink',
headers: {
'Accept' : '*/*',
'Accept-Encoding' : 'gzip, deflate',
'Accept-Language' : 'zh-CN,zh;q=0.9,en;q=0.8',
'Cache-Control' : 'no-cache',
'Pragma' : 'no-cache',
'Connection' : 'keep-alive',
'Host' : 'tinypng.com',
'Referer' : 'https://tinypng.com/',
'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36',
},
body: file.contents,
maxAttempts: self.conf.retryAttempts,
retryDelay: self.conf.retryDelay,
retryStrategy: request.RetryStrategies.HTTPOrNetworkError // (default) retry on 5xx or network errors
}, function(err, res, body) {
var data,
info = {
url: false,
count: (res && 'headers' in res && res.headers['compression-count']) || 0
};
if(res && res.attempts > 1){
self.stats.retries += res.attempts - 1;
self.stats.retried.push({
file: file.relative,
attempts: res.attempts
});
}
if(err) {
err = new Error('Upload failed for ' + file.relative + ' with error: ' + err.message);
} else if(body) {
if(res.statusCode == 200 || res.statusCode == 201) {
try {
data = JSON.parse(body);
} catch(e) {
err = new Error('Upload response JSON parse failed, invalid data returned from API. Failed with message: ' + e.message);
}
if(!err) {
if(data.error){
err = this.handler(data, res.statusCode);
} else if (data.output.url) {
info.url = self.conf.options.keepMetadata ? res.headers.location : data.output.url;
} else {
err = new Error('Invalid TinyPNG response object returned for ' + file.relative);
}
}
} else {
err = new Error('Error: Statuscode ' + res.statusCode + ' returned');
}
} else {
err = new Error('No content returned from TinyPNG API for' + file.relative);
}
cb(err, info);
}.bind(this));
},
download: function(url, cb) {
var options = {
url: url,
encoding: null
};
if (self.conf.options.keepMetadata) {
options.json = { preserve: ["copyright", "creation"] };
options.headers = {
'Authorization': 'Basic ' + self.conf.token
};
}
request.get(options, function(err, res, body) {
err = err ? new Error('Download failed for ' + url + ' with error: ' + err.message) : false;
var buffer = false;
try {
buffer = new Buffer(body);
} catch(err) {
return cb(new Error('Empty Body for Download with error: ' + err.message));
}
return cb(err, buffer);
});
},
handler: function(data, status) {
return new Error((data.error || 'Unknown') + ' (' + status + '): ' + (data.message || 'No message returned') + ' for ' + file.relative);
},
get: function(cb) {
var self = this,
file = this.file;
self.upload(function(err, data) {
if(err) return cb(err, file);
self.download(data.url, function(err, data) {
if(err) return cb(err, file);
var tinyFile = file.clone();
tinyFile.contents = data;
cb(false, tinyFile);
});
});
return this;
}
};
};
this.hasher = function(sigFile) {
return {
sigFile: sigFile || false,
sigs: {},
calc: function(file, cb) {
var md5 = crypto.createHash('md5').update(file.contents).digest('hex');
cb && cb(md5);
return cb ? this : md5;
},
update: function(file, hash) {
this.changed = true;
this.sigs[file.path.replace(file.cwd, '')] = hash;
return this;
},
compare: function(file, cb) {
var md5 = this.calc(file),
filepath = file.path.replace(file.cwd, ''),
result = (filepath in this.sigs && md5 === this.sigs[filepath]);
cb && cb(result, md5);
return cb ? this : { match: result, hash: md5 };
},
populate: function() {
var data = false;
if(this.sigFile) {
try {
data = fs.readFileSync(this.sigFile, 'utf-8');
if(data) data = JSON.parse(data);
} catch(err) {
// meh
}
if(data) this.sigs = data;
}
return this;
},
write: function() {
if(this.changed) {
try {
fs.writeFileSync(this.sigFile, JSON.stringify(this.sigs));
} catch(err) {
// meh
}
}
return this;
}
};
};
this.utils = {
log: function(message, force) {
if(self.conf.options.log || force) gutil.log(PLUGIN_NAME, message);
return this;
},
glob: function(file, glob, opt) {
opt = opt || {};
var result = false;
if(typeof glob === 'boolean') return glob;
try {
result = minimatch(file.path, glob, opt);
} catch(err) {}
if(!result && !opt.matchBase) {
opt.matchBase = true;
return this.glob(file, glob, opt);
}
return result;
},
prettySize: function(bytes) {
if(bytes === 0) return '0.00 B';
var pos = Math.floor(Math.log(bytes) / Math.log(1024));
return (bytes / Math.pow(1024, pos)).toFixed(2) + ' ' + ' KMGTP'.charAt(pos) + 'B';
}
};
return (obj || test) ? this.init(opt) : this.init(opt).stream();
}
module.exports = TinyPNG;