mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-02 17:13:33 +01:00
Merge pull request #986 from tdurieux/fix/client-abort-logging
fix: stop logging cancelled downloads as streamer 500s
This commit is contained in:
3 files changed
+138
-6
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { recordResponseFailure } from "./request-evidence";
|
||||
import { watchClientClose } from "./response-cancellation";
|
||||
import { createGzip } from "zlib";
|
||||
import { createHash } from "crypto";
|
||||
import { Readable, PassThrough } from "stream";
|
||||
@@ -53,12 +54,19 @@ async function sendCached(content: Readable, res: Response,
|
||||
// Install evidence before pipeline's listeners destroy the response.
|
||||
const localFailure = () => { if (!res.destroyed) recordResponseFailure(res, "unknown", "local_abort"); };
|
||||
stream.once("error", localFailure);
|
||||
if (record.encoding === "identity" && !res.req.acceptsEncodings("identity")) {
|
||||
res.setHeader("Content-Encoding", "gzip"); res.removeHeader("Content-Length");
|
||||
const gzip = createGzip();
|
||||
gzip.once("error", localFailure);
|
||||
await pipeline(stream, gzip, res);
|
||||
} else await pipeline(stream, res);
|
||||
const clientClosed = watchClientClose(res, stream);
|
||||
try {
|
||||
if (record.encoding === "identity" && !res.req.acceptsEncodings("identity")) {
|
||||
res.setHeader("Content-Encoding", "gzip"); res.removeHeader("Content-Length");
|
||||
const gzip = createGzip();
|
||||
gzip.once("error", localFailure);
|
||||
await pipeline(stream, gzip, res);
|
||||
} else await pipeline(stream, res);
|
||||
} catch (error) {
|
||||
// A visitor who stops a download is not a server fault.
|
||||
if (clientClosed()) throw Object.assign(new Error("response_cancelled"), { cause: error });
|
||||
throw error;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
const decision = results.decision(key);
|
||||
|
||||
@@ -14,3 +14,19 @@ export function isResponseCancellation(error: unknown, response?: { destroyed?:
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* pipeline() rejects with ERR_STREAM_PREMATURE_CLOSE both when the client goes
|
||||
* away and when the source ends early. Call before piping; the returned check
|
||||
* is true only when the response closed unfinished before the source failed.
|
||||
*/
|
||||
export function watchClientClose(response: { writableFinished?: boolean; once(event: "close", listener: () => void): unknown },
|
||||
source: { readableEnded?: boolean; once(event: "close" | "error", listener: () => void): unknown }): () => boolean {
|
||||
let sourceFailed = false;
|
||||
let clientClosed = false;
|
||||
source.once("error", () => { sourceFailed = true; });
|
||||
// A source that closes without ending was cut short.
|
||||
source.once("close", () => { if (!source.readableEnded) sourceFailed = true; });
|
||||
response.once("close", () => { if (!sourceFailed && !response.writableFinished) clientClosed = true; });
|
||||
return () => clientClosed;
|
||||
}
|
||||
Reference in new issue
Block a user