amark / gun

An open source cybersecurity protocol for syncing decentralized graph data.
https://gun.eco/docs
Other
18.14k stars 1.17k forks source link

Specify absolute path in file parameter raise a stats error #1152

Open Choko256 opened 3 years ago

Choko256 commented 3 years ago

In the Gun constructor on NodeJS, specify an absolute path in the file parameter will result in a stats file creation error.

In my example, I'm building an Electron app with Gun, and specifying something like :

const path = require('path')
const { app } = require('electron')

const gun = new Gun({
  file : path.resolve(app.getPath('userData'), 'gun-data')
})

The specified folder will successfully be created by Radisk, the structure is there, but every second a big error is logged in the console :

[Error: ENOENT: no such file or directory, open 'C:\Users\MyUser\myproject\node_modules\gun\stats.C:\Users\MyUser\AppData\Roaming\myproject\gun-data'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\\Users\\MyUser\\myproject\\node_modules\\gun\\stats.C:\\Users\\MyUser\\AppData\\Roaming\\myproject\\gun-data'
}

Why would I want an absolute path ? On MacOS, applications are embedded within a .app file. The contents inside these files are read-only, so when Radisk tries to create the folder relative to the process, it's failing.

amark commented 3 years ago

@Choko256 ah! Great observation. Making sure cross-everything compatibility/environment is supported is extremely important to me, tho this particular fix is not on an urgent horizon and this is pretty standard NodeJS filesystem stuff, would you be willing to PR the fix?

Offending line is https://github.com/amark/gun/blob/master/lib/rfs.js#L44 I think? (hmm, tho maybe its actually also a bug in the stats code?)

Crash course: Seems you already figured out GUN is in-memory and relies on storage adapters, of which RAD was made default with radisk and the fs plugin to it called lib/rfs.js which is a tiny < 100 file which should fix this for all local filesystem usages (such as Electron, etc.). So this should be a safe easy spot to PR I hope!

Sorry for the delay, been catching up from big legal thing early last week.