blake3_js.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. let imports = {};
  2. imports['__wbindgen_placeholder__'] = module.exports;
  3. let wasm;
  4. const { TextDecoder } = require(String.raw`util`);
  5. let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
  6. cachedTextDecoder.decode();
  7. let cachegetUint8Memory0 = null;
  8. function getUint8Memory0() {
  9. if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
  10. cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
  11. }
  12. return cachegetUint8Memory0;
  13. }
  14. function getStringFromWasm0(ptr, len) {
  15. return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
  16. }
  17. let WASM_VECTOR_LEN = 0;
  18. function passArray8ToWasm0(arg, malloc) {
  19. const ptr = malloc(arg.length * 1);
  20. getUint8Memory0().set(arg, ptr / 1);
  21. WASM_VECTOR_LEN = arg.length;
  22. return ptr;
  23. }
  24. /**
  25. * @param {Uint8Array} data
  26. * @param {Uint8Array} out
  27. */
  28. module.exports.hash = function(data, out) {
  29. try {
  30. var ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
  31. var len0 = WASM_VECTOR_LEN;
  32. var ptr1 = passArray8ToWasm0(out, wasm.__wbindgen_malloc);
  33. var len1 = WASM_VECTOR_LEN;
  34. wasm.hash(ptr0, len0, ptr1, len1);
  35. } finally {
  36. out.set(getUint8Memory0().subarray(ptr1 / 1, ptr1 / 1 + len1));
  37. wasm.__wbindgen_free(ptr1, len1 * 1);
  38. }
  39. };
  40. /**
  41. * @returns {Blake3Hash}
  42. */
  43. module.exports.create_hasher = function() {
  44. var ret = wasm.create_hasher();
  45. return Blake3Hash.__wrap(ret);
  46. };
  47. /**
  48. * @param {Uint8Array} key_slice
  49. * @returns {Blake3Hash}
  50. */
  51. module.exports.create_keyed = function(key_slice) {
  52. var ptr0 = passArray8ToWasm0(key_slice, wasm.__wbindgen_malloc);
  53. var len0 = WASM_VECTOR_LEN;
  54. var ret = wasm.create_keyed(ptr0, len0);
  55. return Blake3Hash.__wrap(ret);
  56. };
  57. let cachegetNodeBufferMemory0 = null;
  58. function getNodeBufferMemory0() {
  59. if (cachegetNodeBufferMemory0 === null || cachegetNodeBufferMemory0.buffer !== wasm.memory.buffer) {
  60. cachegetNodeBufferMemory0 = Buffer.from(wasm.memory.buffer);
  61. }
  62. return cachegetNodeBufferMemory0;
  63. }
  64. function passStringToWasm0(arg, malloc) {
  65. const len = Buffer.byteLength(arg);
  66. const ptr = malloc(len);
  67. getNodeBufferMemory0().write(arg, ptr, len);
  68. WASM_VECTOR_LEN = len;
  69. return ptr;
  70. }
  71. /**
  72. * @param {string} context
  73. * @returns {Blake3Hash}
  74. */
  75. module.exports.create_derive = function(context) {
  76. var ptr0 = passStringToWasm0(context, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
  77. var len0 = WASM_VECTOR_LEN;
  78. var ret = wasm.create_derive(ptr0, len0);
  79. return Blake3Hash.__wrap(ret);
  80. };
  81. const u32CvtShim = new Uint32Array(2);
  82. const uint64CvtShim = new BigUint64Array(u32CvtShim.buffer);
  83. /**
  84. */
  85. class Blake3Hash {
  86. static __wrap(ptr) {
  87. const obj = Object.create(Blake3Hash.prototype);
  88. obj.ptr = ptr;
  89. return obj;
  90. }
  91. free() {
  92. const ptr = this.ptr;
  93. this.ptr = 0;
  94. wasm.__wbg_blake3hash_free(ptr);
  95. }
  96. /**
  97. * @returns {HashReader}
  98. */
  99. reader() {
  100. var ret = wasm.blake3hash_reader(this.ptr);
  101. return HashReader.__wrap(ret);
  102. }
  103. /**
  104. * @param {Uint8Array} input_bytes
  105. */
  106. update(input_bytes) {
  107. var ptr0 = passArray8ToWasm0(input_bytes, wasm.__wbindgen_malloc);
  108. var len0 = WASM_VECTOR_LEN;
  109. wasm.blake3hash_update(this.ptr, ptr0, len0);
  110. }
  111. /**
  112. * @param {Uint8Array} out
  113. */
  114. digest(out) {
  115. try {
  116. var ptr0 = passArray8ToWasm0(out, wasm.__wbindgen_malloc);
  117. var len0 = WASM_VECTOR_LEN;
  118. wasm.blake3hash_digest(this.ptr, ptr0, len0);
  119. } finally {
  120. out.set(getUint8Memory0().subarray(ptr0 / 1, ptr0 / 1 + len0));
  121. wasm.__wbindgen_free(ptr0, len0 * 1);
  122. }
  123. }
  124. }
  125. module.exports.Blake3Hash = Blake3Hash;
  126. /**
  127. */
  128. class HashReader {
  129. static __wrap(ptr) {
  130. const obj = Object.create(HashReader.prototype);
  131. obj.ptr = ptr;
  132. return obj;
  133. }
  134. free() {
  135. const ptr = this.ptr;
  136. this.ptr = 0;
  137. wasm.__wbg_hashreader_free(ptr);
  138. }
  139. /**
  140. * @param {Uint8Array} bytes
  141. */
  142. fill(bytes) {
  143. try {
  144. var ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc);
  145. var len0 = WASM_VECTOR_LEN;
  146. wasm.hashreader_fill(this.ptr, ptr0, len0);
  147. } finally {
  148. bytes.set(getUint8Memory0().subarray(ptr0 / 1, ptr0 / 1 + len0));
  149. wasm.__wbindgen_free(ptr0, len0 * 1);
  150. }
  151. }
  152. /**
  153. * @param {BigInt} position
  154. */
  155. set_position(position) {
  156. uint64CvtShim[0] = position;
  157. const low0 = u32CvtShim[0];
  158. const high0 = u32CvtShim[1];
  159. wasm.hashreader_set_position(this.ptr, low0, high0);
  160. }
  161. }
  162. module.exports.HashReader = HashReader;
  163. module.exports.__wbindgen_throw = function(arg0, arg1) {
  164. throw new Error(getStringFromWasm0(arg0, arg1));
  165. };
  166. const path = require('path').join(__dirname, 'blake3_js_bg.wasm');
  167. const bytes = require('fs').readFileSync(path);
  168. const wasmModule = new WebAssembly.Module(bytes);
  169. const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
  170. wasm = wasmInstance.exports;
  171. module.exports.__wasm = wasm;