index.js 13 KB

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