summaryrefslogtreecommitdiff
path: root/packages/integrations/cloudflare/src
diff options
context:
space:
mode:
authorGravatar Veit Bjarsch <veitbjarsch@web.de> 2024-09-23 08:11:20 +0200
committerGravatar GitHub <noreply@github.com> 2024-09-23 08:11:20 +0200
commit09b6b3fb564ad2349bef2098576a47d18ef401c0 (patch)
tree336ee6f233d80b914c7f781791c0d3c489250648 /packages/integrations/cloudflare/src
parent90771cd5fd034566ffdebfbf2da320bd4417309b (diff)
downloadastro-09b6b3fb564ad2349bef2098576a47d18ef401c0.tar.gz
astro-09b6b3fb564ad2349bef2098576a47d18ef401c0.tar.zst
astro-09b6b3fb564ad2349bef2098576a47d18ef401c0.zip
Reduce amount of rules in routes json file (#394)
Diffstat (limited to 'packages/integrations/cloudflare/src')
-rw-r--r--packages/integrations/cloudflare/src/utils/generate-routes-json.ts35
1 files changed, 33 insertions, 2 deletions
diff --git a/packages/integrations/cloudflare/src/utils/generate-routes-json.ts b/packages/integrations/cloudflare/src/utils/generate-routes-json.ts
index 60f598d13..353d275c6 100644
--- a/packages/integrations/cloudflare/src/utils/generate-routes-json.ts
+++ b/packages/integrations/cloudflare/src/utils/generate-routes-json.ts
@@ -128,6 +128,31 @@ class PathTrie {
}
}
+ /**
+ * 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);
@@ -244,7 +269,6 @@ export async function createRoutesFile(
for (const includePath of includePaths) {
includeTrie.insert(includePath);
}
- const [deduplicatedIncludePaths, includedPathsHaveWildcard] = includeTrie.getAllPaths();
const excludeTrie = new PathTrie();
for (const excludePath of excludePaths) {
@@ -256,7 +280,14 @@ export async function createRoutesFile(
if (excludePath[0] === '*') continue;
excludeTrie.insert(excludePath);
}
- const [deduplicatedExcludePaths, _excludedPathsHaveWildcard] = excludeTrie.getAllPaths();
+
+ const [deduplicatedIncludePaths, includedPathsHaveWildcard] = includeTrie
+ .reduceAllPaths(excludeTrie)
+ .getAllPaths();
+
+ const [deduplicatedExcludePaths, _excludedPathsHaveWildcard] = excludeTrie
+ .reduceAllPaths(includeTrie)
+ .getAllPaths();
/**
* Cloudflare allows no more than 100 include/exclude rules combined