SHARE
TWEET

Untitled

tinyevil Dec 19th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function (exports) {
  2.     var streams = [];
  3.     var free_ids = [];
  4.     var next_id = 0;
  5.  
  6.     function URLStream(ud, idx) {
  7.         console.log("new URLStream created with idx=" + idx + " and ud=" + ud);
  8.         this.idx = idx;
  9.         this.ud = ud;
  10.         this.request = new XMLHttpRequest();
  11.         this.url = null;
  12.         this.request.responseType = 'arraybuffer';
  13.         this.method = "GET";
  14.         this.data = null;
  15.         this.buffer_ptr = 0;
  16.         this.buffer_len = 0;
  17.     }
  18.  
  19.     exports._urlstream_set_buffer = function (idx, ptr, len) {
  20.         var stream = streams[idx];
  21.         if (!stream) {
  22.             throw new Error("invalid stream index");
  23.         }
  24.         stream.buffer_ptr = ptr;
  25.         stream.buffer_len = len;
  26.     };
  27.  
  28.     exports._urlstream_set_url = function (idx, url) {
  29.         var stream = streams[idx];
  30.         if (!stream) {
  31.             throw new Error("invalid stream index");
  32.         }
  33.         stream.url = UTF8ToString(url);
  34.     };
  35.  
  36.     exports._urlstream_set_data = function (idx, ptr, len) {
  37.         var stream = streams[idx];
  38.         if (!stream) {
  39.             throw new Error("invalid stream index");
  40.         }
  41.         stream.data = new Blob([new Uint8Array(HEAPU8.subarray(ptr, ptr + len))]);
  42.     };
  43.  
  44.     exports._urlstream_destroy = function (idx) {
  45.         var stream = streams[idx];
  46.         if (!stream) {
  47.             throw new Error("invalid stream index");
  48.         }
  49.         stream.ud = 0;
  50.         streams[idx] = null;
  51.         free_ids.push(idx);
  52.     };
  53.  
  54.     exports._urlstream_load = function (idx) {
  55.         var stream = streams[idx];
  56.         if (!stream) {
  57.             throw new Error("invalid stream index");
  58.         }
  59.  
  60.         function report_fail(code, message) {
  61.             if (stream.ud) {
  62.                 var ptr = allocate(intArrayFromString(message), 'i8', ALLOC_STACK);
  63.                 _urlstream_on_error(stream.idx, stream.ud, code, ptr);
  64.             }
  65.         }
  66.  
  67.         function on_loadstart(event) {
  68.             if (stream.ud) {
  69.                 _urlstream_on_open(stream.idx, stream.ud);
  70.             }
  71.         }
  72.  
  73.         function on_complete(event) {
  74.             if (stream.ud) {
  75.                 var response = stream.request.response;
  76.                 if (response) {
  77.                     var byteArray = new Uint8Array(response);
  78.                     var size = byteArray.length;
  79.                     var buffer_ptr = stream.buffer_ptr;
  80.                     var buffer_len = stream.buffer_len;
  81.  
  82.                     for (var offset = 0; offset < size; offset += buffer_len) {
  83.                         var to_transfer = Math.min(size - offset, buffer_len);
  84.                         HEAPU8.subarray(buffer_ptr, buffer_ptr + to_transfer).set(byteArray.subarray(offset, offset + to_transfer));
  85.                         _urlstream_on_data(stream.idx, stream.ud, buffer_ptr, to_transfer);
  86.                     }
  87.                 }
  88.                 _urlstream_on_complete(stream.idx, stream.ud);
  89.             }
  90.         }
  91.  
  92.         function on_error(event) {
  93.             if (event.error) {
  94.                 report_fail(event.error.code, event.error.name + ": " + event.error.message);
  95.             } else {
  96.                 report_fail(1000, "stream error");
  97.             }
  98.         }
  99.  
  100.         function on_abort(event) {
  101.             report_fail(300, "aborted");
  102.         }
  103.  
  104.         function on_progress(event) {
  105.             if (stream.ud) {
  106.                 if (event.lengthComputable) {
  107.                     _urlstream_on_progress(stream.idx, stream.ud, event.loaded, event.total);
  108.                 } else if ( 'loaded' in event ) {
  109.                     _urlstream_on_progress(stream.idx, stream.ud, event.loaded, 0);
  110.                 }
  111.             }
  112.         }
  113.  
  114.         stream.request.addEventListener("loadstart", on_loadstart);
  115.         stream.request.addEventListener("progress", on_progress);
  116.         stream.request.addEventListener("error", on_error);
  117.         stream.request.addEventListener("abort", on_abort);
  118.         stream.request.addEventListener("load", on_complete);
  119.  
  120.         stream.request.open(stream.method, stream.url);
  121.         stream.request.send(stream.data);
  122.     };
  123.  
  124.     exports._urlstream_set_method = function (idx, method) {
  125.         var stream = streams[idx];
  126.         if (!stream) {
  127.             throw new Error("invalid stream index");
  128.         }
  129.         switch (method) {
  130.         case 1: {
  131.             stream.method = "GET";
  132.         } break;
  133.         case 2: {
  134.             stream.method = "POST";
  135.         } break;
  136.         default: {
  137.             throw new Error("invalid URLStream method id");
  138.         } break;
  139.         }
  140.     };
  141.  
  142.     exports._urlstream_create = function (ud) {
  143.         var idx = 0;
  144.         if (free_ids.length === 0) {
  145.             idx = next_id;
  146.             next_id++;
  147.         } else {
  148.             idx = free_ids.pop();
  149.         }
  150.         var stream = new URLStream(ud, idx);
  151.         streams[idx] = stream;
  152.         return idx;
  153.     };
  154. })(this);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top