12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- /*
- Copyright 2014 Google Inc. All Rights Reserved.
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- 'use strict';
-
- // TODO: Use self.registration.scope instead of self.location
- var url = new URL('./', self.location);
- var basePath = url.pathname;
- var pathRegexp = require('path-to-regexp');
-
- var Route = function(method, path, handler, options) {
- if (path instanceof RegExp) {
- this.fullUrlRegExp = path;
- } else {
- // The URL() constructor can't parse express-style routes as they are not
- // valid urls. This means we have to manually manipulate relative urls into
- // absolute ones. This check is extremely naive but implementing a tweaked
- // version of the full algorithm seems like overkill
- // (https://url.spec.whatwg.org/#concept-basic-url-parser)
- if (path.indexOf('/') !== 0) {
- path = basePath + path;
- }
-
- this.keys = [];
- this.regexp = pathRegexp(path, this.keys);
- }
-
- this.method = method;
- this.options = options;
- this.handler = handler;
- };
-
- Route.prototype.makeHandler = function(url) {
- var values;
- if (this.regexp) {
- var match = this.regexp.exec(url);
- values = {};
- this.keys.forEach(function(key, index) {
- values[key.name] = match[index + 1];
- });
- }
-
- return function(request) {
- return this.handler(request, values, this.options);
- }.bind(this);
- };
-
- module.exports = Route;
|