index.d.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. type BufferSource = ArrayBuffer | ArrayBufferView;
  26. interface ArrayBufferViewConstructor<T extends ArrayBufferView> {
  27. readonly prototype: T;
  28. new (length: number): T;
  29. new (array: ArrayLike<number> | ArrayBufferLike): T;
  30. new (buffer: ArrayBufferLike, byteOffset?: number, length?: number): T;
  31. }
  32. declare class BufferSourceConverter {
  33. /**
  34. * Checks if incoming data is ArrayBuffer
  35. * @param data Data to be checked
  36. * @returns Returns `true` if incoming data is ArrayBuffer, otherwise `false`
  37. */
  38. static isArrayBuffer(data: any): data is ArrayBuffer;
  39. /**
  40. * Converts incoming buffer source into ArrayBuffer
  41. * @param data Buffer source
  42. * @returns ArrayBuffer representation of data
  43. * @remarks If incoming data is ArrayBuffer then it returns it without copying,
  44. * otherwise it copies data into new ArrayBuffer because incoming data can be
  45. * ArrayBufferView with offset and length which is not equal to buffer length
  46. */
  47. static toArrayBuffer(data: BufferSource): ArrayBuffer;
  48. /**
  49. * Converts incoming buffer source into Uint8Array
  50. * @param data Buffer source
  51. * @returns Uint8Array representation of data
  52. */
  53. static toUint8Array(data: BufferSource): Uint8Array;
  54. /**
  55. * Converts a given `BufferSource` to the specified `ArrayBufferView` type.
  56. * @param data The `BufferSource` to convert.
  57. * @param type The `ArrayBufferView` constructor to use for the conversion.
  58. * @returns The converted `ArrayBufferView`.
  59. * @throws {TypeError} If the provided value is not of type `(ArrayBuffer or ArrayBufferView)`.
  60. * @remarks If incoming data is ArrayBufferView and it's type is equal to specified type
  61. * then it returns it without copying, otherwise it copies data into new ArrayBufferView
  62. * because incoming data can be ArrayBufferView with offset and length which is not equal
  63. * to buffer length
  64. */
  65. static toView<T extends ArrayBufferView>(data: BufferSource, type: ArrayBufferViewConstructor<T>): T;
  66. /**
  67. * Checks if incoming data is BufferSource
  68. * @param data Data to be checked
  69. * @returns Returns `true` if incoming data is BufferSource, otherwise `false`
  70. */
  71. static isBufferSource(data: any): data is BufferSource;
  72. /**
  73. * Checks if incoming data is ArraybufferView
  74. * @param data Data to be checked
  75. * @returns Returns `true` if incoming data is ArraybufferView, otherwise `false`
  76. */
  77. static isArrayBufferView(data: any): data is ArrayBufferView;
  78. /**
  79. * Checks if buffers are equal
  80. * @param a Buffer source
  81. * @param b Buffer source
  82. * @returns Returns `true` if buffers are equal, otherwise `false`
  83. */
  84. static isEqual(a: BufferSource, b: BufferSource): boolean;
  85. /**
  86. * Concatenates buffers
  87. * @param buffers List of buffers
  88. * @returns Concatenated buffer
  89. */
  90. static concat(...buffers: BufferSource[]): ArrayBuffer;
  91. /**
  92. * Concatenates buffers
  93. * @param buffers List of buffers
  94. * @returns Concatenated buffer
  95. */
  96. static concat(buffers: BufferSource[]): ArrayBuffer;
  97. /**
  98. * Concatenates buffers and converts it into specified ArrayBufferView
  99. * @param buffers List of buffers
  100. * @param type ArrayBufferView constructor
  101. * @returns Concatenated buffer of specified type
  102. */
  103. static concat<T extends ArrayBufferView>(buffers: BufferSource[], type: ArrayBufferViewConstructor<T>): T;
  104. }
  105. type BufferEncoding = "utf8" | "binary" | "base64" | "base64url" | "hex" | string;
  106. type TextEncoding = "ascii" | "utf8" | "utf16" | "utf16be" | "utf16le" | "usc2";
  107. declare class Convert {
  108. static isHex(data: any): data is string;
  109. static isBase64(data: any): data is string;
  110. static isBase64Url(data: any): data is string;
  111. static ToString(buffer: BufferSource, enc?: BufferEncoding): string;
  112. static FromString(str: string, enc?: BufferEncoding): ArrayBuffer;
  113. /**
  114. * Converts byte array to Base64 encoded string.
  115. * @param buffer - Byte array to encode.
  116. * @returns Base64 string.
  117. */
  118. static ToBase64(buffer: BufferSource): string;
  119. /**
  120. * Converts byte array to Base64 encoded string.
  121. * @param base64 - Base64 encoded string.
  122. * @returns Byte array.
  123. */
  124. static FromBase64(base64: string): ArrayBuffer;
  125. /**
  126. * Converts Base64Url encoded string to byte array.
  127. * @param base64url - Base64Url encoded string.
  128. * @returns Byte array.
  129. */
  130. static FromBase64Url(base64url: string): ArrayBuffer;
  131. /**
  132. * Converts byte array to Base64Url encoded string.
  133. * @param data - Byte array to encode.
  134. * @returns Base64Url encoded string.
  135. */
  136. static ToBase64Url(data: BufferSource): string;
  137. protected static DEFAULT_UTF8_ENCODING: TextEncoding;
  138. /**
  139. * Converts JavaScript string to ArrayBuffer using specified encoding.
  140. * @param text - JavaScript string to convert.
  141. * @param encoding - Encoding to use. Default is 'utf8'.
  142. * @returns ArrayBuffer representing input string.
  143. */
  144. static FromUtf8String(text: string, encoding?: TextEncoding): ArrayBuffer;
  145. /**
  146. * Converts ArrayBuffer to JavaScript string using specified encoding.
  147. * @param buffer - Buffer to convert.
  148. * @param encoding - Encoding to use. Default is 'utf8'.
  149. * @returns JavaScript string derived from input buffer.
  150. */
  151. static ToUtf8String(buffer: BufferSource, encoding?: TextEncoding): string;
  152. /**
  153. * Converts binary string to ArrayBuffer.
  154. * @param text - Binary string.
  155. * @returns Byte array.
  156. */
  157. static FromBinary(text: string): ArrayBuffer;
  158. /**
  159. * Converts buffer to binary string.
  160. * @param buffer - Input buffer.
  161. * @returns Binary string.
  162. */
  163. static ToBinary(buffer: BufferSource): string;
  164. /**
  165. * Converts buffer to HEX string
  166. * @param {BufferSource} buffer Incoming buffer
  167. * @returns string
  168. */
  169. static ToHex(buffer: BufferSource): string;
  170. /**
  171. * Converts HEX string to buffer
  172. *
  173. * @static
  174. * @param {string} formatted
  175. * @returns {Uint8Array}
  176. *
  177. * @memberOf Convert
  178. */
  179. static FromHex(hexString: string): ArrayBuffer;
  180. /**
  181. * Converts UTF-16 encoded buffer to UTF-8 string
  182. * @param buffer UTF-16 encoded buffer
  183. * @param littleEndian Indicates whether the char code is stored in little- or big-endian format
  184. * @returns UTF-8 string
  185. */
  186. static ToUtf16String(buffer: BufferSource, littleEndian?: boolean): string;
  187. /**
  188. * Converts UTF-8 string to UTF-16 encoded buffer
  189. * @param text UTF-8 string
  190. * @param littleEndian Indicates whether the char code is stored in little- or big-endian format
  191. * @returns UTF-16 encoded buffer
  192. */
  193. static FromUtf16String(text: string, littleEndian?: boolean): ArrayBuffer;
  194. protected static Base64Padding(base64: string): string;
  195. /**
  196. * Removes odd chars from string data
  197. * @param data String data
  198. */
  199. static formatString(data: string): string;
  200. }
  201. declare function assign(target: any, ...sources: any[]): any;
  202. declare function combine(...buf: ArrayBuffer[]): ArrayBufferLike;
  203. declare function isEqual(bytes1: ArrayBuffer, bytes2: ArrayBuffer): boolean;
  204. export { type ArrayBufferViewConstructor, type BufferEncoding, type BufferSource, BufferSourceConverter, Convert, type TextEncoding, assign, combine, isEqual };