@prisma-next/migration-tools 0.11.0-dev.5 → 0.11.0-dev.51

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.
Files changed (133) hide show
  1. package/README.md +4 -4
  2. package/dist/{errors-DGYwcwXs.mjs → errors-4YabujxZ.mjs} +15 -21
  3. package/dist/errors-4YabujxZ.mjs.map +1 -0
  4. package/dist/exports/aggregate.d.mts +275 -179
  5. package/dist/exports/aggregate.d.mts.map +1 -1
  6. package/dist/exports/aggregate.mjs +363 -184
  7. package/dist/exports/aggregate.mjs.map +1 -1
  8. package/dist/exports/enumerate-migration-spaces.d.mts +53 -0
  9. package/dist/exports/enumerate-migration-spaces.d.mts.map +1 -0
  10. package/dist/exports/enumerate-migration-spaces.mjs +107 -0
  11. package/dist/exports/enumerate-migration-spaces.mjs.map +1 -0
  12. package/dist/exports/errors.d.mts +2 -2
  13. package/dist/exports/errors.d.mts.map +1 -1
  14. package/dist/exports/errors.mjs +1 -1
  15. package/dist/exports/graph.d.mts +1 -1
  16. package/dist/exports/hash.d.mts +8 -9
  17. package/dist/exports/hash.d.mts.map +1 -1
  18. package/dist/exports/hash.mjs +1 -1
  19. package/dist/exports/invariants.d.mts +1 -1
  20. package/dist/exports/invariants.d.mts.map +1 -1
  21. package/dist/exports/invariants.mjs +1 -1
  22. package/dist/exports/io.d.mts +2 -83
  23. package/dist/exports/io.mjs +1 -1
  24. package/dist/exports/metadata.d.mts +2 -2
  25. package/dist/exports/migration-graph.d.mts +9 -2
  26. package/dist/exports/migration-graph.d.mts.map +1 -0
  27. package/dist/exports/migration-graph.mjs +16 -2
  28. package/dist/exports/migration-graph.mjs.map +1 -0
  29. package/dist/exports/migration-list-graph-topology.d.mts +13 -0
  30. package/dist/exports/migration-list-graph-topology.d.mts.map +1 -0
  31. package/dist/exports/migration-list-graph-topology.mjs +105 -0
  32. package/dist/exports/migration-list-graph-topology.mjs.map +1 -0
  33. package/dist/exports/migration-list-types.d.mts +2 -0
  34. package/dist/exports/migration-list-types.mjs +1 -0
  35. package/dist/exports/migration-ts.d.mts.map +1 -1
  36. package/dist/exports/migration-ts.mjs.map +1 -1
  37. package/dist/exports/migration.d.mts +5 -6
  38. package/dist/exports/migration.d.mts.map +1 -1
  39. package/dist/exports/migration.mjs +14 -32
  40. package/dist/exports/migration.mjs.map +1 -1
  41. package/dist/exports/package.d.mts +1 -1
  42. package/dist/exports/ref-resolution.d.mts +2 -2
  43. package/dist/exports/ref-resolution.d.mts.map +1 -1
  44. package/dist/exports/ref-resolution.mjs +1 -1
  45. package/dist/exports/ref-resolution.mjs.map +1 -1
  46. package/dist/exports/refs.d.mts +15 -2
  47. package/dist/exports/refs.d.mts.map +1 -0
  48. package/dist/exports/refs.mjs +137 -2
  49. package/dist/exports/refs.mjs.map +1 -0
  50. package/dist/exports/spaces.d.mts +31 -132
  51. package/dist/exports/spaces.d.mts.map +1 -1
  52. package/dist/exports/spaces.mjs +14 -9
  53. package/dist/exports/spaces.mjs.map +1 -1
  54. package/dist/{graph-BrLXqoUc.d.mts → graph-BUZuUeBC.d.mts} +1 -2
  55. package/dist/graph-BUZuUeBC.d.mts.map +1 -0
  56. package/dist/{hash-Cr4WIr4Z.mjs → hash--Y7vCpN3.mjs} +8 -9
  57. package/dist/hash--Y7vCpN3.mjs.map +1 -0
  58. package/dist/{invariants-0daYEzyo.mjs → invariants-CCOAyg6c.mjs} +2 -2
  59. package/dist/{invariants-0daYEzyo.mjs.map → invariants-CCOAyg6c.mjs.map} +1 -1
  60. package/dist/{io-BPLfzvZe.mjs → io-BHl0amF0.mjs} +100 -13
  61. package/dist/io-BHl0amF0.mjs.map +1 -0
  62. package/dist/io-nqFXSSTN.d.mts +124 -0
  63. package/dist/io-nqFXSSTN.d.mts.map +1 -0
  64. package/dist/metadata-Bp9X04gM.d.mts +2 -0
  65. package/dist/{migration-graph-De0dUZoC.d.mts → migration-graph-DtNT-cqc.d.mts} +6 -6
  66. package/dist/migration-graph-DtNT-cqc.d.mts.map +1 -0
  67. package/dist/{migration-graph-nlS4TRpn.mjs → migration-graph-kGBkIZDa.mjs} +6 -26
  68. package/dist/migration-graph-kGBkIZDa.mjs.map +1 -0
  69. package/dist/migration-list-types-BRTuXR8i.d.mts +23 -0
  70. package/dist/migration-list-types-BRTuXR8i.d.mts.map +1 -0
  71. package/dist/op-schema-D5qkXfEf.mjs.map +1 -1
  72. package/dist/{package-DZj8YvD0.d.mts → package-DIttKL7X.d.mts} +1 -1
  73. package/dist/package-DIttKL7X.d.mts.map +1 -0
  74. package/dist/read-contract-space-contract-BS5Oxbgw.mjs +82 -0
  75. package/dist/read-contract-space-contract-BS5Oxbgw.mjs.map +1 -0
  76. package/dist/{refs-BDHo5l_g.mjs → refs-BBKNL45K.mjs} +76 -4
  77. package/dist/refs-BBKNL45K.mjs.map +1 -0
  78. package/dist/{refs-CDaNerhT.d.mts → refs-C8f2IGM8.d.mts} +12 -2
  79. package/dist/refs-C8f2IGM8.d.mts.map +1 -0
  80. package/dist/{read-contract-space-contract-DRueB4Aa.mjs → verify-contract-spaces-BJX5gqtD.mjs} +32 -80
  81. package/dist/verify-contract-spaces-BJX5gqtD.mjs.map +1 -0
  82. package/dist/verify-contract-spaces-T0aiJlBS.d.mts +132 -0
  83. package/dist/verify-contract-spaces-T0aiJlBS.d.mts.map +1 -0
  84. package/package.json +18 -6
  85. package/src/aggregate/aggregate.ts +90 -0
  86. package/src/aggregate/check-integrity.ts +243 -0
  87. package/src/aggregate/loader.ts +156 -334
  88. package/src/aggregate/planner.ts +8 -6
  89. package/src/aggregate/project-schema-to-space.ts +1 -1
  90. package/src/aggregate/strategies/graph-walk.ts +12 -7
  91. package/src/aggregate/strategies/synth.ts +2 -2
  92. package/src/aggregate/types.ts +56 -64
  93. package/src/aggregate/verifier.ts +6 -4
  94. package/src/assert-descriptor-self-consistency.ts +6 -0
  95. package/src/compute-extension-space-apply-path.ts +1 -1
  96. package/src/emit-contract-space-artefacts.ts +4 -3
  97. package/src/enumerate-migration-spaces.ts +127 -0
  98. package/src/errors.ts +17 -2
  99. package/src/exports/aggregate.ts +17 -12
  100. package/src/exports/enumerate-migration-spaces.ts +4 -0
  101. package/src/exports/io.ts +2 -0
  102. package/src/exports/metadata.ts +1 -1
  103. package/src/exports/migration-graph.ts +1 -0
  104. package/src/exports/migration-list-graph-topology.ts +5 -0
  105. package/src/exports/migration-list-types.ts +5 -0
  106. package/src/exports/refs.ts +8 -0
  107. package/src/exports/spaces.ts +3 -0
  108. package/src/graph-membership.ts +17 -0
  109. package/src/graph.ts +0 -1
  110. package/src/hash.ts +7 -8
  111. package/src/integrity-violation.ts +114 -0
  112. package/src/io.ts +139 -14
  113. package/src/metadata.ts +1 -1
  114. package/src/migration-base.ts +10 -30
  115. package/src/migration-graph.ts +7 -35
  116. package/src/migration-list-graph-topology.ts +158 -0
  117. package/src/migration-list-types.ts +21 -0
  118. package/src/read-contract-space-head-ref.ts +5 -2
  119. package/src/refs/snapshot.ts +197 -0
  120. package/src/refs.ts +97 -1
  121. package/src/space-layout.ts +30 -0
  122. package/dist/errors-DGYwcwXs.mjs.map +0 -1
  123. package/dist/exports/io.d.mts.map +0 -1
  124. package/dist/graph-BrLXqoUc.d.mts.map +0 -1
  125. package/dist/hash-Cr4WIr4Z.mjs.map +0 -1
  126. package/dist/io-BPLfzvZe.mjs.map +0 -1
  127. package/dist/metadata-BFX0xdz8.d.mts +0 -2
  128. package/dist/migration-graph-De0dUZoC.d.mts.map +0 -1
  129. package/dist/migration-graph-nlS4TRpn.mjs.map +0 -1
  130. package/dist/package-DZj8YvD0.d.mts.map +0 -1
  131. package/dist/read-contract-space-contract-DRueB4Aa.mjs.map +0 -1
  132. package/dist/refs-BDHo5l_g.mjs.map +0 -1
  133. package/dist/refs-CDaNerhT.d.mts.map +0 -1
@@ -0,0 +1,82 @@
1
+ import { p as errorInvalidRefFile, u as errorInvalidJson, y as errorMissingFile } from "./errors-4YabujxZ.mjs";
2
+ import { c as spaceMigrationDirectory, l as spaceRefsDirectory, o as assertValidSpaceId } from "./verify-contract-spaces-BJX5gqtD.mjs";
3
+ import { join } from "pathe";
4
+ import { readFile } from "node:fs/promises";
5
+ //#region src/read-contract-space-head-ref.ts
6
+ function hasErrnoCode$1(error, code) {
7
+ return error instanceof Error && error.code === code;
8
+ }
9
+ /**
10
+ * Read the head ref (`hash` + `invariants`) for a contract space from
11
+ * `<projectMigrationsDir>/<spaceId>/refs/head.json`.
12
+ *
13
+ * Returns `null` when the file does not exist (first emit). Surfaces
14
+ * `MIGRATION.INVALID_JSON` / `MIGRATION.INVALID_REF_FILE` on a corrupt
15
+ * `refs/head.json` so callers can distinguish "no head ref on disk"
16
+ * (returns `null`) from "head ref present but unreadable" (throws).
17
+ *
18
+ * Validates the space id against `[a-z][a-z0-9_-]{0,63}` for the same
19
+ * filesystem-safety reasons as the rest of the per-space helpers. The
20
+ * helper is uniform across the app and extension spaces.
21
+ */
22
+ async function readContractSpaceHeadRef(projectMigrationsDir, spaceId) {
23
+ assertValidSpaceId(spaceId);
24
+ const filePath = join(spaceRefsDirectory(spaceMigrationDirectory(projectMigrationsDir, spaceId)), "head.json");
25
+ let raw;
26
+ try {
27
+ raw = await readFile(filePath, "utf-8");
28
+ } catch (error) {
29
+ if (hasErrnoCode$1(error, "ENOENT")) return null;
30
+ throw error;
31
+ }
32
+ let parsed;
33
+ try {
34
+ parsed = JSON.parse(raw);
35
+ } catch (e) {
36
+ throw errorInvalidJson(filePath, e instanceof Error ? e.message : String(e));
37
+ }
38
+ if (typeof parsed !== "object" || parsed === null) throw errorInvalidRefFile(filePath, "expected an object");
39
+ const obj = parsed;
40
+ if (typeof obj.hash !== "string") throw errorInvalidRefFile(filePath, "expected an object with a string `hash` field");
41
+ if (!Array.isArray(obj.invariants) || obj.invariants.some((value) => typeof value !== "string")) throw errorInvalidRefFile(filePath, "expected an object with an `invariants` array of strings");
42
+ return {
43
+ hash: obj.hash,
44
+ invariants: obj.invariants
45
+ };
46
+ }
47
+ //#endregion
48
+ //#region src/read-contract-space-contract.ts
49
+ function hasErrnoCode(error, code) {
50
+ return error instanceof Error && error.code === code;
51
+ }
52
+ /**
53
+ * Read the on-disk contract value for a contract space
54
+ * (`<projectMigrationsDir>/<spaceId>/contract.json`). Returns the parsed
55
+ * JSON value as `unknown` — callers that need a typed contract validate
56
+ * via their family's `deserializeContract` to surface schema issues.
57
+ *
58
+ * Companion to {@link import('./read-contract-space-head-ref').readContractSpaceHeadRef}
59
+ * — same ENOENT-throws / corrupt-file-error semantics. Returns the
60
+ * canonical-JSON value the framework wrote during emit, so re-running
61
+ * this helper across machines / runs yields a byte-identical value.
62
+ */
63
+ async function readContractSpaceContract(projectMigrationsDir, spaceId) {
64
+ assertValidSpaceId(spaceId);
65
+ const filePath = join(projectMigrationsDir, spaceId, "contract.json");
66
+ let raw;
67
+ try {
68
+ raw = await readFile(filePath, "utf-8");
69
+ } catch (error) {
70
+ if (hasErrnoCode(error, "ENOENT")) throw errorMissingFile("contract.json", join(projectMigrationsDir, spaceId));
71
+ throw error;
72
+ }
73
+ try {
74
+ return JSON.parse(raw);
75
+ } catch (e) {
76
+ throw errorInvalidJson(filePath, e instanceof Error ? e.message : String(e));
77
+ }
78
+ }
79
+ //#endregion
80
+ export { readContractSpaceHeadRef as n, readContractSpaceContract as t };
81
+
82
+ //# sourceMappingURL=read-contract-space-contract-BS5Oxbgw.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read-contract-space-contract-BS5Oxbgw.mjs","names":["hasErrnoCode"],"sources":["../src/read-contract-space-head-ref.ts","../src/read-contract-space-contract.ts"],"sourcesContent":["import { readFile } from 'node:fs/promises';\nimport type { ContractSpaceHeadRef } from '@prisma-next/framework-components/control';\nimport { join } from 'pathe';\nimport { errorInvalidJson, errorInvalidRefFile } from './errors';\nimport { assertValidSpaceId, spaceMigrationDirectory, spaceRefsDirectory } from './space-layout';\n\nexport type { ContractSpaceHeadRef };\n\nfunction hasErrnoCode(error: unknown, code: string): boolean {\n return error instanceof Error && (error as { code?: string }).code === code;\n}\n\n/**\n * Read the head ref (`hash` + `invariants`) for a contract space from\n * `<projectMigrationsDir>/<spaceId>/refs/head.json`.\n *\n * Returns `null` when the file does not exist (first emit). Surfaces\n * `MIGRATION.INVALID_JSON` / `MIGRATION.INVALID_REF_FILE` on a corrupt\n * `refs/head.json` so callers can distinguish \"no head ref on disk\"\n * (returns `null`) from \"head ref present but unreadable\" (throws).\n *\n * Validates the space id against `[a-z][a-z0-9_-]{0,63}` for the same\n * filesystem-safety reasons as the rest of the per-space helpers. The\n * helper is uniform across the app and extension spaces.\n */\nexport async function readContractSpaceHeadRef(\n projectMigrationsDir: string,\n spaceId: string,\n): Promise<ContractSpaceHeadRef | null> {\n assertValidSpaceId(spaceId);\n\n const filePath = join(\n spaceRefsDirectory(spaceMigrationDirectory(projectMigrationsDir, spaceId)),\n 'head.json',\n );\n\n let raw: string;\n try {\n raw = await readFile(filePath, 'utf-8');\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n return null;\n }\n throw error;\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch (e) {\n throw errorInvalidJson(filePath, e instanceof Error ? e.message : String(e));\n }\n\n if (typeof parsed !== 'object' || parsed === null) {\n throw errorInvalidRefFile(filePath, 'expected an object');\n }\n const obj = parsed as { hash?: unknown; invariants?: unknown };\n if (typeof obj.hash !== 'string') {\n throw errorInvalidRefFile(filePath, 'expected an object with a string `hash` field');\n }\n if (!Array.isArray(obj.invariants) || obj.invariants.some((value) => typeof value !== 'string')) {\n throw errorInvalidRefFile(filePath, 'expected an object with an `invariants` array of strings');\n }\n\n return { hash: obj.hash, invariants: obj.invariants as readonly string[] };\n}\n","import { readFile } from 'node:fs/promises';\nimport { join } from 'pathe';\nimport { errorInvalidJson, errorMissingFile } from './errors';\nimport { assertValidSpaceId } from './space-layout';\n\nfunction hasErrnoCode(error: unknown, code: string): boolean {\n return error instanceof Error && (error as { code?: string }).code === code;\n}\n\n/**\n * Read the on-disk contract value for a contract space\n * (`<projectMigrationsDir>/<spaceId>/contract.json`). Returns the parsed\n * JSON value as `unknown` — callers that need a typed contract validate\n * via their family's `deserializeContract` to surface schema issues.\n *\n * Companion to {@link import('./read-contract-space-head-ref').readContractSpaceHeadRef}\n * — same ENOENT-throws / corrupt-file-error semantics. Returns the\n * canonical-JSON value the framework wrote during emit, so re-running\n * this helper across machines / runs yields a byte-identical value.\n */\nexport async function readContractSpaceContract(\n projectMigrationsDir: string,\n spaceId: string,\n): Promise<unknown> {\n assertValidSpaceId(spaceId);\n\n const filePath = join(projectMigrationsDir, spaceId, 'contract.json');\n\n let raw: string;\n try {\n raw = await readFile(filePath, 'utf-8');\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n throw errorMissingFile('contract.json', join(projectMigrationsDir, spaceId));\n }\n throw error;\n }\n\n try {\n return JSON.parse(raw);\n } catch (e) {\n throw errorInvalidJson(filePath, e instanceof Error ? e.message : String(e));\n }\n}\n"],"mappings":";;;;;AAQA,SAASA,eAAa,OAAgB,MAAuB;CAC3D,OAAO,iBAAiB,SAAU,MAA4B,SAAS;AACzE;;;;;;;;;;;;;;AAeA,eAAsB,yBACpB,sBACA,SACsC;CACtC,mBAAmB,OAAO;CAE1B,MAAM,WAAW,KACf,mBAAmB,wBAAwB,sBAAsB,OAAO,CAAC,GACzE,WACF;CAEA,IAAI;CACJ,IAAI;EACF,MAAM,MAAM,SAAS,UAAU,OAAO;CACxC,SAAS,OAAO;EACd,IAAIA,eAAa,OAAO,QAAQ,GAC9B,OAAO;EAET,MAAM;CACR;CAEA,IAAI;CACJ,IAAI;EACF,SAAS,KAAK,MAAM,GAAG;CACzB,SAAS,GAAG;EACV,MAAM,iBAAiB,UAAU,aAAa,QAAQ,EAAE,UAAU,OAAO,CAAC,CAAC;CAC7E;CAEA,IAAI,OAAO,WAAW,YAAY,WAAW,MAC3C,MAAM,oBAAoB,UAAU,oBAAoB;CAE1D,MAAM,MAAM;CACZ,IAAI,OAAO,IAAI,SAAS,UACtB,MAAM,oBAAoB,UAAU,+CAA+C;CAErF,IAAI,CAAC,MAAM,QAAQ,IAAI,UAAU,KAAK,IAAI,WAAW,MAAM,UAAU,OAAO,UAAU,QAAQ,GAC5F,MAAM,oBAAoB,UAAU,0DAA0D;CAGhG,OAAO;EAAE,MAAM,IAAI;EAAM,YAAY,IAAI;CAAgC;AAC3E;;;AC5DA,SAAS,aAAa,OAAgB,MAAuB;CAC3D,OAAO,iBAAiB,SAAU,MAA4B,SAAS;AACzE;;;;;;;;;;;;AAaA,eAAsB,0BACpB,sBACA,SACkB;CAClB,mBAAmB,OAAO;CAE1B,MAAM,WAAW,KAAK,sBAAsB,SAAS,eAAe;CAEpE,IAAI;CACJ,IAAI;EACF,MAAM,MAAM,SAAS,UAAU,OAAO;CACxC,SAAS,OAAO;EACd,IAAI,aAAa,OAAO,QAAQ,GAC9B,MAAM,iBAAiB,iBAAiB,KAAK,sBAAsB,OAAO,CAAC;EAE7E,MAAM;CACR;CAEA,IAAI;EACF,OAAO,KAAK,MAAM,GAAG;CACvB,SAAS,GAAG;EACV,MAAM,iBAAiB,UAAU,aAAa,QAAQ,EAAE,UAAU,OAAO,CAAC,CAAC;CAC7E;AACF"}
@@ -1,8 +1,14 @@
1
- import { h as errorInvalidRefValue, m as errorInvalidRefName, p as errorInvalidRefFile, t as MigrationToolsError } from "./errors-DGYwcwXs.mjs";
1
+ import { h as errorInvalidRefValue, m as errorInvalidRefName, p as errorInvalidRefFile, t as MigrationToolsError } from "./errors-4YabujxZ.mjs";
2
2
  import { dirname, join, relative } from "pathe";
3
3
  import { mkdir, readFile, readdir, rename, rmdir, unlink, writeFile } from "node:fs/promises";
4
4
  import { type } from "arktype";
5
5
  //#region src/refs.ts
6
+ /**
7
+ * The system head ref lives at `refs/head.json`. It is read (and its
8
+ * corruption judged) through `readContractSpaceHeadRef`, not as a
9
+ * user-authored ref, so {@link readRefsTolerant} excludes it.
10
+ */
11
+ const HEAD_REF_NAME = "head";
6
12
  const REF_NAME_PATTERN = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\/[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/;
7
13
  const REF_VALUE_PATTERN = /^sha256:(empty|[0-9a-f]{64})$/;
8
14
  function validateRefName(name) {
@@ -66,7 +72,7 @@ async function readRefs(refsDir) {
66
72
  if (error instanceof Error && error.code === "ENOENT") return {};
67
73
  throw error;
68
74
  }
69
- const jsonFiles = entries.filter((entry) => entry.endsWith(".json"));
75
+ const jsonFiles = entries.filter((entry) => entry.endsWith(".json") && !entry.endsWith(".contract.json"));
70
76
  const refs = {};
71
77
  for (const jsonFile of jsonFiles) {
72
78
  const filePath = join(refsDir, jsonFile);
@@ -91,6 +97,72 @@ async function readRefs(refsDir) {
91
97
  }
92
98
  return refs;
93
99
  }
100
+ /**
101
+ * Read a space's user-authored refs without ever throwing on disk
102
+ * content. A ref whose JSON is unparseable or whose shape fails
103
+ * {@link RefEntrySchema} is omitted from `refs` and reported as a
104
+ * {@link RefLoadProblem}; the remaining well-formed refs are still
105
+ * returned. A missing `refs/` directory yields no refs and no problems.
106
+ *
107
+ * `refs/head.json` is deliberately skipped here: the system head ref is
108
+ * read through `readContractSpaceHeadRef` (which validates head-ref
109
+ * shape, distinct from the strict user-ref hash grammar), so it is judged
110
+ * there and never doubles as a user ref. Genuine I/O faults (EACCES, EIO,
111
+ * …) still propagate — only parse / schema problems are made tolerant.
112
+ */
113
+ async function readRefsTolerant(refsDir) {
114
+ let entries;
115
+ try {
116
+ entries = await readdir(refsDir, {
117
+ recursive: true,
118
+ encoding: "utf-8"
119
+ });
120
+ } catch (error) {
121
+ if (error instanceof Error && error.code === "ENOENT") return {
122
+ refs: {},
123
+ problems: []
124
+ };
125
+ throw error;
126
+ }
127
+ const jsonFiles = entries.filter((entry) => entry.endsWith(".json") && !entry.endsWith(".contract.json") && entry !== `head.json`);
128
+ const refs = {};
129
+ const problems = [];
130
+ for (const jsonFile of jsonFiles) {
131
+ const filePath = join(refsDir, jsonFile);
132
+ const name = refNameFromPath(refsDir, filePath);
133
+ let raw;
134
+ try {
135
+ raw = await readFile(filePath, "utf-8");
136
+ } catch (error) {
137
+ const code = error instanceof Error ? error.code : void 0;
138
+ if (code === "ENOENT" || code === "EISDIR") continue;
139
+ throw error;
140
+ }
141
+ let parsed;
142
+ try {
143
+ parsed = JSON.parse(raw);
144
+ } catch (e) {
145
+ problems.push({
146
+ refName: name,
147
+ detail: e instanceof Error ? e.message : String(e)
148
+ });
149
+ continue;
150
+ }
151
+ const result = RefEntrySchema(parsed);
152
+ if (result instanceof type.errors) {
153
+ problems.push({
154
+ refName: name,
155
+ detail: result.summary
156
+ });
157
+ continue;
158
+ }
159
+ refs[name] = result;
160
+ }
161
+ return {
162
+ refs,
163
+ problems
164
+ };
165
+ }
94
166
  async function writeRef(refsDir, name, entry) {
95
167
  if (!validateRefName(name)) throw errorInvalidRefName(name);
96
168
  if (!validateRefValue(entry.hash)) throw errorInvalidRefValue(entry.hash);
@@ -143,6 +215,6 @@ function resolveRef(refs, name) {
143
215
  return refs[name];
144
216
  }
145
217
  //#endregion
146
- export { validateRefName as a, resolveRef as i, readRef as n, validateRefValue as o, readRefs as r, writeRef as s, deleteRef as t };
218
+ export { readRefsTolerant as a, validateRefValue as c, readRefs as i, writeRef as l, deleteRef as n, resolveRef as o, readRef as r, validateRefName as s, HEAD_REF_NAME as t };
147
219
 
148
- //# sourceMappingURL=refs-BDHo5l_g.mjs.map
220
+ //# sourceMappingURL=refs-BBKNL45K.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refs-BBKNL45K.mjs","names":[],"sources":["../src/refs.ts"],"sourcesContent":["import { mkdir, readdir, readFile, rename, rmdir, unlink, writeFile } from 'node:fs/promises';\nimport { type } from 'arktype';\nimport { dirname, join, relative } from 'pathe';\nimport {\n errorInvalidRefFile,\n errorInvalidRefName,\n errorInvalidRefValue,\n MigrationToolsError,\n} from './errors';\n\nexport interface RefEntry {\n readonly hash: string;\n readonly invariants: readonly string[];\n}\n\nexport type Refs = Readonly<Record<string, RefEntry>>;\n\n/**\n * The system head ref lives at `refs/head.json`. It is read (and its\n * corruption judged) through `readContractSpaceHeadRef`, not as a\n * user-authored ref, so {@link readRefsTolerant} excludes it.\n */\nexport const HEAD_REF_NAME = 'head';\n\n/**\n * A single ref file that exists on disk but cannot be turned into a\n * {@link RefEntry} (unparseable JSON or schema-invalid content). The ref\n * is omitted from the result; the problem is surfaced for the integrity\n * layer to report as `refUnreadable` rather than aborting the load.\n */\nexport interface RefLoadProblem {\n readonly refName: string;\n readonly detail: string;\n}\n\nexport interface TolerantRefsResult {\n readonly refs: Refs;\n readonly problems: readonly RefLoadProblem[];\n}\n\nconst REF_NAME_PATTERN = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\\/[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/;\nconst REF_VALUE_PATTERN = /^sha256:(empty|[0-9a-f]{64})$/;\n\nexport function validateRefName(name: string): boolean {\n if (name.length === 0) return false;\n if (name.includes('..')) return false;\n if (name.includes('//')) return false;\n if (name.startsWith('.')) return false;\n return REF_NAME_PATTERN.test(name);\n}\n\nexport function validateRefValue(value: string): boolean {\n return REF_VALUE_PATTERN.test(value);\n}\n\nconst RefEntrySchema = type({\n hash: 'string',\n invariants: 'string[]',\n}).narrow((entry, ctx) => {\n if (!validateRefValue(entry.hash))\n return ctx.mustBe(`a valid contract hash (got \"${entry.hash}\")`);\n return true;\n});\n\nfunction refFilePath(refsDir: string, name: string): string {\n return join(refsDir, `${name}.json`);\n}\n\nfunction refNameFromPath(refsDir: string, filePath: string): string {\n const rel = relative(refsDir, filePath);\n return rel.replace(/\\.json$/, '');\n}\n\nexport async function readRef(refsDir: string, name: string): Promise<RefEntry> {\n if (!validateRefName(name)) {\n throw errorInvalidRefName(name);\n }\n\n const filePath = refFilePath(refsDir, name);\n let raw: string;\n try {\n raw = await readFile(filePath, 'utf-8');\n } catch (error) {\n if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {\n throw new MigrationToolsError('MIGRATION.UNKNOWN_REF', `Unknown ref \"${name}\"`, {\n why: `No ref file found at \"${filePath}\".`,\n fix: `Create the ref with: prisma-next ref set ${name} <hash>`,\n details: { refName: name, filePath },\n });\n }\n throw error;\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch {\n throw errorInvalidRefFile(filePath, 'Failed to parse as JSON');\n }\n\n const result = RefEntrySchema(parsed);\n if (result instanceof type.errors) {\n throw errorInvalidRefFile(filePath, result.summary);\n }\n\n return result;\n}\n\nexport async function readRefs(refsDir: string): Promise<Refs> {\n let entries: string[];\n try {\n entries = await readdir(refsDir, { recursive: true, encoding: 'utf-8' });\n } catch (error) {\n if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {\n return {};\n }\n throw error;\n }\n\n const jsonFiles = entries.filter(\n (entry) => entry.endsWith('.json') && !entry.endsWith('.contract.json'),\n );\n const refs: Record<string, RefEntry> = {};\n\n for (const jsonFile of jsonFiles) {\n const filePath = join(refsDir, jsonFile);\n const name = refNameFromPath(refsDir, filePath);\n\n let raw: string;\n try {\n raw = await readFile(filePath, 'utf-8');\n } catch (error) {\n // Tolerate the TOCTOU race between `readdir` and `readFile` (ENOENT) and\n // benign EISDIR if a directory happens to end in `.json`. Anything else\n // (EACCES, EIO, EMFILE, …) is a real failure and propagates so the CLI\n // surfaces it rather than silently dropping the ref.\n const code = error instanceof Error ? (error as { code?: string }).code : undefined;\n if (code === 'ENOENT' || code === 'EISDIR') {\n continue;\n }\n throw error;\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch {\n throw errorInvalidRefFile(filePath, 'Failed to parse as JSON');\n }\n\n const result = RefEntrySchema(parsed);\n if (result instanceof type.errors) {\n throw errorInvalidRefFile(filePath, result.summary);\n }\n\n refs[name] = result;\n }\n\n return refs;\n}\n\n/**\n * Read a space's user-authored refs without ever throwing on disk\n * content. A ref whose JSON is unparseable or whose shape fails\n * {@link RefEntrySchema} is omitted from `refs` and reported as a\n * {@link RefLoadProblem}; the remaining well-formed refs are still\n * returned. A missing `refs/` directory yields no refs and no problems.\n *\n * `refs/head.json` is deliberately skipped here: the system head ref is\n * read through `readContractSpaceHeadRef` (which validates head-ref\n * shape, distinct from the strict user-ref hash grammar), so it is judged\n * there and never doubles as a user ref. Genuine I/O faults (EACCES, EIO,\n * …) still propagate — only parse / schema problems are made tolerant.\n */\nexport async function readRefsTolerant(refsDir: string): Promise<TolerantRefsResult> {\n let entries: string[];\n try {\n entries = await readdir(refsDir, { recursive: true, encoding: 'utf-8' });\n } catch (error) {\n if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {\n return { refs: {}, problems: [] };\n }\n throw error;\n }\n\n const jsonFiles = entries.filter(\n (entry) =>\n entry.endsWith('.json') &&\n !entry.endsWith('.contract.json') &&\n entry !== `${HEAD_REF_NAME}.json`,\n );\n const refs: Record<string, RefEntry> = {};\n const problems: RefLoadProblem[] = [];\n\n for (const jsonFile of jsonFiles) {\n const filePath = join(refsDir, jsonFile);\n const name = refNameFromPath(refsDir, filePath);\n\n let raw: string;\n try {\n raw = await readFile(filePath, 'utf-8');\n } catch (error) {\n // Tolerate the TOCTOU race between `readdir` and `readFile` (ENOENT)\n // and benign EISDIR if a directory happens to end in `.json`.\n // Anything else (EACCES, EIO, …) is a real failure and propagates.\n const code = error instanceof Error ? (error as { code?: string }).code : undefined;\n if (code === 'ENOENT' || code === 'EISDIR') {\n continue;\n }\n throw error;\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch (e) {\n problems.push({ refName: name, detail: e instanceof Error ? e.message : String(e) });\n continue;\n }\n\n const result = RefEntrySchema(parsed);\n if (result instanceof type.errors) {\n problems.push({ refName: name, detail: result.summary });\n continue;\n }\n\n refs[name] = result;\n }\n\n return { refs, problems };\n}\n\nexport async function writeRef(refsDir: string, name: string, entry: RefEntry): Promise<void> {\n if (!validateRefName(name)) {\n throw errorInvalidRefName(name);\n }\n if (!validateRefValue(entry.hash)) {\n throw errorInvalidRefValue(entry.hash);\n }\n\n const filePath = refFilePath(refsDir, name);\n const dir = dirname(filePath);\n await mkdir(dir, { recursive: true });\n\n const tmpPath = join(dir, `.${name.split('/').pop()}.json.${Date.now()}.tmp`);\n await writeFile(\n tmpPath,\n `${JSON.stringify({ hash: entry.hash, invariants: [...entry.invariants] }, null, 2)}\\n`,\n );\n await rename(tmpPath, filePath);\n}\n\nexport async function deleteRef(refsDir: string, name: string): Promise<void> {\n if (!validateRefName(name)) {\n throw errorInvalidRefName(name);\n }\n\n const filePath = refFilePath(refsDir, name);\n try {\n await unlink(filePath);\n } catch (error) {\n if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {\n throw new MigrationToolsError('MIGRATION.UNKNOWN_REF', `Unknown ref \"${name}\"`, {\n why: `No ref file found at \"${filePath}\".`,\n fix: 'Run `prisma-next ref list` to see available refs.',\n details: { refName: name, filePath },\n });\n }\n throw error;\n }\n\n // Clean empty parent directories up to refsDir. Stop walking on the expected\n // \"directory has siblings\" signal (ENOTEMPTY on Linux, EEXIST on some BSDs)\n // and on ENOENT (concurrent removal). Anything else (EACCES, EIO, …) is a\n // real failure and propagates.\n let dir = dirname(filePath);\n while (dir !== refsDir && dir.startsWith(refsDir)) {\n try {\n await rmdir(dir);\n dir = dirname(dir);\n } catch (error) {\n const code = error instanceof Error ? (error as { code?: string }).code : undefined;\n if (code === 'ENOTEMPTY' || code === 'EEXIST' || code === 'ENOENT') {\n break;\n }\n throw error;\n }\n }\n}\n\nexport function resolveRef(refs: Refs, name: string): RefEntry {\n if (!validateRefName(name)) {\n throw errorInvalidRefName(name);\n }\n\n // Object.hasOwn gate: plain-object `refs` would otherwise let\n // `refs['constructor']` return Object.prototype.constructor and bypass the\n // UNKNOWN_REF throw. validateRefName accepts `\"constructor\"` as a name shape.\n if (!Object.hasOwn(refs, name)) {\n throw new MigrationToolsError('MIGRATION.UNKNOWN_REF', `Unknown ref \"${name}\"`, {\n why: `No ref named \"${name}\" exists.`,\n fix: `Available refs: ${Object.keys(refs).join(', ') || '(none)'}. Create a ref with: prisma-next ref set ${name} <hash>`,\n details: { refName: name, availableRefs: Object.keys(refs) },\n });\n }\n\n // biome-ignore lint/style/noNonNullAssertion: Object.hasOwn gate above guarantees this is defined\n return refs[name]!;\n}\n"],"mappings":";;;;;;;;;;AAsBA,MAAa,gBAAgB;AAkB7B,MAAM,mBAAmB;AACzB,MAAM,oBAAoB;AAE1B,SAAgB,gBAAgB,MAAuB;CACrD,IAAI,KAAK,WAAW,GAAG,OAAO;CAC9B,IAAI,KAAK,SAAS,IAAI,GAAG,OAAO;CAChC,IAAI,KAAK,SAAS,IAAI,GAAG,OAAO;CAChC,IAAI,KAAK,WAAW,GAAG,GAAG,OAAO;CACjC,OAAO,iBAAiB,KAAK,IAAI;AACnC;AAEA,SAAgB,iBAAiB,OAAwB;CACvD,OAAO,kBAAkB,KAAK,KAAK;AACrC;AAEA,MAAM,iBAAiB,KAAK;CAC1B,MAAM;CACN,YAAY;AACd,CAAC,EAAE,QAAQ,OAAO,QAAQ;CACxB,IAAI,CAAC,iBAAiB,MAAM,IAAI,GAC9B,OAAO,IAAI,OAAO,+BAA+B,MAAM,KAAK,GAAG;CACjE,OAAO;AACT,CAAC;AAED,SAAS,YAAY,SAAiB,MAAsB;CAC1D,OAAO,KAAK,SAAS,GAAG,KAAK,MAAM;AACrC;AAEA,SAAS,gBAAgB,SAAiB,UAA0B;CAElE,OADY,SAAS,SAAS,QACrB,EAAE,QAAQ,WAAW,EAAE;AAClC;AAEA,eAAsB,QAAQ,SAAiB,MAAiC;CAC9E,IAAI,CAAC,gBAAgB,IAAI,GACvB,MAAM,oBAAoB,IAAI;CAGhC,MAAM,WAAW,YAAY,SAAS,IAAI;CAC1C,IAAI;CACJ,IAAI;EACF,MAAM,MAAM,SAAS,UAAU,OAAO;CACxC,SAAS,OAAO;EACd,IAAI,iBAAiB,SAAU,MAA4B,SAAS,UAClE,MAAM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3B,MAAM,IAAI,oBAAoB,yBAAyB,gBAAgB,KAAK,IAAI;EAC9E,KAAK,iBAAiB,KAAK;EAC3B,KAAK,mBAAmB,OAAO,KAAK,IAAI,EAAE,KAAK,IAAI,KAAK,SAAS,2CAA2C,KAAK;EACjH,SAAS;GAAE,SAAS;GAAM,eAAe,OAAO,KAAK,IAAI;EAAE;CAC7D,CAAC;CAIH,OAAO,KAAK;AACd"}
@@ -4,6 +4,16 @@ interface RefEntry {
4
4
  readonly invariants: readonly string[];
5
5
  }
6
6
  type Refs = Readonly<Record<string, RefEntry>>;
7
+ /**
8
+ * A single ref file that exists on disk but cannot be turned into a
9
+ * {@link RefEntry} (unparseable JSON or schema-invalid content). The ref
10
+ * is omitted from the result; the problem is surfaced for the integrity
11
+ * layer to report as `refUnreadable` rather than aborting the load.
12
+ */
13
+ interface RefLoadProblem {
14
+ readonly refName: string;
15
+ readonly detail: string;
16
+ }
7
17
  declare function validateRefName(name: string): boolean;
8
18
  declare function validateRefValue(value: string): boolean;
9
19
  declare function readRef(refsDir: string, name: string): Promise<RefEntry>;
@@ -12,5 +22,5 @@ declare function writeRef(refsDir: string, name: string, entry: RefEntry): Promi
12
22
  declare function deleteRef(refsDir: string, name: string): Promise<void>;
13
23
  declare function resolveRef(refs: Refs, name: string): RefEntry;
14
24
  //#endregion
15
- export { readRefs as a, validateRefValue as c, readRef as i, writeRef as l, Refs as n, resolveRef as o, deleteRef as r, validateRefName as s, RefEntry as t };
16
- //# sourceMappingURL=refs-CDaNerhT.d.mts.map
25
+ export { readRef as a, validateRefName as c, deleteRef as i, validateRefValue as l, RefLoadProblem as n, readRefs as o, Refs as r, resolveRef as s, RefEntry as t, writeRef as u };
26
+ //# sourceMappingURL=refs-C8f2IGM8.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refs-C8f2IGM8.d.mts","names":[],"sources":["../src/refs.ts"],"mappings":";UAUiB,QAAA;EAAA,SACN,IAAA;EAAA,SACA,UAAU;AAAA;AAAA,KAGT,IAAA,GAAO,QAAA,CAAS,MAAA,SAAe,QAAA;;;;;;;UAe1B,cAAA;EAAA,SACN,OAAA;EAAA,SACA,MAAM;AAAA;AAAA,iBAWD,eAAA,CAAgB,IAAY;AAAA,iBAQ5B,gBAAA,CAAiB,KAAa;AAAA,iBAsBxB,OAAA,CAAQ,OAAA,UAAiB,IAAA,WAAe,OAAO,CAAC,QAAA;AAAA,iBAmChD,QAAA,CAAS,OAAA,WAAkB,OAAO,CAAC,IAAA;AAAA,iBA4HnC,QAAA,CAAS,OAAA,UAAiB,IAAA,UAAc,KAAA,EAAO,QAAA,GAAW,OAAO;AAAA,iBAoBjE,SAAA,CAAU,OAAA,UAAiB,IAAA,WAAe,OAAO;AAAA,iBAsCvD,UAAA,CAAW,IAAA,EAAM,IAAA,EAAM,IAAA,WAAe,QAAQ"}
@@ -1,7 +1,7 @@
1
- import { _ as errorInvalidSpaceId, p as errorInvalidRefFile, u as errorInvalidJson, y as errorMissingFile } from "./errors-DGYwcwXs.mjs";
2
- import { t as MANIFEST_FILE } from "./io-BPLfzvZe.mjs";
1
+ import { _ as errorInvalidSpaceId } from "./errors-4YabujxZ.mjs";
2
+ import { t as MANIFEST_FILE } from "./io-BHl0amF0.mjs";
3
3
  import { join } from "pathe";
4
- import { readFile, readdir, stat } from "node:fs/promises";
4
+ import { readdir, stat } from "node:fs/promises";
5
5
  import { APP_SPACE_ID } from "@prisma-next/framework-components/control";
6
6
  //#region src/space-layout.ts
7
7
  /**
@@ -32,52 +32,36 @@ function spaceMigrationDirectory(projectMigrationsDir, spaceId) {
32
32
  assertValidSpaceId(spaceId);
33
33
  return join(projectMigrationsDir, spaceId);
34
34
  }
35
- //#endregion
36
- //#region src/read-contract-space-head-ref.ts
37
- function hasErrnoCode$2(error, code) {
38
- return error instanceof Error && error.code === code;
39
- }
40
35
  /**
41
- * Read the head ref (`hash` + `invariants`) for a contract space from
42
- * `<projectMigrationsDir>/<spaceId>/refs/head.json`.
43
- *
44
- * Returns `null` when the file does not exist (first emit). Surfaces
45
- * `MIGRATION.INVALID_JSON` / `MIGRATION.INVALID_REF_FILE` on a corrupt
46
- * `refs/head.json` so callers can distinguish "no head ref on disk"
47
- * (returns `null`) from "head ref present but unreadable" (throws).
48
- *
49
- * Validates the space id against `[a-z][a-z0-9_-]{0,63}` for the same
50
- * filesystem-safety reasons as the rest of the per-space helpers. The
51
- * helper is uniform across the app and extension spaces.
36
+ * Per-space subdirectory name reserved for the ref store
37
+ * (`migrations/<space>/<SPACE_REFS_DIRNAME>/*.json`). Single source of
38
+ * truth: every helper that composes a per-space refs path imports this
39
+ * constant, and the enumerator uses it (via
40
+ * {@link RESERVED_SPACE_SUBDIR_NAMES}) to exclude reserved names from
41
+ * the contract-space candidate list.
52
42
  */
53
- async function readContractSpaceHeadRef(projectMigrationsDir, spaceId) {
54
- assertValidSpaceId(spaceId);
55
- const filePath = join(projectMigrationsDir, spaceId, "refs", "head.json");
56
- let raw;
57
- try {
58
- raw = await readFile(filePath, "utf-8");
59
- } catch (error) {
60
- if (hasErrnoCode$2(error, "ENOENT")) return null;
61
- throw error;
62
- }
63
- let parsed;
64
- try {
65
- parsed = JSON.parse(raw);
66
- } catch (e) {
67
- throw errorInvalidJson(filePath, e instanceof Error ? e.message : String(e));
68
- }
69
- if (typeof parsed !== "object" || parsed === null) throw errorInvalidRefFile(filePath, "expected an object");
70
- const obj = parsed;
71
- if (typeof obj.hash !== "string") throw errorInvalidRefFile(filePath, "expected an object with a string `hash` field");
72
- if (!Array.isArray(obj.invariants) || obj.invariants.some((value) => typeof value !== "string")) throw errorInvalidRefFile(filePath, "expected an object with an `invariants` array of strings");
73
- return {
74
- hash: obj.hash,
75
- invariants: obj.invariants
76
- };
43
+ const SPACE_REFS_DIRNAME = "refs";
44
+ /**
45
+ * Names reserved as per-space subdirectories of `migrations/<space>/`.
46
+ * Used by the enumerator to filter contract-space candidates so a
47
+ * reserved name (e.g. a top-level `migrations/refs/` left in the wrong
48
+ * place) is never enumerated as a phantom contract space. Currently
49
+ * holds `SPACE_REFS_DIRNAME`; extend if future per-space layouts add
50
+ * more reserved subdirectories.
51
+ */
52
+ const RESERVED_SPACE_SUBDIR_NAMES = new Set([SPACE_REFS_DIRNAME]);
53
+ /**
54
+ * Resolve the per-space refs directory for `spaceMigrationsDir`
55
+ * (typically the value returned by {@link spaceMigrationDirectory}).
56
+ * Composes the canonical {@link SPACE_REFS_DIRNAME} so callers do not
57
+ * hard-code the literal.
58
+ */
59
+ function spaceRefsDirectory(spaceMigrationsDir) {
60
+ return join(spaceMigrationsDir, SPACE_REFS_DIRNAME);
77
61
  }
78
62
  //#endregion
79
63
  //#region src/verify-contract-spaces.ts
80
- function hasErrnoCode$1(error, code) {
64
+ function hasErrnoCode(error, code) {
81
65
  return error instanceof Error && error.code === code;
82
66
  }
83
67
  /**
@@ -104,7 +88,7 @@ async function listContractSpaceDirectories(projectMigrationsDir) {
104
88
  isDirectory: d.isDirectory()
105
89
  }));
106
90
  } catch (error) {
107
- if (hasErrnoCode$1(error, "ENOENT")) return [];
91
+ if (hasErrnoCode(error, "ENOENT")) return [];
108
92
  throw error;
109
93
  }
110
94
  const namedCandidates = entries.filter((e) => e.isDirectory).map((e) => e.name).filter((name) => !name.startsWith(".")).sort();
@@ -116,7 +100,7 @@ async function listContractSpaceDirectories(projectMigrationsDir) {
116
100
  isMigrationDir: true
117
101
  };
118
102
  } catch (error) {
119
- if (hasErrnoCode$1(error, "ENOENT")) return {
103
+ if (hasErrnoCode(error, "ENOENT")) return {
120
104
  name,
121
105
  isMigrationDir: false
122
106
  };
@@ -222,38 +206,6 @@ function verifyContractSpaces(inputs) {
222
206
  };
223
207
  }
224
208
  //#endregion
225
- //#region src/read-contract-space-contract.ts
226
- function hasErrnoCode(error, code) {
227
- return error instanceof Error && error.code === code;
228
- }
229
- /**
230
- * Read the on-disk contract value for a contract space
231
- * (`<projectMigrationsDir>/<spaceId>/contract.json`). Returns the parsed
232
- * JSON value as `unknown` — callers that need a typed contract validate
233
- * via their family's `deserializeContract` to surface schema issues.
234
- *
235
- * Companion to {@link import('./read-contract-space-head-ref').readContractSpaceHeadRef}
236
- * — same ENOENT-throws / corrupt-file-error semantics. Returns the
237
- * canonical-JSON value the framework wrote during emit, so re-running
238
- * this helper across machines / runs yields a byte-identical value.
239
- */
240
- async function readContractSpaceContract(projectMigrationsDir, spaceId) {
241
- assertValidSpaceId(spaceId);
242
- const filePath = join(projectMigrationsDir, spaceId, "contract.json");
243
- let raw;
244
- try {
245
- raw = await readFile(filePath, "utf-8");
246
- } catch (error) {
247
- if (hasErrnoCode(error, "ENOENT")) throw errorMissingFile("contract.json", join(projectMigrationsDir, spaceId));
248
- throw error;
249
- }
250
- try {
251
- return JSON.parse(raw);
252
- } catch (e) {
253
- throw errorInvalidJson(filePath, e instanceof Error ? e.message : String(e));
254
- }
255
- }
256
- //#endregion
257
- export { APP_SPACE_ID as a, spaceMigrationDirectory as c, readContractSpaceHeadRef as i, listContractSpaceDirectories as n, assertValidSpaceId as o, verifyContractSpaces as r, isValidSpaceId as s, readContractSpaceContract as t };
209
+ export { SPACE_REFS_DIRNAME as a, spaceMigrationDirectory as c, RESERVED_SPACE_SUBDIR_NAMES as i, spaceRefsDirectory as l, verifyContractSpaces as n, assertValidSpaceId as o, APP_SPACE_ID as r, isValidSpaceId as s, listContractSpaceDirectories as t };
258
210
 
259
- //# sourceMappingURL=read-contract-space-contract-DRueB4Aa.mjs.map
211
+ //# sourceMappingURL=verify-contract-spaces-BJX5gqtD.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"verify-contract-spaces-BJX5gqtD.mjs","names":[],"sources":["../src/space-layout.ts","../src/verify-contract-spaces.ts"],"sourcesContent":["import { APP_SPACE_ID } from '@prisma-next/framework-components/control';\nimport { join } from 'pathe';\nimport { errorInvalidSpaceId } from './errors';\n\nexport { APP_SPACE_ID };\n\n/**\n * Branded string carrying a compile-time guarantee that the value has\n * been validated by {@link assertValidSpaceId}. Downstream filesystem\n * helpers (e.g. {@link spaceMigrationDirectory}) accept this type to\n * make \"validated\" tracking visible at the type level rather than\n * relying purely on a runtime check.\n */\nexport type ValidSpaceId = string & { readonly __brand: 'ValidSpaceId' };\n\n/**\n * Pattern a contract-space identifier must match. The constraint is\n * filesystem-friendly: lowercase letters / digits / hyphen / underscore,\n * starts with a letter, max 64 characters.\n */\nconst SPACE_ID_PATTERN = /^[a-z][a-z0-9_-]{0,63}$/;\n\nexport function isValidSpaceId(spaceId: string): spaceId is ValidSpaceId {\n return SPACE_ID_PATTERN.test(spaceId);\n}\n\nexport function assertValidSpaceId(spaceId: string): asserts spaceId is ValidSpaceId {\n if (!isValidSpaceId(spaceId)) {\n throw errorInvalidSpaceId(spaceId);\n }\n}\n\n/**\n * Resolve the migrations subdirectory for a given contract space.\n *\n * Every contract space — including the app space (default `'app'`) —\n * lands under `<projectMigrationsDir>/<spaceId>/`. The space id is\n * validated against {@link SPACE_ID_PATTERN} because it becomes a\n * filesystem directory name verbatim.\n *\n * `projectMigrationsDir` is the project's top-level `migrations/`\n * directory; the helper does not assume anything about its absolute /\n * relative shape and is symmetric with `pathe.join`.\n */\nexport function spaceMigrationDirectory(projectMigrationsDir: string, spaceId: string): string {\n assertValidSpaceId(spaceId);\n return join(projectMigrationsDir, spaceId);\n}\n\n/**\n * Per-space subdirectory name reserved for the ref store\n * (`migrations/<space>/<SPACE_REFS_DIRNAME>/*.json`). Single source of\n * truth: every helper that composes a per-space refs path imports this\n * constant, and the enumerator uses it (via\n * {@link RESERVED_SPACE_SUBDIR_NAMES}) to exclude reserved names from\n * the contract-space candidate list.\n */\nexport const SPACE_REFS_DIRNAME = 'refs';\n\n/**\n * Names reserved as per-space subdirectories of `migrations/<space>/`.\n * Used by the enumerator to filter contract-space candidates so a\n * reserved name (e.g. a top-level `migrations/refs/` left in the wrong\n * place) is never enumerated as a phantom contract space. Currently\n * holds `SPACE_REFS_DIRNAME`; extend if future per-space layouts add\n * more reserved subdirectories.\n */\nexport const RESERVED_SPACE_SUBDIR_NAMES: ReadonlySet<string> = new Set([SPACE_REFS_DIRNAME]);\n\n/**\n * Resolve the per-space refs directory for `spaceMigrationsDir`\n * (typically the value returned by {@link spaceMigrationDirectory}).\n * Composes the canonical {@link SPACE_REFS_DIRNAME} so callers do not\n * hard-code the literal.\n */\nexport function spaceRefsDirectory(spaceMigrationsDir: string): string {\n return join(spaceMigrationsDir, SPACE_REFS_DIRNAME);\n}\n","import { readdir, stat } from 'node:fs/promises';\nimport { join } from 'pathe';\nimport { MANIFEST_FILE } from './io';\nimport { APP_SPACE_ID } from './space-layout';\n\nfunction hasErrnoCode(error: unknown, code: string): boolean {\n return error instanceof Error && (error as { code?: string }).code === code;\n}\n\n/**\n * List the per-space subdirectories under\n * `<projectRoot>/migrations/`. Returns space-id directory names (sorted\n * alphabetically) — i.e. any non-dot-prefixed subdirectory whose root\n * does **not** contain a `migration.json` manifest. The manifest is the\n * structural marker of a user-authored migration directory (see\n * `readMigrationsDir` in `./io`); directory names themselves belong to\n * the user and are not part of the contract.\n *\n * Returns `[]` if the migrations directory does not exist (greenfield\n * project).\n *\n * Reads only the user's repo. **No descriptor import.** The caller\n * (verifier) feeds the result into {@link verifyContractSpaces} alongside\n * the loaded-space set and the marker rows.\n */\nexport async function listContractSpaceDirectories(\n projectMigrationsDir: string,\n): Promise<readonly string[]> {\n let entries: { readonly name: string; readonly isDirectory: boolean }[];\n try {\n const dirents = await readdir(projectMigrationsDir, { withFileTypes: true });\n entries = dirents.map((d) => ({ name: d.name, isDirectory: d.isDirectory() }));\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n return [];\n }\n throw error;\n }\n\n const namedCandidates = entries\n .filter((e) => e.isDirectory)\n .map((e) => e.name)\n .filter((name) => !name.startsWith('.'))\n .sort();\n\n const manifestChecks = await Promise.all(\n namedCandidates.map(async (name) => {\n try {\n await stat(join(projectMigrationsDir, name, MANIFEST_FILE));\n return { name, isMigrationDir: true };\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n return { name, isMigrationDir: false };\n }\n throw error;\n }\n }),\n );\n\n return manifestChecks.filter((c) => !c.isMigrationDir).map((c) => c.name);\n}\n\n/**\n * On-disk head value (`(hash, invariants)`) for one contract space.\n * The verifier compares this against the marker row for the same space\n * to detect drift between the user-emitted artefacts and the live DB\n * marker.\n */\nexport interface ContractSpaceHeadRecord {\n readonly hash: string;\n readonly invariants: readonly string[];\n}\n\n/**\n * Marker row read from `prisma_contract.marker` (one per `space`).\n * Caller resolves these via the family runtime's marker reader before\n * invoking {@link verifyContractSpaces}.\n */\nexport interface SpaceMarkerRecord {\n readonly hash: string;\n readonly invariants: readonly string[];\n}\n\nexport interface VerifyContractSpacesInputs {\n /**\n * Set of contract spaces the project declares: `'app'` plus each\n * extension space in `extensionPacks`. The caller's discovery path\n * never reads the extension descriptor module — it walks the\n * `extensionPacks` configuration in `prisma-next.config.ts` for the\n * space ids.\n */\n readonly loadedSpaces: ReadonlySet<string>;\n\n /**\n * Per-space subdirectories observed under\n * `<projectRoot>/migrations/`. Resolved via\n * {@link listContractSpaceDirectories}.\n */\n readonly spaceDirsOnDisk: readonly string[];\n\n /**\n * Head ref per space, keyed by space id. Caller reads\n * `<projectRoot>/migrations/<space-id>/contract.json` and\n * `<projectRoot>/migrations/<space-id>/refs/head.json` to construct\n * this map. Spaces with no contract-space dir on disk simply omit a\n * map entry.\n */\n readonly headRefsBySpace: ReadonlyMap<string, ContractSpaceHeadRecord>;\n\n /**\n * Marker rows keyed by `space`. Caller reads them from the\n * `prisma_contract.marker` table.\n */\n readonly markerRowsBySpace: ReadonlyMap<string, SpaceMarkerRecord>;\n}\n\nexport type SpaceVerifierViolation =\n | {\n readonly kind: 'declaredButUnmigrated';\n readonly spaceId: string;\n readonly remediation: string;\n }\n | {\n readonly kind: 'orphanMarker';\n readonly spaceId: string;\n readonly remediation: string;\n }\n | {\n readonly kind: 'orphanSpaceDir';\n readonly spaceId: string;\n readonly remediation: string;\n }\n | {\n readonly kind: 'hashMismatch';\n readonly spaceId: string;\n readonly priorHeadHash: string;\n readonly markerHash: string;\n readonly remediation: string;\n }\n | {\n readonly kind: 'invariantsMismatch';\n readonly spaceId: string;\n readonly onDiskInvariants: readonly string[];\n readonly markerInvariants: readonly string[];\n readonly remediation: string;\n };\n\nexport type VerifyContractSpacesResult =\n | { readonly ok: true }\n | { readonly ok: false; readonly violations: readonly SpaceVerifierViolation[] };\n\n/**\n * Pure structural verifier for the per-space mechanism. Aggregates the\n * three orphan / missing checks plus per-space hash and invariant\n * comparison.\n *\n * Algorithm:\n *\n * - For every extension space declared in `loadedSpaces` (`'app'`\n * excluded — the per-space verifier is scoped to extension members;\n * the app is verified through the aggregate path):\n * - If no contract-space dir on disk → `declaredButUnmigrated`.\n * - Else if `markerRowsBySpace` lacks an entry → no violation here;\n * the live-DB compare done outside this helper is where the\n * absence shows up.\n * - Else compare marker hash / invariants vs. on-disk head hash /\n * invariants → `hashMismatch` / `invariantsMismatch` on drift.\n * - For every contract-space dir on disk that is not in `loadedSpaces` →\n * `orphanSpaceDir`.\n * - For every marker row whose `space` is not in `loadedSpaces` →\n * `orphanMarker`. The app-space marker is always loaded (`'app'` is\n * in `loadedSpaces` by definition).\n *\n * Output is deterministic: violations are sorted first by `kind`\n * (`declaredButUnmigrated` → `orphanMarker` → `orphanSpaceDir` →\n * `hashMismatch` → `invariantsMismatch`) then by `spaceId`. Two callers\n * passing equivalent inputs see byte-identical violation lists.\n *\n * Synchronous, pure, no I/O. **Does not import the extension descriptor**\n * (the inputs are pre-resolved by the caller); the verifier reads only\n * the user repo, not `node_modules`.\n */\nexport function verifyContractSpaces(\n inputs: VerifyContractSpacesInputs,\n): VerifyContractSpacesResult {\n const violations: SpaceVerifierViolation[] = [];\n\n for (const spaceId of [...inputs.loadedSpaces].sort()) {\n if (spaceId === APP_SPACE_ID) continue;\n\n if (!inputs.spaceDirsOnDisk.includes(spaceId)) {\n violations.push({\n kind: 'declaredButUnmigrated',\n spaceId,\n remediation: `Extension '${spaceId}' is declared in extensionPacks but has not been emitted; run \\`prisma-next migrate\\`.`,\n });\n continue;\n }\n\n const head = inputs.headRefsBySpace.get(spaceId);\n const marker = inputs.markerRowsBySpace.get(spaceId);\n if (!head || !marker) {\n continue;\n }\n\n if (head.hash !== marker.hash) {\n violations.push({\n kind: 'hashMismatch',\n spaceId,\n priorHeadHash: head.hash,\n markerHash: marker.hash,\n remediation: `Marker row for space '${spaceId}' is keyed at ${marker.hash}, but the on-disk ${join('migrations', spaceId, 'contract.json')} resolves to ${head.hash}. Run \\`prisma-next db update\\` to advance the database, or \\`prisma-next migrate\\` if the descriptor was bumped without re-emitting.`,\n });\n continue;\n }\n\n const onDiskInvariants = [...head.invariants].sort();\n const markerInvariants = new Set(marker.invariants);\n const missing = onDiskInvariants.filter((id) => !markerInvariants.has(id));\n if (missing.length > 0) {\n violations.push({\n kind: 'invariantsMismatch',\n spaceId,\n onDiskInvariants,\n markerInvariants: [...marker.invariants].sort(),\n remediation: `Marker row for space '${spaceId}' is missing invariants [${missing.map((s) => JSON.stringify(s)).join(', ')}]. Run \\`prisma-next db update\\` to apply the corresponding data-transform migrations.`,\n });\n }\n }\n\n for (const dir of [...inputs.spaceDirsOnDisk].sort()) {\n if (!inputs.loadedSpaces.has(dir)) {\n violations.push({\n kind: 'orphanSpaceDir',\n spaceId: dir,\n remediation: `Orphan contract-space directory \\`${join('migrations', dir)}/\\` for an extension not in extensionPacks; remove the directory or re-add the extension.`,\n });\n }\n }\n\n for (const space of [...inputs.markerRowsBySpace.keys()].sort()) {\n if (!inputs.loadedSpaces.has(space)) {\n violations.push({\n kind: 'orphanMarker',\n spaceId: space,\n remediation: `Orphan marker row for space '${space}' (no longer in extensionPacks); remediation: manually delete the row from \\`prisma_contract.marker\\`.`,\n });\n }\n }\n\n if (violations.length === 0) {\n return { ok: true };\n }\n\n const kindOrder: Record<SpaceVerifierViolation['kind'], number> = {\n declaredButUnmigrated: 0,\n orphanMarker: 1,\n orphanSpaceDir: 2,\n hashMismatch: 3,\n invariantsMismatch: 4,\n };\n\n violations.sort((a, b) => {\n const k = kindOrder[a.kind] - kindOrder[b.kind];\n if (k !== 0) return k;\n if (a.spaceId < b.spaceId) return -1;\n if (a.spaceId > b.spaceId) return 1;\n return 0;\n });\n\n return { ok: false, violations };\n}\n"],"mappings":";;;;;;;;;;;AAoBA,MAAM,mBAAmB;AAEzB,SAAgB,eAAe,SAA0C;CACvE,OAAO,iBAAiB,KAAK,OAAO;AACtC;AAEA,SAAgB,mBAAmB,SAAkD;CACnF,IAAI,CAAC,eAAe,OAAO,GACzB,MAAM,oBAAoB,OAAO;AAErC;;;;;;;;;;;;;AAcA,SAAgB,wBAAwB,sBAA8B,SAAyB;CAC7F,mBAAmB,OAAO;CAC1B,OAAO,KAAK,sBAAsB,OAAO;AAC3C;;;;;;;;;AAUA,MAAa,qBAAqB;;;;;;;;;AAUlC,MAAa,8BAAmD,IAAI,IAAI,CAAC,kBAAkB,CAAC;;;;;;;AAQ5F,SAAgB,mBAAmB,oBAAoC;CACrE,OAAO,KAAK,oBAAoB,kBAAkB;AACpD;;;ACxEA,SAAS,aAAa,OAAgB,MAAuB;CAC3D,OAAO,iBAAiB,SAAU,MAA4B,SAAS;AACzE;;;;;;;;;;;;;;;;;AAkBA,eAAsB,6BACpB,sBAC4B;CAC5B,IAAI;CACJ,IAAI;EAEF,WAAU,MADY,QAAQ,sBAAsB,EAAE,eAAe,KAAK,CAAC,GACzD,KAAK,OAAO;GAAE,MAAM,EAAE;GAAM,aAAa,EAAE,YAAY;EAAE,EAAE;CAC/E,SAAS,OAAO;EACd,IAAI,aAAa,OAAO,QAAQ,GAC9B,OAAO,CAAC;EAEV,MAAM;CACR;CAEA,MAAM,kBAAkB,QACrB,QAAQ,MAAM,EAAE,WAAW,EAC3B,KAAK,MAAM,EAAE,IAAI,EACjB,QAAQ,SAAS,CAAC,KAAK,WAAW,GAAG,CAAC,EACtC,KAAK;CAgBR,QAAO,MAdsB,QAAQ,IACnC,gBAAgB,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,kBAAkB,CAAC,GAAG,OAAO,UAAU,EAAE,KAAK;GAC9C,aAAa,yBAAyB,QAAQ,2BAA2B,QAAQ,KAAK,MAAM,KAAK,UAAU,CAAC,CAAC,EAAE,KAAK,IAAI,EAAE;EAC5H,CAAC;CAEL;CAEA,KAAK,MAAM,OAAO,CAAC,GAAG,OAAO,eAAe,EAAE,KAAK,GACjD,IAAI,CAAC,OAAO,aAAa,IAAI,GAAG,GAC9B,WAAW,KAAK;EACd,MAAM;EACN,SAAS;EACT,aAAa,qCAAqC,KAAK,cAAc,GAAG,EAAE;CAC5E,CAAC;CAIL,KAAK,MAAM,SAAS,CAAC,GAAG,OAAO,kBAAkB,KAAK,CAAC,EAAE,KAAK,GAC5D,IAAI,CAAC,OAAO,aAAa,IAAI,KAAK,GAChC,WAAW,KAAK;EACd,MAAM;EACN,SAAS;EACT,aAAa,gCAAgC,MAAM;CACrD,CAAC;CAIL,IAAI,WAAW,WAAW,GACxB,OAAO,EAAE,IAAI,KAAK;CAGpB,MAAM,YAA4D;EAChE,uBAAuB;EACvB,cAAc;EACd,gBAAgB;EAChB,cAAc;EACd,oBAAoB;CACtB;CAEA,WAAW,MAAM,GAAG,MAAM;EACxB,MAAM,IAAI,UAAU,EAAE,QAAQ,UAAU,EAAE;EAC1C,IAAI,MAAM,GAAG,OAAO;EACpB,IAAI,EAAE,UAAU,EAAE,SAAS,OAAO;EAClC,IAAI,EAAE,UAAU,EAAE,SAAS,OAAO;EAClC,OAAO;CACT,CAAC;CAED,OAAO;EAAE,IAAI;EAAO;CAAW;AACjC"}
@@ -0,0 +1,132 @@
1
+ //#region src/verify-contract-spaces.d.ts
2
+ /**
3
+ * List the per-space subdirectories under
4
+ * `<projectRoot>/migrations/`. Returns space-id directory names (sorted
5
+ * alphabetically) — i.e. any non-dot-prefixed subdirectory whose root
6
+ * does **not** contain a `migration.json` manifest. The manifest is the
7
+ * structural marker of a user-authored migration directory (see
8
+ * `readMigrationsDir` in `./io`); directory names themselves belong to
9
+ * the user and are not part of the contract.
10
+ *
11
+ * Returns `[]` if the migrations directory does not exist (greenfield
12
+ * project).
13
+ *
14
+ * Reads only the user's repo. **No descriptor import.** The caller
15
+ * (verifier) feeds the result into {@link verifyContractSpaces} alongside
16
+ * the loaded-space set and the marker rows.
17
+ */
18
+ declare function listContractSpaceDirectories(projectMigrationsDir: string): Promise<readonly string[]>;
19
+ /**
20
+ * On-disk head value (`(hash, invariants)`) for one contract space.
21
+ * The verifier compares this against the marker row for the same space
22
+ * to detect drift between the user-emitted artefacts and the live DB
23
+ * marker.
24
+ */
25
+ interface ContractSpaceHeadRecord {
26
+ readonly hash: string;
27
+ readonly invariants: readonly string[];
28
+ }
29
+ /**
30
+ * Marker row read from `prisma_contract.marker` (one per `space`).
31
+ * Caller resolves these via the family runtime's marker reader before
32
+ * invoking {@link verifyContractSpaces}.
33
+ */
34
+ interface SpaceMarkerRecord {
35
+ readonly hash: string;
36
+ readonly invariants: readonly string[];
37
+ }
38
+ interface VerifyContractSpacesInputs {
39
+ /**
40
+ * Set of contract spaces the project declares: `'app'` plus each
41
+ * extension space in `extensionPacks`. The caller's discovery path
42
+ * never reads the extension descriptor module — it walks the
43
+ * `extensionPacks` configuration in `prisma-next.config.ts` for the
44
+ * space ids.
45
+ */
46
+ readonly loadedSpaces: ReadonlySet<string>;
47
+ /**
48
+ * Per-space subdirectories observed under
49
+ * `<projectRoot>/migrations/`. Resolved via
50
+ * {@link listContractSpaceDirectories}.
51
+ */
52
+ readonly spaceDirsOnDisk: readonly string[];
53
+ /**
54
+ * Head ref per space, keyed by space id. Caller reads
55
+ * `<projectRoot>/migrations/<space-id>/contract.json` and
56
+ * `<projectRoot>/migrations/<space-id>/refs/head.json` to construct
57
+ * this map. Spaces with no contract-space dir on disk simply omit a
58
+ * map entry.
59
+ */
60
+ readonly headRefsBySpace: ReadonlyMap<string, ContractSpaceHeadRecord>;
61
+ /**
62
+ * Marker rows keyed by `space`. Caller reads them from the
63
+ * `prisma_contract.marker` table.
64
+ */
65
+ readonly markerRowsBySpace: ReadonlyMap<string, SpaceMarkerRecord>;
66
+ }
67
+ type SpaceVerifierViolation = {
68
+ readonly kind: 'declaredButUnmigrated';
69
+ readonly spaceId: string;
70
+ readonly remediation: string;
71
+ } | {
72
+ readonly kind: 'orphanMarker';
73
+ readonly spaceId: string;
74
+ readonly remediation: string;
75
+ } | {
76
+ readonly kind: 'orphanSpaceDir';
77
+ readonly spaceId: string;
78
+ readonly remediation: string;
79
+ } | {
80
+ readonly kind: 'hashMismatch';
81
+ readonly spaceId: string;
82
+ readonly priorHeadHash: string;
83
+ readonly markerHash: string;
84
+ readonly remediation: string;
85
+ } | {
86
+ readonly kind: 'invariantsMismatch';
87
+ readonly spaceId: string;
88
+ readonly onDiskInvariants: readonly string[];
89
+ readonly markerInvariants: readonly string[];
90
+ readonly remediation: string;
91
+ };
92
+ type VerifyContractSpacesResult = {
93
+ readonly ok: true;
94
+ } | {
95
+ readonly ok: false;
96
+ readonly violations: readonly SpaceVerifierViolation[];
97
+ };
98
+ /**
99
+ * Pure structural verifier for the per-space mechanism. Aggregates the
100
+ * three orphan / missing checks plus per-space hash and invariant
101
+ * comparison.
102
+ *
103
+ * Algorithm:
104
+ *
105
+ * - For every extension space declared in `loadedSpaces` (`'app'`
106
+ * excluded — the per-space verifier is scoped to extension members;
107
+ * the app is verified through the aggregate path):
108
+ * - If no contract-space dir on disk → `declaredButUnmigrated`.
109
+ * - Else if `markerRowsBySpace` lacks an entry → no violation here;
110
+ * the live-DB compare done outside this helper is where the
111
+ * absence shows up.
112
+ * - Else compare marker hash / invariants vs. on-disk head hash /
113
+ * invariants → `hashMismatch` / `invariantsMismatch` on drift.
114
+ * - For every contract-space dir on disk that is not in `loadedSpaces` →
115
+ * `orphanSpaceDir`.
116
+ * - For every marker row whose `space` is not in `loadedSpaces` →
117
+ * `orphanMarker`. The app-space marker is always loaded (`'app'` is
118
+ * in `loadedSpaces` by definition).
119
+ *
120
+ * Output is deterministic: violations are sorted first by `kind`
121
+ * (`declaredButUnmigrated` → `orphanMarker` → `orphanSpaceDir` →
122
+ * `hashMismatch` → `invariantsMismatch`) then by `spaceId`. Two callers
123
+ * passing equivalent inputs see byte-identical violation lists.
124
+ *
125
+ * Synchronous, pure, no I/O. **Does not import the extension descriptor**
126
+ * (the inputs are pre-resolved by the caller); the verifier reads only
127
+ * the user repo, not `node_modules`.
128
+ */
129
+ declare function verifyContractSpaces(inputs: VerifyContractSpacesInputs): VerifyContractSpacesResult;
130
+ //#endregion
131
+ export { VerifyContractSpacesResult as a, VerifyContractSpacesInputs as i, SpaceMarkerRecord as n, listContractSpaceDirectories as o, SpaceVerifierViolation as r, verifyContractSpaces as s, ContractSpaceHeadRecord as t };
132
+ //# sourceMappingURL=verify-contract-spaces-T0aiJlBS.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"verify-contract-spaces-T0aiJlBS.d.mts","names":[],"sources":["../src/verify-contract-spaces.ts"],"mappings":";;AAyBA;;;;AAEU;AAyCV;;;;AAEqB;AAQrB;;;;AAEqB;iBAvDC,4BAAA,CACpB,oBAAA,WACC,OAAO;;;;;;;UAyCO,uBAAA;EAAA,SACN,IAAA;EAAA,SACA,UAAU;AAAA;;;;;;UAQJ,iBAAA;EAAA,SACN,IAAA;EAAA,SACA,UAAU;AAAA;AAAA,UAGJ,0BAAA;EAiCL;;;;;;;EAAA,SAzBD,YAAA,EAAc,WAAA;EAiCV;;;;;EAAA,SA1BJ,eAAA;EAoCI;;;;;;;EAAA,SA3BJ,eAAA,EAAiB,WAAA,SAAoB,uBAAA;EAqCjC;;AAAW;AAG1B;EAHe,SA/BJ,iBAAA,EAAmB,WAAA,SAAoB,iBAAA;AAAA;AAAA,KAGtC,sBAAA;EAAA,SAEG,IAAA;EAAA,SACA,OAAA;EAAA,SACA,WAAA;AAAA;EAAA,SAGA,IAAA;EAAA,SACA,OAAA;EAAA,SACA,WAAA;AAAA;EAAA,SAGA,IAAA;EAAA,SACA,OAAA;EAAA,SACA,WAAA;AAAA;EAAA,SAGA,IAAA;EAAA,SACA,OAAA;EAAA,SACA,aAAA;EAAA,SACA,UAAA;EAAA,SACA,WAAA;AAAA;EAAA,SAGA,IAAA;EAAA,SACA,OAAA;EAAA,SACA,gBAAA;EAAA,SACA,gBAAA;EAAA,SACA,WAAA;AAAA;AAAA,KAGH,0BAAA;EAAA,SACG,EAAA;AAAA;EAAA,SACA,EAAA;EAAA,SAAoB,UAAA,WAAqB,sBAAsB;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAiC9D,oBAAA,CACd,MAAA,EAAQ,0BAAA,GACP,0BAA0B"}
package/package.json CHANGED
@@ -1,21 +1,21 @@
1
1
  {
2
2
  "name": "@prisma-next/migration-tools",
3
- "version": "0.11.0-dev.5",
3
+ "version": "0.11.0-dev.51",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "description": "On-disk migration persistence, hash verification, and chain reconstruction for Prisma Next",
8
8
  "dependencies": {
9
- "@prisma-next/contract": "0.11.0-dev.5",
10
- "@prisma-next/framework-components": "0.11.0-dev.5",
11
- "@prisma-next/utils": "0.11.0-dev.5",
9
+ "@prisma-next/contract": "0.11.0-dev.51",
10
+ "@prisma-next/framework-components": "0.11.0-dev.51",
11
+ "@prisma-next/utils": "0.11.0-dev.51",
12
12
  "arktype": "^2.2.0",
13
13
  "pathe": "^2.0.3",
14
14
  "prettier": "^3.8.3"
15
15
  },
16
16
  "devDependencies": {
17
- "@prisma-next/tsconfig": "0.11.0-dev.5",
18
- "@prisma-next/tsdown": "0.11.0-dev.5",
17
+ "@prisma-next/tsconfig": "0.11.0-dev.51",
18
+ "@prisma-next/tsdown": "0.11.0-dev.51",
19
19
  "tsdown": "0.22.0",
20
20
  "typescript": "5.9.3",
21
21
  "vitest": "4.1.6"
@@ -60,6 +60,18 @@
60
60
  "types": "./dist/exports/migration-graph.d.mts",
61
61
  "import": "./dist/exports/migration-graph.mjs"
62
62
  },
63
+ "./migration-list-types": {
64
+ "types": "./dist/exports/migration-list-types.d.mts",
65
+ "import": "./dist/exports/migration-list-types.mjs"
66
+ },
67
+ "./enumerate-migration-spaces": {
68
+ "types": "./dist/exports/enumerate-migration-spaces.d.mts",
69
+ "import": "./dist/exports/enumerate-migration-spaces.mjs"
70
+ },
71
+ "./migration-list-graph-topology": {
72
+ "types": "./dist/exports/migration-list-graph-topology.d.mts",
73
+ "import": "./dist/exports/migration-list-graph-topology.mjs"
74
+ },
63
75
  "./refs": {
64
76
  "types": "./dist/exports/refs.d.mts",
65
77
  "import": "./dist/exports/refs.mjs"