@kubb/core 5.0.0-beta.20 → 5.0.0-beta.22

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/src/KubbDriver.ts CHANGED
@@ -1,15 +1,18 @@
1
1
  import { resolve } from 'node:path'
2
- import { arrayToAsyncIterable, type AsyncEventEmitter, memoize, URLPath } from '@internals/utils'
3
- import { createFile, createStreamInput } from '@kubb/ast'
2
+ import { arrayToAsyncIterable, type AsyncEventEmitter, forBatches, formatMs, getElapsedMs, isPromise, memoize, URLPath } from '@internals/utils'
3
+ import { collectUsedSchemaNames, createFile, createStreamInput, transform } from '@kubb/ast'
4
4
  import type { FileNode, InputMeta, InputNode, InputStreamNode, OperationNode, SchemaNode } from '@kubb/ast'
5
- import { DEFAULT_STUDIO_URL } from './constants.ts'
5
+ import { DEFAULT_STUDIO_URL, SCHEMA_PARALLEL, STREAM_FLUSH_EVERY } from './constants.ts'
6
+ import type { Storage } from './createStorage.ts'
6
7
  import type { Generator } from './defineGenerator.ts'
8
+ import type { Parser } from './defineParser.ts'
7
9
  import type { Plugin } from './definePlugin.ts'
8
10
  import { getMode } from './definePlugin.ts'
9
11
  import { defineResolver } from './defineResolver.ts'
10
12
  import { openInStudio as openInStudioFn } from './devtools.ts'
11
13
  import { FileManager } from './FileManager.ts'
12
- import type { RendererFactory } from './createRenderer.ts'
14
+ import { FileProcessor } from './FileProcessor.ts'
15
+ import type { Renderer, RendererFactory } from './createRenderer.ts'
13
16
 
14
17
  import type {
15
18
  Adapter,
@@ -33,6 +36,8 @@ function enforceOrder(enforce: 'pre' | 'post' | undefined): number {
33
36
  return enforce === 'pre' ? -1 : enforce === 'post' ? 1 : 0
34
37
  }
35
38
 
39
+ const OPERATION_FILTER_TYPES = new Set(['tag', 'operationId', 'path', 'method', 'contentType'])
40
+
36
41
  export class KubbDriver {
37
42
  readonly config: Config
38
43
  readonly options: Options
@@ -54,8 +59,8 @@ export class KubbDriver {
54
59
  * The streaming `InputStreamNode` produced by the adapter.
55
60
  * Always set after adapter setup — parse-only adapters are wrapped automatically.
56
61
  */
57
- inputNode: InputStreamNode | undefined = undefined
58
- adapter: Adapter | undefined = undefined
62
+ inputNode: InputStreamNode | null = null
63
+ adapter: Adapter | null = null
59
64
  /**
60
65
  * Studio session state, kept together so `dispose()` can reset it atomically.
61
66
  *
@@ -65,10 +70,10 @@ export class KubbDriver {
65
70
  * - `inputNode` caches the parse promise so `adapter.parse()` is called at most once
66
71
  * per studio session, even when `openInStudio()` is called multiple times.
67
72
  */
68
- #studio: { source: AdapterSource | undefined; isOpen: boolean; inputNode: Promise<InputNode> | undefined } = {
69
- source: undefined,
73
+ #studio: { source: AdapterSource | null; isOpen: boolean; inputNode: Promise<InputNode> | null } = {
74
+ source: null,
70
75
  isOpen: false,
71
- inputNode: undefined,
76
+ inputNode: null,
72
77
  }
73
78
 
74
79
  // Register middleware hooks after all plugin hooks are registered.
@@ -84,6 +89,7 @@ export class KubbDriver {
84
89
  * add files; this property gives direct read/write access when needed.
85
90
  */
86
91
  readonly fileManager = new FileManager()
92
+ readonly #fileProcessor = new FileProcessor()
87
93
 
88
94
  readonly plugins = new Map<string, NormalizedPlugin>()
89
95
 
@@ -99,7 +105,7 @@ export class KubbDriver {
99
105
  constructor(config: Config, options: Options) {
100
106
  this.config = config
101
107
  this.options = options
102
- this.adapter = config.adapter
108
+ this.adapter = config.adapter ?? null
103
109
  }
104
110
 
105
111
  async setup() {
@@ -348,6 +354,347 @@ export class KubbDriver {
348
354
  return this.#eventGeneratorPlugins.has(pluginName)
349
355
  }
350
356
 
357
+ /**
358
+ * Runs the full plugin pipeline. Returns timings/failures collected so far even
359
+ * when an outer hook throws — the orchestrator preserves partial state by capturing
360
+ * the error into `error` instead of propagating.
361
+ */
362
+ async run({ storage }: { storage: Storage }): Promise<{
363
+ failedPlugins: Set<{ plugin: Plugin; error: Error }>
364
+ pluginTimings: Map<string, number>
365
+ error?: Error
366
+ }> {
367
+ const hooks = this.hooks
368
+ const config = this.config
369
+ const failedPlugins = new Set<{ plugin: Plugin; error: Error }>()
370
+ const pluginTimings = new Map<string, number>()
371
+ const parsersMap = new Map<FileNode['extname'], Parser>()
372
+
373
+ for (const parser of config.parsers) {
374
+ if (parser.extNames) {
375
+ for (const ext of parser.extNames) parsersMap.set(ext, parser)
376
+ }
377
+ }
378
+
379
+ const pendingFiles = new Map<string, FileNode>()
380
+ this.fileManager.setOnUpsert((file) => {
381
+ pendingFiles.set(file.path, file)
382
+ })
383
+
384
+ try {
385
+ const flushPending = async (): Promise<void> => {
386
+ if (pendingFiles.size === 0) return
387
+ const files = [...pendingFiles.values()]
388
+ pendingFiles.clear()
389
+
390
+ await hooks.emit('kubb:debug', { date: new Date(), logs: [`Writing ${files.length} files...`] })
391
+ await hooks.emit('kubb:files:processing:start', { files })
392
+
393
+ const items = [...this.#fileProcessor.stream(files, { parsers: parsersMap, extension: config.output.extension })]
394
+
395
+ await hooks.emit('kubb:files:processing:update', {
396
+ files: items.map(({ file, source, processed, total, percentage }) => ({ file, source, processed, total, percentage, config })),
397
+ })
398
+
399
+ const queue: Array<Promise<void>> = []
400
+ for (const { file, source } of items) {
401
+ if (source) {
402
+ queue.push(storage.setItem(file.path, source))
403
+ if (queue.length >= STREAM_FLUSH_EVERY) await Promise.all(queue.splice(0))
404
+ }
405
+ }
406
+ await Promise.all(queue)
407
+
408
+ await hooks.emit('kubb:files:processing:end', { files })
409
+ await hooks.emit('kubb:debug', { date: new Date(), logs: [`✓ File write process completed for ${files.length} files`] })
410
+ }
411
+
412
+ await this.emitSetupHooks()
413
+
414
+ if (this.adapter && this.inputNode) {
415
+ await hooks.emit(
416
+ 'kubb:build:start',
417
+ Object.assign({ config, adapter: this.adapter, meta: this.inputNode.meta, getPlugin: this.getPlugin.bind(this) }, this.#filesPayload()),
418
+ )
419
+ }
420
+
421
+ const generatorPlugins: Array<{ plugin: NormalizedPlugin; context: GeneratorContext; hrStart: ReturnType<typeof process.hrtime> }> = []
422
+
423
+ for (const plugin of this.plugins.values()) {
424
+ const context = this.getContext(plugin)
425
+ const hrStart = process.hrtime()
426
+
427
+ try {
428
+ await hooks.emit('kubb:plugin:start', { plugin })
429
+ await hooks.emit('kubb:debug', { date: new Date(), logs: ['Starting plugin...', ` • Plugin Name: ${plugin.name}`] })
430
+ } catch (caughtError) {
431
+ const error = caughtError as Error
432
+ const duration = getElapsedMs(hrStart)
433
+ pluginTimings.set(plugin.name, duration)
434
+ await this.#emitPluginEnd({ plugin, duration, success: false, error })
435
+ failedPlugins.add({ plugin, error })
436
+ continue
437
+ }
438
+
439
+ if (plugin.generators?.length || this.hasEventGenerators(plugin.name)) {
440
+ generatorPlugins.push({ plugin, context, hrStart })
441
+ continue
442
+ }
443
+
444
+ const duration = getElapsedMs(hrStart)
445
+ pluginTimings.set(plugin.name, duration)
446
+ await this.#emitPluginEnd({ plugin, duration, success: true })
447
+ await hooks.emit('kubb:debug', { date: new Date(), logs: [`✓ Plugin started successfully (${formatMs(duration)})`] })
448
+ }
449
+
450
+ if (generatorPlugins.length > 0) {
451
+ if (this.inputNode) {
452
+ const { timings, failed } = await this.#runGenerators(generatorPlugins, flushPending)
453
+ // Drain any files written after the last batch's flush.
454
+ await flushPending()
455
+ for (const [name, duration] of timings) pluginTimings.set(name, duration)
456
+ for (const entry of failed) failedPlugins.add(entry)
457
+ } else {
458
+ // No adapter input: generator-plugins have nothing to dispatch, but still
459
+ // need their `kubb:plugin:end` so middleware (e.g. barrel) completes.
460
+ for (const { plugin, hrStart } of generatorPlugins) {
461
+ const duration = getElapsedMs(hrStart)
462
+ pluginTimings.set(plugin.name, duration)
463
+ await this.#emitPluginEnd({ plugin, duration, success: true })
464
+ }
465
+ }
466
+ }
467
+
468
+ await hooks.emit('kubb:plugins:end', Object.assign({ config }, this.#filesPayload()))
469
+
470
+ await flushPending()
471
+
472
+ const files = this.fileManager.files
473
+
474
+ await hooks.emit('kubb:build:end', { files, config, outputDir: resolve(config.root, config.output.path) })
475
+
476
+ return { failedPlugins, pluginTimings }
477
+ } catch (caughtError) {
478
+ return { failedPlugins, pluginTimings, error: caughtError as Error }
479
+ } finally {
480
+ this.fileManager.setOnUpsert(null)
481
+ }
482
+ }
483
+
484
+ // Returns a fresh object with a lazy `files` getter and a bound `upsertFile`.
485
+ // Caller must use `Object.assign(extra, this.#filesPayload())`, not object spread —
486
+ // spread would eagerly invoke the getter and freeze a stale snapshot into the payload.
487
+ #filesPayload(): { readonly files: Array<FileNode>; upsertFile: (...files: Array<FileNode>) => Array<FileNode> } {
488
+ const driver = this
489
+ return {
490
+ get files() {
491
+ return driver.fileManager.files
492
+ },
493
+ upsertFile: (...files: Array<FileNode>) => driver.fileManager.upsert(...files),
494
+ }
495
+ }
496
+
497
+ #emitPluginEnd({ plugin, duration, success, error }: { plugin: NormalizedPlugin; duration: number; success: boolean; error?: Error }): Promise<void> | void {
498
+ return this.hooks.emit(
499
+ 'kubb:plugin:end',
500
+ Object.assign({ plugin, duration, success, ...(error ? { error } : {}), config: this.config }, this.#filesPayload()),
501
+ )
502
+ }
503
+
504
+ async #runGenerators(
505
+ entries: Array<{ plugin: NormalizedPlugin; context: GeneratorContext; hrStart: ReturnType<typeof process.hrtime> }>,
506
+ flushPending: () => Promise<void>,
507
+ ): Promise<{ timings: Map<string, number>; failed: Set<{ plugin: Plugin; error: Error }> }> {
508
+ const timings = new Map<string, number>()
509
+ const failed = new Set<{ plugin: Plugin; error: Error }>()
510
+ type PluginState = {
511
+ plugin: NormalizedPlugin
512
+ generatorContext: GeneratorContext
513
+ generators: Generator[]
514
+ hrStart: ReturnType<typeof process.hrtime>
515
+ failed: boolean
516
+ error: Error | null
517
+ optionsAreStatic: boolean
518
+ allowedSchemaNames: Set<string> | null
519
+ }
520
+
521
+ const driver = this
522
+ const { schemas, operations } = this.inputNode!
523
+ const states: PluginState[] = entries.map(({ plugin, context, hrStart }) => {
524
+ const { exclude, include, override } = plugin.options
525
+ const hasExclude = Array.isArray(exclude) && exclude.length > 0
526
+ const hasInclude = Array.isArray(include) && include.length > 0
527
+ const hasOverride = Array.isArray(override) && override.length > 0
528
+ return {
529
+ plugin,
530
+ generatorContext: { ...context, resolver: this.getResolver(plugin.name) },
531
+ generators: plugin.generators ?? [],
532
+ hrStart,
533
+ failed: false,
534
+ error: null,
535
+ optionsAreStatic: !hasExclude && !hasInclude && !hasOverride,
536
+ allowedSchemaNames: null,
537
+ }
538
+ })
539
+
540
+ const emitsSchemaHook = this.hooks.listenerCount('kubb:generate:schema') > 0
541
+ const emitsOperationHook = this.hooks.listenerCount('kubb:generate:operation') > 0
542
+
543
+ // Pre-scan: plugins with operation-based includes (but no schemaName include) need
544
+ // the reachable schema set. This requires the full schema graph in memory at once —
545
+ // transitive reachability can't be derived from a single node. `allSchemas` is
546
+ // released as soon as the pre-scan returns; the main passes get fresh iterators.
547
+ const pruningStates = states.filter(({ plugin }) => {
548
+ const { include } = plugin.options
549
+ return (include?.some(({ type }) => OPERATION_FILTER_TYPES.has(type)) ?? false) && !(include?.some(({ type }) => type === 'schemaName') ?? false)
550
+ })
551
+
552
+ if (pruningStates.length > 0) {
553
+ const allSchemas: SchemaNode[] = []
554
+ for await (const schema of schemas) allSchemas.push(schema)
555
+
556
+ const includedOpsByState = new Map<PluginState, OperationNode[]>(pruningStates.map((s) => [s, []]))
557
+ for await (const operation of operations) {
558
+ for (const state of pruningStates) {
559
+ const { exclude, include, override } = state.plugin.options
560
+ const options = state.generatorContext.resolver.resolveOptions(operation, { options: state.plugin.options, exclude, include, override })
561
+ if (options !== null) includedOpsByState.get(state)?.push(operation)
562
+ }
563
+ }
564
+
565
+ for (const state of pruningStates) {
566
+ state.allowedSchemaNames = collectUsedSchemaNames(includedOpsByState.get(state) ?? [], allSchemas)
567
+ includedOpsByState.delete(state)
568
+ }
569
+ }
570
+
571
+ const resolveRendererFor = (gen: Generator, state: PluginState): RendererFactory | undefined =>
572
+ gen.renderer === null ? undefined : (gen.renderer ?? state.plugin.renderer ?? state.generatorContext.config.renderer)
573
+
574
+ const dispatchSchema = async (state: PluginState, node: SchemaNode): Promise<void> => {
575
+ if (state.failed) return
576
+ try {
577
+ const { plugin, generatorContext, generators } = state
578
+ const transformedNode = plugin.transformer ? transform(node, plugin.transformer) : node
579
+
580
+ if (state.allowedSchemaNames !== null && transformedNode.name && !state.allowedSchemaNames.has(transformedNode.name)) {
581
+ return
582
+ }
583
+
584
+ const { exclude, include, override } = plugin.options
585
+ const options = state.optionsAreStatic
586
+ ? plugin.options
587
+ : generatorContext.resolver.resolveOptions(transformedNode, { options: plugin.options, exclude, include, override })
588
+ if (options === null) return
589
+
590
+ const ctx = { ...generatorContext, options }
591
+ for (const gen of generators) {
592
+ if (!gen.schema) continue
593
+ const raw = gen.schema(transformedNode, ctx)
594
+ const result = isPromise(raw) ? await raw : raw
595
+ const applied = applyHookResult({ result, driver, rendererFactory: resolveRendererFor(gen, state) })
596
+ if (isPromise(applied)) await applied
597
+ }
598
+ if (emitsSchemaHook) await this.hooks.emit('kubb:generate:schema', transformedNode, ctx)
599
+ } catch (caughtError) {
600
+ state.failed = true
601
+ state.error = caughtError as Error
602
+ }
603
+ }
604
+
605
+ const dispatchOperation = async (state: PluginState, node: OperationNode): Promise<void> => {
606
+ if (state.failed) return
607
+ try {
608
+ const { plugin, generatorContext, generators } = state
609
+ const transformedNode = plugin.transformer ? transform(node, plugin.transformer) : node
610
+ const { exclude, include, override } = plugin.options
611
+ const options = state.optionsAreStatic
612
+ ? plugin.options
613
+ : generatorContext.resolver.resolveOptions(transformedNode, { options: plugin.options, exclude, include, override })
614
+ if (options === null) return
615
+
616
+ const ctx = { ...generatorContext, options }
617
+ for (const gen of generators) {
618
+ if (!gen.operation) continue
619
+ const raw = gen.operation(transformedNode, ctx)
620
+ const result = isPromise(raw) ? await raw : raw
621
+ const applied = applyHookResult({ result, driver, rendererFactory: resolveRendererFor(gen, state) })
622
+ if (isPromise(applied)) await applied
623
+ }
624
+ if (emitsOperationHook) await this.hooks.emit('kubb:generate:operation', transformedNode, ctx)
625
+ } catch (caughtError) {
626
+ state.failed = true
627
+ state.error = caughtError as Error
628
+ }
629
+ }
630
+
631
+ // Skip building the aggregated operations array when nothing consumes it.
632
+ // Saves an N-sized allocation that lives until the build ends, on the common
633
+ // path where plugins only define per-node `gen.operation`.
634
+ const needsCollectedOperations = this.hooks.listenerCount('kubb:generate:operations') > 0 || states.some((s) => s.generators.some((g) => !!g.operations))
635
+ const collectedOperations: OperationNode[] = needsCollectedOperations ? [] : (undefined as never)
636
+
637
+ // Run schemas before operations: the two passes share `flushPending` and the
638
+ // FileProcessor's event emitter, so running them concurrently would interleave
639
+ // `kubb:files:processing:start|end` events and race on the shared dirty list.
640
+ await forBatches(schemas, (nodes) => Promise.all(nodes.flatMap((n) => states.map((state) => dispatchSchema(state, n)))), {
641
+ concurrency: SCHEMA_PARALLEL,
642
+ flush: flushPending,
643
+ })
644
+
645
+ await forBatches(
646
+ operations,
647
+ (nodes) => {
648
+ if (needsCollectedOperations) collectedOperations.push(...nodes)
649
+ return Promise.all(nodes.flatMap((n) => states.map((state) => dispatchOperation(state, n))))
650
+ },
651
+ { concurrency: SCHEMA_PARALLEL, flush: flushPending },
652
+ )
653
+
654
+ for (const state of states) {
655
+ if (!state.failed && needsCollectedOperations) {
656
+ try {
657
+ const { plugin, generatorContext, generators } = state
658
+ const ctx = { ...generatorContext, options: plugin.options }
659
+ // Filter to operations this plugin would have dispatched to gen.operation():
660
+ // excludes/includes/overrides that resolve to null in dispatchOperation must also
661
+ // be hidden from the batched gen.operations() hook, otherwise grouped/barrel
662
+ // generators emit references to operation files that the per-op hook intentionally skipped.
663
+ const pluginOperations = state.optionsAreStatic
664
+ ? collectedOperations
665
+ : collectedOperations.filter((node) => {
666
+ const transformed = plugin.transformer ? transform(node, plugin.transformer) : node
667
+ const { exclude, include, override } = plugin.options
668
+
669
+ return generatorContext.resolver.resolveOptions(transformed, { options: plugin.options, exclude, include, override }) !== null
670
+ })
671
+ for (const gen of generators) {
672
+ if (!gen.operations) continue
673
+ const result = await gen.operations(pluginOperations, ctx)
674
+ await applyHookResult({ result, driver, rendererFactory: resolveRendererFor(gen, state) })
675
+ }
676
+ await this.hooks.emit('kubb:generate:operations', pluginOperations, ctx)
677
+ } catch (caughtError) {
678
+ state.failed = true
679
+ state.error = caughtError as Error
680
+ }
681
+ }
682
+
683
+ const duration = getElapsedMs(state.hrStart)
684
+ timings.set(state.plugin.name, duration)
685
+ await this.#emitPluginEnd({ plugin: state.plugin, duration, success: !state.failed, error: state.failed && state.error ? state.error : undefined })
686
+
687
+ if (state.failed && state.error) failed.add({ plugin: state.plugin, error: state.error })
688
+
689
+ await this.hooks.emit('kubb:debug', {
690
+ date: new Date(),
691
+ logs: [state.failed ? '✗ Plugin start failed' : `✓ Plugin started successfully (${formatMs(duration)})`],
692
+ })
693
+ }
694
+
695
+ return { timings, failed }
696
+ }
697
+
351
698
  /**
352
699
  * Unregisters all plugin lifecycle listeners from the shared event emitter.
353
700
  * Called at the end of a build to prevent listener leaks across repeated builds.
@@ -371,8 +718,9 @@ export class KubbDriver {
371
718
  // memory is reclaimed between builds. The returned `BuildOutput.files`
372
719
  // array still references any FileNodes the caller needs to inspect.
373
720
  this.fileManager.dispose()
374
- this.inputNode = undefined
375
- this.#studio = { source: undefined, isOpen: false, inputNode: undefined }
721
+ this.#fileProcessor.dispose()
722
+ this.inputNode = null
723
+ this.#studio = { source: null, isOpen: false, inputNode: null }
376
724
 
377
725
  for (const [event, handler] of this.#middlewareListeners) {
378
726
  this.hooks.off(event, handler as never)
@@ -450,7 +798,7 @@ export class KubbDriver {
450
798
  get meta(): InputMeta {
451
799
  return driver.inputNode?.meta ?? { circularNames: [], enumNames: [] }
452
800
  },
453
- get adapter(): Adapter | undefined {
801
+ get adapter(): Adapter | null {
454
802
  return driver.adapter
455
803
  },
456
804
  get resolver() {
@@ -529,7 +877,7 @@ export function applyHookResult<TElement = unknown>({
529
877
  }: {
530
878
  result: TElement | Array<FileNode> | void
531
879
  driver: KubbDriver
532
- rendererFactory?: RendererFactory<TElement>
880
+ rendererFactory?: RendererFactory<TElement> | null
533
881
  }): void | Promise<void> {
534
882
  if (!result) return
535
883
 
@@ -544,27 +892,19 @@ export function applyHookResult<TElement = unknown>({
544
892
 
545
893
  const renderer = rendererFactory()
546
894
  if (renderer.stream) {
547
- for (const file of renderer.stream(result)) {
895
+ using r = renderer
896
+ for (const file of r.stream!(result)) {
548
897
  driver.fileManager.upsert(file)
549
898
  }
550
- renderer.unmount()
551
899
  return
552
900
  }
553
901
  return applyAsyncRender({ renderer, result, driver })
554
902
  }
555
903
 
556
- async function applyAsyncRender<TElement>({
557
- renderer,
558
- result,
559
- driver,
560
- }: {
561
- renderer: { render(el: TElement): Promise<void>; files: ReadonlyArray<FileNode>; unmount(): void }
562
- result: TElement
563
- driver: KubbDriver
564
- }): Promise<void> {
565
- await renderer.render(result)
566
- driver.fileManager.upsert(...renderer.files)
567
- renderer.unmount()
904
+ async function applyAsyncRender<TElement>({ renderer, result, driver }: { renderer: Renderer<TElement>; result: TElement; driver: KubbDriver }): Promise<void> {
905
+ using r = renderer
906
+ await r.render(result)
907
+ driver.fileManager.upsert(...r.files)
568
908
  }
569
909
 
570
910
  function inputToAdapterSource(config: Config): AdapterSource {