forked from cloudflare/workers-nodejs-compat-matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump-utils.mjs
More file actions
151 lines (130 loc) · 4.31 KB
/
dump-utils.mjs
File metadata and controls
151 lines (130 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
export const sortFn = (a, b) => {
if (a === "*self*") return -2;
if (a === "default") return -1;
return a < b ? -1 : 1;
};
export function visit(traversalNode, targetNode = traversalNode, depth = 0) {
// create a unique object to mark keys that errored during inspection
const INSPECTION_ERROR = {};
const props = collectObjectProps(traversalNode);
const entries = [];
for (const prop of props) {
let value;
try {
value = traversalNode[prop];
} catch (e) {
value = INSPECTION_ERROR;
}
entries.push([prop, value]);
}
entries.sort(([a], [b]) => (a === "default" ? -1 : a < b ? -1 : 1));
const visitResult = {};
for (const [key, traversalValue] of entries) {
// If targetNode doesn't exist OR
// a key doesn't exist on the target node AND it's undefined, we mark it as missing.
// For stubs, the key will not exist in the targetNode (since targetNode) but the value
// will be `function`, since it's a proxy.
let targetValue;
if (key === "*self*") {
// skip synthetic *self* nodes as they'll be set later when we recursively call `visit`
// initialize the key with a temporary value so that we don't *self* naturally sorts at the top
visitResult[key] = "<TODO>";
continue;
}
if (
targetNode == null ||
(!(key in targetNode) && typeof targetNode[key] === "undefined")
) {
targetValue = "missing";
} else {
try {
targetValue = targetNode[key];
} catch {
targetValue = INSPECTION_ERROR;
}
}
if (targetValue === INSPECTION_ERROR) {
visitResult[key] = "<INSPECTION ERROR>";
continue;
}
const isObject =
typeof traversalValue === "object" &&
traversalValue !== null &&
!Array.isArray(traversalValue);
if (isObject || key === "default") {
// don't worry drilling into exported objects beyond listing its top properties
if (depth === 3) {
visitResult[key] = "object";
} else {
const partialResult = visit(
traversalValue,
targetValue === "missing" ? {} : targetValue || {},
depth + 1
);
partialResult["*self*"] =
targetValue === null
? "null"
: targetValue === "missing"
? "missing"
: typeof targetValue;
visitResult[key] = partialResult;
}
} else {
// Detect unenv stubs
if (targetValue && targetValue.__unenv__ === true) {
visitResult[key] = "stub";
continue;
}
if (typeof targetValue === "function") {
const code = targetValue.toString();
// Detect unimplemented stubs
if (
// unenv https://github.com/unjs/unenv/blob/c6dca1dfac95bd6359e8575d4456635914823701/src/runtime/_internal/utils.ts#L30
// deno https://github.com/denoland/deno/blob/8eb1f11112c3ced0ff4a35f3487a4da507db05c2/ext/node/polyfills/_utils.ts#L25
code.includes("notImplemented(")
) {
visitResult[key] = "stub";
continue;
}
}
if (targetValue === "missing") {
visitResult[key] = "missing";
} else if (targetValue === null) {
visitResult[key] = "null";
} else if (isClass(targetValue)) {
visitResult[key] = "class";
} else {
visitResult[key] = typeof targetValue;
}
}
}
return Object.keys(visitResult)
.sort(sortFn)
.reduce((acc, key) => ({ ...acc, [key]: visitResult[key] }), {});
}
const isClass = (node) =>
typeof node === "function" && /^\s*class\s+/.test(node.toString());
export const objectSort = (obj) => {
return Object.keys(obj)
.sort(sortFn)
.reduce(
(acc, key) => ({
...acc,
[key]: typeof obj[key] === "object" ? objectSort(obj[key]) : obj[key],
}),
{}
);
};
/**
* Collects all properties of an object including inherited ones and non-enumerable ones.
* This is done by combining all property descriptors of the object and its prototypes.
*/
export function collectObjectProps(obj, props = []) {
props = [...Object.keys(Object.getOwnPropertyDescriptors(obj)), ...props];
const proto = Object.getPrototypeOf(obj);
return proto !== null &&
proto !== Function.prototype &&
proto !== Object.prototype
? collectObjectProps(proto, props)
: props.sort();
}