basic-ftp
Advanced tools
Comparing version 1.0.9 to 1.1.0
# Changelog | ||
## 1.1.0 | ||
- Add convenience functions to request and change the current working directory. | ||
- Return positive response results whenever reasonable. | ||
## 1.0.9 | ||
@@ -4,0 +9,0 @@ |
@@ -176,2 +176,4 @@ "use strict"; | ||
useDefaultSettings, | ||
cd, | ||
pwd, | ||
// Useful for custom extensions | ||
@@ -183,2 +185,15 @@ parseIPV4PasvResponse, | ||
/** | ||
* @typedef {Object} PositiveResponse | ||
* @property {number} code The FTP return code parsed from the FTP return message. | ||
* @property {string} message The whole unparsed FTP return message. | ||
*/ | ||
/** | ||
* @typedef {Object} NegativeResponse | ||
* @property {Object|string} error The error description. | ||
* | ||
* Negative responses are usually thrown as exceptions, not returned as values. | ||
*/ | ||
/** | ||
* Connect to an FTP server. | ||
@@ -189,3 +204,3 @@ * | ||
* @param {number} port | ||
* @return {Promise<void>} | ||
* @return {Promise<PositiveResponse>} | ||
*/ | ||
@@ -196,3 +211,3 @@ function connect(client, host, port) { | ||
if (res.code === 220) { | ||
task.resolve(); | ||
task.resolve(res); | ||
} | ||
@@ -206,3 +221,4 @@ else { | ||
/** | ||
* Send an FTP command. | ||
* Send an FTP command. If successful it will return a response object that contains | ||
* the return code as well as the whole message. | ||
* | ||
@@ -212,3 +228,3 @@ * @param {Client} client | ||
* @param {boolean} ignoreError | ||
* @return {Promise<number>} | ||
* @return {Promise<PositiveResponse>} | ||
*/ | ||
@@ -232,3 +248,3 @@ function send(client, command, ignoreErrorCodes = false) { | ||
* @param {Object} options Same options as in `tls.connect(options)` | ||
* @return {Promise<void>} | ||
* @return {Promise<PositiveResponse>} | ||
*/ | ||
@@ -242,3 +258,3 @@ function useTLS(client, options) { | ||
client.tlsOptions = options; // Keep the TLS options for later data connections that should use the same options. | ||
task.resolve(); | ||
task.resolve(res); | ||
}).catch(err => task.reject(err)); | ||
@@ -257,2 +273,3 @@ } | ||
* @param {Object} options Same options as in `tls.connect(options)` | ||
* @returns {Promise<TLSSocket>} | ||
*/ | ||
@@ -280,3 +297,3 @@ function upgradeSocket(socket, options) { | ||
* @param {Client} client | ||
* @return {Promise<void>} | ||
* @returns {Promise<PositiveResponse>} | ||
*/ | ||
@@ -307,3 +324,3 @@ function enterPassiveMode(client, parsePasvResponse = parseIPV4PasvResponse) { | ||
client.dataSocket = socket; | ||
task.resolve(); | ||
task.resolve(res); | ||
}); | ||
@@ -386,3 +403,3 @@ } | ||
* @param {string} remoteFilename | ||
* @returns {Promise<void>} | ||
* @returns {Promise<PositiveResponse>} | ||
*/ | ||
@@ -399,3 +416,3 @@ function upload(client, readableStream, remoteFilename) { | ||
else if (res.code === 226) { // Transfer complete | ||
task.resolve(); | ||
task.resolve(res); | ||
} | ||
@@ -418,3 +435,3 @@ else if (res.code > 400 || res.error) { | ||
* @param {number} startAt | ||
* @returns {Promise<void>} | ||
* @returns {Promise<PositiveResponse>} | ||
*/ | ||
@@ -432,3 +449,3 @@ function download(client, writableStream, remoteFilename, startAt = 0) { | ||
client.dataSocket = undefined; | ||
task.resolve(); | ||
task.resolve(res); | ||
} | ||
@@ -448,6 +465,7 @@ else if (res.code > 400 || res.error) { | ||
* @param {string} password | ||
* @returns {Promise<PositiveResponse>} | ||
*/ | ||
async function login(client, user, password) { | ||
await send(client, "USER " + user); | ||
await send(client, "PASS " + password); | ||
return await send(client, "PASS " + password); | ||
} | ||
@@ -468,1 +486,26 @@ | ||
} | ||
/** | ||
* Change the working directory. | ||
* | ||
* @param {Client} client | ||
* @param {string} remotePath | ||
* @returns {Promise<PositiveResponse>} | ||
*/ | ||
function cd(client, remotePath) { | ||
return send(client, "CWD " + remotePath); | ||
} | ||
/** | ||
* Get the current working directory. | ||
* | ||
* @param {Client} client | ||
* @returns {Promise<string>} | ||
*/ | ||
async function pwd(client) { | ||
const res = await send(client, "PWD"); | ||
// The directory is part of the return message, for example: | ||
// 257 "/this/that" is current directory. | ||
return res.message.match(/"(.+)"/)[1]; | ||
} | ||
{ | ||
"name": "basic-ftp", | ||
"version": "1.0.9", | ||
"version": "1.1.0", | ||
"description": "FTP/FTPS client library", | ||
@@ -5,0 +5,0 @@ "main": "./lib/ftp", |
63819
733