Skip to content

Commit fd5e238

Browse files
committed
feat(data-inspector): auto-expose globalThis on the --import path
The zero-code `--import` agent has nowhere to call registerDataSource, so it now auto-registers a `globalThis` source (createGlobalThisDataSource) — assigning to the global object is enough to inspect anything. Opt out with DEVFRAME_DATA_INSPECTOR_GLOBAL=0. Docs updated with the workflow.
1 parent be553d5 commit fd5e238

5 files changed

Lines changed: 40 additions & 0 deletions

File tree

docs/plugins/data-inspector.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ Or with zero code changes:
129129
DEVFRAME_DATA_INSPECTOR=1 node --import @devframes/plugin-data-inspector/inject server.js
130130
```
131131

132+
On this zero-code path there is nowhere to call `registerDataSource`, so the agent auto-registers a **`globalThis`** source. Assign anything you want to inspect onto the global object and query it live:
133+
134+
```ts
135+
// somewhere in the running process
136+
globalThis.store = store
137+
globalThis.cache = cache
138+
```
139+
140+
Then query `store`, `cache`, or `keys($)` to see what's there. The source reads `globalThis` at query time, so assignments made after the agent started show up on the next run. Opt out with `DEVFRAME_DATA_INSPECTOR_GLOBAL=0`.
141+
132142
The agent binds `127.0.0.1`, requires devframe's trust handshake with a per-run pre-shared token, and advertises its endpoint in `node_modules/.data-inspector/agent.json``devframe-data-inspector attach` consumes it automatically (or pass `ws://…` and `--token` explicitly). Queries execute inside the target process, where the live objects are. Treat the endpoint like a debugger port.
133143

134144
## RPC surface

plugins/data-inspector/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ or with zero code changes:
6262
DEVFRAME_DATA_INSPECTOR=1 node --import @devframes/plugin-data-inspector/inject server.js
6363
```
6464

65+
On the zero-code path there's nowhere to call `registerDataSource`, so the agent auto-registers a **`globalThis`** source: assign what you want to inspect onto the global object (`globalThis.store = store`) and query it live. Opt out with `DEVFRAME_DATA_INSPECTOR_GLOBAL=0`.
66+
6567
The agent binds `127.0.0.1`, requires devframe's trust handshake with a per-run token by default, and advertises its endpoint in `node_modules/.data-inspector/agent.json`, which `devframe-data-inspector attach` picks up automatically.
6668

6769
> [!WARNING]

plugins/data-inspector/src/inject/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
* DEVFRAME_DATA_INSPECTOR=1 node --import @devframes/plugin-data-inspector/inject server.js
2424
* ```
2525
*
26+
* On that zero-code path there's nowhere to call `registerDataSource`, so the
27+
* agent auto-registers a **`globalThis`** source: assign anything you want to
28+
* inspect onto the global object (`globalThis.store = store`) and query it
29+
* live. Opt out with `DEVFRAME_DATA_INSPECTOR_GLOBAL=0`.
30+
*
2631
* It binds `127.0.0.1` and requires devframe's trust handshake by
2732
* default: a random pre-shared token is minted per run, printed to stderr,
2833
* and written (with the endpoint) to the discovery file
@@ -91,6 +96,24 @@ export interface DataInspectorAgent {
9196
close: () => Promise<void>
9297
}
9398

99+
/**
100+
* A data source exposing the inspected process's global object. On the
101+
* zero-code `--import` path there is nowhere to call `registerDataSource`, so
102+
* assigning to `globalThis` is how you surface things to inspect
103+
* (`globalThis.store = store`); this source makes them queryable live. The
104+
* factory reads `globalThis` at query time, so late assignments show up on the
105+
* next run.
106+
*/
107+
export function createGlobalThisDataSource(): DataSourceEntry {
108+
return {
109+
id: 'globalThis',
110+
title: 'globalThis',
111+
description: 'The global object of the inspected process. Assign values to inspect them, e.g. globalThis.store = store',
112+
icon: 'i-ph:globe-duotone',
113+
data: () => globalThis,
114+
}
115+
}
116+
94117
/** Start the agent endpoint in the current process. */
95118
export async function exposeDataInspector(options: ExposeDataInspectorOptions = {}): Promise<DataInspectorAgent> {
96119
// Deferred so `--import`ing the agent never pulls the whole node surface
@@ -174,6 +197,9 @@ if (process.env.DEVFRAME_DATA_INSPECTOR === '1' || process.env.DEVFRAME_DATA_INS
174197
auth: process.env.DEVFRAME_DATA_INSPECTOR_AUTH !== '0',
175198
token: process.env.DEVFRAME_DATA_INSPECTOR_TOKEN,
176199
exampleSource: process.env.DEVFRAME_DATA_INSPECTOR_EXAMPLE !== '0',
200+
// Zero-code path: with no chance to call `registerDataSource`, expose
201+
// `globalThis` so assigning to it is enough to inspect anything.
202+
sources: process.env.DEVFRAME_DATA_INSPECTOR_GLOBAL !== '0' ? [createGlobalThisDataSource()] : undefined,
177203
}).catch((error) => {
178204
console.error('[data-inspector] agent failed to start:', error)
179205
})

tests/__snapshots__/tsnapi/@devframes/plugin-data-inspector/inject.snapshot.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface ExposeDataInspectorOptions {
2525
// #endregion
2626

2727
// #region Functions
28+
export declare function createGlobalThisDataSource(): DataSourceEntry;
2829
export declare function exposeDataInspector(_?: ExposeDataInspectorOptions): Promise<DataInspectorAgent>;
2930
// #endregion
3031

tests/__snapshots__/tsnapi/@devframes/plugin-data-inspector/inject.snapshot.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Generated by tsnapi — public API snapshot of `@devframes/plugin-data-inspector/inject`
33
*/
44
// #region Functions
5+
export function createGlobalThisDataSource() {}
56
export async function exposeDataInspector(_) {}
67
// #endregion
78

0 commit comments

Comments
 (0)