Skip to content

Commit 09ad830

Browse files
authored
Merge pull request #104 from ryoppippi/feature/98
refactor: json -> text for cache data and remove validation
2 parents 5ccf4f6 + 65a4e7f commit 09ad830

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

packages/unplugin-typia/src/core/cache.ts

+18-32
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,17 @@ import { accessSync, constants, existsSync, mkdirSync, readFileSync, rmSync, wri
22
import { createHash } from 'node:crypto';
33
import { basename, dirname, join } from 'pathe';
44
import type { Tagged } from 'type-fest';
5+
import { readPackageJSON } from 'pkg-types';
56
import type { transformTypia } from './typia.js';
67
import type { ResolvedOptions } from './options.js';
8+
import { isBun } from './utils.js';
79

810
type ResolvedCacheOptions = ResolvedOptions['cache'];
911

1012
type Data = Awaited<ReturnType<typeof transformTypia>>;
1113

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-
2614
let cacheDir: string | null = null;
15+
let typiaVersion: string | undefined;
2716

2817
/**
2918
* Get cache
@@ -44,26 +33,17 @@ export async function getCache(
4433
const key = getKey(id, source);
4534
const path = getCachePath(key, option);
4635

47-
let data: StoreData | null = null;
4836
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');
5638

57-
/** validate cache */
58-
if (!isStoreData(data)) {
59-
return null;
60-
}
39+
const hashComment = await getHashComment(key);
6140

62-
if (data.source !== source) {
63-
return null;
41+
if (data.endsWith(hashComment)) {
42+
return data;
43+
}
6444
}
6545

66-
return data.data;
46+
return null;
6747
}
6848

6949
/**
@@ -86,14 +66,15 @@ export async function setCache(
8666

8767
const key = getKey(id, source);
8868
const path = getCachePath(key, option);
69+
const hashComment = await getHashComment(key);
8970

9071
if (data == null) {
9172
rmSync(path);
9273
return;
9374
}
9475

95-
const json = JSON.stringify({ data, source });
96-
writeFileSync(path, json, { encoding: 'utf8' });
76+
const cache = data + hashComment;
77+
writeFileSync(path, cache, { encoding: 'utf8' });
9778
}
9879

9980
type CacheKey = Tagged<string, 'cache-key'>;
@@ -152,8 +133,13 @@ function isWritable(filename: string): boolean {
152133
* @returns The hash string.
153134
*/
154135
function hash(input: string): string {
155-
if (globalThis.Bun != null) {
136+
if (isBun()) {
156137
return Bun.hash(input).toString();
157138
}
158139
return createHash('md5').update(input).digest('hex');
159140
}
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+
}

packages/unplugin-typia/test/cache.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ type Data = Parameters<typeof cache.setCache>[2];
88

99
const tmp = tmpdir();
1010

11+
function removeComments(data: string | null) {
12+
if (data == null) {
13+
return data;
14+
}
15+
return data.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '');
16+
}
17+
1118
test('return null if cache is not found', async () => {
1219
const option = { enable: true, base: tmp } as const;
1320
const random = Math.random().toString();
@@ -27,7 +34,10 @@ test('set and get cache', async () => {
2734
/* get cache */
2835
const cacheData = await cache.getCache(filename, random, option);
2936

30-
assertEquals(cacheData, data);
37+
/* delete js asterisk comments */
38+
const cacheDataStr = removeComments(cacheData);
39+
40+
assertEquals(data, cacheDataStr);
3141
});
3242

3343
test('set and get null with different id', async () => {
@@ -40,9 +50,13 @@ test('set and get null with different id', async () => {
4050
await cache.setCache(filename, random, data, option);
4151

4252
/* get cache */
43-
const cacheData = await cache.getCache(`${random}-1`, random, option);
53+
const cacheData = await cache.getCache(`111;${random}`, random, option);
54+
55+
/* delete js asterisk comments */
56+
const cacheDataStr = removeComments(cacheData);
4457

45-
assertNotEquals(cacheData, data);
58+
assertEquals(cacheDataStr, null);
59+
assertNotEquals(data, cacheDataStr);
4660
});
4761

4862
test('set and get null with cache disabled', async () => {

0 commit comments

Comments
 (0)