Skip to content

Commit

Permalink
avoid making copies until we have to
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertCraigie committed Dec 16, 2024
1 parent e9ff584 commit 28bc8e3
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1147,27 +1147,32 @@ export function debug(action: string, ...args: any[]) {
return arg;
}

const modifiedArg = { ...arg };

// Check for sensitive headers in request body 'headers' object
if (arg['headers']) {
// shallow clone so we don't mutate
modifiedArg['headers'] = { ...arg['headers'] };
// clone so we don't mutate
const modifiedArg = { ...arg, headers: { ...arg['headers'] } };

for (const header in arg['headers']) {
if (SENSITIVE_HEADERS.has(header.toLowerCase())) {
modifiedArg['headers'][header] = 'REDACTED';
}
}

return modifiedArg;
}

let modifiedArg = null;

// Check for sensitive headers in headers object
for (const header in modifiedArg) {
for (const header in arg) {
if (SENSITIVE_HEADERS.has(header.toLowerCase())) {
// avoid making a copy until we need to
modifiedArg ??= { ...arg };
modifiedArg[header] = 'REDACTED';
}
}
return modifiedArg;

return modifiedArg ?? arg;
});
console.log(`OpenAI:DEBUG:${action}`, ...modifiedArgs);
}
Expand Down

0 comments on commit 28bc8e3

Please sign in to comment.