-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added jsdom env #79
Added jsdom env #79
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
module.exports = { | ||
Environment: require('./lib/Environment'), | ||
PhantomEnvironment: require('./lib/PhantomEnvironment'), | ||
JsDOMEnvironment: require('./lib/JsDOMEnvironment'), | ||
|
||
Parser: require('./lib/Parser') | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
const Environment = require('./Environment'), | ||
debug = require('debug')('JsDOMEnvironment'), | ||
_ = require('lodash'), | ||
jsdom = require('jsdom'), | ||
path = require('path'), | ||
vow = require('vow'), | ||
vm = require('vm'); | ||
|
||
const defaultOptions = { | ||
// Custom environment options | ||
snapshot: false, | ||
snapshotDir: 'snapshots', | ||
proxy: null, | ||
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12', | ||
}; | ||
|
||
/** | ||
* @param {object} options | ||
* @constructor | ||
*/ | ||
function JsDOMEnvironment(options) { | ||
debug('Initializing...'); | ||
this._options = _.defaults(_.clone(options) || {}, defaultOptions); | ||
this._proxy = this._options.proxy; | ||
this._url = options.url; | ||
|
||
if (!this._url) { | ||
throw new Error('You must pass `url` to JsDOMEnvironment'); | ||
} | ||
this._window = null; | ||
} | ||
|
||
JsDOMEnvironment.prototype = _.create(Environment.prototype, /**@lends JsDOMEnvironment*/{ | ||
|
||
/** | ||
* Prepare environment | ||
* @returns {Promise} | ||
*/ | ||
prepare() { | ||
debug('Preparing...'); | ||
const deferred = vow.defer(); | ||
|
||
const params = { | ||
url: this._url, | ||
scripts: ['file:' + path.resolve(__dirname, '../vendor/sizzle.min.js')], | ||
done: (error, window) => { | ||
debug('Page is initialized in JsDom'); | ||
if (error) { | ||
return deferred.reject(error); | ||
} | ||
this._window = window; | ||
deferred.resolve(); | ||
} | ||
}; | ||
params.userAgent = this.getUserAgent(); | ||
const proxy = this.getProxy(); | ||
if (proxy) { | ||
params.proxy = proxy; | ||
} | ||
|
||
jsdom.env(params); | ||
return deferred.promise(); | ||
}, | ||
|
||
/** | ||
* Tear down environment | ||
* @returns {Promise} | ||
*/ | ||
tearDown() { | ||
debug('Tear down...'); | ||
if (this._window) { | ||
this._window.close(); | ||
} | ||
return Promise.resolve(); | ||
}, | ||
|
||
/** | ||
* EvaluateJs in the environment | ||
* @returns {Promise} | ||
*/ | ||
evaluateJs(...args) { | ||
debug('.evaluateJs() has called'); | ||
|
||
let evalFunc = args.pop(); | ||
if (typeof evalFunc !== 'function') { | ||
throw new Error('You must pass function as last argument to JsDOMEnvironment.evaluateJs'); | ||
} | ||
|
||
const sandbox = { | ||
window: this._window, | ||
document: this._window.document, | ||
Sizzle: this._window.Sizzle, | ||
args, | ||
result: null | ||
}; | ||
vm.createContext(sandbox); | ||
evalFunc = String(evalFunc); | ||
vm.runInContext(`const fn = ${evalFunc}; result = fn(...args);`, sandbox); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, ok, ignore my comments, of course.. of course.. )) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. damn it. will fix it now |
||
|
||
return Promise.resolve(sandbox.result); | ||
}, | ||
|
||
/** | ||
* @return {string|null} | ||
* @private | ||
*/ | ||
getProxy() { | ||
if (!this._proxy) { | ||
return null; | ||
} | ||
|
||
let proxy = ''; | ||
if (this._proxy.username) { | ||
proxy += this._proxy.username; | ||
} | ||
if (this._proxy.password) { | ||
proxy += `:${this._proxy.password}`; | ||
} | ||
if (proxy) { | ||
proxy += '@'; | ||
} | ||
proxy += `${this._proxy.host}:${this._proxy.port}`; | ||
|
||
return proxy; | ||
}, | ||
|
||
/** | ||
* @returns {string} | ||
* @private | ||
*/ | ||
getUserAgent() { | ||
let userAgent = this._options.userAgent; | ||
if (Array.isArray(userAgent)) { | ||
userAgent = _.sample(this._options.userAgent); | ||
} | ||
return userAgent; | ||
}, | ||
}); | ||
|
||
module.exports = JsDOMEnvironment; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just