forked from sindresorhus/got
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove cacheable-request, rename pacakge to got-lite
brings npm pkg from 0.75MB to 0.45MB after yarn autoclean --force, down to 0.25MB from this PR: sindresorhus#284 next big savings is removing progress, from sindresorhus#322
- Loading branch information
Showing
5 changed files
with
70 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict'; | ||
|
||
const EventEmitter = require('events'); | ||
|
||
class CacheableRequest { | ||
constructor(request, cacheAdapter) { | ||
if (typeof request !== 'function') { | ||
throw new TypeError('Parameter `request` must be a function'); | ||
} | ||
|
||
return this.createCacheableRequest(request); | ||
} | ||
|
||
createCacheableRequest(request) { | ||
return (opts, cb) => { | ||
const ee = new EventEmitter(); | ||
|
||
const makeRequest = opts => { | ||
const handler = response => { | ||
ee.emit('response', response); | ||
if (typeof cb === 'function') { | ||
cb(response); | ||
} | ||
}; | ||
|
||
try { | ||
const req = request(opts, handler); | ||
ee.emit('request', req); | ||
} catch (err) { | ||
ee.emit('error', new CacheableRequest.RequestError(err)); | ||
} | ||
}; | ||
|
||
process.nextTick(() => makeRequest(opts)) | ||
|
||
return ee; | ||
}; | ||
} | ||
} | ||
|
||
CacheableRequest.RequestError = class extends Error { | ||
constructor(err) { | ||
super(err.message); | ||
this.name = 'RequestError'; | ||
Object.assign(this, err); | ||
} | ||
}; | ||
|
||
CacheableRequest.CacheError = class extends Error { | ||
constructor(err) { | ||
super(err.message); | ||
this.name = 'CacheError'; | ||
Object.assign(this, err); | ||
} | ||
}; | ||
|
||
module.exports = CacheableRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.