@vita-mojo/utils 1.0.11 → 1.0.12

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vita-mojo/utils",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "peerDependencies": {
5
5
  "tslib": "2.4.1"
6
6
  },
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Safely converts a JavaScript object to a JSON string.
3
+ * If the object contains circular references, they will be replaced with `[Circular]`.
4
+ * Copied from https://github.com/bugsnag/safe-json-stringify because it doesn't work with typescript
5
+ *
6
+ * @param value - The value to stringify.
7
+ * @param replacer - A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string.
8
+ * @param space - A String or Number object that's used to insert white space into the output JSON string for readability purposes.
9
+ * @param options - An object containing options for redacting keys and paths.
10
+ * @param options.redactedKeys - An array of keys or regular expressions that should be redacted from the output.
11
+ * @param options.redactedPaths - An array of strings representing paths to properties that should be redacted from the output.
12
+ * @returns The JSON string representation of the value.
13
+ */
14
+ export declare function fastSafeStringify(data: any, replacer?: null | ((this: any, key: string, value: any) => any), space?: null | string | number, opts?: {
15
+ redactedKeys?: Array<string | RegExp>;
16
+ redactedPaths?: string[];
17
+ }): string;
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fastSafeStringify = void 0;
4
+ /**
5
+ * Safely converts a JavaScript object to a JSON string.
6
+ * If the object contains circular references, they will be replaced with `[Circular]`.
7
+ * Copied from https://github.com/bugsnag/safe-json-stringify because it doesn't work with typescript
8
+ *
9
+ * @param value - The value to stringify.
10
+ * @param replacer - A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string.
11
+ * @param space - A String or Number object that's used to insert white space into the output JSON string for readability purposes.
12
+ * @param options - An object containing options for redacting keys and paths.
13
+ * @param options.redactedKeys - An array of keys or regular expressions that should be redacted from the output.
14
+ * @param options.redactedPaths - An array of strings representing paths to properties that should be redacted from the output.
15
+ * @returns The JSON string representation of the value.
16
+ */
17
+ function fastSafeStringify(data, replacer, space, opts) {
18
+ const redactedKeys = opts && opts.redactedKeys ? opts.redactedKeys : [];
19
+ const redactedPaths = opts && opts.redactedPaths ? opts.redactedPaths : [];
20
+ return JSON.stringify(prepareObjForSerialization(data, redactedKeys, redactedPaths), replacer, space);
21
+ }
22
+ exports.fastSafeStringify = fastSafeStringify;
23
+ const MAX_DEPTH = 20;
24
+ const MAX_EDGES = 25000;
25
+ const MIN_PRESERVED_DEPTH = 8;
26
+ const REPLACEMENT_NODE = '...';
27
+ function isError(o) {
28
+ return (o instanceof Error ||
29
+ /^\[object (Error|(Dom)?Exception)\]$/.test(Object.prototype.toString.call(o)));
30
+ }
31
+ function throwsMessage(err) {
32
+ return '[Throws: ' + (err ? err.message : '?') + ']';
33
+ }
34
+ function find(haystack, needle) {
35
+ for (let i = 0, len = haystack.length; i < len; i++) {
36
+ if (haystack[i] === needle)
37
+ return true;
38
+ }
39
+ return false;
40
+ }
41
+ // returns true if the string `path` starts with any of the provided `paths`
42
+ function isDescendent(paths, path) {
43
+ for (let i = 0, len = paths.length; i < len; i++) {
44
+ if (path.indexOf(paths[i]) === 0)
45
+ return true;
46
+ }
47
+ return false;
48
+ }
49
+ function shouldRedact(patterns, key) {
50
+ for (let i = 0, len = patterns.length; i < len; i++) {
51
+ if (typeof patterns[i] === 'string' &&
52
+ patterns[i].toLowerCase() === key.toLowerCase())
53
+ return true;
54
+ if (patterns[i] &&
55
+ typeof patterns[i].test === 'function' &&
56
+ patterns[i].test(key))
57
+ return true;
58
+ }
59
+ return false;
60
+ }
61
+ function isArray(obj) {
62
+ return Object.prototype.toString.call(obj) === '[object Array]';
63
+ }
64
+ function safelyGetProp(obj, prop) {
65
+ try {
66
+ return obj[prop];
67
+ }
68
+ catch (err) {
69
+ return throwsMessage(err);
70
+ }
71
+ }
72
+ function prepareObjForSerialization(obj, redactedKeys, redactedPaths) {
73
+ const seen = []; // store references to objects we have seen before
74
+ let edges = 0;
75
+ function visit(obj, path) {
76
+ function edgesExceeded() {
77
+ return path.length > MIN_PRESERVED_DEPTH && edges > MAX_EDGES;
78
+ }
79
+ edges++;
80
+ if (path.length > MAX_DEPTH)
81
+ return REPLACEMENT_NODE;
82
+ if (edgesExceeded())
83
+ return REPLACEMENT_NODE;
84
+ if (obj === null || typeof obj !== 'object')
85
+ return obj;
86
+ if (find(seen, obj))
87
+ return '[Circular]';
88
+ seen.push(obj);
89
+ if (typeof obj.toJSON === 'function') {
90
+ try {
91
+ // we're not going to count this as an edge because it
92
+ // replaces the value of the currently visited object
93
+ edges--;
94
+ const fResult = visit(obj.toJSON(), path);
95
+ seen.pop();
96
+ return fResult;
97
+ }
98
+ catch (err) {
99
+ return throwsMessage(err);
100
+ }
101
+ }
102
+ const er = isError(obj);
103
+ if (er) {
104
+ edges--;
105
+ const eResult = visit({ name: obj.name, message: obj.message }, path);
106
+ seen.pop();
107
+ return eResult;
108
+ }
109
+ if (isArray(obj)) {
110
+ const aResult = [];
111
+ for (let i = 0, len = obj.length; i < len; i++) {
112
+ if (edgesExceeded()) {
113
+ aResult.push(REPLACEMENT_NODE);
114
+ break;
115
+ }
116
+ aResult.push(visit(obj[i], path.concat('[]')));
117
+ }
118
+ seen.pop();
119
+ return aResult;
120
+ }
121
+ const result = {};
122
+ try {
123
+ for (const prop in obj) {
124
+ if (!Object.prototype.hasOwnProperty.call(obj, prop))
125
+ continue;
126
+ if (isDescendent(redactedPaths, path.join('.')) &&
127
+ shouldRedact(redactedKeys, prop)) {
128
+ result[prop] = '[REDACTED]';
129
+ continue;
130
+ }
131
+ if (edgesExceeded()) {
132
+ result[prop] = REPLACEMENT_NODE;
133
+ break;
134
+ }
135
+ result[prop] = visit(safelyGetProp(obj, prop), path.concat(prop));
136
+ }
137
+ }
138
+ catch (e) {
139
+ // noop
140
+ }
141
+ seen.pop();
142
+ return result;
143
+ }
144
+ return visit(obj, []);
145
+ }
146
+ //# sourceMappingURL=fast-safe-stringify.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fast-safe-stringify.js","sourceRoot":"","sources":["../../../../packages/utils/src/fast-safe-stringify.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;GAYG;AACH,SAAgB,iBAAiB,CAC/B,IAAS,EACT,QAA+D,EAC/D,KAA8B,EAC9B,IAGC;IAED,MAAM,YAAY,GAAG,IAAI,IAAI,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC;IACxE,MAAM,aAAa,GAAG,IAAI,IAAI,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,CAAC;IAE3E,OAAO,IAAI,CAAC,SAAS,CACnB,0BAA0B,CAAC,IAAI,EAAE,YAAY,EAAE,aAAa,CAAC,EAC7D,QAAe,EACf,KAAY,CACb,CAAC;AACJ,CAAC;AAjBD,8CAiBC;AAED,MAAM,SAAS,GAAG,EAAE,CAAC;AACrB,MAAM,SAAS,GAAG,KAAK,CAAC;AACxB,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAE9B,MAAM,gBAAgB,GAAG,KAAK,CAAC;AAE/B,SAAS,OAAO,CAAC,CAAC;IAChB,OAAO,CACL,CAAC,YAAY,KAAK;QAClB,sCAAsC,CAAC,IAAI,CACzC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAClC,CACF,CAAC;AACJ,CAAC;AAED,SAAS,aAAa,CAAC,GAAG;IACxB,OAAO,WAAW,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;AACvD,CAAC;AAED,SAAS,IAA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AAC,GAAG,gBAAgB,CAAC;oBAChC,MAAM;iBACP;gBAED,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,aAAa,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;aACnE;SACF;QAAC,OAAO,CAAC,EAAE;YACV,OAAO;SACR;QAED,IAAI,CAAC,GAAG,EAAE,CAAC;QAEX,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,OAAO,KAAK,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;AACxB,CAAC"}
package/src/index.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from './add-https-prefix';
2
+ export * from './fast-safe-stringify';
package/src/index.js CHANGED
@@ -2,4 +2,5 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./add-https-prefix"), exports);
5
+ tslib_1.__exportStar(require("./fast-safe-stringify"), exports);
5
6
  //# sourceMappingURL=index.js.map
package/src/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/utils/src/index.ts"],"names":[],"mappings":";;;AAAA,6DAAmC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/utils/src/index.ts"],"names":[],"mappings":";;;AAAA,6DAAmC;AACnC,gEAAsC"}