Skip to content

feat: 增加错误处理,增加退出方法,增加类型提示#692

Open
sylw114 wants to merge 2 commits into
illuspas:mainfrom
sylw114:main
Open

feat: 增加错误处理,增加退出方法,增加类型提示#692
sylw114 wants to merge 2 commits into
illuspas:mainfrom
sylw114:main

Conversation

@sylw114

@sylw114 sylw114 commented Jun 11, 2026

Copy link
Copy Markdown

所有代码都是AI完成的,主要是要是没这几个东西,通过导入使用会有非常大的问题。

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to make Node-Media-Server safer to embed/import as a library by adding (1) explicit error events, (2) graceful shutdown/stop APIs, and (3) TypeScript type declarations.

Changes:

  • Added serverError / sessionError event emissions across servers and sessions.
  • Introduced async close() semantics for some sessions and added stop() methods for HTTP/RTMP servers plus a top-level NodeMediaServer.stop().
  • Added a large src/index.d.ts to provide TypeScript typings.

Reviewed changes

Copilot reviewed 8 out of 12 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
src/session/rtmp_session.js Makes close() awaitable and emits sessionError on socket errors.
src/session/record_session.js Adds async close() to close the recording stream.
src/session/flv_session.js Emits sessionError on socket errors.
src/session/base_session.js Updates abstract close() signature to async/Promise.
src/server/rtmp_server.js Emits serverError and adds stop() for RTMP/RTMPS servers.
src/server/http_server.js Emits serverError and adds stop() for HTTP/HTTPS/WS/WSS servers.
src/index.js Adds NodeMediaServer.stop() orchestration for shutdown.
src/index.d.ts Introduces TypeScript declaration file for consumers.
src/api/handlers/sessions.js Makes session deletion await session.close().
package.json Formatting-only change.
package-lock.json Updates some locked transitive dependency versions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/server/rtmp_server.js
Comment on lines 18 to 23
if (Context.config.rtmp?.port) {
this.tcpServer = net.createServer(this.handleRequest);
this.tcpServer.on("error", (err) => {
Context.eventEmitter.emit("serverError", { server: "rtmp", error: err });
});
}
Comment thread src/server/rtmp_server.js
Comment on lines 24 to 33
if (Context.config.rtmps?.port) {
const opt = {
key: fs.readFileSync(Context.config.rtmps.key),
cert: fs.readFileSync(Context.config.rtmps.cert),
};
this.tlsServer = tls.createServer(opt, this.handleRequest);
this.tlsServer.on("error", (err) => {
Context.eventEmitter.emit("serverError", { server: "rtmps", error: err });
});
}
Comment thread src/server/rtmp_server.js
Comment on lines +49 to +54
await Promise.allSettled([
closeServer(this.tcpServer),
closeServer(this.tlsServer),
]);
console.log("closed rtmps server");
}
Comment thread src/server/http_server.js
Comment on lines 52 to 57
if (Context.config.http?.port) {
this.httpServer = http.createServer(app);
this.httpServer.on("error", (err) => {
Context.eventEmitter.emit("serverError", { server: "http", error: err });
});
this.wsServer = new WebSocket.Server({ server: this.httpServer });
Comment thread src/server/http_server.js
Comment on lines 65 to +74
@@ -63,23 +69,65 @@ class NodeHttpServer {
allowHTTP1: true
};
this.httpsServer = http2.createSecureServer(opt, app);
this.httpsServer.on("error", (err) => {
Context.eventEmitter.emit("serverError", { server: "https", error: err });
});
Comment thread src/index.js
Comment on lines +48 to +55
const closePromises = [];
for (const [id, session] of Context.sessions) {
closePromises.push(
session.close()
);
}
await Promise.allSettled(closePromises)
Context.sessions.clear();
Comment on lines 81 to 83
// Call the close method on the session
session.close();
await session.close();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants