export = Router; /** * @class Router * @author 7kasper * @classdesc * Represents a router object. A router is an easy way to add * routes to the restnio's routing map. */ declare class Router { /** * Constructs a new restnio object. * @param {RestNio} rnio the restnio object to bind on. * @param {string} path the relative path to work on. * @param {RouteMap} [routeMap] - optional explicit RouteMap to write * registered routes into. Defaults to the main `rnio.routes` map. Used by * peer-scoped routers (e.g. {@link InterClient}) to keep their routes * isolated from the server's main route table. */ constructor(rnio: RestNio, path?: string, routeMap?: RouteMap); rnio: import("../RestNio"); path: string; /** @type {RouteMap} */ routeMap: RouteMap; /** * Registers a route on http 'GET' method on specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ httpGet(path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a route on http 'HEAD' method on specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ httpHead(path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a route on http 'POST' method on specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ httpPost(path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a route on http 'PUT' method on specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ httpPut(path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a route on http 'PATCH' method on specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ httpPatch(path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a route on http 'DELETE' method on specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ httpDelete(path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a route on http 'OPTIONS' method on specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ httpOptions(path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a route on http 'TRACE' method on specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ httpTrace(path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a specified route on all or all specified * http methods at specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {(Object.|string[])} [params] - if spreading out, an optional map * of parameter definitions. If a routedef object is given this can be the http methods to register on. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. * @param {string[]} [methods] - if spreading out, the http methods to register on. (Default: all) */ httpAll(path: string, routedef: (RouteDef | RouteFunc), params?: ({ [x: string]: ParamDef; } | string[]), permissions?: string[], isActive?: boolean, methods?: string[]): void; /** * Creates a redirect route for http connections. * @param {string} path the relative path to redirect from. * @param {string} location the relative or absolute path to redirect to. * @param {integer} [code] the http redirect code to use (Default: 302) * @param {boolean} [absolute] wether or not to use an absolute path (Default: false) * @param {string[]} [methods] which methods to redirect from. (Default: all) */ httpRedirect(path: string, location: string, code?: integer, absolute?: boolean, methods?: string[]): void; /** * Get the http prefix for a certain path. * @param {string} methods the allowed http methods. * @returns the http prefix. */ httpPrefix(methods: string): string; /** * Defines / registers a full route specified. * This route functions only for http connections. * @param {string} method the http method to register on. * @param {string} path the relative path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ httpDef(method: string, path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Creates a redirect message for websocket connections. * @param {string} path the relative path to redirect from. * @param {string} location the relative or absolute path to redirect to. * @param {integer} [code] the redirect message to use (Default: 302) * @param {boolean} [absolute] wether or not to use an absolute path (Default: false) */ wsRedirect(path: string, location: string, code?: integer, absolute?: boolean): void; /** * Get the routeprefix for websocket connections. * @returns the websocket prefix. */ wsPrefix(): string; /** * Registers a specific route for websocket only. * @param {string} path the path to register at. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ ws(path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a binary websocket route. * * Named form — `router.wsBin('file', handler)` registers a route at * `wsBin-file`, dispatched when a client has opted into that route via * `client.setBinRoute('file')` and then sends a binary frame. The handler * receives `params = { data: Buffer, size: number }` and the client * instance, matching the standard `(params, client)` signature. * * Unnamed form — `router.wsBin(handler)` registers the *default* binary * route (`wsBin`), which is invoked when a binary frame arrives without * an active `binRoute` (and was not reclaimed as a MessagePack envelope). * If you don't register one, the built-in default throws a `400`. * * @param {(string|RouteDef|RouteFunc)} nameOrDef - route name, or the * route definition itself for the default (unnamed) binary handler. * @param {(RouteDef|RouteFunc)} [routedef] - route definition when a name * is given. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ wsBin(nameOrDef: (string | RouteDef | RouteFunc), routedef?: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a route on http 'GET' method and websocket * on specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ get(path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a route on http 'HEAD' method and websocket * on specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ head(path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a route on http 'POST' method and websocket * on specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ post(path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a route on http 'PUT' method and websocket * on specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ put(path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a route on http 'PATCH' method and websocket * on specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ patch(path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a route on http 'DELETE' method and websocket * on specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ delete(path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a route on http 'OPTIONS' method and websocket * on specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ options(path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a route on http 'TRACE' method and websocket * on specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ trace(path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Registers a specified route on all or all specified * http methods at specified path and websocket on that same specified path. * @param {string} path the path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {(Object.|string[])} [params] - if spreading out, an optional map * of parameter definitions. If a routedef object is given this can be the http methods to register on. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. * @param {string[]} [methods] - if spreading out, the http methods to register on. (Default: all) */ all(path: string, routedef: (RouteDef | RouteFunc), params?: ({ [x: string]: ParamDef; } | string[]), permissions?: string[], isActive?: boolean, methods?: string[]): void; /** * Creates a redirect route for http-clients and a message for websocket-clients. * @param {string} path the relative path to redirect from. * @param {string} location the relative or absolute path to redirect to. * @param {integer} [code] the http redirect code to use (Default: 302) * @param {boolean} [absolute] wether or not to use an absolute path (Default: false) * @param {string[]} [methods] which http methods to redirect from. (Default: all) */ redirect(path: string, location: string, code?: integer, absolute?: boolean, methods?: string[]): void; /** * Defines / registers a full route. * Arguments can be either a route object or an argument * array with function, params and permissions. * @param {string} method the http method to register on. * @param {string} path the relative path to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ def(method: string, path: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Gets the prefix allowing both http and websocket connections. * If methods are profided only those will be allowed in the http prefix. * @param {string} [methods] the http methods to register on. * Defaults to all possible http methods. */ prefix(methods?: string): string; /** * Registers a catch-all relay under `prefix` that transparently forwards * every request landing under it (HTTP + WS) to a target peer link. The * receiving server materialises the caller as a {@link ProxyClient} — * a real persistent in-process client that participates in the local * subscription map, accumulates state, and replies through the same * peer link back to the original caller. From the calling client's * point of view, the forwarded route returns its reply normally: * HTTP gets a response body, WS gets a return frame, subscriptions * deliver shadow-coalesced events. * * Mechanics: * - Internally registers on `${prefix}/:rest*` so any descendant matches. * `params.rest` carries the post-prefix path; the rest of the params * pass through unchanged. * - Resolves `opts.target` (function-or-direct) into a peer-link client * (`InterClient` or peer-promoted `WebSocketClient`). When the * target is missing/falsy, the route throws `[503, 'upstream offline']`. * - Mints one {@link ProxyClient} session per `(caller, peer)` pair. * HTTP callers are single-shot (`mode: 'request'`, fresh id every * request, peer auto-closes the ProxyClient after the handler * returns). WS callers reuse the same id across all requests on * the same peer until disconnect; subs and state persist on the * callee-side ProxyClient. * - Replies (`_proxyr`) re-enter on the calling side and invoke the * matching method on the original caller — `caller.obj(payload)`, * `caller.err(msg, code)`, etc. HTTP callers' first reply writes * the response body and closes; WS callers receive a frame and stay * open. * - Subscriptions made by the callee-side handler back-propagate * automatically (`_proxyrsub`) so the original caller is locally * subscribed on the calling side. Channel publishes on the callee * side fan out as a single `_proxyrchan` shadow frame per peer * regardless of subscriber count. * * Permission gate: pass `opts.permissions` to require perms ON the * calling client (not the upstream). Standard route perm checking * applies, including `:param` substitution. Typical: * `permissions: ['turbine.:turbineID']`. The caller's effective * permissions are also propagated as the actor on the proxy session * and re-clamped against the peer link's connection cap on the * callee side. * * Example: * ```js * router.proxy('/turbine/:turbineID', { * target: (p) => rnio.inter(`turbine-${p.turbineID}`), * permissions: ['turbine.:turbineID'], * }); * ``` * * @param {string} prefix * @param {Object} opts * @param {(Client|function(Object, Client): Client)} opts.target * Peer-link client to forward to, or a resolver function. * @param {string[]} [opts.permissions] * @param {string[]} [opts.methods] - HTTP methods to register for. * Defaults to all. WS dispatch always registered. * @param {number} [opts.timeoutMs] - HTTP single-shot timeout (default * 30000). Persistent WS sessions ignore this. */ proxy(prefix: string, opts?: { target: (Client | ((arg0: any, arg1: Client) => Client)); permissions?: string[]; methods?: string[]; timeoutMs?: number; }): void; /** * Imports & registers another router to a relative path. * You can enter empty or nothign string for path, to stack / merge * routers. * @param {string} [path=''] - the path to register the route on. * @param {RouteBack} router - The actual router function to use. * @param {boolean} [redirect=false] - whether or not to redirect index * of imported router to non trailing slash. If true * is specified /dogs will for instance redirect to /dogs/. */ use(path?: string, router: RouteBack, redirect?: boolean): void; /** * Registers a specified route on a specific full path. * Usually used to define special extra routes such as `wsConnect`, `wsClose` or `404` * WARNING: fullpath is not just the /example/test url, * it includes special formatting differentiating websocket * and multiple HTTP methods. Only use when you know what * you are doing. * @param {string} fullpath - The fullpath to register on. * @param {(RouteDef|RouteFunc)} routedef - either the main route function * that is to be executed or the full definition provided in style of a `RouteDef`. * @param {Object.} [params] - if spreading out, an optional map * of parameter definitions. * @param {string[]} [permissions] - if spreading out, an optional set of permissions. * @param {boolean} [isActive] - if spreading out, an optional chance to define * the route to be inactive. */ on(fullpath: string, routedef: (RouteDef | RouteFunc), params?: { [x: string]: Route.ParamDef; }, permissions?: string[], isActive?: boolean): void; /** * Defines a full route to the parent restnio instance. * @param {string} fullpath - the fullpath to register on. * @param {Route} route - the routing properties that happen on sepecief path. */ defFull(fullpath: string, route: Route): void; } declare namespace Router { export { allHttpMethods, httpRegex, RestNio, Route, RouteFunc, RouteDef, ParamDef, RouteMap, RouteBack }; } import Route = require("./Route"); declare const allHttpMethods: string[]; declare const httpRegex: RegExp; type RestNio = import("../RestNio"); type Route = import("./Route"); type RouteFunc = import("./Route").RouteFunc; type RouteDef = import("./Route").RouteDef; type ParamDef = import("./Route").ParamDef; type RouteMap = import("../util/RouteMap"); /** * Describes the routeback. * In RestNio routers are mostly used inside these callback-style * functions that accept the router instance. */ type RouteBack = (router: Router, rnio: RestNio) => any;