Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

refactor: object APIs write methods now return CIDs #1730

Merged
merged 10 commits into from
Nov 28, 2018
Merged
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -186,7 +186,7 @@ Commands:

### IPFS Daemon

The IPFS Daemon exposes the API defined [`http-api-spec`](https://github.com/ipfs/http-api-spec). You can use any of the IPFS HTTP-API client libraries with it, such as: [js-ipfs-api](https://github.com/ipfs/js-ipfs-api).
The IPFS Daemon exposes the API defined [`http-api-spec`](https://github.com/ipfs/http-api-spec). You can use any of the IPFS HTTP-API client libraries with it, such as: [js-ipfs-http-client](https://github.com/ipfs/js-ipfs-http-client).

If you want a programmatic way to spawn a IPFS Daemon using JavaScript, check out [ipfsd-ctl module](https://github.com/ipfs/js-ipfsd-ctl)

@@ -884,7 +884,7 @@ Listing of the main packages used in the IPFS ecosystem. There are also three sp
| [`libp2p-crypto`](//github.com/libp2p/js-libp2p-crypto) | [![npm](https://img.shields.io/npm/v/libp2p-crypto.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-crypto/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-crypto.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-crypto) | [![jenkins](https://ci.ipfs.team/buildStatus/icon?job=libp2p/js-libp2p-crypto/master)](https://ci.ipfs.team/job/libp2p/job/js-libp2p-crypto/job/master/) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-crypto/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-crypto) | [Friedel Ziegelmayer](mailto:[email protected]) |
| [`libp2p-keychain`](//github.com/libp2p/js-libp2p-keychain) | [![npm](https://img.shields.io/npm/v/libp2p-keychain.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-keychain/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-keychain.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-keychain) | N/A | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-keychain/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/js-libp2p-keychain) | [Vasco Santos](mailto:[email protected]) |
| **Generics/Utils** |
| [`ipfs-api`](//github.com/ipfs/js-ipfs-api) | [![npm](https://img.shields.io/npm/v/ipfs-api.svg?maxAge=86400&style=flat-square)](//github.com/ipfs/js-ipfs-api/releases) | [![Deps](https://david-dm.org/ipfs/js-ipfs-api.svg?style=flat-square)](https://david-dm.org/ipfs/js-ipfs-api) | [![jenkins](https://ci.ipfs.team/buildStatus/icon?job=ipfs/js-ipfs-api/master)](https://ci.ipfs.team/job/ipfs/job/js-ipfs-api/job/master/) | [![codecov](https://codecov.io/gh/ipfs/js-ipfs-api/branch/master/graph/badge.svg)](https://codecov.io/gh/ipfs/js-ipfs-api) | [Alan Shaw](mailto:[email protected]) |
| [`ipfs-http-client`](//github.com/ipfs/js-ipfs-http-client) | [![npm](https://img.shields.io/npm/v/ipfs-http-client.svg?maxAge=86400&style=flat-square)](//github.com/ipfs/js-ipfs-http-client/releases) | [![Deps](https://david-dm.org/ipfs/js-ipfs-http-client.svg?style=flat-square)](https://david-dm.org/ipfs/js-ipfs-http-client) | [![jenkins](https://ci.ipfs.team/buildStatus/icon?job=ipfs/js-ipfs-http-client/master)](https://ci.ipfs.team/job/ipfs/job/js-ipfs-http-client/job/master/) | [![codecov](https://codecov.io/gh/ipfs/js-ipfs-http-client/branch/master/graph/badge.svg)](https://codecov.io/gh/ipfs/js-ipfs-http-client) | [Alan Shaw](mailto:[email protected]) |
| [`ipfs-multipart`](//github.com/ipfs/ipfs-multipart) | [![npm](https://img.shields.io/npm/v/ipfs-multipart.svg?maxAge=86400&style=flat-square)](//github.com/ipfs/ipfs-multipart/releases) | [![Deps](https://david-dm.org/ipfs/ipfs-multipart.svg?style=flat-square)](https://david-dm.org/ipfs/ipfs-multipart) | N/A | [![codecov](https://codecov.io/gh/ipfs/ipfs-multipart/branch/master/graph/badge.svg)](https://codecov.io/gh/ipfs/ipfs-multipart) | N/A |
| [`is-ipfs`](//github.com/ipfs/is-ipfs) | [![npm](https://img.shields.io/npm/v/is-ipfs.svg?maxAge=86400&style=flat-square)](//github.com/ipfs/is-ipfs/releases) | [![Deps](https://david-dm.org/ipfs/is-ipfs.svg?style=flat-square)](https://david-dm.org/ipfs/is-ipfs) | [![jenkins](https://ci.ipfs.team/buildStatus/icon?job=ipfs/is-ipfs/master)](https://ci.ipfs.team/job/ipfs/job/is-ipfs/job/master/) | [![codecov](https://codecov.io/gh/ipfs/is-ipfs/branch/master/graph/badge.svg)](https://codecov.io/gh/ipfs/is-ipfs) | [Marcin Rataj](mailto:[email protected]) |
| [`multihashing`](//github.com/multiformats/js-multihashing) | [![npm](https://img.shields.io/npm/v/multihashing.svg?maxAge=86400&style=flat-square)](//github.com/multiformats/js-multihashing/releases) | [![Deps](https://david-dm.org/multiformats/js-multihashing.svg?style=flat-square)](https://david-dm.org/multiformats/js-multihashing) | [![jenkins](https://ci.ipfs.team/buildStatus/icon?job=multiformats/js-multihashing/master)](https://ci.ipfs.team/job/multiformats/job/js-multihashing/job/master/) | [![codecov](https://codecov.io/gh/multiformats/js-multihashing/branch/master/graph/badge.svg)](https://codecov.io/gh/multiformats/js-multihashing) | N/A |
34 changes: 17 additions & 17 deletions img/overview.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
offline mode - uses IPFS core directly
┌───────────────────────────────────────────
│ online mode - uses IPFS through http-api
┌────────────┐ │ ┌────────────────────┐ │ ┌─────────┐
│ │ │ ┌ ─ ─ ─ ─ ─ ─ │ │ │
│ CLI │───┴── ipfs-api ├──▶│ HTTP-API │───┴───▶│IPFS Core
│ │ └ ─ ─ ─ ─ ─ ─ │ │
└────────────┘ └────────────────────┘ └─────────┘
├───────────────────────────────────────────────────────────────┘
┌────────────┐
│ Tests │
└────────────┘

offline mode - uses IPFS core directly
┌────────────────────────────────────────────┐
│ online mode - uses IPFS through http-api
┌────────────┐ │ ┌─────────────┐ │ ┌─────────┐
│ │ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ │ │
│ CLI │───┴── ipfs-http-client ├──▶│ HTTP-API │───┴───▶│IPFS Core│
│ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ │
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes so much more sense now! :D

└────────────┘ └─────────────┘ └─────────┘
├───────────────────────────────────────────────────────────────┘
┌────────────┐
│ Tests │
└────────────┘
2 changes: 1 addition & 1 deletion package-list.json
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@
["libp2p/js-libp2p-keychain", "libp2p-keychain"],

"Generics/Utils",
["ipfs/js-ipfs-api", "ipfs-api"],
["ipfs/js-ipfs-http-client", "ipfs-http-client"],
["ipfs/ipfs-multipart", "ipfs-multipart"],
["ipfs/is-ipfs", "is-ipfs"],
["multiformats/js-multihashing", "multihashing"],
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -69,8 +69,8 @@
"execa": "^1.0.0",
"form-data": "^2.3.3",
"hat": "0.0.3",
"interface-ipfs-core": "~0.86.0",
"ipfsd-ctl": "ipfs/js-ipfsd-ctl#update-dag-pb-to-not-have-cid-property",
"interface-ipfs-core": "~0.88.0",
"ipfsd-ctl": "~0.40.1",
"ncp": "^2.0.0",
"qs": "^6.5.2",
"rimraf": "^2.6.2",
@@ -86,7 +86,7 @@
"boom": "^7.2.0",
"bs58": "^4.0.1",
"byteman": "^1.3.5",
"cid-tool": "~0.1.0",
"cid-tool": "~0.2.0",
"cids": "~0.5.5",
"datastore-core": "~0.6.0",
"debug": "^4.1.0",
@@ -102,10 +102,10 @@
"hoek": "^5.0.4",
"human-to-milliseconds": "^1.0.0",
"interface-datastore": "~0.6.0",
"ipfs-api": "ipfs/js-ipfs-api",
"ipfs-bitswap": "~0.21.0",
"ipfs-block": "~0.8.0",
"ipfs-block-service": "~0.15.1",
"ipfs-http-client": "^27.0.0",
"ipfs-http-response": "~0.2.1",
"ipfs-mfs": "~0.5.2",
"ipfs-multipart": "~0.1.0",
@@ -124,7 +124,7 @@
"is-stream": "^1.1.0",
"joi": "^13.4.0",
"joi-browser": "^13.4.0",
"joi-multiaddr": "^2.0.0",
"joi-multiaddr": "^3.0.0",
"libp2p": "~0.24.0",
"libp2p-bootstrap": "~0.9.3",
"libp2p-crypto": "~0.14.1",
@@ -144,7 +144,7 @@
"mkdirp": "~0.5.1",
"multiaddr": "^5.0.0",
"multiaddr-to-uri": "^4.0.0",
"multibase": "~0.5.0",
"multibase": "~0.6.0",
"multihashes": "~0.4.14",
"multihashing-async": "~0.5.1",
"once": "^1.4.0",
2 changes: 1 addition & 1 deletion src/cli/commands/block/rm.js
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ module.exports = {

handler (argv) {
if (utils.isDaemonOn()) {
// TODO implement this once `js-ipfs-api` supports it
// TODO implement this once `js-ipfs-http-client` supports it
throw new Error('rm block with daemon running is not yet implemented')
}

8 changes: 2 additions & 6 deletions src/cli/commands/object/get.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
'use strict'

const print = require('../../utils').print
const {
util: {
cid
}
} = require('ipld-dag-pb')
const dagPB = require('ipld-dag-pb')

module.exports = {
command: 'get <key>',
@@ -29,7 +25,7 @@ module.exports = {
throw err
}

cid(node, (err, result) => {
dagPB.util.cid(node, (err, result) => {
if (err) {
throw err
}
15 changes: 2 additions & 13 deletions src/cli/commands/object/new.js
Original file line number Diff line number Diff line change
@@ -4,11 +4,6 @@ const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')
const print = require('../../utils').print
const {
util: {
cid
}
} = require('ipld-dag-pb')

module.exports = {
command: 'new [<template>]',
@@ -23,18 +18,12 @@ module.exports = {
},

handler (argv) {
argv.ipfs.object.new(argv.template, (err, node) => {
argv.ipfs.object.new(argv.template, (err, cid) => {
if (err) {
throw err
}

cid(node, (err, cid) => {
if (err) {
throw err
}

print(cid.toBaseEncodedString(argv.cidBase))
})
print(cid.toBaseEncodedString(argv.cidBase))
})
}
}
17 changes: 3 additions & 14 deletions src/cli/commands/object/patch/add-link.js
Original file line number Diff line number Diff line change
@@ -3,11 +3,6 @@
const dagPB = require('ipld-dag-pb')
const DAGLink = dagPB.DAGLink
const print = require('../../../utils').print
const {
util: {
cid
}
} = require('ipld-dag-pb')

module.exports = {
command: 'add-link <root> <name> <ref>',
@@ -30,7 +25,7 @@ module.exports = {
throw err
}

cid(nodeA, (err, result) => {
dagPB.util.cid(nodeA, (err, result) => {
if (err) {
throw err
}
@@ -39,18 +34,12 @@ module.exports = {

ipfs.object.patch.addLink(argv.root, link, {
enc: 'base58'
}, (err, nodeB) => {
}, (err, cid) => {
if (err) {
throw err
}

cid(nodeB, (err, result) => {
if (err) {
throw err
}

print(result.toBaseEncodedString(argv.cidBase))
})
print(cid.toBaseEncodedString(argv.cidBase))
})
})
})
15 changes: 2 additions & 13 deletions src/cli/commands/object/patch/append-data.js
Original file line number Diff line number Diff line change
@@ -6,27 +6,16 @@ const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')
const print = require('../../../utils').print
const {
util: {
cid
}
} = require('ipld-dag-pb')

function appendData (key, data, ipfs) {
ipfs.object.patch.appendData(key, data, {
enc: 'base58'
}, (err, node) => {
}, (err, cid) => {
if (err) {
throw err
}

cid(node, (err, cid) => {
if (err) {
throw err
}

print(cid.toBaseEncodedString())
})
print(cid.toBaseEncodedString())
})
}

15 changes: 2 additions & 13 deletions src/cli/commands/object/patch/rm-link.js
Original file line number Diff line number Diff line change
@@ -4,11 +4,6 @@ const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')
const print = require('../../../utils').print
const {
util: {
cid
}
} = require('ipld-dag-pb')

module.exports = {
command: 'rm-link <root> <link>',
@@ -25,18 +20,12 @@ module.exports = {
handler (argv) {
argv.ipfs.object.patch.rmLink(argv.root, { name: argv.link }, {
enc: 'base58'
}, (err, node) => {
}, (err, cid) => {
if (err) {
throw err
}

cid(node, (err, cid) => {
if (err) {
throw err
}

print(cid.toBaseEncodedString(argv.cidBase))
})
print(cid.toBaseEncodedString(argv.cidBase))
})
}
}
15 changes: 2 additions & 13 deletions src/cli/commands/object/patch/set-data.js
Original file line number Diff line number Diff line change
@@ -6,27 +6,16 @@ const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')
const print = require('../../../utils').print
const {
util: {
cid
}
} = require('ipld-dag-pb')

function parseAndAddNode (key, data, ipfs) {
ipfs.object.patch.setData(key, data, {
enc: 'base58'
}, (err, node) => {
}, (err, cid) => {
if (err) {
throw err
}

cid(node, (err, cid) => {
if (err) {
throw err
}

print(cid.toBaseEncodedString())
})
print(cid.toBaseEncodedString())
})
}

15 changes: 2 additions & 13 deletions src/cli/commands/object/put.js
Original file line number Diff line number Diff line change
@@ -3,25 +3,14 @@
const bl = require('bl')
const fs = require('fs')
const print = require('../../utils').print
const {
util: {
cid
}
} = require('ipld-dag-pb')

function putNode (buf, enc, ipfs, cidEnc) {
ipfs.object.put(buf, { enc: enc }, (err, node) => {
ipfs.object.put(buf, { enc: enc }, (err, cid) => {
if (err) {
throw err
}

cid(node, (err, cid) => {
if (err) {
throw err
}

print(`added ${cid.toBaseEncodedString(cidEnc)}`)
})
print(`added ${cid.toBaseEncodedString(cidEnc)}`)
})
}

2 changes: 1 addition & 1 deletion src/cli/commands/object/stat.js
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ module.exports = {
throw err
}

delete stats.Hash // only for js-ipfs-api output
delete stats.Hash // only for js-ipfs-http-client output

Object.keys(stats).forEach((key) => {
print(`${key}: ${stats[key]}`)
2 changes: 1 addition & 1 deletion src/cli/utils.js
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ function getAPICtl (apiAddr) {
apiAddr = multiaddr(fs.readFileSync(apiPath).toString()).toString()
}
// Required inline to reduce startup time
const APIctl = require('ipfs-api')
const APIctl = require('ipfs-http-client')
return APIctl(apiAddr)
}

24 changes: 16 additions & 8 deletions src/core/components/files-regular.js
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ const waterfall = require('async/waterfall')
const isStream = require('is-stream')
const isSource = require('is-pull-stream').isSource
const Duplex = require('readable-stream').Duplex
const OtherBuffer = require('buffer').Buffer
const isString = require('lodash/isString')
const CID = require('cids')
const toB58String = require('multihashes').toB58String
const errCode = require('err-code')
@@ -292,15 +292,23 @@ module.exports = function (self) {

options = options || {}

const ok = Buffer.isBuffer(data) ||
isStream.readable(data) ||
Array.isArray(data) ||
OtherBuffer.isBuffer(data) ||
typeof data === 'object' ||
isSource(data)
// Buffer, pull stream or Node.js stream
const isBufferOrStream = obj => Buffer.isBuffer(obj) || isStream.readable(obj) || isSource(obj)
// An object like { content?, path? }, where content isBufferOrStream and path isString
const isContentObject = obj => {
if (typeof obj !== 'object') return false
// path is optional if content is present
if (obj.content) return isBufferOrStream(obj.content)
// path must be a non-empty string if no content
return Boolean(obj.path) && isString(obj.path)
}
// An input atom: a buffer, stream or content object
const isInput = obj => isBufferOrStream(obj) || isContentObject(obj)
// All is ok if data isInput or data is an array of isInput
const ok = isInput(data) || (Array.isArray(data) && data.every(isInput))

if (!ok) {
return callback(new Error('first arg must be a buffer, readable stream, pull stream, an object or array of objects'))
return callback(new Error('invalid input: expected buffer, readable stream, pull stream, object or array of objects'))
}

// CID v0 is for multihashes encoded with sha2-256
12 changes: 7 additions & 5 deletions src/core/components/object.js
Original file line number Diff line number Diff line change
@@ -98,7 +98,7 @@ module.exports = function object (self) {
self._preload(cid)
}

cb(null, node)
cb(null, cid)
})
})
}
@@ -150,7 +150,7 @@ module.exports = function object (self) {
self._preload(cid)
}

callback(null, node)
callback(null, cid)
})
})
}),
@@ -209,9 +209,11 @@ module.exports = function object (self) {
return callback(err)
}

self.object.get(cid, {
preload: options.preload
}, callback)
if (options.preload !== false) {
self._preload(cid)
}

callback(null, cid)
})
}
}),
13 changes: 6 additions & 7 deletions src/http/api/resources/object.js
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ exports.new = (request, reply) => {

waterfall([
(cb) => ipfs.object.new(template, cb),
(node, cb) => dagPB.util.cid(node, (err, cid) => cb(err, { node, cid }))
(cid, cb) => ipfs.object.get(cid, (err, node) => cb(err, { node, cid }))
], (err, results) => {
if (err) {
log.error(err)
@@ -200,8 +200,7 @@ exports.put = {

waterfall([
(cb) => DAGNode.create(Buffer.from(node.Data), node.Links, cb),
(node, cb) => ipfs.object.put(node, cb),
(node, cb) => dagPB.util.cid(node, (err, cid) => cb(err, { cid, node }))
(node, cb) => ipfs.object.put(node, (err, cid) => cb(err, { cid, node }))
], (err, results) => {
if (err) {
log.error(err)
@@ -366,7 +365,7 @@ exports.patchAppendData = {

waterfall([
(cb) => ipfs.object.patch.appendData(key, data, cb),
(node, cb) => dagPB.util.cid(node, (err, cid) => cb(err, { node, cid }))
(cid, cb) => ipfs.object.get(cid, (err, node) => cb(err, { node, cid }))
], (err, results) => {
if (err) {
log.error(err)
@@ -409,7 +408,7 @@ exports.patchSetData = {

waterfall([
(cb) => ipfs.object.patch.setData(key, data, cb),
(node, cb) => dagPB.util.cid(node, (err, cid) => cb(err, { node, cid }))
(cid, cb) => ipfs.object.get(cid, (err, node) => cb(err, { node, cid }))
], (err, results) => {
if (err) {
log.error(err)
@@ -477,7 +476,7 @@ exports.patchAddLink = {
waterfall([
(cb) => ipfs.object.get(ref, cb),
(node, cb) => ipfs.object.patch.addLink(root, new DAGLink(name, node.size, ref), cb),
(node, cb) => dagPB.util.cid(node, (err, cid) => cb(err, { node, cid }))
(cid, cb) => ipfs.object.get(cid, (err, node) => cb(err, { node, cid }))
], (err, results) => {
if (err) {
log.error(err)
@@ -544,7 +543,7 @@ exports.patchRmLink = {

waterfall([
(cb) => ipfs.object.patch.rmLink(root, { name: link }, cb),
(node, cb) => dagPB.util.cid(node, (err, cid) => cb(err, { node, cid }))
(cid, cb) => ipfs.object.get(cid, (err, node) => cb(err, { node, cid }))
], (err, results) => {
if (err) {
log.error(err)
2 changes: 1 addition & 1 deletion src/http/api/resources/pubsub.js
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ exports.subscribe = {
}) + '\n', 'utf8')
}

// js-ipfs-api needs a reply, and go-ipfs does the same thing
// js-ipfs-http-client needs a reply, and go-ipfs does the same thing
res.write('{}\n')

const unsubscribe = () => {
45 changes: 14 additions & 31 deletions test/core/object.spec.js
Original file line number Diff line number Diff line change
@@ -11,11 +11,6 @@ const IPFSFactory = require('ipfsd-ctl')
const auto = require('async/auto')
const waterfall = require('async/waterfall')
const IPFS = require('../../src/core')
const {
util: {
cid
}
} = require('ipld-dag-pb')

describe('object', () => {
let ipfsd, ipfs
@@ -55,16 +50,12 @@ describe('object', () => {
})

it('should not error when passed null options', (done) => {
ipfs.object.put(Buffer.from(hat()), (err, dagNode) => {
ipfs.object.put(Buffer.from(hat()), (err, cid) => {
expect(err).to.not.exist()

cid(dagNode, (err, result) => {
ipfs.object.get(cid, null, (err) => {
expect(err).to.not.exist()

ipfs.object.get(result, null, (err) => {
expect(err).to.not.exist()
done()
})
done()
})
})
})
@@ -85,13 +76,13 @@ describe('object', () => {
a: (cb) => {
waterfall([
(done) => ipfs.object.put(Buffer.from(hat()), done),
(node, done) => cid(node, (err, cid) => done(err, { node, cid }))
(cid, done) => ipfs.object.get(cid, (err, node) => done(err, { node, cid }))
], cb)
},
b: (cb) => {
waterfall([
(done) => ipfs.object.put(Buffer.from(hat()), done),
(node, done) => cid(node, (err, cid) => done(err, { node, cid }))
(cid, done) => ipfs.object.get(cid, (err, node) => done(err, { node, cid }))
], cb)
}
}, (err, results) => {
@@ -117,13 +108,13 @@ describe('object', () => {
nodeA: (cb) => {
waterfall([
(done) => ipfs.object.put(Buffer.from(hat()), done),
(node, done) => cid(node, (err, cid) => done(err, { node, cid }))
(cid, done) => ipfs.object.get(cid, (err, node) => done(err, { node, cid }))
], cb)
},
nodeB: (cb) => {
waterfall([
(done) => ipfs.object.put(Buffer.from(hat()), done),
(node, done) => cid(node, (err, cid) => done(err, { node, cid }))
(cid, done) => ipfs.object.get(cid, (err, node) => done(err, { node, cid }))
], cb)
},
nodeAWithLink: ['nodeA', 'nodeB', (res, cb) => {
@@ -133,7 +124,7 @@ describe('object', () => {
multihash: res.nodeB.cid,
size: res.nodeB.node.size
}, done),
(node, done) => cid(node, (err, cid) => done(err, { node, cid }))
(cid, done) => ipfs.object.get(cid, (err, node) => done(err, { node, cid }))
], cb)
}]
}, (err, res) => {
@@ -150,33 +141,25 @@ describe('object', () => {

describe('patch.appendData', () => {
it('should not error when passed null options', (done) => {
ipfs.object.put(Buffer.from(hat()), null, (err, dagNode) => {
ipfs.object.put(Buffer.from(hat()), null, (err, cid) => {
expect(err).to.not.exist()

cid(dagNode, (err, result) => {
ipfs.object.patch.appendData(cid, Buffer.from(hat()), null, (err) => {
expect(err).to.not.exist()

ipfs.object.patch.appendData(result, Buffer.from(hat()), null, (err) => {
expect(err).to.not.exist()
done()
})
done()
})
})
})
})

describe('patch.setData', () => {
it('should not error when passed null options', (done) => {
ipfs.object.put(Buffer.from(hat()), null, (err, dagNode) => {
ipfs.object.put(Buffer.from(hat()), null, (err, cid) => {
expect(err).to.not.exist()

cid(dagNode, (err, result) => {
ipfs.object.patch.setData(cid, Buffer.from(hat()), null, (err) => {
expect(err).to.not.exist()

ipfs.object.patch.setData(result, Buffer.from(hat()), null, (err) => {
expect(err).to.not.exist()
done()
})
done()
})
})
})
94 changes: 21 additions & 73 deletions test/core/preload.spec.js
Original file line number Diff line number Diff line change
@@ -9,11 +9,6 @@ const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const {
util: {
cid
}
} = require('ipld-dag-pb')

const MockPreloadNode = require('../utils/mock-preload-node')
const IPFS = require('../../src')
@@ -158,26 +153,16 @@ describe('preload', () => {
})

it('should preload content added with object.new', (done) => {
ipfs.object.new((err, node) => {
ipfs.object.new((err, cid) => {
expect(err).to.not.exist()

cid(node, (err, result) => {
expect(err).to.not.exist()

MockPreloadNode.waitForCids(result.toBaseEncodedString(), done)
})
MockPreloadNode.waitForCids(cid.toBaseEncodedString(), done)
})
})

it('should preload content added with object.put', (done) => {
ipfs.object.put({ Data: Buffer.from(hat()), Links: [] }, (err, node) => {
ipfs.object.put({ Data: Buffer.from(hat()), Links: [] }, (err, cid) => {
expect(err).to.not.exist()

cid(node, (err, result) => {
expect(err).to.not.exist()

MockPreloadNode.waitForCids(result.toBaseEncodedString(), done)
})
MockPreloadNode.waitForCids(cid.toBaseEncodedString(), done)
})
})

@@ -186,13 +171,13 @@ describe('preload', () => {
parent: (cb) => {
waterfall([
(done) => ipfs.object.put({ Data: Buffer.from(hat()), Links: [] }, done),
(node, done) => cid(node, (err, cid) => done(err, { node, cid }))
(cid, done) => ipfs.object.get(cid, (err, node) => done(err, { node, cid }))
], cb)
},
link: (cb) => {
waterfall([
(done) => ipfs.object.put({ Data: Buffer.from(hat()), Links: [] }, done),
(node, done) => cid(node, (err, cid) => done(err, { node, cid }))
(cid, done) => ipfs.object.get(cid, (err, node) => done(err, { node, cid }))
], cb)
}
}, (err, result) => {
@@ -202,22 +187,17 @@ describe('preload', () => {
name: 'link',
cid: result.link.cid,
size: result.link.node.size
}, (err, node) => {
}, (err, cid) => {
expect(err).to.not.exist()

cid(node, (err, result) => {
expect(err).to.not.exist()

MockPreloadNode.waitForCids(result.toBaseEncodedString(), done)
})
MockPreloadNode.waitForCids(cid.toBaseEncodedString(), done)
})
})
})

it('should preload content added with object.patch.rmLink', (done) => {
waterfall([
(cb) => ipfs.object.put({ Data: Buffer.from(hat()), Links: [] }, cb),
(node, cb) => cid(node, (err, cid) => cb(err, { node, cid })),
(cid, cb) => ipfs.object.get(cid, (err, node) => cb(err, { node, cid })),
({ node, cid }, cb) => {
ipfs.object.put({
Data: Buffer.from(hat()),
@@ -228,77 +208,45 @@ describe('preload', () => {
}]
}, cb)
}
], (err, parent) => {
], (err, parentCid) => {
expect(err).to.not.exist()

cid(parent, (err, result) => {
ipfs.object.patch.rmLink(parentCid, { name: 'link' }, (err, cid) => {
expect(err).to.not.exist()

ipfs.object.patch.rmLink(result, { name: 'link' }, (err, node) => {
expect(err).to.not.exist()

cid(node, (err, result) => {
expect(err).to.not.exist()

MockPreloadNode.waitForCids(result.toBaseEncodedString(), done)
})
})
MockPreloadNode.waitForCids(cid.toBaseEncodedString(), done)
})
})
})

it('should preload content added with object.patch.setData', (done) => {
ipfs.object.put({ Data: Buffer.from(hat()), Links: [] }, (err, node) => {
ipfs.object.put({ Data: Buffer.from(hat()), Links: [] }, (err, cid) => {
expect(err).to.not.exist()

cid(node, (err, result) => {
ipfs.object.patch.setData(cid, Buffer.from(hat()), (err, cid) => {
expect(err).to.not.exist()

ipfs.object.patch.setData(result, Buffer.from(hat()), (err, node) => {
expect(err).to.not.exist()

cid(node, (err, result) => {
expect(err).to.not.exist()

MockPreloadNode.waitForCids(result.toBaseEncodedString(), done)
})
})
MockPreloadNode.waitForCids(cid.toBaseEncodedString(), done)
})
})
})

it('should preload content added with object.patch.appendData', (done) => {
ipfs.object.put({ Data: Buffer.from(hat()), Links: [] }, (err, node) => {
ipfs.object.put({ Data: Buffer.from(hat()), Links: [] }, (err, cid) => {
expect(err).to.not.exist()

cid(node, (err, result) => {
ipfs.object.patch.appendData(cid, Buffer.from(hat()), (err, cid) => {
expect(err).to.not.exist()

ipfs.object.patch.appendData(result, Buffer.from(hat()), (err, node) => {
expect(err).to.not.exist()

cid(node, (err, result) => {
expect(err).to.not.exist()

MockPreloadNode.waitForCids(result.toBaseEncodedString(), done)
})
})
MockPreloadNode.waitForCids(cid.toBaseEncodedString(), done)
})
})
})

it('should preload content retrieved with object.get', (done) => {
ipfs.object.new(null, { preload: false }, (err, node) => {
ipfs.object.new(null, { preload: false }, (err, cid) => {
expect(err).to.not.exist()

cid(node, (err, result) => {
ipfs.object.get(cid, (err) => {
expect(err).to.not.exist()

ipfs.object.get(result, (err) => {
expect(err).to.not.exist()

MockPreloadNode.waitForCids(result.toBaseEncodedString(), done)
})
MockPreloadNode.waitForCids(cid.toBaseEncodedString(), done)
})
})
})
2 changes: 1 addition & 1 deletion test/http-api/interface.js
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
const tests = require('interface-ipfs-core')
const CommonFactory = require('../utils/interface-common-factory')

describe('interface-ipfs-core over ipfs-api tests', () => {
describe('interface-ipfs-core over ipfs-http-client tests', () => {
const defaultCommonFactory = CommonFactory.create({
factoryOptions: { exec: 'src/cli/bin.js' }
})
79 changes: 40 additions & 39 deletions test/http-api/object.js
Original file line number Diff line number Diff line change
@@ -43,17 +43,17 @@ describe('object endpoint', () => {

describe('.object', () => {
it('.new', (done) => {
ipfs.object.new(asJson((err, res) => {
ipfs.object.new((err, cid) => {
expect(err).to.not.exist()
expect(res.links).to.be.eql([])
expect(cid.toBaseEncodedString())
.to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')

dagPB.util.cid(res, (err, cid) => {
ipfs.object.get(cid, asJson((err, res) => {
expect(err).to.not.exist()
expect(cid.toBaseEncodedString())
.to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
expect(res.links).to.be.eql([])
done()
})
}))
}))
})
})

describe('.get', () => {
@@ -103,17 +103,17 @@ describe('object endpoint', () => {
size: 68
}

ipfs.object.put(filePath, { enc: 'json' }, asJson((err, res) => {
ipfs.object.put(filePath, { enc: 'json' }, (err, cid) => {
expect(err).to.not.exist()
expect(res).to.eql(expectedResult)
expect(cid.toBaseEncodedString())
.to.equal('QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm')

dagPB.util.cid(res, (err, cid) => {
ipfs.object.get(cid, asJson((err, res) => {
expect(err).to.not.exist()
expect(cid.toBaseEncodedString())
.to.equal('QmZZmY4KCu9r3e7M2Pcn46Fc5qbn6NpzaAGaYb22kbfTqm')
expect(res).to.eql(expectedResult)
done()
})
}))
}))
})
})
})

@@ -226,17 +226,17 @@ describe('object endpoint', () => {
size: 19
}

ipfs.object.patch.appendData(key, filePath, { enc: 'base58' }, asJson((err, res) => {
ipfs.object.patch.appendData(key, filePath, { enc: 'base58' }, (err, cid) => {
expect(err).to.not.exist()
expect(res).to.eql(expectedResult)
expect(cid.toBaseEncodedString())
.to.equal('QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6')

dagPB.util.cid(res, (err, cid) => {
ipfs.object.get(cid, asJson((err, res) => {
expect(err).to.not.exist()
expect(cid.toBaseEncodedString())
.to.equal('QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6')
expect(res).to.eql(expectedResult)
done()
})
}))
}))
})
})
})

@@ -266,17 +266,17 @@ describe('object endpoint', () => {
size: 19
}

ipfs.object.patch.setData(key, filePath, { enc: 'base58' }, asJson((err, res) => {
ipfs.object.patch.setData(key, filePath, { enc: 'base58' }, (err, cid) => {
expect(err).to.not.exist()
expect(res).to.eql(expectedResult)
expect(cid.toBaseEncodedString())
.to.equal('QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6')

dagPB.util.cid(res, (err, cid) => {
ipfs.object.get(cid, asJson((err, res) => {
expect(err).to.not.exist()
expect(cid.toBaseEncodedString())
.to.equal('QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6')
expect(res).to.eql(expectedResult)
done()
})
}))
}))
})
})
})

@@ -311,21 +311,22 @@ describe('object endpoint', () => {
const name = 'foo'
const ref = 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
const link = new DAGLink(name, 10, ref)
ipfs.object.patch.addLink(root, link, { enc: 'base58' }, asJson((err, res) => {

ipfs.object.patch.addLink(root, link, { enc: 'base58' }, (err, cid) => {
expect(err).not.to.exist()
expect(res.links[0]).to.eql({
name: 'foo',
cid: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn',
size: 4
})
expect(cid.toBaseEncodedString())
.to.equal('QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK')

dagPB.util.cid(res, (err, cid) => {
ipfs.object.get(cid, asJson((err, res) => {
expect(err).to.not.exist()
expect(cid.toBaseEncodedString())
.to.equal('QmdVHE8fUD6FLNLugtNxqDFyhaCgdob372hs6BYEe75VAK')
expect(res.links[0]).to.eql({
name: 'foo',
cid: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn',
size: 4
})
done()
})
}))
}))
})
})
})

4 changes: 2 additions & 2 deletions test/utils/interface-common-factory.js
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@

const each = require('async/each')
const IPFSFactory = require('ipfsd-ctl')
const IpfsApi = require('ipfs-api')
const ipfsClient = require('ipfs-http-client')
const IPFS = require('../../src')

function createFactory (options) {
@@ -13,7 +13,7 @@ function createFactory (options) {
options.spawnOptions = options.spawnOptions || { initOptions: { bits: 512 }, config: { Bootstrap: [] } }

if (options.factoryOptions.type !== 'proc') {
options.factoryOptions.IpfsApi = options.factoryOptions.IpfsApi || IpfsApi
options.factoryOptions.IpfsApi = options.factoryOptions.IpfsApi || ipfsClient
}

const ipfsFactory = IPFSFactory.create(options.factoryOptions)