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

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ipfs-inactive/js-ipfs-http-client
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: a9833840eb68^
Choose a base ref
...
head repository: ipfs-inactive/js-ipfs-http-client
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 718bddef9be9
Choose a head ref
  • 2 commits
  • 6 files changed
  • 1 contributor

Commits on Oct 11, 2015

  1. Copy the full SHA
    a983384 View commit details
  2. files api tests

    daviddias committed Oct 11, 2015
    Copy the full SHA
    718bdde View commit details
Showing with 198 additions and 19 deletions.
  1. +35 −0 README.md
  2. +12 −0 examples/files-api.js
  3. +1 −0 package.json
  4. +17 −0 src/index.js
  5. +3 −1 src/request-api.js
  6. +130 −18 test/test.js
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -215,3 +215,38 @@ curl 'http://localhost:5001/api/v0/object/get?arg=QmYEqnfCZp7a39Gxrgyv3qRS4MoCTG
#### Pin

#### Gateway

#### Files

##### mkdir

bash:
`curl "http://localhost:5001/api/v0/files/mkdir?arg=%2F<folder name>`"

response: (it returns empty when successful)
```
```

javascript:
```JavaScript
ipfs.files.mkdir(<folderName>, function (err) {})
```

response: (it returns empty when successful)
```
```

##### cp

##### ls

##### stat

##### rm

##### read

##### write

##### mv
curl "http://localhost:5001/api/v0/files/mkdir?arg=%2Ffolder4"
12 changes: 12 additions & 0 deletions examples/files-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var ipfs = require('../src')('localhost', 5001)

ipfs.files.ls('/folder1', function (err, res) {
if (err) {
return console.log(err)
}
if (res.readable) {
res.pipe(process.stdout)
} else {
console.log(res)
}
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@
},
"scripts": {
"test": "./node_modules/.bin/mocha",
"test-usn": "USE_SYSTEM_NODE='true' ./node_modules/.bin/mocha",
"lint": "./node_modules/.bin/standard",
"build": "./node_modules/.bin/browserify -t brfs -s ipfsAPI -e ./src/index.js | tee dist/ipfsapi.js | ./node_modules/.bin/uglifyjs -m > dist/ipfsapi.min.js"
},
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -233,4 +233,21 @@ function IpfsAPI (host_or_multiaddr, port) {
return requestAPI('dht/put', [key, value], opts, null, cb)
}
}

self.files = {
cp: argCommand('files/cp'),
ls: argCommand('files/ls'),
mkdir: argCommand('files/mkdir'),
stat: argCommand('files/stat'),
rm: function (key, value, opts, cb) {
if (typeof (opts) === 'function') {
cb = opts
opts = {}
}
return requestAPI('config', [key, value], opts, null, cb)
},
read: argCommand('files/read'),
write: argCommand('files/write'),
mv: argCommand('files/mv')
}
}
4 changes: 3 additions & 1 deletion src/request-api.js
Original file line number Diff line number Diff line change
@@ -83,7 +83,9 @@ function requestAPI (path, args, opts, files, buffer, cb) {
}

if (res.statusCode >= 400 || !res.statusCode) {
if (!data) data = new Error()
if (!data) {
data = new Error('Response error:' + res.statusCode)
}
return cb(data, null)
}
return cb(null, data)
148 changes: 130 additions & 18 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -22,19 +22,25 @@ describe('ipfs node api', function () {
this.timeout(20000)
log('ipfs node setup')

ipfsd.disposable(function (err, node) {
if (err) throw err
log('ipfs init done')
ipfsNode = node

ipfsNode.startDaemon(function (err, ignore) {
if (!process.env.USE_SYSTEM_NODE) {
ipfsd.disposable(function (err, node) {
if (err) throw err
log('ipfs daemon running')
log('ipfs init done')
ipfsNode = node

ipfs = ipfsApi(ipfsNode.apiAddr)
done()
ipfsNode.startDaemon(function (err, ignore) {
if (err) throw err
log('ipfs daemon running')

ipfs = ipfsApi(ipfsNode.apiAddr)
done()
})
})
})
} else {
log('NOTE: some tests will not pass if not connected to other nodes( ls, refs for e.g')
ipfs = ipfsApi('localhost', 5001)
done()
}
})

it('has the api object', function () {
@@ -287,17 +293,123 @@ describe('ipfs node api', function () {
})
})

it('test for error after daemon stops', function (done) {
this.timeout(6000)
var nodeStopped
ipfsNode.stopDaemon(function () {
if (!nodeStopped) {
nodeStopped = true
ipfs.id(function (err, res) {
assert.equal(err.code, 'ECONNREFUSED')
// [x] files.mkdir
// [x] files.cp
// [x] files.ls
// [x] files.stat
// [ ] files.read
// [ ] files.write
// [ ] files.mv
// [x] files.rm

// added on the add test 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'

// --- files api tests

it('files.mkdir', function (done) {
this.timeout(20000)

ipfs.files.mkdir('/test-folder', function (err) {
console.log('->', err)
assert(!err)
if (err) {
return done()
}
done()
})
})

it('files.cp', function (done) {
this.timeout(20000)

ipfs.files
.cp(['/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', '/test-folder/test-file'], function (err) {
assert(!err)
if (err) {
return done()
}
done()
})
})

it('files.ls', function (done) {
this.timeout(20000)

ipfs.files.ls('/test-folder', function (err, res) {
assert(!err)
if (err) {
return done()
}
assert.equal(res.Entries.length, 1)
done()
})
})

it('files.stat', function (done) {
this.timeout(20000)

ipfs.files.stat('/test-folder/test-file', function (err, res) {
assert(!err)
if (err) {
return done()
}
assert.deepEqual(res, {
Hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
Size: 12,
CumulativeSize: 20,
Blocks: 0
})

done()
})
})

// -

it('files.read', function (done) {
this.timeout(20000)

ipfs.files.read('/test-folder/test-file', function (err, stream) {
if (err) throw err
var buf = ''
stream
.on('error', function (err) { throw err })
.on('data', function (data) { buf += data })
.on('end', function () {
assert.equal(buf, fs.readFileSync(testfile))
done()
})
})
})

// -

it('files.rm', function (done) {
this.timeout(20000)

// TODO fix the recursive qs value based on Jeromy input
ipfs.files.rm('/test-folder', { '-r': true }, function (err) {
assert(!err)
if (err) {
return done()
}
done()
})
})

it('test for error after daemon stops', function (done) {
if (!process.env.USE_SYSTEM_NODE) {
this.timeout(6000)
var nodeStopped
ipfsNode.stopDaemon(function () {
if (!nodeStopped) {
nodeStopped = true
ipfs.id(function (err, res) {
assert.equal(err.code, 'ECONNREFUSED')
done()
})
}
})
} else { done() }
})
})