1- import type { ApiFile } from '@android-cs/api-parser' ;
2- import { BoundedContentValueInterner } from '@android-cs/api-query' ;
1+ import {
2+ BoundedContentValueInterner ,
3+ type AndroidApiStructCacheEntry ,
4+ } from '@android-cs/api-query' ;
5+ import {
6+ decodeApiFileCacheEntry ,
7+ encodeApiFileCacheEntry ,
8+ } from './cache/apiFileEntry' ;
39import {
410 readCacheValue ,
511 writeCacheBytesIfAvailable ,
@@ -15,7 +21,6 @@ import { resetCacheDatabases } from './cache/database';
1521import {
1622 broadcastCacheReset ,
1723 getCacheEpoch ,
18- getLogicalFlight ,
1924 invalidateLocalFlights ,
2025 runSingleFlight ,
2126 setExternalResetHandler ,
@@ -26,7 +31,8 @@ interface UrlCacheKeyBuilder {
2631 ( url : string ) : string ;
2732}
2833
29- const apiFileInterner = new BoundedContentValueInterner < ApiFile > ( 256 ) ;
34+ const apiFileInterner =
35+ new BoundedContentValueInterner < AndroidApiStructCacheEntry > ( 256 ) ;
3036
3137const scheduleStorageEstimateUpdate = ( ) : void => {
3238 void updateStorageEstimate ( ) . catch ( ( ) => undefined ) ;
@@ -79,36 +85,21 @@ export const persistentFetch = async (
7985
8086export const getOrSetApiFileCache = async (
8187 filePath : string ,
82- fallback : ( ) => Promise < ApiFile > ,
83- ) : Promise < ApiFile > => {
88+ fallback : ( ) => Promise < AndroidApiStructCacheEntry > ,
89+ ) : Promise < AndroidApiStructCacheEntry > => {
8490 const expectedEpoch = getCacheEpoch ( ) ;
8591 const keyHash = await sha256String ( filePath ) ;
8692 return runSingleFlight ( STRUCT_DOMAIN , keyHash , expectedEpoch , async ( ) => {
8793 const cached = await readCacheValue (
8894 STRUCT_DOMAIN ,
8995 keyHash ,
90- ( bytes ) : ApiFile => {
91- const value : unknown = JSON . parse ( decodeText ( bytes ) ) ;
92- if (
93- typeof value !== 'object' ||
94- value === null ||
95- ! ( 'package' in value ) ||
96- ! ( 'imports' in value ) ||
97- ! ( 'structs' in value ) ||
98- typeof value . package !== 'string' ||
99- ! Array . isArray ( value . imports ) ||
100- ! Array . isArray ( value . structs )
101- ) {
102- throw new Error ( 'Cached API file value is invalid' ) ;
103- }
104- return value as ApiFile ;
105- } ,
96+ decodeApiFileCacheEntry ,
10697 apiFileInterner ,
10798 ) . catch ( ( ) => undefined ) ;
10899 if ( cached !== undefined ) return cached ;
109100
110101 const value = await fallback ( ) ;
111- const bytes = encodeText ( JSON . stringify ( value ) ) ;
102+ const bytes = encodeApiFileCacheEntry ( value ) ;
112103 const contentHash = await writeCacheBytesIfAvailable (
113104 STRUCT_DOMAIN ,
114105 keyHash ,
@@ -120,24 +111,6 @@ export const getOrSetApiFileCache = async (
120111 } ) ;
121112} ;
122113
123- export const check404File = async ( filePath : string ) : Promise < boolean > => {
124- const expectedEpoch = getCacheEpoch ( ) ;
125- const keyHash = await sha256String ( filePath ) ;
126- const current = getLogicalFlight ( TEXT_DOMAIN , keyHash , expectedEpoch ) ;
127- if ( current ) {
128- try {
129- const value : unknown = await current ;
130- return typeof value === 'string' && value . startsWith ( '404:' ) ;
131- } catch {
132- return false ;
133- }
134- }
135- const value = await readCacheValue ( TEXT_DOMAIN , keyHash , decodeText ) . catch (
136- ( ) => undefined ,
137- ) ;
138- return value ?. startsWith ( '404:' ) ?? false ;
139- } ;
140-
141114export const clearLocalCache = async ( ) : Promise < void > => {
142115 invalidateLocalFlights ( ) ;
143116 apiFileInterner . clear ( ) ;
0 commit comments