Skip to content

Commit

Permalink
Updated fetch exists check
Browse files Browse the repository at this point in the history
  • Loading branch information
theothirteen committed Jun 24, 2023
1 parent dea9623 commit 90ff479
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 12 deletions.
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 @@ -56,7 +56,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
6 changes: 3 additions & 3 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,7 +98,7 @@ test('supports `--prefix` flag ', async () => {
})

test('scripts from https', async () => {
if (!isNodeEighteenOrGreaterThanEighteen) {
if (!isNativeFetchExists) {
return true;
}
$`cat ${path.resolve('test/fixtures/echo.http')} | nc -l 8080`
Expand All @@ -107,7 +107,7 @@ test('scripts from https', async () => {
})

test('scripts from https not ok', async () => {
if (!isNodeEighteenOrGreaterThanEighteen) {
if (!isNativeFetchExists) {
return true;
}
$`echo $'HTTP/1.1 500\n\n' | nc -l 8081`
Expand Down
4 changes: 2 additions & 2 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,7 +50,7 @@ test('globby available', async () => {
})

test('fetch() works', async () => {
if(!isNodeEighteenOrGreaterThanEighteen){
if(!isNativeFetchExists){
return true;
}
assert.match(
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 90ff479

Please sign in to comment.