Skip to content

Commit

Permalink
add resourceProxy option
Browse files Browse the repository at this point in the history
  • Loading branch information
pofider committed Jun 27, 2023
1 parent ce9236a commit fdc1613
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
10 changes: 6 additions & 4 deletions lib/dedicatedProcessStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 4 additions & 0 deletions lib/scripts/conversionScriptPart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion lib/scripts/serverScriptPart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
3 changes: 2 additions & 1 deletion lib/scripts/standaloneScriptPart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion lib/serverStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 33 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(\'<h1>Hello from Page 1</h1><div style="page-break-before: always;"></div><h1>Hello from Page 2</h1>\')');
})
server.listen(2000, 'localhost', () => {
var cvn = require("../lib/conversion.js")({
timeout: 10000,
tmpDir: tmpDir,
strategy: strategy,
resourceProxy: 'http://localhost:2000/?url='
});

cvn({
html: '<script src="http://myurl.js"></script>'
}, function(err, res) {
server.close()
if (err)
return done(err);

res.numberOfPages.should.be.eql(2);
done();
});
})
}
}
})

function common(strategy) {
Expand Down

0 comments on commit fdc1613

Please sign in to comment.