@spine-event-engine/proto-tools 2.0.0-snapshot.25 → 2.0.0-snapshot.26

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 (42) hide show
  1. package/README.md +10 -3
  2. package/REFERENCE.md +55 -3
  3. package/dist/src/generation/app-query-generator.d.ts +33 -0
  4. package/dist/src/generation/app-query-generator.d.ts.map +1 -0
  5. package/dist/src/generation/app-query-generator.js +73 -0
  6. package/dist/src/generation/app-query-generator.js.map +1 -0
  7. package/dist/src/generation/build-time-handler-analyzer.d.ts +53 -1
  8. package/dist/src/generation/build-time-handler-analyzer.d.ts.map +1 -1
  9. package/dist/src/generation/build-time-handler-analyzer.js +414 -9
  10. package/dist/src/generation/build-time-handler-analyzer.js.map +1 -1
  11. package/dist/src/generation/entity-query-generator.d.ts.map +1 -1
  12. package/dist/src/generation/entity-query-generator.js +3 -0
  13. package/dist/src/generation/entity-query-generator.js.map +1 -1
  14. package/dist/src/generation/generated-registry-writer.d.ts +61 -0
  15. package/dist/src/generation/generated-registry-writer.d.ts.map +1 -1
  16. package/dist/src/generation/generated-registry-writer.js +470 -13
  17. package/dist/src/generation/generated-registry-writer.js.map +1 -1
  18. package/dist/src/generation/generated-source-policy.d.ts.map +1 -1
  19. package/dist/src/generation/generated-source-policy.js +50 -13
  20. package/dist/src/generation/generated-source-policy.js.map +1 -1
  21. package/dist/src/generation/generator.d.ts +4 -0
  22. package/dist/src/generation/generator.d.ts.map +1 -1
  23. package/dist/src/generation/generator.js +193 -45
  24. package/dist/src/generation/generator.js.map +1 -1
  25. package/dist/src/generation/handler-codegen.d.ts +8 -0
  26. package/dist/src/generation/handler-codegen.d.ts.map +1 -1
  27. package/dist/src/generation/handler-codegen.js +202 -13
  28. package/dist/src/generation/handler-codegen.js.map +1 -1
  29. package/dist/src/generation/handler-generator.d.ts +3 -0
  30. package/dist/src/generation/handler-generator.d.ts.map +1 -1
  31. package/dist/src/generation/handler-generator.js +5 -1
  32. package/dist/src/generation/handler-generator.js.map +1 -1
  33. package/dist/src/generation/runtime-metadata-assets.d.ts +48 -0
  34. package/dist/src/generation/runtime-metadata-assets.d.ts.map +1 -0
  35. package/dist/src/generation/runtime-metadata-assets.js +263 -0
  36. package/dist/src/generation/runtime-metadata-assets.js.map +1 -0
  37. package/dist/src/generation/runtime-metadata-graph.d.ts +82 -0
  38. package/dist/src/generation/runtime-metadata-graph.d.ts.map +1 -0
  39. package/dist/src/generation/runtime-metadata-graph.js +390 -0
  40. package/dist/src/generation/runtime-metadata-graph.js.map +1 -0
  41. package/dist/tsconfig.tsbuildinfo +1 -1
  42. package/package.json +2 -2
@@ -14,9 +14,9 @@
14
14
  import { spawnSync } from "node:child_process";
15
15
  import { closeSync, constants, lstatSync, mkdirSync, openSync, writeFileSync } from "node:fs";
16
16
  import { dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
17
- const defaultRegistryConst = "generatedHandlerRegistry";
17
+ const defaultRegistryConst = "spineMetadata";
18
18
  const defaultRegistryModule = "@spine-event-engine/server/spi/handler-registry";
19
- const registryTypeName = "GeneratedHandlerRegistry";
19
+ const registryTypeName = "SpineMetadata";
20
20
  const identRe = /^[$A-Z_a-z][$\w]*$/;
21
21
  const reservedWords = new Set([
22
22
  "arguments",
@@ -73,6 +73,91 @@ const reservedWords = new Set([
73
73
  */
74
74
  export class GeneratedRegistryWriter {
75
75
  // prettier-ignore
76
+ /**
77
+ * Renders the local application bootstrap without starting a Server.
78
+ *
79
+ * @param options Metadata export name used by the generated bootstrap.
80
+ * @returns Static metadata registration source.
81
+ */
82
+ renderBootstrap(options = {}) {
83
+ const registryName = options.registryName ?? defaultRegistryConst;
84
+ RegistrySource.assertRegistryName(registryName, new Set([registryTypeName]));
85
+ const registrationName = registryName === "registerSpineMetadata"
86
+ ? "registerSpineMetadata_2" : "registerSpineMetadata";
87
+ const registrationImport = registrationName === "registerSpineMetadata"
88
+ ? registrationName : `registerSpineMetadata as ${registrationName}`;
89
+ return [
90
+ `import { ${registrationImport} } from "@spine-event-engine/server/spi/handler-registry";`,
91
+ `import { ${registryName} } from "./spine-metadata.js";`,
92
+ "",
93
+ `${registrationName}(${registryName}, "./spine-metadata.js");`,
94
+ "",
95
+ ].join("\n");
96
+ }
97
+ /**
98
+ * Renders a passive adapter against one bootstrap location.
99
+ *
100
+ * @param dependencies Resolved public Spine exports and portable direct imports.
101
+ * @param fromDirectory Directory receiving the adapter.
102
+ * @param format Runtime module condition of the consuming package.
103
+ * @returns Static JavaScript re-exports and matching declaration bindings.
104
+ */
105
+ renderDependencies(dependencies, fromDirectory, format = "import") {
106
+ const header = "/* Generated by Spine TypeScript. Do not edit manually. */\n";
107
+ const runtime = dependencies.map(({ runtimeFile, moduleSpecifier }, index) => {
108
+ const path = moduleSpecifier ?? RegistrySource.relativeModule(fromDirectory, runtimeFile);
109
+ const binding = `dependencyMetadata${String(index)}`;
110
+ const specifier = RegistrySource.stringLiteral(path);
111
+ const member = `dependency${String(index)}.spineMetadata`;
112
+ return format === "import"
113
+ ? `import * as dependency${String(index)} from ${specifier};\nexport const ${binding} = () => ${member};`
114
+ : `const dependency${String(index)} = require(${specifier});\nexports.${binding} = () => ${member};`;
115
+ });
116
+ const declarations = dependencies.map(({ runtimeFile, moduleSpecifier }, index) => {
117
+ const binding = `dependencyMetadata${String(index)}`;
118
+ const path = moduleSpecifier ?? RegistrySource.relativeModule(fromDirectory, runtimeFile);
119
+ const dependency = `typeof import(${RegistrySource.stringLiteral(path)}).spineMetadata`;
120
+ return `export declare const ${binding}: () => ${dependency};`;
121
+ });
122
+ return {
123
+ runtime: `${header}${runtime.join("\n")}\n${runtime.length === 0 && format === "import" ? "export {};\n" : ""}`,
124
+ declarations: `${header}${declarations.join("\n")}\n${declarations.length === 0 ? "export {};\n" : ""}`,
125
+ };
126
+ }
127
+ /**
128
+ * Writes the generated bootstrap under the same output guard as metadata.
129
+ *
130
+ * @param options Output path and repository guards.
131
+ * @returns Written bootstrap source.
132
+ */
133
+ writeBootstrap(options) {
134
+ RegistrySource.assertWriteOptions(options);
135
+ const source = this.renderBootstrap(options);
136
+ const repoRoot = resolve(options.repoRoot);
137
+ const outputDir = dirname(resolve(options.outputFile));
138
+ RegistrySource.assertGitIgnored(repoRoot, options.outputFile);
139
+ mkdirSync(outputDir, { recursive: true });
140
+ RegistrySource.assertNoSymlinkPath(repoRoot, outputDir, "Generated output directory");
141
+ RegistrySource.assertNoSymlinkOutput(options.outputFile);
142
+ RegistrySource.writeOutput(options.outputFile, source);
143
+ return source;
144
+ }
145
+ /**
146
+ * Rejects unsafe generated destinations before a publication snapshot reads them.
147
+ *
148
+ * @param repoRoot Repository checkout containing generated output.
149
+ * @param paths Metadata, bootstrap, adapter, and record destinations.
150
+ */
151
+ validateDestinations(repoRoot, paths) {
152
+ const root = resolve(repoRoot);
153
+ RegistrySource.assertRepoRoot(root);
154
+ for (const path of paths) {
155
+ const file = resolve(path);
156
+ RegistrySource.assertWithin(root, file, "Generated output");
157
+ RegistrySource.assertNoSymlinkPath(root, dirname(file), "Generated output directory");
158
+ RegistrySource.assertNoSymlinkOutput(file);
159
+ }
160
+ }
76
161
  /**
77
162
  * Renders generated registry source without writing to disk.
78
163
  *
@@ -85,13 +170,19 @@ export class GeneratedRegistryWriter {
85
170
  const registryName = options.registryName ?? defaultRegistryConst;
86
171
  const registryModule = options.registryModuleSpecifier ?? defaultRegistryModule;
87
172
  const receivers = analysis.receivers;
88
- const refs = RegistrySource.buildRefs(receivers, outputFile);
173
+ const appQueries = analysis.appQueries ?? [];
174
+ const refs = RegistrySource.buildRefs(receivers, appQueries, outputFile);
89
175
  RegistrySource.assertRegistryName(registryName, RegistrySource.importNames(refs));
176
+ const protoModule = RegistrySource.bindProtoModule(options, registryName, refs);
90
177
  const lines = [
91
178
  `import type { ${registryTypeName} } from ${RegistrySource.stringLiteral(registryModule)};`,
179
+ ...(protoModule === undefined ? [] : [protoModule.importLine]),
180
+ ...((options.dependencies?.length ?? 0) === 0
181
+ ? []
182
+ : ['import * as directMetadata from "./spine-dependencies.js";']),
92
183
  ...refs.imports,
93
184
  "",
94
- ...RegistrySource.renderRegistry(receivers, outputFile, registryName, refs),
185
+ ...RegistrySource.renderRegistry(receivers, appQueries, outputFile, registryName, refs, protoModule?.localName, options.dependencies?.length ?? 0),
95
186
  "",
96
187
  ];
97
188
  return lines.join("\n");
@@ -124,23 +215,73 @@ export class GeneratedRegistryWriter {
124
215
  * Renders deterministic generated registries and guards file output.
125
216
  */
126
217
  const RegistrySource = Object.freeze({
218
+ /**
219
+ * Binds a Proto module import distinct from metadata and receiver symbols.
220
+ *
221
+ * @param options Proto export and module specifier from caller configuration.
222
+ * @param registryName Generated metadata declaration name.
223
+ * @param refs Already bound class and schema imports.
224
+ * @returns Aliased import and local binding when a Proto module is requested.
225
+ */
226
+ bindProtoModule(options, registryName, refs) {
227
+ const exported = options.protoModuleExport;
228
+ if (exported === undefined)
229
+ return undefined;
230
+ if (!identRe.test(exported) || reservedWords.has(exported))
231
+ throw new Error(`Invalid Proto module export: ${exported}`);
232
+ const used = new Set([...RegistrySource.importNames(refs), registryName]);
233
+ const localName = RegistrySource.allocName(exported, used);
234
+ const binding = localName === exported ? exported : `${exported} as ${localName}`;
235
+ const specifier = RegistrySource.stringLiteral(options.protoModuleSpecifier ?? "./proto-module.js");
236
+ return {
237
+ importLine: `import { ${binding} } from ${specifier};`,
238
+ localName,
239
+ };
240
+ },
241
+ /**
242
+ * Collects local identifiers that generated imports must not shadow.
243
+ *
244
+ * @param refs Allocated import and receiver references.
245
+ * @returns Identifiers reserved by generated imports.
246
+ */
127
247
  importNames(refs) {
128
248
  return new Set([registryTypeName, ...refs.localNames]);
129
249
  },
130
- buildRefs(receivers, outputFile) {
250
+ /**
251
+ * Binds receiver classes and schemas to deterministic TypeScript imports.
252
+ *
253
+ * @param outputFile Generated output file used for import or publication paths.
254
+ * @param receivers Analyzed receiver descriptions.
255
+ * @param appQueries Analyzed application query implementations.
256
+ * @returns Bound class and schema import references.
257
+ */
258
+ buildRefs(receivers, appQueries, outputFile) {
131
259
  const used = new Set();
132
260
  const entityRefs = new Map();
133
261
  const entityImports = [];
134
262
  const schemaRaw = new Map();
135
263
  const entityRaw = new Map();
136
- for (const receiver of receivers) {
137
- RegistrySource.addReceiverRef(receiver, outputFile, entityRaw, entityRefs, entityImports, used);
138
- RegistrySource.addReceiverSchemas(receiver, outputFile, schemaRaw);
264
+ RegistrySource.addReceiverRefs(receivers, outputFile, entityRaw, entityRefs, entityImports, schemaRaw, used);
265
+ for (const query of appQueries) {
266
+ RegistrySource.addQueryRefs(query, outputFile, entityRaw, entityRefs, entityImports, schemaRaw, used);
139
267
  }
140
268
  const { schemaImports, schemaRefs } = RegistrySource.bindSchemaRefs(schemaRaw, used);
269
+ return RegistrySource.finishRefs(entityRefs, schemaRefs, entityImports, schemaImports, used);
270
+ },
271
+ /**
272
+ * Builds the render reference table from allocated class and schema imports.
273
+ *
274
+ * @param entities Bound implementation class names.
275
+ * @param schemas Bound generated schema names.
276
+ * @param entityImports Ordered implementation imports.
277
+ * @param schemaImports Ordered generated schema imports.
278
+ * @param used Reserved local names.
279
+ * @returns References for metadata rendering.
280
+ */
281
+ finishRefs(entities, schemas, entityImports, schemaImports, used) {
141
282
  return {
142
- entityNames: entityRefs,
143
- schemaNames: schemaRefs,
283
+ entityNames: entities,
284
+ schemaNames: schemas,
144
285
  imports: [
145
286
  ...entityImports.map(RegistrySource.renderImport),
146
287
  ...RegistrySource.renderSchemaImports(schemaImports),
@@ -148,6 +289,49 @@ const RegistrySource = Object.freeze({
148
289
  localNames: used,
149
290
  };
150
291
  },
292
+ /**
293
+ * Adds all receiver classes and schemas to the shared reference allocation.
294
+ *
295
+ * @param receivers Descriptions to import.
296
+ * @param outputFile Generated metadata path.
297
+ * @param raw Unbound class imports.
298
+ * @param names Bound class names.
299
+ * @param imports Ordered class imports.
300
+ * @param schemas Unbound schema imports.
301
+ * @param used Reserved local names.
302
+ */
303
+ addReceiverRefs(receivers, outputFile, raw, names, imports, schemas, used) {
304
+ for (const receiver of receivers) {
305
+ RegistrySource.addReceiverRef(receiver, outputFile, raw, names, imports, used);
306
+ RegistrySource.addReceiverSchemas(receiver, outputFile, schemas);
307
+ }
308
+ },
309
+ /**
310
+ * Adds one query class and its two generated schemas to shared import allocation.
311
+ *
312
+ * @param query Analyzed implementation and paired schemas.
313
+ * @param outputFile Generated metadata location.
314
+ * @param raw Class import references.
315
+ * @param names Bound class names.
316
+ * @param imports Ordered class imports.
317
+ * @param schemas Generated schema imports.
318
+ * @param used Already reserved local identifiers.
319
+ */
320
+ addQueryRefs(query, outputFile, raw, names, imports, schemas, used) {
321
+ RegistrySource.addReceiverRef(query, outputFile, raw, names, imports, used);
322
+ RegistrySource.addSchemaRef(schemas, outputFile, query.sourceFile, query.querySchema);
323
+ RegistrySource.addSchemaRef(schemas, outputFile, query.sourceFile, query.responseSchema);
324
+ },
325
+ /**
326
+ * Records the class import and local identifier for an analyzed receiver.
327
+ *
328
+ * @param imports Ordered bound import references.
329
+ * @param names Receiver keys mapped to local identifiers.
330
+ * @param outputFile Generated output file used for import or publication paths.
331
+ * @param raw Collected unbound import references.
332
+ * @param receiver Analyzed handler receiver.
333
+ * @param used Already allocated local identifiers.
334
+ */
151
335
  addReceiverRef(receiver, outputFile, raw, names, imports, used) {
152
336
  const moduleSpecifier = RegistrySource.entityModule(outputFile, receiver.sourceFile);
153
337
  const key = RegistrySource.entityKey(moduleSpecifier, receiver.className);
@@ -165,6 +349,13 @@ const RegistrySource = Object.freeze({
165
349
  }
166
350
  names.set(RegistrySource.entityKey(ref.moduleSpecifier, ref.importedName), ref.localName);
167
351
  },
352
+ /**
353
+ * Collects Proto schemas referenced by a receiver and its handlers.
354
+ *
355
+ * @param outputFile Generated output file used for import or publication paths.
356
+ * @param receiver Analyzed handler receiver.
357
+ * @param schemas Mutable schema import collection.
358
+ */
168
359
  addReceiverSchemas(receiver, outputFile, schemas) {
169
360
  const add = (schema) => {
170
361
  RegistrySource.addSchemaRef(schemas, outputFile, receiver.sourceFile, schema);
@@ -177,6 +368,13 @@ const RegistrySource = Object.freeze({
177
368
  handler.outcomes.thrown.forEach(add);
178
369
  }
179
370
  },
371
+ /**
372
+ * Binds stable local names for collected schema imports.
373
+ *
374
+ * @param raw Collected unbound import references.
375
+ * @param used Already allocated local identifiers.
376
+ * @returns Schema imports and their allocated local names.
377
+ */
180
378
  bindSchemaRefs(raw, used) {
181
379
  const schemaRefs = new Map();
182
380
  const schemaImports = [...raw.values()].sort(RegistrySource.compareRef).map((ref) => {
@@ -186,14 +384,75 @@ const RegistrySource = Object.freeze({
186
384
  });
187
385
  return { schemaImports, schemaRefs };
188
386
  },
189
- renderRegistry(receivers, outputFile, registryName, refs) {
190
- const lines = [`export const ${registryName}: GeneratedHandlerRegistry = {`, " receivers: ["];
387
+ /**
388
+ * Renders the complete passive metadata source from bound receiver references.
389
+ *
390
+ * @param outputFile Generated output file used for import or publication paths.
391
+ * @param protoModuleExport Value used by this operation.
392
+ * @param receivers Analyzed receiver descriptions.
393
+ * @param appQueries Analyzed application query implementations.
394
+ * @param refs Allocated import and receiver references.
395
+ * @param registryName Generated metadata export name.
396
+ * @param dependencyCount Number of direct dependency accessors.
397
+ * @returns Complete passive metadata source.
398
+ */
399
+ renderRegistry(receivers, appQueries, outputFile, registryName, refs, protoModuleExport, dependencyCount = 0) {
400
+ const lines = [
401
+ `export const ${registryName}: ${registryTypeName} = {`,
402
+ ...(protoModuleExport === undefined ? [] : [` protoModule: ${protoModuleExport},`]),
403
+ ...RegistrySource.dependencyLines(dependencyCount),
404
+ " receivers: [",
405
+ ];
191
406
  receivers.forEach((receiver) => {
192
407
  lines.push(...RegistrySource.renderReceiver(receiver, outputFile, refs));
193
408
  });
194
- lines.push(" ],", "};");
409
+ lines.push(" ],");
410
+ if (appQueries.length > 0)
411
+ lines.push(" appQueries: [", ...appQueries.flatMap((query) => RegistrySource.renderAppQuery(query, outputFile, refs)), " ],");
412
+ lines.push("};");
195
413
  return lines;
196
414
  },
415
+ /**
416
+ * Renders the passive direct-dependency accessor, when needed.
417
+ *
418
+ * @param count Number of direct dependencies.
419
+ * @returns The generated dependency lines.
420
+ */
421
+ dependencyLines(count) {
422
+ if (count === 0)
423
+ return [];
424
+ const entries = Array.from({ length: count }, (_value, index) => `directMetadata.dependencyMetadata${String(index)}()`);
425
+ return [` dependencies: () => [${entries.join(", ")}],`];
426
+ },
427
+ /**
428
+ * Renders one passive query implementation reference.
429
+ *
430
+ * @param query Analyzed implementation and paired schemas.
431
+ * @param outputFile Generated metadata location.
432
+ * @param refs Bound import identifiers.
433
+ * @returns Constructor and schema data without executing the handler.
434
+ */
435
+ renderAppQuery(query, outputFile, refs) {
436
+ const handler = RegistrySource.entityName(refs, outputFile, query);
437
+ const request = RegistrySource.schemaName(refs, outputFile, query.sourceFile, query.querySchema);
438
+ const response = RegistrySource.schemaName(refs, outputFile, query.sourceFile, query.responseSchema);
439
+ return [
440
+ " {",
441
+ ` handlerType: ${handler},`,
442
+ ` queryTypeName: ${RegistrySource.stringLiteral(query.queryTypeName)},`,
443
+ ` querySchema: ${request},`,
444
+ ` responseSchema: ${response},`,
445
+ " },",
446
+ ];
447
+ },
448
+ /**
449
+ * Renders one generated entity or standalone receiver description.
450
+ *
451
+ * @param outputFile Generated output file used for import or publication paths.
452
+ * @param receiver Analyzed handler receiver.
453
+ * @param refs Allocated import and receiver references.
454
+ * @returns Receiver description source.
455
+ */
197
456
  renderReceiver(receiver, outputFile, refs) {
198
457
  const entityType = RegistrySource.entityName(refs, outputFile, receiver);
199
458
  const receiverKind = receiver.receiverKind;
@@ -213,6 +472,15 @@ const RegistrySource = Object.freeze({
213
472
  lines.push(" ],", " },");
214
473
  return lines;
215
474
  },
475
+ /**
476
+ * Renders one declared handler with its input and outcome schemas.
477
+ *
478
+ * @param handler Analyzed handler declaration.
479
+ * @param outputFile Generated output file used for import or publication paths.
480
+ * @param refs Allocated import and receiver references.
481
+ * @param sourceFile Source file containing the referenced symbol.
482
+ * @returns Handler description source.
483
+ */
216
484
  renderHandler(handler, sourceFile, outputFile, refs) {
217
485
  const schemaName = (schema) => RegistrySource.schemaName(refs, outputFile, sourceFile, schema);
218
486
  const returned = handler.outcomes.returned.map(schemaName).join(", ");
@@ -234,6 +502,12 @@ const RegistrySource = Object.freeze({
234
502
  " },",
235
503
  ];
236
504
  },
505
+ /**
506
+ * Renders a declared event filter as a source expression.
507
+ *
508
+ * @param where Declared event filter.
509
+ * @returns Event filter source expression.
510
+ */
237
511
  renderWhere(where) {
238
512
  return where === undefined
239
513
  ? []
@@ -244,6 +518,14 @@ const RegistrySource = Object.freeze({
244
518
  " },",
245
519
  ];
246
520
  },
521
+ /**
522
+ * Records an import reference for one Proto schema.
523
+ *
524
+ * @param outputFile Generated output file used for import or publication paths.
525
+ * @param schema Referenced Proto schema.
526
+ * @param schemaImports Collected Proto schema imports.
527
+ * @param sourceFile Source file containing the referenced symbol.
528
+ */
247
529
  addSchemaRef(schemaImports, outputFile, sourceFile, schema) {
248
530
  const moduleSpecifier = RegistrySource.importedSchemaModule(outputFile, sourceFile, schema.moduleSpecifier);
249
531
  const key = RegistrySource.entityKey(moduleSpecifier, schema.exportName);
@@ -256,9 +538,24 @@ const RegistrySource = Object.freeze({
256
538
  moduleSpecifier,
257
539
  });
258
540
  },
541
+ /**
542
+ * Resolves an Entity source module from the generated output.
543
+ *
544
+ * @param outputFile Generated output file used for import or publication paths.
545
+ * @param sourceFile Source file containing the referenced symbol.
546
+ * @returns Entity module import specifier.
547
+ */
259
548
  entityModule(outputFile, sourceFile) {
260
549
  return RegistrySource.relativeModule(dirname(outputFile), RegistrySource.replaceSourceExtension(sourceFile));
261
550
  },
551
+ /**
552
+ * Resolves a schema module import against the generated output.
553
+ *
554
+ * @param moduleSpecifier Imported module specifier.
555
+ * @param outputFile Generated output file used for import or publication paths.
556
+ * @param sourceFile Source file containing the referenced symbol.
557
+ * @returns Schema module import specifier.
558
+ */
262
559
  importedSchemaModule(outputFile, sourceFile, moduleSpecifier) {
263
560
  if (!moduleSpecifier.startsWith(".")) {
264
561
  return moduleSpecifier;
@@ -266,10 +563,23 @@ const RegistrySource = Object.freeze({
266
563
  const resolved = resolve(dirname(sourceFile), moduleSpecifier);
267
564
  return RegistrySource.relativeModule(dirname(outputFile), resolved);
268
565
  },
566
+ /**
567
+ * Builds a relative JavaScript module specifier between source paths.
568
+ *
569
+ * @param fromDirectory Importing source directory.
570
+ * @param targetPath Generated or compiler path to validate.
571
+ * @returns Relative JavaScript module specifier.
572
+ */
269
573
  relativeModule(fromDirectory, targetPath) {
270
574
  const value = relative(fromDirectory, targetPath).split(sep).join("/");
271
575
  return value.startsWith(".") ? value : `./${value}`;
272
576
  },
577
+ /**
578
+ * Converts a TypeScript source path to its runtime JavaScript path.
579
+ *
580
+ * @param path Path or Proto import path to inspect.
581
+ * @returns Runtime JavaScript path.
582
+ */
273
583
  replaceSourceExtension(path) {
274
584
  const extension = extname(path);
275
585
  switch (extension) {
@@ -284,16 +594,37 @@ const RegistrySource = Object.freeze({
284
594
  return path;
285
595
  }
286
596
  },
597
+ /**
598
+ * Compares import references by stable module and symbol identity.
599
+ *
600
+ * @param left First value in stable ordering.
601
+ * @param right Second value in stable ordering.
602
+ * @returns Negative, zero, or positive stable comparison.
603
+ */
287
604
  compareRef(left, right) {
288
605
  return (RegistrySource.compareString(left.moduleSpecifier, right.moduleSpecifier) ||
289
606
  RegistrySource.compareString(left.importedName, right.importedName));
290
607
  },
608
+ /**
609
+ * Binds an unused local identifier to an import reference.
610
+ *
611
+ * @param ref Bound import reference.
612
+ * @param used Already allocated local identifiers.
613
+ * @returns Import reference with an allocated local identifier.
614
+ */
291
615
  bindRef(ref, used) {
292
616
  return {
293
617
  ...ref,
294
618
  localName: RegistrySource.allocName(ref.importedName, used),
295
619
  };
296
620
  },
621
+ /**
622
+ * Returns a collision-free generated import identifier.
623
+ *
624
+ * @param base Preferred generated identifier.
625
+ * @param used Already allocated local identifiers.
626
+ * @returns Collision-free local identifier.
627
+ */
297
628
  allocName(base, used) {
298
629
  let next = base;
299
630
  let index = 2;
@@ -304,6 +635,12 @@ const RegistrySource = Object.freeze({
304
635
  used.add(next);
305
636
  return next;
306
637
  },
638
+ /**
639
+ * Renders the import statement for a bound symbol.
640
+ *
641
+ * @param ref Bound import reference.
642
+ * @returns TypeScript import statement.
643
+ */
307
644
  renderImport(ref) {
308
645
  if (ref.defaultExport === true) {
309
646
  return `import ${ref.localName} from ${RegistrySource.stringLiteral(ref.moduleSpecifier)};`;
@@ -313,6 +650,12 @@ const RegistrySource = Object.freeze({
313
650
  : `${ref.importedName} as ${ref.localName}`;
314
651
  return `import { ${local} } from ${RegistrySource.stringLiteral(ref.moduleSpecifier)};`;
315
652
  },
653
+ /**
654
+ * Renders deterministic import statements for collected schemas.
655
+ *
656
+ * @param refs Allocated import and receiver references.
657
+ * @returns Ordered schema import statements.
658
+ */
316
659
  renderSchemaImports(refs) {
317
660
  const grouped = new Map();
318
661
  refs.forEach((ref) => {
@@ -328,12 +671,36 @@ const RegistrySource = Object.freeze({
328
671
  });
329
672
  return [...grouped.entries()].map(([moduleSpecifier, names]) => `import { ${names.join(", ")} } from ${RegistrySource.stringLiteral(moduleSpecifier)};`);
330
673
  },
674
+ /**
675
+ * Finds the allocated local identifier for an Entity class.
676
+ *
677
+ * @param entity Entity class description.
678
+ * @param outputFile Generated output file used for import or publication paths.
679
+ * @param refs Allocated import and receiver references.
680
+ * @returns Allocated Entity class identifier.
681
+ */
331
682
  entityName(refs, outputFile, entity) {
332
683
  return RegistrySource.refName(refs.entityNames, RegistrySource.entityKey(RegistrySource.entityModule(outputFile, entity.sourceFile), entity.className));
333
684
  },
685
+ /**
686
+ * Finds the allocated local identifier for a Proto schema.
687
+ *
688
+ * @param outputFile Generated output file used for import or publication paths.
689
+ * @param refs Allocated import and receiver references.
690
+ * @param schema Referenced Proto schema.
691
+ * @param sourceFile Source file containing the referenced symbol.
692
+ * @returns Allocated Proto schema identifier.
693
+ */
334
694
  schemaName(refs, outputFile, sourceFile, schema) {
335
695
  return RegistrySource.refName(refs.schemaNames, RegistrySource.entityKey(RegistrySource.importedSchemaModule(outputFile, sourceFile, schema.moduleSpecifier), schema.exportName));
336
696
  },
697
+ /**
698
+ * Finds a bound local name by its stable reference key.
699
+ *
700
+ * @param key Stable reference key.
701
+ * @param refs Allocated import and receiver references.
702
+ * @returns Bound local identifier.
703
+ */
337
704
  refName(refs, key) {
338
705
  const value = refs.get(key);
339
706
  if (value !== undefined) {
@@ -341,20 +708,45 @@ const RegistrySource = Object.freeze({
341
708
  }
342
709
  throw new Error(`Missing generated registry import binding for ${key}.`);
343
710
  },
711
+ /**
712
+ * Builds the stable module-and-symbol key for a receiver class.
713
+ *
714
+ * @param importedName Value used by this operation.
715
+ * @param moduleSpecifier Imported module specifier.
716
+ * @returns Stable receiver class key.
717
+ */
344
718
  entityKey(moduleSpecifier, importedName) {
345
719
  return `${moduleSpecifier}::${importedName}`;
346
720
  },
721
+ /**
722
+ * Encodes a value as a safe TypeScript string literal.
723
+ *
724
+ * @param value Value being read or validated.
725
+ * @returns Escaped TypeScript string literal.
726
+ */
347
727
  stringLiteral(value) {
348
728
  return JSON.stringify(value)
349
729
  .replace(/\u2028/g, "\\u2028")
350
730
  .replace(/\u2029/g, "\\u2029");
351
731
  },
732
+ /**
733
+ * Rejects a symbol name that cannot be emitted as an identifier.
734
+ *
735
+ * @param label Diagnostic label for the checked path.
736
+ * @param value Value being read or validated.
737
+ */
352
738
  assertIdentifier(value, label) {
353
739
  if (identRe.test(value)) {
354
740
  return;
355
741
  }
356
742
  throw new Error(`${label} must be a valid TypeScript identifier: ${value}`);
357
743
  },
744
+ /**
745
+ * Rejects an invalid or colliding metadata export name.
746
+ *
747
+ * @param localNames Already allocated local identifiers.
748
+ * @param value Value being read or validated.
749
+ */
358
750
  assertRegistryName(value, localNames) {
359
751
  RegistrySource.assertIdentifier(value, "Registry name");
360
752
  if (reservedWords.has(value)) {
@@ -364,6 +756,11 @@ const RegistrySource = Object.freeze({
364
756
  throw new Error(`Registry name collides with generated import binding: ${value}`);
365
757
  }
366
758
  },
759
+ /**
760
+ * Rejects analyzer findings before source publication.
761
+ *
762
+ * @param analysis Analyzed receiver descriptions and diagnostics.
763
+ */
367
764
  assertNoDiagnostics(analysis) {
368
765
  if (analysis.diagnostics.length === 0) {
369
766
  return;
@@ -374,6 +771,11 @@ const RegistrySource = Object.freeze({
374
771
  : `${first.sourceFile}:${String(first.line)}:${String(first.column)}`;
375
772
  throw new Error(`Cannot write generated handler registry with analyzer diagnostics. First diagnostic: ${location}.`);
376
773
  },
774
+ /**
775
+ * Validates generated output paths and publication settings.
776
+ *
777
+ * @param options Caller configuration for the operation.
778
+ */
377
779
  assertWriteOptions(options) {
378
780
  const repoRoot = resolve(options.repoRoot);
379
781
  const generatedRoot = resolve(options.generatedRoot);
@@ -391,6 +793,11 @@ const RegistrySource = Object.freeze({
391
793
  RegistrySource.assertNoSymlinkPath(repoRoot, generatedRoot, "Generated root");
392
794
  RegistrySource.assertNoSymlinkPath(repoRoot, dirname(outputFile), "Generated output directory");
393
795
  },
796
+ /**
797
+ * Checks that the repository root is a real directory.
798
+ *
799
+ * @param repoRoot Repository root used for output guards.
800
+ */
394
801
  assertRepoRoot(repoRoot) {
395
802
  RegistrySource.assertNoSymlinkAncestors(repoRoot, "Repository root path");
396
803
  const stat = RegistrySource.lstatIfPresent(repoRoot);
@@ -404,6 +811,12 @@ const RegistrySource = Object.freeze({
404
811
  throw new Error(`Repository root must be a directory: ${repoRoot}`);
405
812
  }
406
813
  },
814
+ /**
815
+ * Rejects symlinks along a generated output path.
816
+ *
817
+ * @param label Diagnostic label for the checked path.
818
+ * @param targetPath Generated or compiler path to validate.
819
+ */
407
820
  assertNoSymlinkAncestors(targetPath, label) {
408
821
  const parts = resolve(targetPath).split(sep).filter(Boolean);
409
822
  let current = sep;
@@ -418,12 +831,23 @@ const RegistrySource = Object.freeze({
418
831
  }
419
832
  }
420
833
  },
834
+ /**
835
+ * Rejects a symlink at the generated output file.
836
+ *
837
+ * @param outputFile Generated output file used for import or publication paths.
838
+ */
421
839
  assertNoSymlinkOutput(outputFile) {
422
840
  const stat = RegistrySource.lstatIfPresent(resolve(outputFile));
423
841
  if (stat?.isSymbolicLink()) {
424
842
  throw new Error(`Generated output file must not be a symlink: ${outputFile}`);
425
843
  }
426
844
  },
845
+ /**
846
+ * Writes generated source through a temporary file and atomic rename.
847
+ *
848
+ * @param outputFile Generated output file used for import or publication paths.
849
+ * @param source Generated or source TypeScript text.
850
+ */
427
851
  writeOutput(outputFile, source) {
428
852
  const flags = constants.O_WRONLY | constants.O_CREAT | constants.O_TRUNC | constants.O_NOFOLLOW;
429
853
  const fd = openSync(outputFile, flags, 0o666);
@@ -434,12 +858,26 @@ const RegistrySource = Object.freeze({
434
858
  closeSync(fd);
435
859
  }
436
860
  },
861
+ /**
862
+ * Checks that a target path remains inside its allowed root.
863
+ *
864
+ * @param label Diagnostic label for the checked path.
865
+ * @param root Confinement or generated-tree root.
866
+ * @param target Target path to validate.
867
+ */
437
868
  assertWithin(root, target, label) {
438
869
  const value = relative(root, target);
439
870
  if (value === "" || value.startsWith("..") || isAbsolute(value)) {
440
871
  throw new Error(`${label} must stay within ${root}.`);
441
872
  }
442
873
  },
874
+ /**
875
+ * Checks every existing component of a generated path for symlinks.
876
+ *
877
+ * @param label Diagnostic label for the checked path.
878
+ * @param repoRoot Repository root used for output guards.
879
+ * @param targetPath Generated or compiler path to validate.
880
+ */
443
881
  assertNoSymlinkPath(repoRoot, targetPath, label) {
444
882
  const relativePath = relative(repoRoot, targetPath);
445
883
  if (relativePath === "" || relativePath.startsWith("..") || isAbsolute(relativePath)) {
@@ -461,6 +899,12 @@ const RegistrySource = Object.freeze({
461
899
  }
462
900
  }
463
901
  },
902
+ /**
903
+ * Rejects generated output that is not ignored by Git.
904
+ *
905
+ * @param outputFile Generated output file used for import or publication paths.
906
+ * @param repoRoot Repository root used for output guards.
907
+ */
464
908
  assertGitIgnored(repoRoot, outputFile) {
465
909
  const relativePath = relative(resolve(repoRoot), resolve(outputFile));
466
910
  const result = spawnSync("git", ["check-ignore", "--quiet", "--", relativePath], {
@@ -475,6 +919,12 @@ const RegistrySource = Object.freeze({
475
919
  }
476
920
  throw new Error(`Failed to verify Git ignore status for generated output: ${relativePath}`);
477
921
  },
922
+ /**
923
+ * Reads filesystem metadata when a path exists.
924
+ *
925
+ * @param path Path or Proto import path to inspect.
926
+ * @returns Filesystem metadata, or undefined for an absent path.
927
+ */
478
928
  lstatIfPresent(path) {
479
929
  try {
480
930
  return lstatSync(path);
@@ -486,6 +936,13 @@ const RegistrySource = Object.freeze({
486
936
  throw error;
487
937
  }
488
938
  },
939
+ /**
940
+ * Compares strings deterministically for generated source.
941
+ *
942
+ * @param left First value in stable ordering.
943
+ * @param right Second value in stable ordering.
944
+ * @returns Negative, zero, or positive lexical comparison.
945
+ */
489
946
  compareString(left, right) {
490
947
  if (left === right) {
491
948
  return 0;