Home / Class/ PathTrie Class — astro Architecture

PathTrie Class — astro Architecture

Architecture documentation for the PathTrie class in generate-routes-json.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  55782c85_47d4_4354_44a2_7507d9d6dbee["PathTrie"]
  da981689_3d65_7d59_3c95_800d340ef108["generate-routes-json.ts"]
  55782c85_47d4_4354_44a2_7507d9d6dbee -->|defined in| da981689_3d65_7d59_3c95_800d340ef108
  7d5a9279_f4ed_84fd_5a84_bdbc283d0de6["constructor()"]
  55782c85_47d4_4354_44a2_7507d9d6dbee -->|method| 7d5a9279_f4ed_84fd_5a84_bdbc283d0de6
  50db76a0_b0ab_7605_0653_203d0278f34d["insert()"]
  55782c85_47d4_4354_44a2_7507d9d6dbee -->|method| 50db76a0_b0ab_7605_0653_203d0278f34d
  1516aa5b_1a39_c867_d966_059e1c41856a["dfs()"]
  55782c85_47d4_4354_44a2_7507d9d6dbee -->|method| 1516aa5b_1a39_c867_d966_059e1c41856a
  55fc1e94_7bfe_6a4d_6e4d_c4ae7e7158bb["reduce()"]
  55782c85_47d4_4354_44a2_7507d9d6dbee -->|method| 55fc1e94_7bfe_6a4d_6e4d_c4ae7e7158bb
  298714b3_acee_7cfa_6509_a32cf6723cc8["reduceAllPaths()"]
  55782c85_47d4_4354_44a2_7507d9d6dbee -->|method| 298714b3_acee_7cfa_6509_a32cf6723cc8
  fb74c78c_04f8_7b51_cb60_ee71ba657144["getAllPaths()"]
  55782c85_47d4_4354_44a2_7507d9d6dbee -->|method| fb74c78c_04f8_7b51_cb60_ee71ba657144

Relationship Graph

Source Code

packages/integrations/cloudflare/src/utils/generate-routes-json.ts lines 88–164

class PathTrie {
	root: TrieNode;
	returnHasWildcard = false;

	constructor() {
		this.root = new TrieNode();
	}

	insert(thisPath: string[]) {
		let node = this.root;
		for (const segment of thisPath) {
			if (segment === '*') {
				node.hasWildcardChild = true;
				break;
			}
			if (!node.children.has(segment)) {
				node.children.set(segment, new TrieNode());
			}

			node = node.children.get(segment)!;
		}

		node.isEndOfPath = true;
	}

	/**
	 * Depth-first search (dfs), traverses the "graph"  segment by segment until the end or wildcard (*).
	 * It makes sure that all necessary paths are returned, but not paths with an existing wildcard prefix.
	 * e.g. if we have a path like /foo/* and /foo/bar, we only want to return /foo/*
	 */
	private dfs(node: TrieNode, thisPath: string[], allPaths: string[][]): void {
		if (node.hasWildcardChild) {
			this.returnHasWildcard = true;
			allPaths.push([...thisPath, '*']);
			return;
		}

		if (node.isEndOfPath) {
			allPaths.push([...thisPath]);
		}

		for (const [segment, childNode] of node.children) {
			this.dfs(childNode, [...thisPath, segment], allPaths);
		}
	}

	/**
	 * The reduce function is used to remove unnecessary paths from the trie.
	 * It receives a trie node to compare with the current node.
	 */
	private reduce(compNode: TrieNode, node: TrieNode): void {
		if (node.hasWildcardChild || compNode.hasWildcardChild) return;

		for (const [segment, childNode] of node.children) {
			if (childNode.children.size === 0) continue;

			const compChildNode = compNode.children.get(segment);
			if (compChildNode === undefined) {
				childNode.hasWildcardChild = true;
				continue;
			}

			this.reduce(compChildNode, childNode);
		}
	}

	reduceAllPaths(compTrie: PathTrie): this {
		this.reduce(compTrie.root, this.root);
		return this;
	}

	getAllPaths(): [string[][], boolean] {
		const allPaths: string[][] = [];
		this.dfs(this.root, [], allPaths);
		return [allPaths, this.returnHasWildcard];
	}
}

Frequently Asked Questions

What is the PathTrie class?
PathTrie is a class in the astro codebase, defined in packages/integrations/cloudflare/src/utils/generate-routes-json.ts.
Where is PathTrie defined?
PathTrie is defined in packages/integrations/cloudflare/src/utils/generate-routes-json.ts at line 88.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free