Skip to content

Commit a25c3d8

Browse files
committed
feat(http): Alternate strategies for http middleware
.set({'strategy':'default'});
1 parent 8a6abcb commit a25c3d8

File tree

4 files changed

+146
-29
lines changed

4 files changed

+146
-29
lines changed

lib/http/strategy.js renamed to lib/http/default.strategy.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ function init() {
2727
}
2828

2929
function load(paths,settings) {
30-
if (settings.compress) {
31-
module.exports.compressMiddleware = connect.compress();
32-
}
30+
module.exports.compressMiddleware = connect.compress();
3331
module.exports.assetsMiddleware = serveStatic('/assets',paths.assets, settings['static']);
3432
module.exports.staticMiddleware = serveStatic(null,paths['static'], settings['static']);
3533
loadStaticDirs(paths.static, paths.assets);
@@ -58,21 +56,27 @@ function isStatic(url) {
5856
function loadStaticDirs(staticPath,assetsPath) {
5957
var pathLength;
6058

59+
/* Get a list of all static files we know about (used to prevent connect.session from loading unnecessarily) */
60+
6161
if (fs.existsSync(staticPath)) {
62+
staticDirs = staticDirs.concat(fs.readdirSync(staticPath));
6263

63-
/* Get a list of all static files we know about (used to prevent connect.session from loading unnecessarily) */
64-
pathLength = staticPath.length;
64+
staticFiles = staticFiles.concat(fileUtils.readDirSync(staticPath).files);
65+
}
66+
67+
if (fs.existsSync(assetsPath)) {
68+
fileUtils.readDirSync(assetsPath).files.forEach(function(name) {
69+
staticFiles.push('/assets' + name);
70+
});
6571
}
6672

6773
/* Ensure /assets is always present, even if the dir has yet to be created */
6874
if (staticDirs.indexOf('assets') === -1) {
6975
staticDirs.push('assets');
7076
}
7177

72-
staticFiles = staticFiles.concat(fileUtils.readDirSync(staticPath).files);
73-
7478
return staticFiles.map(function(file) {
75-
return file.substr(pathLength);
79+
return path.relative(staticPath, file);
7680
});
7781
}
7882

lib/http/index.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var fs = require('fs'),
55
router = new (require('./router').Router),
66
app,
77
settings = { // User-configurable settings with sensible defaults
8-
'strategy': require('./strategy'),
8+
'strategy': 'default',
99
'static': {
1010
maxAge: 2592000000 // (30 * 24 * 60 * 60 * 1000) cache static assets in the browser for 30 days
1111
},
@@ -82,6 +82,9 @@ module.exports = function(root) {
8282
get middleware() {
8383
if (app == null) {
8484
// Create new Connect app instance which can be accessed from your app.js file with ss.http.middleware
85+
if (typeof settings.strategy == "string") {
86+
settings.strategy = require('./' + settings.strategy + '.strategy');
87+
}
8588
app = settings.strategy.init();
8689

8790
/* Alias app.use to indicate this will be added to the stack BEFORE SocketStream middleware */
@@ -145,30 +148,38 @@ module.exports = function(root) {
145148

146149
staticPath = path.join(root, staticPath);
147150
assetsPath = path.join(root, assetsPath);
151+
148152
settings.strategy.load({
149153
root: root,
150154
static: staticPath,
151155
assets: assetsPath
152156
},settings);
153157

154158
/* connect.compress() should be added to middleware stack on highest possible position */
155-
app.use(settings.strategy.compressMiddleware);
159+
if (settings.strategy.compressMiddleware) {
160+
app.use(settings.strategy.compressMiddleware);
161+
}
156162

157163
/* Append SocketStream middleware upon server load */
158-
app.use(settings.strategy.cookieParser(secret));
159-
160-
161-
app.use(settings.strategy.favicon(staticPath + '/favicon.ico'))
162-
app.use(settings.strategy.session({
163-
cookie: {
164-
path: '/',
165-
httpOnly: false,
166-
maxAge: sessionOptions.maxAge,
167-
secure: settings.secure
168-
},
169-
store: sessionStore,
170-
secret: secret
171-
}));
164+
if (settings.strategy.cookieParser) {
165+
app.use(settings.strategy.cookieParser(secret));
166+
}
167+
168+
if (settings.strategy.favicon) {
169+
app.use(settings.strategy.favicon(staticPath + '/favicon.ico'))
170+
}
171+
if (settings.strategy.session) {
172+
app.use(settings.strategy.session({
173+
cookie: {
174+
path: '/',
175+
httpOnly: false,
176+
maxAge: sessionOptions.maxAge,
177+
secure: settings.secure
178+
},
179+
store: sessionStore,
180+
secret: secret
181+
}));
182+
}
172183

173184
  /* Append any custom-defined middleware (e.g. everyauth) */
174185
useAfterStack.forEach(function(m) {

lib/http/minimal.strategy.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* The minimal strategy for loading HTTP resources. You can set an alternate
3+
* strategy with `ss.http.set({'strategy':{..}})`.
4+
*/
5+
'use strict';
6+
7+
var fs = require('fs'),
8+
path = require('path'),
9+
connect = require('connect'),
10+
serveStatic = require('../utils/serve-static'),
11+
fileUtils = require('../utils/file'),
12+
staticDirs = [],
13+
staticFiles = [];
14+
15+
16+
module.exports = {
17+
init: init,
18+
load: load,
19+
cookieParser: connect.cookieParser,
20+
favicon: connect.favicon,
21+
isStatic: isStatic
22+
};
23+
24+
function init() {
25+
return connect();
26+
}
27+
28+
function load(paths,settings) {
29+
module.exports.assetsMiddleware = serveStatic('/assets',paths.assets, settings['static']);
30+
module.exports.staticMiddleware = serveStatic(null,paths['static'], settings['static']);
31+
loadStaticDirs(paths.static, paths.assets);
32+
33+
if (settings.staticCache) {
34+
module.exports.cacheMiddleware = connect.staticCache(settings['staticCache']);
35+
}
36+
}
37+
38+
function isStatic(url) {
39+
//return staticUrls[url] !== undefined;
40+
41+
var initialDir = url.split('/')[1];
42+
43+
return staticDirs.indexOf(initialDir) >= 0;
44+
}
45+
46+
47+
/**
48+
* Loads static directories (client/static)
49+
*
50+
* @param {String} staticPath Path string
51+
* @param {String} assetsPath Path string
52+
* @return {Array} Array of all static files we know about (used to prevent connect.session from loading unnecessarily)
53+
*/
54+
function loadStaticDirs(staticPath,assetsPath) {
55+
var pathLength;
56+
57+
/* Get a list of all static files we know about (used to prevent connect.session from loading unnecessarily) */
58+
59+
if (fs.existsSync(staticPath)) {
60+
staticDirs = staticDirs.concat(fs.readdirSync(path));
61+
62+
staticFiles = staticFiles.concat(fileUtils.readDirSync(staticPath).files);
63+
}
64+
65+
if (fs.existsSync(assetsPath)) {
66+
staticDirs = staticDirs.concat(fs.readdirSync(path));
67+
68+
fileUtils.readDirSync(staticPath).files.forEach(function(name) {
69+
staticFiles.push('/assets' + name);
70+
});
71+
}
72+
73+
/* Ensure /assets is always present, even if the dir has yet to be created */
74+
if (staticDirs.indexOf('assets') === -1) {
75+
staticDirs.push('assets');
76+
}
77+
78+
return staticFiles.map(function(file) {
79+
return path.relative(staticPath, file);
80+
});
81+
}
82+

test/unit/http/index.test.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var request = require('supertest'),
2121
connect = require('connect'),
2222
util = require('util'),
2323
httpFunc = require( path.join(process.env.PWD, 'lib/http/index') ),
24+
serveStatic = require('../../../lib/utils/serve-static'),
2425
http = null,
2526
app = null,
2627
utils = require('../../helpers/utils.js'),
@@ -148,11 +149,23 @@ describe('lib/http/index', function () {
148149
});
149150
});
150151

152+
describe('app setup without SS middleware',function() {
153+
154+
});
155+
156+
describe('minimal strategy',function() {
157+
it('should serve static files',function(){});
158+
it('should serve assets',function(){});
159+
it('should serve assets outside the static root',function(){});
160+
});
161+
151162
describe('#load()', function () {
152-
var staticPath = '';
163+
var staticPath = '',
164+
assetsPath = '';
153165

154166
function setUp(done) {
155167
staticPath = 'client/static';
168+
assetsPath = 'client/static/assets';
156169

157170
/* Loading http function */
158171
http = httpFunc(root);
@@ -162,7 +175,7 @@ describe('lib/http/index', function () {
162175
http.middleware.append(testAppendMiddleware);
163176

164177
/* loading http server assets */
165-
app = http.load(staticPath, sessionStore, sessionOptions);
178+
app = http.load(staticPath, assetsPath, sessionStore, sessionOptions);
166179

167180
done();
168181
}
@@ -186,7 +199,7 @@ describe('lib/http/index', function () {
186199

187200
/* testing correct length of http.middleware.stack */
188201
http.middleware.stack.should.be.an.instanceOf(Array);
189-
http.middleware.stack.should.have.length(8);
202+
http.middleware.stack.should.have.length(9);
190203

191204
/**
192205
* testing testPrependMiddleware
@@ -250,11 +263,18 @@ describe('lib/http/index', function () {
250263
utils.getfunctionName( http.middleware.stack[6].handle ).should.equal( 'eventMiddleware' );
251264

252265
/**
253-
* testing connect["static"]()
266+
* testing assets resource middleware
254267
* lib/http/index.js:161
255268
*/
256269
http.middleware.stack[7].handle.should.be.an.instanceOf(Function);
257-
http.middleware.stack[7].handle.toString().should.equal( connect.static(staticPath, settings.static).toString() );
270+
http.middleware.stack[7].handle.toString().should.equal( serveStatic(assetsPath, settings.static).toString() );
271+
272+
/**
273+
* testing static resource middleware
274+
* lib/http/index.js:161
275+
*/
276+
http.middleware.stack[8].handle.should.be.an.instanceOf(Function);
277+
http.middleware.stack[8].handle.toString().should.equal( serveStatic(staticPath, settings.static).toString() );
258278

259279
done();
260280
});

0 commit comments

Comments
 (0)