Has anybody forked Undici to implement fetching file: protocol yet?

38 Views Asked by At

After reading this question How to get a local file via fetch/axios? and posting an answer I revisited the subject matter and compiled a short list of relevant Node.js, Undici, and WinterCG issues

I went ahead and fetched the Undici module and ran some code to so we can get the files and lines where Undici's fetch() throws

fetch_file.js

import { fetch } from "undici";
const url = import.meta.resolve("../nm_host.js");
fetch(url).then((r) => r.text())
.then(console.log)
.catch(console.error);

which winds up looking like undici_fetch_throwing_on_file_protocol_request.md

TypeError: fetch failed
    at fetch (/node_modules/undici/index.js:109:13) {
  [cause]: TypeError: Invalid URL
      at new URL (node:internal/url:804:36)
      at parseURL (/node_modules/undici/lib/core/util.js:51:11)
      at Object.parseOrigin (/node_modules/undici/lib/core/util.js:117:9)
      at new Pool (/node_modules/undici/lib/dispatcher/pool.js:70:23)
      at Agent.defaultFactory (/node_modules/undici/lib/dispatcher/agent.js:22:7)
      at [dispatch] (/node_modules/undici/lib/dispatcher/agent.js:93:34)
      at Intercept (/node_modules/undici/lib/interceptor/redirect-interceptor.js:11:16)
      at [Intercepted Dispatch] (/node_modules/undici/lib/dispatcher/dispatcher-base.js:158:12)
      at Agent.dispatch (/node_modules/undici/lib/dispatcher/dispatcher-base.js:179:40)
      at /node_modules/undici/lib/web/fetch/index.js:2079:51 {
    code: 'ERR_INVALID_URL',
    input: 'null'
  }

I know with node we can do something like this

const url = "file:///home/user/bin/nm_host.js";
new Response((await import("node:fs")).readFileSync(new URL(url)))
.text()
.then(console.log).catch(console.error);

We could also just use deno or bun to use fetch() to request file: protocol.

In lieu of waiting for the various stakeholders in Node.js world on other domains to reach a consensus on this matter I decided to take a peek myself.

I've dove in to /lib/web/fetch/index.js, /lib/dispatcher/dispatcher-base.js, /lib/core/util.js a little.

Before I proceed any further taking apart and putting back together this part of Undici's WHATWG fetch() implementation I thought I should ask has anybody forked Undici and done this before to implement fetching file: protocol for that library?

0

There are 0 best solutions below