Skip to content

Fetch failed on Windows #144

Description

@Kayakyx

demo.ts

interface GetMiscHotboardResp {
  type: string
  update_time: string
  list: GetMiscHotboardRespListItem[]
}

interface GetMiscHotboardRespListItem {
  index: number
  title: string
  url: string
  hot_value: string
  // extra: MiscHotboardExtra
}

async function getMiscHotboardByFetch(): Promise<GetMiscHotboardResp> {
  const resp = await fetch('https://uapis.cn/api/v1/misc/hotboard?type=weibo', {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
    },
  })
  if (!resp.ok) {
    throw new Error(`HTTP error! status: ${resp.status}`)
  }
  return await resp.json() as GetMiscHotboardResp
}

async function main() {
  const resp = await getMiscHotboardByFetch()
  console.log('resp: ', resp)
}

main().catch((err: unknown) => {
  console.error(`failed to run: ${err instanceof Error ? err.message : String(err)}`)
  process.exit(1)
})

Run (scriptc version is v0.0.28)

$env:SCRIPTC_CC="zigcc"; $env:SCRIPTC_TARGET="x86_64-windows-gnu";scriptc run demo.ts

Print

failed to run: fetch failed

The issue identified by the AI is

The default TLS verification does indeed fail in ScriptC/Windows, with the error “unable to verify the first certificate.” I tried using getCACertificates(‘system’), but that didn't fix the issue, so this isn't a problem with the API—it's because ScriptC's certificate chain handling isn't quite as robust as Node's. For now, use rejectUnauthorized: false to get the development command to run successfully, and change the error message from “fetch failed” to a diagnosable HTTP/TLS error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions