@punks/backend-core 0.0.47 → 0.0.49

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,8 @@ 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
+ removeNonSerializableObjects?: boolean;
12
+ removeCircularReferences?: boolean;
11
13
  stripBinaryData?: boolean;
12
14
  maxStringsLength?: number;
13
15
  prettify?: boolean;
package/dist/esm/index.js CHANGED
@@ -287,14 +287,39 @@ const mergeDeep = (a, b) => {
287
287
  }
288
288
  return c;
289
289
  };
290
+ const isSerializable = (obj) => {
291
+ try {
292
+ JSON.stringify(obj);
293
+ return true;
294
+ }
295
+ catch (e) {
296
+ return false;
297
+ }
298
+ };
290
299
  const jsonSerialize = (obj, options) => {
291
- const { stripBinaryData = true, prettify = false, maxStringsLength, } = options ?? {};
300
+ const { removeNonSerializableObjects = true, removeCircularReferences = true, stripBinaryData = true, prettify = false, maxStringsLength, } = options ?? {};
292
301
  const replacer = (key, value) => {
293
- if (typeof value === "string" && maxStringsLength) {
302
+ // remove non serializable objects
303
+ if (removeNonSerializableObjects && !isSerializable(value)) {
304
+ return `<non serializable property ${typeof value}>`;
305
+ }
306
+ // remove circular references
307
+ const seen = new WeakSet();
308
+ if (removeCircularReferences &&
309
+ typeof value === "object" &&
310
+ value !== null) {
311
+ if (seen.has(value)) {
312
+ return;
313
+ }
314
+ seen.add(value);
315
+ }
316
+ // trim long strings
317
+ if (maxStringsLength && typeof value === "string") {
294
318
  return value.length > maxStringsLength
295
319
  ? `${value.substring(0, maxStringsLength)}...`
296
320
  : value;
297
321
  }
322
+ // strip binary data
298
323
  if (stripBinaryData) {
299
324
  if (value instanceof Buffer) {
300
325
  return `<binary data (${value.length} bytes)>`;
@@ -28471,16 +28496,18 @@ class InternalLogger {
28471
28496
  }
28472
28497
  }
28473
28498
  serializeMeta(meta) {
28474
- const serializedMeta = jsonSerialize(meta, {
28475
- prettify: true,
28476
- stripBinaryData: true,
28477
- });
28499
+ const serializedMeta = meta
28500
+ ? jsonSerialize(meta, {
28501
+ prettify: true,
28502
+ stripBinaryData: true,
28503
+ })
28504
+ : undefined;
28478
28505
  switch (this.options.serialization) {
28479
28506
  case MetaSerializationType.JSON:
28480
28507
  return serializedMeta;
28481
28508
  case MetaSerializationType.None:
28482
28509
  default:
28483
- return JSON.parse(serializedMeta);
28510
+ return serializedMeta ? JSON.parse(serializedMeta) : undefined;
28484
28511
  }
28485
28512
  }
28486
28513
  }