Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

Commit

Permalink
Rename to @vercel/fetch (#50)
Browse files Browse the repository at this point in the history
* Rename to `@vercel/fetch`

* Run `yarn upgrade`

* Use `@vercel/fetch-retry`

* Use `@vercel/fetch-cached-dns`

* Update `@vercel/fetch-cached-dns` to v2.0.1
  • Loading branch information
TooTallNate authored Oct 16, 2020
1 parent 516c1c6 commit 0bd5870
Show file tree
Hide file tree
Showing 6 changed files with 594 additions and 375 deletions.
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export interface RetryOptions extends BaseRetryOptions {
}

export type FetchOptions = RequestInit & {
agent?: https.Agent | http.Agent
retry?: RetryOptions
}
agent?: https.Agent | http.Agent;
retry?: RetryOptions;
};

export type Fetch = (
url: string | Request,
Expand All @@ -20,7 +20,7 @@ export type Fetch = (
export type FetchModule = {
default: Fetch;
Headers: typeof Headers;
}
};

export default function SetupFetch(
fetchModule?: FetchModule,
Expand Down
29 changes: 17 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const {parse: parseUrl} = require('url');
const HttpAgent = require('agentkeepalive');
const debug = require('debug')('@zeit/fetch');
const setupFetchRetry = require('@zeit/fetch-retry');
const setupFetchCachedDns = require('@zeit/fetch-cached-dns');
const debug = require('debug')('@vercel/fetch');
const setupFetchRetry = require('@vercel/fetch-retry');
const setupFetchCachedDns = require('@vercel/fetch-cached-dns');
const urlModule = require('url');

const {HttpsAgent} = HttpAgent;
Expand All @@ -20,13 +20,15 @@ let defaultHttpGlobalAgent;
let defaultHttpsGlobalAgent;

function getDefaultHttpGlobalAgent(agentOpts) {
return defaultHttpGlobalAgent = defaultHttpGlobalAgent ||
(debug('init http agent'), new HttpAgent(agentOpts));
return (defaultHttpGlobalAgent =
defaultHttpGlobalAgent ||
(debug('init http agent'), new HttpAgent(agentOpts)));
}

function getDefaultHttpsGlobalAgent(agentOpts) {
return defaultHttpsGlobalAgent = defaultHttpsGlobalAgent ||
(debug('init https agent'), new HttpsAgent(agentOpts));
return (defaultHttpsGlobalAgent =
defaultHttpsGlobalAgent ||
(debug('init https agent'), new HttpsAgent(agentOpts)));
}

function getAgent(url, agentOpts) {
Expand All @@ -35,8 +37,8 @@ function getAgent(url, agentOpts) {
: getDefaultHttpGlobalAgent(agentOpts);
}

function setupZeitFetch(fetch, agentOpts = {}) {
return async function zeitFetch(url, opts = {}) {
function setupVercelFetch(fetch, agentOpts = {}) {
return async function vercelFetch(url, opts = {}) {
if (!opts.agent) {
// Add default `agent` if none was provided
opts.agent = getAgent(url, {AGENT_OPTIONS, ...agentOpts});
Expand All @@ -45,7 +47,10 @@ function setupZeitFetch(fetch, agentOpts = {}) {
opts.redirect = 'manual';
opts.headers = new fetch.Headers(opts.headers);
// Workaround for node-fetch + agentkeepalive bug/issue
opts.headers.set('host', opts.headers.get('host') || parseUrl(url).host);
opts.headers.set(
'host',
opts.headers.get('host') || parseUrl(url).host
);

// Convert Object bodies to JSON if they are JS objects
if (
Expand Down Expand Up @@ -89,13 +94,13 @@ function setup(fetch, options) {

if (typeof fetch !== 'function') {
throw new Error(
"fetch() argument isn't a function; did you forget to initialize your @zeit/fetch import?"
"fetch() argument isn't a function; did you forget to initialize your `@vercel/fetch` import?"
);
}

fetch = setupFetchCachedDns(fetch);
fetch = setupFetchRetry(fetch);
fetch = setupZeitFetch(fetch, options);
fetch = setupVercelFetch(fetch, options);
return fetch;
}

Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@zeit/fetch",
"name": "@vercel/fetch",
"version": "6.0.0",
"description": "Opinionated `fetch` optimized for use inside microservices",
"license": "MIT",
Expand All @@ -11,17 +11,17 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/zeit/fetch.git"
"url": "git+https://github.com/vercel/fetch.git"
},
"author": "Nathan Rajlich <nate@zeit.co>",
"author": "Nathan Rajlich <nate@vercel.com>",
"bugs": {
"url": "https://github.com/zeit/fetch/issues"
"url": "https://github.com/vercel/fetch/issues"
},
"homepage": "https://github.com/zeit/fetch#readme",
"homepage": "https://github.com/vercel/fetch#readme",
"dependencies": {
"@types/async-retry": "1.2.1",
"@zeit/fetch-cached-dns": "1.2.0",
"@zeit/fetch-retry": "^5.0.0",
"@vercel/fetch-cached-dns": "^2.0.1",
"@vercel/fetch-retry": "^5.0.2",
"agentkeepalive": "3.4.1",
"debug": "3.1.0"
},
Expand Down
16 changes: 8 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# @zeit/fetch
# @vercel/fetch

[![Build Status](https://github.com/vercel/fetch/workflows/Node%20CI/badge.svg)](https://github.com/vercel/fetch/actions?workflow=Node+CI)

Opinionated `fetch` optimized for use inside microservices. Bundles:

- https://github.com/zeit/fetch-retry
- https://github.com/zeit/fetch-cached-dns
- https://github.com/node-modules/agentkeepalive
- https://github.com/vercel/fetch-retry
- https://github.com/vercel/fetch-cached-dns
- https://github.com/node-modules/agentkeepalive

It automatically configures an `agent` via [agentkeepalive](https://github.com/node-modules/agentkeepalive),
if not provided, with the following settings:

| Name | Value |
|------------------------------|-------|
| ---------------------------- | ----- |
| `maxSockets` | 200 |
| `maxFreeSockets` | 20 |
| `timeout` | 60000 |
Expand All @@ -23,14 +23,14 @@ if not provided, with the following settings:
JavaScript

```js
const fetch = require('@zeit/fetch')(require('some-fetch-implementation'))
const fetch = require('@vercel/fetch')(require('some-fetch-implementation'));
```

TypeScript

```typescript
import createFetch from "@zeit/fetch"
import * as fetch from "some-fetch-implementation";
import createFetch from '@vercel/fetch';
import * as fetch from 'some-fetch-implementation';
const fetch = createFetch(fetch);
```

Expand Down
11 changes: 4 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ exports.supportsObjectRequestBody = async () => {
const body = await toBuffer(req);
assert(Buffer.isBuffer(body));
assert.deepEqual(JSON.parse(body.toString()), {foo: 'bar'});
assert.equal(
req.headers['content-type'],
'application/json'
);
assert.equal(req.headers['content-type'], 'application/json');
res.end();
});
await listen(server);
Expand Down Expand Up @@ -104,14 +101,14 @@ exports.supportsSearchParamsRequestBody = async () => {

exports.errorContext = async () => {
let err;
const url = `http://127.0.0.1/\u0019`;
const u = `http://127.0.0.1/\u0019`;
try {
await fetch(url);
await fetch(u);
} catch (_err) {
err = _err;
}
assert(err);
assert.equal(err.message, 'Request path contains unescaped characters');
assert.equal(err.url, url);
assert.equal(err.url, u);
assert.equal(err.opts.redirect, 'manual');
};
Loading

0 comments on commit 0bd5870

Please sign in to comment.