diff --git a/lib/dedicatedProcessStrategy.js b/lib/dedicatedProcessStrategy.js index 83674d8..6203ac2 100644 --- a/lib/dedicatedProcessStrategy.js +++ b/lib/dedicatedProcessStrategy.js @@ -4,7 +4,7 @@ var path = require("path"), objectAssign = require('object-assign'); -module.exports = function(options, requestOptions, id, cb) { +module.exports = function(options, requestOptions, id, cb) { if (!options.phantomPath) { options.phantomPath = require('phantomjs').path; } @@ -37,9 +37,11 @@ module.exports = function(options, requestOptions, id, cb) { childArgs.push(settingsFilePath); var childOptions = { - env: objectAssign({}, process.env, { 'PHANTON_MAX_LOG_ENTRY_SIZE': options.maxLogEntrySize || 1000 }) - } - + env: objectAssign({}, process.env, { + 'PHANTOM_MAX_LOG_ENTRY_SIZE': options.maxLogEntrySize || 1000, + 'PHANTOM_RESOURCE_PROXY': options.resourceProxy + }) + } var isDone = false; diff --git a/lib/scripts/conversionScriptPart.js b/lib/scripts/conversionScriptPart.js index daa6498..adb0eb5 100644 --- a/lib/scripts/conversionScriptPart.js +++ b/lib/scripts/conversionScriptPart.js @@ -38,6 +38,10 @@ page.onResourceRequested = function (request, networkRequest) { if (body.waitForJS && request.url.lastIndexOf("http://intruct-javascript-ending", 0) === 0) { pageJSisDone = true; } + + if (resourceProxy) { + networkRequest.changeUrl(resourceProxy + encodeURI(request.url)); + } }; page.onConsoleMessage = function(msg, line, source) { diff --git a/lib/scripts/serverScriptPart.js b/lib/scripts/serverScriptPart.js index 27412bf..38f13d0 100644 --- a/lib/scripts/serverScriptPart.js +++ b/lib/scripts/serverScriptPart.js @@ -11,7 +11,8 @@ var system = require('system'); var fs = require('fs'); var port = require("system").env['PHANTOM_WORKER_PORT']; var host = require("system").env['PHANTOM_WORKER_HOST']; -var maxLogEntrySize = require("system").env['PHANTON_MAX_LOG_ENTRY_SIZE'] +var maxLogEntrySize = require("system").env['PHANTOM_MAX_LOG_ENTRY_SIZE']; +var resourceProxy = require("system").env['PHANTOM_RESOURCE_PROXY']; var page = webpage.create(); var service = webserver.listen(host + ':' + port, function (req, res){ diff --git a/lib/scripts/standaloneScriptPart.js b/lib/scripts/standaloneScriptPart.js index e726713..3ccccce 100644 --- a/lib/scripts/standaloneScriptPart.js +++ b/lib/scripts/standaloneScriptPart.js @@ -3,7 +3,8 @@ */ /* globals phantom */ -var maxLogEntrySize = require("system").env['PHANTON_MAX_LOG_ENTRY_SIZE'] +var maxLogEntrySize = require("system").env['PHANTOM_MAX_LOG_ENTRY_SIZE'] +var resourceProxy = require("system").env['PHANTOM_RESOURCE_PROXY']; $log try { diff --git a/lib/serverStrategy.js b/lib/serverStrategy.js index 505b2d5..9c5f3bf 100644 --- a/lib/serverStrategy.js +++ b/lib/serverStrategy.js @@ -29,7 +29,8 @@ module.exports = function(options, requestOptions, id, cb) { if (!phantoms[phantomInstanceId]) { var opts = _.extend({}, options); opts.workerEnv = { - 'PHANTON_MAX_LOG_ENTRY_SIZE': options.maxLogEntrySize || 1000 + 'PHANTOM_MAX_LOG_ENTRY_SIZE': options.maxLogEntrySize || 1000, + 'PHANTOM_RESOURCE_PROXY': options.resourceProxy } opts.phantomPath = requestOptions.phantomPath || options.phantomPath; phantoms[phantomInstanceId] = Phantom(opts); diff --git a/test/test.js b/test/test.js index 6d2e288..a737c4d 100644 --- a/test/test.js +++ b/test/test.js @@ -4,6 +4,7 @@ var should = require("should"), phantomjs = require("phantomjs"), phantomjs2 = require("phantomjs-prebuilt") tmpDir = path.join(__dirname, "temp"), + http = require('http'), conversion = require("../lib/conversion.js")({ timeout: 10000, tmpDir: tmpDir, @@ -47,6 +48,38 @@ describe("phantom html to pdf", function () { }); } } + + it('should be able to route requests to resourceProxy in dedicated-process', testResourceProxy('dedicated-process')); + it('should be able to route requests to resourceProxy in phantom-server', testResourceProxy('phantom-server')); + + function testResourceProxy(strategy) { + return function(done) { + conversion.kill(); + const server = http.createServer((req, res) => { + res.writeHead(200); + res.end('document.write(\'