move to correct structure

This commit is contained in:
JOLIMAITRE Matthieu 2023-12-09 00:08:48 +01:00
parent 44825c4c31
commit 54c4e91af8
12 changed files with 118 additions and 1 deletions

View file

@ -0,0 +1,21 @@
import { container_paths } from "./paths.ts";
import { exists } from "./utils.ts";
export type ContainerConfig = ReturnType<typeof new_container_config>;
export function new_container_config(name: string) {
return {
name,
version: 0,
redirects: [] as [number, number][],
};
}
export async function load_container_config(name: string) {
const config_path = container_paths(name).config;
if (!exists(config_path)) return null;
const content = await Deno.readTextFile(config_path);
const read = JSON.parse(content);
const default_ = new_container_config(name);
if (read.version < default_.version) throw new Error("read conf version is outdated");
return { ...default_, ...read };
}

View file

@ -0,0 +1 @@
// wrapper

22
instance/src/lib/paths.ts Normal file
View file

@ -0,0 +1,22 @@
import { dirname } from "https://deno.land/std@0.208.0/path/mod.ts";
// '/bdmgrdts'
export function instance_root_path() {
const file_path = new URL("", import.meta.url).pathname;
return dirname(dirname(dirname(file_path)));
}
export function socket_path() {
return instance_root_path() + "/socket.socket";
}
export function containers_path() {
return instance_root_path() + "/local/containers";
}
export function container_paths(name: string) {
const base = containers_path() + "/" + name;
const conf = base + "/config.json";
const root = base + "/root";
return { base, configuration: conf, root };
}

43
instance/src/lib/utils.ts Normal file
View file

@ -0,0 +1,43 @@
import { TextLineStream } from "https://deno.land/std@0.208.0/streams/mod.ts";
export function channel<T>() {
const inner = {
queue: [] as T[],
resolver: null as (() => void) | null,
};
const send = (item: T) => {
inner.queue.push(item);
if (inner.resolver === null) return;
inner.resolver();
inner.resolver = null;
};
const receive = async () => {
if (inner.queue.length < 1) await new Promise<void>((res) => inner.resolver = res);
const [head] = inner.queue.splice(0, 1);
return head;
};
return { send, receive };
}
export function* range(from: number, to: number) {
while (from < to) yield from++;
}
export async function exists(path: string) {
try {
await Deno.stat(path);
return true;
} catch (_) {
return false;
}
}
export async function next<T>(readable: ReadableStream<T>) {
for await (const item of readable) return item;
}
export function lines(readable: ReadableStream<Uint8Array>) {
return readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream());
}