mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-30 04:11:57 +00:00
chore: Adding mainFn helpers
This commit is contained in:
4
lib/mainFn/AddressReceipt.ts
Normal file
4
lib/mainFn/AddressReceipt.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
declare const AddressProof: unique symbol;
|
||||
export type AddressReceipt = {
|
||||
[AddressProof]: never;
|
||||
};
|
||||
11
lib/mainFn/LocalBinding.ts
Normal file
11
lib/mainFn/LocalBinding.ts
Normal 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
13
lib/mainFn/LocalPort.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
17
lib/mainFn/NetworkBuilder.ts
Normal file
17
lib/mainFn/NetworkBuilder.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
38
lib/mainFn/NetworkInterfaceBuilder.ts
Normal file
38
lib/mainFn/NetworkInterfaceBuilder.ts
Normal 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
3
lib/mainFn/Origin.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export class Origin {
|
||||
constructor(readonly protocol: string, readonly address: string) {}
|
||||
}
|
||||
7
lib/mainFn/RunningMainRet.ts
Normal file
7
lib/mainFn/RunningMainRet.ts
Normal 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
8
lib/mainFn/TorBinding.ts
Normal 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
14
lib/mainFn/TorHostname.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
8
lib/mainFn/exportInterfaces.ts
Normal file
8
lib/mainFn/exportInterfaces.ts
Normal 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
22
lib/mainFn/index.ts
Normal 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();
|
||||
};
|
||||
};
|
||||
4
lib/mainFn/interfaceReceipt.ts
Normal file
4
lib/mainFn/interfaceReceipt.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
declare const InterfaceProof: unique symbol;
|
||||
export type InterfaceReceipt = {
|
||||
[InterfaceProof]: never;
|
||||
};
|
||||
Reference in New Issue
Block a user