aboutsummaryrefslogtreecommitdiff
path: root/node_modules/workbox-routing/RegExpRoute.mjs
diff options
context:
space:
mode:
authorruki <waruqi@gmail.com>2018-11-08 00:38:48 +0800
committerruki <waruqi@gmail.com>2018-11-07 21:53:09 +0800
commit26105034da4fcce7ac883c899d781f016559310d (patch)
treec459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/workbox-routing/RegExpRoute.mjs
parent2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff)
downloadxmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz
xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip
switch to vuepress
Diffstat (limited to 'node_modules/workbox-routing/RegExpRoute.mjs')
-rw-r--r--node_modules/workbox-routing/RegExpRoute.mjs93
1 files changed, 93 insertions, 0 deletions
diff --git a/node_modules/workbox-routing/RegExpRoute.mjs b/node_modules/workbox-routing/RegExpRoute.mjs
new file mode 100644
index 00000000..7d217d31
--- /dev/null
+++ b/node_modules/workbox-routing/RegExpRoute.mjs
@@ -0,0 +1,93 @@
+/*
+ Copyright 2017 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.
+*/
+
+import {assert} from 'workbox-core/_private/assert.mjs';
+import {logger} from 'workbox-core/_private/logger.mjs';
+import {Route} from './Route.mjs';
+import './_version.mjs';
+
+/**
+ * RegExpRoute makes it easy to create a regular expression based
+ * [Route]{@link workbox.routing.Route}.
+ *
+ * For same-origin requests the RegExp only needs to match part of the URL. For
+ * requests against third-party servers, you must define a RegExp that matches
+ * the start of the URL.
+ *
+ * [See the module docs for info.]{@link https://developers.google.com/web/tools/workbox/modules/workbox-routing}
+ *
+ * @memberof workbox.routing
+ * @extends workbox.routing.Route
+ */
+class RegExpRoute extends Route {
+ /**
+ * If the regulard expression contains
+ * [capture groups]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#grouping-back-references},
+ * th ecaptured values will be passed to the
+ * [handler's]{@link workbox.routing.Route~handlerCallback} `params`
+ * argument.
+ *
+ * @param {RegExp} regExp The regular expression to match against URLs.
+ * @param {workbox.routing.Route~handlerCallback} handler A callback
+ * function that returns a Promise resulting in a Response.
+ * @param {string} [method='GET'] The HTTP method to match the Route
+ * against.
+ */
+ constructor(regExp, handler, method) {
+ if (process.env.NODE_ENV !== 'production') {
+ assert.isInstance(regExp, RegExp, {
+ moduleName: 'workbox-routing',
+ className: 'RegExpRoute',
+ funcName: 'constructor',
+ paramName: 'pattern',
+ });
+ }
+
+ const match = ({url}) => {
+ const result = regExp.exec(url.href);
+
+ // Return null immediately if there's no match.
+ if (!result) {
+ return null;
+ }
+
+ // Require that the match start at the first character in the URL string
+ // if it's a cross-origin request.
+ // See https://github.com/GoogleChrome/workbox/issues/281 for the context
+ // behind this behavior.
+ if ((url.origin !== location.origin) && (result.index !== 0)) {
+ if (process.env.NODE_ENV !== 'production') {
+ logger.debug(
+ `The regular expression '${regExp}' only partially matched ` +
+ `against the cross-origin URL '${url}'. RegExpRoute's will only ` +
+ `handle cross-origin requests if they match the entire URL.`
+ );
+ }
+
+ return null;
+ }
+
+ // If the route matches, but there aren't any capture groups defined, then
+ // this will return [], which is truthy and therefore sufficient to
+ // indicate a match.
+ // If there are capture groups, then it will return their values.
+ return result.slice(1);
+ };
+
+ super(match, handler, method);
+ }
+}
+
+export {RegExpRoute};