canvas-to-buffer
Advanced tools
Comparing version 1.0.9 to 1.0.10
357
index.js
@@ -1,229 +0,234 @@ | ||
var toBuffer = require('typedarray-to-buffer'), | ||
isBrowser = typeof(document) !== 'undefined' && typeof(document.createElement) === 'function', | ||
var toBuffer = require('typedarray-to-buffer') | ||
var atob = require('atob') | ||
var isBrowser = typeof (document) !== 'undefined' && typeof (document.createElement) === 'function' | ||
// cached, used only once for browser environments | ||
verifiedImageType | ||
// cached, used only once for browser environments | ||
var verifiedImageType | ||
module.exports = function(canvas, options) { | ||
module.exports = function (canvas, options) { | ||
var self = this | ||
var quality | ||
var self = this, quality | ||
options = options || {} | ||
options.image = options.image ? options.image : {} | ||
options.image.types = options.image.types ? options.image.types : [] | ||
options = options ? options : {} | ||
options.image = options.image ? options.image : {} | ||
options.image.types = options.image.types ? options.image.types : [] | ||
// validate some options this class needs | ||
if (options.image.types.length > 2) { | ||
throw new Error('Too many image types are specified!') | ||
} else if (options.image.types.length < 1) { | ||
// Set a default image type, just to be robust | ||
options.image.types = isBrowser ? ['webp', 'jpeg'] : ['png'] | ||
} | ||
// validate some options this class needs | ||
if (options.image.types.length > 2) | ||
throw new Error('Too many image types are specified!') | ||
if (!options.image.quality) { | ||
options.image.quality = 0.5 // default | ||
} | ||
else if (options.image.types.length < 1) { | ||
quality = parseFloat(options.image.quality) | ||
// Set a default image type, just to be robust | ||
options.image.types = isBrowser ? ['webp', 'jpeg'] : ['png'] | ||
function composeImageType (index) { | ||
var imageType | ||
if (options.image.types[index]) { | ||
imageType = 'image/' + options.image.types[index] | ||
} | ||
if (!options.image.quality) | ||
options.image.quality = .5 // default | ||
return imageType | ||
} | ||
quality = parseFloat(options.image.quality) | ||
function isMatch (uri, imageType) { | ||
var match = uri && uri.match(imageType) | ||
function composeImageType(index) { | ||
var imageType | ||
match && options.debug && options.debug('Image type %s verified', imageType) | ||
if (options.image.types[index]) | ||
imageType = 'image/' + options.image.types[index] | ||
return match | ||
} | ||
return imageType | ||
} | ||
// Performance tweak, we do not need a big canvas for finding out the supported image type | ||
function getTestCanvas () { | ||
var testCanvas | ||
function isMatch(uri, imageType) { | ||
var match = uri && uri.match(imageType) | ||
match && options.debug && options.debug('Image type %s verified', imageType) | ||
return match | ||
if (isBrowser) { | ||
testCanvas = document.createElement('canvas') | ||
testCanvas.width = testCanvas.height = 1 | ||
} else { | ||
testCanvas = canvas | ||
} | ||
// Performance tweak, we do not need a big canvas for finding out the supported image type | ||
function getTestCanvas() { | ||
return testCanvas | ||
} | ||
var testCanvas | ||
if (isBrowser) { | ||
testCanvas = document.createElement('canvas') | ||
testCanvas.width = testCanvas.height = 1 | ||
} else | ||
testCanvas = canvas | ||
return testCanvas | ||
} | ||
function canvasSupportsImageTypeAsync(imageType, cb) { | ||
try { | ||
getTestCanvas().toDataURL(imageType, function(err, uri) { | ||
if (err) | ||
cb(err) | ||
else | ||
cb(null, isMatch(uri, imageType)) | ||
}) | ||
} catch (exc) { | ||
cb(null, false) | ||
function canvasSupportsImageTypeAsync (imageType, cb) { | ||
try { | ||
getTestCanvas().toDataURL(imageType, function (err, uri) { | ||
if (err) { | ||
cb(err) | ||
} else { | ||
cb(null, isMatch(uri, imageType)) | ||
} | ||
}) | ||
} catch (exc) { | ||
cb(null, false) | ||
} | ||
} | ||
function canvasSupportsImageTypeSync(imageType) { | ||
var match | ||
function canvasSupportsImageTypeSync (imageType) { | ||
var match | ||
try { | ||
var testCanvas = getTestCanvas(), | ||
uri = testCanvas.toDataURL && testCanvas.toDataURL(imageType) | ||
try { | ||
var testCanvas = getTestCanvas() | ||
var uri = testCanvas.toDataURL && testCanvas.toDataURL(imageType) | ||
match = isMatch(uri, imageType) | ||
} catch (exc) { | ||
// Can happen when i.E. a spider is coming. Just be robust here and continue. | ||
options.debug && | ||
options.logger.debug('Failed to call toDataURL() on canvas for image type %s', imageType) | ||
} | ||
return match | ||
match = isMatch(uri, imageType) | ||
} catch (exc) { | ||
// Can happen when i.E. a spider is coming. Just be robust here and continue. | ||
options.debug && | ||
options.logger.debug('Failed to call toDataURL() on canvas for image type %s', imageType) | ||
} | ||
function verifyImageTypeAsync(imageType, cb) { | ||
canvasSupportsImageTypeAsync(imageType, function(err, match) { | ||
if (err) | ||
cb(err) | ||
else { | ||
return match | ||
} | ||
if (match) | ||
cb(null, imageType) | ||
else { | ||
imageType = composeImageType(1) | ||
function verifyImageTypeAsync (imageType, cb) { | ||
canvasSupportsImageTypeAsync(imageType, function (err, match) { | ||
if (err) { | ||
cb(err) | ||
} else { | ||
if (match) { | ||
cb(null, imageType) | ||
} else { | ||
imageType = composeImageType(1) | ||
canvasSupportsImageTypeAsync(imageType, function(err, match) { | ||
if (err) | ||
cb(err) | ||
else | ||
cb(null, match ? imageType: null) | ||
}) | ||
} | ||
canvasSupportsImageTypeAsync(imageType, function (err, match) { | ||
if (err) { | ||
cb(err) | ||
} else { | ||
cb(null, match ? imageType : null) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
function verifyImageTypeSync(imageType) { | ||
function verifyImageTypeSync (imageType) { | ||
if (!canvasSupportsImageTypeSync(imageType)) { | ||
if (options.image.types[1]) { | ||
imageType = composeImageType(1) | ||
if (!canvasSupportsImageTypeSync(imageType)) { | ||
imageType = null | ||
} | ||
} else { | ||
imageType = null | ||
} | ||
} | ||
if (options.image.types[1]) { | ||
imageType = composeImageType(1) | ||
!imageType && options.debug && options.logger.debug('Unable to verify image type') | ||
if (!canvasSupportsImageTypeSync(imageType)) | ||
imageType = null | ||
} else | ||
imageType = null | ||
} | ||
return imageType | ||
} | ||
!imageType && options.debug && options.logger.debug('Unable to verify image type') | ||
// callbacks are needed for server side tests | ||
function verifyImageType (cb) { | ||
var imageType = composeImageType(0) | ||
return imageType | ||
if (cb) { | ||
verifyImageTypeAsync(imageType, cb) | ||
} else { | ||
return verifyImageTypeSync(imageType) | ||
} | ||
} | ||
// callbacks are needed for server side tests | ||
function verifyImageType(cb) { | ||
var imageType = composeImageType(0) | ||
// this method is proven to be fast, see | ||
// http://jsperf.com/data-uri-to-buffer-performance/3 | ||
function uriToBuffer (uri) { | ||
var uriSplitted = uri.split(',')[1] | ||
var bytes | ||
if (cb) { | ||
verifyImageTypeAsync(imageType, cb) | ||
} else { | ||
return verifyImageTypeSync(imageType) | ||
} | ||
// Beware that the atob function might be a static one for server side tests | ||
if (typeof (atob) === 'function') { | ||
bytes = atob(uriSplitted) | ||
} else if (typeof (self.constructor.atob) === 'function') { | ||
bytes = self.constructor.atob(uriSplitted) | ||
} else { | ||
throw new Error('atob function is missing') | ||
} | ||
// this method is proven to be fast, see | ||
// http://jsperf.com/data-uri-to-buffer-performance/3 | ||
function uriToBuffer(uri) { | ||
var arr = new Uint8Array(bytes.length) | ||
var uri = uri.split(',')[1], | ||
bytes | ||
// http://mrale.ph/blog/2014/12/24/array-length-caching.html | ||
for (var i = 0, l = bytes.length; i < l; i++) { | ||
arr[i] = bytes.charCodeAt(i) | ||
} | ||
// Beware that the atob function might be a static one for server side tests | ||
if (typeof(atob) === 'function') | ||
bytes = atob(uri) | ||
else if (typeof(self.constructor.atob) === 'function') | ||
bytes = self.constructor.atob(uri) | ||
else | ||
throw new Error('atob function is missing') | ||
return toBuffer(arr) | ||
} | ||
var arr = new Uint8Array(bytes.length) | ||
function toBufferSync () { | ||
var imageType = self.getImageType() | ||
var buffer | ||
// http://mrale.ph/blog/2014/12/24/array-length-caching.html | ||
for (var i = 0, l = bytes.length; i < l; i++) { | ||
arr[i] = bytes.charCodeAt(i) | ||
} | ||
return toBuffer(arr) | ||
if (imageType) { | ||
var uri = canvas.toDataURL(imageType, quality) | ||
buffer = uriToBuffer(uri) | ||
} | ||
function toBufferSync() { | ||
var imageType = self.getImageType(), | ||
buffer | ||
return buffer | ||
} | ||
if (imageType) { | ||
var uri = canvas.toDataURL(imageType, quality) | ||
buffer = uriToBuffer(uri) | ||
} | ||
return buffer | ||
} | ||
function toBufferAsync(cb) { | ||
self.getImageType(function(err, imageType) { | ||
if (err) | ||
cb(err) | ||
else if (!imageType) | ||
cb() | ||
else | ||
canvas.toDataURL(imageType, function(err, uri) { | ||
if (err) | ||
cb(err) | ||
else | ||
cb(null, uriToBuffer(uri)) | ||
}) | ||
function toBufferAsync (cb) { | ||
self.getImageType(function (err, imageType) { | ||
if (err) { | ||
cb(err) | ||
} else if (!imageType) { | ||
cb() | ||
} else { | ||
canvas.toDataURL(imageType, function (err, uri) { | ||
if (err) { | ||
cb(err) | ||
} else { | ||
cb(null, uriToBuffer(uri)) | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
this.toBuffer = function(cb) { | ||
if (cb) | ||
toBufferAsync(cb) | ||
else | ||
return toBufferSync() | ||
this.toBuffer = function (cb) { | ||
if (cb) { | ||
toBufferAsync(cb) | ||
} else { | ||
return toBufferSync() | ||
} | ||
} | ||
// browsers do not need a callback, but tests do | ||
this.getImageType = function(cb) { | ||
// browsers do not need a callback, but tests do | ||
this.getImageType = function (cb) { | ||
// only run for the first time this constructor is called and | ||
// cache result for the next calls | ||
// only run for the first time this constructor is called and | ||
// cache result for the next calls | ||
if (cb) { | ||
if (!verifiedImageType || !isBrowser) { | ||
verifyImageType(function (err, newVerifiedImageType) { | ||
if (err) { | ||
cb(err) | ||
} else { | ||
verifiedImageType = newVerifiedImageType | ||
cb(null, verifiedImageType) | ||
} | ||
}) | ||
} else { | ||
cb(null, verifiedImageType) | ||
} | ||
} else { | ||
// on the browser side we do cache it for speed | ||
if (!verifiedImageType || !isBrowser) { | ||
verifiedImageType = verifyImageType() | ||
} | ||
if (cb) { | ||
if (!verifiedImageType || !isBrowser) | ||
verifyImageType(function(err, newVerifiedImageType) { | ||
if (err) | ||
cb(err) | ||
else { | ||
verifiedImageType = newVerifiedImageType | ||
cb(null, verifiedImageType) | ||
} | ||
}) | ||
else | ||
cb(null, verifiedImageType) | ||
} else { | ||
// on the browser side we do cache it for speed | ||
if (!verifiedImageType || !isBrowser) | ||
verifiedImageType = verifyImageType() | ||
return verifiedImageType | ||
} | ||
return verifiedImageType | ||
} | ||
} | ||
} |
{ | ||
"name": "canvas-to-buffer", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "Converts a Canvas graphic into a Buffer, as fast as possible and without a copy.", | ||
@@ -27,10 +27,10 @@ "main": "index.js", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"atob": "2.0.0", | ||
"canvas": "1.3.12", | ||
"tape": "4.5.1" | ||
}, | ||
"dependencies": { | ||
"atob": "2.0.3", | ||
"typedarray-to-buffer": "3.1.2" | ||
}, | ||
"devDependencies": { | ||
"canvas": "1.6.7", | ||
"tape": "4.8.0" | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
21735
2
2
6
193
1
+ Addedatob@2.0.3
+ Addedatob@2.0.3(transitive)