Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove node-fetch types #1

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 0 additions & 127 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"fx": "*",
"globby": "^13.1.4",
"minimist": "^1.2.8",
"node-fetch": "3.3.1",
"ps-tree": "^1.2.0",
"webpod": "^0",
"which": "^3.0.0",
Expand Down
6 changes: 3 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { createRequire } from 'node:module'
import { basename, dirname, extname, join, resolve } from 'node:path'
import url from 'node:url'
import { updateArgv } from './goods.js'
import { $, chalk, fetch, ProcessOutput } from './index.js'
import { $, chalk, globalFetch, ProcessOutput } from './index.js'
import { startRepl } from './repl.js'
import { randomId } from './util.js'
import { installDeps, parseDeps } from './deps.js'
Expand Down Expand Up @@ -125,8 +125,8 @@ async function scriptFromStdin() {
}

async function scriptFromHttp(remote: string) {
const res = await fetch(remote)
if (!res.ok) {
const res = await globalFetch(remote)
if (!res?.ok) {
console.error(`Error: Can't get ${remote}`)
process.exit(1)
}
Expand Down
1 change: 0 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { ChildProcess, spawn, StdioNull, StdioPipe } from 'node:child_process'
import { AsyncLocalStorage, createHook } from 'node:async_hooks'
import { Readable, Writable } from 'node:stream'
import { inspect } from 'node:util'
import { RequestInfo, RequestInit } from 'node-fetch'
import chalk, { ChalkInstance } from 'chalk'
import which from 'which'
import {
Expand Down
18 changes: 13 additions & 5 deletions src/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
import assert from 'node:assert'
import * as globbyModule from 'globby'
import minimist from 'minimist'
import nodeFetch, { RequestInfo, RequestInit } from 'node-fetch'
import { createInterface } from 'node:readline'
import { $, within, ProcessOutput } from './core.js'
import { Duration, isString, parseDuration } from './util.js'
import {
Duration,
isNativeFetchExists,
isString,
parseDuration,
} from './util.js'
import chalk from 'chalk'

export { default as chalk } from 'chalk'
Expand Down Expand Up @@ -51,9 +55,13 @@ export function sleep(duration: Duration) {
})
}

export async function fetch(url: RequestInfo, init?: RequestInit) {
$.log({ kind: 'fetch', url, init })
return nodeFetch(url, init)
export async function globalFetch(url: RequestInfo, init?: RequestInit) {
if (isNativeFetchExists) {
$.log({ kind: 'fetch', url, init })
return globalThis.fetch(url, init)
}
console.error('Fetch is not supported')
return
}

export function echo(...args: any[]): void
Expand Down
2 changes: 2 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ export function formatCmd(cmd?: string): string {
return out + '\n'
}

export const isNativeFetchExists = 'fetch' in globalThis

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

const test = suite('cli')

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

test('scripts from https', async () => {
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 (!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()
assert.match(out.stderr, "Error: Can't get")
Expand Down
5 changes: 5 additions & 0 deletions test/goods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import chalk from 'chalk'
import { suite } from 'uvu'
import * as assert from 'uvu/assert'

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

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

test('fetch() works', async () => {
if (!isNativeFetchExists) {
return true
}
assert.match(
await fetch('https://medv.io').then((res) => res.text()),
/Anton Medvedev/
Expand Down
17 changes: 17 additions & 0 deletions test/utils/test-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

const isNativeFetchExists = 'fetch' in globalThis

export { isNativeFetchExists }