UNPKG

jitar

Version:

Distributed runtime for JavaScript and TypeScript to chop monolithic applications into micros.

2 lines (1 loc) 49.3 kB
import e,{promises as t}from"node:fs";import s from"node:path";import{glob as r}from"glob";import i from"mime-types";import n from"dotenv";const o={MODULE_PATTERN:"**/*.js",RESOURCE_PATTERN:"**/*.json",SEGMENT_PATTERN:"**/*.json"};class a extends Error{#e;constructor(e){super(`Invalid location: ${e}`),this.#e=e}get location(){return this.#e}}class c extends Error{#t;constructor(e){super(`The file '${e}' could not be found`),this.#t=e}get filename(){return this.#t}}class u{#e;#s;#r;constructor(e,t,s){this.#e=e,this.#s=t,this.#r=s}get location(){return this.#e}get type(){return this.#s}get content(){return this.#r}get size(){return this.#r.length}}class l{#e;#i;#n;constructor(e,t){const s=t.resolve(e);this.#e=t.normalize(e),this.#i=t.normalize(s),this.#n=t}getAbsoluteLocation(e){const t=this.#n.isAbsolute(e)?e:this.#n.join(this.#e,e),s=this.#n.resolve(t),r=this.#n.normalize(s);return this.#o(r,e),r}getRelativeLocation(e){const t=this.#n.relative(this.#e,e);return this.#n.normalize(t)}normalizeLocation(e){return this.#n.normalize(e)}async getType(e){const t=this.getAbsoluteLocation(e);return await this.#n.mimeType(t)??"application/octet-stream"}async getContent(e){const t=this.getAbsoluteLocation(e);if(!1===await this.#n.exists(t))throw new c(e);return this.#n.read(t)}async exists(e){const t=this.getAbsoluteLocation(e);return this.#n.exists(t)}isDirectory(e){const t=this.getAbsoluteLocation(e);return this.#n.isDirectory(t)}async read(e){const t=this.getAbsoluteLocation(e),s=await this.getType(t),r=await this.getContent(t);return new u(e,s,r)}async write(e,t){const s=this.getAbsoluteLocation(e);return this.#n.write(s,t)}async copy(e,t){const s=this.getAbsoluteLocation(e),r=this.getAbsoluteLocation(t);return this.#n.copy(s,r)}async delete(e){const t=this.getAbsoluteLocation(e);return this.#n.delete(t)}async filter(e){const t=this.getAbsoluteLocation("./"),s=this.#n.normalize(e);return(await this.#n.filter(t,s)).map(e=>this.#n.normalize(e))}async filterWithIgnores(e,t){const s=await this.filter(e),r=t.map(e=>this.filter(e)),i=await Promise.all(r),n=new Set(i.flat());return s.filter(e=>!1===n.has(e))}#o(e,t){if(!1===e.startsWith(this.#i))throw new a(t)}}class h{copy(e,s){return t.cp(e,s,{recursive:!0,force:!0})}delete(e){return t.rm(e,{recursive:!0,force:!0})}async exists(e){try{return await t.stat(e),!0}catch{return!1}}isAbsolute(e){return s.isAbsolute(e)}isDirectory(t){try{return e.statSync(t).isDirectory()}catch{return!1}}filter(e,t){return r(`${e}/${t}`)}join(...e){return s.join(...e)}read(e){return t.readFile(e)}resolve(e){return s.resolve(e)}relative(e,t){return s.relative(e,t)}normalize(e){return e.replaceAll(s.win32.sep,s.posix.sep)}async mimeType(e){const t=i.lookup(e);if(!1!==t)return t}async write(r,i){const n=s.dirname(r);return e.mkdirSync(n,{recursive:!0}),t.writeFile(r,i)}}class p extends l{constructor(e){super(e,new h)}}class d{#a;#c;#u;constructor(e,t,s){this.#a=e,this.#c=t,this.#u=s}get resources(){return this.#c}get repository(){return this.#a}get segmentation(){return this.#u}}class m{#c;constructor(e){this.#c=e}isResourceModule(e){return this.#c.includes(e)}}const f="private",g="0.0.0",E="index.js",y=".json",w="default";class S{#l=0;next(){return"$"+ ++this.#l}}const b="js",A=/\.js$/,T=[".","/","http:","https:"];class I{translatePath(e){const t=e.split("/"),s=[];for(const e of t){switch(e.trim()){case"":case".":continue;case"..":s.pop();continue}s.push(e)}return s.join("/")}makePathRelative(e,t){if(""===t)return`./${e}`;const s=e.split("/"),r=t.split("/");for(;s[0]===r[0];)s.shift(),r.shift();const i=r.map(()=>"..").join("/");return`${r.length>0?i:"."}/${s.join("/")}`}makePathAbsolute(e,t,s="/"){const r=""!==t?`${t}/${e}`:e;return`${s}${this.translatePath(r)}`}extractPath(e){return e.split("/").slice(0,-1).join("/")}extractFilename(e){return e.split("/").pop()}assureExtension(e){return e.endsWith(`.${b}`)?e:`${e}.${b}`}addSubExtension(e,t){return e.replace(A,`.${t}.${b}`)}isApplicationModule(e){return T.some(t=>e.startsWith(t))}}let v=class extends Error{constructor(e,t){super(`Failed to load resource file '${e}' because of: ${t}`)}};class O{#h;#p;#d=new I;constructor(e,t){this.#h=e,this.#p=t}async readAll(e){const t=await Promise.all(e.map(e=>this.#m(e)));return new m(t.flat())}async#m(e){try{const t=await this.#h.getContent(e);return JSON.parse(t.toString()).map(e=>this.#f(e))}catch(t){const s=t instanceof Error?t.message:String(t);throw new v(e,s)}}#f(e){const t=this.#p.normalizeLocation(e),s=this.#p.isDirectory(t)?`${t}/${E}`:this.#d.assureExtension(t);return s.startsWith("./")?s.substring(2):s.startsWith("/")?s.substring(1):s}}let N=class{#t;#g;constructor(e,t){this.#t=e,this.#g=t}get filename(){return this.#t}get model(){return this.#g}};class R{#E;constructor(e){this.#E=e}get modules(){return this.#E}get(e){return this.#E.find(t=>t.filename===e)}}class x{}class M extends x{elements;constructor(e){super(),this.elements=e}clone(){const e=this.elements.map(e=>e.clone());return new M(e)}toString(){return`[${this.elements.map(e=>e.toString()).join(",")}]`}}class C{}class $ extends C{identifier;constructor(e){super(),this.identifier=e}is(e){return this.identifier===e}}class L extends ${parameters;body;isAsync;constructor(e,t,s,r=!1){super(e),this.parameters=t,this.body=s,this.isAsync=r}clone(){const e=this.parameters.map(e=>e.clone()),t=this.body.clone();return new L(this.identifier,e,t,this.isAsync)}toString(){const e=this.isAsync?"async ":"",t=this.identifier?` ${this.identifier}`:"",s=this.parameters.map(e=>e.toString()),r=this.body.toString();return`${e}function${t}(${s.join(",")})${r}`}}class P extends L{constructor(e,t,s=!1){super(void 0,e,t,s)}clone(){const e=this.parameters.map(e=>e.clone()),t=this.body.clone();return new P(e,t,this.isAsync)}toString(){const e=this.isAsync?"async ":"",t=this.parameters.map(e=>e.toString()),s=this.body.toString();return`${e}(${t.join(", ")})=>${s}`}}class F{binding;initializer;constructor(e,t){this.binding=e,this.initializer=t}clone(){const e=this.binding.clone(),t=this.initializer?.clone();return new F(e,t)}toString(){return`${this.binding.toString()}${void 0!==this.initializer?`=${this.initializer.toString(!1)}`:""}`}}class V extends C{code;constructor(e){super(),this.code=e}clone(){return new V(this.code)}toString(){return this.code}}class D{identifier;visibility;location;constructor(e,t,s){this.identifier=e,this.visibility=t,this.location=s}is(e){return this.identifier===e}}class k extends D{parameters;body;constructor(e,t){super("constructor","public","instance"),this.parameters=e,this.body=t}clone(){const e=this.parameters.map(e=>e.clone()),t=this.body.clone();return new k(e,t)}toString(){const e=this.parameters.map(e=>e.toString()),t=this.body.toString();return`${this.identifier}(${e.join(",")}) ${t}`}}class B extends D{body;constructor(e,t,s,r){super(e,t,s),this.body=r}clone(){const e=this.body.clone();return new B(this.identifier,this.visibility,this.location,e)}toString(){const e="static"===this.location?"static ":"",t="private"===this.visibility?"#":"",s=this.body.toString();return`${e}get ${t}${this.identifier}()${s}`}}class j extends D{parameter;body;constructor(e,t,s,r,i){super(e,t,s),this.parameter=r,this.body=i}clone(){const e=this.parameter.clone(),t=this.body.clone();return new j(this.identifier,this.visibility,this.location,e,t)}toString(){const e="static"===this.location?"static ":"",t="private"===this.visibility?"#":"",s=this.parameter.toString(),r=this.body.toString();return`${e}set ${t}${this.identifier}(${s})${r}`}}class q extends D{parameters;body;isAsync;constructor(e,t,s,r,i,n=!1){super(e,t,s),this.parameters=r,this.body=i,this.isAsync=n}clone(){const e=this.parameters.map(e=>e.clone()),t=this.body.clone();return new q(this.identifier,this.visibility,this.location,e,t,this.isAsync)}toString(){const e="static"===this.location?"static ":"",t="private"===this.visibility?"#":"",s=this.isAsync?"async ":"",r=this.parameters.map(e=>e.toString()),i=this.body.toString();return`${e}${s}${t}${this.identifier}(${r.join(",")})${i}`}}class U extends D{initializer;constructor(e,t,s,r){super(e,t,s),this.initializer=r}clone(){const e=this.initializer?.clone();return new U(this.identifier,this.visibility,this.location,e)}toString(){const e="static"===this.location?"static ":"",t="private"===this.visibility?"#":"",s=void 0!==this.initializer?`=${this.initializer.toString(!0)}`:";";return`${e}${t}${this.identifier}${s}`}}class G extends ${parent;members;constructor(e,t,s){super(e),this.parent=t,this.members=s}get construct(){return this.members.find(e=>e instanceof k)}get fields(){return this.members.filter(e=>e instanceof U)}get getters(){return this.members.filter(e=>e instanceof B)}get setters(){return this.members.filter(e=>e instanceof j)}get methods(){return this.members.filter(e=>e instanceof q)}get publicFields(){return this.fields.filter(e=>"public"===e.visibility)}get publicGetters(){return this.getters.filter(e=>"public"===e.visibility)}get publicSetters(){return this.setters.filter(e=>"public"===e.visibility)}get publicMethods(){return this.methods.filter(e=>"public"===e.visibility)}get readable(){return[...this.publicFields,...this.publicGetters]}get writable(){return[...this.publicFields,...this.publicSetters]}getMember(e){return this.members.find(t=>t.is(e))}hasMember(e){return this.members.some(t=>t.is(e))}getField(e){return this.fields.find(t=>t.is(e))}hasField(e){return this.fields.some(t=>t.is(e))}getGetter(e){return this.getters.find(t=>t.is(e))}hasGetter(e){return this.getters.some(t=>t.is(e))}getSetter(e){return this.setters.find(t=>t.is(e))}hasSetter(e){return this.setters.some(t=>t.is(e))}getMethod(e){return this.methods.find(t=>t.is(e))}hasMethod(e){return this.methods.some(t=>t.is(e))}canRead(e){return this.readable.some(t=>t.is(e))}canWrite(e){return this.writable.some(t=>t.is(e))}clone(){const e=this.members.map(e=>e.clone());return new G(this.identifier,this.parent,e)}toString(){return`class ${this.identifier??""}${void 0!==this.parent?` extends ${this.parent}`:""}{${this.members.map(e=>e.toString()).join("")}}`}}class H extends C{members;from;constructor(e,t){super(),this.members=e,this.from=t}hasMember(e){return this.members.some(t=>t.is(e))}getMember(e){return this.members.find(t=>t.is(e))}clone(){const e=this.members.map(e=>e.clone());return new H(e,this.from)}toString(){const e=this.members.map(e=>e.toString()),t=this.from?` from '${this.from}'`:"";return`export {${e.join(",")}}${t};`}}class K extends C{code;constructor(e){super(),this.code=e}clone(){return new K(this.code)}toString(e=!0){const t=!1===e||this.code.endsWith("}")?"":";";return`${this.code}${t}`}}class _ extends L{clone(){const e=this.parameters.map(e=>e.clone()),t=this.body.clone();return new _(this.identifier,e,t,this.isAsync)}toString(){const e=this.isAsync?"async ":"",t=this.identifier??"",s=this.parameters.map(e=>e.toString()),r=this.body.toString();return`${e}function* ${t}(${s.join(",")})${r}`}}class W extends q{clone(){const e=this.parameters.map(e=>e.clone()),t=this.body.clone();return new W(this.identifier,this.visibility,this.location,e,t,this.isAsync)}toString(){const e=this.isAsync?"async ":"",t=this.identifier,s=this.parameters.map(e=>e.toString()),r=this.body.toString();return`${e}*${t}(${s.join(",")})${r}`}}class z extends x{identifier;isRest;constructor(e,t=!1){super(),this.identifier=e,this.isRest=t}clone(){return new z(this.identifier,this.isRest)}toString(){return`${this.isRest?"...":""}${this.identifier}`}}class Y extends C{members;from;constructor(e,t){super(),this.members=e,this.from=t}hasMember(e){return this.members.some(t=>t.is(e))}getMember(e){return this.members.find(t=>t.is(e))}clone(){const e=this.members.map(e=>e.clone());return new Y(e,this.from)}toString(){if(0===this.members.length)return`import '${this.from}';`;return`import {${this.members.map(e=>e.toString()).join(",")}} from '${this.from}';`}}class X extends ${type;binding;initializer;constructor(e,t,s){super(t.toString()),this.type=e,this.binding=t,this.initializer=s}clone(){const e=this.binding.clone(),t=this.initializer?.clone();return new X(this.type,e,t)}toString(){const e=void 0!==this.initializer?`=${this.initializer.toString(!0)}`:"",t=e.endsWith(";")?"":";";return`${this.type} ${this.identifier}${e}${t}`}}class Q{statements;constructor(e){this.statements=e}get exports(){return this.statements.filter(e=>e instanceof H)}get imports(){return this.statements.filter(e=>e instanceof Y)}get expressions(){return this.statements.filter(e=>e instanceof K)}get declarations(){return this.statements.filter(e=>e instanceof $)}get variables(){return this.statements.filter(e=>e instanceof X)}get functions(){return this.statements.filter(e=>e instanceof L)}get classes(){return this.statements.filter(e=>e instanceof G)}get exported(){const e=[];for(const t of this.exports)for(const s of t.members){const t=this.getDeclaration(s.identifier);void 0!==t&&e.push(t)}return e}get exportedVariables(){return this.exported.filter(e=>e instanceof X)}get exportedFunctions(){return this.exported.filter(e=>e instanceof L)}get exportedClasses(){return this.exported.filter(e=>e instanceof G)}getExport(e){return this.exports.find(t=>t.hasMember(e))}getImport(e){return this.imports.find(t=>t.hasMember(e))}hasDeclaration(e){return this.declarations.some(t=>t.is(e))}getDeclaration(e){return this.declarations.find(t=>t.is(e))}hasVariable(e){return this.variables.some(t=>t.is(e))}getVariable(e){return this.variables.find(t=>t.is(e))}hasFunction(e){return this.functions.some(t=>t.is(e))}getFunction(e){return this.functions.find(t=>t.is(e))}hasClass(e){return this.classes.some(t=>t.is(e))}getClass(e){return this.classes.find(t=>t.is(e))}clone(){const e=this.statements.map(e=>e.clone());return new Q(e)}toString(){return this.statements.map(e=>e.toString(!0)).join("\n")}}class J{identifier;alias;constructor(e,t){this.identifier=e,this.alias=t}is(e){return this.alias===e||void 0===this.alias&&this.identifier===e}clone(){return new J(this.identifier,this.alias)}toString(){return void 0===this.alias?this.identifier:`${this.identifier} as ${this.alias}`}}class Z extends x{elements;constructor(e){super(),this.elements=e}clone(){const e=this.elements.map(e=>e.clone());return new Z(e)}toString(){return`{${this.elements.map(e=>e.toString()).join(",")}}`}}const ee={SINGLE:"//",MULTI_START:"/*",MULTI_END:"*/"},te=Object.values(ee);const se=".",re="(",ie=")",ne="[",oe="]",ae="{",ce="}",ue="_",le={SCOPE:":",SEPARATOR:",",TERMINATOR:";"},he=Object.values(le);function pe(e){return he.includes(e)}const de={UNDEFINED:void 0,NULL:null,STRING:""},me=["null","undefined"],fe=Object.values(de);function ge(e){return fe.includes(e)}const Ee={OPEN:re,CLOSE:ie};function ye(e){return e===Ee.OPEN||e===Ee.CLOSE}const we={IMPORT:"import",EXPORT:"export",AS:"as",FROM:"from",DEFAULT:"default",ASYNC:"async",AWAIT:"await",USING:"using",CONST:"const",LET:"let",VAR:"var",FUNCTION:"function",RETURN:"return",YIELD:"yield",CLASS:"class",CONSTRUCTOR:"constructor",GET:"get",SET:"set",EXTENDS:"extends",STATIC:"static",NEW:"new",THIS:"this",DO:"do",WHILE:"while",FOR:"for",OF:"of",IN:"in",BREAK:"break",CONTINUE:"continue",IF:"if",ELSE:"else",SWITCH:"switch",CASE:"case",THROW:"throw",TRY:"try",CATCH:"catch",FINALLY:"finally"},Se=Object.values(we),be=[we.IMPORT,we.EXPORT,we.VAR,we.LET,we.CONST,we.FUNCTION,we.CLASS,we.ASYNC],Ae=[we.VAR,we.LET,we.CONST,we.FUNCTION,we.CLASS,we.USING],Te=[we.AS,we.ASYNC,we.GET,we.SET,we.OF,we.YIELD,we.FROM,we.STATIC];function Ie(e){return be.includes(e)}function ve(e){return Te.includes(e)}const Oe={OPEN:ne,CLOSE:oe};function Ne(e){return e===Oe.OPEN||e===Oe.CLOSE}const Re=Object.values({SINGLE:"'",DOUBLE:'"',BACKTICK:"`"});function xe(e){return Re.includes(e)}const Me={ADD:"+",ARROW:"=>",ASSIGN:"=",ASSIGN_ADD:"+=",ASSIGN_BITWISE_AND:"&=",ASSIGN_BITWISE_OR:"|=",ASSIGN_BITWISE_XOR:"^=",ASSIGN_DIVIDE:"/=",ASSIGN_EXPONENTIAL:"**=",ASSIGN_LEFT_SHIFT:"<<=",ASSIGN_LOGICAL_AND:"&&=",ASSIGN_LOGICAL_OR:"||=",ASSIGN_REMAINDER:"%=",ASSIGN_MULTIPLY:"*=",ASSIGN_NULLISH:"??=",ASSIGN_RIGHT_SHIFT:">>=",ASSIGN_SUBTRACT:"-=",BITWISE_AND:"&",BITWISE_OR:"|",BITWISE_NOT:"~",BITWISE_XOR:"^",DECREMENT:"--",DIVIDE:"/",EQUAL:"==",EQUAL_STRICT:"===",EXPONENTIAL:"**",GREATER:">",GREATER_EQUAL:">=",INCREMENT:"++",LEFT_SHIFT:"<<",LESS:"<",LESS_EQUAL:"<=",LOGICAL_AND:"&&",LOGICAL_OR:"||",REMAINDER:"%",MULTIPLY:"*",NULLISH:"??",CHAINING:".",OPTIONAL_CHAINING:"?.",SPREAD:"...",NOT:"!",NOT_EQUAL:"!=",NOT_EQUAL_STRICT:"!==",RIGHT_SHIFT:">>",SUBTRACT:"-",TERNARY:"?",$BRIDGE_CHAINING_SPREAD:".."},Ce=Object.values(Me);function $e(e){return Ce.includes(e)}const Le={OPEN:ae,CLOSE:ce};function Pe(e){return e===Le.OPEN||e===Le.CLOSE}const Fe={BOOLEAN:"boolean",COMMENT:"comment",DIVIDER:"divider",GROUP:"group",IDENTIFIER:"identifier",KEYWORD:"keyword",LIST:"list",LITERAL:"literal",NOTHING:"nothing",NUMBER:"number",OPERATOR:"operator",INDICATOR:"indicator",REGEX:"regex",SCOPE:"scope",WHITESPACE:"whitespace",NONE:"none"},Ve={SPACE:" ",TAB:"\t",NEWLINE:"\n",CARRIAGE_RETURN:"\r"},De=Object.values(Ve);function ke(e){return De.includes(e)}class Be{#y;#w;constructor(e){this.#y=e,this.#w=0}get items(){return this.#y}get position(){return this.#w}get size(){return this.#y.length}get eol(){return this.#w>=this.#y.length}get current(){return this.#y[this.#w]}get next(){return this.#y[this.#w+1]}get previous(){return this.#y[this.#w-1]}notAtEnd(){return!1===this.eol}get(e){return this.#y[e]}step(e=1){return this.#w+=e,this.current}stepBack(e=1){return this.#w-=e,this.current}hasNext(){return this.#w+1<this.#y.length}insert(...e){this.#y.splice(this.#w,0,...e)}}class je extends Be{constructor(e){super(e.split(""))}}class qe{#s;#S;#b;#A;constructor(e,t,s,r){this.#s=e,this.#S=t,this.#b=s,this.#A=r}get type(){return this.#s}get value(){return this.#S}get start(){return this.#b}get end(){return this.#A}isType(e){return this.#s===e}hasValue(e){return this.#S===e}toString(){return`${this.#S}`}}class Ue extends Be{}const Ge=["true","false"];const He=["0","1","2","3","4","5","6","7","8","9"],Ke=[...He,"a","b","c","d","e","f","A","B","C","D","E","F"],_e=["0","1"];function We(e){return He.includes(e)}const ze={GENERATOR:"*",PRIVATE:"#"},Ye=Object.values(ze);class Xe{tokenize(e){const t=new je(e),s=[];let r;for(;t.notAtEnd();){const e=this.#T(t,r);if(void 0===e)break;e.isType(Fe.WHITESPACE)||e.isType(Fe.COMMENT)?t.step():(s.push(e),this.#I(e)&&(r=e),t.step())}return new Ue(s)}#I(e){return!1===[Fe.WHITESPACE,Fe.COMMENT].includes(e.type)}#T(e,t){const s=e.current,r=e.position;if(ke(s)){const t=e.position;return new qe(Fe.WHITESPACE,s,r,t)}if(function(e){return te.includes(e)}(s+e.next)){const t=this.#v(e),s=e.position;return new qe(Fe.COMMENT,t,r,s)}if(this.#O(s,t)){const t=this.#N(e),s=e.position;return new qe(Fe.REGEX,t,r,s)}if(this.#R(e,t)){const t=this.#x(e),s=e.position;return new qe(Fe.NUMBER,t,r,s)}if(xe(s)){const t=this.#M(e),s=e.position;return new qe(Fe.LITERAL,t,r,s)}if($e(s)){const t=this.#C(e),s=e.position;return new qe(Fe.OPERATOR,t,r,s)}if(function(e){return Ye.includes(e)}(s)){const t=this.#C(e),s=e.position;return new qe(Fe.INDICATOR,t,r,s)}if(pe(s)){const t=e.position;return new qe(Fe.DIVIDER,s,r,t)}if(ye(s)){const t=e.position;return new qe(Fe.GROUP,s,r,t)}if(Pe(s)){const t=e.position;return new qe(Fe.SCOPE,s,r,t)}if(Ne(s)){const t=e.position;return new qe(Fe.LIST,s,r,t)}if(ge(s))return;const i=this.#$(e),n=e.position;return function(e){return Se.includes(e)}(i)?new qe(Fe.KEYWORD,i,r,n):function(e){return Ge.includes(e)}(i)?new qe(Fe.BOOLEAN,i,r,n):function(e){return me.includes(e)}(i)?new qe(Fe.NOTHING,i,r,n):new qe(Fe.IDENTIFIER,i,r,n)}#v(e){const t=e.current+e.next===ee.MULTI_START,s=t?ee.MULTI_END:Ve.NEWLINE;let r=t?ee.MULTI_START:ee.SINGLE;for(e.step(2);e.notAtEnd();){const i=e.current;if((t?i+e.next:i)===s){e.step(s.length-1);break}r+=i,e.step()}return t?r+ee.MULTI_END:r.trim()}#O(e,t){return e===Me.DIVIDE&&(void 0===t||([Fe.OPERATOR,Fe.DIVIDER,Fe.KEYWORD].includes(t.type)||[Ee.OPEN,Oe.OPEN].includes(t.value)))}#L(e){return ke(e)||e==se||!1===this.#P(e)}#N(e){let t=e.current,s=!1;for(e.step();e.notAtEnd();){const r=e.current,i=e.previous;if(r===Me.DIVIDE&&"\\"!==i)s=!0;else if(!0===s&&this.#L(r)){e.stepBack();break}t+=r,e.step()}return t}#R(e,t){const s=e.current,r=e.next;return!!We(s)||s===Me.SUBTRACT&&(!t?.isType(Fe.NUMBER)&&We(r))}#F(e,t,s){return t?!1===(r=e,Ke.includes(r))&&e!==ue:s?!1===function(e){return _e.includes(e)}(e)&&e!==ue:!1===We(e)&&e!==se&&e!==ue&&"e"!==e;var r}#x(e){let t=e.current;t===Me.SUBTRACT&&(t+=e.step()),e.step();const s="x"===e.current,r="b"===e.current;for((s||r)&&(t+=e.current,e.step());e.notAtEnd();){const i=e.current;if(this.#F(i,s,r)){e.stepBack();break}t+=i,e.step()}return t}#M(e){const t=e.current;let s=t,r=!1;for(e.step();e.notAtEnd();){const i=e.current;if(!1===r){if(i===t){s+=i;break}"\\"===i&&(r=!0)}else r=!1;s+=i,e.step()}return s}#P(e){return!1===(ge(e)||ke(e)||$e(e)||xe(e)||pe(e)||ye(e)||Pe(e)||Ne(e))}#$(e){let t="";for(;e.notAtEnd();){const s=e.current;if(!1===this.#P(s)){e.stepBack();break}t+=s,e.step()}return t}#C(e){let t=e.current;for(e.step();e.notAtEnd();){const s=e.current;if(!1===$e(s)||!1===$e(t+s)){e.stepBack();break}t+=s,e.step()}return t}}class Qe extends Error{constructor(e,t){super(`Expected keyword '${e}' at position ${t}`)}}class Je extends Error{constructor(e,t){super(`Expected token '${e}' at position ${t}`)}}class Ze extends Error{constructor(e,t){super(`Unexpected keyword '${e}' at position ${t}`)}}class et extends Error{constructor(e){super(`The given code does not contain ${e}`)}}class tt extends Error{constructor(e,t){super(`Unexpected token '${e}' at position ${t}`)}}const st=new qe(Fe.NONE,"",-1,0);class rt{#V;constructor(e=[]){this.#V=e}get tokens(){return this.#V}append(...e){this.#V.push(...e)}merge(e){this.append(...e.tokens)}generate(){let e="";for(let t=0;t<this.#V.length;t++){const s=this.#V[t-1]??st,r=this.#V[t],i=this.#V[t+1]??st,n=this.#D(s,r)?" ":"",o=this.#k(r,i)?" ":"";e+=`${n}${r.value}${o}`}return e}#D(e,t){return!(!t.isType(Fe.KEYWORD)||!this.#B(e))||!(!t.isType(Fe.OPERATOR)||!e.isType(Fe.OPERATOR))}#k(e,t){return e.isType(Fe.KEYWORD)&&(t.isType(Fe.KEYWORD)||this.#B(t))}#B(e){return e.isType(Fe.IDENTIFIER)||e.isType(Fe.BOOLEAN)||e.isType(Fe.NOTHING)||e.isType(Fe.NUMBER)}}const it="default";class nt{#j;constructor(e=new Xe){this.#j=e}parse(e){const t=this.#j.tokenize(e),s=this.#q(t);return new Q(s)}parseStatement(e){const t=this.#j.tokenize(e),s=this.#U(t);if(void 0===s)throw new et("a statement");return s}parseImport(e){const t=this.#j.tokenize(e),s=this.#G(t);if(s instanceof Y==!1)throw new et("an import definition");return s}parseExport(e){const t=this.#j.tokenize(e),s=this.#G(t);if(s instanceof H==!1)throw new et("an export definition");return s}parseVariable(e){const t=this.#j.tokenize(e),s=this.#G(t);if(s instanceof X==!1)throw new et("a variable definition");return s}parseFunction(e){const t=this.#j.tokenize(e),s=this.#G(t);if(s instanceof L==!1)throw new et("a function definition");return s}parseClass(e){const t=this.#j.tokenize(e),s=this.#G(t);if(s instanceof G==!1)throw new et("a class definition");return s}#q(e){const t=[];for(;e.notAtEnd();){const s=this.#U(e);void 0!==s&&t.push(s)}return t}#U(e,t=!1){const s=e.current;if(this.#H(s))return this.#K(e);if(s.isType(Fe.IDENTIFIER)){const s=e.next;return s?.hasValue(Me.ARROW)?this.#_(e,t):this.#K(e)}if(s.isType(Fe.KEYWORD)){if(ve(s.value)){const t=e.next,r=void 0!==t&&(t.hasValue(we.FUNCTION)||t.hasValue(Ee.OPEN));if(s.hasValue(we.ASYNC)&&r)return e.step(),this.#U(e,!0);if(void 0===t||this.#W(t))return this.#K(e)}return s.hasValue(we.RETURN)?this.#K(e):!1===Ie(s.value)?this.#K(e):this.#G(e,t)}if(s.isType(Fe.REGEX))return this.#K(e);if(s.hasValue(Ee.OPEN)){const s=this.#z(e,Ee.OPEN,Ee.CLOSE);return s?.hasValue(Me.ARROW)?this.#_(e,t):this.#K(e)}if(s.hasValue(Le.OPEN))return this.#K(e);if(s.hasValue(Oe.OPEN))return this.#K(e);if(s.hasValue(Me.NOT)||s.hasValue(Me.SUBTRACT))return this.#K(e);if(!pe(s.value))throw new tt(s.value,s.start);e.step()}#H(e){return e.isType(Fe.LITERAL)||e.isType(Fe.NUMBER)||e.isType(Fe.BOOLEAN)||e.isType(Fe.NOTHING)||e.hasValue(we.NEW)}#G(e,t=!1){const s=e.current;switch(e.step(),s.value){case we.IMPORT:return this.#Y(e);case we.EXPORT:return this.#X(e);case we.CLASS:return this.#Q(e);case we.FUNCTION:return this.#J(e,t);case we.VAR:return this.#Z(e,"var");case we.LET:return this.#Z(e,"let");case we.CONST:return this.#Z(e,"const");case we.ASYNC:return this.#U(e,!0);default:throw new Ze(s.value,s.start)}}#Y(e){const t=[];let s=e.current;if(s.isType(Fe.LITERAL)){const e=this.#ee(s.value);return new Y(t,e)}if(s.hasValue(Ee.OPEN)){s=e.step();const r=this.#ee(s.value);return e.step(2),new Y(t,r)}if(!1===s.hasValue(Le.OPEN)){const r=s.hasValue(Me.MULTIPLY)?Me.MULTIPLY:it;let i=s.value;s=e.step(),s.hasValue(we.AS)&&(s=e.step(),i=s.value,s=e.step()),t.push(new J(r,i))}if(s.hasValue(le.SEPARATOR)&&(s=e.step()),s.hasValue(Le.OPEN)){const r=this.#te(e);t.push(...r),s=e.current}if(!1===s.hasValue(we.FROM))throw new Qe(we.FROM,s.start);s=e.step();const r=this.#ee(s.value);return e.step(),new Y(t,r)}#X(e){switch(e.current.value){case we.DEFAULT:return e.step(),this.#se(e,!0);case Le.OPEN:return this.#re(e);default:return this.#se(e,!1)}}#se(e,t){let s=e.current,r=0;if(t&&this.#ie(e)){const t=`$_EXPORT_${s.start}_${s.end}`;e.insert(new qe(Fe.KEYWORD,we.CONST,0,0),new qe(Fe.IDENTIFIER,t,0,0),new qe(Fe.OPERATOR,Me.ASSIGN,0,0)),s=e.current}var i;s.hasValue(we.ASYNC)&&(s=e.step(),r++),i=s.value,Ae.includes(i)&&(s=e.step(),r++);const n=this.#P(s)?s.value:"",o=t?it:void 0;let a;s=e.step(),s?.hasValue(we.FROM)&&(s=e.step(),a=this.#ee(s.value)),r>0&&(r++,e.stepBack(r));const c=new J(n,o);return new H([c],a)}#ie(e){const t=e.current;if(this.#H(t))return!0;const s=e.next;return this.#P(t)&&s?.hasValue(Ee.OPEN)}#re(e){const t=this.#te(e);let s,r=e.current;return r?.hasValue(we.FROM)&&(r=e.step(),s=this.#ee(r.value)),e.step(),new H(t,s)}#ee(e){return e.slice(1,-1)}#te(e){const t=[];let s=e.step();for(;e.notAtEnd();){if(s.hasValue(Le.CLOSE)){e.step();break}if(s.hasValue(le.SEPARATOR)){s=e.step();continue}const r=this.#ne(e);t.push(r),s=e.step()}return t}#ne(e){let t=e.current;const s=t.value;let r;return e.next.hasValue(we.AS)&&(t=e.step(2),r=t.value),new J(s,r)}#Z(e,t){const s=this.#oe(e),r=this.#ae(e);return new X(t,s,r)}#oe(e){const t=e.current;return t.hasValue(Oe.OPEN)?this.#ce(e):t.hasValue(Le.OPEN)?this.#ue(e):this.#le(e)}#ce(e){const t=this.#he(e,Oe.CLOSE);return new M(t)}#ue(e){const t=this.#he(e,Le.CLOSE);return new Z(t)}#he(e,t){const s=[];for(e.step();e.notAtEnd();){const r=e.current;if(r.hasValue(t)){e.step();break}if(r.hasValue(le.SEPARATOR)){e.step();continue}const i=this.#oe(e),n=this.#ae(e),o=new F(i,n);s.push(o)}return s}#le(e){let t=e.current,s=!1;t.hasValue(Me.SPREAD)&&(s=!0,t=e.step());const r=t.value;return e.step(),new z(r,s)}#ae(e){const t=e.current;if(!1!==t.hasValue(Me.ASSIGN))return e.step(),this.#U(e,!1);t.hasValue(le.TERMINATOR)&&e.step()}#J(e,t){let s,r=e.current,i=!1;r.hasValue(ze.GENERATOR)&&(i=!0,r=e.step()),this.#P(r)&&(s=r.value,e.step());const n=this.#he(e,Ee.CLOSE);if(r=e.current,!1===r.hasValue(Le.OPEN))throw new Je(Le.OPEN,r.start);const o=this.#pe(e);return i?new _(s,n,o,t):new L(s,n,o,t)}#_(e,t){let s,r,i=e.current;if(i.hasValue(Ee.OPEN))s=this.#he(e,Ee.CLOSE);else{const t=this.#le(e);s=[new F(t,void 0)]}if(i=e.current,!1===i.hasValue(Me.ARROW))throw new Je(Me.ARROW,i.start);if(i=e.step(),i.hasValue(Le.OPEN))r=this.#pe(e);else{const t=this.#K(e);r=new V(t.code)}return new P(s,r,t)}#Q(e){let t,s,r=e.current;if(this.#P(r)&&(t=r.value,r=e.step()),r.hasValue(we.EXTENDS)&&(r=e.step(),s=r.value,r=e.step()),!1===r.hasValue(Le.OPEN))throw new Je(Le.OPEN,r.start);const i=this.#de(e);return new G(t,s,i)}#de(e){let t=e.step();const s=[];for(;e.notAtEnd();){if(t.hasValue(Le.CLOSE)){e.step();break}const r=this.#me(e);s.push(r),t=e.current}return s}#me(e){let t=e.current,s="public",r="instance",i=!1;for(;e.notAtEnd();){if(t.hasValue(ze.PRIVATE))s="private";else if(t.hasValue(we.STATIC))r="static";else{if(!t.hasValue(we.ASYNC)){if(t.hasValue(we.CONSTRUCTOR))return this.#fe(e);if(t.hasValue(we.GET))return this.#ge(e,r);if(t.hasValue(we.SET))return this.#Ee(e,r);if(t.hasValue(ze.GENERATOR))return e.step(),this.#ye(e,s,r,i,!0);break}i=!0}t=e.step()}return e.next.hasValue(Ee.OPEN)?this.#ye(e,s,r,i,!1):this.#we(e,s,r)}#fe(e){e.step();const t=this.#he(e,Ee.CLOSE),s=e.current;if(!1===s.hasValue(Le.OPEN))throw new Je(Le.OPEN,s.start);const r=this.#pe(e);return new k(t,r)}#ge(e,t){let s="public",r=e.step();r.hasValue(ze.PRIVATE)&&(s="private",r=e.step());const i=r.value;e.step();if(0!==this.#he(e,Ee.CLOSE).length)throw new et("an empty parameter list");if(r=e.current,!1===r.hasValue(Le.OPEN))throw new Je(Le.OPEN,r.start);const n=this.#pe(e);return new B(i,s,t,n)}#Ee(e,t){let s="public",r=e.step();r.hasValue(ze.PRIVATE)&&(s="private",r=e.step());const i=r.value;e.step();const n=this.#he(e,Ee.CLOSE);if(r=e.current,1!==n.length)throw new et("exactly one setter parameter");if(!1===r.hasValue(Le.OPEN))throw new Je(Le.OPEN,r.start);const o=this.#pe(e);return new j(i,s,t,n[0],o)}#ye(e,t,s,r,i){let n=e.current;const o=n.value;e.step();const a=this.#he(e,Ee.CLOSE);if(n=e.current,!1===n.hasValue(Le.OPEN))throw new Je(Le.OPEN,n.start);const c=this.#pe(e);return i?new W(o,t,s,a,c,r):new q(o,t,s,a,c,r)}#we(e,t,s){const r=e.current.value;e.step();const i=this.#ae(e);return new U(r,t,s,i)}#pe(e){const t=this.#Se(e,Le.OPEN,Le.CLOSE).generate();return new V(t)}#K(e){const t=new rt;let s=e.current;for(;e.notAtEnd();){if(s.hasValue(Oe.OPEN)){const r=this.#Se(e,Oe.OPEN,Oe.CLOSE);t.merge(r),s=e.current}else if(s.hasValue(Ee.OPEN)){const r=this.#Se(e,Ee.OPEN,Ee.CLOSE);t.merge(r),s=e.current}else if(s.hasValue(Le.OPEN)){const r=this.#Se(e,Le.OPEN,Le.CLOSE);t.merge(r),s=e.current}else t.append(s),s=e.step();if(void 0===s)break;if(this.#W(s)){s.hasValue(le.TERMINATOR)&&e.step();break}}const r=t.generate();return new K(r)}#Se(e,t,s){let r=e.current;const i=new rt([r]);for(r=e.step();e.notAtEnd();){if(r.hasValue(t)){const n=this.#Se(e,t,s);i.merge(n),r=e.current;continue}if(r.hasValue(s))return e.step(),i.append(r),i;i.append(r),r=e.step()}return i}#z(e,t,s){const r=e.position;this.#Se(e,t,s);const i=e.current,n=e.position;return e.stepBack(n-r),i}#W(e){return[le.TERMINATOR,le.SEPARATOR].includes(e.value)||[Oe.CLOSE,Ee.CLOSE,Le.CLOSE].includes(e.value)||Ie(e.value)}#P(e){return e.isType(Fe.IDENTIFIER)||e.isType(Fe.KEYWORD)&&ve(e.value)}}let ot=class extends Error{constructor(e,t){super(`Failed to load module file '${e}' because of: ${t}`)}};class at{#p;#d=new I;constructor(e){this.#p=e}rewrite(e,t){e.imports.forEach(e=>this.#be(e,t)),e.exports.forEach(e=>this.#Ae(e,t))}#be(e,t){const s=this.#p.normalizeLocation(e.from);!1!==this.#d.isApplicationModule(s)&&(e.from=this.#Te(t,s))}#Ae(e,t){if(void 0===e.from)return;const s=this.#p.normalizeLocation(e.from);!1!==this.#d.isApplicationModule(s)&&(e.from=this.#Te(t,s))}#Te(e,t){const s=this.#d.extractPath(e),r=this.#d.makePathAbsolute(t,s,"");return this.#p.isDirectory(r)?`${t}/${E}`:this.#d.assureExtension(t)}}let ct=class{#p;#Ie;#ve;constructor(e,t=new nt){this.#p=e,this.#Ie=t,this.#ve=new at(e)}async readAll(e){const t=await Promise.all(e.map(e=>this.read(e)));return new R(t)}async read(e){try{const t=this.#p.getRelativeLocation(e),s=await this.#Oe(e),r=this.#Ie.parse(s);return this.#ve.rewrite(r,t),new N(t,r)}catch(t){const s=t instanceof Error?t.message:String(t);throw new ot(e,s)}}async#Oe(e){return(await this.#p.getContent(e)).toString()}},ut=class{#Ne;#E=new Map;#Re=new Map;#xe=new Map;constructor(e){this.#Ne=e}get name(){return this.#Ne}get modules(){return[...this.#E.values()]}get classes(){return[...this.#Re.values()]}get procedures(){return[...this.#xe.values()]}hasModule(e){return this.#E.has(e)}getModule(e){return this.#E.get(e)}getSegmentedModules(){return this.modules.filter(e=>e.segmented)}setModule(e){this.#E.set(e.filename,e)}hasProcedure(e){return this.#xe.has(e)}getProcedure(e){return this.#xe.get(e)}setProcedure(e){this.#xe.set(e.fqn,e)}setClass(e){this.#Re.set(e.fqn,e)}};class lt{#l;#Me;#Ce;constructor(e,t,s){this.#l=e,this.#Me=t,this.#Ce=s}get id(){return this.#l}get importKey(){return this.#Me}get fqn(){return this.#Ce}}class ht extends lt{#g;constructor(e,t,s,r){super(e,t,s),this.#g=r}get model(){return this.#g}}class pt extends lt{#$e;#Le;#g;constructor(e,t,s,r,i,n){super(e,t,s),this.#$e=r,this.#Le=i,this.#g=n}get access(){return this.#$e}get version(){return this.#Le}get model(){return this.#g}}class dt{#t;#e;#Pe;#Fe=[];#Ve;constructor(e,t,s,r){this.#t=e,this.#e=t,this.#Pe=s,this.#Ve=r}get filename(){return this.#t}get location(){return this.#e}get imports(){return this.#Pe}get members(){return this.#Fe}get segmented(){return this.#Ve}hasClasses(){return this.#Fe.some(e=>e instanceof ht)}getClasses(){return this.#Fe.filter(e=>e instanceof ht)}hasImplementations(){return this.#Fe.some(e=>e instanceof pt)}getImplementations(){return this.#Fe.filter(e=>e instanceof pt)}addMember(e){this.#Fe.push(e)}addImports(e){const t=Object.keys(e);for(const s of t){const t=s.trim();this.#Pe[t]=e[s]}}}class mt{#Ce;#De=[];constructor(e,t=[]){this.#Ce=e,this.#De=t}get fqn(){return this.#Ce}get implementations(){return this.#De}addImplementation(e){this.#De.push(e)}}class ft{#ke;constructor(e){this.#ke=e}get segments(){return this.#ke}getSegment(e){return this.#ke.find(t=>t.name===e)}isSegmentedModule(e){return this.#ke.some(t=>t.hasModule(e))}getSegments(e){return this.#ke.filter(t=>t.hasModule(e))}}class gt extends Error{constructor(e,t){super(`Function '${t}' from file '${e}' is not async`)}}class Et extends Error{constructor(e){super(`Segment filename '${e}' is invalid`)}}class yt extends Error{constructor(e,t){super(`Failed to load segment file '${e}' because of: ${t}`)}}class wt extends Error{constructor(e,t){super(`The export '${t}' from file '${e}' is not a function or a class.`)}}class St extends Error{constructor(e,t){super(`Module '${e}' does not export '${t}'`)}}class bt extends Error{constructor(e){super(`Segmented module not found '${e}'`)}}class At{#a;#d=new I;constructor(e){this.#a=e}locate(e,t){const s=[];return{trace:s,model:this.#Be(e,t,s)}}#Be(e,t,s){s.push({filename:e,importKey:t});const r=this.#je(e);return this.#qe(r,t)?this.#Ue(r,t,s):this.#Ge(r,t)}#je(e){const t=this.#a.get(e);if(void 0===t)throw new bt(e);return t}#qe(e,t){const s=e.model.getImport(t),r=e.model.getExport(t);return void 0!==s||void 0!==r?.from}#Ue(e,t,s){const r=this.#He(e,t)??this.#Ke(e,t),i=r?.from,n=r?.name,o=this.#d.extractPath(e.filename),a=i,c=this.#d.makePathAbsolute(a,o,"");return this.#Be(c,n,s)}#Ge(e,t){const s=this.#Ke(e,t);if(void 0===s)throw new St(e.filename,t);const r=e.model.getDeclaration(s.name);if(void 0===r)throw new St(e.filename,s.name);return r}#Ke(e,t){const s=e.model.getExport(t),r=s?.getMember(t);if(void 0!==r)return{from:s?.from,name:r.identifier}}#He(e,t){const s=e.model.getImport(t),r=s?.getMember(t);if(void 0!==s&&void 0!==r)return{from:s.from,name:r.identifier}}}class Tt{#_e;#p;#We;#d=new I;constructor(e,t,s){this.#_e=e,this.#p=t,this.#We=new At(s)}async readAll(e){const t=await Promise.all(e.map(e=>this.#ze(e)));return new ft(t)}async#ze(e){const t=await this.#Ye(e),s=this.#Xe(e),r=new ut(s);return this.#Qe(r,t),this.#Je(r),r}#Xe(e){const t=e.split("/").pop();if(void 0===t||""===t)throw new Et(e);return t.replace(y,"")}async#Ye(e){try{const t=await this.#_e.getContent(e);return JSON.parse(t.toString())}catch(t){const s=t instanceof Error?t.message:String(t);throw new yt(e,s)}}#Qe(e,t){for(const[s,r]of Object.entries(t))this.#Ze(e,s,r,!0)}#Ze(e,t,s,r){const i=this.#et(t),n=this.#d.extractPath(i),o=e.hasModule(i)?e.getModule(i):new dt(i,n,{},r);o.addImports(s),e.setModule(o)}#et(e){const t=e.trim(),s=this.#p.normalizeLocation(t),r=this.#p.isDirectory(s)?`${s}/${E}`:this.#d.assureExtension(s);return r.startsWith("./")?r.substring(2):r.startsWith("/")?r.substring(1):r}#Je(e){const t=new S;for(const s of e.modules)for(const r in s.imports)this.#tt(e,s,r,t)}#tt(e,t,s,r){const{model:i,trace:n}=this.#We.locate(t.filename,s),o=this.#st(t,i,s,r);this.#rt(e,t,i,o),n.shift();for(const t of n){const s={[t.importKey]:{access:o.access}};this.#Ze(e,t.filename,s,!1)}}#st(e,t,s,r){const i=e.imports[s],n=r.next(),o=i.as??t.identifier;return{id:n,importKey:s,name:o,access:i.access??f,version:i.version??g,fqn:this.#it(e,o,s)}}#it(e,t,s){return this.#nt(e)?t:this.#ot(e)&&this.#at(s)?e.location:`${e.location}/${t}`}#nt(e){return""===e.location}#ot(e){return e.filename.endsWith(E)}#at(e){return e===w}#rt(e,t,s,r){if(s instanceof G)return this.#ct(e,t,s,r);if(s instanceof L)return this.#ut(e,t,s,r);throw new wt(t.filename,r.importKey)}#ct(e,t,s,r){const i=new ht(r.id,r.importKey,r.fqn,s);t.addMember(i),e.setClass(i)}#ut(e,t,s,r){if(!1===s.isAsync)throw new gt(t.filename,r.name);const i=new pt(r.id,r.importKey,r.fqn,r.access,r.version,s);this.#lt(e,t,i)}#lt(e,t,s){const r=e.hasProcedure(s.fqn)?e.getProcedure(s.fqn):new mt(s.fqn);r.addImplementation(s),t.addMember(s),e.setProcedure(r)}}class It{#ht;constructor(e){this.#ht=e}async read(e,t,s){const r=this.#ht.source,i=this.#ht.resource,n=this.#ht.segment,o=new ct(r),a=await o.readAll(e),c=new O(i,r),u=await c.readAll(t),l=new Tt(n,r,a),h=await l.readAll(s);return new d(a,u,h)}}class vt extends Error{constructor(e){super(`Could not find the rewrite item: ${e}`)}}class Ot{#pt;#c;#u;#dt;#d=new I;constructor(e,t,s,r){this.#pt=e,this.#c=t,this.#u=s,this.#dt=r}generate(){const e=this.#pt.model.clone();return this.#mt(e,e.imports),this.#mt(e,e.exports),e.toString()}#mt(e,t){for(const s of t){const t=this.#ft(s);if(0===t.length)continue;const r=e.statements.indexOf(s);if(-1===r)throw new vt(s.toString());e.statements.splice(r,1,...t)}}#ft(e){if(this.#gt(e))return[];const t=this.#Et(e);return this.#c.isResourceModule(t)?this.#yt(e,t):this.#wt(e,t)}#gt(e){return void 0===e.from||this.#St(e)}#St(e){return!1===this.#d.isApplicationModule(e.from)}#Et(e){const t=e.from,s=this.#d.extractPath(this.#pt.filename);return this.#d.makePathAbsolute(t,s,"")}#yt(e,t){const s=e.clone();return s.from=this.#bt(t),[s]}#wt(e,t){if(0===e.members.length)return[this.#At(t,e,[])];const{segmentKeys:s,remoteKeys:r,commonKeys:i}=this.#Tt(t,e),n=[];return s.length>0&&n.push(this.#It(t,e,s)),r.length>0&&n.push(this.#vt(t,e,r)),i.length>0&&n.push(this.#At(t,e,i)),n}#Tt(e,t){const s=this.#Ot(e,this.#dt),r=this.#Nt(e,s),i=this.#Rt(t,s),n=this.#Rt(t,r);return{segmentKeys:i,remoteKeys:n,commonKeys:this.#xt(t,[...i,...n])}}#Ot(e,t){if(void 0===t)return[];const s=t.getModule(e);return void 0===s?[]:Object.keys(s.imports)}#Nt(e,t){const s=this.#u.getSegments(e).filter(e=>e!==this.#dt).flatMap(t=>this.#Ot(e,t));return[...new Set(s)].filter(e=>!1===t.includes(e))}#Rt(e,t){return e.members.filter(e=>t.includes(e.identifier)).map(e=>e.identifier)}#xt(e,t){return e.members.filter(e=>!1===t.includes(e.identifier)).map(e=>e.identifier)}#It(e,t,s){const r=t.clone();return r.from=this.#bt(e,this.#dt?.name),r.members=this.#Mt(t.members,s),r}#vt(e,t,s){const r=t.clone();return r.from=this.#bt(e,"remote"),r.members=this.#Mt(t.members,s),r}#At(e,t,s){const r=t.clone();return r.from=this.#bt(e),r.members=this.#Mt(t.members,s),r}#Mt(e,t){const s=[];for(const r of t){const t=e.find(e=>e.identifier===r);void 0!==t&&s.push(t)}return s}#bt(e,t){const s=this.#d.extractPath(this.#pt.filename),r=this.#d.makePathRelative(e,s);return void 0!==t?this.#d.addSubExtension(r,t):r}}const Nt={PRIVATE:"private",PROTECTED:"protected",PUBLIC:"public"};class Rt extends Error{constructor(e="Invalid request"){super(e)}}class xt extends Error{constructor(e="Forbidden"){super(e)}}class Mt extends Error{constructor(e="Not found"){super(e)}}class Ct extends Error{constructor(e="Not implemented"){super(e)}}class $t extends Error{constructor(e="Payment required"){super(e)}}class Lt extends Error{constructor(e="Server error"){super(e)}}class Pt extends Error{constructor(e="I'm a teapot"){super(e)}}class Ft extends Error{constructor(e="Unauthorized"){super(e)}}class Vt{#l;#Re=new Map;#xe=new Map;constructor(e){this.#l=e}get id(){return this.#l}addClass(e){return this.#Re.set(e.fqn,e),this}hasClass(e){return void 0!==this.getClass(e)}getClass(e){return this.#Re.get(e)}getClassByImplementation(e){return this.getClasses().find(t=>t.implementation===e)}getClasses(){return[...this.#Re.values()]}addProcedure(e){return this.#xe.set(e.fqn,e),this}hasProcedure(e){return this.#xe.has(e)}getProcedure(e){return this.#xe.get(e)}getExposedProcedures(){return[...this.#xe.values()].filter(e=>e.public||e.protected)}}class Dt{static get DEFAULT(){return new Dt(0,0,0)}#Ct;#$t;#Lt;constructor(e=0,t=0,s=0){this.#Ct=e,this.#$t=t,this.#Lt=s}get major(){return this.#Ct}get minor(){return this.#$t}get patch(){return this.#Lt}equals(e){return this.#Ct===e.major&&this.#$t===e.minor&&this.#Lt===e.patch}greater(e){return this.#Ct!==e.major?this.#Ct>e.major:this.#$t!==e.minor?this.#$t>e.minor:this.#Lt!==e.patch&&this.#Lt>e.patch}less(e){return this.#Ct!==e.major?this.#Ct<e.major:this.#$t!==e.minor?this.#$t<e.minor:this.#Lt!==e.patch&&this.#Lt<e.patch}toString(){return`${this.#Ct}.${this.#$t}.${this.#Lt}`}}class kt{#Ne;#Pt;constructor(e,t=!1){this.#Ne=e,this.#Pt=t}get name(){return this.#Ne}get isOptional(){return this.#Pt}}class Bt extends kt{}class jt extends kt{#Ft;constructor(e,t,s){super(t??"(anonymous)",s),this.#Ft=e}get variables(){return this.#Ft}}class qt extends jt{}class Ut extends jt{}class Gt{#Ce;#Le;#Vt;#Dt=new Map;#kt;constructor(e,t,s,r,i){this.#Ce=e,this.#Le=t,this.#Vt=s,this.#Dt=r,this.#kt=i}get fqn(){return this.#Ce}get version(){return this.#Le}get args(){return this.#Vt}get headers(){return this.#Dt}get mode(){return this.#kt}clearArguments(){this.#Vt.clear()}setArgument(e,t){this.#Vt.set(e,t)}getArgument(e){return this.#Vt.get(e)}hasArgument(e){return this.#Vt.has(e)}removeArgument(e){this.#Vt.delete(e)}clearHeaders(){this.#Dt.clear()}setHeader(e,t){this.#Dt.set(e.toLowerCase(),t)}getHeader(e){return this.#Dt.get(e.toLowerCase())}hasHeader(e){return this.#Dt.has(e.toLowerCase())}removeHeader(e){this.#Dt.delete(e.toLowerCase())}}class Ht{#Bt;#jt;#Dt;constructor(e,t=void 0,s=new Map){this.#Bt=e,this.#jt=t,this.#Dt=s}get status(){return this.#Bt}get result(){return this.#jt}set result(e){this.#jt=e}get headers(){return this.#Dt}clearHeaders(){this.#Dt.clear()}setHeader(e,t){this.#Dt.set(e.toLowerCase(),t)}getHeader(e){return this.#Dt.get(e.toLowerCase())}hasHeader(e){return this.#Dt.has(e.toLowerCase())}removeHeader(e){this.#Dt.delete(e.toLowerCase())}}class Kt{#pt;#ke;constructor(e,t){this.#pt=e,this.#ke=t}generate(){const e=[],t=this.#qt();for(const s of t){const t=s.access===Nt.PRIVATE?this.#Ut(s):this.#Gt(s);e.push(...t)}return new Q(e).toString()}#qt(){const e=this.#ke.map(e=>e.getModule(this.#pt.filename)).flatMap(e=>e.getImplementations()),t=new Map;for(const s of e){const e=`${s.fqn}:${s.version.toString()}`;t.set(e,s)}return[...t.values()]}#Ut(e){const t=`throw new ProcedureNotAccessible('${e.fqn}','${e.version}');`;return[this.#Ht(e),this.#Kt(e,t)]}#Gt(e){const t=`return __run('${e.fqn}','${e.version}',{${this.#_t(e.model.parameters)}},this);`;return[this.#Ht(e),this.#Kt(e,t)]}#Ht(e){const t=e.model.identifier,s=e.importKey===w?w:void 0,r=new J(t,s);return new H([r],void 0)}#Kt(e,t){const s=e.model.identifier,r=e.model.parameters,i=new V(`{${t}}`),n=e.model.isAsync;return new L(s,r,i,n)}#_t(e){return this.#Wt(e).join(",")}#Wt(e){const t=[];for(const s of e)if(s.binding instanceof z){const e=this.#zt(s.binding);t.push(e)}else if(s.binding instanceof M){const e=this.#Wt(s.binding.elements);t.push(...e)}else if(s.binding instanceof Z){const e=this.#Wt(s.binding.elements);t.push(...e)}return t}#zt(e){return`'${e.identifier}':${e.toString()}`}}class _t{#Yt;#Xt;#Qt;#dt;#Jt;constructor(e,t,s,r,i){this.#Yt=e,this.#Xt=t,this.#Qt=s,this.#dt=r,this.#Jt=i}get source(){return this.#Yt}get target(){return this.#Xt}get resource(){return this.#Qt}get segment(){return this.#dt}get sourceIgnores(){return this.#Jt}}class Wt{#Zt;constructor(e=!0){this.#Zt=e}validate(e,t){const s=[];this.#es("",e,t,s);return{valid:0===s.length,errors:s}}#es(e,t,s,r){this.#Zt&&this.#ts(e,t,s,r),this.#ss(e,t,s,r)}#ts(e,t,s,r){const i=Object.keys(t),n=Object.keys(s);for(const t of i)if(!1===n.includes(t)){const s=this.#rs(e,t);r.push(`Unknown field '${s}'`)}}#ss(e,t,s,r){const i=Object.keys(s);for(const n of i){const i=this.#rs(e,n),o=s[n],a=t[n];this.#is(i,a,o,r)}}#is(e,t,s,r){if(void 0!==t)switch(s.type){case"string":return this.#ns(e,t,s,r);case"integer":return this.#os(e,t,s,r);case"real":return this.#as(e,t,s,r);case"boolean":return this.#cs(e,t,s,r);case"url":return this.#us(e,t,s,r);case"group":return this.#ls(e,t,s,r);case"list":return this.#hs(e,t,s,r)}else!0===s.required&&r.push(`Field '${e}' is required`)}#ns(e,t,s,r){"string"!=typeof t&&r.push(`Field '${e}' is not a string`)}#os(e,t,s,r){"number"==typeof t&&!1!==Number.isInteger(t)||r.push(`Field '${e}' is not an integer`)}#as(e,t,s,r){"number"!=typeof t&&r.push(`Field '${e}' is not a real number`)}#cs(e,t,s,r){"boolean"!=typeof t&&r.push(`Field '${e}' is not a boolean`)}#us(e,t,s,r){"string"==typeof t&&!1!==t.startsWith("http")||r.push(`Field '${e}' is not a valid URL`)}#ls(e,t,s,r){"object"==typeof t?this.#es(e,t,s.fields,r):r.push(`Field '${e}' is not an object`)}#hs(e,t,s,r){if(!Array.isArray(t))return void r.push(`Field '${e}' is not a list`);const i=t;for(const t in i){const n=this.#rs(e,t),o=i[t];this.#is(n,o,s.items,r)}}#rs(e,t){return""===e?t:`${e}.${t}`}}class zt{#ps;constructor(e){this.#ps=e}async configure(e){const t=this.#ps.getAbsoluteLocation(e);n.config({path:t})}}class Yt extends Error{constructor(e){super(`Runtime configuration is invalid:\n${e.errors.join("\n")}`)}}const Xt="./jitar.json",Qt="./src",Jt="./dist",Zt="./segments",es="./resources",ts={ignore:[]},ss=[],rs={source:{type:"string",required:!1},target:{type:"string",required:!1},segments:{type:"string",required:!1},resources:{type:"string",required:!1},build:{type:"group",fields:{ignore:{type:"list",items:{type:"string"},required:!1}},required:!1}};let is=class{#ds;#ms;constructor(e,t){this.#ds=e,this.#ms=t}async build(e=Xt){const t=await this.#ds.read(e),s=this.#ms.validate(t,rs);if(!1===s.valid)throw new Yt(s);return t.source??=Qt,t.target??=Jt,t.segments??=Zt,t.resources??=es,t.build??=ts,t.build.ignore??=ss,t.meta={root:this.#ds.getRootLocation(),configFile:e},t}};const ns="\n => ";class os extends Error{constructor(e){const t=e.errors.join(ns);super(`Invalid server configuration:${ns}${t}`)}}const as={indexFilename:{type:"string",required:!1},serveIndexOnNotFound:{type:"boolean",required:!1},assetRoot:{type:"string",required:!1},assets:{type:"list",required:!1,items:{type:"string"}}},cs={gateway:{type:"url",required:!1},segments:{type:"list",required:!0,items:{type:"string"}},trustKey:{type:"string",required:!1},reportInterval:{type:"integer",required:!1}},us={url:{type:"url",required:!0},setUp:{type:"list",required:!1,items:{type:"string"}},tearDown:{type:"list",required:!1,items:{type:"string"}},middleware:{type:"list",required:!1,items:{type:"string"}},healthChecks:{type:"list",required:!1,items:{type:"string"}},gateway:{type:"group",required:!1,fields:{monitorInterval:{type:"integer",required:!1},trustKey:{type:"string",required:!1}}},proxy:{type:"group",required:!1,fields:{gateway:{type:"url",required:!0},repository:{type:"url",required:!0}}},repository:{type:"group",required:!1,fields:as},standalone:{type:"group",required:!1,fields:{...as,segments:cs.segments}},worker:{type:"group",required:!1,fields:cs},remoteWorker:{type:"group",required:!1,fields:{unavailableThreshold:{type:"integer",required:!1},stoppedThreshold:{type:"integer",required:!1}}}};class ls{#ds;#ms;constructor(e,t){this.#ds=e,this.#ms=t}async build(e){const t=await this.#ds.read(e),s=this.#ms.validate(t,us);if(!1===s.valid)throw new os(s);return t.meta={root:this.#ds.getRootLocation(),configFile:e},t}}class hs extends Error{constructor(e){super(`${e} is not a valid configuration file.`)}}const ps=/\${([^}]*)}/g;class ds{#ps;constructor(e){this.#ps=e}getRootLocation(){return this.#ps.getAbsoluteLocation(".")}async read(e){if(!1===await this.#ps.exists(e))return{};const t=await this.#ps.read(e);if(!1===t.type.includes("json"))throw new hs(e);const s=t.content.toString(),r=this.#fs(s);return this.#gs(r)}#fs(e){return e.replace(ps,(e,t)=>process.env[t]??"null")}#gs(e){return JSON.parse(e)}}class ms{#Es;#ys;#ws;constructor(e="./"){const t=new p(e),s=new ds(t),r=new Wt;this.#Es=new zt(t),this.#ys=new is(s,r),this.#ws=new ls(s,r)}configureEnvironment(e=".env"){return this.#Es.configure(e)}getRuntimeConfiguration(e){return this.#ys.build(e)}getServerConfiguration(e){return this.#ws.build(e)}}export{qt as A,Rt as B,ms as C,c as D,G as E,xt as F,Gt as G,Nt as H,p as L,Mt as N,Ut as O,nt as P,Ht as R,Lt as S,Pt as T,Ft as U,Dt as V,z as a,K as b,X as c,Q as d,U as e,k as f,B as g,j as h,q as i,Bt as j,Ct as k,$t as l,Vt as m,I as n,Ot as o,Kt as p,Y as q,J as r,H as s,M as t,Z as u,_t as v,It as w,o as x,Wt as y,u as z};