@nextclaw/app-runtime 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/README.md +12 -4
  2. package/dist/cli/app-runtime-cli.service.d.ts +2 -0
  3. package/dist/cli/app-runtime-cli.service.js +55 -2
  4. package/dist/cli/app-runtime-options.service.d.ts +10 -1
  5. package/dist/cli/app-runtime-options.service.js +39 -2
  6. package/dist/commands/create.controller.d.ts +2 -1
  7. package/dist/commands/create.controller.js +3 -2
  8. package/dist/commands/inspect.controller.js +2 -1
  9. package/dist/commands/run.controller.js +25 -7
  10. package/dist/host/app-host.service.d.ts +3 -1
  11. package/dist/host/app-host.service.js +5 -1
  12. package/dist/host/app-instance.service.js +1 -0
  13. package/dist/index.d.ts +8 -3
  14. package/dist/index.js +6 -1
  15. package/dist/install/app-installation.service.js +1 -0
  16. package/dist/install/app-installation.types.d.ts +1 -0
  17. package/dist/manifest/app-manifest.service.js +9 -3
  18. package/dist/manifest/app-manifest.types.d.ts +9 -3
  19. package/dist/package.js +1 -1
  20. package/dist/runtime/app-build.service.d.ts +24 -0
  21. package/dist/runtime/app-build.service.js +55 -0
  22. package/dist/runtime/app-runtime-toolchain.service.d.ts +30 -0
  23. package/dist/runtime/app-runtime-toolchain.service.js +96 -0
  24. package/dist/runtime/wasm-main-runner.service.js +1 -0
  25. package/dist/runtime/wasmtime-wasi-http-component.service.d.ts +24 -0
  26. package/dist/runtime/wasmtime-wasi-http-component.service.js +129 -0
  27. package/dist/scaffold/app-scaffold.service.d.ts +11 -2
  28. package/dist/scaffold/app-scaffold.service.js +26 -2
  29. package/dist/scaffold/app-ts-http-scaffold-template.service.d.ts +25 -0
  30. package/dist/scaffold/app-ts-http-scaffold-template.service.js +496 -0
  31. package/package.json +1 -1
@@ -0,0 +1,55 @@
1
+ import { AppManifestService } from "../manifest/app-manifest.service.js";
2
+ import { AppRuntimeToolchainService } from "./app-runtime-toolchain.service.js";
3
+ import { access } from "node:fs/promises";
4
+ import path from "node:path";
5
+ //#region src/runtime/app-build.service.ts
6
+ var AppBuildService = class {
7
+ constructor(manifestService = new AppManifestService(), toolchainService = new AppRuntimeToolchainService()) {
8
+ this.manifestService = manifestService;
9
+ this.toolchainService = toolchainService;
10
+ }
11
+ build = async (params) => {
12
+ const appDirectory = path.resolve(params.appDirectory);
13
+ const bundle = await this.manifestService.load(appDirectory);
14
+ if (bundle.manifest.main.kind !== "wasi-http-component") return {
15
+ appDirectory,
16
+ mainKind: bundle.manifest.main.kind,
17
+ mainEntryPath: bundle.mainEntryPath,
18
+ installedDependencies: false,
19
+ built: false,
20
+ skippedReason: "main.kind=wasm 应用不需要 TS/WASI HTTP 构建。"
21
+ };
22
+ await this.toolchainService.assertReadyForWasiHttpBuild();
23
+ const mainDirectory = path.join(appDirectory, "main");
24
+ await access(path.join(mainDirectory, "package.json"));
25
+ const shouldInstall = params.install || !await this.pathExists(path.join(mainDirectory, "node_modules"));
26
+ if (shouldInstall) await this.toolchainService.runCommand({
27
+ command: "npm",
28
+ args: ["install"],
29
+ cwd: mainDirectory
30
+ });
31
+ await this.toolchainService.runCommand({
32
+ command: "npm",
33
+ args: ["run", "build"],
34
+ cwd: mainDirectory
35
+ });
36
+ await access(bundle.mainEntryPath);
37
+ return {
38
+ appDirectory,
39
+ mainKind: bundle.manifest.main.kind,
40
+ mainEntryPath: bundle.mainEntryPath,
41
+ installedDependencies: shouldInstall,
42
+ built: true
43
+ };
44
+ };
45
+ pathExists = async (targetPath) => {
46
+ try {
47
+ await access(targetPath);
48
+ return true;
49
+ } catch {
50
+ return false;
51
+ }
52
+ };
53
+ };
54
+ //#endregion
55
+ export { AppBuildService };
@@ -0,0 +1,30 @@
1
+ //#region src/runtime/app-runtime-toolchain.service.d.ts
2
+ type AppRuntimeToolStatus = {
3
+ name: string;
4
+ command: string;
5
+ ok: boolean;
6
+ version?: string;
7
+ installHint: string;
8
+ error?: string;
9
+ };
10
+ type AppRuntimeDoctorResult = {
11
+ ok: boolean;
12
+ tools: AppRuntimeToolStatus[];
13
+ };
14
+ type AppRuntimeCommandResult = {
15
+ stdout: string;
16
+ stderr: string;
17
+ };
18
+ declare class AppRuntimeToolchainService {
19
+ doctor: () => Promise<AppRuntimeDoctorResult>;
20
+ assertReadyForWasiHttpBuild: () => Promise<void>;
21
+ runCommand: (params: {
22
+ command: string;
23
+ args: string[];
24
+ cwd: string;
25
+ }) => Promise<AppRuntimeCommandResult>;
26
+ private checkTool;
27
+ private collectProcess;
28
+ }
29
+ //#endregion
30
+ export { AppRuntimeCommandResult, AppRuntimeDoctorResult, AppRuntimeToolStatus, AppRuntimeToolchainService };
@@ -0,0 +1,96 @@
1
+ import { spawn } from "node:child_process";
2
+ //#region src/runtime/app-runtime-toolchain.service.ts
3
+ const REQUIRED_TOOLS = [
4
+ {
5
+ name: "npm",
6
+ command: "npm",
7
+ args: ["--version"],
8
+ installHint: "请安装 Node.js,或使用 NextClaw 桌面版内置运行环境。"
9
+ },
10
+ {
11
+ name: "wasmtime",
12
+ command: "wasmtime",
13
+ args: ["--version"],
14
+ installHint: "请安装 Wasmtime,或使用后续内置 Wasmtime 的 NextClaw 发行包。"
15
+ },
16
+ {
17
+ name: "wkg",
18
+ command: "wkg",
19
+ args: ["--version"],
20
+ installHint: "请安装 Bytecode Alliance wkg;有 Rust/Cargo 时可执行:cargo install wkg --locked。"
21
+ }
22
+ ];
23
+ var AppRuntimeToolchainService = class {
24
+ doctor = async () => {
25
+ const tools = await Promise.all(REQUIRED_TOOLS.map((tool) => this.checkTool(tool)));
26
+ return {
27
+ ok: tools.every((tool) => tool.ok),
28
+ tools
29
+ };
30
+ };
31
+ assertReadyForWasiHttpBuild = async () => {
32
+ const missing = (await this.doctor()).tools.filter((tool) => !tool.ok);
33
+ if (missing.length === 0) return;
34
+ throw new Error(["NApp WASI HTTP 开发环境尚未就绪。", ...missing.map((tool) => `- 缺少 ${tool.name}: ${tool.installHint}`)].join("\n"));
35
+ };
36
+ runCommand = async (params) => {
37
+ const { command, args, cwd } = params;
38
+ const child = spawn(command, args, {
39
+ cwd,
40
+ stdio: [
41
+ "ignore",
42
+ "pipe",
43
+ "pipe"
44
+ ]
45
+ });
46
+ return await this.collectProcess(child, command);
47
+ };
48
+ checkTool = async (tool) => {
49
+ try {
50
+ const result = await this.runCommand({
51
+ command: tool.command,
52
+ args: tool.args,
53
+ cwd: process.cwd()
54
+ });
55
+ const version = (result.stdout || result.stderr).trim().split(/\r?\n/)[0]?.trim();
56
+ return {
57
+ name: tool.name,
58
+ command: tool.command,
59
+ ok: true,
60
+ version,
61
+ installHint: tool.installHint
62
+ };
63
+ } catch (error) {
64
+ return {
65
+ name: tool.name,
66
+ command: tool.command,
67
+ ok: false,
68
+ installHint: tool.installHint,
69
+ error: error instanceof Error ? error.message : String(error)
70
+ };
71
+ }
72
+ };
73
+ collectProcess = async (child, command) => {
74
+ const stdoutChunks = [];
75
+ const stderrChunks = [];
76
+ child.stdout.on("data", (chunk) => {
77
+ stdoutChunks.push(chunk);
78
+ });
79
+ child.stderr.on("data", (chunk) => {
80
+ stderrChunks.push(chunk);
81
+ });
82
+ const exitCode = await new Promise((resolve, reject) => {
83
+ child.once("error", reject);
84
+ child.once("exit", resolve);
85
+ });
86
+ const stdout = Buffer.concat(stdoutChunks).toString("utf-8");
87
+ const stderr = Buffer.concat(stderrChunks).toString("utf-8");
88
+ if (exitCode !== 0) throw new Error(`${command} exited with ${exitCode ?? "unknown"}\n${stderr || stdout}`);
89
+ return {
90
+ stdout,
91
+ stderr
92
+ };
93
+ };
94
+ };
95
+ //#endregion
96
+ export { AppRuntimeToolchainService };
@@ -7,6 +7,7 @@ var WasmMainRunnerService = class extends MainRunnerService {
7
7
  this.sidecarClient = sidecarClient;
8
8
  }
9
9
  runDocumentSummary = async (request) => {
10
+ if (request.bundle.manifest.main.kind !== "wasm") throw new Error("runDocumentSummary 只支持 main.kind=wasm。");
10
11
  const output = await this.sidecarClient.runExport({
11
12
  wasmPath: request.bundle.mainEntryPath,
12
13
  exportName: request.bundle.manifest.main.export,
@@ -0,0 +1,24 @@
1
+ import { IncomingMessage, ServerResponse } from "node:http";
2
+
3
+ //#region src/runtime/wasmtime-wasi-http-component.service.d.ts
4
+ type WasmtimeWasiHttpComponentStartOptions = {
5
+ wasmPath: string;
6
+ dataDirectory: string;
7
+ };
8
+ type WasmtimeWasiHttpComponentHandle = {
9
+ url: string;
10
+ port: number;
11
+ };
12
+ declare class WasmtimeWasiHttpComponentService {
13
+ private process?;
14
+ private handle?;
15
+ start: (options: WasmtimeWasiHttpComponentStartOptions) => Promise<WasmtimeWasiHttpComponentHandle>;
16
+ stop: () => Promise<void>;
17
+ handleRequest: (request: IncomingMessage, response: ServerResponse) => Promise<boolean>;
18
+ private readBody;
19
+ private methodSupportsBody;
20
+ private findAvailablePort;
21
+ private waitUntilReady;
22
+ }
23
+ //#endregion
24
+ export { WasmtimeWasiHttpComponentHandle, WasmtimeWasiHttpComponentService, WasmtimeWasiHttpComponentStartOptions };
@@ -0,0 +1,129 @@
1
+ import { mkdir } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { createServer } from "node:http";
4
+ import { spawn } from "node:child_process";
5
+ //#region src/runtime/wasmtime-wasi-http-component.service.ts
6
+ var WasmtimeWasiHttpComponentService = class {
7
+ process;
8
+ handle;
9
+ start = async (options) => {
10
+ if (this.process) throw new Error("WASI HTTP component 已经启动。");
11
+ const dataDirectory = path.resolve(options.dataDirectory);
12
+ await mkdir(dataDirectory, { recursive: true });
13
+ const port = await this.findAvailablePort();
14
+ const child = spawn("wasmtime", [
15
+ "serve",
16
+ "-S",
17
+ "cli=y",
18
+ "-S",
19
+ "http=y",
20
+ "-S",
21
+ "inherit-network=y",
22
+ "--addr",
23
+ `127.0.0.1:${port}`,
24
+ "--dir",
25
+ `${dataDirectory}::/data`,
26
+ options.wasmPath
27
+ ], { stdio: [
28
+ "ignore",
29
+ "pipe",
30
+ "pipe"
31
+ ] });
32
+ this.process = child;
33
+ const handle = {
34
+ url: `http://127.0.0.1:${port}`,
35
+ port
36
+ };
37
+ this.handle = handle;
38
+ await this.waitUntilReady(handle.url, child);
39
+ return handle;
40
+ };
41
+ stop = async () => {
42
+ const child = this.process;
43
+ if (!child) return;
44
+ await new Promise((resolve) => {
45
+ child.once("exit", () => {
46
+ resolve();
47
+ });
48
+ child.kill("SIGTERM");
49
+ setTimeout(() => {
50
+ if (!child.killed) child.kill("SIGKILL");
51
+ resolve();
52
+ }, 1e3).unref();
53
+ });
54
+ this.process = void 0;
55
+ this.handle = void 0;
56
+ };
57
+ handleRequest = async (request, response) => {
58
+ const handle = this.handle;
59
+ if (!handle) return false;
60
+ const requestUrl = new URL(request.url ?? "/", handle.url);
61
+ if (!requestUrl.pathname.startsWith("/api/")) return false;
62
+ const headers = new Headers();
63
+ for (const [key, value] of Object.entries(request.headers)) {
64
+ if (value === void 0 || key.toLowerCase() === "host") continue;
65
+ if (Array.isArray(value)) {
66
+ for (const item of value) headers.append(key, item);
67
+ continue;
68
+ }
69
+ headers.set(key, value);
70
+ }
71
+ const body = await this.readBody(request);
72
+ const upstreamResponse = await fetch(requestUrl, {
73
+ method: request.method,
74
+ headers,
75
+ body: this.methodSupportsBody(request.method) ? body : void 0
76
+ });
77
+ response.writeHead(upstreamResponse.status, Object.fromEntries(upstreamResponse.headers));
78
+ response.end(Buffer.from(await upstreamResponse.arrayBuffer()));
79
+ return true;
80
+ };
81
+ readBody = async (request) => {
82
+ const chunks = [];
83
+ for await (const chunk of request) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
84
+ return Buffer.concat(chunks);
85
+ };
86
+ methodSupportsBody = (method) => {
87
+ return method !== void 0 && method !== "GET" && method !== "HEAD";
88
+ };
89
+ findAvailablePort = async () => {
90
+ const server = createServer();
91
+ await new Promise((resolve, reject) => {
92
+ server.once("error", reject);
93
+ server.listen(0, "127.0.0.1", () => {
94
+ server.off("error", reject);
95
+ resolve();
96
+ });
97
+ });
98
+ const address = server.address();
99
+ await new Promise((resolve, reject) => {
100
+ server.close((error) => {
101
+ if (error) {
102
+ reject(error);
103
+ return;
104
+ }
105
+ resolve();
106
+ });
107
+ });
108
+ if (!address || typeof address === "string") throw new Error("无法分配 WASI HTTP component 端口。");
109
+ return address.port;
110
+ };
111
+ waitUntilReady = async (url, child) => {
112
+ const errors = [];
113
+ child.stderr.on("data", (chunk) => {
114
+ errors.push(chunk.toString("utf-8"));
115
+ });
116
+ for (let attempt = 0; attempt < 50; attempt += 1) {
117
+ if (child.exitCode !== null) throw new Error(`WASI HTTP component 启动失败:${errors.join("").trim() || `exit ${child.exitCode}`}`);
118
+ try {
119
+ await fetch(url);
120
+ return;
121
+ } catch {
122
+ await new Promise((resolve) => setTimeout(resolve, 100));
123
+ }
124
+ }
125
+ throw new Error(`WASI HTTP component 启动超时:${errors.join("").trim()}`);
126
+ };
127
+ };
128
+ //#endregion
129
+ export { WasmtimeWasiHttpComponentService };
@@ -1,10 +1,19 @@
1
+ import { AppTsHttpScaffoldTemplateService } from "./app-ts-http-scaffold-template.service.js";
2
+
1
3
  //#region src/scaffold/app-scaffold.service.d.ts
4
+ type AppScaffoldTemplate = "starter" | "ts-http";
2
5
  type AppScaffoldResult = {
3
6
  appDirectory: string;
4
7
  manifestPath: string;
8
+ template: AppScaffoldTemplate;
5
9
  };
6
10
  declare class AppScaffoldService {
7
- scaffold: (targetDirectory: string) => Promise<AppScaffoldResult>;
11
+ private readonly tsHttpTemplateService;
12
+ constructor(tsHttpTemplateService?: AppTsHttpScaffoldTemplateService);
13
+ scaffold: (targetDirectory: string, options?: {
14
+ template?: AppScaffoldTemplate;
15
+ }) => Promise<AppScaffoldResult>;
16
+ private writeTemplateFiles;
8
17
  private assertTargetDoesNotExist;
9
18
  private buildAppId;
10
19
  private buildAppName;
@@ -18,4 +27,4 @@ declare class AppScaffoldService {
18
27
  private buildIconSvg;
19
28
  }
20
29
  //#endregion
21
- export { AppScaffoldService };
30
+ export { AppScaffoldResult, AppScaffoldService, AppScaffoldTemplate };
@@ -1,9 +1,14 @@
1
+ import { AppTsHttpScaffoldTemplateService } from "./app-ts-http-scaffold-template.service.js";
1
2
  import { access, mkdir, writeFile } from "node:fs/promises";
2
3
  import path from "node:path";
3
4
  //#region src/scaffold/app-scaffold.service.ts
4
5
  var AppScaffoldService = class {
5
- scaffold = async (targetDirectory) => {
6
+ constructor(tsHttpTemplateService = new AppTsHttpScaffoldTemplateService()) {
7
+ this.tsHttpTemplateService = tsHttpTemplateService;
8
+ }
9
+ scaffold = async (targetDirectory, options) => {
6
10
  const appDirectory = path.resolve(targetDirectory);
11
+ const template = options?.template ?? "starter";
7
12
  await this.assertTargetDoesNotExist(appDirectory);
8
13
  await mkdir(path.join(appDirectory, "main"), { recursive: true });
9
14
  await mkdir(path.join(appDirectory, "ui"), { recursive: true });
@@ -11,6 +16,17 @@ var AppScaffoldService = class {
11
16
  const appName = this.buildAppName(appDirectory);
12
17
  const appId = this.buildAppId(appDirectory);
13
18
  const manifestPath = path.join(appDirectory, "manifest.json");
19
+ if (template === "ts-http") {
20
+ await this.writeTemplateFiles(appDirectory, this.tsHttpTemplateService.buildFiles({
21
+ appId,
22
+ appName
23
+ }));
24
+ return {
25
+ appDirectory,
26
+ manifestPath,
27
+ template
28
+ };
29
+ }
14
30
  await Promise.all([
15
31
  writeFile(manifestPath, `${JSON.stringify(this.buildManifest(appId, appName), null, 2)}\n`, "utf-8"),
16
32
  writeFile(path.join(appDirectory, "marketplace.json"), `${JSON.stringify(this.buildMarketplaceMetadata(appName), null, 2)}\n`, "utf-8"),
@@ -23,9 +39,17 @@ var AppScaffoldService = class {
23
39
  ]);
24
40
  return {
25
41
  appDirectory,
26
- manifestPath
42
+ manifestPath,
43
+ template
27
44
  };
28
45
  };
46
+ writeTemplateFiles = async (appDirectory, files) => {
47
+ await Promise.all(files.map(async (file) => {
48
+ const filePath = path.join(appDirectory, file.relativePath);
49
+ await mkdir(path.dirname(filePath), { recursive: true });
50
+ await writeFile(filePath, file.content);
51
+ }));
52
+ };
29
53
  assertTargetDoesNotExist = async (targetDirectory) => {
30
54
  try {
31
55
  await access(targetDirectory);
@@ -0,0 +1,25 @@
1
+ //#region src/scaffold/app-ts-http-scaffold-template.service.d.ts
2
+ type AppScaffoldFile = {
3
+ relativePath: string;
4
+ content: string | Buffer;
5
+ };
6
+ declare class AppTsHttpScaffoldTemplateService {
7
+ buildFiles: (params: {
8
+ appId: string;
9
+ appName: string;
10
+ }) => AppScaffoldFile[];
11
+ private buildManifest;
12
+ private buildMainPackageJson;
13
+ private buildTsconfig;
14
+ private buildMarketplaceMetadata;
15
+ private buildReadme;
16
+ private buildRolldownConfig;
17
+ private buildWitWorld;
18
+ private buildComponentSource;
19
+ private buildUiHtml;
20
+ private buildUiScript;
21
+ private buildIconSvg;
22
+ private normalizeSlug;
23
+ }
24
+ //#endregion
25
+ export { AppScaffoldFile, AppTsHttpScaffoldTemplateService };