Improve handling of local paths and CORS on Node.js

This commit is contained in:
Jürg Lehni 2016-04-05 12:11:54 -07:00
parent 093227be7e
commit 5a9f3dfb27
5 changed files with 13 additions and 13 deletions

View file

@ -292,12 +292,6 @@ var PaperScope = Base.extend(/** @lends PaperScope# */{
delete PaperScope._scopes[this._id]; delete PaperScope._scopes[this._id];
}, },
resolvePath: function(url) {
// On Node.js, resolve relative URLs to local files:
return url && this.agent.node && !/^[a-z]+:/i.test(url)
? 'file://' + require('path').resolve(url) : url;
},
statics: new function() { statics: new function() {
// Produces helpers to e.g. check for both 'canvas' and // Produces helpers to e.g. check for both 'canvas' and
// 'data-paper-canvas' attributes: // 'data-paper-canvas' attributes:

View file

@ -371,7 +371,7 @@ var Raster = Item.extend(/** @lends Raster# */{
setSource: function(src) { setSource: function(src) {
var image = new window.Image(), var image = new window.Image(),
crossOrigin = this._crossOrigin; crossOrigin = this._crossOrigin;
image.src = paper.resolvePath(src); image.src = src;
if (crossOrigin) if (crossOrigin)
image.crossOrigin = crossOrigin; image.crossOrigin = crossOrigin;
this.setImage(image); this.setImage(image);

View file

@ -14,8 +14,7 @@ var Http = {
request: function(options) { request: function(options) {
// Code borrowed from Coffee Script and extended: // Code borrowed from Coffee Script and extended:
var xhr = new window.XMLHttpRequest(); var xhr = new window.XMLHttpRequest();
xhr.open((options.method || 'get').toUpperCase(), xhr.open((options.method || 'get').toUpperCase(), options.url,
paper.resolvePath(options.url),
Base.pick(options.async, true)); Base.pick(options.async, true));
if (options.mimeType) if (options.mimeType)
xhr.overrideMimeType(options.mimeType); xhr.overrideMimeType(options.mimeType);

View file

@ -18,10 +18,9 @@ var jsdom = require('jsdom');
// Create our document and window objects through jsdom. // Create our document and window objects through jsdom.
/* global document:true, window:true */ /* global document:true, window:true */
var document = jsdom.jsdom('<html><body></body></html>', { var document = jsdom.jsdom('<html><body></body></html>', {
// Give the resulting document the same origins as the XMLHttpRequest // Use the current working directory as the document's origin, so
// objects for local files, so files can be loaded locally without CORS. // requests to local files work correctly with CORS.
// TODO: Find a proper solution instead of this hack. url: 'file://' + process.cwd() + '/',
url: 'file://',
features: { features: {
FetchExternalResources: ['img', 'script'] FetchExternalResources: ['img', 'script']
} }

View file

@ -60,6 +60,10 @@ test('Create a raster from a dom image', function(assert) {
equals(raster.size, new Size(146, 146), true); equals(raster.size, new Size(146, 146), true);
document.body.removeChild(img); document.body.removeChild(img);
done(); done();
},
error: function() {
pushFailure('Loading from a valid data URL should not give an error.');
done();
} }
}); });
}); });
@ -82,6 +86,10 @@ test('Create a raster from a dom id', function(assert) {
equals(raster.size, new Size(146, 146), true); equals(raster.size, new Size(146, 146), true);
document.body.removeChild(img); document.body.removeChild(img);
done(); done();
},
error: function() {
pushFailure('Loading from a valid data URL should not give an error.');
done();
} }
}); });
}); });