Skip to content

Commit

Permalink
Updated fetch exists check
Browse files Browse the repository at this point in the history
  • Loading branch information
theothirteen authored and theotwelvee committed Sep 18, 2023
1 parent c53a425 commit 1f6a3d8
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"tsd": "^0.28.1",
"typescript": "^5.0.4",
"uvu": "^0.5.6",
"@types/node-fetch":"^2.6.3"
"@types/node-fetch": "^2.6.3"
},
"publishConfig": {
"registry": "https://wombat-dressing-room.appspot.com"
Expand Down
4 changes: 2 additions & 2 deletions src/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { createInterface } from 'node:readline'
import { $, within, ProcessOutput } from './core.js'
import {
Duration,
isNodeEighteenOrGreaterThanEighteen,
isNativeFetchExists,
isString,
parseDuration,
} from './util.js'
Expand Down Expand Up @@ -57,7 +57,7 @@ export function sleep(duration: Duration) {
}

export async function globalFetch(url: RequestInfo, init?: RequestInit) {
if (isNodeEighteenOrGreaterThanEighteen) {
if (isNativeFetchExists) {
$.log({ kind: 'fetch', url, init })
return (globalThis as any).fetch(url, init)
}
Expand Down
3 changes: 1 addition & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,7 @@ export function formatCmd(cmd?: string): string {
return out + '\n'
}

export const isNodeEighteenOrGreaterThanEighteen =
process.versions && Number(process.versions.node.split('.')[0]) >= 18
export const isNativeFetchExists = 'fetch' in globalThis

const reservedWords = [
'if',
Expand Down
10 changes: 5 additions & 5 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import { suite } from 'uvu'
import * as assert from 'uvu/assert'
import '../build/globals.js'
import { isNodeEighteenOrGreaterThanEighteen } from './utils/test-utils.js'
import { isNativeFetchExists } from './utils/test-utils.js'

const test = suite('cli')

Expand Down Expand Up @@ -98,17 +98,17 @@ test('supports `--prefix` flag ', async () => {
})

test('scripts from https', async () => {
if (!isNodeEighteenOrGreaterThanEighteen) {
return true;
if (!isNativeFetchExists) {
return true
}
$`cat ${path.resolve('test/fixtures/echo.http')} | nc -l 8080`
let out = await $`node build/cli.js http://127.0.0.1:8080/echo.mjs`
assert.match(out.stderr, 'test')
})

test('scripts from https not ok', async () => {
if (!isNodeEighteenOrGreaterThanEighteen) {
return true;
if (!isNativeFetchExists) {
return true
}
$`echo $'HTTP/1.1 500\n\n' | nc -l 8081`
let out = await $`node build/cli.js http://127.0.0.1:8081`.nothrow()
Expand Down
8 changes: 4 additions & 4 deletions test/goods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import chalk from 'chalk'
import { suite } from 'uvu'
import * as assert from 'uvu/assert'

import { isNodeEighteenOrGreaterThanEighteen } from './utils/test-utils.js'
import { isNativeFetchExists } from './utils/test-utils.js'
import '../build/globals.js'

const test = suite('goods')
Expand Down Expand Up @@ -50,9 +50,9 @@ test('globby available', async () => {
})

test('fetch() works', async () => {
if(!isNodeEighteenOrGreaterThanEighteen){
return true;
}
if (!isNativeFetchExists) {
return true
}
assert.match(
await fetch('https://medv.io').then((res) => res.text()),
/Anton Medvedev/
Expand Down
5 changes: 2 additions & 3 deletions test/utils/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const isNodeEighteenOrGreaterThanEighteen =
process.versions && Number(process.versions.node.split('.')[0]) >= 18
const isNativeFetchExists = 'fetch' in globalThis

export { isNodeEighteenOrGreaterThanEighteen }
export { isNativeFetchExists }

0 comments on commit 1f6a3d8

Please sign in to comment.