helper.d.mts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /// <reference types="node" />
  2. /// <reference types="node" />
  3. import * as stream from 'node:stream';
  4. import _ from 'lodash';
  5. import type { Binary, Encryption, ObjectMetaData, RequestHeaders, ResponseHeader } from "./type.mjs";
  6. export declare function hashBinary(buf: Buffer, enableSHA256: boolean): {
  7. md5sum: string;
  8. sha256sum: string;
  9. };
  10. export declare function uriEscape(uriStr: string): string;
  11. export declare function uriResourceEscape(string: string): string;
  12. export declare function getScope(region: string, date: Date, serviceName?: string): string;
  13. /**
  14. * isAmazonEndpoint - true if endpoint is 's3.amazonaws.com' or 's3.cn-north-1.amazonaws.com.cn'
  15. */
  16. export declare function isAmazonEndpoint(endpoint: string): boolean;
  17. /**
  18. * isVirtualHostStyle - verify if bucket name is support with virtual
  19. * hosts. bucketNames with periods should be always treated as path
  20. * style if the protocol is 'https:', this is due to SSL wildcard
  21. * limitation. For all other buckets and Amazon S3 endpoint we will
  22. * default to virtual host style.
  23. */
  24. export declare function isVirtualHostStyle(endpoint: string, protocol: string, bucket: string, pathStyle: boolean): boolean;
  25. export declare function isValidIP(ip: string): boolean;
  26. /**
  27. * @returns if endpoint is valid domain.
  28. */
  29. export declare function isValidEndpoint(endpoint: string): boolean;
  30. /**
  31. * @returns if input host is a valid domain.
  32. */
  33. export declare function isValidDomain(host: string): boolean;
  34. /**
  35. * Probes contentType using file extensions.
  36. *
  37. * @example
  38. * ```
  39. * // return 'image/png'
  40. * probeContentType('file.png')
  41. * ```
  42. */
  43. export declare function probeContentType(path: string): string;
  44. /**
  45. * is input port valid.
  46. */
  47. export declare function isValidPort(port: unknown): port is number;
  48. export declare function isValidBucketName(bucket: unknown): boolean;
  49. /**
  50. * check if objectName is a valid object name
  51. */
  52. export declare function isValidObjectName(objectName: unknown): boolean;
  53. /**
  54. * check if prefix is valid
  55. */
  56. export declare function isValidPrefix(prefix: unknown): prefix is string;
  57. /**
  58. * check if typeof arg number
  59. */
  60. export declare function isNumber(arg: unknown): arg is number;
  61. export type AnyFunction = (...args: any[]) => any;
  62. /**
  63. * check if typeof arg function
  64. */
  65. export declare function isFunction(arg: unknown): arg is AnyFunction;
  66. /**
  67. * check if typeof arg string
  68. */
  69. export declare function isString(arg: unknown): arg is string;
  70. /**
  71. * check if typeof arg object
  72. */
  73. export declare function isObject(arg: unknown): arg is object;
  74. /**
  75. * check if typeof arg is plain object
  76. */
  77. export declare function isPlainObject(arg: unknown): arg is Record<string, unknown>;
  78. /**
  79. * check if object is readable stream
  80. */
  81. export declare function isReadableStream(arg: unknown): arg is stream.Readable;
  82. /**
  83. * check if arg is boolean
  84. */
  85. export declare function isBoolean(arg: unknown): arg is boolean;
  86. export declare function isEmpty(o: unknown): o is null | undefined;
  87. export declare function isEmptyObject(o: Record<string, unknown>): boolean;
  88. export declare function isDefined<T>(o: T): o is Exclude<T, null | undefined>;
  89. /**
  90. * check if arg is a valid date
  91. */
  92. export declare function isValidDate(arg: unknown): arg is Date;
  93. /**
  94. * Create a Date string with format: 'YYYYMMDDTHHmmss' + Z
  95. */
  96. export declare function makeDateLong(date?: Date): string;
  97. /**
  98. * Create a Date string with format: 'YYYYMMDD'
  99. */
  100. export declare function makeDateShort(date?: Date): string;
  101. /**
  102. * pipesetup sets up pipe() from left to right os streams array
  103. * pipesetup will also make sure that error emitted at any of the upstream Stream
  104. * will be emitted at the last stream. This makes error handling simple
  105. */
  106. export declare function pipesetup(...streams: [stream.Readable, ...stream.Duplex[], stream.Writable]): stream.Readable | stream.Duplex | stream.Writable;
  107. /**
  108. * return a Readable stream that emits data
  109. */
  110. export declare function readableStream(data: unknown): stream.Readable;
  111. /**
  112. * Process metadata to insert appropriate value to `content-type` attribute
  113. */
  114. export declare function insertContentType(metaData: ObjectMetaData, filePath: string): ObjectMetaData;
  115. /**
  116. * Function prepends metadata with the appropriate prefix if it is not already on
  117. */
  118. export declare function prependXAMZMeta(metaData?: ObjectMetaData): RequestHeaders;
  119. /**
  120. * Checks if it is a valid header according to the AmazonS3 API
  121. */
  122. export declare function isAmzHeader(key: string): boolean;
  123. /**
  124. * Checks if it is a supported Header
  125. */
  126. export declare function isSupportedHeader(key: string): boolean;
  127. /**
  128. * Checks if it is a storage header
  129. */
  130. export declare function isStorageClassHeader(key: string): boolean;
  131. export declare function extractMetadata(headers: ResponseHeader): _.Dictionary<string>;
  132. export declare function getVersionId(headers?: ResponseHeader): string | null;
  133. export declare function getSourceVersionId(headers?: ResponseHeader): string | null;
  134. export declare function sanitizeETag(etag?: string): string;
  135. export declare function toMd5(payload: Binary): string;
  136. export declare function toSha256(payload: Binary): string;
  137. /**
  138. * toArray returns a single element array with param being the element,
  139. * if param is just a string, and returns 'param' back if it is an array
  140. * So, it makes sure param is always an array
  141. */
  142. export declare function toArray<T = unknown>(param: T | T[]): Array<T>;
  143. export declare function sanitizeObjectKey(objectName: string): string;
  144. export declare function sanitizeSize(size?: string): number | undefined;
  145. export declare const PART_CONSTRAINTS: {
  146. ABS_MIN_PART_SIZE: number;
  147. MIN_PART_SIZE: number;
  148. MAX_PARTS_COUNT: number;
  149. MAX_PART_SIZE: number;
  150. MAX_SINGLE_PUT_OBJECT_SIZE: number;
  151. MAX_MULTIPART_PUT_OBJECT_SIZE: number;
  152. };
  153. /**
  154. * Return Encryption headers
  155. * @param encConfig
  156. * @returns an object with key value pairs that can be used in headers.
  157. */
  158. export declare function getEncryptionHeaders(encConfig: Encryption): RequestHeaders;
  159. export declare function partsRequired(size: number): number;
  160. /**
  161. * calculateEvenSplits - computes splits for a source and returns
  162. * start and end index slices. Splits happen evenly to be sure that no
  163. * part is less than 5MiB, as that could fail the multipart request if
  164. * it is not the last part.
  165. */
  166. export declare function calculateEvenSplits<T extends {
  167. Start?: number;
  168. }>(size: number, objInfo: T): {
  169. startIndex: number[];
  170. objInfo: T;
  171. endIndex: number[];
  172. } | null;
  173. export declare function parseXml(xml: string): any;
  174. /**
  175. * get content size of object content to upload
  176. */
  177. export declare function getContentLength(s: stream.Readable | Buffer | string): Promise<number | null>;