trie.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /**
  2. * Hostbane-Optimized Trie based on Mnemonist Trie
  3. */
  4. import { fastStringCompare } from './misc';
  5. import util from 'node:util';
  6. import { noop } from 'foxts/noop';
  7. import { fastStringArrayJoin } from 'foxts/fast-string-array-join';
  8. import { deleteBit, getBit, missingBit, setBit } from 'foxts/bitwise';
  9. import { toASCII } from 'punycode/';
  10. const START = 1 << 1;
  11. const INCLUDE_ALL_SUBDOMAIN = 1 << 2;
  12. type TrieNode<Meta = any> = [
  13. /** end, includeAllSubdomain (.example.org, ||example.com) */ flag: number,
  14. /** parent */ TrieNode | null,
  15. /** children */ Map<string, TrieNode>,
  16. /** token */ token: string,
  17. /** meta */ Meta
  18. ];
  19. function deepTrieNodeToJSON<Meta = unknown>(node: TrieNode,
  20. unpackMeta: ((meta?: Meta) => string) | undefined) {
  21. const obj: Record<string, unknown> = {};
  22. obj['[start]'] = getBit(node[0], START);
  23. obj['[subdomain]'] = getBit(node[0], INCLUDE_ALL_SUBDOMAIN);
  24. if (node[4] != null) {
  25. if (unpackMeta) {
  26. obj['[meta]'] = unpackMeta(node[4]);
  27. } else {
  28. obj['[meta]'] = node[4];
  29. }
  30. }
  31. node[2].forEach((value, key) => {
  32. obj[key] = deepTrieNodeToJSON(value, unpackMeta);
  33. });
  34. return obj;
  35. }
  36. const createNode = <Meta = unknown>(token: string, parent: TrieNode | null = null): TrieNode => [1, parent, new Map<string, TrieNode>(), token, null] as TrieNode<Meta>;
  37. function hostnameToTokens(hostname: string, hostnameFromIndex: number): string[] {
  38. const tokens = hostname.split('.');
  39. const results: string[] = [];
  40. let token = '';
  41. for (let i = hostnameFromIndex, l = tokens.length; i < l; i++) {
  42. token = tokens[i];
  43. if (token.length > 0) {
  44. results.push(token);
  45. } else {
  46. throw new TypeError(JSON.stringify({ hostname, hostnameFromIndex }, null, 2));
  47. }
  48. }
  49. return results;
  50. }
  51. function walkHostnameTokens(hostname: string, onToken: (token: string) => boolean | null, hostnameFromIndex: number): boolean | null {
  52. const tokens = hostname.split('.');
  53. const l = tokens.length - 1;
  54. // we are at the first of hostname, no splitor there
  55. let token = '';
  56. for (let i = l; i >= hostnameFromIndex; i--) {
  57. token = tokens[i];
  58. if (token.length > 0) {
  59. const t = onToken(token);
  60. if (t === null) {
  61. return null;
  62. }
  63. // if the callback returns true, we should skip the rest
  64. if (t) {
  65. return true;
  66. }
  67. }
  68. }
  69. return false;
  70. }
  71. interface FindSingleChildLeafResult<Meta> {
  72. node: TrieNode<Meta>,
  73. toPrune: TrieNode<Meta> | null,
  74. tokenToPrune: string | null,
  75. parent: TrieNode<Meta>
  76. }
  77. abstract class Triebase<Meta = unknown> {
  78. protected readonly $root: TrieNode<Meta> = createNode('$root');
  79. protected $size = 0;
  80. get root() {
  81. return this.$root;
  82. }
  83. constructor(from?: string[] | Set<string> | null) {
  84. // Actually build trie
  85. if (Array.isArray(from)) {
  86. for (let i = 0, l = from.length; i < l; i++) {
  87. this.add(from[i]);
  88. }
  89. } else if (from) {
  90. from.forEach((value) => this.add(value));
  91. }
  92. }
  93. public abstract add(suffix: string, includeAllSubdomain?: boolean, meta?: Meta, hostnameFromIndex?: number): void;
  94. protected walkIntoLeafWithTokens(
  95. tokens: string[],
  96. onLoop: (node: TrieNode, parent: TrieNode, token: string) => void = noop
  97. ) {
  98. let node: TrieNode = this.$root;
  99. let parent: TrieNode = node;
  100. let token: string;
  101. let child: Map<string, TrieNode<Meta>> = node[2];
  102. // reverse lookup from end to start
  103. for (let i = tokens.length - 1; i >= 0; i--) {
  104. token = tokens[i];
  105. // if (token === '') {
  106. // break;
  107. // }
  108. parent = node;
  109. child = node[2];
  110. // cache node index access is 20% faster than direct access when doing twice
  111. if (child.has(token)) {
  112. node = child.get(token)!;
  113. } else {
  114. return null;
  115. }
  116. onLoop(node, parent, token);
  117. }
  118. return { node, parent };
  119. };
  120. protected walkIntoLeafWithSuffix(
  121. suffix: string,
  122. hostnameFromIndex: number,
  123. onLoop: (node: TrieNode, parent: TrieNode, token: string) => void = noop
  124. ) {
  125. let node: TrieNode = this.$root;
  126. let parent: TrieNode = node;
  127. let child: Map<string, TrieNode<Meta>> = node[2];
  128. const onToken = (token: string) => {
  129. // if (token === '') {
  130. // return true;
  131. // }
  132. parent = node;
  133. child = node[2];
  134. if (child.has(token)) {
  135. node = child.get(token)!;
  136. } else {
  137. return null;
  138. }
  139. onLoop(node, parent, token);
  140. return false;
  141. };
  142. if (walkHostnameTokens(suffix, onToken, hostnameFromIndex) === null) {
  143. return null;
  144. }
  145. return { node, parent };
  146. };
  147. public contains(suffix: string, includeAllSubdomain = suffix[0] === '.'): boolean {
  148. const hostnameFromIndex = suffix[0] === '.' ? 1 : 0;
  149. const res = this.walkIntoLeafWithSuffix(suffix, hostnameFromIndex);
  150. if (!res) return false;
  151. if (includeAllSubdomain) return getBit(res.node[0], INCLUDE_ALL_SUBDOMAIN);
  152. return true;
  153. };
  154. private static bfsResults: [node: TrieNode | null, suffix: string[]] = [null, []];
  155. private static dfs<Meta>(this: void, nodeStack: Array<TrieNode<Meta>>, suffixStack: string[][]) {
  156. const node = nodeStack.pop()!;
  157. const suffix = suffixStack.pop()!;
  158. node[2].forEach((childNode, k) => {
  159. // Pushing the child node to the stack for next iteration of DFS
  160. nodeStack.push(childNode);
  161. suffixStack.push([k, ...suffix]);
  162. });
  163. Triebase.bfsResults[0] = node;
  164. Triebase.bfsResults[1] = suffix;
  165. return Triebase.bfsResults;
  166. }
  167. private static dfsWithSort<Meta>(this: void, nodeStack: Array<TrieNode<Meta>>, suffixStack: string[][]) {
  168. const node = nodeStack.pop()!;
  169. const suffix = suffixStack.pop()!;
  170. const child = node[2];
  171. if (child.size) {
  172. const keys = Array.from(child.keys()).sort(Triebase.compare);
  173. for (let i = 0, l = keys.length; i < l; i++) {
  174. const key = keys[i];
  175. const childNode = child.get(key)!;
  176. // Pushing the child node to the stack for next iteration of DFS
  177. nodeStack.push(childNode);
  178. suffixStack.push([key, ...suffix]);
  179. }
  180. }
  181. Triebase.bfsResults[0] = node;
  182. Triebase.bfsResults[1] = suffix;
  183. return Triebase.bfsResults;
  184. }
  185. private walk(
  186. onMatches: (suffix: string[], subdomain: boolean, meta: Meta) => void,
  187. initialNode = this.$root,
  188. initialSuffix: string[] = [],
  189. withSort = false
  190. ) {
  191. const bfsImpl = withSort ? Triebase.dfsWithSort : Triebase.dfs;
  192. const nodeStack: Array<TrieNode<Meta>> = [];
  193. nodeStack.push(initialNode);
  194. // Resolving initial string (begin the start of the stack)
  195. const suffixStack: string[][] = [];
  196. suffixStack.push(initialSuffix);
  197. let node: TrieNode<Meta> = initialNode;
  198. let r;
  199. do {
  200. r = bfsImpl(nodeStack, suffixStack);
  201. node = r[0]!;
  202. const suffix = r[1];
  203. // If the node is a sentinel, we push the suffix to the results
  204. if (getBit(node[0], START)) {
  205. onMatches(suffix, getBit(node[0], INCLUDE_ALL_SUBDOMAIN), node[4]);
  206. }
  207. } while (nodeStack.length);
  208. };
  209. static compare(this: void, a: string, b: string) {
  210. if (a === b) return 0;
  211. return (a.length - b.length) || fastStringCompare(a, b);
  212. }
  213. private walkWithSort(
  214. onMatches: (suffix: string[], subdomain: boolean, meta: Meta) => void,
  215. initialNode = this.$root,
  216. initialSuffix: string[] = []
  217. ) {
  218. const nodeStack: Array<TrieNode<Meta>> = [];
  219. nodeStack.push(initialNode);
  220. // Resolving initial string (begin the start of the stack)
  221. const suffixStack: string[][] = [];
  222. suffixStack.push(initialSuffix);
  223. let node: TrieNode<Meta> = initialNode;
  224. let child: Map<string, TrieNode<Meta>> = node[2];
  225. do {
  226. node = nodeStack.pop()!;
  227. const suffix = suffixStack.pop()!;
  228. child = node[2];
  229. if (child.size) {
  230. const keys = Array.from(child.keys()).sort(Triebase.compare);
  231. for (let i = 0, l = keys.length; i < l; i++) {
  232. const key = keys[i];
  233. const childNode = child.get(key)!;
  234. // Pushing the child node to the stack for next iteration of DFS
  235. nodeStack.push(childNode);
  236. suffixStack.push([key, ...suffix]);
  237. }
  238. }
  239. // If the node is a sentinel, we push the suffix to the results
  240. if (getBit(node[0], START)) {
  241. onMatches(suffix, getBit(node[0], INCLUDE_ALL_SUBDOMAIN), node[4]);
  242. }
  243. } while (nodeStack.length);
  244. };
  245. protected getSingleChildLeaf(tokens: string[]): FindSingleChildLeafResult<Meta> | null {
  246. let toPrune: TrieNode | null = null;
  247. let tokenToPrune: string | null = null;
  248. const onLoop = (node: TrieNode, parent: TrieNode, token: string) => {
  249. // Keeping track of a potential branch to prune
  250. const child = node[2];
  251. const childSize = child.size + (getBit(node[0], INCLUDE_ALL_SUBDOMAIN) ? 1 : 0);
  252. if (toPrune !== null) { // the most near branch that could potentially being pruned
  253. if (childSize >= 1) {
  254. // The branch has some children, the branch need retain.
  255. // And we need to abort prune that parent branch, so we set it to null
  256. toPrune = null;
  257. tokenToPrune = null;
  258. }
  259. } else if (childSize < 1) {
  260. // There is only one token child, or no child at all, we can prune it safely
  261. // It is now the top-est branch that could potentially being pruned
  262. toPrune = parent;
  263. tokenToPrune = token;
  264. }
  265. };
  266. const res = this.walkIntoLeafWithTokens(tokens, onLoop);
  267. if (res === null) return null;
  268. return { node: res.node, toPrune, tokenToPrune, parent: res.parent };
  269. };
  270. /**
  271. * Method used to retrieve every item in the trie with the given prefix.
  272. */
  273. public find(
  274. inputSuffix: string,
  275. subdomainOnly = inputSuffix[0] === '.',
  276. hostnameFromIndex = inputSuffix[0] === '.' ? 1 : 0
  277. // /** @default true */ includeEqualWithSuffix = true
  278. ): string[] {
  279. const inputTokens = hostnameToTokens(inputSuffix, hostnameFromIndex);
  280. const res = this.walkIntoLeafWithTokens(inputTokens);
  281. if (res === null) return [];
  282. const results: string[] = [];
  283. const onMatches = subdomainOnly
  284. ? (suffix: string[], subdomain: boolean) => { // fast path (default option)
  285. const d = toASCII(fastStringArrayJoin(suffix, '.'));
  286. if (!subdomain && subStringEqual(inputSuffix, d, 1)) return;
  287. results.push(subdomain ? '.' + d : d);
  288. }
  289. : (suffix: string[], subdomain: boolean) => { // fast path (default option)
  290. const d = toASCII(fastStringArrayJoin(suffix, '.'));
  291. results.push(subdomain ? '.' + d : d);
  292. };
  293. this.walk(
  294. onMatches,
  295. res.node, // Performing DFS from prefix
  296. inputTokens
  297. );
  298. return results;
  299. };
  300. /**
  301. * Method used to delete a prefix from the trie.
  302. */
  303. public remove(suffix: string): boolean {
  304. const res = this.getSingleChildLeaf(hostnameToTokens(suffix, 0));
  305. if (res === null) return false;
  306. if (missingBit(res.node[0], START)) return false;
  307. this.$size--;
  308. const { node, toPrune, tokenToPrune } = res;
  309. if (tokenToPrune && toPrune) {
  310. toPrune[2].delete(tokenToPrune);
  311. } else {
  312. node[0] = deleteBit(node[0], START);
  313. }
  314. return true;
  315. };
  316. // eslint-disable-next-line @typescript-eslint/unbound-method -- safe
  317. public delete = this.remove;
  318. /**
  319. * Method used to assert whether the given prefix exists in the Trie.
  320. */
  321. public has(suffix: string, includeAllSubdomain = suffix[0] === '.'): boolean {
  322. const hostnameFromIndex = suffix[0] === '.' ? 1 : 0;
  323. const res = this.walkIntoLeafWithSuffix(suffix, hostnameFromIndex);
  324. if (res === null) return false;
  325. if (missingBit(res.node[0], START)) return false;
  326. if (includeAllSubdomain) return getBit(res.node[0], INCLUDE_ALL_SUBDOMAIN);
  327. return true;
  328. };
  329. public dumpWithoutDot(onSuffix: (suffix: string, subdomain: boolean) => void, withSort = false) {
  330. const handleSuffix = (suffix: string[], subdomain: boolean) => {
  331. onSuffix(toASCII(fastStringArrayJoin(suffix, '.')), subdomain);
  332. };
  333. if (withSort) {
  334. this.walkWithSort(handleSuffix);
  335. } else {
  336. this.walk(handleSuffix);
  337. }
  338. }
  339. public dump(onSuffix: (suffix: string) => void, withSort?: boolean): void;
  340. public dump(onSuffix?: null, withSort?: boolean): string[];
  341. public dump(onSuffix?: ((suffix: string) => void) | null, withSort = false): string[] | void {
  342. const results: string[] = [];
  343. const handleSuffix = onSuffix
  344. ? (suffix: string[], subdomain: boolean) => {
  345. const d = toASCII(fastStringArrayJoin(suffix, '.'));
  346. onSuffix(subdomain ? '.' + d : d);
  347. }
  348. : (suffix: string[], subdomain: boolean) => {
  349. const d = toASCII(fastStringArrayJoin(suffix, '.'));
  350. results.push(subdomain ? '.' + d : d);
  351. };
  352. if (withSort) {
  353. this.walkWithSort(handleSuffix);
  354. } else {
  355. this.walk(handleSuffix);
  356. }
  357. return results;
  358. };
  359. public dumpMeta(onMeta: (meta: Meta) => void, withSort?: boolean): void;
  360. public dumpMeta(onMeta?: null, withSort?: boolean): Meta[];
  361. public dumpMeta(onMeta?: ((meta: Meta) => void) | null, withSort = false): Meta[] | void {
  362. const results: Meta[] = [];
  363. const handleMeta = onMeta
  364. ? (_suffix: string[], _subdomain: boolean, meta: Meta) => onMeta(meta)
  365. : (_suffix: string[], _subdomain: boolean, meta: Meta) => results.push(meta);
  366. if (withSort) {
  367. this.walkWithSort(handleMeta);
  368. } else {
  369. this.walk(handleMeta);
  370. }
  371. return results;
  372. };
  373. public dumpWithMeta(onSuffix: (suffix: string, meta: Meta | undefined) => void, withSort?: boolean): void;
  374. public dumpWithMeta(onMeta?: null, withSort?: boolean): Array<[string, Meta | undefined]>;
  375. public dumpWithMeta(onSuffix?: ((suffix: string, meta: Meta | undefined) => void) | null, withSort = false): Array<[string, Meta | undefined]> | void {
  376. const results: Array<[string, Meta | undefined]> = [];
  377. const handleSuffix = onSuffix
  378. ? (suffix: string[], subdomain: boolean, meta: Meta | undefined) => {
  379. const d = toASCII(fastStringArrayJoin(suffix, '.'));
  380. return onSuffix(subdomain ? '.' + d : d, meta);
  381. }
  382. : (suffix: string[], subdomain: boolean, meta: Meta | undefined) => {
  383. const d = toASCII(fastStringArrayJoin(suffix, '.'));
  384. results.push([subdomain ? '.' + d : d, meta]);
  385. };
  386. if (withSort) {
  387. this.walkWithSort(handleSuffix);
  388. } else {
  389. this.walk(handleSuffix);
  390. }
  391. return results;
  392. };
  393. public inspect(depth: number, unpackMeta?: (meta?: Meta) => any) {
  394. return fastStringArrayJoin(
  395. JSON.stringify(deepTrieNodeToJSON(this.$root, unpackMeta), null, 2).split('\n').map((line) => ' '.repeat(depth) + line),
  396. '\n'
  397. );
  398. }
  399. public [util.inspect.custom](depth: number) {
  400. return this.inspect(depth);
  401. };
  402. public merge(trie: Triebase<Meta>) {
  403. const handleSuffix = (suffix: string[], subdomain: boolean, meta: Meta) => {
  404. this.add(fastStringArrayJoin(suffix, '.'), subdomain, meta);
  405. };
  406. trie.walk(handleSuffix);
  407. return this;
  408. }
  409. }
  410. export class HostnameSmolTrie<Meta = unknown> extends Triebase<Meta> {
  411. public smolTree = true;
  412. add(suffix: string, includeAllSubdomain = suffix[0] === '.', meta?: Meta, hostnameFromIndex = suffix[0] === '.' ? 1 : 0): void {
  413. let node: TrieNode<Meta> = this.$root;
  414. let curNodeChildren: Map<string, TrieNode<Meta>> = node[2];
  415. const onToken = (token: string) => {
  416. curNodeChildren = node[2];
  417. if (curNodeChildren.has(token)) {
  418. node = curNodeChildren.get(token)!;
  419. // During the adding of `[start]blog|.skk.moe` and find out that there is a `[start].skk.moe` in the trie, skip adding the rest of the node
  420. if (getBit(node[0], INCLUDE_ALL_SUBDOMAIN)) {
  421. return true;
  422. }
  423. } else {
  424. const newNode = createNode(token, node);
  425. curNodeChildren.set(token, newNode);
  426. node = newNode;
  427. }
  428. return false;
  429. };
  430. // When walkHostnameTokens returns true, we should skip the rest
  431. if (walkHostnameTokens(suffix, onToken, hostnameFromIndex)) {
  432. return;
  433. }
  434. // If we are in smolTree mode, we need to do something at the end of the loop
  435. if (includeAllSubdomain) {
  436. // Trying to add `[.]sub.example.com` where there is already a `blog.sub.example.com` in the trie
  437. // Make sure parent `[start]sub.example.com` (without dot) is removed (SETINEL to false)
  438. // (/** parent */ node[2]!)[0] = false;
  439. // Removing the rest of the parent's child nodes
  440. node[2].clear();
  441. // The SENTINEL of this node will be set to true at the end of the function, so we don't need to set it here
  442. // we can use else-if here, because the children is now empty, we don't need to check the leading "."
  443. } else if (getBit(node[0], INCLUDE_ALL_SUBDOMAIN)) {
  444. // Trying to add `example.com` when there is already a `.example.com` in the trie
  445. // No need to increment size and set SENTINEL to true (skip this "new" item)
  446. return;
  447. }
  448. node[0] = setBit(node[0], START);
  449. if (includeAllSubdomain) {
  450. node[0] = setBit(node[0], INCLUDE_ALL_SUBDOMAIN);
  451. } else {
  452. node[0] = deleteBit(node[0], INCLUDE_ALL_SUBDOMAIN);
  453. }
  454. node[4] = meta!;
  455. }
  456. public whitelist(suffix: string, includeAllSubdomain = suffix[0] === '.', hostnameFromIndex = suffix[0] === '.' ? 1 : 0) {
  457. const tokens = hostnameToTokens(suffix, hostnameFromIndex);
  458. const res = this.getSingleChildLeaf(tokens);
  459. if (res === null) return;
  460. const { node, toPrune, tokenToPrune } = res;
  461. // Trying to whitelist `[start].sub.example.com` where there might already be a `[start]blog.sub.example.com` in the trie
  462. if (includeAllSubdomain) {
  463. // If there is a `[start]sub.example.com` here, remove it
  464. node[0] = deleteBit(node[0], INCLUDE_ALL_SUBDOMAIN);
  465. // Removing all the child nodes by empty the children
  466. node[2].clear();
  467. // we do not remove sub.example.com for now, we will do that later
  468. } else {
  469. // Trying to whitelist `example.com` when there is already a `.example.com` in the trie
  470. node[0] = deleteBit(node[0], INCLUDE_ALL_SUBDOMAIN);
  471. }
  472. if (includeAllSubdomain) {
  473. node[1]?.[2].delete(node[3]);
  474. } else if (missingBit(node[0], START) && node[1]) {
  475. return;
  476. }
  477. if (toPrune && tokenToPrune) {
  478. toPrune[2].delete(tokenToPrune);
  479. } else {
  480. node[0] = deleteBit(node[0], START);
  481. }
  482. cleanUpEmptyTrailNode(node);
  483. };
  484. }
  485. function cleanUpEmptyTrailNode(node: TrieNode<unknown>) {
  486. if (
  487. // the current node is not an "end node", a.k.a. not the start of a domain
  488. missingBit(node[0], START)
  489. // also no leading "." (no subdomain)
  490. && missingBit(node[0], INCLUDE_ALL_SUBDOMAIN)
  491. // child is empty
  492. && node[2].size === 0
  493. // has parent: we need to detele the cureent node from the parent
  494. // we also need to recursively clean up the parent node
  495. && node[1]
  496. ) {
  497. node[1][2].delete(node[3]);
  498. // finish of the current stack
  499. return cleanUpEmptyTrailNode(node[1]);
  500. }
  501. }
  502. export class HostnameTrie<Meta = unknown> extends Triebase<Meta> {
  503. get size() {
  504. return this.$size;
  505. }
  506. add(suffix: string, includeAllSubdomain = suffix[0] === '.', meta?: Meta, hostnameFromIndex = suffix[0] === '.' ? 1 : 0): void {
  507. let node: TrieNode<Meta> = this.$root;
  508. let child: Map<string, TrieNode<Meta>> = node[2];
  509. const onToken = (token: string) => {
  510. child = node[2];
  511. if (child.has(token)) {
  512. node = child.get(token)!;
  513. } else {
  514. const newNode = createNode(token, node);
  515. child.set(token, newNode);
  516. node = newNode;
  517. }
  518. return false;
  519. };
  520. // When walkHostnameTokens returns true, we should skip the rest
  521. if (walkHostnameTokens(suffix, onToken, hostnameFromIndex)) {
  522. return;
  523. }
  524. // if same entry has been added before, skip
  525. if (getBit(node[0], START)) {
  526. return;
  527. }
  528. this.$size++;
  529. node[0] = setBit(node[0], START);
  530. if (includeAllSubdomain) {
  531. node[0] = setBit(node[0], INCLUDE_ALL_SUBDOMAIN);
  532. } else {
  533. node[0] = deleteBit(node[0], INCLUDE_ALL_SUBDOMAIN);
  534. }
  535. node[4] = meta!;
  536. }
  537. }
  538. // function deepEqualArray(a: string[], b: string[]) {
  539. // let len = a.length;
  540. // if (len !== b.length) return false;
  541. // while (len--) {
  542. // if (a[len] !== b[len]) return false;
  543. // }
  544. // return true;
  545. // };
  546. function subStringEqual(needle: string, haystack: string, needleIndex = 0) {
  547. for (let i = 0, l = haystack.length; i < l; i++) {
  548. if (needle[i + needleIndex] !== haystack[i]) return false;
  549. }
  550. return true;
  551. }