@takumi-rs/helpers 2.3.3 → 2.4.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.
@@ -0,0 +1 @@
1
+ const e=32*1024*1024,t=/url\(\s*(['"]?)(.*?)\1\s*\)/g;async function n(e,t={}){if(t.allowUrl&&!t.allowUrl(e))throw Error(`URL blocked by allowUrl policy: ${e}`);let n=t.fetch??globalThis.fetch,i=t.timeout??5e3,a=i<=0?void 0:AbortSignal.timeout(i),o=[t.signal,t.init?.signal,a].filter(e=>e!==void 0),s=o.length?AbortSignal.any(o):void 0,c={...t.init,signal:s},l=t.allowUrl?await r(e,t.allowUrl,n,c):await n(e,c);if(!l.ok)throw Error(`HTTP ${l.status} ${l.statusText} fetching ${e}`);return l}async function r(e,t,n,r){let i=e;for(let e=0;e<5;e++){let e=await n(i,{...r,redirect:`manual`}),a=e.headers.get(`location`);if(e.status<300||e.status>=400||!a)return e;if(await e.body?.cancel().catch(()=>{}),i=new URL(a,i).toString(),!t(i))throw Error(`URL blocked by allowUrl policy: ${i}`)}throw Error(`Too many redirects fetching ${e}`)}async function i(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}function a(e){return e.startsWith(`https://`)||e.startsWith(`http://`)}function o(e,n){if(typeof e==`string`)for(let r of e.matchAll(t)){let e=r[2]?.trim();e&&a(e)&&n.add(e)}else if(Array.isArray(e))for(let t of e)o(t,n)}function s(e){let t=new Set,n=e=>{let r=e=>{e&&(o(e.backgroundImage,t),o(e.maskImage,t))};if(r(e.style),r(e.preset),o(e.tw,t),e.type===`image`){typeof e.src==`string`&&a(e.src)&&t.add(e.src);return}if(e.type===`container`)for(let t of e.children??[])n(t)};return n(e),[...t]}function c(e,t,r){let a=r?.get(e);if(a)return a;let o=n(e,t).then(e=>i(e,t.maxBytes??33554432)).catch(t=>{throw r?.delete(e),t});return r?.set(e,o),o}async function l({node:e,sources:t=[],fetchCache:n,fetch:r,timeout:i,signal:a,maxBytes:o,allowUrl:l,throwOnError:u=!0}){let d=Array.isArray(e)?e:[e],f=new Map;for(let e of t)f.set(e.src,e);let p=[...new Set(d.flatMap(s))].filter(e=>!f.has(e)),m={fetch:r,timeout:i,signal:a,maxBytes:o,allowUrl:l},h=p.map(async e=>({src:e,data:await c(e,m,n)})),g=u?await Promise.all(h):(await Promise.allSettled(h)).filter(e=>e.status===`fulfilled`).map(e=>e.value);return[...f.values(),...g]}const u=new Map;function d(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 f(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`,d(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`,d(n.name,r,a,o))}return e.display&&t.searchParams.set(`display`,e.display),t.toString()}function p(e,t){return n(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=>i(e,t.maxBytes??2097152)).then(e=>new TextDecoder().decode(e))}function m(e,t){let n=t.cache??u,r=n.get(e);if(r)return r;let i=p(e,t);return n.set(e,i),i.catch(()=>n.delete(e)),i}const h=/(?:\/\*\s*([^*]+?)\s*\*\/\s*)?@font-face\s*\{([^}]*)\}/g;function g(e,t,r){let a=`${e.family} ${e.subset}`,o=e.ranges.map(([e,t])=>`${e}-${t}`).join(`,`);return{name:a,subsetOf:e.family,key:`${a}:${e.weight??``}:${e.style??``}:${o}:${r??``}`,weight:e.weight,style:e.style,ranges:e.ranges,generic:r,data:()=>n(e.url,t).then(e=>i(e,t.maxBytes??33554432))}}function _(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 v(e){let t=[],n=0;for(let r of e.matchAll(h)){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?_(o):[]}),n+=1}return t}function y(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 b(e){let t=new Set,n=e=>{for(let n of e)t.add(n.codePointAt(0))},r=e=>{e.type===`text`?n(e.text):e.type===`container`&&e.children?.forEach(r)};return typeof e==`string`?n(e):Array.isArray(e)?e.forEach(r):r(e),t}function x(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 S({fonts:e,source:t}){let n=b(t);return e.filter(e=>x(e.ranges??[],n))}function C(e,t={}){return{key:e,data:()=>n(e,t).then(e=>i(e,t.maxBytes??33554432))}}async function w(e){if(Array.isArray(e)&&(e={families:e}),e.families.length===0)return[];let t=await m(f(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 y(v(t)).sort((e,t)=>(n.get(e.family)??0)-(n.get(t.family)??0)).map(t=>g(t,e,r.get(t.family)))}export{e as a,i as c,S as i,C as n,n as o,w as r,l as s,b as t};
@@ -0,0 +1 @@
1
+ const e=/url\(\s*(['"]?)(.*?)\1\s*\)/g;async function t(e,t={}){if(t.allowUrl&&!t.allowUrl(e))throw Error(`URL blocked by allowUrl policy: ${e}`);let r=t.fetch??globalThis.fetch,i=t.timeout??5e3,a=i<=0?void 0:AbortSignal.timeout(i),o=[t.signal,t.init?.signal,a].filter(e=>e!==void 0),s=o.length?AbortSignal.any(o):void 0,c={...t.init,signal:s},l=t.allowUrl?await n(e,t.allowUrl,r,c):await r(e,c);if(!l.ok)throw Error(`HTTP ${l.status} ${l.statusText} fetching ${e}`);return l}async function n(e,t,n,r){let i=e;for(let e=0;e<5;e++){let e=await n(i,{...r,redirect:`manual`}),a=e.headers.get(`location`);if(e.status<300||e.status>=400||!a)return e;if(await e.body?.cancel().catch(()=>{}),i=new URL(a,i).toString(),!t(i))throw Error(`URL blocked by allowUrl policy: ${i}`)}throw Error(`Too many redirects fetching ${e}`)}async function r(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}function i(e){return e.startsWith(`https://`)||e.startsWith(`http://`)}function a(t,n){if(typeof t==`string`)for(let r of t.matchAll(e)){let e=r[2]?.trim();e&&i(e)&&n.add(e)}else if(Array.isArray(t))for(let e of t)a(e,n)}function o(e){let t=new Set,n=e=>{let r=e=>{e&&(a(e.backgroundImage,t),a(e.maskImage,t))};if(r(e.style),r(e.preset),a(e.tw,t),e.type===`image`){typeof e.src==`string`&&i(e.src)&&t.add(e.src);return}if(e.type===`container`)for(let t of e.children??[])n(t)};return n(e),[...t]}function s(e,n,i){let a=i?.get(e);if(a)return a;let o=t(e,n).then(e=>r(e,n.maxBytes??33554432)).catch(t=>{throw i?.delete(e),t});return i?.set(e,o),o}async function c({node:e,sources:t=[],fetchCache:n,fetch:r,timeout:i,signal:a,maxBytes:c,allowUrl:l,throwOnError:u=!0}){let d=Array.isArray(e)?e:[e],f=new Map;for(let e of t)f.set(e.src,e);let p=[...new Set(d.flatMap(o))].filter(e=>!f.has(e)),m={fetch:r,timeout:i,signal:a,maxBytes:c,allowUrl:l},h=p.map(async e=>({src:e,data:await s(e,m,n)})),g=u?await Promise.all(h):(await Promise.allSettled(h)).filter(e=>e.status===`fulfilled`).map(e=>e.value);return[...f.values(),...g]}const l=new Map;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()}function f(e,n){return t(e,{...n,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=>r(e,n.maxBytes??2097152)).then(e=>new TextDecoder().decode(e))}function p(e,t){let n=t.cache??l,r=n.get(e);if(r)return r;let i=f(e,t);return n.set(e,i),i.catch(()=>n.delete(e)),i}const m=/(?:\/\*\s*([^*]+?)\s*\*\/\s*)?@font-face\s*\{([^}]*)\}/g;function h(e,n,i){let a=`${e.family} ${e.subset}`,o=e.ranges.map(([e,t])=>`${e}-${t}`).join(`,`);return{name:a,subsetOf:e.family,key:`${a}:${e.weight??``}:${e.style??``}:${o}:${i??``}`,weight:e.weight,style:e.style,ranges:e.ranges,generic:i,data:()=>t(e.url,n).then(e=>r(e,n.maxBytes??33554432))}}function g(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 _(e){let t=[],n=0;for(let r of e.matchAll(m)){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?g(o):[]}),n+=1}return t}function v(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 y(e){let t=new Set,n=e=>{for(let n of e)t.add(n.codePointAt(0))},r=e=>{e.type===`text`?n(e.text):e.type===`container`&&e.children?.forEach(r)};return typeof e==`string`?n(e):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,n={}){return{key:e,data:()=>t(e,n).then(e=>r(e,n.maxBytes??33554432))}}async function C(e){if(Array.isArray(e)&&(e={families:e}),e.families.length===0)return[];let t=await p(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 v(_(t)).sort((e,t)=>(n.get(e.family)??0)-(n.get(t.family)??0)).map(t=>h(t,e,r.get(t.family)))}Object.defineProperty(exports,"a",{enumerable:!0,get:function(){return 33554432}}),Object.defineProperty(exports,"c",{enumerable:!0,get:function(){return r}}),Object.defineProperty(exports,"i",{enumerable:!0,get:function(){return x}}),Object.defineProperty(exports,"n",{enumerable:!0,get:function(){return S}}),Object.defineProperty(exports,"o",{enumerable:!0,get:function(){return t}}),Object.defineProperty(exports,"r",{enumerable:!0,get:function(){return C}}),Object.defineProperty(exports,"s",{enumerable:!0,get:function(){return c}}),Object.defineProperty(exports,"t",{enumerable:!0,get:function(){return y}});
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./fonts-D8OaJ4MQ.cjs"),t=require("./helpers-4kGcvc-Q.cjs");exports.collectCodepoints=e.t,exports.container=t.t,exports.defaultMaxFetchBytes=e.a,exports.em=t.n,exports.fetchOk=e.o,exports.fontFromUrl=e.n,exports.fr=t.r,exports.googleFonts=e.r,exports.image=t.i,exports.percentage=t.a,exports.prepareImages=e.s,exports.readBodyLimited=e.c,exports.rem=t.o,exports.rgba=t.s,exports.style=t.c,exports.subsetFonts=e.i,exports.text=t.l,exports.vh=t.u,exports.vw=t.d;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./fonts-DI_b5zLT.cjs"),t=require("./helpers-4kGcvc-Q.cjs");exports.collectCodepoints=e.t,exports.container=t.t,exports.defaultMaxFetchBytes=e.a,exports.em=t.n,exports.fetchOk=e.o,exports.fontFromUrl=e.n,exports.fr=t.r,exports.googleFonts=e.r,exports.image=t.i,exports.percentage=t.a,exports.prepareImages=e.s,exports.readBodyLimited=e.c,exports.rem=t.o,exports.rgba=t.s,exports.style=t.c,exports.subsetFonts=e.i,exports.text=t.l,exports.vh=t.u,exports.vw=t.d;
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{a as e,c as t,i as n,n as r,o as i,r as a,s as o,t as s}from"./fonts-Bmi5c76G.mjs";import{a as c,c as l,d as u,i as d,l as f,n as p,o as m,r as h,s as g,t as _,u as v}from"./helpers-CVQCT1Rh.mjs";export{s as collectCodepoints,_ as container,e as defaultMaxFetchBytes,p as em,i as fetchOk,r as fontFromUrl,h as fr,a as googleFonts,d as image,c as percentage,o as prepareImages,t as readBodyLimited,m as rem,g as rgba,l as style,n as subsetFonts,f as text,v as vh,u as vw};
1
+ import{a as e,c as t,i as n,n as r,o as i,r as a,s as o,t as s}from"./fonts-BXhtO--P.mjs";import{a as c,c as l,d as u,i as d,l as f,n as p,o as m,r as h,s as g,t as _,u as v}from"./helpers-CVQCT1Rh.mjs";export{s as collectCodepoints,_ as container,e as defaultMaxFetchBytes,p as em,i as fetchOk,r as fontFromUrl,h as fr,a as googleFonts,d as image,c as percentage,o as prepareImages,t as readBodyLimited,m as rem,g as rgba,l as style,n as subsetFonts,f as text,v as vh,u as vw};
package/dist/renderer.cjs CHANGED
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./fonts-D8OaJ4MQ.cjs");function t(e){return e instanceof Uint8Array||e instanceof ArrayBuffer||typeof Buffer<`u`&&Buffer.isBuffer(e)}function n(e){return t(e)?e:typeof e.data==`function`?e.data():e.data}function r(e){return t(e)?e:`key`in e&&e.key?e.key:typeof e.data==`function`?`${e.name??``}:${e.weight??``}:${e.style??``}`:e.data}async function i(e){let{sources:t=[],cache:n}=Array.isArray(e)?{sources:e}:e,r=new Map;for(let e of t)r.set(e.src,e);return Promise.all([...r.values()].map(async({src:e,data:t,cache:r})=>({src:e,data:typeof t==`function`?await t():t,cache:r??n})))}var a=class{registerInner;byKey=new Map;byData=new WeakMap;constructor(e){this.registerInner=e}getFont(e){return typeof e==`string`?this.byKey.get(e):this.byData.get(e)}setFont(e,t){typeof e==`string`?this.byKey.set(e,t):this.byData.set(e,t)}deleteFont(e){typeof e==`string`?this.byKey.delete(e):this.byData.delete(e)}async register(i){let a=typeof i==`string`?e.n(i):i,o=r(a),s=this.getFont(o);if(s)return s;let c=n(a),l=Promise.resolve(c).then(e=>this.registerInner(t(a)?e:{...a,data:e})).catch(e=>{throw this.deleteFont(o),e});return this.setFont(o,l),l}async prepareFonts(e){if(!e)return;let t=await Promise.all(e.map(e=>this.register(e)));return[...new Set(t.flat().map(e=>e.name))]}async resolveResources(e,t,n){let r=await this.prepareFonts(e);return{images:t?await i(t):void 0,fontFamilies:n??r}}};async function o(t,n,r){let{fonts:i,fontFamilies:a,signal:o,images:s,...c}=n;o?.throwIfAborted();let l=await i,u=await t.resolveResources(l&&e.i({fonts:l,source:r}),s,a);return o?.throwIfAborted(),{options:{...c,...u},signal:o}}exports.FontRegistry=a,exports.prepareRenderInput=o,exports.resolveImageLoaders=i;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./fonts-DI_b5zLT.cjs");function t(e){return e instanceof Uint8Array||e instanceof ArrayBuffer||typeof Buffer<`u`&&Buffer.isBuffer(e)}function n(e){return t(e)?e:typeof e.data==`function`?e.data():e.data}function r(e){return t(e)?e:`key`in e&&e.key?e.key:typeof e.data==`function`?`${e.name??``}:${e.weight??``}:${e.style??``}`:e.data}async function i(e){let{sources:t=[],cache:n}=Array.isArray(e)?{sources:e}:e,r=new Map;for(let e of t)r.set(e.src,e);return Promise.all([...r.values()].map(async({src:e,data:t,cache:r})=>({src:e,data:typeof t==`function`?await t():t,cache:r??n})))}var a=class{registerInner;byKey=new Map;byData=new WeakMap;constructor(e){this.registerInner=e}getFont(e){return typeof e==`string`?this.byKey.get(e):this.byData.get(e)}setFont(e,t){typeof e==`string`?this.byKey.set(e,t):this.byData.set(e,t)}deleteFont(e){typeof e==`string`?this.byKey.delete(e):this.byData.delete(e)}async register(i){let a=typeof i==`string`?e.n(i):i,o=r(a),s=this.getFont(o);if(s)return s;let c=n(a),l=Promise.resolve(c).then(e=>this.registerInner(t(a)?e:{...a,data:e})).catch(e=>{throw this.deleteFont(o),e});return this.setFont(o,l),l}async prepareFonts(e){if(!e)return;let t=await Promise.all(e.map(e=>this.register(e)));return[...new Set(t.flat().map(e=>e.name))]}async resolveResources(e,t,n){let r=await this.prepareFonts(e);return{images:t?await i(t):void 0,fontFamilies:n??r}}};async function o(t,n,r){let{fonts:i,fontFamilies:a,signal:o,images:s,...c}=n;o?.throwIfAborted();let l=await i,u=await t.resolveResources(l&&e.i({fonts:l,source:r}),s,a);return o?.throwIfAborted(),{options:{...c,...u},signal:o}}exports.FontRegistry=a,exports.prepareRenderInput=o,exports.resolveImageLoaders=i;
package/dist/renderer.mjs CHANGED
@@ -1 +1 @@
1
- import{i as e,n as t}from"./fonts-Bmi5c76G.mjs";function n(e){return e instanceof Uint8Array||e instanceof ArrayBuffer||typeof Buffer<`u`&&Buffer.isBuffer(e)}function r(e){return n(e)?e:typeof e.data==`function`?e.data():e.data}function i(e){return n(e)?e:`key`in e&&e.key?e.key:typeof e.data==`function`?`${e.name??``}:${e.weight??``}:${e.style??``}`:e.data}async function a(e){let{sources:t=[],cache:n}=Array.isArray(e)?{sources:e}:e,r=new Map;for(let e of t)r.set(e.src,e);return Promise.all([...r.values()].map(async({src:e,data:t,cache:r})=>({src:e,data:typeof t==`function`?await t():t,cache:r??n})))}var o=class{registerInner;byKey=new Map;byData=new WeakMap;constructor(e){this.registerInner=e}getFont(e){return typeof e==`string`?this.byKey.get(e):this.byData.get(e)}setFont(e,t){typeof e==`string`?this.byKey.set(e,t):this.byData.set(e,t)}deleteFont(e){typeof e==`string`?this.byKey.delete(e):this.byData.delete(e)}async register(e){let a=typeof e==`string`?t(e):e,o=i(a),s=this.getFont(o);if(s)return s;let c=r(a),l=Promise.resolve(c).then(e=>this.registerInner(n(a)?e:{...a,data:e})).catch(e=>{throw this.deleteFont(o),e});return this.setFont(o,l),l}async prepareFonts(e){if(!e)return;let t=await Promise.all(e.map(e=>this.register(e)));return[...new Set(t.flat().map(e=>e.name))]}async resolveResources(e,t,n){let r=await this.prepareFonts(e);return{images:t?await a(t):void 0,fontFamilies:n??r}}};async function s(t,n,r){let{fonts:i,fontFamilies:a,signal:o,images:s,...c}=n;o?.throwIfAborted();let l=await i,u=await t.resolveResources(l&&e({fonts:l,source:r}),s,a);return o?.throwIfAborted(),{options:{...c,...u},signal:o}}export{o as FontRegistry,s as prepareRenderInput,a as resolveImageLoaders};
1
+ import{i as e,n as t}from"./fonts-BXhtO--P.mjs";function n(e){return e instanceof Uint8Array||e instanceof ArrayBuffer||typeof Buffer<`u`&&Buffer.isBuffer(e)}function r(e){return n(e)?e:typeof e.data==`function`?e.data():e.data}function i(e){return n(e)?e:`key`in e&&e.key?e.key:typeof e.data==`function`?`${e.name??``}:${e.weight??``}:${e.style??``}`:e.data}async function a(e){let{sources:t=[],cache:n}=Array.isArray(e)?{sources:e}:e,r=new Map;for(let e of t)r.set(e.src,e);return Promise.all([...r.values()].map(async({src:e,data:t,cache:r})=>({src:e,data:typeof t==`function`?await t():t,cache:r??n})))}var o=class{registerInner;byKey=new Map;byData=new WeakMap;constructor(e){this.registerInner=e}getFont(e){return typeof e==`string`?this.byKey.get(e):this.byData.get(e)}setFont(e,t){typeof e==`string`?this.byKey.set(e,t):this.byData.set(e,t)}deleteFont(e){typeof e==`string`?this.byKey.delete(e):this.byData.delete(e)}async register(e){let a=typeof e==`string`?t(e):e,o=i(a),s=this.getFont(o);if(s)return s;let c=r(a),l=Promise.resolve(c).then(e=>this.registerInner(n(a)?e:{...a,data:e})).catch(e=>{throw this.deleteFont(o),e});return this.setFont(o,l),l}async prepareFonts(e){if(!e)return;let t=await Promise.all(e.map(e=>this.register(e)));return[...new Set(t.flat().map(e=>e.name))]}async resolveResources(e,t,n){let r=await this.prepareFonts(e);return{images:t?await a(t):void 0,fontFamilies:n??r}}};async function s(t,n,r){let{fonts:i,fontFamilies:a,signal:o,images:s,...c}=n;o?.throwIfAborted();let l=await i,u=await t.resolveResources(l&&e({fonts:l,source:r}),s,a);return o?.throwIfAborted(),{options:{...c,...u},signal:o}}export{o as FontRegistry,s as prepareRenderInput,a as resolveImageLoaders};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@takumi-rs/helpers",
3
- "version": "2.3.3",
3
+ "version": "2.4.0",
4
4
  "description": "Utility helpers for converting JSX/HTML to Takumi node trees, loading fonts, and processing emoji.",
5
5
  "keywords": [
6
6
  "css",
@@ -1 +0,0 @@
1
- const e=32*1024*1024,t=/url\(\s*(['"]?)(.*?)\1\s*\)/g;async function n(e,t={}){if(t.allowUrl&&!t.allowUrl(e))throw Error(`URL blocked by allowUrl policy: ${e}`);let n=t.fetch??globalThis.fetch,r=t.timeout??5e3,i=r<=0?void 0:AbortSignal.timeout(r),a=[t.signal,t.init?.signal,i].filter(e=>e!==void 0),o=a.length?AbortSignal.any(a):void 0,s=await n(e,{...t.init,signal:o});if(!s.ok)throw Error(`HTTP ${s.status} ${s.statusText} fetching ${e}`);return s}async function r(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}function i(e){return e.startsWith(`https://`)||e.startsWith(`http://`)}function a(e,n){if(typeof e==`string`)for(let r of e.matchAll(t)){let e=r[2]?.trim();e&&i(e)&&n.add(e)}else if(Array.isArray(e))for(let t of e)a(t,n)}function o(e){let t=new Set,n=e=>{let r=e=>{e&&(a(e.backgroundImage,t),a(e.maskImage,t))};if(r(e.style),r(e.preset),a(e.tw,t),e.type===`image`){typeof e.src==`string`&&i(e.src)&&t.add(e.src);return}if(e.type===`container`)for(let t of e.children??[])n(t)};return n(e),[...t]}function s(e,t,i){let a=i?.get(e);if(a)return a;let o=n(e,t).then(e=>r(e,t.maxBytes??33554432)).catch(t=>{throw i?.delete(e),t});return i?.set(e,o),o}async function c({node:e,sources:t=[],fetchCache:n,fetch:r,timeout:i,signal:a,maxBytes:c,allowUrl:l,throwOnError:u=!0}){let d=Array.isArray(e)?e:[e],f=new Map;for(let e of t)f.set(e.src,e);let p=[...new Set(d.flatMap(o))].filter(e=>!f.has(e)),m={fetch:r,timeout:i,signal:a,maxBytes:c,allowUrl:l},h=p.map(async e=>({src:e,data:await s(e,m,n)})),g=u?await Promise.all(h):(await Promise.allSettled(h)).filter(e=>e.status===`fulfilled`).map(e=>e.value);return[...f.values(),...g]}const l=new Map;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()}function f(e,t){return n(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=>r(e,t.maxBytes??2097152)).then(e=>new TextDecoder().decode(e))}function p(e,t){let n=t.cache??l,r=n.get(e);if(r)return r;let i=f(e,t);return n.set(e,i),i.catch(()=>n.delete(e)),i}const m=/(?:\/\*\s*([^*]+?)\s*\*\/\s*)?@font-face\s*\{([^}]*)\}/g;function h(e,t,i){let a=`${e.family} ${e.subset}`,o=e.ranges.map(([e,t])=>`${e}-${t}`).join(`,`);return{name:a,subsetOf:e.family,key:`${a}:${e.weight??``}:${e.style??``}:${o}:${i??``}`,weight:e.weight,style:e.style,ranges:e.ranges,generic:i,data:()=>n(e.url,t).then(e=>r(e,t.maxBytes??33554432))}}function g(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 _(e){let t=[],n=0;for(let r of e.matchAll(m)){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?g(o):[]}),n+=1}return t}function v(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 y(e){let t=new Set,n=e=>{for(let n of e)t.add(n.codePointAt(0))},r=e=>{e.type===`text`?n(e.text):e.type===`container`&&e.children?.forEach(r)};return typeof e==`string`?n(e):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:()=>n(e,t).then(e=>r(e,t.maxBytes??33554432))}}async function C(e){if(Array.isArray(e)&&(e={families:e}),e.families.length===0)return[];let t=await p(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 v(_(t)).sort((e,t)=>(n.get(e.family)??0)-(n.get(t.family)??0)).map(t=>h(t,e,r.get(t.family)))}export{e as a,r as c,x as i,S as n,n as o,C as r,c as s,y as t};
@@ -1 +0,0 @@
1
- const e=/url\(\s*(['"]?)(.*?)\1\s*\)/g;async function t(e,t={}){if(t.allowUrl&&!t.allowUrl(e))throw Error(`URL blocked by allowUrl policy: ${e}`);let n=t.fetch??globalThis.fetch,r=t.timeout??5e3,i=r<=0?void 0:AbortSignal.timeout(r),a=[t.signal,t.init?.signal,i].filter(e=>e!==void 0),o=a.length?AbortSignal.any(a):void 0,s=await n(e,{...t.init,signal:o});if(!s.ok)throw Error(`HTTP ${s.status} ${s.statusText} fetching ${e}`);return s}async function n(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}function r(e){return e.startsWith(`https://`)||e.startsWith(`http://`)}function i(t,n){if(typeof t==`string`)for(let i of t.matchAll(e)){let e=i[2]?.trim();e&&r(e)&&n.add(e)}else if(Array.isArray(t))for(let e of t)i(e,n)}function a(e){let t=new Set,n=e=>{let a=e=>{e&&(i(e.backgroundImage,t),i(e.maskImage,t))};if(a(e.style),a(e.preset),i(e.tw,t),e.type===`image`){typeof e.src==`string`&&r(e.src)&&t.add(e.src);return}if(e.type===`container`)for(let t of e.children??[])n(t)};return n(e),[...t]}function o(e,r,i){let a=i?.get(e);if(a)return a;let o=t(e,r).then(e=>n(e,r.maxBytes??33554432)).catch(t=>{throw i?.delete(e),t});return i?.set(e,o),o}async function s({node:e,sources:t=[],fetchCache:n,fetch:r,timeout:i,signal:s,maxBytes:c,allowUrl:l,throwOnError:u=!0}){let d=Array.isArray(e)?e:[e],f=new Map;for(let e of t)f.set(e.src,e);let p=[...new Set(d.flatMap(a))].filter(e=>!f.has(e)),m={fetch:r,timeout:i,signal:s,maxBytes:c,allowUrl:l},h=p.map(async e=>({src:e,data:await o(e,m,n)})),g=u?await Promise.all(h):(await Promise.allSettled(h)).filter(e=>e.status===`fulfilled`).map(e=>e.value);return[...f.values(),...g]}const c=new Map;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()}function d(e,r){return t(e,{...r,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=>n(e,r.maxBytes??2097152)).then(e=>new TextDecoder().decode(e))}function f(e,t){let n=t.cache??c,r=n.get(e);if(r)return r;let i=d(e,t);return n.set(e,i),i.catch(()=>n.delete(e)),i}const p=/(?:\/\*\s*([^*]+?)\s*\*\/\s*)?@font-face\s*\{([^}]*)\}/g;function m(e,r,i){let a=`${e.family} ${e.subset}`,o=e.ranges.map(([e,t])=>`${e}-${t}`).join(`,`);return{name:a,subsetOf:e.family,key:`${a}:${e.weight??``}:${e.style??``}:${o}:${i??``}`,weight:e.weight,style:e.style,ranges:e.ranges,generic:i,data:()=>t(e.url,r).then(e=>n(e,r.maxBytes??33554432))}}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(p)){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}function v(e){let t=new Set,n=e=>{for(let n of e)t.add(n.codePointAt(0))},r=e=>{e.type===`text`?n(e.text):e.type===`container`&&e.children?.forEach(r)};return typeof e==`string`?n(e):Array.isArray(e)?e.forEach(r):r(e),t}function y(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 b({fonts:e,source:t}){let n=v(t);return e.filter(e=>y(e.ranges??[],n))}function x(e,r={}){return{key:e,data:()=>t(e,r).then(e=>n(e,r.maxBytes??33554432))}}async function S(e){if(Array.isArray(e)&&(e={families:e}),e.families.length===0)return[];let t=await f(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(t)).sort((e,t)=>(n.get(e.family)??0)-(n.get(t.family)??0)).map(t=>m(t,e,r.get(t.family)))}Object.defineProperty(exports,"a",{enumerable:!0,get:function(){return 33554432}}),Object.defineProperty(exports,"c",{enumerable:!0,get:function(){return n}}),Object.defineProperty(exports,"i",{enumerable:!0,get:function(){return b}}),Object.defineProperty(exports,"n",{enumerable:!0,get:function(){return x}}),Object.defineProperty(exports,"o",{enumerable:!0,get:function(){return t}}),Object.defineProperty(exports,"r",{enumerable:!0,get:function(){return S}}),Object.defineProperty(exports,"s",{enumerable:!0,get:function(){return s}}),Object.defineProperty(exports,"t",{enumerable:!0,get:function(){return v}});