zen-fs / core

A filesystem, anywhere
https://zen-fs.github.io/core/
MIT License
103 stars 14 forks source link

Request for `Fetch` Backend Examples #59

Closed hawav closed 4 months ago

hawav commented 4 months ago

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.

james-pre commented 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
});

Base URL

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.

Index

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?

hawav commented 4 months ago

@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:

Steps to Set Up:

  1. I created a file in my directory:
echo "Hello ZenFS!" > test.txt
  1. I generated the index file using the make-index script provided by ZenFS:
npx -p @zenfs/core make-index . -o index.json
  1. I started an HTTP file server using serve:
serve . -p 8080 --cors

Configuration:

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'
});

Asynchronous File Fetching:

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()));

Synchronous File Fetching Problem:

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.