This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
/
server.js
192 lines (158 loc) · 5.58 KB
/
server.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
/**
* Server for testing Mobify.js!
*/
var http = require('http');
var express = require('express');
var fs = require('fs');
var Url = require('url');
var hbs = require('hbs');
http.globalAgent.maxSockets = 100;
/**
* Used for test "capture captures the complete document" in `tests/capture.html`.
*/
var slowResponse = function(req, res) {
var split = fs.readFileSync(__dirname + req.path, 'utf8').split('<!-- SPLIT -->')
res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'});
res.write(split[0]);
var chunk = 1;
var iid = setInterval(function() {
res.write(split[chunk]);
chunk++;
if (chunk === split.length){
clearInterval(iid);
res.end();
}
}, 1000);
}
/**
* Used for test "capture captures the complete document" in `tests/capture.html`.
*/
var cachedResponse = function(req, res, next) {
res.header('Content-Type', 'application/javascript');
res.header('Cache-Control', 'max-age=10000');
next();
}
/**
* Browser entry point for Jazzcat Performance tests.
*/
var jazzcatPerformanceIndex = function(req, res) {
res.render('jazzcat', {});
};
/**
* Main code for jazzcat runners. Passed in as a context variable for the
* bootstrap tag using toString.
*/
var jazzcatMainExec = function(){
var capturing = window.Mobify && window.Mobify.capturing || false;
Jazzcat.httpCache.save = function() {
return console.log("mocking save to do nothing");
}
if (capturing) {
// Initiate capture
Mobify.Capture.init(function(capture){
// Grab reference to a newly created document
var capturedDoc = capture.capturedDoc;
var matchType = location.href.match(/responseType=([^&;]*)/);
var responseType = (matchType && matchType[1]) || 'jsonp';
var matchConcat = location.href.match(/concat=([^&;]*)/);
var concat = (matchConcat && matchConcat[1] === 'true');
if (concat === null) {
concat = true;
}
// Grab all scripts to be concatenated into one request
if (!/disableJazzcat=1/.test(location.href)) {
var scripts = capturedDoc.querySelectorAll('script');
Mobify.Jazzcat.optimizeScripts(scripts, {
responseType: responseType,
base: '',
concat: concat
});
}
// Render source DOM to document
capture.renderCapturedDoc();
});
}
};
/**
* Page that runs the Jazzcat performance test.
*/
var jazzcatPerformanceRunner = function(req, res) {
var numScripts = parseInt(req.params.numScripts);
var scripts = [];
for (var i = 0; i < numScripts; i++) {
var index = i % files.length;
scripts.push(resourcesUrl + scriptsArray[index].fileName + "?" + i);
}
// Append timestamp in order to ensure the mobify.js does not always
// get loaded cached, and also ensures the second load of mobify.js
// on a test does not load again.
var library = '/build/mobify.min.js';
//var library = '/build/mobify.js'; // uncomment for debugging
library += "?" + new Date().getTime();
res.header('Connection', 'close');
res.render('fixtures/jazzcatRunner', {
library: library,
main: jazzcatMainExec.toString(),
scripts: scripts
});
};
/**
* Implements the Jazzcat JSONP API.
*/
var jazzcatJsonp = function(req, res) {
var scripts = JSON.parse(req.params.scripts);
var scriptData = scripts.map(function(script){
var pathname = Url.parse(script).pathname;
return {
url: script,
data: JSON.stringify(scriptsObj[pathname])
}
});
res.header('Content-Type', 'application/javascript')
res.render('fixtures/jazzcatJSONResponse', {scripts: scriptData});
};
/**
* Implements the Jazzcat JS API.
*/
var jazzcatJs = function(req, res) {
var scripts = JSON.parse(req.params.scripts);
var scriptData = scripts.map(function(script){
var pathname = Url.parse(script).pathname;
return scriptsObj[pathname].toString()
});
res.header('Content-Type', 'application/javascript')
res.render('fixtures/jazzcatJSResponse', {scripts: scriptData})
};
// Load scripts for the mock mock Jazzcat API.
var resourcesUrl = '/mobifyjs/performance/resources/samplescripts/';
var files = fs.readdirSync(__dirname + resourcesUrl).filter(function(folder) {
return folder[0] !== '.';
});
var scriptsObj = {};
var scriptsArray = [];
for (file in files) {
var fileName = files[file];
var fileData = fs.readFileSync(__dirname + resourcesUrl + files[file], 'utf8');
scriptsArray.push({fileName: fileName, fileData: fileData});
scriptsObj[resourcesUrl + fileName] = fileData;
}
hbs.registerPartial('bootstrap', fs.readFileSync(__dirname + '/tag/bootstrap.html', 'utf8'));
var app = express();
app.set('views', __dirname + '/performance');
app.set('view engine', 'html');
app.engine('html', require('hbs').__express);
app.use(function(req, res, next) {
res.header('Cache-Control' , 'max-age=0, no-cache, no-store');
next();
});
app.get('/build/mobify(.min)?.js', cachedResponse);
app.get('/tests/fixtures/split*', slowResponse);
app.get('/performance/jazzcat/', jazzcatPerformanceIndex);
app.get('/performance/jazzcat/runner/:numScripts', jazzcatPerformanceRunner);
app.get('/jsonp/Jazzcat.load/:scripts', jazzcatJsonp);
app.get('/js/:scripts', jazzcatJs);
if (require.main === module) {
app.use(express.static(__dirname));
app.listen(process.env.PORT || 3000);
}
module.exports = app;