@deviltea/widget-inspector 0.0.0-stage → 0.0.3

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/value.mjs ADDED
@@ -0,0 +1,152 @@
1
+ //#region src/value.ts
2
+ const DEFAULT_MAX_DEPTH = 4;
3
+ const DEFAULT_MAX_ENTRIES = 50;
4
+ const DEFAULT_MAX_STRING_LENGTH = 500;
5
+ function encodeString(value, maxLength) {
6
+ const truncated = value.length > maxLength;
7
+ return {
8
+ type: "string",
9
+ value: truncated ? value.slice(0, maxLength) : value,
10
+ truncated
11
+ };
12
+ }
13
+ function opaque(kind) {
14
+ return {
15
+ type: "opaque",
16
+ kind
17
+ };
18
+ }
19
+ function isDomNode(value) {
20
+ const NodeConstructor = globalThis.Node;
21
+ if (typeof NodeConstructor !== "function") return false;
22
+ try {
23
+ return value instanceof NodeConstructor;
24
+ } catch {
25
+ return false;
26
+ }
27
+ }
28
+ function encodeInspectableValue(value, options = {}) {
29
+ const maxDepth = options.maxDepth ?? DEFAULT_MAX_DEPTH;
30
+ const maxEntries = options.maxEntries ?? DEFAULT_MAX_ENTRIES;
31
+ const maxStringLength = options.maxStringLength ?? DEFAULT_MAX_STRING_LENGTH;
32
+ const seen = /* @__PURE__ */ new WeakMap();
33
+ let nextId = 1;
34
+ function encode(current, depth) {
35
+ if (current === null) return { type: "null" };
36
+ if (current === void 0) return { type: "undefined" };
37
+ switch (typeof current) {
38
+ case "boolean": return {
39
+ type: "boolean",
40
+ value: current
41
+ };
42
+ case "string": return encodeString(current, maxStringLength);
43
+ case "number":
44
+ if (Number.isNaN(current)) return {
45
+ type: "number-special",
46
+ value: "nan"
47
+ };
48
+ if (current === Infinity) return {
49
+ type: "number-special",
50
+ value: "positive-infinity"
51
+ };
52
+ if (current === -Infinity) return {
53
+ type: "number-special",
54
+ value: "negative-infinity"
55
+ };
56
+ if (Object.is(current, -0)) return {
57
+ type: "number-special",
58
+ value: "negative-zero"
59
+ };
60
+ return {
61
+ type: "number",
62
+ value: current
63
+ };
64
+ case "bigint": return {
65
+ type: "bigint",
66
+ value: current.toString()
67
+ };
68
+ case "function": return opaque("function");
69
+ case "symbol": return opaque("symbol");
70
+ case "object": break;
71
+ default: return opaque("unknown-object");
72
+ }
73
+ if (depth >= maxDepth) return {
74
+ type: "truncated",
75
+ reason: "max-depth"
76
+ };
77
+ const objectValue = current;
78
+ if (isDomNode(objectValue)) return opaque("dom-node");
79
+ if (Array.isArray(objectValue)) {
80
+ let length;
81
+ try {
82
+ length = objectValue.length;
83
+ } catch {
84
+ return opaque("uninspectable");
85
+ }
86
+ const prior = seen.get(objectValue);
87
+ if (prior !== void 0) return {
88
+ type: "reference",
89
+ ref: prior
90
+ };
91
+ const id = nextId++;
92
+ seen.set(objectValue, id);
93
+ const limit = Math.min(length, maxEntries);
94
+ const items = [];
95
+ for (let index = 0; index < limit; index++) {
96
+ let descriptor;
97
+ try {
98
+ descriptor = Object.getOwnPropertyDescriptor(objectValue, String(index));
99
+ } catch {
100
+ items.push(opaque("uninspectable"));
101
+ continue;
102
+ }
103
+ items.push(descriptor !== void 0 && "value" in descriptor ? encode(descriptor.value, depth + 1) : opaque("uninspectable"));
104
+ }
105
+ return {
106
+ type: "array",
107
+ id,
108
+ items,
109
+ truncated: length > limit
110
+ };
111
+ }
112
+ let prototype;
113
+ let descriptors;
114
+ try {
115
+ prototype = Object.getPrototypeOf(objectValue);
116
+ if (prototype !== Object.prototype && prototype !== null) return opaque("class-instance");
117
+ descriptors = Object.getOwnPropertyDescriptors(objectValue);
118
+ } catch {
119
+ return opaque("uninspectable");
120
+ }
121
+ const prior = seen.get(objectValue);
122
+ if (prior !== void 0) return {
123
+ type: "reference",
124
+ ref: prior
125
+ };
126
+ const id = nextId++;
127
+ seen.set(objectValue, id);
128
+ const keys = Object.keys(descriptors).sort();
129
+ const entries = [];
130
+ for (const key of keys.slice(0, maxEntries)) {
131
+ const descriptor = descriptors[key];
132
+ if (descriptor === void 0) continue;
133
+ entries.push({
134
+ key,
135
+ value: "value" in descriptor ? encode(descriptor.value, depth + 1) : opaque("uninspectable")
136
+ });
137
+ }
138
+ return {
139
+ type: "object",
140
+ id,
141
+ entries,
142
+ truncated: keys.length > entries.length
143
+ };
144
+ }
145
+ try {
146
+ return encode(value, 0);
147
+ } catch {
148
+ return opaque("uninspectable");
149
+ }
150
+ }
151
+ //#endregion
152
+ export { encodeInspectableValue };
package/package.json CHANGED
@@ -1,6 +1,85 @@
1
1
  {
2
2
  "name": "@deviltea/widget-inspector",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "type": "module",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "version": "0.0.3",
8
+ "description": "Read-only Widget inspector protocol, client, DOM agent, and transports for @deviltea/widget-core Runtimes.",
9
+ "author": "DevilTea <ch19980814@gmail.com>",
10
+ "license": "MIT",
11
+ "homepage": "https://github.com/DevilTea/widget/tree/main/packages/inspector#readme",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/DevilTea/widget.git",
15
+ "directory": "packages/inspector"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/DevilTea/widget/issues"
19
+ },
20
+ "keywords": [
21
+ "widget",
22
+ "inspector",
23
+ "devtools",
24
+ "protocol"
25
+ ],
26
+ "exports": {
27
+ "./agent": {
28
+ "types": "./dist/agent.d.mts",
29
+ "import": "./dist/agent.mjs"
30
+ },
31
+ "./anchor": {
32
+ "types": "./dist/anchor.d.mts",
33
+ "import": "./dist/anchor.mjs"
34
+ },
35
+ "./channel": {
36
+ "types": "./dist/channel.d.mts",
37
+ "import": "./dist/channel.mjs"
38
+ },
39
+ "./client": {
40
+ "types": "./dist/client.d.mts",
41
+ "import": "./dist/client.mjs"
42
+ },
43
+ "./frame-bootstrap": {
44
+ "types": "./dist/frame-bootstrap.d.mts",
45
+ "import": "./dist/frame-bootstrap.mjs"
46
+ },
47
+ "./protocol": {
48
+ "types": "./dist/protocol.d.mts",
49
+ "import": "./dist/protocol.mjs"
50
+ },
51
+ "./transport": {
52
+ "types": "./dist/transport.d.mts",
53
+ "import": "./dist/transport.mjs"
54
+ },
55
+ "./value": {
56
+ "types": "./dist/value.d.mts",
57
+ "import": "./dist/value.mjs"
58
+ }
59
+ },
60
+ "files": [
61
+ "dist"
62
+ ],
63
+ "engines": {
64
+ "node": "^22.14.0 || ^24.0.0"
65
+ },
66
+ "peerDependencies": {
67
+ "@deviltea/widget-core": ">=0.0.3 <0.1.0"
68
+ },
69
+ "devDependencies": {
70
+ "@deviltea/tsconfig": "1.0.0",
71
+ "happy-dom": "^20.11.2",
72
+ "tsdown": "^0.22.3",
73
+ "vitest": "^4.1.9",
74
+ "@deviltea/widget-core": "0.0.3"
75
+ },
76
+ "scripts": {
77
+ "build": "tsdown",
78
+ "build:pack": "pnpm build && pnpm pack",
79
+ "dev": "tsdown --watch",
80
+ "test": "vitest run --config vitest.config.ts",
81
+ "typecheck": "pnpm typecheck:package && pnpm typecheck:test",
82
+ "typecheck:package": "tsc --project ./tsconfig.package.json --noEmit",
83
+ "typecheck:test": "tsc --project ./tsconfig.tests.json --noEmit"
84
+ }
6
85
  }