|
2 | 2 | // We are simply using this hack because, there is no way to alter |
3 | 3 | // Meteor's html content on the server side |
4 | 4 |
|
5 | | -var http = Npm.require('http'); |
6 | | - |
7 | | -var originalWrite = http.OutgoingMessage.prototype.write; |
8 | | -http.OutgoingMessage.prototype.write = function(chunk, encoding) { |
9 | | - //prevent hijacking other http requests |
10 | | - if(!this.iInjected && |
11 | | - encoding === undefined && /^<!DOCTYPE html>/.test(chunk)) { |
12 | | - chunk = chunk.toString(); |
13 | | - |
14 | | - for (id in Inject.rawModHtmlFuncs) { |
15 | | - chunk = Inject.rawModHtmlFuncs[id](chunk, this); |
16 | | - if (!_.isString(chunk)) { |
17 | | - throw new Error('Inject func id "' + id + '" must return HTML, not ' |
18 | | - + typeof(chunk) + '\n' + JSON.stringify(chunk, null, 2)); |
| 5 | +Inject._hijackWrite = function(res) { |
| 6 | + var originalWrite = res.write; |
| 7 | + res.write = function(chunk, encoding) { |
| 8 | + //prevent hijacking other http requests |
| 9 | + if(!res.iInjected && |
| 10 | + encoding === undefined && /^<!DOCTYPE html>/.test(chunk)) { |
| 11 | + chunk = chunk.toString(); |
| 12 | + |
| 13 | + for (id in Inject.rawModHtmlFuncs) { |
| 14 | + chunk = Inject.rawModHtmlFuncs[id](chunk, res); |
| 15 | + if (!_.isString(chunk)) { |
| 16 | + throw new Error('Inject func id "' + id + '" must return HTML, not ' |
| 17 | + + typeof(chunk) + '\n' + JSON.stringify(chunk, null, 2)); |
| 18 | + } |
19 | 19 | } |
| 20 | + |
| 21 | + res.iInjected = true; |
20 | 22 | } |
21 | 23 |
|
22 | | - this.iInjected = true; |
23 | | - } |
| 24 | + originalWrite.call(res, chunk, encoding); |
| 25 | + }; |
| 26 | +} |
24 | 27 |
|
25 | | - originalWrite.call(this, chunk, encoding); |
26 | | -}; |
| 28 | +WebApp.connectHandlers.use(function(req, res, next) { |
| 29 | + // We only separate this to make testing easier |
| 30 | + Inject._hijackWrite(res); |
| 31 | + |
| 32 | + next(); |
| 33 | +}); |
27 | 34 |
|
28 | 35 | //meteor algorithm to check if this is a meteor serving http request or not |
29 | 36 | Inject.appUrl = function(url) { |
|
0 commit comments