@spine-event-engine/proto-tools 2.0.0-snapshot.25 → 2.0.0-snapshot.28
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/README.md +10 -3
- package/REFERENCE.md +58 -3
- package/dist/src/generation/app-query-generator.d.ts +33 -0
- package/dist/src/generation/app-query-generator.d.ts.map +1 -0
- package/dist/src/generation/app-query-generator.js +73 -0
- package/dist/src/generation/app-query-generator.js.map +1 -0
- package/dist/src/generation/build-time-handler-analyzer.d.ts +53 -1
- package/dist/src/generation/build-time-handler-analyzer.d.ts.map +1 -1
- package/dist/src/generation/build-time-handler-analyzer.js +414 -9
- package/dist/src/generation/build-time-handler-analyzer.js.map +1 -1
- package/dist/src/generation/entity-query-generator.d.ts.map +1 -1
- package/dist/src/generation/entity-query-generator.js +4 -3
- package/dist/src/generation/entity-query-generator.js.map +1 -1
- package/dist/src/generation/generated-registry-writer.d.ts +61 -0
- package/dist/src/generation/generated-registry-writer.d.ts.map +1 -1
- package/dist/src/generation/generated-registry-writer.js +470 -13
- package/dist/src/generation/generated-registry-writer.js.map +1 -1
- package/dist/src/generation/generated-source-policy.d.ts.map +1 -1
- package/dist/src/generation/generated-source-policy.js +50 -13
- package/dist/src/generation/generated-source-policy.js.map +1 -1
- package/dist/src/generation/generator.d.ts +4 -0
- package/dist/src/generation/generator.d.ts.map +1 -1
- package/dist/src/generation/generator.js +193 -45
- package/dist/src/generation/generator.js.map +1 -1
- package/dist/src/generation/handler-codegen.d.ts +8 -0
- package/dist/src/generation/handler-codegen.d.ts.map +1 -1
- package/dist/src/generation/handler-codegen.js +202 -13
- package/dist/src/generation/handler-codegen.js.map +1 -1
- package/dist/src/generation/handler-generator.d.ts +3 -0
- package/dist/src/generation/handler-generator.d.ts.map +1 -1
- package/dist/src/generation/handler-generator.js +5 -1
- package/dist/src/generation/handler-generator.js.map +1 -1
- package/dist/src/generation/runtime-metadata-assets.d.ts +48 -0
- package/dist/src/generation/runtime-metadata-assets.d.ts.map +1 -0
- package/dist/src/generation/runtime-metadata-assets.js +263 -0
- package/dist/src/generation/runtime-metadata-assets.js.map +1 -0
- package/dist/src/generation/runtime-metadata-graph.d.ts +82 -0
- package/dist/src/generation/runtime-metadata-graph.d.ts.map +1 -0
- package/dist/src/generation/runtime-metadata-graph.js +390 -0
- package/dist/src/generation/runtime-metadata-graph.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* the License.
|
|
13
13
|
*/
|
|
14
14
|
import { Buffer } from "node:buffer";
|
|
15
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
15
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
16
16
|
import { createRequire } from "node:module";
|
|
17
17
|
import { dirname, join, relative, resolve } from "node:path";
|
|
18
18
|
import ts from "typescript";
|
|
@@ -31,6 +31,7 @@ export const BuildHandlerAnalyzer = Object.freeze({
|
|
|
31
31
|
analyze(program, sourceFiles = HandlerSources.appSourceFiles(program)) {
|
|
32
32
|
PackageDependencies.load(program);
|
|
33
33
|
const receivers = [];
|
|
34
|
+
const appQueries = [];
|
|
34
35
|
const diagnostics = [];
|
|
35
36
|
for (const source of sourceFiles) {
|
|
36
37
|
const syntaxDiagnostics = program.getSyntacticDiagnostics(source);
|
|
@@ -46,8 +47,9 @@ export const BuildHandlerAnalyzer = Object.freeze({
|
|
|
46
47
|
};
|
|
47
48
|
const sourceReceivers = HandlerSources.analyzeSource(scope);
|
|
48
49
|
receivers.push(...sourceReceivers);
|
|
50
|
+
appQueries.push(...AppQuerySources.analyzeSource(scope));
|
|
49
51
|
}
|
|
50
|
-
return { receivers, diagnostics };
|
|
52
|
+
return { receivers, appQueries, diagnostics };
|
|
51
53
|
},
|
|
52
54
|
});
|
|
53
55
|
const handlerDecorators = new Set(["Assign", "Command", "React", "Subscribe"]);
|
|
@@ -2446,9 +2448,26 @@ const HandlerSources = Object.freeze({
|
|
|
2446
2448
|
found: true,
|
|
2447
2449
|
kind: message?.descriptor.isEntityState
|
|
2448
2450
|
? "state"
|
|
2449
|
-
:
|
|
2451
|
+
: file.sourceFile.endsWith("_queries.proto")
|
|
2452
|
+
? HandlerSources.appQueryKind(indexes, message)
|
|
2453
|
+
: HandlerSources.signalKindFromProto(file.sourceFile, indexes),
|
|
2450
2454
|
};
|
|
2451
2455
|
},
|
|
2456
|
+
/**
|
|
2457
|
+
* Determines only nested Query/Response messages in an application query operation. This
|
|
2458
|
+
* distinguishes application pairs from unrelated generated nested messages.
|
|
2459
|
+
*
|
|
2460
|
+
* @param indexes Descriptor path beneath the source file.
|
|
2461
|
+
* @param message Resolved nested descriptor and generated export name.
|
|
2462
|
+
* @returns The query member role, or undefined for unrelated messages.
|
|
2463
|
+
*/
|
|
2464
|
+
appQueryKind(indexes, message) {
|
|
2465
|
+
if (indexes?.length !== 2)
|
|
2466
|
+
return undefined;
|
|
2467
|
+
if (message?.descriptor.name === "Query")
|
|
2468
|
+
return "app-query";
|
|
2469
|
+
return message?.descriptor.name === "Response" ? "app-response" : undefined;
|
|
2470
|
+
},
|
|
2452
2471
|
/**
|
|
2453
2472
|
* Finds descriptor message indexes in a generated declaration call.
|
|
2454
2473
|
*
|
|
@@ -2491,7 +2510,13 @@ const HandlerSources = Object.freeze({
|
|
|
2491
2510
|
path.push(message.name);
|
|
2492
2511
|
messages = message.nested;
|
|
2493
2512
|
}
|
|
2494
|
-
return message === undefined
|
|
2513
|
+
return message === undefined
|
|
2514
|
+
? undefined
|
|
2515
|
+
: {
|
|
2516
|
+
descriptor: message,
|
|
2517
|
+
exportName: path.join("_"),
|
|
2518
|
+
typePath: path.join("."),
|
|
2519
|
+
};
|
|
2495
2520
|
},
|
|
2496
2521
|
/**
|
|
2497
2522
|
* Reads a file descriptor from a generated initializer.
|
|
@@ -2512,6 +2537,7 @@ const HandlerSources = Object.freeze({
|
|
|
2512
2537
|
? undefined
|
|
2513
2538
|
: {
|
|
2514
2539
|
sourceFile: file.name,
|
|
2540
|
+
packageName: file.package,
|
|
2515
2541
|
messages: file.messageType.map(HandlerSources.descriptorMessage),
|
|
2516
2542
|
};
|
|
2517
2543
|
}
|
|
@@ -2585,22 +2611,401 @@ const HandlerSources = Object.freeze({
|
|
|
2585
2611
|
};
|
|
2586
2612
|
},
|
|
2587
2613
|
});
|
|
2614
|
+
/**
|
|
2615
|
+
* Resolves concrete App Query subclasses through the same generated descriptor inventory.
|
|
2616
|
+
*/
|
|
2617
|
+
const AppQuerySources = Object.freeze({
|
|
2618
|
+
/**
|
|
2619
|
+
* Finds exported concrete App Query implementations in one source file.
|
|
2620
|
+
*
|
|
2621
|
+
* @param scope The source, imports, and diagnostic collection.
|
|
2622
|
+
* @returns Concrete query implementations found in the source.
|
|
2623
|
+
*/
|
|
2624
|
+
analyzeSource(scope) {
|
|
2625
|
+
const records = [];
|
|
2626
|
+
for (const statement of scope.source.statements) {
|
|
2627
|
+
if (!ts.isClassDeclaration(statement) ||
|
|
2628
|
+
HandlerTypes.hasModifier(statement, ts.SyntaxKind.AbstractKeyword))
|
|
2629
|
+
continue;
|
|
2630
|
+
const base = AppQuerySources.baseFor(statement, scope, new Set());
|
|
2631
|
+
if (base === undefined)
|
|
2632
|
+
continue;
|
|
2633
|
+
const record = AppQuerySources.record(statement, base, scope);
|
|
2634
|
+
if (record !== undefined)
|
|
2635
|
+
records.push(record);
|
|
2636
|
+
}
|
|
2637
|
+
return records;
|
|
2638
|
+
},
|
|
2639
|
+
/**
|
|
2640
|
+
* Finds the declared AppQueryHandler base through imported concrete or abstract classes.
|
|
2641
|
+
*
|
|
2642
|
+
* @param node Class whose ancestry is inspected.
|
|
2643
|
+
* @param scope Source and imports for this class.
|
|
2644
|
+
* @param seen Class declarations already visited on this ancestry path.
|
|
2645
|
+
* @param bindings Concrete type substitutions collected from the subclass.
|
|
2646
|
+
* @returns The framework base clause, or undefined for unrelated classes.
|
|
2647
|
+
*/
|
|
2648
|
+
baseFor(node, scope, seen, bindings = new Map()) {
|
|
2649
|
+
if (seen.has(node))
|
|
2650
|
+
return undefined;
|
|
2651
|
+
seen.add(node);
|
|
2652
|
+
for (const clause of node.heritageClauses ?? [])
|
|
2653
|
+
for (const type of clause.types) {
|
|
2654
|
+
if (AppQuerySources.isBase(type.expression, scope))
|
|
2655
|
+
return { clause: type, bindings };
|
|
2656
|
+
const parent = HandlerSources.classDeclarationFor(type.expression, scope);
|
|
2657
|
+
if (parent === undefined)
|
|
2658
|
+
continue;
|
|
2659
|
+
const source = parent.getSourceFile();
|
|
2660
|
+
const inherited = AppQuerySources.baseFor(parent, {
|
|
2661
|
+
...scope,
|
|
2662
|
+
source,
|
|
2663
|
+
imports: HandlerSources.buildImportState(source, scope.program),
|
|
2664
|
+
}, seen, AppQuerySources.bindings(parent, type, scope, bindings));
|
|
2665
|
+
if (inherited !== undefined)
|
|
2666
|
+
return inherited;
|
|
2667
|
+
}
|
|
2668
|
+
return undefined;
|
|
2669
|
+
},
|
|
2670
|
+
/**
|
|
2671
|
+
* Maps an imported parent class's generic parameters to concrete child arguments.
|
|
2672
|
+
*
|
|
2673
|
+
* @param parent Resolved parent class declaration.
|
|
2674
|
+
* @param use Child's instantiated heritage clause.
|
|
2675
|
+
* @param scope Child source and TypeScript program.
|
|
2676
|
+
* @param inherited Existing substitutions from a lower subclass.
|
|
2677
|
+
* @returns Substitutions available when walking the parent.
|
|
2678
|
+
*/
|
|
2679
|
+
bindings(parent, use, scope, inherited) {
|
|
2680
|
+
const bound = new Map(inherited);
|
|
2681
|
+
const checker = scope.program.getTypeChecker();
|
|
2682
|
+
for (const [index, parameter] of (parent.typeParameters ?? []).entries()) {
|
|
2683
|
+
const symbol = checker.getSymbolAtLocation(parameter.name);
|
|
2684
|
+
const argument = use.typeArguments?.[index];
|
|
2685
|
+
if (symbol === undefined || argument === undefined)
|
|
2686
|
+
continue;
|
|
2687
|
+
const inheritedSymbol = ts.isTypeReferenceNode(argument)
|
|
2688
|
+
? checker.getSymbolAtLocation(argument.typeName)
|
|
2689
|
+
: undefined;
|
|
2690
|
+
bound.set(symbol, inheritedSymbol === undefined ? argument : (inherited.get(inheritedSymbol) ?? argument));
|
|
2691
|
+
}
|
|
2692
|
+
return bound;
|
|
2693
|
+
},
|
|
2694
|
+
/**
|
|
2695
|
+
* Checks for only the framework AppQueryHandler import or namespace member. This
|
|
2696
|
+
* excludes lookalike classes that did not import the server contract.
|
|
2697
|
+
*
|
|
2698
|
+
* @param expression Base-class expression in an extends clause.
|
|
2699
|
+
* @param scope Source, import index and TypeScript program.
|
|
2700
|
+
* @returns Whether the expression names the framework base.
|
|
2701
|
+
*/
|
|
2702
|
+
isBase(expression, scope) {
|
|
2703
|
+
const imports = scope.imports;
|
|
2704
|
+
if (ts.isIdentifier(expression))
|
|
2705
|
+
if (imports.serverSymbols.get(expression.text) === "AppQueryHandler")
|
|
2706
|
+
return true;
|
|
2707
|
+
if (ts.isPropertyAccessExpression(expression) &&
|
|
2708
|
+
ts.isIdentifier(expression.expression) &&
|
|
2709
|
+
imports.serverNamespaces.has(expression.expression.text) &&
|
|
2710
|
+
expression.name.text === "AppQueryHandler")
|
|
2711
|
+
return true;
|
|
2712
|
+
const checker = scope.program.getTypeChecker();
|
|
2713
|
+
const location = ts.isPropertyAccessExpression(expression) ? expression.name : expression;
|
|
2714
|
+
const found = checker.getSymbolAtLocation(location);
|
|
2715
|
+
const symbol = found?.flags && found.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(found) : found;
|
|
2716
|
+
return (symbol?.declarations?.some((declaration) => ts.isClassDeclaration(declaration) &&
|
|
2717
|
+
declaration.name?.text === "AppQueryHandler" &&
|
|
2718
|
+
PackageIdentity.nameFor(declaration.getSourceFile().fileName) ===
|
|
2719
|
+
"@spine-event-engine/server") === true);
|
|
2720
|
+
},
|
|
2721
|
+
/**
|
|
2722
|
+
* Validates generated Query/Response provenance and their shared operation.
|
|
2723
|
+
*
|
|
2724
|
+
* @param node Concrete implementation class.
|
|
2725
|
+
* @param base AppQueryHandler heritage clause.
|
|
2726
|
+
* @param scope The source, imports, and diagnostic collection.
|
|
2727
|
+
* @returns Passive description after both generated members are paired.
|
|
2728
|
+
*/
|
|
2729
|
+
record(node, base, scope) {
|
|
2730
|
+
const className = node.name?.text ?? "DefaultAppQueryHandler";
|
|
2731
|
+
const issue = HandlerSources.receiverExportIssue(node, scope.source);
|
|
2732
|
+
if (issue !== undefined) {
|
|
2733
|
+
HandlerTypes.pushDiagnostic(scope, issue.code, node, issue.message, className);
|
|
2734
|
+
return undefined;
|
|
2735
|
+
}
|
|
2736
|
+
const pair = AppQuerySources.validatedPair(base, scope, className);
|
|
2737
|
+
if (pair === undefined)
|
|
2738
|
+
return undefined;
|
|
2739
|
+
return {
|
|
2740
|
+
className,
|
|
2741
|
+
sourceFile: scope.source.fileName,
|
|
2742
|
+
queryTypeName: pair.query.typeName,
|
|
2743
|
+
querySchema: pair.query.reference,
|
|
2744
|
+
responseSchema: pair.response.reference,
|
|
2745
|
+
...(HandlerTypes.hasModifier(node, ts.SyntaxKind.DefaultKeyword)
|
|
2746
|
+
? { defaultExport: true }
|
|
2747
|
+
: {}),
|
|
2748
|
+
};
|
|
2749
|
+
},
|
|
2750
|
+
/**
|
|
2751
|
+
* Resolves and checks a complete generated request/result pair.
|
|
2752
|
+
*
|
|
2753
|
+
* @param base AppQueryHandler heritage clause.
|
|
2754
|
+
* @param scope The source, imports, and diagnostic collection.
|
|
2755
|
+
* @param className Implementation name for a located diagnostic.
|
|
2756
|
+
* @returns The paired generated schema uses, or undefined on error.
|
|
2757
|
+
*/
|
|
2758
|
+
validatedPair(base, scope, className) {
|
|
2759
|
+
const [queryNode, responseNode] = base.clause.typeArguments ?? [];
|
|
2760
|
+
const query = AppQuerySources.resolve(queryNode, scope, new Set(), base.bindings);
|
|
2761
|
+
const response = AppQuerySources.resolve(responseNode, scope, new Set(), base.bindings);
|
|
2762
|
+
const pair = AppQuerySources.pair(query, response);
|
|
2763
|
+
if (pair !== undefined)
|
|
2764
|
+
return pair;
|
|
2765
|
+
HandlerTypes.pushDiagnostic(scope, "INVALID_APP_QUERY_PAIR", base.clause, "AppQueryHandler requires a matching generated _queries.proto Query/Response pair.", className);
|
|
2766
|
+
return undefined;
|
|
2767
|
+
},
|
|
2768
|
+
/**
|
|
2769
|
+
* Matches operation names from generated Query and Response descriptors. Both
|
|
2770
|
+
* members must come from the same declared application operation.
|
|
2771
|
+
*
|
|
2772
|
+
* @param query Resolved request type and schema.
|
|
2773
|
+
* @param response Resolved result type and schema.
|
|
2774
|
+
* @returns The matched pair, or undefined when the declarations differ.
|
|
2775
|
+
*/
|
|
2776
|
+
pair(query, response) {
|
|
2777
|
+
if (query?.kind !== "app-query" || response?.kind !== "app-response")
|
|
2778
|
+
return undefined;
|
|
2779
|
+
return query.typeName.slice(0, -".Query".length) ===
|
|
2780
|
+
response.typeName.slice(0, -".Response".length)
|
|
2781
|
+
? { query, response }
|
|
2782
|
+
: undefined;
|
|
2783
|
+
},
|
|
2784
|
+
/**
|
|
2785
|
+
* Reads imported and grouped type aliases to the generated message declaration. Symbol
|
|
2786
|
+
* identity prevents accepting a structurally compatible unrelated type.
|
|
2787
|
+
*
|
|
2788
|
+
* @param node Declared generic message type argument.
|
|
2789
|
+
* @param scope The source, imports, and program.
|
|
2790
|
+
* @param seen Alias symbols already visited on this path.
|
|
2791
|
+
* @param bindings Concrete generic type substitutions from handler ancestry.
|
|
2792
|
+
* @returns Generated schema use and message name, if resolved.
|
|
2793
|
+
*/
|
|
2794
|
+
resolve(node, scope, seen, bindings = new Map()) {
|
|
2795
|
+
if (node === undefined || !ts.isTypeReferenceNode(node))
|
|
2796
|
+
return undefined;
|
|
2797
|
+
const checker = scope.program.getTypeChecker();
|
|
2798
|
+
const found = checker.getSymbolAtLocation(node.typeName);
|
|
2799
|
+
if (found === undefined || seen.has(found))
|
|
2800
|
+
return undefined;
|
|
2801
|
+
seen.add(found);
|
|
2802
|
+
const bound = bindings.get(found);
|
|
2803
|
+
if (bound !== undefined)
|
|
2804
|
+
return AppQuerySources.resolve(bound, scope, seen, bindings);
|
|
2805
|
+
const symbol = found.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(found) : found;
|
|
2806
|
+
for (const declaration of symbol.declarations ?? []) {
|
|
2807
|
+
if (!ts.isTypeAliasDeclaration(declaration))
|
|
2808
|
+
continue;
|
|
2809
|
+
const source = declaration.getSourceFile();
|
|
2810
|
+
if (/(^|\/)generated\/.+_pb\.d\.ts$/u.test(source.fileName))
|
|
2811
|
+
return AppQuerySources.installed(declaration, source, scope);
|
|
2812
|
+
if (/(^|\/)generated\/.+_pb\.ts$/u.test(source.fileName))
|
|
2813
|
+
return AppQuerySources.generated(declaration, source, scope);
|
|
2814
|
+
const alias = AppQuerySources.resolve(declaration.type, scope, seen, bindings);
|
|
2815
|
+
if (alias !== undefined)
|
|
2816
|
+
return alias;
|
|
2817
|
+
}
|
|
2818
|
+
return undefined;
|
|
2819
|
+
},
|
|
2820
|
+
/**
|
|
2821
|
+
* Checks a packaged declaration against its published Proto manifest and descriptor JS.
|
|
2822
|
+
*
|
|
2823
|
+
* @param declaration Generated message type declaration.
|
|
2824
|
+
* @param source Published declaration module.
|
|
2825
|
+
* @param scope Handler source and TypeScript program.
|
|
2826
|
+
* @returns Schema reference through the package export when provenance agrees.
|
|
2827
|
+
*/
|
|
2828
|
+
installed(declaration, source, scope) {
|
|
2829
|
+
const packageRoot = PackageIdentity.rootFor(source.fileName);
|
|
2830
|
+
if (packageRoot === undefined)
|
|
2831
|
+
return undefined;
|
|
2832
|
+
try {
|
|
2833
|
+
const modulePath = relative(packageRoot, source.fileName)
|
|
2834
|
+
.replace(/^dist\//u, "")
|
|
2835
|
+
.replace(/\.d\.ts$/u, ".js");
|
|
2836
|
+
const name = AppQuerySources.publishedPackage(packageRoot, modulePath);
|
|
2837
|
+
if (name === undefined)
|
|
2838
|
+
return undefined;
|
|
2839
|
+
return AppQuerySources.publishedSchema(declaration, source, scope, packageRoot, name, modulePath);
|
|
2840
|
+
}
|
|
2841
|
+
catch {
|
|
2842
|
+
return undefined;
|
|
2843
|
+
}
|
|
2844
|
+
},
|
|
2845
|
+
/**
|
|
2846
|
+
* Resolves a generated module to its package after checking the published Proto provenance.
|
|
2847
|
+
*
|
|
2848
|
+
* @param root Installed package location.
|
|
2849
|
+
* @param modulePath Generated module path relative to the package's dist directory.
|
|
2850
|
+
* @returns Published package name when its manifest contains this query source.
|
|
2851
|
+
*/
|
|
2852
|
+
publishedPackage(root, modulePath) {
|
|
2853
|
+
const info = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
2854
|
+
const manifest = JSON.parse(readFileSync(join(root, "spine-proto-manifest.json"), "utf8"));
|
|
2855
|
+
if (!info.name ||
|
|
2856
|
+
manifest.packageName !== info.name ||
|
|
2857
|
+
manifest.packageVersion !== info.version)
|
|
2858
|
+
return undefined;
|
|
2859
|
+
const proto = Object.entries(manifest.generatedExports ?? {}).find(([, exported]) => exported === modulePath)?.[0];
|
|
2860
|
+
return proto?.endsWith("_queries.proto") && manifest.protoFiles?.includes(proto)
|
|
2861
|
+
? info.name
|
|
2862
|
+
: undefined;
|
|
2863
|
+
},
|
|
2864
|
+
/**
|
|
2865
|
+
* Parses published generated JavaScript as data without evaluating package code.
|
|
2866
|
+
*
|
|
2867
|
+
* @param path Published runtime module.
|
|
2868
|
+
* @returns Source syntax containing descriptor initializers.
|
|
2869
|
+
*/
|
|
2870
|
+
publishedRuntime(path) {
|
|
2871
|
+
return ts.createSourceFile(path, readFileSync(path, "utf8"), ts.ScriptTarget.ESNext, true, ts.ScriptKind.JS);
|
|
2872
|
+
},
|
|
2873
|
+
/**
|
|
2874
|
+
* Reads a published runtime descriptor without importing or executing the module.
|
|
2875
|
+
*
|
|
2876
|
+
* @param declaration Generated message type declaration.
|
|
2877
|
+
* @param source Published declaration module.
|
|
2878
|
+
* @param scope Handler source and TypeScript program.
|
|
2879
|
+
* @param root Installed package location.
|
|
2880
|
+
* @param name Package export name.
|
|
2881
|
+
* @param modulePath Manifest-listed generated module path.
|
|
2882
|
+
* @returns Verified schema reference for emitted metadata.
|
|
2883
|
+
*/
|
|
2884
|
+
publishedSchema(declaration, source, scope, root, name, modulePath) {
|
|
2885
|
+
const specifier = `${name}/${modulePath}`;
|
|
2886
|
+
const runtimePath = createRequire(scope.source.fileName).resolve(specifier);
|
|
2887
|
+
if (realpathSync(runtimePath) !== realpathSync(join(root, "dist", modulePath)))
|
|
2888
|
+
return undefined;
|
|
2889
|
+
const runtime = AppQuerySources.publishedRuntime(runtimePath);
|
|
2890
|
+
const schemaName = `${declaration.name.text}Schema`;
|
|
2891
|
+
const declarations = HandlerSources.exportedNames(source);
|
|
2892
|
+
const exported = HandlerSources.exportedNames(runtime);
|
|
2893
|
+
const typeName = AppQuerySources.typeName(declaration);
|
|
2894
|
+
if (!declarations.types.has(declaration.name.text) ||
|
|
2895
|
+
!declarations.values.has(schemaName) ||
|
|
2896
|
+
!exported.values.has(schemaName) ||
|
|
2897
|
+
typeName === undefined ||
|
|
2898
|
+
typeName !== AppQuerySources.descriptorName(runtime, schemaName))
|
|
2899
|
+
return undefined;
|
|
2900
|
+
return {
|
|
2901
|
+
kind: exported.schemaRoles.get(schemaName),
|
|
2902
|
+
typeName,
|
|
2903
|
+
reference: { moduleSpecifier: specifier, exportName: schemaName },
|
|
2904
|
+
};
|
|
2905
|
+
},
|
|
2906
|
+
/**
|
|
2907
|
+
* Checks one generated declaration against its decoded Proto descriptor role.
|
|
2908
|
+
*
|
|
2909
|
+
* @param declaration Generated message type alias.
|
|
2910
|
+
* @param source Generated Protobuf-ES module.
|
|
2911
|
+
* @param scope The handler source and program.
|
|
2912
|
+
* @returns Its exact schema use, if the generated declaration is coherent.
|
|
2913
|
+
*/
|
|
2914
|
+
generated(declaration, source, scope) {
|
|
2915
|
+
const schemaName = `${declaration.name.text}Schema`;
|
|
2916
|
+
const exports = HandlerSources.exportedNames(source);
|
|
2917
|
+
const kind = exports.schemaRoles.get(schemaName);
|
|
2918
|
+
const typeName = AppQuerySources.typeName(declaration);
|
|
2919
|
+
if (!exports.types.has(declaration.name.text) ||
|
|
2920
|
+
!exports.values.has(schemaName) ||
|
|
2921
|
+
typeName === undefined ||
|
|
2922
|
+
typeName !== AppQuerySources.descriptorName(source, schemaName))
|
|
2923
|
+
return undefined;
|
|
2924
|
+
const path = relative(dirname(scope.source.fileName), source.fileName).replace(/\.ts$/u, ".js");
|
|
2925
|
+
return {
|
|
2926
|
+
kind,
|
|
2927
|
+
typeName,
|
|
2928
|
+
reference: {
|
|
2929
|
+
moduleSpecifier: path.startsWith(".") ? path : `./${path}`,
|
|
2930
|
+
exportName: schemaName,
|
|
2931
|
+
},
|
|
2932
|
+
};
|
|
2933
|
+
},
|
|
2934
|
+
/**
|
|
2935
|
+
* Reads the canonical message name from the generated schema's binary file descriptor.
|
|
2936
|
+
*
|
|
2937
|
+
* @param source Generated Protobuf-ES module.
|
|
2938
|
+
* @param schemaName Generated schema export name.
|
|
2939
|
+
* @returns Descriptor-derived message name, if the schema initializer is coherent.
|
|
2940
|
+
*/
|
|
2941
|
+
descriptorName(source, schemaName) {
|
|
2942
|
+
const declarations = source.statements
|
|
2943
|
+
.filter(ts.isVariableStatement)
|
|
2944
|
+
.flatMap((statement) => [...statement.declarationList.declarations]);
|
|
2945
|
+
const schema = declarations.find((declaration) => ts.isIdentifier(declaration.name) && declaration.name.text === schemaName);
|
|
2946
|
+
const call = HandlerSources.callExpression(schema?.initializer, "messageDesc");
|
|
2947
|
+
const fileName = call?.arguments[0];
|
|
2948
|
+
if (call === undefined || fileName === undefined || !ts.isIdentifier(fileName))
|
|
2949
|
+
return undefined;
|
|
2950
|
+
const file = HandlerSources.generatedFiles(source).get(fileName.text);
|
|
2951
|
+
if (file === undefined)
|
|
2952
|
+
return undefined;
|
|
2953
|
+
const selected = HandlerSources.descriptorMessageAt(file, HandlerSources.messageDescIndexes(call));
|
|
2954
|
+
return selected === undefined ? undefined : `${file.packageName}.${selected.typePath}`;
|
|
2955
|
+
},
|
|
2956
|
+
/**
|
|
2957
|
+
* Reads Protobuf-ES's literal Message type argument without trusting a structural lookalike.
|
|
2958
|
+
*
|
|
2959
|
+
* @param declaration Generated message type alias.
|
|
2960
|
+
* @returns Its literal Protobuf name, if present.
|
|
2961
|
+
*/
|
|
2962
|
+
typeName(declaration) {
|
|
2963
|
+
const parts = ts.isIntersectionTypeNode(declaration.type)
|
|
2964
|
+
? declaration.type.types
|
|
2965
|
+
: [declaration.type];
|
|
2966
|
+
const message = parts.find((part) => ts.isTypeReferenceNode(part) &&
|
|
2967
|
+
ts.isIdentifier(part.typeName) &&
|
|
2968
|
+
part.typeName.text === "Message");
|
|
2969
|
+
if (message === undefined || !ts.isTypeReferenceNode(message))
|
|
2970
|
+
return undefined;
|
|
2971
|
+
const name = message.typeArguments?.[0];
|
|
2972
|
+
return name !== undefined && ts.isLiteralTypeNode(name) && ts.isStringLiteral(name.literal)
|
|
2973
|
+
? name.literal.text
|
|
2974
|
+
: undefined;
|
|
2975
|
+
},
|
|
2976
|
+
});
|
|
2588
2977
|
/**
|
|
2589
2978
|
* Finds the package identity that owns an analyzed source file.
|
|
2590
2979
|
*/
|
|
2591
2980
|
export const PackageIdentity = Object.freeze({
|
|
2592
|
-
|
|
2981
|
+
/**
|
|
2982
|
+
* Locates the nearest package manifest above a declaration.
|
|
2983
|
+
*
|
|
2984
|
+
* @param sourceFile Source within the package.
|
|
2985
|
+
* @returns Package directory when a manifest exists.
|
|
2986
|
+
*/
|
|
2987
|
+
rootFor(sourceFile) {
|
|
2593
2988
|
let directory = resolve(dirname(sourceFile));
|
|
2594
|
-
|
|
2595
|
-
while (!existsSync(manifest)) {
|
|
2989
|
+
while (!existsSync(join(directory, "package.json"))) {
|
|
2596
2990
|
const parent = dirname(directory);
|
|
2597
2991
|
if (parent === directory)
|
|
2598
2992
|
return undefined;
|
|
2599
2993
|
directory = parent;
|
|
2600
|
-
manifest = join(directory, "package.json");
|
|
2601
2994
|
}
|
|
2995
|
+
return directory;
|
|
2996
|
+
},
|
|
2997
|
+
/**
|
|
2998
|
+
* Reads the nearest package's declared name.
|
|
2999
|
+
*
|
|
3000
|
+
* @param sourceFile Source within the package.
|
|
3001
|
+
* @returns Declared package name, if readable.
|
|
3002
|
+
*/
|
|
3003
|
+
nameFor(sourceFile) {
|
|
3004
|
+
const root = PackageIdentity.rootFor(sourceFile);
|
|
3005
|
+
if (root === undefined)
|
|
3006
|
+
return undefined;
|
|
2602
3007
|
try {
|
|
2603
|
-
const { name } = JSON.parse(readFileSync(
|
|
3008
|
+
const { name } = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
2604
3009
|
return typeof name === "string" ? name : undefined;
|
|
2605
3010
|
}
|
|
2606
3011
|
catch {
|