Skip to content

Commit

Permalink
👷 Add .pipe() method
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-dimitru committed May 4, 2020
1 parent 00eee20 commit d43a223
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ __This is a server-only package.__ This package was created due to a lack of sta
- 🛡 Repeat (*built-in retries*) request on failed or broken connection;
- 😎 HTTP/2 support;
- 🎒 Send GET/POST with custom `body` and headers;
- 🗂 Pipe to the file;
- 🚦 Follow or deny redirects;
- 📤 [Upload files with a single line](https://github.com/VeliovGroup/request-extra#file-upload);
- 🔐 Ignore or deny "broken" SSL/TLS certificates;
Expand All @@ -41,6 +42,7 @@ __This is a server-only package.__ This package was created due to a lack of sta
- [GET](https://github.com/VeliovGroup/request-extra#get-request)
- [POST](https://github.com/VeliovGroup/request-extra#post-request)
- [POST (*advanced*)](https://github.com/VeliovGroup/request-extra#post-request-with-extra-options)
- [File download via `.pipe()`](https://github.com/VeliovGroup/request-extra#file-download)
- [File upload](https://github.com/VeliovGroup/request-extra#file-upload)
- [File upload (*multipart*)](https://github.com/VeliovGroup/request-extra#file-upload-multipartform-data)
- [Known Issues](https://github.com/VeliovGroup/request-extra#known-issues)
Expand Down Expand Up @@ -340,6 +342,32 @@ request({
});
```

### File download

Download a file to the FileSystem using `.pipe()` method:

```js
const fs = require('fs');
const request = require('request-libcurl');
const req = request({
url: 'https://example.com/file.pdf',
wait: true
}, (error, resp) => {
if (error) {
throw error;
} else {
// File successfully downloaded
fs.stat('/path/to/file.pdf', (error, stats) => {
// do something with downloaded file
});
}
});
req.pipe(fs.createWriteStream('/path/to/file.pdf', {flags: 'w'}));
req.send();
```

### File upload

```js
Expand Down
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,21 @@ const sendRequest = (libcurl, url, cb) => {
curl.on('header', libcurl.onHeader);
}

if (libcurl.pipeTo) {
curl.on('data', (data) => {
libcurl.pipeTo.write(data);
});
}

curl.on('end', (statusCode, body, _headers) => {
libcurl._debug('[END EVENT]', opts.retries, url.href, finished, statusCode);
stopRequestTimeout();
if (finished) { return; }

finished = true;
if (libcurl.pipeTo) {
libcurl.pipeTo.end();
}
curl.close();

const headers = {};
Expand Down Expand Up @@ -335,12 +344,23 @@ class LibCurlRequest {
}
}

pipe(writableStream) {
if (writableStream.write && writableStream.end) {
this.pipeTo = writableStream;
} else {
throw new TypeError('[request-libcurl] [.pipe()] method accepts only {stream.Writable}');
}
return this;
}

onData(callback) {
this.onData = callback;
return this;
}

onHeader(callback) {
this.onHeader = callback;
return this;
}

_retry() {
Expand Down

0 comments on commit d43a223

Please sign in to comment.