basic-ftp
Advanced tools
Comparing version 2.8.2 to 2.8.3
# Changelog | ||
## 2.8.3 | ||
- Improve introduction. | ||
- More unit tests. | ||
## 2.8.2 | ||
@@ -4,0 +9,0 @@ |
@@ -16,2 +16,4 @@ "use strict"; | ||
const LF = "\n"; | ||
/** | ||
@@ -397,3 +399,3 @@ * FTPContext holds the state of an FTP client – its control and data connections – and provides a | ||
// The first and last line wrap the multiline response, ignore them. | ||
res.message.split("\n").slice(1, -1).forEach(line => { | ||
res.message.split(LF).slice(1, -1).forEach(line => { | ||
// A typical lines looks like: " REST STREAM" or " MDTM". | ||
@@ -690,7 +692,7 @@ // Servers might not use an indentation though. | ||
token = ""; | ||
messages.push(lines.slice(startAt, i + 1).join("\n")); | ||
messages.push(lines.slice(startAt, i + 1).join(LF)); | ||
} | ||
} | ||
// The last group might not have been closed, report it as a rest. | ||
const rest = token !== "" ? lines.slice(startAt).join("\n") + "\n" : ""; | ||
const rest = token !== "" ? lines.slice(startAt).join(LF) + LF : ""; | ||
return { messages, rest }; | ||
@@ -697,0 +699,0 @@ } |
{ | ||
"name": "basic-ftp", | ||
"version": "2.8.2", | ||
"version": "2.8.3", | ||
"description": "FTP client for Node.js with support for explicit FTPS over TLS.", | ||
@@ -25,5 +25,5 @@ "main": "./lib/ftp", | ||
"devDependencies": { | ||
"eslint": "^4.17.0", | ||
"mocha": "^5.0.0" | ||
"eslint": "4.18.1", | ||
"mocha": "5.0.1" | ||
} | ||
} |
@@ -22,45 +22,46 @@ # Basic FTP | ||
```js | ||
const ftp = require("basic-ftp"); | ||
const ftp = require("basic-ftp") | ||
async function example() { | ||
const client = new ftp.Client(); | ||
client.ftp.verbose = true; | ||
const client = new ftp.Client() | ||
try { | ||
await client.connect("192.168.0.10", 21); | ||
await client.useTLS(); | ||
await client.login("very", "password"); | ||
await client.useDefaultSettings(); | ||
console.log(await client.list()); | ||
await client.upload(fs.createReadStream("README.md"), "README.md"); | ||
await client.connect("192.168.0.10", 21) | ||
await client.useTLS() | ||
await client.login("very", "password") | ||
await client.useDefaultSettings() | ||
console.log(await client.list()) | ||
await client.upload(fs.createReadStream("README.md"), "README.md") | ||
} | ||
catch(err) { | ||
console.log(err); | ||
console.log(err) | ||
} | ||
client.close(); | ||
client.close() | ||
} | ||
example(); | ||
example() | ||
``` | ||
The example sets the client to be `verbose`. This will log out all communication, making it easier to spot an issue and address it. Why the setting is behind a property `.ftp` will be explained in a later section. | ||
You can also work with directories and their content. The next example makes sure a remote path exists, creating all intermediate directories as necessary. It makes sure the target directory is empty and uploads the contents of a local one. | ||
Here is another example that removes all files and directories recursively. It shows that not all FTP commands are backed by a method. Also, be aware that the FTP protocol doesn't allow parallel requests. | ||
```js | ||
await client.ensureDir("my/remote/path") | ||
await client.clearWorkingDir() | ||
await client.uploadDir("my/local/path") | ||
``` | ||
You can always use Promises instead of async/await. Be aware that the FTP protocol doesn't allow multiple parallel requests. | ||
```js | ||
async clearWorkingDir(client) { | ||
for (const file of await client.list()) { | ||
if (file.isDirectory) { | ||
await client.cd(file.name); | ||
await clearWorkingDir(client); | ||
await client.send("CDUP"); | ||
await client.send("RMD " + file.name); | ||
} | ||
else { | ||
await client.remove(file.name); | ||
} | ||
} | ||
} | ||
client.ensureDir("my/remote/path") | ||
.then(() => client.clearWorkingDir()) | ||
.then(() => client.uploadDir("my/local/path")) | ||
.catch(err => console.log("Oh no!", err)) | ||
``` | ||
If you encounter a problem, it can be helpful to let the client log out all communication with the FTP server. | ||
```js | ||
client.ftp.verbose = true | ||
``` | ||
## Client API | ||
@@ -134,3 +135,3 @@ | ||
Remove all files and directories from a given directory, including the directory itself. | ||
Remove all files and directories from a given directory, including the directory itself. When this task is done, the working directory will be the parent directory of `remoteDirPath`. | ||
@@ -147,3 +148,3 @@ `uploadDir(localDirPath, [remoteDirName]): Promise<void>` | ||
Make 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. The working directory is at `remoteDirPath` after calling this method. | ||
@@ -236,3 +237,3 @@ ## Error Handling | ||
function mySimpleUpload(ftp, readableStream, remoteFilename) { | ||
const command = "STOR " + remoteFilename; | ||
const command = "STOR " + remoteFilename | ||
return ftp.handle(command, (res, task) => { | ||
@@ -243,11 +244,11 @@ if (res.code === 150) { // Ready to upload | ||
else if (res.code === 226) { // Transfer complete | ||
task.resolve(res); | ||
task.resolve(res) | ||
} | ||
else if (res.code >= 400 || res.error) { | ||
task.reject(res); | ||
task.reject(res) | ||
} | ||
}); | ||
}) | ||
} | ||
await mySimpleUpload(client.ftp, myStream, myName); | ||
await mySimpleUpload(client.ftp, myStream, myName) | ||
``` | ||
@@ -254,0 +255,0 @@ |
@@ -90,2 +90,7 @@ const assert = require("assert"); | ||
exp: undefined | ||
}, | ||
{ | ||
title: "Empty list", | ||
list: " \r\n \r\n \r\n ", | ||
exp: [] | ||
} | ||
@@ -92,0 +97,0 @@ ]; |
Sorry, the diff of this file is not supported yet
113400
24
1875
253