@powerhousedao/devtools 0.0.0-stage → 6.3.0-dev.12

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.
@@ -0,0 +1,360 @@
1
+ import { Plugin } from "vite";
2
+ import { IncomingMessage, ServerResponse } from "node:http";
3
+ //#region src/paths.d.ts
4
+ /** The standalone page, below the dev server's base path. */
5
+ export declare const DEVTOOLS_PAGE_PATH = "/__ph/devtools";
6
+ /** The API's routes, below the dev server's base path. */
7
+ export declare const DEVTOOLS_API_PATH = "/__ph/devtools/api";
8
+ /** The `<meta name>` the dev server delivers the token in. */
9
+ export declare const DEVTOOLS_TOKEN_META = "ph-devtools-token";
10
+ //#endregion
11
+ //#region src/project-types.d.ts
12
+ /**
13
+ * What DevTools reads about a project and its running stack, as its API
14
+ * answers it.
15
+ */
16
+ /** The project a dev server runs and where the parts of its stack answer.
17
+ * A part the server does not know about is null. */
18
+ type DevStackInfo = {
19
+ project: {
20
+ name: string | null;
21
+ version: string | null;
22
+ };
23
+ /** The tool that runs the dev server, the `ph` CLI as a rule. */
24
+ ph: {
25
+ version: string;
26
+ };
27
+ /** Where the app the dev server serves answers. */
28
+ app: {
29
+ url: string | null;
30
+ };
31
+ /** The Switchboard's GraphQL endpoint, when `ph vetra` started one. */
32
+ switchboard: {
33
+ url: string | null;
34
+ };
35
+ mcp: {
36
+ url: string | null;
37
+ };
38
+ /** When `ph vetra` came up, as an ISO timestamp. */
39
+ startedAt: string | null;
40
+ };
41
+ /** A file of the project, as a POSIX path relative to the project root. */
42
+ type ProjectFile = {
43
+ path: string;
44
+ };
45
+ type ProjectCheckId = "registered" | "manifest" | "document-types" | "definition";
46
+ /** One fact verified about a module's code. */
47
+ type ProjectCheck = {
48
+ id: ProjectCheckId;
49
+ state: "pass" | "warn" | "fail";
50
+ /** A full sentence without the final period, for example
51
+ * `Registered in editors/editors.ts`. */
52
+ label: string;
53
+ /** What to do about a check that did not pass. */
54
+ detail?: string;
55
+ };
56
+ /** A part of the project that was read, or the reason it could not be. */
57
+ type ProjectSection<T> = {
58
+ ok: true;
59
+ items: T[];
60
+ } | {
61
+ ok: false;
62
+ message: string;
63
+ };
64
+ type ProjectModuleKind = "document-model" | "editor" | "app" | "subgraph" | "processor";
65
+ type ProjectDocumentModel = {
66
+ documentType: string;
67
+ name: string;
68
+ extension: string;
69
+ description: string;
70
+ author: string;
71
+ /** The author's website as the model states it. Null when it states none. */
72
+ authorWebsite: string | null;
73
+ /** `stored`: a `<name>.json` specification.
74
+ * `code-first`: an `index.ts` that exports the model. */
75
+ layout: "stored" | "code-first";
76
+ /** The specification file or the entry module. */
77
+ source: string;
78
+ folder: string;
79
+ /** Ascending. */
80
+ versions: number[];
81
+ /** Counts of the latest version. */
82
+ modules: number;
83
+ operations: number;
84
+ checks: ProjectCheck[];
85
+ };
86
+ type ProjectEditor = {
87
+ id: string;
88
+ name: string;
89
+ folder: string;
90
+ documentTypes: string[];
91
+ checks: ProjectCheck[];
92
+ };
93
+ type ProjectApp = ProjectEditor & {
94
+ /** Null when `config.ts` is missing or could not be read. An empty list
95
+ * allows every type. */
96
+ allowedDocumentTypes: string[] | null;
97
+ dragAndDrop: boolean | null;
98
+ };
99
+ type ProjectSubgraph = {
100
+ name: string;
101
+ folder: string;
102
+ style: "schema-first" | "code-first";
103
+ /** The SDL and the file it was read from. Null for a code-first subgraph,
104
+ * whose schema exists only when its module runs. */
105
+ schema: {
106
+ source: string;
107
+ file: string;
108
+ } | null;
109
+ /** Field signatures such as `invoiceStats.example(driveId: String!): String`. */
110
+ queries: string[];
111
+ mutations: string[];
112
+ checks: ProjectCheck[];
113
+ };
114
+ type ProjectProcessor = {
115
+ name: string;
116
+ folder: string;
117
+ /** Null when the code does not say. */
118
+ type: "analytics" | "relationalDb" | null;
119
+ apps: ("connect" | "switchboard")[];
120
+ documentTypes: string[];
121
+ checks: ProjectCheck[];
122
+ };
123
+ type ProjectDiagnostic = {
124
+ severity: "error" | "warning";
125
+ code?: string;
126
+ message: string;
127
+ /** The file it is about, when known. */
128
+ file?: string;
129
+ repair?: string;
130
+ };
131
+ /** Every module in the project's code. */
132
+ type ProjectSnapshot = {
133
+ /** Changes whenever a module's files change. Also the response's `ETag`. */
134
+ revision: string;
135
+ /** Absolute path of the project. */
136
+ root: string;
137
+ documentModels: ProjectSection<ProjectDocumentModel>;
138
+ editors: ProjectSection<ProjectEditor>;
139
+ apps: ProjectSection<ProjectApp>;
140
+ subgraphs: ProjectSection<ProjectSubgraph>;
141
+ processors: ProjectSection<ProjectProcessor>;
142
+ /** What no one module's checks carry: findings of the document model
143
+ * checks that are not an error of a listed model, and modules whose code
144
+ * could not be read. A listed model's errors are in its `definition` check. */
145
+ diagnostics: ProjectDiagnostic[];
146
+ };
147
+ type ProjectStateScope = {
148
+ schema: string;
149
+ /** JSON text. */
150
+ initialValue: string;
151
+ };
152
+ type ProjectOperation = {
153
+ id: string;
154
+ name: string;
155
+ scope: "global" | "local";
156
+ description: string;
157
+ /** The input's SDL. Empty when the operation takes none. */
158
+ schema: string;
159
+ errors: {
160
+ name: string;
161
+ code: string;
162
+ description: string;
163
+ }[];
164
+ /** JSON text of each example input. */
165
+ examples: string[];
166
+ };
167
+ type ProjectDocumentModelVersion = {
168
+ version: number;
169
+ changeLog: string[];
170
+ state: {
171
+ global: ProjectStateScope;
172
+ local: ProjectStateScope | null;
173
+ };
174
+ modules: {
175
+ id: string;
176
+ name: string;
177
+ description: string;
178
+ operations: ProjectOperation[];
179
+ }[];
180
+ };
181
+ /** One document model in full. */
182
+ type ProjectDocumentModelDetail = ProjectDocumentModel & {
183
+ revision: string;
184
+ /** Ascending by version. */
185
+ specifications: ProjectDocumentModelVersion[];
186
+ };
187
+ type ProjectModuleFiles = {
188
+ folder: string;
189
+ /** Sorted by path. */
190
+ files: ProjectFile[];
191
+ };
192
+ /** The files of each editor, drive app, subgraph and
193
+ * processor. */
194
+ type ProjectFileList = {
195
+ revision: string;
196
+ modules: ProjectModuleFiles[];
197
+ };
198
+ /** The text of one file that the file list
199
+ * lists. `path` is escaped as one segment. */
200
+ type ProjectFileText = {
201
+ revision: string;
202
+ path: string;
203
+ /** Up to the server's limit, cut where a character starts. */
204
+ text: string;
205
+ /** The file goes on past `text`. */
206
+ truncated: boolean;
207
+ };
208
+ /**
209
+ * A read-only view of a project's modules, as its code has them. Each answer
210
+ * carries the revision it was read at.
211
+ */
212
+ type ProjectReader = {
213
+ /** Cheap: asked before every answer, so an unchanged project costs no read. */
214
+ revision: () => string | Promise<string>;
215
+ snapshot: () => Promise<ProjectSnapshot>;
216
+ /** Undefined when the project has no model of that type. */
217
+ documentModel: (documentType: string) => Promise<ProjectDocumentModelDetail | undefined>;
218
+ fileList: () => Promise<ProjectFileList>;
219
+ /** Undefined for a path `fileList` does not list. */
220
+ file: (path: string) => Promise<ProjectFileText | undefined>;
221
+ };
222
+ //#endregion
223
+ //#region src/api.d.ts
224
+ export declare const DEVTOOLS_TOKEN_HEADER = "x-ph-devtools-token";
225
+ type DevtoolsApiErrorCode = "bad_host" | "bad_origin" | "bad_token" | "bad_method" | "not_found" | "internal";
226
+ type DevtoolsApiError = {
227
+ error: {
228
+ code: DevtoolsApiErrorCode;
229
+ message: string;
230
+ };
231
+ };
232
+ //#endregion
233
+ //#region src/server/handler.d.ts
234
+ type DevtoolsApiOptions = {
235
+ /** What the served page sends back in the token header. */
236
+ token: string;
237
+ /** The dev server's base path, for example `/app/`. The API answers below
238
+ * it and at the bare path, as the dev server's other routes do. */
239
+ base?: string;
240
+ /** Read at each request. */
241
+ info: () => DevStackInfo | Promise<DevStackInfo>;
242
+ /** Without it the `project` routes answer `not_found`. */
243
+ project?: ProjectReader;
244
+ };
245
+ type DevtoolsApiHandler = (req: IncomingMessage, res: ServerResponse, next: () => void) => void;
246
+ export declare function createDevtoolsToken(): string;
247
+ /**
248
+ * Serves the DevTools API behind its guards. Every route needs a loopback
249
+ * `Host`, the page's own origin and the page's token. Any other path goes to
250
+ * `next`, the standalone page's included.
251
+ */
252
+ export declare function createDevtoolsApiHandler(options: DevtoolsApiOptions): DevtoolsApiHandler;
253
+ //#endregion
254
+ //#region src/server/stack.d.ts
255
+ /** What the tool that started the server knows of the run. */
256
+ type DevtoolsRun = {
257
+ /** The tool that runs the server, for example the `ph` CLI. */
258
+ tool: {
259
+ name: string;
260
+ version: string;
261
+ };
262
+ /** When the run came up, as an ISO timestamp. */
263
+ startedAt: string | null;
264
+ /** Where the run's other services answer: a URL by service name. DevTools
265
+ * shows `connect`, `switchboard` and `mcp`. */
266
+ services: Readonly<Record<string, string>>;
267
+ };
268
+ type DevtoolsStackOptions = {
269
+ /** The folder of the project the server runs. */
270
+ projectRoot: string;
271
+ /** Read at each request: the services of a run come up after its server. */
272
+ run?: () => DevtoolsRun;
273
+ /** Where the app answers, when the server knows better than `run`. */
274
+ appUrl?: () => string | undefined;
275
+ };
276
+ //#endregion
277
+ //#region src/server/standalone.d.ts
278
+ type DevtoolsServerOptions = DevtoolsStackOptions & {
279
+ /** The base path the server serves the app under. "/" by default. */
280
+ base?: string;
281
+ /** In place of the reader DevTools makes for `projectRoot`. */
282
+ project?: ProjectReader;
283
+ };
284
+ type DevtoolsServer = {
285
+ /** Answers the API's paths and hands every other request to `next`. It
286
+ * goes ahead of the app's own routes. */
287
+ handler: DevtoolsApiHandler;
288
+ /** The tag for the `<head>` of every page the server serves. It is what
289
+ * turns DevTools on in the page. */
290
+ tokenTag: string;
291
+ };
292
+ /**
293
+ * DevTools for a development server that is not Vite's: the API as a Node
294
+ * `(req, res, next)` handler, and the tag for the page. The API reads the
295
+ * project's files for the browser, so this refuses to start under
296
+ * `NODE_ENV=production`.
297
+ */
298
+ export declare function createDevtoolsServer({ base, project, ...stack }: DevtoolsServerOptions): DevtoolsServer;
299
+ //#endregion
300
+ //#region src/server/project/reader.d.ts
301
+ type ProjectReaderOptions = {
302
+ /** How long one read may take before it is stopped. A code-first model's
303
+ * module is evaluated in this process and can wait forever. */
304
+ buildTimeoutMs?: number;
305
+ /** How long a revision is reused before the files are fingerprinted again. */
306
+ revisionTtlMs?: number;
307
+ /** How long a read that is not settled is kept before the next request reads again. */
308
+ retryAfterMs?: number;
309
+ now?: () => number;
310
+ };
311
+ /**
312
+ * Reads what a project's code contains. It never writes to the
313
+ * project and watches nothing: a request fingerprints the files again.
314
+ *
315
+ * - Requests for one revision share one read, including those that arrive
316
+ * while it runs. Reads never overlap: a newer revision waits for the read
317
+ * in flight, then is read once.
318
+ * - A read that takes longer than the limit is stopped and fails. A module
319
+ * it was evaluating may run on: the same files are not read again until
320
+ * that module ends.
321
+ * - A read that failed or is not settled (a section failed, or a model's
322
+ * source did not import) can depend on files outside the fingerprint, so
323
+ * it gets a new revision and is read again after `retryAfterMs`.
324
+ */
325
+ export declare function createProjectReader(projectDir: string, { buildTimeoutMs, revisionTtlMs, retryAfterMs, now }?: ProjectReaderOptions): ProjectReader;
326
+ //#endregion
327
+ //#region src/server/vite.d.ts
328
+ /** What the page loads. `connect` also does what DevTools needs from a
329
+ * Connect app: its page route and what the app knows. */
330
+ type DevtoolsClient = "page" | "connect";
331
+ type DevtoolsVitePluginOptions = {
332
+ /** The folder of the project the dev server runs. The working directory
333
+ * by default. */
334
+ projectRoot?: string;
335
+ /** What the tool that started the dev server knows of the run. */
336
+ run?: () => DevtoolsRun;
337
+ /** In place of the reader DevTools makes for `projectRoot`. */
338
+ project?: ProjectReader;
339
+ /** "page" by default: DevTools mounted on the page, and nothing else. */
340
+ client?: DevtoolsClient;
341
+ };
342
+ /** Whether this run serves DevTools: `PH_DEVTOOLS=0` turns it off, and so
343
+ * does `NODE_ENV=production`, whatever the server is. */
344
+ export declare function devtoolsRequested(env?: Record<string, string | undefined>): boolean;
345
+ /**
346
+ * DevTools for a Vite dev server: the loopback API, the token tag that turns
347
+ * DevTools on in the page, and the module that mounts it there. It applies
348
+ * to a dev server only, never to a build or a preview, and not at all under
349
+ * `PH_DEVTOOLS=0` or `NODE_ENV=production`: a build has no reference to
350
+ * DevTools. A page the server sends to another machine (`--host`) gets
351
+ * neither the tag nor the module: the API would refuse it.
352
+ *
353
+ * One token for the plugin's life, so the servers a config change restarts
354
+ * keep answering the tab that is already open.
355
+ */
356
+ export declare function devtoolsVitePlugin({ projectRoot, run, project, client }?: DevtoolsVitePluginOptions): Plugin;
357
+ //#endregion
358
+ export type { DevStackInfo, DevtoolsApiError, DevtoolsApiErrorCode, DevtoolsApiHandler, DevtoolsApiOptions, DevtoolsClient, DevtoolsRun, DevtoolsServer, DevtoolsServerOptions, DevtoolsStackOptions, DevtoolsVitePluginOptions, ProjectApp, ProjectCheck, ProjectCheckId, ProjectDiagnostic, ProjectDocumentModel, ProjectDocumentModelDetail, ProjectDocumentModelVersion, ProjectEditor, ProjectFile, ProjectFileList, ProjectFileText, ProjectModuleFiles, ProjectModuleKind, ProjectOperation, ProjectProcessor, ProjectReader, ProjectReaderOptions, ProjectSection, ProjectSnapshot, ProjectStateScope, ProjectSubgraph };
359
+ export {};
360
+ //# sourceMappingURL=server.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.mts","names":[],"sources":["../src/paths.ts","../src/project-types.ts","../src/api.ts","../src/server/handler.ts","../src/server/stack.ts","../src/server/standalone.ts","../src/server/project/reader.ts","../src/server/vite.ts"],"mappings":";;;;qBACa;;qBAGA;;qBAGA;;;;;;;;;KCAD;EACV;IAAW;IAAqB;;;EAEhC;IAAM;;;EAEN;IAAO;;;EAEP;IAAe;;EACf;IAAO;;;EAEP;;;KAIU;EAAgB;;KAEhB;;KAOA;EACV,IAAI;EACJ;;;EAGA;;EAEA;;;KAIU,eAAe;EACrB;EAAU,OAAO;;EACjB;EAAW;;KAEL;KAOA;EACV;EACA;EACA;EACA;EACA;;EAEA;;;EAGA;;EAEA;EACA;;EAEA;;EAEA;EACA;EACA,QAAQ;;KAGE;EACV;EACA;EACA;EACA;EACA,QAAQ;;KAGE,aAAa;;;EAGvB;EACA;;KAGU;EACV;EACA;EACA;;;EAGA;IAAU;IAAgB;;;EAE1B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AsBK,kBACd,MAAK;;;;;;;;;;;;wBAgBS,qBACd,aACA,KACA,SACA,WACC,4BAAiC"}
@@ -0,0 +1,25 @@
1
+ import { S as DEVTOOLS_TOKEN_META, _ as createDevtoolsApiHandler, b as DEVTOOLS_API_PATH, g as devtoolsTokenTag, i as createProjectReader, n as devtoolsVitePlugin, r as readStack, t as devtoolsRequested, v as createDevtoolsToken, x as DEVTOOLS_PAGE_PATH, y as DEVTOOLS_TOKEN_HEADER } from "./vite-INmnb9pz.mjs";
2
+ //#region src/server/standalone.ts
3
+ /**
4
+ * DevTools for a development server that is not Vite's: the API as a Node
5
+ * `(req, res, next)` handler, and the tag for the page. The API reads the
6
+ * project's files for the browser, so this refuses to start under
7
+ * `NODE_ENV=production`.
8
+ */
9
+ function createDevtoolsServer({ base = "/", project, ...stack }) {
10
+ if (process.env.NODE_ENV === "production") throw new Error("DevTools is development only: createDevtoolsServer does not run under NODE_ENV=production.");
11
+ const token = createDevtoolsToken();
12
+ return {
13
+ tokenTag: devtoolsTokenTag(token, base),
14
+ handler: createDevtoolsApiHandler({
15
+ token,
16
+ base,
17
+ info: () => readStack(stack),
18
+ project: project ?? createProjectReader(stack.projectRoot)
19
+ })
20
+ };
21
+ }
22
+ //#endregion
23
+ export { DEVTOOLS_API_PATH, DEVTOOLS_PAGE_PATH, DEVTOOLS_TOKEN_HEADER, DEVTOOLS_TOKEN_META, createDevtoolsApiHandler, createDevtoolsServer, createDevtoolsToken, createProjectReader, devtoolsRequested, devtoolsVitePlugin };
24
+
25
+ //# sourceMappingURL=server.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.mjs","names":[],"sources":["../src/server/standalone.ts"],"sourcesContent":["import type { ProjectReader } from \"../api.js\";\nimport { devtoolsTokenTag } from \"../host.js\";\nimport {\n createDevtoolsApiHandler,\n createDevtoolsToken,\n type DevtoolsApiHandler,\n} from \"./handler.js\";\nimport { createProjectReader } from \"./project/reader.js\";\nimport { readStack, type DevtoolsStackOptions } from \"./stack.js\";\n\nexport type DevtoolsServerOptions = DevtoolsStackOptions & {\n /** The base path the server serves the app under. \"/\" by default. */\n base?: string;\n /** In place of the reader DevTools makes for `projectRoot`. */\n project?: ProjectReader;\n};\n\nexport type DevtoolsServer = {\n /** Answers the API's paths and hands every other request to `next`. It\n * goes ahead of the app's own routes. */\n handler: DevtoolsApiHandler;\n /** The tag for the `<head>` of every page the server serves. It is what\n * turns DevTools on in the page. */\n tokenTag: string;\n};\n\n/**\n * DevTools for a development server that is not Vite's: the API as a Node\n * `(req, res, next)` handler, and the tag for the page. The API reads the\n * project's files for the browser, so this refuses to start under\n * `NODE_ENV=production`.\n */\nexport function createDevtoolsServer({\n base = \"/\",\n project,\n ...stack\n}: DevtoolsServerOptions): DevtoolsServer {\n if (process.env.NODE_ENV === \"production\") {\n throw new Error(\n \"DevTools is development only: createDevtoolsServer does not run under NODE_ENV=production.\",\n );\n }\n const token = createDevtoolsToken();\n return {\n tokenTag: devtoolsTokenTag(token, base),\n handler: createDevtoolsApiHandler({\n token,\n base,\n info: () => readStack(stack),\n project: project ?? createProjectReader(stack.projectRoot),\n }),\n };\n}\n"],"mappings":";;;;;;;;AAgCA,SAAgB,qBAAqB,EACnC,OAAO,KACP,SACA,GAAG,SACqC;CACxC,IAAI,QAAQ,IAAI,aAAa,cAC3B,MAAM,IAAI,MACR,4FACF;CAEF,MAAM,QAAQ,oBAAoB;CAClC,OAAO;EACL,UAAU,iBAAiB,OAAO,IAAI;EACtC,SAAS,yBAAyB;GAChC;GACA;GACA,YAAY,UAAU,KAAK;GAC3B,SAAS,WAAW,oBAAoB,MAAM,WAAW;EAC3D,CAAC;CACH;AACF"}