@kubb/core 5.18.8 → 5.19.0

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.d.cts CHANGED
@@ -37,6 +37,57 @@ export declare function pascalCase(text: string, { prefix, suffix }?: Options$1)
37
37
  * ```
38
38
  */
39
39
  type PossiblePromise<T> = Promise<T> | T;
40
+ type Store<TKey, TValue> = {
41
+ has(key: TKey): boolean;
42
+ get(key: TKey): TValue | undefined;
43
+ set(key: TKey, value: TValue): unknown;
44
+ };
45
+ /**
46
+ * Returns the value cached under `key` in `store`, computing and storing it on the first call.
47
+ * Pass a `WeakMap` for object keys, so an entry goes away with its key, or a `Map` for primitive
48
+ * keys. A cached `null` or `undefined` counts as a hit, so `compute` runs once per key.
49
+ *
50
+ * @example
51
+ * `const refs = memo(refCache, node, () => collectRefs(node))`
52
+ */
53
+ declare function memo$1<TKey, TValue>(store: Store<TKey, TValue>, key: TKey, compute: () => TValue): TValue;
54
+ declare namespace memo$1 {
55
+ export { createStore as create };
56
+ }
57
+ /**
58
+ * Creates a store for `memo`. With no options it is a `Map`. Each option changes one thing:
59
+ *
60
+ * - `weak` holds the keys weakly, in a `WeakMap`, so an entry goes away with its key.
61
+ * - `max` bounds the store: it empties itself before the entry that would pass `max`, which keeps a
62
+ * cache in a long-running process from growing without end. `max: 1` keeps only the last entry.
63
+ *
64
+ * @example
65
+ * `const patterns = memo.create<string, RegExp>()`
66
+ *
67
+ * @example Freed with its key
68
+ * `const refs = memo.create<SchemaNode, Array<string>>({ weak: true })`
69
+ *
70
+ * @example Bounded
71
+ * `const names = memo.create<string, string>({ max: 100_000 })`
72
+ *
73
+ * @example Many values per key
74
+ * ```ts
75
+ * const filesByPlugin = memo.create<string, Array<string>>()
76
+ * memo(filesByPlugin, 'plugin-ts', () => []).push('models.ts')
77
+ * ```
78
+ */
79
+ declare function createStore<TKey, TValue>(options?: {
80
+ weak?: false;
81
+ max?: undefined;
82
+ }): Map<TKey, TValue>;
83
+ declare function createStore<TKey extends WeakKey, TValue>(options: {
84
+ weak: true;
85
+ max?: undefined;
86
+ }): WeakMap<TKey, TValue>;
87
+ declare function createStore<TKey, TValue>(options: {
88
+ max: number;
89
+ weak?: false;
90
+ }): Store<TKey, TValue>;
40
91
  /** A union of known literals that still accepts any other value of `Base`, without collapsing
41
92
  * the literals away. Editors keep autocompleting the known members while arbitrary strings stay
42
93
  * assignable.
@@ -3344,8 +3395,20 @@ export declare function setMacros(macros: ReadonlyArray<Macro>): void;
3344
3395
  export declare function injectFile(file: UserFileNode): void;
3345
3396
  /**
3346
3397
  * Returns a value computed once per build and shared by every plugin, like `ctx.memo`.
3398
+ *
3399
+ * Pass a store first to cache in it instead, with no build needed: a `Map` or `WeakMap`, or one
3400
+ * from `memo.create`.
3401
+ *
3402
+ * @example Once per build
3403
+ * `const names = memo('myPlugin:names', () => collectNames(schemas))`
3404
+ *
3405
+ * @example In your own store
3406
+ * ```ts
3407
+ * const cache = memo.create<ast.SchemaNode, Array<string>>({ weak: true })
3408
+ * const refs = memo(cache, node, () => collectRefs(node))
3409
+ * ```
3347
3410
  */
3348
- export declare const memo: Memo;
3411
+ export declare const memo: Memo & typeof memo$1;
3349
3412
  /**
3350
3413
  * Returns the options the user passed to the plugin being set up, like `ctx.options`.
3351
3414
  */
package/dist/index.d.ts CHANGED
@@ -37,6 +37,57 @@ export declare function pascalCase(text: string, { prefix, suffix }?: Options$1)
37
37
  * ```
38
38
  */
39
39
  type PossiblePromise<T> = Promise<T> | T;
40
+ type Store<TKey, TValue> = {
41
+ has(key: TKey): boolean;
42
+ get(key: TKey): TValue | undefined;
43
+ set(key: TKey, value: TValue): unknown;
44
+ };
45
+ /**
46
+ * Returns the value cached under `key` in `store`, computing and storing it on the first call.
47
+ * Pass a `WeakMap` for object keys, so an entry goes away with its key, or a `Map` for primitive
48
+ * keys. A cached `null` or `undefined` counts as a hit, so `compute` runs once per key.
49
+ *
50
+ * @example
51
+ * `const refs = memo(refCache, node, () => collectRefs(node))`
52
+ */
53
+ declare function memo$1<TKey, TValue>(store: Store<TKey, TValue>, key: TKey, compute: () => TValue): TValue;
54
+ declare namespace memo$1 {
55
+ export { createStore as create };
56
+ }
57
+ /**
58
+ * Creates a store for `memo`. With no options it is a `Map`. Each option changes one thing:
59
+ *
60
+ * - `weak` holds the keys weakly, in a `WeakMap`, so an entry goes away with its key.
61
+ * - `max` bounds the store: it empties itself before the entry that would pass `max`, which keeps a
62
+ * cache in a long-running process from growing without end. `max: 1` keeps only the last entry.
63
+ *
64
+ * @example
65
+ * `const patterns = memo.create<string, RegExp>()`
66
+ *
67
+ * @example Freed with its key
68
+ * `const refs = memo.create<SchemaNode, Array<string>>({ weak: true })`
69
+ *
70
+ * @example Bounded
71
+ * `const names = memo.create<string, string>({ max: 100_000 })`
72
+ *
73
+ * @example Many values per key
74
+ * ```ts
75
+ * const filesByPlugin = memo.create<string, Array<string>>()
76
+ * memo(filesByPlugin, 'plugin-ts', () => []).push('models.ts')
77
+ * ```
78
+ */
79
+ declare function createStore<TKey, TValue>(options?: {
80
+ weak?: false;
81
+ max?: undefined;
82
+ }): Map<TKey, TValue>;
83
+ declare function createStore<TKey extends WeakKey, TValue>(options: {
84
+ weak: true;
85
+ max?: undefined;
86
+ }): WeakMap<TKey, TValue>;
87
+ declare function createStore<TKey, TValue>(options: {
88
+ max: number;
89
+ weak?: false;
90
+ }): Store<TKey, TValue>;
40
91
  /** A union of known literals that still accepts any other value of `Base`, without collapsing
41
92
  * the literals away. Editors keep autocompleting the known members while arbitrary strings stay
42
93
  * assignable.
@@ -3344,8 +3395,20 @@ export declare function setMacros(macros: ReadonlyArray<Macro>): void;
3344
3395
  export declare function injectFile(file: UserFileNode): void;
3345
3396
  /**
3346
3397
  * Returns a value computed once per build and shared by every plugin, like `ctx.memo`.
3398
+ *
3399
+ * Pass a store first to cache in it instead, with no build needed: a `Map` or `WeakMap`, or one
3400
+ * from `memo.create`.
3401
+ *
3402
+ * @example Once per build
3403
+ * `const names = memo('myPlugin:names', () => collectNames(schemas))`
3404
+ *
3405
+ * @example In your own store
3406
+ * ```ts
3407
+ * const cache = memo.create<ast.SchemaNode, Array<string>>({ weak: true })
3408
+ * const refs = memo(cache, node, () => collectRefs(node))
3409
+ * ```
3347
3410
  */
3348
- export declare const memo: Memo;
3411
+ export declare const memo: Memo & typeof memo$1;
3349
3412
  /**
3350
3413
  * Returns the options the user passed to the plugin being set up, like `ctx.options`.
3351
3414
  */
package/dist/index.js CHANGED
@@ -21,7 +21,65 @@ var __name = (target, value) => __defProp(target, "name", {
21
21
  configurable: true
22
22
  });
23
23
  // package.json
24
- var version = "5.18.8";
24
+ var version = "5.19.0";
25
+ // ../../internals/utils/src/promise.ts
26
+ /**
27
+ * Returns the value cached under `key` in `store`, computing and storing it on the first call.
28
+ * Pass a `WeakMap` for object keys, so an entry goes away with its key, or a `Map` for primitive
29
+ * keys. A cached `null` or `undefined` counts as a hit, so `compute` runs once per key.
30
+ *
31
+ * @example
32
+ * `const refs = memo(refCache, node, () => collectRefs(node))`
33
+ */
34
+ function memo$1(store, key, compute) {
35
+ const cached = store.get(key);
36
+ if (cached !== void 0 || store.has(key)) return cached;
37
+ const value = compute();
38
+ store.set(key, value);
39
+ return value;
40
+ }
41
+ __name(memo$1, "memo");
42
+ function createStore({ weak, max } = {}) {
43
+ if (weak) return /* @__PURE__ */ new WeakMap();
44
+ const entry = /* @__PURE__ */ new Map();
45
+ if (max === void 0) return entry;
46
+ const set = (key, value) => {
47
+ if (entry.size >= max) entry.clear();
48
+ entry.set(key, value);
49
+ };
50
+ if (max !== 1) return {
51
+ has: (key) => entry.has(key),
52
+ get: (key) => entry.get(key),
53
+ set
54
+ };
55
+ const touch = (key) => {
56
+ if (!entry.has(key)) return false;
57
+ set(key, entry.get(key));
58
+ return true;
59
+ };
60
+ return {
61
+ has: touch,
62
+ get: (key) => touch(key) ? entry.get(key) : void 0,
63
+ set
64
+ };
65
+ }
66
+ memo$1.create = createStore;
67
+ /**
68
+ * Runs `run` over every item with at most `limit` in flight. Workers share one iterator, so each
69
+ * takes the next item the moment it frees up instead of waiting for a batch to drain.
70
+ *
71
+ * @example
72
+ * ```ts
73
+ * await inParallel({ items: files, limit: 50, run: (file) => storage.writeItem(file.path, file.source) })
74
+ * ```
75
+ */
76
+ async function inParallel({ items, limit, run }) {
77
+ const queue = items.entries();
78
+ const worker = async () => {
79
+ for (const [index, item] of queue) await run(item, index);
80
+ };
81
+ await Promise.all(Array.from({ length: Math.min(limit, items.length) }, () => worker()));
82
+ }
25
83
  // ../../internals/utils/src/casing.ts
26
84
  /**
27
85
  * Shared implementation for camelCase and PascalCase conversion.
@@ -36,16 +94,10 @@ function convert(text, pascal) {
36
94
  return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
37
95
  }).join("").replace(/[^a-zA-Z0-9]/g, "");
38
96
  }
39
- const cache = /* @__PURE__ */ new Map();
40
- const cacheLimit = 1e5;
97
+ const cache = memo$1.create({ max: 1e5 });
41
98
  function toCamelOrPascal(text, pascal) {
42
99
  const key = `${pascal ? "P" : "c"}${text}`;
43
- const cached = cache.get(key);
44
- if (cached !== void 0) return cached;
45
- const result = convert(text, pascal);
46
- if (cache.size >= cacheLimit) cache.clear();
47
- cache.set(key, result);
48
- return result;
100
+ return memo$1(cache, key, () => convert(text, pascal));
49
101
  }
50
102
  /**
51
103
  * Converts `text` to camelCase.
@@ -342,59 +394,6 @@ function isPathInside(path, parent) {
342
394
  const rel = relative(resolvedParent, resolvedPath);
343
395
  return rel !== "" && !rel.startsWith("..") && !isAbsolute(rel);
344
396
  }
345
- // ../../internals/utils/src/promise.ts
346
- /**
347
- * Returns the value cached under `key` in `store`, computing and storing it on the first call.
348
- * Pass a `WeakMap` for object keys, so an entry goes away with its key, or a `Map` for primitive
349
- * keys. A cached `null` or `undefined` counts as a hit, so `compute` runs once per key.
350
- *
351
- * @example
352
- * `const refs = memo(refCache, node, () => collectRefs(node))`
353
- */
354
- function memo$1(store, key, compute) {
355
- const cached = store.get(key);
356
- if (cached !== void 0 || store.has(key)) return cached;
357
- const value = compute();
358
- store.set(key, value);
359
- return value;
360
- }
361
- __name(memo$1, "memo");
362
- function createStore({ type }) {
363
- if (type === "weak") return /* @__PURE__ */ new WeakMap();
364
- const entry = /* @__PURE__ */ new Map();
365
- if (type === "map") return entry;
366
- const set = (key, value) => {
367
- entry.clear();
368
- entry.set(key, value);
369
- };
370
- const touch = (key) => {
371
- if (!entry.has(key)) return false;
372
- set(key, entry.get(key));
373
- return true;
374
- };
375
- return {
376
- has: touch,
377
- get: (key) => touch(key) ? entry.get(key) : void 0,
378
- set
379
- };
380
- }
381
- memo$1.create = createStore;
382
- /**
383
- * Runs `run` over every item with at most `limit` in flight. Workers share one iterator, so each
384
- * takes the next item the moment it frees up instead of waiting for a batch to drain.
385
- *
386
- * @example
387
- * ```ts
388
- * await inParallel({ items: files, limit: 50, run: (file) => storage.writeItem(file.path, file.source) })
389
- * ```
390
- */
391
- async function inParallel({ items, limit, run }) {
392
- const queue = items.entries();
393
- const worker = async () => {
394
- for (const [index, item] of queue) await run(item, index);
395
- };
396
- await Promise.all(Array.from({ length: Math.min(limit, items.length) }, () => worker()));
397
- }
398
397
  // ../../internals/utils/src/tools.ts
399
398
  /**
400
399
  * CLI command descriptors for each supported code formatter.
@@ -1394,8 +1393,20 @@ function injectFile(file) {
1394
1393
  }
1395
1394
  /**
1396
1395
  * Returns a value computed once per build and shared by every plugin, like `ctx.memo`.
1396
+ *
1397
+ * Pass a store first to cache in it instead, with no build needed: a `Map` or `WeakMap`, or one
1398
+ * from `memo.create`.
1399
+ *
1400
+ * @example Once per build
1401
+ * `const names = memo('myPlugin:names', () => collectNames(schemas))`
1402
+ *
1403
+ * @example In your own store
1404
+ * ```ts
1405
+ * const cache = memo.create<ast.SchemaNode, Array<string>>({ weak: true })
1406
+ * const refs = memo(cache, node, () => collectRefs(node))
1407
+ * ```
1397
1408
  */
1398
- const memo = (key, compute) => requireContext("memo").memo(key, compute);
1409
+ const memo = Object.assign((...args) => args.length === 3 ? memo$1(...args) : requireContext("memo").memo(...args), { create: memo$1.create });
1399
1410
  /**
1400
1411
  * Returns the options the user passed to the plugin being set up, like `ctx.options`.
1401
1412
  */
@@ -1756,9 +1767,9 @@ function inputToAdapterSource(config) {
1756
1767
  };
1757
1768
  }
1758
1769
  // src/resolveOptions.ts
1759
- const patterns = /* @__PURE__ */ new Map();
1760
- const methodPatterns = /* @__PURE__ */ new Map();
1761
- const nodeCaches = /* @__PURE__ */ new WeakMap();
1770
+ const patterns = memo$1.create();
1771
+ const methodPatterns = memo$1.create();
1772
+ const nodeCaches = memo$1.create({ weak: true });
1762
1773
  function testPattern(value, pattern) {
1763
1774
  if (typeof pattern === "string") return memo$1(patterns, pattern, () => new RegExp(pattern)).test(value);
1764
1775
  return value.match(pattern) !== null;
@@ -1889,12 +1900,12 @@ var Resolver = class Resolver {
1889
1900
  /** The built-in machinery, untouched by `name`/`file` overrides and built once since the driver reads it per node. */
1890
1901
  default;
1891
1902
  #options;
1892
- #memo = /* @__PURE__ */ new Map();
1903
+ #memo = memo$1.create();
1893
1904
  #usedNames = /* @__PURE__ */ new Set();
1894
- #stringNames = /* @__PURE__ */ new Map();
1905
+ #stringNames = memo$1.create();
1895
1906
  #arrayNames = createNameNode();
1896
- #outputDirs = /* @__PURE__ */ new WeakMap();
1897
- #importPaths = /* @__PURE__ */ new WeakMap();
1907
+ #outputDirs = memo$1.create({ weak: true });
1908
+ #importPaths = memo$1.create({ weak: true });
1898
1909
  #tagParents = /* @__PURE__ */ new Map();
1899
1910
  #baseName;
1900
1911
  #filePath;
@@ -2701,7 +2712,7 @@ var KubbDriver = class {
2701
2712
  */
2702
2713
  fileManager = new FileManager();
2703
2714
  plugins = /* @__PURE__ */ new Map();
2704
- #memo = /* @__PURE__ */ new Map();
2715
+ #memo = memo$1.create();
2705
2716
  /**
2706
2717
  * Removers for every listener the driver added (plugin, generator) so `dispose()` can detach
2707
2718
  * them in one pass. External `hooks.hook(...)` listeners are not tracked.
@@ -3543,7 +3554,7 @@ const cacheStorage = createStorage(({ root = process.cwd() }) => {
3543
3554
  * ```
3544
3555
  */
3545
3556
  var Hookable = class {
3546
- #listeners = /* @__PURE__ */ new Map();
3557
+ #listeners = memo$1.create();
3547
3558
  /**
3548
3559
  * Calls `hookName` and awaits all registered listeners sequentially.
3549
3560
  * Throws if any listener rejects, wrapping the cause in an error that names the hook.
@@ -3575,9 +3586,7 @@ var Hookable = class {
3575
3586
  * ```
3576
3587
  */
3577
3588
  hook(hookName, handler) {
3578
- let listeners = this.#listeners.get(hookName);
3579
- if (!listeners) this.#listeners.set(hookName, listeners = /* @__PURE__ */ new Set());
3580
- listeners.add(handler);
3589
+ memo$1(this.#listeners, hookName, () => /* @__PURE__ */ new Set()).add(handler);
3581
3590
  return () => this.removeHook(hookName, handler);
3582
3591
  }
3583
3592
  /**