@smoothbricks/nx-plugin 0.7.17 → 0.7.19

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 (199) hide show
  1. package/README.md +315 -114
  2. package/dist/.build-inputs.json +47 -38
  3. package/dist/built-output-packages.js +1 -1
  4. package/dist/bun-lock-scope.d.ts +128 -20
  5. package/dist/bun-lock-scope.d.ts.map +1 -1
  6. package/dist/bun-lock-scope.js +267 -44
  7. package/dist/cargo-closure-input.d.ts +8 -6
  8. package/dist/cargo-closure-input.d.ts.map +1 -1
  9. package/dist/cargo-closure-input.js +15 -12
  10. package/dist/cargo-source-hash.d.ts +26 -25
  11. package/dist/cargo-source-hash.d.ts.map +1 -1
  12. package/dist/cargo-source-hash.js +84 -105
  13. package/dist/cargo-toolchain-paths.d.ts +2 -2
  14. package/dist/cargo-toolchain-paths.js +2 -2
  15. package/dist/cargo-workspace.d.ts +20 -2
  16. package/dist/cargo-workspace.d.ts.map +1 -1
  17. package/dist/cargo-workspace.js +43 -22
  18. package/dist/ensure-built-receipt.d.ts +3 -2
  19. package/dist/ensure-built-receipt.d.ts.map +1 -1
  20. package/dist/ensure-built-receipt.js +3 -2
  21. package/dist/eslint-config-closure.d.ts +8 -3
  22. package/dist/eslint-config-closure.d.ts.map +1 -1
  23. package/dist/eslint-config-closure.js +24 -5
  24. package/dist/executors/bounded-exec/admission-queue.d.ts +3 -1
  25. package/dist/executors/bounded-exec/admission-queue.d.ts.map +1 -1
  26. package/dist/executors/bounded-exec/admission-queue.js +9 -10
  27. package/dist/executors/bounded-exec/awake-clock.d.ts +5 -0
  28. package/dist/executors/bounded-exec/awake-clock.d.ts.map +1 -0
  29. package/dist/executors/bounded-exec/awake-clock.js +22 -0
  30. package/dist/executors/bounded-exec/bounds.d.ts +19 -6
  31. package/dist/executors/bounded-exec/bounds.d.ts.map +1 -1
  32. package/dist/executors/bounded-exec/bounds.js +22 -15
  33. package/dist/executors/bounded-exec/case-gate.d.ts +114 -0
  34. package/dist/executors/bounded-exec/case-gate.d.ts.map +1 -0
  35. package/dist/executors/bounded-exec/case-gate.js +342 -0
  36. package/dist/executors/bounded-exec/cpu-pool-thread.d.ts +1 -0
  37. package/dist/executors/bounded-exec/cpu-pool-thread.d.ts.map +1 -1
  38. package/dist/executors/bounded-exec/cpu-pool-thread.js +2 -2
  39. package/dist/executors/bounded-exec/cpu-pool.d.ts +28 -1
  40. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  41. package/dist/executors/bounded-exec/cpu-pool.js +102 -16
  42. package/dist/executors/bounded-exec/cpu-rise.d.ts +73 -0
  43. package/dist/executors/bounded-exec/cpu-rise.d.ts.map +1 -0
  44. package/dist/executors/bounded-exec/cpu-rise.js +111 -0
  45. package/dist/executors/bounded-exec/endpoint-life.d.ts +146 -0
  46. package/dist/executors/bounded-exec/endpoint-life.d.ts.map +1 -0
  47. package/dist/executors/bounded-exec/endpoint-life.js +228 -0
  48. package/dist/executors/bounded-exec/executor.d.ts +1 -1
  49. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  50. package/dist/executors/bounded-exec/executor.js +256 -86
  51. package/dist/executors/bounded-exec/gateway-lease.d.ts +1 -1
  52. package/dist/executors/bounded-exec/gateway-lease.d.ts.map +1 -1
  53. package/dist/executors/bounded-exec/gateway-lease.js +8 -1
  54. package/dist/executors/bounded-exec/go-run-summary.d.ts +51 -0
  55. package/dist/executors/bounded-exec/go-run-summary.d.ts.map +1 -0
  56. package/dist/executors/bounded-exec/go-run-summary.js +98 -0
  57. package/dist/executors/bounded-exec/hang-diagnostics.d.ts +11 -3
  58. package/dist/executors/bounded-exec/hang-diagnostics.d.ts.map +1 -1
  59. package/dist/executors/bounded-exec/hang-diagnostics.js +125 -47
  60. package/dist/executors/bounded-exec/lease-wait.d.ts +1 -0
  61. package/dist/executors/bounded-exec/lease-wait.d.ts.map +1 -1
  62. package/dist/executors/bounded-exec/lease-wait.js +1 -0
  63. package/dist/executors/bounded-exec/mbx-pool.d.ts +42 -7
  64. package/dist/executors/bounded-exec/mbx-pool.d.ts.map +1 -1
  65. package/dist/executors/bounded-exec/mbx-pool.js +45 -8
  66. package/dist/executors/bounded-exec/mbx-reservations.d.ts +12 -6
  67. package/dist/executors/bounded-exec/mbx-reservations.d.ts.map +1 -1
  68. package/dist/executors/bounded-exec/mbx-reservations.js +190 -284
  69. package/dist/executors/bounded-exec/parent-grant.d.ts +2 -0
  70. package/dist/executors/bounded-exec/parent-grant.d.ts.map +1 -1
  71. package/dist/executors/bounded-exec/parent-grant.js +1 -0
  72. package/dist/executors/bounded-exec/pool-admission.d.ts +29 -3
  73. package/dist/executors/bounded-exec/pool-admission.d.ts.map +1 -1
  74. package/dist/executors/bounded-exec/pool-admission.js +47 -1
  75. package/dist/executors/bounded-exec/progress-channel.d.ts +38 -0
  76. package/dist/executors/bounded-exec/progress-channel.d.ts.map +1 -0
  77. package/dist/executors/bounded-exec/progress-channel.js +113 -0
  78. package/dist/executors/bounded-exec/progress.d.ts +5 -1
  79. package/dist/executors/bounded-exec/progress.d.ts.map +1 -1
  80. package/dist/executors/bounded-exec/progress.js +31 -1
  81. package/dist/executors/bounded-exec/ram-temp.d.ts +24 -1
  82. package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -1
  83. package/dist/executors/bounded-exec/ram-temp.js +123 -16
  84. package/dist/executors/bounded-exec/reservation-connection.d.ts +90 -0
  85. package/dist/executors/bounded-exec/reservation-connection.d.ts.map +1 -0
  86. package/dist/executors/bounded-exec/reservation-connection.js +248 -0
  87. package/dist/executors/bounded-exec/run-cpu-pool.d.ts.map +1 -1
  88. package/dist/executors/bounded-exec/run-cpu-pool.js +3 -0
  89. package/dist/executors/bounded-exec/runner-sizing.d.ts +9 -5
  90. package/dist/executors/bounded-exec/runner-sizing.d.ts.map +1 -1
  91. package/dist/executors/bounded-exec/runner-sizing.js +13 -9
  92. package/dist/executors/bounded-exec/schema.d.ts +7 -2
  93. package/dist/executors/bounded-exec/schema.d.ts.map +1 -1
  94. package/dist/executors/bounded-exec/schema.js +17 -1
  95. package/dist/executors/bounded-exec/suite-minimum.d.ts +21 -0
  96. package/dist/executors/bounded-exec/suite-minimum.d.ts.map +1 -0
  97. package/dist/executors/bounded-exec/suite-minimum.js +85 -0
  98. package/dist/executors/bounded-exec/test-result-writer.d.ts +2 -0
  99. package/dist/executors/bounded-exec/test-result-writer.d.ts.map +1 -1
  100. package/dist/executors/bounded-exec/test-result-writer.js +1 -0
  101. package/dist/executors/bounded-exec/test-results.d.ts +5 -0
  102. package/dist/executors/bounded-exec/test-results.d.ts.map +1 -1
  103. package/dist/executors/bounded-exec/test-results.js +2 -2
  104. package/dist/executors/bounded-exec/verdict.d.ts +39 -10
  105. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  106. package/dist/executors/bounded-exec/verdict.js +60 -14
  107. package/dist/executors/cargo-zigbuild/executor.d.ts +10 -8
  108. package/dist/executors/cargo-zigbuild/executor.d.ts.map +1 -1
  109. package/dist/executors/cargo-zigbuild/executor.js +32 -19
  110. package/dist/index.d.ts.map +1 -1
  111. package/dist/index.js +294 -152
  112. package/dist/managed-files/ci-workflow.d.ts +4 -1
  113. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  114. package/dist/managed-files/ci-workflow.js +9 -14
  115. package/dist/managed-files/files.d.ts +3 -3
  116. package/dist/managed-files/files.js +3 -3
  117. package/dist/managed-files/generator.d.ts +5 -1
  118. package/dist/managed-files/generator.d.ts.map +1 -1
  119. package/dist/managed-files/generator.js +22 -7
  120. package/dist/managed-files/render-memo.d.ts +13 -0
  121. package/dist/managed-files/render-memo.d.ts.map +1 -0
  122. package/dist/managed-files/render-memo.js +94 -0
  123. package/dist/task-results.js +2 -1
  124. package/dist/test-program.d.ts +11 -6
  125. package/dist/test-program.d.ts.map +1 -1
  126. package/dist/test-program.js +39 -35
  127. package/dist/tool-bins.d.ts +12 -0
  128. package/dist/tool-bins.d.ts.map +1 -1
  129. package/dist/tool-bins.js +31 -6
  130. package/dist/typecheck-test-policy.d.ts +10 -1
  131. package/dist/typecheck-test-policy.d.ts.map +1 -1
  132. package/dist/typecheck-test-policy.js +40 -24
  133. package/dist/workspace-config-policy.d.ts.map +1 -1
  134. package/dist/workspace-config-policy.js +135 -42
  135. package/dist/workspace-index.d.ts +14 -1
  136. package/dist/workspace-index.d.ts.map +1 -1
  137. package/dist/workspace-index.js +36 -4
  138. package/executors.json +2 -0
  139. package/managed/raw/envrc +24 -0
  140. package/managed/raw/patches/nx@24.0.3.patch +1851 -221
  141. package/managed/raw/patches/ttsc@0.37.1.patch +1055 -0
  142. package/managed/raw/tooling/direnv/developer-links.ts +122 -14
  143. package/managed/raw/tooling/direnv/devenv.smoo.nix +20 -19
  144. package/managed/templates/github/actions/save-nix-devenv/action.yml +19 -1
  145. package/managed/templates/github/actions/setup-devenv/action.yml +2 -0
  146. package/package.json +68 -11
  147. package/runtime/nx-publish/.build-inputs.json +2 -2
  148. package/runtime/nx-publish/cache_adapter.js +293 -3
  149. package/runtime/nx-publish/nx_publish.js +36 -14
  150. package/src/built-output-packages.ts +1 -1
  151. package/src/bun-lock-scope.ts +293 -40
  152. package/src/cargo-closure-input.ts +20 -12
  153. package/src/cargo-source-hash.ts +119 -145
  154. package/src/cargo-toolchain-paths.ts +2 -2
  155. package/src/cargo-workspace.ts +59 -21
  156. package/src/ensure-built-receipt.ts +3 -2
  157. package/src/eslint-config-closure.ts +27 -6
  158. package/src/executors/bounded-exec/admission-queue.ts +11 -10
  159. package/src/executors/bounded-exec/awake-clock.ts +20 -0
  160. package/src/executors/bounded-exec/bounds.ts +41 -19
  161. package/src/executors/bounded-exec/case-gate.ts +365 -0
  162. package/src/executors/bounded-exec/cpu-pool-thread.ts +3 -3
  163. package/src/executors/bounded-exec/cpu-pool.ts +139 -12
  164. package/src/executors/bounded-exec/cpu-rise.ts +156 -0
  165. package/src/executors/bounded-exec/endpoint-life.ts +279 -0
  166. package/src/executors/bounded-exec/executor.ts +341 -123
  167. package/src/executors/bounded-exec/gateway-lease.ts +6 -0
  168. package/src/executors/bounded-exec/go-run-summary.ts +137 -0
  169. package/src/executors/bounded-exec/hang-diagnostics.ts +150 -50
  170. package/src/executors/bounded-exec/lease-wait.ts +1 -0
  171. package/src/executors/bounded-exec/mbx-pool.ts +57 -9
  172. package/src/executors/bounded-exec/mbx-reservations.ts +216 -183
  173. package/src/executors/bounded-exec/parent-grant.ts +3 -0
  174. package/src/executors/bounded-exec/pool-admission.ts +59 -2
  175. package/src/executors/bounded-exec/progress-channel.ts +129 -0
  176. package/src/executors/bounded-exec/progress.ts +39 -3
  177. package/src/executors/bounded-exec/ram-temp.ts +137 -17
  178. package/src/executors/bounded-exec/reservation-connection.ts +221 -0
  179. package/src/executors/bounded-exec/run-cpu-pool.ts +2 -0
  180. package/src/executors/bounded-exec/runner-sizing.ts +13 -8
  181. package/src/executors/bounded-exec/schema.json +1 -1
  182. package/src/executors/bounded-exec/schema.ts +21 -2
  183. package/src/executors/bounded-exec/suite-minimum.ts +81 -0
  184. package/src/executors/bounded-exec/test-result-writer.ts +3 -0
  185. package/src/executors/bounded-exec/test-results.ts +5 -0
  186. package/src/executors/bounded-exec/verdict.ts +83 -22
  187. package/src/executors/cargo-zigbuild/executor.ts +30 -19
  188. package/src/index.ts +357 -166
  189. package/src/managed-files/ci-workflow.ts +13 -17
  190. package/src/managed-files/files.ts +3 -3
  191. package/src/managed-files/generator.ts +21 -6
  192. package/src/managed-files/render-memo.ts +135 -0
  193. package/src/test-program.ts +36 -41
  194. package/src/tool-bins.ts +35 -6
  195. package/src/typecheck-test-policy.ts +49 -24
  196. package/src/workspace-config-policy.ts +161 -43
  197. package/src/workspace-index.ts +40 -5
  198. package/test-timings.json +157 -141
  199. package/managed/raw/patches/ttsc@0.36.3.patch +0 -1660
@@ -13,8 +13,9 @@ function openNxCache(root) {
13
13
  // `<machine>-v<DB_VERSION>.db` there (v3 in 23.2.1, v6 in 24.0.3: `native/db/mod.rs`, `initialize.rs`),
14
14
  // reached only through Nx's own `NxCache.get`; an entry is `<cache>/<hash>/` with its terminal output in
15
15
  // `<cache>/terminalOutputs/<hash>` (`native/cache/cache.rs`); `CachedResult` is `{ code, terminalOutput,
16
- // outputsPath, size }`; and `HttpRemoteCache.store` archives the entry alike (`native/cache/http_remote_cache.rs`,
17
- // the same source). 23.2.1's `get` answers a row whose entry directory is gone; 24.0.3's does not.
16
+ // outputsPath, size }`; and `HttpRemoteCache` stores and restores an entry as the archive nxArchive (below) writes
17
+ // (`native/cache/http_remote_cache.rs`, the same source at both tags, built with tar 0.4.44 and flate2 1.1.9 in each
18
+ // tag's Cargo.lock). 23.2.1's `get` answers a row whose entry directory is gone; 24.0.3's does not.
18
19
  const layouts = new Map([
19
20
  ['23.2.1', { workspaceData: '.nx/workspace-data', cache: '.nx/cache' }],
20
21
  ['24.0.3', { workspaceData: '.nx/workspace-data', cache: '.nx/cache' }],
@@ -54,5 +55,294 @@ function openNxCache(root) {
54
55
  if (path.resolve(native.cacheDirectory) !== path.resolve(cacheDir)) {
55
56
  throw new Error(`Nx ${version} cache directory changed: expected ${cacheDir}, actual ${native.cacheDirectory}`);
56
57
  }
57
- return { requireNx, cache, cacheDir: native.cacheDirectory, getLocal: (hash) => native.get(hash) };
58
+ return {
59
+ requireNx,
60
+ cache,
61
+ cacheDir: native.cacheDirectory,
62
+ getLocal: (hash) => native.get(hash),
63
+ archive: (hash, terminalOutput, code) => nxArchive(path.join(native.cacheDirectory, hash), terminalOutput, code),
64
+ };
65
+ }
66
+
67
+ // The archive of `<cache>/<hash>` Nx's native `HttpRemoteCache.store` sends and its `retrieve` unpacks in any checkout,
68
+ // written here so a land archives only a key its store lacks, and at gzip level 1 rather than Nx's default 6 (retrieve
69
+ // gunzips any level). With tar 0.4.44's `Builder::new()` (complete header mode) and symlinks not followed, `store` writes:
70
+ // - `append_dir_all("", <cache>/<hash>)`: a stack walk from the entry directory, which pushes each directory's
71
+ // `read_dir` entries in readdir order and takes the last pushed first, writing a directory before its children. Each
72
+ // entry has a GNU header (magic "ustar ", version " \0") filled from its metadata: the mtime in whole seconds, uid,
73
+ // gid, the whole st_mode, the size (0 for a directory or symlink), device fields of zero, and a symlink's target. A
74
+ // path or target over 100 bytes is first written whole and NUL-terminated in a `././@LongLink` entry ('L' or 'K');
75
+ // - `terminalOutput` (its UTF-8 bytes) and `code` (four big-endian bytes) under old-style headers;
76
+ // - two zero blocks.
77
+ // Numbers are tar 0.4.44's: octal, NUL-terminated and zero-padded, or big-endian binary with the high bit set past what
78
+ // the field holds. These are Nx's own bytes (the publisher's tests compare them), except that a sparse file, which tar
79
+ // 0.4.44 records as GNU sparse on Linux only, is written whole; `retrieve` restores the same file either way.
80
+ const TAR_BLOCK = 512;
81
+ /** gzip is fed writes of at least 1 MiB, not one per 512-byte header. */
82
+ const TAR_WRITE = 1 << 20;
83
+ const TAR_TYPES = new Map([
84
+ [0o100000n, '0'],
85
+ [0o120000n, '2'],
86
+ [0o040000n, '5'],
87
+ ]);
88
+
89
+ /**
90
+ * Nx's archive of the entry directory `entry` (above), gzipped at level 1: the body of one PUT as its compressed chunks
91
+ * and their total length, sent as they are rather than joined into a second copy.
92
+ */
93
+ async function nxArchive(entry, terminalOutput, code) {
94
+ const { createGzip } = require('node:zlib');
95
+ const { pipeline } = require('node:stream/promises');
96
+ const compressed = [];
97
+ await pipeline(tarWrites(tarStream(entry, terminalOutput, code)), createGzip({ level: 1 }), async (gzip) => {
98
+ for await (const chunk of gzip) compressed.push(chunk);
99
+ });
100
+ return { chunks: compressed, length: compressed.reduce((sum, chunk) => sum + chunk.length, 0) };
101
+ }
102
+
103
+ /** Joins successive byte strings into writes of at least TAR_WRITE bytes. */
104
+ function* tarWrites(parts) {
105
+ let held = [];
106
+ let size = 0;
107
+ for (const part of parts) {
108
+ held.push(part);
109
+ size += part.length;
110
+ if (size >= TAR_WRITE) {
111
+ yield held.length === 1 ? held[0] : Buffer.concat(held, size);
112
+ held = [];
113
+ size = 0;
114
+ }
115
+ }
116
+ if (size > 0) yield Buffer.concat(held, size);
117
+ }
118
+
119
+ /** The tar stream `HttpRemoteCache.store` builds for `entry`, as successive byte strings. */
120
+ function* tarStream(entry, terminalOutput, code) {
121
+ const fs = require('node:fs');
122
+ // append_dir_all's stack: [source path, archive path (null for the entry itself), is a directory].
123
+ const stack = [[Buffer.from(entry), null, true]];
124
+ while (stack.length > 0) {
125
+ const [source, name, isDirectory] = stack.pop();
126
+ if (isDirectory) {
127
+ const directory = fs.opendirSync(source, { encoding: 'buffer' });
128
+ try {
129
+ for (let child = directory.readSync(); child !== null; child = directory.readSync()) {
130
+ const archived = name === null ? child.name : Buffer.concat([name, TAR_SLASH, child.name]);
131
+ stack.push([Buffer.concat([source, TAR_SLASH, child.name]), archived, child.isDirectory()]);
132
+ }
133
+ } finally {
134
+ directory.closeSync();
135
+ }
136
+ if (name !== null) yield* tarEntry(name, fs.statSync(source, { bigint: true }), null);
137
+ continue;
138
+ }
139
+ const stat = fs.lstatSync(source, { bigint: true });
140
+ if (stat.isSymbolicLink()) {
141
+ yield* tarEntry(name, stat, fs.readlinkSync(source, { encoding: 'buffer' }));
142
+ } else if (stat.isFile()) {
143
+ yield* tarFile(fs, source, name);
144
+ } else {
145
+ throw new Error(`${source} is neither a directory, a file nor a symlink, which no Nx cache entry holds`);
146
+ }
147
+ }
148
+ yield* tarOld('terminalOutput', Buffer.from(terminalOutput, 'utf8'));
149
+ const exitCode = Buffer.alloc(4);
150
+ exitCode.writeUInt32BE(code);
151
+ yield* tarOld('code', exitCode);
152
+ yield Buffer.alloc(2 * TAR_BLOCK);
153
+ }
154
+
155
+ /** A regular file's header and bytes, its length the one its header names: a file that changes meanwhile is refused. */
156
+ function* tarFile(fs, source, name) {
157
+ const fd = fs.openSync(source, 'r');
158
+ try {
159
+ const stat = fs.fstatSync(fd, { bigint: true });
160
+ yield* tarEntry(name, stat, null);
161
+ let left = Number(stat.size);
162
+ while (left > 0) {
163
+ const chunk = Buffer.allocUnsafe(Math.min(left, TAR_WRITE));
164
+ const read = fs.readSync(fd, chunk, 0, chunk.length, null);
165
+ if (read === 0) throw new Error(`${source} shrank while it was archived`);
166
+ left -= read;
167
+ yield read === chunk.length ? chunk : chunk.subarray(0, read);
168
+ }
169
+ if (fs.readSync(fd, Buffer.alloc(1), 0, 1, null) !== 0) throw new Error(`${source} grew while it was archived`);
170
+ yield* tarPadding(stat.size);
171
+ } finally {
172
+ fs.closeSync(fd);
173
+ }
174
+ }
175
+
176
+ /** tar 0.4.44's `append_fs`/`append_file` header for one walked entry, after any long-name entries it needs. */
177
+ function* tarEntry(name, stat, link) {
178
+ const header = tarGnuHeader();
179
+ yield* tarPath(header, name);
180
+ tarNumber(header, 136, 12, tarSeconds(stat.mtimeNs));
181
+ tarNumber(header, 108, 8, stat.uid);
182
+ tarNumber(header, 116, 8, stat.gid);
183
+ tarOctal(header, 100, 8, stat.mode);
184
+ header.write(TAR_TYPES.get(stat.mode & 0o170000n), 156, 'latin1');
185
+ tarNumber(header, 124, 12, stat.isFile() ? stat.size : 0n);
186
+ tarOctal(header, 329, 8, 0n);
187
+ tarOctal(header, 337, 8, 0n);
188
+ if (link !== null) yield* tarLink(header, link);
189
+ tarChecksum(header);
190
+ yield header;
191
+ }
192
+
193
+ /** `append_data` under `Header::new_old()`: no magic, mode, owner or type, only the name, size and a zero mtime. */
194
+ function* tarOld(name, data) {
195
+ const header = Buffer.alloc(TAR_BLOCK);
196
+ tarNumber(header, 136, 12, 0n);
197
+ tarNumber(header, 124, 12, BigInt(data.length));
198
+ tarCopyPath(header, 0, 100, Buffer.from(name), false, false);
199
+ tarChecksum(header);
200
+ yield header;
201
+ if (data.length > 0) yield data;
202
+ yield* tarPadding(BigInt(data.length));
203
+ }
204
+
205
+ function tarGnuHeader() {
206
+ const header = Buffer.alloc(TAR_BLOCK);
207
+ header.write('ustar ', 257, 'latin1');
208
+ header.write(' \0', 263, 'latin1');
209
+ tarNumber(header, 136, 12, 0n);
210
+ return header;
211
+ }
212
+
213
+ /** `prepare_header_path`: the name field, or a GNU long-name entry and the name cut to its valid UTF-8 within 100 bytes. */
214
+ function* tarPath(header, name) {
215
+ try {
216
+ tarCopyPath(header, 0, 100, name, false, false);
217
+ } catch (error) {
218
+ if (name.length < 100) throw error;
219
+ yield* tarLongLink(name, 'L');
220
+ const { isUtf8 } = require('node:buffer');
221
+ let cut = 100;
222
+ while (cut > 0 && !isUtf8(name.subarray(0, cut))) cut -= 1;
223
+ tarCopyPath(header, 0, 100, name.subarray(0, cut), false, true);
224
+ }
225
+ }
226
+
227
+ /** `prepare_header_link`: the link-name field, or a GNU long-link entry before the header. */
228
+ function* tarLink(header, link) {
229
+ try {
230
+ tarCopyPath(header, 157, 100, link, true, false);
231
+ } catch (error) {
232
+ if (link.length < 100) throw error;
233
+ yield* tarLongLink(link, 'K');
234
+ }
235
+ }
236
+
237
+ /** tar 0.4.44's `prepare_header`: a `././@LongLink` entry holding `data` NUL-terminated. */
238
+ function* tarLongLink(data, type) {
239
+ const header = tarGnuHeader();
240
+ header.write('././@LongLink', 0, 'latin1');
241
+ tarOctal(header, 100, 8, 0o644n);
242
+ tarNumber(header, 108, 8, 0n);
243
+ tarNumber(header, 116, 8, 0n);
244
+ tarNumber(header, 124, 12, BigInt(data.length + 1));
245
+ header.write(type, 156, 'latin1');
246
+ tarChecksum(header);
247
+ yield header;
248
+ yield Buffer.concat([data, Buffer.alloc(1)]);
249
+ yield* tarPadding(BigInt(data.length + 1));
250
+ }
251
+
252
+ const TAR_SLASH = Buffer.from('/');
253
+
254
+ /**
255
+ * tar 0.4.44's `copy_path_into_inner`: `path`'s components (std's `Path::components`) joined by '/' into the field,
256
+ * each copy NUL-terminated while room remains. Like it, a component that does not fit throws after the ones before it
257
+ * were written, and those stay in the header.
258
+ */
259
+ function tarCopyPath(header, offset, length, path, isLinkName, isTruncatedGnuLongPath) {
260
+ const end = offset + length;
261
+ let at = offset;
262
+ const copy = (bytes) => {
263
+ if (bytes.length > end - at) throw new Error(`${path} is too long for a ${length}-byte tar field`);
264
+ if (bytes.includes(0)) throw new Error(`${path} contains a NUL byte`);
265
+ bytes.copy(header, at);
266
+ if (at + bytes.length < end) header[at + bytes.length] = 0;
267
+ at += bytes.length;
268
+ };
269
+ const components = tarComponents(path);
270
+ let needsSlash = false;
271
+ let emitted = false;
272
+ for (const [index, { kind, bytes }] of components.entries()) {
273
+ if (!isLinkName) {
274
+ if (kind === 'root') throw new Error(`${path}: paths in archives must be relative`);
275
+ if (kind === 'parent' && (!isTruncatedGnuLongPath || index < components.length - 1)) {
276
+ throw new Error(`${path}: paths in archives must not have '..'`);
277
+ }
278
+ if (kind === 'current' && components.length !== 1) continue;
279
+ }
280
+ if (needsSlash) copy(TAR_SLASH);
281
+ copy(bytes);
282
+ if (kind !== 'root') needsSlash = true;
283
+ emitted = true;
284
+ }
285
+ if (!emitted) throw new Error(`${path}: paths in archives must have at least one component`);
286
+ if (path.length > 0 && path[path.length - 1] === 0x2f) copy(TAR_SLASH);
287
+ }
288
+
289
+ /** std's Unix `Path::components`: a leading root, a leading '.', then the '/'-separated names past empty and '.' ones. */
290
+ function tarComponents(path) {
291
+ const components = [];
292
+ if (path[0] === 0x2f) components.push({ kind: 'root', bytes: TAR_SLASH });
293
+ else if (path[0] === 0x2e && (path.length === 1 || path[1] === 0x2f)) {
294
+ components.push({ kind: 'current', bytes: Buffer.from('.') });
295
+ }
296
+ let start = 0;
297
+ for (let index = 0; index <= path.length; index++) {
298
+ if (index < path.length && path[index] !== 0x2f) continue;
299
+ const bytes = path.subarray(start, index);
300
+ start = index + 1;
301
+ if (bytes.length === 0 || (bytes.length === 1 && bytes[0] === 0x2e)) continue;
302
+ const parent = bytes.length === 2 && bytes[0] === 0x2e && bytes[1] === 0x2e;
303
+ components.push({ kind: parent ? 'parent' : 'normal', bytes });
304
+ }
305
+ return components;
306
+ }
307
+
308
+ /** tar 0.4.44's `octal_into`: the low digits that fit, zero-padded, NUL last. */
309
+ function tarOctal(header, offset, length, value) {
310
+ const digits = value.toString(8);
311
+ header[offset + length - 1] = 0;
312
+ for (let place = 1; place < length; place++) {
313
+ const digit = digits.length - place;
314
+ header[offset + length - 1 - place] = digit >= 0 ? digits.charCodeAt(digit) : 0x30;
315
+ }
316
+ }
317
+
318
+ /** tar 0.4.44's `num_field_wrapper_into`: octal, or big-endian binary with the high bit set past 2^33 (2^21 in 8 bytes). */
319
+ function tarNumber(header, offset, length, value) {
320
+ if (value >= 1n << 33n || (value >= 1n << 21n && length === 8)) {
321
+ header.fill(0, offset, offset + length - 8);
322
+ header.writeBigUInt64BE(value, offset + length - 8);
323
+ header[offset] |= 0x80;
324
+ } else {
325
+ tarOctal(header, offset, length, value);
326
+ }
327
+ }
328
+
329
+ /** `set_cksum`: the byte sum with the checksum field read as spaces, in its octal form. */
330
+ function tarChecksum(header) {
331
+ let sum = 8 * 0x20;
332
+ for (let index = 0; index < TAR_BLOCK; index++) {
333
+ if (index < 148 || index >= 156) sum += header[index];
334
+ }
335
+ tarOctal(header, 148, 8, BigInt(sum));
336
+ }
337
+
338
+ /** st_mtime as `MetadataExt::mtime() as u64`: whole seconds, floored, two's complement when negative. */
339
+ function tarSeconds(nanoseconds) {
340
+ const seconds = nanoseconds / 1_000_000_000n;
341
+ const floored = nanoseconds < 0n && seconds * 1_000_000_000n !== nanoseconds ? seconds - 1n : seconds;
342
+ return BigInt.asUintN(64, floored);
343
+ }
344
+
345
+ function* tarPadding(size) {
346
+ const rest = Number(size % BigInt(TAR_BLOCK));
347
+ if (rest !== 0) yield Buffer.alloc(TAR_BLOCK - rest);
58
348
  }
@@ -1,6 +1,8 @@
1
- // A land's publish (05_gateway.md "`/nx/v1/cache/{hash}`"): after the gate passed, every task result
2
- // the gate's Nx runs named is stored to the project's Nx cache from this checkout's own cache, as
3
- // plain PUTs by the stock native client. Prefixed once by cache_adapter::script.
1
+ // A land's publish (05_gateway.md, "Publication"; 03_caches.md, "Nx accepted-task artifact service"): after the gate
2
+ // passed, every task result the gate's Nx runs named is stored to the project's Nx cache from this checkout's own cache.
3
+ // Each key is asked for first: a stored key is counted and never archived. Only a missing key is archived, by
4
+ // cache_adapter's nxArchive in Nx's own layout at gzip level 1, and sent in one plain PUT. Prefixed once by
5
+ // cache_adapter::script.
4
6
  // Usage: node - <absolute Nx root> <JSON array of task hashes>; answers one JSON line on stdout.
5
7
  const answer = process.stdout.write.bind(process.stdout);
6
8
  process.stdout.write = process.stderr.write.bind(process.stderr);
@@ -22,11 +24,12 @@ for (const hash of hashes) {
22
24
  }
23
25
  const server = process.env.NX_SELF_HOSTED_REMOTE_CACHE_SERVER;
24
26
  if (!server) throw new Error('NX_SELF_HOSTED_REMOTE_CACHE_SERVER names no project Nx cache');
27
+ // The headers Nx's own client sends (`HttpRemoteCache::new`): the configured bearer, and an octet-stream body.
28
+ const token = process.env.NX_SELF_HOSTED_REMOTE_CACHE_ACCESS_TOKEN;
29
+ const bearer = token === undefined ? {} : { authorization: `Bearer ${token}` };
25
30
 
26
31
  async function publish() {
27
- const { requireNx, cacheDir, getLocal } = openNxCache(root);
28
- const { HttpRemoteCache } = requireNx('nx/src/native');
29
- const remote = new HttpRemoteCache();
32
+ const { getLocal, archive } = openNxCache(root);
30
33
  let published = 0;
31
34
  let existing = 0;
32
35
  let skipped = 0;
@@ -38,17 +41,36 @@ async function publish() {
38
41
  skipped += 1;
39
42
  continue;
40
43
  }
41
- if (await remote.store(hash, cacheDir, result.terminalOutput ?? '', 0)) {
42
- published += 1;
44
+ const url = `${server}/v1/cache/${hash}`;
45
+ const probe = await fetch(url, { headers: { ...bearer, accept: 'application/octet-stream' } });
46
+ await probe.body?.cancel();
47
+ if (probe.status === 200) {
48
+ existing += 1;
43
49
  continue;
44
50
  }
45
- // The native store answers false for an existing key (409) and for a refusal: tell them apart.
46
- const response = await fetch(`${server}/v1/cache/${hash}`);
47
- await response.body?.cancel();
48
- if (response.status !== 200) {
49
- throw new Error(`task ${hash} was not stored, and the project cache answers GET ${response.status}`);
51
+ if (probe.status !== 404) {
52
+ throw new Error(`task ${hash} was not stored: the project cache answers GET ${probe.status}`);
53
+ }
54
+ const { chunks, length } = await archive(hash, result.terminalOutput ?? '', 0);
55
+ const response = await fetch(url, {
56
+ method: 'PUT',
57
+ headers: { ...bearer, 'content-type': 'application/octet-stream', 'content-length': String(length) },
58
+ body: (async function* () {
59
+ yield* chunks;
60
+ })(),
61
+ duplex: 'half',
62
+ });
63
+ const said = (await response.text()).trim();
64
+ if (response.status === 200) {
65
+ published += 1;
66
+ } else if (response.status === 409) {
67
+ // Stored between the GET and this PUT: the store keeps the archive it holds.
68
+ existing += 1;
69
+ } else {
70
+ throw new Error(
71
+ `task ${hash} was not stored: the project cache answers PUT ${response.status}${said === '' ? '' : `: ${said}`}`,
72
+ );
50
73
  }
51
- existing += 1;
52
74
  }
53
75
  return { published, existing, skipped };
54
76
  }
@@ -72,7 +72,7 @@ function readLockfile(context: CreateNodesContextV2): LockfileRead {
72
72
  if (!existsSync(path)) throw new Error(`builtOutputPackages names packages, but ${path} does not exist`);
73
73
  const contents = packageManager === 'bun' ? readBunLockFile(path) : readFileSync(path, 'utf-8');
74
74
  const hash = hashArray([nxVersion, contents]);
75
- if (published?.hash === hash) return published;
75
+ if (packageManager !== 'bun' && published?.hash === hash) return published;
76
76
  const { nodes, keyMap } = getLockFileNodes(packageManager, lockFile, contents, hash, context);
77
77
  return { packageManager, lockFile, contents, hash, nodes, keyMap };
78
78
  }