@@ -14,23 +14,108 @@ const COMMAND_CACHE_FILENAME = "codeql-action-command-cache.json";
1414/** A key used to identify cached command output. */
1515export type CommandCacheKey = "version" | "resolve languages" ;
1616
17+ export interface VersionInfo {
18+ version : string ;
19+ features ?: { [ name : string ] : boolean } ;
20+ /**
21+ * The overlay version helps deal with backward incompatible changes for
22+ * overlay analysis. When a precompiled query pack reports the same overlay
23+ * version as the CodeQL CLI, we can use the CodeQL CLI to perform overlay
24+ * analysis with that pack. Otherwise, if the overlay versions are different,
25+ * or if either the pack or the CLI does not report an overlay version,
26+ * we need to revert to non-overlay analysis.
27+ */
28+ overlayVersion ?: number ;
29+ }
30+
31+ /** Returns true if `x` is a {@link VersionInfo}. */
32+ export function isVersionInfo ( x : unknown ) : x is VersionInfo {
33+ return (
34+ json . isObject ( x ) &&
35+ json . validateSchema (
36+ {
37+ version : json . string ,
38+ features : json . optional ( json . object ) ,
39+ overlayVersion : json . optional ( json . number ) ,
40+ } ,
41+ x ,
42+ )
43+ ) ;
44+ }
45+
46+ export interface ResolveLanguagesOutput {
47+ aliases ?: {
48+ [ alias : string ] : string ;
49+ } ;
50+ extractors : {
51+ [ language : string ] : Array < {
52+ extractor_root : string ;
53+ extractor_options ?: any ;
54+ } > ;
55+ } ;
56+ }
57+
58+ /** Returns true if `x` is a {@link ResolveLanguagesOutput}. */
59+ export function isResolveLanguagesOutput (
60+ x : unknown ,
61+ ) : x is ResolveLanguagesOutput {
62+ return (
63+ json . isObject < ResolveLanguagesOutput > ( x ) &&
64+ json . isObject ( x . extractors ) &&
65+ Object . values ( x . extractors ) . every (
66+ ( extractorList ) =>
67+ json . isArray ( extractorList ) &&
68+ extractorList . every (
69+ ( extractor ) =>
70+ json . isObject < { extractor_root : unknown } > ( extractor ) &&
71+ json . isString ( extractor . extractor_root ) ,
72+ ) ,
73+ ) &&
74+ ( x . aliases === undefined ||
75+ ( json . isObject ( x . aliases ) &&
76+ Object . values ( x . aliases ) . every ( ( alias ) => json . isString ( alias ) ) ) )
77+ ) ;
78+ }
79+
80+ export type CommandCacheKeyOutputMap = {
81+ version : VersionInfo ;
82+ "resolve languages" : ResolveLanguagesOutput ;
83+ } ;
84+
85+ const commandCacheValidators : {
86+ [ K in CommandCacheKey ] : (
87+ output : unknown ,
88+ ) => output is CommandCacheKeyOutputMap [ K ] ;
89+ } = {
90+ version : isVersionInfo ,
91+ "resolve languages" : isResolveLanguagesOutput ,
92+ } ;
93+
94+ interface StoredCommandCacheEntry {
95+ cmd : string ;
96+ output : unknown ;
97+ }
98+
1799/** A single cached command output together with the CLI path it came from. */
18- interface CommandCacheEntry {
100+ export interface CommandCacheEntry < K extends CommandCacheKey > {
19101 /**
20102 * The path to the CodeQL CLI that produced `output`. Persisted so that a
21103 * different step using a different CodeQL bundle doesn't pick up a stale
22104 * value.
23105 */
24106 cmd : string ;
25- output : unknown ;
107+ output : CommandCacheKeyOutputMap [ K ] ;
26108}
27109
28110/**
29111 * Tier 1: the in-process memo. Consulted first on every lookup and populated
30112 * whenever a value is read from the file (tier 2) or computed via the CLI
31113 * (tier 3).
32114 */
33- const inMemoryCache = new Map < CommandCacheKey , CommandCacheEntry > ( ) ;
115+ const inMemoryCache = new Map <
116+ CommandCacheKey ,
117+ CommandCacheEntry < CommandCacheKey >
118+ > ( ) ;
34119const logger = getActionsLogger ( ) ;
35120
36121function getCommandCacheFilePath ( ) : string {
@@ -41,7 +126,7 @@ function getCommandCacheFilePath(): string {
41126 * Reads and parses the temporary cache file. Best-effort: a missing, malformed,
42127 * or otherwise unreadable file is treated as an empty cache.
43128 */
44- function readCommandCacheFile ( ) : Record < string , CommandCacheEntry > {
129+ function readCommandCacheFile ( ) : Record < string , StoredCommandCacheEntry > {
45130 if ( ! fs . existsSync ( getCommandCacheFilePath ( ) ) ) {
46131 return { } ;
47132 }
@@ -75,12 +160,12 @@ export function writeCommandCacheFile(): void {
75160/**
76161 * Stores the output of a CLI command under `key` in a module-global object.
77162 */
78- export function cacheCommandOutput (
79- key : CommandCacheKey ,
163+ export function cacheCommandOutput < K extends CommandCacheKey > (
164+ key : K ,
80165 cmd : string ,
81- output : unknown ,
166+ output : CommandCacheKeyOutputMap [ K ] ,
82167) : void {
83- const entry : CommandCacheEntry = { cmd, output } ;
168+ const entry : CommandCacheEntry < K > = { cmd, output } ;
84169 inMemoryCache . set ( key , entry ) ;
85170}
86171
@@ -95,32 +180,33 @@ export function cacheCommandOutput(
95180 * A return value of `undefined` signals the caller to fall back to tier 3 (the
96181 * CLI).
97182 */
98- export function getCachedCommandOutput < T > (
99- key : CommandCacheKey ,
183+ export function getCachedCommandOutput < K extends CommandCacheKey > (
184+ key : K ,
100185 cmd ?: string ,
101- validate ?: ( output : unknown ) => output is T ,
102- ) : T | undefined {
186+ ) : CommandCacheKeyOutputMap [ K ] | undefined {
103187 // Tier 1: the in-memory variable.
104188 const memoized = inMemoryCache . get ( key ) ;
105189 if ( memoized !== undefined ) {
106- return memoized . output as T ;
190+ return memoized . output as CommandCacheKeyOutputMap [ K ] ;
107191 }
108192
109193 // Tier 2: the temporary file persisted by an earlier step, if any.
110194 const entry = readCommandCacheFile ( ) [ key ] as unknown ;
111195 if (
112- ! json . isObject < CommandCacheEntry > ( entry ) ||
196+ ! json . isObject < StoredCommandCacheEntry > ( entry ) ||
113197 ! json . isString ( entry . cmd ) ||
114198 ( cmd !== undefined && entry . cmd !== cmd ) ||
115- ( validate !== undefined && ! validate ( entry . output ) )
199+ ! commandCacheValidators [ key ] ( entry . output )
116200 ) {
117201 logger . warning ( "Received invalid data from the command-cache file." ) ;
118202 return undefined ;
119203 }
120204
121205 // Memoize so subsequent lookups in this process hit tier 1.
122- cacheCommandOutput ( key , entry . cmd , entry . output ) ;
123- return entry . output as T ;
206+ const cachedEntry = entry as StoredCommandCacheEntry ;
207+ const output = cachedEntry . output as CommandCacheKeyOutputMap [ K ] ;
208+ cacheCommandOutput ( key , cachedEntry . cmd , output ) ;
209+ return output ;
124210}
125211
126212/**
0 commit comments