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

feat(object): add template support to object.new #476

Merged
merged 1 commit into from
Dec 12, 2016
Merged
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
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