Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
feat(object): add template support to object.new
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Dec 10, 2016
1 parent 7ff6803 commit 2784863
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"glob": "^7.1.1",
"glob-escape": "0.0.2",
"ipfs-block": "^0.5.1",
"ipfs-unixfs": "^0.1.8",
"ipld-dag-pb": "^0.9.3",
"is-ipfs": "^0.2.1",
"isstream": "^0.1.2",
Expand Down Expand Up @@ -62,7 +63,7 @@
"eslint-plugin-react": "^6.8.0",
"gulp": "^3.9.1",
"hapi": "^16.0.1",
"interface-ipfs-core": "^0.22.0",
"interface-ipfs-core": "^0.22.1",
"ipfsd-ctl": "^0.17.0",
"pre-commit": "^1.2.0",
"socket.io": "^1.7.1",
Expand Down Expand Up @@ -116,4 +117,4 @@
"url": "https://github.com/ipfs/js-ipfs-api/issues"
},
"homepage": "https://github.com/ipfs/js-ipfs-api"
}
}
24 changes: 20 additions & 4 deletions src/api/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const LRU = require('lru-cache')
const lruOptions = {
max: 128
}

const Unixfs = require('ipfs-unixfs')
const cache = LRU(lruOptions)

module.exports = (send) => {
Expand Down Expand Up @@ -253,15 +253,31 @@ module.exports = (send) => {
args: multihash
}, callback)
}),
new: promisify((callback) => {
new: promisify((template, callback) => {
if (typeof template === 'function') {
callback = template
template = undefined
}
send({
path: 'object/new'
path: 'object/new',
args: template
}, (err, result) => {
if (err) {
return callback(err)
}

DAGNode.create(new Buffer(0), (err, node) => {
let data

if (template) {
if (template !== 'unixfs-dir') {
return callback(new Error('unkown template: ' + template))
}
data = (new Unixfs('directory')).marshal()
} else {
data = new Buffer(0)
}

DAGNode.create(data, (err, node) => {
if (err) {
return callback(err)
}
Expand Down

0 comments on commit 2784863

Please sign in to comment.