-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add synchronous mode as an add-on (#164)
* Add a `sync` plugin with a very similar API except synchronous * Run `npm run build` * Update docs * Rename sync to UniversalRouterSync * Reduce bundle size of sync router * Export UniversalRouterSync in umd build
- Loading branch information
Showing
17 changed files
with
1,470 additions
and
63 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,55 @@ | ||
/** | ||
* Universal Router (https://www.kriasoft.com/universal-router/) | ||
* | ||
* Copyright (c) 2015-present Kriasoft. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE.txt file in the root directory of this source tree. | ||
*/ | ||
|
||
import pathToRegexp = require('path-to-regexp') | ||
|
||
export interface Params { | ||
[paramName: string]: any | ||
} | ||
|
||
export interface Context { | ||
[propName: string]: any | ||
} | ||
|
||
export interface ResolveContext extends Context { | ||
pathname: string | ||
} | ||
|
||
export interface RouteContext<C extends Context, R = any> extends ResolveContext { | ||
router: UniversalRouter<C, R> | ||
route: Route | ||
baseUrl: string | ||
path: string | ||
params: Params | ||
keys: pathToRegexp.Key[] | ||
next: (resume?: boolean) => R | ||
} | ||
|
||
export interface Route<C extends Context = any, R = any> { | ||
path?: string | RegExp | Array<string | RegExp> | ||
name?: string | ||
parent?: Route | null | ||
children?: Routes<C, R> | null | ||
action?: (context: RouteContext<C, R> & C, params: Params) => R | void | ||
} | ||
|
||
export type Routes<C extends Context = Context, R = any> = Array<Route<C, R>> | ||
|
||
export interface Options<C extends Context = Context, R = any> { | ||
context?: C | ||
baseUrl?: string | ||
resolveRoute?: (context: C & RouteContext<C, R>, params: Params) => any | ||
errorHandler?: (error: Error & { status?: number }, context: C & RouteContext<C, R>) => any | ||
} | ||
|
||
export default class UniversalRouter<C extends Context = Context, R = any> { | ||
static pathToRegexp: typeof pathToRegexp | ||
constructor(routes: Route<C, R> | Routes<C, R>, options?: Options<C>) | ||
resolve(pathnameOrContext: string | ResolveContext): R | ||
} |
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,90 @@ | ||
/** | ||
* Universal Router (https://www.kriasoft.com/universal-router/) | ||
* | ||
* Copyright (c) 2015-present Kriasoft. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE.txt file in the root directory of this source tree. | ||
*/ | ||
|
||
import pathToRegexp from 'path-to-regexp' | ||
import matchRoute from './matchRoute' | ||
import resolveRoute from './resolveRoute' | ||
import isChildRoute from './isChildRoute' | ||
|
||
class UniversalRouterSync { | ||
constructor(routes, options = {}) { | ||
if (!routes || typeof routes !== 'object') { | ||
throw new TypeError('Invalid routes') | ||
} | ||
|
||
this.baseUrl = options.baseUrl || '' | ||
this.errorHandler = options.errorHandler | ||
this.resolveRoute = options.resolveRoute || resolveRoute | ||
this.context = { router: this, ...options.context } | ||
this.root = Array.isArray(routes) ? { path: '', children: routes, parent: null } : routes | ||
this.root.parent = null | ||
} | ||
|
||
resolve(pathnameOrContext) { | ||
const context = { | ||
...this.context, | ||
...(typeof pathnameOrContext === 'string' | ||
? { pathname: pathnameOrContext } | ||
: pathnameOrContext), | ||
} | ||
const match = matchRoute( | ||
this.root, | ||
this.baseUrl, | ||
context.pathname.substr(this.baseUrl.length), | ||
[], | ||
null, | ||
) | ||
const resolve = this.resolveRoute | ||
let matches = null | ||
let nextMatches = null | ||
let currentContext = context | ||
|
||
function next(resume, parent = matches.value.route, prevResult) { | ||
const routeToSkip = prevResult === null && matches.value.route | ||
matches = nextMatches || match.next(routeToSkip) | ||
nextMatches = null | ||
|
||
if (!resume) { | ||
if (matches.done || !isChildRoute(parent, matches.value.route)) { | ||
nextMatches = matches | ||
return null | ||
} | ||
} | ||
|
||
if (matches.done) { | ||
const error = new Error('Route not found') | ||
error.status = 404 | ||
throw error | ||
} | ||
|
||
currentContext = { ...context, ...matches.value } | ||
|
||
const result = resolve(currentContext, matches.value.params) | ||
if (result !== null && result !== undefined) { | ||
return result | ||
} | ||
return next(resume, parent, result) | ||
} | ||
|
||
context.next = next | ||
|
||
try { | ||
return next(true, this.root) | ||
} catch (error) { | ||
if (this.errorHandler) { | ||
return this.errorHandler(error, currentContext) | ||
} | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
UniversalRouterSync.pathToRegexp = pathToRegexp | ||
|
||
export default UniversalRouterSync |
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,21 @@ | ||
/** | ||
* Universal Router (https://www.kriasoft.com/universal-router/) | ||
* | ||
* Copyright (c) 2015-present Kriasoft. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE.txt file in the root directory of this source tree. | ||
*/ | ||
|
||
function isChildRoute(parentRoute, childRoute) { | ||
let route = childRoute | ||
while (route) { | ||
route = route.parent | ||
if (route === parentRoute) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
export default isChildRoute |
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,17 @@ | ||
/** | ||
* Universal Router (https://www.kriasoft.com/universal-router/) | ||
* | ||
* Copyright (c) 2015-present Kriasoft. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE.txt file in the root directory of this source tree. | ||
*/ | ||
|
||
function resolveRoute(context, params) { | ||
if (typeof context.route.action === 'function') { | ||
return context.route.action(context, params) | ||
} | ||
return undefined | ||
} | ||
|
||
export default resolveRoute |
Oops, something went wrong.