From 14d54b19eeb61e3f81c345376aec782b63dadab8 Mon Sep 17 00:00:00 2001 From: Miroslav Bajtos Date: Thu, 12 Sep 2013 11:06:30 +0100 Subject: [PATCH] NetworkAgent: support data scheme URLs Implemented support for data scheme URLs. This allows sourcemap files to be embedded in the sourceMappingURL in the generated js file. --- lib/NetworkAgent.js | 31 ++++++++++++++++++++++++------- package.json | 3 ++- test/NetworkAgent.js | 21 +++++++++++++++++++++ 3 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 test/NetworkAgent.js diff --git a/lib/NetworkAgent.js b/lib/NetworkAgent.js index 6a08bd9a..14e108b3 100644 --- a/lib/NetworkAgent.js +++ b/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( @@ -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 diff --git a/package.json b/package.json index 3a69b19d..40825f73 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/NetworkAgent.js b/test/NetworkAgent.js new file mode 100644 index 00000000..c69804a4 --- /dev/null +++ b/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(); + } + ); + }); + }); +}) +