Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use circular-safe toJson in AwsLogger #686

Merged
merged 1 commit into from
Jul 27, 2024
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
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
60 changes: 33 additions & 27 deletions howdju-text-fragments/dist/rangeToFragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,19 +598,19 @@
// ../node_modules/lodash/identity.js
var require_identity = __commonJS({
"../node_modules/lodash/identity.js"(exports, module) {
function identity(value) {
function identity2(value) {
return value;
}
module.exports = identity;
module.exports = identity2;
}
});

// ../node_modules/lodash/_castFunction.js
var require_castFunction = __commonJS({
"../node_modules/lodash/_castFunction.js"(exports, module) {
var identity = require_identity();
var identity2 = require_identity();
function castFunction(value) {
return typeof value == "function" ? value : identity;
return typeof value == "function" ? value : identity2;
}
module.exports = castFunction;
}
Expand Down Expand Up @@ -2138,7 +2138,7 @@
return value;
}
if (value == null) {
return identity;
return identity2;
}
if (typeof value == "object") {
return isArray3(value) ? baseMatchesProperty(value[0], value[1]) : baseMatches(value);
Expand Down Expand Up @@ -2276,7 +2276,7 @@
return iteratee2;
});
} else {
iteratees = [identity];
iteratees = [identity2];
}
var index = -1;
iteratees = arrayMap(iteratees, baseUnary(getIteratee()));
Expand Down Expand Up @@ -2372,7 +2372,7 @@
return result2;
}
function baseRest(func, start) {
return setToString(overRest(func, start, identity), func + "");
return setToString(overRest(func, start, identity2), func + "");
}
function baseSample(collection) {
return arraySample(values2(collection));
Expand Down Expand Up @@ -2404,11 +2404,11 @@
}
return object;
}
var baseSetData = !metaMap ? identity : function(func, data) {
var baseSetData = !metaMap ? identity2 : function(func, data) {
metaMap.set(func, data);
return func;
};
var baseSetToString = !defineProperty ? identity : function(func, string) {
var baseSetToString = !defineProperty ? identity2 : function(func, string) {
return defineProperty(func, "toString", {
"configurable": true,
"enumerable": false,
Expand Down Expand Up @@ -2457,7 +2457,7 @@
}
return high;
}
return baseSortedIndexBy(array, value, identity, retHighest);
return baseSortedIndexBy(array, value, identity2, retHighest);
}
function baseSortedIndexBy(array, value, iteratee2, retHighest) {
var low = 0, high = array == null ? 0 : array.length;
Expand Down Expand Up @@ -2613,7 +2613,7 @@
return isArrayLikeObject(value) ? value : [];
}
function castFunction(value) {
return typeof value == "function" ? value : identity;
return typeof value == "function" ? value : identity2;
}
function castPath(value, object) {
if (isArray3(value)) {
Expand Down Expand Up @@ -4963,7 +4963,7 @@
value = nativeObjectToString.call(value);
}
result2[value] = key;
}, constant(identity));
}, constant(identity2));
var invertBy = createInverter(function(result2, value, key) {
if (value != null && typeof value.toString != "function") {
value = nativeObjectToString.call(value);
Expand Down Expand Up @@ -5466,7 +5466,7 @@
}
var flow = createFlow();
var flowRight = createFlow(true);
function identity(value) {
function identity2(value) {
return value;
}
function iteratee(func) {
Expand Down Expand Up @@ -5590,19 +5590,19 @@
}, 1);
var floor = createRound("floor");
function max(array) {
return array && array.length ? baseExtremum(array, identity, baseGt) : undefined2;
return array && array.length ? baseExtremum(array, identity2, baseGt) : undefined2;
}
function maxBy(array, iteratee2) {
return array && array.length ? baseExtremum(array, getIteratee(iteratee2, 2), baseGt) : undefined2;
}
function mean(array) {
return baseMean(array, identity);
return baseMean(array, identity2);
}
function meanBy(array, iteratee2) {
return baseMean(array, getIteratee(iteratee2, 2));
}
function min(array) {
return array && array.length ? baseExtremum(array, identity, baseLt) : undefined2;
return array && array.length ? baseExtremum(array, identity2, baseLt) : undefined2;
}
function minBy(array, iteratee2) {
return array && array.length ? baseExtremum(array, getIteratee(iteratee2, 2), baseLt) : undefined2;
Expand All @@ -5615,7 +5615,7 @@
return minuend - subtrahend;
}, 0);
function sum(array) {
return array && array.length ? baseSum(array, identity) : 0;
return array && array.length ? baseSum(array, identity2) : 0;
}
function sumBy(array, iteratee2) {
return array && array.length ? baseSum(array, getIteratee(iteratee2, 2)) : 0;
Expand Down Expand Up @@ -5812,7 +5812,7 @@
lodash.has = has;
lodash.hasIn = hasIn;
lodash.head = head2;
lodash.identity = identity;
lodash.identity = identity2;
lodash.includes = includes;
lodash.indexOf = indexOf2;
lodash.inRange = inRange;
Expand Down Expand Up @@ -5982,7 +5982,7 @@
};
});
LazyWrapper.prototype.compact = function() {
return this.filter(identity);
return this.filter(identity2);
};
LazyWrapper.prototype.find = function(predicate) {
return this.filter(predicate).head();
Expand Down Expand Up @@ -10178,11 +10178,15 @@
return void 0;
};
}
function toJsonWithReplacer(val, replacer) {
const seen = /* @__PURE__ */ new WeakSet();
return JSON.stringify(val, handleCircularReferences(seen, replacer));
}
function toJson(val) {
const seen = /* @__PURE__ */ new WeakSet();
return JSON.stringify(val, handleCircularReferences(seen));
}
function handleCircularReferences(seen) {
function handleCircularReferences(seen, replacer = import_lodash2.identity) {
return function(_key, value) {
if (value === null || typeof value !== "object") {
return value;
Expand All @@ -10196,7 +10200,8 @@
seen.add(value);
const newValue = 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);
return newValue;
Expand Down Expand Up @@ -28295,15 +28300,15 @@
"../node_modules/lodash/_baseIteratee.js"(exports, module) {
var baseMatches = require_baseMatches();
var baseMatchesProperty = require_baseMatchesProperty();
var identity = require_identity();
var identity2 = require_identity();
var isArray3 = require_isArray();
var property = require_property();
function baseIteratee(value) {
if (typeof value == "function") {
return value;
}
if (value == null) {
return identity;
return identity2;
}
if (typeof value == "object") {
return isArray3(value) ? baseMatchesProperty(value[0], value[1]) : baseMatches(value);
Expand Down Expand Up @@ -28438,8 +28443,8 @@
"../node_modules/lodash/_baseSetToString.js"(exports, module) {
var constant = require_constant();
var defineProperty = require_defineProperty();
var identity = require_identity();
var baseSetToString = !defineProperty ? identity : function(func, string) {
var identity2 = require_identity();
var baseSetToString = !defineProperty ? identity2 : function(func, string) {
return defineProperty(func, "toString", {
"configurable": true,
"enumerable": false,
Expand Down Expand Up @@ -28489,11 +28494,11 @@
// ../node_modules/lodash/_baseRest.js
var require_baseRest = __commonJS({
"../node_modules/lodash/_baseRest.js"(exports, module) {
var identity = require_identity();
var identity2 = require_identity();
var overRest = require_overRest();
var setToString = require_setToString();
function baseRest(func, start) {
return setToString(overRest(func, start, identity), func + "");
return setToString(overRest(func, start, identity2), func + "");
}
module.exports = baseRest;
}
Expand Down Expand Up @@ -32514,6 +32519,7 @@
timestampFormatString: () => timestampFormatString,
toEntries: () => toEntries,
toJson: () => toJson,
toJsonWithReplacer: () => toJsonWithReplacer,
toPlainTextContent: () => toPlainTextContent,
toSingleLine: () => toSingleLine,
toSlug: () => toSlug,
Expand Down
Loading