Skip to content

Commit

Permalink
NetworkAgent: support data scheme URLs
Browse files Browse the repository at this point in the history
Implemented support for data scheme URLs. This allows sourcemap files to be
embedded in the sourceMappingURL in the generated js file.
  • Loading branch information
Miroslav Bajtos committed Sep 12, 2013
1 parent 405dd3d commit 14d54b1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
31 changes: 24 additions & 7 deletions lib/NetworkAgent.js
@@ -1,11 +1,28 @@
var fs = require('fs');
var path = require('path');
var dataUri = require('strong-data-uri');

function NetworkAgent() {
}

NetworkAgent.prototype.loadResourceForFrontend = function(params, done) {
var uri
if (/^data:/.test(params.url)) {
try {
done(null, {
statusCode: 200,
headers: {},
content: dataUri.decode(params.url).toString('ascii')
});
} catch (err) {
done(err);
}
return;
}

loadFileResource(params, done);
}

function loadFileResource(params, done) {
var match = params.url.match(/^file:\/\/(.*)$/);
if (!match) {
return done(
Expand All @@ -16,13 +33,13 @@ NetworkAgent.prototype.loadResourceForFrontend = function(params, done) {
var filePath = match[1];

if (process.platform == 'win32') {
// On Windows, we should receive '/C:/path/to/file'.
if (!/^\/[a-zA-Z]:\//.test(filePath)) {
return done('Invalid windows path: ' + filePath);
}
// On Windows, we should receive '/C:/path/to/file'.
if (!/^\/[a-zA-Z]:\//.test(filePath)) {
return done('Invalid windows path: ' + filePath);
}

// Remove leading '/' and replace all other '/' with '\'
filePath = filePath.slice(1).split('/').join(path.sep);
// Remove leading '/' and replace all other '/' with '\'
filePath = filePath.slice(1).split('/').join(path.sep);
}

// ensure there are no ".." in the path
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -25,7 +25,8 @@
"express": "~3.4",
"async": "~0.2.8",
"glob": "~3.2.1",
"rc": "~0.3.0"
"rc": "~0.3.0",
"strong-data-uri": "~0.1.0"
},
"devDependencies": {
"mocha": "latest",
Expand Down
21 changes: 21 additions & 0 deletions test/NetworkAgent.js
@@ -0,0 +1,21 @@
var expect = require('chai').expect,
NetworkAgent = require('../lib/NetworkAgent.js').NetworkAgent;

describe('NetworkAgent', function() {
describe('loadResourceForFrontend', function() {
it('should load data URLs', function(done) {
var agent = new NetworkAgent();
agent.loadResourceForFrontend(
{
url: 'data:text/plain;base64,aGVsbG8gd29ybGQ='
},
function(err, result) {
if (err) return done(err);
expect(result.content).to.equal('hello world');
done();
}
);
});
});
})

0 comments on commit 14d54b1

Please sign in to comment.