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];
},
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() {
// Produces helpers to e.g. check for both 'canvas' and
// 'data-paper-canvas' attributes:

View file

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

View file

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

View file

@ -18,10 +18,9 @@ var jsdom = require('jsdom');
// Create our document and window objects through jsdom.
/* global document:true, window:true */
var document = jsdom.jsdom('<html><body></body></html>', {
// Give the resulting document the same origins as the XMLHttpRequest
// objects for local files, so files can be loaded locally without CORS.
// TODO: Find a proper solution instead of this hack.
url: 'file://',
// Use the current working directory as the document's origin, so
// requests to local files work correctly with CORS.
url: 'file://' + process.cwd() + '/',
features: {
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);
document.body.removeChild(img);
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);
document.body.removeChild(img);
done();
},
error: function() {
pushFailure('Loading from a valid data URL should not give an error.');
done();
}
});
});