From db00682c099caa02e982c156b7290f8ac6e724cc Mon Sep 17 00:00:00 2001 From: Matt Ober Date: Thu, 25 Jul 2019 17:11:56 -0500 Subject: [PATCH 1/7] Added in a test for the opts parameter. The opts parameter wasn't correctly passed in when calling ipfs.object.stat. This is the test that makes sure opts is being correctly passed to through the send function to the IPFS daemon. I've also removed the "base58" encoding that was being passed in for some tests as base58 isn't a valid option for encoding and would provide this error: Uncaught AssertionError: expected [Error: invalid encoding: base58] to not exist --- src/object/stat.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/object/stat.js b/src/object/stat.js index 285300bad..92a53bf3b 100644 --- a/src/object/stat.js +++ b/src/object/stat.js @@ -67,7 +67,7 @@ module.exports = (createCommon, options) => { } await ipfs.object.put(testObj) - const stats = await ipfs.object.stat('QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', { enc: 'base58' }) + const stats = await ipfs.object.stat('QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ') const expected = { Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', @@ -80,6 +80,30 @@ module.exports = (createCommon, options) => { expect(expected).to.deep.equal(stats) }) + + it('should pass on the opts', (done) => { + const testObj = { + Data: Buffer.from('get test object'), + Links: [] + } + + ipfs.object.put(testObj, (err, cid) => { + expect(err).to.not.exist() + const timeout = 2; + const startTime = new Date(); + const badCid = 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3MzzzzzZ'; + + //we can test that we are passing in opts by testing the timeout option for a CID that doesn't exist + ipfs.object.stat(badCid, {timeout: `${timeout}s`}, (err, stats) => { + let timeForRequest = (new Date () - startTime)/1000; + console.log(err); + expect(err.message).to.equal("failed to get block for QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3MzzzzzZ: context deadline exceeded"); + expect(stats).to.not.exist() + expect(timeForRequest).to.not.lessThan(timeout); + done() + }) + }) + }) it('should get stats for object with links by multihash', (done) => { let node1a @@ -151,7 +175,7 @@ module.exports = (createCommon, options) => { ipfs.object.put(testObj, (err, cid) => { expect(err).to.not.exist() - ipfs.object.stat(cid.buffer, { enc: 'base58' }, (err, stats) => { + ipfs.object.stat(cid.buffer, (err, stats) => { expect(err).to.not.exist() const expected = { Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', @@ -176,7 +200,7 @@ module.exports = (createCommon, options) => { ipfs.object.put(testObj, (err, cid) => { expect(err).to.not.exist() - ipfs.object.stat(cid.toBaseEncodedString(), { enc: 'base58' }, (err, stats) => { + ipfs.object.stat(cid.toBaseEncodedString(), (err, stats) => { expect(err).to.not.exist() const expected = { Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', From 32e72888a1406bf03f29b9972f6cb40c4e5f65c6 Mon Sep 17 00:00:00 2001 From: Matt Ober Date: Thu, 25 Jul 2019 17:18:05 -0500 Subject: [PATCH 2/7] Updated the readme --- SPEC/OBJECT.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/SPEC/OBJECT.md b/SPEC/OBJECT.md index 1d34ca096..cbf55cb49 100644 --- a/SPEC/OBJECT.md +++ b/SPEC/OBJECT.md @@ -198,7 +198,8 @@ A great source of [examples][] can be found in the tests for this API. `options` is a optional argument of type object, that can contain the following properties: -- `enc`, the encoding of multihash (base58, base64, etc), if any. +- `enc`, The encoding type the output should beencoded with (json, xml, or text), if any. +- `timeout`, A timeout to pass to the IPFS daemon so the request expires after a certain amount of time. `callback` must follow `function (err, stats) {}` signature, where `err` is an error if the operation was not successful and `stats` is an Object with following format: @@ -220,7 +221,7 @@ If no `callback` is passed, a [promise][] is returned. ```JavaScript const multihash = 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD' -ipfs.object.stat(multihash, (err, stats) => { +ipfs.object.stat(multihash, {timeout: '10s'}, (err, stats) => { if (err) { throw err } From 7a46b50405e312273a7256d79774e58c1fdce663 Mon Sep 17 00:00:00 2001 From: Matt Ober Date: Fri, 26 Jul 2019 08:31:41 -0500 Subject: [PATCH 3/7] updated for linting --- src/object/stat.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/object/stat.js b/src/object/stat.js index 92a53bf3b..f9ba466cc 100644 --- a/src/object/stat.js +++ b/src/object/stat.js @@ -80,26 +80,26 @@ module.exports = (createCommon, options) => { expect(expected).to.deep.equal(stats) }) - + it('should pass on the opts', (done) => { const testObj = { Data: Buffer.from('get test object'), Links: [] } - ipfs.object.put(testObj, (err, cid) => { + ipfs.object.put(testObj, (err) => { expect(err).to.not.exist() - const timeout = 2; - const startTime = new Date(); - const badCid = 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3MzzzzzZ'; - - //we can test that we are passing in opts by testing the timeout option for a CID that doesn't exist - ipfs.object.stat(badCid, {timeout: `${timeout}s`}, (err, stats) => { - let timeForRequest = (new Date () - startTime)/1000; - console.log(err); - expect(err.message).to.equal("failed to get block for QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3MzzzzzZ: context deadline exceeded"); + const timeout = 2 + const startTime = new Date() + const badCid = 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3MzzzzzZ' + + // we can test that we are passing in opts by testing the timeout option for a CID that doesn't exist + ipfs.object.stat(badCid, { timeout: `${timeout}s` }, (err, stats) => { + const timeForRequest = (new Date() - startTime) / 1000 + expect(err).to.exist() + expect(err.message).to.equal('failed to get block for QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3MzzzzzZ: context deadline exceeded') expect(stats).to.not.exist() - expect(timeForRequest).to.not.lessThan(timeout); + expect(timeForRequest).to.not.lessThan(timeout) done() }) }) From a9e3d2012a0a701f16f0d7d3ea09043585e0ce44 Mon Sep 17 00:00:00 2001 From: Matt Ober Date: Fri, 26 Jul 2019 08:31:51 -0500 Subject: [PATCH 4/7] Update src/object/stat.js Co-Authored-By: Alan Shaw --- src/object/stat.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/object/stat.js b/src/object/stat.js index f9ba466cc..4d3aa4f2c 100644 --- a/src/object/stat.js +++ b/src/object/stat.js @@ -81,7 +81,7 @@ module.exports = (createCommon, options) => { expect(expected).to.deep.equal(stats) }) - it('should pass on the opts', (done) => { + it('should respect timeout option', (done) => { const testObj = { Data: Buffer.from('get test object'), Links: [] From 4719de6cd032da450740023a7666e9bb7ccad8b5 Mon Sep 17 00:00:00 2001 From: Matt Ober Date: Fri, 26 Jul 2019 08:32:04 -0500 Subject: [PATCH 5/7] Update SPEC/OBJECT.md Co-Authored-By: Alan Shaw --- SPEC/OBJECT.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SPEC/OBJECT.md b/SPEC/OBJECT.md index cbf55cb49..fb12538b8 100644 --- a/SPEC/OBJECT.md +++ b/SPEC/OBJECT.md @@ -199,7 +199,7 @@ A great source of [examples][] can be found in the tests for this API. `options` is a optional argument of type object, that can contain the following properties: - `enc`, The encoding type the output should beencoded with (json, xml, or text), if any. -- `timeout`, A timeout to pass to the IPFS daemon so the request expires after a certain amount of time. +- `timeout`, A timeout to pass to the IPFS daemon so the request expires after a certain amount of time without any response. NOTE: not yet supported in JS IPFS. `callback` must follow `function (err, stats) {}` signature, where `err` is an error if the operation was not successful and `stats` is an Object with following format: From 3407dc865f8464ce524fb87ae466d5443baf54d2 Mon Sep 17 00:00:00 2001 From: Matt Ober Date: Fri, 26 Jul 2019 08:32:29 -0500 Subject: [PATCH 6/7] Update SPEC/OBJECT.md Co-Authored-By: Alan Shaw --- SPEC/OBJECT.md | 1 - 1 file changed, 1 deletion(-) diff --git a/SPEC/OBJECT.md b/SPEC/OBJECT.md index fb12538b8..d1d3690a1 100644 --- a/SPEC/OBJECT.md +++ b/SPEC/OBJECT.md @@ -198,7 +198,6 @@ A great source of [examples][] can be found in the tests for this API. `options` is a optional argument of type object, that can contain the following properties: -- `enc`, The encoding type the output should beencoded with (json, xml, or text), if any. - `timeout`, A timeout to pass to the IPFS daemon so the request expires after a certain amount of time without any response. NOTE: not yet supported in JS IPFS. `callback` must follow `function (err, stats) {}` signature, where `err` is an error if the operation was not successful and `stats` is an Object with following format: From 7b8fc644161e3dda904942129463594bc1ff9528 Mon Sep 17 00:00:00 2001 From: Matt Ober Date: Fri, 26 Jul 2019 08:54:44 -0500 Subject: [PATCH 7/7] added upper bound for timeout test --- src/object/stat.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/object/stat.js b/src/object/stat.js index 4d3aa4f2c..0559ad104 100644 --- a/src/object/stat.js +++ b/src/object/stat.js @@ -100,6 +100,7 @@ module.exports = (createCommon, options) => { expect(err.message).to.equal('failed to get block for QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3MzzzzzZ: context deadline exceeded') expect(stats).to.not.exist() expect(timeForRequest).to.not.lessThan(timeout) + expect(timeForRequest).to.not.greaterThan(timeout + 1) done() }) })