forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'remotes/origin/pr/679' into devel. Fixes …
- Loading branch information
Showing
8 changed files
with
179 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Package.describe({ | ||
summary: "route policy declarations", | ||
internal: true | ||
}); | ||
|
||
Package.on_use(function (api) { | ||
api.add_files('routepolicy.js', 'server'); | ||
}); | ||
|
||
Package.on_test(function (api) { | ||
api.add_files(['routepolicy_tests.js'], 'server'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
(function () { | ||
|
||
// The route policy is a singleton in a running application, but we | ||
// can't unit test the real singleton because messing with the real | ||
// routes would break tinytest... so allow policy instances to be | ||
// constructed for testing. | ||
|
||
Meteor.__RoutePolicyConstructor = function () { | ||
var self = this; | ||
self.urlPrefixTypes = {}; | ||
}; | ||
|
||
_.extend(Meteor.__RoutePolicyConstructor.prototype, { | ||
|
||
urlPrefixMatches: function (urlPrefix, url) { | ||
return url.substr(0, urlPrefix.length) === urlPrefix; | ||
}, | ||
|
||
checkType: function (type) { | ||
if (! _.contains(['network'], type)) | ||
return 'the route type must be "network"'; | ||
return null; | ||
}, | ||
|
||
checkUrlPrefix: function (urlPrefix) { | ||
var self = this; | ||
if (urlPrefix.charAt(0) !== '/') | ||
return 'a route URL prefix must begin with a slash'; | ||
if (urlPrefix === '/') | ||
return 'a route URL prefix cannot be /'; | ||
if (self.urlPrefixTypes[urlPrefix] && self.urlPrefixTypes[urlPrefix] !== type) | ||
return 'the route URL prefix ' + urlPrefix + ' has already been declared to be of type ' + type; | ||
return null; | ||
}, | ||
|
||
checkForConflictWithStatic: function (urlPrefix, type, _testManifest) { | ||
var self = this; | ||
var manifest = _testManifest || __meteor_bootstrap__.bundle.manifest; | ||
var conflict = _.find(manifest, function (resource) { | ||
return (resource.type === 'static' && | ||
resource.where === 'client' && | ||
self.urlPrefixMatches(urlPrefix, resource.url)); | ||
}); | ||
if (conflict) | ||
return ('static resource ' + conflict.url + ' conflicts with ' + | ||
type + ' route ' + urlPrefix); | ||
else | ||
return null; | ||
}, | ||
|
||
declare: function (urlPrefix, type) { | ||
var self = this; | ||
var problem = self.checkType(type) || | ||
self.checkUrlPrefix(urlPrefix) || | ||
self.checkForConflictWithStatic(urlPrefix, type); | ||
if (problem) | ||
throw new Error(problem); | ||
// TODO overlapping prefixes, e.g. /foo/ and /foo/bar/ | ||
self.urlPrefixTypes[urlPrefix] = type; | ||
}, | ||
|
||
classify: function (url) { | ||
var self = this; | ||
if (url.charAt(0) !== '/') | ||
throw new Error('url must be a relative URL: ' + url); | ||
var prefix = _.find(_.keys(self.urlPrefixTypes), function (_prefix) { | ||
return self.urlPrefixMatches(_prefix, url); | ||
}); | ||
if (prefix) | ||
return self.urlPrefixTypes[prefix]; | ||
else | ||
return null; | ||
}, | ||
|
||
urlPrefixesFor: function (type) { | ||
var self = this; | ||
var prefixes = []; | ||
_.each(self.urlPrefixTypes, function (_type, _prefix) { | ||
if (_type === type) | ||
prefixes.push(_prefix); | ||
}); | ||
return prefixes.sort(); | ||
} | ||
}); | ||
|
||
__meteor_bootstrap__._routePolicy = Meteor._routePolicy = | ||
new Meteor.__RoutePolicyConstructor(); | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
Tinytest.add("routepolicy", function (test) { | ||
var policy = new Meteor.__RoutePolicyConstructor(); | ||
|
||
policy.declare('/sockjs/', 'network'); | ||
// App routes might look like this... | ||
// policy.declare('/posts/', 'app'); | ||
// policy.declare('/about', 'app'); | ||
|
||
test.equal(policy.classify('/'), null); | ||
test.equal(policy.classify('/foo'), null); | ||
test.equal(policy.classify('/sockjs'), null); | ||
|
||
test.equal(policy.classify('/sockjs/'), 'network'); | ||
test.equal(policy.classify('/sockjs/foo'), 'network'); | ||
|
||
// test.equal(policy.classify('/posts/'), 'app'); | ||
// test.equal(policy.classify('/posts/1234'), 'app'); | ||
|
||
test.equal(policy.urlPrefixesFor('network'), ['/sockjs/']); | ||
// test.equal(policy.urlPrefixesFor('app'), ['/about', '/posts/']); | ||
}); | ||
|
||
Tinytest.add("routepolicy - static conflicts", function (test) { | ||
var manifest = [ | ||
{ | ||
"path": "static/sockjs/socks-are-comfy.jpg", | ||
"type": "static", | ||
"where": "client", | ||
"cacheable": false, | ||
"url": "/sockjs/socks-are-comfy.jpg" | ||
}, | ||
]; | ||
var policy = new Meteor.__RoutePolicyConstructor(); | ||
|
||
test.equal( | ||
policy.checkForConflictWithStatic('/sockjs/', 'network', manifest), | ||
"static resource /sockjs/socks-are-comfy.jpg conflicts with network route /sockjs/" | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters