blake3_js.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. let wasm;
  2. let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
  3. cachedTextDecoder.decode();
  4. let cachegetUint8Memory0 = null;
  5. function getUint8Memory0() {
  6. if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
  7. cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
  8. }
  9. return cachegetUint8Memory0;
  10. }
  11. function getStringFromWasm0(ptr, len) {
  12. return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
  13. }
  14. let WASM_VECTOR_LEN = 0;
  15. function passArray8ToWasm0(arg, malloc) {
  16. const ptr = malloc(arg.length * 1);
  17. getUint8Memory0().set(arg, ptr / 1);
  18. WASM_VECTOR_LEN = arg.length;
  19. return ptr;
  20. }
  21. /**
  22. * @param {Uint8Array} data
  23. * @param {Uint8Array} out
  24. */
  25. export function hash(data, out) {
  26. try {
  27. var ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
  28. var len0 = WASM_VECTOR_LEN;
  29. var ptr1 = passArray8ToWasm0(out, wasm.__wbindgen_malloc);
  30. var len1 = WASM_VECTOR_LEN;
  31. wasm.hash(ptr0, len0, ptr1, len1);
  32. } finally {
  33. out.set(getUint8Memory0().subarray(ptr1 / 1, ptr1 / 1 + len1));
  34. wasm.__wbindgen_free(ptr1, len1 * 1);
  35. }
  36. }
  37. /**
  38. * @returns {Blake3Hash}
  39. */
  40. export function create_hasher() {
  41. var ret = wasm.create_hasher();
  42. return Blake3Hash.__wrap(ret);
  43. }
  44. /**
  45. * @param {Uint8Array} key_slice
  46. * @returns {Blake3Hash}
  47. */
  48. export function create_keyed(key_slice) {
  49. var ptr0 = passArray8ToWasm0(key_slice, wasm.__wbindgen_malloc);
  50. var len0 = WASM_VECTOR_LEN;
  51. var ret = wasm.create_keyed(ptr0, len0);
  52. return Blake3Hash.__wrap(ret);
  53. }
  54. let cachedTextEncoder = new TextEncoder('utf-8');
  55. const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
  56. ? function (arg, view) {
  57. return cachedTextEncoder.encodeInto(arg, view);
  58. }
  59. : function (arg, view) {
  60. const buf = cachedTextEncoder.encode(arg);
  61. view.set(buf);
  62. return {
  63. read: arg.length,
  64. written: buf.length
  65. };
  66. });
  67. function passStringToWasm0(arg, malloc, realloc) {
  68. if (realloc === undefined) {
  69. const buf = cachedTextEncoder.encode(arg);
  70. const ptr = malloc(buf.length);
  71. getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
  72. WASM_VECTOR_LEN = buf.length;
  73. return ptr;
  74. }
  75. let len = arg.length;
  76. let ptr = malloc(len);
  77. const mem = getUint8Memory0();
  78. let offset = 0;
  79. for (; offset < len; offset++) {
  80. const code = arg.charCodeAt(offset);
  81. if (code > 0x7F) break;
  82. mem[ptr + offset] = code;
  83. }
  84. if (offset !== len) {
  85. if (offset !== 0) {
  86. arg = arg.slice(offset);
  87. }
  88. ptr = realloc(ptr, len, len = offset + arg.length * 3);
  89. const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
  90. const ret = encodeString(arg, view);
  91. offset += ret.written;
  92. }
  93. WASM_VECTOR_LEN = offset;
  94. return ptr;
  95. }
  96. /**
  97. * @param {string} context
  98. * @returns {Blake3Hash}
  99. */
  100. export function create_derive(context) {
  101. var ptr0 = passStringToWasm0(context, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
  102. var len0 = WASM_VECTOR_LEN;
  103. var ret = wasm.create_derive(ptr0, len0);
  104. return Blake3Hash.__wrap(ret);
  105. }
  106. const u32CvtShim = new Uint32Array(2);
  107. const uint64CvtShim = new BigUint64Array(u32CvtShim.buffer);
  108. /**
  109. */
  110. export class Blake3Hash {
  111. static __wrap(ptr) {
  112. const obj = Object.create(Blake3Hash.prototype);
  113. obj.ptr = ptr;
  114. return obj;
  115. }
  116. free() {
  117. const ptr = this.ptr;
  118. this.ptr = 0;
  119. wasm.__wbg_blake3hash_free(ptr);
  120. }
  121. /**
  122. * @returns {HashReader}
  123. */
  124. reader() {
  125. var ret = wasm.blake3hash_reader(this.ptr);
  126. return HashReader.__wrap(ret);
  127. }
  128. /**
  129. * @param {Uint8Array} input_bytes
  130. */
  131. update(input_bytes) {
  132. var ptr0 = passArray8ToWasm0(input_bytes, wasm.__wbindgen_malloc);
  133. var len0 = WASM_VECTOR_LEN;
  134. wasm.blake3hash_update(this.ptr, ptr0, len0);
  135. }
  136. /**
  137. * @param {Uint8Array} out
  138. */
  139. digest(out) {
  140. try {
  141. var ptr0 = passArray8ToWasm0(out, wasm.__wbindgen_malloc);
  142. var len0 = WASM_VECTOR_LEN;
  143. wasm.blake3hash_digest(this.ptr, ptr0, len0);
  144. } finally {
  145. out.set(getUint8Memory0().subarray(ptr0 / 1, ptr0 / 1 + len0));
  146. wasm.__wbindgen_free(ptr0, len0 * 1);
  147. }
  148. }
  149. }
  150. /**
  151. */
  152. export class HashReader {
  153. static __wrap(ptr) {
  154. const obj = Object.create(HashReader.prototype);
  155. obj.ptr = ptr;
  156. return obj;
  157. }
  158. free() {
  159. const ptr = this.ptr;
  160. this.ptr = 0;
  161. wasm.__wbg_hashreader_free(ptr);
  162. }
  163. /**
  164. * @param {Uint8Array} bytes
  165. */
  166. fill(bytes) {
  167. try {
  168. var ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc);
  169. var len0 = WASM_VECTOR_LEN;
  170. wasm.hashreader_fill(this.ptr, ptr0, len0);
  171. } finally {
  172. bytes.set(getUint8Memory0().subarray(ptr0 / 1, ptr0 / 1 + len0));
  173. wasm.__wbindgen_free(ptr0, len0 * 1);
  174. }
  175. }
  176. /**
  177. * @param {BigInt} position
  178. */
  179. set_position(position) {
  180. uint64CvtShim[0] = position;
  181. const low0 = u32CvtShim[0];
  182. const high0 = u32CvtShim[1];
  183. wasm.hashreader_set_position(this.ptr, low0, high0);
  184. }
  185. }
  186. async function load(module, imports) {
  187. if (typeof Response === 'function' && module instanceof Response) {
  188. if (typeof WebAssembly.instantiateStreaming === 'function') {
  189. try {
  190. return await WebAssembly.instantiateStreaming(module, imports);
  191. } catch (e) {
  192. if (module.headers.get('Content-Type') != 'application/wasm') {
  193. console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
  194. } else {
  195. throw e;
  196. }
  197. }
  198. }
  199. const bytes = await module.arrayBuffer();
  200. return await WebAssembly.instantiate(bytes, imports);
  201. } else {
  202. const instance = await WebAssembly.instantiate(module, imports);
  203. if (instance instanceof WebAssembly.Instance) {
  204. return { instance, module };
  205. } else {
  206. return instance;
  207. }
  208. }
  209. }
  210. async function init(input) {
  211. if (typeof input === 'undefined') {
  212. input = import.meta.url.replace(/\.js$/, '_bg.wasm');
  213. }
  214. const imports = {};
  215. imports.wbg = {};
  216. imports.wbg.__wbindgen_throw = function(arg0, arg1) {
  217. throw new Error(getStringFromWasm0(arg0, arg1));
  218. };
  219. if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
  220. input = fetch(input);
  221. }
  222. const { instance, module } = await load(await input, imports);
  223. wasm = instance.exports;
  224. init.__wbindgen_wasm_module = module;
  225. return wasm;
  226. }
  227. export default init;