Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
perf: cache browser recorder bundle to speed up html instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
lachrist committed Apr 11, 2023
1 parent 521d96b commit 662d759
Showing 1 changed file with 47 additions and 62 deletions.
109 changes: 47 additions & 62 deletions components/mitm/default/intercept.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { readFile } from "node:fs";
import { readFileSync } from "node:fs";
import { Buffer } from "node:buffer";
import {
logDebugWhen,
logErrorWhen,
logWarning,
logDebug,
} from "../../log/index.mjs";
import { logDebugWhen, logWarning, logDebug } from "../../log/index.mjs";
import { self_directory } from "../../self/index.mjs";
import { URL } from "../../url/index.mjs";
import { InternalAppmapError } from "../../error/index.mjs";
Expand Down Expand Up @@ -38,6 +33,11 @@ const isJavascriptContentType = (header) =>

const isHtmlContentType = (header) => header.startsWith("text/html");

const recorder_browser_content = readFileSync(
new URL("dist/bundles/recorder-browser.mjs", self_directory),
"utf8",
);

const getContentTypeCharset = (header) => {
logDebugWhen(
!header.toLowerCase().includes("charset=utf-8"),
Expand Down Expand Up @@ -90,61 +90,46 @@ export const interceptRequest = (
hasOwnProperty(inc_res.headers, "content-type") &&
isHtmlContentType(inc_res.headers["content-type"])
) {
readFile(
new URL("dist/bundles/recorder-browser.mjs", self_directory),
"utf8",
(error, content) => {
assert(
!logErrorWhen(
error !== null,
"could not read recorder bundle for browser >> %o",
error,
),
"could not read recorder bundle for browser",
InternalAppmapError,
);
bufferReadable(inc_res, (buffer) => {
const encoding = getContentTypeCharset(
inc_res.headers["content-type"],
);
const body = toBuffer(
instrumentHtml(
partialxx_(instrumentJs, configuration, backend),
[
{
type: "script",
url: null,
content: `
"use strict";
((() => {
if (globalThis.__APPMAP_CONFIGURATION__ === void 0) {
globalThis.__APPMAP_CONFIGURATION__ = ${stringifyJSON(
configuration,
)};
globalThis.__APPMAP_LOG_LEVEL__ = ${stringifyJSON(
configuration.log.level,
)};
${content}
}
}) ());
`,
},
],
{
url: resolveHostPath(host, inc_req.url),
content: buffer.toString(encoding),
},
),
encoding,
);
out_res.writeHead(inc_res.statusCode, inc_res.statusMessage, {
...inc_res.headers,
"content-length": body.length,
});
out_res.end(body);
});
},
);
bufferReadable(inc_res, (buffer) => {
const encoding = getContentTypeCharset(
inc_res.headers["content-type"],
);
const body = toBuffer(
instrumentHtml(
partialxx_(instrumentJs, configuration, backend),
[
{
type: "script",
url: null,
content: `
"use strict";
((() => {
if (globalThis.__APPMAP_CONFIGURATION__ === void 0) {
globalThis.__APPMAP_CONFIGURATION__ = ${stringifyJSON(
configuration,
)};
globalThis.__APPMAP_LOG_LEVEL__ = ${stringifyJSON(
configuration.log.level,
)};
${recorder_browser_content}
}
}) ());
`,
},
],
{
url: resolveHostPath(host, inc_req.url),
content: buffer.toString(encoding),
},
),
encoding,
);
out_res.writeHead(inc_res.statusCode, inc_res.statusMessage, {
...inc_res.headers,
"content-length": body.length,
});
out_res.end(body);
});
} else if (
hasOwnProperty(inc_res.headers, "content-type") &&
isJavascriptContentType(inc_res.headers["content-type"])
Expand Down

0 comments on commit 662d759

Please sign in to comment.