fix(api): завершать запрос при обрыве ответа RouterOS

This commit is contained in:
Denozordec committed 2026-10-10 02:06:56 +07:00
1 parent 5944e5e751
commit 7f0c2aad0f
13 files changed
+1005 -31

No files matched your search

+12
View File
@@ -15,6 +15,12 @@ import type {
const MAX_ROS_BODY_BYTES = 8 * 1024 * 1024
function watchRosResponseFailure(res: http.IncomingMessage, reject: (error: Error) => void): void {
res.once("aborted", () => reject(new Error("RouterOS response interrupted (ECONNRESET)")))
// IncomingMessage errors are separate from ClientRequest errors after headers arrive.
res.once("error", reject)
}
function appendRosBody(body: string, chunk: string, req?: http.ClientRequest): string {
if (body.length + chunk.length > MAX_ROS_BODY_BYTES) {
req?.destroy(new Error("RouterOS: ответ больше 8 МиБ"))
@@ -62,6 +68,7 @@ function rosRequest(
}, timeoutMs)
const req = lib.request(options, (res) => {
watchRosResponseFailure(res, error => { clearTimeout(timer); reject(error) })
let body = ""
res.setEncoding("utf8")
res.on("data", (chunk: string) => { body = appendRosBody(body, chunk, req) })
@@ -145,6 +152,7 @@ function rosPost(
externalSignal?.addEventListener("abort", onExternalAbort)
req = lib.request(options, (res) => {
watchRosResponseFailure(res, error => settle(() => reject(error)))
let buf = ""
res.setEncoding("utf8")
res.on("data", (chunk: string) => { buf = appendRosBody(buf, chunk, req) })
@@ -199,6 +207,7 @@ function rosPut(
}, timeoutMs)
const req = lib.request(options, (res) => {
watchRosResponseFailure(res, error => { clearTimeout(timer); reject(error) })
let buf = ""
res.setEncoding("utf8")
res.on("data", (chunk: string) => { buf = appendRosBody(buf, chunk, req) })
@@ -249,6 +258,7 @@ function rosDelete(
}, timeoutMs)
const req = lib.request(options, (res) => {
watchRosResponseFailure(res, error => { clearTimeout(timer); reject(error) })
let body = ""
res.setEncoding("utf8")
res.on("data", (chunk: string) => { body = appendRosBody(body, chunk, req) })
@@ -296,6 +306,7 @@ function rosRawRequest(
const lib = params.useSsl ? https : http
const req = lib.request(options, (res) => {
watchRosResponseFailure(res, error => { clearTimeout(timer); reject(error) })
const chunks: Buffer[] = []
res.on("data", (chunk: Buffer) => { chunks.push(chunk) })
res.on("end", () => {
@@ -351,6 +362,7 @@ function rosPatch(
}, timeoutMs)
const req = lib.request(options, (res) => {
watchRosResponseFailure(res, error => { clearTimeout(timer); reject(error) })
let buf = ""
res.setEncoding("utf8")
res.on("data", (chunk: string) => { buf = appendRosBody(buf, chunk, req) })