index.es.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*!
  2. * MIT License
  3. *
  4. * Copyright (c) 2017-2024 Peculiar Ventures, LLC
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. */
  25. const ARRAY_BUFFER_NAME = "[object ArrayBuffer]";
  26. class BufferSourceConverter {
  27. static isArrayBuffer(data) {
  28. return Object.prototype.toString.call(data) === ARRAY_BUFFER_NAME;
  29. }
  30. static toArrayBuffer(data) {
  31. if (this.isArrayBuffer(data)) {
  32. return data;
  33. }
  34. if (data.byteLength === data.buffer.byteLength) {
  35. return data.buffer;
  36. }
  37. if (data.byteOffset === 0 && data.byteLength === data.buffer.byteLength) {
  38. return data.buffer;
  39. }
  40. return this.toUint8Array(data.buffer)
  41. .slice(data.byteOffset, data.byteOffset + data.byteLength)
  42. .buffer;
  43. }
  44. static toUint8Array(data) {
  45. return this.toView(data, Uint8Array);
  46. }
  47. static toView(data, type) {
  48. if (data.constructor === type) {
  49. return data;
  50. }
  51. if (this.isArrayBuffer(data)) {
  52. return new type(data);
  53. }
  54. if (this.isArrayBufferView(data)) {
  55. return new type(data.buffer, data.byteOffset, data.byteLength);
  56. }
  57. throw new TypeError("The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
  58. }
  59. static isBufferSource(data) {
  60. return this.isArrayBufferView(data)
  61. || this.isArrayBuffer(data);
  62. }
  63. static isArrayBufferView(data) {
  64. return ArrayBuffer.isView(data)
  65. || (data && this.isArrayBuffer(data.buffer));
  66. }
  67. static isEqual(a, b) {
  68. const aView = BufferSourceConverter.toUint8Array(a);
  69. const bView = BufferSourceConverter.toUint8Array(b);
  70. if (aView.length !== bView.byteLength) {
  71. return false;
  72. }
  73. for (let i = 0; i < aView.length; i++) {
  74. if (aView[i] !== bView[i]) {
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. static concat(...args) {
  81. let buffers;
  82. if (Array.isArray(args[0]) && !(args[1] instanceof Function)) {
  83. buffers = args[0];
  84. }
  85. else if (Array.isArray(args[0]) && args[1] instanceof Function) {
  86. buffers = args[0];
  87. }
  88. else {
  89. if (args[args.length - 1] instanceof Function) {
  90. buffers = args.slice(0, args.length - 1);
  91. }
  92. else {
  93. buffers = args;
  94. }
  95. }
  96. let size = 0;
  97. for (const buffer of buffers) {
  98. size += buffer.byteLength;
  99. }
  100. const res = new Uint8Array(size);
  101. let offset = 0;
  102. for (const buffer of buffers) {
  103. const view = this.toUint8Array(buffer);
  104. res.set(view, offset);
  105. offset += view.length;
  106. }
  107. if (args[args.length - 1] instanceof Function) {
  108. return this.toView(res, args[args.length - 1]);
  109. }
  110. return res.buffer;
  111. }
  112. }
  113. const STRING_TYPE = "string";
  114. const HEX_REGEX = /^[0-9a-f\s]+$/i;
  115. const BASE64_REGEX = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
  116. const BASE64URL_REGEX = /^[a-zA-Z0-9-_]+$/;
  117. class Utf8Converter {
  118. static fromString(text) {
  119. const s = unescape(encodeURIComponent(text));
  120. const uintArray = new Uint8Array(s.length);
  121. for (let i = 0; i < s.length; i++) {
  122. uintArray[i] = s.charCodeAt(i);
  123. }
  124. return uintArray.buffer;
  125. }
  126. static toString(buffer) {
  127. const buf = BufferSourceConverter.toUint8Array(buffer);
  128. let encodedString = "";
  129. for (let i = 0; i < buf.length; i++) {
  130. encodedString += String.fromCharCode(buf[i]);
  131. }
  132. const decodedString = decodeURIComponent(escape(encodedString));
  133. return decodedString;
  134. }
  135. }
  136. class Utf16Converter {
  137. static toString(buffer, littleEndian = false) {
  138. const arrayBuffer = BufferSourceConverter.toArrayBuffer(buffer);
  139. const dataView = new DataView(arrayBuffer);
  140. let res = "";
  141. for (let i = 0; i < arrayBuffer.byteLength; i += 2) {
  142. const code = dataView.getUint16(i, littleEndian);
  143. res += String.fromCharCode(code);
  144. }
  145. return res;
  146. }
  147. static fromString(text, littleEndian = false) {
  148. const res = new ArrayBuffer(text.length * 2);
  149. const dataView = new DataView(res);
  150. for (let i = 0; i < text.length; i++) {
  151. dataView.setUint16(i * 2, text.charCodeAt(i), littleEndian);
  152. }
  153. return res;
  154. }
  155. }
  156. class Convert {
  157. static isHex(data) {
  158. return typeof data === STRING_TYPE
  159. && HEX_REGEX.test(data);
  160. }
  161. static isBase64(data) {
  162. return typeof data === STRING_TYPE
  163. && BASE64_REGEX.test(data);
  164. }
  165. static isBase64Url(data) {
  166. return typeof data === STRING_TYPE
  167. && BASE64URL_REGEX.test(data);
  168. }
  169. static ToString(buffer, enc = "utf8") {
  170. const buf = BufferSourceConverter.toUint8Array(buffer);
  171. switch (enc.toLowerCase()) {
  172. case "utf8":
  173. return this.ToUtf8String(buf);
  174. case "binary":
  175. return this.ToBinary(buf);
  176. case "hex":
  177. return this.ToHex(buf);
  178. case "base64":
  179. return this.ToBase64(buf);
  180. case "base64url":
  181. return this.ToBase64Url(buf);
  182. case "utf16le":
  183. return Utf16Converter.toString(buf, true);
  184. case "utf16":
  185. case "utf16be":
  186. return Utf16Converter.toString(buf);
  187. default:
  188. throw new Error(`Unknown type of encoding '${enc}'`);
  189. }
  190. }
  191. static FromString(str, enc = "utf8") {
  192. if (!str) {
  193. return new ArrayBuffer(0);
  194. }
  195. switch (enc.toLowerCase()) {
  196. case "utf8":
  197. return this.FromUtf8String(str);
  198. case "binary":
  199. return this.FromBinary(str);
  200. case "hex":
  201. return this.FromHex(str);
  202. case "base64":
  203. return this.FromBase64(str);
  204. case "base64url":
  205. return this.FromBase64Url(str);
  206. case "utf16le":
  207. return Utf16Converter.fromString(str, true);
  208. case "utf16":
  209. case "utf16be":
  210. return Utf16Converter.fromString(str);
  211. default:
  212. throw new Error(`Unknown type of encoding '${enc}'`);
  213. }
  214. }
  215. static ToBase64(buffer) {
  216. const buf = BufferSourceConverter.toUint8Array(buffer);
  217. if (typeof btoa !== "undefined") {
  218. const binary = this.ToString(buf, "binary");
  219. return btoa(binary);
  220. }
  221. else {
  222. return Buffer.from(buf).toString("base64");
  223. }
  224. }
  225. static FromBase64(base64) {
  226. const formatted = this.formatString(base64);
  227. if (!formatted) {
  228. return new ArrayBuffer(0);
  229. }
  230. if (!Convert.isBase64(formatted)) {
  231. throw new TypeError("Argument 'base64Text' is not Base64 encoded");
  232. }
  233. if (typeof atob !== "undefined") {
  234. return this.FromBinary(atob(formatted));
  235. }
  236. else {
  237. return new Uint8Array(Buffer.from(formatted, "base64")).buffer;
  238. }
  239. }
  240. static FromBase64Url(base64url) {
  241. const formatted = this.formatString(base64url);
  242. if (!formatted) {
  243. return new ArrayBuffer(0);
  244. }
  245. if (!Convert.isBase64Url(formatted)) {
  246. throw new TypeError("Argument 'base64url' is not Base64Url encoded");
  247. }
  248. return this.FromBase64(this.Base64Padding(formatted.replace(/\-/g, "+").replace(/\_/g, "/")));
  249. }
  250. static ToBase64Url(data) {
  251. return this.ToBase64(data).replace(/\+/g, "-").replace(/\//g, "_").replace(/\=/g, "");
  252. }
  253. static FromUtf8String(text, encoding = Convert.DEFAULT_UTF8_ENCODING) {
  254. switch (encoding) {
  255. case "ascii":
  256. return this.FromBinary(text);
  257. case "utf8":
  258. return Utf8Converter.fromString(text);
  259. case "utf16":
  260. case "utf16be":
  261. return Utf16Converter.fromString(text);
  262. case "utf16le":
  263. case "usc2":
  264. return Utf16Converter.fromString(text, true);
  265. default:
  266. throw new Error(`Unknown type of encoding '${encoding}'`);
  267. }
  268. }
  269. static ToUtf8String(buffer, encoding = Convert.DEFAULT_UTF8_ENCODING) {
  270. switch (encoding) {
  271. case "ascii":
  272. return this.ToBinary(buffer);
  273. case "utf8":
  274. return Utf8Converter.toString(buffer);
  275. case "utf16":
  276. case "utf16be":
  277. return Utf16Converter.toString(buffer);
  278. case "utf16le":
  279. case "usc2":
  280. return Utf16Converter.toString(buffer, true);
  281. default:
  282. throw new Error(`Unknown type of encoding '${encoding}'`);
  283. }
  284. }
  285. static FromBinary(text) {
  286. const stringLength = text.length;
  287. const resultView = new Uint8Array(stringLength);
  288. for (let i = 0; i < stringLength; i++) {
  289. resultView[i] = text.charCodeAt(i);
  290. }
  291. return resultView.buffer;
  292. }
  293. static ToBinary(buffer) {
  294. const buf = BufferSourceConverter.toUint8Array(buffer);
  295. let res = "";
  296. for (let i = 0; i < buf.length; i++) {
  297. res += String.fromCharCode(buf[i]);
  298. }
  299. return res;
  300. }
  301. static ToHex(buffer) {
  302. const buf = BufferSourceConverter.toUint8Array(buffer);
  303. let result = "";
  304. const len = buf.length;
  305. for (let i = 0; i < len; i++) {
  306. const byte = buf[i];
  307. if (byte < 16) {
  308. result += "0";
  309. }
  310. result += byte.toString(16);
  311. }
  312. return result;
  313. }
  314. static FromHex(hexString) {
  315. let formatted = this.formatString(hexString);
  316. if (!formatted) {
  317. return new ArrayBuffer(0);
  318. }
  319. if (!Convert.isHex(formatted)) {
  320. throw new TypeError("Argument 'hexString' is not HEX encoded");
  321. }
  322. if (formatted.length % 2) {
  323. formatted = `0${formatted}`;
  324. }
  325. const res = new Uint8Array(formatted.length / 2);
  326. for (let i = 0; i < formatted.length; i = i + 2) {
  327. const c = formatted.slice(i, i + 2);
  328. res[i / 2] = parseInt(c, 16);
  329. }
  330. return res.buffer;
  331. }
  332. static ToUtf16String(buffer, littleEndian = false) {
  333. return Utf16Converter.toString(buffer, littleEndian);
  334. }
  335. static FromUtf16String(text, littleEndian = false) {
  336. return Utf16Converter.fromString(text, littleEndian);
  337. }
  338. static Base64Padding(base64) {
  339. const padCount = 4 - (base64.length % 4);
  340. if (padCount < 4) {
  341. for (let i = 0; i < padCount; i++) {
  342. base64 += "=";
  343. }
  344. }
  345. return base64;
  346. }
  347. static formatString(data) {
  348. return (data === null || data === void 0 ? void 0 : data.replace(/[\n\r\t ]/g, "")) || "";
  349. }
  350. }
  351. Convert.DEFAULT_UTF8_ENCODING = "utf8";
  352. function assign(target, ...sources) {
  353. const res = arguments[0];
  354. for (let i = 1; i < arguments.length; i++) {
  355. const obj = arguments[i];
  356. for (const prop in obj) {
  357. res[prop] = obj[prop];
  358. }
  359. }
  360. return res;
  361. }
  362. function combine(...buf) {
  363. const totalByteLength = buf.map((item) => item.byteLength).reduce((prev, cur) => prev + cur);
  364. const res = new Uint8Array(totalByteLength);
  365. let currentPos = 0;
  366. buf.map((item) => new Uint8Array(item)).forEach((arr) => {
  367. for (const item2 of arr) {
  368. res[currentPos++] = item2;
  369. }
  370. });
  371. return res.buffer;
  372. }
  373. function isEqual(bytes1, bytes2) {
  374. if (!(bytes1 && bytes2)) {
  375. return false;
  376. }
  377. if (bytes1.byteLength !== bytes2.byteLength) {
  378. return false;
  379. }
  380. const b1 = new Uint8Array(bytes1);
  381. const b2 = new Uint8Array(bytes2);
  382. for (let i = 0; i < bytes1.byteLength; i++) {
  383. if (b1[i] !== b2[i]) {
  384. return false;
  385. }
  386. }
  387. return true;
  388. }
  389. export { BufferSourceConverter, Convert, assign, combine, isEqual };