dat-node icon indicating copy to clipboard operation
dat-node copied to clipboard

dat.archive.writeFile results in empty file

Open joehand opened this issue 6 years ago • 2 comments

Writing a file from archive in dat-node results in empty file.

var Dat = require('dat')

var filePath = 'hello.txt'
var fileContent = 'echo 123'

Dat('./test', function (err, dat) {
  if (err) throw err

  dat.archive.writeFile(filePath, fileContent, function (err) {
    if (err) throw err
    console.log('done')
  })
})

However, this works with plain hyperdrive and dat-storage:

var hyperdrive = require('hyperdrive')
var storage = require('dat-storage')

var filePath = 'hello.txt'
var fileContent = 'echo 123'
var archive = hyperdrive(storage('./test'), {latest: true})

archive.writeFile(filePath, fileContent, function (err) {
  if (err) throw err
  console.log('done')
})

joehand avatar Jul 19 '17 17:07 joehand

@benrogmans you can address this for now by using the indexing option:

var Dat = require('dat')

var filePath = 'hello.txt'
var fileContent = 'echo 123'

Dat('./test', {indexing: false}, function (err, dat) {
  if (err) throw err

  dat.archive.writeFile(filePath, fileContent, function (err) {
    if (err) throw err
    console.log('done')
  })
})

Sorry, this is a bit hacky. Normally in dat-node we import "in-place" (e.g. files are just being scanned, not created). If files aren't imported in place, we set the indexing false. But those assumptions all fail in this case.

I may make false the default since this is definitely unexpected and not user friendly. But we had some unresolved TODOs around the indexing that I need to refresh myself on to see if we can solve more directly.

I thought this would also fix #164 but that seems to be a separate issue.

joehand avatar Jul 20 '17 17:07 joehand

found out more about this issue here: https://github.com/datproject/dat-storage/issues/7.

With { indexing: true }, not just the files aren't created on disk, a following dat.archive.readFile() also can't read them.

This is a temporary fix for multidat: https://github.com/datproject/multidat/commit/11b702bc7a7b312b9c42387ff4e9b594885bce3d

juliangruber avatar Jan 26 '18 11:01 juliangruber