Skip to content
Open
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
4 changes: 2 additions & 2 deletions library/agent/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export type Context = {
method: string | undefined;
query: ParsedQs;
headers: Record<string, string | string[] | undefined>;
routeParams: Record<string, string> | undefined;
routeParams: Record<string, string | string[]> | undefined;
remoteAddress: string | undefined;
body: unknown; // Can be an object, string or undefined (the body is parsed by something like body-parser)
cookies: Record<string, string>;
cookies: Record<string, unknown>;
attackDetected?: boolean;
blockedDueToIPOrBot?: boolean;
consumedRateLimit?: boolean;
Expand Down
4 changes: 1 addition & 3 deletions library/agent/hooks/instrumentation/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { onModuleLoad } from "./loadHook";
import * as mod from "node:module";
import type { RegisterHookFunction } from "./types";
import { patchProcessGetBuiltinModule } from "./processGetBuiltin";
import { checkHooks } from "./checkHooks";

Expand All @@ -18,8 +17,7 @@ export function registerNodeHooks() {
hooksRegistered = true;

// Hook into the ESM & CJS module loading process
// Types are required because official Node.js typings are not up-to-date
(mod.registerHooks as RegisterHookFunction)({
mod.registerHooks({
load(url, context, nextLoad) {
const result = nextLoad(url, context);
return onModuleLoad(url, context, result);
Expand Down
25 changes: 11 additions & 14 deletions library/agent/hooks/instrumentation/loadHook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { LoadFunction } from "./types";
import { getModuleInfoFromPath } from "../getModuleInfoFromPath";
import { isBuiltinModule } from "../isBuiltinModule";
import { getPackageVersionFromPath } from "./getPackageVersionFromPath";
Expand All @@ -11,17 +10,21 @@ import {
} from "./instructions";
import { removeNodePrefix } from "../../../helpers/removeNodePrefix";
import { getInstance } from "../../AgentSingleton";
import { syncBuiltinESMExports } from "module";
import {
type LoadFnOutput,
type LoadHookContext,
syncBuiltinESMExports,
} from "module";
import { getBuiltinModuleWithoutPatching } from "./processGetBuiltin";
import { wrapBuiltinExports } from "./wrapBuiltinExports";

const builtinPatchedSymbol = Symbol("zen.instrumentation.builtin.patched");

export function onModuleLoad(
path: string,
context: Parameters<LoadFunction>[1],
previousLoadResult: ReturnType<LoadFunction>
): ReturnType<LoadFunction> {
_context: LoadHookContext,
previousLoadResult: LoadFnOutput
): LoadFnOutput {
try {
// Ignore unsupported formats, e.g. wasm, native addons or json
if (
Expand Down Expand Up @@ -65,10 +68,7 @@ export function onModuleLoad(
}
}

function patchPackage(
path: string,
previousLoadResult: ReturnType<LoadFunction>
) {
function patchPackage(path: string, previousLoadResult: LoadFnOutput) {
const moduleInfo = getModuleInfoFromPath(path);
if (!moduleInfo) {
// This is e.g. the case for user code (not a dependency)
Expand Down Expand Up @@ -144,10 +144,7 @@ function patchPackage(
};
}

function patchBuiltin(
builtinName: string,
previousLoadResult: ReturnType<LoadFunction>
) {
function patchBuiltin(builtinName: string, previousLoadResult: LoadFnOutput) {
const builtinNameWithoutPrefix = removeNodePrefix(builtinName);

const builtin = shouldPatchBuiltin(builtinNameWithoutPrefix);
Expand Down Expand Up @@ -188,7 +185,7 @@ function isSelfCheckImport(path: string) {
.includes("hooks/instrumentation/zenHooksCheckImport."); // .js or .ts
}

function updateSelfCheckSource(previousLoadResult: ReturnType<LoadFunction>) {
function updateSelfCheckSource(previousLoadResult: LoadFnOutput) {
const sourceString =
typeof previousLoadResult.source === "string"
? previousLoadResult.source
Expand Down
49 changes: 0 additions & 49 deletions library/agent/hooks/instrumentation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,6 @@ import type {
} from "../wrapExport";
import type { PartialWrapPackageInfo } from "../WrapPackageInfo";

type TypedArray =
| Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array
| BigInt64Array
| BigUint64Array;

type LoadReturnValue = {
format: string;
shortCircuit: boolean | undefined;
source: string | ArrayBuffer | TypedArray;
};

type ResolveReturnValue = {
format: string | null | undefined;
importAttributes: object | undefined;
shortCircuit: boolean | undefined;
url: string;
};

export type LoadFunction = (
url: string,
context: {
conditions: Set<string> | string[];
format: string | null | undefined;
importAttributes: object;
},
nextLoad: (url: string, context: object) => LoadReturnValue
) => LoadReturnValue;

export type RegisterHookFunction = (options: {
load?: LoadFunction;
resolve?: (
specifier: string,
context: {
conditions: string[];
importAttributes: object;
parentURL: string;
},
nextResolve: (specifier: string, context: object) => ResolveReturnValue
) => ResolveReturnValue;
}) => void;

export type IntereptorFunctionsObj = {
inspectArgs?: InspectArgsInterceptor;
modifyArgs?: ModifyArgsInterceptor;
Expand Down
Loading
Loading