Front end of the Slack clone application.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. Copyright 2014 Google Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. 'use strict';
  14. // TODO: Use self.registration.scope instead of self.location
  15. var url = new URL('./', self.location);
  16. var basePath = url.pathname;
  17. var pathRegexp = require('path-to-regexp');
  18. var Route = function(method, path, handler, options) {
  19. if (path instanceof RegExp) {
  20. this.fullUrlRegExp = path;
  21. } else {
  22. // The URL() constructor can't parse express-style routes as they are not
  23. // valid urls. This means we have to manually manipulate relative urls into
  24. // absolute ones. This check is extremely naive but implementing a tweaked
  25. // version of the full algorithm seems like overkill
  26. // (https://url.spec.whatwg.org/#concept-basic-url-parser)
  27. if (path.indexOf('/') !== 0) {
  28. path = basePath + path;
  29. }
  30. this.keys = [];
  31. this.regexp = pathRegexp(path, this.keys);
  32. }
  33. this.method = method;
  34. this.options = options;
  35. this.handler = handler;
  36. };
  37. Route.prototype.makeHandler = function(url) {
  38. var values;
  39. if (this.regexp) {
  40. var match = this.regexp.exec(url);
  41. values = {};
  42. this.keys.forEach(function(key, index) {
  43. values[key.name] = match[index + 1];
  44. });
  45. }
  46. return function(request) {
  47. return this.handler(request, values, this.options);
  48. }.bind(this);
  49. };
  50. module.exports = Route;