Permalink
Browse files

Update to use latest version of proxy-gremlin.

  • Loading branch information...
1 parent a9895f1 commit a1346f00371d58736af5769c5d0d1f3b7d9755c3 @omphalos committed Mar 10, 2013
Showing with 64 additions and 68 deletions.
  1. +10 −10 hosts/iframe.js
  2. +32 −38 hosts/proxy.js
  3. +1 −1 package.json
  4. +21 −19 universal-reloader.js
View
@@ -1,23 +1,23 @@
-var fs = require('fs')
+var fs = require('fs')
, http = require('http')
-module.exports = function(options) {
+var listenAndReloadScript
+ , index
- var listenAndReloadScript = fs.
+module.exports = function onIFramRequest(req, res, options) {
+
+ listenAndReloadScript = listenAndReloadScript || fs.
readFileSync(__dirname + '/../assets/listenAndReload.js').
toString();
- var index = fs.
+ index = index || fs.
readFileSync(__dirname + '/../assets/index.html').
toString().
replace("{{url}}", options.url).
replace("{{listenAndReload}}", listenAndReloadScript)
- return http.createServer(function(req, res) {
-
- if(options.verbose) console.log('serving index.html')
+ if(options.verbose) console.log('serving index.html')
- res.writeHead(200, {'Content-Type': 'text/html'})
- res.end(index)
- })
+ res.writeHead(200, {'Content-Type': 'text/html'})
+ res.end(index)
}
View
@@ -1,55 +1,49 @@
-var _ = require('underscore')
- , cheerio = require('cheerio')
- , fs = require('fs')
- , gremlin = require('proxy-gremlin')
- , http = require('http')
+var _ = require('underscore')
+ , cheerio = require('cheerio')
+ , fs = require('fs')
+ , gremlin = require('proxy-gremlin')
+ , http = require('http')
, httpProxy = require('http-proxy')
- , url = require('url')
+ , url = require('url')
var listenAndReloadScript = fs.
readFileSync(__dirname + '/../assets/listenAndReload.js').
toString();
-module.exports = function(options) {
+var proxy = new httpProxy.RoutingProxy()
+ , parsedUrl
- var parsedUrl = url.parse(options.url)
- , proxy = new httpProxy.RoutingProxy()
+module.exports = function onProxyRequest(req, res, options) {
- return http.createServer(function(req, res) {
+ // Intercept requests with proxy-gremlin.
+ gremlin.intercept(res, interceptor)
- // buffer the response using proxy-gremlin
- var buffer = new gremlin.Buffer(res)
+ // Proxy the request.
+ parsedUrl = parsedUrl || url.parse(options.url)
- // set up a listener for when the response is complete
- res.on('end', function() {
+ if(options.verbose)
+ console.log('proxying ' + parsedUrl.protocol +'//' + parsedUrl.host + req.url)
- // insert the script into the outgoing html
- if(buffer.headers['content-type'] === 'text/html') {
-
- $ = cheerio.load(buffer.getData())
- var root = $('body').length ? $('body') : $.root
-
- if(options.verbose)
- console.log('intercepting ' + req.url)
+ proxy.proxyRequest(req, res, {
+ host: parsedUrl.hostname,
+ port: parsedUrl.port || 80
+ })
+}
- root.append('<script src="/socket.io/socket.io.js"></script>\n')
- root.append('<script>\n' + listenAndReloadScript + '\n</script>\n')
+// This function intercepts outgoing html documents
+// and adds our websocket and reload scripts.
+function interceptor(buffer) {
- // overwrite data in the request
- buffer.setData($.html())
- }
+ // Do nothing if the response isn't html.
+ if(buffer.headers['content-type'] !== 'text/html') return
- // send the buffered request
- buffer.send()
- })
+ // Parse the page.
+ $ = cheerio.load(buffer.getData())
+ var root = $('body').length ? $('body') : $.root
- if(options.verbose)
- console.log({ message: 'proxying url', url: parsedUrl })
+ root.append('<script src="/socket.io/socket.io.js"></script>\n')
+ root.append('<script>\n' + listenAndReloadScript + '\n</script>\n')
- // Proxy the request
- proxy.proxyRequest(req, res, {
- host: parsedUrl.hostname,
- port: parsedUrl.port || 80
- })
- })
+ // overwrite data in the request
+ buffer.setData($.html())
}
View
@@ -35,7 +35,7 @@
"http-proxy" : "0.8.x",
"minimatch" : "0.2.x",
"optimist" : "0.3.x",
- "proxy-gremlin": "0.1.x",
+ "proxy-gremlin": "0.2.x",
"socket.io" : "0.9.x",
"underscore" : "1.4.x",
"watch" : "0.5.x"
View
@@ -1,11 +1,12 @@
#!/usr/bin/env node
-var _ = require('underscore')
- , fs = require('fs')
+var _ = require('underscore')
+ , http = require('http')
+ , fs = require('fs')
, minimatch = require('minimatch')
- , optimist = require('optimist')
- , socketIo = require('socket.io')
- , watch = require('watch')
+ , optimist = require('optimist')
+ , socketIo = require('socket.io')
+ , watch = require('watch')
var argv = optimist.
usage('Automatically reload your browser when files change.\nUsage: $0').
@@ -31,28 +32,32 @@ var options = {
verbose: argv.verbose === true
}
-//////////////////////
-// start web server //
-//////////////////////
+if(options.verbose) console.log(options)
-var httpServer = require('./hosts/' + options.hostingType + '.js')(options)
- , io = socketIo.listen(httpServer)
+// Start web server.
+var onHostedRequest = require('./hosts/' + options.hostingType + '.js')
+
+var httpServer = http.createServer(function onRequest(req, res) {
+ onHostedRequest(req, res, options)
+})
httpServer.listen(options.port)
console.log('Listening on port ' + options.port + '.')
console.log('Navigate to http://localhost:' + options.port + ' to see your universal-reloadable page.')
+var io = socketIo.listen(httpServer)
+
var notify = _.debounce(function innerNotify() {
- if(options.verbose) console.log('emitting update')
+ if(options.verbose)
+ console.log('emitting update')
+
io.sockets.emit('update', {})
-}, options.debounce)
-/////////////////
-// watch files //
-/////////////////
+}, options.debounce)
+// Watch files.
var mostRecentFile
function filter(file) {
@@ -76,10 +81,7 @@ function onWatchEvent(file, current, previous) {
watch.watchTree(options.folderRoot, onWatchEvent)
-////////////////////////////////////////////////////////////////////
-// set up most-recent-file listener (for optimized file watching) //
-////////////////////////////////////////////////////////////////////
-
+// Set up most-recent-file listener (for optimized file watching).
setInterval(function checkMostRecentFile() {
if(!mostRecentFile) return

0 comments on commit a1346f0

Please sign in to comment.