goatly 0.1.0 → 0.1.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 (158) hide show
  1. package/README.md +853 -1
  2. package/bin/goatly.js +91 -0
  3. package/dist/cli/index.js +234 -0
  4. package/dist/compat/gatsby-node-entry.js +16 -0
  5. package/dist/compat/gatsby-package.json +6 -0
  6. package/dist/compat/md5-file.js +47 -0
  7. package/dist/compat-apis.json +72 -0
  8. package/dist/compat-apis.json.js +62 -0
  9. package/dist/graphql.js +3 -0
  10. package/dist/node/actions/index.js +619 -0
  11. package/dist/node/actions/job-store.js +180 -0
  12. package/dist/node/actions/jobs.js +174 -0
  13. package/dist/node/api-runner.js +258 -0
  14. package/dist/node/bootstrap.js +539 -0
  15. package/dist/node/build/assets.js +110 -0
  16. package/dist/node/build/incremental.js +208 -0
  17. package/dist/node/build/index.js +752 -0
  18. package/dist/node/build/memory-governor.js +448 -0
  19. package/dist/node/build/output-dirs.js +70 -0
  20. package/dist/node/build/page-data.js +40 -0
  21. package/dist/node/build/page-files.js +54 -0
  22. package/dist/node/build/partitioned-state.js +258 -0
  23. package/dist/node/build/query-worker.js +536 -0
  24. package/dist/node/build/query-workers.js +832 -0
  25. package/dist/node/build/render-worker.js +131 -0
  26. package/dist/node/build/state.js +24 -0
  27. package/dist/node/build/timing-breakdown.js +155 -0
  28. package/dist/node/build/worker-pool.js +236 -0
  29. package/dist/node/cache-layout.js +99 -0
  30. package/dist/node/cache.js +1430 -0
  31. package/dist/node/compat/chokidar-shim.js +148 -0
  32. package/dist/node/compat/core-utils-lmdb.js +145 -0
  33. package/dist/node/compat/create-node-id-shim.js +1 -0
  34. package/dist/node/compat/module-hooks.js +245 -0
  35. package/dist/node/compat/redux-shim.js +1 -0
  36. package/dist/node/config/index.js +162 -0
  37. package/dist/node/config/not-found-pages.js +95 -0
  38. package/dist/node/datastore/codec.js +144 -0
  39. package/dist/node/datastore/index.js +127 -0
  40. package/dist/node/datastore/logfile/cache.js +89 -0
  41. package/dist/node/datastore/logfile/index.js +1329 -0
  42. package/dist/node/datastore/logfile/record.js +71 -0
  43. package/dist/node/datastore/types.js +1 -0
  44. package/dist/node/develop/express-compat.js +207 -0
  45. package/dist/node/develop/graphiql.js +22 -0
  46. package/dist/node/develop/hydration-report.js +41 -0
  47. package/dist/node/develop/index.js +618 -0
  48. package/dist/node/develop/local-url.js +10 -0
  49. package/dist/node/develop/ssr-runner.js +66 -0
  50. package/dist/node/gql.js +5 -0
  51. package/dist/node/index.js +15 -0
  52. package/dist/node/info.js +270 -0
  53. package/dist/node/internal-plugins/data-bridge.js +196 -0
  54. package/dist/node/internal-plugins/page-creator.js +318 -0
  55. package/dist/node/loader/import-module.js +160 -0
  56. package/dist/node/node-helpers.js +66 -0
  57. package/dist/node/not-found-pages.js +220 -0
  58. package/dist/node/plugins/api-list.js +12 -0
  59. package/dist/node/plugins/load-plugins.js +427 -0
  60. package/dist/node/plugins/resolve-plugin.js +79 -0
  61. package/dist/node/plugins/static-exports.js +64 -0
  62. package/dist/node/query/compile.js +326 -0
  63. package/dist/node/query/dependencies.js +45 -0
  64. package/dist/node/query/execute.js +417 -0
  65. package/dist/node/query/extract.js +372 -0
  66. package/dist/node/query/gql-tada-introspection.js +124 -0
  67. package/dist/node/query/graphql-env.js +231 -0
  68. package/dist/node/query/graphql-runner.js +79 -0
  69. package/dist/node/redux/actions.js +1 -0
  70. package/dist/node/redux/index.js +133 -0
  71. package/dist/node/redux/node-changes.js +128 -0
  72. package/dist/node/redux/reducers.js +193 -0
  73. package/dist/node/redux/state.js +71 -0
  74. package/dist/node/renderer.js +124 -0
  75. package/dist/node/reporter-transports.js +259 -0
  76. package/dist/node/reporter.js +497 -0
  77. package/dist/node/schema/date.js +539 -0
  78. package/dist/node/schema/extensions.js +315 -0
  79. package/dist/node/schema/index.js +542 -0
  80. package/dist/node/schema/infer.js +388 -0
  81. package/dist/node/schema/inline-objects.js +128 -0
  82. package/dist/node/schema/naming.js +20 -0
  83. package/dist/node/schema/node-model.js +207 -0
  84. package/dist/node/schema/query-engine.js +1808 -0
  85. package/dist/node/schema/resolver-kinds.js +7 -0
  86. package/dist/node/schema/resolvers.js +273 -0
  87. package/dist/node/schema/type-builders.js +45 -0
  88. package/dist/node/schema/types.js +241 -0
  89. package/dist/node/serve.js +90 -0
  90. package/dist/node/site-page-nodes.js +277 -0
  91. package/dist/node/types.js +1 -0
  92. package/dist/node/utils/content-digest.js +107 -0
  93. package/dist/node/utils/create-node-id.js +51 -0
  94. package/dist/node/utils/env.js +24 -0
  95. package/dist/node/utils/fs-retry.js +74 -0
  96. package/dist/node/utils/memory-signals.js +410 -0
  97. package/dist/node/utils/memory.js +400 -0
  98. package/dist/node/utils/murmurhash.js +32 -0
  99. package/dist/node/utils/parser.js +33 -0
  100. package/dist/node/utils/partition-hash.js +19 -0
  101. package/dist/node/utils/paths.js +122 -0
  102. package/dist/node/utils/resolver.js +48 -0
  103. package/dist/node/utils/runtime.js +5 -0
  104. package/dist/node/utils/serializable.js +41 -0
  105. package/dist/node/utils/strings.js +41 -0
  106. package/dist/node/vite/config.js +289 -0
  107. package/dist/node/vite/paths.js +12 -0
  108. package/dist/node/vite/plugin-deps.js +232 -0
  109. package/dist/node/vite/plugin.js +281 -0
  110. package/dist/node/vite/react-compiler.js +362 -0
  111. package/dist/node/vite/source-files.js +83 -0
  112. package/dist/node/vite/static-image.js +136 -0
  113. package/dist/node/vite/styled-components.js +73 -0
  114. package/dist/node/vite/transforms.js +299 -0
  115. package/dist/node/wasm-data/host.js +406 -0
  116. package/dist/node/wasm-data/lower.js +406 -0
  117. package/dist/node/wasm-data/runner.js +79 -0
  118. package/dist/node/wasm-data/worker.js +95 -0
  119. package/dist/reporter.js +5 -0
  120. package/dist/runtime/api-runner.js +81 -0
  121. package/dist/runtime/app.js +256 -0
  122. package/dist/runtime/client-entry.js +4 -0
  123. package/dist/runtime/client.js +383 -0
  124. package/dist/runtime/default-html.js +31 -0
  125. package/dist/runtime/head-markup.js +100 -0
  126. package/dist/runtime/head-sync.js +101 -0
  127. package/dist/runtime/head-tags.js +45 -0
  128. package/dist/runtime/hydration-report.js +111 -0
  129. package/dist/runtime/index.js +12 -0
  130. package/dist/runtime/link.js +293 -0
  131. package/dist/runtime/loader.js +449 -0
  132. package/dist/runtime/not-found.js +61 -0
  133. package/dist/runtime/page-renderer.js +35 -0
  134. package/dist/runtime/preamble.js +47 -0
  135. package/dist/runtime/react-compat.js +14 -0
  136. package/dist/runtime/renderer-shared.js +10 -0
  137. package/dist/runtime/renderers/react-client.js +14 -0
  138. package/dist/runtime/renderers/react-server.js +99 -0
  139. package/dist/runtime/renderers/react.js +17 -0
  140. package/dist/runtime/renderers/types.js +1 -0
  141. package/dist/runtime/root-error-handlers.js +29 -0
  142. package/dist/runtime/router/history.js +132 -0
  143. package/dist/runtime/router/index.js +240 -0
  144. package/dist/runtime/router/utils.js +118 -0
  145. package/dist/runtime/script.js +110 -0
  146. package/dist/runtime/scroll.js +40 -0
  147. package/dist/runtime/slice.js +11 -0
  148. package/dist/runtime/ssr.js +240 -0
  149. package/dist/runtime/static-query.js +47 -0
  150. package/dist/utils.js +4 -0
  151. package/package.json +105 -3
  152. package/types/graphql-tada.d.ts +223 -0
  153. package/types/graphql.d.ts +2 -0
  154. package/types/index.d.ts +2287 -0
  155. package/types/node.d.ts +380 -0
  156. package/types/reporter.d.ts +187 -0
  157. package/types/router.d.ts +102 -0
  158. package/types/vendor/gql-tada.d.ts +2529 -0
@@ -0,0 +1,258 @@
1
+ import { writeFileIfChanged } from "../cache-layout.js";
2
+ import { partitionHash } from "../utils/partition-hash.js";
3
+ import { hashString } from "./incremental.js";
4
+ import path from "node:path";
5
+ import fs from "node:fs";
6
+ // src/node/build/partitioned-state.ts
7
+ /**
8
+ * The build state (see `cache-layout.ts`): a directory with a
9
+ * small head file and the large maps split by a hash of their keys into partition files. A build
10
+ * that changes a few entries writes a few partitions, not the whole map: when `.cache` travels as
11
+ * image layers, overlayfs copies every modified file into the new layer as a whole.
12
+ *
13
+ * - Partition files are immutable and content-addressed (`nodes-07-<hash>.json`): new content goes
14
+ * to a new file, an unchanged partition is not touched.
15
+ * - A map has a power of two of partitions (at least 64), about 256 KB each: the count follows the
16
+ * map's size (see `targetBytes`), a change of it writes every partition again under a new head.
17
+ * - The head lists the partition files and is written last (tmp + rename): until then the previous
18
+ * head and the files it lists are intact, so a crash leaves the previous state.
19
+ * - Files the head does not list are deleted after it is written (by the next completed write when
20
+ * a write leaves them, see `keepUnlisted`).
21
+ * - Reading verifies every listed file against the hash in its name. Any problem: no state.
22
+ */
23
+ /** the fewest partitions per map; their count is a power of two (a changed entry rewrites one) */
24
+ const MIN_PARTITION_COUNT = 64;
25
+ /** (indexes in file names have at most 4 hex digits) */
26
+ const MAX_PARTITION_COUNT = 65536;
27
+ const HEAD_FILE = "head.json";
28
+ const PARTITION_FILE = /^([a-z]+)-([0-9a-f]{2,4})-([0-9a-f]{16})\.json$/;
29
+ /**
30
+ * The partition size a map aims at, so a changed entry rewrites about this much however large the
31
+ * map grows. The count changes (and every partition is written again, once) only when the average
32
+ * leaves [target / 2, target * 2], to the count that puts it within a factor of √2 of the target
33
+ * (halfway to either bound): a map that stays near a bound does not switch back and forth.
34
+ */
35
+ let targetBytes = 262144;
36
+ /** @internal (exported for tests) */
37
+ function setPartitionTargetBytes(bytes) {
38
+ targetBytes = bytes;
39
+ }
40
+ function isPartitionCount(n) {
41
+ return n >= 64 && n <= MAX_PARTITION_COUNT && (n & n - 1) === 0;
42
+ }
43
+ /** @internal (exported for tests) the partition count for `bytes` of a map written with `count` */
44
+ function partitionCountFor(bytes, count) {
45
+ const average = bytes / count;
46
+ if (average >= targetBytes / 2 && average <= targetBytes * 2) return count;
47
+ let next = 64;
48
+ while (next < MAX_PARTITION_COUNT && bytes / next > targetBytes * Math.SQRT2) next *= 2;
49
+ return next;
50
+ }
51
+ /**
52
+ * The partitions each map (by directory) was last written with. A partition with the same keys and
53
+ * values (by identity: entries are replaced, never changed in place) is not serialized again: a
54
+ * build writes the large node map two or three times.
55
+ */
56
+ const lastWritten = /* @__PURE__ */ new Map();
57
+ function sameItems(a, b) {
58
+ if (a.length !== b.length) return false;
59
+ for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
60
+ return true;
61
+ }
62
+ /** `entries` split into `count` partitions; `previous` are reused, new ones are not written yet */
63
+ function split(dir, map, entries, count, previous) {
64
+ const buckets = Array.from({ length: count }, () => []);
65
+ for (const key in entries) buckets[partitionHash(key) % count].push(key);
66
+ if (previous?.length !== count) previous = void 0;
67
+ const digits = Math.max(2, (count - 1).toString(16).length);
68
+ return buckets.map((keys, i) => {
69
+ if (!keys.length) return null;
70
+ const values = keys.map((key) => entries[key]);
71
+ const before = previous?.[i];
72
+ if (before && sameItems(before.keys, keys) && sameItems(before.values, values) && fs.existsSync(path.join(dir, before.name))) return before;
73
+ const partition = Object.create(null);
74
+ for (const key of [...keys].sort()) partition[key] = entries[key];
75
+ const data = JSON.stringify(partition);
76
+ return {
77
+ keys,
78
+ values,
79
+ name: `${map}-${i.toString(16).padStart(digits, "0")}-${hashString(data)}.json`,
80
+ bytes: Buffer.byteLength(data),
81
+ data
82
+ };
83
+ });
84
+ }
85
+ /**
86
+ * Writes the partitions of `entries` that do not exist yet; returns their names by partition (null:
87
+ * no entries). Keys are sorted within a partition: the same entries give the same files whatever
88
+ * order the map was built in. `previous`: the names the map was last written with, whose count is
89
+ * kept while the map's size stays in its range (see `targetBytes`).
90
+ */
91
+ function writePartitions(dir, map, entries, previous) {
92
+ const last = lastWritten.get(path.join(dir, map));
93
+ const count = Array.isArray(previous) && isPartitionCount(previous.length) ? previous.length : 64;
94
+ let partitions = split(dir, map, entries, count, last);
95
+ const next = partitionCountFor(partitions.reduce((sum, p) => sum + (p?.bytes ?? 0), 0), count);
96
+ if (next !== count) partitions = split(dir, map, entries, next, last);
97
+ for (const p of partitions) if (p?.data !== void 0) {
98
+ writeFileIfChanged(path.join(dir, p.name), p.data, {
99
+ atomic: true,
100
+ contentAddressed: true
101
+ });
102
+ delete p.data;
103
+ }
104
+ lastWritten.set(path.join(dir, map), partitions);
105
+ return partitions.map((p) => p?.name ?? null);
106
+ }
107
+ /** the entries of the listed partitions (without prototype), or null when one is missing or broken */
108
+ function readPartitions(dir, map, names) {
109
+ if (!Array.isArray(names) || !isPartitionCount(names.length)) return null;
110
+ const bodies = [];
111
+ for (let i = 0; i < names.length; i++) {
112
+ const name = names[i];
113
+ if (name === null) continue;
114
+ const match = typeof name === "string" ? PARTITION_FILE.exec(name) : null;
115
+ if (!match || match[1] !== map || parseInt(match[2], 16) !== i) return null;
116
+ const file = path.join(dir, name);
117
+ let data;
118
+ try {
119
+ data = fs.readFileSync(file, "utf8");
120
+ } catch {
121
+ return null;
122
+ }
123
+ if (hashString(data) !== match[3] || data[0] !== "{" || data[data.length - 1] !== "}") {
124
+ fs.rmSync(file, { force: true });
125
+ return null;
126
+ }
127
+ if (data.length > 2) bodies.push(data.slice(1, -1));
128
+ }
129
+ let entries;
130
+ try {
131
+ entries = JSON.parse(`{${bodies.join(",")}}`);
132
+ } catch {
133
+ return null;
134
+ }
135
+ Object.setPrototypeOf(entries, null);
136
+ return entries;
137
+ }
138
+ function readHead(dir) {
139
+ try {
140
+ const head = JSON.parse(fs.readFileSync(path.join(dir, HEAD_FILE), "utf8"));
141
+ return head && typeof head === "object" ? head : null;
142
+ } catch {
143
+ return null;
144
+ }
145
+ }
146
+ /** writes the head (when it changed) and deletes the files it does not list */
147
+ function commitHead(dir, head, keepUnlisted = false) {
148
+ writeFileIfChanged(path.join(dir, HEAD_FILE), JSON.stringify(head), { atomic: true });
149
+ if (keepUnlisted) return;
150
+ const listed = /* @__PURE__ */ new Set();
151
+ for (const value of Object.values(head)) if (Array.isArray(value)) {
152
+ for (const name of value) if (typeof name === "string") listed.add(name);
153
+ }
154
+ for (const entry of fs.readdirSync(dir)) if (!listed.has(entry) && (PARTITION_FILE.test(entry) || entry.startsWith(".tmp-"))) fs.rmSync(path.join(dir, entry), { force: true });
155
+ }
156
+ /** (2: partitions by `partitionHash`) */
157
+ const QUERY_HEAD_VERSION = 2;
158
+ function readPartitionedQueryState(dir) {
159
+ const head = readHead(dir);
160
+ if (head?.version !== QUERY_HEAD_VERSION || head.stateVersion !== 1) return null;
161
+ if (typeof head.schemaHash !== "string") return null;
162
+ const nodes = readPartitions(dir, "nodes", head.nodes);
163
+ const pages = nodes && readPartitions(dir, "pages", head.pages);
164
+ if (!nodes || !pages) return null;
165
+ return {
166
+ version: 1,
167
+ generation: typeof head.generation === "string" ? head.generation : void 0,
168
+ schemaHash: head.schemaHash,
169
+ nodes,
170
+ nodeCount: typeof head.nodeCount === "number" ? head.nodeCount : void 0,
171
+ pages
172
+ };
173
+ }
174
+ /**
175
+ * `keepUnlisted`: the files of the previous head stay (the pending state written before rendering:
176
+ * the final state usually lists most of them again, and a file deleted and written again within a
177
+ * build is in its layer diff).
178
+ */
179
+ function writePartitionedQueryState(dir, state, options = {}) {
180
+ fs.mkdirSync(dir, { recursive: true });
181
+ const previous = readHead(dir);
182
+ commitHead(dir, {
183
+ version: QUERY_HEAD_VERSION,
184
+ stateVersion: state.version,
185
+ generation: state.generation,
186
+ schemaHash: state.schemaHash,
187
+ nodeCount: state.nodeCount,
188
+ nodes: writePartitions(dir, "nodes", state.nodes, previous?.nodes),
189
+ pages: writePartitions(dir, "pages", state.pages, previous?.pages)
190
+ }, options.keepUnlisted);
191
+ }
192
+ /** (2: partitions by `partitionHash`) */
193
+ const HTML_HEAD_VERSION = 2;
194
+ /**
195
+ * Marks the state as in progress: a build writing pages (see `markHtmlStatePending`). The head is
196
+ * left as it is, so an unchanged build ends with the files it started with.
197
+ */
198
+ const PENDING_FILE = "pending.json";
199
+ function readPending(dir) {
200
+ let data;
201
+ try {
202
+ data = fs.readFileSync(path.join(dir, PENDING_FILE), "utf8");
203
+ } catch {
204
+ return;
205
+ }
206
+ try {
207
+ const pending = JSON.parse(data);
208
+ return Array.isArray(pending?.paths) ? pending.paths : null;
209
+ } catch {
210
+ return null;
211
+ }
212
+ }
213
+ function readCommittedHtmlState(dir) {
214
+ const head = readHead(dir);
215
+ if (head?.version !== HTML_HEAD_VERSION || typeof head.ssrHash !== "string") return null;
216
+ const pages = readPartitions(dir, "pages", head.pages);
217
+ return pages && {
218
+ ssrHash: head.ssrHash,
219
+ pages
220
+ };
221
+ }
222
+ /**
223
+ * The HTML state; while a build is in progress (or failed) it has no ssrHash and also lists the
224
+ * paths that build may have written, like the single layout's mid-build state.
225
+ */
226
+ function readPartitionedHtmlState(dir) {
227
+ const committed = readCommittedHtmlState(dir);
228
+ const pending = readPending(dir);
229
+ if (pending === void 0) return committed;
230
+ const pages = Object.create(null);
231
+ for (const p of pending ?? []) if (typeof p === "string") pages[p] = "";
232
+ if (committed) Object.assign(pages, committed.pages);
233
+ return {
234
+ ssrHash: "",
235
+ pages
236
+ };
237
+ }
238
+ /**
239
+ * Marks the state as in progress before pages are written, with the paths the build may write that
240
+ * the state does not list (`added`; kept with those of an earlier unfinished build).
241
+ */
242
+ function markHtmlStatePending(dir, added) {
243
+ const paths = new Set(readPending(dir) ?? []);
244
+ for (const p of added) paths.add(p);
245
+ const data = JSON.stringify({ paths: [...paths].sort() });
246
+ writeFileIfChanged(path.join(dir, PENDING_FILE), data, { atomic: true });
247
+ }
248
+ /** the state of a completed build: written, then the in-progress marker removed */
249
+ function writePartitionedHtmlState(dir, state) {
250
+ fs.mkdirSync(dir, { recursive: true });
251
+ commitHead(dir, {
252
+ version: HTML_HEAD_VERSION,
253
+ ssrHash: state.ssrHash,
254
+ pages: writePartitions(dir, "pages", state.pages, readHead(dir)?.pages)
255
+ });
256
+ fs.rmSync(path.join(dir, PENDING_FILE), { force: true });
257
+ }
258
+ export { MIN_PARTITION_COUNT, markHtmlStatePending, partitionCountFor, readPartitionedHtmlState, readPartitionedQueryState, readPartitions, setPartitionTargetBytes, writePartitionedHtmlState, writePartitionedQueryState, writePartitions };