11import { randomUUID } from 'node:crypto' ;
22import { mkdir , readFile , rename , rm , writeFile } from 'node:fs/promises' ;
33import path from 'node:path' ;
4- import { isCacheHash } from './cacheHash.ts' ;
54
65const fmtCacheFileName = 'v2.json' ;
76const fmtCacheVersion = 2 ;
@@ -60,14 +59,6 @@ const createEmptyCache = (namespace: string): ParsedFmtCacheFile => ({
6059 optionsUseCounts : [ ] ,
6160} ) ;
6261
63- const isFmtCacheStateId = ( value : unknown ) : value is FmtCacheStateId =>
64- typeof value === 'number' &&
65- Number . isInteger ( value ) &&
66- value >= 0 &&
67- value < fmtCacheStates . length ;
68-
69- const isUnknownArray = ( value : unknown ) : value is unknown [ ] => Array . isArray ( value ) ;
70-
7162const parseCacheFile = ( content : string ) : ParsedFmtCacheFile | undefined => {
7263 let value : unknown ;
7364 try {
@@ -76,60 +67,38 @@ const parseCacheFile = (content: string): ParsedFmtCacheFile | undefined => {
7667 return ;
7768 }
7869
70+ if ( typeof value !== 'object' || value === null || Array . isArray ( value ) ) {
71+ return ;
72+ }
73+
74+ const cache = value as FmtCacheFile ;
75+ const { version, namespace, options, files } = cache ;
7976 if (
80- typeof value !== 'object' ||
81- value === null ||
82- isUnknownArray ( value ) ||
83- ! ( 'version' in value ) ||
84- value . version !== fmtCacheVersion ||
85- ! ( 'namespace' in value ) ||
86- typeof value . namespace !== 'string' ||
87- ! ( 'options' in value ) ||
88- ! isUnknownArray ( value . options ) ||
89- ! ( 'files' in value ) ||
90- ! isUnknownArray ( value . files ) ||
91- value . files . length % fileEntryWidth !== 0
77+ version !== fmtCacheVersion ||
78+ typeof namespace !== 'string' ||
79+ ! Array . isArray ( options ) ||
80+ ! Array . isArray ( files ) ||
81+ files . length % fileEntryWidth !== 0
9282 ) {
9383 return ;
9484 }
9585
9686 const optionsIndexes = new Map < string , number > ( ) ;
97- for ( let index = 0 ; index < value . options . length ; index ++ ) {
98- const optionsHash = value . options [ index ] ;
99- if ( ! isCacheHash ( optionsHash ) || optionsIndexes . has ( optionsHash ) ) {
100- return ;
101- }
102- optionsIndexes . set ( optionsHash , index ) ;
87+ for ( let index = 0 ; index < options . length ; index ++ ) {
88+ optionsIndexes . set ( options [ index ] , index ) ;
10389 }
10490
10591 const fileOffsets = new Map < string , number > ( ) ;
106- const optionsUseCounts = new Array < number > ( value . options . length ) . fill ( 0 ) ;
107- for ( let offset = 0 ; offset < value . files . length ; offset += fileEntryWidth ) {
108- const filePath = value . files [ offset ] ;
109- const contentHash = value . files [ offset + contentHashOffset ] ;
110- const optionsIndex = value . files [ offset + optionsIndexOffset ] ;
111- const state = value . files [ offset + stateOffset ] ;
112- if (
113- typeof filePath !== 'string' ||
114- fileOffsets . has ( filePath ) ||
115- typeof optionsIndex !== 'number' ||
116- ! Number . isInteger ( optionsIndex ) ||
117- optionsIndex < 0 ||
118- optionsIndex >= value . options . length ||
119- ! isFmtCacheStateId ( state ) ||
120- ( state === fmtCacheStateIds . unsupported
121- ? contentHash !== null && ! isCacheHash ( contentHash )
122- : ! isCacheHash ( contentHash ) )
123- ) {
124- return ;
125- }
126-
92+ const optionsUseCounts = new Array < number > ( options . length ) . fill ( 0 ) ;
93+ for ( let offset = 0 ; offset < files . length ; offset += fileEntryWidth ) {
94+ const filePath = files [ offset ] as string ;
95+ const optionsIndex = files [ offset + optionsIndexOffset ] as number ;
12796 fileOffsets . set ( filePath , offset ) ;
12897 optionsUseCounts [ optionsIndex ] ++ ;
12998 }
13099
131100 return {
132- cache : value as FmtCacheFile ,
101+ cache,
133102 fileOffsets,
134103 optionsIndexes,
135104 optionsUseCounts,
0 commit comments