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

Commit 2784863

Browse files
feat(object): add template support to object.new
1 parent 7ff6803 commit 2784863

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"glob": "^7.1.1",
3232
"glob-escape": "0.0.2",
3333
"ipfs-block": "^0.5.1",
34+
"ipfs-unixfs": "^0.1.8",
3435
"ipld-dag-pb": "^0.9.3",
3536
"is-ipfs": "^0.2.1",
3637
"isstream": "^0.1.2",
@@ -62,7 +63,7 @@
6263
"eslint-plugin-react": "^6.8.0",
6364
"gulp": "^3.9.1",
6465
"hapi": "^16.0.1",
65-
"interface-ipfs-core": "^0.22.0",
66+
"interface-ipfs-core": "^0.22.1",
6667
"ipfsd-ctl": "^0.17.0",
6768
"pre-commit": "^1.2.0",
6869
"socket.io": "^1.7.1",
@@ -116,4 +117,4 @@
116117
"url": "https://github.com/ipfs/js-ipfs-api/issues"
117118
},
118119
"homepage": "https://github.com/ipfs/js-ipfs-api"
119-
}
120+
}

src/api/object.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const LRU = require('lru-cache')
1111
const lruOptions = {
1212
max: 128
1313
}
14-
14+
const Unixfs = require('ipfs-unixfs')
1515
const cache = LRU(lruOptions)
1616

1717
module.exports = (send) => {
@@ -253,15 +253,31 @@ module.exports = (send) => {
253253
args: multihash
254254
}, callback)
255255
}),
256-
new: promisify((callback) => {
256+
new: promisify((template, callback) => {
257+
if (typeof template === 'function') {
258+
callback = template
259+
template = undefined
260+
}
257261
send({
258-
path: 'object/new'
262+
path: 'object/new',
263+
args: template
259264
}, (err, result) => {
260265
if (err) {
261266
return callback(err)
262267
}
263268

264-
DAGNode.create(new Buffer(0), (err, node) => {
269+
let data
270+
271+
if (template) {
272+
if (template !== 'unixfs-dir') {
273+
return callback(new Error('unkown template: ' + template))
274+
}
275+
data = (new Unixfs('directory')).marshal()
276+
} else {
277+
data = new Buffer(0)
278+
}
279+
280+
DAGNode.create(data, (err, node) => {
265281
if (err) {
266282
return callback(err)
267283
}

0 commit comments

Comments
 (0)