@brianbuie/node-kit 0.4.0 → 0.5.0
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/README.md +3 -1
- package/{.dist → dist}/Log.js +3 -3
- package/{.dist → dist}/snapshot.d.ts +1 -1
- package/{.dist → dist}/snapshot.js +12 -7
- package/package.json +5 -5
- package/src/Log.ts +3 -3
- package/src/snapshot.test.ts +19 -0
- package/src/snapshot.ts +10 -6
- /package/{.dist → dist}/Cache.d.ts +0 -0
- /package/{.dist → dist}/Cache.js +0 -0
- /package/{.dist → dist}/Dir.d.ts +0 -0
- /package/{.dist → dist}/Dir.js +0 -0
- /package/{.dist → dist}/Fetcher.d.ts +0 -0
- /package/{.dist → dist}/Fetcher.js +0 -0
- /package/{.dist → dist}/File.d.ts +0 -0
- /package/{.dist → dist}/File.js +0 -0
- /package/{.dist → dist}/Jwt.d.ts +0 -0
- /package/{.dist → dist}/Jwt.js +0 -0
- /package/{.dist → dist}/Log.d.ts +0 -0
- /package/{.dist → dist}/TypeWriter.d.ts +0 -0
- /package/{.dist → dist}/TypeWriter.js +0 -0
- /package/{.dist → dist}/_index.d.ts +0 -0
- /package/{.dist → dist}/_index.js +0 -0
- /package/{.dist → dist}/timeout.d.ts +0 -0
- /package/{.dist → dist}/timeout.js +0 -0
package/README.md
CHANGED
package/{.dist → dist}/Log.js
RENAMED
|
@@ -23,7 +23,7 @@ export class Log {
|
|
|
23
23
|
static #toConsole(entry, color) {
|
|
24
24
|
if (entry.message)
|
|
25
25
|
console.log(color(`[${entry.severity}] ${entry.message}`));
|
|
26
|
-
entry.details?.forEach(detail => {
|
|
26
|
+
entry.details?.forEach((detail) => {
|
|
27
27
|
console.log(inspect(detail, { depth: 10, breakLength: 100, compact: true, colors: true }));
|
|
28
28
|
});
|
|
29
29
|
}
|
|
@@ -46,7 +46,7 @@ export class Log {
|
|
|
46
46
|
* Also snapshots special objects (eg Error, Response) to keep props in later JSON.stringify output
|
|
47
47
|
*/
|
|
48
48
|
static prepare(...input) {
|
|
49
|
-
let [first, ...rest] = input.map(snapshot);
|
|
49
|
+
let [first, ...rest] = input.map((i) => snapshot(i));
|
|
50
50
|
if (typeof first === 'string')
|
|
51
51
|
return { message: first, details: rest };
|
|
52
52
|
// @ts-ignore
|
|
@@ -73,7 +73,7 @@ export class Log {
|
|
|
73
73
|
return this.#log({ severity: 'INFO', color: chalk.white }, ...input);
|
|
74
74
|
}
|
|
75
75
|
static debug(...input) {
|
|
76
|
-
const debugging = process.argv.some(arg => arg.includes('--debug')) || process.env.DEBUG !== undefined;
|
|
76
|
+
const debugging = process.argv.some((arg) => arg.includes('--debug')) || process.env.DEBUG !== undefined;
|
|
77
77
|
if (debugging || process.env.NODE_ENV !== 'production') {
|
|
78
78
|
return this.#log({ severity: 'DEBUG', color: chalk.gray }, ...input);
|
|
79
79
|
}
|
|
@@ -3,19 +3,24 @@ import { isObjectLike } from 'lodash-es';
|
|
|
3
3
|
* Allows special objects (Error, Headers, Set) to be included in JSON.stringify output
|
|
4
4
|
* functions are removed
|
|
5
5
|
*/
|
|
6
|
-
export function snapshot(i) {
|
|
7
|
-
if (Array.isArray(i))
|
|
8
|
-
|
|
6
|
+
export function snapshot(i, max = 50, depth = 0) {
|
|
7
|
+
if (Array.isArray(i)) {
|
|
8
|
+
if (depth === max)
|
|
9
|
+
return [];
|
|
10
|
+
return i.map((c) => snapshot(c, max, depth + 1));
|
|
11
|
+
}
|
|
9
12
|
if (typeof i === 'function')
|
|
10
13
|
return undefined;
|
|
11
14
|
if (!isObjectLike(i))
|
|
12
15
|
return i;
|
|
16
|
+
if (depth === max)
|
|
17
|
+
return {};
|
|
13
18
|
let output = {};
|
|
14
19
|
// @ts-ignore If it has an 'entries' function, use that for looping (eg. Set, Map, Headers)
|
|
15
20
|
if (typeof i.entries === 'function') {
|
|
16
21
|
// @ts-ignore
|
|
17
22
|
for (let [k, v] of i.entries()) {
|
|
18
|
-
output[k] = snapshot(v);
|
|
23
|
+
output[k] = snapshot(v, max, depth + 1);
|
|
19
24
|
}
|
|
20
25
|
return output;
|
|
21
26
|
}
|
|
@@ -23,11 +28,11 @@ export function snapshot(i) {
|
|
|
23
28
|
// Get Enumerable, inherited properties
|
|
24
29
|
const obj = i;
|
|
25
30
|
for (let key in obj) {
|
|
26
|
-
output[key] = snapshot(obj[key]);
|
|
31
|
+
output[key] = snapshot(obj[key], max, depth + 1);
|
|
27
32
|
}
|
|
28
33
|
// Get Non-enumberable, own properties
|
|
29
|
-
Object.getOwnPropertyNames(obj).forEach(key => {
|
|
30
|
-
output[key] = snapshot(obj[key]);
|
|
34
|
+
Object.getOwnPropertyNames(obj).forEach((key) => {
|
|
35
|
+
output[key] = snapshot(obj[key], max, depth + 1);
|
|
31
36
|
});
|
|
32
37
|
return output;
|
|
33
38
|
}
|
package/package.json
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@brianbuie/node-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"license": "ISC",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/brianbuie/node-kit.git"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
|
-
"test": "tsc && node --test \"
|
|
10
|
+
"test": "tsc && node --test \"dist/**/*.test.js\" --quiet",
|
|
11
11
|
"preversion": "npm test",
|
|
12
12
|
"postversion": "git push --follow-tags"
|
|
13
13
|
},
|
|
14
14
|
"type": "module",
|
|
15
15
|
"exports": {
|
|
16
|
-
".": "
|
|
16
|
+
".": "./dist/_index.js"
|
|
17
17
|
},
|
|
18
18
|
"files": [
|
|
19
19
|
"src",
|
|
20
|
-
"
|
|
21
|
-
"
|
|
20
|
+
"dist",
|
|
21
|
+
"!dist/**/*.test.*",
|
|
22
22
|
"README.md"
|
|
23
23
|
],
|
|
24
24
|
"engines": {
|
package/src/Log.ts
CHANGED
|
@@ -38,7 +38,7 @@ export class Log {
|
|
|
38
38
|
*/
|
|
39
39
|
static #toConsole(entry: Entry, color: ChalkInstance) {
|
|
40
40
|
if (entry.message) console.log(color(`[${entry.severity}] ${entry.message}`));
|
|
41
|
-
entry.details?.forEach(detail => {
|
|
41
|
+
entry.details?.forEach((detail) => {
|
|
42
42
|
console.log(inspect(detail, { depth: 10, breakLength: 100, compact: true, colors: true }));
|
|
43
43
|
});
|
|
44
44
|
}
|
|
@@ -63,7 +63,7 @@ export class Log {
|
|
|
63
63
|
* Also snapshots special objects (eg Error, Response) to keep props in later JSON.stringify output
|
|
64
64
|
*/
|
|
65
65
|
static prepare(...input: unknown[]): { message?: string; details: unknown[] } {
|
|
66
|
-
let [first, ...rest] = input.map(snapshot);
|
|
66
|
+
let [first, ...rest] = input.map((i) => snapshot(i));
|
|
67
67
|
if (typeof first === 'string') return { message: first, details: rest };
|
|
68
68
|
// @ts-ignore
|
|
69
69
|
if (isObjectLike(first) && typeof first['message'] === 'string') {
|
|
@@ -94,7 +94,7 @@ export class Log {
|
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
static debug(...input: unknown[]) {
|
|
97
|
-
const debugging = process.argv.some(arg => arg.includes('--debug')) || process.env.DEBUG !== undefined;
|
|
97
|
+
const debugging = process.argv.some((arg) => arg.includes('--debug')) || process.env.DEBUG !== undefined;
|
|
98
98
|
if (debugging || process.env.NODE_ENV !== 'production') {
|
|
99
99
|
return this.#log({ severity: 'DEBUG', color: chalk.gray }, ...input);
|
|
100
100
|
}
|
package/src/snapshot.test.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { describe, it } from 'node:test';
|
|
2
2
|
import assert from 'node:assert';
|
|
3
3
|
import { snapshot } from './snapshot.js';
|
|
4
|
+
import { temp } from './Dir.js';
|
|
4
5
|
|
|
5
6
|
describe('snapshot', () => {
|
|
6
7
|
it('Captures Error details', () => {
|
|
@@ -32,4 +33,22 @@ describe('snapshot', () => {
|
|
|
32
33
|
const shot = snapshot(test) as Record<string, any>;
|
|
33
34
|
assert(shot.func === undefined);
|
|
34
35
|
});
|
|
36
|
+
|
|
37
|
+
it('Handles recursive references', () => {
|
|
38
|
+
type Thing = { id: number; thing?: Thing; things: Thing[] };
|
|
39
|
+
function createThing(id: number, thing?: Thing) {
|
|
40
|
+
const newThing: Thing = { id, thing, things: [] };
|
|
41
|
+
if (thing) {
|
|
42
|
+
thing.things.push(newThing);
|
|
43
|
+
}
|
|
44
|
+
return newThing;
|
|
45
|
+
}
|
|
46
|
+
const t1 = createThing(1);
|
|
47
|
+
const t2 = createThing(2, t1);
|
|
48
|
+
const result = snapshot(t1, 20) as Thing;
|
|
49
|
+
const f1 = temp.file('recursive').json(result);
|
|
50
|
+
const parsed = f1.read();
|
|
51
|
+
const f2 = temp.file('recursive2').json(parsed);
|
|
52
|
+
assert.deepEqual(f1.read(), f2.read());
|
|
53
|
+
});
|
|
35
54
|
});
|
package/src/snapshot.ts
CHANGED
|
@@ -4,17 +4,21 @@ import { isObjectLike } from 'lodash-es';
|
|
|
4
4
|
* Allows special objects (Error, Headers, Set) to be included in JSON.stringify output
|
|
5
5
|
* functions are removed
|
|
6
6
|
*/
|
|
7
|
-
export function snapshot(i: unknown): any {
|
|
8
|
-
if (Array.isArray(i))
|
|
7
|
+
export function snapshot(i: unknown, max = 50, depth = 0): any {
|
|
8
|
+
if (Array.isArray(i)) {
|
|
9
|
+
if (depth === max) return [];
|
|
10
|
+
return i.map((c) => snapshot(c, max, depth + 1));
|
|
11
|
+
}
|
|
9
12
|
if (typeof i === 'function') return undefined;
|
|
10
13
|
if (!isObjectLike(i)) return i;
|
|
11
14
|
|
|
15
|
+
if (depth === max) return {};
|
|
12
16
|
let output: Record<string, any> = {};
|
|
13
17
|
// @ts-ignore If it has an 'entries' function, use that for looping (eg. Set, Map, Headers)
|
|
14
18
|
if (typeof i.entries === 'function') {
|
|
15
19
|
// @ts-ignore
|
|
16
20
|
for (let [k, v] of i.entries()) {
|
|
17
|
-
output[k] = snapshot(v);
|
|
21
|
+
output[k] = snapshot(v, max, depth + 1);
|
|
18
22
|
}
|
|
19
23
|
return output;
|
|
20
24
|
}
|
|
@@ -24,12 +28,12 @@ export function snapshot(i: unknown): any {
|
|
|
24
28
|
// Get Enumerable, inherited properties
|
|
25
29
|
const obj: Record<string, any> = i!;
|
|
26
30
|
for (let key in obj) {
|
|
27
|
-
output[key] = snapshot(obj[key]);
|
|
31
|
+
output[key] = snapshot(obj[key], max, depth + 1);
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
// Get Non-enumberable, own properties
|
|
31
|
-
Object.getOwnPropertyNames(obj).forEach(key => {
|
|
32
|
-
output[key] = snapshot(obj[key]);
|
|
35
|
+
Object.getOwnPropertyNames(obj).forEach((key) => {
|
|
36
|
+
output[key] = snapshot(obj[key], max, depth + 1);
|
|
33
37
|
});
|
|
34
38
|
|
|
35
39
|
return output;
|
|
File without changes
|
/package/{.dist → dist}/Cache.js
RENAMED
|
File without changes
|
/package/{.dist → dist}/Dir.d.ts
RENAMED
|
File without changes
|
/package/{.dist → dist}/Dir.js
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/{.dist → dist}/File.js
RENAMED
|
File without changes
|
/package/{.dist → dist}/Jwt.d.ts
RENAMED
|
File without changes
|
/package/{.dist → dist}/Jwt.js
RENAMED
|
File without changes
|
/package/{.dist → dist}/Log.d.ts
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|