This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
server.js
280 lines (229 loc) · 7.99 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
* Server for testing Mobify.js!
*/
var http = require('http');
var express = require('express');
var fs = require('fs');
var path = require('path');
var Url = require('url');
var hbs = require('hbs');
http.globalAgent.maxSockets = 100;
/**
* Inlines the V7 Tag on test fixtures.
*/
var inlineTag = function(req, res, next) {
try {
var page = fs.readFileSync(__dirname + req.path, 'utf8');
} catch (e) {
return next();
}
var tagSource = '/tag/v7.exposed.min.js';
if (req.query['tag'] == "min") {
tagSource = '/tag/v7.min.js';
}
var tag = fs.readFileSync(__dirname + tagSource, 'utf8');
tag = tag.replace("https://preview.mobify.com/v7/", "/tests/fixtures/tag/preview.js");
page = page.replace("<script src=\"/tag/v7.js\"></script>", "<script>" + tag + "</script>");
res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8', 'Cache-Control': 'no-cache'});
res.end(page);
};
/**
* 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();
}
}, 500);
}
/**
* 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'));
/**
* Image resizing error test cases
*/
var textMime = function(req, res) {
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World');
};
var emptyResponse = function(req, res) {
res.setHeader('Content-Type', 'image/jpeg');
res.end();
};
var imageData = fs.readFileSync(path.normalize('./examples/assets/images/grumpycat.jpg'));
var wrongMime = function(req, res) {
res.setHeader('Content-Type', 'image/png');
res.end(imageData);
};
var partialImage = function(bytes) {
return function(req, res) {
res.setHeader('Content-Type', 'image/jpeg');
res.end(imageData.slice(0, bytes));
};
};
var abortedImage = function(req, res) {
res.setHeader('Content-Type', 'image/jpeg');
res.write(imageData.slice(0, 1024));
res.socket.destroy();
};
var notFound = function(req, res) {
res.statusCode = 404;
res.setHeader('Cotnent-Type', 'text/plain');
res.end('Not Found!');
};
var serverError = function(req, res) {
res.statusCode = 500;
res.setHeader('Content-Type', 'text/plain');
res.end('Faux Internal Server Error');
};
var fakeJPEG = function(req, res) {
res.setHeader('Content-Type', 'image/jpeg');
res.end('asdf');
};
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('/tests/fixtures/tag/*', inlineTag);
app.get('/performance/jazzcat/', jazzcatPerformanceIndex);
app.get('/performance/jazzcat/runner/:numScripts', jazzcatPerformanceRunner);
app.get('/jsonp/Jazzcat.load/:scripts', jazzcatJsonp);
app.get('/js/:scripts', jazzcatJs);
app.get('/imagetests/textmime', textMime);
app.get('/imagetests/emptyresponse', emptyResponse);
app.get('/imagetests/wrongmime', wrongMime);
app.get('/imagetests/partialimage100b', partialImage(100));
app.get('/imagetests/partialimage1k', partialImage(1024));
app.get('/imagetests/abortedimage', abortedImage);
app.get('/imagetests/notFound', notFound);
app.get('/imagetests/serverError', serverError);
app.get('/imagetests/fakejpeg', fakeJPEG);
if (require.main === module) {
app.use(express.static(__dirname));
app.listen(process.env.PORT || 3000);
}
module.exports = app;