Skip to content

Commit

Permalink
feat(node): support http agent with keepAlive (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Nov 4, 2021
1 parent dfa0b55 commit 18a952a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const { $fetch } = require('ohmyfetch')
We use [conditional exports](https://nodejs.org/api/packages.html#packages_conditional_exports) to detect Node.js
and automatically use [node-fetch](https://github.com/node-fetch/node-fetch). If `globalThis.fetch` is available, will be used instead.

### undici support
### `undici` support

In order to use experimental fetch implementation from [nodejs/undici](https://github.com/nodejs/undici), You can import from `ohmyfetch/undici`.

Expand All @@ -49,6 +49,12 @@ import { $fetch } from 'ohmyfetch/undici'

On Node.js versions older than `16.5`, node-fetch will be used as the fallback.

### `keepAlive` support

By setting `FETCH_KEEP_ALIVE` environment variable to `true`, A http/https agent will be registred that keeps sockets around even when there are no outstanding requests, so they can be used for future requests without having to reestablish a TCP connection.

**Note:** This option can potentially introduce memory leaks. Please check [node-fetch/node-fetch#1325](https://github.com/unjs/ohmyfetch/pull/22).

## ✔️ Parsing Response

`$fetch` Smartly parses JSON and native values using [destr](https://github.com/unjs/destr) and fallback to text if it fails to parse.
Expand Down
26 changes: 25 additions & 1 deletion src/node.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import http from 'http'
import https, { AgentOptions } from 'https'
import nodeFetch from 'node-fetch'

import { createFetch } from './base'

export * from './base'

export const fetch = globalThis.fetch || nodeFetch as any as typeof globalThis.fetch
export function createNodeFetch () {
const useKeepAlive = JSON.parse(process.env.FETCH_KEEP_ALIVE || 'false')
if (!useKeepAlive) {
return nodeFetch
}

// https://github.com/node-fetch/node-fetch#custom-agent
const agentOpts: AgentOptions = { keepAlive: true }
const httpAgent = new http.Agent(agentOpts)
const httpsAgent = new https.Agent(agentOpts)
const nodeFetchOptions = {
agent (parsedURL: any) {
return parsedURL.protocol === 'http:' ? httpAgent : httpsAgent
}
}

return function nodeFetchWithKeepAlive (input: RequestInfo, init?: RequestInit) {
return (nodeFetch as any)(input, { ...nodeFetchOptions, ...init })
}
}

export const fetch = globalThis.fetch || createNodeFetch()

export const $fetch = createFetch({ fetch })
4 changes: 2 additions & 2 deletions src/undici.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { fetch as undiciFetch } from 'undici'
import nodeFetch from 'node-fetch'
import { createNodeFetch } from './node'
import { createFetch } from './base'

export * from './base'

export const fetch = globalThis.fetch || undiciFetch || nodeFetch
export const fetch = globalThis.fetch || undiciFetch || createNodeFetch()

export const $fetch = createFetch({ fetch })

0 comments on commit 18a952a

Please sign in to comment.