typicode / lowdb

Simple and fast JSON database

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Throws ERR_INVALID_URL

pjobson opened this issue · comments

Not sure if you're still updating this or not.

Using the default example it throws ERR_INVALID_URL node 18.18.2.

node:internal/url:783
    this.#updateContext(bindingUrl.parse(input, base));
                                   ^

TypeError: Invalid URL
    at new URL (node:internal/url:783:36)
    at Database.setupDBs (file:///dvr/fonts/FontFool/cli/lib/database.js:22:18)
    at indexerInit (file:///dvr/fonts/FontFool/cli/indexer.js:81:13) {
  code: 'ERR_INVALID_URL',
  input: 'db.json',
  base: '/dvr/fonts/FontFool/cli/'
}

Changing the default to point to a url based path remedies the issue:

    const defaultData = { posts: [] }
    const db = await JSONPreset('db.json', defaultData, 'file:///dvr/fonts/FontFool/cli/')
    db.data.posts.push('hello world');

Edit... I take that back, it doesn't actually fix it, it doesn't do anything. No error, no file creation.

I also run in to this problem (running on Mac), (node.js 18 LTS).

So according to nodes url, the const myUrl = new URL(filename) isn't valid (https://nodejs.org/api/url.html#urlpathtofileurlpath).
Instead it's recommended to use url.pathToFileURL to get a file:///path/to/db. However, deeper down in the stack (in the steno package) (and also in the TextFileSync), path.join(...) is used, and at least on the Mac a path.join("file:///path/to/db") returns file:/path/to/db which also fails.

I'm using this in node environment.

Looks like JSONPreset calls new URL on the filename, which apparently means it requires a file: scheme.
Then when it's stringified and passed to fsPromises.writeFile('file:/path/to/blah') in steno/lib/index.js,
node errors with

Uncaught [Error: ENOENT: no such file or directory, open 'file:/path/to/blah']

Thanks for the report, working on a fix

I published a fix. Can you give it another try?

It seems to work! Thanks.

Awesome :)