hntrie 1.0.0 → 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/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 +1 -1
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};
|