Skip to content

Commit

Permalink
Use circular-safe toJson in AwsLogger
Browse files Browse the repository at this point in the history
Signed-off-by: Carl Gieringer <78054+carlgieringer@users.noreply.github.com>
  • Loading branch information
carlgieringer committed Jul 27, 2024
1 parent a84a2fe commit 0bd8042
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
14 changes: 12 additions & 2 deletions howdju-common/lib/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
reject,
replace,
trim,
identity,
} from "lodash";
import moment, { Moment, unitOfTime, Duration, TemplateFunction } from "moment";
import isAbsoluteUrlLib from "is-absolute-url";
Expand Down Expand Up @@ -454,12 +455,20 @@ export const keysTo = (obj: { [k: string]: any }, val: any) =>
{} as { [k: string]: typeof val }
);

export function toJsonWithReplacer(val: any, replacer: (val: any) => any) {
const seen = new WeakSet();
return JSON.stringify(val, handleCircularReferences(seen, replacer));
}

export function toJson(val: any) {
const seen = new WeakSet();
return JSON.stringify(val, handleCircularReferences(seen));
}

function handleCircularReferences(seen: WeakSet<any>) {
function handleCircularReferences(
seen: WeakSet<any>,
replacer: (val: any) => any = identity
) {
return function (_key: string, value: any) {
if (value === null || typeof value !== "object") {
return value;
Expand All @@ -478,7 +487,8 @@ function handleCircularReferences(seen: WeakSet<any>) {
const newValue: Record<string, any> = Array.isArray(value) ? [] : {};

for (const [key, val] of Object.entries(value)) {
newValue[key] = handleCircularReferences(seen)(key, val);
const replaced = replacer(val);
newValue[key] = handleCircularReferences(seen, replacer)(key, replaced);
}

seen.delete(value);
Expand Down
4 changes: 2 additions & 2 deletions howdju-service-common/lib/logging/AwsLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const join = require("lodash/join");
const map = require("lodash/map");
const mapValues = require("lodash/mapValues");

const { utcTimestamp } = require("howdju-common");
const { toJsonWithReplacer, utcTimestamp } = require("howdju-common");

const { processArgs } = require("./processArgs");

Expand Down Expand Up @@ -91,7 +91,7 @@ const makeJsonLogArguments = function (logLevel, logLevelNumber, ...args) {
logRecord["data"] = data;
}

const logRecordJson = JSON.stringify(logRecord, jsonStringifyReplacer);
const logRecordJson = toJsonWithReplacer(logRecord, jsonStringifyReplacer);
return [logRecordJson];
};

Expand Down
3 changes: 1 addition & 2 deletions howdju-service-common/lib/logging/processArgs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const isObject = require("lodash/isObject");
const map = require("lodash/map");
const toString = require("lodash/toString");

const { toJson } = require("howdju-common");
Expand Down Expand Up @@ -32,7 +31,7 @@ const processArgs = (args, doUseCarriageReturns) => {
// If the args have interleaved strings and objects, then the objects' JSON should be included in the message
// Add all the previously seen objects

const datasJson = map(datas, toJson);
const datasJson = datas.map(toJson);
const spliceArgs = [messageParts.length, 0].concat(datasJson);
messageParts.splice.apply(messageParts, spliceArgs);
}
Expand Down

0 comments on commit 0bd8042

Please sign in to comment.