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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ development, not ready for production use, and the feature set is currently lim
- Node 18+ supported.
- Instruments all the files under current directory that aren't node_modules.
- Only captures named `function`s and methods.
- Only whole process recording and Jest, mocha, vitest per-test recordings.
- Remote recording not supported yet.
6 changes: 5 additions & 1 deletion src/Recording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import compactObject from "./util/compactObject";

export default class Recording {
constructor(type: AppMap.RecorderType, recorder: string, ...names: string[]) {
const dirs = [recorder, ...names];
const dirs = [recorder, ...names].map(quotePathSegment);
const name = dirs.pop()!; // it must have at least one element
this.path = join(appMapDir, ...dirs, makeAppMapFilename(name));
this.stream = new AppMapStream(this.path);
Expand Down Expand Up @@ -211,3 +211,7 @@ function makeAppMapFilename(name: string): string {
// TODO make sure it isn't too long
return name + ".appmap.json";
}

function quotePathSegment(value: string): string {
return value.replaceAll(/[/\\]/g, "-");
}
25 changes: 21 additions & 4 deletions src/hooks/http.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import assert from "node:assert";
import { ClientRequest } from "node:http";
import type http from "node:http";
import { ClientRequest } from "node:http";
import type https from "node:https";
import { URL } from "node:url";

import type AppMap from "../AppMap";
import Recording from "../Recording";
import { info } from "../message";
import { parameter } from "../parameter";
import { recording } from "../recorder";
import { recording, start } from "../recorder";
import { getTime } from "../util/getTime";

type HTTP = typeof http | typeof https;
Expand Down Expand Up @@ -111,6 +113,7 @@ function handleRequest(request: http.IncomingMessage, response: http.ServerRespo
if (requests.has(request)) return;
if (!(request.method && request.url)) return;
const url = new URL(request.url, "http://example");
const timestamp = startRequestRecording(url.pathname);
const requestEvent = recording.httpRequest(
request.method,
url.pathname,
Expand All @@ -121,8 +124,7 @@ function handleRequest(request: http.IncomingMessage, response: http.ServerRespo
const startTime = getTime();
requests.add(request);
response.once("finish", () => {
if (fixupEvent(request, requestEvent)) recording.fixup(requestEvent);
handleResponse(requestEvent, startTime, response);
handleResponse(request, requestEvent, startTime, timestamp, response);
});
}

Expand Down Expand Up @@ -184,16 +186,31 @@ function normalizeHeaders(
}

function handleResponse(
request: http.IncomingMessage,
requestEvent: AppMap.HttpServerRequestEvent,
startTime: number,
timestamp: string,
response: http.ServerResponse<http.IncomingMessage>,
): void {
if (fixupEvent(request, requestEvent)) recording.fixup(requestEvent);
recording.httpResponse(
requestEvent.id,
getTime() - startTime,
response.statusCode,
normalizeHeaders(response.getHeaders()),
);
const { request_method, path_info } = requestEvent.http_server_request;
recording.metadata.name = `${request_method} ${path_info} (${response.statusCode}) — ${timestamp}`;
recording.finish();
info("Wrote %s", recording.path);
start(); // just so there's always a recording running
}

function startRequestRecording(pathname: string): string {
recording.abandon();
const timestamp = new Date().toISOString();
start(new Recording("requests", "requests", [timestamp, pathname].join(" ")));
return timestamp;
}

function capitalize(str: string): string {
Expand Down
10 changes: 7 additions & 3 deletions src/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { FunctionInfo } from "./registry";
import commonPathPrefix from "./util/commonPathPrefix";
import { getTime } from "./util/getTime";

export let recording: Recording = new Recording("process", "process", new Date().toISOString());
export let recording: Recording;

export function record<This, Return>(
this: This,
Expand Down Expand Up @@ -64,11 +64,15 @@ function isNullPrototype(obj: unknown) {
return obj != null && Object.getPrototypeOf(obj) === null;
}

export function start(newRecording: Recording) {
assert(!recording.running);
export function start(
newRecording: Recording = new Recording("process", "process", new Date().toISOString()),
) {
assert(!recording?.running);
recording = newRecording;
}

start();

process.on("exit", () => {
recording.finish();
if (writtenAppMaps.length === 1) info("Wrote %s", writtenAppMaps[0]);
Expand Down
Loading