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 all 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
11 changes: 11 additions & 0 deletions lib/dat-share.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var Dat = require('dat-node')

module.exports = function (dir, done) {
Dat(dir, function (err, dat) {
if (err) return done(err)
dat.importFiles({watch: true})
dat.joinNetwork()
var link = 'dat://' + dat.key.toString('hex')
done(null, {dat: dat, link: link})
})
}
9 changes: 7 additions & 2 deletions lib/spawn-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var resolveAvailable = require('lib/resolve-available')
var uuid = require('uuid/v1')
var mkdirp = require('mkdirp')
var extend = require('xtend')
var getBaseName = require('path').basename

Expand All @@ -17,6 +19,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 All @@ -26,8 +29,10 @@ module.exports = function (collection, nodeName, index, cb) {
var externalDescriptor = (typeof info.external === 'function')
? info.external(context)
: info.external

var path = fileObject.resolvePath(name + '.json')
var rn = uuid().substring(0,5)
var folder = fileObject.resolvePath(rn)
mkdirp.sync(folder)
var path = fileObject.resolvePath(folder + '/index.json')
resolveAvailable(path, context.fs, function (err, path) {
if (err) return cb && cb(err)
var id = collection.resolveAvailable(getBaseName(path, '.json'))
Expand Down
18 changes: 17 additions & 1 deletion nodes/chromatic-chunk/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var Observ = require('mutant/value')
var Property = require('lib/property')
var Slots = require('lib/slots')
var TemplateSlot = require('lib/template-slot')
var datShare = require('lib/dat-share')

var computed = require('mutant/computed')
var lookup = require('mutant/lookup')
Expand Down Expand Up @@ -40,7 +41,22 @@ function ChromaticChunk (parentContext) {
inputs: Property([]),
outputs: Property(['output']),
params: MutantArray([]),
selectedSlotId: Observ()
selectedSlotId: Observ(),
publish: Property(),
publishedName: Property(),
publishedTags: Property(),
publishedShape: Property(),
publishedUrl: Property()
})

obs.publish(value => {
if (!value) return
obs.publishedShape.set(parentContext.shape().join(','))
var fullPath = parentContext.fileObject.resolvePath('.')
datShare(fullPath, function (err, details) {
if (err) return console.log(err)
obs.publishedUrl.set(details.link)
})
})

obs.slots.onAdd(function (slot) {
Expand Down
8 changes: 7 additions & 1 deletion nodes/chromatic-chunk/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var ParamEditor = require('lib/widgets/param-editor')
var Range = require('lib/params/range')
var ToggleButton = require('lib/params/toggle-button')
var ScaleChooser = require('lib/params/scale-chooser')
var Text = require('lib/params/text')
var QueryParam = require('lib/query-param')
var renderNode = require('lib/render-node')

Expand Down Expand Up @@ -44,7 +45,12 @@ function renderChromaticChunk (chunk) {
]),

h('h1', 'Params'),
ParamEditor(chunk)
ParamEditor(chunk),
h('h1', 'Publish'),
Text(chunk.publishedName, {placeholder: 'Published Name', size: 15}),
Text(chunk.publishedTags, {placeholder: 'bass,funk', size: 15}),
ToggleButton(chunk.publish, {title: 'publish', offTitle: 'publish'}),
h('div.publishedUrl', chunk.publishedUrl)
]),

h('div.slot', [
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)
}
}
18 changes: 17 additions & 1 deletion nodes/triggers-chunk/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var Property = require('lib/property')
var destroyAll = require('lib/destroy-all')
var resolve = require('mutant/resolve')
var MutantArray = require('mutant/array')
var datShare = require('lib/dat-share')

module.exports = TriggersChunk

Expand All @@ -16,7 +17,22 @@ function TriggersChunk (parentContext) {
inputs: Property([]),
outputs: Property([]),
params: MutantArray([]),
selectedSlotId: Property()
selectedSlotId: Property(),
publish: Property(),
publishedName: Property(),
publishedTags: Property(),
publishedShape: Property(),
publishedUrl: Property()
})

obs.publish(value => {
if (!value) return
obs.publishedShape.set(parentContext.shape().join(','))
var fullPath = parentContext.fileObject.resolvePath('.')
datShare(fullPath, function (err, details) {
if (err) return console.log(err)
obs.publishedUrl.set(details.link)
})
})

context.externalChunk = obs
Expand Down
8 changes: 7 additions & 1 deletion nodes/triggers-chunk/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var computed = require('mutant/computed')
var ParamEditor = require('lib/widgets/param-editor')

var Range = require('lib/params/range')
var Text = require('lib/params/text')
var ToggleButton = require('lib/params/toggle-button')
var IndexParam = require('lib/index-param')

Expand All @@ -30,7 +31,12 @@ function renderTriggersChunk (chunk) {
])
]),
h('h1', 'Params'),
ParamEditor(chunk)
ParamEditor(chunk),
h('h1', 'Publish'),
Text(chunk.publishedName, {placeholder: 'Published Name', size: 15}),
Text(chunk.publishedTags, {placeholder: 'bass,funk', size: 15}),
ToggleButton(chunk.publish, {title: 'publish', offTitle: 'publish'}),
h('div.publishedUrl', chunk.publishedUrl)
]),
h('div.slot', [
currentSlotEditor(chunk)
Expand Down
5 changes: 5 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,12 +55,15 @@
"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",
"through": "~2.3.4",
"uuid": "^3.1.0",
"wav": "~1.0.1",
"wave-recorder": "^2.3.0",
"web-midi": "^2.0.0",
Expand Down
14 changes: 13 additions & 1 deletion styles/chunk-node.mcss
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ ChunkNode {
div.options {
flex: 1
padding: 8px

div.publishedUrl {
user-select: text;
word-wrap: normal;
width: 300px;
text-overflow: ellipsis;
font-size: 8px;
color: white;
margin: 3px 1px;
}

}

div.slot {
Expand All @@ -17,4 +28,5 @@ ChunkNode {
overflow-y: scroll
}

}

}
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;
}