mutorjs 1.4.0 → 1.5.2
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/cli.cjs +19 -26
- package/dist/cli.cjs.map +1 -1
- package/dist/index.cjs +13 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +28 -8
- package/dist/index.d.ts +28 -8
- package/dist/index.js +13 -16
- package/dist/index.js.map +1 -1
- package/dist/mutor.global.js +13 -16
- package/dist/mutor.global.js.map +1 -1
- package/dist/server.cjs +12 -19
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.cts +28 -10
- package/dist/server.d.ts +28 -10
- package/dist/server.js +13 -20
- package/dist/server.js.map +1 -1
- package/package.json +3 -2
package/dist/index.cjs
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var V=Object.defineProperty;var Oe=Object.getOwnPropertyDescriptor;var Re=Object.getOwnPropertyNames;var be=Object.prototype.hasOwnProperty;var xe=(e,r)=>{for(var t in r)V(e,t,{get:r[t],enumerable:!0})},_e=(e,r,t,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of Re(r))!be.call(e,s)&&s!==t&&V(e,s,{get:()=>r[s],enumerable:!(o=Oe(r,s))||o.enumerable});return e};var Ae=e=>_e(V({},"__esModule",{value:!0}),e);var Ce={};xe(Ce,{default:()=>Se});module.exports=Ae(Ce);function $(e,r=""){return{context:e,renderedPath:r,includeStack:new Set([r&&r])}}function Z(e,r){return`${" ".repeat(e+r)}^`}var u=class e extends Error{constructor(t){super(t);this.name="MutorError";Object.setPrototypeOf(this,e.prototype)}},C=class e extends u{constructor(t,o,s,c,f){let d=o.toString().length+2,a=`${t}
|
|
2
2
|
|
|
3
|
-
`;
|
|
4
|
-
`,
|
|
5
|
-
`),
|
|
6
|
-
`,
|
|
7
|
-
Access to this computed property '${e}' is forbidden.`);return e}var
|
|
8
|
-
`).length,
|
|
9
|
-
`,r-1)+1;return[
|
|
10
|
-
`,r);return e.slice(r,t===-1?void 0:t).replaceAll(" "," ")}function
|
|
11
|
-
Expected an operator or the end of the expression.`};return
|
|
12
|
-
${
|
|
13
|
-
`)}
|
|
14
|
-
${r}`);let
|
|
15
|
-
Component '${r}' not found.
|
|
16
|
-
The component was included from ${`'${a}'`||"an anonymous component"}.
|
|
17
|
-
`;try{return this.__includeStack.add(r),this.renderComponent(r,t??this.__currentContext)}catch(p){if(this.__config.onIncludeFail==="throw")throw new g(o);let u={from:a,path:r,absolutePath:void 0};return this.__config.onIncludeFail==="ignoreLog"&&!this.__config.onIncludeError&&console.log(o),this.__config.onIncludeError?.(u,p)??""}finally{this.__includeStack.delete(r),this.__currentRenderedPath=a}}}};this.addConfig(r),Object.defineProperty(this.__namespaces.Mutor,"$$context",{get:()=>this.__currentContext})}addConfig(r){let{autoEscape:t,delimiters:a,allowedProps:o,forbiddenProps:p,keepOpeningTagEscapeDelimiter:u,allowFnCalls:y,cache:i,build:f,onIncludeFail:d,onIncludeError:m}=r;return this.__config={build:{include:new Set([...f?.include||b.build.include]),exclude:new Set([...b.build.exclude,...f?.exclude||[]])},autoEscape:t===!0?!0:t!==!1,allowedProps:o||b.allowedProps,allowFnCalls:!!y,onIncludeFail:d||b.onIncludeFail,onIncludeError:m||b.onIncludeError,cache:{...b.cache,...i||{}},forbiddenProps:new Set([...b.forbiddenProps,...p||[]]),keepOpeningTagEscapeDelimiter:u===!0?!0:u!==!1,delimiters:{...b.delimiters,...a||{}}},this.__config}restoreDefaultConfig(){this.__config={...b}}compile(r){return X(r,this.__config,{path:this.__currentRenderedPath||"anonymous"})}render(r,t){let a=this.__currentContext;a!==t&&(this.__currentContext=t);let o=this.compile(r)(K(t),this.__namespaces,this.__config.allowedProps,this.__config.forbiddenProps,Y,G);return this.__currentContext=a,o}renderComponent(r,t){if(!this.__compiledTemplatesMap.has(r))throw new g(`No template exists with the identifier '${r}'`);let a=this.__currentRenderedPath,o=this.__currentContext,p=this.__compiledTemplatesMap.get(r);this.__currentContext=t,this.__currentRenderedPath=r;let u=p.fn(K(t),this.__namespaces,this.__config.allowedProps,this.__config.forbiddenProps,Y,G);return this.__currentContext=o,this.__currentRenderedPath=a,u}registerComponent(r,t){let a=t.length*2+500;if(this.__cacheSize+a>this.__config.cache.maxSize&&!this.createEntrySpaceForTemplate(a))throw new g(`The template for the component '${r}' is too large and will not fit in the cache. Consider increasing 'cache.maxSize' in the config`);this.__cacheSize+=t.length*2+500,this.__compiledTemplatesMap.set(r,{fn:this.compile(t),size:a})}reset(){this.__config={...b},this.__compiledTemplatesMap.clear(),this.__currentContext=null,this.__cacheSize=0}createEntrySpaceForTemplate(r){if(this.__cacheSize+r<this.__config.cache.maxSize)return!0;if(r>this.__config.cache.maxSize)return!1;let t=this.__compiledTemplatesMap.entries().next().value;if(t){let[a,o]=t;this.__compiledTemplatesMap.delete(a),this.__cacheSize-=o.size}return this.createEntrySpaceForTemplate(r)}getDiagnostics(){let r=this.__config.cache.maxSize;return{bytesUsed:this.__cacheSize,bytesMax:r,readableUsed:`${(this.__cacheSize/1024/1024).toFixed(2)} MB`,readableMax:`${(r/1024/1024).toFixed(2)} MB`,totalEntries:this.__compiledTemplatesMap.size,percentFull:r>0?Math.min(100,Math.round(this.__cacheSize/r*100)):0,avgTemplateSize:this.__compiledTemplatesMap.size>0?Math.round(this.__cacheSize/this.__compiledTemplatesMap.size):0}}};var we=j;
|
|
3
|
+
`;a+=`at ${f}:${o}:${c+1}
|
|
4
|
+
`,o>1&&(a+=`${(o-1).toString().padStart(d-2)} | ...
|
|
5
|
+
`),a+=`${o} | ${s}
|
|
6
|
+
`,a+=Z(c,d+1);super(a);this.name="MutorCompilerError";Object.setPrototypeOf(this,e.prototype)}};var oe=new Set(["for","if","else","true","false","null","undefined","end","in","of"]),D=new Set(["::","||","??","&&","**","^","|","&","!","-","%","+","*","/",">","<",">=","<=","==","!=",">>","<<",".","?.","(",")","[","]",",",":","?"]);var se=new Set(["==","!="]),X=new Set([">","<",">=","<="]);var ie=new Set(["+","-"]),ae=new Set(["*","/","%"]),ce=new Set([".","?.","[","::"]),pe=new Set(["-","+","!"]),ue={"&":"&","<":"<",">":">",'"':""","'":"'"},A={build:{include:new Set([".html",".txt"]),exclude:new Set(["node_modules",".git"])},autoEscape:!0,allowedProps:new Set,forbiddenProps:new Set(["__proto__","constructor","prototype"]),allowFnCalls:!1,delimiters:{closingTag:"}}",openingTag:"{{",openingTagEscape:"\\",whitespaceTrim:"~",commentTag:"#"},keepOpeningTagEscapeDelimiter:!1,onIncludeFail:"throw",cache:{active:!0,maxSize:50*1024*1024}},le={JSON:{stringify(e,r){try{return JSON.stringify(e,null,r)}catch{throw new u("JSON::stringify failed")}},parse(e){if(typeof e!="string")throw new u("JSON::parse expects a string");try{return JSON.parse(e)}catch{throw new u("JSON::parse failed: invalid JSON string")}}},Object:{keys(e){if(!e||typeof e!="object")throw new u("Object::keys expects an object");return Object.keys(e)},values(e){if(!e||typeof e!="object")throw new u("Object::values expects an object");return Object.values(e)},entries(e){if(!e||typeof e!="object")throw new u("Object::entries expects an object");return Object.entries(e)},hasOwn(e,r){if(!e||typeof e!="object")throw new u("Object::hasOwn expects an object");return Object.hasOwn(e,r)},fromEntries(e){if(!Array.isArray(e))throw new u("Object::fromEntries expects an array");return Object.fromEntries(e)},pick(e,r){if(!e||typeof e!="object")throw new u("Object::pick expects an object");if(!Array.isArray(r))throw new u("Object::pick expects an array of keys");let t={};for(let o of r)Object.hasOwn(e,o)&&(t[o]=e[o]);return t},omit(e,r){if(!e||typeof e!="object")throw new u("Object::omit expects an object");if(!Array.isArray(r))throw new u("Object::omit expects an array of keys");let t={...e};for(let o of r)delete t[o];return t}},Array:{isArray(e){return Array.isArray(e)},from(e){return Array.from(e)},of(...e){return Array.of(...e)},unique(e){if(!Array.isArray(e))throw new u("Array::unique expects an array");return[...new Set(e)]},compact(e){if(!Array.isArray(e))throw new u("Array::compact expects an array");return e.filter(Boolean)},chunk(e,r){if(!Array.isArray(e))throw new u("Array::chunk expects an array");if(!Number.isInteger(r)||r<=0)throw new u("Array::chunk expects a positive integer size");let t=[];for(let o=0;o<e.length;o+=r)t.push(e.slice(o,o+r));return t},range(e,r,t=1){if(!Number.isFinite(e)||!Number.isFinite(r)||!Number.isFinite(t))throw new u("Array::range expects finite numbers");if(t===0)throw new u("Array::range step cannot be 0");let o=[];if(e<=r)for(let s=e;s<=r;s+=t)o.push(s);else for(let s=e;s>=r;s-=Math.abs(t))o.push(s);return o}},Number:{isFinite(e){return Number.isFinite(e)},isNaN(e){return Number.isNaN(e)},isInteger(e){return Number.isInteger(e)},parseInt(e,r=10){return Number.parseInt(e,r)},parseFloat(e){return Number.parseFloat(e)},clamp(e,r,t){return Math.min(Math.max(e,r),t)},toFixed(e,r=0){if(typeof e!="number")throw new u("Number::toFixed expects a number");return e.toFixed(r)},random(e=0,r=1){return Math.random()*(r-e)+e}},String:{fromCharCode(...e){return String.fromCharCode(...e)},capitalize(e){if(typeof e!="string")throw new u("String::capitalize expects a string");return e.length?e[0].toUpperCase()+e.slice(1):e}},Math:{abs(e){return Math.abs(e)},floor(e){return Math.floor(e)},ceil(e){return Math.ceil(e)},round(e){return Math.round(e)},trunc(e){return Math.trunc(e)},sign(e){return Math.sign(e)},sqrt(e){return Math.sqrt(e)},pow(e,r){return e**r},max(...e){return Math.max(...e)},min(...e){return Math.min(...e)},random(){return Math.random()}},Date:{now(){return Date.now()},parse(e){if(typeof e!="string")throw new u("Date::parse expects a string");return Date.parse(e)},new(e){return e===void 0?new Date:new Date(e)},iso(e){return new Date(e??Date.now()).toISOString()},timestamp(e){return new Date(e??Date.now()).getTime()}},Boolean:{valueOf(e){return!!e}},RegExp:{test(e,r,t=""){if(typeof e!="string")throw new u("RegExp::test expects a pattern string");if(typeof r!="string")throw new u("RegExp::test expects a value string");return new RegExp(e,t).test(r)},match(e,r,t=""){if(typeof e!="string")throw new u("RegExp::match expects a pattern string");if(typeof r!="string")throw new u("RegExp::match expects a value string");return r.match(new RegExp(e,t))}},URL:{encode(e){if(typeof e!="string")throw new u("URL::encode expects a string");return encodeURIComponent(e)},decode(e){if(typeof e!="string")throw new u("URL::decode expects a string");return decodeURIComponent(e)}}},fe=/('|"|`)use\s+async\1/,me=(async()=>{}).constructor;function U(e){return typeof e!="string"?e:/[&<>"']/.test(e)?e.replace(/[&<>"']/g,r=>ue[r]):e}function L(e,r,t){if(t.has(e)&&!r.has(e))throw new u(`Forbidden property access.
|
|
7
|
+
Access to this computed property '${e}' is forbidden.`);return e}var j="object",ge=Symbol("__mutor_safe_context");function B(e){if(!e||typeof e!==j||ge in e)return e;let r=new WeakSet;function t(s,c=""){if(!s||typeof s!==j||r.has(s))return s;r.add(s);let f=Object.getPrototypeOf(s);if(f&&f!==Object.prototype&&f!==Array.prototype)throw new u(`Unsafe prototype detected at ${c||"root"} in context.`);if(Array.isArray(s)){for(let a=0;a<s.length;a++)s[a]=t(s[a],`${c}[${a}]`);return s}if(s instanceof Map){for(let[a,m]of s.entries())typeof a===j&&t(a,`${c}.mapKey`),s.set(a,t(m,`${c}.mapValue`));return s}if(s instanceof Set){let a=new Set;for(let m of s.values())a.add(t(m,c));s.clear();for(let m of a)s.add(m);return s}let d=Object.getOwnPropertyDescriptors(s);for(let a of Object.keys(d)){let m=d[a];if(m.get||m.set)throw new u(`Getter/setter not allowed at '${c}.${a}' in context.`);let h=s[a];h&&typeof h===j&&(s[a]=t(h,`${c}.${a}`))}return s}let o=t(e);return o&&typeof o===j&&Object.defineProperty(o,ge,{value:!0,enumerable:!1,writable:!1,configurable:!1}),o}function F(e){return e.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$/g,"\\$")}function W(e,r){let o=e.slice(0,r).split(`
|
|
8
|
+
`).length,s=e.lastIndexOf(`
|
|
9
|
+
`,r-1)+1;return[o,s]}function z(e,r){let t=e.indexOf(`
|
|
10
|
+
`,r);return e.slice(r,t===-1?void 0:t).replaceAll(" "," ")}function v(e,r){let{scope:t,forbiddenProps:o,allowedProps:s}=r;function c(i){return t.includes(i)?i:`ctx.${i}`}function f(i){switch(i.type){case 17:return"}";case 5:return i.value;case 4:return`\`${/\$\\/.test(i.value)?F(i.value):i.value}\``;case 10:return i.true?"true":"false";case 12:return"null";case 11:return"undefined";case 7:return c(i.value);case 9:return`(${f(i.expr)})`;case 2:return`${i.operator}${f(i.expr)}`;case 0:return`${f(i.left)} ${i.operator} ${f(i.right)}`;case 1:return`${f(i.condition)} ? ${f(i.left)} : ${f(i.right)}`;case 8:return a(i);case 3:return m(i);case 6:return d(i);case 13:return h(i);case 16:return"} else {";case 14:return w(i);case 15:return E(i);default:throw new u(`Unsupported expression type '${i.type}'`)}}function d(i){if(i.left.type!==7)throw{message:"Invalid usage of namespace operator.",pos:i.pos};return`namespaces.${i.left.value}.${i.right.value}`}function a(i){let R=f(i.left);if(i.bracketNotation){let g=f(i.right),O=i.optional?"?.":"";return i.right.type===4||i.right.type===5?`${R}${O}[${g}]`:`${R}${O}[validateComputedProps(${g}, allowedProps, forbiddenProps)]`}else{let g=i.right.value,O=i.optional?"?.":".";if(o.has(g)&&!s.has(g))throw{message:"Forbidden property access.",pos:i.right.pos};return`${R}${O}${g}`}}function m(i){let R=f(i.expr),g=i.optional?"?.":"",O=i.args.map(N=>f(N)).join(", ");return`${R}${g}(${O})`}function h(i){let{iterable:R,loopType:g,variable:O}=i;return`for(const ${O} ${g===1?"in":"of"} ${v(R,r)}){`}function w(i){let{condition:R}=i;return`if(${v(R,r)}){`}function E(i){let{condition:R}=i;return`}else if(${v(R,r)}){`}let x=f(e);return x.includes("namespaces.Mutor.await")?x.replaceAll("namespaces.Mutor.await","await namespaces.Mutor.await"):x}function Y(e){switch(e){case 0:return"identifier";case 1:return"keyword";case 2:return"number";case 4:return"operator";case 3:return"string"}}function H(e,r){let t=0,o=!1;function s(n,p){let l=e[t],P=e[e.length-1];if(!l)throw{message:`Unexpected end of expression. Expected ${p?`'${p}'`:`${n===0?"an":"a"} ${Y(n)}`}.`,pos:P.pos+P.value.length-1};if(l.type!==n)throw{message:`Unexpected token type. Expected ${p?`'${p}'`:Y(n)} but got ${Y(l.type)} instead.`,pos:l.pos};if(p!==void 0&&l.value!==p)throw{message:`Unexpected token '${l?.value}'. Expected ${n===0?"an":"a"} ${Y(n)} instead.`,pos:l.pos};return e[t++]}function c(){let n=e[t-1].pos,p=s(0).value,l;try{l=s(1,"in")}catch{l=s(1,"of")}let P=l.value==="in"?1:0,M=b();return{type:13,loopType:P,iterable:M,variable:p,pos:n}}function f(){let n=b();return{condition:n,pos:n.pos,type:14}}function d(){let n=e[t-1].pos;try{return s(1,"if"),{...f(),type:15,pos:n}}catch{return{type:16,pos:n}}}function a(){let n=[];if(e[t]?.type===4&&e[t]?.value===")")return n;for(n.push(b());e[t]?.type===4&&e[t]?.value===","&&e[t]?.value!==")";)t++,n.push(b());return n}function m(){let n=e[t++];if(n?.type===2)return{type:5,value:n.value,pos:n.pos};if(n?.type===3)return{type:4,value:n.value,pos:n.pos};if(n?.type===1){if(n.value==="for"&&t===1)return c();if(n.value==="true"||n.value==="false")return{type:10,true:n.value==="true",pos:n.pos};if(n.value==="undefined")return{type:11,pos:n.pos};if(n.value==="null")return{type:12,pos:n.pos};if(n.value==="end"&&e.length===1)return{type:17,pos:n.pos};if(n.value==="if"&&t===1)return f();if(n.value==="else"&&t===1)return d()}if(n?.type===0)return{type:7,value:n.value,pos:n.pos};if(n?.type===4&&n.value==="("){let p=b();return s(4,")"),{type:9,expr:p,pos:n.pos}}if(n?.type===4&&pe.has(n.value)){let p=n.value,l=b();return{type:2,operator:p,expr:l,pos:n.pos}}throw t>e.length?{message:"Unexpected end of expression.",pos:e[e.length-1].pos}:{message:`Unexpected token '${n?.value}'.`,pos:n.pos}}function h(){let n=m();for(;e[t];){let p=e[t];if(p?.type===4&&p?.value==="("){if(t++,!o&&!r.allowFnCalls)throw{message:"Function calls are not allowed.",pos:e[t-1].pos};let l=a();s(4,")"),n={type:3,expr:n,args:l,pos:e[t-1].pos}}else if(p?.type===4&&p?.value==="?."&&e[t+1]?.type===4&&e[t+1]?.value==="("){if(t++,t++,!o&&!r.allowFnCalls)throw{message:"Function calls are not allowed.",pos:e[t-1].pos};let l=a();s(4,")"),n={type:3,expr:n,args:l,optional:!0,pos:e[t-1].pos}}else if(p?.type===4&&ce.has(p?.value)){let l=p?.value==="::",P=p?.value==="[",M=p?.value==="?.";if(t++,l&&(e[t-2]?.type!==0||e[t]?.type!==0))throw{message:`Invalid namespaces access. Expected syntax <IDENTIFIER>::<IDENTIFIER>, but got '${e[t-2]?.value}::${e[t]?.value??""}' instead.`,pos:e[t]?.pos??e[t-1].pos};if(l){o=!0;let _=m();n={type:6,left:n,right:_,pos:e[t-1].pos}}else if(P){let _=b();s(4,"]"),n={type:8,right:_,left:n,bracketNotation:!0,pos:e[t-1].pos}}else if(M)if(e[t]?.type===4&&e[t]?.value==="["){t++;let _=b();s(4,"]"),n={type:8,left:n,right:_,bracketNotation:!0,optional:!0,pos:e[t-1].pos}}else{let _=m();n={type:8,left:n,right:_,optional:!0,pos:e[t-1].pos}}else{let _=m();n={type:8,left:n,right:_,pos:e[t-1].pos}}}else break}return o=!1,n}function w(){let n=h();for(;e[t]?.type===4&&ae.has(e[t]?.value);){let p=e[t++].value,l=h();n={type:0,left:n,right:l,operator:p,pos:e[t-1].pos}}return n}function E(){let n=w();for(;e[t]?.type===4&&ie.has(e[t]?.value);){let p=e[t++].value,l=w();n={type:0,left:n,right:l,operator:p,pos:e[t-1].pos}}return n}function x(){let n=E();for(;e[t]?.type===4&&X.has(e[t]?.value);){let p=e[t++].value,l=E();n={type:0,left:n,right:l,operator:p,pos:e[t-1].pos}}return n}function i(){let n=x();for(;e[t]?.type===4&&X.has(e[t]?.value);){let p=e[t++].value,l=x();n={type:0,left:n,right:l,operator:p,pos:e[t-1].pos}}return n}function R(){let n=i();for(;e[t]?.type===4&&se.has(e[t]?.value);){let p=e[t++].value,l=i();n={type:0,left:n,right:l,operator:p,pos:e[t-1].pos}}return n}function g(){let n=O();for(;e[t]?.type===4&&e[t]?.value==="||";){let p=e[t++].value,l=O();n={type:0,left:n,right:l,operator:p,pos:e[t-1].pos}}return n}function O(){let n=N();for(;e[t]?.type===4&&e[t]?.value==="&&";){let p=e[t++].value,l=N();n={type:0,left:n,right:l,operator:p,pos:e[t-1].pos}}return n}function N(){let n=R();for(;e[t]?.type===4&&e[t]?.value==="??";){let p=e[t++].value,l=R();n={type:0,left:n,right:l,operator:p,pos:e[t-1].pos}}return n}function b(){let n=g();if(e[t]?.type!==4||e[t]?.value!=="?")return n;t++;let p=b();s(4,":");let l=b();return{type:1,left:p,right:l,condition:n,pos:e[t-1].pos}}let J=b();if(t!==e.length)throw{pos:e[t].pos,message:`Expected token '${e[t].value}'.
|
|
11
|
+
Expected an operator or the end of the expression.`};return J}function Q(e,{delimiters:r}){let t=`${r.openingTag}${r.whitespaceTrim}`,o=`${r.whitespaceTrim}${r.closingTag}`,s=e.startsWith(t),c=e.endsWith(o),f=e.startsWith(s?t+r.commentTag:r.openingTag+r.commentTag),d=e.slice(s?t.length:r.openingTag.length,e.length-(c?o.length:r.closingTag.length));if(f)return{isComment:f,leftTrim:s,rightTrim:c,async:fe.test(d)};let a=d.trim(),m=a.startsWith("for")||a.startsWith("if")||a.startsWith("else"),h=a.startsWith("for")||a.startsWith("if"),w=a==="end",E=a.startsWith("for");return{leftTrim:s,rightTrim:c,inner:d,isBlock:m,isBlockEnd:w,hasContext:E,requiresBlockClose:h,usesAwait:d.includes("Mutor::await")}}function k(e){let r=0,t="",o=[];function s(){let a="";if(/[a-zA-Z$_]/.test(t)){let m=r;for(;/[a-zA-Z$_0-9]/.test(e[m])&&m<e.length;)a+=e[m],m++;o.push({type:oe.has(a)?1:0,value:a,pos:r}),r=m,t=e[r]}}function c(){if(t!=='"'&&t!=="'"&&t!=="`")return!1;let a=t,m=r,h=r+1,w="";for(;h<e.length;){let E=e[h];if(E==="\\"){if(h+1>=e.length)throw{pos:h,message:"Unexpected end of string after escape character."};w+=E,w+=e[h+1],h+=2;continue}if(E===a)break;w+=E,h++}if(h>=e.length||e[h]!==a)throw{pos:m,message:`String literal missing closing ${a}.`};o.push({type:3,value:w,pos:m}),r=h}function f(){if(/[0-9]/.test(t)){let a=r,m="";for(;/[0-9.oxe]/.test(e[a])&&a<e.length;)m+=e[a],a++;let h=Number(m);if(Number.isNaN(h))throw{pos:r,message:"Found invalid number literal."};o.push({type:2,value:`${h}`,pos:r}),r=a-1,t=e[r]}}function d(){let a=`${t}${e[r+1]}`;if(D.has(a)){o.push({type:4,value:a,pos:r}),r++;return}if(D.has(t)){o.push({type:4,value:t,pos:r});return}}for(;r<e.length;){if(t=e[r],f(),s(),c(),d(),!/[a-zA-Z$_0-9\s\t\r\n'"`]/.test(t)&&!D.has(t)&&!D.has(e[r-1]+t))throw{message:`Unexpected token '${t}' in expression.`,pos:r};r++}return o.length?o:[{type:3,value:"",pos:0}]}function ee(e,r,t){let o=[],s=[],{delimiters:c,keepOpeningTagEscapeDelimiter:f,allowFnCalls:d,allowedProps:a,forbiddenProps:m,autoEscape:h}=r,w="sync",E=!1,x=0,i='let acc="";';for(;x<e.length;){let O=function(){let y=g,S=0;for(;y>=c.openingTagEscape.length&&e.slice(y-c.openingTagEscape.length,y)===c.openingTagEscape;)S++,y-=c.openingTagEscape.length;return S%2===1};var R=O;let g=e.indexOf(c.openingTag,x);if(g===-1){let y=e.slice(x);E&&(y=y.trimStart()),y&&(i+=`acc+=\`${F(y)}\`;`);break}if(O()){let y=e.slice(x,f?g+c.openingTagEscape.length+1:g-c.openingTag.length+1);E&&(y=y.trimStart(),E=!1),i+=`acc+=\`${F(y)}\`;`,f||(i+=`acc+=\`${c.openingTag}\`;`),x=g+c.openingTag.length;continue}let N=e.indexOf(c.closingTag,g);if(N===-1){let[y,S]=W(e,g),I=z(e,S);throw new C("No closing tag found.",y,I,g,t.path)}let b=e.slice(g,N+c.closingTag.length),{async:J,inner:n,leftTrim:p,rightTrim:l,isBlock:P,isBlockEnd:M,hasContext:_,requiresBlockClose:te,isComment:de,usesAwait:ye}=Q(b,{delimiters:c});if(ye&&w!=="async")throw{pos:g,message:`Mutor::await() requires async mode.
|
|
12
|
+
Add {{# 'use async' }} at the top of the template.`};let T=e.slice(x,g);T&&(E&&(T=T.trimStart()),p&&(T=T.trimEnd()),T&&(i+=`acc+=\`${F(T)}\`;`)),E=!1,x=N+c.closingTag.length;try{if(de)J&&(w="async");else{let y=k(n),S=H(y,{allowFnCalls:d});if(P&&te&&_?(o.push(S.variable),s.push({type:0,pos:g})):P&&te&&!_&&s.push({type:1,pos:g}),M){let K=s.pop();if(K?.type===0&&o.pop(),K===void 0)throw{message:"Unexpected end of block",pos:g}}let I=v(S,{allowedProps:a,forbiddenProps:m,scope:o});P||M?i+=I:i+=h&&!I.startsWith("namespaces.Mutor.include")?`acc+=escapeFn(${I});`:`acc+=${I};`}l&&(E=!0)}catch(y){let{message:S,pos:I}=y,K=p?c.whitespaceTrim.length+c.openingTag.length:c.openingTag.length,re=g+I+K,[we,ne]=W(e,re),Ee=z(e,ne);throw new C(S,we,Ee,re-ne,t.path)}}if(s.length){let g=s.pop()?.pos,[O,N]=W(e,g),b=z(e,N);throw new C("Unclosed block detected.",O,b,g-N,t.path)}return i+="return acc;",w==="async"?new me("ctx","namespaces","allowedProps","forbiddenProps","escapeFn","validateComputedProps",i):new Function("ctx","namespaces","allowedProps","forbiddenProps","escapeFn","validateComputedProps",i)}var G=class{constructor(r={}){this.__cacheSize=0;this.__config={...A};this.__compiledTemplatesMap=new Map;this.__namespaces={...le,Mutor:{await:async r=>await r}};this.addConfig(r)}addConfig(r){let{autoEscape:t,delimiters:o,allowedProps:s,forbiddenProps:c,keepOpeningTagEscapeDelimiter:f,allowFnCalls:d,cache:a,build:m,onIncludeFail:h,onIncludeError:w}=r;return this.__config={build:{include:new Set([...m?.include||A.build.include]),exclude:new Set([...A.build.exclude,...m?.exclude||[]])},autoEscape:t===!0?!0:t!==!1,allowedProps:s||A.allowedProps,allowFnCalls:!!d,onIncludeFail:h||A.onIncludeFail,onIncludeError:w||A.onIncludeError,cache:{...A.cache,...a||{}},forbiddenProps:new Set([...A.forbiddenProps,...c||[]]),keepOpeningTagEscapeDelimiter:f===!0?!0:f!==!1,delimiters:{...A.delimiters,...o||{}}},this.__config}restoreDefaultConfig(){this.__config={...A}}compile(r,t){return ee(r,this.__config,{path:t?.renderedPath||"anonymous"})}renderAsync(r,t){return new Promise(o=>{o(this.render(r,t))})}render(r,t){let o=$(t,"anonymous");return this.__renderWithRuntime(r,o)}__renderWithRuntime(r,t){return this.compile(r,t)(B(t.context),this.__createNamespacesWithRuntime(t),this.__config.allowedProps,this.__config.forbiddenProps,U,L)}__createNamespacesWithRuntime(r){return{...this.__namespaces,Mutor:{...this.__namespaces.Mutor,$$context:r.context}}}handleError(r,t,o,s){if(this.__config.onIncludeFail==="throw")throw r instanceof C?r:new u(r.message);let c={from:t,path:o,absolutePath:s};return this.__config.onIncludeFail==="ignoreLog"&&!this.__config.onIncludeError&&(r instanceof C?console.log(r):console.log(`[Mutor.js]
|
|
13
|
+
${r.message}`)),this.__config.onIncludeError?.(c,r)??""}createEntrySpaceForTemplate(r){if(this.__cacheSize+r<this.__config.cache.maxSize)return!0;if(r>this.__config.cache.maxSize)return!1;let t=this.__compiledTemplatesMap.entries().next().value;if(t){let[o,s]=t;this.__compiledTemplatesMap.delete(o),this.__cacheSize-=s.size}return this.createEntrySpaceForTemplate(r)}getDiagnostics(){let r=this.__config.cache.maxSize;return{bytesUsed:this.__cacheSize,bytesMax:r,readableUsed:`${(this.__cacheSize/1024/1024).toFixed(2)} MB`,readableMax:`${(r/1024/1024).toFixed(2)} MB`,totalEntries:this.__compiledTemplatesMap.size,percentFull:r>0?Math.min(100,Math.round(this.__cacheSize/r*100)):0,avgTemplateSize:this.__compiledTemplatesMap.size>0?Math.round(this.__cacheSize/this.__compiledTemplatesMap.size):0}}register(r,t){let o=t.length*2+500;if(this.__cacheSize+o>this.__config.cache.maxSize&&!this.createEntrySpaceForTemplate(o))throw new u(`The template for the component '${r}' is too large and will not fit in the cache. Consider increasing 'cache.maxSize' in the config`);let s=$(null,r);this.__cacheSize+=t.length*2+500,this.__compiledTemplatesMap.set(r,{fn:this.compile(t,s),size:o})}reset(){this.__config={...A},this.__compiledTemplatesMap.clear(),this.__cacheSize=0}};var q=class extends G{constructor(r={}){super(r)}__setupIncludeForRuntime(r){this.__namespaces.Mutor.include=(t,o)=>{if(r.includeStack.has(t))throw new u(`Circular include detected.
|
|
14
|
+
${Array.from(r.includeStack).join(" -> ")} -> ${t}`);let s=r.renderedPath;r.includeStack.add(t),r.renderedPath=t;try{return this.__renderComponent(t,o??r.context,r)}catch(c){return this.handleError(c,s,t,void 0)}finally{r.includeStack.delete(t),r.renderedPath=s}}}render(r,t){let o=$(t,"anonymous");return this.__setupIncludeForRuntime(o),this.__renderWithRuntime(r,o)}renderAsync(r,t){return new Promise(o=>{o(this.render(r,t))})}__renderComponent(r,t,o){if(!this.__compiledTemplatesMap.has(r))throw new u(`No template exists with the identifier '${r}'`);let s=this.__compiledTemplatesMap.get(r),c=o.context,f=o.renderedPath;o.context=t??c,o.renderedPath=r;try{return s.fn(B(o.context),this.__createNamespacesWithRuntime(o),this.__config.allowedProps,this.__config.forbiddenProps,U,L)}finally{o.context=c,o.renderedPath=f}}renderComponent(r,t){return this.__renderComponent(r,t,$(t,r))}renderAsyncComponent(r,t){return new Promise(o=>{o(this.renderComponent(r,t))})}registerComponent(r,t){return this.register(r,t)}};var Se=q;
|
|
18
15
|
//# sourceMappingURL=index.cjs.map
|