Skip to content

Commit

Permalink
feat!: replace node-fetch and cross-fetch with platform natives (r…
Browse files Browse the repository at this point in the history
…esolves #29)
  • Loading branch information
pi0 committed May 5, 2022
1 parent 8ed02ad commit 276d44d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Module | Status | Source
-------|--------|---------------
[node:assert](https://nodejs.org/api/assert.html) | Mocked | -
[node:async_hooks](https://nodejs.org/api/async_hooks.html) | Mocked | -
[node:buffer](https://nodejs.org/api/buffer.html) | Polyfilled | [npm:buffer](https://www.npmjs.com/package/buffer)
[node:buffer](https://nodejs.org/api/buffer.html) | Polyfilled | [npm/buffer](https://www.npmjs.com/package/buffer)
[node:child_process](https://nodejs.org/api/child_process.html) | Mocked | -
[node:cluster](https://nodejs.org/api/cluster.html) | Mocked | -
[node:console](https://nodejs.org/api/console.html) | Mocked | -
Expand All @@ -52,7 +52,7 @@ Module | Status | Source
[node:diagnostics_channel](https://nodejs.org/api/diagnostics_channel.html) | Mocked | -
[node:dns](https://nodejs.org/api/dns.html) | Mocked | -
[node:domain](https://nodejs.org/api/domain.html) | Mocked | -
[node:events](https://nodejs.org/api/events.html) | Polyfilled | [npm:events](https://www.npmjs.com/package/events)
[node:events](https://nodejs.org/api/events.html) | Polyfilled | [npm/events](https://www.npmjs.com/package/events)
[node:fs/promises](https://nodejs.org/api/fs/promises.html) | Mocked | -
[node:fs](https://nodejs.org/api/fs.html) | Mocked | -
[node:http2](https://nodejs.org/api/http2.html) | Mocked | -
Expand All @@ -77,17 +77,18 @@ Module | Status | Source
[node:trace_events](https://nodejs.org/api/trace_events.html) | Mocked | -
[node:tty](https://nodejs.org/api/tty.html) | Mocked | -
[node:url](https://nodejs.org/api/url.html) | Polyfilled | [unenv/runtime/url/request](./src/runtime/node/url)
[node:util](https://nodejs.org/api/util.html) | Polyfilled | [npm:util](https://www.npmjs.com/package/util)
[node:util](https://nodejs.org/api/util.html) | Polyfilled | [npm/util](https://www.npmjs.com/package/util)
[node:v8](https://nodejs.org/api/v8.html) | Mocked | -
[node:vm](https://nodejs.org/api/vm.html) | Mocked | -
[node:wasi](https://nodejs.org/api/wasi.html) | Mocked | -
[node:worker_threads](https://nodejs.org/api/worker_threads.html) | Mocked | -
[node:zlib](https://nodejs.org/api/zlib.html) | Mocked | -
[npm:etag](https://www.npmjs.com/package/etag) | Polyfilled | [unenv/runtime/npm:etag](./src/runtime/npm:etag)
[npm:fsevents](https://www.npmjs.com/package/fsevents) | Polyfilled | [unenv/runtime/npm:fsevents](./src/runtime/npm:fsevents)
[npm:mime-db](https://www.npmjs.com/package/mime-db) | Polyfilled | [unenv/runtime/npm:mime-db](./src/runtime/npm:mime-db)
[npm:mime](https://www.npmjs.com/package/mime) | Polyfilled | [unenv/runtime/npm:mime](./src/runtime/npm:mime)
[npm:node-fetch](https://www.npmjs.com/package/node-fetch) | Polyfilled | [unenv/runtime/npm:node-fetch](./src/runtime/npm:node-fetch)
[npm/etag](https://www.npmjs.com/package/etag) | Polyfilled | [unenv/runtime/npm/etag](./src/runtime/npm/etag.ts)
[npm/fsevents](https://www.npmjs.com/package/fsevents) | Polyfilled | [unenv/runtime/npm/fsevents](./src/runtime/npm/fsevents.ts)
[npm/mime-db](https://www.npmjs.com/package/mime-db) | Polyfilled | [unenv/runtime/npm/mime-db](./src/runtime/npm/mime-db.ts)
[npm/mime](https://www.npmjs.com/package/mime) | Polyfilled | [unenv/runtime/npm/mime](./src/runtime/npm/mime.ts)
[npm/node-fetch](https://www.npmjs.com/package/node-fetch) | Replace with Platform Native | [unenv/runtime/npm/node-fetch](./src/runtime/npm/node-fetch.ts)
[npm/cross-fetch](https://www.npmjs.com/package/node-fetch) | Replace with Platform Native | [unenv/runtime/npm/cross-fetch](./src/runtime/npm/cross-fetch.ts)



Expand Down
11 changes: 6 additions & 5 deletions src/presets/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { NodeBuiltinModules } from '../utils'
import type { Preset } from '../types'

export default {
inject: {
fetch: 'node-fetch',
Request: ['node-fetch', 'Request'],
Response: ['node-fetch', 'Response'],
Headers: ['node-fetch', 'Headers']
alias: {
'node-fetch': 'unenv/runtime/npm/node-fetch',
'_node-fetch': 'node-fetch',
'cross-fetch': 'unenv/runtime/npm/cross-fetch',
'cross-fetch/polyfill': 'unenv/runtime/mock/empty',
'isomorphic-fetch': 'unenv/runtime/mock/empty'
},

polyfill: [
Expand Down
5 changes: 4 additions & 1 deletion src/presets/nodeless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ const nodeless: Preset = {
etag: 'unenv/runtime/mock/noop',
'mime-db': 'unenv/runtime/npm/mime-db',
mime: 'unenv/runtime/npm/mime',
_mime: 'mime/lite.js',
fsevents: 'unenv/runtime/npm/fsevents',
'node-fetch': 'unenv/runtime/npm/node-fetch',
_mime: 'mime/lite.js'
'cross-fetch': 'unenv/runtime/npm/cross-fetch',
'cross-fetch/polyfill': 'unenv/runtime/mock/empty',
'isomorphic-fetch': 'unenv/runtime/mock/empty'
},

inject: {
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/npm/cross-fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// https://github.com/lquixada/cross-fetch

export const fetch = (...args: Parameters<typeof globalThis['fetch']>) => globalThis.fetch(...args)
export default fetch

export const Headers = globalThis.Headers
export const Request = globalThis.Request
export const Response = globalThis.Response
3 changes: 2 additions & 1 deletion src/runtime/polyfill/fetch.node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as nodeFetch from 'node-fetch'
// @ts-ignore
import * as nodeFetch from '_node-fetch'
import _global from './globalThis'

_global.fetch = _global.fetch || nodeFetch.default || nodeFetch
Expand Down

0 comments on commit 276d44d

Please sign in to comment.