bunsenbrowser / bunsen

🔥 Bunsen Browser provides easy to use offline P2P application and file distribution that is verifiable on Android devices. https://bunsenbrowser.github.io/
117 stars 7 forks source link

New feature: fork dat #47

Open chrisekelley opened 6 years ago

chrisekelley commented 6 years ago

I've noticed in node-dat-archive that fork is not yet implemented? What are the issues in making this work? Some background:

Following the code:

from dat-archive.js forkArchive: 
newArchiveUrl = await datLibrary.forkArchive(url, {title, description, type, author}, {networked})
from datLibrary.forkArchive - https://github.com/beakerbrowser/beaker/blob/master/app/background-process/networks/dat/library.js

import pda from 'pauls-dat-api'

https://github.com/beakerbrowser/pauls-dat-api

-- snip --

export async function forkArchive (srcArchiveUrl, manifest = {}, settings = false) {
  srcArchiveUrl = fromKeyToURL(srcArchiveUrl)

  // get the old archive
  var srcArchive = getArchive(srcArchiveUrl)
  if (!srcArchive) {
    throw new Error('Invalid archive key')
  }

  // fetch old archive meta
  var srcManifest = await pda.readManifest(srcArchive).catch(_ => {})
  srcManifest = srcManifest || {}

  // override any manifest data
  var dstManifest = {
    title: (manifest.title) ? manifest.title : srcManifest.title,
    description: (manifest.description) ? manifest.description : srcManifest.description,
    type: (manifest.type) ? manifest.type : srcManifest.type,
    author: manifest.author
  }
  DAT_PRESERVED_FIELDS_ON_FORK.forEach(field => {
    if (srcManifest[field]) {
      dstManifest[field] = srcManifest[field]
    }
  })

  // create the new archive
  var dstArchiveUrl = await createNewArchive(dstManifest, settings)
  var dstArchive = getArchive(dstArchiveUrl)

  // copy files
  var ignore = ['/.dat', '/.git', '/dat.json']
  await pda.exportArchiveToArchive({
    srcArchive,
    dstArchive,
    skipUndownloadedFiles: true,
    ignore
  })

  return dstArchiveUrl
}

more on pda.readManifest(srcArchive). from https://github.com/beakerbrowser/pauls-dat-api

// helper to read the manifest into an object
function readManifest (archive, cb) {
  return maybe(cb, async function () {
    var data = await readFile(archive, DAT_MANIFEST_FILENAME)
    return JSON.parse(data.toString())
  })
}
rjcorwin commented 6 years ago

@chrisekelley From what I understand, DatArchive.fork creates a new Dat Archive and then copies the contents from the source Archive into the destination Archive. It's like git pull, a command that is a shortcut to a handful of other commands. When dat-archive-web is implemented in dat-gateway, DatArchive.fork could be an abstraction over DatArchive.create, DatArchive.readFile, and DatArchive.writeFile.

chrisekelley commented 6 years ago

Re: our discussion on what stuff should happen in the gateway, vs what should happen in the client - want to note https://dat-multiwriter-web.glitch.me - amazing all the stuff that is happening in this client!

chrisekelley commented 6 years ago

hey @rjsteinert , since dat-archive-web now supports fork can we now use it to fork dats in bunsen browser?