From d98283339b2ab8c78c4cda6932e25e49b8d05bde Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 20 Nov 2020 00:30:12 +0100 Subject: [PATCH] feat: node preset and improved fetch --- README.md | 14 +++++++------- package.json | 2 +- src.runtime/fetch/{local => }/call.ts | 4 ++-- src.runtime/fetch/{local/fetch.ts => index.ts} | 3 ++- src.runtime/fetch/local/index.ts | 2 -- src.runtime/fetch/polyfill/node.ts | 6 ------ src.runtime/{shims => polyfill}/process.ts | 0 src/presets/index.ts | 1 + src/presets/node.ts | 13 +++++++++++++ src/presets/nodeless.ts | 4 ++-- yarn.lock | 5 +++++ 11 files changed, 33 insertions(+), 21 deletions(-) rename src.runtime/fetch/{local => }/call.ts (90%) rename src.runtime/fetch/{local/fetch.ts => index.ts} (86%) delete mode 100644 src.runtime/fetch/local/index.ts delete mode 100644 src.runtime/fetch/polyfill/node.ts rename src.runtime/{shims => polyfill}/process.ts (100%) create mode 100644 src/presets/node.ts diff --git a/README.md b/README.md index a8b8e6c8..40f74406 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Yet expected to run code that had to work both of them :} ## What is un? -un is a collection of modules, shims and presets that work perfectly with any Javascript environemnt +un is a collection of modules, polyfills and presets that work perfectly with any Javascript environemnt including Browsers, Workers, NodeJS or pure JavaScript. You still need a bundler like [rollup.js](https://rollupjs.org) and un will disapear as soon as is bundled. @@ -51,14 +51,14 @@ const { alias, inject } = env(nodeless, { ### Presets -- [nodeless](./src.runtime/env/presets/nodeless.ts) -- [vue2](./src.runtime/env/presets/vue2.ts) -- [vue3](./src.runtime/env/presets/vue3.ts) +- [node](./src/presets/node.ts) +- [nodeless](./src/presets/nodeless.ts) +- [vue2](./src/presets/vue2.ts) +- [vue3](./src/presets/vue3.ts) -## Shims +## polyfills -- [fetch](./src.runtime/shims/fetch.ts) -- [process](./src.runtime/shims/process.ts) +- [process](./src.runtime/polyfill/process.ts) ## NodeJS diff --git a/package.json b/package.json index be9f3650..a5558d93 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,6 @@ "description": "", "repository": "nuxt-contrib/un", "license": "MIT", - "sideEffects": false, "main": "./dist/index.js", "files": [ "dist", @@ -23,6 +22,7 @@ "events": "^3.2.0", "inherits": "^2.0.4", "mime": "^2.4.6", + "node-fetch": "^2.6.1", "process": "^0.11.10", "upath": "^2.0.1", "util": "^0.12.3" diff --git a/src.runtime/fetch/local/call.ts b/src.runtime/fetch/call.ts similarity index 90% rename from src.runtime/fetch/local/call.ts rename to src.runtime/fetch/call.ts index 6db95d66..8638f72d 100644 --- a/src.runtime/fetch/local/call.ts +++ b/src.runtime/fetch/call.ts @@ -1,5 +1,5 @@ -import { IncomingMessage } from '../../node/http/request' -import { ServerResponse } from '../../node/http/response' +import { IncomingMessage } from '../node/http/request' +import { ServerResponse } from '../node/http/response' export type Handle = (req: IncomingMessage, res: ServerResponse) => Promise diff --git a/src.runtime/fetch/local/fetch.ts b/src.runtime/fetch/index.ts similarity index 86% rename from src.runtime/fetch/local/fetch.ts rename to src.runtime/fetch/index.ts index f4342b55..0ef24a79 100644 --- a/src.runtime/fetch/local/fetch.ts +++ b/src.runtime/fetch/index.ts @@ -1,9 +1,10 @@ import { CallContext, CallHandle } from './call' +export * from './call' export type FetchOptions = globalThis.RequestInit & CallContext export function createFetch (call: CallHandle) { - return async function localFetch (input: string | Request, init: FetchOptions): Promise { + return async function fetch (input: string | Request, init: FetchOptions): Promise { const url = input.toString() if (!url.startsWith('/')) { return fetch(url, init) diff --git a/src.runtime/fetch/local/index.ts b/src.runtime/fetch/local/index.ts deleted file mode 100644 index 4a6114a4..00000000 --- a/src.runtime/fetch/local/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './call' -export * from './fetch' diff --git a/src.runtime/fetch/polyfill/node.ts b/src.runtime/fetch/polyfill/node.ts deleted file mode 100644 index 79257c3a..00000000 --- a/src.runtime/fetch/polyfill/node.ts +++ /dev/null @@ -1,6 +0,0 @@ -import fetch, { Request, Response, Headers } from 'node-fetch' - -global.fetch = global.fetch || fetch -global.Request = global.Request || Request -global.Response = global.Response || Response -global.Headers = global.Headers || Headers diff --git a/src.runtime/shims/process.ts b/src.runtime/polyfill/process.ts similarity index 100% rename from src.runtime/shims/process.ts rename to src.runtime/polyfill/process.ts diff --git a/src/presets/index.ts b/src/presets/index.ts index 171f8e6f..f1a50de5 100644 --- a/src/presets/index.ts +++ b/src/presets/index.ts @@ -1,3 +1,4 @@ +export { default as node } from './node' export { default as nodeless } from './nodeless' export { default as vue2 } from './vue2' export { default as vue3 } from './vue3' diff --git a/src/presets/node.ts b/src/presets/node.ts new file mode 100644 index 00000000..89c7f1ac --- /dev/null +++ b/src/presets/node.ts @@ -0,0 +1,13 @@ +import type { Preset } from '../types' + +export default { + alias: { + 'node-fetch': require.resolve('node-fetch/lib/index.js') + }, + inject: { + fetch: 'node-fetch', + Request: ['node-fetch', 'Request'], + Response: ['node-fetch', 'Response'], + Headers: ['node-fetch', 'Headers'] + } +} as Preset diff --git a/src/presets/nodeless.ts b/src/presets/nodeless.ts index 83c70c31..4ac9540e 100644 --- a/src/presets/nodeless.ts +++ b/src/presets/nodeless.ts @@ -7,7 +7,7 @@ export default { // Custom http: r('node/http'), - process: r('shims/process'), + process: r('polyfill/process'), _process: require.resolve('process/browser.js'), // Browserify @@ -24,7 +24,7 @@ export default { }, inject: { - process: r('shims/process'), + process: r('polyfill/process'), Buffer: ['buffer', 'Buffer'] } } as Preset diff --git a/yarn.lock b/yarn.lock index f40cd6cf..2ad3ee89 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2341,6 +2341,11 @@ neo-async@^2.6.0: resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.2.tgz#b4aafb93e3aeb2d8174ca53cf163ab7d7308305f" integrity sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw== +node-fetch@^2.6.1: + version "2.6.1" + resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052" + integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw== + normalize-package-data@^2.3.0, normalize-package-data@^2.3.2, normalize-package-data@^2.3.4, normalize-package-data@^2.5.0: version "2.5.0" resolved "https://registry.yarnpkg.com/normalize-package-data/-/normalize-package-data-2.5.0.tgz#e66db1838b200c1dfc233225d12cb36520e234a8"