@valbuild/server 0.155.0 → 0.156.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.
Files changed (49) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/browser/dist/valbuild-server-browser.cjs.dev.js +2 -2
  3. package/browser/dist/valbuild-server-browser.cjs.prod.js +2 -2
  4. package/browser/dist/valbuild-server-browser.esm.js +2 -2
  5. package/dist/{ValOpsMemory-f000531b.cjs.prod.js → ValOpsMemory-6155aa9b.cjs.prod.js} +36 -4
  6. package/dist/{ValOpsMemory-1a0bc2dc.esm.js → ValOpsMemory-a00c3187.esm.js} +36 -4
  7. package/dist/{ValOpsMemory-6e829758.cjs.dev.js → ValOpsMemory-c1453e6a.cjs.dev.js} +36 -4
  8. package/dist/{createPrettierFormatter-5d830bdf.cjs.dev.js → createPrettierFormatter-8e60f596.cjs.prod.js} +1 -1
  9. package/dist/{createPrettierFormatter-b31bfbe5.cjs.prod.js → createPrettierFormatter-a9109288.cjs.dev.js} +1 -1
  10. package/dist/{createPrettierFormatter-ce478de1.esm.js → createPrettierFormatter-fb68e363.esm.js} +1 -1
  11. package/dist/{createValRuntime-f55300a0.esm.js → createValRuntime-4371c69b.esm.js} +6 -4
  12. package/dist/{createValRuntime-a519939b.cjs.prod.js → createValRuntime-5eb0336d.cjs.prod.js} +6 -4
  13. package/dist/{createValRuntime-1c7d891e.cjs.dev.js → createValRuntime-82f38747.cjs.dev.js} +6 -4
  14. package/dist/declarations/src/internal/index.d.ts +14 -0
  15. package/dist/declarations/src/proposalMerge/foldContent.d.ts +45 -0
  16. package/dist/declarations/src/proposalMerge/project.d.ts +124 -0
  17. package/dist/declarations/src/proposalMerge/threeWay.d.ts +51 -0
  18. package/dist/declarations/src/proposalMerge/validate.d.ts +16 -0
  19. package/dist/foldContent-2c218a30.cjs.dev.js +216 -0
  20. package/dist/foldContent-54f2c87c.cjs.prod.js +216 -0
  21. package/dist/foldContent-f065c19f.esm.js +214 -0
  22. package/dist/{inTabFetch-dae5dd87.esm.js → inTabFetch-22fc6815.esm.js} +1 -1
  23. package/dist/{inTabFetch-3d8ceb84.cjs.prod.js → inTabFetch-4efbd411.cjs.prod.js} +1 -1
  24. package/dist/{inTabFetch-75753a09.cjs.dev.js → inTabFetch-f5eedbd0.cjs.dev.js} +1 -1
  25. package/dist/{proposalJsonEntries-2cd6f6fe.esm.js → jsonValuesModule-12fc4bb0.esm.js} +2 -128
  26. package/dist/{proposalJsonEntries-d21900d4.cjs.dev.js → jsonValuesModule-6cf73937.cjs.prod.js} +2 -130
  27. package/dist/{proposalJsonEntries-d4623810.cjs.prod.js → jsonValuesModule-ed68a9de.cjs.dev.js} +2 -130
  28. package/dist/merge-5a8fc685.cjs.prod.js +1596 -0
  29. package/dist/merge-a21a9c78.cjs.dev.js +1596 -0
  30. package/dist/merge-eca8ec38.esm.js +1566 -0
  31. package/dist/proposalJsonEntries-4fcfab08.cjs.prod.js +135 -0
  32. package/dist/proposalJsonEntries-878ad5b6.cjs.dev.js +135 -0
  33. package/dist/proposalJsonEntries-e1b07915.esm.js +128 -0
  34. package/dist/{publishKit-395b92e5.cjs.prod.js → publishKit-0ec427f3.cjs.prod.js} +19 -22
  35. package/dist/{publishKit-04b6d255.esm.js → publishKit-a40062cd.esm.js} +11 -14
  36. package/dist/{publishKit-de4baed6.cjs.dev.js → publishKit-ffcaf2af.cjs.dev.js} +19 -22
  37. package/dist/{valRuntime-e922e372.cjs.prod.js → valRuntime-175e2032.cjs.prod.js} +21 -995
  38. package/dist/{valRuntime-7fd12e16.esm.js → valRuntime-18d4c693.esm.js} +11 -982
  39. package/dist/{valRuntime-d3cc6e0d.cjs.dev.js → valRuntime-c4b2e906.cjs.dev.js} +21 -995
  40. package/dist/valbuild-server.cjs.dev.js +45 -462
  41. package/dist/valbuild-server.cjs.prod.js +45 -462
  42. package/dist/valbuild-server.esm.js +15 -431
  43. package/internal/browser/dist/valbuild-server-internal-browser.cjs.dev.js +2 -2
  44. package/internal/browser/dist/valbuild-server-internal-browser.cjs.prod.js +2 -2
  45. package/internal/browser/dist/valbuild-server-internal-browser.esm.js +2 -2
  46. package/internal/dist/valbuild-server-internal.cjs.dev.js +20 -2
  47. package/internal/dist/valbuild-server-internal.cjs.prod.js +20 -2
  48. package/internal/dist/valbuild-server-internal.esm.js +20 -3
  49. package/package.json +3 -3
@@ -0,0 +1,16 @@
1
+ import { type ModuleFilePath, type Schema, type SelectorSource } from "@valbuild/core";
2
+ import { type SchemaError } from "@valbuild/shared/internal";
3
+ import type { ProjectModule } from "./project.js";
4
+ import type { Value } from "./threeWay.js";
5
+ export type { SchemaError } from "@valbuild/shared/internal";
6
+ /** What a schema reports about this content: see `schemaErrorsOf`. */
7
+ export declare const validateWith: (schema: Schema<SelectorSource>, moduleFilePath: ModuleFilePath, source: Value) => SchemaError[];
8
+ /**
9
+ * A module's errors for this content: its own `validate` where the schema was
10
+ * evaluated, custom rules and all; otherwise what its serialized schema says,
11
+ * which is every rule but a custom `.validate(...)` (a closure does not
12
+ * serialize).
13
+ */
14
+ export declare function validateModule(moduleFilePath: ModuleFilePath, module: ProjectModule, source: Value): SchemaError[];
15
+ /** The errors of a module's own Source: as evaluated where they were, else found here. */
16
+ export declare function errorsOfSource(moduleFilePath: ModuleFilePath, module: ProjectModule): SchemaError[];
@@ -0,0 +1,216 @@
1
+ 'use strict';
2
+
3
+ var merge = require('./merge-a21a9c78.cjs.dev.js');
4
+ require('typescript');
5
+ require('@valbuild/core/patch');
6
+ require('@valbuild/core/fp');
7
+ require('@valbuild/shared/internal');
8
+ require('path');
9
+ require('./jsonValuesModule-ed68a9de.cjs.dev.js');
10
+ require('@valbuild/core');
11
+ require('crypto');
12
+ require('fs');
13
+ require('node:vm');
14
+ require('node:module');
15
+
16
+ /**
17
+ * Content written back into a project's files: each module's content as it is
18
+ * recorded, written into its `.val.ts` (and a `.jsonValues()` record's
19
+ * `.val.json` entries) the way a save writes them.
20
+ *
21
+ * What connecting a project that has history needs (valbuild/home,
22
+ * `docs/plans/connect-with-history.md`, phase 6a). A project Val Build hosts
23
+ * keeps its content as each module's Source, never as file text: its
24
+ * `.val.ts` files still hold what the project was made with. Before its code
25
+ * can go into a repository, the content has to be in the files.
26
+ *
27
+ * The same writer a merge uses (`writeContent`, `writeEntries`): the smallest
28
+ * patch, through the AST operations a save uses, so the code around the
29
+ * content -- imports, comments, formatting, the schema -- is left exactly as
30
+ * it was. Then the project is loaded again, and every module must read back
31
+ * as exactly what was recorded, key order too. A module whose targeted write
32
+ * does not read back is written whole; one that still does not -- content its
33
+ * code computes rather than holds, say -- is refused by name. Nothing is
34
+ * guessed.
35
+ */
36
+
37
+ async function foldContent(input) {
38
+ const {
39
+ files,
40
+ content
41
+ } = input;
42
+ const ids = Object.keys(content).sort();
43
+ let project;
44
+ try {
45
+ project = merge.projectOf(files, await merge.loadProject(files));
46
+ } catch (error) {
47
+ return refusedWhole(`The project's own files could not be loaded`, error);
48
+ }
49
+ const refusals = [];
50
+ for (const id of ids) {
51
+ if (project.modules[id] === undefined || files[id.slice(1)] == null) {
52
+ refusals.push({
53
+ module: id,
54
+ reason: `The project's code has no module ${id}, so its content has nowhere to be written.`
55
+ });
56
+ }
57
+ }
58
+ if (refusals.length > 0) return {
59
+ status: "refused",
60
+ refusals
61
+ };
62
+
63
+ // First, the smallest patch for every module whose content changed. One
64
+ // that did not is not written at all: even a write of nothing re-prints a
65
+ // `.jsonValues()` module's `.val.ts`, which can change its code.
66
+ const changedIds = ids.filter(id => {
67
+ var _project$modules$id;
68
+ return JSON.stringify((_project$modules$id = project.modules[id]) === null || _project$modules$id === void 0 ? void 0 : _project$modules$id.source) !== JSON.stringify(content[id]);
69
+ });
70
+ const targeted = write(project, changedIds, content, "targeted");
71
+ if (targeted.status === "refused") return targeted;
72
+ let written = targeted.files;
73
+ let wrong = await readsBackWrong(written, ids, content);
74
+ if ("refused" in wrong) return wrong.refused;
75
+
76
+ // Then, whole, what did not read back: a record in another order, which
77
+ // the smallest patch does not see as a change.
78
+ if (wrong.modules.length > 0) {
79
+ const retried = {
80
+ ...written
81
+ };
82
+ const whole = write({
83
+ ...project,
84
+ files: retried
85
+ }, wrong.modules, content, "whole");
86
+ if (whole.status === "refused") return whole;
87
+ written = {
88
+ ...retried,
89
+ ...whole.files
90
+ };
91
+ wrong = await readsBackWrong(written, ids, content);
92
+ if ("refused" in wrong) return wrong.refused;
93
+ if (wrong.modules.length > 0) {
94
+ return {
95
+ status: "refused",
96
+ refusals: wrong.modules.map(id => ({
97
+ module: id,
98
+ reason: isJsonValues(project.modules[id]) ? `Its entries were written into their .val.json files, but ${id.slice(1)} lists them in another order than the one recorded. Reordering a .jsonValues() record's entries is not written yet.` : `Its content was written into ${id.slice(1)}, but the file reads back as something else. Its code computes its content rather than holding it, so it cannot be written back.`
99
+ }))
100
+ };
101
+ }
102
+ }
103
+ const tree = {};
104
+ const changed = [];
105
+ for (const file of new Set([...Object.keys(files), ...Object.keys(written)])) {
106
+ const before = files[file] ?? null;
107
+ const after = file in written ? written[file] : before;
108
+ if (after !== before) changed.push(file);
109
+ if (after !== null) tree[file] = after;
110
+ }
111
+ return {
112
+ status: "folded",
113
+ files: tree,
114
+ changed: changed.sort()
115
+ };
116
+ }
117
+
118
+ /**
119
+ * Writes each module's recorded content into its files. `targeted`: the
120
+ * smallest patch from what the file holds. `whole`: the content argument
121
+ * written whole, for a module whose smallest patch did not read back.
122
+ */
123
+ function write(project, ids, content, how) {
124
+ const files = {
125
+ ...project.files
126
+ };
127
+ const refusals = [];
128
+ for (const id of ids) {
129
+ const module = project.modules[id];
130
+ const file = id.slice(1);
131
+ const text = files[file];
132
+ if (module === undefined || text == null) continue;
133
+ try {
134
+ if (isJsonValues(module)) {
135
+ if (content[id] === null) {
136
+ // A nullable record recorded as null: the content argument is
137
+ // null, and its entries' files go.
138
+ files[file] = merge.writeContent(file, text, module.source, null);
139
+ for (const entry of Object.values(module.entries ?? {})) {
140
+ files[entry] = null;
141
+ }
142
+ } else {
143
+ Object.assign(files, merge.writeEntries(file, text,
144
+ // `whole`: every entry's file written anew, in the recorded
145
+ // order of its fields -- the smallest patch keeps a file whose
146
+ // content is the same in another order.
147
+ how === "whole" ? {} : files,
148
+ // An empty record has no entries yet: the first is added as a
149
+ // `c.json(...)` line and a file of its own, as a save adds it.
150
+ module.entries ?? {},
151
+ // `whole` runs on what `targeted` wrote, which already names
152
+ // the recorded entries.
153
+ how === "whole" ? content[id] : module.source, content[id], []));
154
+ }
155
+ } else {
156
+ files[file] = merge.writeContent(file, text, how === "targeted" ? module.source : undefined, content[id]);
157
+ }
158
+ } catch (error) {
159
+ refusals.push({
160
+ module: id,
161
+ reason: `Its content could not be written into ${file}: ${messageOf(error)}`
162
+ });
163
+ }
164
+ }
165
+ return refusals.length > 0 ? {
166
+ status: "refused",
167
+ refusals
168
+ } : {
169
+ status: "written",
170
+ files
171
+ };
172
+ }
173
+
174
+ /** The modules whose written files do not read back as exactly what was recorded. */
175
+ async function readsBackWrong(files, ids, content) {
176
+ let reloaded;
177
+ try {
178
+ reloaded = await merge.loadProject(files);
179
+ } catch (error) {
180
+ return {
181
+ refused: refusedWhole("The project could not be loaded with the content written in", error)
182
+ };
183
+ }
184
+ return {
185
+ modules: ids.filter(id => {
186
+ var _reloaded$modules$id;
187
+ return (
188
+ // Key order counts: a record's order is the editor's.
189
+ JSON.stringify((_reloaded$modules$id = reloaded.modules[id]) === null || _reloaded$modules$id === void 0 ? void 0 : _reloaded$modules$id.source) !== JSON.stringify(content[id])
190
+ );
191
+ })
192
+ };
193
+ }
194
+ function refusedWhole(what, error) {
195
+ return {
196
+ status: "refused",
197
+ refusals: [{
198
+ module: null,
199
+ reason: `${what}: ${messageOf(error)}`
200
+ }]
201
+ };
202
+ }
203
+ function messageOf(error) {
204
+ return error instanceof Error ? error.message : String(error);
205
+ }
206
+
207
+ /**
208
+ * A `.jsonValues()` record, by its schema: its content lives in `.val.json`
209
+ * entries, also while it has none yet (and so no entry files to tell it by).
210
+ */
211
+ function isJsonValues(module) {
212
+ const schema = module === null || module === void 0 ? void 0 : module.schema;
213
+ return schema !== undefined && schema.type === "record" && "jsonValues" in schema && schema.jsonValues === true;
214
+ }
215
+
216
+ exports.foldContent = foldContent;
@@ -0,0 +1,216 @@
1
+ 'use strict';
2
+
3
+ var merge = require('./merge-5a8fc685.cjs.prod.js');
4
+ require('typescript');
5
+ require('@valbuild/core/patch');
6
+ require('@valbuild/core/fp');
7
+ require('@valbuild/shared/internal');
8
+ require('path');
9
+ require('./jsonValuesModule-6cf73937.cjs.prod.js');
10
+ require('@valbuild/core');
11
+ require('crypto');
12
+ require('fs');
13
+ require('node:vm');
14
+ require('node:module');
15
+
16
+ /**
17
+ * Content written back into a project's files: each module's content as it is
18
+ * recorded, written into its `.val.ts` (and a `.jsonValues()` record's
19
+ * `.val.json` entries) the way a save writes them.
20
+ *
21
+ * What connecting a project that has history needs (valbuild/home,
22
+ * `docs/plans/connect-with-history.md`, phase 6a). A project Val Build hosts
23
+ * keeps its content as each module's Source, never as file text: its
24
+ * `.val.ts` files still hold what the project was made with. Before its code
25
+ * can go into a repository, the content has to be in the files.
26
+ *
27
+ * The same writer a merge uses (`writeContent`, `writeEntries`): the smallest
28
+ * patch, through the AST operations a save uses, so the code around the
29
+ * content -- imports, comments, formatting, the schema -- is left exactly as
30
+ * it was. Then the project is loaded again, and every module must read back
31
+ * as exactly what was recorded, key order too. A module whose targeted write
32
+ * does not read back is written whole; one that still does not -- content its
33
+ * code computes rather than holds, say -- is refused by name. Nothing is
34
+ * guessed.
35
+ */
36
+
37
+ async function foldContent(input) {
38
+ const {
39
+ files,
40
+ content
41
+ } = input;
42
+ const ids = Object.keys(content).sort();
43
+ let project;
44
+ try {
45
+ project = merge.projectOf(files, await merge.loadProject(files));
46
+ } catch (error) {
47
+ return refusedWhole(`The project's own files could not be loaded`, error);
48
+ }
49
+ const refusals = [];
50
+ for (const id of ids) {
51
+ if (project.modules[id] === undefined || files[id.slice(1)] == null) {
52
+ refusals.push({
53
+ module: id,
54
+ reason: `The project's code has no module ${id}, so its content has nowhere to be written.`
55
+ });
56
+ }
57
+ }
58
+ if (refusals.length > 0) return {
59
+ status: "refused",
60
+ refusals
61
+ };
62
+
63
+ // First, the smallest patch for every module whose content changed. One
64
+ // that did not is not written at all: even a write of nothing re-prints a
65
+ // `.jsonValues()` module's `.val.ts`, which can change its code.
66
+ const changedIds = ids.filter(id => {
67
+ var _project$modules$id;
68
+ return JSON.stringify((_project$modules$id = project.modules[id]) === null || _project$modules$id === void 0 ? void 0 : _project$modules$id.source) !== JSON.stringify(content[id]);
69
+ });
70
+ const targeted = write(project, changedIds, content, "targeted");
71
+ if (targeted.status === "refused") return targeted;
72
+ let written = targeted.files;
73
+ let wrong = await readsBackWrong(written, ids, content);
74
+ if ("refused" in wrong) return wrong.refused;
75
+
76
+ // Then, whole, what did not read back: a record in another order, which
77
+ // the smallest patch does not see as a change.
78
+ if (wrong.modules.length > 0) {
79
+ const retried = {
80
+ ...written
81
+ };
82
+ const whole = write({
83
+ ...project,
84
+ files: retried
85
+ }, wrong.modules, content, "whole");
86
+ if (whole.status === "refused") return whole;
87
+ written = {
88
+ ...retried,
89
+ ...whole.files
90
+ };
91
+ wrong = await readsBackWrong(written, ids, content);
92
+ if ("refused" in wrong) return wrong.refused;
93
+ if (wrong.modules.length > 0) {
94
+ return {
95
+ status: "refused",
96
+ refusals: wrong.modules.map(id => ({
97
+ module: id,
98
+ reason: isJsonValues(project.modules[id]) ? `Its entries were written into their .val.json files, but ${id.slice(1)} lists them in another order than the one recorded. Reordering a .jsonValues() record's entries is not written yet.` : `Its content was written into ${id.slice(1)}, but the file reads back as something else. Its code computes its content rather than holding it, so it cannot be written back.`
99
+ }))
100
+ };
101
+ }
102
+ }
103
+ const tree = {};
104
+ const changed = [];
105
+ for (const file of new Set([...Object.keys(files), ...Object.keys(written)])) {
106
+ const before = files[file] ?? null;
107
+ const after = file in written ? written[file] : before;
108
+ if (after !== before) changed.push(file);
109
+ if (after !== null) tree[file] = after;
110
+ }
111
+ return {
112
+ status: "folded",
113
+ files: tree,
114
+ changed: changed.sort()
115
+ };
116
+ }
117
+
118
+ /**
119
+ * Writes each module's recorded content into its files. `targeted`: the
120
+ * smallest patch from what the file holds. `whole`: the content argument
121
+ * written whole, for a module whose smallest patch did not read back.
122
+ */
123
+ function write(project, ids, content, how) {
124
+ const files = {
125
+ ...project.files
126
+ };
127
+ const refusals = [];
128
+ for (const id of ids) {
129
+ const module = project.modules[id];
130
+ const file = id.slice(1);
131
+ const text = files[file];
132
+ if (module === undefined || text == null) continue;
133
+ try {
134
+ if (isJsonValues(module)) {
135
+ if (content[id] === null) {
136
+ // A nullable record recorded as null: the content argument is
137
+ // null, and its entries' files go.
138
+ files[file] = merge.writeContent(file, text, module.source, null);
139
+ for (const entry of Object.values(module.entries ?? {})) {
140
+ files[entry] = null;
141
+ }
142
+ } else {
143
+ Object.assign(files, merge.writeEntries(file, text,
144
+ // `whole`: every entry's file written anew, in the recorded
145
+ // order of its fields -- the smallest patch keeps a file whose
146
+ // content is the same in another order.
147
+ how === "whole" ? {} : files,
148
+ // An empty record has no entries yet: the first is added as a
149
+ // `c.json(...)` line and a file of its own, as a save adds it.
150
+ module.entries ?? {},
151
+ // `whole` runs on what `targeted` wrote, which already names
152
+ // the recorded entries.
153
+ how === "whole" ? content[id] : module.source, content[id], []));
154
+ }
155
+ } else {
156
+ files[file] = merge.writeContent(file, text, how === "targeted" ? module.source : undefined, content[id]);
157
+ }
158
+ } catch (error) {
159
+ refusals.push({
160
+ module: id,
161
+ reason: `Its content could not be written into ${file}: ${messageOf(error)}`
162
+ });
163
+ }
164
+ }
165
+ return refusals.length > 0 ? {
166
+ status: "refused",
167
+ refusals
168
+ } : {
169
+ status: "written",
170
+ files
171
+ };
172
+ }
173
+
174
+ /** The modules whose written files do not read back as exactly what was recorded. */
175
+ async function readsBackWrong(files, ids, content) {
176
+ let reloaded;
177
+ try {
178
+ reloaded = await merge.loadProject(files);
179
+ } catch (error) {
180
+ return {
181
+ refused: refusedWhole("The project could not be loaded with the content written in", error)
182
+ };
183
+ }
184
+ return {
185
+ modules: ids.filter(id => {
186
+ var _reloaded$modules$id;
187
+ return (
188
+ // Key order counts: a record's order is the editor's.
189
+ JSON.stringify((_reloaded$modules$id = reloaded.modules[id]) === null || _reloaded$modules$id === void 0 ? void 0 : _reloaded$modules$id.source) !== JSON.stringify(content[id])
190
+ );
191
+ })
192
+ };
193
+ }
194
+ function refusedWhole(what, error) {
195
+ return {
196
+ status: "refused",
197
+ refusals: [{
198
+ module: null,
199
+ reason: `${what}: ${messageOf(error)}`
200
+ }]
201
+ };
202
+ }
203
+ function messageOf(error) {
204
+ return error instanceof Error ? error.message : String(error);
205
+ }
206
+
207
+ /**
208
+ * A `.jsonValues()` record, by its schema: its content lives in `.val.json`
209
+ * entries, also while it has none yet (and so no entry files to tell it by).
210
+ */
211
+ function isJsonValues(module) {
212
+ const schema = module === null || module === void 0 ? void 0 : module.schema;
213
+ return schema !== undefined && schema.type === "record" && "jsonValues" in schema && schema.jsonValues === true;
214
+ }
215
+
216
+ exports.foldContent = foldContent;
@@ -0,0 +1,214 @@
1
+ import { p as projectOf, o as loadProject, w as writeContent, q as writeEntries } from './merge-eca8ec38.esm.js';
2
+ import 'typescript';
3
+ import '@valbuild/core/patch';
4
+ import '@valbuild/core/fp';
5
+ import '@valbuild/shared/internal';
6
+ import 'path';
7
+ import './jsonValuesModule-12fc4bb0.esm.js';
8
+ import '@valbuild/core';
9
+ import 'crypto';
10
+ import 'fs';
11
+ import 'node:vm';
12
+ import 'node:module';
13
+
14
+ /**
15
+ * Content written back into a project's files: each module's content as it is
16
+ * recorded, written into its `.val.ts` (and a `.jsonValues()` record's
17
+ * `.val.json` entries) the way a save writes them.
18
+ *
19
+ * What connecting a project that has history needs (valbuild/home,
20
+ * `docs/plans/connect-with-history.md`, phase 6a). A project Val Build hosts
21
+ * keeps its content as each module's Source, never as file text: its
22
+ * `.val.ts` files still hold what the project was made with. Before its code
23
+ * can go into a repository, the content has to be in the files.
24
+ *
25
+ * The same writer a merge uses (`writeContent`, `writeEntries`): the smallest
26
+ * patch, through the AST operations a save uses, so the code around the
27
+ * content -- imports, comments, formatting, the schema -- is left exactly as
28
+ * it was. Then the project is loaded again, and every module must read back
29
+ * as exactly what was recorded, key order too. A module whose targeted write
30
+ * does not read back is written whole; one that still does not -- content its
31
+ * code computes rather than holds, say -- is refused by name. Nothing is
32
+ * guessed.
33
+ */
34
+
35
+ async function foldContent(input) {
36
+ const {
37
+ files,
38
+ content
39
+ } = input;
40
+ const ids = Object.keys(content).sort();
41
+ let project;
42
+ try {
43
+ project = projectOf(files, await loadProject(files));
44
+ } catch (error) {
45
+ return refusedWhole(`The project's own files could not be loaded`, error);
46
+ }
47
+ const refusals = [];
48
+ for (const id of ids) {
49
+ if (project.modules[id] === undefined || files[id.slice(1)] == null) {
50
+ refusals.push({
51
+ module: id,
52
+ reason: `The project's code has no module ${id}, so its content has nowhere to be written.`
53
+ });
54
+ }
55
+ }
56
+ if (refusals.length > 0) return {
57
+ status: "refused",
58
+ refusals
59
+ };
60
+
61
+ // First, the smallest patch for every module whose content changed. One
62
+ // that did not is not written at all: even a write of nothing re-prints a
63
+ // `.jsonValues()` module's `.val.ts`, which can change its code.
64
+ const changedIds = ids.filter(id => {
65
+ var _project$modules$id;
66
+ return JSON.stringify((_project$modules$id = project.modules[id]) === null || _project$modules$id === void 0 ? void 0 : _project$modules$id.source) !== JSON.stringify(content[id]);
67
+ });
68
+ const targeted = write(project, changedIds, content, "targeted");
69
+ if (targeted.status === "refused") return targeted;
70
+ let written = targeted.files;
71
+ let wrong = await readsBackWrong(written, ids, content);
72
+ if ("refused" in wrong) return wrong.refused;
73
+
74
+ // Then, whole, what did not read back: a record in another order, which
75
+ // the smallest patch does not see as a change.
76
+ if (wrong.modules.length > 0) {
77
+ const retried = {
78
+ ...written
79
+ };
80
+ const whole = write({
81
+ ...project,
82
+ files: retried
83
+ }, wrong.modules, content, "whole");
84
+ if (whole.status === "refused") return whole;
85
+ written = {
86
+ ...retried,
87
+ ...whole.files
88
+ };
89
+ wrong = await readsBackWrong(written, ids, content);
90
+ if ("refused" in wrong) return wrong.refused;
91
+ if (wrong.modules.length > 0) {
92
+ return {
93
+ status: "refused",
94
+ refusals: wrong.modules.map(id => ({
95
+ module: id,
96
+ reason: isJsonValues(project.modules[id]) ? `Its entries were written into their .val.json files, but ${id.slice(1)} lists them in another order than the one recorded. Reordering a .jsonValues() record's entries is not written yet.` : `Its content was written into ${id.slice(1)}, but the file reads back as something else. Its code computes its content rather than holding it, so it cannot be written back.`
97
+ }))
98
+ };
99
+ }
100
+ }
101
+ const tree = {};
102
+ const changed = [];
103
+ for (const file of new Set([...Object.keys(files), ...Object.keys(written)])) {
104
+ const before = files[file] ?? null;
105
+ const after = file in written ? written[file] : before;
106
+ if (after !== before) changed.push(file);
107
+ if (after !== null) tree[file] = after;
108
+ }
109
+ return {
110
+ status: "folded",
111
+ files: tree,
112
+ changed: changed.sort()
113
+ };
114
+ }
115
+
116
+ /**
117
+ * Writes each module's recorded content into its files. `targeted`: the
118
+ * smallest patch from what the file holds. `whole`: the content argument
119
+ * written whole, for a module whose smallest patch did not read back.
120
+ */
121
+ function write(project, ids, content, how) {
122
+ const files = {
123
+ ...project.files
124
+ };
125
+ const refusals = [];
126
+ for (const id of ids) {
127
+ const module = project.modules[id];
128
+ const file = id.slice(1);
129
+ const text = files[file];
130
+ if (module === undefined || text == null) continue;
131
+ try {
132
+ if (isJsonValues(module)) {
133
+ if (content[id] === null) {
134
+ // A nullable record recorded as null: the content argument is
135
+ // null, and its entries' files go.
136
+ files[file] = writeContent(file, text, module.source, null);
137
+ for (const entry of Object.values(module.entries ?? {})) {
138
+ files[entry] = null;
139
+ }
140
+ } else {
141
+ Object.assign(files, writeEntries(file, text,
142
+ // `whole`: every entry's file written anew, in the recorded
143
+ // order of its fields -- the smallest patch keeps a file whose
144
+ // content is the same in another order.
145
+ how === "whole" ? {} : files,
146
+ // An empty record has no entries yet: the first is added as a
147
+ // `c.json(...)` line and a file of its own, as a save adds it.
148
+ module.entries ?? {},
149
+ // `whole` runs on what `targeted` wrote, which already names
150
+ // the recorded entries.
151
+ how === "whole" ? content[id] : module.source, content[id], []));
152
+ }
153
+ } else {
154
+ files[file] = writeContent(file, text, how === "targeted" ? module.source : undefined, content[id]);
155
+ }
156
+ } catch (error) {
157
+ refusals.push({
158
+ module: id,
159
+ reason: `Its content could not be written into ${file}: ${messageOf(error)}`
160
+ });
161
+ }
162
+ }
163
+ return refusals.length > 0 ? {
164
+ status: "refused",
165
+ refusals
166
+ } : {
167
+ status: "written",
168
+ files
169
+ };
170
+ }
171
+
172
+ /** The modules whose written files do not read back as exactly what was recorded. */
173
+ async function readsBackWrong(files, ids, content) {
174
+ let reloaded;
175
+ try {
176
+ reloaded = await loadProject(files);
177
+ } catch (error) {
178
+ return {
179
+ refused: refusedWhole("The project could not be loaded with the content written in", error)
180
+ };
181
+ }
182
+ return {
183
+ modules: ids.filter(id => {
184
+ var _reloaded$modules$id;
185
+ return (
186
+ // Key order counts: a record's order is the editor's.
187
+ JSON.stringify((_reloaded$modules$id = reloaded.modules[id]) === null || _reloaded$modules$id === void 0 ? void 0 : _reloaded$modules$id.source) !== JSON.stringify(content[id])
188
+ );
189
+ })
190
+ };
191
+ }
192
+ function refusedWhole(what, error) {
193
+ return {
194
+ status: "refused",
195
+ refusals: [{
196
+ module: null,
197
+ reason: `${what}: ${messageOf(error)}`
198
+ }]
199
+ };
200
+ }
201
+ function messageOf(error) {
202
+ return error instanceof Error ? error.message : String(error);
203
+ }
204
+
205
+ /**
206
+ * A `.jsonValues()` record, by its schema: its content lives in `.val.json`
207
+ * entries, also while it has none yet (and so no entry files to tell it by).
208
+ */
209
+ function isJsonValues(module) {
210
+ const schema = module === null || module === void 0 ? void 0 : module.schema;
211
+ return schema !== undefined && schema.type === "record" && "jsonValues" in schema && schema.jsonValues === true;
212
+ }
213
+
214
+ export { foldContent };
@@ -1,5 +1,5 @@
1
1
  import { DEFAULT_CONTENT_HOST } from '@valbuild/core';
2
- import { o as ValOpsMemory, L as bytesToBase64, V as ValServer } from './ValOpsMemory-1a0bc2dc.esm.js';
2
+ import { o as ValOpsMemory, L as bytesToBase64, V as ValServer } from './ValOpsMemory-a00c3187.esm.js';
3
3
  import { c as createValRequestHandler } from './ValRouterCore-f7d6394e.esm.js';
4
4
 
5
5
  const NO_SESSIONS = "Val: sessions are not available here -- this server runs in the browser, " + "has no secret to sign them with, and serves without authenticating.";
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var core = require('@valbuild/core');
4
- var ValOpsMemory = require('./ValOpsMemory-f000531b.cjs.prod.js');
4
+ var ValOpsMemory = require('./ValOpsMemory-6155aa9b.cjs.prod.js');
5
5
  var ValRouterCore = require('./ValRouterCore-65bb6323.cjs.prod.js');
6
6
 
7
7
  const NO_SESSIONS = "Val: sessions are not available here -- this server runs in the browser, " + "has no secret to sign them with, and serves without authenticating.";
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var core = require('@valbuild/core');
4
- var ValOpsMemory = require('./ValOpsMemory-6e829758.cjs.dev.js');
4
+ var ValOpsMemory = require('./ValOpsMemory-c1453e6a.cjs.dev.js');
5
5
  var ValRouterCore = require('./ValRouterCore-adb69698.cjs.dev.js');
6
6
 
7
7
  const NO_SESSIONS = "Val: sessions are not available here -- this server runs in the browser, " + "has no secret to sign them with, and serves without authenticating.";