Last active
          November 23, 2020 22:24 
        
      - 
      
- 
        Save tpmccallum/d6e73226f3c33585001afc9451d62e87 to your computer and use it in GitHub Desktop. 
    A wrapper which can internally call faas and return the result
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function searchBytes(_haystack_array_buffer, _needle_array_buffer, _wasm_id, _function_name) { | |
| return new Promise(function(resolve, reject) { | |
| var needle = new Uint8Array(_needle_array_buffer); | |
| var haystack = new Uint8Array(_haystack_array_buffer); | |
| needle_length = needle.length; | |
| haystack_length = haystack.length; | |
| needle_length_string = needle_length.toString(); | |
| for (i = needle_length_string.length; i < 10; i++) { | |
| needle_length_string = "0" + needle_length_string; | |
| } | |
| if (needle_length < 1 || needle_length > 9999999999 || needle_length > haystack_length) { | |
| alert("The needle's length is not correct, must be between 1 and 9999999999"); | |
| } else { | |
| var base_array = needle_length_string.split(''); | |
| for (i = 0; i < base_array.length; i++) base_array[i] = +base_array[i] | 0; | |
| console.log(base_array); | |
| } | |
| var buffer_to_go = new ArrayBuffer(10 + needle_length + haystack_length); | |
| var array_to_go = new Uint8Array(buffer_to_go); | |
| array_to_go.set(base_array); | |
| array_to_go.set(needle, 10); | |
| array_to_go.set(haystack, 10 + needle_length); | |
| var url = "https://rpc.ssvm.secondstate.io:8081/api/run/" + _wasm_id + "/" + _function_name; | |
| var xhr = new XMLHttpRequest(); | |
| xhr.onreadystatechange = function() { | |
| if (xhr.readyState === 4) { | |
| resolve(xhr.response); | |
| } | |
| } | |
| xhr.open('POST', url, true); | |
| xhr.setRequestHeader("Content-Type", "application/octet-stream"); | |
| xhr.send(buffer_to_go); | |
| }); | |
| } | |
| // Start Skip. Generate some data (but you would already have the ArrayBuffer with data pre-filled) | |
| var buffer_1 = new ArrayBuffer(1000000); | |
| var buffer_2 = new ArrayBuffer(1000000); | |
| var needle = new Uint8Array(buffer_1); | |
| var haystack = new Uint8Array(buffer_2); | |
| needle.fill(111); | |
| haystack.fill(222); | |
| // End Skip. | |
| // | |
| // You would just need to run the following code | |
| searchBytes(buffer_2, buffer_1, 226, "search_bytes_single_input") | |
| .then(function(the_result) { | |
| console.log("Result: " + the_result); | |
| }) | |
| .catch(function() { | |
| console.log("Error"); | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment