11
11
12
12
'use strict' ;
13
13
14
+ const crypto = require ( 'crypto' ) ;
14
15
const imurmurhash = require ( 'imurmurhash' ) ;
15
16
const invariant = require ( 'invariant' ) ;
16
17
const jsonStableStringify = require ( 'json-stable-stringify' ) ;
17
18
const path = require ( 'path' ) ;
18
19
const request = require ( 'request' ) ;
19
- const toFixedHex = require ( './toFixedHex' ) ;
20
20
21
21
import type { Options as TransformOptions } from '../JSTransformer/worker/worker' ;
22
22
import type { CachedResult } from './TransformCache' ;
@@ -173,6 +173,30 @@ function validateCachedResult(cachedResult: mixed): ?CachedResult {
173
173
return undefined ;
174
174
}
175
175
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
+
176
200
/**
177
201
* One can enable the global cache by calling configure() from a custom CLI
178
202
* script. Eventually we may make it more flexible.
@@ -190,16 +214,13 @@ class GlobalTransformCache {
190
214
* Return a key for identifying uniquely a source file.
191
215
*/
192
216
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 ) } ` ;
203
224
}
204
225
205
226
/**
0 commit comments