diff --git a/cacheable-request-stub.js b/cacheable-request-stub.js new file mode 100644 index 000000000..a540f19d5 --- /dev/null +++ b/cacheable-request-stub.js @@ -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; diff --git a/index.js b/index.js index 8bf06d5a0..0ef152d81 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,7 @@ const Transform = require('stream').Transform; const urlLib = require('url'); const fs = require('fs'); const querystring = require('querystring'); -const CacheableRequest = require('cacheable-request'); +const CacheableRequest = require('./cacheable-request-stub'); const duplexer3 = require('duplexer3'); const intoStream = require('into-stream'); const is = require('@sindresorhus/is'); diff --git a/package.json b/package.json index 4be16a241..5aedcd6a7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "got", - "version": "8.0.0", + "name": "got-lite", + "version": "8.0.1", "description": "Simplified HTTP requests", "license": "MIT", "repository": "sindresorhus/got", @@ -29,7 +29,8 @@ "coveralls": "nyc report --reporter=text-lcov | coveralls" }, "files": [ - "index.js" + "index.js", + "cacheable-request-stub.js" ], "keywords": [ "http", @@ -51,7 +52,6 @@ ], "dependencies": { "@sindresorhus/is": "^0.6.0", - "cacheable-request": "^2.1.1", "decompress-response": "^3.3.0", "duplexer3": "^0.1.4", "get-stream": "^3.0.0", diff --git a/readme.md b/readme.md index 79fb726f5..b3e59d7d7 100644 --- a/readme.md +++ b/readme.md @@ -1,3 +1,11 @@ +## This is a fork of got that reduces npm package size + +This fork removes cacheable-request, introduced in https://github.com/sindresorhus/got/pull/284 bringing the npm package from 0.75MB to 0.45MB. + +Run `yarn autoclean --init && yarn autoclean --force` to bring it down even more to 0.25MB + +The use case is Lambda@Edge or other environments where code size is important +


got diff --git a/test/cache.js b/test/cache.js deleted file mode 100644 index 7148f21ef..000000000 --- a/test/cache.js +++ /dev/null @@ -1,107 +0,0 @@ -import test from 'ava'; -import got from '..'; -import {createServer} from './helpers/server'; - -let s; - -test.before('setup', async () => { - s = await createServer(); - - let noStoreIndex = 0; - s.on('/no-store', (req, res) => { - res.setHeader('Cache-Control', 'public, no-cache, no-store'); - res.end(noStoreIndex.toString()); - noStoreIndex++; - }); - - let cacheIndex = 0; - s.on('/cache', (req, res) => { - res.setHeader('Cache-Control', 'public, max-age=60'); - res.end(cacheIndex.toString()); - cacheIndex++; - }); - - let status301Index = 0; - s.on('/301', (req, res) => { - if (status301Index === 0) { - res.setHeader('Cache-Control', 'public, max-age=60'); - res.setHeader('Location', s.url + '/302'); - res.statusCode = 301; - } - res.end(); - status301Index++; - }); - - let status302Index = 0; - s.on('/302', (req, res) => { - if (status302Index === 0) { - res.setHeader('Cache-Control', 'public, max-age=60'); - res.setHeader('Location', s.url + '/cache'); - res.statusCode = 302; - } - res.end(); - status302Index++; - }); - - await s.listen(s.port); -}); - -test('Non cacheable responses are not cached', async t => { - const endpoint = '/no-store'; - const cache = new Map(); - - const firstResponseInt = Number((await got(s.url + endpoint, {cache})).body); - const secondResponseInt = Number((await got(s.url + endpoint, {cache})).body); - - t.is(cache.size, 0); - t.true(firstResponseInt < secondResponseInt); -}); - -test('Cacheable responses are cached', async t => { - const endpoint = '/cache'; - const cache = new Map(); - - const firstResponse = await got(s.url + endpoint, {cache}); - const secondResponse = await got(s.url + endpoint, {cache}); - - t.is(cache.size, 1); - t.is(firstResponse.body, secondResponse.body); -}); - -test('Cached response is re-encoded to current encoding option', async t => { - const endpoint = '/cache'; - const cache = new Map(); - const firstEncoding = 'base64'; - const secondEncoding = 'hex'; - - const firstResponse = await got(s.url + endpoint, {cache, encoding: firstEncoding}); - const secondResponse = await got(s.url + endpoint, {cache, encoding: secondEncoding}); - - const expectedSecondResponseBody = Buffer.from(firstResponse.body, firstEncoding).toString(secondEncoding); - - t.is(cache.size, 1); - t.is(secondResponse.body, expectedSecondResponseBody); -}); - -test('Redirects are cached and re-used internally', async t => { - const endpoint = '/301'; - const cache = new Map(); - - const firstResponse = await got(s.url + endpoint, {cache}); - const secondResponse = await got(s.url + endpoint, {cache}); - - t.is(cache.size, 3); - t.is(firstResponse.body, secondResponse.body); -}); - -test('Cache error throws got.CacheError', async t => { - const endpoint = '/no-store'; - const cache = {}; - - const err = await t.throws(got(s.url + endpoint, {cache})); - t.is(err.name, 'CacheError'); -}); - -test.after('cleanup', async () => { - await s.close(); -});