tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

router-rules.js (5051B)


      1 const TEST_CACHE_NAME = 'v1';
      2 // https://w3c.github.io/ServiceWorker/#check-router-registration-limit-algorithm
      3 const CONDITION_MAX_RECURSION_DEPTH = 10;
      4 const CONDITION_MAX_COUNT = 1024;
      5 
      6 const routerRules = {
      7  'condition-urlpattern-constructed-source-network': [{
      8    condition: {urlPattern: new URLPattern({pathname: '/**/direct.txt'})},
      9    source: 'network'
     10  }],
     11  'condition-urlpattern-not-source-network': [{
     12    condition: {not: {urlPattern: new URLPattern({pathname: '/**/not.txt'})}},
     13    source: 'network'
     14  }],
     15  'condition-urlpattern-constructed-match-all-source-cache': [
     16    {condition: {urlPattern: new URLPattern({})}, source: 'cache'},
     17  ],
     18  'condition-urlpattern-urlpatterncompatible-source-network': [
     19    {condition: {urlPattern: {pathname: '/**/direct.txt'}}, source: 'network'},
     20  ],
     21  'condition-urlpattern-string-source-network': [
     22    {condition: {urlPattern: '/**/direct.txt'}, source: 'network'},
     23  ],
     24  'condition-urlpattern-string-source-cache': [
     25    {condition: {urlPattern: '/**/cache.txt'}, source: 'cache'},
     26  ],
     27  'condition-urlpattern-string-source-cache-with-name': [
     28    {condition: {urlPattern: '/**/cache.txt'}, source: {cacheName: TEST_CACHE_NAME}},
     29  ],
     30  'condition-urlpattern-constructed-ignore-case-source-network': [{
     31    condition: {
     32      urlPattern:
     33          new URLPattern({pathname: '/**/DiReCT.TxT'}, {ignoreCase: true})
     34    },
     35    source: 'network'
     36  }],
     37  'condition-urlpattern-constructed-respect-case-source-network': [{
     38    condition: {urlPattern: new URLPattern({pathname: '/**/DiReCT.TxT'})},
     39    source: 'network'
     40  }],
     41  'condition-request-source-network':
     42      [{condition: {requestMode: 'no-cors'}, source: 'network'}],
     43  'condition-request-navigate-source-cache':
     44      [{condition: {requestMode: 'navigate'}, source: 'cache'}],
     45  'condition-request-method-get-network':
     46      [{condition: {requestMethod: 'GET'}, source: 'network'}],
     47  'condition-request-method-post-network':
     48      [{condition: {requestMethod: 'POST'}, source: 'network'}],
     49  'condition-request-method-put-network':
     50      [{condition: {requestMethod: 'PUT'}, source: 'network'}],
     51  'condition-request-method-delete-network':
     52      [{condition: {requestMethod: 'DELETE'}, source: 'network'}],
     53  'condition-lack-of-condition': [{
     54    source: 'network'
     55  }],
     56  'condition-lack-of-source': [{
     57    condition: {requestMode: 'no-cors'},
     58  }],
     59  'condition-invalid-bytestring-request-method': [{
     60    condition: {requestMethod: String.fromCodePoint(0x3042)},
     61    source: 'network'
     62  }],
     63  'condition-invalid-http-request-method': [{
     64    condition: {requestMethod: '(GET|POST)'},
     65    source: 'network'
     66  }],
     67  'condition-forbidden-request-method': [{
     68    condition: {requestMethod: 'connect'},
     69    source: 'network'
     70  }],
     71  'condition-invalid-or-condition-depth': (() => {
     72    const addOrCondition = (depth) => {
     73      if (depth > CONDITION_MAX_RECURSION_DEPTH + 1) {
     74        return {urlPattern: '/foo'};
     75      }
     76      return {
     77        or: [addOrCondition(depth + 1)]
     78      };
     79    };
     80    return {condition: addOrCondition(1), source: 'network'};
     81  })(),
     82  'condition-invalid-not-condition-depth': (() => {
     83    const generateNotCondition = (depth) => {
     84      if (depth > CONDITION_MAX_RECURSION_DEPTH + 1) {
     85        return {
     86          urlPattern: '/**/example.txt',
     87        };
     88      }
     89      return {not: generateNotCondition(depth + 1)};
     90    };
     91    return {condition: generateNotCondition(1), source: 'network'};
     92  })(),
     93  'condition-invalid-router-size': [...Array(CONDITION_MAX_COUNT + 1)].map((val, i) => {
     94    return {
     95      condition: {urlPattern: `/foo-${i}`},
     96      source: 'network'
     97    };
     98  }),
     99  'condition-request-destination-script-network':
    100      [{condition: {requestDestination: 'script'}, source: 'network'}],
    101  'condition-or-source-network': [{
    102    condition: {
    103      or: [
    104        {
    105          or: [{urlPattern: '/**/or-test/direct1.*??*'}],
    106        },
    107        {urlPattern: '/**/or-test/direct2.*??*'}
    108      ]
    109    },
    110    source: 'network'
    111  }],
    112  'condition-request-source-fetch-event':
    113      [{condition: {requestMode: 'no-cors'}, source: 'fetch-event'}],
    114  'condition-urlpattern-string-source-fetch-event':
    115      [{condition: {urlPattern: '/**/*'}, source: 'fetch-event'}],
    116  'multiple-router-rules': [
    117    {
    118      condition: {
    119        urlPattern: '/**/direct.txt',
    120      },
    121      source: 'network'
    122    },
    123    {condition: {urlPattern: '/**/direct.html'}, source: 'network'}
    124  ],
    125  'condition-urlpattern-string-source-race-network-and-fetch-handler': [
    126    {
    127      condition: {urlPattern: '/**/direct.py'},
    128      source: 'race-network-and-fetch-handler'
    129    },
    130  ],
    131  'multiple-conditions-network': {
    132    condition: {
    133      urlPattern: new URLPattern({search: 'test'}),
    134      requestMode: 'cors',
    135      requestMethod: 'post',
    136    },
    137    source: 'network'
    138  },
    139  'multiple-conditions-with-destination-network' : {
    140    condition: {
    141      urlPattern: new URLPattern({search: 'test'}),
    142      requestDestination: 'style'
    143    },
    144    source: 'network'
    145  }
    146 };
    147 
    148 export {routerRules, TEST_CACHE_NAME as cacheName};