@@ -2,28 +2,17 @@ import { accessSync, constants, existsSync, mkdirSync, readFileSync, rmSync, wri
2
2
import { createHash } from 'node:crypto' ;
3
3
import { basename , dirname , join } from 'pathe' ;
4
4
import type { Tagged } from 'type-fest' ;
5
+ import { readPackageJSON } from 'pkg-types' ;
5
6
import type { transformTypia } from './typia.js' ;
6
7
import type { ResolvedOptions } from './options.js' ;
8
+ import { isBun } from './utils.js' ;
7
9
8
10
type ResolvedCacheOptions = ResolvedOptions [ 'cache' ] ;
9
11
10
12
type Data = Awaited < ReturnType < typeof transformTypia > > ;
11
13
12
- interface StoreData {
13
- data : NonNullable < Data > ;
14
- source : string ;
15
- }
16
-
17
- function isStoreData ( value : unknown ) : value is StoreData {
18
- if ( typeof value !== 'object' || value === null ) {
19
- return false ;
20
- }
21
- const data = value as StoreData ;
22
- return data . data != null
23
- && typeof data . source === 'string' ;
24
- }
25
-
26
14
let cacheDir : string | null = null ;
15
+ let typiaVersion : string | undefined ;
27
16
28
17
/**
29
18
* Get cache
@@ -44,26 +33,17 @@ export async function getCache(
44
33
const key = getKey ( id , source ) ;
45
34
const path = getCachePath ( key , option ) ;
46
35
47
- let data : StoreData | null = null ;
48
36
if ( existsSync ( path ) ) {
49
- const cache = readFileSync ( path , 'utf8' ) ;
50
- const json = JSON . parse ( cache ) ;
51
- if ( ! isStoreData ( json ) ) {
52
- return null ;
53
- }
54
- data = json ;
55
- }
37
+ const data = readFileSync ( path , 'utf8' ) ;
56
38
57
- /** validate cache */
58
- if ( ! isStoreData ( data ) ) {
59
- return null ;
60
- }
39
+ const hashComment = await getHashComment ( key ) ;
61
40
62
- if ( data . source !== source ) {
63
- return null ;
41
+ if ( data . endsWith ( hashComment ) ) {
42
+ return data ;
43
+ }
64
44
}
65
45
66
- return data . data ;
46
+ return null ;
67
47
}
68
48
69
49
/**
@@ -86,14 +66,15 @@ export async function setCache(
86
66
87
67
const key = getKey ( id , source ) ;
88
68
const path = getCachePath ( key , option ) ;
69
+ const hashComment = await getHashComment ( key ) ;
89
70
90
71
if ( data == null ) {
91
72
rmSync ( path ) ;
92
73
return ;
93
74
}
94
75
95
- const json = JSON . stringify ( { data, source } ) ;
96
- writeFileSync ( path , json , { encoding : 'utf8' } ) ;
76
+ const cache = data + hashComment ;
77
+ writeFileSync ( path , cache , { encoding : 'utf8' } ) ;
97
78
}
98
79
99
80
type CacheKey = Tagged < string , 'cache-key' > ;
@@ -152,8 +133,13 @@ function isWritable(filename: string): boolean {
152
133
* @returns The hash string.
153
134
*/
154
135
function hash ( input : string ) : string {
155
- if ( globalThis . Bun != null ) {
136
+ if ( isBun ( ) ) {
156
137
return Bun . hash ( input ) . toString ( ) ;
157
138
}
158
139
return createHash ( 'md5' ) . update ( input ) . digest ( 'hex' ) ;
159
140
}
141
+
142
+ export async function getHashComment ( cachePath : CacheKey ) {
143
+ typiaVersion = typiaVersion ?? await readPackageJSON ( 'typia' ) . then ( pkg => pkg . version ) ;
144
+ return `/* unplugin-typia-${ typiaVersion ?? '' } -${ cachePath } */` ;
145
+ }
0 commit comments