hntrie 1.0.0-beta.1 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +144 -0
- package/dist/_utils.js +1 -1
- package/dist/_utils.mjs +1 -1
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/dist/smol.js +1 -1
- package/dist/smol.mjs +1 -1
- package/package.json +2 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sukka
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# hntrie
|
|
2
|
+
|
|
3
|
+
> The extremely fast Trie implementation optimized for hostnames
|
|
4
|
+
|
|
5
|
+
`hntrie` indexes hostnames label-by-label, right to left (TLD first, the way a domain actually parses), with built-in exact-match vs. subdomain-match semantics, radix compression, and serialization. It ships two implementations:
|
|
6
|
+
|
|
7
|
+
- **`HostnameTrie`** — a full trie that stores an arbitrary value per entry. Use it when you need to look up data associated with a hostname (rules, categories, config, feature flags, etc.).
|
|
8
|
+
- **`HostnameSmolTrie`** (`hntrie/smol`) — a minimal boolean-only trie optimized purely for deduplication checks, with no per-entry value storage. Use it when all you need is "does this set contain/cover this hostname" — e.g. deduplicating domain lists when building blocklists/allowlists/split-tunnel vpn configs.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install hntrie
|
|
14
|
+
yarn add hntrie
|
|
15
|
+
pnpm add hntrie
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
### `HostnameTrie`
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { HostnameTrie } from 'hntrie';
|
|
24
|
+
|
|
25
|
+
const trie = new HostnameTrie<string>();
|
|
26
|
+
|
|
27
|
+
trie.add('example.com', 'exact-rule');
|
|
28
|
+
trie.addSubdomain('cdn.example.com', 'subdomain-rule'); // matches cdn.example.com AND *.cdn.example.com
|
|
29
|
+
|
|
30
|
+
trie.match('example.com');
|
|
31
|
+
//=> 'exact-rule'
|
|
32
|
+
trie.match('foo.cdn.example.com');
|
|
33
|
+
//=> 'subdomain-rule'
|
|
34
|
+
trie.match('other.com');
|
|
35
|
+
//=> null
|
|
36
|
+
|
|
37
|
+
trie.has('example.com'); // exact entry exists
|
|
38
|
+
//=> true
|
|
39
|
+
trie.hasSubdomain('cdn.example.com'); // subdomain entry exists
|
|
40
|
+
//=> true
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
A dot-prefix (`.example.com`) is shorthand for `addSubdomain`/`removeSubdomain`, so a trie can also be built straight from an iterable:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
const trie = new HostnameTrie([
|
|
47
|
+
'example.com', // exact only
|
|
48
|
+
'.cdn.example.com' // subdomain, covers cdn.example.com and all of its subdomains
|
|
49
|
+
]);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Call `.compact()` to radix-compress single-child chains (e.g. collapsing `a -> b -> c` into one node) and reduce memory footprint for large, mostly-static tries. The trie keeps working normally afterwards — any mutation transparently expands it back first.
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
trie.compact();
|
|
56
|
+
trie.match('example.com'); // still works
|
|
57
|
+
trie.add('new.com'); // automatically expands, then adds
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
`serialize`/`deserialize` round-trip a trie to/from a string, with optional `valueToString`/`valueFromString` for non-JSON-serializable values:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
const serialized = trie.serialize();
|
|
64
|
+
const restored = HostnameTrie.deserialize<string>(serialized);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Iterate entries directly, or stream them through a callback with `dump` (no intermediate array allocation, so entries can be pushed straight into whatever container is already on hand):
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
for (const [hostname, value, kind] of trie) {
|
|
71
|
+
// kind is 'exact' | 'subdomain'
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
trie.dump((hostname, includeSubdomain, value) => {
|
|
75
|
+
// called once per entry; includeSubdomain mirrors the dot-prefix convention
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### `HostnameSmolTrie`
|
|
80
|
+
|
|
81
|
+
Same hostname/subdomain matching semantics as `HostnameTrie`, but without per-entry values — it only tracks whether a hostname (or subdomain) is present. Well suited for large domain lists where only membership matters; it automatically dedupes and prunes redundant entries as they're added.
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { HostnameSmolTrie } from 'hntrie/smol';
|
|
85
|
+
|
|
86
|
+
const trie = new HostnameSmolTrie();
|
|
87
|
+
|
|
88
|
+
trie.addSubdomain('example.com'); // covers example.com and all subdomains
|
|
89
|
+
trie.add('foo.example.com'); // redundant, already covered — silently ignored
|
|
90
|
+
|
|
91
|
+
trie.match('foo.example.com');
|
|
92
|
+
//=> true
|
|
93
|
+
trie.match('example.com');
|
|
94
|
+
//=> true
|
|
95
|
+
trie.match('other.com');
|
|
96
|
+
//=> false
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Building from a list dedupes overlapping entries automatically:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
const trie = new HostnameSmolTrie([
|
|
103
|
+
'.example.com', // covers the whole example.com subtree
|
|
104
|
+
'foo.example.com', // redundant, dropped
|
|
105
|
+
'bar.com'
|
|
106
|
+
]);
|
|
107
|
+
|
|
108
|
+
trie.dump((hostname, includeSubdomain) => {
|
|
109
|
+
// only '.example.com' and 'bar.com' come out — foo.example.com was deduped away
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
`whitelist(hostname)` removes a hostname (and everything under it, for subdomain entries) — handy for carving out exceptions from a blocklist:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
const blocklist = new HostnameSmolTrie(['foo.example.com', 'bar.com']);
|
|
117
|
+
blocklist.whitelist('foo.example.com');
|
|
118
|
+
|
|
119
|
+
blocklist.match('foo.example.com');
|
|
120
|
+
//=> false
|
|
121
|
+
blocklist.match('bar.com');
|
|
122
|
+
//=> true
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Whitelisting only removes an entry that exists as its own node — if `foo.example.com` is already covered by a broader `.example.com` subdomain entry, whitelist that broader entry instead.
|
|
126
|
+
|
|
127
|
+
`HostnameSmolTrie` also supports `.compact()`, `.find(prefix)`, and `.dump()`/`HostnameSmolTrie.load()` for round-tripping, with the same semantics as `HostnameTrie` minus the stored values.
|
|
128
|
+
|
|
129
|
+
## License
|
|
130
|
+
|
|
131
|
+
[MIT](LICENSE)
|
|
132
|
+
|
|
133
|
+
----
|
|
134
|
+
|
|
135
|
+
**hntrie** © [Sukka](https://github.com/SukkaW), Released under the [MIT](./LICENSE) License.
|
|
136
|
+
Authored and maintained by Sukka with help from contributors ([list](https://github.com/SukkaW/hntrie/graphs/contributors)).
|
|
137
|
+
|
|
138
|
+
> [Personal Website](https://skk.moe) · [Blog](https://blog.skk.moe) · GitHub [@SukkaW](https://github.com/SukkaW) · Telegram Channel [@SukkaChannel](https://t.me/SukkaChannel) · Mastodon [@sukka@acg.mn](https://acg.mn/@sukka) · Twitter [@isukkaw](https://twitter.com/isukkaw) · BlueSky [@skk.moe](https://bsky.app/profile/skk.moe)
|
|
139
|
+
|
|
140
|
+
<p align="center">
|
|
141
|
+
<a href="https://github.com/sponsors/SukkaW/">
|
|
142
|
+
<img src="https://sponsor.cdn.skk.moe/sponsors.svg"/>
|
|
143
|
+
</a>
|
|
144
|
+
</p>
|
package/dist/_utils.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_tlds.js");function t(t,l){let r=t.length;if(0===r)return!1!==l("");46===t.codePointAt(r-1)&&r--;let n=t.lastIndexOf(".",r-1),o=t.slice(n+1,r);if(!1===l(e.TLD_TO_ID.get(o)??o))return!1;for(;n>=0;)if(r=n,n=t.lastIndexOf(".",r-1),!1===l(t.slice(n+1,r)))return!1;return!0}exports.RADIX_SEP="|",exports.labelsToHostname=function(t){let l=t.length
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_tlds.js");function t(t,l){let r=t.length;if(0===r)return!1!==l("");46===t.codePointAt(r-1)&&r--;let n=t.lastIndexOf(".",r-1),o=t.slice(n+1,r);if(!1===l(e.TLD_TO_ID.get(o)??o))return!1;for(;n>=0;)if(r=n,n=t.lastIndexOf(".",r-1),!1===l(t.slice(n+1,r)))return!1;return!0}exports.RADIX_SEP="|",exports.labelsToHostname=function(t){let l=t.length;if(1===l){let l=t[0];return e.ID_TO_TLD.get(l)??l}let r=t[l-1];for(let e=l-2;e>0;e--)r+="."+t[e];let n=t[0];return r+("."+(e.ID_TO_TLD.get(n)??n))},exports.splitHostname=function(e){let l=[];return t(e,e=>{l.push(e)}),l},exports.trieCleanup=function(e,t){let l=[e],r=e;for(let e of t){let t=r.c?.get(e);if(!t)return;l.push(t),r=t}for(let e=l.length-1;e>0;e--){let t=l[e];if(0!==t.f||null!==t.c&&t.c.size>0)break;let r=l[e-1];r.c?.delete(t.k),r.c?.size===0&&(r.c=null)}},exports.trieCompressNode=function e(t){if(null!==t.c){for(let l of t.c.values())e(l);for(let[e,l]of t.c)if(null!==l.c&&1===l.c.size&&0===l.f){let r=l.c.values().next().value;r.k=l.k+"|"+r.k,t.c.set(e,r)}}},exports.trieExpandNode=function e(t,l,r){if(null!==t.c)for(let[n,o]of[...t.c]){let f=o.k.split("|"),i=f.length;if(i>1){let u=l(f[0]),s=u;for(let e=1;e<i;e++){let t=l(f[e]);e===i-1&&(t.c=o.c,t.f=o.f,r(t,o)),s.c=new Map,s.c.set(f[e],t),s=t}t.c.set(n,u),e(s,l,r)}else e(o,l,r)}},exports.trieWalkFind=function(e,t){let l=e,r=t.length;for(let e=0;e<r;e++){let r=l.c?.get(t[e]);if(!r)return null;l=r}return l},exports.trieWalkFindCompacted=function(e,t){let l=e,r=0,n=t.length;for(;r<n;){let e=l.c?.get(t[r]);if(!e)return null;let o=e.k.split("|"),f=o.length;for(let e=0;e<f;e++){if(r>=n||o[e]!==t[r])return null;r++}l=e}return l},exports.trieWalkFindH=function(e,l){let r=e;return t(l,e=>{let t=r.c?.get(e);if(!t)return!1;r=t})?r:null},exports.walkHostname=t;
|
package/dist/_utils.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{TLD_TO_ID as e,ID_TO_TLD as t}from"./_tlds.mjs";let l="|";function n(t,l){let n=t.length;if(0===n)return!1!==l("");46===t.codePointAt(n-1)&&n--;let r=t.lastIndexOf(".",n-1),f=t.slice(r+1,n);if(!1===l(e.get(f)??f))return!1;for(;r>=0;)if(n=r,r=t.lastIndexOf(".",n-1),!1===l(t.slice(r+1,n)))return!1;return!0}function r(e){let t=[];return n(e,e=>{t.push(e)}),t}function f(e){let l=e.length
|
|
1
|
+
import{TLD_TO_ID as e,ID_TO_TLD as t}from"./_tlds.mjs";let l="|";function n(t,l){let n=t.length;if(0===n)return!1!==l("");46===t.codePointAt(n-1)&&n--;let r=t.lastIndexOf(".",n-1),f=t.slice(r+1,n);if(!1===l(e.get(f)??f))return!1;for(;r>=0;)if(n=r,r=t.lastIndexOf(".",n-1),!1===l(t.slice(r+1,n)))return!1;return!0}function r(e){let t=[];return n(e,e=>{t.push(e)}),t}function f(e){let l=e.length;if(1===l){let l=e[0];return t.get(l)??l}let n=e[l-1];for(let t=l-2;t>0;t--)n+="."+e[t];let r=e[0];return n+("."+(t.get(r)??r))}function i(e,t){let l=e;return n(t,e=>{let t=l.c?.get(e);if(!t)return!1;l=t})?l:null}function u(e,t){let l=e,n=t.length;for(let e=0;e<n;e++){let n=l.c?.get(t[e]);if(!n)return null;l=n}return l}function o(e,t){let n=e,r=0,f=t.length;for(;r<f;){let e=n.c?.get(t[r]);if(!e)return null;let i=e.k.split(l),u=i.length;for(let e=0;e<u;e++){if(r>=f||i[e]!==t[r])return null;r++}n=e}return n}function c(e){if(null!==e.c){for(let t of e.c.values())c(t);for(let[t,n]of e.c)if(null!==n.c&&1===n.c.size&&0===n.f){let r=n.c.values().next().value;r.k=n.k+l+r.k,e.c.set(t,r)}}}function s(e,t,n){if(null!==e.c)for(let[r,f]of[...e.c]){let i=f.k.split(l),u=i.length;if(u>1){let l=t(i[0]),o=l;for(let e=1;e<u;e++){let l=t(i[e]);e===u-1&&(l.c=f.c,l.f=f.f,n(l,f)),o.c=new Map,o.c.set(i[e],l),o=l}e.c.set(r,l),s(o,t,n)}else s(f,t,n)}}function a(e,t){let l=[e],n=e;for(let e of t){let t=n.c?.get(e);if(!t)return;l.push(t),n=t}for(let e=l.length-1;e>0;e--){let t=l[e];if(0!==t.f||null!==t.c&&t.c.size>0)break;let n=l[e-1];n.c?.delete(t.k),n.c?.size===0&&(n.c=null)}}export{l as RADIX_SEP,f as labelsToHostname,r as splitHostname,a as trieCleanup,c as trieCompressNode,s as trieExpandNode,u as trieWalkFind,o as trieWalkFindCompacted,i as trieWalkFindH,n as walkHostname};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,"__esModule",{value:!0});var t,e=require("./_utils.js"),i=require("foxts/bitwise"),s=require("foxts/fast-string-array-join");let
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0});var t,e=require("./_utils.js"),i=require("foxts/bitwise"),s=require("foxts/fast-string-array-join");let o="hntrie:1";function r(t){return{k:t,c:null,f:0,e:void 0,s:void 0}}function l(t,e){t.e=e.e,t.s=e.s}t=Symbol.iterator;class n{constructor(t){if(this._root=r(""),this._compacted=!1,this._size=0,t)for(let e of t)46===e.codePointAt(0)?this.addSubdomain(e.slice(1)):this.add(e)}get size(){return this._size}get compacted(){return this._compacted}add(t,e=!0){if(46===t.codePointAt(0))return this.addSubdomain(t.slice(1),e);this._expandIfCompacted();let s=this._walkCreateH(t);return i.missingBit(s.f,1)&&this._size++,s.f=i.setBit(s.f,1),s.e=e,this}addSubdomain(t,e=!0){this._expandIfCompacted();let s=this._walkCreateH(t);return i.missingBit(s.f,2)&&this._size++,s.f=i.setBit(s.f,2),s.s=e,this}remove(t){this._expandIfCompacted();let s=e.splitHostname(t),o=e.trieWalkFind(this._root,s);return!(!o||i.missingBit(o.f,1))&&(o.f=i.deleteBit(o.f,1),o.e=void 0,this._size--,e.trieCleanup(this._root,s),!0)}removeSubdomain(t){this._expandIfCompacted();let s=e.splitHostname(t),o=e.trieWalkFind(this._root,s);return!(!o||i.missingBit(o.f,2))&&(o.f=i.deleteBit(o.f,2),o.s=void 0,this._size--,e.trieCleanup(this._root,s),!0)}delete(t){return 46===t.codePointAt(0)?this.removeSubdomain(t.slice(1)):this.remove(t)}has(t){if(this._compacted){let s=e.splitHostname(t);return i.getBit(e.trieWalkFindCompacted(this._root,s)?.f??0,1)}return i.getBit(e.trieWalkFindH(this._root,t)?.f??0,1)}hasSubdomain(t){if(this._compacted){let s=e.splitHostname(t);return i.getBit(e.trieWalkFindCompacted(this._root,s)?.f??0,2)}return i.getBit(e.trieWalkFindH(this._root,t)?.f??0,2)}match(t){if(this._compacted){let i=e.splitHostname(t);return this._matchCompacted(i)}return this._matchUnfrozenH(t)}find(t){let s=this._compacted;this._expandIfCompacted();let o=46===t.codePointAt(0),r=o?t.slice(1):t,l=e.splitHostname(r),n=e.trieWalkFind(this._root,l),a=[];if(!n)return s&&this.compact(),a;if(o){if(i.getBit(n.f,2)&&a.push("."+r),null!==n.c)for(let t of n.c.values())this._collectEntries(t,l,a)}else this._collectEntries(n,l.slice(0,-1),a);return s&&this.compact(),a}dump(t){this._walkDump(this._root,[],t)}compact(){return this._compacted||(e.trieCompressNode(this._root),this._compacted=!0),this}serialize(t){let e=[o];return this._serializeNode(this._root,e,t),s.fastStringArrayJoin(e,"\n")}static deserialize(t,s){let l=new n,a=t.split("\n");if(a[0]!==o)throw Error("Invalid hntrie serialization format");let f=0,c=[[l._root,-1]],h=a.length;for(let t=1;t<h;t++){let o=a[t];if(0===o.length)continue;let l=o.split(" "),n=Number.parseInt(l[0],36),h=l[1],d=Number.parseInt(l[2],36),u=l[3]||"",p=l[4]||"",_=r(h);for(_.f=d,i.getBit(d,1)&&(_.e=!u||(s?s(u):JSON.parse(u)),f++),i.getBit(d,2)&&(_.s=!p||(s?s(p):JSON.parse(p)),f++);c.length>1&&c[c.length-1][1]>=n;)c.pop();let m=c[c.length-1][0];null===m.c&&(m.c=new Map);let g=h.includes(e.RADIX_SEP)?h.slice(0,h.indexOf(e.RADIX_SEP)):h;m.c.set(g,_),c.push([_,n])}return l._size=f,l._compacted=function t(i){if(i.k.includes(e.RADIX_SEP))return!0;if(null!==i.c){for(let e of i.c.values())if(t(e))return!0}return!1}(l._root),l}toJSON(){return this.serialize()}static fromJSON(t,e){return n.deserialize(t,e)}*[t](){yield*this._iterateNode(this._root,[])}_expandIfCompacted(){this._compacted&&(e.trieExpandNode(this._root,r,l),this._compacted=!1)}_walkCreateH(t){let i=this._root;return e.walkHostname(t,t=>{null===i.c&&(i.c=new Map);let e=i.c.get(t);e||(e=r(t),i.c.set(t,e)),i=e}),i}_matchUnfrozenH(t){let s=this._root,o=null;return e.walkHostname(t,t=>{let e=s.c?.get(t);if(!e)return!1;s=e,i.getBit(s.f,2)&&(o=s.s)})&&i.getBit(s.f,1)?s.e:o}_matchCompacted(t){let s=this._root,o=null,r=0,l=t.length;for(;r<l;){let n=s.c?.get(t[r]);if(!n)return o;let a=n.k.split(e.RADIX_SEP),f=a.length;for(let e=0;e<f;e++){if(r>=l||a[e]!==t[r])return o;r++}s=n,i.getBit(s.f,2)&&(o=s.s)}return i.getBit(s.f,1)?s.e:o}_serializeNode(t,e,s,o=-1){if(o>=0){let r="",l="";i.getBit(t.f,1)&&!0!==t.e&&(r=s?s(t.e):JSON.stringify(t.e)),i.getBit(t.f,2)&&!0!==t.s&&(l=s?s(t.s):JSON.stringify(t.s));let n=o.toString(36)+" "+t.k+" "+t.f.toString(36);(r||l)&&(n+=" "+r,l&&(n+=" "+l)),e.push(n)}if(null!==t.c)for(let i of t.c.values())this._serializeNode(i,e,s,o+1)}*_iterateNode(t,s){let o=""===t.k?[]:t.k.split(e.RADIX_SEP),r=o.length;for(let t=0;t<r;t++)s.push(o[t]);if(i.getBit(t.f,1)&&(yield[e.labelsToHostname(s),t.e,"exact"]),i.getBit(t.f,2)&&(yield[e.labelsToHostname(s),t.s,"subdomain"]),null!==t.c)for(let e of t.c.values())yield*this._iterateNode(e,s);for(let t=0;t<r;t++)s.pop()}_collectEntries(t,s,o){let r=""===t.k?[]:t.k.split(e.RADIX_SEP),l=r.length;for(let t=0;t<l;t++)s.push(r[t]);if(i.getBit(t.f,2)&&o.push("."+e.labelsToHostname(s)),i.getBit(t.f,1)&&o.push(e.labelsToHostname(s)),null!==t.c)for(let e of t.c.values())this._collectEntries(e,s,o);for(let t=0;t<l;t++)s.pop()}_walkDump(t,s,o){let r=""===t.k?[]:t.k.split(e.RADIX_SEP),l=r.length;for(let t=0;t<l;t++)s.push(r[t]);if(i.getBit(t.f,2)&&o(e.labelsToHostname(s),!0,t.s),i.getBit(t.f,1)&&o(e.labelsToHostname(s),!1,t.e),null!==t.c)for(let e of t.c.values())this._walkDump(e,s,o);for(let t=0;t<l;t++)s.pop()}}exports.HostnameTrie=n;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var t;import{splitHostname as e,trieWalkFind as i,trieCleanup as r,trieWalkFindCompacted as o,trieWalkFindH as s,trieCompressNode as l,RADIX_SEP as n,trieExpandNode as f,walkHostname as
|
|
1
|
+
var t;import{splitHostname as e,trieWalkFind as i,trieCleanup as r,trieWalkFindCompacted as o,trieWalkFindH as s,trieCompressNode as l,RADIX_SEP as n,trieExpandNode as f,walkHostname as c,labelsToHostname as a}from"./_utils.mjs";import{missingBit as h,setBit as u,deleteBit as d,getBit as p}from"foxts/bitwise";import{fastStringArrayJoin as _}from"foxts/fast-string-array-join";let m="hntrie:1";function g(t){return{k:t,c:null,f:0,e:void 0,s:void 0}}function z(t,e){t.e=e.e,t.s=e.s}t=Symbol.iterator;class k{constructor(t){if(this._root=g(""),this._compacted=!1,this._size=0,t)for(let e of t)46===e.codePointAt(0)?this.addSubdomain(e.slice(1)):this.add(e)}get size(){return this._size}get compacted(){return this._compacted}add(t,e=!0){if(46===t.codePointAt(0))return this.addSubdomain(t.slice(1),e);this._expandIfCompacted();let i=this._walkCreateH(t);return h(i.f,1)&&this._size++,i.f=u(i.f,1),i.e=e,this}addSubdomain(t,e=!0){this._expandIfCompacted();let i=this._walkCreateH(t);return h(i.f,2)&&this._size++,i.f=u(i.f,2),i.s=e,this}remove(t){this._expandIfCompacted();let o=e(t),s=i(this._root,o);return!(!s||h(s.f,1))&&(s.f=d(s.f,1),s.e=void 0,this._size--,r(this._root,o),!0)}removeSubdomain(t){this._expandIfCompacted();let o=e(t),s=i(this._root,o);return!(!s||h(s.f,2))&&(s.f=d(s.f,2),s.s=void 0,this._size--,r(this._root,o),!0)}delete(t){return 46===t.codePointAt(0)?this.removeSubdomain(t.slice(1)):this.remove(t)}has(t){if(this._compacted){let i=e(t);return p(o(this._root,i)?.f??0,1)}return p(s(this._root,t)?.f??0,1)}hasSubdomain(t){if(this._compacted){let i=e(t);return p(o(this._root,i)?.f??0,2)}return p(s(this._root,t)?.f??0,2)}match(t){if(this._compacted){let i=e(t);return this._matchCompacted(i)}return this._matchUnfrozenH(t)}find(t){let r=this._compacted;this._expandIfCompacted();let o=46===t.codePointAt(0),s=o?t.slice(1):t,l=e(s),n=i(this._root,l),f=[];if(!n)return r&&this.compact(),f;if(o){if(p(n.f,2)&&f.push("."+s),null!==n.c)for(let t of n.c.values())this._collectEntries(t,l,f)}else this._collectEntries(n,l.slice(0,-1),f);return r&&this.compact(),f}dump(t){this._walkDump(this._root,[],t)}compact(){return this._compacted||(l(this._root),this._compacted=!0),this}serialize(t){let e=[m];return this._serializeNode(this._root,e,t),_(e,"\n")}static deserialize(t,e){let i=new k,r=t.split("\n");if(r[0]!==m)throw Error("Invalid hntrie serialization format");let o=0,s=[[i._root,-1]],l=r.length;for(let t=1;t<l;t++){let i=r[t];if(0===i.length)continue;let l=i.split(" "),f=Number.parseInt(l[0],36),c=l[1],a=Number.parseInt(l[2],36),h=l[3]||"",u=l[4]||"",d=g(c);for(d.f=a,p(a,1)&&(d.e=!h||(e?e(h):JSON.parse(h)),o++),p(a,2)&&(d.s=!u||(e?e(u):JSON.parse(u)),o++);s.length>1&&s[s.length-1][1]>=f;)s.pop();let _=s[s.length-1][0];null===_.c&&(_.c=new Map);let m=c.includes(n)?c.slice(0,c.indexOf(n)):c;_.c.set(m,d),s.push([d,f])}return i._size=o,i._compacted=function t(e){if(e.k.includes(n))return!0;if(null!==e.c){for(let i of e.c.values())if(t(i))return!0}return!1}(i._root),i}toJSON(){return this.serialize()}static fromJSON(t,e){return k.deserialize(t,e)}*[t](){yield*this._iterateNode(this._root,[])}_expandIfCompacted(){this._compacted&&(f(this._root,g,z),this._compacted=!1)}_walkCreateH(t){let e=this._root;return c(t,t=>{null===e.c&&(e.c=new Map);let i=e.c.get(t);i||(i=g(t),e.c.set(t,i)),e=i}),e}_matchUnfrozenH(t){let e=this._root,i=null;return c(t,t=>{let r=e.c?.get(t);if(!r)return!1;p((e=r).f,2)&&(i=e.s)})&&p(e.f,1)?e.e:i}_matchCompacted(t){let e=this._root,i=null,r=0,o=t.length;for(;r<o;){let s=e.c?.get(t[r]);if(!s)return i;let l=s.k.split(n),f=l.length;for(let e=0;e<f;e++){if(r>=o||l[e]!==t[r])return i;r++}p((e=s).f,2)&&(i=e.s)}return p(e.f,1)?e.e:i}_serializeNode(t,e,i,r=-1){if(r>=0){let o="",s="";p(t.f,1)&&!0!==t.e&&(o=i?i(t.e):JSON.stringify(t.e)),p(t.f,2)&&!0!==t.s&&(s=i?i(t.s):JSON.stringify(t.s));let l=r.toString(36)+" "+t.k+" "+t.f.toString(36);(o||s)&&(l+=" "+o,s&&(l+=" "+s)),e.push(l)}if(null!==t.c)for(let o of t.c.values())this._serializeNode(o,e,i,r+1)}*_iterateNode(t,e){let i=""===t.k?[]:t.k.split(n),r=i.length;for(let t=0;t<r;t++)e.push(i[t]);if(p(t.f,1)&&(yield[a(e),t.e,"exact"]),p(t.f,2)&&(yield[a(e),t.s,"subdomain"]),null!==t.c)for(let i of t.c.values())yield*this._iterateNode(i,e);for(let t=0;t<r;t++)e.pop()}_collectEntries(t,e,i){let r=""===t.k?[]:t.k.split(n),o=r.length;for(let t=0;t<o;t++)e.push(r[t]);if(p(t.f,2)&&i.push("."+a(e)),p(t.f,1)&&i.push(a(e)),null!==t.c)for(let r of t.c.values())this._collectEntries(r,e,i);for(let t=0;t<o;t++)e.pop()}_walkDump(t,e,i){let r=""===t.k?[]:t.k.split(n),o=r.length;for(let t=0;t<o;t++)e.push(r[t]);if(p(t.f,2)&&i(a(e),!0,t.s),p(t.f,1)&&i(a(e),!1,t.e),null!==t.c)for(let r of t.c.values())this._walkDump(r,e,i);for(let t=0;t<o;t++)e.pop()}}export{k as HostnameTrie};
|
package/dist/smol.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,"__esModule",{value:!0});var t=require("./_utils.js"),e=require("foxts/bitwise");function i(t){return{k:t,c:null,f:0}}let o=require("foxts/noop").noop;exports.HostnameSmolTrie=class
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0});var t=require("./_utils.js"),e=require("foxts/bitwise");function i(t){return{k:t,c:null,f:0}}let o=require("foxts/noop").noop;exports.HostnameSmolTrie=class s{constructor(t){if(this._root=i(""),this._compacted=!1,t)for(let e of t)46===e.codePointAt(0)?this.addSubdomain(e.slice(1)):this.add(e)}get compacted(){return this._compacted}add(t){if(46===t.codePointAt(0))return this.addSubdomain(t.slice(1));this._expandIfCompacted();let i=this._walkCreateOrCovered(t);return null===i||(i.f=e.setBit(i.f,1)),this}addSubdomain(t){this._expandIfCompacted();let i=this._walkCreateOrCovered(t);return null===i||e.missingBit(i.f,2)&&(i.c=null,i.f=e.setBit(i.f,2),i.f=e.deleteBit(i.f,1)),this}remove(i){this._expandIfCompacted();let o=t.splitHostname(i),s=t.trieWalkFind(this._root,o);return!(!s||e.missingBit(s.f,1))&&(s.f=e.deleteBit(s.f,1),t.trieCleanup(this._root,o),!0)}removeSubdomain(i){this._expandIfCompacted();let o=t.splitHostname(i),s=t.trieWalkFind(this._root,o);return!(!s||e.missingBit(s.f,2))&&(s.f=e.deleteBit(s.f,2),t.trieCleanup(this._root,o),!0)}delete(t){return 46===t.codePointAt(0)?this.removeSubdomain(t.slice(1)):this.remove(t)}whitelist(i){this._expandIfCompacted();let o=46===i.codePointAt(0),s=o?i.slice(1):i,r=t.splitHostname(s),l=t.trieWalkFind(this._root,r);l&&(o?(l.f=0,l.c=null):(l.f=e.deleteBit(l.f,1),l.f=e.deleteBit(l.f,2)),t.trieCleanup(this._root,r))}has(i){if(this._compacted){let o=t.splitHostname(i);return e.getBit(t.trieWalkFindCompacted(this._root,o)?.f??0,1)}return e.getBit(t.trieWalkFindH(this._root,i)?.f??0,1)}hasSubdomain(i){if(this._compacted){let o=t.splitHostname(i);return e.getBit(t.trieWalkFindCompacted(this._root,o)?.f??0,2)}return e.getBit(t.trieWalkFindH(this._root,i)?.f??0,2)}match(e){if(this._compacted){let i=t.splitHostname(e);return this._matchCompacted(i)}return this._matchUnfrozenH(e)}contains(t){return this.match(t)}find(i){let o=this._compacted;this._expandIfCompacted();let s=46===i.codePointAt(0),r=s?i.slice(1):i,l=t.splitHostname(r),n=t.trieWalkFind(this._root,l),a=[];if(!n)return o&&this.compact(),a;if(s){if(e.getBit(n.f,2)&&a.push("."+r),null!==n.c)for(let t of n.c.values())this._collectEntries(t,l,a)}else this._collectEntries(n,l.slice(0,-1),a);return o&&this.compact(),a}compact(){return this._compacted||(t.trieCompressNode(this._root),this._compacted=!0),this}dump(t){this._walkDump(this._root,[],t)}static load(t){let e=new s,i=t.length;for(let o=0;o<i;o++){let i=t[o];46===i.codePointAt(0)?e.addSubdomain(i.slice(1)):e.add(i)}return e}_expandIfCompacted(){this._compacted&&(t.trieExpandNode(this._root,i,o),this._compacted=!1)}_walkCreateOrCovered(o){let s=this._root;return t.walkHostname(o,t=>{null===s.c&&(s.c=new Map);let o=s.c.get(t);if(o||(o=i(t),s.c.set(t,o)),s=o,e.getBit(s.f,2))return!1})?s:null}_matchUnfrozenH(i){let o=this._root;return t.walkHostname(i,t=>{let i=o.c?.get(t);return!!i&&(o=i,!e.getBit(o.f,2)&&void 0)})?0!==o.f:e.getBit(o.f,2)}_matchCompacted(i){let o=this._root,s=0,r=i.length;for(;s<r;){let l=o.c?.get(i[s]);if(!l)return!1;let n=l.k.split(t.RADIX_SEP),a=n.length;for(let t=0;t<a;t++){if(s>=r||n[t]!==i[s])return!1;s++}if(o=l,e.getBit(o.f,2))return!0}return 0!==o.f}_collectEntries(i,o,s){let r=""===i.k?[]:i.k.split(t.RADIX_SEP),l=r.length;for(let t=0;t<l;t++)o.push(r[t]);if(e.getBit(i.f,2)?s.push("."+t.labelsToHostname(o)):e.getBit(i.f,1)&&s.push(t.labelsToHostname(o)),null!==i.c&&e.missingBit(i.f,2))for(let t of i.c.values())this._collectEntries(t,o,s);for(let t=0;t<l;t++)o.pop()}_walkDump(i,o,s){let r=""===i.k?[]:i.k.split(t.RADIX_SEP),l=r.length;for(let t=0;t<l;t++)o.push(r[t]);if(e.getBit(i.f,2)?s(t.labelsToHostname(o),!0):e.getBit(i.f,1)&&s(t.labelsToHostname(o),!1),null!==i.c&&e.missingBit(i.f,2))for(let t of i.c.values())this._walkDump(t,o,s);for(let t=0;t<l;t++)o.pop()}};
|
package/dist/smol.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{splitHostname as t,trieWalkFind as e,trieCleanup as o,trieWalkFindCompacted as r,trieWalkFindH as i,trieCompressNode as
|
|
1
|
+
import{splitHostname as t,trieWalkFind as e,trieCleanup as o,trieWalkFindCompacted as r,trieWalkFindH as i,trieCompressNode as s,trieExpandNode as l,walkHostname as c,RADIX_SEP as f,labelsToHostname as n}from"./_utils.mjs";import{setBit as h,missingBit as a,deleteBit as d,getBit as u}from"foxts/bitwise";import{noop as m}from"foxts/noop";function p(t){return{k:t,c:null,f:0}}class _{constructor(t){if(this._root=p(""),this._compacted=!1,t)for(let e of t)46===e.codePointAt(0)?this.addSubdomain(e.slice(1)):this.add(e)}get compacted(){return this._compacted}add(t){if(46===t.codePointAt(0))return this.addSubdomain(t.slice(1));this._expandIfCompacted();let e=this._walkCreateOrCovered(t);return null===e||(e.f=h(e.f,1)),this}addSubdomain(t){this._expandIfCompacted();let e=this._walkCreateOrCovered(t);return null===e||a(e.f,2)&&(e.c=null,e.f=h(e.f,2),e.f=d(e.f,1)),this}remove(r){this._expandIfCompacted();let i=t(r),s=e(this._root,i);return!(!s||a(s.f,1))&&(s.f=d(s.f,1),o(this._root,i),!0)}removeSubdomain(r){this._expandIfCompacted();let i=t(r),s=e(this._root,i);return!(!s||a(s.f,2))&&(s.f=d(s.f,2),o(this._root,i),!0)}delete(t){return 46===t.codePointAt(0)?this.removeSubdomain(t.slice(1)):this.remove(t)}whitelist(r){this._expandIfCompacted();let i=46===r.codePointAt(0),s=t(i?r.slice(1):r),l=e(this._root,s);l&&(i?(l.f=0,l.c=null):(l.f=d(l.f,1),l.f=d(l.f,2)),o(this._root,s))}has(e){if(this._compacted){let o=t(e);return u(r(this._root,o)?.f??0,1)}return u(i(this._root,e)?.f??0,1)}hasSubdomain(e){if(this._compacted){let o=t(e);return u(r(this._root,o)?.f??0,2)}return u(i(this._root,e)?.f??0,2)}match(e){if(this._compacted){let o=t(e);return this._matchCompacted(o)}return this._matchUnfrozenH(e)}contains(t){return this.match(t)}find(o){let r=this._compacted;this._expandIfCompacted();let i=46===o.codePointAt(0),s=i?o.slice(1):o,l=t(s),c=e(this._root,l),f=[];if(!c)return r&&this.compact(),f;if(i){if(u(c.f,2)&&f.push("."+s),null!==c.c)for(let t of c.c.values())this._collectEntries(t,l,f)}else this._collectEntries(c,l.slice(0,-1),f);return r&&this.compact(),f}compact(){return this._compacted||(s(this._root),this._compacted=!0),this}dump(t){this._walkDump(this._root,[],t)}static load(t){let e=new _,o=t.length;for(let r=0;r<o;r++){let o=t[r];46===o.codePointAt(0)?e.addSubdomain(o.slice(1)):e.add(o)}return e}_expandIfCompacted(){this._compacted&&(l(this._root,p,m),this._compacted=!1)}_walkCreateOrCovered(t){let e=this._root;return c(t,t=>{null===e.c&&(e.c=new Map);let o=e.c.get(t);if(o||(o=p(t),e.c.set(t,o)),u((e=o).f,2))return!1})?e:null}_matchUnfrozenH(t){let e=this._root;return c(t,t=>{let o=e.c?.get(t);if(!o||u((e=o).f,2))return!1})?0!==e.f:u(e.f,2)}_matchCompacted(t){let e=this._root,o=0,r=t.length;for(;o<r;){let i=e.c?.get(t[o]);if(!i)return!1;let s=i.k.split(f),l=s.length;for(let e=0;e<l;e++){if(o>=r||s[e]!==t[o])return!1;o++}if(u((e=i).f,2))return!0}return 0!==e.f}_collectEntries(t,e,o){let r=""===t.k?[]:t.k.split(f),i=r.length;for(let t=0;t<i;t++)e.push(r[t]);if(u(t.f,2)?o.push("."+n(e)):u(t.f,1)&&o.push(n(e)),null!==t.c&&a(t.f,2))for(let r of t.c.values())this._collectEntries(r,e,o);for(let t=0;t<i;t++)e.pop()}_walkDump(t,e,o){let r=""===t.k?[]:t.k.split(f),i=r.length;for(let t=0;t<i;t++)e.push(r[t]);if(u(t.f,2)?o(n(e),!0):u(t.f,1)&&o(n(e),!1),null!==t.c&&a(t.f,2))for(let r of t.c.values())this._walkDump(r,e,o);for(let t=0;t<i;t++)e.pop()}}export{_ as HostnameSmolTrie};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hntrie",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "The extremely fast Trie implementation optimized for Hostname.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"lint": "eslint --format=sukka .",
|
|
57
57
|
"build": "bunchee --minify --no-sourcemap",
|
|
58
58
|
"bench": "SWC_NODE_IGNORE_DYNAMIC=1 node src/index.bench.ts",
|
|
59
|
-
"test": "SWC_NODE_IGNORE_DYNAMIC=1 SWC_NODE_INLINE_SOURCE_MAP=1 nyc mocha --require @swc-node/register --full-trace '
|
|
59
|
+
"test": "SWC_NODE_IGNORE_DYNAMIC=1 SWC_NODE_INLINE_SOURCE_MAP=1 nyc mocha --require @swc-node/register --full-trace 'test/**/*.test.ts'",
|
|
60
60
|
"prerelease": "pnpm run lint && pnpm run test && pnpm run build",
|
|
61
61
|
"release": "bumpp --install -r --all --commit \"release: %s\" --tag \"%s\""
|
|
62
62
|
}
|