diff --git a/index.js b/index.js index f4230a5..068de6d 100644 --- a/index.js +++ b/index.js @@ -1,29 +1,20 @@ 'use strict'; +var rewrite = require('connect-body-rewrite'); + module.exports = function (options) { if (!options) { options = {}; } var host = options.host || "http://localhost:35729"; - var script = ""; - - return function (req, res, next) { - var write = res.write; - - res.write = function (chunk, encoding, callback) { - var length = res.getHeader('content-length'); - var header = res.getHeader('content-type'); - - if (header && header.match(/text\/html/) && chunk) { - chunk = chunk.toString(); - var origLength = chunk.length; - chunk = chunk.replace(/(<\/body>)/, script + "$1"); - var newLength = chunk.length; + var script = "\n"; - res.setHeader('content-length', length - (origLength - newLength)); - } - write.call(res, chunk, encoding, callback); - }; - next(); - }; + return rewrite({ + accept: function (res) { + return res.getHeader('content-type').match(/text\/html/); + }, + rewrite: function (body) { + return body.replace(/(<\/body>)/, script + "$1"); + } + }); }; diff --git a/package.json b/package.json index 8475603..5080743 100644 --- a/package.json +++ b/package.json @@ -27,5 +27,8 @@ "repository": { "type": "git", "url": "git://github.com/rubenv/connect-livereload-safe.git" + }, + "dependencies": { + "connect-body-rewrite": "0.0.1" } }