Skip to content

Commit ed113df

Browse files
xiaoqchShrBox
authored andcommitted
fix: improve logging in NodeJs plugin loading
1 parent 028c815 commit ed113df

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/legacy/main/NodeJsHelper.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "ll/api/chrono/GameChrono.h"
1010
#include "ll/api/coro/CoroTask.h"
1111
#include "ll/api/io/FileUtils.h"
12+
#include "ll/api/io/LogLevel.h"
1213
#include "ll/api/service/GamingStatus.h"
1314
#include "ll/api/service/ServerInfo.h"
1415
#include "ll/api/thread/ServerThreadExecutor.h"
@@ -162,16 +163,18 @@ bool loadPluginCode(script::ScriptEngine* engine, std::string entryScriptPath, s
162163
if (esm) {
163164
compiler += fmt::format(
164165
R"(
165-
import('url').then(url => {{
166-
const moduleUrl = url.pathToFileURL('{1}').href;
167-
import(moduleUrl).catch(error => {{
168-
console.error('Failed to load ESM module:', error);
166+
Promise.all([import("url"), import("util")])
167+
.then(([url, util]) => {{
168+
const moduleUrl = url.pathToFileURL("{1}").href;
169+
import(moduleUrl).catch((error) => {{
170+
logger.error(`Failed to load ESM module: `, util.inspect(error));
171+
process.exit(1);
172+
}});
173+
}})
174+
.catch((error) => {{
175+
console.error(`Failed to import "url" or "util" module:`, error);
169176
process.exit(1);
170177
}});
171-
}}).catch(error => {{
172-
console.error('Failed to import url module:', error);
173-
process.exit(1);
174-
}});
175178
)",
176179
pluginDirPath,
177180
entryScriptPath
@@ -206,10 +209,19 @@ bool loadPluginCode(script::ScriptEngine* engine, std::string entryScriptPath, s
206209
}
207210

208211
// Set exit handler
209-
node::SetProcessExitHandler(env, [](node::Environment* env_, int exit_code) { stopEngine(getEngine(env_)); });
212+
node::SetProcessExitHandler(env, [](node::Environment* env_, int exit_code) {
213+
auto engine = getEngine(env_);
214+
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().log(
215+
exit_code == 0 ? ll::io::LogLevel::Debug : ll::io::LogLevel::Error,
216+
"NodeJs plugin {} exited with code {}.",
217+
getEngineData(engine)->pluginName,
218+
exit_code
219+
);
220+
stopEngine(engine);
221+
});
210222

211223
// Load code
212-
MaybeLocal<v8::Value> loadenv_ret = node::LoadEnvironment(env, compiler.c_str());
224+
MaybeLocal<v8::Value> loadenv_ret = node::LoadEnvironment(env, compiler);
213225
if (loadenv_ret.IsEmpty()) // There has been a JS exception.
214226
{
215227
node::Stop(env);
@@ -278,10 +290,6 @@ bool stopEngine(node::Environment* env) {
278290
}
279291

280292
bool stopEngine(script::ScriptEngine* engine) {
281-
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().info(
282-
"NodeJs plugin {} exited.",
283-
getEngineData(engine)->pluginName
284-
);
285293
auto env = NodeJsHelper::getEnvironmentOf(engine);
286294
return stopEngine(env);
287295
}
@@ -324,7 +332,7 @@ std::string getPluginPackageName(const std::string& dirPath) {
324332
std::ifstream file(ll::string_utils::u8str2str(packageFilePath.make_preferred().u8string()));
325333
nlohmann::json j;
326334
file >> j;
327-
std::string packageName = "";
335+
std::string packageName{};
328336
if (j.contains("name")) {
329337
packageName = j["name"].get<std::string>();
330338
}
@@ -385,7 +393,7 @@ bool processConsoleNpmCmd(const std::string& cmd) {
385393
#endif
386394
}
387395

388-
int executeNpmCommand(std::string cmd, std::string workingDir) {
396+
int executeNpmCommand(const std::string& cmd, std::string workingDir) {
389397
if (!nodeJsInited && !initNodeJs()) {
390398
return -1;
391399
}
@@ -431,7 +439,7 @@ int executeNpmCommand(std::string cmd, std::string workingDir) {
431439

432440
try {
433441
node::SetProcessExitHandler(env, [&](node::Environment* env_, int exit_code) { node::Stop(env); });
434-
MaybeLocal<v8::Value> loadenv_ret = node::LoadEnvironment(env, executeJs.c_str());
442+
MaybeLocal<v8::Value> loadenv_ret = node::LoadEnvironment(env, executeJs);
435443
if (loadenv_ret.IsEmpty()) // There has been a JS exception.
436444
throw "error";
437445
exit_code = node::SpinEventLoop(env).FromMaybe(0);

src/legacy/main/NodeJsHelper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ bool doesPluginPackHasDependency(const std::string& dirPath);
3030
bool isESModulesSystem(const std::string& dirPath);
3131

3232
bool processConsoleNpmCmd(const std::string& cmd);
33-
int executeNpmCommand(std::string cmd, std::string workingDir = LLSE_NPM_EXECUTE_PATH);
33+
int executeNpmCommand(const std::string& cmd, std::string workingDir = LLSE_NPM_EXECUTE_PATH);
3434

3535
} // namespace NodeJsHelper

0 commit comments

Comments
 (0)