1
- import { ModuleImport , ModuleMeta , ModulePart , ModuleLengths , ModuleUID } from "../shared/types" ;
2
- import { getUid } from "./uid" ;
3
-
4
- const nanoid = getUid ( "1234567890abcdef" , 4 ) ;
5
-
6
- const UNIQUE_PREFIX = nanoid ( ) ;
7
- let COUNTER = 0 ;
8
-
9
- const uniqueId = ( ) : ModuleUID => `${ UNIQUE_PREFIX } -${ COUNTER ++ } ` ;
1
+ import {
2
+ ModuleImport ,
3
+ ModuleMeta ,
4
+ ModulePart ,
5
+ ModuleLengths ,
6
+ ModuleUID ,
7
+ VisualizerData ,
8
+ } from "../shared/types" ;
9
+ import * as crypto from "crypto" ;
10
+
11
+ const HASH_PLACEHOLDER = "!{ROLLUP_VISUALIZER_HASH_PLACEHOLDER}" ;
12
+ const HASH_PLACEHOLDER_REGEXP = new RegExp ( `"${ HASH_PLACEHOLDER } -(\\d+)"` , "g" ) ;
10
13
11
14
type ModuleIdStorage = {
12
15
uid : ModuleUID ;
@@ -16,9 +19,23 @@ type ModuleIdStorage = {
16
19
} ;
17
20
} ;
18
21
22
+ export const getDataHash = ( json : string ) => {
23
+ const hash = crypto . createHash ( "sha1" ) . update ( json ) . digest ( "hex" ) ;
24
+ const hashSub = hash . substring ( 0 , 8 ) ;
25
+ return hashSub ;
26
+ } ;
27
+
28
+ export const replaceHashPlaceholders = ( data : VisualizerData ) => {
29
+ const json = JSON . stringify ( data ) ;
30
+ const hash = getDataHash ( json ) ;
31
+ const jsonWithHash = json . replace ( HASH_PLACEHOLDER_REGEXP , ( _ , num ) => `"${ hash } -${ num } "` ) ;
32
+ return jsonWithHash ;
33
+ } ;
34
+
19
35
export class ModuleMapper {
20
36
private nodeParts : Record < ModuleUID , ModulePart > = { } ;
21
37
private nodeMetas : Record < string , ModuleIdStorage > = { } ;
38
+ private counter : number = 0 ;
22
39
23
40
constructor ( private projectRoot : string | RegExp ) { }
24
41
@@ -29,10 +46,14 @@ export class ModuleMapper {
29
46
return moduleId . replace ( this . projectRoot , "" ) ;
30
47
}
31
48
49
+ uniqueId ( ) : ModuleUID {
50
+ return `${ HASH_PLACEHOLDER } -${ this . counter ++ } ` ;
51
+ }
52
+
32
53
getModuleUid ( moduleId : string ) : ModuleUID {
33
54
if ( ! ( moduleId in this . nodeMetas ) ) {
34
55
this . nodeMetas [ moduleId ] = {
35
- uid : uniqueId ( ) ,
56
+ uid : this . uniqueId ( ) ,
36
57
meta : {
37
58
id : this . trimProjectRootId ( moduleId ) ,
38
59
moduleParts : { } ,
@@ -48,7 +69,7 @@ export class ModuleMapper {
48
69
getBundleModuleUid ( bundleId : string , moduleId : string ) : ModuleUID {
49
70
if ( ! ( moduleId in this . nodeMetas ) ) {
50
71
this . nodeMetas [ moduleId ] = {
51
- uid : uniqueId ( ) ,
72
+ uid : this . uniqueId ( ) ,
52
73
meta : {
53
74
id : this . trimProjectRootId ( moduleId ) ,
54
75
moduleParts : { } ,
@@ -58,7 +79,7 @@ export class ModuleMapper {
58
79
} ;
59
80
}
60
81
if ( ! ( bundleId in this . nodeMetas [ moduleId ] . meta . moduleParts ) ) {
61
- this . nodeMetas [ moduleId ] . meta . moduleParts [ bundleId ] = uniqueId ( ) ;
82
+ this . nodeMetas [ moduleId ] . meta . moduleParts [ bundleId ] = this . uniqueId ( ) ;
62
83
}
63
84
64
85
return this . nodeMetas [ moduleId ] . meta . moduleParts [ bundleId ] ;
0 commit comments