question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow one file sources

See original GitHub issue

We currently only support specifying a directory as a custom source like this:

{
   name: 'fa-ir',
   prefix: '/fa', // All contents inside this source will be prefixed with `/fa`
   driver: 'fs',
   base: resolve(__dirname, 'content-fa') // Path for source directory
}

Would it be possible to support specifying a complete file path as a source?

It would be useful for cases like:

  • Adding nuxt and @nuxt/content dependencies to a repository
  • Only have a nuxt.config.ts at the root of the project
  • Render a single file like README.md with a custom theme as a website for the repository

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
pi0commented, Jul 26, 2022

Content can create a virtual or overlay driver for single soures with a nested unstorage instance. There is a performance side-effect since with single file mounts, we want to avoid listing operations on those mountpoints that this driver can do. I would suggest instead of extending driver key, introduce a new option such as fileName: 'README.md' that enables this behavior.

1reaction
farnabazcommented, Jul 25, 2022

There is two way to create single-file drivers for unstorage:

1 - Allow unstorage to expose storage instance into drivers and create a simple alias driver

// simple aliasing
export default defineDriver((_opts: DriverOptions) => {
  const opts = { ..._opts, ...defaultOptions }
  return {
    getKeys () {
      return Promise.resolve([opts.alias as string])
    },
    hasItem (key) {
      return Promise.resolve(key === opts.alias)
    },
    getItem (key) {
      if (key === opts.alias) {
        return storageInstance.getItem(opts.source)
      }
      return Promise.resolve(null)
    },
    setItem (key, value) {
      if (key === opts.alias) {
        return storageInstance.setItem(opts.source, value)
      }
      return Promise.resolve(undefined)
    }
  }
})

Pros

  • With aliases we do not need to create separate drivers for each file source (fs-single, github-single, cloudflare-kv-single, …)
  • With aliases, we don’t need to mount multiple drivers for the same source, For example, we can simple alias root:README.md into content:index.md without the need for any extra fs-driver

Cons

  • The storage instance should expose to drivers, it might not be safe to expose the whole storage to drivers.
  • In some cases, the user might need to define two sources. (for example for reading README.md from GitHub source, she needs to define two sources, one for the GitHub repo and the other for README.md alias

2 - Create a separate single driver for each driver

Pros

  • This approach is straightforward and it does not require any change in unstorage core.

Cons

  • We need to create multiple drivers for this purpose (fs-single, github-single, cloudflare-kv-single, …)
  • Performance-wise, it is slow. For example, reading content and README.md from github require two drivers that both tries to fetch repo
Read more comments on GitHub >

github_iconTop Results From Across the Web

Gale General OneFile
Our largest general-interest periodical resource, General OneFile allows researchers to find the information they want quickly.
Read more >
javascript - Plupload - Restrict to only one file - Stack Overflow
Allow only one file to be uploaded: uploader.bind('FilesAdded', function(up, files) { $.each(files, function(i, ...
Read more >
The 15 Best Ways to Share Files With Anyone Over the Web
1. ShareDrop ... Powered by WebRTC, ShareDrop is a browser-based P2P file-sharing software. It works a little differently than the other file ......
Read more >
Import or link to data in a text file - Microsoft Support
Open the source text file in a text editor, such as Notepad. Note: You can import only one text file during an import...
Read more >
<input type="file"> - HTML: HyperText Markup Language | MDN
elements with type="file" let the user choose one or more files from ... an <input type="file"> , the real path to the source...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found