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 4 commits
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
1 change: 1 addition & 0 deletions lib/spawn-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = function (collection, nodeName, index, cb) {
var lookup = context.nodeInfo.lookup
var info = lookup[nodeName]
if (info) {
if (typeof info.action === 'function') return info.action({collection, index, context, fileObject}, cb)
var name = (info.name || 'Node') + ' 1'
var spawnDescriptor = (typeof info.spawn === 'function')
? info.spawn(context)
Expand Down
65 changes: 65 additions & 0 deletions nodes/dat-chunk/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',
Copy link
Owner

@mmckegg mmckegg Sep 19, 2017

Choose a reason for hiding this comment

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

Like I mentioned in this comment, you might want to include some of these params (particularly shape) in the export itself.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, thats a good idea. I need to start thinking of how to add meta to the chunk so publishers can help people find them. I think for the exported chunk, I will add a property to the index.json called "published". So it can contain published metadata that can be used by the externalDescriptor

so the file might look like this at the top
e9569d6ff35812c3269e55b3be144512ac6bad1f3ec8510fd389afd0fdcd202a/index.json

{
  "published": {
    "name": "large drumkit",
    "publisher": "Ryan Ramage",
    "tags": ["drumkit", "samples"], 
    "shape": [8, 8]
    "color": [251, 141, 197],
    "paramValues": { "dist": 0, "delayinfit": 0 },
    "volume": 1.4428
  },
  slots: {
    ...

Later we can start indexing this info into a db and make a search that can help people find stuff.

I think I want to add a 'publish' button to the tab that appears when you edit a chunk.

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)
}
})
}
}
12 changes: 12 additions & 0 deletions nodes/dat-chunk/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var createDatNode = require('./create-dat-node')

module.exports = {
name: 'dat',
node: 'dat-chunk',
group: 'simpleChunks',
description: 'Load a chunk from dat p2p',
action: (opts, cb) => {
console.log('spwan', opts)
createDatNode(opts.collection, opts.index, opts.context, opts.fileObject, cb)
}
}
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
14 changes: 14 additions & 0 deletions styles/smalltalk.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.smalltalk {
position: absolute;
z-index: 1;
border: 10px solid springgreen;
background: whitesmoke;
padding: 10px;
border-radius: 10px;
margin-left: 40%;
margin-top: 300px;
}

.smalltalk input {
width: 380px;
}