utils.d.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /// <reference types="node" />
  2. import { errorCode, targetType } from './constant';
  3. import { EventEmitter } from 'events';
  4. import { ScpClient } from '.';
  5. import { CheckResult, ErrorCustom } from './types';
  6. /**
  7. * Generate a new Error object with a reformatted error message which
  8. * is a little more informative and useful to users.
  9. *
  10. * @param {Error|string} err - The Error object the new error will be based on
  11. * @param {number} retryCount - For those functions which use retry. Number of
  12. * attempts to complete before giving up
  13. * @returns {Error} New error with custom error message
  14. */
  15. export declare function formatError(err: ErrorCustom | string, name?: string, eCode?: errorCode, retryCount?: number): ErrorCustom;
  16. /**
  17. * Tests an error to see if it is one which has already been customised
  18. * by this module or not. If not, applies appropriate customisation.
  19. *
  20. * @param {Error} err - an Error object
  21. * @param {String} name - name to be used in customised error message
  22. * @param {Function} reject - If defined, call this function instead of
  23. * throwing the error
  24. * @throws {Error}
  25. */
  26. export declare function handleError(err: ErrorCustom, name: string, reject: (e: any) => void): void;
  27. /**
  28. * Remove all ready, error and end listeners.
  29. *
  30. * @param {Emitter} emitter - The emitter object to remove listeners from
  31. */
  32. /**
  33. * Simple default error listener. Will reformat the error message and
  34. * throw a new error.
  35. *
  36. * @param {Error} err - source for defining new error
  37. * @throws {Error} Throws new error
  38. */
  39. export declare function makeErrorListener(reject: (e: any) => void, client: ScpClient, name: string): (err: Error) => void;
  40. export declare function makeEndListener(client: ScpClient): () => void;
  41. export declare function makeCloseListener(client: ScpClient, reject?: (e: any) => void, name?: string): () => void;
  42. /**
  43. * @async
  44. *
  45. * Tests to see if a path identifies an existing item. Returns either
  46. * 'd' = directory, 'l' = sym link or '-' regular file if item exists. Returns
  47. * false if it does not
  48. *
  49. * @param {String} localPath
  50. * @returns {Boolean | String}
  51. */
  52. export declare function localExists(localPath: string): Promise<string>;
  53. /**
  54. * Used by checkRemotePath and checkLocalPath to help ensure consistent
  55. * error messages.
  56. *
  57. * @param {Error} err - original error
  58. * @param {String} testPath - path associated with the error
  59. * @returns {Object} with properties of 'msg' and 'code'.
  60. */
  61. export declare function classifyError(err: ErrorCustom, testPath: string): {
  62. msg: string;
  63. code: string;
  64. };
  65. export declare function localAccess(localPath: string, mode: number): Promise<CheckResult>;
  66. export declare function checkLocalReadFile(localPath: string, localType: string): Promise<CheckResult>;
  67. export declare function checkLocalReadDir(localPath: string, localType: string): Promise<CheckResult>;
  68. export declare function checkLocalWriteFile(localPath: string, localType: string): Promise<CheckResult>;
  69. export declare function checkLocalWriteDir(localPath: string, localType: string): Promise<CheckResult>;
  70. export declare function checkLocalPath(lPath: string, target?: targetType): Promise<CheckResult>;
  71. export declare function normalizeRemotePath(client: ScpClient, aPath: string): Promise<string>;
  72. export declare function checkReadObject(aPath: string, type: string): {
  73. path: string;
  74. type: string;
  75. valid: boolean;
  76. msg: string | undefined;
  77. code: errorCode | undefined;
  78. };
  79. export declare function checkReadFile(aPath: string, type: string): {
  80. path: string;
  81. type: string;
  82. valid: boolean;
  83. msg: string;
  84. code: errorCode;
  85. } | {
  86. path: string;
  87. type: string;
  88. valid: boolean;
  89. msg?: undefined;
  90. code?: undefined;
  91. };
  92. export declare function checkReadDir(aPath: string, type: string): {
  93. path: string;
  94. type: string;
  95. valid: boolean;
  96. msg: string;
  97. code: errorCode;
  98. } | {
  99. path: string;
  100. type: string;
  101. valid: boolean;
  102. msg?: undefined;
  103. code?: undefined;
  104. };
  105. export declare function checkWriteFile(client: ScpClient, aPath: string, type: string): Promise<{
  106. path: string;
  107. type: string;
  108. valid: boolean;
  109. msg: string;
  110. code: errorCode;
  111. } | {
  112. path: string;
  113. type: boolean;
  114. valid: boolean;
  115. msg: string;
  116. code: errorCode;
  117. } | {
  118. path: string;
  119. type: string;
  120. valid: boolean;
  121. msg?: undefined;
  122. code?: undefined;
  123. }>;
  124. export declare function checkWriteDir(client: ScpClient, aPath: string, type: string): Promise<{
  125. path: string;
  126. type: string;
  127. valid: boolean;
  128. msg: string;
  129. code: errorCode;
  130. } | {
  131. path: string;
  132. type: string;
  133. valid: boolean;
  134. msg?: undefined;
  135. code?: undefined;
  136. } | {
  137. path: string;
  138. type: boolean;
  139. valid: boolean;
  140. msg: string;
  141. code: errorCode;
  142. }>;
  143. export declare function checkWriteObject(aPath: string, type: string): {
  144. path: string;
  145. type: string;
  146. valid: boolean;
  147. };
  148. export declare function checkRemotePath(client: ScpClient, rPath: string, target?: targetType): Promise<{
  149. path: string;
  150. type: boolean;
  151. valid: boolean;
  152. msg: string;
  153. code: errorCode;
  154. } | {
  155. path: string;
  156. type: string;
  157. valid: boolean;
  158. }>;
  159. /**
  160. * Check to see if there is an active sftp connection
  161. *
  162. * @param {Object} client - current sftp object
  163. * @param {String} name - name given to this connection
  164. * @param {Function} reject - if defined, call this rather than throw
  165. * an error
  166. * @returns {Boolean} True if connection OK
  167. * @throws {Error}
  168. */
  169. export declare function haveConnection(client: ScpClient, name: string, reject?: (e: any) => void): boolean;
  170. export declare function dumpListeners(emitter: EventEmitter): void;
  171. export declare function hasListener(emitter: EventEmitter, eventName: string, listenerName: string): boolean;
  172. export declare function joinRemote(client: ScpClient, ...args: string[]): string;