mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-13 01:30:37 +08:00
Fix: trie meta
This commit is contained in:
parent
385147f784
commit
f58c10e34c
@ -62,7 +62,7 @@ export async function compareAndWriteFile(span: Span, linesA: string[], filePath
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isEqual) {
|
if (isEqual) {
|
||||||
console.log(picocolors.dim(`same content, bail out writing: ${filePath}`));
|
console.log(picocolors.gray(picocolors.dim(`same content, bail out writing: ${filePath}`)));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,6 +154,14 @@ const processRuleSet = (ruleSet: string[]) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ruleSet.includes('DOMAIN,github.com')) {
|
||||||
|
console.log(trie.inspect(0, (meta) => ({
|
||||||
|
index: ruleSet[unpack(meta!)[0]],
|
||||||
|
flag: unpack(meta!)[1] === flagDomain ? 'DOMAIN' : 'DOMAIN-SUFFIX'
|
||||||
|
})));
|
||||||
|
console.log(trie.root);
|
||||||
|
}
|
||||||
|
|
||||||
const dumped = trie.dumpWithMeta();
|
const dumped = trie.dumpWithMeta();
|
||||||
for (let i = 0, len = dumped.length; i < len; i++) {
|
for (let i = 0, len = dumped.length; i < len; i++) {
|
||||||
const [originalIndex, flag] = unpack(dumped[i][1]);
|
const [originalIndex, flag] = unpack(dumped[i][1]);
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { fastStringArrayJoin } from './misc';
|
import { fastStringArrayJoin } from './misc';
|
||||||
import { inspect } from 'node:util';
|
import util from 'node:util';
|
||||||
|
|
||||||
const noop = () => { /** noop */ };
|
const noop = () => { /** noop */ };
|
||||||
|
|
||||||
@ -14,22 +14,29 @@ type TrieNode<Meta = any> = [
|
|||||||
Meta /** meta */
|
Meta /** meta */
|
||||||
];
|
];
|
||||||
|
|
||||||
const deepTrieNodeToJSON = (node: TrieNode) => {
|
const deepTrieNodeToJSON = (
|
||||||
|
node: TrieNode,
|
||||||
|
unpackMeta: ((meta?: any) => string) | undefined
|
||||||
|
) => {
|
||||||
const obj: Record<string, any> = {};
|
const obj: Record<string, any> = {};
|
||||||
if (node[0]) {
|
if (node[0]) {
|
||||||
obj['[start]'] = node[0];
|
obj['[start]'] = node[0];
|
||||||
}
|
}
|
||||||
if (node[3] !== undefined) {
|
if (node[3] != null) {
|
||||||
obj['[meta]'] = node[3];
|
if (unpackMeta) {
|
||||||
|
obj['[meta]'] = unpackMeta(node[3]);
|
||||||
|
} else {
|
||||||
|
obj['[meta]'] = node[3];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
node[2].forEach((value, key) => {
|
node[2].forEach((value, key) => {
|
||||||
obj[key] = deepTrieNodeToJSON(value);
|
obj[key] = deepTrieNodeToJSON(value, unpackMeta);
|
||||||
});
|
});
|
||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
||||||
|
|
||||||
const createNode = <Meta = any>(parent: TrieNode | null = null, meta: Meta | null = null): TrieNode => {
|
const createNode = <Meta = any>(parent: TrieNode | null = null): TrieNode => {
|
||||||
return [false, parent, new Map<string, TrieNode>(), meta] as TrieNode<Meta>;
|
return [false, parent, new Map<string, TrieNode>(), null] as TrieNode<Meta>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const hostnameToTokens = (hostname: string): string[] => {
|
export const hostnameToTokens = (hostname: string): string[] => {
|
||||||
@ -102,7 +109,6 @@ export const createTrie = <Meta = any>(from?: string[] | Set<string> | null, smo
|
|||||||
node = newNode;
|
node = newNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
node[3] = meta!;
|
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -132,6 +138,7 @@ export const createTrie = <Meta = any>(from?: string[] | Set<string> | null, smo
|
|||||||
}
|
}
|
||||||
|
|
||||||
node[0] = true;
|
node[0] = true;
|
||||||
|
node[3] = meta!;
|
||||||
}
|
}
|
||||||
: (suffix: string, meta?: Meta): void => {
|
: (suffix: string, meta?: Meta): void => {
|
||||||
let node: TrieNode<Meta> = root;
|
let node: TrieNode<Meta> = root;
|
||||||
@ -145,7 +152,6 @@ export const createTrie = <Meta = any>(from?: string[] | Set<string> | null, smo
|
|||||||
node = newNode;
|
node = newNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
node[3] = meta!;
|
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -154,9 +160,10 @@ export const createTrie = <Meta = any>(from?: string[] | Set<string> | null, smo
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!node[0]) { // smol tree don't have size, so else-if here
|
if (!node[0]) {
|
||||||
size++;
|
size++;
|
||||||
node[0] = true;
|
node[0] = true;
|
||||||
|
node[3] = meta!;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -447,6 +454,11 @@ export const createTrie = <Meta = any>(from?: string[] | Set<string> | null, smo
|
|||||||
from.forEach((value) => add(value));
|
from.forEach((value) => add(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const inspect = (depth: number, unpackMeta?: (meta?: Meta) => any) => fastStringArrayJoin(
|
||||||
|
JSON.stringify(deepTrieNodeToJSON(root, unpackMeta), null, 2).split('\n').map((line) => ' '.repeat(depth) + line),
|
||||||
|
'\n'
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
add,
|
add,
|
||||||
contains,
|
contains,
|
||||||
@ -467,11 +479,8 @@ export const createTrie = <Meta = any>(from?: string[] | Set<string> | null, smo
|
|||||||
return root;
|
return root;
|
||||||
},
|
},
|
||||||
whitelist,
|
whitelist,
|
||||||
|
inspect,
|
||||||
[inspect.custom]: (depth: number) => fastStringArrayJoin(
|
[util.inspect.custom]: inspect,
|
||||||
JSON.stringify(deepTrieNodeToJSON(root), null, 2).split('\n').map((line) => ' '.repeat(depth) + line),
|
|
||||||
'\n'
|
|
||||||
),
|
|
||||||
smolTree
|
smolTree
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user