feat: 增加错误处理,增加退出方法,增加类型提示#692
Open
sylw114 wants to merge 2 commits into
Open
Conversation
There was a problem hiding this comment.
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/sessionErrorevent emissions across servers and sessions. - Introduced async
close()semantics for some sessions and addedstop()methods for HTTP/RTMP servers plus a top-levelNodeMediaServer.stop(). - Added a large
src/index.d.tsto 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 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 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 on lines
+49
to
+54
| await Promise.allSettled([ | ||
| closeServer(this.tcpServer), | ||
| closeServer(this.tlsServer), | ||
| ]); | ||
| console.log("closed rtmps server"); | ||
| } |
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 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 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(); | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
所有代码都是AI完成的,主要是要是没这几个东西,通过导入使用会有非常大的问题。