chore: Adding mainFn helpers

This commit is contained in:
BluJ
2023-04-07 11:43:05 -06:00
parent 74e765511e
commit 49277bfc78
35 changed files with 457 additions and 183 deletions

View File

@@ -0,0 +1,4 @@
declare const AddressProof: unique symbol;
export type AddressReceipt = {
[AddressProof]: never;
};

View File

@@ -0,0 +1,11 @@
import { Origin } from "./Origin";
export class LocalBinding {
constructor(readonly localAddress: string, readonly ipAddress: string) {}
createOrigins(protocol: string) {
return {
local: new Origin(protocol, this.localAddress),
ip: new Origin(protocol, this.ipAddress),
};
}
}

13
lib/mainFn/LocalPort.ts Normal file
View File

@@ -0,0 +1,13 @@
import { Effects } from "../types";
import { LocalBinding } from "./LocalBinding";
export class LocalPort {
constructor(readonly id: string, readonly effects: Effects) {}
async bindLan(internalPort: number) {
const [localAddress, ipAddress] = await this.effects.bindLan({
internalPort,
name: this.id,
});
return new LocalBinding(localAddress, ipAddress);
}
}

View File

@@ -0,0 +1,17 @@
import { Effects } from "../types";
import { LocalPort } from "./LocalPort";
import { TorHostname } from "./TorHostname";
export class NetworkBuilder {
static of(effects: Effects) {
return new NetworkBuilder(effects);
}
private constructor(private effects: Effects) {}
getTorHostName(id: string) {
return new TorHostname(id, this.effects);
}
getPort(id: string) {
return new LocalPort(id, this.effects);
}
}

View File

@@ -0,0 +1,38 @@
import { Effects } from "../types";
import { AddressReceipt } from "./AddressReceipt";
import { Origin } from "./Origin";
export class NetworkInterfaceBuilder {
constructor(
readonly options: {
effects: Effects;
name: string;
id: string;
description: string;
ui: boolean;
basic?: null | { password: string; username: string };
path?: string;
search?: Record<string, string>;
}
) {}
async exportAddresses(addresses: Iterable<Origin>) {
const { name, description, id, ui, path, search } = this.options;
// prettier-ignore
const urlAuth = !!(this.options?.basic) ? `${this.options.basic.username}:${this.options.basic.password}@` :
'';
for (const origin of addresses) {
const address = `${origin.protocol}://${urlAuth}${origin.address}`;
await this.options.effects.exportAddress({
name,
description,
address,
id,
ui,
path,
search,
});
}
return {} as AddressReceipt;
}
}

3
lib/mainFn/Origin.ts Normal file
View File

@@ -0,0 +1,3 @@
export class Origin {
constructor(readonly protocol: string, readonly address: string) {}
}

View File

@@ -0,0 +1,7 @@
import { Daemon } from "../types";
import { ReadyProof } from "../health/ReadyProof";
export type RunningMainRet = {
[ReadyProof]: never;
daemon: Daemon;
};

8
lib/mainFn/TorBinding.ts Normal file
View File

@@ -0,0 +1,8 @@
import { Origin } from "./Origin";
export class TorBinding {
constructor(readonly address: string) {}
createOrigin(protocol: string) {
return new Origin(protocol, this.address);
}
}

14
lib/mainFn/TorHostname.ts Normal file
View File

@@ -0,0 +1,14 @@
import { Effects } from "../types";
import { TorBinding } from "./TorBinding";
export class TorHostname {
constructor(readonly id: string, readonly effects: Effects) {}
async bindTor(internalPort: number, externalPort: number) {
const address = await this.effects.bindTor({
internalPort,
name: this.id,
externalPort,
});
return new TorBinding(address);
}
}

View File

@@ -0,0 +1,8 @@
import { AddressReceipt } from "./AddressReceipt";
import { InterfaceReceipt } from "./interfaceReceipt";
export const exportInterfaces = (
_firstProof: AddressReceipt,
..._rest: AddressReceipt[]
) => ({} as InterfaceReceipt);
export default exportInterfaces;

22
lib/mainFn/index.ts Normal file
View File

@@ -0,0 +1,22 @@
import { RunningMainRet } from "./RunningMainRet";
import { Effects, ExpectedExports } from "../types";
export * as network from "./exportInterfaces";
export { LocalBinding } from "./LocalBinding";
export { LocalPort } from "./LocalPort";
export { NetworkBuilder } from "./NetworkBuilder";
export { NetworkInterfaceBuilder } from "./NetworkInterfaceBuilder";
export { Origin } from "./Origin";
export { TorBinding } from "./TorBinding";
export { TorHostname } from "./TorHostname";
export const runningMain: (
fn: (o: {
effects: Effects;
started(onTerm: () => void): null;
}) => Promise<RunningMainRet>
) => ExpectedExports.main = (fn) => {
return async (options) => {
const { daemon } = await fn(options);
daemon.wait();
};
};

View File

@@ -0,0 +1,4 @@
declare const InterfaceProof: unique symbol;
export type InterfaceReceipt = {
[InterfaceProof]: never;
};