Client.d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /// <reference types="node" />
  2. /// <reference types="node" />
  3. import { Readable, Writable } from "stream";
  4. import { ConnectionOptions as TLSConnectionOptions } from "tls";
  5. import { FileInfo } from "./FileInfo";
  6. import { FTPContext, FTPResponse } from "./FtpContext";
  7. import { ProgressHandler, ProgressTracker } from "./ProgressTracker";
  8. import { UploadCommand } from "./transfer";
  9. export interface AccessOptions {
  10. /** Host the client should connect to. Optional, default is "localhost". */
  11. readonly host?: string;
  12. /** Port the client should connect to. Optional, default is 21. */
  13. readonly port?: number;
  14. /** Username to use for login. Optional, default is "anonymous". */
  15. readonly user?: string;
  16. /** Password to use for login. Optional, default is "guest". */
  17. readonly password?: string;
  18. /** Use FTPS over TLS. Optional, default is false. True is preferred explicit TLS, "implicit" supports legacy, non-standardized implicit TLS. */
  19. readonly secure?: boolean | "implicit";
  20. /** TLS options as in [tls.connect(options)](https://nodejs.org/api/tls.html#tls_tls_connect_options_callback), optional. */
  21. readonly secureOptions?: TLSConnectionOptions;
  22. }
  23. /** Prepares a data connection for transfer. */
  24. export type TransferStrategy = (ftp: FTPContext) => Promise<FTPResponse>;
  25. /** Parses raw directoy listing data. */
  26. export type RawListParser = (rawList: string) => FileInfo[];
  27. export interface UploadOptions {
  28. /** Offset in the local file to start uploading from. */
  29. localStart?: number;
  30. /** Final byte position to include in upload from the local file. */
  31. localEndInclusive?: number;
  32. }
  33. /**
  34. * High-level API to interact with an FTP server.
  35. */
  36. export declare class Client {
  37. prepareTransfer: TransferStrategy;
  38. parseList: RawListParser;
  39. availableListCommands: string[];
  40. /** Low-level API to interact with FTP server. */
  41. readonly ftp: FTPContext;
  42. /** Tracks progress of data transfers. */
  43. protected _progressTracker: ProgressTracker;
  44. /**
  45. * Instantiate an FTP client.
  46. *
  47. * @param timeout Timeout in milliseconds, use 0 for no timeout. Optional, default is 30 seconds.
  48. */
  49. constructor(timeout?: number);
  50. /**
  51. * Close the client and all open socket connections.
  52. *
  53. * Close the client and all open socket connections. The client can’t be used anymore after calling this method,
  54. * you have to either reconnect with `access` or `connect` or instantiate a new instance to continue any work.
  55. * A client is also closed automatically if any timeout or connection error occurs.
  56. */
  57. close(): void;
  58. /**
  59. * Returns true if the client is closed and can't be used anymore.
  60. */
  61. get closed(): boolean;
  62. /**
  63. * Connect (or reconnect) to an FTP server.
  64. *
  65. * This is an instance method and thus can be called multiple times during the lifecycle of a `Client`
  66. * instance. Whenever you do, the client is reset with a new control connection. This also implies that
  67. * you can reopen a `Client` instance that has been closed due to an error when reconnecting with this
  68. * method. In fact, reconnecting is the only way to continue using a closed `Client`.
  69. *
  70. * @param host Host the client should connect to. Optional, default is "localhost".
  71. * @param port Port the client should connect to. Optional, default is 21.
  72. */
  73. connect(host?: string, port?: number): Promise<FTPResponse>;
  74. /**
  75. * As `connect` but using implicit TLS. Implicit TLS is not an FTP standard and has been replaced by
  76. * explicit TLS. There are still FTP servers that support only implicit TLS, though.
  77. */
  78. connectImplicitTLS(host?: string, port?: number, tlsOptions?: TLSConnectionOptions): Promise<FTPResponse>;
  79. /**
  80. * Handles the first reponse by an FTP server after the socket connection has been established.
  81. */
  82. private _handleConnectResponse;
  83. /**
  84. * Send an FTP command and handle the first response.
  85. */
  86. send(command: string, ignoreErrorCodesDEPRECATED?: boolean): Promise<FTPResponse>;
  87. /**
  88. * Send an FTP command and ignore an FTP error response. Any other kind of error or timeout will still reject the Promise.
  89. *
  90. * @param command
  91. */
  92. sendIgnoringError(command: string): Promise<FTPResponse>;
  93. /**
  94. * Upgrade the current socket connection to TLS.
  95. *
  96. * @param options TLS options as in `tls.connect(options)`, optional.
  97. * @param command Set the authentication command. Optional, default is "AUTH TLS".
  98. */
  99. useTLS(options?: TLSConnectionOptions, command?: string): Promise<FTPResponse>;
  100. /**
  101. * Login a user with a password.
  102. *
  103. * @param user Username to use for login. Optional, default is "anonymous".
  104. * @param password Password to use for login. Optional, default is "guest".
  105. */
  106. login(user?: string, password?: string): Promise<FTPResponse>;
  107. /**
  108. * Set the usual default settings.
  109. *
  110. * Settings used:
  111. * * Binary mode (TYPE I)
  112. * * File structure (STRU F)
  113. * * Additional settings for FTPS (PBSZ 0, PROT P)
  114. */
  115. useDefaultSettings(): Promise<void>;
  116. /**
  117. * Convenience method that calls `connect`, `useTLS`, `login` and `useDefaultSettings`.
  118. *
  119. * This is an instance method and thus can be called multiple times during the lifecycle of a `Client`
  120. * instance. Whenever you do, the client is reset with a new control connection. This also implies that
  121. * you can reopen a `Client` instance that has been closed due to an error when reconnecting with this
  122. * method. In fact, reconnecting is the only way to continue using a closed `Client`.
  123. */
  124. access(options?: AccessOptions): Promise<FTPResponse>;
  125. /**
  126. * Get the current working directory.
  127. */
  128. pwd(): Promise<string>;
  129. /**
  130. * Get a description of supported features.
  131. *
  132. * This sends the FEAT command and parses the result into a Map where keys correspond to available commands
  133. * and values hold further information. Be aware that your FTP servers might not support this
  134. * command in which case this method will not throw an exception but just return an empty Map.
  135. */
  136. features(): Promise<Map<string, string>>;
  137. /**
  138. * Set the working directory.
  139. */
  140. cd(path: string): Promise<FTPResponse>;
  141. /**
  142. * Switch to the parent directory of the working directory.
  143. */
  144. cdup(): Promise<FTPResponse>;
  145. /**
  146. * Get the last modified time of a file. This is not supported by every FTP server, in which case
  147. * calling this method will throw an exception.
  148. */
  149. lastMod(path: string): Promise<Date>;
  150. /**
  151. * Get the size of a file.
  152. */
  153. size(path: string): Promise<number>;
  154. /**
  155. * Rename a file.
  156. *
  157. * Depending on the FTP server this might also be used to move a file from one
  158. * directory to another by providing full paths.
  159. */
  160. rename(srcPath: string, destPath: string): Promise<FTPResponse>;
  161. /**
  162. * Remove a file from the current working directory.
  163. *
  164. * You can ignore FTP error return codes which won't throw an exception if e.g.
  165. * the file doesn't exist.
  166. */
  167. remove(path: string, ignoreErrorCodes?: boolean): Promise<FTPResponse>;
  168. /**
  169. * Report transfer progress for any upload or download to a given handler.
  170. *
  171. * This will also reset the overall transfer counter that can be used for multiple transfers. You can
  172. * also call the function without a handler to stop reporting to an earlier one.
  173. *
  174. * @param handler Handler function to call on transfer progress.
  175. */
  176. trackProgress(handler?: ProgressHandler): void;
  177. /**
  178. * Upload data from a readable stream or a local file to a remote file.
  179. *
  180. * @param source Readable stream or path to a local file.
  181. * @param toRemotePath Path to a remote file to write to.
  182. */
  183. uploadFrom(source: Readable | string, toRemotePath: string, options?: UploadOptions): Promise<FTPResponse>;
  184. /**
  185. * Upload data from a readable stream or a local file by appending it to an existing file. If the file doesn't
  186. * exist the FTP server should create it.
  187. *
  188. * @param source Readable stream or path to a local file.
  189. * @param toRemotePath Path to a remote file to write to.
  190. */
  191. appendFrom(source: Readable | string, toRemotePath: string, options?: UploadOptions): Promise<FTPResponse>;
  192. /**
  193. * @protected
  194. */
  195. protected _uploadWithCommand(source: Readable | string, remotePath: string, command: UploadCommand, options: UploadOptions): Promise<FTPResponse>;
  196. /**
  197. * @protected
  198. */
  199. protected _uploadLocalFile(localPath: string, remotePath: string, command: UploadCommand, options: UploadOptions): Promise<FTPResponse>;
  200. /**
  201. * @protected
  202. */
  203. protected _uploadFromStream(source: Readable, remotePath: string, command: UploadCommand): Promise<FTPResponse>;
  204. /**
  205. * Download a remote file and pipe its data to a writable stream or to a local file.
  206. *
  207. * You can optionally define at which position of the remote file you'd like to start
  208. * downloading. If the destination you provide is a file, the offset will be applied
  209. * to it as well. For example: To resume a failed download, you'd request the size of
  210. * the local, partially downloaded file and use that as the offset. Assuming the size
  211. * is 23, you'd download the rest using `downloadTo("local.txt", "remote.txt", 23)`.
  212. *
  213. * @param destination Stream or path for a local file to write to.
  214. * @param fromRemotePath Path of the remote file to read from.
  215. * @param startAt Position within the remote file to start downloading at. If the destination is a file, this offset is also applied to it.
  216. */
  217. downloadTo(destination: Writable | string, fromRemotePath: string, startAt?: number): Promise<FTPResponse>;
  218. /**
  219. * @protected
  220. */
  221. protected _downloadToFile(localPath: string, remotePath: string, startAt: number): Promise<FTPResponse>;
  222. /**
  223. * @protected
  224. */
  225. protected _downloadToStream(destination: Writable, remotePath: string, startAt: number): Promise<FTPResponse>;
  226. /**
  227. * List files and directories in the current working directory, or from `path` if specified.
  228. *
  229. * @param [path] Path to remote file or directory.
  230. */
  231. list(path?: string): Promise<FileInfo[]>;
  232. /**
  233. * @protected
  234. */
  235. protected _requestListWithCommand(command: string): Promise<FileInfo[]>;
  236. /**
  237. * Remove a directory and all of its content.
  238. *
  239. * @param remoteDirPath The path of the remote directory to delete.
  240. * @example client.removeDir("foo") // Remove directory 'foo' using a relative path.
  241. * @example client.removeDir("foo/bar") // Remove directory 'bar' using a relative path.
  242. * @example client.removeDir("/foo/bar") // Remove directory 'bar' using an absolute path.
  243. * @example client.removeDir("/") // Remove everything.
  244. */
  245. removeDir(remoteDirPath: string): Promise<void>;
  246. /**
  247. * Remove all files and directories in the working directory without removing
  248. * the working directory itself.
  249. */
  250. clearWorkingDir(): Promise<void>;
  251. /**
  252. * Upload the contents of a local directory to the remote working directory.
  253. *
  254. * This will overwrite existing files with the same names and reuse existing directories.
  255. * Unrelated files and directories will remain untouched. You can optionally provide a `remoteDirPath`
  256. * to put the contents inside a directory which will be created if necessary including all
  257. * intermediate directories. If you did provide a remoteDirPath the working directory will stay
  258. * the same as before calling this method.
  259. *
  260. * @param localDirPath Local path, e.g. "foo/bar" or "../test"
  261. * @param [remoteDirPath] Remote path of a directory to upload to. Working directory if undefined.
  262. */
  263. uploadFromDir(localDirPath: string, remoteDirPath?: string): Promise<void>;
  264. /**
  265. * @protected
  266. */
  267. protected _uploadToWorkingDir(localDirPath: string): Promise<void>;
  268. /**
  269. * Download all files and directories of the working directory to a local directory.
  270. *
  271. * @param localDirPath The local directory to download to.
  272. * @param remoteDirPath Remote directory to download. Current working directory if not specified.
  273. */
  274. downloadToDir(localDirPath: string, remoteDirPath?: string): Promise<void>;
  275. /**
  276. * @protected
  277. */
  278. protected _downloadFromWorkingDir(localDirPath: string): Promise<void>;
  279. /**
  280. * Make sure a given remote path exists, creating all directories as necessary.
  281. * This function also changes the current working directory to the given path.
  282. */
  283. ensureDir(remoteDirPath: string): Promise<void>;
  284. /**
  285. * Try to create a directory and enter it. This will not raise an exception if the directory
  286. * couldn't be created if for example it already exists.
  287. * @protected
  288. */
  289. protected _openDir(dirName: string): Promise<void>;
  290. /**
  291. * Remove an empty directory, will fail if not empty.
  292. */
  293. removeEmptyDir(path: string): Promise<FTPResponse>;
  294. /**
  295. * FTP servers can't handle filenames that have leading whitespace. This method transforms
  296. * a given path to fix that issue for most cases.
  297. */
  298. protectWhitespace(path: string): Promise<string>;
  299. protected _exitAtCurrentDirectory<T>(func: () => Promise<T>): Promise<T>;
  300. /**
  301. * Try all available transfer strategies and pick the first one that works. Update `client` to
  302. * use the working strategy for all successive transfer requests.
  303. *
  304. * @returns a function that will try the provided strategies.
  305. */
  306. protected _enterFirstCompatibleMode(strategies: TransferStrategy[]): TransferStrategy;
  307. /**
  308. * DEPRECATED, use `uploadFrom`.
  309. * @deprecated
  310. */
  311. upload(source: Readable | string, toRemotePath: string, options?: UploadOptions): Promise<FTPResponse>;
  312. /**
  313. * DEPRECATED, use `appendFrom`.
  314. * @deprecated
  315. */
  316. append(source: Readable | string, toRemotePath: string, options?: UploadOptions): Promise<FTPResponse>;
  317. /**
  318. * DEPRECATED, use `downloadTo`.
  319. * @deprecated
  320. */
  321. download(destination: Writable | string, fromRemotePath: string, startAt?: number): Promise<FTPResponse>;
  322. /**
  323. * DEPRECATED, use `uploadFromDir`.
  324. * @deprecated
  325. */
  326. uploadDir(localDirPath: string, remoteDirPath?: string): Promise<void>;
  327. /**
  328. * DEPRECATED, use `downloadToDir`.
  329. * @deprecated
  330. */
  331. downloadDir(localDirPath: string): Promise<void>;
  332. }