formacao
This commit is contained in:
+73
-3
@@ -186,6 +186,17 @@ var requestPrototype = {
|
||||
}
|
||||
});
|
||||
});
|
||||
Object.defineProperty(requestPrototype, Symbol.for("nodejs.util.inspect.custom"), {
|
||||
value: function(depth, options, inspectFn) {
|
||||
const props = {
|
||||
method: this.method,
|
||||
url: this.url,
|
||||
headers: this.headers,
|
||||
nativeRequest: this[requestCache]
|
||||
};
|
||||
return `Request (lightweight) ${inspectFn(props, { ...options, depth: depth == null ? null : depth - 1 })}`;
|
||||
}
|
||||
});
|
||||
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
||||
var newRequest = (incoming, defaultHostname) => {
|
||||
const req = Object.create(requestPrototype);
|
||||
@@ -292,6 +303,17 @@ var Response2 = class _Response {
|
||||
}
|
||||
});
|
||||
});
|
||||
Object.defineProperty(Response2.prototype, Symbol.for("nodejs.util.inspect.custom"), {
|
||||
value: function(depth, options, inspectFn) {
|
||||
const props = {
|
||||
status: this.status,
|
||||
headers: this.headers,
|
||||
ok: this.ok,
|
||||
nativeResponse: this[responseCache]
|
||||
};
|
||||
return `Response (lightweight) ${inspectFn(props, { ...options, depth: depth == null ? null : depth - 1 })}`;
|
||||
}
|
||||
});
|
||||
Object.setPrototypeOf(Response2, GlobalResponse);
|
||||
Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
||||
|
||||
@@ -372,6 +394,50 @@ if (typeof global.crypto === "undefined") {
|
||||
|
||||
// src/listener.ts
|
||||
var outgoingEnded = Symbol("outgoingEnded");
|
||||
var incomingDraining = Symbol("incomingDraining");
|
||||
var DRAIN_TIMEOUT_MS = 500;
|
||||
var MAX_DRAIN_BYTES = 64 * 1024 * 1024;
|
||||
var drainIncoming = (incoming) => {
|
||||
const incomingWithDrainState = incoming;
|
||||
if (incoming.destroyed || incomingWithDrainState[incomingDraining]) {
|
||||
return;
|
||||
}
|
||||
incomingWithDrainState[incomingDraining] = true;
|
||||
if (incoming instanceof import_node_http22.Http2ServerRequest) {
|
||||
try {
|
||||
;
|
||||
incoming.stream?.close?.(import_node_http22.constants.NGHTTP2_NO_ERROR);
|
||||
} catch {
|
||||
}
|
||||
return;
|
||||
}
|
||||
let bytesRead = 0;
|
||||
const cleanup = () => {
|
||||
clearTimeout(timer);
|
||||
incoming.off("data", onData);
|
||||
incoming.off("end", cleanup);
|
||||
incoming.off("error", cleanup);
|
||||
};
|
||||
const forceClose = () => {
|
||||
cleanup();
|
||||
const socket = incoming.socket;
|
||||
if (socket && !socket.destroyed) {
|
||||
socket.destroySoon();
|
||||
}
|
||||
};
|
||||
const timer = setTimeout(forceClose, DRAIN_TIMEOUT_MS);
|
||||
timer.unref?.();
|
||||
const onData = (chunk) => {
|
||||
bytesRead += chunk.length;
|
||||
if (bytesRead > MAX_DRAIN_BYTES) {
|
||||
forceClose();
|
||||
}
|
||||
};
|
||||
incoming.on("data", onData);
|
||||
incoming.on("end", cleanup);
|
||||
incoming.on("error", cleanup);
|
||||
incoming.resume();
|
||||
};
|
||||
var handleRequestError = () => new Response(null, {
|
||||
status: 400
|
||||
});
|
||||
@@ -543,14 +609,18 @@ var getRequestListener = (fetchCallback, options = {}) => {
|
||||
setTimeout(() => {
|
||||
if (!incomingEnded) {
|
||||
setTimeout(() => {
|
||||
incoming.destroy();
|
||||
outgoing.destroy();
|
||||
drainIncoming(incoming);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
outgoing.on("finish", () => {
|
||||
if (!incomingEnded) {
|
||||
drainIncoming(incoming);
|
||||
}
|
||||
});
|
||||
}
|
||||
outgoing.on("close", () => {
|
||||
const abortController = req[abortControllerKey];
|
||||
@@ -565,7 +635,7 @@ var getRequestListener = (fetchCallback, options = {}) => {
|
||||
setTimeout(() => {
|
||||
if (!incomingEnded) {
|
||||
setTimeout(() => {
|
||||
incoming.destroy();
|
||||
drainIncoming(incoming);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user