@jxsuite/server 9.2.1 → 9.3.1

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/src/sse.ts CHANGED
@@ -33,7 +33,7 @@ import type { FsEventPayload } from "./refactor/fs-events.ts";
33
33
  export const RECONNECT_MS = 500;
34
34
 
35
35
  /** How often a comment frame is sent to keep an idle connection from being reaped. */
36
- const HEARTBEAT_MS = 15_000;
36
+ export const HEARTBEAT_MS = 15_000;
37
37
 
38
38
  /** The `fs` event's data: one debounced batch of filesystem changes for the studio sidebar. */
39
39
  export interface FsEventBatch {
package/src/studio-api.ts CHANGED
@@ -21,6 +21,7 @@ import {
21
21
  } from "@jxsuite/compiler/format-host";
22
22
  import { readBundledProjectSchemas } from "@jxsuite/compiler/schema-command";
23
23
  import { projectCommitArgs, projectRelativePath, readGitPrefix } from "@jxsuite/protocol/git-scope";
24
+ import { PROJECT_RENAME_PATH } from "@jxsuite/protocol/project-state-frames";
24
25
  import { handleDataApi } from "./data-api.ts";
25
26
  import { containedPath } from "./net-guard.ts";
26
27
  import { startSitePreview } from "./site-preview.ts";
@@ -32,6 +33,8 @@ import {
32
33
  } from "./live-preview.ts";
33
34
  import { applyRename } from "./refactor/apply.ts";
34
35
  import { findReferences } from "./refactor/find-refs.ts";
36
+ import { isConditionalFileRequest } from "./project-events.ts";
37
+ import type { ProjectEventsService } from "./project-events.ts";
35
38
  import {
36
39
  addPackage,
37
40
  dependenciesNeedInstall,
@@ -129,7 +132,7 @@ const extensionRegistryCache = new Map<
129
132
  * @param {string} projectRoot
130
133
  * @returns {Promise<ExtensionRegistry>}
131
134
  */
132
- async function getExtensionRegistry(projectRoot: string): Promise<ExtensionRegistry> {
135
+ export async function getExtensionRegistry(projectRoot: string): Promise<ExtensionRegistry> {
133
136
  const stamp = projectFileStamp(projectRoot) ?? { mtime: 0, path: "" };
134
137
  let projectConfig: ProjectConfig | undefined;
135
138
  try {
@@ -384,6 +387,13 @@ export interface StudioApiOptions {
384
387
  * path lets the registry reset that room, which deletes it and makes the later flush a no-op.
385
388
  */
386
389
  onFileMoved?: (absPath: string) => void;
390
+ /**
391
+ * The project state service (`project-state.md` §10.2). With it, a `PUT` or `DELETE` of a file
392
+ * that names a base (`If-Match`, `If-None-Match`) or an issuer (`Jx-Write-Id`) is the conditional
393
+ * write or delete of §5, the usage query answers from the host's graph once it is built, and the
394
+ * live preview overlay of the project it serves is keyed by document and client (§11.3).
395
+ */
396
+ projectEvents?: ProjectEventsService;
387
397
  }
388
398
 
389
399
  /**
@@ -1020,23 +1030,37 @@ export async function handleStudioApi(
1020
1030
  */
1021
1031
  if (path === "/__studio/preview/overlay") {
1022
1032
  const dir = activeProjectRoot ?? root;
1023
- let body: { path?: string; contents?: string };
1033
+ let body: { path?: string; contents?: string; clientId?: string };
1024
1034
  try {
1025
- /* SAFETY: both members are re-checked with isString below before either reaches the
1026
- overlay store. */
1027
- body = (await req.json()) as { path?: string; contents?: string };
1035
+ /* SAFETY: every member is re-checked with isString below before it reaches the overlay
1036
+ store. */
1037
+ body = (await req.json()) as { path?: string; contents?: string; clientId?: string };
1028
1038
  } catch {
1029
1039
  return problem("invalidRequest", "Invalid JSON body");
1030
1040
  }
1041
+ /* The project the host serves keys its overlays by document and client (`project-state.md`
1042
+ §11.3), so a rename moves one and a removal drops it; any other is keyed by path. */
1043
+ const keyed = opts.projectEvents?.root === resolve(dir) ? opts.projectEvents : undefined;
1044
+ const clientId = isString(body.clientId) && body.clientId !== "" ? body.clientId : undefined;
1045
+ const owner = clientId === undefined ? {} : { clientId };
1031
1046
  if (req.method === "POST") {
1032
1047
  if (!isString(body.path) || !isString(body.contents)) {
1033
1048
  return problem("invalidRequest", "Missing path or contents");
1034
1049
  }
1035
- setLivePreviewOverlay(dir, body.path, body.contents);
1050
+ if (keyed) {
1051
+ keyed.setOverlay({ contents: body.contents, path: body.path, ...owner });
1052
+ } else {
1053
+ setLivePreviewOverlay(dir, body.path, body.contents);
1054
+ }
1036
1055
  return new Response(null, { status: 204 });
1037
1056
  }
1038
1057
  if (req.method === "DELETE") {
1039
- clearLivePreviewOverlay(dir, isString(body.path) ? body.path : undefined);
1058
+ const at = isString(body.path) ? body.path : undefined;
1059
+ if (keyed) {
1060
+ keyed.clearOverlay({ ...owner, ...(at !== undefined && { path: at }) });
1061
+ } else {
1062
+ clearLivePreviewOverlay(dir, at);
1063
+ }
1040
1064
  return new Response(null, { status: 204 });
1041
1065
  }
1042
1066
  }
@@ -1304,6 +1328,9 @@ export async function handleStudioApi(
1304
1328
  } catch (error) {
1305
1329
  return new Response(errorMessage(error), { status: 400 });
1306
1330
  }
1331
+ if (opts.projectEvents && isConditionalFileRequest(req)) {
1332
+ return opts.projectEvents.handleWrite(req, abs);
1333
+ }
1307
1334
  try {
1308
1335
  await mkdir(dirname(abs), { recursive: true });
1309
1336
  await writeFile(abs, await req.text(), "utf8");
@@ -1347,6 +1374,9 @@ export async function handleStudioApi(
1347
1374
  } catch (error) {
1348
1375
  return new Response(errorMessage(error), { status: 400 });
1349
1376
  }
1377
+ if (opts.projectEvents && isConditionalFileRequest(req)) {
1378
+ return opts.projectEvents.handleDelete(req, abs);
1379
+ }
1350
1380
  try {
1351
1381
  /* A directory is removed only when it is EMPTY (`rmdir`), never recursively: a folder of
1352
1382
  files is a decision about every file in it, which the client makes one file at a time. */
@@ -1363,6 +1393,24 @@ export async function handleStudioApi(
1363
1393
  }
1364
1394
  }
1365
1395
 
1396
+ // A rename as one operation (project-state.md §9): project-relative paths, served by the host
1397
+ if (path === PROJECT_RENAME_PATH && req.method === "POST") {
1398
+ const events = opts.projectEvents;
1399
+ if (!events) {
1400
+ return problem("notFound", "This server does not serve the project state.");
1401
+ }
1402
+ return events.handleRename(
1403
+ req,
1404
+ url,
1405
+ (named) => resolve(root, named),
1406
+ (moved) => {
1407
+ if (events.root !== null) {
1408
+ opts.onFileMoved?.(resolve(events.root, moved));
1409
+ }
1410
+ },
1411
+ );
1412
+ }
1413
+
1366
1414
  // Rename file
1367
1415
  if (path === "/__studio/file/rename" && req.method === "POST") {
1368
1416
  let body: { from?: string; to?: string };
@@ -1441,8 +1489,11 @@ export async function handleStudioApi(
1441
1489
  }
1442
1490
  try {
1443
1491
  const registry = await getFormatRegistry(scanRoot);
1492
+ /* The running host's graph answers once it is indexed and settled (`project-state.md` §12);
1493
+ without one, or for another project, the engine walks the tree. */
1494
+ const graph = (await opts.projectEvents?.references(scanRoot)) ?? null;
1444
1495
  return Response.json(
1445
- await findReferences({ path: target, registry, root: scanRoot, tagName: tag }),
1496
+ await findReferences({ graph, path: target, registry, root: scanRoot, tagName: tag }),
1446
1497
  );
1447
1498
  } catch (error) {
1448
1499
  return problem("internalError", errorMessage(error));
@@ -1873,7 +1924,23 @@ export async function handleStudioApi(
1873
1924
  current = name!;
1874
1925
  }
1875
1926
  }
1876
- return Response.json({ branches, current });
1927
+ /* Branches holding commits the current one lacks. A repository with no commits, or a
1928
+ detached HEAD, cannot answer, and the member is left out rather than sent empty. */
1929
+ let unmerged: string[] | undefined;
1930
+ try {
1931
+ const behind = await runGit([
1932
+ "branch",
1933
+ "--no-merged",
1934
+ "HEAD",
1935
+ "--format=%(refname:short)",
1936
+ ]);
1937
+ unmerged = behind.split("\n").filter(Boolean);
1938
+ } catch {
1939
+ unmerged = undefined;
1940
+ }
1941
+ return Response.json(
1942
+ unmerged === undefined ? { branches, current } : { branches, current, unmerged },
1943
+ );
1877
1944
  }
1878
1945
 
1879
1946
  if (gitCmd === "log" && req.method === "GET") {