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

Allow to fetch("file://") #8967

Closed
Nemo64 opened this issue Jan 2, 2021 · 1 comment
Closed

Allow to fetch("file://") #8967

Nemo64 opened this issue Jan 2, 2021 · 1 comment

Comments

@Nemo64
Copy link

Nemo64 commented Jan 2, 2021

The title says it all. I'd like if fetch could also open local files, just like the browser can if the origin ist file://.

There is even a test that checks that this does not work here:

await assertThrowsAsync(
async (): Promise<void> => {
await fetch("file:///");
},
TypeError,
"not supported",
);

Allowing file:// could profit any library or script that really just needs the contents of a file and doesn't really care where it is.... just like deno run in fact.

At this moment, you'd have to implement some kind of wrapper.

async function readTextFile(url: URL): Promise<string> {
    if (url.protocol === 'file:' && typeof Deno.readTextFile !== 'undefined') {
        return await Deno.readTextFile(url.pathname)
    } else {
        const response = await fetch(url)
        return await response.text()
    }
}

There are a few things interesting points to look out for:

  • Error code mapping aka: 404 = not found/file does not exist, 403 = unauthorized/--allow-read-missing/filemod
  • Partial request?
  • Post/Put request?
@lucacasonato
Copy link
Member

Duplicate of #2150

@lucacasonato lucacasonato marked this as a duplicate of #2150 Jan 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants