From 35e8c5f8ccea7700980c3057c45f668d3e76284a Mon Sep 17 00:00:00 2001 From: Andy Burke Date: Thu, 14 Nov 2019 15:35:42 -0800 Subject: [PATCH 1/2] fix: don't fail on routes that begin with the page base setting --- index.js | 4 +- page.js | 429 ++------------------------------------------------ page.mjs | 415 +----------------------------------------------- test/tests.js | 14 +- 4 files changed, 31 insertions(+), 831 deletions(-) diff --git a/index.js b/index.js index 2fdbe101..6daa3d78 100755 --- a/index.js +++ b/index.js @@ -411,7 +411,7 @@ var orig = path; var pageBase = this._getBase(); - if (path.indexOf(pageBase) === 0) { + if (path === pageBase || path.indexOf(pageBase + '/') === 0 || path.indexOf(pageBase + '?') === 0 || path.indexOf(pageBase + '#') === 0) { path = path.substr(pageBase.length); } @@ -667,7 +667,7 @@ var hashbang = _page._hashbang; var pageBase = _page._getBase(); - if ('/' === path[0] && 0 !== path.indexOf(pageBase)) path = pageBase + (hashbang ? '#!' : '') + path; + if ('/' === path[0] && path !== pageBase && 0 !== path.indexOf(pageBase + '/') && 0 !== path.indexOf(pageBase + '?') && 0 !== path.indexOf(pageBase + '#')) path = pageBase + (hashbang ? '#!' : '') + path; var i = path.indexOf('?'); this.canonicalPath = path; diff --git a/page.js b/page.js index 82858a5e..2b1f37b1 100644 --- a/page.js +++ b/page.js @@ -1,412 +1,14 @@ (function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : + typeof exports === 'object' && typeof module !== 'undefined' ? factory() : typeof define === 'function' && define.amd ? define(factory) : - (global.page = factory()); + (factory()); }(this, (function () { 'use strict'; -var isarray = Array.isArray || function (arr) { - return Object.prototype.toString.call(arr) == '[object Array]'; -}; - -/** - * Expose `pathToRegexp`. - */ -var pathToRegexp_1 = pathToRegexp; -var parse_1 = parse; -var compile_1 = compile; -var tokensToFunction_1 = tokensToFunction; -var tokensToRegExp_1 = tokensToRegExp; - -/** - * The main path matching regexp utility. - * - * @type {RegExp} - */ -var PATH_REGEXP = new RegExp([ - // Match escaped characters that would otherwise appear in future matches. - // This allows the user to escape special characters that won't transform. - '(\\\\.)', - // Match Express-style parameters and un-named parameters with a prefix - // and optional suffixes. Matches appear as: - // - // "/:test(\\d+)?" => ["/", "test", "\d+", undefined, "?", undefined] - // "/route(\\d+)" => [undefined, undefined, undefined, "\d+", undefined, undefined] - // "/*" => ["/", undefined, undefined, undefined, undefined, "*"] - '([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^()])+)\\))?|\\(((?:\\\\.|[^()])+)\\))([+*?])?|(\\*))' -].join('|'), 'g'); - -/** - * Parse a string for the raw tokens. - * - * @param {String} str - * @return {Array} - */ -function parse (str) { - var tokens = []; - var key = 0; - var index = 0; - var path = ''; - var res; - - while ((res = PATH_REGEXP.exec(str)) != null) { - var m = res[0]; - var escaped = res[1]; - var offset = res.index; - path += str.slice(index, offset); - index = offset + m.length; - - // Ignore already escaped sequences. - if (escaped) { - path += escaped[1]; - continue - } - - // Push the current path onto the tokens. - if (path) { - tokens.push(path); - path = ''; - } - - var prefix = res[2]; - var name = res[3]; - var capture = res[4]; - var group = res[5]; - var suffix = res[6]; - var asterisk = res[7]; - - var repeat = suffix === '+' || suffix === '*'; - var optional = suffix === '?' || suffix === '*'; - var delimiter = prefix || '/'; - var pattern = capture || group || (asterisk ? '.*' : '[^' + delimiter + ']+?'); - - tokens.push({ - name: name || key++, - prefix: prefix || '', - delimiter: delimiter, - optional: optional, - repeat: repeat, - pattern: escapeGroup(pattern) - }); - } - - // Match any characters still remaining. - if (index < str.length) { - path += str.substr(index); - } - - // If the path exists, push it onto the end. - if (path) { - tokens.push(path); - } - - return tokens -} - -/** - * Compile a string to a template function for the path. - * - * @param {String} str - * @return {Function} - */ -function compile (str) { - return tokensToFunction(parse(str)) -} - -/** - * Expose a method for transforming tokens into the path function. - */ -function tokensToFunction (tokens) { - // Compile all the tokens into regexps. - var matches = new Array(tokens.length); - - // Compile all the patterns before compilation. - for (var i = 0; i < tokens.length; i++) { - if (typeof tokens[i] === 'object') { - matches[i] = new RegExp('^' + tokens[i].pattern + '$'); - } - } - - return function (obj) { - var path = ''; - var data = obj || {}; - - for (var i = 0; i < tokens.length; i++) { - var token = tokens[i]; - - if (typeof token === 'string') { - path += token; - - continue - } - - var value = data[token.name]; - var segment; - - if (value == null) { - if (token.optional) { - continue - } else { - throw new TypeError('Expected "' + token.name + '" to be defined') - } - } - - if (isarray(value)) { - if (!token.repeat) { - throw new TypeError('Expected "' + token.name + '" to not repeat, but received "' + value + '"') - } - - if (value.length === 0) { - if (token.optional) { - continue - } else { - throw new TypeError('Expected "' + token.name + '" to not be empty') - } - } - - for (var j = 0; j < value.length; j++) { - segment = encodeURIComponent(value[j]); - - if (!matches[i].test(segment)) { - throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '", but received "' + segment + '"') - } - - path += (j === 0 ? token.prefix : token.delimiter) + segment; - } - - continue - } - - segment = encodeURIComponent(value); - - if (!matches[i].test(segment)) { - throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '", but received "' + segment + '"') - } - - path += token.prefix + segment; - } - - return path - } -} - -/** - * Escape a regular expression string. - * - * @param {String} str - * @return {String} - */ -function escapeString (str) { - return str.replace(/([.+*?=^!:${}()[\]|\/])/g, '\\$1') -} - -/** - * Escape the capturing group by escaping special characters and meaning. - * - * @param {String} group - * @return {String} - */ -function escapeGroup (group) { - return group.replace(/([=!:$\/()])/g, '\\$1') -} - -/** - * Attach the keys as a property of the regexp. - * - * @param {RegExp} re - * @param {Array} keys - * @return {RegExp} - */ -function attachKeys (re, keys) { - re.keys = keys; - return re -} - -/** - * Get the flags for a regexp from the options. - * - * @param {Object} options - * @return {String} - */ -function flags (options) { - return options.sensitive ? '' : 'i' -} - -/** - * Pull out keys from a regexp. - * - * @param {RegExp} path - * @param {Array} keys - * @return {RegExp} - */ -function regexpToRegexp (path, keys) { - // Use a negative lookahead to match only capturing groups. - var groups = path.source.match(/\((?!\?)/g); - - if (groups) { - for (var i = 0; i < groups.length; i++) { - keys.push({ - name: i, - prefix: null, - delimiter: null, - optional: false, - repeat: false, - pattern: null - }); - } - } - - return attachKeys(path, keys) -} - -/** - * Transform an array into a regexp. - * - * @param {Array} path - * @param {Array} keys - * @param {Object} options - * @return {RegExp} - */ -function arrayToRegexp (path, keys, options) { - var parts = []; - - for (var i = 0; i < path.length; i++) { - parts.push(pathToRegexp(path[i], keys, options).source); - } - - var regexp = new RegExp('(?:' + parts.join('|') + ')', flags(options)); - - return attachKeys(regexp, keys) -} - -/** - * Create a path regexp from string input. - * - * @param {String} path - * @param {Array} keys - * @param {Object} options - * @return {RegExp} - */ -function stringToRegexp (path, keys, options) { - var tokens = parse(path); - var re = tokensToRegExp(tokens, options); - - // Attach keys back to the regexp. - for (var i = 0; i < tokens.length; i++) { - if (typeof tokens[i] !== 'string') { - keys.push(tokens[i]); - } - } - - return attachKeys(re, keys) -} - -/** - * Expose a function for taking tokens and returning a RegExp. - * - * @param {Array} tokens - * @param {Array} keys - * @param {Object} options - * @return {RegExp} - */ -function tokensToRegExp (tokens, options) { - options = options || {}; - - var strict = options.strict; - var end = options.end !== false; - var route = ''; - var lastToken = tokens[tokens.length - 1]; - var endsWithSlash = typeof lastToken === 'string' && /\/$/.test(lastToken); - - // Iterate over the tokens and create our regexp string. - for (var i = 0; i < tokens.length; i++) { - var token = tokens[i]; - - if (typeof token === 'string') { - route += escapeString(token); - } else { - var prefix = escapeString(token.prefix); - var capture = token.pattern; - - if (token.repeat) { - capture += '(?:' + prefix + capture + ')*'; - } - - if (token.optional) { - if (prefix) { - capture = '(?:' + prefix + '(' + capture + '))?'; - } else { - capture = '(' + capture + ')?'; - } - } else { - capture = prefix + '(' + capture + ')'; - } - - route += capture; - } - } - - // In non-strict mode we allow a slash at the end of match. If the path to - // match already ends with a slash, we remove it for consistency. The slash - // is valid at the end of a path match, not in the middle. This is important - // in non-ending mode, where "/test/" shouldn't match "/test//route". - if (!strict) { - route = (endsWithSlash ? route.slice(0, -2) : route) + '(?:\\/(?=$))?'; - } - - if (end) { - route += '$'; - } else { - // In non-ending mode, we need the capturing groups to match as much as - // possible by using a positive lookahead to the end or next path segment. - route += strict && endsWithSlash ? '' : '(?=\\/|$)'; - } - - return new RegExp('^' + route, flags(options)) -} - -/** - * Normalize the given path string, returning a regular expression. - * - * An empty array can be passed in for the keys, which will hold the - * placeholder key descriptions. For example, using `/user/:id`, `keys` will - * contain `[{ name: 'id', delimiter: '/', optional: false, repeat: false }]`. - * - * @param {(String|RegExp|Array)} path - * @param {Array} [keys] - * @param {Object} [options] - * @return {RegExp} - */ -function pathToRegexp (path, keys, options) { - keys = keys || []; - - if (!isarray(keys)) { - options = keys; - keys = []; - } else if (!options) { - options = {}; - } - - if (path instanceof RegExp) { - return regexpToRegexp(path, keys, options) - } - - if (isarray(path)) { - return arrayToRegexp(path, keys, options) - } - - return stringToRegexp(path, keys, options) -} - -pathToRegexp_1.parse = parse_1; -pathToRegexp_1.compile = compile_1; -pathToRegexp_1.tokensToFunction = tokensToFunction_1; -pathToRegexp_1.tokensToRegExp = tokensToRegExp_1; - /** * Module dependencies. */ - + var pathtoRegexp = require('path-to-regexp'); /** * Short-cuts for global-object checks @@ -811,7 +413,7 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1; var orig = path; var pageBase = this._getBase(); - if (path.indexOf(pageBase) === 0) { + if (path === pageBase || path.indexOf(pageBase + '/') === 0 || path.indexOf(pageBase + '?') === 0 || path.indexOf(pageBase + '#') === 0) { path = path.substr(pageBase.length); } @@ -893,15 +495,9 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1; var window = this._window; var loc = window.location; - - /* - when the port is the default http port 80, internet explorer 11 - returns an empty string for loc.port, so we need to compare loc.port - with an empty string if url.port is the default port 80. - */ return loc.protocol === url.protocol && loc.hostname === url.hostname && - (loc.port === url.port || loc.port === '' && url.port === 80); + loc.port === url.port; }; /** @@ -1073,7 +669,8 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1; var hashbang = _page._hashbang; var pageBase = _page._getBase(); - if ('/' === path[0] && 0 !== path.indexOf(pageBase)) path = pageBase + (hashbang ? '#!' : '') + path; + if ('/' === path[0] && path !== pageBase && 0 !== path.indexOf(pageBase + '/') && 0 !== path.indexOf(pageBase + '?') && 0 !== path.indexOf(pageBase + '#')) path = pageBase + (hashbang ? '#!' : '') + path; +// if ('/' === path[0] && 0 !== path.indexOf(pageBase)) path = pageBase + (hashbang ? '#!' : '') + path; var i = path.indexOf('?'); this.canonicalPath = path; @@ -1152,7 +749,7 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1; opts.strict = opts.strict || page._strict; this.path = (path === '*') ? '(.*)' : path; this.method = 'GET'; - this.regexp = pathToRegexp_1(this.path, this.keys = [], opts); + this.regexp = pathtoRegexp(this.path, this.keys = [], opts); } /** @@ -1189,8 +786,6 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1; m = this.regexp.exec(decodeURIComponent(pathname)); if (!m) return false; - - delete params[0] for (var i = 1, len = m.length; i < len; ++i) { var key = keys[i - 1]; @@ -1209,11 +804,7 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1; */ var globalPage = createPage(); - var page_js = globalPage; - var default_1 = globalPage; - -page_js.default = default_1; - -return page_js; + module.exports = globalPage; + module.exports.default = globalPage; }))); diff --git a/page.mjs b/page.mjs index bb48775d..9aa3132a 100644 --- a/page.mjs +++ b/page.mjs @@ -1,406 +1,8 @@ -var isarray = Array.isArray || function (arr) { - return Object.prototype.toString.call(arr) == '[object Array]'; -}; - -/** - * Expose `pathToRegexp`. - */ -var pathToRegexp_1 = pathToRegexp; -var parse_1 = parse; -var compile_1 = compile; -var tokensToFunction_1 = tokensToFunction; -var tokensToRegExp_1 = tokensToRegExp; - -/** - * The main path matching regexp utility. - * - * @type {RegExp} - */ -var PATH_REGEXP = new RegExp([ - // Match escaped characters that would otherwise appear in future matches. - // This allows the user to escape special characters that won't transform. - '(\\\\.)', - // Match Express-style parameters and un-named parameters with a prefix - // and optional suffixes. Matches appear as: - // - // "/:test(\\d+)?" => ["/", "test", "\d+", undefined, "?", undefined] - // "/route(\\d+)" => [undefined, undefined, undefined, "\d+", undefined, undefined] - // "/*" => ["/", undefined, undefined, undefined, undefined, "*"] - '([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^()])+)\\))?|\\(((?:\\\\.|[^()])+)\\))([+*?])?|(\\*))' -].join('|'), 'g'); - -/** - * Parse a string for the raw tokens. - * - * @param {String} str - * @return {Array} - */ -function parse (str) { - var tokens = []; - var key = 0; - var index = 0; - var path = ''; - var res; - - while ((res = PATH_REGEXP.exec(str)) != null) { - var m = res[0]; - var escaped = res[1]; - var offset = res.index; - path += str.slice(index, offset); - index = offset + m.length; - - // Ignore already escaped sequences. - if (escaped) { - path += escaped[1]; - continue - } - - // Push the current path onto the tokens. - if (path) { - tokens.push(path); - path = ''; - } - - var prefix = res[2]; - var name = res[3]; - var capture = res[4]; - var group = res[5]; - var suffix = res[6]; - var asterisk = res[7]; - - var repeat = suffix === '+' || suffix === '*'; - var optional = suffix === '?' || suffix === '*'; - var delimiter = prefix || '/'; - var pattern = capture || group || (asterisk ? '.*' : '[^' + delimiter + ']+?'); - - tokens.push({ - name: name || key++, - prefix: prefix || '', - delimiter: delimiter, - optional: optional, - repeat: repeat, - pattern: escapeGroup(pattern) - }); - } - - // Match any characters still remaining. - if (index < str.length) { - path += str.substr(index); - } - - // If the path exists, push it onto the end. - if (path) { - tokens.push(path); - } - - return tokens -} - -/** - * Compile a string to a template function for the path. - * - * @param {String} str - * @return {Function} - */ -function compile (str) { - return tokensToFunction(parse(str)) -} - -/** - * Expose a method for transforming tokens into the path function. - */ -function tokensToFunction (tokens) { - // Compile all the tokens into regexps. - var matches = new Array(tokens.length); - - // Compile all the patterns before compilation. - for (var i = 0; i < tokens.length; i++) { - if (typeof tokens[i] === 'object') { - matches[i] = new RegExp('^' + tokens[i].pattern + '$'); - } - } - - return function (obj) { - var path = ''; - var data = obj || {}; - - for (var i = 0; i < tokens.length; i++) { - var token = tokens[i]; - - if (typeof token === 'string') { - path += token; - - continue - } - - var value = data[token.name]; - var segment; - - if (value == null) { - if (token.optional) { - continue - } else { - throw new TypeError('Expected "' + token.name + '" to be defined') - } - } - - if (isarray(value)) { - if (!token.repeat) { - throw new TypeError('Expected "' + token.name + '" to not repeat, but received "' + value + '"') - } - - if (value.length === 0) { - if (token.optional) { - continue - } else { - throw new TypeError('Expected "' + token.name + '" to not be empty') - } - } - - for (var j = 0; j < value.length; j++) { - segment = encodeURIComponent(value[j]); - - if (!matches[i].test(segment)) { - throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '", but received "' + segment + '"') - } - - path += (j === 0 ? token.prefix : token.delimiter) + segment; - } - - continue - } - - segment = encodeURIComponent(value); - - if (!matches[i].test(segment)) { - throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '", but received "' + segment + '"') - } - - path += token.prefix + segment; - } - - return path - } -} - -/** - * Escape a regular expression string. - * - * @param {String} str - * @return {String} - */ -function escapeString (str) { - return str.replace(/([.+*?=^!:${}()[\]|\/])/g, '\\$1') -} - -/** - * Escape the capturing group by escaping special characters and meaning. - * - * @param {String} group - * @return {String} - */ -function escapeGroup (group) { - return group.replace(/([=!:$\/()])/g, '\\$1') -} - -/** - * Attach the keys as a property of the regexp. - * - * @param {RegExp} re - * @param {Array} keys - * @return {RegExp} - */ -function attachKeys (re, keys) { - re.keys = keys; - return re -} - -/** - * Get the flags for a regexp from the options. - * - * @param {Object} options - * @return {String} - */ -function flags (options) { - return options.sensitive ? '' : 'i' -} - -/** - * Pull out keys from a regexp. - * - * @param {RegExp} path - * @param {Array} keys - * @return {RegExp} - */ -function regexpToRegexp (path, keys) { - // Use a negative lookahead to match only capturing groups. - var groups = path.source.match(/\((?!\?)/g); - - if (groups) { - for (var i = 0; i < groups.length; i++) { - keys.push({ - name: i, - prefix: null, - delimiter: null, - optional: false, - repeat: false, - pattern: null - }); - } - } - - return attachKeys(path, keys) -} - -/** - * Transform an array into a regexp. - * - * @param {Array} path - * @param {Array} keys - * @param {Object} options - * @return {RegExp} - */ -function arrayToRegexp (path, keys, options) { - var parts = []; - - for (var i = 0; i < path.length; i++) { - parts.push(pathToRegexp(path[i], keys, options).source); - } - - var regexp = new RegExp('(?:' + parts.join('|') + ')', flags(options)); - - return attachKeys(regexp, keys) -} - -/** - * Create a path regexp from string input. - * - * @param {String} path - * @param {Array} keys - * @param {Object} options - * @return {RegExp} - */ -function stringToRegexp (path, keys, options) { - var tokens = parse(path); - var re = tokensToRegExp(tokens, options); - - // Attach keys back to the regexp. - for (var i = 0; i < tokens.length; i++) { - if (typeof tokens[i] !== 'string') { - keys.push(tokens[i]); - } - } - - return attachKeys(re, keys) -} - -/** - * Expose a function for taking tokens and returning a RegExp. - * - * @param {Array} tokens - * @param {Array} keys - * @param {Object} options - * @return {RegExp} - */ -function tokensToRegExp (tokens, options) { - options = options || {}; - - var strict = options.strict; - var end = options.end !== false; - var route = ''; - var lastToken = tokens[tokens.length - 1]; - var endsWithSlash = typeof lastToken === 'string' && /\/$/.test(lastToken); - - // Iterate over the tokens and create our regexp string. - for (var i = 0; i < tokens.length; i++) { - var token = tokens[i]; - - if (typeof token === 'string') { - route += escapeString(token); - } else { - var prefix = escapeString(token.prefix); - var capture = token.pattern; - - if (token.repeat) { - capture += '(?:' + prefix + capture + ')*'; - } - - if (token.optional) { - if (prefix) { - capture = '(?:' + prefix + '(' + capture + '))?'; - } else { - capture = '(' + capture + ')?'; - } - } else { - capture = prefix + '(' + capture + ')'; - } - - route += capture; - } - } - - // In non-strict mode we allow a slash at the end of match. If the path to - // match already ends with a slash, we remove it for consistency. The slash - // is valid at the end of a path match, not in the middle. This is important - // in non-ending mode, where "/test/" shouldn't match "/test//route". - if (!strict) { - route = (endsWithSlash ? route.slice(0, -2) : route) + '(?:\\/(?=$))?'; - } - - if (end) { - route += '$'; - } else { - // In non-ending mode, we need the capturing groups to match as much as - // possible by using a positive lookahead to the end or next path segment. - route += strict && endsWithSlash ? '' : '(?=\\/|$)'; - } - - return new RegExp('^' + route, flags(options)) -} - -/** - * Normalize the given path string, returning a regular expression. - * - * An empty array can be passed in for the keys, which will hold the - * placeholder key descriptions. For example, using `/user/:id`, `keys` will - * contain `[{ name: 'id', delimiter: '/', optional: false, repeat: false }]`. - * - * @param {(String|RegExp|Array)} path - * @param {Array} [keys] - * @param {Object} [options] - * @return {RegExp} - */ -function pathToRegexp (path, keys, options) { - keys = keys || []; - - if (!isarray(keys)) { - options = keys; - keys = []; - } else if (!options) { - options = {}; - } - - if (path instanceof RegExp) { - return regexpToRegexp(path, keys, options) - } - - if (isarray(path)) { - return arrayToRegexp(path, keys, options) - } - - return stringToRegexp(path, keys, options) -} - -pathToRegexp_1.parse = parse_1; -pathToRegexp_1.compile = compile_1; -pathToRegexp_1.tokensToFunction = tokensToFunction_1; -pathToRegexp_1.tokensToRegExp = tokensToRegExp_1; - /** * Module dependencies. */ - + var pathtoRegexp = require('path-to-regexp'); /** * Short-cuts for global-object checks @@ -805,7 +407,7 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1; var orig = path; var pageBase = this._getBase(); - if (path.indexOf(pageBase) === 0) { + if (path === pageBase || path.indexOf(pageBase + '/') === 0 || path.indexOf(pageBase + '?') === 0 || path.indexOf(pageBase + '#') === 0) { path = path.substr(pageBase.length); } @@ -1061,7 +663,8 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1; var hashbang = _page._hashbang; var pageBase = _page._getBase(); - if ('/' === path[0] && 0 !== path.indexOf(pageBase)) path = pageBase + (hashbang ? '#!' : '') + path; + if ('/' === path[0] && path !== pageBase && 0 !== path.indexOf(pageBase + '/') && 0 !== path.indexOf(pageBase + '?') && 0 !== path.indexOf(pageBase + '#')) path = pageBase + (hashbang ? '#!' : '') + path; +// if ('/' === path[0] && 0 !== path.indexOf(pageBase)) path = pageBase + (hashbang ? '#!' : '') + path; var i = path.indexOf('?'); this.canonicalPath = path; @@ -1140,7 +743,7 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1; opts.strict = opts.strict || page._strict; this.path = (path === '*') ? '(.*)' : path; this.method = 'GET'; - this.regexp = pathToRegexp_1(this.path, this.keys = [], opts); + this.regexp = pathtoRegexp(this.path, this.keys = [], opts); } /** @@ -1195,9 +798,5 @@ pathToRegexp_1.tokensToRegExp = tokensToRegExp_1; */ var globalPage = createPage(); - var page_js = globalPage; - var default_1 = globalPage; - -page_js.default = default_1; - -export default page_js; + module.exports = globalPage; + module.exports.default = globalPage; diff --git a/test/tests.js b/test/tests.js index 5ed57bcd..7bcf5f87 100644 --- a/test/tests.js +++ b/test/tests.js @@ -542,6 +542,14 @@ page('/user/tj'); }); + it('should handle routes that may match page base', function(done) { + page('/enroll', function(ctx) { + done(); + }); + + page('/enroll'); + }); + it('should handle trailing slashes in path', function(done) { page('/no-trailing', function() { expect(page.strict()).to.equal(false); @@ -699,10 +707,12 @@ describe('Different Base', function() { + /* test a page base we might typically see in localization, which will + exercise a test of a url like '/enroll' above */ before(function(done) { - base = '/newBase'; + base = '/en'; beforeTests({ - base: '/newBase' + base: '/en' }, done); }); From 05a745da9a9201d45c8613eb6b0742959b51359b Mon Sep 17 00:00:00 2001 From: Andy Burke Date: Mon, 2 Dec 2019 18:44:13 -0800 Subject: [PATCH 2/2] fix: rebuild --- page.js | 1 - page.mjs | 1 - 2 files changed, 2 deletions(-) diff --git a/page.js b/page.js index 2b1f37b1..21e47332 100644 --- a/page.js +++ b/page.js @@ -670,7 +670,6 @@ var pageBase = _page._getBase(); if ('/' === path[0] && path !== pageBase && 0 !== path.indexOf(pageBase + '/') && 0 !== path.indexOf(pageBase + '?') && 0 !== path.indexOf(pageBase + '#')) path = pageBase + (hashbang ? '#!' : '') + path; -// if ('/' === path[0] && 0 !== path.indexOf(pageBase)) path = pageBase + (hashbang ? '#!' : '') + path; var i = path.indexOf('?'); this.canonicalPath = path; diff --git a/page.mjs b/page.mjs index 9aa3132a..4ffc8473 100644 --- a/page.mjs +++ b/page.mjs @@ -664,7 +664,6 @@ var pageBase = _page._getBase(); if ('/' === path[0] && path !== pageBase && 0 !== path.indexOf(pageBase + '/') && 0 !== path.indexOf(pageBase + '?') && 0 !== path.indexOf(pageBase + '#')) path = pageBase + (hashbang ? '#!' : '') + path; -// if ('/' === path[0] && 0 !== path.indexOf(pageBase)) path = pageBase + (hashbang ? '#!' : '') + path; var i = path.indexOf('?'); this.canonicalPath = path;