node-plume 1.0.1

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.
@@ -0,0 +1,239 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
8
+ // src/start/index.ts
9
+ import { fork } from "child_process";
10
+ import { existsSync } from "fs";
11
+ import { join, dirname } from "path";
12
+ import { fileURLToPath } from "url";
13
+ import { logger } from "@plume/logger";
14
+ var ProcessLauncher = class _ProcessLauncher {
15
+ process = null;
16
+ state = "idle";
17
+ stats = {
18
+ startTime: 0,
19
+ restartCount: 0,
20
+ lastRestartTime: 0
21
+ };
22
+ eventHandlers = /* @__PURE__ */ new Map();
23
+ options;
24
+ shutdownPromise = null;
25
+ resolveShutdown = null;
26
+ static DEFAULT_OPTIONS = {
27
+ entry: "",
28
+ cwd: process.cwd(),
29
+ watch: false,
30
+ env: {}
31
+ };
32
+ constructor(options = {}) {
33
+ this.options = { ..._ProcessLauncher.DEFAULT_OPTIONS, ...options };
34
+ this.setupSignalHandlers();
35
+ }
36
+ setupSignalHandlers() {
37
+ const signals = ["SIGINT", "SIGTERM", "SIGUSR2"];
38
+ signals.forEach((signal) => {
39
+ process.on(signal, () => {
40
+ logger.info(`\u6536\u5230\u4FE1\u53F7 ${signal}\uFF0C\u6B63\u5728\u5173\u95ED...`);
41
+ this.stop();
42
+ });
43
+ });
44
+ process.on("exit", () => {
45
+ this.kill();
46
+ });
47
+ }
48
+ async start() {
49
+ if (this.state === "running" && this.process) {
50
+ logger.warn("\u8FDB\u7A0B\u5DF2\u5728\u8FD0\u884C\u4E2D");
51
+ return this.process;
52
+ }
53
+ this.state = "starting";
54
+ const entry = this.resolveEntry();
55
+ if (!entry) {
56
+ const error = new Error("\u672A\u627E\u5230\u6709\u6548\u7684\u5165\u53E3\u6587\u4EF6");
57
+ this.emit("error", error);
58
+ throw error;
59
+ }
60
+ logger.info(`Launcher cwd=${this.options.cwd} mode=${this.options.watch ? "watch" : "foreground"}`);
61
+ logger.info(`\u5165\u53E3 ${entry}`);
62
+ this.process = fork(entry, [], {
63
+ cwd: this.options.cwd,
64
+ env: { ...process.env, ...this.options.env },
65
+ stdio: "inherit"
66
+ });
67
+ this.stats.startTime = Date.now();
68
+ this.stats.lastRestartTime = this.stats.startTime;
69
+ this.state = "running";
70
+ this.setupProcessEventHandlers();
71
+ this.emit("start", { pid: this.process.pid, entry });
72
+ logger.info(`\u5B50\u8FDB\u7A0B pid=${this.process.pid}`);
73
+ return this.process;
74
+ }
75
+ async restart() {
76
+ if (this.state !== "running") {
77
+ logger.warn("\u8FDB\u7A0B\u672A\u8FD0\u884C\uFF0C\u76F4\u63A5\u542F\u52A8");
78
+ return this.start();
79
+ }
80
+ logger.warn("\u6B63\u5728\u91CD\u542F\u5E94\u7528");
81
+ this.stats.restartCount++;
82
+ this.stats.lastRestartTime = Date.now();
83
+ await this.stop();
84
+ this.state = "idle";
85
+ const newProcess = await this.start();
86
+ this.emit("restart", {
87
+ pid: newProcess.pid,
88
+ restartCount: this.stats.restartCount
89
+ });
90
+ return newProcess;
91
+ }
92
+ async stop() {
93
+ if (this.state !== "running" || !this.process) {
94
+ return;
95
+ }
96
+ this.state = "stopping";
97
+ logger.warn("\u6B63\u5728\u505C\u6B62\u5E94\u7528");
98
+ this.shutdownPromise = new Promise((resolve) => {
99
+ this.resolveShutdown = resolve;
100
+ });
101
+ this.process.kill("SIGTERM");
102
+ const timeout = setTimeout(() => {
103
+ if (this.process && this.process.pid) {
104
+ logger.warn("\u8FDB\u7A0B\u672A\u5728\u8D85\u65F6\u65F6\u95F4\u5185\u9000\u51FA\uFF0C\u5F3A\u5236\u7EC8\u6B62");
105
+ this.process.kill("SIGKILL");
106
+ }
107
+ }, 1e4);
108
+ await this.shutdownPromise;
109
+ clearTimeout(timeout);
110
+ this.state = "stopped";
111
+ this.emit("stop");
112
+ logger.info("Launcher \u5DF2\u505C\u6B62");
113
+ }
114
+ kill() {
115
+ if (this.process && this.process.pid) {
116
+ try {
117
+ process.kill(this.process.pid, "SIGKILL");
118
+ } catch {
119
+ }
120
+ }
121
+ this.process = null;
122
+ this.state = "stopped";
123
+ }
124
+ getState() {
125
+ return this.state;
126
+ }
127
+ getStats() {
128
+ return { ...this.stats };
129
+ }
130
+ getUptime() {
131
+ if (this.state !== "running") return 0;
132
+ return Date.now() - this.stats.startTime;
133
+ }
134
+ on(event, handler) {
135
+ if (!this.eventHandlers.has(event)) {
136
+ this.eventHandlers.set(event, /* @__PURE__ */ new Set());
137
+ }
138
+ this.eventHandlers.get(event).add(handler);
139
+ return this;
140
+ }
141
+ off(event, handler) {
142
+ this.eventHandlers.get(event)?.delete(handler);
143
+ return this;
144
+ }
145
+ emit(event, data) {
146
+ this.eventHandlers.get(event)?.forEach((handler) => {
147
+ try {
148
+ handler(event, data);
149
+ } catch (error) {
150
+ const message = error instanceof Error ? error.message : String(error);
151
+ logger.error(`\u4E8B\u4EF6\u5904\u7406\u5668\u9519\u8BEF [${event}]: ${message}`);
152
+ }
153
+ });
154
+ }
155
+ setupProcessEventHandlers() {
156
+ if (!this.process) return;
157
+ this.process.on("exit", (code, signal) => {
158
+ this.handleProcessExit(code, signal);
159
+ });
160
+ this.process.on("error", (error) => {
161
+ this.handleProcessError(error);
162
+ });
163
+ this.process.on("message", (message) => {
164
+ this.handleProcessMessage(message);
165
+ });
166
+ }
167
+ handleProcessExit(code, signal) {
168
+ const uptime = this.getUptime();
169
+ if (signal) {
170
+ logger.warn(`\u8FDB\u7A0B\u9000\u51FA signal=${signal} uptime=${uptime}ms`);
171
+ } else {
172
+ logger.info(`\u8FDB\u7A0B\u9000\u51FA code=${code ?? "unknown"} uptime=${uptime}ms`);
173
+ }
174
+ this.emit("exit", { code, signal, uptime });
175
+ if (this.resolveShutdown) {
176
+ this.resolveShutdown();
177
+ this.shutdownPromise = null;
178
+ this.resolveShutdown = null;
179
+ }
180
+ this.process = null;
181
+ this.state = "stopped";
182
+ }
183
+ handleProcessError(error) {
184
+ logger.error(`\u8FDB\u7A0B\u9519\u8BEF: ${error.message}`);
185
+ this.emit("error", error);
186
+ }
187
+ handleProcessMessage(message) {
188
+ if (typeof message === "object" && message !== null) {
189
+ const msg = message;
190
+ if (msg.type === "plume:restart") {
191
+ this.restart().catch((err) => logger.error(`\u91CD\u542F\u5931\u8D25: ${err instanceof Error ? err.message : String(err)}`));
192
+ } else if (msg.type === "plume:stop") {
193
+ this.stop().catch((err) => logger.error(`\u505C\u6B62\u5931\u8D25: ${err instanceof Error ? err.message : String(err)}`));
194
+ }
195
+ }
196
+ }
197
+ resolveEntry() {
198
+ if (this.options.entry && existsSync(this.options.entry)) {
199
+ return this.options.entry;
200
+ }
201
+ const cwd = this.options.cwd;
202
+ const currentDir = dirname(fileURLToPath(import.meta.url));
203
+ const possibleEntries = [
204
+ join(currentDir, "start", "app.mjs"),
205
+ join(currentDir, "app.mjs"),
206
+ join(cwd, "dist", "main.js"),
207
+ join(cwd, "dist", "index.js"),
208
+ join(cwd, "index.js"),
209
+ join(cwd, "src", "main.ts"),
210
+ join(cwd, "src", "index.ts"),
211
+ join(cwd, "index.ts")
212
+ ];
213
+ for (const entry of possibleEntries) {
214
+ if (existsSync(entry)) {
215
+ return entry;
216
+ }
217
+ }
218
+ return null;
219
+ }
220
+ };
221
+ async function launch(options) {
222
+ const launcher = new ProcessLauncher(options);
223
+ await launcher.start();
224
+ return launcher;
225
+ }
226
+ var currentFile = fileURLToPath(import.meta.url);
227
+ var entryFile = process.argv[1] ? fileURLToPath(`file:///${process.argv[1].replace(/\\/g, "/")}`) : "";
228
+ if (currentFile === entryFile || process.argv[1]?.includes("start/index.mjs")) {
229
+ launch().catch((error) => {
230
+ logger.error(`\u542F\u52A8\u5931\u8D25: ${error instanceof Error ? error.message : String(error)}`);
231
+ process.exit(1);
232
+ });
233
+ }
234
+
235
+ export {
236
+ __require,
237
+ ProcessLauncher,
238
+ launch
239
+ };
@@ -0,0 +1,8 @@
1
+ import {
2
+ ProcessLauncher,
3
+ launch
4
+ } from "./chunk-T7M5RXO7.mjs";
5
+ export {
6
+ ProcessLauncher,
7
+ launch
8
+ };
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "node-plume",
3
+ "version": "1.0.1",
4
+ "description": "Core runtime and plugin system for Plume bots",
5
+ "type": "module",
6
+ "main": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "bin": {
9
+ "plume": "./dist/cli/index.mjs"
10
+ },
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.mjs"
15
+ },
16
+ "./cli": {
17
+ "import": "./dist/cli/index.mjs"
18
+ },
19
+ "./start": {
20
+ "import": "./dist/start/index.mjs"
21
+ },
22
+ "./app": {
23
+ "import": "./dist/start/app.mjs"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "dependencies": {
30
+ "commander": "^14.0.0",
31
+ "picocolors": "^1.0.0",
32
+ "@plume/types": "1.0.0",
33
+ "@plume/logger": "1.0.0"
34
+ },
35
+ "devDependencies": {
36
+ "@types/node": "^20.19.41",
37
+ "tsup": "^8.5.0",
38
+ "tsx": "^4.20.6",
39
+ "typescript": "^5.9.3"
40
+ },
41
+ "engines": {
42
+ "node": ">=18.18.0"
43
+ },
44
+ "scripts": {
45
+ "app": "tsx src/index.ts",
46
+ "build": "tsup",
47
+ "build:dev": "tsup",
48
+ "cli": "node dist/cli/index.mjs",
49
+ "cli:dev": "tsx src/cli/index.ts",
50
+ "dev": "tsx src/index.ts",
51
+ "typecheck": "tsc --noEmit -p tsconfig.json",
52
+ "clean": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\""
53
+ }
54
+ }