Skip to content

Commit

Permalink
Add features() method
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickjuchli committed Jan 13, 2018
1 parent 90b23ab commit 0b94f44
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 2.3.0

- Add features() method to client that returns a parsed result of the FEAT command.
- Give access to internal list, upload and download functions for reuse in custom contexts.

## 2.2.1

- Handle case when downloading, a server might report transfer complete when it isn't.
Expand Down
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ Changes the working directory.

`pwd()`

Returns the path of the current working directory.
Get the path of the current working directory.

`features()`

Get a description of supported features. This will return a Map where keys correspond to FTP commands and values contain further details.

`list()`

Expand All @@ -99,23 +103,23 @@ Download a file with a given filename from the current working directory and pip

`removeDir(remoteDirPath)`

Removes a directory at a given path, including all of its files and directories.
Remove a directory at a given path, including all of its files and directories.

`clearWorkingDir()`

Removes all files and directories from the working directory.
Remove all files and directories from the working directory.

`uploadDir(localDirPath, remoteDirName = undefined)`

Uploads all files and directories of a local directory to the current working directory. If you specify a `remoteDirName` it will place the uploads inside a directory of the given name.
Upload all files and directories of a local directory to the current working directory. If you specify a `remoteDirName` it will place the uploads inside a directory of the given name.

`downloadDir(localDirPath)`

Downloads all files and directories of the current working directory to a given local directory.
Download all files and directories of the current working directory to a given local directory.

`ensureDir(remoteDirPath)`

Makes sure that the given `remoteDirPath` exists on the server, creating all directories as necessary.
Make sure that the given `remoteDirPath` exists on the server, creating all directories as necessary.

## Customize

Expand Down
32 changes: 30 additions & 2 deletions lib/ftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,31 @@ class Client {
return res.message.match(/"(.+)"/)[1];
}

/**
* Get a description of supported features.
*
* This sends the FEAT command and parses the result into a Map where keys correspond to available commands
* and values hold further information. Be aware that your FTP servers might not support this
* command in which case this method will not throw an exception but just return an empty Map.
*
* @returns {Map<string, string>}
*/
async features() {
const res = await this.send("FEAT", true);
let features = new Map();
// Not supporting any special features will be reported with a single line.
if (res.code < 400 && isMultiline(res.message)) {
// The first and last line wrap the multiline response, ignore them.
res.message.split("\n").slice(1, -1).map(line => {
// A typical lines looks like: " REST STREAM" or " MDTM".
// Servers might not use an indentation though.
const entry = line.trim().split(" ");
features.set(entry[0], entry[1] || "");
});
}
return features;
}

/**
* Upload data from a readable stream and store it as a file with
* a given filename in the current working directory.
Expand Down Expand Up @@ -752,8 +777,7 @@ function groupControlResponse(text) {
const line = lines[i];
// No group has been opened.
if (token === "") {
const isMultiline = line.charAt(3) === "-";
if (isMultiline) {
if (isMultiline(line)) {
token = line.substr(0, 3) + " ";
startAt = i;
}
Expand All @@ -770,4 +794,8 @@ function groupControlResponse(text) {
// The last group might not have been closed, report it as a rest.
const rest = token !== "" ? lines.slice(startAt).join("\n") + "\n" : "";
return { groups, rest };
}

function isMultiline(line) {
return line.charAt(3) === "-";
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "basic-ftp",
"version": "2.2.1",
"version": "2.3.0",
"description": "FTP/FTPS client library",
"main": "./lib/ftp",
"scripts": {
Expand Down

0 comments on commit 0b94f44

Please sign in to comment.