Skip to content

Commit 5e2e5d8

Browse files
authored
refactor: Support url based cache entries (#7639)
1 parent 6dd9e9c commit 5e2e5d8

File tree

6 files changed

+38
-38
lines changed

6 files changed

+38
-38
lines changed

packages/cspell/src/util/cache/DiskCache.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as path from 'node:path';
1+
import { pathToFileURL } from 'node:url';
22

33
import { afterEach, beforeEach, describe, expect, type Mock, test, vi } from 'vitest';
44

@@ -56,14 +56,14 @@ describe('DiskCache', () => {
5656
}
5757

5858
beforeEach(async () => {
59-
diskCache = await createDiskCache('.foobar', false, 'version', false);
59+
diskCache = await createDiskCache(pathToFileURL('.foobar'), false, 'version', false);
6060
_fileEntryCache = mockCreateFileEntryCache.mock.results[0].value;
6161
});
6262

6363
describe('constructor', () => {
6464
test('creates file-entry-cache in specified location', () => {
6565
expect(mockCreateFileEntryCache).toHaveBeenCalledTimes(1);
66-
expect(mockCreateFileEntryCache).toHaveBeenCalledWith(path.resolve('.foobar'), false, undefined);
66+
expect(mockCreateFileEntryCache).toHaveBeenCalledWith(pathToFileURL('.foobar'), false, undefined);
6767
});
6868
});
6969

packages/cspell/src/util/cache/DiskCache.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import assert from 'node:assert';
22
import * as crypto from 'node:crypto';
33
import * as fs from 'node:fs';
4-
import { dirname, isAbsolute as isAbsolutePath, relative as relativePath, resolve as resolvePath } from 'node:path';
4+
import {
5+
isAbsolute as isAbsolutePath,
6+
relative as relativePath,
7+
resolve as resolvePath,
8+
sep as pathSep,
9+
} from 'node:path';
10+
import { fileURLToPath } from 'node:url';
511

612
import { readFileInfo } from '../../util/fileHelper.js';
713
import type { LintFileResult } from '../../util/LintFileResult.js';
814
import type { CSpellLintResultCache } from './CSpellLintResultCache.js';
915
import type { FileDescriptor, FileEntryCache } from './fileEntryCache.js';
10-
import { createFromFile, normalizePath } from './fileEntryCache.js';
16+
import { createFromFile } from './fileEntryCache.js';
1117
import { ShallowObjectCollection } from './ObjectCollection.js';
1218

1319
export type CachedFileResult = Omit<LintFileResult, 'fileInfo' | 'elapsedTimeMs' | 'cached'>;
@@ -66,7 +72,6 @@ interface DependencyCacheTree {
6672
* Caches cspell results on disk
6773
*/
6874
export class DiskCache implements CSpellLintResultCache {
69-
public readonly cacheFileLocation: string;
7075
private cacheDir: string;
7176
private dependencyCache: Map<string, Dependency> = new Map();
7277
private dependencyCacheTree: DependencyCacheTree = {};
@@ -75,14 +80,13 @@ export class DiskCache implements CSpellLintResultCache {
7580
readonly version: string;
7681

7782
constructor(
78-
cacheFileLocation: string,
83+
readonly cacheFileLocation: URL,
7984
readonly useCheckSum: boolean,
8085
readonly cspellVersion: string,
8186
readonly useUniversalCache: boolean,
8287
private fileEntryCache: FileEntryCache,
8388
) {
84-
this.cacheFileLocation = resolvePath(cacheFileLocation);
85-
this.cacheDir = dirname(this.cacheFileLocation);
89+
this.cacheDir = fileURLToPath(new URL('./', cacheFileLocation));
8690
this.version = calcVersion(cspellVersion);
8791
}
8892

@@ -244,7 +248,7 @@ export class DiskCache implements CSpellLintResultCache {
244248
}
245249

246250
export async function createDiskCache(
247-
cacheFileLocation: string,
251+
cacheFileLocation: URL,
248252
useCheckSum: boolean,
249253
cspellVersion: string,
250254
useUniversalCache: boolean,
@@ -293,6 +297,11 @@ function calcVersion(version: string): string {
293297
return version + META_DATA_VERSION_SUFFIX;
294298
}
295299

300+
export function normalizePath(filePath: string): string {
301+
if (pathSep === '/') return filePath;
302+
return filePath.split(pathSep).join('/');
303+
}
304+
296305
export const __testing__: {
297306
calcVersion: typeof calcVersion;
298307
} = {

packages/cspell/src/util/cache/createCache.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { stat } from 'node:fs/promises';
33
import path from 'node:path';
44

55
import type { CacheSettings, CSpellSettings } from '@cspell/cspell-types';
6+
import { toFileURL } from '@cspell/url';
67

78
import { isErrorLike } from '../errors.js';
89
import type { CacheOptions } from './CacheOptions.js';
@@ -33,7 +34,7 @@ const versionSuffix = '';
3334
*/
3435
export async function createCache(options: CreateCacheSettings): Promise<CSpellLintResultCache> {
3536
const { useCache, cacheLocation, cacheStrategy, reset } = options;
36-
const location = path.resolve(cacheLocation);
37+
const location = toFileURL(cacheLocation);
3738
const useChecksum = cacheStrategy === 'content';
3839
const version = normalizeVersion(options.version);
3940
const useUniversal = options.cacheFormat === 'universal';

packages/cspell/src/util/cache/file-entry-cache/file-entry-cache.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ import crypto from 'node:crypto';
22
import type { Stats } from 'node:fs';
33
import fs from 'node:fs/promises';
44
import path from 'node:path';
5+
import { fileURLToPath } from 'node:url';
56

67
import type { FlatCache } from './flatCache.js';
78
import { loadCacheFile as loadFlatCache } from './flatCache.js';
89

910
export async function createFromFile(
10-
filePath: string,
11+
cacheFileUrl: URL,
1112
useChecksum?: boolean,
12-
currentWorkingDir?: string,
13+
currentWorkingDir?: URL,
1314
): Promise<FileEntryCache> {
14-
const cache = await loadFlatCache<Meta>(filePath);
15+
const cache = await loadFlatCache<Meta>(cacheFileUrl);
1516
const fec = new ImplFileEntryCache(cache, useChecksum ?? false, currentWorkingDir);
1617
await fec.removeNotFoundFiles();
1718
return fec;
@@ -27,10 +28,10 @@ class ImplFileEntryCache implements FileEntryCache {
2728
*/
2829
readonly currentWorkingDir: string | undefined;
2930

30-
constructor(cache: FlatCache<Meta>, useChecksum?: boolean, currentWorkingDir?: string) {
31+
constructor(cache: FlatCache<Meta>, useChecksum?: boolean, currentWorkingDir?: URL) {
3132
this.cache = cache;
3233
this.useChecksum = useChecksum || false;
33-
this.currentWorkingDir = currentWorkingDir;
34+
this.currentWorkingDir = currentWorkingDir ? fileURLToPath(currentWorkingDir) : undefined;
3435
}
3536

3637
async removeNotFoundFiles() {

packages/cspell/src/util/cache/file-entry-cache/flatCache.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import fs from 'node:fs/promises';
2-
import path from 'node:path';
32

43
import { parse, stringify } from 'flatted';
54

65
export class FlatCache<T> {
76
#cache: Map<string, T>;
87

9-
constructor(readonly cacheFilename: string) {
8+
constructor(readonly cacheFilename: URL) {
109
this.#cache = new Map<string, T>();
1110
}
1211

@@ -41,7 +40,7 @@ export class FlatCache<T> {
4140
}
4241

4342
async save(): Promise<void> {
44-
const dir = path.dirname(this.cacheFilename);
43+
const dir = new URL('.', this.cacheFilename);
4544
await fs.mkdir(dir, { recursive: true });
4645
const content = stringify(Object.fromEntries(this.#cache.entries()));
4746
await fs.writeFile(this.cacheFilename, content, 'utf8');
@@ -61,7 +60,12 @@ export class FlatCache<T> {
6160
}
6261
}
6362

64-
export function loadCacheFile<T>(file: string): Promise<FlatCache<T>> {
65-
const cache = new FlatCache<T>(file);
63+
/**
64+
*
65+
* @param cachefile - The location of the cache file.
66+
* @returns
67+
*/
68+
export function loadCacheFile<T>(cachefile: URL): Promise<FlatCache<T>> {
69+
const cache = new FlatCache<T>(cachefile);
6670
return cache.load();
6771
}
Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
1-
import { mkdirSync } from 'node:fs';
2-
import * as path from 'node:path';
3-
41
import type { FileEntryCache } from './file-entry-cache/index.js';
52
import * as fec from './file-entry-cache/index.js';
63

74
export type { FileDescriptor, FileEntryCache } from './file-entry-cache/index.js';
85

9-
export function createFromFile(
10-
pathToCache: string,
11-
useCheckSum: boolean,
12-
useRelative: boolean,
13-
): Promise<FileEntryCache> {
14-
const absPathToCache = path.resolve(pathToCache);
15-
const relDir = path.dirname(absPathToCache);
16-
mkdirSync(relDir, { recursive: true });
17-
return fec.createFromFile(absPathToCache, useCheckSum, useRelative ? relDir : undefined);
18-
}
19-
20-
export function normalizePath(filePath: string): string {
21-
if (path.sep === '/') return filePath;
22-
return filePath.split(path.sep).join('/');
6+
export function createFromFile(cacheFileUrl: URL, useCheckSum: boolean, useRelative: boolean): Promise<FileEntryCache> {
7+
return fec.createFromFile(cacheFileUrl, useCheckSum, useRelative ? new URL('./', cacheFileUrl) : undefined);
238
}

0 commit comments

Comments
 (0)