@takumi-rs/helpers 2.13.7 → 2.15.0

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/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # @takumi-rs/helpers
2
2
 
3
- **Utility functions and types for working with Takumi node trees.**
3
+ **Convert JSX and HTML into Takumi node trees and prepare fonts, images, and emoji.**
4
4
 
5
- Convert JSX and HTML into the node trees Takumi's Rust engine renders. Load fonts and process emoji.
5
+ Use these helpers when working with the native or WebAssembly bindings directly. With `takumi-js`, import them from `takumi-js/helpers` and its subpaths.
6
6
 
7
7
  [Documentation](https://takumi.kane.tw/docs/helpers#parsing-templates) · [GitHub](https://github.com/kane50613/takumi)
8
8
 
@@ -16,7 +16,7 @@ npm install @takumi-rs/helpers
16
16
 
17
17
  ### JSX to Node Tree
18
18
 
19
- Convert React-like elements into a serializable node tree + CSS.
19
+ Convert React-like elements into a serializable node tree and its CSS.
20
20
 
21
21
  ```tsx
22
22
  import { fromJsx } from "@takumi-rs/helpers/jsx";
@@ -36,7 +36,7 @@ const { node, css } = await fromHtml("<div style='color: red'>Hello</div>");
36
36
 
37
37
  ### Emoji Processing
38
38
 
39
- Find and replace emoji characters in text nodes with image nodes (Twemoji or custom).
39
+ Replace emoji in text nodes with image nodes from the selected provider.
40
40
 
41
41
  ```ts
42
42
  import { extractEmojis } from "@takumi-rs/helpers/emoji";
package/dist/emoji.cjs CHANGED
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./helpers-4kGcvc-Q.cjs"),t=/\uFE0F/g,n=/^\p{Extended_Pictographic}/u,r=/^\p{Emoji_Presentation}/u,i=/^\p{Emoji_Modifier_Base}\p{Emoji_Modifier}/u,a=/^(?:\p{Regional_Indicator}){1,2}$/u,o=/^[#*0-9]\uFE0F?\u20E3$/u;function s(e){return[...e.indexOf(`‍`)<0?e.replace(t,``):e].map(e=>e.codePointAt(0)?.toString(16)).join(`-`)}const c={twemoji:e=>`https://cdn.jsdelivr.net/gh/jdecked/twemoji@17.0.2/assets/svg/${e.toLowerCase()}.svg`,openmoji:`https://cdn.jsdelivr.net/npm/@svgmoji/openmoji@2.0.0/svg/`,blobmoji:`https://cdn.jsdelivr.net/npm/@svgmoji/blob@2.0.0/svg/`,noto:e=>`https://cdn.jsdelivr.net/gh/googlefonts/noto-emoji@v2.051/svg/emoji_u${e.toLowerCase().replaceAll(`-`,`_`)}.svg`,fluent:e=>`https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${e.toLowerCase()}_color.svg`,fluentFlat:e=>`https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${e.toLowerCase()}_flat.svg`};function l(e,t){let n=s(e),r=c[t];return typeof r==`function`?r(n):`${r}${n.toUpperCase()}.svg`}const u=new Intl.Segmenter(`en`,{granularity:`grapheme`});function d(e){return Array.from(u.segment(e))}function f(e){return e.includes(`︎`)?!1:a.test(e)||o.test(e)?!0:n.test(e)&&(e.includes(`️`)||e.includes(`‍`)||r.test(e)||i.test(e))}function p(t,n){let r=[],i=``,a=d(t.text);for(let{segment:t}of a)f(t)?(i&&=(r.push(e.l({text:i})),``),r.push(e.i({src:l(t,n),style:{display:`inline-block`,width:`1em`,height:`1em`,margin:`0 0.05em 0 0.1em`,verticalAlign:`-0.1em`}}))):i+=t;return i&&r.push(e.l({text:i})),r}function m(t,n){if(t.type===`text`){if(d(t.text).some(({segment:e})=>f(e))){let{type:r,...i}=t;return e.t({...i,children:p(t,n)})}}else if(t.type===`container`&&t.children)return{...t,children:t.children.map(e=>e&&m(e,n))};return t}exports.extractEmojis=m;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./helpers-_gtvoVXa.cjs"),t=/^\p{Extended_Pictographic}/u,n=/^\p{Emoji_Presentation}/u,r=/^\p{Emoji_Modifier_Base}\p{Emoji_Modifier}/u,i=/^(?:\p{Regional_Indicator}){1,2}$/u,a=/^[#*0-9]\uFE0F?\u20E3$/u;function o(e){return[...e.includes(`‍`)?e:e.replaceAll(`️`,``)].map(e=>e.codePointAt(0)?.toString(16)).join(`-`)}const s={twemoji:e=>`https://cdn.jsdelivr.net/gh/jdecked/twemoji@17.0.2/assets/svg/${e.toLowerCase()}.svg`,openmoji:`https://cdn.jsdelivr.net/npm/@svgmoji/openmoji@2.0.0/svg/`,blobmoji:`https://cdn.jsdelivr.net/npm/@svgmoji/blob@2.0.0/svg/`,noto:e=>`https://cdn.jsdelivr.net/gh/googlefonts/noto-emoji@v2.051/svg/emoji_u${e.toLowerCase().replaceAll(`-`,`_`)}.svg`,fluent:e=>`https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${e.toLowerCase()}_color.svg`,fluentFlat:e=>`https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${e.toLowerCase()}_flat.svg`};function c(e,t){let n=o(e),r=s[t];return typeof r==`function`?r(n):`${r}${n.toUpperCase()}.svg`}const l=new Intl.Segmenter(`en`,{granularity:`grapheme`});function u(e){return Array.from(l.segment(e))}function d(e){return e.includes(`︎`)?!1:i.test(e)||a.test(e)?!0:t.test(e)&&(e.includes(`️`)||e.includes(`‍`)||n.test(e)||r.test(e))}function f(t,n){let r=[],i=``;for(let{segment:a}of u(t.text))d(a)?(i&&=(r.push(e.l({text:i})),``),r.push(e.i({src:c(a,n),style:{display:`inline-block`,width:`1em`,height:`1em`,margin:`0 0.05em 0 0.1em`,verticalAlign:`-0.1em`}}))):i+=a;return i&&r.push(e.l({text:i})),r}function p(t,n){if(t.type===`text`){if(u(t.text).some(({segment:e})=>d(e))){let{type:r,...i}=t;return e.t({...i,children:f(t,n)})}}else if(t.type===`container`&&t.children)return{...t,children:t.children.map(e=>e&&p(e,n))};return t}exports.extractEmojis=p;
package/dist/emoji.mjs CHANGED
@@ -1 +1 @@
1
- import{i as e,l as t,t as n}from"./helpers-CVQCT1Rh.mjs";const r=/\uFE0F/g,i=/^\p{Extended_Pictographic}/u,a=/^\p{Emoji_Presentation}/u,o=/^\p{Emoji_Modifier_Base}\p{Emoji_Modifier}/u,s=/^(?:\p{Regional_Indicator}){1,2}$/u,c=/^[#*0-9]\uFE0F?\u20E3$/u;function l(e){return[...e.indexOf(`‍`)<0?e.replace(r,``):e].map(e=>e.codePointAt(0)?.toString(16)).join(`-`)}const u={twemoji:e=>`https://cdn.jsdelivr.net/gh/jdecked/twemoji@17.0.2/assets/svg/${e.toLowerCase()}.svg`,openmoji:`https://cdn.jsdelivr.net/npm/@svgmoji/openmoji@2.0.0/svg/`,blobmoji:`https://cdn.jsdelivr.net/npm/@svgmoji/blob@2.0.0/svg/`,noto:e=>`https://cdn.jsdelivr.net/gh/googlefonts/noto-emoji@v2.051/svg/emoji_u${e.toLowerCase().replaceAll(`-`,`_`)}.svg`,fluent:e=>`https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${e.toLowerCase()}_color.svg`,fluentFlat:e=>`https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${e.toLowerCase()}_flat.svg`};function d(e,t){let n=l(e),r=u[t];return typeof r==`function`?r(n):`${r}${n.toUpperCase()}.svg`}const f=new Intl.Segmenter(`en`,{granularity:`grapheme`});function p(e){return Array.from(f.segment(e))}function m(e){return e.includes(`︎`)?!1:s.test(e)||c.test(e)?!0:i.test(e)&&(e.includes(`️`)||e.includes(`‍`)||a.test(e)||o.test(e))}function h(n,r){let i=[],a=``,o=p(n.text);for(let{segment:n}of o)m(n)?(a&&=(i.push(t({text:a})),``),i.push(e({src:d(n,r),style:{display:`inline-block`,width:`1em`,height:`1em`,margin:`0 0.05em 0 0.1em`,verticalAlign:`-0.1em`}}))):a+=n;return a&&i.push(t({text:a})),i}function g(e,t){if(e.type===`text`){if(p(e.text).some(({segment:e})=>m(e))){let{type:r,...i}=e;return n({...i,children:h(e,t)})}}else if(e.type===`container`&&e.children)return{...e,children:e.children.map(e=>e&&g(e,t))};return e}export{g as extractEmojis};
1
+ import{i as e,l as t,t as n}from"./helpers-CrbDeBIp.mjs";const r=/^\p{Extended_Pictographic}/u,i=/^\p{Emoji_Presentation}/u,a=/^\p{Emoji_Modifier_Base}\p{Emoji_Modifier}/u,o=/^(?:\p{Regional_Indicator}){1,2}$/u,s=/^[#*0-9]\uFE0F?\u20E3$/u;function c(e){return[...e.includes(`‍`)?e:e.replaceAll(`️`,``)].map(e=>e.codePointAt(0)?.toString(16)).join(`-`)}const l={twemoji:e=>`https://cdn.jsdelivr.net/gh/jdecked/twemoji@17.0.2/assets/svg/${e.toLowerCase()}.svg`,openmoji:`https://cdn.jsdelivr.net/npm/@svgmoji/openmoji@2.0.0/svg/`,blobmoji:`https://cdn.jsdelivr.net/npm/@svgmoji/blob@2.0.0/svg/`,noto:e=>`https://cdn.jsdelivr.net/gh/googlefonts/noto-emoji@v2.051/svg/emoji_u${e.toLowerCase().replaceAll(`-`,`_`)}.svg`,fluent:e=>`https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${e.toLowerCase()}_color.svg`,fluentFlat:e=>`https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${e.toLowerCase()}_flat.svg`};function u(e,t){let n=c(e),r=l[t];return typeof r==`function`?r(n):`${r}${n.toUpperCase()}.svg`}const d=new Intl.Segmenter(`en`,{granularity:`grapheme`});function f(e){return Array.from(d.segment(e))}function p(e){return e.includes(`︎`)?!1:o.test(e)||s.test(e)?!0:r.test(e)&&(e.includes(`️`)||e.includes(`‍`)||i.test(e)||a.test(e))}function m(n,r){let i=[],a=``;for(let{segment:o}of f(n.text))p(o)?(a&&=(i.push(t({text:a})),``),i.push(e({src:u(o,r),style:{display:`inline-block`,width:`1em`,height:`1em`,margin:`0 0.05em 0 0.1em`,verticalAlign:`-0.1em`}}))):a+=o;return a&&i.push(t({text:a})),i}function h(e,t){if(e.type===`text`){if(f(e.text).some(({segment:e})=>p(e))){let{type:r,...i}=e;return n({...i,children:m(e,t)})}}else if(e.type===`container`&&e.children)return{...e,children:e.children.map(e=>e&&h(e,t))};return e}export{h as extractEmojis};
@@ -0,0 +1 @@
1
+ var e=class{signal;constructor(e,t){let n=e.timeout??3e4,r=[e.signal,t,n<=0?void 0:AbortSignal.timeout(n)].filter(e=>e!=null);this.signal=r.length?AbortSignal.any(r):void 0}waitFor(e){let t=this.signal;return t?new Promise((n,r)=>{let i=()=>{t.removeEventListener(`abort`,i),r(t.reason)};t.addEventListener(`abort`,i,{once:!0}),e.then(e=>{t.removeEventListener(`abort`,i),n(e)},e=>{t.removeEventListener(`abort`,i),r(e)}),t.aborted&&i()}):e}};const t=new Set([301,302,303,307,308]),n=new Set([408,429,500,502,503,504]);async function r(e,t={}){return new i(e,t).send()}var i=class r{url;allowUrl;fetchImpl;init;constructor(t,n){if(n.allowUrl&&!n.allowUrl(t))throw Error(`URL blocked by allowUrl policy: ${t}`);this.url=t,this.allowUrl=n.allowUrl,this.fetchImpl=n.fetch??globalThis.fetch,this.init={...n.init,signal:new e(n,n.init?.signal).signal}}async send(){let e=this.allowUrl?await this.followRedirects(this.allowUrl):await this.attempt(this.url,this.init);if(!e.ok)throw Error(`HTTP ${e.status} ${e.statusText} fetching ${this.url}`);return e}async attempt(e,t){let r=t.method?.toUpperCase()??`GET`,i=r===`GET`||r===`HEAD`;for(let r=0;;r++){t.signal?.throwIfAborted();let a=100*2**r;try{let o=await this.fetchImpl.call(void 0,e,t);if(t.signal?.throwIfAborted(),!i||r===2||!n.has(o.status))return o;let s=o.headers.get(`retry-after`);if(s!==null){let e=Number(s),t=Number.isFinite(e)?e*1e3:Date.parse(s)-Date.now();Number.isFinite(t)&&(a=Math.max(a,t))}if(a>1e3)return o;o.body?.cancel().catch(()=>{})}catch(e){if(t.signal?.throwIfAborted(),!i||r===2||!(e instanceof Error)||e.name!==`TypeError`&&e.name!==`TimeoutError`)throw e}await this.wait(a)}}wait(e){let t=this.init.signal;return t?.throwIfAborted(),new Promise((n,r)=>{let i=setTimeout(()=>{t?.removeEventListener(`abort`,a),n()},e);function a(){clearTimeout(i),r(t?.reason)}t?.addEventListener(`abort`,a,{once:!0})})}async followRedirects(e){let n=this.url,i={...this.init,headers:new Headers(this.init.headers),redirect:`manual`};for(let a=0;a<5;a++){let a=await this.attempt(n,i),o=a.headers.get(`location`);if(!t.has(a.status)||o===null)return a;await a.body?.cancel().catch(()=>{});let s=new URL(o,n);if(!e(s.href))throw Error(`URL blocked by allowUrl policy: ${s.href}`);if(s.protocol!==`https:`&&s.protocol!==`http:`||s.username||s.password)throw Error(`Invalid redirect URL: ${s.href}`);i=r.redirectInit(i,a.status,n,s),n=s.href}throw Error(`Too many redirects fetching ${this.url}`)}static redirectInit(e,t,n,r){if(t!==303&&e.body instanceof ReadableStream)throw Error(`Cannot replay a streamed request body after a redirect`);let i=new Headers(e.headers),a=e.method?.toUpperCase()??`GET`,o=(t===301||t===302)&&a===`POST`||t===303&&a!==`GET`&&a!==`HEAD`;if(o)for(let e of[`content-encoding`,`content-language`,`content-location`,`content-type`,`content-length`])i.delete(e);if(new URL(n).origin!==r.origin)for(let e of[`authorization`,`proxy-authorization`,`cookie`,`cookie2`])i.delete(e);return{...e,headers:i,method:o?`GET`:e.method,body:o?void 0:e.body}}};async function a(e,t){let n=Number(e.headers.get(`content-length`));if(Number.isFinite(n)&&n>t)throw Error(`Response exceeds ${t} bytes (content-length ${n})`);let r=e.body;if(!r)return e.arrayBuffer();let i=r.getReader(),a=[],o=0;for(;;){let{done:e,value:n}=await i.read();if(e)break;if(o+=n.byteLength,o>t)throw await i.cancel().catch(()=>{}),Error(`Response exceeds ${t} bytes`);a.push(n)}let s=new Uint8Array(o),c=0;for(let e of a)s.set(e,c),c+=e.byteLength;return s.buffer}const o=2097152,s=1048576;var c=class e{static byFetch=new WeakMap;entries=new Map;bytes=0;static forFetch(t){let n=this.byFetch.get(t);return n||(n=new e,this.byFetch.set(t,n)),n}get(e){let t=this.entries.get(e);return t&&(this.entries.delete(e),this.entries.set(e,t)),t?.pending}set(e,t){this.delete(e);let n={pending:t,bytes:e.length*2};this.entries.set(e,n),this.bytes+=n.bytes,this.trim(),t.then(t=>{this.entries.get(e)===n&&(n.bytes+=t.length*2,this.bytes+=t.length*2,n.bytes>s&&this.delete(e),this.trim())},()=>{this.entries.get(e)===n&&this.delete(e)})}delete(e){let t=this.entries.get(e);t&&(this.bytes-=t.bytes,this.entries.delete(e))}trim(){for(let e of this.entries.keys()){if(this.entries.size<=64&&this.bytes<=s)break;this.delete(e)}}static read(e,t){return r(e,{...t,init:{headers:{"User-Agent":`Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36`}}}).then(e=>a(e,t.maxBytes??o)).then(e=>new TextDecoder().decode(e))}static load(e,t){if(t.signal?.throwIfAborted(),t.signal||t.allowUrl)return this.read(e,t);let n=t.cache??this.forFetch(t.fetch??globalThis.fetch),r=n.get(e);if(r)return r.then(e=>{let n=t.maxBytes??o;if(new TextEncoder().encode(e).byteLength>n)throw Error(`Response exceeds ${n} bytes`);return e});let i=this.read(e,t);return n.set(e,i),t.cache&&i.catch(()=>{n.get(e)===i&&n.delete(e)}),i}};function l(e,t,n,r){let i=new Map(r),a=n.includes(`italic`),o=a?n.includes(`normal`)?[0,1]:[1]:[void 0],s=[...a?[`ital`]:[],...i.keys(),`wght`].sort(),c=o.flatMap(e=>t.map(t=>s.map(n=>n===`ital`?String(e):n===`wght`?t:i.get(n)??``).join(`,`))).sort();return`${e}:${s.join(`,`)}@${c.join(`;`)}`}function u(e){let t=new URL(e.baseUrl??`https://fonts.googleapis.com/css2`);for(let n of e.families){if(typeof n==`string`){t.searchParams.append(`family`,l(n,[`400`],[`normal`],[]));continue}let e=n.weight??400,r=(Array.isArray(e)?[...e].sort((e,t)=>e-t):[e]).map(String),i=n.style??`normal`,a=(Array.isArray(i)?i:[i]).map(String),o=Object.entries(n.axes??{}).filter(([e])=>e!==`ital`&&e!==`wght`).map(([e,t])=>[e,String(t)]);t.searchParams.append(`family`,l(n.name,r,a,o))}return e.display&&t.searchParams.set(`display`,e.display),t.toString()}const d=/(?:\/\*\s*([^*]+?)\s*\*\/\s*)?@font-face\s*\{([^}]*)\}/g;function f(e,t,n){let i=`${e.family} ${e.subset}`,o=e.ranges.map(([e,t])=>`${e}-${t}`).join(`,`);return{name:i,subsetOf:e.family,subsetRank:p(e.ranges),key:`${i}:${e.weight??``}:${e.style??``}:${o}:${n??``}`,weight:e.weight,style:e.style,ranges:e.ranges,generic:n,data:()=>r(e.url,t).then(e=>a(e,t.maxBytes??33554432))}}function p(e){return e.length===0?4294967295:Math.min(...e.map(([e])=>e))}function m(e){let t=[];for(let n of e.split(`,`)){let e=n.trim().replace(/^U\+/i,``);if(e){if(e.includes(`-`)){let[n,r]=e.split(`-`);n&&r&&t.push([parseInt(n,16),parseInt(r,16)])}else if(e.includes(`?`))t.push([parseInt(e.replace(/\?/g,`0`),16),parseInt(e.replace(/\?/g,`F`),16)]);else{let n=parseInt(e,16);t.push([n,n])}}}return t}function h(e){let t=[],n=0;for(let r of e.matchAll(d)){let e=r[2];if(!e)continue;let i=e.match(/src:\s*url\(([^)]+)\)/)?.[1]?.replace(/['"]/g,``).trim(),a=e.match(/font-family:\s*['"]?([^'";]+)['"]?/)?.[1]?.trim();if(!i||!a)continue;let o=e.match(/unicode-range:\s*([^;]+)/)?.[1],s=e.match(/font-weight:\s*(\d+)(?:\s+(\d+))?/);t.push({family:a,subset:r[1]?.trim()||`subset-${n}`,url:i,weight:s&&!s[2]?Number(s[1]):void 0,style:e.match(/font-style:\s*([a-z]+)/i)?.[1],ranges:o?m(o):[]}),n+=1}return t}function g(e){let t=new Map;for(let n of e){let e=t.get(n.url)??new Set;e.add(n.weight),t.set(n.url,e)}let n=new Set,r=[];for(let i of e){if(n.has(i.url))continue;n.add(i.url);let e=(t.get(i.url)?.size??0)>1;r.push(e?{...i,weight:void 0}:i)}return r}function _(e){let t=new Set,n=e=>{for(let n of e)t.add(n.codePointAt(0))},r=e=>{typeof e==`string`?n(e):e.type===`text`?n(e.text):e.type===`container`&&e.children?.forEach(r)};return Array.isArray(e)?e.forEach(r):r(e),t}function v(e,t){if(e.length===0)return!0;for(let n of t)for(let[t,r]of e)if(n>=t&&n<=r)return!0;return!1}function y({fonts:e,source:t}){let n=_(t);return e.filter(e=>v(e.ranges??[],n))}function b(e,t={}){return{key:e,data:()=>r(e,t).then(e=>a(e,t.maxBytes??33554432))}}async function x(e){if(Array.isArray(e)&&(e={families:e}),e.families.length===0)return[];let t=await c.load(u(e),e),n=new Map(e.families.map((e,t)=>[typeof e==`string`?e:e.name,t])),r=new Map(e.families.flatMap(e=>typeof e==`string`||!e.generic?[]:[[e.name,e.generic]]));return g(h(t)).sort((e,t)=>(n.get(e.family)??0)-(n.get(t.family)??0)).map(t=>f(t,e,r.get(t.family)))}Object.defineProperty(exports,"a",{enumerable:!0,get:function(){return y}}),Object.defineProperty(exports,"c",{enumerable:!0,get:function(){return a}}),Object.defineProperty(exports,"i",{enumerable:!0,get:function(){return x}}),Object.defineProperty(exports,"l",{enumerable:!0,get:function(){return e}}),Object.defineProperty(exports,"n",{enumerable:!0,get:function(){return _}}),Object.defineProperty(exports,"o",{enumerable:!0,get:function(){return 33554432}}),Object.defineProperty(exports,"r",{enumerable:!0,get:function(){return b}}),Object.defineProperty(exports,"s",{enumerable:!0,get:function(){return r}}),Object.defineProperty(exports,"t",{enumerable:!0,get:function(){return`•◦■▪▸◂▾ 0123456789.-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`}});
@@ -0,0 +1 @@
1
+ var e=class{signal;constructor(e,t){let n=e.timeout??3e4,r=[e.signal,t,n<=0?void 0:AbortSignal.timeout(n)].filter(e=>e!=null);this.signal=r.length?AbortSignal.any(r):void 0}waitFor(e){let t=this.signal;return t?new Promise((n,r)=>{let i=()=>{t.removeEventListener(`abort`,i),r(t.reason)};t.addEventListener(`abort`,i,{once:!0}),e.then(e=>{t.removeEventListener(`abort`,i),n(e)},e=>{t.removeEventListener(`abort`,i),r(e)}),t.aborted&&i()}):e}};const t=new Set([301,302,303,307,308]),n=new Set([408,429,500,502,503,504]),r=33554432;async function i(e,t={}){return new a(e,t).send()}var a=class r{url;allowUrl;fetchImpl;init;constructor(t,n){if(n.allowUrl&&!n.allowUrl(t))throw Error(`URL blocked by allowUrl policy: ${t}`);this.url=t,this.allowUrl=n.allowUrl,this.fetchImpl=n.fetch??globalThis.fetch,this.init={...n.init,signal:new e(n,n.init?.signal).signal}}async send(){let e=this.allowUrl?await this.followRedirects(this.allowUrl):await this.attempt(this.url,this.init);if(!e.ok)throw Error(`HTTP ${e.status} ${e.statusText} fetching ${this.url}`);return e}async attempt(e,t){let r=t.method?.toUpperCase()??`GET`,i=r===`GET`||r===`HEAD`;for(let r=0;;r++){t.signal?.throwIfAborted();let a=100*2**r;try{let o=await this.fetchImpl.call(void 0,e,t);if(t.signal?.throwIfAborted(),!i||r===2||!n.has(o.status))return o;let s=o.headers.get(`retry-after`);if(s!==null){let e=Number(s),t=Number.isFinite(e)?e*1e3:Date.parse(s)-Date.now();Number.isFinite(t)&&(a=Math.max(a,t))}if(a>1e3)return o;o.body?.cancel().catch(()=>{})}catch(e){if(t.signal?.throwIfAborted(),!i||r===2||!(e instanceof Error)||e.name!==`TypeError`&&e.name!==`TimeoutError`)throw e}await this.wait(a)}}wait(e){let t=this.init.signal;return t?.throwIfAborted(),new Promise((n,r)=>{let i=setTimeout(()=>{t?.removeEventListener(`abort`,a),n()},e);function a(){clearTimeout(i),r(t?.reason)}t?.addEventListener(`abort`,a,{once:!0})})}async followRedirects(e){let n=this.url,i={...this.init,headers:new Headers(this.init.headers),redirect:`manual`};for(let a=0;a<5;a++){let a=await this.attempt(n,i),o=a.headers.get(`location`);if(!t.has(a.status)||o===null)return a;await a.body?.cancel().catch(()=>{});let s=new URL(o,n);if(!e(s.href))throw Error(`URL blocked by allowUrl policy: ${s.href}`);if(s.protocol!==`https:`&&s.protocol!==`http:`||s.username||s.password)throw Error(`Invalid redirect URL: ${s.href}`);i=r.redirectInit(i,a.status,n,s),n=s.href}throw Error(`Too many redirects fetching ${this.url}`)}static redirectInit(e,t,n,r){if(t!==303&&e.body instanceof ReadableStream)throw Error(`Cannot replay a streamed request body after a redirect`);let i=new Headers(e.headers),a=e.method?.toUpperCase()??`GET`,o=(t===301||t===302)&&a===`POST`||t===303&&a!==`GET`&&a!==`HEAD`;if(o)for(let e of[`content-encoding`,`content-language`,`content-location`,`content-type`,`content-length`])i.delete(e);if(new URL(n).origin!==r.origin)for(let e of[`authorization`,`proxy-authorization`,`cookie`,`cookie2`])i.delete(e);return{...e,headers:i,method:o?`GET`:e.method,body:o?void 0:e.body}}};async function o(e,t){let n=Number(e.headers.get(`content-length`));if(Number.isFinite(n)&&n>t)throw Error(`Response exceeds ${t} bytes (content-length ${n})`);let r=e.body;if(!r)return e.arrayBuffer();let i=r.getReader(),a=[],o=0;for(;;){let{done:e,value:n}=await i.read();if(e)break;if(o+=n.byteLength,o>t)throw await i.cancel().catch(()=>{}),Error(`Response exceeds ${t} bytes`);a.push(n)}let s=new Uint8Array(o),c=0;for(let e of a)s.set(e,c),c+=e.byteLength;return s.buffer}const s=2097152,c=1048576;var l=class e{static byFetch=new WeakMap;entries=new Map;bytes=0;static forFetch(t){let n=this.byFetch.get(t);return n||(n=new e,this.byFetch.set(t,n)),n}get(e){let t=this.entries.get(e);return t&&(this.entries.delete(e),this.entries.set(e,t)),t?.pending}set(e,t){this.delete(e);let n={pending:t,bytes:e.length*2};this.entries.set(e,n),this.bytes+=n.bytes,this.trim(),t.then(t=>{this.entries.get(e)===n&&(n.bytes+=t.length*2,this.bytes+=t.length*2,n.bytes>c&&this.delete(e),this.trim())},()=>{this.entries.get(e)===n&&this.delete(e)})}delete(e){let t=this.entries.get(e);t&&(this.bytes-=t.bytes,this.entries.delete(e))}trim(){for(let e of this.entries.keys()){if(this.entries.size<=64&&this.bytes<=c)break;this.delete(e)}}static read(e,t){return i(e,{...t,init:{headers:{"User-Agent":`Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36`}}}).then(e=>o(e,t.maxBytes??s)).then(e=>new TextDecoder().decode(e))}static load(e,t){if(t.signal?.throwIfAborted(),t.signal||t.allowUrl)return this.read(e,t);let n=t.cache??this.forFetch(t.fetch??globalThis.fetch),r=n.get(e);if(r)return r.then(e=>{let n=t.maxBytes??s;if(new TextEncoder().encode(e).byteLength>n)throw Error(`Response exceeds ${n} bytes`);return e});let i=this.read(e,t);return n.set(e,i),t.cache&&i.catch(()=>{n.get(e)===i&&n.delete(e)}),i}};function u(e,t,n,r){let i=new Map(r),a=n.includes(`italic`),o=a?n.includes(`normal`)?[0,1]:[1]:[void 0],s=[...a?[`ital`]:[],...i.keys(),`wght`].sort(),c=o.flatMap(e=>t.map(t=>s.map(n=>n===`ital`?String(e):n===`wght`?t:i.get(n)??``).join(`,`))).sort();return`${e}:${s.join(`,`)}@${c.join(`;`)}`}function d(e){let t=new URL(e.baseUrl??`https://fonts.googleapis.com/css2`);for(let n of e.families){if(typeof n==`string`){t.searchParams.append(`family`,u(n,[`400`],[`normal`],[]));continue}let e=n.weight??400,r=(Array.isArray(e)?[...e].sort((e,t)=>e-t):[e]).map(String),i=n.style??`normal`,a=(Array.isArray(i)?i:[i]).map(String),o=Object.entries(n.axes??{}).filter(([e])=>e!==`ital`&&e!==`wght`).map(([e,t])=>[e,String(t)]);t.searchParams.append(`family`,u(n.name,r,a,o))}return e.display&&t.searchParams.set(`display`,e.display),t.toString()}const f=/(?:\/\*\s*([^*]+?)\s*\*\/\s*)?@font-face\s*\{([^}]*)\}/g;function p(e,t,n){let r=`${e.family} ${e.subset}`,a=e.ranges.map(([e,t])=>`${e}-${t}`).join(`,`);return{name:r,subsetOf:e.family,subsetRank:m(e.ranges),key:`${r}:${e.weight??``}:${e.style??``}:${a}:${n??``}`,weight:e.weight,style:e.style,ranges:e.ranges,generic:n,data:()=>i(e.url,t).then(e=>o(e,t.maxBytes??33554432))}}function m(e){return e.length===0?4294967295:Math.min(...e.map(([e])=>e))}function h(e){let t=[];for(let n of e.split(`,`)){let e=n.trim().replace(/^U\+/i,``);if(e){if(e.includes(`-`)){let[n,r]=e.split(`-`);n&&r&&t.push([parseInt(n,16),parseInt(r,16)])}else if(e.includes(`?`))t.push([parseInt(e.replace(/\?/g,`0`),16),parseInt(e.replace(/\?/g,`F`),16)]);else{let n=parseInt(e,16);t.push([n,n])}}}return t}function g(e){let t=[],n=0;for(let r of e.matchAll(f)){let e=r[2];if(!e)continue;let i=e.match(/src:\s*url\(([^)]+)\)/)?.[1]?.replace(/['"]/g,``).trim(),a=e.match(/font-family:\s*['"]?([^'";]+)['"]?/)?.[1]?.trim();if(!i||!a)continue;let o=e.match(/unicode-range:\s*([^;]+)/)?.[1],s=e.match(/font-weight:\s*(\d+)(?:\s+(\d+))?/);t.push({family:a,subset:r[1]?.trim()||`subset-${n}`,url:i,weight:s&&!s[2]?Number(s[1]):void 0,style:e.match(/font-style:\s*([a-z]+)/i)?.[1],ranges:o?h(o):[]}),n+=1}return t}function _(e){let t=new Map;for(let n of e){let e=t.get(n.url)??new Set;e.add(n.weight),t.set(n.url,e)}let n=new Set,r=[];for(let i of e){if(n.has(i.url))continue;n.add(i.url);let e=(t.get(i.url)?.size??0)>1;r.push(e?{...i,weight:void 0}:i)}return r}const v=`•◦■▪▸◂▾ 0123456789.-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`;function y(e){let t=new Set,n=e=>{for(let n of e)t.add(n.codePointAt(0))},r=e=>{typeof e==`string`?n(e):e.type===`text`?n(e.text):e.type===`container`&&e.children?.forEach(r)};return Array.isArray(e)?e.forEach(r):r(e),t}function b(e,t){if(e.length===0)return!0;for(let n of t)for(let[t,r]of e)if(n>=t&&n<=r)return!0;return!1}function x({fonts:e,source:t}){let n=y(t);return e.filter(e=>b(e.ranges??[],n))}function S(e,t={}){return{key:e,data:()=>i(e,t).then(e=>o(e,t.maxBytes??33554432))}}async function C(e){if(Array.isArray(e)&&(e={families:e}),e.families.length===0)return[];let t=await l.load(d(e),e),n=new Map(e.families.map((e,t)=>[typeof e==`string`?e:e.name,t])),r=new Map(e.families.flatMap(e=>typeof e==`string`||!e.generic?[]:[[e.name,e.generic]]));return _(g(t)).sort((e,t)=>(n.get(e.family)??0)-(n.get(t.family)??0)).map(t=>p(t,e,r.get(t.family)))}export{x as a,o as c,C as i,e as l,y as n,r as o,S as r,i as s,v as t};