11// Import Third-party Dependencies
2+ import { Extractors } from "@nodesecure/scanner/extractors" ;
23import prettyBytes from "pretty-bytes" ;
34import { DataSet } from "vis-data" ;
45
@@ -93,87 +94,97 @@ export default class NodeSecureDataSet extends EventTarget {
9394 return acc ;
9495 } , { names : new Set ( ) , emails : new Set ( ) } ) ;
9596
96- const dataEntries = Object . entries ( data . dependencies ) ;
97- this . dependenciesCount = dataEntries . length ;
97+ const dependencies = Object . entries ( data . dependencies ) ;
98+ this . dependenciesCount = dependencies . length ;
9899
99100 this . rawEdgesData = [ ] ;
100101 this . rawNodesData = [ ] ;
101102
102- const rootDependency = dataEntries . find ( ( [ name ] ) => name === data . rootDependency . name ) ;
103- const rootContributors = [
103+ const rootDependency = dependencies . find ( ( [ name ] ) => name === data . rootDependency . name ) ;
104+ this . rootContributors = [
104105 rootDependency [ 1 ] . metadata . author ,
105106 ...rootDependency [ 1 ] . metadata . maintainers ,
106107 ...rootDependency [ 1 ] . metadata . publishers
107108 ] ;
108- for ( const [ packageName , descriptor ] of dataEntries ) {
109- const contributors = [ descriptor . metadata . author , ...descriptor . metadata . maintainers , ...descriptor . metadata . publishers ] ;
110- for ( const [ currVersion , opt ] of Object . entries ( descriptor . versions ) ) {
111- const { id, usedBy, flags, size, uniqueLicenseIds, author, composition, warnings, links } = opt ;
112- const filteredWarnings = warnings
113- . filter ( ( row ) => ! this . warningsToIgnore . has ( row . kind ) ) ;
114- const hasWarnings = filteredWarnings . length > 0 ;
115-
116- opt . name = packageName ;
117- opt . version = currVersion ;
118- opt . hidden = false ;
119- opt . hasWarnings = hasWarnings ;
120-
121- this . computeExtension ( composition . extensions ) ;
122- this . computeLicense ( uniqueLicenseIds ) ;
123- this . computeAuthor ( author , `${ packageName } @${ currVersion } ` , contributors ) ;
124-
125- if ( flags . includes ( "hasIndirectDependencies" ) ) {
126- this . indirectDependencies ++ ;
127- }
128- this . size += size ;
129-
130- const flagStr = utils . getFlagsEmojisInlined (
131- flags ,
132- hasWarnings ? this . flagsToIgnore : new Set ( [ ...this . flagsToIgnore , "hasWarnings" ] )
133- ) ;
134- const isFriendly = window . settings . config . showFriendlyDependencies & rootContributors . some (
135- ( rootContributor ) => contributors . some ( ( contributor ) => {
136- if ( contributor === null || rootContributor === null ) {
137- return false ;
138- }
139- else if ( contributor . email && contributor . email === rootContributor . email ) {
140- return true ;
141- }
142- else if ( contributor . name && contributor . name === rootContributor . name ) {
143- return true ;
144- }
145109
110+ const extractor = new Extractors . Payload ( data , [
111+ new Extractors . Probes . Licenses ( ) ,
112+ new Extractors . Probes . Extensions ( )
113+ ] ) ;
114+
115+ extractor . on ( "manifest" , ( currVersion , opt , { name, dependency } ) => {
116+ const contributors = [ dependency . metadata . author , ...dependency . metadata . maintainers , ...dependency . metadata . publishers ] ;
117+ const packageName = name ;
118+ const { id, usedBy, flags, size, author, warnings, links } = opt ;
119+ const filteredWarnings = warnings
120+ . filter ( ( row ) => ! this . warningsToIgnore . has ( row . kind ) ) ;
121+ const hasWarnings = filteredWarnings . length > 0 ;
122+
123+ opt . name = packageName ;
124+ opt . version = currVersion ;
125+ opt . hidden = false ;
126+ opt . hasWarnings = hasWarnings ;
127+
128+ this . computeAuthor ( author , `${ packageName } @${ currVersion } ` , contributors ) ;
129+
130+ if ( flags . includes ( "hasIndirectDependencies" ) ) {
131+ this . indirectDependencies ++ ;
132+ }
133+ this . size += size ;
134+
135+ const flagStr = utils . getFlagsEmojisInlined (
136+ flags ,
137+ hasWarnings ? this . flagsToIgnore : new Set ( [ ...this . flagsToIgnore , "hasWarnings" ] )
138+ ) ;
139+ const isFriendly = window . settings . config . showFriendlyDependencies & this . rootContributors . some (
140+ ( rootContributor ) => contributors . some ( ( contributor ) => {
141+ if ( contributor === null || rootContributor === null ) {
146142 return false ;
147- } )
148- ) ;
149- opt . isFriendly = isFriendly ;
150- this . packages . push ( {
151- id,
152- name : packageName ,
153- version : currVersion ,
154- hasWarnings,
155- flags : flagStr . replace ( / \s / g, "" ) ,
156- links,
157- isFriendly
158- } ) ;
159-
160- const label = `<b>${ packageName } @${ currVersion } </b>${ flagStr } \n<b>[${ prettyBytes ( size ) } ]</b>` ;
161- const color = utils . getNodeColor ( {
162- id,
163- hasWarnings,
164- isFriendly,
165- theme : this . theme . toUpperCase ( )
166- } ) ;
167- color . font . multi = "html" ;
168-
169- this . linker . set ( Number ( id ) , opt ) ;
170- this . rawNodesData . push ( Object . assign ( { id, label } , color ) ) ;
171-
172- for ( const [ name , version ] of Object . entries ( usedBy ) ) {
173- this . rawEdgesData . push ( { from : id , to : data . dependencies [ name ] . versions [ version ] . id } ) ;
174- }
143+ }
144+ else if ( contributor . email && contributor . email === rootContributor . email ) {
145+ return true ;
146+ }
147+ else if ( contributor . name && contributor . name === rootContributor . name ) {
148+ return true ;
149+ }
150+
151+ return false ;
152+ } )
153+ ) ;
154+ opt . isFriendly = isFriendly ;
155+
156+ this . packages . push ( {
157+ id,
158+ name : packageName ,
159+ version : currVersion ,
160+ hasWarnings,
161+ flags : flagStr . replace ( / \s / g, "" ) ,
162+ links,
163+ isFriendly
164+ } ) ;
165+
166+ const label = `<b>${ packageName } @${ currVersion } </b>${ flagStr } \n<b>[${ prettyBytes ( size ) } ]</b>` ;
167+ const color = utils . getNodeColor ( {
168+ id,
169+ hasWarnings,
170+ isFriendly,
171+ theme : this . theme . toUpperCase ( )
172+ } ) ;
173+ color . font . multi = "html" ;
174+
175+ this . linker . set ( Number ( id ) , opt ) ;
176+ this . rawNodesData . push ( Object . assign ( { id, label } , color ) ) ;
177+
178+ for ( const [ name , version ] of Object . entries ( usedBy ) ) {
179+ this . rawEdgesData . push ( { from : id , to : this . data . dependencies [ name ] . versions [ version ] . id } ) ;
175180 }
176- }
181+ } ) ;
182+
183+ const { extensions, licenses } = extractor . extractAndMerge ( ) ;
184+
185+ this . extensions = extensions ;
186+ this . licenses = licenses ;
187+
177188 console . log ( "[NodeSecureDataSet] Initialization done!" ) ;
178189 }
179190
@@ -187,20 +198,6 @@ export default class NodeSecureDataSet extends EventTarget {
187198 return null ;
188199 }
189200
190- computeExtension ( extensions ) {
191- for ( const extName of extensions ) {
192- if ( extName !== "" ) {
193- this . extensions [ extName ] = Reflect . has ( this . extensions , extName ) ? ++ this . extensions [ extName ] : 1 ;
194- }
195- }
196- }
197-
198- computeLicense ( uniqueLicenseIds ) {
199- for ( const licenseName of uniqueLicenseIds ) {
200- this . licenses [ licenseName ] = Reflect . has ( this . licenses , licenseName ) ? ++ this . licenses [ licenseName ] : 1 ;
201- }
202- }
203-
204201 computeAuthor ( author , spec , contributors = [ ] ) {
205202 if ( author === null ) {
206203 return ;
0 commit comments