@botiverse/porthole 0.7.0 → 0.9.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 +9 -2
- package/THIRD_PARTY_NOTICES.md +421 -0
- package/dist/annotate/types.d.ts +40 -4
- package/dist/annotate/types.js +6 -1
- package/dist/annotate/validate.d.ts +10 -0
- package/dist/annotate/validate.js +24 -1
- package/dist/bridge/bounded-text.d.ts +34 -0
- package/dist/bridge/bounded-text.js +188 -0
- package/dist/bridge/bridge.d.ts +25 -5
- package/dist/bridge/bridge.js +311 -73
- package/dist/bridge/json-data.d.ts +28 -0
- package/dist/bridge/json-data.js +86 -0
- package/dist/bridge/navigation.d.ts +5 -0
- package/dist/bridge/navigation.js +7 -0
- package/dist/bridge/refusal.js +1 -0
- package/dist/bridge/types.d.ts +32 -1
- package/dist/check/page-source.js +1 -1
- package/dist/check/page.js +2 -0
- package/dist/compile/compile.d.ts +6 -0
- package/dist/compile/compile.js +3 -1
- package/dist/compile/policy.d.ts +38 -2
- package/dist/compile/policy.js +122 -7
- package/dist/config/capabilities.d.ts +23 -4
- package/dist/config/capabilities.js +12 -2
- package/dist/config/schema.d.ts +11 -0
- package/dist/config/validate.d.ts +8 -2
- package/dist/config/validate.js +28 -6
- package/dist/config/warnings.d.ts +20 -0
- package/dist/config/warnings.js +25 -0
- package/dist/conformance/behavior.js +46 -1
- package/dist/conformance/cookies.d.ts +15 -0
- package/dist/conformance/cookies.js +80 -0
- package/dist/conformance/harness/page.d.ts +1 -0
- package/dist/conformance/harness/page.js +9 -2
- package/dist/conformance/harness-source.js +1 -1
- package/dist/conformance/host-kit.js +5 -1
- package/dist/conformance/index.d.ts +10 -0
- package/dist/conformance/limits.js +38 -1
- package/dist/conformance/runtime.js +10 -0
- package/dist/conformance/served.d.ts +2 -0
- package/dist/conformance/served.js +737 -4
- package/dist/files/paths.js +4 -0
- package/dist/guide/gates.d.ts +7 -1
- package/dist/guide/gates.js +12 -0
- package/dist/guide/guide.js +2 -2
- package/dist/guide/runtime.js +11 -3
- package/dist/host/create-host.js +81 -39
- package/dist/host/pages-of-set.d.ts +43 -0
- package/dist/host/pages-of-set.js +196 -0
- package/dist/host/types.d.ts +8 -1
- package/dist/index.d.ts +5 -4
- package/dist/index.js +3 -2
- package/dist/mcp/adapter.js +11 -15
- package/dist/node/index.d.ts +36 -4
- package/dist/node/index.js +402 -101
- package/dist/platform/types.d.ts +9 -0
- package/dist/platform/web.js +8 -0
- package/dist/presets/presets.d.ts +16 -8
- package/dist/presets/presets.js +39 -8
- package/dist/protocol/live-data.d.ts +6 -2
- package/dist/protocol/live-data.js +20 -1
- package/dist/protocol/mcp-apps.d.ts +13 -2
- package/dist/protocol/mcp-apps.js +10 -2
- package/dist/protocol/message-text.d.ts +8 -0
- package/dist/protocol/message-text.js +29 -0
- package/dist/protocol/runtime-calls.js +3 -0
- package/dist/protocol/shell-adapter.js +16 -22
- package/dist/react/context-notice.d.ts +32 -0
- package/dist/react/context-notice.js +28 -0
- package/dist/react/dialog.js +62 -18
- package/dist/react/frame.d.ts +18 -3
- package/dist/react/frame.js +79 -2
- package/dist/react/index.d.ts +2 -1
- package/dist/react/index.js +2 -1
- package/dist/runtime/contract.d.ts +9 -1
- package/dist/runtime/contract.js +31 -2
- package/dist/runtime/db.d.ts +41 -6
- package/dist/runtime/db.js +120 -16
- package/dist/runtime/declaration.d.ts +15 -1
- package/dist/runtime/declaration.js +184 -87
- package/dist/runtime/requests.js +27 -11
- package/dist/runtime/types.generated.js +1 -1
- package/dist/server/files.d.ts +5 -0
- package/dist/server/files.js +22 -7
- package/dist/server/host.d.ts +18 -1
- package/dist/server/host.js +132 -2
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/peer.d.ts +4 -0
- package/dist/server/peer.js +19 -0
- package/dist/server/proxy.d.ts +6 -0
- package/dist/server/proxy.js +12 -3
- package/dist/server/server.js +113 -26
- package/dist/server/types.d.ts +6 -3
- package/dist/shell/annotate.d.ts +7 -2
- package/dist/shell/annotate.js +233 -15
- package/dist/shell/document.d.ts +8 -0
- package/dist/shell/document.js +3 -0
- package/dist/shell/runtime.js +361 -14
- package/docs/ADOPTING.md +119 -14
- package/docs/ARCHITECTURE.md +15 -4
- package/docs/DECISIONS.md +1610 -9
- package/docs/DESIGN.md +114 -30
- package/docs/THREAT_MODEL.md +652 -0
- package/package.json +14 -3
- package/src/annotate/types.ts +37 -5
- package/src/annotate/validate.ts +30 -1
- package/src/bridge/bounded-text.ts +240 -0
- package/src/bridge/bridge.ts +373 -84
- package/src/bridge/json-data.ts +95 -0
- package/src/bridge/navigation.ts +10 -0
- package/src/bridge/refusal.ts +1 -0
- package/src/bridge/types.ts +30 -1
- package/src/check/page.ts +2 -0
- package/src/compile/compile.ts +9 -1
- package/src/compile/policy.ts +130 -7
- package/src/config/capabilities.ts +22 -3
- package/src/config/schema.ts +11 -0
- package/src/config/validate.ts +43 -5
- package/src/config/warnings.ts +42 -0
- package/src/conformance/behavior.ts +54 -1
- package/src/conformance/cookies.ts +93 -0
- package/src/conformance/harness/page.ts +11 -2
- package/src/conformance/host-kit.ts +10 -1
- package/src/conformance/index.ts +10 -0
- package/src/conformance/limits.ts +39 -1
- package/src/conformance/runtime.ts +21 -0
- package/src/conformance/served.ts +888 -4
- package/src/files/paths.ts +4 -0
- package/src/guide/gates.ts +13 -1
- package/src/guide/guide.ts +3 -3
- package/src/guide/runtime.ts +12 -3
- package/src/host/create-host.ts +88 -38
- package/src/host/pages-of-set.ts +226 -0
- package/src/host/types.ts +15 -1
- package/src/index.ts +5 -0
- package/src/mcp/adapter.ts +16 -20
- package/src/node/index.ts +507 -116
- package/src/platform/types.ts +6 -0
- package/src/platform/web.ts +10 -0
- package/src/presets/presets.ts +60 -17
- package/src/protocol/live-data.ts +31 -3
- package/src/protocol/mcp-apps.ts +11 -2
- package/src/protocol/message-text.ts +36 -0
- package/src/protocol/runtime-calls.ts +3 -0
- package/src/protocol/shell-adapter.ts +27 -23
- package/src/react/context-notice.tsx +95 -0
- package/src/react/dialog.tsx +77 -17
- package/src/react/frame.tsx +107 -2
- package/src/react/index.ts +8 -1
- package/src/runtime/contract.ts +36 -2
- package/src/runtime/db.ts +140 -20
- package/src/runtime/declaration.ts +227 -89
- package/src/runtime/requests.ts +27 -11
- package/src/runtime/types.generated.ts +1 -1
- package/src/server/files.ts +29 -11
- package/src/server/host.ts +158 -3
- package/src/server/index.ts +1 -0
- package/src/server/peer.ts +21 -0
- package/src/server/proxy.ts +13 -3
- package/src/server/server.ts +139 -36
- package/src/server/types.ts +6 -3
- package/src/shell/annotate.ts +233 -15
- package/src/shell/document.ts +8 -0
- package/src/shell/runtime.ts +361 -14
- package/types/runtime/v1/context.d.ts +13 -2
package/package.json
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@botiverse/porthole",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "A host-side sandbox for agent-made HTML: isolation, egress hardening, a themed shell, gated host functions, and the agent manual, from one config.",
|
|
5
|
+
"homepage": "https://github.com/botiverse/porthole#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/botiverse/porthole/issues"
|
|
8
|
+
},
|
|
5
9
|
"license": "Apache-2.0",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/botiverse/porthole.git",
|
|
13
|
+
"directory": "packages/porthole"
|
|
14
|
+
},
|
|
6
15
|
"bin": {
|
|
7
16
|
"porthole": "./dist/cli.js"
|
|
8
17
|
},
|
|
@@ -12,7 +21,8 @@
|
|
|
12
21
|
"types/runtime/**/*.d.ts",
|
|
13
22
|
"docs",
|
|
14
23
|
"examples",
|
|
15
|
-
"README.md"
|
|
24
|
+
"README.md",
|
|
25
|
+
"THIRD_PARTY_NOTICES.md"
|
|
16
26
|
],
|
|
17
27
|
"type": "module",
|
|
18
28
|
"sideEffects": false,
|
|
@@ -70,7 +80,8 @@
|
|
|
70
80
|
"react": "19.3.0",
|
|
71
81
|
"react-dom": "19.3.0",
|
|
72
82
|
"rolldown": "1.2.11",
|
|
73
|
-
"typescript": "7.0.2"
|
|
83
|
+
"typescript": "7.0.2",
|
|
84
|
+
"yaml": "2.9.1"
|
|
74
85
|
},
|
|
75
86
|
"peerDependencies": {
|
|
76
87
|
"react": ">=19"
|
package/src/annotate/types.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Annotations (P053): a person points at part of a display (an element, or text they select) to
|
|
3
|
-
* comment on it, and the host marks the anchors already made
|
|
3
|
+
* comment on it, and the host marks the anchors already made, each with a clickable marker and a
|
|
4
|
+
* number where it gives a count (P104). The host drives the shell with
|
|
4
5
|
* Porthole's own messages (`porthole/annotate.*`); the shell answers with anchors and where they
|
|
5
6
|
* are. An anchor is a set of W3C Web Annotation selectors, so it can be found again after the
|
|
6
7
|
* content changes; finding it is best-effort, and says so (`orphaned`).
|
|
@@ -46,7 +47,11 @@ export interface AnnotationAnchor {
|
|
|
46
47
|
readonly label: string;
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
/**
|
|
50
|
+
/**
|
|
51
|
+
* A box in the frame's viewport, CSS px: add the frame element's position to place it on the page.
|
|
52
|
+
* The page's word: the host clamps each it reports to the frame's viewport (P104), so one wholly
|
|
53
|
+
* outside is a box of no size on the nearest edge. A positioning hint only.
|
|
54
|
+
*/
|
|
50
55
|
export interface AnnotationRect {
|
|
51
56
|
readonly x: number;
|
|
52
57
|
readonly y: number;
|
|
@@ -62,10 +67,21 @@ export interface AnnotateMode {
|
|
|
62
67
|
readonly text?: boolean;
|
|
63
68
|
}
|
|
64
69
|
|
|
65
|
-
/**
|
|
70
|
+
/**
|
|
71
|
+
* An anchor the host marks in the frame, under its own id. Everything here is sent into the
|
|
72
|
+
* frame, so the content can read it: give ids that say nothing (never a person's name or the
|
|
73
|
+
* comment's words).
|
|
74
|
+
*/
|
|
66
75
|
export interface AnnotationMark {
|
|
67
76
|
readonly id: string;
|
|
68
77
|
readonly anchor: AnnotationAnchor;
|
|
78
|
+
/**
|
|
79
|
+
* A marker (P104): with a count, the shell also draws a small bubble with this number (0 to
|
|
80
|
+
* `ANNOTATION_LIMITS.count`; above 99 it reads "99+", 0 draws the bubble alone) at the end of a
|
|
81
|
+
* text anchor and the top right of an element. The person can click it: the frame reports
|
|
82
|
+
* `activated`. The content can read the number; give one only where the page may know it.
|
|
83
|
+
*/
|
|
84
|
+
readonly count?: number;
|
|
69
85
|
}
|
|
70
86
|
|
|
71
87
|
/** Whether a mark's anchor was found in the content as it is now. */
|
|
@@ -90,8 +106,20 @@ export type AnnotationReport =
|
|
|
90
106
|
| { readonly type: "unselected" }
|
|
91
107
|
/** The person pressed Escape while picking an element. */
|
|
92
108
|
| { readonly type: "cancelled" }
|
|
93
|
-
/**
|
|
94
|
-
|
|
109
|
+
/**
|
|
110
|
+
* Where the marked anchors are now: every mark after `show`, the focused one after `focus`,
|
|
111
|
+
* every mark when the content changed one's state, and, while any mark has a `count`, once a
|
|
112
|
+
* scroll, a resize or a change of the content has settled, only the marks that moved since the
|
|
113
|
+
* host was last told, at most once a second (P104).
|
|
114
|
+
*/
|
|
115
|
+
| { readonly type: "anchored"; readonly results: readonly AnchorResult[] }
|
|
116
|
+
/**
|
|
117
|
+
* The person clicked the marker of a mark the host shows with a `count` (P104); `rect` is the
|
|
118
|
+
* marker's box, to open the host's own UI beside it. The content can fake it for any mark the
|
|
119
|
+
* host shows with a count, and can tell a real one happened: open the host's own view of that
|
|
120
|
+
* mark, never act on it.
|
|
121
|
+
*/
|
|
122
|
+
| { readonly type: "activated"; readonly id: string; readonly rect: AnnotationRect };
|
|
95
123
|
|
|
96
124
|
/** Porthole's own messages for annotations (own-prefixed, P002). */
|
|
97
125
|
export const ANNOTATE_METHODS = {
|
|
@@ -110,6 +138,8 @@ export const ANNOTATE_METHODS = {
|
|
|
110
138
|
unselected: "porthole/annotate.unselected",
|
|
111
139
|
cancelled: "porthole/annotate.cancelled",
|
|
112
140
|
anchored: "porthole/annotate.anchored",
|
|
141
|
+
/** Frame → host: a mark's marker was clicked (P104). */
|
|
142
|
+
activated: "porthole/annotate.activated",
|
|
113
143
|
} as const;
|
|
114
144
|
|
|
115
145
|
/** The most of each part of an anchor or report the host takes. */
|
|
@@ -123,4 +153,6 @@ export const ANNOTATION_LIMITS = {
|
|
|
123
153
|
id: 128,
|
|
124
154
|
position: 50_000_000,
|
|
125
155
|
coordinate: 10_000_000,
|
|
156
|
+
/** The largest `count` a mark's marker takes (P104). */
|
|
157
|
+
count: 999,
|
|
126
158
|
} as const;
|
package/src/annotate/validate.ts
CHANGED
|
@@ -106,6 +106,26 @@ export function parseRect(value: unknown): AnnotationRect | null {
|
|
|
106
106
|
return { x, y, width, height };
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
+
/**
|
|
110
|
+
* A box the frame reported, clamped to the frame's viewport (`width` by `height`, CSS px, P104):
|
|
111
|
+
* each side is held within it, so what overlaps the viewport keeps the part inside, and what lies
|
|
112
|
+
* wholly outside becomes a box of no size on the nearest edge. Where the viewport isn't known, a
|
|
113
|
+
* box of no size at the frame's corner. The page still chooses any point inside its own frame.
|
|
114
|
+
*/
|
|
115
|
+
export function clampRect(
|
|
116
|
+
rect: AnnotationRect,
|
|
117
|
+
viewport: { readonly width: number; readonly height: number } | undefined,
|
|
118
|
+
): AnnotationRect {
|
|
119
|
+
const width = viewport !== undefined && Number.isFinite(viewport.width) ? viewport.width : 0;
|
|
120
|
+
const height = viewport !== undefined && Number.isFinite(viewport.height) ? viewport.height : 0;
|
|
121
|
+
const within = (value: number, most: number) => Math.min(Math.max(value, 0), Math.max(0, most));
|
|
122
|
+
const left = within(rect.x, width);
|
|
123
|
+
const top = within(rect.y, height);
|
|
124
|
+
const right = within(rect.x + rect.width, width);
|
|
125
|
+
const bottom = within(rect.y + rect.height, height);
|
|
126
|
+
return { x: left, y: top, width: right - left, height: bottom - top };
|
|
127
|
+
}
|
|
128
|
+
|
|
109
129
|
const markId = (value: unknown): value is string => text(value, LIMITS.id, 1);
|
|
110
130
|
|
|
111
131
|
function results(value: unknown): AnchorResult[] | null {
|
|
@@ -152,6 +172,10 @@ export function parseReport(value: unknown): AnnotationReport | null {
|
|
|
152
172
|
const checked = results(value.results);
|
|
153
173
|
return checked === null ? null : { type: "anchored", results: checked };
|
|
154
174
|
}
|
|
175
|
+
case "activated": {
|
|
176
|
+
const rect = parseRect(value.rect);
|
|
177
|
+
return !markId(value.id) || rect === null ? null : { type: "activated", id: value.id, rect };
|
|
178
|
+
}
|
|
155
179
|
default:
|
|
156
180
|
return null;
|
|
157
181
|
}
|
|
@@ -180,7 +204,12 @@ export function checkMarks(marks: readonly AnnotationMark[]): AnnotationMark[] {
|
|
|
180
204
|
`annotations.show: mark ${index} needs a unique id (1 to ${LIMITS.id} characters) and a valid anchor`,
|
|
181
205
|
);
|
|
182
206
|
}
|
|
207
|
+
if (mark.count !== undefined && !count(mark.count, LIMITS.count)) {
|
|
208
|
+
throw new TypeError(
|
|
209
|
+
`annotations.show: mark ${index}'s count is a whole number from 0 to ${LIMITS.count}`,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
183
212
|
seen.add(mark.id);
|
|
184
|
-
return { id: mark.id, anchor };
|
|
213
|
+
return { id: mark.id, anchor, ...(mark.count === undefined ? {} : { count: mark.count }) };
|
|
185
214
|
});
|
|
186
215
|
}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { cutText } from "./navigation.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What content said, as text a host can show (S05, P095), at a cost bounded whatever the frame
|
|
5
|
+
* sent. A frame's messages come structured-cloned, which keeps cycles and shared references (13
|
|
6
|
+
* objects each holding the next twice are 2^13 nodes written as a tree) and carries BigInt, Maps
|
|
7
|
+
* and nesting of any depth: `JSON.stringify` throws on the first and the second, and is
|
|
8
|
+
* exponential in the third.
|
|
9
|
+
*
|
|
10
|
+
* A string is as it is, cut. Anything else is written the way JSON writes it, with each object
|
|
11
|
+
* read once (`"[Circular]"` where it is inside itself, `"[Repeated]"` where it was written
|
|
12
|
+
* already), at most `nodes` values (`…` where it stops) and `depth` levels (`"[Object]"`,
|
|
13
|
+
* `"[Array]"` below), a boxed primitive as its value (as JSON does), a BigInt as `"5n"`
|
|
14
|
+
* (`"[BigInt]"` past 2,000 digits), a Date as its time, an Error as its name and message, any other
|
|
15
|
+
* object as its kind (`"[Map]"`), and anything that throws when read as `"[unreadable]"`; any of
|
|
16
|
+
* these but a boxed primitive and a Date means the text isn't `whole`. Every cut is `cutText`'s:
|
|
17
|
+
* no part of a sealed address's access is left at one. Never throws.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export interface TextLimits {
|
|
21
|
+
/** The longest the text is. */
|
|
22
|
+
readonly chars: number;
|
|
23
|
+
/** The most values read. */
|
|
24
|
+
readonly nodes: number;
|
|
25
|
+
/** The deepest an object or array is read. */
|
|
26
|
+
readonly depth: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** An error report's: read past what the bridge keeps (2,000 characters, after the scrub). */
|
|
30
|
+
export const REPORT_TEXT_LIMITS: TextLimits = { chars: 8_000, nodes: 1_000, depth: 16 };
|
|
31
|
+
|
|
32
|
+
/** The text, and whether it is all of the value (nothing cut, stopped or left below the depth). */
|
|
33
|
+
export interface BoundedText {
|
|
34
|
+
readonly text: string;
|
|
35
|
+
readonly whole: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** A BigInt past this has more digits than a report shows; its decimal text is superlinear. */
|
|
39
|
+
const BIGGEST = 10n ** 2_000n;
|
|
40
|
+
|
|
41
|
+
/** A boxed primitive's value, read by its own prototype's `valueOf` (which throws for a fake). */
|
|
42
|
+
function unboxed(kind: string, boxed: object): unknown {
|
|
43
|
+
switch (kind) {
|
|
44
|
+
case "String":
|
|
45
|
+
return String.prototype.valueOf.call(boxed);
|
|
46
|
+
case "Number":
|
|
47
|
+
return Number.prototype.valueOf.call(boxed);
|
|
48
|
+
case "Boolean":
|
|
49
|
+
return Boolean.prototype.valueOf.call(boxed);
|
|
50
|
+
default:
|
|
51
|
+
return BigInt.prototype.valueOf.call(boxed);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A field whose read threw (a getter, a proxy: what structured clone never delivers). */
|
|
56
|
+
const UNREADABLE: unique symbol = Symbol("unreadable");
|
|
57
|
+
|
|
58
|
+
/** `value` as bounded text (above); with `indent`, as `JSON.stringify(value, null, indent)` lays it out. */
|
|
59
|
+
export function boundedText(
|
|
60
|
+
value: unknown,
|
|
61
|
+
limits: TextLimits = REPORT_TEXT_LIMITS,
|
|
62
|
+
indent = 0,
|
|
63
|
+
): string {
|
|
64
|
+
return formatBounded(value, limits, indent).text;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function formatBounded(value: unknown, limits: TextLimits, indent = 0): BoundedText {
|
|
68
|
+
if (typeof value === "string") {
|
|
69
|
+
const text = cutText(value, limits.chars);
|
|
70
|
+
return { text, whole: text === value };
|
|
71
|
+
}
|
|
72
|
+
let out = "";
|
|
73
|
+
let nodes = limits.nodes;
|
|
74
|
+
let stopped = false;
|
|
75
|
+
let whole = true;
|
|
76
|
+
const written = new WeakSet<object>();
|
|
77
|
+
const inside = new WeakSet<object>();
|
|
78
|
+
const colon = indent > 0 ? ": " : ":";
|
|
79
|
+
const quoted = (text: string) => {
|
|
80
|
+
const room = Math.max(0, limits.chars - out.length);
|
|
81
|
+
if (text.length > room) {
|
|
82
|
+
whole = false;
|
|
83
|
+
}
|
|
84
|
+
return JSON.stringify(cutText(text, room));
|
|
85
|
+
};
|
|
86
|
+
/** Where a nested value starts, at `depth`: a new line, indented, when laid out. */
|
|
87
|
+
const line = (depth: number) => (indent > 0 ? `\n${" ".repeat(indent * depth)}` : "");
|
|
88
|
+
|
|
89
|
+
/** Whether there is room for another value; if not, `…` once (after `separator`), then none. */
|
|
90
|
+
const room = (separator: string): boolean => {
|
|
91
|
+
nodes -= 1;
|
|
92
|
+
if (!stopped && (nodes < 0 || out.length >= limits.chars)) {
|
|
93
|
+
stopped = true;
|
|
94
|
+
whole = false;
|
|
95
|
+
out += `${separator}…`;
|
|
96
|
+
}
|
|
97
|
+
return !stopped;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/** What JSON leaves out of an object (and writes as null in an array). */
|
|
101
|
+
const unwritten = (each: unknown): boolean =>
|
|
102
|
+
each === undefined || typeof each === "function" || typeof each === "symbol";
|
|
103
|
+
|
|
104
|
+
function write(each: unknown, depth: number): void {
|
|
105
|
+
switch (typeof each) {
|
|
106
|
+
case "string":
|
|
107
|
+
out += quoted(each);
|
|
108
|
+
return;
|
|
109
|
+
case "number":
|
|
110
|
+
out += Number.isFinite(each) ? String(each) : "null";
|
|
111
|
+
return;
|
|
112
|
+
case "boolean":
|
|
113
|
+
out += each ? "true" : "false";
|
|
114
|
+
return;
|
|
115
|
+
case "bigint":
|
|
116
|
+
// No JSON for it: its digits, or past 2,000 of them its kind, and either way not whole.
|
|
117
|
+
whole = false;
|
|
118
|
+
out += each < BIGGEST && each > -BIGGEST ? `"${String(each)}n"` : '"[BigInt]"';
|
|
119
|
+
return;
|
|
120
|
+
case "object":
|
|
121
|
+
break;
|
|
122
|
+
default:
|
|
123
|
+
out += "null";
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (each === null) {
|
|
127
|
+
out += "null";
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (inside.has(each)) {
|
|
131
|
+
whole = false;
|
|
132
|
+
out += '"[Circular]"';
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (written.has(each)) {
|
|
136
|
+
whole = false;
|
|
137
|
+
out += '"[Repeated]"';
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
written.add(each);
|
|
141
|
+
if (Array.isArray(each)) {
|
|
142
|
+
if (depth >= limits.depth) {
|
|
143
|
+
whole = false;
|
|
144
|
+
out += '"[Array]"';
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const list = each as readonly unknown[];
|
|
148
|
+
inside.add(list);
|
|
149
|
+
out += "[";
|
|
150
|
+
const inner = line(depth + 1);
|
|
151
|
+
for (
|
|
152
|
+
let index = 0;
|
|
153
|
+
index < list.length && room(index === 0 ? inner : `,${inner}`);
|
|
154
|
+
index += 1
|
|
155
|
+
) {
|
|
156
|
+
out += index === 0 ? inner : `,${inner}`;
|
|
157
|
+
guarded(list[index], depth + 1);
|
|
158
|
+
}
|
|
159
|
+
out += list.length === 0 ? "]" : `${line(depth)}]`;
|
|
160
|
+
inside.delete(list);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const kind = Object.prototype.toString.call(each).slice(8, -1);
|
|
164
|
+
const proto: unknown = Object.getPrototypeOf(each);
|
|
165
|
+
// A boxed primitive is its value, as JSON writes it (`new String("x")` is "x").
|
|
166
|
+
if (kind === "String" || kind === "Number" || kind === "Boolean" || kind === "BigInt") {
|
|
167
|
+
write(unboxed(kind, each), depth);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (kind === "Date") {
|
|
171
|
+
const time = Date.prototype.getTime.call(each);
|
|
172
|
+
out += quoted(Number.isFinite(time) ? new Date(time).toISOString() : "Invalid Date");
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (kind === "Error") {
|
|
176
|
+
whole = false;
|
|
177
|
+
const { name, message } = each as { name?: unknown; message?: unknown };
|
|
178
|
+
out += quoted(
|
|
179
|
+
`${typeof name === "string" ? cutText(name, 100) : "Error"}: ${typeof message === "string" ? cutText(message, limits.chars) : ""}`,
|
|
180
|
+
);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (proto !== Object.prototype && proto !== null) {
|
|
184
|
+
// A Map, a typed array, a Blob: its kind only, so the text is no longer all of it.
|
|
185
|
+
whole = false;
|
|
186
|
+
out += quoted(`[${kind}]`);
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (depth >= limits.depth) {
|
|
190
|
+
whole = false;
|
|
191
|
+
out += '"[Object]"';
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
inside.add(each);
|
|
195
|
+
out += "{";
|
|
196
|
+
const inner = line(depth + 1);
|
|
197
|
+
let first = true;
|
|
198
|
+
for (const key of Object.keys(each)) {
|
|
199
|
+
if (!room(first ? inner : `,${inner}`)) {
|
|
200
|
+
first = false;
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
let field: unknown;
|
|
204
|
+
try {
|
|
205
|
+
field = (each as Record<string, unknown>)[key];
|
|
206
|
+
} catch {
|
|
207
|
+
field = UNREADABLE;
|
|
208
|
+
}
|
|
209
|
+
if (field !== UNREADABLE && unwritten(field)) {
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
out += `${first ? inner : `,${inner}`}${quoted(key)}${colon}`;
|
|
213
|
+
first = false;
|
|
214
|
+
guarded(field, depth + 1);
|
|
215
|
+
}
|
|
216
|
+
out += first ? "}" : `${line(depth)}}`;
|
|
217
|
+
inside.delete(each);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** One value, or `"[unreadable]"` where reading it throws (a getter, a proxy: never cloned). */
|
|
221
|
+
function guarded(each: unknown, depth: number): void {
|
|
222
|
+
if (each === UNREADABLE) {
|
|
223
|
+
out += '"[unreadable]"';
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
const before = out.length;
|
|
227
|
+
try {
|
|
228
|
+
write(each, depth);
|
|
229
|
+
} catch {
|
|
230
|
+
whole = false;
|
|
231
|
+
out = `${out.slice(0, before)}"[unreadable]"`;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
nodes -= 1;
|
|
236
|
+
guarded(value, 0);
|
|
237
|
+
// Cut past the length, it still ends in `…`.
|
|
238
|
+
const text = out.length <= limits.chars ? out : `${cutText(out, limits.chars - 1)}…`;
|
|
239
|
+
return { text, whole: whole && text === out };
|
|
240
|
+
}
|