jitar 0.10.0 → 0.10.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/Response-DEjrdAZ9.js +1 -0
- package/dist/cli.js +1 -1
- package/dist/client.js +1 -1
- package/dist/lib.js +1 -1
- package/dist/templates/{jitar-only → backend}/package.json +6 -3
- package/dist/templates/react/package.json +12 -11
- package/dist/templates/react/services/standalone.json +1 -1
- package/dist/templates/react/src/webui/App.tsx +2 -2
- package/dist/templates/vue/package.json +10 -9
- package/dist/templates/vue/services/standalone.json +1 -1
- package/dist/templates/vue/src/webui/App.vue +1 -1
- package/package.json +12 -6
- package/dist/Response-B4oYW-ep.js +0 -1
- /package/dist/templates/{jitar-only → backend}/_gitignore +0 -0
- /package/dist/templates/{jitar-only → backend}/jitar.json +0 -0
- /package/dist/templates/{jitar-only → backend}/requests.http +0 -0
- /package/dist/templates/{jitar-only → backend}/segments/hello.json +0 -0
- /package/dist/templates/{jitar-only → backend}/segments/hi.json +0 -0
- /package/dist/templates/{jitar-only → backend}/services/gateway.json +0 -0
- /package/dist/templates/{jitar-only → backend}/services/hello.json +0 -0
- /package/dist/templates/{jitar-only → backend}/services/hi.json +0 -0
- /package/dist/templates/{jitar-only → backend}/services/standalone.json +0 -0
- /package/dist/templates/{jitar-only → backend}/src/greetings/sayBoth.ts +0 -0
- /package/dist/templates/{jitar-only → backend}/src/greetings/sayHello.ts +0 -0
- /package/dist/templates/{jitar-only → backend}/src/greetings/sayHi.ts +0 -0
- /package/dist/templates/{jitar-only → backend}/tsconfig.json +0 -0
package/dist/cli.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import path from"path";import fs from"fs/promises";import{fileURLToPath}from"url";import fsp from"node:fs/promises";import fs$1 from"node:fs";import{glob}from"glob";import mime from"mime-types";import dotenv from"dotenv";import{B as BadRequest,N as NotFound,F as Forbidden,a as NamedParameter,A as ArrayParameter,O as ObjectParameter,b as NotImplemented,P as PaymentRequired,T as Teapot,U as Unauthorized,S as ServerError,V as Version,c as Segment$1,R as Response,d as AccessLevels,e as Request}from"./Response-B4oYW-ep.js";import crypto from"crypto";import express from"express";class MissingArgument extends Error{constructor(name){super(`Missing argument '${name}'`)}}class ArgumentProcessor{#command;#args;constructor(args){this.#command=args[2],this.#args=this.#parseArguments(args)}getCommand(){return this.#command}containsKey(key){return this.#args.has(key)}getRequiredArgument(name){const value=this.#args.get(name);if(void 0===value)throw new MissingArgument(name);return value}getOptionalArgument(name,defaultValue){return this.#args.get(name)??defaultValue}#parseArguments(args){const commandArgs=args.slice(3),map=new Map;return commandArgs.forEach(arg=>{const[key,value]=arg.split("=");map.set(key.trim(),value?.trim())}),map}}class ShowHelp{name="help";description="Shows all available commands (this message).";options=[{key:"[command]",required:!1,description:"The command name to show more details for"}];#commands;constructor(commands){this.#commands=commands}async execute(args){for(const command of this.#commands.values())if(args.containsKey(command.name))return this.#showCommandDetails(command);this.#listCommands()}#listCommands(){console.log("\njitar <command>\n"),console.log("Available commands:");const commands=[...this.#commands.values()],commandNames=commands.map(command=>command.name),paddingSize=this.#determinePaddingSize(commandNames);for(const command of commands){const name=command.name.padEnd(paddingSize," "),description=command.description;console.log(` ${name} ${description}`)}console.log("\nFor the options per command, use:"),console.log(" jitar help [command]\n")}#showCommandDetails(command){console.log(`\n${command.description}\n`);const options=command.options;if(0===options.length)return void console.log("No options available.\n");console.log("Options:");const optionKeys=options.map(option=>option.key),paddingSize=this.#determinePaddingSize(optionKeys);for(const option of options){const key=option.key.padEnd(paddingSize," "),description=option.description,defaultValue=option.defaultValue?`default: ${option.defaultValue}`:"no default value",required=option.required?"required":`optional - ${defaultValue}`;console.log(` ${key} ${description} (${required})`)}console.log()}#determinePaddingSize(labels){return labels.reduce((maxSize,label)=>label.length>maxSize?label.length:maxSize,0)+3}}class ShowAbout{name="about";description="Shows information about Jitar.";options=[];async execute(args){console.log("\nJitar is a JavaScript Distributed Runtime created and maintained by Masking Technology.\n\nMore information can be found at:\n- https://jitar.dev\n- https://masking.tech\n")}}class ShowVersion{name="version";description="Shows the installed version of Jitar.";options=[];async execute(args){console.log("v0.9.4")}}class InvalidName extends Error{constructor(name,type){super(`Invalid ${type} name: ${name}`)}}class UnknownTemplate extends Error{constructor(templateName){super(`Unknown template: ${templateName}`)}}const NAME_REGEX=/^[a-zA-Z.\-_]+$/,RENAME_FILES={_gitignore:".gitignore"};class InitManager{#templateRootPath;#projectRootPath;constructor(rootPath="./"){this.#templateRootPath=this.#createTemplateRootPath(),this.#projectRootPath=this.#createProjectRootPath(rootPath)}async init(projectName,templateName){this.#validateName(projectName,"project"),this.#validateName(templateName,"template");if(!1===await this.#verifyTemplateName(templateName))throw new UnknownTemplate(templateName);await this.#copyTemplate(projectName,templateName),this.#renameFiles(projectName)}#createTemplateRootPath(){const runtimePath=path.dirname(fileURLToPath(import.meta.url));return path.join(runtimePath,"templates")}#createProjectRootPath(rootPath){return path.resolve(rootPath)}#validateName(name,type){if(!1===NAME_REGEX.test(name))throw new InvalidName(name,type)}async#verifyTemplateName(templateName){try{const location=path.join(this.#templateRootPath,templateName);return(await fs.stat(location)).isDirectory()}catch{return!1}}#copyTemplate(projectName,templateName){const templateLocation=path.join(this.#templateRootPath,templateName),projectLocation=path.join(this.#projectRootPath,projectName);return fs.cp(templateLocation,projectLocation,{recursive:!0,force:!0})}async#renameFiles(projectName){const projectLocation=path.join(this.#projectRootPath,projectName),files=Object.entries(RENAME_FILES),promises=[];for(const[source,target]of files){const sourceFileLocation=path.join(projectLocation,source),targetFileLocation=path.join(projectLocation,target);promises.push(fs.rename(sourceFileLocation,targetFileLocation))}await Promise.all(promises)}}class InitApp{name="init";description="Initializes a new Jitar project from a template.";options=[{key:"--name",required:!0,description:"Project name used for creating the project folder"},{key:"--template",required:!0,description:"Template to use [react, vue, jitar-only]"},{key:"--location",required:!1,description:"The root location of the project",defaultValue:"./"}];async execute(args){const projectName=args.getRequiredArgument("--name"),templateName=args.getRequiredArgument("--template"),rootPath=args.getOptionalArgument("--location",void 0);this.#printStartupMessage(projectName,templateName),await this.#initProject(projectName,templateName,rootPath),this.#printNextSteps(projectName)}#printStartupMessage(projectName,templateName){console.log(`Initializing project '${projectName}' with '${templateName}' template.`)}#initProject(projectName,templateName,rootPath){return new InitManager(rootPath).init(projectName,templateName)}#printNextSteps(projectName){console.log(`SUCCESS! Run the following commands to continue:\ncd ${projectName}\nnpm install\nnpm run build\nnpm run standalone`)}}const LogLevels_DEBUG=0,LogLevels_INFO=1,LogLevels_WARN=2,LogLevels_ERROR=3,LogLevels_FATAL=4;class Logger{#logLevel;#writer;constructor(logLevel=LogLevels_INFO,writer=console){this.#logLevel=logLevel,this.#writer=writer}debug(...message){if(this.#logLevel>LogLevels_DEBUG)return;const messageString=this.#createMessage("DEBUG",message);this.#writer.debug(messageString)}info(...message){if(this.#logLevel>LogLevels_INFO)return;const messageString=this.#createMessage("INFO",message);this.#writer.info(messageString)}warn(...message){if(this.#logLevel>LogLevels_WARN)return;const messageString=this.#createMessage("WARN",message);this.#writer.warn(messageString)}error(...message){if(this.#logLevel>LogLevels_ERROR)return;const messageString=this.#createMessage("ERROR",message);this.#writer.error(messageString)}fatal(...message){const messageString=this.#createMessage("FATAL",message);this.#writer.error(messageString)}#createMessage(logLevel,messages){return`[${logLevel}][${(new Date).toISOString()}] ${messages.map(value=>this.#interpretValue(value)).join(" ")}`}#interpretValue(value,level=0){let result;switch(typeof value){case"string":result=value;break;case"object":result=this.#interpretObject(value,level+1);break;case"undefined":result="undefined";break;case"function":result="function";break;default:result=String(value)}return`${this.#indent(level)}${result}`}#interpretObject(object,level){if(null===object)return"null";if(Array.isArray(object)){return`[\n${object.map(value=>this.#interpretValue(value,level)).join(",\n")}\n${this.#indent(level-1)}]`}return object instanceof Error?object.stack??object.message:JSON.stringify(object)}#indent(level){return" ".repeat(level)}}class InvalidLogLevel extends Error{constructor(logLevel){super(`Invalid log level: ${logLevel}`)}}class LogLevelParser{parse(logLevel){switch(logLevel.toUpperCase()){case"DEBUG":return LogLevels_DEBUG;case"INFO":return LogLevels_INFO;case"WARN":return LogLevels_WARN;case"ERROR":return LogLevels_ERROR;case"FATAL":return LogLevels_FATAL;default:throw new InvalidLogLevel(logLevel)}}}const Files$1_MODULE_PATTERN="**/*.js",Files$1_RESOURCE_PATTERN="**/*.json",Files$1_SEGMENT_PATTERN="**/*.json";class InvalidPath extends Error{#location;constructor(location){super(`Invalid location: ${location}`),this.#location=location}get location(){return this.#location}}class FileNotFound extends Error{#filename;constructor(filename){super(`The file '${filename}' could not be found`),this.#filename=filename}get filename(){return this.#filename}}class File{#location;#type;#content;constructor(location,type,content){this.#location=location,this.#type=type,this.#content=content}get location(){return this.#location}get type(){return this.#type}get content(){return this.#content}get size(){return this.#content.length}}class FileManager{#location;#rootLocation;#fileSystem;constructor(location,fileSystem){const rootLocation=fileSystem.resolve(location);this.#location=fileSystem.normalize(location),this.#rootLocation=fileSystem.normalize(rootLocation),this.#fileSystem=fileSystem}getAbsoluteLocation(filename){const location=this.#fileSystem.isAbsolute(filename)?filename:this.#fileSystem.join(this.#location,filename),absolutePath=this.#fileSystem.resolve(location),normalizedPath=this.#fileSystem.normalize(absolutePath);return this.#validateLocation(normalizedPath,filename),normalizedPath}getRelativeLocation(filename){const location=this.#fileSystem.relative(this.#location,filename);return this.#fileSystem.normalize(location)}normalizeLocation(location){return this.#fileSystem.normalize(location)}async getType(filename){const location=this.getAbsoluteLocation(filename);return await this.#fileSystem.mimeType(location)??"application/octet-stream"}async getContent(filename){const location=this.getAbsoluteLocation(filename);if(!1===await this.#fileSystem.exists(location))throw new FileNotFound(filename);return this.#fileSystem.read(location)}async exists(filename){const location=this.getAbsoluteLocation(filename);return this.#fileSystem.exists(location)}isDirectory(filename){const location=this.getAbsoluteLocation(filename);return this.#fileSystem.isDirectory(location)}async read(filename){const absoluteFilename=this.getAbsoluteLocation(filename),type=await this.getType(absoluteFilename),content=await this.getContent(absoluteFilename);return new File(filename,type,content)}async write(filename,content){const location=this.getAbsoluteLocation(filename);return this.#fileSystem.write(location,content)}async copy(source,destination){const sourceLocation=this.getAbsoluteLocation(source),destinationLocation=this.getAbsoluteLocation(destination);return this.#fileSystem.copy(sourceLocation,destinationLocation)}async delete(filename){const location=this.getAbsoluteLocation(filename);return this.#fileSystem.delete(location)}async filter(pattern){const location=this.getAbsoluteLocation("./"),normalizedPattern=this.#fileSystem.normalize(pattern);return(await this.#fileSystem.filter(location,normalizedPattern)).map(filename=>this.#fileSystem.normalize(filename))}#validateLocation(location,filename){if(!1===location.startsWith(this.#rootLocation))throw new InvalidPath(filename)}}class LocalFileSystem{copy(source,destination){return fsp.cp(source,destination,{recursive:!0,force:!0})}delete(location){return fsp.rm(location,{recursive:!0,force:!0})}async exists(location){try{return await fsp.stat(location),!0}catch{return!1}}isAbsolute(location){return path.isAbsolute(location)}isDirectory(location){try{return fs$1.statSync(location).isDirectory()}catch{return!1}}filter(location,pattern){return glob(`${location}/${pattern}`)}join(...paths){return path.join(...paths)}read(location){return fsp.readFile(location)}resolve(location){return path.resolve(location)}relative(from,to){return path.relative(from,to)}normalize(location){return location.replaceAll(path.win32.sep,path.posix.sep)}async mimeType(location){const mimeType=mime.lookup(location);if(!1!==mimeType)return mimeType}async write(location,content){const directory=path.dirname(location);return fs$1.mkdirSync(directory,{recursive:!0}),fsp.writeFile(location,content)}}class LocalFileManager extends FileManager{constructor(location){super(location,new LocalFileSystem)}}let ModuleNotLoaded$1=class extends Error{#url;#reason;constructor(url,reason){super(`Module '${url}' could not be loaded${void 0!==reason?` | ${reason}`:""}`),this.#url=url,this.#reason=reason}get url(){return this.#url}get reason(){return this.#reason}};class ImportManager{#moduleLocator;constructor(moduleLocator){this.#moduleLocator=moduleLocator}async import(filename){const location=this.#moduleLocator.locate(filename);try{return await import(location)}catch(error){const message=error instanceof Error?error.message:String(error);throw new ModuleNotLoaded$1(location,message)}}}class LocalModuleLocator{#fileManager;constructor(fileManager){this.#fileManager=fileManager}locate(filename){return`file://${filename.startsWith("/")?this.#fileManager.getAbsoluteLocation(`.${filename}`):this.#fileManager.getAbsoluteLocation(filename)}`}}class LocalImportManager extends ImportManager{constructor(fileManager){super(new LocalModuleLocator(fileManager))}}class SourcingManager{#fileManager;#importManager;constructor(fileManager,importManager){this.#fileManager=fileManager,this.#importManager=importManager}async filter(...patterns){return(await Promise.all(patterns.map(pattern=>this.#fileManager.filter(pattern)))).flat().map(file=>this.#fileManager.getRelativeLocation(file))}exists(filename){return this.#fileManager.exists(filename)}read(filename){return this.#fileManager.read(filename)}import(filename){return this.#importManager.import(filename)}}class LocalSourcingManager extends SourcingManager{constructor(location){const fileManager=new LocalFileManager(location);super(fileManager,new LocalImportManager(fileManager))}}let Application$1=class{#repository;#resources;#segmentation;constructor(repository,resources,segmentation){this.#repository=repository,this.#resources=resources,this.#segmentation=segmentation}get resources(){return this.#resources}get repository(){return this.#repository}get segmentation(){return this.#segmentation}};class ResourcesList{#resources;constructor(resources){this.#resources=resources}isResourceModule(moduleFilename){return this.#resources.includes(moduleFilename)}}const Defaults$1_ACCESS_LEVEL="private",Defaults$1_VERSION_NUMBER="0.0.0",Files_INDEX="index.js",Files_JSON=".json",Keywords$1_DEFAULT="default",Patterns_IMPORT=/import\s(?:["'\s]*([\w*{}\n, ]+)from\s*)?["'\s]*([@\w/._-]+)["'\s].*/g,Patterns_EXPORT=/export\s(?:["'\s]*([\w*{}\n, ]+)from\s*)?["'\s]*([@\w/._-]+)["'\s].*/g,Values_ASTERISK="*";let IdGenerator$1=class{#id=0;next(){return"$"+ ++this.#id}};const EXTENSION_PATTERN=/\.js$/,APPLICATION_MODULE_INDICATORS=[".","/","http:","https:"];class FileHelper{translatePath(filename){const parts=filename.split("/"),translated=[];for(const part of parts){switch(part.trim()){case"":case".":continue;case"..":translated.pop();continue}translated.push(part)}return translated.join("/")}makePathRelative(absoluteFilename,relativeToPath){if(""===relativeToPath)return`./${absoluteFilename}`;const absoluteFilenameParts=absoluteFilename.split("/"),relativeToParts=relativeToPath.split("/");for(;absoluteFilenameParts[0]===relativeToParts[0];)absoluteFilenameParts.shift(),relativeToParts.shift();const relativePath=relativeToParts.map(()=>"..").join("/");return`${relativeToParts.length>0?relativePath:"."}/${absoluteFilenameParts.join("/")}`}makePathAbsolute(relativeFilename,relativeToPath){const fullPath=""!==relativeToPath?`${relativeToPath}/${relativeFilename}`:relativeFilename;return this.translatePath(fullPath)}extractPath(filename){return filename.split("/").slice(0,-1).join("/")}stripPath(path){return path.substring(1,path.length-1)}extractFilename(filename){return filename.split("/").pop()}assureExtension(filename){return filename.endsWith(".js")?filename:`${filename}.js`}addSubExtension(filename,subExtension){return filename.replace(EXTENSION_PATTERN,`.${subExtension}.js`)}isApplicationModule(from){return APPLICATION_MODULE_INDICATORS.some(indicator=>from.startsWith(indicator))}}let FileNotLoaded$2=class extends Error{constructor(filename,message){super(`Failed to load resource file '${filename}' because of: ${message}`)}};class ResourceReader{#resourcesFileManager;#sourceFileManager;#fileHelper=new FileHelper;constructor(resourcesFileManager,sourceFileManager){this.#resourcesFileManager=resourcesFileManager,this.#sourceFileManager=sourceFileManager}async readAll(filenames){const resources=await Promise.all(filenames.map(filename=>this.#loadResourceDefinition(filename)));return new ResourcesList(resources.flat())}async#loadResourceDefinition(filename){try{const content=await this.#resourcesFileManager.getContent(filename);return JSON.parse(content.toString()).map(resource=>this.#makeResourceFilename(resource))}catch(error){const message=error instanceof Error?error.message:String(error);throw new FileNotLoaded$2(filename,message)}}#makeResourceFilename(filename){const normalizedFilename=this.#sourceFileManager.normalizeLocation(filename),fullFilename=this.#sourceFileManager.isDirectory(normalizedFilename)?`${normalizedFilename}/${Files_INDEX}`:this.#fileHelper.assureExtension(normalizedFilename);return fullFilename.startsWith("./")?fullFilename.substring(2):fullFilename.startsWith("/")?fullFilename.substring(1):fullFilename}}let Module$1=class{#filename;#code;#model;constructor(filename,code,model){this.#code=code,this.#filename=filename,this.#model=model}get filename(){return this.#filename}get code(){return this.#code}get model(){return this.#model}};class Repository{#modules;constructor(modules){this.#modules=modules}get modules(){return this.#modules}get(filename){return this.#modules.find(module=>module.filename===filename)}}class ESAlias{#name;#as;constructor(name,as){this.#name=name,this.#as=as}get name(){return this.#name}get as(){return this.#as}toString(){return`${this.#name} as ${this.#as}`}}class ESValue{#definition;constructor(definition){this.#definition=definition}get definition(){return this.#definition}toString(){return this.#definition}}class ESArray extends ESValue{}class ESMember{#name;#isStatic;#isPrivate;constructor(name,isStatic=!1,isPrivate=!1){this.#name=name,this.#isStatic=isStatic,this.#isPrivate=isPrivate}get name(){return this.#name}get isStatic(){return this.#isStatic}get isPrivate(){return this.#isPrivate}get isPublic(){return!1===this.#isPrivate}}class ESClass extends ESMember{#parentName;#scope;constructor(name,parentName,scope){super(name),this.#parentName=parentName,this.#scope=scope}get parentName(){return this.#parentName}get scope(){return this.#scope}get members(){return this.#scope.members}get declarations(){return this.#scope.declarations}get functions(){return this.#scope.functions}get getters(){return this.#scope.getters}get setters(){return this.#scope.setters}get generators(){return this.#scope.generators}get readable(){const members=new Map;return this.getters.forEach(getter=>{members.set(getter.name,getter)}),this.declarations.forEach(declaration=>{declaration.isPublic&&members.set(declaration.name,declaration)}),[...members.values()]}get writable(){const members=new Map;return this.setters.forEach(setter=>{members.set(setter.name,setter)}),this.declarations.forEach(declaration=>{declaration.isPublic&&members.set(declaration.name,declaration)}),[...members.values()]}get callable(){return this.functions.filter(funktion=>funktion.isPublic)}getMember(name){return this.#scope.getMember(name)}getDeclaration(name){return this.#scope.getDeclaration(name)}getFunction(name){return this.#scope.getFunction(name)}getGetter(name){return this.#scope.getGetter(name)}getSetter(name){return this.#scope.getSetter(name)}getGenerator(name){return this.#scope.getGenerator(name)}hasMember(name){return this.#scope.hasMember(name)}hasDeclaration(name){return this.#scope.hasDeclaration(name)}hasFunction(name){return this.#scope.hasFunction(name)}hasGetter(name){return this.#scope.hasGetter(name)}hasSetter(name){return this.#scope.hasSetter(name)}hasGenerator(name){return this.#scope.hasGenerator(name)}canRead(name){const declaration=this.getDeclaration(name);return declaration?.isPublic||this.hasGetter(name)}canWrite(name){const declaration=this.getDeclaration(name);return declaration?.isPublic||this.hasSetter(name)}canCall(name){const funktion=this.getFunction(name);return funktion?.isPublic??!1}toString(){const infix=void 0!==this.#parentName?` extends ${this.#parentName}`:"";return`class ${this.name}${infix} { ${this.#scope.toString()} }`}}class ESDeclaration extends ESMember{#identifier;#value;constructor(identifier,value,isStatic=!1,isPrivate=!1){super(identifier.toString(),isStatic,isPrivate),this.#identifier=identifier,this.#value=value}get identifier(){return this.#identifier}get value(){return this.#value}toString(){return`${this.name}${this.value?" = "+this.value.toString():""}`}}class ESDestructuredValue{#members;constructor(members){this.#members=members}get members(){return this.#members}toString(){return this.#members.map(member=>member.toString()).join(" , ")}}class ESDestructuredArray extends ESDestructuredValue{toString(){return`[ ${super.toString()} ]`}}class ESDestructuredObject extends ESDestructuredValue{toString(){return`{ ${super.toString()} }`}}class ESExport extends ESMember{#members;#from;constructor(members,from){super(""),this.#members=members,this.#from=from}get members(){return this.#members}get from(){return this.#from}hasMember(name){return this.#members.some(member=>member.as===name)}getMember(name){return this.#members.find(member=>member.as===name)}toString(){const postfix=this.#from?` from '${this.#from}'`:"";return`export { ${this.#members.join(", ")} }${postfix}`}}class ESExpression extends ESValue{}class ESField{#name;#value;constructor(name,value){this.#name=name,this.#value=value}get name(){return this.#name}get value(){return this.#value}toString(){return`${this.name}${this.value?" = "+this.value.toString():""}`}}class ESFunction extends ESMember{#parameters;#body;#isAsync;constructor(name,parameters,body,isStatic=!1,isAsync=!1,isPrivate=!1){super(name,isStatic,isPrivate),this.#parameters=parameters,this.#body=body,this.#isAsync=isAsync}get parameters(){return this.#parameters}get body(){return this.#body}get isAsync(){return this.#isAsync}toString(){const parameters=this.parameters.map(parameter=>parameter.toString());return`${this.isAsync?"async ":""}${this.name}(${parameters.join(", ")}) { ${this.body} }`}}class ESGenerator extends ESFunction{toString(){const parameters=this.parameters.map(parameter=>parameter.toString());return`${this.isAsync?"async ":""}${this.name}*(${parameters.join(", ")}) { ${this.body} }`}}class ESGetter extends ESFunction{toString(){return`get ${super.toString()}`}}class ESImport extends ESMember{#members;#from;constructor(members,from){super(""),this.#members=members,this.#from=from}get members(){return this.#members}get from(){return this.#from}hasMember(name){return this.#members.some(member=>member.as===name)}getMember(name){return this.#members.find(member=>member.as===name)}toString(){return`import { ${this.#members.map(member=>member.toString()).join(", ")} } from '${this.#from}';`}}class ESModule{#scope;constructor(scope){this.#scope=scope}get scope(){return this.#scope}get members(){return this.#scope.members}get exportedMembers(){return this.#filterExported(this.#scope.members)}get imports(){return this.#scope.imports}get exports(){return this.#scope.exports}get declarations(){return this.#scope.declarations}get exportedDeclarations(){return this.#filterExported(this.#scope.declarations)}get functions(){return this.#scope.functions}get exportedFunctions(){return this.#filterExported(this.#scope.functions)}get generators(){return this.#scope.generators}get exportedGenerators(){return this.#filterExported(this.#scope.generators)}get classes(){return this.#scope.classes}get exportedClasses(){return this.#filterExported(this.#scope.classes)}get exported(){const exported=new Map;for(const exportItem of this.exports)for(const alias of exportItem.members){const member=this.getMember(alias.name);void 0!==member&&exported.set(alias.as,member)}return exported}getMember(name){return this.#scope.getMember(name)}getDeclaration(name){return this.#scope.getDeclaration(name)}getFunction(name){return this.#scope.getFunction(name)}getGenerator(name){return this.#scope.getGenerator(name)}getClass(name){return this.#scope.getClass(name)}hasMember(name){return this.#scope.hasMember(name)}hasDeclaration(name){return this.#scope.hasDeclaration(name)}hasFunction(name){return this.#scope.hasFunction(name)}hasGenerator(name){return this.#scope.hasGenerator(name)}hasClass(name){return this.#scope.hasClass(name)}getImport(name){return this.imports.find(importItem=>importItem.hasMember(name))}getImported(name){for(const importItem of this.imports)for(const alias of importItem.members)if(alias.as===name)return this.getMember(alias.name)}isExported(member){for(const exportItem of this.exports)for(const alias of exportItem.members)if(alias.name===member.name)return!0;return!1}getExport(name){return this.exports.find(exportItem=>exportItem.hasMember(name))}getExported(name){for(const exportItem of this.exports)for(const alias of exportItem.members)if(alias.as===name)return this.getMember(alias.name)}#filterExported(members){return members.filter(member=>this.isExported(member))}}class ESObject extends ESValue{}class ESSetter extends ESFunction{toString(){return`set ${super.toString()}`}}const IMPORT_NAME=ESImport.name,EXPORT_NAME=ESExport.name,DECLARATION_NAME=ESDeclaration.name,FUNCTION_NAME=ESFunction.name,GETTER_NAME=ESGetter.name,SETTER_NAME=ESSetter.name,GENERATOR_NAME=ESGenerator.name,CLASS_NAME=ESClass.name;class ESScope{#members;constructor(members){this.#members=members}get members(){return this.#members}get imports(){return this.#members.filter(member=>member.constructor.name===IMPORT_NAME)}get exports(){return this.#members.filter(member=>member.constructor.name===EXPORT_NAME)}get declarations(){return this.#members.filter(member=>member.constructor.name===DECLARATION_NAME)}get functions(){return this.#members.filter(member=>member.constructor.name===FUNCTION_NAME)}get getters(){return this.#members.filter(member=>member.constructor.name===GETTER_NAME)}get setters(){return this.#members.filter(member=>member.constructor.name===SETTER_NAME)}get generators(){return this.#members.filter(member=>member.constructor.name===GENERATOR_NAME)}get classes(){return this.#members.filter(member=>member.constructor.name===CLASS_NAME)}getMember(name){return this.#members.find(member=>member.name===name)}getDeclaration(name){return this.declarations.find(member=>member.name===name)}getFunction(name){return this.functions.find(member=>member.name===name)}getGetter(name){return this.getters.find(member=>member.name===name)}getSetter(name){return this.setters.find(member=>member.name===name)}getGenerator(name){return this.generators.find(member=>member.name===name)}getClass(name){return this.classes.find(member=>member.name===name)}hasMember(name){return void 0!==this.getMember(name)}hasDeclaration(name){return void 0!==this.getDeclaration(name)}hasFunction(name){return void 0!==this.getFunction(name)}hasGetter(name){return void 0!==this.getGetter(name)}hasSetter(name){return void 0!==this.getSetter(name)}hasGenerator(name){return void 0!==this.getGenerator(name)}hasClass(name){return void 0!==this.getClass(name)}toString(){return this.#members.map(member=>member.toString()).join("\n")}}const Comment={SINGLE:"//",MULTI_START:"/*",MULTI_END:"*/"},Comments=Object.values(Comment);const Punctuation_DOT=".",Punctuation_LEFT_PARENTHESIS="(",Punctuation_RIGHT_PARENTHESIS=")",Punctuation_LEFT_BRACKET="[",Punctuation_RIGHT_BRACKET="]",Punctuation_LEFT_BRACE="{",Punctuation_RIGHT_BRACE="}",Divider={SCOPE:":",SEPARATOR:",",TERMINATOR:";"},Divisions=Object.values(Divider);function isDivider(value){return Divisions.includes(value)}const Empty={UNDEFINED:void 0,NULL:null,STRING:""},Empties=Object.values(Empty);function isEmpty(value){return Empties.includes(value)}const Group={OPEN:Punctuation_LEFT_PARENTHESIS,CLOSE:Punctuation_RIGHT_PARENTHESIS};function isGroup(value){return value===Group.OPEN||value===Group.CLOSE}const Keyword={EXPORT:"export",DEFAULT:"default",CLASS:"class",FUNCTION:"function",CONST:"const",LET:"let",VAR:"var",AS:"as",FROM:"from",IMPORT:"import",GET:"get",SET:"set",EXTENDS:"extends",STATIC:"static",ASYNC:"async",RETURN:"return"},Keywords=Object.values(Keyword);function isKeyword(value){return Keywords.includes(value)}function isNotReserved(value){return value===Keyword.AS||value===Keyword.ASYNC||value===Keyword.FROM||value===Keyword.GET||value===Keyword.SET}const List={OPEN:Punctuation_LEFT_BRACKET,CLOSE:Punctuation_RIGHT_BRACKET};function isList(value){return value===List.OPEN||value===List.CLOSE}const Literals=Object.values({SINGLE:"'",DOUBLE:'"',BACKTICK:"`"});function isLiteral(value){return Literals.includes(value)}const Operator={ADD:"+",ARROW:"=>",ASSIGN:"=",ASSIGN_ADD:"+=",ASSIGN_BITWISE_AND:"&=",ASSIGN_BITWISE_OR:"|=",ASSIGN_DIVIDE:"/=",ASSIGN_LEFT_SHIFT:"<<=",ASSIGN_LOGICAL_AND:"&&=",ASSIGN_LOGICAL_OR:"||=",ASSIGN_MODULO:"%=",ASSIGN_MULTIPLY:"*=",ASSIGN_RIGHT_SHIFT:">>=",ASSIGN_SUBTRACT:"-=",ASSIGN_XOR:"^=",BITWISE_AND:"&",BITWISE_OR:"|",DECREMENT:"--",DIVIDE:"/",EQUAL:"==",EQUAL_STRICT:"===",GREATER:">",GREATER_EQUAL:">=",INCREMENT:"++",LEFT_SHIFT:"<<",LESS:"<",LESS_EQUAL:"<=",LOGICAL_AND:"&&",LOGICAL_OR:"||",MODULO:"%",MULTIPLY:"*",NOT:"!",NOT_EQUAL:"!=",NOT_EQUAL_STRICT:"!==",RIGHT_SHIFT:">>",SUBTRACT:"-",TERNARY:"?",XOR:"^"},Operators=Object.values(Operator);function isOperator(value){return Operators.includes(value)}const Scope={OPEN:Punctuation_LEFT_BRACE,CLOSE:Punctuation_RIGHT_BRACE};function isScope(value){return value===Scope.OPEN||value===Scope.CLOSE}const TokenType={COMMENT:"comment",DIVIDER:"divider",GROUP:"group",IDENTIFIER:"identifier",KEYWORD:"keyword",LIST:"list",LITERAL:"literal",OPERATOR:"operator",REGEX:"regex",SCOPE:"scope",WHITESPACE:"whitespace"},Whitespace={SPACE:" ",TAB:"\t",NEWLINE:"\n",CARRIAGE_RETURN:"\r"},Whitespaces=Object.values(Whitespace);function isWhitespace(value){return Whitespaces.includes(value)}class ItemList{#items;#position;constructor(items){this.#items=items,this.#position=0}get items(){return this.#items}get position(){return this.#position}get size(){return this.#items.length}get eol(){return this.#position>=this.#items.length}get current(){return this.#items[this.#position]}get next(){return this.#items[this.#position+1]}get previous(){return this.#items[this.#position-1]}notAtEnd(){return!1===this.eol}get(index){return this.#items[index]}step(amount=1){return this.#position+=amount,this.current}stepBack(amount=1){return this.#position-=amount,this.current}hasNext(){return this.#position+1<this.#items.length}}class CharList extends ItemList{constructor(code){super(code.split(""))}}class Token{#type;#value;#start;#end;constructor(type,value,start,end){this.#type=type,this.#value=value,this.#start=start,this.#end=end}get type(){return this.#type}get value(){return this.#value}get start(){return this.#start}get end(){return this.#end}isType(type){return this.#type===type}hasValue(value){return this.#value===value}toString(){return`${this.#value}`}}class TokenList extends ItemList{}class Lexer{tokenize(code){const charList=new CharList(code),tokens=[];let last;for(;charList.notAtEnd();){const token=this.#getNextToken(charList,last);if(void 0===token)break;token.isType(TokenType.WHITESPACE)||token.isType(TokenType.COMMENT)?charList.step():(tokens.push(token),this.#isCodeToken(token)&&(last=token),charList.step())}return new TokenList(tokens)}#isCodeToken(token){return!1===[TokenType.WHITESPACE,TokenType.COMMENT].includes(token.type)}#getNextToken(charList,lastToken){const char=charList.current,start=charList.position;if(isWhitespace(char)){const end=charList.position;return new Token(TokenType.WHITESPACE,char,start,end)}if(function(value){return Comments.includes(value)}(char+charList.next)){const value=this.#readComment(charList),end=charList.position;return new Token(TokenType.COMMENT,value,start,end)}if(this.#startsRegex(char,lastToken)){const value=this.#readRegex(charList),end=charList.position;return new Token(TokenType.REGEX,value,start,end)}if(isLiteral(char)){const value=this.#readLiteral(charList),end=charList.position;return new Token(TokenType.LITERAL,value,start,end)}if(isOperator(char)){const value=this.#readOperation(charList),end=charList.position;return new Token(TokenType.OPERATOR,value,start,end)}if(isDivider(char)){const end=charList.position;return new Token(TokenType.DIVIDER,char,start,end)}if(isGroup(char)){const end=charList.position;return new Token(TokenType.GROUP,char,start,end)}if(isScope(char)){const end=charList.position;return new Token(TokenType.SCOPE,char,start,end)}if(isList(char)){const end=charList.position;return new Token(TokenType.LIST,char,start,end)}if(isEmpty(char))return;const value=this.#readIdentifier(charList),type=isKeyword(value)?TokenType.KEYWORD:TokenType.IDENTIFIER,end=charList.position;return new Token(type,value,start,end)}#readComment(charList){const isMulti=charList.current+charList.next===Comment.MULTI_START,terminator=isMulti?Comment.MULTI_END:Whitespace.NEWLINE;let value=isMulti?Comment.MULTI_START:Comment.SINGLE;for(charList.step(2);charList.notAtEnd();){const char=charList.current;if((isMulti?char+charList.next:char)===terminator){charList.step(terminator.length-1);break}value+=char,charList.step()}return isMulti?value+Comment.MULTI_END:value.trim()}#startsRegex(char,lastToken){return char===Operator.DIVIDE&&(void 0===lastToken||([TokenType.OPERATOR,TokenType.DIVIDER,TokenType.KEYWORD].includes(lastToken.type)||[Group.OPEN,List.OPEN].includes(lastToken.value)))}#endsRegex(char){return isWhitespace(char)||char==Punctuation_DOT||!1===this.#isIdentifier(char)}#readRegex(charList){let value=charList.current,closed=!1;for(charList.step();charList.notAtEnd();){const current=charList.current,previous=charList.previous;if(current===Operator.DIVIDE&&"\\"!==previous)closed=!0;else if(!0===closed&&this.#endsRegex(current)){charList.stepBack();break}value+=current,charList.step()}return value}#readLiteral(charList){const identifier=charList.current;let value=identifier,escaped=!1;for(charList.step();charList.notAtEnd();){const char=charList.current;if(!1===escaped){if(char===identifier){value+=char;break}"\\"===char&&(escaped=!0)}else escaped=!1;value+=char,charList.step()}return value}#isIdentifier(char){return!1===(isEmpty(char)||isWhitespace(char)||isOperator(char)||isLiteral(char)||isDivider(char)||isGroup(char)||isScope(char)||isList(char))}#readIdentifier(charList){let value="";for(;charList.notAtEnd();){const char=charList.current;if(!1===this.#isIdentifier(char)){charList.stepBack();break}value+=char,charList.step()}return value}#readOperation(charList){let value=charList.current;for(charList.step();charList.notAtEnd();){const char=charList.current;if(!1===isOperator(char)||!1===isOperator(value+char)){charList.stepBack();break}value+=char,charList.step()}return value}}class ExpectedKeyword extends Error{constructor(value,position){super(`Expected keyword '${value}' at position ${position}`)}}class ExpectedToken extends Error{constructor(value,position){super(`Expected token '${value}' at position ${position}`)}}class UnexpectedKeyword extends Error{constructor(keyword,position){super(`Unexpected keyword '${keyword}' at position ${position}`)}}class UnexpectedParseResult extends Error{constructor(expected){super(`The given code does not contain ${expected}`)}}class UnexpectedToken extends Error{constructor(value,position){super(`Unexpected token '${value}' at position ${position}`)}}class Parser{#lexer;constructor(lexer=new Lexer){this.#lexer=lexer}parse(code){const tokenList=this.#lexer.tokenize(code),scope=this.#parseScope(tokenList);return new ESModule(scope)}parseFirst(code){const tokenList=this.#lexer.tokenize(code);return this.#parseNext(tokenList)}parseValue(code){const model=this.parseFirst(code);if(model instanceof ESValue==!1)throw new UnexpectedParseResult("a value definition");return model}parseImport(code){const model=this.parseFirst(code);if(model instanceof ESImport==!1)throw new UnexpectedParseResult("an import definition");return model}parseExport(code){const model=this.parseFirst(code);if(model instanceof ESExport==!1)throw new UnexpectedParseResult("an export definition");return model}parseDeclaration(code){const model=this.parseFirst(code);if(model instanceof ESDeclaration==!1)throw new UnexpectedParseResult("a declaration definition");return model}parseFunction(code){const tokenList=this.#lexer.tokenize(code),model=this.#parseMember(tokenList);if(model instanceof ESFunction==!1)throw new UnexpectedParseResult("a function definition");return model}parseClass(code){const tokenList=this.#lexer.tokenize(code),model=this.#parseMember(tokenList);if(model instanceof ESClass==!1)throw new UnexpectedParseResult("a class definition");return model}#parseScope(tokenList){const members=[];for(;tokenList.notAtEnd();){const member=this.#parseNext(tokenList);member instanceof ESMember&&members.push(member)}return new ESScope(members)}#parseNext(tokenList,isAsync=!1){const token=tokenList.current;if(token.isType(TokenType.LITERAL))return this.#parseExpression(tokenList);if(token.isType(TokenType.IDENTIFIER)){const next=tokenList.next;return next?.hasValue(Operator.ARROW)?this.#parseArrowFunction(tokenList,isAsync):this.#parseExpression(tokenList)}if(token.isType(TokenType.KEYWORD)){if(isNotReserved(token.value)){const next=tokenList.next,nextIsFunction=void 0!==next&&(next.hasValue(Keyword.FUNCTION)||next.hasValue(Group.OPEN));if(token.hasValue(Keyword.ASYNC)&&nextIsFunction)return tokenList.step(),this.#parseNext(tokenList,!0);if(void 0===next||this.#atEndOfStatement(next))return this.#parseExpression(tokenList)}return token.hasValue(Keyword.RETURN)?this.#parseExpression(tokenList):this.#parseMember(tokenList,isAsync)}if(token.isType(TokenType.REGEX))return this.#parseExpression(tokenList);if(token.hasValue(Group.OPEN)){const next=this.#peekAfterBlock(tokenList,Group.OPEN,Group.CLOSE);return next?.hasValue(Operator.ARROW)?this.#parseArrowFunction(tokenList,isAsync):this.#parseExpression(tokenList)}if(token.hasValue(Scope.OPEN))return this.#parseObject(tokenList);if(token.hasValue(List.OPEN))return this.#parseArray(tokenList);if(token.hasValue(Operator.NOT)||token.hasValue(Operator.SUBTRACT))return this.#parseExpression(tokenList);if(!isDivider(token.value))throw new UnexpectedToken(token.value,token.start);tokenList.step()}#parseMember(tokenList,isAsync=!1){const token=tokenList.current;switch(tokenList.step(),token.value){case Keyword.IMPORT:return this.#parseImport(tokenList);case Keyword.EXPORT:return this.#parseExport(tokenList);case Keyword.CLASS:return this.#parseClass(tokenList);case Keyword.FUNCTION:return this.#parseFunction(tokenList,isAsync);case Keyword.VAR:case Keyword.LET:case Keyword.CONST:return this.#parseDeclaration(tokenList,!1,!0);case Keyword.ASYNC:return this.#parseMember(tokenList,!0);default:throw new UnexpectedKeyword(token.value,token.start)}}#parseImport(tokenList){const members=[];let token=tokenList.current;if(token.isType(TokenType.LITERAL))return new ESImport(members,token.value);if(token.hasValue(Group.OPEN)){token=tokenList.step();const from=token.value;return tokenList.step(2),new ESImport(members,from)}if(!1===token.hasValue(Scope.OPEN)){const name=token.hasValue(Operator.MULTIPLY)?Operator.MULTIPLY:"default";let as=token.value;token=tokenList.step(),token.hasValue(Keyword.AS)&&(token=tokenList.step(),as=token.value,token=tokenList.step()),members.push(new ESAlias(name,as))}if(token.hasValue(Divider.SEPARATOR)&&(token=tokenList.step()),token.hasValue(Scope.OPEN)){const aliases=this.#parseAliasList(tokenList);members.push(...aliases),token=tokenList.current}if(!1===token.hasValue(Keyword.FROM))throw new ExpectedKeyword(Keyword.FROM,token.start);token=tokenList.step();const from=token.value;return tokenList.step(),new ESImport(members,from)}#parseExport(tokenList){switch(tokenList.current.value){case Keyword.DEFAULT:return tokenList.step(),this.#parseSingleExport(tokenList,!0);case Scope.OPEN:return this.#parseMultiExport(tokenList);default:return this.#parseSingleExport(tokenList,!1)}}#parseSingleExport(tokenList,isDefault){let token=tokenList.current,stepSize=0;var value;token.hasValue(Keyword.ASYNC)&&(token=tokenList.step(),stepSize++),((value=token.value)===Keyword.CLASS||value===Keyword.FUNCTION||value===Keyword.CONST||value===Keyword.LET||value===Keyword.VAR)&&(token=tokenList.step(),stepSize++);const name=this.#isIdentifier(token)?token.value:"",as=isDefault?"default":name;let from;token=tokenList.step(),token?.hasValue(Keyword.FROM)&&(token=tokenList.step(),from=token.value),stepSize>0&&(stepSize++,tokenList.stepBack(stepSize));const alias=new ESAlias(name,as);return new ESExport([alias],from)}#parseMultiExport(tokenList){const members=this.#parseAliasList(tokenList);let from,token=tokenList.current;return token?.hasValue(Keyword.FROM)&&(token=tokenList.step(),from=token.value),tokenList.step(),new ESExport(members,from)}#parseAliasList(tokenList){const aliases=[];let token=tokenList.step();for(;tokenList.notAtEnd();){if(token.hasValue(Scope.CLOSE)){tokenList.step();break}if(token.hasValue(Divider.SEPARATOR)){token=tokenList.step();continue}const alias=this.#parseAlias(tokenList);aliases.push(alias),token=tokenList.step()}return aliases}#parseAlias(tokenList){let token=tokenList.current;const name=token.value;let as=name;return tokenList.next.hasValue(Keyword.AS)&&(token=tokenList.step(2),as=token.value),new ESAlias(name,as)}#parseDeclaration(tokenList,isStatic,parseMultiple=!1){let identifier,value,token=tokenList.current,isPrivate=!1;return token.hasValue(List.OPEN)?(identifier=this.#parseDestructuredArray(tokenList),token=tokenList.current):token.hasValue(Scope.OPEN)?(identifier=this.#parseDestructuredObject(tokenList),token=tokenList.current):(isPrivate=token.value.startsWith("#"),identifier=isPrivate?token.value.substring(1):token.value,token=tokenList.step()),token.hasValue(Operator.ASSIGN)&&(tokenList.step(),value=this.#parseNext(tokenList,!1),token=tokenList.current),void 0!==token&&(token.hasValue(Divider.TERMINATOR)?tokenList.step():!0===parseMultiple&&token.hasValue(Divider.SEPARATOR)&&(tokenList.step(),this.#parseDeclaration(tokenList,isStatic,!0))),value instanceof ESGenerator?new ESGenerator(identifier.toString(),value.parameters,value.body,isStatic,value.isAsync,isPrivate):value instanceof ESFunction?new ESFunction(identifier.toString(),value.parameters,value.body,isStatic,value.isAsync,isPrivate):value instanceof ESClass?new ESClass(identifier.toString(),value.parentName,value.scope):new ESDeclaration(identifier,value,isStatic,isPrivate)}#parseFunction(tokenList,isAsync,isStatic=!1,isGetter=!1,isSetter=!1){let token=tokenList.current,name="",isGenerator=!1,isPrivate=!1;token.hasValue(Operator.MULTIPLY)&&(isGenerator=!0,token=tokenList.step()),this.#isIdentifier(token)&&(isPrivate=token.value.startsWith("#"),name=isPrivate?token.value.substring(1):token.value,token=tokenList.step());const parameters=this.#parseParameters(tokenList,Group.CLOSE);if(token=tokenList.current,!1===token.hasValue(Scope.OPEN))throw new ExpectedToken(Scope.OPEN,token.start);const body=this.#parseBlock(tokenList,Scope.OPEN,Scope.CLOSE);return isGenerator?new ESGenerator(name,parameters,body,isStatic,isAsync,isPrivate):isGetter?new ESGetter(name,parameters,body,isStatic,isAsync,isPrivate):isSetter?new ESSetter(name,parameters,body,isStatic,isAsync,isPrivate):new ESFunction(name,parameters,body,isStatic,isAsync,isPrivate)}#parseArrowFunction(tokenList,isAsync){let parameters,token=tokenList.current;if(token.hasValue(Group.OPEN)?(parameters=this.#parseParameters(tokenList,Group.CLOSE),token=tokenList.current):(parameters=[new ESField(token.value,void 0)],token=tokenList.step()),!1===token.hasValue(Operator.ARROW))throw new ExpectedToken(Operator.ARROW,token.start);token=tokenList.step();const body=token.hasValue(Scope.OPEN)?this.#parseBlock(tokenList,Scope.OPEN,Scope.CLOSE):this.#parseExpression(tokenList).definition;return new ESFunction("",parameters,body,!1,isAsync,!1)}#parseParameters(tokenList,closeId){const parameters=[];for(tokenList.step();tokenList.notAtEnd();){const token=tokenList.current;if(token.hasValue(closeId)){tokenList.step();break}if(token.hasValue(Divider.SEPARATOR)){tokenList.step();continue}let parameter;parameter=token.hasValue(Scope.OPEN)?this.#parseDestructuredObject(tokenList):token.hasValue(List.OPEN)?this.#parseDestructuredArray(tokenList):this.#parseField(tokenList),parameters.push(parameter)}return parameters}#parseClass(tokenList){let parent,token=tokenList.current,name="";if(this.#isIdentifier(token)&&(name=token.value,token=tokenList.step()),token.hasValue(Keyword.EXTENDS)&&(token=tokenList.step(),parent=token.value,token=tokenList.step()),!1===token.hasValue(Scope.OPEN))throw new ExpectedToken(Scope.OPEN,token.start);const scope=this.#parseClassScope(tokenList);return new ESClass(name,parent,scope)}#parseClassScope(tokenList){let token=tokenList.step();const members=[];for(;tokenList.notAtEnd();){if(token.hasValue(Scope.CLOSE)){tokenList.step();break}const member=this.#parseClassMember(tokenList);members.push(member),token=tokenList.current}return new ESScope(members)}#parseClassMember(tokenList){let token=tokenList.current,isAsync=!1,isStatic=!1,isGetter=!1,isSetter=!1;for(;tokenList.notAtEnd();){if(token.hasValue(Keyword.STATIC))isStatic=!0;else if(token.hasValue(Keyword.ASYNC))isAsync=!0;else if(token.hasValue(Keyword.GET))isGetter=!0;else{if(!token.hasValue(Keyword.SET)){if(token.hasValue(Operator.MULTIPLY))return this.#parseFunction(tokenList,isAsync,isStatic,!1,!1);break}isSetter=!0}token=tokenList.step()}return tokenList.next.hasValue(Group.OPEN)?this.#parseFunction(tokenList,isAsync,isStatic,isGetter,isSetter):this.#parseDeclaration(tokenList,isStatic)}#parseArray(tokenList){const items=this.#parseBlock(tokenList,List.OPEN,List.CLOSE);return new ESArray(items)}#parseDestructuredArray(tokenList){const fields=this.#parseParameters(tokenList,List.CLOSE);return new ESDestructuredArray(fields)}#parseObject(tokenList){const fields=this.#parseBlock(tokenList,Scope.OPEN,Scope.CLOSE);return new ESObject(fields)}#parseDestructuredObject(tokenList){const fields=this.#parseParameters(tokenList,Scope.CLOSE);return new ESDestructuredObject(fields)}#parseField(tokenList){let token=tokenList.current;const name=token.value;let value;return token=tokenList.step(),token.hasValue(Operator.ASSIGN)&&(tokenList.step(),value=this.#parseNext(tokenList,!1)),new ESField(name,value)}#parseExpression(tokenList){let token=tokenList.current,code="";for(;tokenList.notAtEnd();){if(token.hasValue(List.OPEN)){code+=this.#parseBlock(tokenList,List.OPEN,List.CLOSE)+" ",token=tokenList.current}else if(token.hasValue(Group.OPEN)){code+=this.#parseBlock(tokenList,Group.OPEN,Group.CLOSE)+" ",token=tokenList.current}else if(token.hasValue(Scope.OPEN)){code+=this.#parseBlock(tokenList,Scope.OPEN,Scope.CLOSE)+" ",token=tokenList.current}else code+=token.toString()+" ",token=tokenList.step();if(void 0===token||this.#atEndOfStatement(token))break}return new ESExpression(code.trim())}#parseBlock(tokenList,openId,closeId){let token=tokenList.step(),code=openId+" ";for(;tokenList.notAtEnd();)if(token.hasValue(openId))code+=this.#parseBlock(tokenList,openId,closeId)+" ",token=tokenList.current;else{if(token.hasValue(closeId))return tokenList.step(),code+=closeId,code;code+=token.toString()+" ",token=tokenList.step()}return code}#peekAfterBlock(tokenList,openId,closeId){const start=tokenList.position;this.#parseBlock(tokenList,openId,closeId);const token=tokenList.current,end=tokenList.position;return tokenList.stepBack(end-start),token}#atEndOfStatement(token){return[Divider.TERMINATOR,Divider.SEPARATOR].includes(token.value)||[List.CLOSE,Group.CLOSE,Scope.CLOSE].includes(token.value)||isKeyword(token.value)}#isIdentifier(token){return token.isType(TokenType.IDENTIFIER)||token.isType(TokenType.KEYWORD)&&isNotReserved(token.value)}}class ClassMerger{merge(model,parent){const declarations=this.#mergeDeclarations(model.declarations,parent.declarations),functions=this.#mergeFunctions(model.functions,parent.functions),getters=this.#mergeFunctions(model.getters,parent.getters),setters=this.#mergeFunctions(model.setters,parent.setters),members=[...declarations.values(),...functions.values(),...getters.values(),...setters.values()];return new ESClass(model.name,parent.name,new ESScope(members))}#mergeDeclarations(model,parent){const declarations=new Map;return parent.forEach(declaration=>declarations.set(declaration.name,declaration)),model.forEach(declaration=>declarations.set(declaration.name,declaration)),[...declarations.values()]}#mergeFunctions(model,parent){const functions=new Map;return parent.forEach(funktion=>functions.set(funktion.name,funktion)),model.forEach(funktion=>functions.set(funktion.name,funktion)),[...functions.values()]}}class Reflector{#parser=new Parser;#merger=new ClassMerger;fromModule(module,inherit=!1){const entries=Object.entries(module),members=[];for(const[key,member]of entries){if("function"!=typeof member.toString)continue;const code=member.toString();if(code.startsWith("class"))members.push(this.fromClass(member,inherit));else if(code.startsWith("function"))members.push(this.fromFunction(member));else{const expression=new ESExpression(code);members.push(new ESDeclaration(key,expression))}}return new ESModule(new ESScope(members))}fromClass(clazz,inherit=!1){const model=this.isClass(clazz)?this.#reflectStatic(clazz):this.#reflectDynamic(clazz);if(!1===inherit)return model;const parentClazz=this.getParentClass(clazz);if(""===parentClazz.name)return model;const parentModel=this.fromClass(parentClazz,!0);return this.#merger.merge(model,parentModel)}fromObject(object,inherit=!0){const clazz=this.getClass(object);return this.fromClass(clazz,inherit)}fromFunction(funktion){const code=funktion.toString();return this.#parser.parseFunction(code)}createInstance(clazz,args=[]){return new clazz(...args)}getClass(object){return object.constructor}getParentClass(clazz){return Object.getPrototypeOf(clazz)}isClassObject(object){return this.isClass(object.constructor)}isFunctionObject(object){return this.isFunction(object.constructor)}isClass(clazz){return clazz.toString().startsWith("class")}isFunction(clazz){return clazz.toString().startsWith("function")||clazz.toString().startsWith("async function")}#reflectStatic(clazz){const code=clazz.toString();return this.#parser.parseClass(code)}#reflectDynamic(clazz){const object=this.createInstance(clazz),members=this.#getMembers(clazz,object),scope=new ESScope(members);return new ESClass(clazz.name,void 0,scope)}#getMembers(clazz,object){return[...this.#getDeclarations(object),...this.#getFunctions(clazz)]}#getDeclarations(object){const fieldNames=Object.getOwnPropertyNames(object),values=object,models=[];for(const fieldName of fieldNames){const content=values[fieldName],value=void 0!==content?new ESValue(String(content)):void 0,model=new ESDeclaration(fieldName,value);models.push(model)}return models}#getFunctions(clazz){const functionDescriptions=Object.getOwnPropertyDescriptors(clazz.prototype),models=[];for(const functionName in functionDescriptions){const description=functionDescriptions[functionName],funktion=description.value;if(funktion instanceof Function==!1)continue;const model=this.fromFunction(funktion);void 0!==description.get?models.push(new ESGetter(model.name,model.parameters,model.body,model.isStatic,model.isAsync,model.isPrivate)):void 0!==description.set?models.push(new ESSetter(model.name,model.parameters,model.body,model.isStatic,model.isAsync,model.isPrivate)):models.push(model)}return models}}let FileNotLoaded$1=class extends Error{constructor(filename,message){super(`Failed to load module file '${filename}' because of: ${message}`)}},LocationRewriter$1=class{#sourceFileManager;#parser=new Parser;#fileHelper=new FileHelper;constructor(sourceFileManager){this.#sourceFileManager=sourceFileManager}rewrite(filename,code){const replacedImports=this.#rewriteImports(filename,code);return this.#rewriteExports(filename,replacedImports)}#rewriteImports(filename,code){return code.replaceAll(Patterns_IMPORT,statement=>this.#replaceImport(filename,statement))}#rewriteExports(filename,code){return code.replaceAll(Patterns_EXPORT,statement=>this.#replaceExport(filename,statement))}#replaceImport(filename,statement){const dependency=this.#parser.parseImport(statement),from=this.#fileHelper.stripPath(dependency.from),normalizedFrom=this.#sourceFileManager.normalizeLocation(from);if(!1===this.#fileHelper.isApplicationModule(normalizedFrom))return statement;const rewrittenFrom=this.#rewriteFrom(filename,normalizedFrom);return statement.replace(from,rewrittenFrom)}#replaceExport(filename,statement){const dependency=this.#parser.parseExport(statement);if(void 0===dependency.from)return statement;const from=this.#fileHelper.stripPath(dependency.from),normalizedFrom=this.#sourceFileManager.normalizeLocation(from);if(!1===this.#fileHelper.isApplicationModule(normalizedFrom))return statement;const rewrittenFrom=this.#rewriteFrom(filename,normalizedFrom);return statement.replace(from,rewrittenFrom)}#rewriteFrom(filename,from){const callingModulePath=this.#fileHelper.extractPath(filename),translated=this.#fileHelper.makePathAbsolute(from,callingModulePath);return this.#sourceFileManager.isDirectory(translated)?`${from}/${Files_INDEX}`:this.#fileHelper.assureExtension(from)}},Reader$1=class{#sourceFileManager;#parser;#locationRewriter;constructor(sourceFileManager,parser=new Parser){this.#sourceFileManager=sourceFileManager,this.#parser=parser,this.#locationRewriter=new LocationRewriter$1(sourceFileManager)}async readAll(filenames){const modules=await Promise.all(filenames.map(filename=>this.read(filename)));return new Repository(modules)}async read(filename){const relativeLocation=this.#sourceFileManager.getRelativeLocation(filename),code=await this.#loadCode(filename),rewrittenCode=this.#locationRewriter.rewrite(relativeLocation,code),module=this.#parser.parse(rewrittenCode);return new Module$1(relativeLocation,rewrittenCode,module)}async#loadCode(filename){try{return(await this.#sourceFileManager.getContent(filename)).toString()}catch(error){const message=error instanceof Error?error.message:String(error);throw new FileNotLoaded$1(filename,message)}}};class Segment{#name;#modules=new Map;#classes=new Map;#procedures=new Map;constructor(name){this.#name=name}get name(){return this.#name}get modules(){return[...this.#modules.values()]}get classes(){return[...this.#classes.values()]}get procedures(){return[...this.#procedures.values()]}hasModule(filename){return this.#modules.has(filename)}getModule(filename){return this.#modules.get(filename)}getSegmentedModules(){return this.modules.filter(module=>module.segmented)}setModule(module){this.#modules.set(module.filename,module)}hasProcedure(fqn){return this.#procedures.has(fqn)}getProcedure(fqn){return this.#procedures.get(fqn)}setProcedure(procedure){this.#procedures.set(procedure.fqn,procedure)}setClass(klass){this.#classes.set(klass.fqn,klass)}}class Member{#id;#importKey;#fqn;constructor(id,importKey,fqn){this.#id=id,this.#importKey=importKey,this.#fqn=fqn}get id(){return this.#id}get importKey(){return this.#importKey}get fqn(){return this.#fqn}}class Class extends Member{#model;constructor(id,importKey,fqn,model){super(id,importKey,fqn),this.#model=model}get model(){return this.#model}}class Implementation extends Member{#access;#version;#model;constructor(id,importKey,fqn,access,version,model){super(id,importKey,fqn),this.#access=access,this.#version=version,this.#model=model}get access(){return this.#access}get version(){return this.#version}get model(){return this.#model}}class Module{#filename;#location;#imports;#members=[];#segmented;constructor(filename,location,imports,segmented){this.#filename=filename,this.#location=location,this.#imports=imports,this.#segmented=segmented}get filename(){return this.#filename}get location(){return this.#location}get imports(){return this.#imports}get members(){return this.#members}get segmented(){return this.#segmented}hasClasses(){return this.#members.some(member=>member instanceof Class)}getClasses(){return this.#members.filter(member=>member instanceof Class)}hasImplementations(){return this.#members.some(member=>member instanceof Implementation)}getImplementations(){return this.#members.filter(member=>member instanceof Implementation)}addMember(members){this.#members.push(members)}addImports(imports){const keys=Object.keys(imports);for(const key of keys)this.#imports[key]=imports[key]}}class Procedure{#fqn;#implementations=[];constructor(fqn,implementations=[]){this.#fqn=fqn,this.#implementations=implementations}get fqn(){return this.#fqn}get implementations(){return this.#implementations}addImplementation(implementation){this.#implementations.push(implementation)}}class Segmentation{#segments;constructor(segments){this.#segments=segments}get segments(){return this.#segments}getSegment(segmentName){return this.#segments.find(segment=>segment.name===segmentName)}isSegmentedModule(moduleFilename){return this.#segments.some(segment=>segment.hasModule(moduleFilename))}getSegments(moduleFilename){return this.#segments.filter(segment=>segment.hasModule(moduleFilename))}}class FunctionNotAsync extends Error{constructor(filename,functionName){super(`Function '${functionName}' from file '${filename}' is not async`)}}class InvalidFilename extends Error{constructor(filename){super(`Segment filename '${filename}' is invalid`)}}class FileNotLoaded extends Error{constructor(filename,message){super(`Failed to load segment file '${filename}' because of: ${message}`)}}class InvalidModuleExport extends Error{constructor(filename,exportKey){super(`The export '${exportKey}' from file '${filename}' is not a function or a class.`)}}class MissingModuleExport extends Error{constructor(filename,key){super(`Module '${filename}' does not export '${key}'`)}}class ModuleNotLoaded extends Error{constructor(filename){super(`Segmented module not found '${filename}'`)}}class MemberLocator{#repository;#fileHelper=new FileHelper;constructor(repository){this.#repository=repository}locate(filename,importKey){const trace=[];return{trace:trace,model:this.#locate(filename,importKey,trace)}}#locate(filename,importKey,trace){trace.push({filename:filename,importKey:importKey});const module=this.#getModule(filename);return this.#isReexported(module,importKey)?this.#relocate(module,importKey,trace):this.#extract(module,importKey)}#getModule(filename){const module=this.#repository.get(filename);if(void 0===module)throw new ModuleNotLoaded(filename);return module}#isReexported(module,importKey){const importItem=module.model.getImport(importKey),exportItem=module.model.getExport(importKey);return void 0!==importItem||void 0!==exportItem?.from}#relocate(module,importKey,trace){const relocateInfo=this.#getImportInfo(module,importKey)??this.#getExportInfo(module,importKey),relocatePath=relocateInfo?.from,relocateKey=relocateInfo?.name,callingModulePath=this.#fileHelper.extractPath(module.filename),relativeFrom=this.#fileHelper.stripPath(relocatePath),absoluteFrom=this.#fileHelper.makePathAbsolute(relativeFrom,callingModulePath);return this.#locate(absoluteFrom,relocateKey,trace)}#extract(module,importKey){const exportInfo=this.#getExportInfo(module,importKey);if(void 0===exportInfo)throw new MissingModuleExport(module.filename,importKey);const member=module.model.getMember(exportInfo.name);if(void 0===member)throw new MissingModuleExport(module.filename,exportInfo.name);return member}#getExportInfo(module,importKey){const exportItem=module.model.getExport(importKey),exportAlias=exportItem?.getMember(importKey);if(void 0!==exportAlias)return{from:exportItem?.from,name:exportAlias.name}}#getImportInfo(module,importKey){const importItem=module.model.getImport(importKey),importAlias=importItem?.getMember(importKey);if(void 0!==importItem&&void 0!==importAlias)return{from:importItem.from,name:importAlias.name}}}class SegmentReader{#segmentsFileManager;#sourceFileManager;#memberLocator;#fileHelper=new FileHelper;constructor(segmentsFileManager,sourceFileManager,repository){this.#segmentsFileManager=segmentsFileManager,this.#sourceFileManager=sourceFileManager,this.#memberLocator=new MemberLocator(repository)}async readAll(filenames){const segments=await Promise.all(filenames.map(filename=>this.#read(filename)));return new Segmentation(segments)}async#read(filename){const definition=await this.#loadSegmentDefinition(filename),name=this.#extractSegmentName(filename),segment=new Segment(name);return this.#registerModules(segment,definition),this.#registerMembers(segment),segment}#extractSegmentName(filename){const file=filename.split("/").pop();if(void 0===file||""===file)throw new InvalidFilename(filename);return file.replace(Files_JSON,"")}async#loadSegmentDefinition(filename){try{const content=await this.#segmentsFileManager.getContent(filename);return JSON.parse(content.toString())}catch(error){const message=error instanceof Error?error.message:String(error);throw new FileNotLoaded(filename,message)}}#registerModules(segment,definition){for(const[filename,moduleImports]of Object.entries(definition))this.#createModule(segment,filename,moduleImports,!0)}#createModule(segment,filename,moduleImports,segmented){const moduleFilename=this.#makeModuleFilename(filename),location=this.#fileHelper.extractPath(moduleFilename),module=segment.hasModule(moduleFilename)?segment.getModule(moduleFilename):new Module(moduleFilename,location,{},segmented);module.addImports(moduleImports),segment.setModule(module)}#makeModuleFilename(filename){const normalizedFilename=this.#sourceFileManager.normalizeLocation(filename),fullFilename=this.#sourceFileManager.isDirectory(normalizedFilename)?`${normalizedFilename}/${Files_INDEX}`:this.#fileHelper.assureExtension(normalizedFilename);return fullFilename.startsWith("./")?fullFilename.substring(2):fullFilename.startsWith("/")?fullFilename.substring(1):fullFilename}#registerMembers(segment){const idGenerator=new IdGenerator$1;for(const module of segment.modules)for(const importKey in module.imports)this.#registerModuleMember(segment,module,importKey,idGenerator)}#registerModuleMember(segment,module,importKey,idGenerator){const{model:model,trace:trace}=this.#memberLocator.locate(module.filename,importKey),properties=this.#constructProperties(module,model,importKey,idGenerator);this.#createMember(segment,module,model,properties),trace.shift();for(const entry of trace){const entryImport={[entry.importKey]:{access:properties.access}};this.#createModule(segment,entry.filename,entryImport,!1)}}#constructProperties(module,model,importKey,idGenerator){const configuration=module.imports[importKey],id=idGenerator.next(),name=configuration.as??model.name;return{id:id,importKey:importKey,name:name,access:configuration.access??Defaults$1_ACCESS_LEVEL,version:configuration.version??Defaults$1_VERSION_NUMBER,fqn:this.#constructFqn(module,name,importKey)}}#constructFqn(module,name,importKey){return this.#isRootModule(module)?name:this.#isIndexModule(module)&&this.#isDefaultImport(importKey)?module.location:`${module.location}/${name}`}#isRootModule(module){return""===module.location}#isIndexModule(module){return module.filename.endsWith(Files_INDEX)}#isDefaultImport(importKey){return importKey===Keywords$1_DEFAULT}#createMember(segment,module,model,properties){if(model instanceof ESClass)return this.#createClass(segment,module,model,properties);if(model instanceof ESFunction)return this.#createImplementation(segment,module,model,properties);throw new InvalidModuleExport(module.filename,properties.importKey)}#createClass(segment,module,model,properties){const clazz=new Class(properties.id,properties.importKey,properties.fqn,model);module.addMember(clazz),segment.setClass(clazz)}#createImplementation(segment,module,model,properties){if(!1===model.isAsync)throw new FunctionNotAsync(module.filename,properties.name);const implementation=new Implementation(properties.id,properties.importKey,properties.fqn,properties.access,properties.version,model);this.#createProcedure(segment,module,implementation)}#createProcedure(segment,module,implementation){const procedure=segment.hasProcedure(implementation.fqn)?segment.getProcedure(implementation.fqn):new Procedure(implementation.fqn);procedure.addImplementation(implementation),module.addMember(implementation),segment.setProcedure(procedure)}}class Reader{#projectFileManager;constructor(projectFileManager){this.#projectFileManager=projectFileManager}async read(moduleFiles,resourceFiles,segmentFiles){const sourceFileManager=this.#projectFileManager.source,resourceFileManager=this.#projectFileManager.resource,segmentFileManager=this.#projectFileManager.segment,moduleReader=new Reader$1(sourceFileManager),repository=await moduleReader.readAll(moduleFiles),resourceReader=new ResourceReader(resourceFileManager,sourceFileManager),resources=await resourceReader.readAll(resourceFiles),segmentReader=new SegmentReader(segmentFileManager,sourceFileManager,repository),segmentation=await segmentReader.readAll(segmentFiles);return new Application$1(repository,resources,segmentation)}}class LocationRewriter{#module;#resources;#segmentation;#segment;#parser=new Parser;#fileHelper=new FileHelper;constructor(module,resources,segmentation,segment){this.#module=module,this.#resources=resources,this.#segmentation=segmentation,this.#segment=segment}get parser(){return this.#parser}rewrite(code){return code.replaceAll(this.replacementPattern,statement=>this.replaceStatement(statement))}replaceStatement(statement){const dependency=this.parseStatement(statement);return void 0===dependency.from?statement:this.#isApplicationModule(dependency)?this.#rewriteForApplication(dependency):this.#rewriteForRuntime(dependency)}#rewriteForApplication(dependency){const targetModuleFilename=this.#getTargetModuleFilename(dependency);return this.#resources.isResourceModule(targetModuleFilename)?this.#rewriteToResource(targetModuleFilename,dependency):this.#rewriteModule(targetModuleFilename,dependency)}#rewriteForRuntime(dependency){const from=this.#rewriteRuntimeFrom(dependency),keys=dependency.members.map(member=>member.name);return this.includeInBundle(dependency,from,keys)}#rewriteModule(targetModuleFilename,dependency){if(0===dependency.members.length)return this.#rewriteToCommon(targetModuleFilename,dependency,[]);const{segmentKeys:segmentKeys,remoteKeys:remoteKeys,commonKeys:commonKeys}=this.#getModuleImportKeys(targetModuleFilename,dependency),imports=[];return segmentKeys.length>0&&imports.push(this.#rewriteToSegment(targetModuleFilename,dependency,segmentKeys)),remoteKeys.length>0&&imports.push(this.#rewriteToRemote(targetModuleFilename,dependency,remoteKeys)),commonKeys.length>0&&imports.push(this.#rewriteToCommon(targetModuleFilename,dependency,commonKeys)),imports.filter(item=>item.length>0).join("\n")}#rewriteToResource(targetModuleFilename,dependency){const from=this.#rewriteApplicationFrom(targetModuleFilename);return this.excludeFromBundle(dependency,from)}#rewriteToSegment(targetModuleFilename,dependency,keys){const from=this.#rewriteApplicationFrom(targetModuleFilename,this.#segment.name);return this.includeInBundle(dependency,from,keys)}#rewriteToRemote(targetModuleFilename,dependency,keys){const from=this.#rewriteApplicationFrom(targetModuleFilename,"remote");return this.includeInBundle(dependency,from,keys)}#rewriteToCommon(targetModuleFilename,dependency,keys){const from=this.#rewriteApplicationFrom(targetModuleFilename);return this.includeInBundle(dependency,from,keys)}#isApplicationModule(dependency){const from=this.#fileHelper.stripPath(dependency.from);return this.#fileHelper.isApplicationModule(from)}#getTargetModuleFilename(dependency){const from=this.#fileHelper.stripPath(dependency.from),callingModulePath=this.#fileHelper.extractPath(this.#module.filename);return this.#fileHelper.makePathAbsolute(from,callingModulePath)}#getModuleImportKeys(targetModuleFilename,dependency){const moduleSegmentKeys=this.#getSegmentImportKeys(targetModuleFilename,this.#segment),moduleRemoteKeys=this.#getRemoteImportKeys(targetModuleFilename,moduleSegmentKeys),segmentKeys=this.#filterMemberKeys(dependency,moduleSegmentKeys),remoteKeys=this.#filterMemberKeys(dependency,moduleRemoteKeys);return{segmentKeys:segmentKeys,remoteKeys:remoteKeys,commonKeys:this.#extractUnsegmentedImportKeys(dependency,[...segmentKeys,...remoteKeys])}}#getSegmentImportKeys(targetModuleFilename,segment){if(void 0===segment)return[];const module=segment.getModule(targetModuleFilename);return void 0!==module?Object.keys(module.imports):[]}#getRemoteImportKeys(targetModuleFilename,segmentKeys){const importKeys=this.#segmentation.getSegments(targetModuleFilename).filter(segment=>segment!==this.#segment).map(segment=>this.#getSegmentImportKeys(targetModuleFilename,segment)).flat();return[...new Set(importKeys)].filter(key=>!1===segmentKeys.includes(key))}#filterMemberKeys(dependency,keys){return dependency.members.filter(member=>keys.includes(member.name)).map(member=>member.name)}#extractUnsegmentedImportKeys(dependency,segmentedKeys){return dependency.members.filter(member=>!1===segmentedKeys.includes(member.name)).map(member=>member.name)}#rewriteApplicationFrom(filename,scope){const callingModulePath=this.#fileHelper.extractPath(this.#module.filename),relativeFilename=this.#fileHelper.makePathRelative(filename,callingModulePath);return void 0!==scope?this.#fileHelper.addSubExtension(relativeFilename,scope):relativeFilename}#rewriteRuntimeFrom(dependency){return this.#fileHelper.stripPath(dependency.from)}}class ImportRewriter extends LocationRewriter{get replacementPattern(){return Patterns_IMPORT}parseStatement(statement){return this.parser.parseImport(statement)}includeInBundle(dependency,from,keys){if(0===dependency.members.length)return`import "${from}";`;return`import ${this.#rewriteStaticImportMembers(dependency,keys)} from "${from}";`}excludeFromBundle(dependency,from){if(0===dependency.members.length)return`await import("${from}");`;return`const ${this.#rewriteDynamicImportMembers(dependency)} = await import("${from}");`}#rewriteStaticImportMembers(dependency,keys){const members=dependency.members.filter(member=>keys.includes(member.name)),defaultMember=members.find(member=>member.name===Keywords$1_DEFAULT),hasDefaultMember=void 0!==defaultMember,defaultMemberImport=hasDefaultMember?defaultMember.as:"",namedMemberImports=members.filter(member=>member.name!==Keywords$1_DEFAULT).map(member=>member.name!==member.as?`${member.name} as ${member.as}`:member.name),hasNamedMembers=namedMemberImports.length>0;return`${defaultMemberImport}${hasDefaultMember&&hasNamedMembers?", ":""}${hasNamedMembers?`{ ${namedMemberImports.join(", ")} }`:""}`}#rewriteDynamicImportMembers(dependency){if(this.#doesImportAll(dependency))return dependency.members[0].as;return`{ ${dependency.members.map(member=>member.name!==member.as?`${member.name} : ${member.as}`:member.name).join(", ")} }`}#doesImportAll(dependency){return 1===dependency.members.length&&dependency.members[0].name===Values_ASTERISK}}class ExportRewriter extends LocationRewriter{get replacementPattern(){return Patterns_EXPORT}parseStatement(statement){return this.parser.parseExport(statement)}includeInBundle(dependency,from,keys){if(0===dependency.members.length)return`export "${from}";`;return`export ${this.#rewriteStaticExportMembers(dependency,keys)} from "${from}";`}excludeFromBundle(dependency,from){const keys=dependency.members.map(member=>member.name);return this.includeInBundle(dependency,from,keys)}#rewriteStaticExportMembers(dependency,keys){const members=dependency.members.filter(member=>keys.includes(member.name));if(1===members.length&&""===members[0].name){const member=members[0];return member.name!==member.as?`${Values_ASTERISK} as ${member.as}`:Values_ASTERISK}return`{ ${members.map(member=>member.name!==member.as?`${member.name} as ${member.as}`:member.name).join(", ")} }`}}class LocalBuilder{build(module,resources,segmentation,segment){const importRewriter=new ImportRewriter(module,resources,segmentation,segment),exportRewriter=new ExportRewriter(module,resources,segmentation,segment),importCode=importRewriter.rewrite(module.code);return exportRewriter.rewrite(importCode)}}const RunModes_NORMAL="normal",RunModes_DRY="dry",StatusCodes$1_OK=200,StatusCodes$1_BAD_REQUEST=400,StatusCodes$1_UNAUTHORIZED=401,StatusCodes$1_PAYMENT_REQUIRED=402,StatusCodes$1_FORBIDDEN=403,StatusCodes$1_NOT_FOUND=404,StatusCodes$1_TEAPOT=418,StatusCodes$1_SERVER_ERROR=500,StatusCodes$1_NOT_IMPLEMENTED=501;class InvalidVersionNumber extends BadRequest{#number;constructor(number){super(`Invalid version number '${number}'`),this.#number=number}get number(){return this.#number}}class ProcedureNotFound extends NotFound{#fqn;constructor(fqn){super(`Procedure '${fqn}' not found`),this.#fqn=fqn}get fqn(){return this.#fqn}}class ProcedureNotAccessible extends Forbidden{constructor(fqn,versionNumber){super(`Procedure '${fqn}' (v${versionNumber}) is not accessible`)}}class UnknownParameter extends BadRequest{#parameterName;constructor(parameterName){super(`Unknown parameter ${parameterName}`),this.#parameterName=parameterName}get parameterName(){return this.#parameterName}}class MissingParameterValue extends BadRequest{#parameterName;constructor(parameterName){super(`Missing value for parameter '${parameterName}'`),this.#parameterName=parameterName}get parameterName(){return this.#parameterName}}class InvalidParameterValue extends BadRequest{#parameterName;constructor(parameterName){super(`Invalid value for parameter '${parameterName}'`),this.#parameterName=parameterName}get parameterName(){return this.#parameterName}}class ArgumentExtractor{extract(parameters,args){const argsCopy=this.#copyArguments(parameters,args),values=[];for(const parameter of parameters){const value=this.#extractArgumentValue(parameter,argsCopy);values.push(value)}if(argsCopy.size>0){const name=argsCopy.keys().next().value;throw new UnknownParameter(name)}return values}#copyArguments(parameters,args){const copy=new Map;for(const[key,value]of args){if(this.#isOptionalArgument(key)){const name=this.#getParameterName(key);!0===this.#containsParameter(parameters,name)&©.set(name,value);continue}copy.set(key,value)}return copy}#isOptionalArgument(argument){return argument.startsWith("*")}#getParameterName(argument){return argument.substring(1)}#containsParameter(parameters,name){return void 0!==parameters.find(parameter=>parameter.name===name)}#extractArgumentValue(parameter,args,parent){return parameter instanceof NamedParameter?this.#extractNamedArgumentValue(parameter,args,parent):this.#extractDestructedArgumentValue(parameter,args)}#extractNamedArgumentValue(parameter,args,parent){const value=args.get(parameter.name);if(this.#isMissingParameterValue(parameter,value,parent))throw new MissingParameterValue(parameter.name);if(this.#isInvalidRestParameter(parameter,value,parent))throw new InvalidParameterValue(parameter.name);return args.delete(parameter.name),value}#extractDestructedArgumentValue(parameter,args){return parameter instanceof ArrayParameter?this.#extractArrayArgumentValue(parameter,args):this.#extractObjectArgumentValue(parameter,args)}#extractArrayArgumentValue(parameter,args){const values=this.#extractVariableValues(parameter,args);return void 0!==values?Object.values(values):void 0}#extractObjectArgumentValue(parameter,args){return this.#extractVariableValues(parameter,args)}#extractVariableValues(parameter,args){const useIndex=parameter instanceof ArrayParameter,values={},missingValues=[];let containsValues=!1,index=0;for(const variable of parameter.variables){const key=useIndex?index++:variable.name,value=this.#extractArgumentValue(variable,args,parameter);void 0!==value?containsValues=!0:!1===variable.isOptional&&missingValues.push(variable.name),values[key]=value}if(!0===containsValues&&missingValues.length>0)throw new MissingParameterValue(missingValues[0]);return containsValues?values:void 0}#isMissingParameterValue(parameter,value,parent){return void 0===value&&(!0!==parameter.isOptional&&!0!==parent?.isOptional)}#isInvalidRestParameter(parameter,value,parent){return!1!==parameter.name.startsWith("...")&&(void 0===parent&&value instanceof Array==!1||parent instanceof ArrayParameter&&value instanceof Array==!1||parent instanceof ObjectParameter&&value instanceof Object==!1)}}class ErrorConverter{toStatus(error){return error instanceof BadRequest?StatusCodes$1_BAD_REQUEST:error instanceof Forbidden?StatusCodes$1_FORBIDDEN:error instanceof NotFound?StatusCodes$1_NOT_FOUND:error instanceof NotImplemented?StatusCodes$1_NOT_IMPLEMENTED:error instanceof PaymentRequired?StatusCodes$1_PAYMENT_REQUIRED:error instanceof Teapot?StatusCodes$1_TEAPOT:error instanceof Unauthorized?StatusCodes$1_UNAUTHORIZED:StatusCodes$1_SERVER_ERROR}fromStatus(status,message){switch(status){case StatusCodes$1_BAD_REQUEST:return new BadRequest(message);case StatusCodes$1_FORBIDDEN:return new Forbidden(message);case StatusCodes$1_NOT_FOUND:return new NotFound(message);case StatusCodes$1_NOT_IMPLEMENTED:return new NotImplemented(message);case StatusCodes$1_PAYMENT_REQUIRED:return new PaymentRequired(message);case StatusCodes$1_TEAPOT:return new Teapot(message);case StatusCodes$1_UNAUTHORIZED:return new Unauthorized(message);default:return new ServerError(message)}}}const VERSION_EXPRESSION=/^\d+(?:\.\d+){0,2}$/;class VersionParser{parse(number){if(0===number.trim().length)return Version.DEFAULT;if(!1===VERSION_EXPRESSION.test(number))throw new InvalidVersionNumber(number);const parts=number.split(".");switch(parts.length){case 1:return new Version(Number.parseInt(parts[0]));case 2:return new Version(Number.parseInt(parts[0]),Number.parseInt(parts[1]));default:return new Version(Number.parseInt(parts[0]),Number.parseInt(parts[1]),Number.parseInt(parts[2]))}}}class ImplementationNotFound extends NotFound{#fqn;#version;constructor(fqn,version){super(`No implementation found for procedure '${fqn}' with version '${version}'`),this.#fqn=fqn,this.#version=version}get fqn(){return this.#fqn}get version(){return this.#version}}class InvalidSegment extends ServerError{constructor(){super("Invalid segment")}}class Application{#segments=new Map;addSegment(segment){this.#segments.set(segment.id,segment)}clearSegments(){this.#segments.clear()}getClassNames(){const names=new Set;for(const segment of this.#segments.values()){segment.getClasses().forEach(clazz=>names.add(clazz.fqn))}return[...names.values()]}hasClass(fqn){return this.getClassNames().includes(fqn)}getClass(fqn){for(const segment of this.#segments.values())if(segment.hasClass(fqn))return segment.getClass(fqn)}getClassByImplementation(implementation){for(const segment of this.#segments.values()){const clazz=segment.getClassByImplementation(implementation);if(void 0!==clazz)return clazz}}getProcedureNames(){const names=new Set;for(const segment of this.#segments.values()){segment.getExposedProcedures().forEach(procedure=>names.add(procedure.fqn))}return[...names.values()]}hasProcedure(fqn){return this.getProcedureNames().includes(fqn)}getProcedure(fqn){for(const segment of this.#segments.values())if(segment.hasProcedure(fqn))return segment.getProcedure(fqn)}}class ExecutionManager{#moduleImporter;#segmentFiles;#argumentConstructor=new ArgumentExtractor;#errorConverter=new ErrorConverter;#application=new Application;constructor(moduleImporter,segmentFiles=[]){this.#moduleImporter=moduleImporter,this.#segmentFiles=segmentFiles}async start(){return this.#loadSegments()}async stop(){return this.#clearSegments()}async loadSegment(filename){const module=await this.#moduleImporter.import(filename);this.addSegment(module.default)}async addSegment(segment){if(segment instanceof Segment$1==!1)throw new InvalidSegment;this.#application.addSegment(segment)}getClassNames(){return this.#application.getClassNames()}hasClass(fqn){return this.#application.hasClass(fqn)}getClass(fqn){return this.#application.getClass(fqn)}getClassByImplementation(implementation){return this.#application.getClassByImplementation(implementation)}getProcedureNames(){return this.#application.getProcedureNames()}hasProcedure(fqn){return this.#application.hasProcedure(fqn)}getProcedure(fqn){return this.#application.getProcedure(fqn)}async run(request){const implementation=this.#getImplementation(request.fqn,request.version),args=this.#argumentConstructor.extract(implementation.parameters,request.args);return request.mode===RunModes_DRY?new Response(StatusCodes$1_OK,void 0):this.#runImplementation(request,implementation,args)}async#loadSegments(){await Promise.all(this.#segmentFiles.map(filename=>this.loadSegment(filename)))}#clearSegments(){this.#application.clearSegments()}#getImplementation(fqn,version){const procedure=this.#application.getProcedure(fqn);if(void 0===procedure)throw new ProcedureNotFound(fqn);const implementation=procedure.getImplementation(version);if(void 0===implementation)throw new ImplementationNotFound(procedure.fqn,version.toString());return implementation}async#runImplementation(request,implementation,args){try{const result=await implementation.executable.call(request,...args);return new Response(StatusCodes$1_OK,result)}catch(error){const status=this.#errorConverter.toStatus(error);return new Response(status,error)}}}class RemoteBuilder{build(implementations){let code="";for(const implementation of implementations)code+=implementation.access===AccessLevels.PRIVATE?this.#createPrivateCode(implementation):this.#createPublicCode(implementation);return code.trim()}#createPrivateCode(implementation){const fqn=implementation.fqn,version=implementation.version,declaration=this.#createDeclaration(implementation),body=`throw new ProcedureNotAccessible('${fqn}', '${version}');`;return this.#createFunction(declaration,body)}#createPublicCode(implementation){const fqn=implementation.fqn,version=implementation.version,args=this.#createArguments(implementation.model.parameters),declaration=this.#createDeclaration(implementation),body=`return __run('${fqn}', '${version}', { ${args} }, this);`;return this.#createFunction(declaration,body)}#createParameters(parameters){const result=[];for(const parameter of parameters)parameter instanceof ESField?result.push(parameter.name):(parameter instanceof ESDestructuredArray||parameter instanceof ESDestructuredObject)&&result.push(parameter.toString());return result.join(", ")}#createArguments(parameters){return this.#extractArguments(parameters).join(", ")}#extractArguments(parameters){const result=[];for(const parameter of parameters)if(parameter instanceof ESDestructuredValue){const argumentz=this.#extractArguments(parameter.members);result.push(...argumentz)}else if(parameter instanceof ESField){const argument=this.#createNamedArgument(parameter);result.push(argument)}return result}#createNamedArgument(parameter){const key=parameter.name,value=key.startsWith("...")?key.substring(3):key;return`'${key}': ${value}`}#createDeclaration(implementation){const name=implementation.model.name,parameters=this.#createParameters(implementation.model.parameters);return`\nexport ${implementation.importKey===Keywords$1_DEFAULT?`${Keywords$1_DEFAULT} `:""}async function ${name}(${parameters})`}#createFunction(declaration,body){return`${declaration} {\n\t${body}\n}\n`}}let Builder$2=class{#targetFileManager;#localBuilder=new LocalBuilder;#remoteBuilder=new RemoteBuilder;#fileHelper=new FileHelper;constructor(targetFileManager){this.#targetFileManager=targetFileManager}async build(application){const repository=application.repository,segmentation=application.segmentation,resources=application.resources,builds=repository.modules.map(module=>this.#buildModule(module,resources,segmentation));await Promise.all(builds)}async#buildModule(module,resources,segmentation){const moduleSegments=segmentation.getSegments(module.filename);if(0===moduleSegments.length)return this.#buildCommonModule(module,resources,segmentation);const segmentBuilds=moduleSegments.map(segment=>this.#buildSegmentModule(module,resources,segment,segmentation)),remoteBuild=moduleSegments[0].getModule(module.filename).hasImplementations()?this.#buildRemoteModule(module,moduleSegments):Promise.resolve();await Promise.all([...segmentBuilds,remoteBuild]),this.#targetFileManager.delete(module.filename)}async#buildCommonModule(module,resources,segmentation){const filename=module.filename,code=this.#localBuilder.build(module,resources,segmentation);return this.#targetFileManager.write(filename,code)}async#buildSegmentModule(module,resources,segment,segmentation){const filename=this.#fileHelper.addSubExtension(module.filename,segment.name),code=this.#localBuilder.build(module,resources,segmentation,segment);return this.#targetFileManager.write(filename,code)}async#buildRemoteModule(module,segments){const implementations=this.#getImplementations(module,segments),filename=this.#fileHelper.addSubExtension(module.filename,"remote"),code=this.#remoteBuilder.build(implementations);return this.#targetFileManager.write(filename,code)}#getImplementations(module,segments){const implementations=segments.map(segment=>segment.getModule(module.filename)).flatMap(segmentModule=>segmentModule.getImplementations()),unique=new Map;for(const implementation of implementations){const key=`${implementation.fqn}:${implementation.version.toString()}`;unique.set(key,implementation)}return[...unique.values()]}};let Builder$1=class{#targetFileManager;#logger;#fileHelper=new FileHelper;#versionParser=new VersionParser;constructor(targetFileManager,logger){this.#targetFileManager=targetFileManager,this.#logger=logger}async build(application){const builds=application.segmentation.segments.map(segment=>this.#buildSegment(segment));await Promise.all(builds)}async#buildSegment(segment){const filename=`${segment.name}.segment.js`,code=this.#createCode(segment);await this.#targetFileManager.write(filename,code),this.#logger.info(`Built ${segment.name} segment (${segment.modules.length} modules, ${segment.procedures.length} procedures, ${segment.classes.length} classes)`)}#createCode(segment){return`${this.#createImportCode(segment)}\n${this.#createSegmentCode(segment)}`}#createImportCode(segment){return`import { Segment, Class, Procedure, Implementation, Version, NamedParameter, ArrayParameter, ObjectParameter } from "jitar";\n${this.#createModuleImports(segment)}`}#createModuleImports(segment){const imports=[];for(const module of segment.getSegmentedModules()){if(0===module.members.length)continue;const filename=this.#fileHelper.addSubExtension(module.filename,segment.name),importRule=`import ${this.#createModuleImportMembers(module)} from "./${filename}";`;imports.push(importRule)}return imports.join("\n")}#createModuleImportMembers(module){const members=module.members,defaultImplementation=members.find(member=>member.importKey===Keywords$1_DEFAULT),hasDefaultImplementation=void 0!==defaultImplementation,defaultMemberImport=hasDefaultImplementation?defaultImplementation.id:"",namedImplementations=members.filter(member=>member.importKey!==Keywords$1_DEFAULT),nameImplementationImports=namedImplementations.map(member=>`${member.importKey} as ${member.id}`),hasNamedImplementations=namedImplementations.length>0;return`${defaultMemberImport}${hasDefaultImplementation&&hasNamedImplementations?", ":""}${hasNamedImplementations?`{ ${nameImplementationImports.join(", ")} }`:""}`}#createSegmentCode(segment){const lines=[];lines.push(`export default new Segment("${segment.name}")`);for(const clazz of segment.classes)lines.push(`\t.addClass(new Class("${clazz.fqn}", ${clazz.id}))`);for(const procedure of segment.procedures){lines.push(`\t.addProcedure(new Procedure("${procedure.fqn}")`);for(const implementation of procedure.implementations){const version=this.#createVersionCode(implementation.version),parameters=this.#createParametersCode(implementation.model);lines.push(`\t\t.addImplementation(new Implementation(${version}, "${implementation.access}", ${parameters}, ${implementation.id}))`)}lines.push("\t)")}return lines.join("\n")}#createVersionCode(versionString){const version=this.#versionParser.parse(versionString);return`new Version(${version.major}, ${version.minor}, ${version.patch})`}#createParametersCode(model){return`[${this.#extractParameters(model.parameters).join(", ")}]`}#extractParameters(parameters){const result=[];for(const parameter of parameters)result.push(this.#extractParameter(parameter));return result}#extractParameter(parameter){return parameter instanceof ESDestructuredArray?this.#createArrayParameter(parameter):parameter instanceof ESDestructuredObject?this.#createObjectParameter(parameter):this.#createNamedParameter(parameter)}#createNamedParameter(parameter){return`new NamedParameter("${parameter.name}", ${void 0!==parameter.value})`}#createArrayParameter(parameter){return`new ArrayParameter([${this.#extractParameters(parameter.members).join(", ")}])`}#createObjectParameter(parameter){return`new ObjectParameter([${this.#extractParameters(parameter.members).join(", ")}])`}};class Builder{#moduleBuilder;#segmentBuilder;constructor(projectFileManager,logger){const targetFileManager=projectFileManager.target;this.#moduleBuilder=new Builder$2(targetFileManager),this.#segmentBuilder=new Builder$1(targetFileManager,logger)}async build(application){await Promise.all([this.#moduleBuilder.build(application),this.#segmentBuilder.build(application)])}}class ProjectFileManager{#source;#target;#resource;#segment;constructor(source,target,resource,segment){this.#source=source,this.#target=target,this.#resource=resource,this.#segment=segment}get source(){return this.#source}get target(){return this.#target}get resource(){return this.#resource}get segment(){return this.#segment}}class BuildManager{#logger;#fileManager;#applicationReader;#applicationBuilder;constructor(configuration,logLevel){this.#logger=new Logger(logLevel);const sourceFileManager=new LocalFileManager(configuration.source),targetFileManager=new LocalFileManager(configuration.target),resourceFileManager=new LocalFileManager(configuration.resources),segmentFileManager=new LocalFileManager(configuration.segments);this.#fileManager=new ProjectFileManager(sourceFileManager,targetFileManager,resourceFileManager,segmentFileManager),this.#applicationReader=new Reader(this.#fileManager),this.#applicationBuilder=new Builder(this.#fileManager,this.#logger)}async build(){const sourceFileManager=this.#fileManager.source,resourceFileManager=this.#fileManager.resource,segmentFileManager=this.#fileManager.segment,moduleFiles=await sourceFileManager.filter(Files$1_MODULE_PATTERN),resourceFiles=await resourceFileManager.filter(Files$1_RESOURCE_PATTERN),segmentFiles=await segmentFileManager.filter(Files$1_SEGMENT_PATTERN),applicationModel=await this.#applicationReader.read(moduleFiles,resourceFiles,segmentFiles);return this.#applicationBuilder.build(applicationModel)}}class Validator{#strict;constructor(strict=!0){this.#strict=strict}validate(data,scheme){const errors=[];this.#validateData("",data,scheme,errors);return{valid:0===errors.length,errors:errors}}#validateData(key,data,scheme,errors){this.#strict&&this.#validateKeys(key,data,scheme,errors),this.#validateValues(key,data,scheme,errors)}#validateKeys(key,data,scheme,errors){const dataKeys=Object.keys(data),schemeKeys=Object.keys(scheme);for(const dataKey of dataKeys)if(!1===schemeKeys.includes(dataKey)){const absoluteKey=this.#composeKey(key,dataKey);errors.push(`Unknown field '${absoluteKey}'`)}}#validateValues(key,data,scheme,errors){const valueKeys=Object.keys(scheme);for(const valueKey of valueKeys){const absoluteKey=this.#composeKey(key,valueKey),fieldScheme=scheme[valueKey],value=data[valueKey];this.#validateValue(absoluteKey,value,fieldScheme,errors)}}#validateValue(key,value,scheme,errors){if(void 0!==value)switch(scheme.type){case"string":return this.#validateString(key,value,scheme,errors);case"integer":return this.#validateInteger(key,value,scheme,errors);case"real":return this.#validateReal(key,value,scheme,errors);case"boolean":return this.#validateBoolean(key,value,scheme,errors);case"url":return this.#validateUrl(key,value,scheme,errors);case"group":return this.#validateGroup(key,value,scheme,errors);case"list":return this.#validateList(key,value,scheme,errors)}else!0===scheme.required&&errors.push(`Field '${key}' is required`)}#validateString(key,value,scheme,errors){"string"!=typeof value&&errors.push(`Field '${key}' is not a string`)}#validateInteger(key,value,scheme,errors){"number"==typeof value&&!1!==Number.isInteger(value)||errors.push(`Field '${key}' is not an integer`)}#validateReal(key,value,scheme,errors){"number"!=typeof value&&errors.push(`Field '${key}' is not a real number`)}#validateBoolean(key,value,scheme,errors){"boolean"!=typeof value&&errors.push(`Field '${key}' is not a boolean`)}#validateUrl(key,value,scheme,errors){"string"==typeof value&&!1!==value.startsWith("http")||errors.push(`Field '${key}' is not a valid URL`)}#validateGroup(key,value,scheme,errors){"object"==typeof value?this.#validateData(key,value,scheme.fields,errors):errors.push(`Field '${key}' is not an object`)}#validateList(key,value,scheme,errors){if(!Array.isArray(value))return void errors.push(`Field '${key}' is not a list`);const data=value;for(const itemIndex in data){const itemKey=this.#composeKey(key,itemIndex),itemValue=data[itemIndex];this.#validateValue(itemKey,itemValue,scheme.items,errors)}}#composeKey(parent,key){return""===parent?key:`${parent}.${key}`}}class Configurator{async configure(filename){dotenv.config({path:filename})}}class RuntimeConfigurationInvalid extends Error{constructor(validation){super(`Runtime configuration is invalid:\n${validation.errors.join("\n")}`)}}const DefaultValues_FILENAME="./jitar.json",DefaultValues_SOURCE="./src",DefaultValues_TARGET="./dist",DefaultValues_SEGMENTS="./segments",DefaultValues_RESOURCES="./resources",validationScheme$7={source:{type:"string",required:!1},target:{type:"string",required:!1},segments:{type:"string",required:!1},resources:{type:"string",required:!1}};let ConfigurationBuilder$1=class{#reader;#validator;constructor(reader,validator){this.#reader=reader,this.#validator=validator}async build(filename=DefaultValues_FILENAME){const configuration=await this.#reader.read(filename),validation=this.#validator.validate(configuration,validationScheme$7);if(!1===validation.valid)throw new RuntimeConfigurationInvalid(validation);return configuration.source??=DefaultValues_SOURCE,configuration.target??=DefaultValues_TARGET,configuration.segments??=DefaultValues_SEGMENTS,configuration.resources??=DefaultValues_RESOURCES,configuration}};class ServerConfigurationInvalid extends Error{constructor(validation){super(`Invalid server configuration:\n => ${validation.errors.join("\n => ")}`)}}const validationScheme={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:{indexFilename:{type:"string",required:!1},serveIndexOnNotFound:{type:"boolean",required:!1},assets:{type:"list",required:!1,items:{type:"string"}}}},standalone:{type:"group",required:!1,fields:{segments:{type:"list",required:!0,items:{type:"string"}},indexFilename:{type:"string",required:!1},serveIndexOnNotFound:{type:"boolean",required:!1},assets:{type:"list",required:!1,items:{type:"string"}}}},worker:{type:"group",required:!1,fields:{gateway:{type:"url",required:!1},segments:{type:"list",required:!0,items:{type:"string"}},trustKey:{type:"string",required:!1},reportInterval:{type:"integer",required:!1}}},remoteWorker:{type:"group",required:!1,fields:{unavailableThreshold:{type:"integer",required:!1},stoppedThreshold:{type:"integer",required:!1}}}};class ConfigurationBuilder{#reader;#validator;constructor(reader,validator){this.#reader=reader,this.#validator=validator}async build(filename){const configuration=await this.#reader.read(filename),validation=this.#validator.validate(configuration,validationScheme);if(!1===validation.valid)throw new ServerConfigurationInvalid(validation);return configuration}}class InvalidConfigurationFile extends Error{constructor(filename){super(`${filename} is not a valid configuration file.`)}}const ENVIRONMENT_VARIABLE_REGEX=/\${([^}]*)}/g;class ConfigurationReader{#fileManager;constructor(fileManager){this.#fileManager=fileManager}async read(filename){if(!1===await this.#fileManager.exists(filename))return{};const file=await this.#fileManager.read(filename);if(!1===file.type.includes("json"))throw new InvalidConfigurationFile(filename);const content=file.content.toString(),configuration=this.#replaceEnvironmentVariables(content);return this.#parseJson(configuration)}#replaceEnvironmentVariables(content){return content.replace(ENVIRONMENT_VARIABLE_REGEX,(match,key)=>process.env[key]??"null")}#parseJson(configuration){return JSON.parse(configuration)}}class ConfigurationManager{#environmentConfigurator;#runtimeConfigurationBuilder;#serverConfigurationBuilder;constructor(rootPath="./"){const fileManager=new LocalFileManager(rootPath),reader=new ConfigurationReader(fileManager),validator=new Validator;this.#environmentConfigurator=new Configurator,this.#runtimeConfigurationBuilder=new ConfigurationBuilder$1(reader,validator),this.#serverConfigurationBuilder=new ConfigurationBuilder(reader,validator)}configureEnvironment(filename=".env"){return this.#environmentConfigurator.configure(filename)}getRuntimeConfiguration(filename){return this.#runtimeConfigurationBuilder.build(filename)}getServerConfiguration(filename){return this.#serverConfigurationBuilder.build(filename)}}class BuildApp{name="build";description="Builds the application (creates segment bundles).";options=[{key:"--env-file",required:!1,description:"Path to the environment file"},{key:"--config",required:!1,description:"Path to the configuration file",defaultValue:"jitar.json"},{key:"--log-level",required:!1,description:"Level of logging [info, debug, warn, error, fatal]",defaultValue:"info"}];async execute(args){const environmentFile=args.getOptionalArgument("--env-file",void 0),runtimeConfigFile=args.getOptionalArgument("--config",void 0),logLevelString=args.getOptionalArgument("--log-level",void 0),logLevel=this.#parseLogLevel(logLevelString),configurationManager=new ConfigurationManager;await configurationManager.configureEnvironment(environmentFile);const configuration=await configurationManager.getRuntimeConfiguration(runtimeConfigFile);return new BuildManager(configuration,logLevel).build()}#parseLogLevel(logLevel){if(void 0===logLevel)return;return(new LogLevelParser).parse(logLevel)}}const HeaderKeys_CONTENT_TYPE="content-type",HeaderKeys_CONTENT_TYPE_OPTIONS="x-content-type-options",HeaderKeys_JITAR_CONTENT_TYPE="x-jitar-content-type",HeaderKeys_JITAR_PROCEDURE_VERSION="x-jitar-procedure-version",HeaderKeys_POWERED_BY="x-powered-by",HeaderValues_NO_SNIFF="nosniff",HeaderValues_APPLICATION_JSON="application/json",HeaderValues_APPLICATION_STREAM="application/octet-stream";class InvalidWorkerId extends Error{constructor(){super("Invalid worker id")}}class HttpRemote{#url;#httpClient;#errorConverter=new ErrorConverter;#validator=new Validator;constructor(url,httpClient){this.#url=url,this.#httpClient=httpClient}connect(){return Promise.resolve()}disconnect(){return Promise.resolve()}async provide(filename){const remoteUrl=`${this.#url}/${filename}`,response=await this.#callRemote(remoteUrl,{method:"GET"}),type=response.headers.get(HeaderKeys_CONTENT_TYPE)??HeaderValues_APPLICATION_STREAM,result=await response.arrayBuffer(),content=Buffer.from(result);return new File(filename,type,content)}async isHealthy(){const remoteUrl=`${this.#url}/health/status`,response=await this.#callRemote(remoteUrl,{method:"GET"});return"true"===await response.text()}async getHealth(){const remoteUrl=`${this.#url}/health`,response=await this.#callRemote(remoteUrl,{method:"GET"}),health=await response.json();return new Map(Object.entries(health))}async addWorker(url,procedureNames,trustKey){const remoteUrl=`${this.#url}/workers`,body={url:url,procedureNames:procedureNames,trustKey:trustKey},options={method:"POST",headers:{"Content-Type":HeaderValues_APPLICATION_JSON},body:JSON.stringify(body)},response=await this.#callRemote(remoteUrl,options),contentType=response.headers.get(HeaderKeys_CONTENT_TYPE);if(null===contentType||!1===contentType.includes(HeaderValues_APPLICATION_JSON))throw new InvalidWorkerId;const result=await response.json();if(!1===this.#validator.validate(result,{id:{type:"string",required:!0}}).valid)throw new InvalidWorkerId;return result.id}async reportWorker(id,state){const remoteUrl=`${this.#url}/workers/${id}/report`,body={state:state},options={method:"POST",headers:{"Content-Type":HeaderValues_APPLICATION_JSON},body:JSON.stringify(body)};await this.#callRemote(remoteUrl,options)}async removeWorker(id){const remoteUrl=`${this.#url}/workers/${id}`,options={method:"DELETE",headers:{"Content-Type":HeaderValues_APPLICATION_JSON}};await this.#callRemote(remoteUrl,options)}async run(request){request.setHeader(HeaderKeys_CONTENT_TYPE,HeaderValues_APPLICATION_JSON);const argsObject=Object.fromEntries(request.args),headersObject=Object.fromEntries(request.headers),versionString=request.version.toString();headersObject[HeaderKeys_JITAR_PROCEDURE_VERSION]=versionString;const remoteUrl=`${this.#url}/rpc/${request.fqn}`,options={method:"POST",redirect:"manual",headers:headersObject,body:await this.#createRequestBody(argsObject)},response=await this.#callRemote(remoteUrl,options,!1),status=response.status,result=await this.#getResponseResult(response),headers=this.#createResponseHeaders(response);return new Response(status,result,headers)}async#callRemote(remoteUrl,options,throwOnError=!0){const response=await this.#httpClient.execute(remoteUrl,options);if(throwOnError&&this.#isErrorResponse(response)){const result=await this.#getResponseResult(response);throw this.#errorConverter.fromStatus(response.status,String(result))}return response}#isErrorResponse(response){return response.status<200||response.status>399}async#createRequestBody(body){return JSON.stringify(body)}async#getResponseResult(response){const contentType=response.headers.get(HeaderKeys_JITAR_CONTENT_TYPE)??response.headers.get(HeaderKeys_CONTENT_TYPE);if(contentType?.includes("undefined"))return;if(contentType?.includes("null"))return null;if(contentType?.includes("json"))return response.json();const content=await response.text();return contentType?.includes("boolean")?"true"===content:contentType?.includes("number")?Number(content):content}#createResponseHeaders(response){const headers=new Map;for(const[name,value]of response.headers)headers.set(name,value);return headers}}class FetchHttpClient{async execute(url,options){return fetch(url,options)}}class HttpRemoteBuilder{#httpClient;constructor(httpClient=new FetchHttpClient){this.#httpClient=httpClient}build(url){return new HttpRemote(url,this.#httpClient)}}const States$2={STARTING:"starting",AVAILABLE:"available",UNAVAILABLE:"unavailable",STOPPING:"stopping",STOPPED:"stopped"};class DummyProvider{get url(){throw new NotImplemented}get state(){return States$2.AVAILABLE}start(){return Promise.resolve()}stop(){return Promise.resolve()}async isHealthy(){return!0}async getHealth(){return new Map}async updateState(){return States$2.AVAILABLE}provide(filename){throw new NotImplemented}}class DummyRunner{get url(){throw new NotImplemented}get state(){return States$2.AVAILABLE}get trustKey(){throw new NotImplemented}start(){return Promise.resolve()}stop(){return Promise.resolve()}async isHealthy(){return!0}async getHealth(){return new Map}async updateState(){return States$2.AVAILABLE}getProcedureNames(){throw new NotImplemented}hasProcedure(name){throw new NotImplemented}run(request){throw new NotImplemented}}class StateManager{#state=States$2.STOPPED;get state(){return this.#state}set state(state){this.#state=state}async start(task){if(!this.isStarted())try{this.setStarting(),await task()}catch(error){throw this.setStopped(),error}}async stop(task){this.isStopped()||(this.setStopping(),await task(),this.setStopped())}isStarted(){return this.#notHasState(States$2.STOPPED)}isStopped(){return this.#hasState(States$2.STOPPED)}isAvailable(){return this.#hasState(States$2.AVAILABLE)}setStarting(){this.#state=States$2.STARTING}setAvailable(){this.#state=States$2.AVAILABLE}setUnavailable(){this.#state=States$2.UNAVAILABLE}setAvailability(available){return this.#state=available?States$2.AVAILABLE:States$2.UNAVAILABLE,this.#state}setStopping(){this.#state=States$2.STOPPING}setStopped(){this.#state=States$2.STOPPED}#hasState(...states){return states.includes(this.#state)}#notHasState(...states){return!1===this.#hasState(...states)}}class InvalidTrustKey extends Unauthorized{constructor(){super("Invalid trust key")}}class LocalGateway{#url;#trustKey;#healthManager;#workerManager;#stateManager=new StateManager;constructor(configuration){this.#url=configuration.url,this.#trustKey=configuration.trustKey,this.#healthManager=configuration.healthManager,this.#workerManager=configuration.workerManager}get url(){return this.#url}get state(){return this.#stateManager.state}get trustKey(){return this.#trustKey}async start(){return this.#stateManager.start(async()=>{await Promise.all([this.#healthManager.start(),this.#workerManager.start()]),await this.updateState()})}async stop(){return this.#stateManager.stop(async()=>{await Promise.all([this.#workerManager.stop(),this.#healthManager.stop()])})}async isHealthy(){return this.#healthManager.isHealthy()}async getHealth(){return this.#healthManager.getHealth()}async updateState(){const healthy=await this.isHealthy();return this.#stateManager.setAvailability(healthy)}async addWorker(worker){if(this.#isInvalidTrustKey(worker.trustKey))throw new InvalidTrustKey;return await worker.start(),this.#workerManager.addWorker(worker)}getWorker(id){return this.#workerManager.getWorker(id)}async reportWorker(id,state){return this.#workerManager.reportWorker(id,state)}async removeWorker(id){return this.#workerManager.removeWorker(id).stop()}getProcedureNames(){return this.#workerManager.getProcedureNames()}hasProcedure(name){return this.#workerManager.hasProcedure(name)}async run(request){return this.#workerManager.run(request)}#isInvalidTrustKey(trustKey){return void 0!==this.#trustKey&&trustKey!==this.#trustKey}}class RemoteGateway{#url;#remote;#stateManager=new StateManager;constructor(configuration){this.#url=configuration.url,this.#remote=configuration.remote}get url(){return this.#url}get state(){return this.#stateManager.state}get trustKey(){}async start(){return this.#stateManager.start(async()=>{await this.#remote.connect(),await this.updateState()})}async stop(){return this.#stateManager.stop(async()=>{await this.#remote.disconnect()})}isHealthy(){return this.#remote.isHealthy()}getHealth(){return this.#remote.getHealth()}async updateState(){const healthy=await this.isHealthy();return this.#stateManager.setAvailability(healthy)}getProcedureNames(){throw new NotImplemented}hasProcedure(name){throw new NotImplemented}addWorker(worker){return this.#remote.addWorker(worker.url,worker.getProcedureNames(),worker.trustKey)}reportWorker(id,state){return this.#remote.reportWorker(id,state)}removeWorker(id){return this.#remote.removeWorker(id)}run(request){return this.#remote.run(request)}}class NoWorkerAvailable extends ServerError{#name;constructor(name){super(`No worker available for procedure '${name}'`),this.#name=name}get name(){return this.#name}}class WorkerBalancer{#workers=[];#currentIndex=0;get workers(){return this.#workers}addWorker(worker){this.#workers.includes(worker)||this.#workers.push(worker)}removeWorker(worker){const index=this.#workers.indexOf(worker);-1!==index&&this.#workers.splice(index,1)}getNextWorker(){const workers=this.#workers.filter(worker=>worker.isAvailable());if(0!==workers.length)return this.#currentIndex>=workers.length&&(this.#currentIndex=0),workers[this.#currentIndex++];this.#currentIndex=0}async run(request){const worker=this.getNextWorker();if(void 0===worker)throw new NoWorkerAvailable(request.fqn);return worker.run(request)}}class IdGenerator{generate(){return crypto.randomUUID()}}class UnknownWorker extends ServerError{#id;constructor(id){super(`Unknown worker id '${id}'`),this.#id=id}get id(){return this.#id}}class WorkerManager{#workers=new Map;#balancers=new Map;#idGenerator=new IdGenerator;#monitorTask;constructor(scheduleManager,monitorInterval){this.#monitorTask=scheduleManager.create(()=>this.#monitor(),monitorInterval)}get workers(){return[...this.#workers.values()]}get balancers(){return this.#balancers}start(){this.#monitorTask.start()}stop(){this.#monitorTask.stop()}getProcedureNames(){const procedureNames=this.workers.map(worker=>worker.getProcedureNames());return[...new Set(procedureNames.flat()).values()]}hasProcedure(fqn){return this.getProcedureNames().includes(fqn)}addWorker(worker){worker.id=this.#idGenerator.generate(),this.#workers.set(worker.id,worker);for(const name of worker.getProcedureNames()){this.#getOrCreateBalancer(name).addWorker(worker)}return worker.id}getWorker(id){const worker=this.#workers.get(id);if(void 0===worker)throw new UnknownWorker(id);return worker}reportWorker(id,state){this.getWorker(id).reportState(state)}removeWorker(id){const worker=this.getWorker(id);this.#workers.delete(id);for(const name of worker.getProcedureNames()){const balancer=this.#getBalancer(name);void 0!==balancer&&balancer.removeWorker(worker)}return worker}#getBalancer(fqn){return this.#balancers.get(fqn)}#getOrCreateBalancer(fqn){let balancer=this.#getBalancer(fqn);return void 0===balancer&&(balancer=new WorkerBalancer,this.#balancers.set(fqn,balancer)),balancer}async run(request){const balancer=this.#getBalancer(request.fqn);if(void 0===balancer)throw new ProcedureNotFound(request.fqn);return balancer.run(request)}async#monitor(){const promises=this.workers.map(worker=>this.#checkWorker(worker));await Promise.allSettled(promises)}async#checkWorker(worker){await worker.updateState()===States$2.STOPPED&&this.removeWorker(worker.id)}}class LocalProxy{#url;#provider;#runner;#stateManager=new StateManager;constructor(configuration){this.#url=configuration.url,this.#provider=configuration.provider,this.#runner=configuration.runner}get url(){return this.#url}get state(){return this.#stateManager.state}get trustKey(){return this.#runner.trustKey}get provider(){return this.#provider}get runner(){return this.#runner}async start(){return this.#stateManager.start(async()=>{await Promise.all([this.#provider.start(),this.#runner.start()]),await this.updateState()})}async stop(){return this.#stateManager.stop(async()=>{await Promise.allSettled([this.#runner.stop(),this.#provider.stop()])})}async isHealthy(){const[providerHealthy,runnerHealthy]=await Promise.all([this.#provider.isHealthy(),this.#runner.isHealthy()]);return providerHealthy&&runnerHealthy}async getHealth(){const[providerHealth,runnerHealth]=await Promise.all([this.#provider.getHealth(),this.#runner.getHealth()]);return new Map([...providerHealth,...runnerHealth])}async updateState(){const healthy=await this.isHealthy();return this.#stateManager.setAvailability(healthy)}provide(filename){return this.#provider.provide(filename)}getProcedureNames(){return this.#runner.getProcedureNames()}hasProcedure(fqn){return this.#runner.hasProcedure(fqn)}run(request){return this.#runner.run(request)}}class LocalRepository{#url;#healthManager;#sourcingManager;#assets;#indexFilename;#serveIndexOnNotFound;#stateManager=new StateManager;constructor(configuration){this.#url=configuration.url,this.#healthManager=configuration.healthManager,this.#sourcingManager=configuration.sourcingManager,this.#assets=configuration.assets,this.#indexFilename=configuration.indexFilename??"index.html",this.#serveIndexOnNotFound=configuration.serveIndexOnNotFound??false}get url(){return this.#url}get state(){return this.#stateManager.state}async start(){return this.#stateManager.start(async()=>{await this.#healthManager.start(),await this.updateState()})}async stop(){return this.#stateManager.stop(async()=>{await this.#healthManager.stop()})}isHealthy(){return this.#healthManager.isHealthy()}getHealth(){return this.#healthManager.getHealth()}async updateState(){const healthy=await this.isHealthy();return this.#stateManager.setAvailability(healthy)}async provide(filename){if(this.#mustProvideIndex(filename))return this.provide(this.#indexFilename);if(!1===this.#assets.has(filename))throw new FileNotFound(filename);return this.#sourcingManager.read(filename)}#mustProvideIndex(filename){return""===filename||filename!==this.#indexFilename&&(this.#serveIndexOnNotFound&&!1===this.#assets.has(filename))}}class RemoteRepository{#url;#remote;#stateManager=new StateManager;constructor(configuration){this.#url=configuration.url,this.#remote=configuration.remote}get url(){return this.#url}get state(){return this.#stateManager.state}async start(){return this.#stateManager.start(async()=>{await this.#remote.connect(),await this.updateState()})}async stop(){return this.#stateManager.stop(async()=>{await this.#remote.disconnect()})}isHealthy(){return this.#remote.isHealthy()}getHealth(){return this.#remote.getHealth()}async updateState(){const healthy=await this.isHealthy();return this.#stateManager.setAvailability(healthy)}provide(filename){return this.#remote.provide(filename)}}class ClassNotFound extends Error{constructor(name){super(`The class '${name}' could not be found`)}}class InvalidClass extends Error{constructor(name){super(`The class '${name}' is invalid`)}}class NoDeserializerFound extends Error{constructor(type){super(`No deserializer found for value of type '${type}'`)}}class NoSerializerFound extends Error{constructor(type){super(`No serializer found for value of type '${type}'`)}}class Serializer{#serializers=[];addSerializer(serializer){serializer.parent=this,this.#serializers.unshift(serializer)}async serialize(value){const serializer=this.#serializers.find(serializer=>serializer.canSerialize(value));if(void 0===serializer)throw new NoSerializerFound(typeof value);return serializer.serialize(value)}async deserialize(value){const serializer=this.#serializers.find(serializer=>serializer.canDeserialize(value));if(void 0===serializer)throw new NoDeserializerFound(typeof value);return serializer.deserialize(value)}}class ParentSerializerNotSet extends Error{constructor(){super("Parent serializer not set")}}class ValueSerializer{#parent;set parent(parent){this.#parent=parent}serializeOther(value){if(void 0===this.#parent)throw new ParentSerializerNotSet;return this.#parent.serialize(value)}deserializeOther(value){if(void 0===this.#parent)throw new ParentSerializerNotSet;return this.#parent.deserialize(value)}}class ArraySerializer extends ValueSerializer{canSerialize(value){return value instanceof Array}canDeserialize(value){return value instanceof Array}async serialize(array){const values=[];for(const value of array)values.push(await this.serializeOther(value));return values}async deserialize(array){return Promise.all(array.map(async value=>this.deserializeOther(value)))}}class InvalidBigIntString extends Error{constructor(bigIntString){super(`Invalid BigInt string '${bigIntString}'`)}}class BigIntSerializer extends ValueSerializer{canSerialize(value){return"bigint"==typeof value}canDeserialize(value){const bigInt=value;return bigInt instanceof Object&&!0===bigInt.serialized&&"BigInt"===bigInt.name&&"string"==typeof bigInt.value}async serialize(bigInt){return{serialized:!0,name:"BigInt",value:bigInt.toString()}}async deserialize(object){try{return BigInt(object.value)}catch{throw new InvalidBigIntString(object.value)}}}class InvalidBufferString extends Error{constructor(bufferString){super(`Invalid Buffer string '${bufferString}'`)}}class BufferSerializer extends ValueSerializer{canSerialize(value){return value instanceof Buffer}canDeserialize(value){const buffer=value;return buffer instanceof Object&&!0===buffer.serialized&&"Buffer"===buffer.name&&"string"==typeof buffer.base64}async serialize(buffer){return{serialized:!0,name:"Buffer",base64:buffer.toString("base64")}}async deserialize(object){try{return Buffer.from(object.base64,"base64")}catch{throw new InvalidBufferString(object.base64)}}}const reflector$1=new Reflector;class ClassSerializer extends ValueSerializer{#classResolver;constructor(classResolver){super(),this.#classResolver=classResolver}canSerialize(value){return value instanceof Object&&reflector$1.isClassObject(value)}canDeserialize(value){const object=value;return object instanceof Object&&!0===object.serialized&&"class"===object.name&&"string"==typeof object.key&&object.args instanceof Object&&object.args.constructor===Object&&object.fields instanceof Object&&object.fields.constructor===Object}async serialize(object){const clazz=reflector$1.getClass(object),model=reflector$1.fromClass(clazz,!0),parameterNames=this.#extractConstructorParameters(model),key=this.#classResolver.resolveKey(clazz);if(void 0===key)throw new ClassNotFound(clazz.name);return{serialized:!0,key:key,name:"class",args:await this.#serializeConstructor(model,parameterNames,object),fields:await this.#serializeFields(model,parameterNames,object)}}#extractConstructorParameters(model){const constructor=model.getFunction("constructor");return(constructor?.parameters??[]).map(parameter=>parameter.name)}async#serializeConstructor(model,includeNames,object){const args={};for(const[index,name]of includeNames.entries()){const value=model.canRead(name)?await this.serializeOther(object[name]):void 0;args[index.toString()]=value}return args}async#serializeFields(model,excludeNames,object){const fields={};for(const property of model.writable){const name=property.name;excludeNames.includes(name)||!1===model.canRead(name)||(fields[name]=await this.serializeOther(object[name]))}return fields}async deserialize(object){const clazz=await this.#getClass(object);if(void 0===clazz)throw new ClassNotFound(object.key);if(clazz instanceof Function==!1)throw new InvalidClass(object.key);const args=await this.#deserializeConstructor(clazz,object.args),instance=reflector$1.createInstance(clazz,args);for(const name in object.fields){const fieldValue=object.fields[name];instance[name]=await this.deserializeOther(fieldValue)}return instance}async#deserializeConstructor(clazz,args){const constructor=reflector$1.fromClass(clazz,!0).getFunction("constructor"),values=(constructor?.parameters??[]).map((_,index)=>{const key=index.toString(),value=args[key];return this.deserializeOther(value)});return Promise.all(values)}async#getClass(resolvable){return globalThis[resolvable.key]??this.#classResolver.resolveClass(resolvable.key)}}class InvalidDateString extends Error{constructor(dateString){super(`Invalid date string '${dateString}'`)}}class DateSerializer extends ValueSerializer{canSerialize(value){return value instanceof Date}canDeserialize(value){const date=value;return date instanceof Object&&!0===date.serialized&&"Date"===date.name&&"string"==typeof date.value}async serialize(date){return{serialized:!0,name:"Date",value:date.toISOString()}}async deserialize(object){const date=new Date(object.value);if("Invalid Date"===date.toString())throw new InvalidDateString(object.value);return date}}class ErrorSerializer extends ValueSerializer{canSerialize(value){if(value instanceof Object==!1)return!1;const error=value;return error.constructor===Error||error.constructor===EvalError||error.constructor===RangeError||error.constructor===ReferenceError||error.constructor===SyntaxError||error.constructor===TypeError||error.constructor===URIError||error.constructor===AggregateError}canDeserialize(value){const error=value;return error instanceof Object&&!0===error.serialized&&"Error"===error.name&&error.type in globalThis}async serialize(error){return{serialized:!0,name:"Error",type:error.constructor.name,stack:error.stack,message:error.message,cause:error.cause}}async deserialize(object){const error=new(0,globalThis[object.type])(object.message,{cause:object.cause});return error.stack=object.stack,error}}class MapSerializer extends ValueSerializer{canSerialize(value){return value instanceof Map}canDeserialize(value){const map=value;return map instanceof Object&&!0===map.serialized&&"Map"===map.name&&map.entries instanceof Object&&map.entries.keys instanceof Array&&map.entries.values instanceof Array}async serialize(map){const keys=[],values=[];for(const[key,value]of map)keys.push(await this.serializeOther(key)),values.push(await this.serializeOther(value));return{serialized:!0,name:"Map",entries:{keys:keys,values:values}}}async deserialize(object){const keys=object.entries.keys,values=object.entries.values,result=new Map;for(let index=0;index<keys.length;index++){const key=await this.deserializeOther(keys[index]),value=await this.deserializeOther(values[index]);result.set(key,value)}return result}}class ObjectSerializer extends ValueSerializer{canSerialize(value){return value instanceof Object&&value.constructor===Object}canDeserialize(value){return value instanceof Object&&value.constructor===Object}async serialize(object){const result={};for(const key in object){const value=object[key];result[key]=await this.serializeOther(value)}return result}async deserialize(object){const result={};for(const key in object){const value=object[key];result[key]=await this.deserializeOther(value)}return result}}class PrimitiveSerializer extends ValueSerializer{canSerialize(value){return value instanceof Object==!1}canDeserialize(value){return value instanceof Object==!1}async serialize(primitive){return primitive}async deserialize(primitive){return primitive}}class InvalidRegExp extends Error{constructor(source,flags){super(`Invalid regular expression '${source}' with flags '${flags}'`)}}class RegExpSerializer extends ValueSerializer{canSerialize(value){return value instanceof RegExp}canDeserialize(value){const regExp=value;return regExp instanceof Object&&!0===regExp.serialized&&"RegExp"===regExp.name&&"string"==typeof regExp.source&&"string"==typeof regExp.flags}async serialize(regExp){return{serialized:!0,name:"RegExp",source:regExp.source,flags:regExp.flags}}async deserialize(object){try{return new RegExp(object.source,object.flags)}catch{throw new InvalidRegExp(object.source,object.flags)}}}class SetSerializer extends ValueSerializer{canSerialize(value){return value instanceof Set}canDeserialize(value){const set=value;return set instanceof Object&&!0===set.serialized&&"Set"===set.name&&set.values instanceof Array}async serialize(set){const values=[];for(const value of set.values())values.push(await this.serializeOther(value));return{serialized:!0,name:"Set",values:values}}async deserialize(object){const values=await Promise.all(object.values.map(async value=>this.deserializeOther(value)));return new Set([...values])}}const reflector=new Reflector;class ArrayBufferSerializer extends ValueSerializer{canSerialize(value){return value instanceof Int8Array||value instanceof Uint8Array||value instanceof Uint8ClampedArray||value instanceof Int16Array||value instanceof Uint16Array||value instanceof Int32Array||value instanceof Uint32Array||value instanceof Float32Array||value instanceof Float64Array||value instanceof BigInt64Array||value instanceof BigUint64Array}canDeserialize(value){const array=value;return array instanceof Object&&!0===array.serialized&&"TypedArray"===array.name&&array.type in globalThis&&array.bytes instanceof Array}async serialize(array){const type=array.constructor.name,view=new DataView(array.buffer),bytes=[];for(let index=0;index<view.byteLength;index++)bytes.push(view.getUint8(index));return{serialized:!0,name:"TypedArray",type:type,bytes:bytes}}async deserialize(object){const type=object.type,bytes=object.bytes,buffer=new ArrayBuffer(bytes.length),view=new DataView(buffer);for(let index=0;index<bytes.length;index++)view.setUint8(index,bytes[index]);const clazz=globalThis[type];return reflector.createInstance(clazz,[buffer])}}class InvalidUrlString extends Error{constructor(urlString){super(`Invalid url string '${urlString}'`)}}class UrlSerializer extends ValueSerializer{canSerialize(value){return value instanceof URL}canDeserialize(value){const url=value;return url instanceof Object&&!0===url.serialized&&"Url"===url.name&&"string"==typeof url.value}async serialize(url){return{serialized:!0,name:"Url",value:url.toString()}}async deserialize(object){try{return new URL(object.value)}catch{throw new InvalidUrlString(object.value)}}}class SerializerBuilder{static build(classResolver){const serializer=new Serializer;return serializer.addSerializer(new PrimitiveSerializer),serializer.addSerializer(new ObjectSerializer),void 0!==classResolver&&serializer.addSerializer(new ClassSerializer(classResolver)),serializer.addSerializer(new ErrorSerializer),serializer.addSerializer(new RegExpSerializer),serializer.addSerializer(new BigIntSerializer),serializer.addSerializer(new UrlSerializer),serializer.addSerializer(new DateSerializer),serializer.addSerializer(new SetSerializer),serializer.addSerializer(new MapSerializer),serializer.addSerializer(new ArraySerializer),serializer.addSerializer(new ArrayBufferSerializer),"undefined"!=typeof Buffer&&serializer.addSerializer(new BufferSerializer),serializer}}class ExecutionClassResolver{#executionManager;constructor(executionManager){this.#executionManager=executionManager}resolveKey(clazz){const model=this.#executionManager.getClassByImplementation(clazz);return model?.fqn}resolveClass(key){const model=this.#executionManager.getClass(key);return model?.implementation}}class RequestNotTrusted extends Unauthorized{constructor(){super("Request not trusted")}}class LocalWorker{#id;#url;#trustKey;#gateway;#registerAtGateway;#executionManager;#healthManager;#reportTask;#serializer;#stateManager=new StateManager;constructor(configuration){this.#url=configuration.url,this.#trustKey=configuration.trustKey,this.#gateway=configuration.gateway,this.#registerAtGateway=!0===configuration.registerAtGateway,this.#executionManager=configuration.executionManager,this.#healthManager=configuration.healthManager;const scheduleManager=configuration.scheduleManager;this.#reportTask=scheduleManager.create(()=>this.#report(),configuration.reportInterval);const classResolver=new ExecutionClassResolver(this.#executionManager);this.#serializer=SerializerBuilder.build(classResolver)}get id(){return this.#id}set id(id){this.#id=id}get state(){return this.#stateManager.state}get url(){return this.#url}get trustKey(){return this.#trustKey}async start(){return this.#stateManager.start(async()=>{await Promise.all([this.#executionManager.start(),this.#healthManager.start()]),void 0!==this.#gateway&&(await this.#gateway.start(),this.#registerAtGateway&&(this.#id=await this.#gateway.addWorker(this),this.#reportTask.start())),await this.updateState()})}async stop(){return this.#stateManager.stop(async()=>{void 0!==this.#gateway&&(void 0!==this.#id&&(this.#reportTask.stop(),await this.#gateway.removeWorker(this.#id)),await this.#gateway.stop()),await Promise.all([this.#healthManager.stop(),this.#executionManager.stop()])})}getProcedureNames(){return this.#executionManager.getProcedureNames()}hasProcedure(name){return this.#executionManager.hasProcedure(name)}isHealthy(){return this.#healthManager.isHealthy()}getHealth(){return this.#healthManager.getHealth()}isAvailable(){return this.#stateManager.isAvailable()}async updateState(){const healthy=await this.isHealthy();return this.#stateManager.setAvailability(healthy)}async reportState(state){if(void 0!==this.#gateway&&void 0!==this.#id)return this.#gateway.reportWorker(this.#id,state)}async run(request){return this.#mustRunLocal(request)?this.#runLocal(request):this.#runRemote(request)}#mustRunLocal(request){return void 0===this.#gateway||this.#executionManager.hasProcedure(request.fqn)}async#runLocal(request){const procedure=this.#executionManager.getProcedure(request.fqn),implementation=procedure?.getImplementation(request.version);if(this.#procedureNotFound(implementation))throw new ProcedureNotFound(request.fqn);if(this.#requestNotTrusted(request,implementation))throw new RequestNotTrusted;const dataEncoding=request.getHeader("X-Jitar-Data-Encoding");"serialized"===dataEncoding&&(request=await this.#deserializeRequest(request)),request.removeHeader("X-Jitar-Data-Encoding");const response=await this.#executionManager.run(request);return"serialized"===dataEncoding?this.#serializeResponse(response):response}#procedureNotFound(implementation){return void 0===implementation||implementation.private}#requestNotTrusted(request,implementation){if(implementation.public)return!1;const trustKey=request.getHeader("X-Jitar-Trust-Key");return this.#trustKey!==trustKey}async#runRemote(request){(request=await this.#serializeRequest(request)).setHeader("X-Jitar-Data-Encoding","serialized"),void 0!==this.#trustKey&&request.setHeader("X-Jitar-Trust-Key",this.#trustKey);const response=await this.#gateway.run(request);return this.#deserializeResponse(response)}async#serializeRequest(request){const serializedArgs=new Map;for(const[key,value]of request.args){const serializedValue=await this.#serializer.serialize(value);serializedArgs.set(key,serializedValue)}return new Request(request.fqn,request.version,serializedArgs,request.headers,request.mode)}async#deserializeRequest(request){const deserializedArgs=new Map;for(const[key,value]of request.args){const deserializedValue=await this.#serializer.deserialize(value);deserializedArgs.set(key,deserializedValue)}return new Request(request.fqn,request.version,deserializedArgs,request.headers,request.mode)}async#serializeResponse(response){const serializedResult=await this.#serializer.serialize(response.result);return new Response(response.status,serializedResult,response.headers)}async#deserializeResponse(response){const deserializedResult=await this.#serializer.deserialize(response.result);return new Response(response.status,deserializedResult,response.headers)}async#report(){const state=await this.updateState();return this.reportState(state)}}class ReportedStateManager extends StateManager{#unavailableThreshold;#stoppedThreshold;#lastReport=Date.now();#lastUpdate=Date.now();constructor(unavailableThreshold=6e3,stoppedThreshold=18e3){super(),this.#unavailableThreshold=unavailableThreshold,this.#stoppedThreshold=stoppedThreshold}report(state){this.#lastReport=Date.now(),this.state=state}update(){this.#lastUpdate=Date.now();const interval=this.#lastUpdate-this.#lastReport;return interval>=this.#unavailableThreshold&&(this.state=interval>=this.#stoppedThreshold?States$2.STOPPED:States$2.UNAVAILABLE),this.state}}class RemoteWorker{#id;#url;#trustKey;#procedureNames;#remote;#stateManager;constructor(configuration){this.#url=configuration.url,this.#trustKey=configuration.trustKey,this.#procedureNames=configuration.procedureNames,this.#remote=configuration.remote,this.#stateManager=new ReportedStateManager(configuration.unavailableThreshold,configuration.stoppedThreshold)}get id(){return this.#id}set id(id){this.#id=id}get state(){return this.#stateManager.state}get url(){return this.#url}get trustKey(){return this.#trustKey}async start(){return this.#stateManager.start(async()=>{await this.#remote.connect(),await this.updateState()})}async stop(){return this.#stateManager.stop(async()=>{await this.#remote.disconnect()})}getProcedureNames(){return[...this.#procedureNames.values()]}hasProcedure(name){return this.#procedureNames.has(name)}isHealthy(){return this.#remote.isHealthy()}getHealth(){return this.#remote.getHealth()}isAvailable(){return this.#stateManager.isAvailable()}async updateState(){return this.#stateManager.update()}async reportState(state){this.#stateManager.report(state)}run(request){return this.#remote.run(request)}}class RemoteWorkerBuilder{#remoteBuilder;#unavailableThreshold;#stoppedThreshold;constructor(remoteBuilder,unavailableThreshold,stoppedThreshold){this.#remoteBuilder=remoteBuilder,this.#unavailableThreshold=unavailableThreshold,this.#stoppedThreshold=stoppedThreshold}build(url,procedures,trustKey){const remote=this.#remoteBuilder.build(url),procedureNames=new Set(procedures),unavailableThreshold=this.#unavailableThreshold,stoppedThreshold=this.#stoppedThreshold;return new RemoteWorker({url:url,trustKey:trustKey,remote:remote,procedureNames:procedureNames,unavailableThreshold:unavailableThreshold,stoppedThreshold:stoppedThreshold})}}class ProcedureRunner{#runner;constructor(runner){this.#runner=runner}async handle(request,next){return this.#runner.run(request)}}class Runtime{#versionParser=new VersionParser;constructor(){this.#initGlobals()}#initGlobals(){const globals=globalThis;globals.__run=this.#run.bind(this),globals.ProcedureNotAccessible=ProcedureNotAccessible}async#run(fqn,versionNumber,args,sourceRequest){const version=this.#versionParser.parse(versionNumber),argsMap=new Map(Object.entries(args)),headersMap=sourceRequest instanceof Request?sourceRequest.headers:new Map,targetRequest=new Request(fqn,version,argsMap,headersMap,RunModes_NORMAL),trustKey=this.getTrustKey();void 0!==trustKey&&targetRequest.setHeader("X-Jitar-Trust-Key",trustKey);const targetResponse=await this.runInternal(targetRequest);if(targetResponse.status!==StatusCodes$1_OK)throw targetResponse.result;return targetResponse.result}}const States$1_STARTING="starting",States$1_STARTED="started",States$1_STOPPING="stopping",States$1_STOPPED="stopped";class InvalidHealthCheck extends ServerError{constructor(){super("Invalid health check")}}class HealthManager{#state=States$1_STOPPED;#healthChecks=new Map;#moduleImporter;#healthCheckFiles;constructor(moduleImporter,healthCheckFiles=[]){this.#moduleImporter=moduleImporter,this.#healthCheckFiles=healthCheckFiles}get state(){return this.#state}async start(){if(this.#state===States$1_STOPPED)try{this.#state=States$1_STARTING,await this.#loadHealthChecks(),this.#state=States$1_STARTED}catch(error){throw this.#state=States$1_STOPPED,error}}async stop(){if(this.#state===States$1_STARTED)try{this.#state=States$1_STOPPING,this.clearHealthChecks(),this.#state=States$1_STOPPED}catch(error){throw this.#state=States$1_STARTED,error}}async loadHealthCheck(filename){const healthCheck=await this.#loadHealthCheck(filename);this.addHealthCheck(healthCheck)}addHealthCheck(healthCheck){if(void 0===healthCheck.isHealthy)throw new InvalidHealthCheck;this.#healthChecks.set(healthCheck.name,healthCheck)}clearHealthChecks(){this.#healthChecks.clear()}async isHealthy(){const promises=[];for(const healthCheck of this.#healthChecks.values()){const promise=this.#executeHealthCheck(healthCheck);promises.push(promise)}return Promise.all(promises).then(results=>results.every(result=>result)).catch(()=>!1)}async getHealth(){const promises=[];for(const[name,healthCheck]of this.#healthChecks){const promise=this.#executeHealthCheck(healthCheck).then(result=>({name:name,isHealthy:result})).catch(()=>({name:name,isHealthy:!1}));promises.push(promise)}const healthChecks=new Map;return Promise.allSettled(promises).then(results=>results.forEach(result=>this.#handleHealthCheckResult(result,healthChecks))).then(()=>healthChecks)}async#loadHealthChecks(){(await Promise.all(this.#healthCheckFiles.map(filename=>this.#loadHealthCheck(filename)))).forEach(healthCheck=>this.addHealthCheck(healthCheck))}async#loadHealthCheck(filename){return(await this.#moduleImporter.import(filename)).default}#handleHealthCheckResult(result,healthChecks){"fulfilled"===result.status?healthChecks.set(result.value.name,result.value.isHealthy):healthChecks.set(result.reason.name,!1)}async#executeHealthCheck(healthCheck){const health=healthCheck.isHealthy(),milliseconds=healthCheck.timeout;if(void 0===milliseconds)return health;const timeout=new Promise(resolve=>{setTimeout(resolve,milliseconds)}).then(()=>!1);return Promise.race([timeout,health])}}const States_STARTING="starting",States_STARTED="started",States_STOPPING="stopping",States_STOPPED="stopped";class InvalidMiddleware extends ServerError{constructor(){super("Invalid middleware")}}class MiddlewareManager{#state=States_STOPPED;#middlewares=[];#moduleImporter;#middlewareFiles;constructor(moduleImporter,middlewareFiles=[]){this.#moduleImporter=moduleImporter,this.#middlewareFiles=middlewareFiles}get state(){return this.#state}async start(){if(this.#state===States_STOPPED)try{this.#state=States_STARTING,await this.#loadMiddlewares(),this.#state=States_STARTED}catch(error){throw this.#state=States_STOPPED,error}}async stop(){if(this.#state===States_STARTED)try{this.#state=States_STOPPING,this.clearMiddlewares(),this.#state=States_STOPPED}catch(error){throw this.#state=States_STARTED,error}}async loadMiddleware(filename){const middleware=await this.#loadMiddleware(filename);this.addMiddleware(middleware)}addMiddleware(middleware){if(void 0===middleware?.handle)throw new InvalidMiddleware;this.#middlewares.push(middleware)}getMiddleware(type){return this.#middlewares.find(middleware=>middleware instanceof type)}clearMiddlewares(){this.#middlewares=[]}handle(request){return this.#getNextHandler(request,0)()}async#loadMiddlewares(){(await Promise.all(this.#middlewareFiles.map(filename=>this.#loadMiddleware(filename)))).forEach(middleware=>this.addMiddleware(middleware))}async#loadMiddleware(filename){return(await this.#moduleImporter.import(filename)).default}#getNextHandler(request,index){const next=this.#middlewares[index];if(void 0===next)return async()=>new Response(StatusCodes$1_OK);const nextHandler=this.#getNextHandler(request,index+1);return async()=>await next.handle(request,nextHandler)}}class ScheduledTask{#logger;#task;#interval;#timeout=null;constructor(logger,task,interval=5e3){this.#logger=logger,this.#task=task,this.#interval=interval}start(){this.#scheduleNextExecution()}stop(){null!==this.#timeout&&(clearTimeout(this.#timeout),this.#timeout=null)}#scheduleNextExecution(){this.#timeout=setTimeout(async()=>{null!==this.#timeout&&(await this.#executeTask(),this.#scheduleNextExecution())},this.#interval)}async#executeTask(){try{await this.#task()}catch(error){this.#logger.warn("Scheduled task failed",error)}}}class ScheduleManager{#scheduledTasks=[];#logger;constructor(logger){this.#logger=logger}create(task,interval){const scheduledTask=new ScheduledTask(this.#logger,task,interval);return this.#scheduledTasks.push(scheduledTask),scheduledTask}remove(scheduledTask){const index=this.#scheduledTasks.indexOf(scheduledTask);index<0||this.#scheduledTasks.splice(index,1)}startAll(){this.#scheduledTasks.forEach(scheduledTask=>scheduledTask.start())}stopAll(){this.#scheduledTasks.forEach(scheduledTask=>scheduledTask.stop())}}const ContentTypes_NULL="application/null",ContentTypes_UNDEFINED="application/undefined",ContentTypes_BOOLEAN="application/boolean",ContentTypes_NUMBER="application/number",ContentTypes_JSON="application/json",ContentTypes_TEXT="text/plain",StatusCodes_OK=200,StatusCodes_BAD_REQUEST=400,StatusCodes_UNAUTHORIZED=401,StatusCodes_PAYMENT_REQUIRED=402,StatusCodes_FORBIDDEN=403,StatusCodes_NOT_FOUND=404,StatusCodes_TEAPOT=418,StatusCodes_SERVER_ERROR=500,StatusCodes_NOT_IMPLEMENTED=501;class Server extends Runtime{#proxy;#remoteWorkerBuilder;#resourceManager;#middlewareManager;#logger;#versionParser=new VersionParser;constructor(configuration){super(),this.#proxy=configuration.proxy,this.#remoteWorkerBuilder=configuration.remoteWorkerBuilder,this.#resourceManager=configuration.resourceManager,this.#middlewareManager=configuration.middlewareManager,this.#logger=configuration.logger}get proxy(){return this.#proxy}getTrustKey(){return this.#proxy.trustKey}async start(){await this.#setUp(),this.#logger.info(`Server started at ${this.#proxy.url}`),this.#proxy.runner instanceof LocalWorker&&this.#logger.info("RPC procedures:",this.#proxy.runner.getProcedureNames())}async stop(){await this.#tearDown(),this.#logger.info("Server stopped")}async getHealth(){try{const health=await this.#proxy.getHealth();return this.#logger.debug("Got health"),this.#respondHealth(health)}catch(error){const message=error instanceof Error?error.message:String(error);return this.#logger.error("Failed to get health:",message),this.#respondError(error)}}async isHealthy(){try{const healthy=await this.#proxy.isHealthy();return this.#logger.debug("Got health status"),this.#respondHealthy(healthy)}catch(error){const message=error instanceof Error?error.message:String(error);return this.#logger.error("Failed to get health status:",message),this.#respondError(error)}}async provide(provideRequest){try{const file=await this.#proxy.provide(provideRequest.filename);return this.#logger.info("Provided file:",provideRequest.filename),this.#respondFile(file)}catch(error){const message=error instanceof Error?error.message:String(error);return this.#logger.warn("Failed to provide file:",message),this.#respondError(error)}}async run(runRequest){try{const request=this.#transformRunRequest(runRequest),response=await this.#middlewareManager.handle(request);return this.#logger.info("Ran request:",request.fqn),this.#respondResponse(response)}catch(error){const message=error instanceof Error?error.message:String(error);return this.#logger.error("Failed run request:",message),this.#respondError(error)}}async runInternal(request){return this.#proxy.run(request)}async addWorker(addRequest){try{const gateway=this.#extractLocalGatewayFromProxy(),worker=this.#remoteWorkerBuilder.build(addRequest.url,addRequest.procedureNames,addRequest.trustKey),id=await gateway.addWorker(worker);return this.#logger.info("Added worker:",worker.url),this.#respondSuccess({id:id})}catch(error){const message=error instanceof Error?error.message:String(error);return this.#logger.error("Failed to add worker:",message),this.#respondError(error)}}async reportWorker(reportRequest){try{const state=this.#translateState(reportRequest.state),gateway=this.#extractLocalGatewayFromProxy();return await gateway.reportWorker(reportRequest.id,state),this.#logger.debug("Reported worker:",reportRequest.id),this.#respondSuccess()}catch(error){const message=error instanceof Error?error.message:String(error);return this.#logger.error("Failed to report worker:",message),this.#respondError(error)}}async removeWorker(removeRequest){try{const gateway=this.#extractLocalGatewayFromProxy();return await gateway.removeWorker(removeRequest.id),this.#logger.info("Removed worker:",removeRequest.id),this.#respondSuccess()}catch(error){const message=error instanceof Error?error.message:String(error);return this.#logger.error("Failed to remove worker:",message),this.#respondError(error)}}async#setUp(){await this.#resourceManager.start(),await Promise.all([this.#proxy.start(),this.#middlewareManager.start()]);const procedureRunner=new ProcedureRunner(this.#proxy);this.#middlewareManager.addMiddleware(procedureRunner)}async#tearDown(){await Promise.all([this.#middlewareManager.stop(),this.#proxy.stop()]),await this.#resourceManager.stop()}#transformRunRequest(request){const fqn=this.#processFqn(request.fqn),version=this.#parseVersion(request.version),args=this.#mapArguments(request.args),headers=this.#mapHeaders(request.headers);return new Request(fqn,version,args,headers,request.mode)}#processFqn(fqn){if(0===fqn.length)throw new BadRequest("Missing procedure name");if(fqn.includes(".."))throw new BadRequest("Invalid procedure name");return fqn}#parseVersion(version){return"string"!=typeof version?Version.DEFAULT:this.#versionParser.parse(version)}#mapArguments(args){return new Map(Object.entries(args))}#mapHeaders(headers){const map=new Map;for(const[key,value]of Object.entries(headers)){if(void 0===value)continue;const lowerKey=key.toLowerCase(),stringValue=value.toString();map.set(lowerKey,stringValue)}return map}#translateState(state){if(!1===Object.values(States$2).includes(state))throw new BadRequest("Invalid state value");return state}#respondHealth(health){return{result:Object.fromEntries(health),contentType:ContentTypes_JSON,headers:{},status:StatusCodes_OK}}#respondHealthy(healthy){return{result:healthy,contentType:ContentTypes_BOOLEAN,headers:{},status:StatusCodes_OK}}#respondFile(file){return{result:file.content,contentType:file.type,headers:{},status:StatusCodes_OK}}#respondResponse(response){const result=response.result instanceof Error?response.result.message:response.result;return{result:result,contentType:this.#determineContentType(result),headers:this.#unmapHeaders(response.headers),status:response.status}}#respondError(error){const result=error instanceof Error?error.message:String(error);return{result:result,contentType:this.#determineContentType(result),headers:{},status:this.#determineStatusCode(error)}}#respondSuccess(result){return{result:result,contentType:this.#determineContentType(result),headers:{},status:StatusCodes_OK}}#determineContentType(content){if(void 0===content)return ContentTypes_UNDEFINED;if(null===content)return ContentTypes_NULL;switch(typeof content){case"boolean":return ContentTypes_BOOLEAN;case"number":return ContentTypes_NUMBER;case"object":return ContentTypes_JSON;default:return ContentTypes_TEXT}}#unmapHeaders(headers){return Object.fromEntries(headers)}#determineStatusCode(error){return error instanceof BadRequest?StatusCodes_BAD_REQUEST:error instanceof Forbidden?StatusCodes_FORBIDDEN:error instanceof NotFound?StatusCodes_NOT_FOUND:error instanceof NotImplemented?StatusCodes_NOT_IMPLEMENTED:error instanceof PaymentRequired?StatusCodes_PAYMENT_REQUIRED:error instanceof Teapot?StatusCodes_TEAPOT:error instanceof Unauthorized?StatusCodes_UNAUTHORIZED:StatusCodes_SERVER_ERROR}#extractLocalGatewayFromProxy(){const runner=this.#proxy.runner;if(runner instanceof LocalGateway==!1)throw new BadRequest("Cannot remove worker from remote gateway");return runner}}class UnknownServiceConfigured extends Error{constructor(){super("Unknown service configured")}}class ResourceManager{#sourcingManager;#setUpScripts;#tearDownScripts;constructor(sourcingManager,setUpScripts=[],tearDownScripts=[]){this.#sourcingManager=sourcingManager,this.#setUpScripts=setUpScripts,this.#tearDownScripts=tearDownScripts}async start(){return this.#runSetupScripts()}async stop(){return this.#runTearDownScripts()}#runSetupScripts(){return this.#runScripts(this.#setUpScripts)}#runTearDownScripts(){return this.#runScripts(this.#tearDownScripts)}async#runScripts(scripts){await Promise.all(scripts.map(script=>this.#sourcingManager.import(script)))}}class RuntimeBuilder{#sourcingManager;#remoteBuilder;constructor(sourcingManager,remoteBuilder){this.#sourcingManager=sourcingManager,this.#remoteBuilder=remoteBuilder}async build(configuration,logLevel){const setUp=configuration.setUp,tearDown=configuration.tearDown,middleware=configuration.middleware,healthChecks=configuration.healthChecks,logger=new Logger(logLevel),healthManager=this.#buildHealthManager(healthChecks),scheduleManager=this.#buildScheduleManager(logger),proxy=await this.#buildService(configuration,healthManager,scheduleManager),sourcingManager=this.#sourcingManager,resourceManager=this.#buildResourceManager(setUp,tearDown),middlewareManager=this.#buildMiddlewareManager(middleware),remoteWorkerBuilder=this.#buildRemoteWorkerBuilder(configuration);return new Server({proxy:proxy,sourcingManager:sourcingManager,resourceManager:resourceManager,middlewareManager:middlewareManager,remoteWorkerBuilder:remoteWorkerBuilder,logger:logger})}#buildService(configuration,healthManager,scheduleManager){if(void 0!==configuration.gateway)return this.#buildGatewayProxy(configuration.url,configuration.gateway,healthManager,scheduleManager);if(void 0!==configuration.worker)return this.#buildWorkerProxy(configuration.url,configuration.worker,healthManager,scheduleManager);if(void 0!==configuration.repository)return this.#buildRepositoryProxy(configuration.url,configuration.repository,healthManager);if(void 0!==configuration.proxy)return this.#buildProxy(configuration.url,configuration.proxy);if(void 0!==configuration.standalone)return this.#buildStandalone(configuration.url,configuration.standalone,healthManager,scheduleManager);throw new UnknownServiceConfigured}async#buildGatewayProxy(url,configuration,healthManager,scheduleManager){const provider=new DummyProvider,runner=this.#buildLocalGateway(url,configuration,healthManager,scheduleManager);return new LocalProxy({url:url,provider:provider,runner:runner})}async#buildWorkerProxy(url,configuration,healthManager,scheduleManager){const provider=new DummyProvider,runner=this.#buildLocalWorker(url,configuration,healthManager,scheduleManager);return new LocalProxy({url:url,provider:provider,runner:runner})}async#buildRepositoryProxy(url,configuration,healthManager){const provider=await this.#buildLocalRepository(url,configuration,healthManager),runner=new DummyRunner;return new LocalProxy({url:url,provider:provider,runner:runner})}#buildLocalGateway(url,configuration,healthManager,scheduleManager){const trustKey=configuration.trustKey,monitorInterval=configuration.monitorInterval,workerManager=new WorkerManager(scheduleManager,monitorInterval);return new LocalGateway({url:url,trustKey:trustKey,healthManager:healthManager,workerManager:workerManager})}#buildRemoteGateway(url){const remote=this.#remoteBuilder.build(url);return new RemoteGateway({url:url,remote:remote})}#buildLocalWorker(url,configuration,healthManager,scheduleManager){const trustKey=configuration.trustKey,gateway=configuration.gateway?this.#buildRemoteGateway(configuration.gateway):void 0,reportInterval=configuration.reportInterval,registerAtGateway=void 0!==gateway,executionManager=this.#buildExecutionManager(configuration.segments);return new LocalWorker({url:url,trustKey:trustKey,reportInterval:reportInterval,gateway:gateway,registerAtGateway:registerAtGateway,executionManager:executionManager,healthManager:healthManager,scheduleManager:scheduleManager})}async#buildLocalRepository(url,configuration,healthManager){const sourcingManager=this.#sourcingManager,assets=await this.#buildAssetSet(configuration.assets),indexFilename=configuration.indexFilename,serveIndexOnNotFound=configuration.serveIndexOnNotFound;return new LocalRepository({url:url,sourcingManager:sourcingManager,assets:assets,indexFilename:indexFilename,serveIndexOnNotFound:serveIndexOnNotFound,healthManager:healthManager})}#buildRemoteRepository(url){const remote=this.#remoteBuilder.build(url);return new RemoteRepository({url:url,remote:remote})}async#buildProxy(url,configuration){const provider=this.#buildRemoteRepository(configuration.repository),runner=this.#buildRemoteGateway(configuration.gateway);return new LocalProxy({url:url,provider:provider,runner:runner})}async#buildStandalone(url,configuration,healthManager,scheduleManager){const provider=await this.#buildLocalRepository(url,configuration,healthManager),runner=this.#buildLocalWorker(url,configuration,healthManager,scheduleManager);return new LocalProxy({url:url,provider:provider,runner:runner})}#buildResourceManager(setUp=[],tearDown=[]){const translatedSetUp=setUp.map(filename=>this.#assureExtension(filename)),translatedTearDown=tearDown.map(filename=>this.#assureExtension(filename));return new ResourceManager(this.#sourcingManager,translatedSetUp,translatedTearDown)}#buildHealthManager(filenames=[]){const translatedFilenames=filenames.map(filename=>this.#assureExtension(filename));return new HealthManager(this.#sourcingManager,translatedFilenames)}#buildMiddlewareManager(filenames=[]){const translatedFilenames=filenames.map(filename=>this.#assureExtension(filename));return new MiddlewareManager(this.#sourcingManager,translatedFilenames)}#buildExecutionManager(segmentNames=[]){const filenames=segmentNames.map(name=>`./${name}.segment.js`);return new ExecutionManager(this.#sourcingManager,filenames)}#buildScheduleManager(logger){return new ScheduleManager(logger)}#buildRemoteWorkerBuilder(configuration){const unavailableThreshold=configuration.remoteWorker?.unavailableThreshold,stoppedThreshold=configuration.remoteWorker?.stoppedThreshold;return new RemoteWorkerBuilder(this.#remoteBuilder,unavailableThreshold,stoppedThreshold)}async#buildAssetSet(patterns){if(void 0===patterns)return new Set;const filenames=await this.#sourcingManager.filter(...patterns);return new Set(filenames)}#assureExtension(filename){return filename.endsWith(".js")?filename:filename+".js"}}const Defaults_PORT_NUMBER="3000",Defaults_BODY_LIMIT=204800,IgnoredHeaderKeys=["host","connection","content-length","accept-encoding","user-agent","keep-alive"];class HttpServer{#server;#port;#app;#http;#validator=new Validator;constructor(server,port=Defaults_PORT_NUMBER,bodyLimit=Defaults_BODY_LIMIT){this.#server=server,this.#port=port,this.#app=express(),this.#setupExpress(bodyLimit),this.#setupRoutes()}async start(){return await this.#server.start(),this.#startHttp()}async stop(){return await this.#stopHttp(),this.#server.stop()}#setupExpress(bodyLimit){this.#app.use(express.json({limit:bodyLimit})),this.#app.use(express.urlencoded({extended:!0})),this.#app.use(this.#addDefaultHeaders.bind(this)),this.#app.disable(HeaderKeys_POWERED_BY)}#setupRoutes(){this.#app.get("/health",(request,response)=>{this.#getHealth(request,response)}),this.#app.get("/health/status",(request,response)=>{this.#isHealthy(request,response)}),this.#app.get("/rpc/*procedure",(request,response)=>{this.#runGet(request,response)}),this.#app.post("/rpc/*procedure",(request,response)=>{this.#runPost(request,response)}),this.#app.options("/rpc/*procedure",(request,response)=>{this.#runOptions(request,response)}),this.#app.post("/workers",(request,response)=>{this.#addWorker(request,response)}),this.#app.post("/workers/:id/report",(request,response)=>{this.#reportWorker(request,response)}),this.#app.delete("/workers/:id",(request,response)=>{this.#removeWorker(request,response)}),this.#app.use((request,response)=>{this.#provide(request,response)})}#startHttp(){return void 0!==this.#http?Promise.resolve():new Promise((resolve,reject)=>{this.#http=this.#app.listen(this.#port,reject),resolve()})}#stopHttp(){return void 0===this.#http?Promise.resolve():new Promise(resolve=>{this.#http.close(()=>resolve())})}#addDefaultHeaders(request,response,next){response.setHeader(HeaderKeys_CONTENT_TYPE_OPTIONS,HeaderValues_NO_SNIFF),next()}async#getHealth(request,response){const serverResponse=await this.#server.getHealth();return this.#transformResponse(response,serverResponse)}async#isHealthy(request,response){const serverResponse=await this.#server.isHealthy();return this.#transformResponse(response,serverResponse)}async#runGet(request,response){const fqn=this.#extractFqn(request),version=this.#extractVersion(request),args=this.#extractQueryArguments(request),headers=this.#extractHeaders(request),mode=RunModes_NORMAL,serverResponse=await this.#server.run({fqn:fqn,version:version,args:args,headers:headers,mode:mode});return this.#transformResponse(response,serverResponse)}async#runPost(request,response){const fqn=this.#extractFqn(request),version=this.#extractVersion(request),args=this.#extractBodyArguments(request),headers=this.#extractHeaders(request),mode=RunModes_NORMAL,serverResponse=await this.#server.run({fqn:fqn,version:version,args:args,headers:headers,mode:mode});return this.#transformResponse(response,serverResponse)}async#runOptions(request,response){const fqn=this.#extractFqn(request),version=this.#extractVersion(request),args=this.#extractBodyArguments(request),headers=this.#extractHeaders(request),mode=RunModes_DRY,serverResponse=await this.#server.run({fqn:fqn,version:version,args:args,headers:headers,mode:mode});return this.#transformResponse(response,serverResponse)}async#addWorker(request,response){const args=this.#extractBodyArguments(request),validation=this.#validator.validate(args,{url:{type:"url",required:!0},procedureNames:{type:"list",required:!0,items:{type:"string"}},trustKey:{type:"string",required:!1}});if(!1===validation.valid)return response.status(400).send(validation.errors.join("\n"));const url=args.url,procedureNames=args.procedureNames,trustKey=args.trustKey;try{const serverResponse=await this.#server.addWorker({url:url,procedureNames:procedureNames,trustKey:trustKey});return this.#transformResponse(response,serverResponse)}catch(error){const message=error instanceof Error?error.message:"Server error";return response.status(500).send(message)}}async#reportWorker(request,response){const args={id:request.params.id,state:request.body.state},validation=this.#validator.validate(args,{id:{type:"string",required:!0},state:{type:"string",required:!0}});if(!1===validation.valid)return response.status(400).send(validation.errors.join("\n"));const id=args.id,state=args.state;try{const serverResponse=await this.#server.reportWorker({id:id,state:state});return this.#transformResponse(response,serverResponse)}catch(error){const message=error instanceof Error?error.message:"Server error";return response.status(500).send(message)}}async#removeWorker(request,response){const args={id:request.params.id},validation=this.#validator.validate(args,{id:{type:"string",required:!0}});if(!1===validation.valid)return response.status(400).send(validation.errors.join("\n"));const id=args.id;try{const serverResponse=await this.#server.removeWorker({id:id});return this.#transformResponse(response,serverResponse)}catch(error){const message=error instanceof Error?error.message:"Server error";return response.status(500).send(message)}}async#provide(request,response){const path=request.path.substring(1).trim(),filename=decodeURIComponent(path),serverResponse=await this.#server.provide({filename:filename});return this.#transformResponse(response,serverResponse)}#extractFqn(request){return decodeURIComponent(request.path.trim()).substring(5).trim()}#extractVersion(request){const versionString=request.headers[HeaderKeys_JITAR_PROCEDURE_VERSION];return Array.isArray(versionString)?versionString[0]:versionString}#extractQueryArguments(request){const args={};for(const[key,value]of Object.entries(request.query))args[key]=value;return args}#extractBodyArguments(request){return request.body}#extractHeaders(request){const headers={};for(const[key,value]of Object.entries(request.headers)){if(void 0===value)continue;const lowerKey=key.toLowerCase(),stringValue=value.toString();IgnoredHeaderKeys.includes(lowerKey)||(headers[lowerKey]=stringValue)}return headers}#transformResponse(response,serverResponse){const status=this.#transformStatus(serverResponse),contentType=this.#transformContentType(serverResponse);response.status(status),response.setHeader(HeaderKeys_CONTENT_TYPE,contentType),response.setHeader(HeaderKeys_JITAR_CONTENT_TYPE,serverResponse.contentType);for(const[name,value]of Object.entries(serverResponse.headers))response.setHeader(name,value);const result=this.#transformResult(serverResponse);return response.send(result)}#transformStatus(serverResponse){return void 0!==serverResponse.headers.location?302:serverResponse.status}#transformContentType(serverResponse){const contentType=serverResponse.contentType.toLowerCase();switch(contentType){case ContentTypes_BOOLEAN:case ContentTypes_NUMBER:case ContentTypes_UNDEFINED:case ContentTypes_NULL:return ContentTypes_TEXT}return contentType}#transformResult(serverResponse){const result=serverResponse.result;return"number"==typeof result||"boolean"==typeof result?String(result):result}}class StartServer{name="start";description="Starts a server with the configured service.";options=[{key:"--service",required:!0,description:"Path to the service configuration file"},{key:"--env-file",required:!1,description:"Path to the environment file"},{key:"--config",required:!1,description:"Path to the configuration file",defaultValue:"jitar.json"},{key:"--log-level",required:!1,description:"Level of logging [info, debug, warn, error, fatal]",defaultValue:"info"},{key:"--http-body-limit",required:!1,description:"Maximum HTTP body size in bytes",defaultValue:204800}];async execute(args){const environmentFile=args.getOptionalArgument("--env-file",void 0),runtimeConfigFile=args.getOptionalArgument("--config",void 0),serviceConfigFile=args.getRequiredArgument("--service"),logLevelString=args.getOptionalArgument("--log-level",void 0),logLevel=this.#parseLogLevel(logLevelString),bodyLimitString=args.getOptionalArgument("--http-body-limit",void 0),bodyLimit=this.#parseBodyLimit(bodyLimitString),configurationManager=new ConfigurationManager;await configurationManager.configureEnvironment(environmentFile);const runtimeConfiguration=await configurationManager.getRuntimeConfiguration(runtimeConfigFile),serverConfiguration=await configurationManager.getServerConfiguration(serviceConfigFile),httpServer=await this.#buildServer(runtimeConfiguration,serverConfiguration,bodyLimit,logLevel);return this.#runServer(httpServer)}async#buildServer(runtimeConfiguration,serverConfiguration,bodyLimit,logLevel){const[,,port]=serverConfiguration.url.split(":"),sourcingManager=new LocalSourcingManager(runtimeConfiguration.target),remoteBuilder=new HttpRemoteBuilder,serverBuilder=new RuntimeBuilder(sourcingManager,remoteBuilder),server=await serverBuilder.build(serverConfiguration,logLevel);return new HttpServer(server,port,bodyLimit)}#runServer(httpServer){let isShuttingDown=!1;return process.on("SIGINT",async()=>{isShuttingDown||(isShuttingDown=!0,httpServer.stop())}),console.log("\n ██╗██╗████████╗ █████╗ ██████╗ \n ██║██║╚══██╔══╝██╔══██╗██╔══██╗\n ██║██║ ██║ ███████║██████╔╝\n██ ██║██║ ██║ ██╔══██║██╔══██╗\n╚█████╔╝██║ ██║ ██║ ██║██║ ██║\n ╚════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝\n ~ Distributed JavaScript Runtime ~\n____________________________________\n"),httpServer.start()}#parseLogLevel(logLevel){if(void 0===logLevel)return;return(new LogLevelParser).parse(logLevel)}#parseBodyLimit(bodyLimitString){const bodyLimit=Number.parseInt(bodyLimitString);return Number.isNaN(bodyLimit)?void 0:bodyLimit}}class CommandNotFound extends Error{constructor(name){super(`Command ${name} not found`)}}class CommandRunner{#commands=new Set;constructor(){this.#commands.add(new ShowHelp(this.#commands)),this.#commands.add(new ShowAbout),this.#commands.add(new ShowVersion),this.#commands.add(new InitApp),this.#commands.add(new BuildApp),this.#commands.add(new StartServer)}run(name,args){for(const command of this.#commands)if(command.name===name)return command.execute(args);throw new CommandNotFound(name)}}class Cli{#argumentProcessor;#commandRunner;constructor(){this.#argumentProcessor=new ArgumentProcessor(process.argv),this.#commandRunner=new CommandRunner}start(){const commandName=this.#argumentProcessor.getCommand()??"help";return this.#commandRunner.run(commandName,this.#argumentProcessor)}}try{const cli=new Cli;await cli.start()}catch(error){const logger=new Logger,message=error instanceof Error?error.message:String(error);logger.fatal(message)}
|
|
2
|
+
import e from"node:path";import t,{promises as r}from"node:fs";import{fileURLToPath as s}from"node:url";import{glob as n}from"glob";import a from"mime-types";import i from"dotenv";import{B as o,N as c,F as u,a as l,A as h,O as d,b as m,P as p,T as g,U as f,S as w,V as y,c as v,R as b,d as S,e as M}from"./Response-DEjrdAZ9.js";import x from"crypto";import E from"express";class A extends Error{constructor(e){super(`Missing argument '${e}'`)}}class P{#e;#t;constructor(e){this.#e=e[2],this.#t=this.#r(e)}getCommand(){return this.#e}containsKey(e){return this.#t.has(e)}getRequiredArgument(e){const t=this.#t.get(e);if(void 0===t)throw new A(e);return t}getOptionalArgument(e,t){return this.#t.get(e)??t}#r(e){const t=e.slice(3),r=new Map;return t.forEach(e=>{const[t,s]=e.split("=");r.set(t.trim(),s?.trim())}),r}}class I{name="help";description="Shows all available commands (this message).";options=[{key:"[command]",required:!1,description:"The command name to show more details for"}];#s;constructor(e){this.#s=e}async execute(e){for(const t of this.#s.values())if(e.containsKey(t.name))return this.#n(t);this.#a()}#a(){console.log("\njitar <command>\n"),console.log("Available commands:");const e=[...this.#s.values()],t=e.map(e=>e.name),r=this.#i(t);for(const t of e){const e=t.name.padEnd(r," "),s=t.description;console.log(` ${e} ${s}`)}console.log("\nFor the options per command, use:"),console.log(" jitar help [command]\n")}#n(e){console.log(`\n${e.description}\n`);const t=e.options;if(0===t.length)return void console.log("No options available.\n");console.log("Options:");const r=t.map(e=>e.key),s=this.#i(r);for(const e of t){const t=e.key.padEnd(s," "),r=e.description,n=e.defaultValue?`default: ${e.defaultValue}`:"no default value",a=e.required?"required":`optional - ${n}`;console.log(` ${t} ${r} (${a})`)}console.log()}#i(e){return e.reduce((e,t)=>t.length>e?t.length:e,0)+3}}class O{name="about";description="Shows information about Jitar.";options=[];async execute(e){console.log("\nJitar is a JavaScript Distributed Runtime created and maintained by Masking Technology.\n\nMore information can be found at:\n- https://jitar.dev\n- https://masking.tech\n")}}class R{name="version";description="Shows the installed version of Jitar.";options=[];async execute(e){console.log("v0.10.1")}}class k extends Error{constructor(e,t){super(`Invalid ${t} name: ${e}`)}}class T extends Error{constructor(e){super(`Unknown template: ${e}`)}}const N=/^[a-zA-Z.\-_]+$/,C={_gitignore:".gitignore"};class ${#o;#c;constructor(e="./"){this.#o=this.#u(),this.#c=this.#l(e)}async init(e,t){this.#h(e,"project"),this.#h(t,"template");if(!1===await this.#d(t))throw new T(t);await this.#m(e,t),this.#p(e)}#u(){const t=e.dirname(s(import.meta.url));return e.join(t,"templates")}#l(t){return e.resolve(t)}#h(e,t){if(!1===N.test(e))throw new k(e,t)}async#d(t){try{const s=e.join(this.#o,t);return(await r.stat(s)).isDirectory()}catch{return!1}}#m(t,s){const n=e.join(this.#o,s),a=e.join(this.#c,t);return r.cp(n,a,{recursive:!0,force:!0})}async#p(t){const s=e.join(this.#c,t),n=Object.entries(C),a=[];for(const[t,i]of n){const n=e.join(s,t),o=e.join(s,i);a.push(r.rename(n,o))}await Promise.all(a)}}class F{name="init";description="Initializes a new Jitar project from a template.";options=[{key:"--name",required:!0,description:"Project name used for creating the project folder"},{key:"--template",required:!0,description:"Template to use [react, vue, backend]"},{key:"--location",required:!1,description:"The root location of the project",defaultValue:"./"}];async execute(e){const t=e.getRequiredArgument("--name"),r=e.getRequiredArgument("--template"),s=e.getOptionalArgument("--location",void 0);this.#g(t,r),await this.#f(t,r,s),this.#w(t)}#g(e,t){console.log(`Initializing project '${e}' with '${t}' template.`)}#f(e,t,r){return new $(r).init(e,t)}#w(e){console.log(`SUCCESS! Run the following commands to continue:\ncd ${e}\nnpm install\nnpm run build\nnpm run standalone`)}}const L=0,z=1,j=2,H=3,D=4;class q{#y;#v;constructor(e=z,t=console){this.#y=e,this.#v=t}debug(...e){if(this.#y>L)return;const t=this.#b("DEBUG",e);this.#v.debug(t)}info(...e){if(this.#y>z)return;const t=this.#b("INFO",e);this.#v.info(t)}warn(...e){if(this.#y>j)return;const t=this.#b("WARN",e);this.#v.warn(t)}error(...e){if(this.#y>H)return;const t=this.#b("ERROR",e);this.#v.error(t)}fatal(...e){const t=this.#b("FATAL",e);this.#v.error(t)}#b(e,t){return`[${e}][${(new Date).toISOString()}] ${t.map(e=>this.#S(e)).join(" ")}`}#S(e,t=0){let r;switch(typeof e){case"string":r=e;break;case"object":r=this.#M(e,t+1);break;case"undefined":r="undefined";break;case"function":r="function";break;default:r=String(e)}return`${this.#x(t)}${r}`}#M(e,t){if(null===e)return"null";if(Array.isArray(e)){return`[\n${e.map(e=>this.#S(e,t)).join(",\n")}\n${this.#x(t-1)}]`}return e instanceof Error?e.stack??e.message:JSON.stringify(e)}#x(e){return" ".repeat(e)}}class V extends Error{constructor(e){super(`Invalid log level: ${e}`)}}class B{parse(e){switch(e.toUpperCase()){case"DEBUG":return L;case"INFO":return z;case"WARN":return j;case"ERROR":return H;case"FATAL":return D;default:throw new V(e)}}}const W="**/*.js",K="**/*.json",G="**/*.json";class U extends Error{#E;constructor(e){super(`Invalid location: ${e}`),this.#E=e}get location(){return this.#E}}class _ extends Error{#A;constructor(e){super(`The file '${e}' could not be found`),this.#A=e}get filename(){return this.#A}}class J{#E;#P;#I;constructor(e,t,r){this.#E=e,this.#P=t,this.#I=r}get location(){return this.#E}get type(){return this.#P}get content(){return this.#I}get size(){return this.#I.length}}class Y{#E;#O;#R;constructor(e,t){const r=t.resolve(e);this.#E=t.normalize(e),this.#O=t.normalize(r),this.#R=t}getAbsoluteLocation(e){const t=this.#R.isAbsolute(e)?e:this.#R.join(this.#E,e),r=this.#R.resolve(t),s=this.#R.normalize(r);return this.#k(s,e),s}getRelativeLocation(e){const t=this.#R.relative(this.#E,e);return this.#R.normalize(t)}normalizeLocation(e){return this.#R.normalize(e)}async getType(e){const t=this.getAbsoluteLocation(e);return await this.#R.mimeType(t)??"application/octet-stream"}async getContent(e){const t=this.getAbsoluteLocation(e);if(!1===await this.#R.exists(t))throw new _(e);return this.#R.read(t)}async exists(e){const t=this.getAbsoluteLocation(e);return this.#R.exists(t)}isDirectory(e){const t=this.getAbsoluteLocation(e);return this.#R.isDirectory(t)}async read(e){const t=this.getAbsoluteLocation(e),r=await this.getType(t),s=await this.getContent(t);return new J(e,r,s)}async write(e,t){const r=this.getAbsoluteLocation(e);return this.#R.write(r,t)}async copy(e,t){const r=this.getAbsoluteLocation(e),s=this.getAbsoluteLocation(t);return this.#R.copy(r,s)}async delete(e){const t=this.getAbsoluteLocation(e);return this.#R.delete(t)}async filter(e){const t=this.getAbsoluteLocation("./"),r=this.#R.normalize(e);return(await this.#R.filter(t,r)).map(e=>this.#R.normalize(e))}#k(e,t){if(!1===e.startsWith(this.#O))throw new U(t)}}class X{copy(e,t){return r.cp(e,t,{recursive:!0,force:!0})}delete(e){return r.rm(e,{recursive:!0,force:!0})}async exists(e){try{return await r.stat(e),!0}catch{return!1}}isAbsolute(t){return e.isAbsolute(t)}isDirectory(e){try{return t.statSync(e).isDirectory()}catch{return!1}}filter(e,t){return n(`${e}/${t}`)}join(...t){return e.join(...t)}read(e){return r.readFile(e)}resolve(t){return e.resolve(t)}relative(t,r){return e.relative(t,r)}normalize(t){return t.replaceAll(e.win32.sep,e.posix.sep)}async mimeType(e){const t=a.lookup(e);if(!1!==t)return t}async write(s,n){const a=e.dirname(s);return t.mkdirSync(a,{recursive:!0}),r.writeFile(s,n)}}class Q extends Y{constructor(e){super(e,new X)}}let Z=class extends Error{#T;#N;constructor(e,t){super(`Module '${e}' could not be loaded${void 0!==t?` | ${t}`:""}`),this.#T=e,this.#N=t}get url(){return this.#T}get reason(){return this.#N}};class ee{#C;constructor(e){this.#C=e}async import(e){const t=this.#C.locate(e);try{return await import(t)}catch(e){const r=e instanceof Error?e.message:String(e);throw new Z(t,r)}}}class te{#$;constructor(e){this.#$=e}locate(e){return`file://${e.startsWith("/")?this.#$.getAbsoluteLocation(`.${e}`):this.#$.getAbsoluteLocation(e)}`}}class re extends ee{constructor(e){super(new te(e))}}class se{#$;#F;constructor(e,t){this.#$=e,this.#F=t}async filter(...e){return(await Promise.all(e.map(e=>this.#$.filter(e)))).flat().map(e=>this.#$.getRelativeLocation(e))}exists(e){return this.#$.exists(e)}read(e){return this.#$.read(e)}import(e){return this.#F.import(e)}}class ne extends se{constructor(e){const t=new Q(e);super(t,new re(t))}}let ae=class{#L;#z;#j;constructor(e,t,r){this.#L=e,this.#z=t,this.#j=r}get resources(){return this.#z}get repository(){return this.#L}get segmentation(){return this.#j}};class ie{#z;constructor(e){this.#z=e}isResourceModule(e){return this.#z.includes(e)}}const oe="private",ce="0.0.0",ue="index.js",le=".json",he="default",de=/import\s(?:["'\s]*([\w*{}\n, ]+)from\s*)?["'\s]*([@\w/._-]+)["'\s].*/g,me=/export\s(?:["'\s]*([\w*{}\n, ]+)from\s*)?["'\s]*([@\w/._-]+)["'\s].*/g,pe="*";let ge=class{#H=0;next(){return"$"+ ++this.#H}};const fe="js",we=/\.js$/,ye=[".","/","http:","https:"];class ve{translatePath(e){const t=e.split("/"),r=[];for(const e of t){switch(e.trim()){case"":case".":continue;case"..":r.pop();continue}r.push(e)}return r.join("/")}makePathRelative(e,t){if(""===t)return`./${e}`;const r=e.split("/"),s=t.split("/");for(;r[0]===s[0];)r.shift(),s.shift();const n=s.map(()=>"..").join("/");return`${s.length>0?n:"."}/${r.join("/")}`}makePathAbsolute(e,t){const r=""!==t?`${t}/${e}`:e;return this.translatePath(r)}extractPath(e){return e.split("/").slice(0,-1).join("/")}stripPath(e){return e.substring(1,e.length-1)}extractFilename(e){return e.split("/").pop()}assureExtension(e){return e.endsWith(`.${fe}`)?e:`${e}.${fe}`}addSubExtension(e,t){return e.replace(we,`.${t}.${fe}`)}isApplicationModule(e){return ye.some(t=>e.startsWith(t))}}let be=class extends Error{constructor(e,t){super(`Failed to load resource file '${e}' because of: ${t}`)}};class Se{#D;#q;#V=new ve;constructor(e,t){this.#D=e,this.#q=t}async readAll(e){const t=await Promise.all(e.map(e=>this.#B(e)));return new ie(t.flat())}async#B(e){try{const t=await this.#D.getContent(e);return JSON.parse(t.toString()).map(e=>this.#W(e))}catch(t){const r=t instanceof Error?t.message:String(t);throw new be(e,r)}}#W(e){const t=this.#q.normalizeLocation(e),r=this.#q.isDirectory(t)?`${t}/${ue}`:this.#V.assureExtension(t);return r.startsWith("./")?r.substring(2):r.startsWith("/")?r.substring(1):r}}let Me=class{#A;#K;#G;constructor(e,t,r){this.#K=t,this.#A=e,this.#G=r}get filename(){return this.#A}get code(){return this.#K}get model(){return this.#G}};class xe{#U;constructor(e){this.#U=e}get modules(){return this.#U}get(e){return this.#U.find(t=>t.filename===e)}}class Ee{#_;#J;constructor(e,t){this.#_=e,this.#J=t}get name(){return this.#_}get as(){return this.#J}toString(){return`${this.#_} as ${this.#J}`}}class Ae{#Y;constructor(e){this.#Y=e}get definition(){return this.#Y}toString(){return this.#Y}}class Pe extends Ae{}class Ie{#_;#X;#Q;constructor(e,t=!1,r=!1){this.#_=e,this.#X=t,this.#Q=r}get name(){return this.#_}get isStatic(){return this.#X}get isPrivate(){return this.#Q}get isPublic(){return!1===this.#Q}}class Oe extends Ie{#Z;#ee;constructor(e,t,r){super(e),this.#Z=t,this.#ee=r}get parentName(){return this.#Z}get scope(){return this.#ee}get members(){return this.#ee.members}get declarations(){return this.#ee.declarations}get functions(){return this.#ee.functions}get getters(){return this.#ee.getters}get setters(){return this.#ee.setters}get generators(){return this.#ee.generators}get readable(){const e=new Map;return this.getters.forEach(t=>{e.set(t.name,t)}),this.declarations.forEach(t=>{t.isPublic&&e.set(t.name,t)}),[...e.values()]}get writable(){const e=new Map;return this.setters.forEach(t=>{e.set(t.name,t)}),this.declarations.forEach(t=>{t.isPublic&&e.set(t.name,t)}),[...e.values()]}get callable(){return this.functions.filter(e=>e.isPublic)}getMember(e){return this.#ee.getMember(e)}getDeclaration(e){return this.#ee.getDeclaration(e)}getFunction(e){return this.#ee.getFunction(e)}getGetter(e){return this.#ee.getGetter(e)}getSetter(e){return this.#ee.getSetter(e)}getGenerator(e){return this.#ee.getGenerator(e)}hasMember(e){return this.#ee.hasMember(e)}hasDeclaration(e){return this.#ee.hasDeclaration(e)}hasFunction(e){return this.#ee.hasFunction(e)}hasGetter(e){return this.#ee.hasGetter(e)}hasSetter(e){return this.#ee.hasSetter(e)}hasGenerator(e){return this.#ee.hasGenerator(e)}canRead(e){const t=this.getDeclaration(e);return t?.isPublic||this.hasGetter(e)}canWrite(e){const t=this.getDeclaration(e);return t?.isPublic||this.hasSetter(e)}canCall(e){const t=this.getFunction(e);return t?.isPublic??!1}toString(){const e=void 0!==this.#Z?` extends ${this.#Z}`:"";return`class ${this.name}${e} { ${this.#ee.toString()} }`}}class Re extends Ie{#te;#re;constructor(e,t,r=!1,s=!1){super(e.toString(),r,s),this.#te=e,this.#re=t}get identifier(){return this.#te}get value(){return this.#re}toString(){return`${this.name}${this.value?" = "+this.value.toString():""}`}}class ke{#se;constructor(e){this.#se=e}get members(){return this.#se}toString(){return this.#se.map(e=>e.toString()).join(" , ")}}class Te extends ke{toString(){return`[ ${super.toString()} ]`}}class Ne extends ke{toString(){return`{ ${super.toString()} }`}}class Ce extends Ie{#se;#ne;constructor(e,t){super(""),this.#se=e,this.#ne=t}get members(){return this.#se}get from(){return this.#ne}hasMember(e){return this.#se.some(t=>t.as===e)}getMember(e){return this.#se.find(t=>t.as===e)}toString(){const e=this.#ne?` from '${this.#ne}'`:"";return`export { ${this.#se.join(", ")} }${e}`}}class $e extends Ae{}class Fe{#_;#re;constructor(e,t){this.#_=e,this.#re=t}get name(){return this.#_}get value(){return this.#re}toString(){return`${this.name}${this.value?" = "+this.value.toString():""}`}}class Le extends Ie{#ae;#ie;#oe;constructor(e,t,r,s=!1,n=!1,a=!1){super(e,s,a),this.#ae=t,this.#ie=r,this.#oe=n}get parameters(){return this.#ae}get body(){return this.#ie}get isAsync(){return this.#oe}toString(){const e=this.parameters.map(e=>e.toString());return`${this.isAsync?"async ":""}${this.name}(${e.join(", ")}) { ${this.body} }`}}class ze extends Le{toString(){const e=this.parameters.map(e=>e.toString());return`${this.isAsync?"async ":""}${this.name}*(${e.join(", ")}) { ${this.body} }`}}class je extends Le{toString(){return`get ${super.toString()}`}}class He extends Ie{#se;#ne;constructor(e,t){super(""),this.#se=e,this.#ne=t}get members(){return this.#se}get from(){return this.#ne}hasMember(e){return this.#se.some(t=>t.as===e)}getMember(e){return this.#se.find(t=>t.as===e)}toString(){return`import { ${this.#se.map(e=>e.toString()).join(", ")} } from '${this.#ne}';`}}class De{#ee;constructor(e){this.#ee=e}get scope(){return this.#ee}get members(){return this.#ee.members}get exportedMembers(){return this.#ce(this.#ee.members)}get imports(){return this.#ee.imports}get exports(){return this.#ee.exports}get declarations(){return this.#ee.declarations}get exportedDeclarations(){return this.#ce(this.#ee.declarations)}get functions(){return this.#ee.functions}get exportedFunctions(){return this.#ce(this.#ee.functions)}get generators(){return this.#ee.generators}get exportedGenerators(){return this.#ce(this.#ee.generators)}get classes(){return this.#ee.classes}get exportedClasses(){return this.#ce(this.#ee.classes)}get exported(){const e=new Map;for(const t of this.exports)for(const r of t.members){const t=this.getMember(r.name);void 0!==t&&e.set(r.as,t)}return e}getMember(e){return this.#ee.getMember(e)}getDeclaration(e){return this.#ee.getDeclaration(e)}getFunction(e){return this.#ee.getFunction(e)}getGenerator(e){return this.#ee.getGenerator(e)}getClass(e){return this.#ee.getClass(e)}hasMember(e){return this.#ee.hasMember(e)}hasDeclaration(e){return this.#ee.hasDeclaration(e)}hasFunction(e){return this.#ee.hasFunction(e)}hasGenerator(e){return this.#ee.hasGenerator(e)}hasClass(e){return this.#ee.hasClass(e)}getImport(e){return this.imports.find(t=>t.hasMember(e))}getImported(e){for(const t of this.imports)for(const r of t.members)if(r.as===e)return this.getMember(r.name)}isExported(e){for(const t of this.exports)for(const r of t.members)if(r.name===e.name)return!0;return!1}getExport(e){return this.exports.find(t=>t.hasMember(e))}getExported(e){for(const t of this.exports)for(const r of t.members)if(r.as===e)return this.getMember(r.name)}#ce(e){return e.filter(e=>this.isExported(e))}}class qe extends Ae{}class Ve extends Le{toString(){return`set ${super.toString()}`}}const Be=He.name,We=Ce.name,Ke=Re.name,Ge=Le.name,Ue=je.name,_e=Ve.name,Je=ze.name,Ye=Oe.name;class Xe{#se;constructor(e){this.#se=e}get members(){return this.#se}get imports(){return this.#se.filter(e=>e.constructor.name===Be)}get exports(){return this.#se.filter(e=>e.constructor.name===We)}get declarations(){return this.#se.filter(e=>e.constructor.name===Ke)}get functions(){return this.#se.filter(e=>e.constructor.name===Ge)}get getters(){return this.#se.filter(e=>e.constructor.name===Ue)}get setters(){return this.#se.filter(e=>e.constructor.name===_e)}get generators(){return this.#se.filter(e=>e.constructor.name===Je)}get classes(){return this.#se.filter(e=>e.constructor.name===Ye)}getMember(e){return this.#se.find(t=>t.name===e)}getDeclaration(e){return this.declarations.find(t=>t.name===e)}getFunction(e){return this.functions.find(t=>t.name===e)}getGetter(e){return this.getters.find(t=>t.name===e)}getSetter(e){return this.setters.find(t=>t.name===e)}getGenerator(e){return this.generators.find(t=>t.name===e)}getClass(e){return this.classes.find(t=>t.name===e)}hasMember(e){return void 0!==this.getMember(e)}hasDeclaration(e){return void 0!==this.getDeclaration(e)}hasFunction(e){return void 0!==this.getFunction(e)}hasGetter(e){return void 0!==this.getGetter(e)}hasSetter(e){return void 0!==this.getSetter(e)}hasGenerator(e){return void 0!==this.getGenerator(e)}hasClass(e){return void 0!==this.getClass(e)}toString(){return this.#se.map(e=>e.toString()).join("\n")}}const Qe={SINGLE:"//",MULTI_START:"/*",MULTI_END:"*/"},Ze=Object.values(Qe);const et=".",tt="(",rt=")",st="[",nt="]",at="{",it="}",ot={SCOPE:":",SEPARATOR:",",TERMINATOR:";"},ct=Object.values(ot);function ut(e){return ct.includes(e)}const lt={UNDEFINED:void 0,NULL:null,STRING:""},ht=Object.values(lt);function dt(e){return ht.includes(e)}const mt={OPEN:tt,CLOSE:rt};function pt(e){return e===mt.OPEN||e===mt.CLOSE}const gt={EXPORT:"export",DEFAULT:"default",CLASS:"class",FUNCTION:"function",CONST:"const",LET:"let",VAR:"var",AS:"as",FROM:"from",IMPORT:"import",GET:"get",SET:"set",EXTENDS:"extends",STATIC:"static",ASYNC:"async",RETURN:"return"},ft=Object.values(gt);function wt(e){return ft.includes(e)}function yt(e){return e===gt.AS||e===gt.ASYNC||e===gt.FROM||e===gt.GET||e===gt.SET}const vt={OPEN:st,CLOSE:nt};function bt(e){return e===vt.OPEN||e===vt.CLOSE}const St=Object.values({SINGLE:"'",DOUBLE:'"',BACKTICK:"`"});function Mt(e){return St.includes(e)}const xt={ADD:"+",ARROW:"=>",ASSIGN:"=",ASSIGN_ADD:"+=",ASSIGN_BITWISE_AND:"&=",ASSIGN_BITWISE_OR:"|=",ASSIGN_DIVIDE:"/=",ASSIGN_LEFT_SHIFT:"<<=",ASSIGN_LOGICAL_AND:"&&=",ASSIGN_LOGICAL_OR:"||=",ASSIGN_MODULO:"%=",ASSIGN_MULTIPLY:"*=",ASSIGN_RIGHT_SHIFT:">>=",ASSIGN_SUBTRACT:"-=",ASSIGN_XOR:"^=",BITWISE_AND:"&",BITWISE_OR:"|",DECREMENT:"--",DIVIDE:"/",EQUAL:"==",EQUAL_STRICT:"===",GREATER:">",GREATER_EQUAL:">=",INCREMENT:"++",LEFT_SHIFT:"<<",LESS:"<",LESS_EQUAL:"<=",LOGICAL_AND:"&&",LOGICAL_OR:"||",MODULO:"%",MULTIPLY:"*",NOT:"!",NOT_EQUAL:"!=",NOT_EQUAL_STRICT:"!==",RIGHT_SHIFT:">>",SUBTRACT:"-",TERNARY:"?",XOR:"^"},Et=Object.values(xt);function At(e){return Et.includes(e)}const Pt={OPEN:at,CLOSE:it};function It(e){return e===Pt.OPEN||e===Pt.CLOSE}const Ot={COMMENT:"comment",DIVIDER:"divider",GROUP:"group",IDENTIFIER:"identifier",KEYWORD:"keyword",LIST:"list",LITERAL:"literal",OPERATOR:"operator",REGEX:"regex",SCOPE:"scope",WHITESPACE:"whitespace"},Rt={SPACE:" ",TAB:"\t",NEWLINE:"\n",CARRIAGE_RETURN:"\r"},kt=Object.values(Rt);function Tt(e){return kt.includes(e)}class Nt{#ue;#le;constructor(e){this.#ue=e,this.#le=0}get items(){return this.#ue}get position(){return this.#le}get size(){return this.#ue.length}get eol(){return this.#le>=this.#ue.length}get current(){return this.#ue[this.#le]}get next(){return this.#ue[this.#le+1]}get previous(){return this.#ue[this.#le-1]}notAtEnd(){return!1===this.eol}get(e){return this.#ue[e]}step(e=1){return this.#le+=e,this.current}stepBack(e=1){return this.#le-=e,this.current}hasNext(){return this.#le+1<this.#ue.length}}class Ct extends Nt{constructor(e){super(e.split(""))}}class $t{#P;#re;#he;#de;constructor(e,t,r,s){this.#P=e,this.#re=t,this.#he=r,this.#de=s}get type(){return this.#P}get value(){return this.#re}get start(){return this.#he}get end(){return this.#de}isType(e){return this.#P===e}hasValue(e){return this.#re===e}toString(){return`${this.#re}`}}class Ft extends Nt{}class Lt{tokenize(e){const t=new Ct(e),r=[];let s;for(;t.notAtEnd();){const e=this.#me(t,s);if(void 0===e)break;e.isType(Ot.WHITESPACE)||e.isType(Ot.COMMENT)?t.step():(r.push(e),this.#pe(e)&&(s=e),t.step())}return new Ft(r)}#pe(e){return!1===[Ot.WHITESPACE,Ot.COMMENT].includes(e.type)}#me(e,t){const r=e.current,s=e.position;if(Tt(r)){const t=e.position;return new $t(Ot.WHITESPACE,r,s,t)}if(function(e){return Ze.includes(e)}(r+e.next)){const t=this.#ge(e),r=e.position;return new $t(Ot.COMMENT,t,s,r)}if(this.#fe(r,t)){const t=this.#we(e),r=e.position;return new $t(Ot.REGEX,t,s,r)}if(Mt(r)){const t=this.#ye(e),r=e.position;return new $t(Ot.LITERAL,t,s,r)}if(At(r)){const t=this.#ve(e),r=e.position;return new $t(Ot.OPERATOR,t,s,r)}if(ut(r)){const t=e.position;return new $t(Ot.DIVIDER,r,s,t)}if(pt(r)){const t=e.position;return new $t(Ot.GROUP,r,s,t)}if(It(r)){const t=e.position;return new $t(Ot.SCOPE,r,s,t)}if(bt(r)){const t=e.position;return new $t(Ot.LIST,r,s,t)}if(dt(r))return;const n=this.#be(e),a=wt(n)?Ot.KEYWORD:Ot.IDENTIFIER,i=e.position;return new $t(a,n,s,i)}#ge(e){const t=e.current+e.next===Qe.MULTI_START,r=t?Qe.MULTI_END:Rt.NEWLINE;let s=t?Qe.MULTI_START:Qe.SINGLE;for(e.step(2);e.notAtEnd();){const n=e.current;if((t?n+e.next:n)===r){e.step(r.length-1);break}s+=n,e.step()}return t?s+Qe.MULTI_END:s.trim()}#fe(e,t){return e===xt.DIVIDE&&(void 0===t||([Ot.OPERATOR,Ot.DIVIDER,Ot.KEYWORD].includes(t.type)||[mt.OPEN,vt.OPEN].includes(t.value)))}#Se(e){return Tt(e)||e==et||!1===this.#Me(e)}#we(e){let t=e.current,r=!1;for(e.step();e.notAtEnd();){const s=e.current,n=e.previous;if(s===xt.DIVIDE&&"\\"!==n)r=!0;else if(!0===r&&this.#Se(s)){e.stepBack();break}t+=s,e.step()}return t}#ye(e){const t=e.current;let r=t,s=!1;for(e.step();e.notAtEnd();){const n=e.current;if(!1===s){if(n===t){r+=n;break}"\\"===n&&(s=!0)}else s=!1;r+=n,e.step()}return r}#Me(e){return!1===(dt(e)||Tt(e)||At(e)||Mt(e)||ut(e)||pt(e)||It(e)||bt(e))}#be(e){let t="";for(;e.notAtEnd();){const r=e.current;if(!1===this.#Me(r)){e.stepBack();break}t+=r,e.step()}return t}#ve(e){let t=e.current;for(e.step();e.notAtEnd();){const r=e.current;if(!1===At(r)||!1===At(t+r)){e.stepBack();break}t+=r,e.step()}return t}}class zt extends Error{constructor(e,t){super(`Expected keyword '${e}' at position ${t}`)}}class jt extends Error{constructor(e,t){super(`Expected token '${e}' at position ${t}`)}}class Ht extends Error{constructor(e,t){super(`Unexpected keyword '${e}' at position ${t}`)}}class Dt extends Error{constructor(e){super(`The given code does not contain ${e}`)}}class qt extends Error{constructor(e,t){super(`Unexpected token '${e}' at position ${t}`)}}const Vt="default",Bt=" ";class Wt{#xe;constructor(e=new Lt){this.#xe=e}parse(e){const t=this.#xe.tokenize(e),r=this.#Ee(t);return new De(r)}parseFirst(e){const t=this.#xe.tokenize(e);return this.#Ae(t)}parseValue(e){const t=this.parseFirst(e);if(t instanceof Ae==!1)throw new Dt("a value definition");return t}parseImport(e){const t=this.parseFirst(e);if(t instanceof He==!1)throw new Dt("an import definition");return t}parseExport(e){const t=this.parseFirst(e);if(t instanceof Ce==!1)throw new Dt("an export definition");return t}parseDeclaration(e){const t=this.parseFirst(e);if(t instanceof Re==!1)throw new Dt("a declaration definition");return t}parseFunction(e){const t=this.#xe.tokenize(e),r=this.#Pe(t);if(r instanceof Le==!1)throw new Dt("a function definition");return r}parseClass(e){const t=this.#xe.tokenize(e),r=this.#Pe(t);if(r instanceof Oe==!1)throw new Dt("a class definition");return r}#Ee(e){const t=[];for(;e.notAtEnd();){const r=this.#Ae(e);r instanceof Ie&&t.push(r)}return new Xe(t)}#Ae(e,t=!1){const r=e.current;if(r.isType(Ot.LITERAL))return this.#Ie(e);if(r.isType(Ot.IDENTIFIER)){const r=e.next;return r?.hasValue(xt.ARROW)?this.#Oe(e,t):this.#Ie(e)}if(r.isType(Ot.KEYWORD)){if(yt(r.value)){const t=e.next,s=void 0!==t&&(t.hasValue(gt.FUNCTION)||t.hasValue(mt.OPEN));if(r.hasValue(gt.ASYNC)&&s)return e.step(),this.#Ae(e,!0);if(void 0===t||this.#Re(t))return this.#Ie(e)}return r.hasValue(gt.RETURN)?this.#Ie(e):this.#Pe(e,t)}if(r.isType(Ot.REGEX))return this.#Ie(e);if(r.hasValue(mt.OPEN)){const r=this.#ke(e,mt.OPEN,mt.CLOSE);return r?.hasValue(xt.ARROW)?this.#Oe(e,t):this.#Ie(e)}if(r.hasValue(Pt.OPEN))return this.#Te(e);if(r.hasValue(vt.OPEN))return this.#Ne(e);if(r.hasValue(xt.NOT)||r.hasValue(xt.SUBTRACT))return this.#Ie(e);if(!ut(r.value))throw new qt(r.value,r.start);e.step()}#Pe(e,t=!1){const r=e.current;switch(e.step(),r.value){case gt.IMPORT:return this.#Ce(e);case gt.EXPORT:return this.#$e(e);case gt.CLASS:return this.#Fe(e);case gt.FUNCTION:return this.#Le(e,t);case gt.VAR:case gt.LET:case gt.CONST:return this.#ze(e,!1,!0);case gt.ASYNC:return this.#Pe(e,!0);default:throw new Ht(r.value,r.start)}}#Ce(e){const t=[];let r=e.current;if(r.isType(Ot.LITERAL))return new He(t,r.value);if(r.hasValue(mt.OPEN)){r=e.step();const s=r.value;return e.step(2),new He(t,s)}if(!1===r.hasValue(Pt.OPEN)){const s=r.hasValue(xt.MULTIPLY)?xt.MULTIPLY:Vt;let n=r.value;r=e.step(),r.hasValue(gt.AS)&&(r=e.step(),n=r.value,r=e.step()),t.push(new Ee(s,n))}if(r.hasValue(ot.SEPARATOR)&&(r=e.step()),r.hasValue(Pt.OPEN)){const s=this.#je(e);t.push(...s),r=e.current}if(!1===r.hasValue(gt.FROM))throw new zt(gt.FROM,r.start);r=e.step();const s=r.value;return e.step(),new He(t,s)}#$e(e){switch(e.current.value){case gt.DEFAULT:return e.step(),this.#He(e,!0);case Pt.OPEN:return this.#De(e);default:return this.#He(e,!1)}}#He(e,t){let r=e.current,s=0;var n;r.hasValue(gt.ASYNC)&&(r=e.step(),s++),((n=r.value)===gt.CLASS||n===gt.FUNCTION||n===gt.CONST||n===gt.LET||n===gt.VAR)&&(r=e.step(),s++);const a=this.#Me(r)?r.value:"",i=t?Vt:a;let o;r=e.step(),r?.hasValue(gt.FROM)&&(r=e.step(),o=r.value),s>0&&(s++,e.stepBack(s));const c=new Ee(a,i);return new Ce([c],o)}#De(e){const t=this.#je(e);let r,s=e.current;return s?.hasValue(gt.FROM)&&(s=e.step(),r=s.value),e.step(),new Ce(t,r)}#je(e){const t=[];let r=e.step();for(;e.notAtEnd();){if(r.hasValue(Pt.CLOSE)){e.step();break}if(r.hasValue(ot.SEPARATOR)){r=e.step();continue}const s=this.#qe(e);t.push(s),r=e.step()}return t}#qe(e){let t=e.current;const r=t.value;let s=r;return e.next.hasValue(gt.AS)&&(t=e.step(2),s=t.value),new Ee(r,s)}#ze(e,t,r=!1){let s,n,a=e.current,i=!1;return a.hasValue(vt.OPEN)?(s=this.#Ve(e),a=e.current):a.hasValue(Pt.OPEN)?(s=this.#Be(e),a=e.current):(i=a.value.startsWith("#"),s=i?a.value.substring(1):a.value,a=e.step()),a.hasValue(xt.ASSIGN)&&(e.step(),n=this.#Ae(e,!1),a=e.current),void 0!==a&&(a.hasValue(ot.TERMINATOR)?e.step():!0===r&&a.hasValue(ot.SEPARATOR)&&(e.step(),this.#ze(e,t,!0))),n instanceof ze?new ze(s.toString(),n.parameters,n.body,t,n.isAsync,i):n instanceof Le?new Le(s.toString(),n.parameters,n.body,t,n.isAsync,i):n instanceof Oe?new Oe(s.toString(),n.parentName,n.scope):new Re(s,n,t,i)}#Le(e,t,r=!1,s=!1,n=!1){let a=e.current,i="",o=!1,c=!1;a.hasValue(xt.MULTIPLY)&&(o=!0,a=e.step()),this.#Me(a)&&(c=a.value.startsWith("#"),i=c?a.value.substring(1):a.value,a=e.step());const u=this.#We(e,mt.CLOSE);if(a=e.current,!1===a.hasValue(Pt.OPEN))throw new jt(Pt.OPEN,a.start);const l=this.#Ke(e,Pt.OPEN,Pt.CLOSE);return o?new ze(i,u,l,r,t,c):s?new je(i,u,l,r,t,c):n?new Ve(i,u,l,r,t,c):new Le(i,u,l,r,t,c)}#Oe(e,t){let r,s=e.current;if(s.hasValue(mt.OPEN)?(r=this.#We(e,mt.CLOSE),s=e.current):(r=[new Fe(s.value,void 0)],s=e.step()),!1===s.hasValue(xt.ARROW))throw new jt(xt.ARROW,s.start);s=e.step();const n=s.hasValue(Pt.OPEN)?this.#Ke(e,Pt.OPEN,Pt.CLOSE):this.#Ie(e).definition;return new Le("",r,n,!1,t,!1)}#We(e,t){const r=[];for(e.step();e.notAtEnd();){const s=e.current;if(s.hasValue(t)){e.step();break}if(s.hasValue(ot.SEPARATOR)){e.step();continue}let n;n=s.hasValue(Pt.OPEN)?this.#Be(e):s.hasValue(vt.OPEN)?this.#Ve(e):this.#Ge(e),r.push(n)}return r}#Fe(e){let t,r=e.current,s="";if(this.#Me(r)&&(s=r.value,r=e.step()),r.hasValue(gt.EXTENDS)&&(r=e.step(),t=r.value,r=e.step()),!1===r.hasValue(Pt.OPEN))throw new jt(Pt.OPEN,r.start);const n=this.#Ue(e);return new Oe(s,t,n)}#Ue(e){let t=e.step();const r=[];for(;e.notAtEnd();){if(t.hasValue(Pt.CLOSE)){e.step();break}const s=this.#_e(e);r.push(s),t=e.current}return new Xe(r)}#_e(e){let t=e.current,r=!1,s=!1,n=!1,a=!1;for(;e.notAtEnd();){if(t.hasValue(gt.STATIC))s=!0;else if(t.hasValue(gt.ASYNC))r=!0;else if(t.hasValue(gt.GET))n=!0;else{if(!t.hasValue(gt.SET)){if(t.hasValue(xt.MULTIPLY))return this.#Le(e,r,s,!1,!1);break}a=!0}t=e.step()}return e.next.hasValue(mt.OPEN)?this.#Le(e,r,s,n,a):this.#ze(e,s)}#Ne(e){const t=this.#Ke(e,vt.OPEN,vt.CLOSE);return new Pe(t)}#Ve(e){const t=this.#We(e,vt.CLOSE);return new Te(t)}#Te(e){const t=this.#Ke(e,Pt.OPEN,Pt.CLOSE);return new qe(t)}#Be(e){const t=this.#We(e,Pt.CLOSE);return new Ne(t)}#Ge(e){let t=e.current;const r=t.value;let s;return t=e.step(),t.hasValue(xt.ASSIGN)&&(e.step(),s=this.#Ae(e,!1)),new Fe(r,s)}#Ie(e){let t=e.current,r="";for(;e.notAtEnd();){if(t.hasValue(vt.OPEN)){r+=this.#Ke(e,vt.OPEN,vt.CLOSE)+Bt,t=e.current}else if(t.hasValue(mt.OPEN)){r+=this.#Ke(e,mt.OPEN,mt.CLOSE)+Bt,t=e.current}else if(t.hasValue(Pt.OPEN)){r+=this.#Ke(e,Pt.OPEN,Pt.CLOSE)+Bt,t=e.current}else r+=t.toString()+Bt,t=e.step();if(void 0===t||this.#Re(t))break}return new $e(r.trim())}#Ke(e,t,r){let s=e.step(),n=t+Bt;for(;e.notAtEnd();)if(s.hasValue(t))n+=this.#Ke(e,t,r)+Bt,s=e.current;else{if(s.hasValue(r))return e.step(),n+=r,n;n+=s.toString()+Bt,s=e.step()}return n}#ke(e,t,r){const s=e.position;this.#Ke(e,t,r);const n=e.current,a=e.position;return e.stepBack(a-s),n}#Re(e){return[ot.TERMINATOR,ot.SEPARATOR].includes(e.value)||[vt.CLOSE,mt.CLOSE,Pt.CLOSE].includes(e.value)||wt(e.value)}#Me(e){return e.isType(Ot.IDENTIFIER)||e.isType(Ot.KEYWORD)&&yt(e.value)}}class Kt{merge(e,t){const r=this.#Je(e.declarations,t.declarations),s=this.#Ye(e.functions,t.functions),n=this.#Ye(e.getters,t.getters),a=this.#Ye(e.setters,t.setters),i=[...r.values(),...s.values(),...n.values(),...a.values()];return new Oe(e.name,t.name,new Xe(i))}#Je(e,t){const r=new Map;return t.forEach(e=>r.set(e.name,e)),e.forEach(e=>r.set(e.name,e)),[...r.values()]}#Ye(e,t){const r=new Map;return t.forEach(e=>r.set(e.name,e)),e.forEach(e=>r.set(e.name,e)),[...r.values()]}}class Gt{#Xe=new Wt;#Qe=new Kt;fromModule(e,t=!1){const r=Object.entries(e),s=[];for(const[e,n]of r){if("function"!=typeof n.toString)continue;const r=n.toString();if(r.startsWith("class"))s.push(this.fromClass(n,t));else if(r.startsWith("function"))s.push(this.fromFunction(n));else{const t=new $e(r);s.push(new Re(e,t))}}return new De(new Xe(s))}fromClass(e,t=!1){const r=this.isClass(e)?this.#Ze(e):this.#et(e);if(!1===t)return r;const s=this.getParentClass(e);if(""===s.name)return r;const n=this.fromClass(s,!0);return this.#Qe.merge(r,n)}fromObject(e,t=!0){const r=this.getClass(e);return this.fromClass(r,t)}fromFunction(e){const t=e.toString();return this.#Xe.parseFunction(t)}createInstance(e,t=[]){return new e(...t)}getClass(e){return e.constructor}getParentClass(e){return Object.getPrototypeOf(e)}isClassObject(e){return this.isClass(e.constructor)}isFunctionObject(e){return this.isFunction(e.constructor)}isClass(e){return e.toString().startsWith("class")}isFunction(e){return e.toString().startsWith("function")||e.toString().startsWith("async function")}#Ze(e){const t=e.toString();return this.#Xe.parseClass(t)}#et(e){const t=this.createInstance(e),r=this.#tt(e,t),s=new Xe(r);return new Oe(e.name,void 0,s)}#tt(e,t){return[...this.#rt(t),...this.#st(e)]}#rt(e){const t=Object.getOwnPropertyNames(e),r=e,s=[];for(const e of t){const t=r[e],n=void 0!==t?new Ae(String(t)):void 0,a=new Re(e,n);s.push(a)}return s}#st(e){const t=Object.getOwnPropertyDescriptors(e.prototype),r=[];for(const e in t){const s=t[e],n=s.value;if(n instanceof Function==!1)continue;const a=this.fromFunction(n);void 0!==s.get?r.push(new je(a.name,a.parameters,a.body,a.isStatic,a.isAsync,a.isPrivate)):void 0!==s.set?r.push(new Ve(a.name,a.parameters,a.body,a.isStatic,a.isAsync,a.isPrivate)):r.push(a)}return r}}let Ut=class extends Error{constructor(e,t){super(`Failed to load module file '${e}' because of: ${t}`)}},_t=class{#q;#Xe=new Wt;#V=new ve;constructor(e){this.#q=e}rewrite(e,t){const r=this.#nt(e,t);return this.#at(e,r)}#nt(e,t){return t.replaceAll(de,t=>this.#it(e,t))}#at(e,t){return t.replaceAll(me,t=>this.#ot(e,t))}#it(e,t){const r=this.#Xe.parseImport(t),s=this.#V.stripPath(r.from),n=this.#q.normalizeLocation(s);if(!1===this.#V.isApplicationModule(n))return t;const a=this.#ct(e,n);return t.replace(s,a)}#ot(e,t){const r=this.#Xe.parseExport(t);if(void 0===r.from)return t;const s=this.#V.stripPath(r.from),n=this.#q.normalizeLocation(s);if(!1===this.#V.isApplicationModule(n))return t;const a=this.#ct(e,n);return t.replace(s,a)}#ct(e,t){const r=this.#V.extractPath(e),s=this.#V.makePathAbsolute(t,r);return this.#q.isDirectory(s)?`${t}/${ue}`:this.#V.assureExtension(t)}},Jt=class{#q;#Xe;#ut;constructor(e,t=new Wt){this.#q=e,this.#Xe=t,this.#ut=new _t(e)}async readAll(e){const t=await Promise.all(e.map(e=>this.read(e)));return new xe(t)}async read(e){const t=this.#q.getRelativeLocation(e),r=await this.#lt(e),s=this.#ut.rewrite(t,r),n=this.#Xe.parse(s);return new Me(t,s,n)}async#lt(e){try{return(await this.#q.getContent(e)).toString()}catch(t){const r=t instanceof Error?t.message:String(t);throw new Ut(e,r)}}};class Yt{#_;#U=new Map;#ht=new Map;#dt=new Map;constructor(e){this.#_=e}get name(){return this.#_}get modules(){return[...this.#U.values()]}get classes(){return[...this.#ht.values()]}get procedures(){return[...this.#dt.values()]}hasModule(e){return this.#U.has(e)}getModule(e){return this.#U.get(e)}getSegmentedModules(){return this.modules.filter(e=>e.segmented)}setModule(e){this.#U.set(e.filename,e)}hasProcedure(e){return this.#dt.has(e)}getProcedure(e){return this.#dt.get(e)}setProcedure(e){this.#dt.set(e.fqn,e)}setClass(e){this.#ht.set(e.fqn,e)}}class Xt{#H;#mt;#pt;constructor(e,t,r){this.#H=e,this.#mt=t,this.#pt=r}get id(){return this.#H}get importKey(){return this.#mt}get fqn(){return this.#pt}}class Qt extends Xt{#G;constructor(e,t,r,s){super(e,t,r),this.#G=s}get model(){return this.#G}}class Zt extends Xt{#gt;#ft;#G;constructor(e,t,r,s,n,a){super(e,t,r),this.#gt=s,this.#ft=n,this.#G=a}get access(){return this.#gt}get version(){return this.#ft}get model(){return this.#G}}class er{#A;#E;#wt;#se=[];#yt;constructor(e,t,r,s){this.#A=e,this.#E=t,this.#wt=r,this.#yt=s}get filename(){return this.#A}get location(){return this.#E}get imports(){return this.#wt}get members(){return this.#se}get segmented(){return this.#yt}hasClasses(){return this.#se.some(e=>e instanceof Qt)}getClasses(){return this.#se.filter(e=>e instanceof Qt)}hasImplementations(){return this.#se.some(e=>e instanceof Zt)}getImplementations(){return this.#se.filter(e=>e instanceof Zt)}addMember(e){this.#se.push(e)}addImports(e){const t=Object.keys(e);for(const r of t)this.#wt[r]=e[r]}}class tr{#pt;#vt=[];constructor(e,t=[]){this.#pt=e,this.#vt=t}get fqn(){return this.#pt}get implementations(){return this.#vt}addImplementation(e){this.#vt.push(e)}}class rr{#bt;constructor(e){this.#bt=e}get segments(){return this.#bt}getSegment(e){return this.#bt.find(t=>t.name===e)}isSegmentedModule(e){return this.#bt.some(t=>t.hasModule(e))}getSegments(e){return this.#bt.filter(t=>t.hasModule(e))}}class sr extends Error{constructor(e,t){super(`Function '${t}' from file '${e}' is not async`)}}class nr extends Error{constructor(e){super(`Segment filename '${e}' is invalid`)}}class ar extends Error{constructor(e,t){super(`Failed to load segment file '${e}' because of: ${t}`)}}class ir extends Error{constructor(e,t){super(`The export '${t}' from file '${e}' is not a function or a class.`)}}class or extends Error{constructor(e,t){super(`Module '${e}' does not export '${t}'`)}}class cr extends Error{constructor(e){super(`Segmented module not found '${e}'`)}}class ur{#L;#V=new ve;constructor(e){this.#L=e}locate(e,t){const r=[];return{trace:r,model:this.#St(e,t,r)}}#St(e,t,r){r.push({filename:e,importKey:t});const s=this.#Mt(e);return this.#xt(s,t)?this.#Et(s,t,r):this.#At(s,t)}#Mt(e){const t=this.#L.get(e);if(void 0===t)throw new cr(e);return t}#xt(e,t){const r=e.model.getImport(t),s=e.model.getExport(t);return void 0!==r||void 0!==s?.from}#Et(e,t,r){const s=this.#Pt(e,t)??this.#It(e,t),n=s?.from,a=s?.name,i=this.#V.extractPath(e.filename),o=this.#V.stripPath(n),c=this.#V.makePathAbsolute(o,i);return this.#St(c,a,r)}#At(e,t){const r=this.#It(e,t);if(void 0===r)throw new or(e.filename,t);const s=e.model.getMember(r.name);if(void 0===s)throw new or(e.filename,r.name);return s}#It(e,t){const r=e.model.getExport(t),s=r?.getMember(t);if(void 0!==s)return{from:r?.from,name:s.name}}#Pt(e,t){const r=e.model.getImport(t),s=r?.getMember(t);if(void 0!==r&&void 0!==s)return{from:r.from,name:s.name}}}class lr{#Ot;#q;#Rt;#V=new ve;constructor(e,t,r){this.#Ot=e,this.#q=t,this.#Rt=new ur(r)}async readAll(e){const t=await Promise.all(e.map(e=>this.#kt(e)));return new rr(t)}async#kt(e){const t=await this.#Tt(e),r=this.#Nt(e),s=new Yt(r);return this.#Ct(s,t),this.#$t(s),s}#Nt(e){const t=e.split("/").pop();if(void 0===t||""===t)throw new nr(e);return t.replace(le,"")}async#Tt(e){try{const t=await this.#Ot.getContent(e);return JSON.parse(t.toString())}catch(t){const r=t instanceof Error?t.message:String(t);throw new ar(e,r)}}#Ct(e,t){for(const[r,s]of Object.entries(t))this.#Ft(e,r,s,!0)}#Ft(e,t,r,s){const n=this.#Lt(t),a=this.#V.extractPath(n),i=e.hasModule(n)?e.getModule(n):new er(n,a,{},s);i.addImports(r),e.setModule(i)}#Lt(e){const t=this.#q.normalizeLocation(e),r=this.#q.isDirectory(t)?`${t}/${ue}`:this.#V.assureExtension(t);return r.startsWith("./")?r.substring(2):r.startsWith("/")?r.substring(1):r}#$t(e){const t=new ge;for(const r of e.modules)for(const s in r.imports)this.#zt(e,r,s,t)}#zt(e,t,r,s){const{model:n,trace:a}=this.#Rt.locate(t.filename,r),i=this.#jt(t,n,r,s);this.#Ht(e,t,n,i),a.shift();for(const t of a){const r={[t.importKey]:{access:i.access}};this.#Ft(e,t.filename,r,!1)}}#jt(e,t,r,s){const n=e.imports[r],a=s.next(),i=n.as??t.name;return{id:a,importKey:r,name:i,access:n.access??oe,version:n.version??ce,fqn:this.#Dt(e,i,r)}}#Dt(e,t,r){return this.#qt(e)?t:this.#Vt(e)&&this.#Bt(r)?e.location:`${e.location}/${t}`}#qt(e){return""===e.location}#Vt(e){return e.filename.endsWith(ue)}#Bt(e){return e===he}#Ht(e,t,r,s){if(r instanceof Oe)return this.#Wt(e,t,r,s);if(r instanceof Le)return this.#Kt(e,t,r,s);throw new ir(t.filename,s.importKey)}#Wt(e,t,r,s){const n=new Qt(s.id,s.importKey,s.fqn,r);t.addMember(n),e.setClass(n)}#Kt(e,t,r,s){if(!1===r.isAsync)throw new sr(t.filename,s.name);const n=new Zt(s.id,s.importKey,s.fqn,s.access,s.version,r);this.#Gt(e,t,n)}#Gt(e,t,r){const s=e.hasProcedure(r.fqn)?e.getProcedure(r.fqn):new tr(r.fqn);s.addImplementation(r),t.addMember(r),e.setProcedure(s)}}class hr{#Ut;constructor(e){this.#Ut=e}async read(e,t,r){const s=this.#Ut.source,n=this.#Ut.resource,a=this.#Ut.segment,i=new Jt(s),o=await i.readAll(e),c=new Se(n,s),u=await c.readAll(t),l=new lr(a,s,o),h=await l.readAll(r);return new ae(o,u,h)}}class dr{#_t;#z;#j;#Jt;#Xe=new Wt;#V=new ve;constructor(e,t,r,s){this.#_t=e,this.#z=t,this.#j=r,this.#Jt=s}get parser(){return this.#Xe}rewrite(e){return e.replaceAll(this.replacementPattern,e=>this.replaceStatement(e))}replaceStatement(e){const t=this.parseStatement(e);return void 0===t.from?e:this.#Yt(t)?this.#Xt(t):this.#Qt(t)}#Xt(e){const t=this.#Zt(e);return this.#z.isResourceModule(t)?this.#er(t,e):this.#tr(t,e)}#Qt(e){const t=this.#rr(e),r=e.members.map(e=>e.name);return this.includeInBundle(e,t,r)}#tr(e,t){if(0===t.members.length)return this.#sr(e,t,[]);const{segmentKeys:r,remoteKeys:s,commonKeys:n}=this.#nr(e,t),a=[];return r.length>0&&a.push(this.#ar(e,t,r)),s.length>0&&a.push(this.#ir(e,t,s)),n.length>0&&a.push(this.#sr(e,t,n)),a.filter(e=>e.length>0).join("\n")}#er(e,t){const r=this.#or(e);return this.excludeFromBundle(t,r)}#ar(e,t,r){const s=this.#or(e,this.#Jt.name);return this.includeInBundle(t,s,r)}#ir(e,t,r){const s=this.#or(e,"remote");return this.includeInBundle(t,s,r)}#sr(e,t,r){const s=this.#or(e);return this.includeInBundle(t,s,r)}#Yt(e){const t=this.#V.stripPath(e.from);return this.#V.isApplicationModule(t)}#Zt(e){const t=this.#V.stripPath(e.from),r=this.#V.extractPath(this.#_t.filename);return this.#V.makePathAbsolute(t,r)}#nr(e,t){const r=this.#cr(e,this.#Jt),s=this.#ur(e,r),n=this.#lr(t,r),a=this.#lr(t,s);return{segmentKeys:n,remoteKeys:a,commonKeys:this.#hr(t,[...n,...a])}}#cr(e,t){if(void 0===t)return[];const r=t.getModule(e);return void 0!==r?Object.keys(r.imports):[]}#ur(e,t){const r=this.#j.getSegments(e).filter(e=>e!==this.#Jt).map(t=>this.#cr(e,t)).flat();return[...new Set(r)].filter(e=>!1===t.includes(e))}#lr(e,t){return e.members.filter(e=>t.includes(e.name)).map(e=>e.name)}#hr(e,t){return e.members.filter(e=>!1===t.includes(e.name)).map(e=>e.name)}#or(e,t){const r=this.#V.extractPath(this.#_t.filename),s=this.#V.makePathRelative(e,r);return void 0!==t?this.#V.addSubExtension(s,t):s}#rr(e){return this.#V.stripPath(e.from)}}class mr extends dr{get replacementPattern(){return de}parseStatement(e){return this.parser.parseImport(e)}includeInBundle(e,t,r){if(0===e.members.length)return`import "${t}";`;return`import ${this.#dr(e,r)} from "${t}";`}excludeFromBundle(e,t){if(0===e.members.length)return`await import("${t}");`;return`const ${this.#mr(e)} = await import("${t}");`}#dr(e,t){const r=e.members.filter(e=>t.includes(e.name)),s=r.find(e=>e.name===he),n=void 0!==s,a=n?s.as:"",i=r.filter(e=>e.name!==he).map(e=>e.name!==e.as?`${e.name} as ${e.as}`:e.name),o=i.length>0;return`${a}${n&&o?", ":""}${o?`{ ${i.join(", ")} }`:""}`}#mr(e){if(this.#pr(e))return e.members[0].as;return`{ ${e.members.map(e=>e.name!==e.as?`${e.name} : ${e.as}`:e.name).join(", ")} }`}#pr(e){return 1===e.members.length&&e.members[0].name===pe}}class pr extends dr{get replacementPattern(){return me}parseStatement(e){return this.parser.parseExport(e)}includeInBundle(e,t,r){if(0===e.members.length)return`export "${t}";`;return`export ${this.#gr(e,r)} from "${t}";`}excludeFromBundle(e,t){const r=e.members.map(e=>e.name);return this.includeInBundle(e,t,r)}#gr(e,t){const r=e.members.filter(e=>t.includes(e.name));if(1===r.length&&""===r[0].name){const e=r[0];return e.name!==e.as?`${pe} as ${e.as}`:pe}return`{ ${r.map(e=>e.name!==e.as?`${e.name} as ${e.as}`:e.name).join(", ")} }`}}class gr{build(e,t,r,s){const n=new mr(e,t,r,s),a=new pr(e,t,r,s),i=n.rewrite(e.code);return a.rewrite(i)}}const fr="normal",wr="dry",yr=200,vr=400,br=401,Sr=402,Mr=403,xr=404,Er=418,Ar=500,Pr=501;class Ir extends o{#fr;constructor(e){super(`Invalid version number '${e}'`),this.#fr=e}get number(){return this.#fr}}class Or extends c{#pt;constructor(e){super(`Procedure '${e}' not found`),this.#pt=e}get fqn(){return this.#pt}}class Rr extends u{constructor(e,t){super(`Procedure '${e}' (v${t}) is not accessible`)}}class kr extends o{#wr;constructor(e){super(`Unknown parameter ${e}`),this.#wr=e}get parameterName(){return this.#wr}}class Tr extends o{#wr;constructor(e){super(`Missing value for parameter '${e}'`),this.#wr=e}get parameterName(){return this.#wr}}class Nr extends o{#wr;constructor(e){super(`Invalid value for parameter '${e}'`),this.#wr=e}get parameterName(){return this.#wr}}class Cr{extract(e,t){const r=this.#yr(e,t),s=[];for(const t of e){const e=this.#vr(t,r);s.push(e)}if(r.size>0){const e=r.keys().next().value;throw new kr(e)}return s}#yr(e,t){const r=new Map;for(const[s,n]of t){if(this.#br(s)){const t=this.#Sr(s);!0===this.#Mr(e,t)&&r.set(t,n);continue}r.set(s,n)}return r}#br(e){return e.startsWith("*")}#Sr(e){return e.substring(1)}#Mr(e,t){return void 0!==e.find(e=>e.name===t)}#vr(e,t,r){return e instanceof l?this.#xr(e,t,r):this.#Er(e,t)}#xr(e,t,r){const s=t.get(e.name);if(this.#Ar(e,s,r))throw new Tr(e.name);if(this.#Pr(e,s,r))throw new Nr(e.name);return t.delete(e.name),s}#Er(e,t){return e instanceof h?this.#Ir(e,t):this.#Or(e,t)}#Ir(e,t){const r=this.#Rr(e,t);return void 0!==r?Object.values(r):void 0}#Or(e,t){return this.#Rr(e,t)}#Rr(e,t){const r=e instanceof h,s={},n=[];let a=!1,i=0;for(const o of e.variables){const c=r?i++:o.name,u=this.#vr(o,t,e);void 0!==u?a=!0:!1===o.isOptional&&n.push(o.name),s[c]=u}if(!0===a&&n.length>0)throw new Tr(n[0]);return a?s:void 0}#Ar(e,t,r){return void 0===t&&(!0!==e.isOptional&&!0!==r?.isOptional)}#Pr(e,t,r){return!1!==e.name.startsWith("...")&&(void 0===r&&t instanceof Array==!1||r instanceof h&&t instanceof Array==!1||r instanceof d&&t instanceof Object==!1)}}class $r{toStatus(e){return e instanceof o?vr:e instanceof u?Mr:e instanceof c?xr:e instanceof m?Pr:e instanceof p?Sr:e instanceof g?Er:e instanceof f?br:Ar}fromStatus(e,t){switch(e){case vr:return new o(t);case Mr:return new u(t);case xr:return new c(t);case Pr:return new m(t);case Sr:return new p(t);case Er:return new g(t);case br:return new f(t);default:return new w(t)}}}const Fr=/^\d+(?:\.\d+){0,2}$/;class Lr{parse(e){if(0===e.trim().length)return y.DEFAULT;if(!1===Fr.test(e))throw new Ir(e);const t=e.split(".");switch(t.length){case 1:return new y(Number.parseInt(t[0]));case 2:return new y(Number.parseInt(t[0]),Number.parseInt(t[1]));default:return new y(Number.parseInt(t[0]),Number.parseInt(t[1]),Number.parseInt(t[2]))}}}class zr extends c{#pt;#ft;constructor(e,t){super(`No implementation found for procedure '${e}' with version '${t}'`),this.#pt=e,this.#ft=t}get fqn(){return this.#pt}get version(){return this.#ft}}class jr extends w{constructor(){super("Invalid segment")}}class Hr{#bt=new Map;addSegment(e){this.#bt.set(e.id,e)}clearSegments(){this.#bt.clear()}getClassNames(){const e=new Set;for(const t of this.#bt.values()){t.getClasses().forEach(t=>e.add(t.fqn))}return[...e.values()]}hasClass(e){return this.getClassNames().includes(e)}getClass(e){for(const t of this.#bt.values())if(t.hasClass(e))return t.getClass(e)}getClassByImplementation(e){for(const t of this.#bt.values()){const r=t.getClassByImplementation(e);if(void 0!==r)return r}}getProcedureNames(){const e=new Set;for(const t of this.#bt.values()){t.getExposedProcedures().forEach(t=>e.add(t.fqn))}return[...e.values()]}hasProcedure(e){return this.getProcedureNames().includes(e)}getProcedure(e){for(const t of this.#bt.values())if(t.hasProcedure(e))return t.getProcedure(e)}}class Dr{#kr;#Tr;#Nr=new Cr;#Cr=new $r;#$r=new Hr;constructor(e,t=[]){this.#kr=e,this.#Tr=t}async start(){return this.#Fr()}async stop(){return this.#Lr()}async loadSegment(e){const t=await this.#kr.import(e);this.addSegment(t.default)}async addSegment(e){if(e instanceof v==!1)throw new jr;this.#$r.addSegment(e)}getClassNames(){return this.#$r.getClassNames()}hasClass(e){return this.#$r.hasClass(e)}getClass(e){return this.#$r.getClass(e)}getClassByImplementation(e){return this.#$r.getClassByImplementation(e)}getProcedureNames(){return this.#$r.getProcedureNames()}hasProcedure(e){return this.#$r.hasProcedure(e)}getProcedure(e){return this.#$r.getProcedure(e)}async run(e){const t=this.#zr(e.fqn,e.version),r=this.#Nr.extract(t.parameters,e.args);return e.mode===wr?new b(yr,void 0):this.#jr(e,t,r)}async#Fr(){await Promise.all(this.#Tr.map(e=>this.loadSegment(e)))}#Lr(){this.#$r.clearSegments()}#zr(e,t){const r=this.#$r.getProcedure(e);if(void 0===r)throw new Or(e);const s=r.getImplementation(t);if(void 0===s)throw new zr(r.fqn,t.toString());return s}async#jr(e,t,r){try{const s=await t.executable.call(e,...r);return new b(yr,s)}catch(e){const t=this.#Cr.toStatus(e);return new b(t,e)}}}class qr{build(e){let t="";for(const r of e)t+=r.access===S.PRIVATE?this.#Hr(r):this.#Dr(r);return t.trim()}#Hr(e){const t=e.fqn,r=e.version,s=this.#qr(e),n=`throw new ProcedureNotAccessible('${t}', '${r}');`;return this.#Vr(s,n)}#Dr(e){const t=e.fqn,r=e.version,s=this.#Br(e.model.parameters),n=this.#qr(e),a=`return __run('${t}', '${r}', { ${s} }, this);`;return this.#Vr(n,a)}#Wr(e){const t=[];for(const r of e)r instanceof Fe?t.push(r.name):(r instanceof Te||r instanceof Ne)&&t.push(r.toString());return t.join(", ")}#Br(e){return this.#Kr(e).join(", ")}#Kr(e){const t=[];for(const r of e)if(r instanceof ke){const e=this.#Kr(r.members);t.push(...e)}else if(r instanceof Fe){const e=this.#Gr(r);t.push(e)}return t}#Gr(e){const t=e.name,r=t.startsWith("...")?t.substring(3):t;return`'${t}': ${r}`}#qr(e){const t=e.model.name,r=this.#Wr(e.model.parameters);return`\nexport ${e.importKey===he?`${he} `:""}async function ${t}(${r})`}#Vr(e,t){return`${e} {\n\t${t}\n}\n`}}let Vr=class{#Ur;#_r=new gr;#Jr=new qr;#V=new ve;constructor(e){this.#Ur=e}async build(e){const t=e.repository,r=e.segmentation,s=e.resources,n=t.modules.map(e=>this.#Yr(e,s,r));await Promise.all(n)}async#Yr(e,t,r){const s=r.getSegments(e.filename);if(0===s.length)return this.#Xr(e,t,r);const n=s.map(s=>this.#Qr(e,t,s,r)),a=s[0].getModule(e.filename).hasImplementations()?this.#Zr(e,s):Promise.resolve();await Promise.all([...n,a]),this.#Ur.delete(e.filename)}async#Xr(e,t,r){const s=e.filename,n=this.#_r.build(e,t,r);return this.#Ur.write(s,n)}async#Qr(e,t,r,s){const n=this.#V.addSubExtension(e.filename,r.name),a=this.#_r.build(e,t,s,r);return this.#Ur.write(n,a)}async#Zr(e,t){const r=this.#es(e,t),s=this.#V.addSubExtension(e.filename,"remote"),n=this.#Jr.build(r);return this.#Ur.write(s,n)}#es(e,t){const r=t.map(t=>t.getModule(e.filename)).flatMap(e=>e.getImplementations()),s=new Map;for(const e of r){const t=`${e.fqn}:${e.version.toString()}`;s.set(t,e)}return[...s.values()]}};let Br=class{#Ur;#ts;#V=new ve;#rs=new Lr;constructor(e,t){this.#Ur=e,this.#ts=t}async build(e){const t=e.segmentation.segments.map(e=>this.#ss(e));await Promise.all(t)}async#ss(e){const t=`${e.name}.segment.js`,r=this.#ns(e);await this.#Ur.write(t,r),this.#ts.info(`Built ${e.name} segment (${e.modules.length} modules, ${e.procedures.length} procedures, ${e.classes.length} classes)`)}#ns(e){return`${this.#as(e)}\n${this.#is(e)}`}#as(e){return`import { Segment, Class, Procedure, Implementation, Version, NamedParameter, ArrayParameter, ObjectParameter } from "jitar";\n${this.#os(e)}`}#os(e){const t=[];for(const r of e.getSegmentedModules()){if(0===r.members.length)continue;const s=this.#V.addSubExtension(r.filename,e.name),n=`import ${this.#cs(r)} from "./${s}";`;t.push(n)}return t.join("\n")}#cs(e){const t=e.members,r=t.find(e=>e.importKey===he),s=void 0!==r,n=s?r.id:"",a=t.filter(e=>e.importKey!==he),i=a.map(e=>`${e.importKey} as ${e.id}`),o=a.length>0;return`${n}${s&&o?", ":""}${o?`{ ${i.join(", ")} }`:""}`}#is(e){const t=[];t.push(`export default new Segment("${e.name}")`);for(const r of e.classes)t.push(`\t.addClass(new Class("${r.fqn}", ${r.id}))`);for(const r of e.procedures){t.push(`\t.addProcedure(new Procedure("${r.fqn}")`);for(const e of r.implementations){const r=this.#us(e.version),s=this.#ls(e.model);t.push(`\t\t.addImplementation(new Implementation(${r}, "${e.access}", ${s}, ${e.id}))`)}t.push("\t)")}return t.join("\n")}#us(e){const t=this.#rs.parse(e);return`new Version(${t.major}, ${t.minor}, ${t.patch})`}#ls(e){return`[${this.#hs(e.parameters).join(", ")}]`}#hs(e){const t=[];for(const r of e)t.push(this.#ds(r));return t}#ds(e){return e instanceof Te?this.#ms(e):e instanceof Ne?this.#ps(e):this.#gs(e)}#gs(e){return`new NamedParameter("${e.name}", ${void 0!==e.value})`}#ms(e){return`new ArrayParameter([${this.#hs(e.members).join(", ")}])`}#ps(e){return`new ObjectParameter([${this.#hs(e.members).join(", ")}])`}};class Wr{#fs;#ws;constructor(e,t){const r=e.target;this.#fs=new Vr(r),this.#ws=new Br(r,t)}async build(e){await Promise.all([this.#fs.build(e),this.#ws.build(e)])}}class Kr{#ys;#vs;#bs;#Jt;constructor(e,t,r,s){this.#ys=e,this.#vs=t,this.#bs=r,this.#Jt=s}get source(){return this.#ys}get target(){return this.#vs}get resource(){return this.#bs}get segment(){return this.#Jt}}class Gr{#ts;#$;#Ss;#Ms;constructor(e,t){this.#ts=new q(t);const r=new Q(e.source),s=new Q(e.target),n=new Q(e.resources),a=new Q(e.segments);this.#$=new Kr(r,s,n,a),this.#Ss=new hr(this.#$),this.#Ms=new Wr(this.#$,this.#ts)}async build(){const e=this.#$.source,t=this.#$.resource,r=this.#$.segment,s=await e.filter(W),n=await t.filter(K),a=await r.filter(G),i=await this.#Ss.read(s,n,a);return this.#Ms.build(i)}}class Ur{#xs;constructor(e=!0){this.#xs=e}validate(e,t){const r=[];this.#Es("",e,t,r);return{valid:0===r.length,errors:r}}#Es(e,t,r,s){this.#xs&&this.#As(e,t,r,s),this.#Ps(e,t,r,s)}#As(e,t,r,s){const n=Object.keys(t),a=Object.keys(r);for(const t of n)if(!1===a.includes(t)){const r=this.#Is(e,t);s.push(`Unknown field '${r}'`)}}#Ps(e,t,r,s){const n=Object.keys(r);for(const a of n){const n=this.#Is(e,a),i=r[a],o=t[a];this.#Os(n,o,i,s)}}#Os(e,t,r,s){if(void 0!==t)switch(r.type){case"string":return this.#Rs(e,t,r,s);case"integer":return this.#ks(e,t,r,s);case"real":return this.#Ts(e,t,r,s);case"boolean":return this.#Ns(e,t,r,s);case"url":return this.#Cs(e,t,r,s);case"group":return this.#$s(e,t,r,s);case"list":return this.#Fs(e,t,r,s)}else!0===r.required&&s.push(`Field '${e}' is required`)}#Rs(e,t,r,s){"string"!=typeof t&&s.push(`Field '${e}' is not a string`)}#ks(e,t,r,s){"number"==typeof t&&!1!==Number.isInteger(t)||s.push(`Field '${e}' is not an integer`)}#Ts(e,t,r,s){"number"!=typeof t&&s.push(`Field '${e}' is not a real number`)}#Ns(e,t,r,s){"boolean"!=typeof t&&s.push(`Field '${e}' is not a boolean`)}#Cs(e,t,r,s){"string"==typeof t&&!1!==t.startsWith("http")||s.push(`Field '${e}' is not a valid URL`)}#$s(e,t,r,s){"object"==typeof t?this.#Es(e,t,r.fields,s):s.push(`Field '${e}' is not an object`)}#Fs(e,t,r,s){if(!Array.isArray(t))return void s.push(`Field '${e}' is not a list`);const n=t;for(const t in n){const a=this.#Is(e,t),i=n[t];this.#Os(a,i,r.items,s)}}#Is(e,t){return""===e?t:`${e}.${t}`}}class _r{async configure(e){i.config({path:e})}}class Jr extends Error{constructor(e){super(`Runtime configuration is invalid:\n${e.errors.join("\n")}`)}}const Yr="./jitar.json",Xr="./src",Qr="./dist",Zr="./segments",es="./resources",ts={source:{type:"string",required:!1},target:{type:"string",required:!1},segments:{type:"string",required:!1},resources:{type:"string",required:!1}};let rs=class{#Ls;#zs;constructor(e,t){this.#Ls=e,this.#zs=t}async build(e=Yr){const t=await this.#Ls.read(e),r=this.#zs.validate(t,ts);if(!1===r.valid)throw new Jr(r);return t.source??=Xr,t.target??=Qr,t.segments??=Zr,t.resources??=es,t}};const ss="\n => ";class ns extends Error{constructor(e){const t=e.errors.join(ss);super(`Invalid server configuration:${ss}${t}`)}}const as={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:{indexFilename:{type:"string",required:!1},serveIndexOnNotFound:{type:"boolean",required:!1},assets:{type:"list",required:!1,items:{type:"string"}}}},standalone:{type:"group",required:!1,fields:{segments:{type:"list",required:!0,items:{type:"string"}},indexFilename:{type:"string",required:!1},serveIndexOnNotFound:{type:"boolean",required:!1},assets:{type:"list",required:!1,items:{type:"string"}}}},worker:{type:"group",required:!1,fields:{gateway:{type:"url",required:!1},segments:{type:"list",required:!0,items:{type:"string"}},trustKey:{type:"string",required:!1},reportInterval:{type:"integer",required:!1}}},remoteWorker:{type:"group",required:!1,fields:{unavailableThreshold:{type:"integer",required:!1},stoppedThreshold:{type:"integer",required:!1}}}};class is{#Ls;#zs;constructor(e,t){this.#Ls=e,this.#zs=t}async build(e){const t=await this.#Ls.read(e),r=this.#zs.validate(t,as);if(!1===r.valid)throw new ns(r);return t}}class os extends Error{constructor(e){super(`${e} is not a valid configuration file.`)}}const cs=/\${([^}]*)}/g;class us{#$;constructor(e){this.#$=e}async read(e){if(!1===await this.#$.exists(e))return{};const t=await this.#$.read(e);if(!1===t.type.includes("json"))throw new os(e);const r=t.content.toString(),s=this.#js(r);return this.#Hs(s)}#js(e){return e.replace(cs,(e,t)=>process.env[t]??"null")}#Hs(e){return JSON.parse(e)}}class ls{#Ds;#qs;#Vs;constructor(e="./"){const t=new Q(e),r=new us(t),s=new Ur;this.#Ds=new _r,this.#qs=new rs(r,s),this.#Vs=new is(r,s)}configureEnvironment(e=".env"){return this.#Ds.configure(e)}getRuntimeConfiguration(e){return this.#qs.build(e)}getServerConfiguration(e){return this.#Vs.build(e)}}class hs{name="build";description="Builds the application (creates segment bundles).";options=[{key:"--env-file",required:!1,description:"Path to the environment file"},{key:"--config",required:!1,description:"Path to the configuration file",defaultValue:"jitar.json"},{key:"--log-level",required:!1,description:"Level of logging [info, debug, warn, error, fatal]",defaultValue:"info"}];async execute(e){const t=e.getOptionalArgument("--env-file",void 0),r=e.getOptionalArgument("--config",void 0),s=e.getOptionalArgument("--log-level",void 0),n=this.#Bs(s),a=new ls;await a.configureEnvironment(t);const i=await a.getRuntimeConfiguration(r);return new Gr(i,n).build()}#Bs(e){if(void 0===e)return;return(new B).parse(e)}}const ds="content-type",ms="x-content-type-options",ps="x-jitar-content-type",gs="x-jitar-procedure-version",fs="x-powered-by",ws="nosniff",ys="application/json",vs="application/octet-stream";class bs extends Error{constructor(){super("Invalid worker id")}}class Ss{#T;#Ws;#Cr=new $r;#zs=new Ur;constructor(e,t){this.#T=e,this.#Ws=t}connect(){return Promise.resolve()}disconnect(){return Promise.resolve()}async provide(e){const t=`${this.#T}/${e}`,r=await this.#Ks(t,{method:"GET"}),s=r.headers.get(ds)??vs,n=await r.arrayBuffer(),a=Buffer.from(n);return new J(e,s,a)}async isHealthy(){const e=`${this.#T}/health/status`,t=await this.#Ks(e,{method:"GET"});return"true"===await t.text()}async getHealth(){const e=`${this.#T}/health`,t=await this.#Ks(e,{method:"GET"}),r=await t.json();return new Map(Object.entries(r))}async addWorker(e,t,r){const s=`${this.#T}/workers`,n={url:e,procedureNames:t,trustKey:r},a={method:"POST",headers:{"Content-Type":ys},body:JSON.stringify(n)},i=await this.#Ks(s,a),o=i.headers.get(ds);if(null===o||!1===o.includes(ys))throw new bs;const c=await i.json();if(!1===this.#zs.validate(c,{id:{type:"string",required:!0}}).valid)throw new bs;return c.id}async reportWorker(e,t){const r=`${this.#T}/workers/${e}/report`,s={state:t},n={method:"POST",headers:{"Content-Type":ys},body:JSON.stringify(s)};await this.#Ks(r,n)}async removeWorker(e){const t=`${this.#T}/workers/${e}`,r={method:"DELETE",headers:{"Content-Type":ys}};await this.#Ks(t,r)}async run(e){e.setHeader(ds,ys);const t=Object.fromEntries(e.args),r=Object.fromEntries(e.headers),s=e.version.toString();r[gs]=s;const n=`${this.#T}/rpc/${e.fqn}`,a={method:"POST",redirect:"manual",headers:r,body:await this.#Gs(t)},i=await this.#Ks(n,a,!1),o=i.status,c=await this.#Us(i),u=this.#_s(i);return new b(o,c,u)}async#Ks(e,t,r=!0){const s=await this.#Ws.execute(e,t);if(r&&this.#Js(s)){const e=await this.#Us(s);throw this.#Cr.fromStatus(s.status,String(e))}return s}#Js(e){return e.status<200||e.status>399}async#Gs(e){return JSON.stringify(e)}async#Us(e){const t=e.headers.get(ps)??e.headers.get(ds);if(t?.includes("undefined"))return;if(t?.includes("null"))return null;if(t?.includes("json"))return e.json();const r=await e.text();return t?.includes("boolean")?"true"===r:t?.includes("number")?Number(r):r}#_s(e){const t=new Map;for(const[r,s]of e.headers)t.set(r,s);return t}}class Ms{async execute(e,t){return fetch(e,t)}}class xs{#Ws;constructor(e=new Ms){this.#Ws=e}build(e){return new Ss(e,this.#Ws)}}const Es={STARTING:"starting",AVAILABLE:"available",UNAVAILABLE:"unavailable",STOPPING:"stopping",STOPPED:"stopped"};class As{get url(){throw new m}get state(){return Es.AVAILABLE}start(){return Promise.resolve()}stop(){return Promise.resolve()}async isHealthy(){return!0}async getHealth(){return new Map}async updateState(){return Es.AVAILABLE}provide(e){throw new m}}class Ps{get url(){throw new m}get state(){return Es.AVAILABLE}get trustKey(){throw new m}start(){return Promise.resolve()}stop(){return Promise.resolve()}async isHealthy(){return!0}async getHealth(){return new Map}async updateState(){return Es.AVAILABLE}getProcedureNames(){throw new m}hasProcedure(e){throw new m}run(e){throw new m}}class Is{#Ys=Es.STOPPED;get state(){return this.#Ys}set state(e){this.#Ys=e}async start(e){if(!this.isStarted())try{this.setStarting(),await e()}catch(e){throw this.setStopped(),e}}async stop(e){this.isStopped()||(this.setStopping(),await e(),this.setStopped())}isStarted(){return this.#Xs(Es.STOPPED)}isStopped(){return this.#Qs(Es.STOPPED)}isAvailable(){return this.#Qs(Es.AVAILABLE)}setStarting(){this.#Ys=Es.STARTING}setAvailable(){this.#Ys=Es.AVAILABLE}setUnavailable(){this.#Ys=Es.UNAVAILABLE}setAvailability(e){return this.#Ys=e?Es.AVAILABLE:Es.UNAVAILABLE,this.#Ys}setStopping(){this.#Ys=Es.STOPPING}setStopped(){this.#Ys=Es.STOPPED}#Qs(...e){return e.includes(this.#Ys)}#Xs(...e){return!1===this.#Qs(...e)}}class Os extends f{constructor(){super("Invalid trust key")}}class Rs{#T;#Zs;#en;#tn;#rn=new Is;constructor(e){this.#T=e.url,this.#Zs=e.trustKey,this.#en=e.healthManager,this.#tn=e.workerManager}get url(){return this.#T}get state(){return this.#rn.state}get trustKey(){return this.#Zs}async start(){return this.#rn.start(async()=>{await Promise.all([this.#en.start(),this.#tn.start()]),await this.updateState()})}async stop(){return this.#rn.stop(async()=>{await Promise.all([this.#tn.stop(),this.#en.stop()])})}async isHealthy(){return this.#en.isHealthy()}async getHealth(){return this.#en.getHealth()}async updateState(){const e=await this.isHealthy();return this.#rn.setAvailability(e)}async addWorker(e){if(this.#sn(e.trustKey))throw new Os;return await e.start(),this.#tn.addWorker(e)}getWorker(e){return this.#tn.getWorker(e)}async reportWorker(e,t){return this.#tn.reportWorker(e,t)}async removeWorker(e){return this.#tn.removeWorker(e).stop()}getProcedureNames(){return this.#tn.getProcedureNames()}hasProcedure(e){return this.#tn.hasProcedure(e)}async run(e){return this.#tn.run(e)}#sn(e){return void 0!==this.#Zs&&e!==this.#Zs}}class ks{#T;#nn;#rn=new Is;constructor(e){this.#T=e.url,this.#nn=e.remote}get url(){return this.#T}get state(){return this.#rn.state}get trustKey(){}async start(){return this.#rn.start(async()=>{await this.#nn.connect(),await this.updateState()})}async stop(){return this.#rn.stop(async()=>{await this.#nn.disconnect()})}isHealthy(){return this.#nn.isHealthy()}getHealth(){return this.#nn.getHealth()}async updateState(){const e=await this.isHealthy();return this.#rn.setAvailability(e)}getProcedureNames(){throw new m}hasProcedure(e){throw new m}addWorker(e){return this.#nn.addWorker(e.url,e.getProcedureNames(),e.trustKey)}reportWorker(e,t){return this.#nn.reportWorker(e,t)}removeWorker(e){return this.#nn.removeWorker(e)}run(e){return this.#nn.run(e)}}class Ts extends w{#_;constructor(e){super(`No worker available for procedure '${e}'`),this.#_=e}get name(){return this.#_}}class Ns{#an=[];#in=0;get workers(){return this.#an}addWorker(e){this.#an.includes(e)||this.#an.push(e)}removeWorker(e){const t=this.#an.indexOf(e);-1!==t&&this.#an.splice(t,1)}getNextWorker(){const e=this.#an.filter(e=>e.isAvailable());if(0!==e.length)return this.#in>=e.length&&(this.#in=0),e[this.#in++];this.#in=0}async run(e){const t=this.getNextWorker();if(void 0===t)throw new Ts(e.fqn);return t.run(e)}}class Cs{generate(){return x.randomUUID()}}class $s extends w{#H;constructor(e){super(`Unknown worker id '${e}'`),this.#H=e}get id(){return this.#H}}class Fs{#an=new Map;#on=new Map;#cn=new Cs;#un;constructor(e,t){this.#un=e.create(()=>this.#ln(),t)}get workers(){return[...this.#an.values()]}get balancers(){return this.#on}start(){this.#un.start()}stop(){this.#un.stop()}getProcedureNames(){const e=this.workers.map(e=>e.getProcedureNames());return[...new Set(e.flat()).values()]}hasProcedure(e){return this.getProcedureNames().includes(e)}addWorker(e){e.id=this.#cn.generate(),this.#an.set(e.id,e);for(const t of e.getProcedureNames()){this.#hn(t).addWorker(e)}return e.id}getWorker(e){const t=this.#an.get(e);if(void 0===t)throw new $s(e);return t}reportWorker(e,t){this.getWorker(e).reportState(t)}removeWorker(e){const t=this.getWorker(e);this.#an.delete(e);for(const e of t.getProcedureNames()){const r=this.#dn(e);void 0!==r&&r.removeWorker(t)}return t}#dn(e){return this.#on.get(e)}#hn(e){let t=this.#dn(e);return void 0===t&&(t=new Ns,this.#on.set(e,t)),t}async run(e){const t=this.#dn(e.fqn);if(void 0===t)throw new Or(e.fqn);return t.run(e)}async#ln(){const e=this.workers.map(e=>this.#mn(e));await Promise.allSettled(e)}async#mn(e){await e.updateState()===Es.STOPPED&&this.removeWorker(e.id)}}class Ls{#T;#pn;#gn;#rn=new Is;constructor(e){this.#T=e.url,this.#pn=e.provider,this.#gn=e.runner}get url(){return this.#T}get state(){return this.#rn.state}get trustKey(){return this.#gn.trustKey}get provider(){return this.#pn}get runner(){return this.#gn}async start(){return this.#rn.start(async()=>{await Promise.all([this.#pn.start(),this.#gn.start()]),await this.updateState()})}async stop(){return this.#rn.stop(async()=>{await Promise.allSettled([this.#gn.stop(),this.#pn.stop()])})}async isHealthy(){const[e,t]=await Promise.all([this.#pn.isHealthy(),this.#gn.isHealthy()]);return e&&t}async getHealth(){const[e,t]=await Promise.all([this.#pn.getHealth(),this.#gn.getHealth()]);return new Map([...e,...t])}async updateState(){const e=await this.isHealthy();return this.#rn.setAvailability(e)}provide(e){return this.#pn.provide(e)}getProcedureNames(){return this.#gn.getProcedureNames()}hasProcedure(e){return this.#gn.hasProcedure(e)}run(e){return this.#gn.run(e)}}class zs{#T;#en;#fn;#wn;#yn;#vn;#rn=new Is;constructor(e){this.#T=e.url,this.#en=e.healthManager,this.#fn=e.sourcingManager,this.#wn=e.assets,this.#yn=e.indexFilename??"index.html",this.#vn=e.serveIndexOnNotFound??false}get url(){return this.#T}get state(){return this.#rn.state}async start(){return this.#rn.start(async()=>{await this.#en.start(),await this.updateState()})}async stop(){return this.#rn.stop(async()=>{await this.#en.stop()})}isHealthy(){return this.#en.isHealthy()}getHealth(){return this.#en.getHealth()}async updateState(){const e=await this.isHealthy();return this.#rn.setAvailability(e)}async provide(e){if(this.#bn(e))return this.provide(this.#yn);if(!1===this.#wn.has(e))throw new _(e);return this.#fn.read(e)}#bn(e){return""===e||e!==this.#yn&&(this.#vn&&!1===this.#wn.has(e))}}class js{#T;#nn;#rn=new Is;constructor(e){this.#T=e.url,this.#nn=e.remote}get url(){return this.#T}get state(){return this.#rn.state}async start(){return this.#rn.start(async()=>{await this.#nn.connect(),await this.updateState()})}async stop(){return this.#rn.stop(async()=>{await this.#nn.disconnect()})}isHealthy(){return this.#nn.isHealthy()}getHealth(){return this.#nn.getHealth()}async updateState(){const e=await this.isHealthy();return this.#rn.setAvailability(e)}provide(e){return this.#nn.provide(e)}}class Hs extends Error{constructor(e){super(`The class '${e}' could not be found`)}}class Ds extends Error{constructor(e){super(`The class '${e}' is invalid`)}}class qs extends Error{constructor(e){super(`No deserializer found for value of type '${e}'`)}}class Vs extends Error{constructor(e){super(`No serializer found for value of type '${e}'`)}}class Bs{#Sn=[];addSerializer(e){e.parent=this,this.#Sn.unshift(e)}async serialize(e){const t=this.#Sn.find(t=>t.canSerialize(e));if(void 0===t)throw new Vs(typeof e);return t.serialize(e)}async deserialize(e){const t=this.#Sn.find(t=>t.canDeserialize(e));if(void 0===t)throw new qs(typeof e);return t.deserialize(e)}}class Ws extends Error{constructor(){super("Parent serializer not set")}}class Ks{#Mn;set parent(e){this.#Mn=e}serializeOther(e){if(void 0===this.#Mn)throw new Ws;return this.#Mn.serialize(e)}deserializeOther(e){if(void 0===this.#Mn)throw new Ws;return this.#Mn.deserialize(e)}}class Gs extends Ks{canSerialize(e){return e instanceof Array}canDeserialize(e){return e instanceof Array}async serialize(e){const t=[];for(const r of e)t.push(await this.serializeOther(r));return t}async deserialize(e){return Promise.all(e.map(async e=>this.deserializeOther(e)))}}class Us extends Error{constructor(e){super(`Invalid BigInt string '${e}'`)}}class _s extends Ks{canSerialize(e){return"bigint"==typeof e}canDeserialize(e){const t=e;return t instanceof Object&&!0===t.serialized&&"BigInt"===t.name&&"string"==typeof t.value}async serialize(e){return{serialized:!0,name:"BigInt",value:e.toString()}}async deserialize(e){try{return BigInt(e.value)}catch{throw new Us(e.value)}}}class Js extends Error{constructor(e){super(`Invalid Buffer string '${e}'`)}}class Ys extends Ks{canSerialize(e){return e instanceof Buffer}canDeserialize(e){const t=e;return t instanceof Object&&!0===t.serialized&&"Buffer"===t.name&&"string"==typeof t.base64}async serialize(e){return{serialized:!0,name:"Buffer",base64:e.toString("base64")}}async deserialize(e){try{return Buffer.from(e.base64,"base64")}catch{throw new Js(e.base64)}}}const Xs=new Gt;class Qs extends Ks{#xn;constructor(e){super(),this.#xn=e}canSerialize(e){return e instanceof Object&&Xs.isClassObject(e)}canDeserialize(e){const t=e;return t instanceof Object&&!0===t.serialized&&"class"===t.name&&"string"==typeof t.key&&t.args instanceof Object&&t.args.constructor===Object&&t.fields instanceof Object&&t.fields.constructor===Object}async serialize(e){const t=Xs.getClass(e),r=Xs.fromClass(t,!0),s=this.#En(r),n=this.#xn.resolveKey(t);if(void 0===n)throw new Hs(t.name);return{serialized:!0,key:n,name:"class",args:await this.#An(r,s,e),fields:await this.#Pn(r,s,e)}}#En(e){const t=e.getFunction("constructor");return(t?.parameters??[]).map(e=>e.name)}async#An(e,t,r){const s={};for(const[n,a]of t.entries()){const t=e.canRead(a)?await this.serializeOther(r[a]):void 0;s[n.toString()]=t}return s}async#Pn(e,t,r){const s={};for(const n of e.writable){const a=n.name;t.includes(a)||!1===e.canRead(a)||(s[a]=await this.serializeOther(r[a]))}return s}async deserialize(e){const t=await this.#In(e);if(void 0===t)throw new Hs(e.key);if(t instanceof Function==!1)throw new Ds(e.key);const r=await this.#On(t,e.args),s=Xs.createInstance(t,r);for(const t in e.fields){const r=e.fields[t];s[t]=await this.deserializeOther(r)}return s}async#On(e,t){const r=Xs.fromClass(e,!0).getFunction("constructor"),s=(r?.parameters??[]).map((e,r)=>{const s=r.toString(),n=t[s];return this.deserializeOther(n)});return Promise.all(s)}async#In(e){return globalThis[e.key]??this.#xn.resolveClass(e.key)}}class Zs extends Error{constructor(e){super(`Invalid date string '${e}'`)}}class en extends Ks{canSerialize(e){return e instanceof Date}canDeserialize(e){const t=e;return t instanceof Object&&!0===t.serialized&&"Date"===t.name&&"string"==typeof t.value}async serialize(e){return{serialized:!0,name:"Date",value:e.toISOString()}}async deserialize(e){const t=new Date(e.value);if("Invalid Date"===t.toString())throw new Zs(e.value);return t}}class tn extends Ks{canSerialize(e){if(e instanceof Object==!1)return!1;const t=e;return t.constructor===Error||t.constructor===EvalError||t.constructor===RangeError||t.constructor===ReferenceError||t.constructor===SyntaxError||t.constructor===TypeError||t.constructor===URIError||t.constructor===AggregateError}canDeserialize(e){const t=e;return t instanceof Object&&!0===t.serialized&&"Error"===t.name&&t.type in globalThis}async serialize(e){return{serialized:!0,name:"Error",type:e.constructor.name,stack:e.stack,message:e.message,cause:e.cause}}async deserialize(e){const t=new(0,globalThis[e.type])(e.message,{cause:e.cause});return t.stack=e.stack,t}}class rn extends Ks{canSerialize(e){return e instanceof Map}canDeserialize(e){const t=e;return t instanceof Object&&!0===t.serialized&&"Map"===t.name&&t.entries instanceof Object&&t.entries.keys instanceof Array&&t.entries.values instanceof Array}async serialize(e){const t=[],r=[];for(const[s,n]of e)t.push(await this.serializeOther(s)),r.push(await this.serializeOther(n));return{serialized:!0,name:"Map",entries:{keys:t,values:r}}}async deserialize(e){const t=e.entries.keys,r=e.entries.values,s=new Map;for(let e=0;e<t.length;e++){const n=await this.deserializeOther(t[e]),a=await this.deserializeOther(r[e]);s.set(n,a)}return s}}class sn extends Ks{canSerialize(e){return e instanceof Object&&e.constructor===Object}canDeserialize(e){return e instanceof Object&&e.constructor===Object}async serialize(e){const t={};for(const r in e){const s=e[r];t[r]=await this.serializeOther(s)}return t}async deserialize(e){const t={};for(const r in e){const s=e[r];t[r]=await this.deserializeOther(s)}return t}}class nn extends Ks{canSerialize(e){return e instanceof Object==!1}canDeserialize(e){return e instanceof Object==!1}async serialize(e){return e}async deserialize(e){return e}}class an extends Error{constructor(e,t){super(`Invalid regular expression '${e}' with flags '${t}'`)}}class on extends Ks{canSerialize(e){return e instanceof RegExp}canDeserialize(e){const t=e;return t instanceof Object&&!0===t.serialized&&"RegExp"===t.name&&"string"==typeof t.source&&"string"==typeof t.flags}async serialize(e){return{serialized:!0,name:"RegExp",source:e.source,flags:e.flags}}async deserialize(e){try{return new RegExp(e.source,e.flags)}catch{throw new an(e.source,e.flags)}}}class cn extends Ks{canSerialize(e){return e instanceof Set}canDeserialize(e){const t=e;return t instanceof Object&&!0===t.serialized&&"Set"===t.name&&t.values instanceof Array}async serialize(e){const t=[];for(const r of e.values())t.push(await this.serializeOther(r));return{serialized:!0,name:"Set",values:t}}async deserialize(e){const t=await Promise.all(e.values.map(async e=>this.deserializeOther(e)));return new Set([...t])}}const un=new Gt;class ln extends Ks{canSerialize(e){return e instanceof Int8Array||e instanceof Uint8Array||e instanceof Uint8ClampedArray||e instanceof Int16Array||e instanceof Uint16Array||e instanceof Int32Array||e instanceof Uint32Array||e instanceof Float32Array||e instanceof Float64Array||e instanceof BigInt64Array||e instanceof BigUint64Array}canDeserialize(e){const t=e;return t instanceof Object&&!0===t.serialized&&"TypedArray"===t.name&&t.type in globalThis&&t.bytes instanceof Array}async serialize(e){const t=e.constructor.name,r=new DataView(e.buffer),s=[];for(let e=0;e<r.byteLength;e++)s.push(r.getUint8(e));return{serialized:!0,name:"TypedArray",type:t,bytes:s}}async deserialize(e){const t=e.type,r=e.bytes,s=new ArrayBuffer(r.length),n=new DataView(s);for(let e=0;e<r.length;e++)n.setUint8(e,r[e]);const a=globalThis[t];return un.createInstance(a,[s])}}class hn extends Error{constructor(e){super(`Invalid url string '${e}'`)}}class dn extends Ks{canSerialize(e){return e instanceof URL}canDeserialize(e){const t=e;return t instanceof Object&&!0===t.serialized&&"Url"===t.name&&"string"==typeof t.value}async serialize(e){return{serialized:!0,name:"Url",value:e.toString()}}async deserialize(e){try{return new URL(e.value)}catch{throw new hn(e.value)}}}class mn{static build(e){const t=new Bs;return t.addSerializer(new nn),t.addSerializer(new sn),void 0!==e&&t.addSerializer(new Qs(e)),t.addSerializer(new tn),t.addSerializer(new on),t.addSerializer(new _s),t.addSerializer(new dn),t.addSerializer(new en),t.addSerializer(new cn),t.addSerializer(new rn),t.addSerializer(new Gs),t.addSerializer(new ln),"undefined"!=typeof Buffer&&t.addSerializer(new Ys),t}}class pn{#Rn;constructor(e){this.#Rn=e}resolveKey(e){const t=this.#Rn.getClassByImplementation(e);return t?.fqn}resolveClass(e){const t=this.#Rn.getClass(e);return t?.implementation}}class gn extends f{constructor(){super("Request not trusted")}}const fn="X-Jitar-Trust-Key",wn="X-Jitar-Data-Encoding",yn="serialized";class vn{#H;#T;#Zs;#kn;#Tn;#Rn;#en;#Nn;#Cn;#rn=new Is;constructor(e){this.#T=e.url,this.#Zs=e.trustKey,this.#kn=e.gateway,this.#Tn=!0===e.registerAtGateway,this.#Rn=e.executionManager,this.#en=e.healthManager;const t=e.scheduleManager;this.#Nn=t.create(()=>this.#$n(),e.reportInterval);const r=new pn(this.#Rn);this.#Cn=mn.build(r)}get id(){return this.#H}set id(e){this.#H=e}get state(){return this.#rn.state}get url(){return this.#T}get trustKey(){return this.#Zs}async start(){return this.#rn.start(async()=>{await Promise.all([this.#Rn.start(),this.#en.start()]),void 0!==this.#kn&&(await this.#kn.start(),this.#Tn&&(this.#H=await this.#kn.addWorker(this),this.#Nn.start())),await this.updateState()})}async stop(){return this.#rn.stop(async()=>{void 0!==this.#kn&&(void 0!==this.#H&&(this.#Nn.stop(),await this.#kn.removeWorker(this.#H)),await this.#kn.stop()),await Promise.all([this.#en.stop(),this.#Rn.stop()])})}getProcedureNames(){return this.#Rn.getProcedureNames()}hasProcedure(e){return this.#Rn.hasProcedure(e)}isHealthy(){return this.#en.isHealthy()}getHealth(){return this.#en.getHealth()}isAvailable(){return this.#rn.isAvailable()}async updateState(){const e=await this.isHealthy();return this.#rn.setAvailability(e)}async reportState(e){if(void 0!==this.#kn&&void 0!==this.#H)return this.#kn.reportWorker(this.#H,e)}async run(e){return this.#Fn(e)?this.#Ln(e):this.#zn(e)}#Fn(e){return void 0===this.#kn||this.#Rn.hasProcedure(e.fqn)}async#Ln(e){const t=this.#Rn.getProcedure(e.fqn),r=t?.getImplementation(e.version);if(this.#jn(r))throw new Or(e.fqn);if(this.#Hn(e,r))throw new gn;const s=e.getHeader(wn);s===yn&&(e=await this.#Dn(e)),e.removeHeader(wn);const n=await this.#Rn.run(e);return s===yn?this.#qn(n):n}#jn(e){return void 0===e||e.private}#Hn(e,t){if(t.public)return!1;const r=e.getHeader(fn);return this.#Zs!==r}async#zn(e){(e=await this.#Vn(e)).setHeader(wn,yn),void 0!==this.#Zs&&e.setHeader(fn,this.#Zs);const t=await this.#kn.run(e);return this.#Bn(t)}async#Vn(e){const t=new Map;for(const[r,s]of e.args){const e=await this.#Cn.serialize(s);t.set(r,e)}return new M(e.fqn,e.version,t,e.headers,e.mode)}async#Dn(e){const t=new Map;for(const[r,s]of e.args){const e=await this.#Cn.deserialize(s);t.set(r,e)}return new M(e.fqn,e.version,t,e.headers,e.mode)}async#qn(e){const t=await this.#Cn.serialize(e.result);return new b(e.status,t,e.headers)}async#Bn(e){const t=await this.#Cn.deserialize(e.result);return new b(e.status,t,e.headers)}async#$n(){const e=await this.updateState();return this.reportState(e)}}class bn extends Is{#Wn;#Kn;#Gn=Date.now();#Un=Date.now();constructor(e=6e3,t=18e3){super(),this.#Wn=e,this.#Kn=t}report(e){this.#Gn=Date.now(),this.state=e}update(){this.#Un=Date.now();const e=this.#Un-this.#Gn;return e>=this.#Wn&&(this.state=e>=this.#Kn?Es.STOPPED:Es.UNAVAILABLE),this.state}}class Sn{#H;#T;#Zs;#_n;#nn;#rn;constructor(e){this.#T=e.url,this.#Zs=e.trustKey,this.#_n=e.procedureNames,this.#nn=e.remote,this.#rn=new bn(e.unavailableThreshold,e.stoppedThreshold)}get id(){return this.#H}set id(e){this.#H=e}get state(){return this.#rn.state}get url(){return this.#T}get trustKey(){return this.#Zs}async start(){return this.#rn.start(async()=>{await this.#nn.connect(),await this.updateState()})}async stop(){return this.#rn.stop(async()=>{await this.#nn.disconnect()})}getProcedureNames(){return[...this.#_n.values()]}hasProcedure(e){return this.#_n.has(e)}isHealthy(){return this.#nn.isHealthy()}getHealth(){return this.#nn.getHealth()}isAvailable(){return this.#rn.isAvailable()}async updateState(){return this.#rn.update()}async reportState(e){this.#rn.report(e)}run(e){return this.#nn.run(e)}}class Mn{#Jr;#Wn;#Kn;constructor(e,t,r){this.#Jr=e,this.#Wn=t,this.#Kn=r}build(e,t,r){const s=this.#Jr.build(e),n=new Set(t),a=this.#Wn,i=this.#Kn;return new Sn({url:e,trustKey:r,remote:s,procedureNames:n,unavailableThreshold:a,stoppedThreshold:i})}}class xn{#gn;constructor(e){this.#gn=e}async handle(e,t){return this.#gn.run(e)}}class En{#rs=new Lr;constructor(){this.#Jn()}#Jn(){const e=globalThis;e.__run=this.#Yn.bind(this),e.ProcedureNotAccessible=Rr}async#Yn(e,t,r,s){const n=this.#rs.parse(t),a=new Map(Object.entries(r)),i=s instanceof M?s.headers:new Map,o=new M(e,n,a,i,fr),c=this.getTrustKey();void 0!==c&&o.setHeader("X-Jitar-Trust-Key",c);const u=await this.runInternal(o);if(u.status!==yr)throw u.result;return u.result}}const An="starting",Pn="started",In="stopping",On="stopped";class Rn extends w{constructor(){super("Invalid health check")}}class kn{#Ys=On;#Xn=new Map;#kr;#Qn;constructor(e,t=[]){this.#kr=e,this.#Qn=t}get state(){return this.#Ys}async start(){if(this.#Ys===On)try{this.#Ys=An,await this.#Zn(),this.#Ys=Pn}catch(e){throw this.#Ys=On,e}}async stop(){if(this.#Ys===Pn)try{this.#Ys=In,this.clearHealthChecks(),this.#Ys=On}catch(e){throw this.#Ys=Pn,e}}async loadHealthCheck(e){const t=await this.#ea(e);this.addHealthCheck(t)}addHealthCheck(e){if(void 0===e.isHealthy)throw new Rn;this.#Xn.set(e.name,e)}clearHealthChecks(){this.#Xn.clear()}async isHealthy(){const e=[];for(const t of this.#Xn.values()){const r=this.#ta(t);e.push(r)}return Promise.all(e).then(e=>e.every(e=>e)).catch(()=>!1)}async getHealth(){const e=[];for(const[t,r]of this.#Xn){const s=this.#ta(r).then(e=>({name:t,isHealthy:e})).catch(()=>({name:t,isHealthy:!1}));e.push(s)}const t=new Map;return Promise.allSettled(e).then(e=>e.forEach(e=>this.#ra(e,t))).then(()=>t)}async#Zn(){(await Promise.all(this.#Qn.map(e=>this.#ea(e)))).forEach(e=>this.addHealthCheck(e))}async#ea(e){return(await this.#kr.import(e)).default}#ra(e,t){"fulfilled"===e.status?t.set(e.value.name,e.value.isHealthy):t.set(e.reason.name,!1)}async#ta(e){const t=e.isHealthy(),r=e.timeout;if(void 0===r)return t;const s=new Promise(e=>{setTimeout(e,r)}).then(()=>!1);return Promise.race([s,t])}}const Tn="starting",Nn="started",Cn="stopping",$n="stopped";class Fn extends w{constructor(){super("Invalid middleware")}}class Ln{#Ys=$n;#sa=[];#kr;#na;constructor(e,t=[]){this.#kr=e,this.#na=t}get state(){return this.#Ys}async start(){if(this.#Ys===$n)try{this.#Ys=Tn,await this.#aa(),this.#Ys=Nn}catch(e){throw this.#Ys=$n,e}}async stop(){if(this.#Ys===Nn)try{this.#Ys=Cn,this.clearMiddlewares(),this.#Ys=$n}catch(e){throw this.#Ys=Nn,e}}async loadMiddleware(e){const t=await this.#ia(e);this.addMiddleware(t)}addMiddleware(e){if(void 0===e?.handle)throw new Fn;this.#sa.push(e)}getMiddleware(e){return this.#sa.find(t=>t instanceof e)}clearMiddlewares(){this.#sa=[]}handle(e){return this.#oa(e,0)()}async#aa(){(await Promise.all(this.#na.map(e=>this.#ia(e)))).forEach(e=>this.addMiddleware(e))}async#ia(e){return(await this.#kr.import(e)).default}#oa(e,t){const r=this.#sa[t];if(void 0===r)return async()=>new b(yr);const s=this.#oa(e,t+1);return async()=>await r.handle(e,s)}}class zn{#ts;#ca;#ua;#la=null;constructor(e,t,r=5e3){this.#ts=e,this.#ca=t,this.#ua=r}start(){this.#ha()}stop(){null!==this.#la&&(clearTimeout(this.#la),this.#la=null)}#ha(){this.#la=setTimeout(async()=>{null!==this.#la&&(await this.#da(),this.#ha())},this.#ua)}async#da(){try{await this.#ca()}catch(e){this.#ts.warn("Scheduled task failed",e)}}}class jn{#ma=[];#ts;constructor(e){this.#ts=e}create(e,t){const r=new zn(this.#ts,e,t);return this.#ma.push(r),r}remove(e){const t=this.#ma.indexOf(e);t<0||this.#ma.splice(t,1)}startAll(){this.#ma.forEach(e=>e.start())}stopAll(){this.#ma.forEach(e=>e.stop())}}const Hn="application/null",Dn="application/undefined",qn="application/boolean",Vn="application/number",Bn="application/json",Wn="text/plain",Kn=200,Gn=400,Un=401,_n=402,Jn=403,Yn=404,Xn=418,Qn=500,Zn=501;class ea extends En{#pa;#ga;#fa;#wa;#ts;#rs=new Lr;constructor(e){super(),this.#pa=e.proxy,this.#ga=e.remoteWorkerBuilder,this.#fa=e.resourceManager,this.#wa=e.middlewareManager,this.#ts=e.logger}get proxy(){return this.#pa}getTrustKey(){return this.#pa.trustKey}async start(){await this.#ya(),this.#ts.info(`Server started at ${this.#pa.url}`),this.#pa.runner instanceof vn&&this.#ts.info("RPC procedures:",this.#pa.runner.getProcedureNames())}async stop(){await this.#va(),this.#ts.info("Server stopped")}async getHealth(){try{const e=await this.#pa.getHealth();return this.#ts.debug("Got health"),this.#ba(e)}catch(e){const t=e instanceof Error?e.message:String(e);return this.#ts.error("Failed to get health:",t),this.#Sa(e)}}async isHealthy(){try{const e=await this.#pa.isHealthy();return this.#ts.debug("Got health status"),this.#Ma(e)}catch(e){const t=e instanceof Error?e.message:String(e);return this.#ts.error("Failed to get health status:",t),this.#Sa(e)}}async provide(e){try{const t=await this.#pa.provide(e.filename);return this.#ts.info("Provided file:",e.filename),this.#xa(t)}catch(e){const t=e instanceof Error?e.message:String(e);return this.#ts.warn("Failed to provide file:",t),this.#Sa(e)}}async run(e){try{const t=this.#Ea(e),r=await this.#wa.handle(t);return this.#ts.info("Ran request:",t.fqn),this.#Aa(r)}catch(e){const t=e instanceof Error?e.message:String(e);return this.#ts.error("Failed run request:",t),this.#Sa(e)}}async runInternal(e){return this.#pa.run(e)}async addWorker(e){try{const t=this.#Pa(),r=this.#ga.build(e.url,e.procedureNames,e.trustKey),s=await t.addWorker(r);return this.#ts.info("Added worker:",r.url),this.#Ia({id:s})}catch(e){const t=e instanceof Error?e.message:String(e);return this.#ts.error("Failed to add worker:",t),this.#Sa(e)}}async reportWorker(e){try{const t=this.#Oa(e.state),r=this.#Pa();return await r.reportWorker(e.id,t),this.#ts.debug("Reported worker:",e.id),this.#Ia()}catch(e){const t=e instanceof Error?e.message:String(e);return this.#ts.error("Failed to report worker:",t),this.#Sa(e)}}async removeWorker(e){try{const t=this.#Pa();return await t.removeWorker(e.id),this.#ts.info("Removed worker:",e.id),this.#Ia()}catch(e){const t=e instanceof Error?e.message:String(e);return this.#ts.error("Failed to remove worker:",t),this.#Sa(e)}}async#ya(){await this.#fa.start(),await Promise.all([this.#pa.start(),this.#wa.start()]);const e=new xn(this.#pa);this.#wa.addMiddleware(e)}async#va(){await Promise.all([this.#wa.stop(),this.#pa.stop()]),await this.#fa.stop()}#Ea(e){const t=this.#Ra(e.fqn),r=this.#ka(e.version),s=this.#Ta(e.args),n=this.#Na(e.headers);return new M(t,r,s,n,e.mode)}#Ra(e){if(0===e.length)throw new o("Missing procedure name");if(e.includes(".."))throw new o("Invalid procedure name");return e}#ka(e){return"string"!=typeof e?y.DEFAULT:this.#rs.parse(e)}#Ta(e){return new Map(Object.entries(e))}#Na(e){const t=new Map;for(const[r,s]of Object.entries(e)){if(void 0===s)continue;const e=r.toLowerCase(),n=s.toString();t.set(e,n)}return t}#Oa(e){if(!1===Object.values(Es).includes(e))throw new o("Invalid state value");return e}#ba(e){return{result:Object.fromEntries(e),contentType:Bn,headers:{},status:Kn}}#Ma(e){return{result:e,contentType:qn,headers:{},status:Kn}}#xa(e){return{result:e.content,contentType:e.type,headers:{},status:Kn}}#Aa(e){const t=e.result instanceof Error?e.result.message:e.result;return{result:t,contentType:this.#Ca(t),headers:this.#$a(e.headers),status:e.status}}#Sa(e){const t=e instanceof Error?e.message:String(e);return{result:t,contentType:this.#Ca(t),headers:{},status:this.#Fa(e)}}#Ia(e){return{result:e,contentType:this.#Ca(e),headers:{},status:Kn}}#Ca(e){if(void 0===e)return Dn;if(null===e)return Hn;switch(typeof e){case"boolean":return qn;case"number":return Vn;case"object":return Bn;default:return Wn}}#$a(e){return Object.fromEntries(e)}#Fa(e){return e instanceof o?Gn:e instanceof u?Jn:e instanceof c?Yn:e instanceof m?Zn:e instanceof p?_n:e instanceof g?Xn:e instanceof f?Un:Qn}#Pa(){const e=this.#pa.runner;if(e instanceof Rs==!1)throw new o("Cannot remove worker from remote gateway");return e}}class ta extends Error{constructor(){super("Unknown service configured")}}class ra{#fn;#La;#za;constructor(e,t=[],r=[]){this.#fn=e,this.#La=t,this.#za=r}async start(){return this.#ja()}async stop(){return this.#Ha()}#ja(){return this.#Da(this.#La)}#Ha(){return this.#Da(this.#za)}async#Da(e){await Promise.all(e.map(e=>this.#fn.import(e)))}}class sa{#fn;#Jr;constructor(e,t){this.#fn=e,this.#Jr=t}async build(e,t){const r=e.setUp,s=e.tearDown,n=e.middleware,a=e.healthChecks,i=new q(t),o=this.#qa(a),c=this.#Va(i),u=await this.#Ba(e,o,c),l=this.#fn,h=this.#Wa(r,s),d=this.#Ka(n),m=this.#Ga(e);return new ea({proxy:u,sourcingManager:l,resourceManager:h,middlewareManager:d,remoteWorkerBuilder:m,logger:i})}#Ba(e,t,r){if(void 0!==e.gateway)return this.#Ua(e.url,e.gateway,t,r);if(void 0!==e.worker)return this.#_a(e.url,e.worker,t,r);if(void 0!==e.repository)return this.#Ja(e.url,e.repository,t);if(void 0!==e.proxy)return this.#Ya(e.url,e.proxy);if(void 0!==e.standalone)return this.#Xa(e.url,e.standalone,t,r);throw new ta}async#Ua(e,t,r,s){const n=new As,a=this.#Qa(e,t,r,s);return new Ls({url:e,provider:n,runner:a})}async#_a(e,t,r,s){const n=new As,a=this.#Za(e,t,r,s);return new Ls({url:e,provider:n,runner:a})}async#Ja(e,t,r){const s=await this.#ei(e,t,r),n=new Ps;return new Ls({url:e,provider:s,runner:n})}#Qa(e,t,r,s){const n=t.trustKey,a=t.monitorInterval,i=new Fs(s,a);return new Rs({url:e,trustKey:n,healthManager:r,workerManager:i})}#ti(e){const t=this.#Jr.build(e);return new ks({url:e,remote:t})}#Za(e,t,r,s){const n=t.trustKey,a=t.gateway?this.#ti(t.gateway):void 0,i=t.reportInterval,o=void 0!==a,c=this.#ri(t.segments);return new vn({url:e,trustKey:n,reportInterval:i,gateway:a,registerAtGateway:o,executionManager:c,healthManager:r,scheduleManager:s})}async#ei(e,t,r){const s=this.#fn,n=await this.#si(t.assets),a=t.indexFilename,i=t.serveIndexOnNotFound;return new zs({url:e,sourcingManager:s,assets:n,indexFilename:a,serveIndexOnNotFound:i,healthManager:r})}#ni(e){const t=this.#Jr.build(e);return new js({url:e,remote:t})}async#Ya(e,t){const r=this.#ni(t.repository),s=this.#ti(t.gateway);return new Ls({url:e,provider:r,runner:s})}async#Xa(e,t,r,s){const n=await this.#ei(e,t,r),a=this.#Za(e,t,r,s);return new Ls({url:e,provider:n,runner:a})}#Wa(e=[],t=[]){const r=e.map(e=>this.#ai(e)),s=t.map(e=>this.#ai(e));return new ra(this.#fn,r,s)}#qa(e=[]){const t=e.map(e=>this.#ai(e));return new kn(this.#fn,t)}#Ka(e=[]){const t=e.map(e=>this.#ai(e));return new Ln(this.#fn,t)}#ri(e=[]){const t=e.map(e=>`./${e}.segment.js`);return new Dr(this.#fn,t)}#Va(e){return new jn(e)}#Ga(e){const t=e.remoteWorker?.unavailableThreshold,r=e.remoteWorker?.stoppedThreshold;return new Mn(this.#Jr,t,r)}async#si(e){if(void 0===e)return new Set;const t=await this.#fn.filter(...e);return new Set(t)}#ai(e){return e.endsWith(".js")?e:e+".js"}}const na="3000",aa=204800,ia=["host","connection","content-length","accept-encoding","user-agent","keep-alive"];class oa{#ii;#oi;#ci;#ui;#zs=new Ur;constructor(e,t=na,r=aa){this.#ii=e,this.#oi=t,this.#ci=E(),this.#li(r),this.#hi()}async start(){return await this.#ii.start(),this.#di()}async stop(){return await this.#mi(),this.#ii.stop()}#li(e){this.#ci.use(E.json({limit:e})),this.#ci.use(E.urlencoded({extended:!0})),this.#ci.use(this.#pi.bind(this)),this.#ci.disable(fs)}#hi(){this.#ci.get("/health",(e,t)=>{this.#gi(e,t)}),this.#ci.get("/health/status",(e,t)=>{this.#fi(e,t)}),this.#ci.get("/rpc/*procedure",(e,t)=>{this.#wi(e,t)}),this.#ci.post("/rpc/*procedure",(e,t)=>{this.#yi(e,t)}),this.#ci.options("/rpc/*procedure",(e,t)=>{this.#vi(e,t)}),this.#ci.post("/workers",(e,t)=>{this.#bi(e,t)}),this.#ci.post("/workers/:id/report",(e,t)=>{this.#Si(e,t)}),this.#ci.delete("/workers/:id",(e,t)=>{this.#Mi(e,t)}),this.#ci.use((e,t)=>{this.#xi(e,t)})}#di(){return void 0!==this.#ui?Promise.resolve():new Promise((e,t)=>{this.#ui=this.#ci.listen(this.#oi,t),e()})}#mi(){return void 0===this.#ui?Promise.resolve():new Promise(e=>{this.#ui.close(()=>e())})}#pi(e,t,r){t.setHeader(ms,ws),r()}async#gi(e,t){const r=await this.#ii.getHealth();return this.#Ei(t,r)}async#fi(e,t){const r=await this.#ii.isHealthy();return this.#Ei(t,r)}async#wi(e,t){const r=this.#Ai(e),s=this.#Pi(e),n=this.#Ii(e),a=this.#Oi(e),i=fr,o=await this.#ii.run({fqn:r,version:s,args:n,headers:a,mode:i});return this.#Ei(t,o)}async#yi(e,t){const r=this.#Ai(e),s=this.#Pi(e),n=this.#Ri(e),a=this.#Oi(e),i=fr,o=await this.#ii.run({fqn:r,version:s,args:n,headers:a,mode:i});return this.#Ei(t,o)}async#vi(e,t){const r=this.#Ai(e),s=this.#Pi(e),n=this.#Ri(e),a=this.#Oi(e),i=wr,o=await this.#ii.run({fqn:r,version:s,args:n,headers:a,mode:i});return this.#Ei(t,o)}async#bi(e,t){const r=this.#Ri(e),s=this.#zs.validate(r,{url:{type:"url",required:!0},procedureNames:{type:"list",required:!0,items:{type:"string"}},trustKey:{type:"string",required:!1}});if(!1===s.valid)return t.status(400).send(s.errors.join("\n"));const n=r.url,a=r.procedureNames,i=r.trustKey;try{const e=await this.#ii.addWorker({url:n,procedureNames:a,trustKey:i});return this.#Ei(t,e)}catch(e){const r=e instanceof Error?e.message:"Server error";return t.status(500).send(r)}}async#Si(e,t){const r={id:e.params.id,state:e.body.state},s=this.#zs.validate(r,{id:{type:"string",required:!0},state:{type:"string",required:!0}});if(!1===s.valid)return t.status(400).send(s.errors.join("\n"));const n=r.id,a=r.state;try{const e=await this.#ii.reportWorker({id:n,state:a});return this.#Ei(t,e)}catch(e){const r=e instanceof Error?e.message:"Server error";return t.status(500).send(r)}}async#Mi(e,t){const r={id:e.params.id},s=this.#zs.validate(r,{id:{type:"string",required:!0}});if(!1===s.valid)return t.status(400).send(s.errors.join("\n"));const n=r.id;try{const e=await this.#ii.removeWorker({id:n});return this.#Ei(t,e)}catch(e){const r=e instanceof Error?e.message:"Server error";return t.status(500).send(r)}}async#xi(e,t){const r=e.path.substring(1).trim(),s=decodeURIComponent(r),n=await this.#ii.provide({filename:s});return this.#Ei(t,n)}#Ai(e){return decodeURIComponent(e.path.trim()).substring(5).trim()}#Pi(e){const t=e.headers[gs];return Array.isArray(t)?t[0]:t}#Ii(e){const t={};for(const[r,s]of Object.entries(e.query))t[r]=s;return t}#Ri(e){return e.body}#Oi(e){const t={};for(const[r,s]of Object.entries(e.headers)){if(void 0===s)continue;const e=r.toLowerCase(),n=s.toString();ia.includes(e)||(t[e]=n)}return t}#Ei(e,t){const r=this.#ki(t),s=this.#Ti(t);e.status(r),e.setHeader(ds,s),e.setHeader(ps,t.contentType);for(const[r,s]of Object.entries(t.headers))e.setHeader(r,s);const n=this.#Ni(t);return e.send(n)}#ki(e){return void 0!==e.headers.location?302:e.status}#Ti(e){const t=e.contentType.toLowerCase();switch(t){case qn:case Vn:case Dn:case Hn:return Wn}return t}#Ni(e){const t=e.result;return"number"==typeof t||"boolean"==typeof t?String(t):t}}class ca{name="start";description="Starts a server with the configured service.";options=[{key:"--service",required:!0,description:"Path to the service configuration file"},{key:"--env-file",required:!1,description:"Path to the environment file"},{key:"--config",required:!1,description:"Path to the configuration file",defaultValue:"jitar.json"},{key:"--log-level",required:!1,description:"Level of logging [info, debug, warn, error, fatal]",defaultValue:"info"},{key:"--http-body-limit",required:!1,description:"Maximum HTTP body size in bytes",defaultValue:204800}];async execute(e){const t=e.getOptionalArgument("--env-file",void 0),r=e.getOptionalArgument("--config",void 0),s=e.getRequiredArgument("--service"),n=e.getOptionalArgument("--log-level",void 0),a=this.#Bs(n),i=e.getOptionalArgument("--http-body-limit",void 0),o=this.#Ci(i),c=new ls;await c.configureEnvironment(t);const u=await c.getRuntimeConfiguration(r),l=await c.getServerConfiguration(s),h=await this.#$i(u,l,o,a);return this.#Fi(h)}async#$i(e,t,r,s){const[,,n]=t.url.split(":"),a=new ne(e.target),i=new xs,o=new sa(a,i),c=await o.build(t,s);return new oa(c,n,r)}#Fi(e){let t=!1;return process.on("SIGINT",async()=>{t||(t=!0,e.stop())}),console.log("\n ██╗██╗████████╗ █████╗ ██████╗ \n ██║██║╚══██╔══╝██╔══██╗██╔══██╗\n ██║██║ ██║ ███████║██████╔╝\n██ ██║██║ ██║ ██╔══██║██╔══██╗\n╚█████╔╝██║ ██║ ██║ ██║██║ ██║\n ╚════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝\n ~ Distributed JavaScript Runtime ~\n____________________________________\n"),e.start()}#Bs(e){if(void 0===e)return;return(new B).parse(e)}#Ci(e){const t=Number.parseInt(e);return Number.isNaN(t)?void 0:t}}class ua extends Error{constructor(e){super(`Command ${e} not found`)}}class la{#s=new Set;constructor(){this.#s.add(new I(this.#s)),this.#s.add(new O),this.#s.add(new R),this.#s.add(new F),this.#s.add(new hs),this.#s.add(new ca)}run(e,t){for(const r of this.#s)if(r.name===e)return r.execute(t);throw new ua(e)}}class ha{#Li;#zi;constructor(){this.#Li=new P(process.argv),this.#zi=new la}start(){const e=this.#Li.getCommand()??"help";return this.#zi.run(e,this.#Li)}}try{const e=new ha;await e.start()}catch(e){const t=new q,r=e instanceof Error?e.message:String(e);t.fatal(r)}
|