22
33( function ( exports ) {
44
5- const util = require ( './util' ) ;
5+ const util = require ( './util' ) ,
6+ wasm = require ( './wasm' ) ;
67
78let settings = {
89 enableMixedRulesets : false ,
@@ -220,6 +221,9 @@ RuleSets.prototype = {
220221 loadFromBrowserStorage : async function ( store , applyStoredFunc ) {
221222 this . store = store ;
222223 this . ruleActiveStates = await this . store . get_promise ( 'ruleActiveStates' , { } ) ;
224+ try {
225+ this . wasm_rs = wasm . RuleSets . new ( ) ;
226+ } catch ( e ) { }
223227 await applyStoredFunc ( this ) ;
224228 await this . loadStoredUserRules ( ) ;
225229 await this . addStoredCustomRulesets ( ) ;
@@ -241,11 +245,20 @@ RuleSets.prototype = {
241245
242246 addFromJson : function ( ruleJson , scope ) {
243247 const scope_obj = getScope ( scope ) ;
244- for ( let ruleset of ruleJson ) {
245- try {
246- this . parseOneJsonRuleset ( ruleset , scope_obj ) ;
247- } catch ( e ) {
248- util . log ( util . WARN , 'Error processing ruleset:' + e ) ;
248+
249+ if ( this . wasm_rs ) {
250+ this . wasm_rs . add_all_from_js_array (
251+ ruleJson ,
252+ settings . enableMixedRulesets ,
253+ this . ruleActiveStates ,
254+ scope ) ;
255+ } else {
256+ for ( let ruleset of ruleJson ) {
257+ try {
258+ this . parseOneJsonRuleset ( ruleset , scope_obj ) ;
259+ } catch ( e ) {
260+ util . log ( util . WARN , 'Error processing ruleset:' + e ) ;
261+ }
249262 }
250263 }
251264 } ,
@@ -322,7 +335,15 @@ RuleSets.prototype = {
322335 */
323336 addUserRule : function ( params , scope ) {
324337 util . log ( util . INFO , 'adding new user rule for ' + JSON . stringify ( params ) ) ;
325- this . parseOneJsonRuleset ( params , scope ) ;
338+ if ( this . wasm_rs ) {
339+ this . wasm_rs . add_all_from_js_array (
340+ [ params ] ,
341+ settings . enableMixedRulesets ,
342+ this . ruleActiveStates ,
343+ scope ) ;
344+ } else {
345+ this . parseOneJsonRuleset ( params , scope ) ;
346+ }
326347
327348 // clear cache so new rule take effect immediately
328349 for ( const target of params . target ) {
@@ -351,11 +372,16 @@ RuleSets.prototype = {
351372 this . ruleCache . delete ( ruleset . name ) ;
352373
353374 if ( src === 'popup' ) {
354- const tmp = this . targets . get ( ruleset . name ) . filter ( r => ! r . isEquivalentTo ( ruleset ) )
355- this . targets . set ( ruleset . name , tmp ) ;
356375
357- if ( this . targets . get ( ruleset . name ) . length == 0 ) {
358- this . targets . delete ( ruleset . name ) ;
376+ if ( this . wasm_rs ) {
377+ this . wasm_rs . remove_ruleset ( ruleset ) ;
378+ } else {
379+ const tmp = this . targets . get ( ruleset . name ) . filter ( r => ! r . isEquivalentTo ( ruleset ) )
380+ this . targets . set ( ruleset . name , tmp ) ;
381+
382+ if ( this . targets . get ( ruleset . name ) . length == 0 ) {
383+ this . targets . delete ( ruleset . name ) ;
384+ }
359385 }
360386 }
361387
@@ -546,51 +572,80 @@ RuleSets.prototype = {
546572 util . log ( util . DBUG , "Ruleset cache miss for " + host ) ;
547573 }
548574
549- // Let's begin search
550- // Copy the host targets so we don't modify them.
551- let results = ( this . targets . has ( host ) ?
552- new Set ( [ ...this . targets . get ( host ) ] ) :
553- new Set ( ) ) ;
575+ let results ;
576+ if ( this . wasm_rs ) {
577+ let pa = this . wasm_rs . potentially_applicable ( host ) ;
578+ results = [ ...pa ] . map ( ruleset => {
579+ let rs = new RuleSet ( ruleset . name , ruleset . default_state , getScope ( ruleset . scope ) , ruleset . note ) ;
554580
555- // Ensure host is well-formed (RFC 1035)
556- if ( host . length <= 0 || host . length > 255 || host . indexOf ( ".." ) != - 1 ) {
557- util . log ( util . WARN , "Malformed host passed to potentiallyApplicableRulesets: " + host ) ;
558- return nullIterable ;
559- }
581+ if ( ruleset . cookierules ) {
582+ let cookierules = ruleset . cookierules . map ( cookierule => {
583+ return new CookieRule ( cookierule . host , cookierule . name ) ;
584+ } ) ;
585+ rs . cookierules = cookierules ;
586+ } else {
587+ rs . cookierules = null ;
588+ }
560589
561- // Replace www.example.com with www.example.*
562- // eat away from the right for once and only once
563- let segmented = host . split ( "." ) ;
564- if ( segmented . length > 1 ) {
565- const tmp = segmented [ segmented . length - 1 ] ;
566- segmented [ segmented . length - 1 ] = "*" ;
590+ let rules = ruleset . rules . map ( rule => {
591+ return getRule ( rule . from , rule . to ) ;
592+ } ) ;
593+ rs . rules = rules ;
567594
568- results = ( this . targets . has ( segmented . join ( "." ) ) ?
569- new Set ( [ ...results , ...this . targets . get ( segmented . join ( "." ) ) ] ) :
570- results ) ;
595+ if ( ruleset . exclusions ) {
596+ rs . exclusions = new RegExp ( ruleset . exclusions ) ;
597+ } else {
598+ rs . exclusions = null ;
599+ }
600+ return rs ;
601+ } ) ;
602+ } else {
603+ // Let's begin search
604+ // Copy the host targets so we don't modify them.
605+ results = ( this . targets . has ( host ) ?
606+ new Set ( [ ...this . targets . get ( host ) ] ) :
607+ new Set ( ) ) ;
608+
609+ // Ensure host is well-formed (RFC 1035)
610+ if ( host . length <= 0 || host . length > 255 || host . indexOf ( ".." ) != - 1 ) {
611+ util . log ( util . WARN , "Malformed host passed to potentiallyApplicableRulesets: " + host ) ;
612+ return nullIterable ;
613+ }
571614
572- segmented [ segmented . length - 1 ] = tmp ;
573- }
615+ // Replace www.example.com with www.example.*
616+ // eat away from the right for once and only once
617+ let segmented = host . split ( "." ) ;
618+ if ( segmented . length > 1 ) {
619+ const tmp = segmented [ segmented . length - 1 ] ;
620+ segmented [ segmented . length - 1 ] = "*" ;
574621
575- // now eat away from the left, with *, so that for x.y.z.google.com we
576- // check *.y.z.google.com, *.z.google.com and *.google.com
577- for ( let i = 1 ; i <= segmented . length - 2 ; i ++ ) {
578- let t = "*." + segmented . slice ( i , segmented . length ) . join ( "." ) ;
622+ results = ( this . targets . has ( segmented . join ( "." ) ) ?
623+ new Set ( [ ...results , ...this . targets . get ( segmented . join ( "." ) ) ] ) :
624+ results ) ;
579625
580- results = ( this . targets . has ( t ) ?
581- new Set ( [ ...results , ...this . targets . get ( t ) ] ) :
582- results ) ;
583- }
626+ segmented [ segmented . length - 1 ] = tmp ;
627+ }
584628
585- // Clean the results list, which may contain duplicates or undefined entries
586- results . delete ( undefined ) ;
629+ // now eat away from the left, with *, so that for x.y.z.google.com we
630+ // check *.y.z.google.com, *.z.google.com and *.google.com
631+ for ( let i = 1 ; i <= segmented . length - 2 ; i ++ ) {
632+ let t = "*." + segmented . slice ( i , segmented . length ) . join ( "." ) ;
587633
588- util . log ( util . DBUG , "Applicable rules for " + host + ":" ) ;
589- if ( results . size == 0 ) {
590- util . log ( util . DBUG , " None" ) ;
591- results = nullIterable ;
592- } else {
593- results . forEach ( result => util . log ( util . DBUG , " " + result . name ) ) ;
634+ results = ( this . targets . has ( t ) ?
635+ new Set ( [ ...results , ...this . targets . get ( t ) ] ) :
636+ results ) ;
637+ }
638+
639+ // Clean the results list, which may contain duplicates or undefined entries
640+ results . delete ( undefined ) ;
641+
642+ util . log ( util . DBUG , "Applicable rules for " + host + ":" ) ;
643+ if ( results . size == 0 ) {
644+ util . log ( util . DBUG , " None" ) ;
645+ results = nullIterable ;
646+ } else {
647+ results . forEach ( result => util . log ( util . DBUG , " " + result . name ) ) ;
648+ }
594649 }
595650
596651 // Insert results into the ruleset cache
0 commit comments