import { Gateway } from "../Engine/Gateway.js"; import { CMessage, CRequest, DatagramShort, } from "../Independents/Datagram/datagram.js" import { BurpaDriver, BurpanetConfig } from "../Engine/types.js"; import { MachineInit } from "../Independents/Machine.js"; import { HostName, HostNameTemplate, HostNameBase } from "../Independents/DNS/HostName.js"; import { RandomString } from "../Independents/RememberableNames/random-host-name.js"; import { Vagrant } from "../Engine/Host.js" import { IHost } from "../Engine/Host.js"; import { Request, Response, RequestInit, RequestInfo, Message, MessageInfo, MessageInit, RequestInitObject, MessageInitObject } from "../Independents/Datagram/datagram-types.js" import { burpaFetchSubscribed } from "../Engine/fetch.js"; import { Subscription } from "../Independents/util/Subscription.js"; import { updateRoots, CBurpastateWrapper, newBurpaState, newVagrantId } from "../Engine/burpastate.js"; import { processType } from "../Independents/util/misc.js"; import { OpenPromise } from "../Independents/util/OpenPromise.js"; import { config } from "process"; export const Burpanet = new class { constructor() { if (processType() == "browser") { this.state = new CBurpastateWrapper(newBurpaState()); } else { this.state = null as unknown as CBurpastateWrapper; } } // state: BurpaState = { // processId: RandomString(), // localHosts2: [] // } // get rootGateway2(): HostName | null { // if (Burpastate.whole.persistent.rootGateway == null) { // return null; // } // return new HostName(Burpastate.whole.persistent.rootGateway); // }; // set rootGateway2(hn: HostName | null) { // Burpastate.whole.persistent.rootGateway = hn ? hn.name : null; // }; // get rootHost2(): HostName | null { // if (Burpastate.whole.persistent.rootHost == null) { // return null; // } // return new HostName(Burpastate.whole.persistent.rootHost); // } // set rootHost2(hn: HostName | null) { // Burpastate.whole.persistent.rootHost = hn ? hn.name : null; // }; ssl: boolean = false; // addVagrant(host: string, init: MachineInit) { // AddVagrant(new Vagrant([host], init)); // } async spawnHost(hostTemplate: string, init: MachineInit, secret?: string): Promise { await this.whenInit; return this.state.spawnHost(hostTemplate, init, secret); }; send(message: MessageInitObject) { this.whenInit.then(() => { console.log("Mission is to send datagram ", message); let msg = new CMessage(message); console.log("sending", DatagramShort(msg)); msg.from = this.state.self.persistent.localHost!; this.state.LocalGateway().sendDatagram(msg); }); } // async spawnAlias2(hostTemplate: string, host: THost, secret?: string): Promise { // let templateForNewHost = this.state.hostNameTemplate(hostTemplate) // let gateway = await this.state.LocalGateway().registerAtUpstream(templateForNewHost.nameTemplate); // let request: Request = new CRequest({ // port: "burpasys", // from: this.state.LocalGateway().alias.name, // to: templateForNewHost.parent, // method: "POST", // path: "/children", // body: { // template: templateForNewHost.reconstructed, // gateway: gateway, // secret: secret ?? "" // } // }); // let response = await this.state.LocalGateway().sendRequest(request).current; // if (response.status == 201) { // updateRoots(this.state, response.body); // let hostname = response.body.host; // this.state.whole.persistent.aliases[hostname] = host.hostId; // console.log(`Host ${host} now has alias ${hostname}`); // return hostname; // } // return Promise.resolve(null); // }; async fetch(input: RequestInfo, init?: RequestInit): Promise { await this.whenInit; return (await burpaFetchSubscribed(this.state, input, init)).current; }; async fetchSubscribed(input: RequestInfo, init?: RequestInit): Promise> { await this.whenInit; return burpaFetchSubscribed(this.state, input, init); }; async deleteHost(host: string, secret: string): Promise { await this.whenInit; throw "todo deleteHost" }; state: CBurpastateWrapper; whenInit = new OpenPromise(); async assureInit(): Promise { if (this.state.whole.persistent.localGateway) { return Promise.resolve(this.state.whole.persistent.localGateway); } return Burpanet.init({}); } async init(config: BurpanetConfig): Promise { //console.warn("INITIALIZE PUBLIC API 1") // Burpanet.state = { // processId: RandomString(), // localHosts2: [] // } if (config.localGateway) { this.state.whole.persistent.localGateway = config.localGateway.gateway; } //console.log("Creating LocalGateway", this.state.whole.persistent.localGateway); this.state.addGateway_Old(this.state.whole.persistent.localGateway); if (config.localHost) { let lh = this.state.whole.persistent.localHost = config.localHost?.host ?? newVagrantId(); if (!this.state.whole.persistent.hosts[lh]) { this.state.addVagrant_Old(this.state.whole.persistent.localHost, {}); } } updateRoots(this.state, config); // if (config.rootHost) { // if (config.rootHost == this.state.whole.persistent.localHost) { // lh.aliases.push(config.rootHost); // } // } let gwhost: string; // if (config.localGateway) { // debugger // gwhost = await this.state.LocalGateway().spawnAlias(this.state.whole, this.state.whole.persistent.localGateway); // debugger // } else { gwhost = this.state.whole.persistent.localGateway; // } this.whenInit.resolve(undefined); return Promise.resolve(gwhost); } setDriver(driver: BurpaDriver): void { this.state.whole.transient.driver = driver; } }