Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: issue #213 - load chunks from a dat, prototype #214

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions lib/create-dat-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var fs = require('fs')
var url = require('url')
var path = require('path')
var Dat = require('dat-node')
var smalltalk = require('smalltalk')
var mirror = require('mirror-folder')
var ram = require('random-access-memory')

module.exports = function (collection, index, context, fileObject, cb) {
smalltalk.prompt('Enter Dat url', '', 'dat://').then(function (value) {
var datDetails = url.parse(value)
var datHash = datDetails.hostname

var folderPath = fileObject.resolvePath(datHash)
fetchDat(datHash, folderPath, function (err) {
var path = fileObject.resolvePath(datHash + '/index.json')
var externalDescriptor = {
node: 'Node 1',
scale: '$global',
node: 'externalChunk',
src: fileObject.relative(path),
id: 'dat ' + datHash.substring(0, 5),
routes: {output: '$default'}
}
var node = collection.insert(externalDescriptor, index)
awaitResolve(node.loaded, function () {
cb && cb(null, node)
})
})
})
}

function fetchDat(hash, folderPath, cb) {
fs.mkdirSync(folderPath)
Dat(ram, {key: hash, sparse: true}, function (err, dat) {
if (err) return cb(err)
var network = dat.joinNetwork()
network.once('connection', function () { console.log('Connected') })
dat.archive.metadata.update(function () {
var progress = mirror({fs: dat.archive, name: '/'}, folderPath, function (err) {
if (err) return cb(err)
dat.leaveNetwork()
dat.close(cb)
})
progress.on('put', function (src) {
console.log('Downloading', src.name)
})
})
console.log(`Downloading: ${dat.key.toString('hex')}\n`)
})
}


function awaitResolve (obs, cb) {
if (obs()) {
cb(obs())
} else {
var release = obs(function (value) {
if (release) {
release()
cb(value)
}
})
}
}
2 changes: 2 additions & 0 deletions lib/spawn-node.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var resolveAvailable = require('lib/resolve-available')
var extend = require('xtend')
var getBaseName = require('path').basename
var createDatNode = require('./create-dat-node')

module.exports = function (collection, nodeName, index, cb) {
if (typeof index === 'function') {
Expand All @@ -17,6 +18,7 @@ module.exports = function (collection, nodeName, index, cb) {
var lookup = context.nodeInfo.lookup
var info = lookup[nodeName]
if (info) {
if (info.name === 'dat') return createDatNode(collection, index, context, fileObject, cb)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet hack!

I think something like this could be made official by making it a little more generic. It could be something more like this:

if (typeof info.action === 'function') return info.action({collection, index, context, fileObject}, cb)

And then you would move createDatNode into nodes/dat-chunk/index.js.

var createDatNode = require('./create-dat-node')
module.exports = {
  name: 'dat',
  node: 'dat-chunk',
  action: (opts, cb) => {
    createDatNode(opts.collection, opts.index, opts.context, opts.fileObject, cb)
  },
  group: 'simpleChunks',
  description: 'Load a chunk from dat p2p'
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, That's a helpful pointer. I will adjust the code it to that.

var name = (info.name || 'Node') + ' 1'
var spawnDescriptor = (typeof info.spawn === 'function')
? info.spawn(context)
Expand Down
7 changes: 7 additions & 0 deletions nodes/dat-chunk/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var randomColor = require('lib/random-color')
module.exports = {
name: 'dat',
node: 'dat-chunk',
group: 'simpleChunks',
description: 'Load a chunk from dat p2p'
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"bopper": "~2.11.0",
"brace": "~0.8.0",
"bulk-require": "~0.2.1",
"dat-node": "^3.5.3",
"decibels": "^1.0.0",
"deep-equal": "^0.2.1",
"ejs": "^2.5.6",
Expand All @@ -40,6 +41,7 @@
"json-query": "^1.4.0",
"make-distortion-curve": "^1.0.0",
"micro-css": "~2.0.0",
"mirror-folder": "^2.1.1",
"mkdirp": "^0.5.0",
"mutant": "^3.20.0",
"ncp": "^2.0.0",
Expand All @@ -53,8 +55,10 @@
"pull-cat": "^1.1.11",
"pull-stream": "^3.4.5",
"pull-stream-to-stream": "github:mmckegg/pull-stream-to-stream#e436acee18b71af8e71d1b5d32eee642351517c7",
"random-access-memory": "^2.4.0",
"readable-blob-stream": "^1.1.0",
"scroll-into-view": "~1.3.1",
"smalltalk": "^2.4.1",
"strftime": "^0.9.2",
"tap-tempo": "~0.0.0",
"teoria": "~0.4.0",
Expand Down