@kubb/core 5.18.7 → 5.18.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -29,10 +29,10 @@ let node_async_hooks = require("node:async_hooks");
29
29
  let node_process = require("node:process");
30
30
  node_process = __toESM(node_process, 1);
31
31
  let tinyexec = require("tinyexec");
32
+ let node_fs = require("node:fs");
32
33
  let node_fs_promises = require("node:fs/promises");
33
34
  let node_path = require("node:path");
34
35
  node_path = __toESM(node_path, 1);
35
- let node_fs = require("node:fs");
36
36
  let node_util = require("node:util");
37
37
  let _kubb_ast = require("@kubb/ast");
38
38
  let node_dns = require("node:dns");
@@ -45,7 +45,7 @@ let node_readline = require("node:readline");
45
45
  let node_url = require("node:url");
46
46
  let node_module = require("node:module");
47
47
  // package.json
48
- var version = "5.18.7";
48
+ var version = "5.18.8";
49
49
  // ../../internals/utils/src/casing.ts
50
50
  /**
51
51
  * Shared implementation for camelCase and PascalCase conversion.
@@ -220,7 +220,25 @@ async function exists(path) {
220
220
  */
221
221
  async function read(path) {
222
222
  if (runtime.isBun) return Bun.file(path).text();
223
- return (0, node_fs_promises.readFile)(path, { encoding: "utf8" });
223
+ return new Promise((resolvePromise, reject) => (0, node_fs.readFile)(path, { encoding: "utf8" }, (error, data) => error ? reject(error) : resolvePromise(data)));
224
+ }
225
+ /**
226
+ * Reads the file at `path` as a UTF-8 string, or `null` when it does not exist or cannot be read.
227
+ * Uses `Bun.file().text()` when running under Bun, `fs.readFileSync` otherwise.
228
+ *
229
+ * @example
230
+ * ```ts
231
+ * const stored = await readIfExists('./src/gen/Pet.ts') // null on a first run
232
+ * ```
233
+ */
234
+ async function readIfExists(path) {
235
+ if (runtime.isBun) return Bun.file(path).text().catch(() => null);
236
+ if (!(0, node_fs.existsSync)(path)) return null;
237
+ try {
238
+ return (0, node_fs.readFileSync)(path, "utf8");
239
+ } catch (_error) {
240
+ return null;
241
+ }
224
242
  }
225
243
  /**
226
244
  * Whether `stored` already holds `source`, comparing on the trimmed text rather than the exact
@@ -255,7 +273,7 @@ async function write(path, data, options = {}) {
255
273
  const content = `${trimmed}\n`;
256
274
  const resolved = (0, node_path.resolve)(path);
257
275
  if (matchesStored({
258
- stored: (options.stored === void 0 ? await read(resolved).catch(() => null) : options.stored) ?? "",
276
+ stored: (options.stored === void 0 ? await readIfExists(resolved) : options.stored) ?? "",
259
277
  source: trimmed
260
278
  })) return null;
261
279
  await writeRaw(resolved, content);
@@ -280,11 +298,11 @@ async function writeRaw(path, content) {
280
298
  return;
281
299
  }
282
300
  try {
283
- await (0, node_fs_promises.writeFile)(resolved, content, { encoding: "utf-8" });
301
+ await new Promise((resolvePromise, reject) => (0, node_fs.writeFile)(resolved, content, { encoding: "utf-8" }, (error) => error ? reject(error) : resolvePromise()));
284
302
  } catch (error) {
285
303
  if (error.code !== "ENOENT") throw error;
286
304
  await (0, node_fs_promises.mkdir)((0, node_path.dirname)(resolved), { recursive: true });
287
- await (0, node_fs_promises.writeFile)(resolved, content, { encoding: "utf-8" });
305
+ await new Promise((resolvePromise, reject) => (0, node_fs.writeFile)(resolved, content, { encoding: "utf-8" }, (error) => error ? reject(error) : resolvePromise()));
288
306
  }
289
307
  }
290
308
  /**
@@ -358,12 +376,33 @@ function isPathInside(path, parent) {
358
376
  * `const refs = memo(refCache, node, () => collectRefs(node))`
359
377
  */
360
378
  function memo$1(store, key, compute) {
361
- if (store.has(key)) return store.get(key);
379
+ const cached = store.get(key);
380
+ if (cached !== void 0 || store.has(key)) return cached;
362
381
  const value = compute();
363
382
  store.set(key, value);
364
383
  return value;
365
384
  }
366
385
  __name(memo$1, "memo");
386
+ function createStore({ type }) {
387
+ if (type === "weak") return /* @__PURE__ */ new WeakMap();
388
+ const entry = /* @__PURE__ */ new Map();
389
+ if (type === "map") return entry;
390
+ const set = (key, value) => {
391
+ entry.clear();
392
+ entry.set(key, value);
393
+ };
394
+ const touch = (key) => {
395
+ if (!entry.has(key)) return false;
396
+ set(key, entry.get(key));
397
+ return true;
398
+ };
399
+ return {
400
+ has: touch,
401
+ get: (key) => touch(key) ? entry.get(key) : void 0,
402
+ set
403
+ };
404
+ }
405
+ memo$1.create = createStore;
367
406
  /**
368
407
  * Runs `run` over every item with at most `limit` in flight. Workers share one iterator, so each
369
408
  * takes the next item the moment it frees up instead of waiting for a batch to drain.
@@ -1810,6 +1849,7 @@ function isNamespace(value) {
1810
1849
  * reachable and a `file` override is never dropped.
1811
1850
  */
1812
1851
  const resolverOptions = Symbol.for("@kubb/core/resolver/options");
1852
+ const createNameNode = () => ({ next: /* @__PURE__ */ new Map() });
1813
1853
  /**
1814
1854
  * Builds a nested file path from a dotted name. Splits on dots that precede a letter
1815
1855
  * (so version numbers embedded in operationIds like `v2025.0` stay intact), camelCases
@@ -1875,6 +1915,10 @@ var Resolver = class Resolver {
1875
1915
  #options;
1876
1916
  #memo = /* @__PURE__ */ new Map();
1877
1917
  #usedNames = /* @__PURE__ */ new Set();
1918
+ #stringNames = /* @__PURE__ */ new Map();
1919
+ #arrayNames = createNameNode();
1920
+ #outputDirs = /* @__PURE__ */ new WeakMap();
1921
+ #importPaths = /* @__PURE__ */ new WeakMap();
1878
1922
  #tagParents = /* @__PURE__ */ new Map();
1879
1923
  #baseName;
1880
1924
  #filePath;
@@ -1941,16 +1985,30 @@ var Resolver = class Resolver {
1941
1985
  * ```
1942
1986
  */
1943
1987
  resolveName({ key, name, normalize = (name) => name }) {
1944
- const cacheKey = typeof key === "string" ? `s:${key}` : `a:${key.map((segment) => segment === void 0 ? "u" : `${segment.length}:${segment}`).join("")}`;
1945
- const allocate = (suffix) => {
1946
- const candidate = suffix === 1 ? name : `${name}${suffix}`;
1947
- const normalized = normalize(candidate);
1948
- const names = typeof normalized === "string" ? [normalized] : normalized;
1949
- if (names.some((emittedName) => this.#usedNames.has(emittedName))) return allocate(suffix + 1);
1950
- this.reserveNames(names);
1951
- return candidate;
1952
- };
1953
- return this.memo(`resolveName:${cacheKey}`, () => allocate(1));
1988
+ if (typeof key === "string") return memo$1(this.#stringNames, key, () => this.#allocateName({
1989
+ name,
1990
+ normalize,
1991
+ suffix: 1
1992
+ }));
1993
+ const node = key.reduce((parent, segment) => memo$1(parent.next, segment, createNameNode), this.#arrayNames);
1994
+ node.name ??= this.#allocateName({
1995
+ name,
1996
+ normalize,
1997
+ suffix: 1
1998
+ });
1999
+ return node.name;
2000
+ }
2001
+ #allocateName({ name, normalize, suffix }) {
2002
+ const candidate = suffix === 1 ? name : `${name}${suffix}`;
2003
+ const normalized = normalize(candidate);
2004
+ const names = typeof normalized === "string" ? [normalized] : normalized;
2005
+ if (names.some((emittedName) => this.#usedNames.has(emittedName))) return this.#allocateName({
2006
+ name,
2007
+ normalize,
2008
+ suffix: suffix + 1
2009
+ });
2010
+ this.reserveNames(names);
2011
+ return candidate;
1954
2012
  }
1955
2013
  file(options) {
1956
2014
  return this.#resolveFile(options);
@@ -1967,17 +2025,17 @@ var Resolver = class Resolver {
1967
2025
  * reads the same ref set instead of re-scanning it per plugin.
1968
2026
  */
1969
2027
  imports(options) {
1970
- const { node, root, output, group, extname = ".ts", name, checks } = options;
2028
+ const { node, root, output, extname = ".ts", name, checks } = options;
1971
2029
  const resolveName = name ?? ((schemaName) => this.name(schemaName));
2030
+ const paths = memo$1(memo$1(memo$1(this.#importPaths, output, () => /* @__PURE__ */ new Map()), root, () => /* @__PURE__ */ new Map()), extname, () => /* @__PURE__ */ new Map());
1972
2031
  return _kubb_ast.ast.graph.getImportedNames(node, { checks }).map((schemaName) => _kubb_ast.ast.factory.createImport({
1973
2032
  name: [resolveName(schemaName)],
1974
- path: this.#resolveFilePath({
2033
+ path: memo$1(paths, schemaName, () => this.#resolveFilePath({
1975
2034
  name: schemaName,
1976
2035
  extname,
1977
2036
  root,
1978
- output,
1979
- group
1980
- })
2037
+ output
2038
+ }))
1981
2039
  }));
1982
2040
  }
1983
2041
  /**
@@ -2049,8 +2107,8 @@ var Resolver = class Resolver {
2049
2107
  * are provided.
2050
2108
  */
2051
2109
  #resolvePath({ baseName, tag, path: groupPath, root, output, group }) {
2052
- if (output.mode !== "directory") return node_path.default.resolve(root, output.path);
2053
- const outputDir = node_path.default.resolve(root, output.path);
2110
+ const outputDir = memo$1(memo$1(this.#outputDirs, output, () => /* @__PURE__ */ new Map()), root, () => node_path.default.resolve(root, output.path));
2111
+ if (output.mode !== "directory") return outputDir;
2054
2112
  const groupValue = group?.type === "path" ? groupPath : tag;
2055
2113
  const result = group && groupValue ? node_path.default.resolve(outputDir, this.groupDir(group, groupValue), baseName) : node_path.default.resolve(outputDir, baseName);
2056
2114
  assertInside({
@@ -2328,7 +2386,8 @@ function compareFiles(a, b) {
2328
2386
  /**
2329
2387
  * Writes `source` to `file.path` unless the storage already holds it, either verbatim or, per the
2330
2388
  * manifest, as what the output passes make of it. Checking here rather than in a driver gives a
2331
- * storage writing to S3 or a database the same guarantee `fsStorage` has. `false` when skipped.
2389
+ * storage writing to S3 or a database the same guarantee `fsStorage` has. `written` is `false` when
2390
+ * skipped, and `stored` is what the storage held before the write.
2332
2391
  */
2333
2392
  async function writeChanged({ storage, manifest, file, source, signal, cleaned }) {
2334
2393
  signal?.throwIfAborted();
@@ -2340,14 +2399,20 @@ async function writeChanged({ storage, manifest, file, source, signal, cleaned }
2340
2399
  key: file.path,
2341
2400
  source,
2342
2401
  disk: stored
2343
- }))) return false;
2402
+ }))) return {
2403
+ written: false,
2404
+ stored
2405
+ };
2344
2406
  signal?.throwIfAborted();
2345
2407
  await storage.writeItem(file.path, source, { stored });
2346
2408
  manifest?.track({
2347
2409
  key: file.path,
2348
2410
  source
2349
2411
  });
2350
- return true;
2412
+ return {
2413
+ written: true,
2414
+ stored
2415
+ };
2351
2416
  }
2352
2417
  /** Reports finished files in input order: each finished file flushes the run of finished files ahead of it as one update. */
2353
2418
  function createProgress(files, config, hooks) {
@@ -2507,7 +2572,7 @@ var FileManager = class {
2507
2572
  * memory rather than holding every source, while still overlapping each file's write with the
2508
2573
  * next file's parse. Files finish in whatever order they parse, so each finished file flushes the
2509
2574
  * run of finished files ahead of it as one `kubb:files:processing:update`, keeping rows in input order.
2510
- * Each parsed source goes out once on `kubb:file:processed`, so listeners read it without the pool holding it.
2575
+ * Each parsed source goes out once on `kubb:file:processed`, next to what the storage held before the write.
2511
2576
  *
2512
2577
  * A file the storage already holds is skipped, so a rebuild that generates identical output
2513
2578
  * writes nothing and leaves every mtime where it was. `kubb:files:processing:end` lists the
@@ -2525,17 +2590,19 @@ var FileManager = class {
2525
2590
  signal?.throwIfAborted();
2526
2591
  const source = await this.parse(file, { parsers });
2527
2592
  if (source) {
2528
- if (!await writeChanged({
2593
+ const { written, stored } = await writeChanged({
2529
2594
  storage,
2530
2595
  manifest,
2531
2596
  file,
2532
2597
  source,
2533
2598
  signal,
2534
2599
  cleaned
2535
- })) unchanged.push(file.path);
2600
+ });
2601
+ if (!written) unchanged.push(file.path);
2536
2602
  await hooks?.callHook("kubb:file:processed", {
2537
2603
  file,
2538
- source
2604
+ source,
2605
+ stored
2539
2606
  });
2540
2607
  }
2541
2608
  await progress?.(index);
@@ -3412,11 +3479,7 @@ const createStorage = (build) => (options) => build(options ?? {});
3412
3479
  const fsStorage = createStorage(() => ({
3413
3480
  name: "fs",
3414
3481
  async readItem(key) {
3415
- try {
3416
- return await read((0, node_path.resolve)(key));
3417
- } catch (_error) {
3418
- return null;
3419
- }
3482
+ return readIfExists((0, node_path.resolve)(key));
3420
3483
  },
3421
3484
  async writeItem(key, value, options) {
3422
3485
  await write((0, node_path.resolve)(key), value, {