Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions packages/router/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ const PARAM_NAME = /^[\w-]+$/

const FILTER_AFTER = 1024

/**
* Each node starts as a copy of this one, which V8 makes in one step. An
* object literal with `undefined` values would define each property in turn,
* which adds up while a cold start builds the tree.
*/
const EMPTY_NODE: Readonly<Node<never>> = {
literals: undefined,
literal: undefined,
literalNode: undefined,
param: undefined,
catchAlls: undefined,
route: undefined,
pending: undefined,
}

/**
* Matches request paths against OpenAPI path templates, the way oRPC routes
* them:
Expand Down Expand Up @@ -229,7 +244,7 @@ function routesOf<T>(methods: Map<string, MethodRoutes<T>>, method: string): Met
let routes = methods.get(method)
if (routes === undefined) {
routes = {
root: createNode(),
root: { ...EMPTY_NODE },
statics: new Map(),
filter: undefined,
unfiltered: 0,
Expand Down Expand Up @@ -421,7 +436,7 @@ function expand<T>(node: Node<T>, pending: unknown[], start: number, whole: bool
}

if (!segment.catchAll) {
child.param ??= createNode()
child.param ??= { ...EMPTY_NODE }
child = child.param
continue
}
Expand Down Expand Up @@ -471,7 +486,7 @@ function literalChild<T>(node: Node<T>, segment: string): Node<T> {
if (node.literals === undefined) {
if (node.literalNode === undefined) {
node.literal = segment
node.literalNode = createNode()
node.literalNode = { ...EMPTY_NODE }
return node.literalNode
}
if (segment === node.literal) {
Expand All @@ -485,24 +500,12 @@ function literalChild<T>(node: Node<T>, segment: string): Node<T> {

let child = node.literals.get(segment)
if (child === undefined) {
child = createNode()
child = { ...EMPTY_NODE }
node.literals.set(segment, child)
}
return child
}

function createNode<T>(): Node<T> {
return {
literals: undefined,
literal: undefined,
literalNode: undefined,
param: undefined,
catchAlls: undefined,
route: undefined,
pending: undefined,
}
}

function parse(path: `/${string}`): Segment[] {
return trimTrailingSlash(path).split('/').map(toSegment).slice(1)
}
Expand Down
Loading