-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #633 from assetgraph/slim-dependencies
Reduce external dependencies
- Loading branch information
Showing
8 changed files
with
300 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,88 @@ | ||
/* global setImmediate:true */ | ||
// node 0.8 compat | ||
if (typeof setImmediate === 'undefined') { | ||
setImmediate = process.nextTick; | ||
} | ||
|
||
const Stream = require('stream'); | ||
const util = require('util'); | ||
const PngQuant = require('pngquant'); | ||
let histogram; | ||
const histogram = require('histogram'); | ||
|
||
try { | ||
histogram = require('histogram'); | ||
} catch (err) {} | ||
class PngQuantWithHistogram extends Stream { | ||
constructor() { | ||
super(); | ||
|
||
function PngQuantWithHistogram() { | ||
// Fall back to quanting to 256 colors. If you don't want this behavior, check PngQuantWithHistogram.histogramAvailable | ||
if (!histogram) { | ||
return new PngQuant([256]); | ||
this.writable = true; | ||
this.bufferedChunks = []; | ||
} | ||
|
||
Stream.call(this); | ||
this.bufferedChunks = []; | ||
} | ||
|
||
PngQuantWithHistogram.histogramAvailable = !!histogram; | ||
write(chunk) { | ||
this.bufferedChunks.push(chunk); | ||
} | ||
|
||
util.inherits(PngQuantWithHistogram, Stream); | ||
end(chunk) { | ||
if (chunk) { | ||
this.write(chunk); | ||
} | ||
var rawSrc = Buffer.concat(this.bufferedChunks); | ||
|
||
PngQuantWithHistogram.prototype.write = function(chunk) { | ||
this.bufferedChunks.push(chunk); | ||
}; | ||
if (rawSrc.length === 0) { | ||
const err = new Error('PngQuantWithHistogram stream ended with no data'); | ||
|
||
PngQuantWithHistogram.prototype.end = function(chunk) { | ||
if (chunk) { | ||
this.write(chunk); | ||
} | ||
var rawSrc = Buffer.concat(this.bufferedChunks); | ||
this.bufferedChunks = null; | ||
histogram(rawSrc, (err, data) => { | ||
if (err) { | ||
err.message = 'histogram: ' + err.message; | ||
return this.emit('error', err); | ||
} | ||
if (data.colors.rgba < 256) { | ||
// The image has fewer than 256 colors, run rawSrc through a PngQuant filter and proxy its events: | ||
this.pngQuant = new PngQuant([ | ||
data.colors.rgba < 2 ? 2 : data.colors.rgba | ||
]); | ||
this.__defineGetter__('commandLine', () => this.pngQuant.commandLine); | ||
for (const eventName of ['data', 'end', 'error']) { | ||
this.pngQuant.on(eventName, () => { | ||
// ... | ||
this.emit.apply(this, eventName.concat(arguments)); | ||
}); | ||
|
||
this.bufferedChunks = null; | ||
|
||
histogram(rawSrc, (err, data) => { | ||
if (err) { | ||
err.message = 'histogram: ' + err.message; | ||
return this.emit('error', err); | ||
} | ||
this.pngQuant.end(rawSrc); | ||
} else { | ||
// The image has too many colors. Emit all the buffered chunks at once when we aren't paused: | ||
let hasEnded = false; | ||
const emitRawSrcAndEnd = () => { | ||
hasEnded = true; | ||
this.emit('data', rawSrc); | ||
this.emit('end'); | ||
}; | ||
|
||
if (this.isPaused) { | ||
this.resume = function() { | ||
setImmediate(function() { | ||
if (!this.isPaused && !hasEnded) { | ||
emitRawSrcAndEnd(); | ||
} | ||
if (data.colors.rgba < 256) { | ||
// The image has fewer than 256 colors, run rawSrc through a PngQuant filter and proxy its events: | ||
this.pngQuant = new PngQuant([ | ||
data.colors.rgba < 2 ? 2 : data.colors.rgba | ||
]); | ||
this.__defineGetter__('commandLine', () => this.pngQuant.commandLine); | ||
for (const eventName of ['data', 'end', 'error']) { | ||
this.pngQuant.on(eventName, (...args) => { | ||
this.emit(eventName, ...args); | ||
}); | ||
}; | ||
} | ||
this.pngQuant.end(rawSrc); | ||
} else { | ||
emitRawSrcAndEnd(); | ||
// The image has too many colors. Emit all the buffered chunks at once when we aren't paused: | ||
let hasEnded = false; | ||
const emitRawSrcAndEnd = () => { | ||
hasEnded = true; | ||
this.emit('data', rawSrc); | ||
this.emit('end'); | ||
}; | ||
|
||
if (this.isPaused) { | ||
this.resume = function() { | ||
setImmediate(function() { | ||
if (!this.isPaused && !hasEnded) { | ||
emitRawSrcAndEnd(); | ||
} | ||
}); | ||
}; | ||
} else { | ||
emitRawSrcAndEnd(); | ||
} | ||
} | ||
} | ||
}); | ||
}; | ||
}); | ||
} | ||
|
||
PngQuantWithHistogram.prototype.pause = function() { | ||
this.isPaused = true; | ||
if (this.pngQuant) { | ||
this.pngQuant.pause(); | ||
pause() { | ||
this.isPaused = true; | ||
if (this.pngQuant) { | ||
this.pngQuant.pause(); | ||
} | ||
} | ||
}; | ||
|
||
PngQuantWithHistogram.prototype.resume = function() { | ||
this.isPaused = false; | ||
if (this.pngQuant) { | ||
this.pngQuant.resume(); | ||
resume() { | ||
this.isPaused = false; | ||
if (this.pngQuant) { | ||
this.pngQuant.resume(); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
module.exports = PngQuantWithHistogram; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.