buffer.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var buffer_exports = {};
  19. __export(buffer_exports, {
  20. bufferToFormData: () => bufferToFormData,
  21. bufferToString: () => bufferToString,
  22. equal: () => equal,
  23. timingSafeEqual: () => timingSafeEqual
  24. });
  25. module.exports = __toCommonJS(buffer_exports);
  26. var import_crypto = require("./crypto");
  27. const equal = (a, b) => {
  28. if (a === b) {
  29. return true;
  30. }
  31. if (a.byteLength !== b.byteLength) {
  32. return false;
  33. }
  34. const va = new DataView(a);
  35. const vb = new DataView(b);
  36. let i = va.byteLength;
  37. while (i--) {
  38. if (va.getUint8(i) !== vb.getUint8(i)) {
  39. return false;
  40. }
  41. }
  42. return true;
  43. };
  44. const constantTimeEqualString = (a, b) => {
  45. const aLen = a.length;
  46. const bLen = b.length;
  47. const maxLen = Math.max(aLen, bLen);
  48. let out = aLen ^ bLen;
  49. for (let i = 0; i < maxLen; i++) {
  50. const aChar = i < aLen ? a.charCodeAt(i) : 0;
  51. const bChar = i < bLen ? b.charCodeAt(i) : 0;
  52. out |= aChar ^ bChar;
  53. }
  54. return out === 0;
  55. };
  56. const timingSafeEqualString = async (a, b, hashFunction) => {
  57. if (!hashFunction) {
  58. hashFunction = import_crypto.sha256;
  59. }
  60. const [sa, sb] = await Promise.all([hashFunction(a), hashFunction(b)]);
  61. if (sa == null || sb == null || typeof sa !== "string" || typeof sb !== "string") {
  62. return false;
  63. }
  64. const hashEqual = constantTimeEqualString(sa, sb);
  65. const originalEqual = constantTimeEqualString(a, b);
  66. return hashEqual && originalEqual;
  67. };
  68. const timingSafeEqual = async (a, b, hashFunction) => {
  69. if (typeof a === "string" && typeof b === "string") {
  70. return timingSafeEqualString(a, b, hashFunction);
  71. }
  72. if (!hashFunction) {
  73. hashFunction = import_crypto.sha256;
  74. }
  75. const [sa, sb] = await Promise.all([hashFunction(a), hashFunction(b)]);
  76. if (!sa || !sb || typeof sa !== "string" || typeof sb !== "string") {
  77. return false;
  78. }
  79. return timingSafeEqualString(sa, sb);
  80. };
  81. const bufferToString = (buffer) => {
  82. if (buffer instanceof ArrayBuffer) {
  83. const enc = new TextDecoder("utf-8");
  84. return enc.decode(buffer);
  85. }
  86. return buffer;
  87. };
  88. const bufferToFormData = (arrayBuffer, contentType) => {
  89. const response = new Response(arrayBuffer, {
  90. headers: {
  91. "Content-Type": contentType
  92. }
  93. });
  94. return response.formData();
  95. };
  96. // Annotate the CommonJS export names for ESM import in node:
  97. 0 && (module.exports = {
  98. bufferToFormData,
  99. bufferToString,
  100. equal,
  101. timingSafeEqual
  102. });