@punks/backend-core 0.0.46 → 0.0.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,7 @@ export declare const buildObject: <T>(...props: {
8
8
  }[]) => T;
9
9
  export declare const mergeDeep: <T>(a: any, b: any) => T;
10
10
  export declare const jsonSerialize: (obj: any, options?: {
11
+ removeCircularReferences?: boolean;
11
12
  stripBinaryData?: boolean;
12
13
  maxStringsLength?: number;
13
14
  prettify?: boolean;
package/dist/esm/index.js CHANGED
@@ -288,20 +288,32 @@ const mergeDeep = (a, b) => {
288
288
  return c;
289
289
  };
290
290
  const jsonSerialize = (obj, options) => {
291
- const { stripBinaryData = true, prettify = false, maxStringsLength, } = options ?? {};
291
+ const { removeCircularReferences = true, stripBinaryData = true, prettify = false, maxStringsLength, } = options ?? {};
292
292
  const replacer = (key, value) => {
293
- if (typeof value === "string" && maxStringsLength) {
293
+ // remove circular references
294
+ const seen = new WeakSet();
295
+ if (removeCircularReferences &&
296
+ typeof value === "object" &&
297
+ value !== null) {
298
+ if (seen.has(value)) {
299
+ return;
300
+ }
301
+ seen.add(value);
302
+ }
303
+ // trim long strings
304
+ if (maxStringsLength && typeof value === "string") {
294
305
  return value.length > maxStringsLength
295
306
  ? `${value.substring(0, maxStringsLength)}...`
296
307
  : value;
297
308
  }
309
+ // strip binary data
298
310
  if (stripBinaryData) {
299
311
  if (value instanceof Buffer) {
300
312
  return `<binary data (${value.length} bytes)>`;
301
313
  }
302
314
  if (typeof value === "object" &&
303
- value.type === "Buffer" &&
304
- Array.isArray(value.data)) {
315
+ value?.type === "Buffer" &&
316
+ Array.isArray(value?.data)) {
305
317
  return `<binary data (${value.data.length} bytes)>`;
306
318
  }
307
319
  }