<commit_before># node-miniget

A small http(s) GET library with redirects, retries, reconnects, concatenating or streaming, and no dependencies. This keeps filesize small for potential browser use.

[![Build Status](https://secure.travis-ci.org/fent/node-miniget.svg)](http://travis-ci.org/fent/node-miniget)
[![Dependency Status](https://david-dm.org/fent/node-miniget.svg)](https://david-dm.org/fent/node-miniget)
[![codecov](https://codecov.io/gh/fent/node-miniget/branch/master/graph/badge.svg)](https://codecov.io/gh/fent/node-miniget)


# Usage

Concatenates a response

```js
const miniget = require('miniget');

miniget('http://mywebsite.com', (err, body) => {
  console.log('webpage contents: ', body);
}));
```

Request can be streamed right away

```js
miniget('http://api.mywebsite.com/v1/messages.json')
  .pipe(someWritableStream());
```


# API

### miniget(url, [options], [callback(err, body)])

Makes a GET request. `options` can have any properties from the [`http.request()` function](https://nodejs.org/api/http.html#http_http_request_options_callback), in addition to

* `maxRedirects` - Default is `2`.
* `maxRetries` - Number of times to retry the request if there is a 500 or connection error. Default is `1`.
* `maxReconnects` - During a big download, if there is a disconnect, miniget can try to reconnect and continue the download where it left off. Defaults to `0`.
* `backoff` - An object with `inc` and `max` used to calculate how long to wait to retry a request. Defaults to `{ inc: 100, max: 10000 }`.
* `highWaterMark` - Amount of data to buffer when in stream mode.
* `transform` - Use this to add additional features. Called with the object that `http.get()` or `https.get()` would be called with. Must return a transformed object.

If `callback` is given, will concatenate the response, and call `callback` with a possible error, and the response body.

Miniget returns a readable stream if `callback` is not given, errors will then be emitted on the stream. Returned stream also contains an `.abort()` method.


# Install

    npm install miniget


# Tests
Tests are written with [mocha](https://mochajs.org)

```bash
npm test
```<commit_msg>docs: add events<commit_after># node-miniget

A small http(s) GET library with redirects, retries, reconnects, concatenating or streaming, and no dependencies. This keeps filesize small for potential browser use.

[![Build Status](https://secure.travis-ci.org/fent/node-miniget.svg)](http://travis-ci.org/fent/node-miniget)
[![Dependency Status](https://david-dm.org/fent/node-miniget.svg)](https://david-dm.org/fent/node-miniget)
[![codecov](https://codecov.io/gh/fent/node-miniget/branch/master/graph/badge.svg)](https://codecov.io/gh/fent/node-miniget)


# Usage

Concatenates a response

```js
const miniget = require('miniget');

miniget('http://mywebsite.com', (err, body) => {
  console.log('webpage contents: ', body);
}));
```

Request can be streamed right away

```js
miniget('http://api.mywebsite.com/v1/messages.json')
  .pipe(someWritableStream());
```


# API

### miniget(url, [options], [callback(err, body)])

Makes a GET request. `options` can have any properties from the [`http.request()` function](https://nodejs.org/api/http.html#http_http_request_options_callback), in addition to

* `maxRedirects` - Default is `2`.
* `maxRetries` - Number of times to retry the request if there is a 500 or connection error. Default is `1`.
* `maxReconnects` - During a big download, if there is a disconnect, miniget can try to reconnect and continue the download where it left off. Defaults to `0`.
* `backoff` - An object with `inc` and `max` used to calculate how long to wait to retry a request. Defaults to `{ inc: 100, max: 10000 }`.
* `highWaterMark` - Amount of data to buffer when in stream mode.
* `transform` - Use this to add additional features. Called with the object that `http.get()` or `https.get()` would be called with. Must return a transformed object.

If `callback` is given, will concatenate the response, and call `callback` with a possible error, and the response body.

Miniget returns a readable stream if `callback` is not given, errors will then be emitted on the stream. Returned stream also contains an `.abort()` method, and can emit the following events.

#### Event: redirect
* `string` - URL redirected to.

Emitted when the request was redirected with a redirection status code.

#### Event: retry
* `number` - Number of retry.
* `Error` - Request or status code error.

Emitted when the request fails, or the response has a status code >= 500.

#### Event: reconnect
* `number` - Number of reconnect.
* `Error` - Request or response error.

Emitted when the request or response fails after download has started.


# Install

    npm install miniget


# Tests
Tests are written with [mocha](https://mochajs.org)

```bash
npm test
```
{'fraction_non_alphanumeric': 0.13047952253550113, 'fraction_numerical': 0.006791520889071825, 'mean_word_length': 4.448430493273543, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 14, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1706097', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1503, 'n_words': 548}