1111
1212'use strict' ;
1313
14+ const crypto = require ( 'crypto' ) ;
1415const imurmurhash = require ( 'imurmurhash' ) ;
1516const invariant = require ( 'invariant' ) ;
1617const jsonStableStringify = require ( 'json-stable-stringify' ) ;
1718const path = require ( 'path' ) ;
1819const request = require ( 'request' ) ;
19- const toFixedHex = require ( './toFixedHex' ) ;
2020
2121import type { Options as TransformOptions } from '../JSTransformer/worker/worker' ;
2222import type { CachedResult } from './TransformCache' ;
@@ -173,6 +173,30 @@ function validateCachedResult(cachedResult: mixed): ?CachedResult {
173173 return undefined ;
174174}
175175
176+ /**
177+ * The transform options contain absolute paths. This can contain, for
178+ * example, the username if someone works their home directory (very likely).
179+ * We need to get rid of this user-and-machine-dependent data for the global
180+ * cache, otherwise nobody would share the same cache keys.
181+ */
182+ function globalizeTransformOptions (
183+ options : TransformOptions ,
184+ ) : TransformOptions {
185+ const { transform} = options ;
186+ if ( transform == null ) {
187+ return options ;
188+ }
189+ return {
190+ ...options ,
191+ transform : {
192+ ...transform ,
193+ projectRoots : transform . projectRoots . map ( p => {
194+ return path . relative ( path . join ( __dirname , '../../../../..' ) , p ) ;
195+ } ) ,
196+ } ,
197+ } ;
198+ }
199+
176200/**
177201 * One can enable the global cache by calling configure() from a custom CLI
178202 * script. Eventually we may make it more flexible.
@@ -190,16 +214,13 @@ class GlobalTransformCache {
190214 * Return a key for identifying uniquely a source file.
191215 */
192216 static keyOf ( props : FetchProps ) {
193- const sourceDigest = toFixedHex ( 8 , imurmurhash ( props . sourceCode ) . result ( ) ) ;
194- const optionsHash = imurmurhash ( )
195- . hash ( jsonStableStringify ( props . transformOptions ) || '' )
196- . hash ( props . transformCacheKey )
197- . result ( ) ;
198- const optionsDigest = toFixedHex ( 8 , optionsHash ) ;
199- return (
200- `${ optionsDigest } ${ sourceDigest } ` +
201- `${ path . basename ( props . filePath ) } `
202- ) ;
217+ const stableOptions = globalizeTransformOptions ( props . transformOptions ) ;
218+ const digest = crypto . createHash ( 'sha1' ) . update ( [
219+ jsonStableStringify ( stableOptions ) ,
220+ props . transformCacheKey ,
221+ imurmurhash ( props . sourceCode ) . result ( ) . toString ( ) ,
222+ ] . join ( '$' ) ) . digest ( 'hex' ) ;
223+ return `${ digest } -${ path . basename ( props . filePath ) } ` ;
203224 }
204225
205226 /**
0 commit comments