Skip to content

Instantly share code, notes, and snippets.

@UKNC
Last active March 5, 2017 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UKNC/887059b4bf6359ced5d1d982399dd32e to your computer and use it in GitHub Desktop.
Save UKNC/887059b4bf6359ced5d1d982399dd32e to your computer and use it in GitHub Desktop.
Showcase for unsupported gzip decompression in phantom-nodejs
{
"name": "phantomgzip",
"version": "1.0.0",
"description": "Showcase of inability to support gzip",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "UKNC",
"license": "ISC",
"dependencies": {
"bluebird": "^3.5.0",
"phantom": "^4.0.0"
}
}
~
/*
Run as
> node server.js 0
to see that pages are plain HTML
and
> node server.js 1
to see that GZIPPed htmls are not decompressed.
*/
const phantom = require('phantom');
/* jslint ignore: start */
const Promise = require('bluebird');
/* jslink ignore: end */
let url = "http://www.google.com";
let gzip = process.argv[2] || 0;
let phInst, page;
phantom.create()
.then( inst => {
phInst = inst;
return phInst.createPage();
})
.then( p=>{
page = p;
return page.property("customHeaders", { "Accept-Encoding": `identity;q=1.0, gzip;q=${gzip}` });
})
.then( ()=>{
return page.property("onResourceRequested", function( req, net ) {
console.log( "REQ: " + req.method + " " + req.url + " HEADERS: " + JSON.stringify( req.headers) );
});
})
.then( ()=>{
return page.property("onResourceReceived", function( res ) {
console.log("RES: " + "HEADERS: " + JSON.stringify( res.headers ) ) ;
});
})
.then( ()=>{
return page.property("onResourceError", function( resourceError ) {
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
});
})
.then( ()=>{
return page.property("onConsoleMessage", function( msg ) {
console.log('onConsoleMessage:' + msg );
});
})
.then( ()=>{
return page.open( url, function(status) {
console.log("Page load status: " + status );
});
})
.then( ()=>{
return Promise.delay(1000);
})
.then( ()=>{
return page.property("content");
})
.then( content=>{
console.log(content);
})
.catch( err=>{
console.log('Error:' + err );
phInst.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment