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

Commit 1f63e8c

Browse files
achingbrainalanshaw
authored andcommitted
feat: support --raw-leaves (#1454)
Also updates pinning to support CIDv1
1 parent f4344b0 commit 1f63e8c

File tree

6 files changed

+35
-94
lines changed

6 files changed

+35
-94
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@
108108
"ipfs-block": "~0.7.1",
109109
"ipfs-block-service": "~0.14.0",
110110
"ipfs-http-response": "~0.1.2",
111-
"ipfs-mfs": "~0.1.0",
111+
"ipfs-mfs": "~0.2.2",
112112
"ipfs-multipart": "~0.1.0",
113113
"ipfs-repo": "~0.22.1",
114114
"ipfs-unixfs": "~0.1.15",
115-
"ipfs-unixfs-engine": "~0.30.0",
115+
"ipfs-unixfs-engine": "~0.31.3",
116116
"ipld": "~0.17.3",
117117
"ipld-dag-cbor": "~0.12.1",
118118
"ipld-dag-pb": "~0.14.5",

src/cli/commands/files/add.js

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,13 @@ module.exports = {
145145
},
146146
'raw-leaves': {
147147
type: 'boolean',
148-
default: undefined,
148+
default: false,
149149
describe: 'Use raw blocks for leaf nodes. (experimental)'
150150
},
151151
'cid-version': {
152152
type: 'integer',
153-
describe: 'Cid version. Non-zero value will change default of \'raw-leaves\' to true. (experimental)'
153+
describe: 'CID version. Defaults to 0 unless an option that depends on CIDv1 is passed. (experimental)',
154+
default: 0
154155
},
155156
hash: {
156157
type: 'string',
@@ -197,34 +198,6 @@ module.exports = {
197198
pin: argv.pin
198199
}
199200

200-
// Temporary restriction on raw-leaves:
201-
// When cid-version=1 then raw-leaves MUST be present and false.
202-
//
203-
// This is because raw-leaves is not yet implemented in js-ipfs,
204-
// and go-ipfs changes the value of raw-leaves to true when
205-
// cid-version > 0 unless explicitly set to false.
206-
//
207-
// This retains feature parity without having to implement raw-leaves.
208-
if (options.cidVersion > 0 && options.rawLeaves !== false) {
209-
throw new Error('Implied argument raw-leaves must be passed and set to false when cid-version is > 0')
210-
}
211-
212-
// Temporary restriction on raw-leaves:
213-
// When hash != undefined then raw-leaves MUST be present and false.
214-
//
215-
// This is because raw-leaves is not yet implemented in js-ipfs,
216-
// and go-ipfs changes the value of raw-leaves to true when
217-
// hash != undefined unless explicitly set to false.
218-
//
219-
// This retains feature parity without having to implement raw-leaves.
220-
if (options.hash && options.rawLeaves !== false) {
221-
throw new Error('Implied argument raw-leaves must be passed and set to false when hash argument is specified')
222-
}
223-
224-
if (options.rawLeaves) {
225-
throw new Error('Not implemented: raw-leaves')
226-
}
227-
228201
if (options.enableShardingExperiment && utils.isDaemonOn()) {
229202
throw new Error('Error: Enabling the sharding experiment should be done on the daemon')
230203
}

src/core/components/pin.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
const promisify = require('promisify-es6')
55
const { DAGNode, DAGLink } = require('ipld-dag-pb')
66
const CID = require('cids')
7-
const multihashes = require('multihashes')
87
const async = require('async')
98
const { Key } = require('interface-datastore')
109

@@ -34,9 +33,9 @@ module.exports = (self) => {
3433
let recursivePins = new Set()
3534

3635
const directKeys = () =>
37-
Array.from(directPins).map(key => multihashes.fromB58String(key))
36+
Array.from(directPins).map(key => new CID(key).buffer)
3837
const recursiveKeys = () =>
39-
Array.from(recursivePins).map(key => multihashes.fromB58String(key))
38+
Array.from(recursivePins).map(key => new CID(key).buffer)
4039

4140
function getIndirectKeys (callback) {
4241
const indirectKeys = new Set()

src/core/utils.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict'
22

3-
const multihashes = require('multihashes')
43
const promisify = require('promisify-es6')
54
const map = require('async/map')
65
const isIpfs = require('is-ipfs')
6+
const CID = require('cids')
77

88
exports.OFFLINE_ERROR = 'This command must be run in online mode. Try running \'ipfs daemon\' first.'
99

@@ -61,12 +61,15 @@ const resolvePath = promisify(function (objectAPI, ipfsPaths, callback) {
6161

6262
map(ipfsPaths, (path, cb) => {
6363
if (typeof path !== 'string') {
64+
let cid
65+
6466
try {
65-
multihashes.validate(path)
67+
cid = new CID(path)
6668
} catch (err) {
6769
return cb(err)
6870
}
69-
return cb(null, path)
71+
72+
return cb(null, cid.buffer)
7073
}
7174

7275
let parsedPath
@@ -76,10 +79,10 @@ const resolvePath = promisify(function (objectAPI, ipfsPaths, callback) {
7679
return cb(err)
7780
}
7881

79-
const rootHash = multihashes.fromB58String(parsedPath.hash)
82+
const rootHash = new CID(parsedPath.hash)
8083
const rootLinks = parsedPath.links
8184
if (!rootLinks.length) {
82-
return cb(null, rootHash)
85+
return cb(null, rootHash.buffer)
8386
}
8487

8588
objectAPI.get(rootHash, follow.bind(null, rootLinks))

src/http/api/resources/files.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,8 @@ exports.add = {
153153
validate: {
154154
query: Joi.object()
155155
.keys({
156-
'cid-version': Joi.number().integer().min(0).max(1),
157-
// Temporary restriction on raw-leaves:
158-
// When cid-version=1 then raw-leaves MUST be present and false.
159-
//
160-
// This is because raw-leaves is not yet implemented in js-ipfs,
161-
// and go-ipfs changes the value of raw-leaves to true when
162-
// cid-version > 0 unless explicitly set to false.
163-
//
164-
// This retains feature parity without having to implement raw-leaves.
165-
'raw-leaves': Joi.boolean().when('cid-version', {
166-
is: 1,
167-
then: Joi.boolean().valid(false).required(),
168-
otherwise: Joi.boolean().valid(false)
169-
}),
156+
'cid-version': Joi.number().integer().min(0).max(1).default(0),
157+
'raw-leaves': Joi.boolean(),
170158
'only-hash': Joi.boolean(),
171159
pin: Joi.boolean().default(true),
172160
'wrap-with-directory': Joi.boolean()

test/cli/files.js

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -205,56 +205,34 @@ describe('files', () => runOnAndOff((thing) => {
205205
})
206206
})
207207

208-
// Temporarily expect to fail as raw-leaves not yet implemented.
209-
//
210-
// When cid-version=1 then raw-leaves MUST be present and false.
211-
//
212-
// This is because raw-leaves is not yet implemented in js-ipfs,
213-
// and go-ipfs changes the value of raw-leaves to true when
214-
// cid-version > 0 unless explicitly set to false.
215-
//
216-
// This retains feature parity without having to implement raw-leaves.
217208
it('add with cid-version=1', function () {
218209
this.timeout(30 * 1000)
219210

220-
return new Promise((resolve, reject) => {
221-
ipfs('add src/init-files/init-docs/readme --cid-version=1')
222-
.then(() => reject(new Error('Raw leaves not expected to be implemented')))
223-
.catch((err) => {
224-
expect(err).to.exist()
225-
resolve()
226-
})
227-
})
211+
return ipfs('add src/init-files/init-docs/readme --cid-version=1')
212+
.then((out) => {
213+
expect(out)
214+
.to.eql('added zdj7WWeQ43G6JJvLWQWZpyHuAMq6uYWRjkBXFad11vE2LHhQ7 readme\n')
215+
})
228216
})
229217

230-
// TODO: this test is failing, @alanshaw?
231-
it.skip('add with cid-version=1 and raw-leaves=false', () => {
232-
return ipfs('add src/init-files/init-docs/readme --cid-version=1 --raw-leaves=false').then((out) => {
233-
expect(out)
234-
.to.eql('added zdj7WWeQ43G6JJvLWQWZpyHuAMq6uYWRjkBXFad11vE2LHhQ7 readme\n')
235-
})
218+
it('add with cid-version=1 and raw-leaves=false', function () {
219+
this.timeout(30 * 1000)
220+
221+
return ipfs('add src/init-files/init-docs/readme --cid-version=1 --raw-leaves=false')
222+
.then((out) => {
223+
expect(out)
224+
.to.eql('added zdj7WWeQ43G6JJvLWQWZpyHuAMq6uYWRjkBXFad11vE2LHhQ7 readme\n')
225+
})
236226
})
237227

238-
// Temporarily expect to fail as raw-leaves not yet implemented
239-
//
240-
// When cid-version=1 then raw-leaves MUST be present and false.
241-
//
242-
// This is because raw-leaves is not yet implemented in js-ipfs,
243-
// and go-ipfs changes the value of raw-leaves to true when
244-
// cid-version > 0 unless explicitly set to false.
245-
//
246-
// This retains feature parity without having to implement raw-leaves.
247228
it('add with cid-version=1 and raw-leaves=true', function () {
248229
this.timeout(30 * 1000)
249230

250-
return new Promise((resolve, reject) => {
251-
ipfs('add src/init-files/init-docs/readme --cid-version=1 --raw-leaves=true')
252-
.then(() => reject(new Error('Raw leaves not expected to be implemented')))
253-
.catch((err) => {
254-
expect(err).to.exist()
255-
resolve()
256-
})
257-
})
231+
return ipfs('add src/init-files/init-docs/readme --cid-version=1 --raw-leaves=true')
232+
.then((out) => {
233+
expect(out)
234+
.to.eql('added zdj7WiLc855B1KPRgV7Fh8ivjuAhePE1tuJafmxH5HmmSjqaD readme\n')
235+
})
258236
})
259237

260238
it('add --quiet', function () {

0 commit comments

Comments
 (0)