FtpContext.d.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /// <reference types="node" />
  2. /// <reference types="node" />
  3. /// <reference types="node" />
  4. import { Socket } from "net";
  5. import { ConnectionOptions as TLSConnectionOptions, TLSSocket } from "tls";
  6. import { StringEncoding } from "./StringEncoding";
  7. interface Task {
  8. /** Handles a response for a task. */
  9. readonly responseHandler: ResponseHandler;
  10. /** Resolves or rejects a task. */
  11. readonly resolver: TaskResolver;
  12. /** Call stack when task was run. */
  13. readonly stack: string;
  14. }
  15. export interface TaskResolver {
  16. resolve(args: any): void;
  17. reject(err: Error): void;
  18. }
  19. export interface FTPResponse {
  20. /** FTP response code */
  21. readonly code: number;
  22. /** Whole response including response code */
  23. readonly message: string;
  24. }
  25. export type ResponseHandler = (response: Error | FTPResponse, task: TaskResolver) => void;
  26. /**
  27. * Describes an FTP server error response including the FTP response code.
  28. */
  29. export declare class FTPError extends Error {
  30. /** FTP response code */
  31. readonly code: number;
  32. constructor(res: FTPResponse);
  33. }
  34. /**
  35. * FTPContext holds the control and data sockets of an FTP connection and provides a
  36. * simplified way to interact with an FTP server, handle responses, errors and timeouts.
  37. *
  38. * It doesn't implement or use any FTP commands. It's only a foundation to make writing an FTP
  39. * client as easy as possible. You won't usually instantiate this, but use `Client`.
  40. */
  41. export declare class FTPContext {
  42. readonly timeout: number;
  43. /** Debug-level logging of all socket communication. */
  44. verbose: boolean;
  45. /** IP version to prefer (4: IPv4, 6: IPv6, undefined: automatic). */
  46. ipFamily: number | undefined;
  47. /** Options for TLS connections. */
  48. tlsOptions: TLSConnectionOptions;
  49. /** Current task to be resolved or rejected. */
  50. protected _task: Task | undefined;
  51. /** A multiline response might be received as multiple chunks. */
  52. protected _partialResponse: string;
  53. /** The reason why a context has been closed. */
  54. protected _closingError: NodeJS.ErrnoException | undefined;
  55. /** Encoding supported by Node applied to commands, responses and directory listing data. */
  56. protected _encoding: StringEncoding;
  57. /** FTP control connection */
  58. protected _socket: Socket | TLSSocket;
  59. /** FTP data connection */
  60. protected _dataSocket: Socket | TLSSocket | undefined;
  61. /**
  62. * Instantiate an FTP context.
  63. *
  64. * @param timeout - Timeout in milliseconds to apply to control and data connections. Use 0 for no timeout.
  65. * @param encoding - Encoding to use for control connection. UTF-8 by default. Use "latin1" for older servers.
  66. */
  67. constructor(timeout?: number, encoding?: StringEncoding);
  68. /**
  69. * Close the context.
  70. */
  71. close(): void;
  72. /**
  73. * Close the context with an error.
  74. */
  75. closeWithError(err: Error): void;
  76. /**
  77. * Returns true if this context has been closed or hasn't been connected yet. You can reopen it with `access`.
  78. */
  79. get closed(): boolean;
  80. /**
  81. * Reset this contex and all of its state.
  82. */
  83. reset(): void;
  84. /**
  85. * Get the FTP control socket.
  86. */
  87. get socket(): Socket | TLSSocket;
  88. /**
  89. * Set the socket for the control connection. This will only close the current control socket
  90. * if the new one is not an upgrade to the current one.
  91. */
  92. set socket(socket: Socket | TLSSocket);
  93. /**
  94. * Get the current FTP data connection if present.
  95. */
  96. get dataSocket(): Socket | TLSSocket | undefined;
  97. /**
  98. * Set the socket for the data connection. This will automatically close the former data socket.
  99. */
  100. set dataSocket(socket: Socket | TLSSocket | undefined);
  101. /**
  102. * Get the currently used encoding.
  103. */
  104. get encoding(): StringEncoding;
  105. /**
  106. * Set the encoding used for the control socket.
  107. *
  108. * See https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings for what encodings
  109. * are supported by Node.
  110. */
  111. set encoding(encoding: StringEncoding);
  112. /**
  113. * Send an FTP command without waiting for or handling the result.
  114. */
  115. send(command: string): void;
  116. /**
  117. * Send an FTP command and handle the first response. Use this if you have a simple
  118. * request-response situation.
  119. */
  120. request(command: string): Promise<FTPResponse>;
  121. /**
  122. * Send an FTP command and handle any response until you resolve/reject. Use this if you expect multiple responses
  123. * to a request. This returns a Promise that will hold whatever the response handler passed on when resolving/rejecting its task.
  124. */
  125. handle(command: string | undefined, responseHandler: ResponseHandler): Promise<any>;
  126. /**
  127. * Log message if set to be verbose.
  128. */
  129. log(message: string): void;
  130. /**
  131. * Return true if the control socket is using TLS. This does not mean that a session
  132. * has already been negotiated.
  133. */
  134. get hasTLS(): boolean;
  135. /**
  136. * Removes reference to current task and handler. This won't resolve or reject the task.
  137. * @protected
  138. */
  139. protected _stopTrackingTask(): void;
  140. /**
  141. * Handle incoming data on the control socket. The chunk is going to be of type `string`
  142. * because we let `socket` handle encoding with `setEncoding`.
  143. * @protected
  144. */
  145. protected _onControlSocketData(chunk: string): void;
  146. /**
  147. * Send the current handler a response. This is usually a control socket response
  148. * or a socket event, like an error or timeout.
  149. * @protected
  150. */
  151. protected _passToHandler(response: Error | FTPResponse): void;
  152. /**
  153. * Setup all error handlers for a socket.
  154. * @protected
  155. */
  156. protected _setupDefaultErrorHandlers(socket: Socket, identifier: string): void;
  157. /**
  158. * Close the control socket. Sends QUIT, then FIN, and ignores any response or error.
  159. */
  160. protected _closeControlSocket(): void;
  161. /**
  162. * Close a socket, ignores any error.
  163. * @protected
  164. */
  165. protected _closeSocket(socket: Socket | undefined): void;
  166. /**
  167. * Remove all default listeners for socket.
  168. * @protected
  169. */
  170. protected _removeSocketListeners(socket: Socket): void;
  171. /**
  172. * Provide a new socket instance.
  173. *
  174. * Internal use only, replaced for unit tests.
  175. */
  176. _newSocket(): Socket;
  177. }
  178. export {};