index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.overrideBytesInBuffer = overrideBytesInBuffer;
  6. exports.makeBuffer = makeBuffer;
  7. exports.fromHexdump = fromHexdump;
  8. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  9. var debug = require("debug")("webassemblyjs:wasm");
  10. function concatUint8Arrays() {
  11. for (var _len = arguments.length, arrays = new Array(_len), _key = 0; _key < _len; _key++) {
  12. arrays[_key] = arguments[_key];
  13. }
  14. var totalLength = arrays.reduce(function (a, b) {
  15. return a + b.length;
  16. }, 0);
  17. var result = new Uint8Array(totalLength);
  18. var offset = 0;
  19. for (var _i = 0; _i < arrays.length; _i++) {
  20. var arr = arrays[_i];
  21. if (arr instanceof Uint8Array === false) {
  22. throw new Error("arr must be of type Uint8Array");
  23. }
  24. result.set(arr, offset);
  25. offset += arr.length;
  26. }
  27. return result;
  28. }
  29. function overrideBytesInBuffer(buffer, startLoc, endLoc, newBytes) {
  30. var beforeBytes = buffer.slice(0, startLoc);
  31. var afterBytes = buffer.slice(endLoc, buffer.length);
  32. debug("overrideBytesInBuffer start=%d end=%d newBytes=%s", startLoc, endLoc, // $FlowIgnore
  33. newBytes.map(function (dec) {
  34. return dec.toString("16");
  35. }).join()); // replacement is empty, we can omit it
  36. if (newBytes.length === 0) {
  37. return concatUint8Arrays(beforeBytes, afterBytes);
  38. }
  39. var replacement = Uint8Array.from(newBytes);
  40. return concatUint8Arrays(beforeBytes, replacement, afterBytes);
  41. }
  42. function makeBuffer() {
  43. for (var _len2 = arguments.length, splitedBytes = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  44. splitedBytes[_key2] = arguments[_key2];
  45. }
  46. var bytes = [].concat.apply([], splitedBytes);
  47. return new Uint8Array(bytes).buffer;
  48. }
  49. function fromHexdump(str) {
  50. var lines = str.split("\n"); // remove any leading left whitespace
  51. lines = lines.map(function (line) {
  52. return line.trim();
  53. });
  54. var bytes = lines.reduce(function (acc, line) {
  55. var cols = line.split(" "); // remove the offset, left column
  56. cols.shift();
  57. cols = cols.filter(function (x) {
  58. return x !== "";
  59. });
  60. var bytes = cols.map(function (x) {
  61. return parseInt(x, 16);
  62. });
  63. acc.push.apply(acc, _toConsumableArray(bytes));
  64. return acc;
  65. }, []);
  66. return Buffer.from(bytes);
  67. }