|
23 | 23 | * DEVFRAME_DATA_INSPECTOR=1 node --import @devframes/plugin-data-inspector/inject server.js |
24 | 24 | * ``` |
25 | 25 | * |
| 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 | + * |
26 | 31 | * It binds `127.0.0.1` and requires devframe's trust handshake by |
27 | 32 | * default: a random pre-shared token is minted per run, printed to stderr, |
28 | 33 | * and written (with the endpoint) to the discovery file |
@@ -91,6 +96,24 @@ export interface DataInspectorAgent { |
91 | 96 | close: () => Promise<void> |
92 | 97 | } |
93 | 98 |
|
| 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 | + |
94 | 117 | /** Start the agent endpoint in the current process. */ |
95 | 118 | export async function exposeDataInspector(options: ExposeDataInspectorOptions = {}): Promise<DataInspectorAgent> { |
96 | 119 | // 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 |
174 | 197 | auth: process.env.DEVFRAME_DATA_INSPECTOR_AUTH !== '0', |
175 | 198 | token: process.env.DEVFRAME_DATA_INSPECTOR_TOKEN, |
176 | 199 | 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, |
177 | 203 | }).catch((error) => { |
178 | 204 | console.error('[data-inspector] agent failed to start:', error) |
179 | 205 | }) |
|
0 commit comments