11// Derived from @prettier /cli, see THIRD_PARTY_NOTICES.md
22
3+ import { hash } from 'node:crypto' ;
34import { readFileSync , writeFileSync } from 'node:fs' ;
4- import { createCacheHash } from './cacheHash.ts' ;
55import type { FmtCacheEntry } from './cacheStore.ts' ;
66import { hasDottedBasename } from './pathHelpers.ts' ;
77import type { FmtFileCache , FmtFileRequest , FmtWorkerResult } from './types.ts' ;
@@ -12,6 +12,9 @@ interface FormatFileTask {
1212 cache ?: FmtFileCache ;
1313}
1414
15+ const hashContent = ( content : string | Uint8Array ) : string =>
16+ hash ( 'sha256' , content , 'base64url' ) . slice ( 0 , 16 ) ;
17+
1518/**
1619 * Use synchronous direct I/O inside the dedicated worker to avoid libuv
1720 * scheduling overhead. This prioritizes throughput over crash-safe replacement.
@@ -35,7 +38,7 @@ const formatFile = async ({
3538 }
3639
3740 sourceBuffer = readFileSync ( file . path ) ;
38- contentHash = createCacheHash ( sourceBuffer ) ;
41+ contentHash = hashContent ( sourceBuffer ) ;
3942 return sourceBuffer . toString ( 'utf8' ) ;
4043 } ;
4144
@@ -48,14 +51,14 @@ const formatFile = async ({
4851 }
4952 } else {
5053 sourceBuffer = readFileSync ( file . path ) ;
51- contentHash = createCacheHash ( sourceBuffer ) ;
54+ contentHash = hashContent ( sourceBuffer ) ;
5255 if ( entry [ 0 ] === contentHash ) {
5356 return { status : 'unsupported' } ;
5457 }
5558 }
5659 } else {
5760 sourceBuffer = readFileSync ( file . path ) ;
58- contentHash = createCacheHash ( sourceBuffer ) ;
61+ contentHash = hashContent ( sourceBuffer ) ;
5962 if ( entry [ 0 ] === contentHash && ( ! shouldWrite || entry [ 2 ] === 'clean' ) ) {
6063 return { status : entry [ 2 ] === 'clean' ? 'unchanged' : 'changed' } ;
6164 }
@@ -71,7 +74,7 @@ const formatFile = async ({
7174 cacheEntry : [
7275 hasDottedBasename ( file . path )
7376 ? ''
74- : ( contentHash ?? createCacheHash ( sourceBuffer ?? readFileSync ( file . path ) ) ) ,
77+ : ( contentHash ?? hashContent ( sourceBuffer ?? readFileSync ( file . path ) ) ) ,
7578 cache . optionsHash ,
7679 'unsupported' ,
7780 ] ,
@@ -92,8 +95,8 @@ const formatFile = async ({
9295
9396 const cacheHash =
9497 shouldWrite && ! unchanged
95- ? createCacheHash ( result . formatted )
96- : ( contentHash ?? createCacheHash ( result . source ) ) ;
98+ ? hashContent ( result . formatted )
99+ : ( contentHash ?? hashContent ( result . source ) ) ;
97100 const cacheEntry : FmtCacheEntry = [
98101 cacheHash ,
99102 cache . optionsHash ,
0 commit comments