tor-browser

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

recording-streams.js (2618B)


      1 'use strict';
      2 
      3 self.recordingReadableStream = (extras = {}, strategy) => {
      4  let controllerToCopyOver;
      5  const stream = new ReadableStream({
      6    type: extras.type,
      7    start(controller) {
      8      controllerToCopyOver = controller;
      9 
     10      if (extras.start) {
     11        return extras.start(controller);
     12      }
     13 
     14      return undefined;
     15    },
     16    pull(controller) {
     17      stream.events.push('pull');
     18 
     19      if (extras.pull) {
     20        return extras.pull(controller);
     21      }
     22 
     23      return undefined;
     24    },
     25    cancel(reason) {
     26      stream.events.push('cancel', reason);
     27      stream.eventsWithoutPulls.push('cancel', reason);
     28 
     29      if (extras.cancel) {
     30        return extras.cancel(reason);
     31      }
     32 
     33      return undefined;
     34    }
     35  }, strategy);
     36 
     37  stream.controller = controllerToCopyOver;
     38  stream.events = [];
     39  stream.eventsWithoutPulls = [];
     40 
     41  return stream;
     42 };
     43 
     44 self.recordingWritableStream = (extras = {}, strategy) => {
     45  let controllerToCopyOver;
     46  const stream = new WritableStream({
     47    start(controller) {
     48      controllerToCopyOver = controller;
     49 
     50      if (extras.start) {
     51        return extras.start(controller);
     52      }
     53 
     54      return undefined;
     55    },
     56    write(chunk, controller) {
     57      stream.events.push('write', chunk);
     58 
     59      if (extras.write) {
     60        return extras.write(chunk, controller);
     61      }
     62 
     63      return undefined;
     64    },
     65    close() {
     66      stream.events.push('close');
     67 
     68      if (extras.close) {
     69        return extras.close();
     70      }
     71 
     72      return undefined;
     73    },
     74    abort(e) {
     75      stream.events.push('abort', e);
     76 
     77      if (extras.abort) {
     78        return extras.abort(e);
     79      }
     80 
     81      return undefined;
     82    }
     83  }, strategy);
     84 
     85  stream.controller = controllerToCopyOver;
     86  stream.events = [];
     87 
     88  return stream;
     89 };
     90 
     91 self.recordingTransformStream = (extras = {}, writableStrategy, readableStrategy) => {
     92  let controllerToCopyOver;
     93  const stream = new TransformStream({
     94    start(controller) {
     95      controllerToCopyOver = controller;
     96 
     97      if (extras.start) {
     98        return extras.start(controller);
     99      }
    100 
    101      return undefined;
    102    },
    103 
    104    transform(chunk, controller) {
    105      stream.events.push('transform', chunk);
    106 
    107      if (extras.transform) {
    108        return extras.transform(chunk, controller);
    109      }
    110 
    111      controller.enqueue(chunk);
    112 
    113      return undefined;
    114    },
    115 
    116    flush(controller) {
    117      stream.events.push('flush');
    118 
    119      if (extras.flush) {
    120        return extras.flush(controller);
    121      }
    122 
    123      return undefined;
    124    }
    125  }, writableStrategy, readableStrategy);
    126 
    127  stream.controller = controllerToCopyOver;
    128  stream.events = [];
    129 
    130  return stream;
    131 };