Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 })