Skip to content

Commit

Permalink
feat: node preset and improved fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 19, 2020
1 parent 8a91c4b commit d982833
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 21 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"description": "",
"repository": "nuxt-contrib/un",
"license": "MIT",
"sideEffects": false,
"main": "./dist/index.js",
"files": [
"dist",
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src.runtime/fetch/local/call.ts → src.runtime/fetch/call.ts
Original file line number Diff line number Diff line change
@@ -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 <any>

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Response> {
return async function fetch (input: string | Request, init: FetchOptions): Promise<Response> {
const url = input.toString()
if (!url.startsWith('/')) {
return fetch(url, init)
Expand Down
2 changes: 0 additions & 2 deletions src.runtime/fetch/local/index.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src.runtime/fetch/polyfill/node.ts

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions src/presets/index.ts
Original file line number Diff line number Diff line change
@@ -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'
13 changes: 13 additions & 0 deletions src/presets/node.ts
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions src/presets/nodeless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +24,7 @@ export default {
},

inject: {
process: r('shims/process'),
process: r('polyfill/process'),
Buffer: ['buffer', 'Buffer']
}
} as Preset
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit d982833

Please sign in to comment.