underpainting
Advanced tools
Comparing version 2.0.2 to 3.0.1
61
index.js
var http = require('http') | ||
var qs = require('querystring') | ||
var Tab = require('chrome-tab') | ||
var Worker = require('./worker') | ||
@@ -10,2 +12,9 @@ | ||
if (process.env.CHROME_OWNER !== 'false') { | ||
Tab.list((err, tabs) => { | ||
if (err) throw err | ||
tabs.forEach(tab => tab.close()) | ||
}) | ||
} | ||
var server = http.createServer((req, res) => { | ||
@@ -15,21 +24,39 @@ if (req.method !== 'GET') { | ||
return res.end('GET only') | ||
} else if (!/^\/http/.test(req.url)) { | ||
res.statusCode = 400 | ||
return res.end('pathname must contain an HTTP url') | ||
} | ||
var url = req.url.slice(1).replace(/_escaped_fragment_[^&]*/, '') | ||
var readyCheck = url.match(/_ready_check_=([^&]*)/) | ||
if (readyCheck) { | ||
readyCheck = Buffer(readyCheck[1], 'base64').toString() | ||
url = url.replace(/_ready_check_=[^&]*[&]?/, '') | ||
} else { | ||
readyCheck = titleCheck | ||
var url = req.url.slice(1) | ||
var readyCheck = titleCheck | ||
var readyCheckInterval = 100 | ||
var stripJs = false | ||
var params = url.split('?') | ||
if (params.length > 1) { | ||
url = params[0] | ||
params = qs.parse(params[1]) | ||
if (params._ready_check_) { | ||
readyCheck = Buffer(params._ready_check_, 'hex').toString('utf8') | ||
} | ||
if (params._ready_check_interval_) { | ||
readyCheckInterval = parseInt(params._ready_check_interval_, 10) | ||
if (isNaN(readyCheckInterval)) { | ||
readyCheckInterval = 100 | ||
} | ||
} | ||
if (params._strip_js_ !== undefined) { | ||
stripJs = true | ||
} | ||
delete params._escaped_fragment_ | ||
delete params._ready_check_ | ||
delete params._ready_check_interval_ | ||
delete params._strip_js_ | ||
if (Object.keys(params).length) { | ||
url = `${url}?${qs.stringify(params)}` | ||
} | ||
} | ||
requests.push({ | ||
res: res, | ||
url: url, | ||
readyCheck: readyCheck | ||
res, | ||
url, | ||
readyCheck, | ||
readyCheckInterval, | ||
stripJs | ||
}) | ||
@@ -53,3 +80,4 @@ | ||
host: process.env.CHROME_HOST, | ||
port: process.env.CHROME_PORT | ||
port: process.env.CHROME_PORT, | ||
timeout: parseInt(process.env.TIMEOUT) | ||
}) | ||
@@ -74,3 +102,3 @@ worker.open(err => { | ||
var req = requests.shift() | ||
worker.loadUrl(req.url, req.readyCheck, err => { | ||
worker.loadUrl(req, err => { | ||
if (err) { | ||
@@ -104,2 +132,3 @@ if (err.message === 'not opened') { | ||
req.res.end(html) | ||
worker.call('Page.navigate', { url: 'about:blank' }, err => {}) | ||
available.push(worker) | ||
@@ -106,0 +135,0 @@ } |
{ | ||
"name": "underpainting", | ||
"version": "2.0.2", | ||
"version": "3.0.1", | ||
"description": "Render HTML on the server that's supposed to be rendered on the client", | ||
"scripts": { | ||
"start": "node index.js" | ||
"start": ". ./env.sh && node index.js" | ||
}, | ||
"dependencies": { | ||
"chrome-tab": "^1.0.2" | ||
"chrome-tab": "^1.1.0" | ||
}, | ||
@@ -11,0 +11,0 @@ "repository": "https://github.com/jessetane/underpainting", |
@@ -10,3 +10,6 @@ # underpainting | ||
## How | ||
BYO Chromium, talk to it using the [remote debugging protocol](https://developer.chrome.com/devtools/docs/debugger-protocol). Optionally pass a custom "ready check" expression - defaults to `document.querySelector('title').textContent`. You can pass it in the querystring, base64 encoded under the key `_ready_check_`. | ||
BYO Chromium, talk to it using the [remote debugging protocol](https://developer.chrome.com/devtools/docs/debugger-protocol). | ||
* Optionally pass a custom `_ready_check_` expression by passing it in the querystring, hex encoded. Defaults to `document.querySelector('title').textContent`. | ||
* Optionally pass a custom `_ready_check_interval_` to indicate how frequently the `_ready_check_` should be tested. Specified in milliseconds. Defaults to `100`. | ||
* Optionally pass `_strip_js_` to indicate you would like all script tags removed from responses. Defaults to `false`. | ||
@@ -24,12 +27,12 @@ ## Example | ||
#### `CHROME_{HOST,PORT}` | ||
Defaults to localhost:9222. | ||
Defaults are `localhost` and `9222` respectively. | ||
#### `CHROME_OWNER` | ||
Defaults to true (but can be set to `'false'`) and implies that any existing tabs should be closed at start up. | ||
#### `MAX_WORKERS` | ||
You probably want to limit the number of tabs you have open at any given time depending on the resources you have available. Defaults to 5. | ||
You probably want to limit the number of tabs you have open at any given time depending on the resources you have available. Defaults to `5`. | ||
#### `READY_CHECK_INTERVAL` | ||
The interval at which to execute the target's ready check. Defaults to 100ms. | ||
#### `TIMEOUT` | ||
The amount of time workers are allowed to spend processing a request is capped. Defaults to 5000ms. | ||
The number of milliseconds workers are allowed to spend processing a request is capped. Defaults to `5000`. | ||
@@ -40,4 +43,6 @@ ## Notes | ||
``` shell | ||
apt-get install xvfb chromium-browser | ||
xvfb-run chromium-browser --remote-debugging-port=9222 | ||
$ apt-get install xvfb chromium-browser | ||
$ xvfb-run chromium-browser --remote-debugging-port=9222 | ||
$ # or a slightly more customized example: | ||
# xvfb-run --server-args='-screen 0, 1024x768x16' chromium-browser --start-maximized --no-first-run --disable-gpu --remote-debugging-port=9222 | ||
``` | ||
@@ -47,3 +52,3 @@ | ||
``` shell | ||
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222 | ||
$ /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222 | ||
``` | ||
@@ -50,0 +55,0 @@ |
@@ -6,3 +6,5 @@ 'use strict'; | ||
module.exports = class extends Tab { | ||
loadUrl (url, readyCheck, cb) { | ||
loadUrl (req, cb) { | ||
this.req = req | ||
var timeout = setTimeout(() => { | ||
@@ -17,3 +19,3 @@ this.methods = {} | ||
this.call('Runtime.evaluate', { | ||
expression: readyCheck, | ||
expression: req.readyCheck, | ||
returnByValue: true | ||
@@ -28,3 +30,3 @@ }, (err, response) => { | ||
} else { | ||
setTimeout(waitForReady, process.env.READY_CHECK_INTERVAL || 100) | ||
setTimeout(waitForReady, req.readyCheckInterval) | ||
} | ||
@@ -36,3 +38,3 @@ }) | ||
this.call('Page.navigate', { url: url }, err => { | ||
this.call('Page.navigate', { url: req.url }, err => { | ||
if (cb._called) return | ||
@@ -50,11 +52,4 @@ if (err) { | ||
this.call('Runtime.evaluate', { | ||
expression: `Array.prototype.slice.call(document.querySelectorAll('script')).forEach(function (script) { | ||
script.remove() | ||
}); | ||
var contentType = document.contentType | ||
if (contentType === 'text/html') { | ||
document.documentElement.outerHTML | ||
} else { | ||
document.body.firstElementChild.innerHTML | ||
}`, | ||
expression: this.req.stripJs ? `Array.from(document.querySelectorAll('script')).forEach(script => script.remove()); | ||
document.documentElement.outerHTML` : `document.documentElement.outerHTML`, | ||
returnByValue: true | ||
@@ -61,0 +56,0 @@ }, (err, response) => { |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 6 instances in 1 package
7632
173
62
16
Updatedchrome-tab@^1.1.0