2024-03-16 22:29:24 +08:00
|
|
|
import { resolve } from "$std/path/mod.ts";
|
|
|
|
import assets from "../dist/plug_asset_bundle.json" with { type: "json" };
|
2024-01-27 00:05:10 +08:00
|
|
|
import { determineDatabaseBackend } from "../server/db_backend.ts";
|
2024-02-09 04:06:04 +08:00
|
|
|
import { KvPrimitives } from "$lib/data/kv_primitives.ts";
|
|
|
|
import { DiskSpacePrimitives } from "$common/spaces/disk_space_primitives.ts";
|
|
|
|
|
|
|
|
import { ServerSystem } from "../server/server_system.ts";
|
|
|
|
import { AssetBundlePlugSpacePrimitives } from "$common/spaces/asset_bundle_space_primitives.ts";
|
|
|
|
import { LocalShell } from "../server/shell_backend.ts";
|
2024-03-16 22:29:24 +08:00
|
|
|
import { Hono } from "hono/mod.ts";
|
2024-02-09 04:06:04 +08:00
|
|
|
import { DataStore } from "$lib/data/datastore.ts";
|
|
|
|
import { DataStoreMQ } from "$lib/data/mq.datastore.ts";
|
2024-02-28 03:05:12 +08:00
|
|
|
import { EventHook } from "../common/hooks/event.ts";
|
2024-02-09 04:06:04 +08:00
|
|
|
import { sleep } from "$lib/async.ts";
|
|
|
|
import { AssetBundle } from "$lib/asset_bundle/bundle.ts";
|
2023-08-05 00:56:55 +08:00
|
|
|
|
|
|
|
export async function plugRunCommand(
|
|
|
|
{
|
2023-08-12 02:37:13 +08:00
|
|
|
hostname,
|
|
|
|
port,
|
2023-08-05 00:56:55 +08:00
|
|
|
}: {
|
2023-08-12 02:37:13 +08:00
|
|
|
hostname?: string;
|
|
|
|
port?: number;
|
2023-08-05 00:56:55 +08:00
|
|
|
},
|
|
|
|
spacePath: string,
|
2023-08-12 02:37:13 +08:00
|
|
|
functionName: string | undefined,
|
2023-08-05 00:56:55 +08:00
|
|
|
...args: string[]
|
|
|
|
) {
|
2024-03-16 22:29:24 +08:00
|
|
|
spacePath = resolve(spacePath);
|
2023-08-05 00:56:55 +08:00
|
|
|
console.log("Space path", spacePath);
|
|
|
|
console.log("Function to run:", functionName, "with arguments", args);
|
2024-01-27 00:05:10 +08:00
|
|
|
|
|
|
|
const kvPrimitives = await determineDatabaseBackend(spacePath);
|
|
|
|
|
|
|
|
if (!kvPrimitives) {
|
|
|
|
console.error("Cannot run plugs in databaseless mode.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-05 00:56:55 +08:00
|
|
|
try {
|
|
|
|
const result = await runPlug(
|
|
|
|
spacePath,
|
|
|
|
functionName,
|
|
|
|
args,
|
|
|
|
new AssetBundle(assets),
|
2024-01-27 00:05:10 +08:00
|
|
|
kvPrimitives,
|
2023-08-12 02:37:13 +08:00
|
|
|
port,
|
|
|
|
hostname,
|
2023-08-05 00:56:55 +08:00
|
|
|
);
|
2023-08-15 13:55:07 +08:00
|
|
|
if (result) {
|
|
|
|
console.log("Output", result);
|
|
|
|
}
|
2024-01-27 00:05:10 +08:00
|
|
|
kvPrimitives.close();
|
2023-08-12 02:37:13 +08:00
|
|
|
Deno.exit(0);
|
2023-08-05 00:56:55 +08:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e.message);
|
|
|
|
Deno.exit(1);
|
|
|
|
}
|
|
|
|
}
|
2024-02-09 04:06:04 +08:00
|
|
|
|
|
|
|
export async function runPlug(
|
|
|
|
spacePath: string,
|
|
|
|
functionName: string | undefined,
|
2024-02-28 17:54:31 +08:00
|
|
|
args: any[] = [],
|
2024-02-09 04:06:04 +08:00
|
|
|
builtinAssetBundle: AssetBundle,
|
|
|
|
kvPrimitives: KvPrimitives,
|
|
|
|
httpServerPort?: number,
|
|
|
|
httpHostname?: string,
|
|
|
|
) {
|
|
|
|
const serverController = new AbortController();
|
|
|
|
const app = new Hono();
|
|
|
|
|
|
|
|
const ds = new DataStore(kvPrimitives);
|
|
|
|
const mq = new DataStoreMQ(ds);
|
|
|
|
const eventHook = new EventHook();
|
|
|
|
|
|
|
|
const serverSystem = new ServerSystem(
|
|
|
|
new AssetBundlePlugSpacePrimitives(
|
|
|
|
new DiskSpacePrimitives(spacePath),
|
|
|
|
builtinAssetBundle,
|
|
|
|
),
|
|
|
|
kvPrimitives,
|
|
|
|
new LocalShell(spacePath),
|
|
|
|
mq,
|
|
|
|
ds,
|
|
|
|
eventHook,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
await serverSystem.init(true);
|
|
|
|
if (httpHostname && httpServerPort) {
|
|
|
|
Deno.serve({
|
|
|
|
hostname: httpHostname,
|
|
|
|
port: httpServerPort,
|
|
|
|
signal: serverController.signal,
|
|
|
|
}, app.fetch);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (functionName) {
|
|
|
|
const result = await serverSystem.system.invokeFunction(functionName, args);
|
|
|
|
await serverSystem.close();
|
|
|
|
serverController.abort();
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
console.log("Running in server mode, use Ctrl-c to stop");
|
|
|
|
while (true) {
|
|
|
|
await sleep(1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|