Skip to content

Commit

Permalink
Revert "Add cache option (sindresorhus#284)"
Browse files Browse the repository at this point in the history
This reverts commit 3c79205.
  • Loading branch information
lukechilds committed Nov 14, 2017
1 parent 0c5e44c commit a9570d4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 236 deletions.
104 changes: 45 additions & 59 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const Transform = require('stream').Transform;
const urlLib = require('url');
const fs = require('fs');
const querystring = require('querystring');
const CacheableRequest = require('cacheable-request');
const duplexer3 = require('duplexer3');
const intoStream = require('into-stream');
const isStream = require('is-stream');
Expand Down Expand Up @@ -94,8 +93,7 @@ function requestAsEventEmitter(opts) {

let progressInterval;

const cacheableRequest = new CacheableRequest(fn.request, opts.cache);
const cacheReq = cacheableRequest(opts, res => {
const req = fn.request(opts, res => {
clearInterval(progressInterval);

ee.emit('uploadProgress', {
Expand Down Expand Up @@ -180,7 +178,7 @@ function requestAsEventEmitter(opts) {

const response = opts.decompress === true &&
typeof decompressResponse === 'function' &&
opts.method !== 'HEAD' ? decompressResponse(progressStream) : progressStream;
req.method !== 'HEAD' ? decompressResponse(progressStream) : progressStream;

if (!opts.decompress && ['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) {
opts.encoding = null;
Expand All @@ -198,66 +196,62 @@ function requestAsEventEmitter(opts) {
});
});

cacheReq.on('error', err => ee.emit('error', new got.CacheError(err, opts)));
req.once('error', err => {
clearInterval(progressInterval);

cacheReq.on('request', req => {
req.once('error', err => {
clearInterval(progressInterval);
const backoff = opts.retries(++retryCount, err);

const backoff = opts.retries(++retryCount, err);
if (backoff) {
setTimeout(get, backoff, opts);
return;
}

if (backoff) {
setTimeout(get, backoff, opts);
return;
}
ee.emit('error', new got.RequestError(err, opts));
});

ee.emit('error', new got.RequestError(err, opts));
ee.on('request', req => {
ee.emit('uploadProgress', {
percent: 0,
transferred: 0,
total: uploadBodySize
});

ee.on('request', req => {
ee.emit('uploadProgress', {
percent: 0,
transferred: 0,
total: uploadBodySize
});
req.connection.once('connect', () => {
const uploadEventFrequency = 150;

req.connection.once('connect', () => {
const uploadEventFrequency = 150;
progressInterval = setInterval(() => {
const lastUploaded = uploaded;
const headersSize = Buffer.byteLength(req._header);
uploaded = req.connection.bytesWritten - headersSize;

progressInterval = setInterval(() => {
const lastUploaded = uploaded;
const headersSize = Buffer.byteLength(req._header);
uploaded = req.connection.bytesWritten - headersSize;

// Prevent the known issue of `bytesWritten` being larger than body size
if (uploadBodySize && uploaded > uploadBodySize) {
uploaded = uploadBodySize;
}
// Prevent the known issue of `bytesWritten` being larger than body size
if (uploadBodySize && uploaded > uploadBodySize) {
uploaded = uploadBodySize;
}

// Don't emit events with unchanged progress and
// prevent last event from being emitted, because
// it's emitted when `response` is emitted
if (uploaded === lastUploaded || uploaded === uploadBodySize) {
return;
}
// Don't emit events with unchanged progress and
// prevent last event from being emitted, because
// it's emitted when `response` is emitted
if (uploaded === lastUploaded || uploaded === uploadBodySize) {
return;
}

ee.emit('uploadProgress', {
percent: uploadBodySize ? uploaded / uploadBodySize : 0,
transferred: uploaded,
total: uploadBodySize
});
}, uploadEventFrequency);
});
ee.emit('uploadProgress', {
percent: uploadBodySize ? uploaded / uploadBodySize : 0,
transferred: uploaded,
total: uploadBodySize
});
}, uploadEventFrequency);
});
});

if (opts.gotTimeout) {
clearInterval(progressInterval);
timedOut(req, opts.gotTimeout);
}
if (opts.gotTimeout) {
clearInterval(progressInterval);
timedOut(req, opts.gotTimeout);
}

setImmediate(() => {
ee.emit('request', req);
});
setImmediate(() => {
ee.emit('request', req);
});
};

Expand Down Expand Up @@ -447,7 +441,6 @@ function normalizeArguments(url, opts) {
{
path: '',
retries: 2,
cache: false,
decompress: true,
useElectronNet: false
},
Expand Down Expand Up @@ -603,13 +596,6 @@ class StdError extends Error {
}
}

got.CacheError = class extends StdError {
constructor(error, opts) {
super(error.message, error, opts);
this.name = 'CacheError';
}
};

got.RequestError = class extends StdError {
constructor(error, opts) {
super(error.message, error, opts);
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"electron"
],
"dependencies": {
"cacheable-request": "^2.0.0",
"decompress-response": "^3.2.0",
"duplexer3": "^0.1.4",
"get-stream": "^3.0.0",
Expand Down
69 changes: 0 additions & 69 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Created because [`request`](https://github.com/request/request) is bloated *(sev

- [Promise & stream API](#api)
- [Request cancelation](#aborting-the-request)
- [RFC compliant caching](#cache-adapters)
- [Follows redirects](#followredirect)
- [Retries on network failure](#retries)
- [Progress events](#onuploadprogress-progress)
Expand Down Expand Up @@ -70,10 +69,6 @@ It's a `GET` request by default, but can be changed in `options`.

Returns a Promise for a `response` object with a `body` property, a `url` property with the request URL or the final URL after redirects, and a `requestUrl` property with the original request URL.

The response object will normally be a [Node.js HTTP response stream](https://nodejs.org/api/http.html#http_class_http_incomingmessage), however if returned from the cache it will be a [responselike object](https://github.com/lukechilds/responselike) which behaves in the same way.

The response will also have a `fromCache` property set with a boolean value.

##### url

Type: `string` `Object`
Expand Down Expand Up @@ -177,13 +172,6 @@ Decompress the response automatically.

If this is disabled, a compressed response is returned as a `Buffer`. This may be useful if you want to handle decompression yourself or stream the raw compressed data.

###### cache

Type: `Object`<br>
Default: `false`

[Cache adapter instance](#cache-adapters) for storing cached data.

###### useElectronNet

Type: `boolean`<br>
Expand Down Expand Up @@ -267,10 +255,6 @@ Each error contains (if available) `statusCode`, `statusMessage`, `host`, `hostn

In Promise mode, the `response` is attached to the error.

#### got.CacheError

When a cache method fails, for example if the database goes down, or there's a filesystem error.

#### got.RequestError

When a request fails. Contains a `code` property with error class code, like `ECONNREFUSED`.
Expand Down Expand Up @@ -335,59 +319,6 @@ request.cancel();
```


<a name="cache-adapters"></a>
## Cache

You can use the JavaScript `Map` type as an in memory cache:

```js
const got = require('got');
const map = new Map();

(async () => {
let response = await got('sindresorhus.com', {cache: map});
console.log(response.fromCache);
//=> false

response = await got('sindresorhus.com', {cache: map});
console.log(response.fromCache);
//=> true
})();
```

Got uses [Keyv](https://github.com/lukechilds/keyv) internally to support a wide range of storage adapters. For something more scalable you could use an [official Keyv storage adapter](https://github.com/lukechilds/keyv#official-storage-adapters):

```
$ npm install @keyv/redis
```

```js
const got = require('got');
const KeyvRedis = require('@keyv/redis');

const redis = new KeyvRedis('redis://user:pass@localhost:6379');

got('sindresorhus.com', {cache: redis});
```

Got supports anything that follows the Map API, so it's easy to write your own storage adapter or use a third-party solution.

For example, the following are all valid storage adapters:

```js
const storageAdapter = new Map();
// or
const storageAdapter = require('./my-storage-adapter');
// or
const QuickLRU = require('quick-lru');
const storageAdapter = new QuickLRU({maxSize: 1000});

got('sindresorhus.com', {cache: storageAdapter});
```

View the [Keyv docs](https://github.com/lukechilds/keyv) for more information on how to use storage adapters.


## Proxies

You can use the [`tunnel`](https://github.com/koichik/node-tunnel) module with the `agent` option to work with proxies:
Expand Down
107 changes: 0 additions & 107 deletions test/cache.js

This file was deleted.

0 comments on commit a9570d4

Please sign in to comment.