Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion REPLAY_BACKEND_REV
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5d351da35be90b499fe21da825c1bac0a64dfd03
9b4881ad1084b10a14a3b08e2f46b5263d169f63
18 changes: 18 additions & 0 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const {
const {
defaultTriggerAsyncIdScope,
symbols: { async_id_symbol },
dependencyGraphInstrumented,
dependencyGraphResponseData,
dependencyGraphResponseEnd,
} = require('internal/async_hooks');
const {
codes: {
Expand Down Expand Up @@ -1050,6 +1053,13 @@ function emitErrorNt(msg, err, callback) {
}
}

let ServerResponse;
// The dependency graph reports a server's responses, not a client's requests.
function isServerResponse(msg) {
ServerResponse ??= require('_http_server').ServerResponse;
return msg instanceof ServerResponse;
}

function strictContentLength(msg) {
return (
msg.strictContentLength &&
Expand Down Expand Up @@ -1154,6 +1164,11 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
ret = msg._send(chunk, encoding, callback, len);
}

if (dependencyGraphInstrumented() && chunk.length !== 0 && isServerResponse(msg)) {
len ??= typeof chunk === 'string' ? Buffer.byteLength(chunk, encoding) : chunk.byteLength;
dependencyGraphResponseData(len);
}

debug('write ret = ' + ret);
return ret;
}
Expand Down Expand Up @@ -1362,6 +1377,9 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {

this.finished = true;

if (dependencyGraphInstrumented() && isServerResponse(this))
dependencyGraphResponseEnd();

// There is the first message on the outgoing queue, and we've sent
// everything to the socket.
debug('outgoing message end.');
Expand Down
2 changes: 2 additions & 0 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const {
const {
defaultTriggerAsyncIdScope,
getOrSetAsyncId,
dependencyGraphResponseStart,
} = require('internal/async_hooks');
const {
IncomingMessage,
Expand Down Expand Up @@ -513,6 +514,7 @@ function writeHead(statusCode, reason, obj) {
}

this._storeHeader(statusLine, headers);
dependencyGraphResponseStart(statusCode);

return this;
}
Expand Down
19 changes: 19 additions & 0 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,22 @@ function dependencyGraphEndDispatch() {
async_wrap.dependencyGraphEndDispatch();
}

// A server response's head, body writes and end, called by the JS that writes.
function dependencyGraphResponseStart(status) {
if (dependencyGraph)
async_wrap.dependencyGraphResponseStart(status);
}

function dependencyGraphResponseData(length) {
if (dependencyGraph)
async_wrap.dependencyGraphResponseData(length);
}

function dependencyGraphResponseEnd() {
if (dependencyGraph)
async_wrap.dependencyGraphResponseEnd();
}

// The options queueMicrotask() creates its AsyncResource with. The dependency
// graph reports a resource created with them as a microtask.
const microtaskResourceOptions = { requireManualDestroy: true };
Expand Down Expand Up @@ -684,6 +700,9 @@ module.exports = {
dependencyGraphInitMicrotask,
dependencyGraphBeginDispatch,
dependencyGraphEndDispatch,
dependencyGraphResponseStart,
dependencyGraphResponseData,
dependencyGraphResponseEnd,
microtaskResourceOptions,
nativeHooks: {
init: emitInitNative,
Expand Down
30 changes: 30 additions & 0 deletions src/node_dependency_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,30 @@ void EndDispatch(const FunctionCallbackInfo<Value>& args) {
GetCurrent(args).OnEndDispatch();
}

// A server response (http.ServerResponse): responseStart{status} at its head,
// responseData{length} per body write and responseEnd at its end. Each is made
// in the JS that writes it, which is its cause.
void ResponseStart(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsInt32());
if (!v8::recordreplay::DependencyGraphEnabled()) return;
v8::recordreplay::NewDependencyGraphNode(
NodeJson("responseStart",
std::pair{"status", args[0].As<v8::Int32>()->Value()})
.c_str());
}

void ResponseData(const FunctionCallbackInfo<Value>& args) {
double length = NumberArg(args, 0);
if (!v8::recordreplay::DependencyGraphEnabled()) return;
v8::recordreplay::NewDependencyGraphNode(
NodeJson("responseData", std::pair{"length", length}).c_str());
}

void ResponseEnd(const FunctionCallbackInfo<Value>& args) {
if (!v8::recordreplay::DependencyGraphEnabled()) return;
v8::recordreplay::NewDependencyGraphNode(R"({"kind":"responseEnd"})");
}

} // namespace

std::optional<DependencyExecution> DependencyExecution::Begin(int node) {
Expand Down Expand Up @@ -271,6 +295,9 @@ void AsyncDependencyGraph::CreatePerIsolateProperties(
SetMethod(isolate, target, "dependencyGraphPop", Pop);
SetMethod(isolate, target, "dependencyGraphBeginDispatch", BeginDispatch);
SetMethod(isolate, target, "dependencyGraphEndDispatch", EndDispatch);
SetMethod(isolate, target, "dependencyGraphResponseStart", ResponseStart);
SetMethod(isolate, target, "dependencyGraphResponseData", ResponseData);
SetMethod(isolate, target, "dependencyGraphResponseEnd", ResponseEnd);
}

void AsyncDependencyGraph::RegisterExternalReferences(
Expand All @@ -284,6 +311,9 @@ void AsyncDependencyGraph::RegisterExternalReferences(
registry->Register(Pop);
registry->Register(BeginDispatch);
registry->Register(EndDispatch);
registry->Register(ResponseStart);
registry->Register(ResponseData);
registry->Register(ResponseEnd);
}

} // namespace node