trie.ts 20 KB

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