Closed hawav closed 4 months ago
Welcome @hawav!
You can configure ZenFS to use the Fetch
backend like this:
await configure({
backend: Fetch,
baseUrl: '//example.com/files/', // defaults to the directory of the index
index: '//example.com/index.json' // the index (see below), defaults to ./index.json
});
The base URL is the, well, base URL for fetching. If you set it to http://example.com/my-fs
, then readFile
/some-file
, the backend will fetch http://example.com/my-fs/some-file
.
The index is a JSON file which contains the structure of a directory. This is needed since there is no consistent way to enumerate the entries in a directory with the fetch
API. You can generate this index by using the make-index
script. A full command looks like this: npx -p @zenfs/core make-index <directory> -o <output.json>
. You can use --help
to print all of the options along with a short description.
Does that answer your question?
@james-pre Thank you for the detailed explanation! I've tried configuring ZenFS with the Fetch backend using the provided instructions. and it worked successfully for fetching files asynchronously. Here's how I did it:
echo "Hello ZenFS!" > test.txt
make-index
script provided by ZenFS:npx -p @zenfs/core make-index . -o index.json
serve
:serve . -p 8080 --cors
I configured ZenFS to use the Fetch backend by executing the following code:
import * as zenfs from '@zenfs/core'
await zenfs.configure({
backend: zenfs.Fetch,
baseUrl: '//localhost:8080',
index: '//localhost:8080/index.json'
});
After configuring ZenFS, I was able to use the readFile
function to fetch files from the remote server!
// file content should be able print to console
zenfs.readFile('test.txt', (e, content) => console.log(content?.toString()));
However, I found that readFileSync
version does not seems to work as expected:
console.log(zenfs.readFileSync('test.txt'));
// Error: ENOENT: No such file or directory, '/test.txt'
Even though the file exists at the remote location, readFileSync
is throwing an ENOENT
error, indicating that it cannot find the file.
I've opened an issue about this problem at #61. Please take a look when you have the time.
Hello,
I've been exploring ZenFS and have found it very useful. However, I'm having a bit of trouble understanding how to use the
Fetch
backend to retrieve files without an explicit example.I've looked through the documentation and searched the repository, but I haven't found a clear example demonstrating how to fetch a file using the Fetch backend in ZenFS. Would it be possible to add an example to the documentation or the examples directory that shows how to do this?
I believe such an example would be beneficial for other users as well.
Thank you for considering my request and for your hard work on ZenFS.