forked from mozilla/persona
/
run.js
executable file
·213 lines (184 loc) · 5.92 KB
/
run.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env node
// a little node webserver designed to run the unit tests herein
var sys = require("sys"),
http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
express = require("express");
var PRIMARY_HOST = "127.0.0.1";
var boundServers = [ ];
//
// this is the test harness, don't send emails, dump them to stdout
//
var nodemailer = require('nodemailer');
nodemailer.EmailMessage.prototype.send = function(callback) {
this.prepareVariables();
var headers = this.generateHeaders(),
body = this.generateBody();
console.log(headers);
console.log(body);
};
// given a buffer, find and replace all production hostnames
// with development URLs
function subHostNames(data) {
for (var i = 0; i < boundServers.length; i++) {
var o = boundServers[i]
var a = o.server.address();
var from = o.name;
var to = "http://" + a.address + ":" + a.port;
data = data.toString().replace(new RegExp(from, 'g'), to);
// now do another replacement to catch bare hostnames sans http(s)
// and explicit cases where port is appended
var fromWithPort;
if (from.substr(0,5) === 'https') {
from = from.substr(8);
fromWithPort = from + ":443";
} else {
from = from.substr(7);
fromWithPort = from + ":80";
}
to = to.substr(7);
if (o.subPath) to += o.subPath;
data = data.replace(new RegExp(fromWithPort, 'g'), to);
data = data.replace(new RegExp(from, 'g'), to);
}
return data;
}
// Middleware that intercepts outbound textual responses and substitutes
// in development hostnames
function substitutionMiddleware(req, resp, next) {
// cache the *real* functions
var realWrite = resp.write;
var realEnd = resp.end;
var realWriteHead = resp.writeHead;
var realSend = resp.send;
var buf = undefined;
var enc = undefined;
var contentType = undefined;
resp.writeHead = function (sc, reason, hdrs) {
var h = undefined;
if (typeof hdrs === 'object') h = hdrs;
else if (typeof reason === 'object') h = reason;
for (var k in h) {
if (k.toLowerCase() === 'content-type') {
contentType = h[k];
break;
}
}
if (!contentType) contentType = resp.getHeader('content-type');
if (!contentType) contentType = "application/unknown";
realWriteHead.call(resp, sc, reason, hdrs);
};
resp.write = function (chunk, encoding) {
if (buf) buf += chunk;
else buf = chunk;
enc = encoding;
};
resp.send = function(stuff) {
buf = stuff;
realSend.call(resp,stuff);
};
resp.end = function() {
if (!contentType) contentType = resp.getHeader('content-type');
if (contentType && (contentType === "application/javascript" ||
contentType.substr(0,4) === 'text'))
{
if (buf) {
var l = buf.length;
buf = subHostNames(buf);
if (l != buf.length) resp.setHeader('Content-Length', buf.length);
}
}
if (buf && buf.length) {
realWrite.call(resp, buf, enc);
}
realEnd.call(resp);
}
next();
};
function createServer(obj) {
var app = express.createServer();
app.use(express.logger());
// this file is a *test* harness, to make it go, we'll insert a little handler that
// substitutes output, changing production URLs to developement URLs.
app.use(substitutionMiddleware);
// let the specific server interact directly with the express server to register their middleware,
// routes, etc...
if (obj.setup) obj.setup(app);
// now set up the static resource servin'
var p = obj.path, ps = path.join(p, "static");
try { if (fs.statSync(ps).isDirectory()) p = ps; } catch(e) { }
app.use(express.static(p));
// and listen!
app.listen(obj.port, PRIMARY_HOST);
return app;
};
// start up webservers on ephemeral ports for each subdirectory here.
var dirs = [
// the reference verification server. A version is hosted at
// browserid.org and may be used, or the RP may perform their
// own verification.
{
name: "https://browserid.org/verify",
subPath: "/",
path: path.join(__dirname, "verifier")
},
// An example relying party.
{
name: "http://rp.eyedee.me",
path: path.join(__dirname, "rp")
},
// disabled primary for now since it's not in working
// order.
/* // A reference primary identity provider.
{
name: "https://eyedee.me",
path: path.join(__dirname, "primary")
}, */
// BrowserID: the secondary + ip + more.
{
name: "https://browserid.org",
path: path.join(__dirname, "browserid")
}
];
function formatLink(server, extraPath) {
var addr = server.address();
var url = 'http://' + addr.address + ':' + addr.port;
if (extraPath) {
url += extraPath;
}
return url;
}
console.log("Running test servers:");
var port_num=10000;
dirs.forEach(function(dirObj) {
if (!fs.statSync(dirObj.path).isDirectory()) return;
// does this server have a js handler for custom request handling?
var handlerPath = path.join(dirObj.path, "app.js");
var runJS = {};
try {
var runJSExists = false;
try { runJSExists = fs.statSync(handlerPath).isFile() } catch(e) {};
if (runJSExists) {
var runJS = require(handlerPath);
// set to development mode
runJS.production = false;
}
} catch(e) {
console.log("Error loading " + handlerPath + ": " + e);
process.exit(1);
}
var so = {
path: dirObj.path,
server: undefined,
port: port_num++,
name: dirObj.name,
handler: runJS.handler,
setup: runJS.setup,
subPath: dirObj.subPath
};
so.server = createServer(so)
boundServers.push(so);
console.log(" " + dirObj.name + ": " + formatLink(so.server));
});