Open aduh95 opened 2 years ago
I agree. I think this should be a normative optional extension of whatever work we do here, because some runtimes (Node) may not want to enable file:
fetching at all.
The main topics I think need to be covered:
content-type
set on response bodies? I'd argue for no.content-length
set on response bodies? I'd argue for no.Notes on Deno's implemenation of file:
fetching: https://deno.land/manual/runtime/web_platform_apis#fetching-local-files
Having built a CORS proxy server myself using the fetch api then i would argue that i wouldn't at least want to allow the user to fetch local files and doing something like: https://cors.io/url=file://etc/psw
so i have always been against it for security issues. browser dose not allow doing ajax calls on file:// either.
if i would want to read files from the file system then i would have used the file system api.
if i would like to turn it into a Response object then i would probably do something like: new Response(await fs.openAsBlob(path)).text()
btw, there is also the possibility of fetch(URL.createObjectURL(blob))
For reference, Node.JS landing an implementation of fetch that doesn’t support file:
URLs has broken emscripten: https://github.com/emscripten-core/emscripten/pull/16917
This worked briefly in node
v22.0.0-nightly202311151d8483e713
> var scriptText = await (await fetch("file:///home/user/exports.js")).text();
undefined
> scriptText
"const fn = () => 123;\nexport {fn};"
> const mod = await import(URL.createObjectURL(new Blob([scriptText], {type:"text/javascript"})));
undefined
> mod
[Module: null prototype] { fn: [Function: fn] }
> mod.fn()
123
then stopped working in node
v22.0.0-nightly202311250bb5d88871.
Using file:
protocol with fetch()
is possible on Chromium-based browsers in an extension Issue 1227761: Fetch() should support file scheme for extensions
{
"name": "Service Worker-based background script",
"description": "Test that fetching a file scheme URL succeeds",
"version": "1",
"manifest_version": 3,
"host_permissions": ["file:///*"],
"background": {"service_worker": "service_worker_background.js"}
}
and in Deno deno 1.38.3 (release, x86_64-unknown-linux-gnu).
The WHATWG Fetch spec says this regarding fetching of URLs using the
file:
protocol:That'd be nice to have a standard way for handling those.