22 * License, v. 2.0. If a copy of the MPL was not distributed with this
33 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
5- // --- helpers --------------------------------------------------------------
6-
75const escapeRegExp = ( str ) => str . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ;
86
97const MATCHER_FACTORIES = {
@@ -13,33 +11,27 @@ const MATCHER_FACTORIES = {
1311 endsWith : ( v ) => new RegExp ( `${ escapeRegExp ( v ) } $` , 'i' ) ,
1412 regex : ( v ) => new RegExp ( v , 'i' ) ,
1513} ;
16-
1714const VALID_MATCH_TYPES = Object . keys ( MATCHER_FACTORIES ) ;
1815
19- // Environment helpers (Firefox chrome JS) ----------------------------------
2016const PREF_KEY = 'zen.airTrafficControl.rules' ;
2117const readPref = ( ) => Services . prefs . getStringPref ( PREF_KEY , '[]' ) ;
2218const writePref = ( json ) => Services . prefs . setStringPref ( PREF_KEY , json ) ;
2319
24- // --- class ----------------------------------------------------------------
2520class ZenAirTrafficControlImpl {
2621 _rules = [ ] ;
2722 _initialized = false ;
2823
29- /* --------------------------------------------------------------------- */
3024 init ( ) {
3125 if ( this . _initialized ) return ;
32- this . _loadRules ( ) ;
26+ this . loadRules ( ) ;
3327 this . _initialized = true ;
3428 }
3529
36- // Convenience – auto-initialise on first public call.
3730 _ensureInit ( ) {
3831 if ( ! this . _initialized ) this . init ( ) ;
3932 }
4033
41- /* --------------------------------------------------------------------- */
42- _loadRules ( ) {
34+ loadRules ( ) {
4335 try {
4436 this . _rules = JSON . parse ( readPref ( ) ) ;
4537 this . _sortRules ( ) ;
@@ -49,7 +41,7 @@ class ZenAirTrafficControlImpl {
4941 }
5042 }
5143
52- _saveRules ( ) {
44+ saveRules ( ) {
5345 try {
5446 writePref ( JSON . stringify ( this . _rules ) ) ;
5547 } catch ( e ) {
@@ -61,7 +53,6 @@ class ZenAirTrafficControlImpl {
6153 this . _rules . sort ( ( a , b ) => a . createdAt - b . createdAt ) ;
6254 }
6355
64- /* --------------------------------------------------------------------- */
6556 _validateRuleShape ( { matchType, matchValue, workspaceId } ) {
6657 if ( ! VALID_MATCH_TYPES . includes ( matchType ) ) {
6758 throw new Error ( `ZenATC: invalid matchType '${ matchType } '` ) ;
@@ -74,7 +65,6 @@ class ZenAirTrafficControlImpl {
7465 }
7566 }
7667
77- /* --------------------------------------------------------------------- */
7868 createRule ( rule ) {
7969 this . _ensureInit ( ) ;
8070 this . _validateRuleShape ( rule ) ;
@@ -88,7 +78,7 @@ class ZenAirTrafficControlImpl {
8878
8979 this . _rules . push ( newRule ) ;
9080 this . _sortRules ( ) ;
91- this . _saveRules ( ) ;
81+ this . saveRules ( ) ;
9282 return newRule ;
9383 }
9484
@@ -101,22 +91,20 @@ class ZenAirTrafficControlImpl {
10191 this . _validateRuleShape ( updated ) ;
10292
10393 this . _rules [ idx ] = updated ;
104- this . _saveRules ( ) ;
94+ this . saveRules ( ) ;
10595 }
10696
10797 deleteRule ( uuid ) {
10898 this . _ensureInit ( ) ;
10999 this . _rules = this . _rules . filter ( ( r ) => r . uuid !== uuid ) ;
110- this . _saveRules ( ) ;
100+ this . saveRules ( ) ;
111101 }
112102
113- /* --------------------------------------------------------------------- */
114103 getRules ( ) {
115104 this . _ensureInit ( ) ;
116105 return [ ...this . _rules ] ;
117106 }
118107
119- /* --------------------------------------------------------------------- */
120108 _createMatcher ( rule ) {
121109 const factory = MATCHER_FACTORIES [ rule . matchType ] ;
122110 return factory ( rule . matchValue ) ;
@@ -127,26 +115,13 @@ class ZenAirTrafficControlImpl {
127115 return this . _rules . find ( ( r ) => r . enabled && this . _createMatcher ( r ) . test ( url ) ) || null ;
128116 }
129117
130- /* --------------------------------------------------------------------- */
131118 routeURL ( url , _context = { } ) {
132119 this . _ensureInit ( ) ;
133120 const rule = this . _findFirstMatchingRule ( url ) ;
134121 if ( ! rule ) return null ;
135122 return { rule, workspaceId : rule . workspaceId } ;
136123 }
137124
138- /* --------------------------------------------------------------------- */
139- // Compatibility wrappers ------------------------------------------------
140- /** Reload rules from storage (primarily used by tests/UI). */
141- loadRules ( ) {
142- this . _loadRules ( ) ;
143- }
144-
145- /** Persist current rule list to storage (primarily used by tests/UI). */
146- saveRules ( ) {
147- this . _saveRules ( ) ;
148- }
149-
150125 /** Return *all* enabled rules that match the given URL. */
151126 findMatchingRules ( url ) {
152127 this . _ensureInit ( ) ;
@@ -161,5 +136,4 @@ class ZenAirTrafficControlImpl {
161136 }
162137}
163138
164- // Single exported instance (backward-compatible with previous object export)
165139export const ZenAirTrafficControl = new ZenAirTrafficControlImpl ( ) ;
0 commit comments