From 278486305b163d830d61eaffe728f7a14445f899 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Sat, 10 Dec 2016 21:26:36 +0100 Subject: [PATCH] feat(object): add template support to object.new --- package.json | 5 +++-- src/api/object.js | 24 ++++++++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 3fcfa2d8a7..b218884358 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -116,4 +117,4 @@ "url": "https://github.com/ipfs/js-ipfs-api/issues" }, "homepage": "https://github.com/ipfs/js-ipfs-api" -} \ No newline at end of file +} diff --git a/src/api/object.js b/src/api/object.js index ad983ad524..1f403bc036 100644 --- a/src/api/object.js +++ b/src/api/object.js @@ -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) => { @@ -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) }