wasm-hash.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. 'use strict';
  6. // 65536 is the size of a wasm memory page
  7. // 64 is the maximum chunk size for every possible wasm hash implementation
  8. // 4 is the maximum number of bytes per char for string encoding (max is utf-8)
  9. // ~3 makes sure that it's always a block of 4 chars, so avoid partially encoded bytes for base64
  10. const MAX_SHORT_STRING = Math.floor((65536 - 64) / 4) & ~3;
  11. class WasmHash {
  12. /**
  13. * @param {WebAssembly.Instance} instance wasm instance
  14. * @param {WebAssembly.Instance[]} instancesPool pool of instances
  15. * @param {number} chunkSize size of data chunks passed to wasm
  16. * @param {number} digestSize size of digest returned by wasm
  17. */
  18. constructor(instance, instancesPool, chunkSize, digestSize) {
  19. const exports = /** @type {any} */ (instance.exports);
  20. exports.init();
  21. this.exports = exports;
  22. this.mem = Buffer.from(exports.memory.buffer, 0, 65536);
  23. this.buffered = 0;
  24. this.instancesPool = instancesPool;
  25. this.chunkSize = chunkSize;
  26. this.digestSize = digestSize;
  27. }
  28. reset() {
  29. this.buffered = 0;
  30. this.exports.init();
  31. }
  32. /**
  33. * @param {Buffer | string} data data
  34. * @param {BufferEncoding=} encoding encoding
  35. * @returns {this} itself
  36. */
  37. update(data, encoding) {
  38. if (typeof data === 'string') {
  39. while (data.length > MAX_SHORT_STRING) {
  40. this._updateWithShortString(data.slice(0, MAX_SHORT_STRING), encoding);
  41. data = data.slice(MAX_SHORT_STRING);
  42. }
  43. this._updateWithShortString(data, encoding);
  44. return this;
  45. }
  46. this._updateWithBuffer(data);
  47. return this;
  48. }
  49. /**
  50. * @param {string} data data
  51. * @param {BufferEncoding=} encoding encoding
  52. * @returns {void}
  53. */
  54. _updateWithShortString(data, encoding) {
  55. const { exports, buffered, mem, chunkSize } = this;
  56. let endPos;
  57. if (data.length < 70) {
  58. if (!encoding || encoding === 'utf-8' || encoding === 'utf8') {
  59. endPos = buffered;
  60. for (let i = 0; i < data.length; i++) {
  61. const cc = data.charCodeAt(i);
  62. if (cc < 0x80) {
  63. mem[endPos++] = cc;
  64. } else if (cc < 0x800) {
  65. mem[endPos] = (cc >> 6) | 0xc0;
  66. mem[endPos + 1] = (cc & 0x3f) | 0x80;
  67. endPos += 2;
  68. } else {
  69. // bail-out for weird chars
  70. endPos += mem.write(data.slice(i), endPos, encoding);
  71. break;
  72. }
  73. }
  74. } else if (encoding === 'latin1') {
  75. endPos = buffered;
  76. for (let i = 0; i < data.length; i++) {
  77. const cc = data.charCodeAt(i);
  78. mem[endPos++] = cc;
  79. }
  80. } else {
  81. endPos = buffered + mem.write(data, buffered, encoding);
  82. }
  83. } else {
  84. endPos = buffered + mem.write(data, buffered, encoding);
  85. }
  86. if (endPos < chunkSize) {
  87. this.buffered = endPos;
  88. } else {
  89. const l = endPos & ~(this.chunkSize - 1);
  90. exports.update(l);
  91. const newBuffered = endPos - l;
  92. this.buffered = newBuffered;
  93. if (newBuffered > 0) {
  94. mem.copyWithin(0, l, endPos);
  95. }
  96. }
  97. }
  98. /**
  99. * @param {Buffer} data data
  100. * @returns {void}
  101. */
  102. _updateWithBuffer(data) {
  103. const { exports, buffered, mem } = this;
  104. const length = data.length;
  105. if (buffered + length < this.chunkSize) {
  106. data.copy(mem, buffered, 0, length);
  107. this.buffered += length;
  108. } else {
  109. const l = (buffered + length) & ~(this.chunkSize - 1);
  110. if (l > 65536) {
  111. let i = 65536 - buffered;
  112. data.copy(mem, buffered, 0, i);
  113. exports.update(65536);
  114. const stop = l - buffered - 65536;
  115. while (i < stop) {
  116. data.copy(mem, 0, i, i + 65536);
  117. exports.update(65536);
  118. i += 65536;
  119. }
  120. data.copy(mem, 0, i, l - buffered);
  121. exports.update(l - buffered - i);
  122. } else {
  123. data.copy(mem, buffered, 0, l - buffered);
  124. exports.update(l);
  125. }
  126. const newBuffered = length + buffered - l;
  127. this.buffered = newBuffered;
  128. if (newBuffered > 0) {
  129. data.copy(mem, 0, length - newBuffered, length);
  130. }
  131. }
  132. }
  133. digest(type) {
  134. const { exports, buffered, mem, digestSize } = this;
  135. exports.final(buffered);
  136. this.instancesPool.push(this);
  137. const hex = mem.toString('latin1', 0, digestSize);
  138. if (type === 'hex') {
  139. return hex;
  140. }
  141. if (type === 'binary' || !type) {
  142. return Buffer.from(hex, 'hex');
  143. }
  144. return Buffer.from(hex, 'hex').toString(type);
  145. }
  146. }
  147. const create = (wasmModule, instancesPool, chunkSize, digestSize) => {
  148. if (instancesPool.length > 0) {
  149. const old = instancesPool.pop();
  150. old.reset();
  151. return old;
  152. } else {
  153. return new WasmHash(
  154. new WebAssembly.Instance(wasmModule),
  155. instancesPool,
  156. chunkSize,
  157. digestSize
  158. );
  159. }
  160. };
  161. module.exports = create;
  162. module.exports.MAX_SHORT_STRING = MAX_SHORT_STRING;