-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add support for WebP decoding #1280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LinusU
wants to merge
2
commits into
Automattic:master
Choose a base branch
from
LinusU:webp-decoding
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -4,7 +4,6 @@ install: | |
node_js: | ||
- '10' | ||
- '8' | ||
- '6' | ||
addons: | ||
apt: | ||
sources: | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,26 +1,89 @@ | ||
'use strict'; | ||
const fs = require('fs') | ||
const get = require('simple-get') | ||
const webp = require('@cwasm/webp') | ||
|
||
/*! | ||
* Canvas - Image | ||
* Copyright (c) 2010 LearnBoost <[email protected]> | ||
* MIT Licensed | ||
const bindings = require('./bindings') | ||
|
||
const kOriginalSource = Symbol('original-source') | ||
|
||
/** @typedef {Object} Image */ | ||
const Image = module.exports = bindings.Image | ||
|
||
const proto = Image.prototype | ||
const _getSource = proto.getSource | ||
const _setSource = proto.setSource | ||
|
||
delete proto.getSource | ||
delete proto.setSource | ||
|
||
/** | ||
* @param {Image} image | ||
* @param {Error} err | ||
*/ | ||
function signalError (image, err) { | ||
if (typeof image.onerror === 'function') return image.onerror(err) | ||
|
||
throw err | ||
} | ||
|
||
/** | ||
* Module dependencies. | ||
* @param {Image} image | ||
* @param {string} value | ||
*/ | ||
function loadDataUrl (image, value) { | ||
const firstComma = value.indexOf(',') | ||
const isBase64 = value.lastIndexOf('base64', firstComma) !== -1 | ||
const source = value.slice(firstComma + 1) | ||
|
||
const bindings = require('./bindings') | ||
const Image = module.exports = bindings.Image | ||
const http = require("http") | ||
const https = require("https") | ||
let data | ||
try { | ||
data = Buffer.from(source, isBase64 ? 'base64' : 'utf8') | ||
} catch (err) { | ||
return signalError(image, err) | ||
} | ||
|
||
const proto = Image.prototype; | ||
const _getSource = proto.getSource; | ||
const _setSource = proto.setSource; | ||
return setSource(image, data, value) | ||
} | ||
|
||
delete proto.getSource; | ||
delete proto.setSource; | ||
/** | ||
* @param {Image} image | ||
* @param {string} value | ||
*/ | ||
function loadHttpUrl (image, value) { | ||
return get.concat(value, (err, res, data) => { | ||
if (err) return signalError(image, err) | ||
|
||
if (res.statusCode < 200 || res.statusCode >= 300) { | ||
return signalError(image, new Error(`Server responded with ${res.statusCode}`)) | ||
} | ||
|
||
return setSource(image, data, value) | ||
}) | ||
} | ||
|
||
/** | ||
* @param {Image} image | ||
* @param {string} value | ||
*/ | ||
function loadFileUrl (image, value) { | ||
fs.readFile(value.replace('file://', ''), (err, data) => { | ||
if (err) return signalError(image, err) | ||
|
||
setSource(image, data, value) | ||
}) | ||
} | ||
|
||
/** | ||
* @param {Image} image | ||
* @param {string} value | ||
*/ | ||
function loadLocalFile (image, value) { | ||
fs.readFile(value, (err, data) => { | ||
if (err) return signalError(image, err) | ||
|
||
setSource(image, data, value) | ||
}) | ||
} | ||
|
||
Object.defineProperty(Image.prototype, 'src', { | ||
/** | ||
|
@@ -33,49 +96,52 @@ Object.defineProperty(Image.prototype, 'src', { | |
* @param {String|Buffer} val filename, buffer, data URI, URL | ||
* @api public | ||
*/ | ||
set(val) { | ||
if (typeof val === 'string') { | ||
if (/^\s*data:/.test(val)) { // data: URI | ||
const commaI = val.indexOf(',') | ||
// 'base64' must come before the comma | ||
const isBase64 = val.lastIndexOf('base64', commaI) !== -1 | ||
const content = val.slice(commaI + 1) | ||
setSource(this, Buffer.from(content, isBase64 ? 'base64' : 'utf8'), val); | ||
} else if (/^\s*https?:\/\//.test(val)) { // remote URL | ||
const onerror = err => { | ||
if (typeof this.onerror === 'function') { | ||
this.onerror(err) | ||
} else { | ||
throw err | ||
} | ||
} | ||
|
||
const type = /^\s*https:\/\//.test(val) ? https : http | ||
type.get(val, res => { | ||
if (res.statusCode !== 200) { | ||
return onerror(new Error(`Server responded with ${res.statusCode}`)) | ||
} | ||
const buffers = [] | ||
res.on('data', buffer => buffers.push(buffer)) | ||
res.on('end', () => { | ||
setSource(this, Buffer.concat(buffers)); | ||
}) | ||
}).on('error', onerror) | ||
} else { // local file path assumed | ||
setSource(this, val); | ||
} | ||
} else if (Buffer.isBuffer(val)) { | ||
setSource(this, val); | ||
set (val) { | ||
// Clear current source | ||
clearSource(this) | ||
|
||
// Allow directly setting a buffer | ||
if (Buffer.isBuffer(val)) { | ||
this[kOriginalSource] = val | ||
Promise.resolve().then(() => setSource(this, val, val)) | ||
return | ||
} | ||
|
||
// Coerce into string and strip leading & trailing whitespace | ||
val = String(val).trim() | ||
this[kOriginalSource] = val | ||
|
||
// Clear image | ||
if (val === '') { | ||
return | ||
} | ||
|
||
// Data URL | ||
if (/^data:/.test(val)) { | ||
return loadDataUrl(this, val) | ||
} | ||
|
||
// HTTP(S) URL | ||
if (/^https?:\/\//.test(val)) { | ||
return loadHttpUrl(this, val) | ||
} | ||
|
||
// File URL | ||
if (/^file:\/\//.test(val)) { | ||
return loadFileUrl(this, val) | ||
} | ||
|
||
// Assume local file path | ||
loadLocalFile(this, val) | ||
}, | ||
|
||
get() { | ||
// TODO https://github.com/Automattic/node-canvas/issues/118 | ||
return getSource(this); | ||
/** @returns {String|Buffer} */ | ||
get () { | ||
return this[kOriginalSource] || '' | ||
}, | ||
|
||
configurable: true | ||
}); | ||
}) | ||
|
||
/** | ||
* Inspect image. | ||
|
@@ -86,19 +152,35 @@ Object.defineProperty(Image.prototype, 'src', { | |
* @api public | ||
*/ | ||
|
||
Image.prototype.inspect = function(){ | ||
return '[Image' | ||
+ (this.complete ? ':' + this.width + 'x' + this.height : '') | ||
+ (this.src ? ' ' + this.src : '') | ||
+ (this.complete ? ' complete' : '') | ||
+ ']'; | ||
}; | ||
Image.prototype.inspect = function () { | ||
return '[Image' + | ||
(this.complete ? ':' + this.width + 'x' + this.height : '') + | ||
(this.src ? ' ' + this.src : '') + | ||
(this.complete ? ' complete' : '') + | ||
']' | ||
} | ||
|
||
/** | ||
* @param {Buffer} source | ||
*/ | ||
function isWebP (source) { | ||
return (source.toString('ascii', 0, 4) === 'RIFF' && source.toString('ascii', 8, 12) === 'WEBP') | ||
} | ||
|
||
function getSource(img){ | ||
return img._originalSource || _getSource.call(img); | ||
/** | ||
* @param {Image} image | ||
* @param {Buffer} source | ||
* @param {Buffer|string} originalSource | ||
*/ | ||
function setSource (image, source, originalSource) { | ||
if (image[kOriginalSource] === originalSource) { | ||
_setSource.call(image, isWebP(source) ? webp.decode(source) : source) | ||
} | ||
} | ||
|
||
function setSource(img, src, origSrc){ | ||
_setSource.call(img, src); | ||
img._originalSource = origSrc; | ||
/** | ||
* @param {Image} image | ||
*/ | ||
function clearSource (image) { | ||
_setSource.call(image, null) | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LinusU until the memory issue is resolved, do you wanna pull out the nice refactoring you did in this file into a separate PR? Your version is way more readable 😍