@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.
package/dist/cjs/index.js CHANGED
@@ -296,14 +296,39 @@ const mergeDeep = (a, b) => {
296
296
  }
297
297
  return c;
298
298
  };
299
+ const isSerializable = (obj) => {
300
+ try {
301
+ JSON.stringify(obj);
302
+ return true;
303
+ }
304
+ catch (e) {
305
+ return false;
306
+ }
307
+ };
299
308
  const jsonSerialize = (obj, options) => {
300
- const { stripBinaryData = true, prettify = false, maxStringsLength, } = options ?? {};
309
+ const { removeNonSerializableObjects = true, removeCircularReferences = true, stripBinaryData = true, prettify = false, maxStringsLength, } = options ?? {};
301
310
  const replacer = (key, value) => {
302
- if (typeof value === "string" && maxStringsLength) {
311
+ // remove non serializable objects
312
+ if (removeNonSerializableObjects && !isSerializable(value)) {
313
+ return `<non serializable property ${typeof value}>`;
314
+ }
315
+ // remove circular references
316
+ const seen = new WeakSet();
317
+ if (removeCircularReferences &&
318
+ typeof value === "object" &&
319
+ value !== null) {
320
+ if (seen.has(value)) {
321
+ return;
322
+ }
323
+ seen.add(value);
324
+ }
325
+ // trim long strings
326
+ if (maxStringsLength && typeof value === "string") {
303
327
  return value.length > maxStringsLength
304
328
  ? `${value.substring(0, maxStringsLength)}...`
305
329
  : value;
306
330
  }
331
+ // strip binary data
307
332
  if (stripBinaryData) {
308
333
  if (value instanceof Buffer) {
309
334
  return `<binary data (${value.length} bytes)>`;
@@ -28480,16 +28505,18 @@ class InternalLogger {
28480
28505
  }
28481
28506
  }
28482
28507
  serializeMeta(meta) {
28483
- const serializedMeta = jsonSerialize(meta, {
28484
- prettify: true,
28485
- stripBinaryData: true,
28486
- });
28508
+ const serializedMeta = meta
28509
+ ? jsonSerialize(meta, {
28510
+ prettify: true,
28511
+ stripBinaryData: true,
28512
+ })
28513
+ : undefined;
28487
28514
  switch (this.options.serialization) {
28488
28515
  case exports.MetaSerializationType.JSON:
28489
28516
  return serializedMeta;
28490
28517
  case exports.MetaSerializationType.None:
28491
28518
  default:
28492
- return JSON.parse(serializedMeta);
28519
+ return serializedMeta ? JSON.parse(serializedMeta) : undefined;
28493
28520
  }
28494
28521
  }
28495
28522
  }