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

Commit 7544b7b

Browse files
authored
feat: Add --cid-version option to ipfs files add + decodeURIComponent for file and directory names
1 parent b3ad40f commit 7544b7b

File tree

7 files changed

+138
-17
lines changed

7 files changed

+138
-17
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@
7373
"expose-loader": "^0.7.3",
7474
"form-data": "^2.3.1",
7575
"gulp": "^3.9.1",
76-
"interface-ipfs-core": "~0.31.13",
77-
"ipfsd-ctl": "~0.21.0",
76+
"interface-ipfs-core": "~0.31.16",
77+
"ipfsd-ctl": "~0.22.0",
7878
"left-pad": "^1.1.3",
7979
"lodash": "^4.17.4",
8080
"mocha": "^3.5.0",
@@ -99,15 +99,15 @@
9999
"hapi": "^16.5.2",
100100
"hapi-set-header": "^1.0.2",
101101
"hoek": "^4.2.0",
102-
"ipfs-api": "^14.1.2",
102+
"ipfs-api": "^14.2.1",
103103
"ipfs-bitswap": "~0.16.1",
104104
"ipfs-block": "~0.6.0",
105105
"ipfs-block-service": "~0.12.0",
106106
"ipfs-multipart": "~0.1.0",
107107
"ipfs-repo": "~0.17.0",
108108
"ipfs-unixfs": "~0.1.12",
109109
"ipfs-unixfs-engine": "~0.22.0",
110-
"ipld-resolver": "~0.13.0",
110+
"ipld-resolver": "~0.13.1",
111111
"is-ipfs": "^0.3.0",
112112
"is-stream": "^1.1.0",
113113
"joi": "^10.6.0",
@@ -128,7 +128,7 @@
128128
"mafmt": "^2.1.8",
129129
"mkdirp": "~0.5.1",
130130
"multiaddr": "^2.3.0",
131-
"multihashes": "~0.4.8",
131+
"multihashes": "~0.4.9",
132132
"once": "^1.4.0",
133133
"path-exists": "^3.0.0",
134134
"peer-book": "~0.5.0",
@@ -208,4 +208,4 @@
208208
209209
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <[email protected]>"
210210
]
211-
}
211+
}

src/cli/commands/files/add.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ module.exports = {
112112
'shard-split-threshold': {
113113
type: 'integer',
114114
default: 1000
115+
},
116+
'raw-leaves': {
117+
type: 'boolean',
118+
default: undefined,
119+
describe: 'Use raw blocks for leaf nodes. (experimental)'
120+
},
121+
'cid-version': {
122+
type: 'integer',
123+
describe: 'Cid version. Non-zero value will change default of \'raw-leaves\' to true. (experimental)'
115124
}
116125
},
117126

@@ -120,7 +129,25 @@ module.exports = {
120129
const index = inPath.lastIndexOf('/') + 1
121130
const options = {
122131
strategy: argv.trickle ? 'trickle' : 'balanced',
123-
shardSplitThreshold: argv.enableShardingExperiment ? argv.shardSplitThreshold : Infinity
132+
shardSplitThreshold: argv.enableShardingExperiment ? argv.shardSplitThreshold : Infinity,
133+
'cid-version': argv['cid-version'],
134+
'raw-leaves': argv['raw-leaves']
135+
}
136+
137+
// Temporary restriction on raw-leaves:
138+
// When cid-version=1 then raw-leaves MUST be present and false.
139+
//
140+
// This is because raw-leaves is not yet implemented in js-ipfs,
141+
// and go-ipfs changes the value of raw-leaves to true when
142+
// cid-version > 0 unless explicitly set to false.
143+
//
144+
// This retains feature parity without having to implement raw-leaves.
145+
if (argv['cid-version'] > 0 && argv['raw-leaves'] !== false) {
146+
throw new Error('Implied argument raw-leaves must be passed and set to false when cid-version is > 0')
147+
}
148+
149+
if (argv['raw-leaves']) {
150+
throw new Error('Not implemented: raw-leaves')
124151
}
125152

126153
if (argv.enableShardingExperiment && utils.isDaemonOn()) {

src/core/components/files.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const unixfsEngine = require('ipfs-unixfs-engine')
44
const importer = unixfsEngine.importer
55
const exporter = unixfsEngine.exporter
66
const promisify = require('promisify-es6')
7-
const multihashes = require('multihashes')
87
const pull = require('pull-stream')
98
const sort = require('pull-sort')
109
const pushable = require('pull-pushable')
@@ -13,6 +12,7 @@ const toPull = require('stream-to-pull-stream')
1312
const waterfall = require('async/waterfall')
1413
const isStream = require('is-stream')
1514
const Duplex = require('stream').Duplex
15+
const CID = require('cids')
1616

1717
module.exports = function files (self) {
1818
const createAddPullStream = (options) => {
@@ -24,7 +24,7 @@ module.exports = function files (self) {
2424
pull.map(normalizeContent),
2525
pull.flatten(),
2626
importer(self._ipldResolver, opts),
27-
pull.asyncMap(prepareFile.bind(null, self))
27+
pull.asyncMap(prepareFile.bind(null, self, opts))
2828
)
2929
}
3030

@@ -68,7 +68,7 @@ module.exports = function files (self) {
6868
pull(
6969
pull.values(normalizeContent(data)),
7070
importer(self._ipldResolver, options),
71-
pull.asyncMap(prepareFile.bind(null, self)),
71+
pull.asyncMap(prepareFile.bind(null, self, options)),
7272
sort((a, b) => {
7373
if (a.path < b.path) return 1
7474
if (a.path > b.path) return -1
@@ -114,15 +114,23 @@ module.exports = function files (self) {
114114
}
115115
}
116116

117-
function prepareFile (self, file, callback) {
118-
const bs58mh = multihashes.toB58String(file.multihash)
117+
function prepareFile (self, opts, file, callback) {
118+
opts = opts || {}
119119

120120
waterfall([
121121
(cb) => self.object.get(file.multihash, cb),
122122
(node, cb) => {
123+
let cid = new CID(node.multihash)
124+
125+
if (opts['cid-version'] === 1) {
126+
cid = cid.toV1()
127+
}
128+
129+
const b58Hash = cid.toBaseEncodedString()
130+
123131
cb(null, {
124-
path: file.path || bs58mh,
125-
hash: bs58mh,
132+
path: file.path || b58Hash,
133+
hash: b58Hash,
126134
size: node.size
127135
})
128136
}

src/http-api/resources/files.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const toPull = require('stream-to-pull-stream')
1111
const pushable = require('pull-pushable')
1212
const EOL = require('os').EOL
1313
const toStream = require('pull-stream-to-stream')
14+
const Joi = require('joi')
1415

1516
exports = module.exports
1617

@@ -140,6 +141,28 @@ exports.get = {
140141
}
141142

142143
exports.add = {
144+
validate: {
145+
query: Joi.object()
146+
.keys({
147+
'cid-version': Joi.number().integer().min(0).max(1),
148+
// Temporary restriction on raw-leaves:
149+
// When cid-version=1 then raw-leaves MUST be present and false.
150+
//
151+
// This is because raw-leaves is not yet implemented in js-ipfs,
152+
// and go-ipfs changes the value of raw-leaves to true when
153+
// cid-version > 0 unless explicitly set to false.
154+
//
155+
// This retains feature parity without having to implement raw-leaves.
156+
'raw-leaves': Joi.any().when('cid-version', {
157+
is: 1,
158+
then: Joi.boolean().valid(false).required(),
159+
otherwise: Joi.boolean().valid(false)
160+
})
161+
})
162+
// TODO: Necessary until validate "recursive", "stream-channels" etc.
163+
.options({ allowUnknown: true })
164+
},
165+
143166
handler: (request, reply) => {
144167
if (!request.payload) {
145168
return reply({
@@ -156,6 +179,7 @@ exports.add = {
156179
const fileAdder = pushable()
157180

158181
parser.on('file', (fileName, fileStream) => {
182+
fileName = decodeURIComponent(fileName)
159183
const filePair = {
160184
path: fileName,
161185
content: toPull(fileStream)
@@ -165,6 +189,8 @@ exports.add = {
165189
})
166190

167191
parser.on('directory', (directory) => {
192+
directory = decodeURIComponent(directory)
193+
168194
fileAdder.push({
169195
path: directory,
170196
content: ''
@@ -181,9 +207,14 @@ exports.add = {
181207
fileAdder.end()
182208
})
183209

210+
const options = {
211+
'cid-version': request.query['cid-version'],
212+
'raw-leaves': request.query['raw-leaves']
213+
}
214+
184215
pull(
185216
fileAdder,
186-
ipfs.files.createAddPullStream(),
217+
ipfs.files.createAddPullStream(options),
187218
pull.map((file) => {
188219
return {
189220
Name: file.path ? file.path : file.hash,

src/http-api/routes/files.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ module.exports = (server) => {
3838
parse: false,
3939
output: 'stream'
4040
},
41-
handler: resources.files.add.handler
41+
handler: resources.files.add.handler,
42+
validate: resources.files.add.validate
4243
}
4344
})
4445
}

test/cli/files.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,60 @@ describe('files', () => runOnAndOff((thing) => {
136136
})
137137
})
138138

139+
it('add with cid-version=0', () => {
140+
return ipfs('add src/init-files/init-docs/readme --cid-version=0').then((out) => {
141+
expect(out)
142+
.to.eql('added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB readme\n')
143+
})
144+
})
145+
146+
// Temporarily expect to fail as raw-leaves not yet implemented.
147+
//
148+
// When cid-version=1 then raw-leaves MUST be present and false.
149+
//
150+
// This is because raw-leaves is not yet implemented in js-ipfs,
151+
// and go-ipfs changes the value of raw-leaves to true when
152+
// cid-version > 0 unless explicitly set to false.
153+
//
154+
// This retains feature parity without having to implement raw-leaves.
155+
it('add with cid-version=1', () => {
156+
return new Promise((resolve, reject) => {
157+
ipfs('add src/init-files/init-docs/readme --cid-version=1')
158+
.then(() => reject(new Error('Raw leaves not expected to be implemented')))
159+
.catch((err) => {
160+
expect(err).to.exist()
161+
resolve()
162+
})
163+
})
164+
})
165+
166+
it('add with cid-version=1 and raw-leaves=false', () => {
167+
return ipfs('add src/init-files/init-docs/readme --cid-version=1 --raw-leaves=false').then((out) => {
168+
expect(out)
169+
.to.eql('added zdj7WWeQ43G6JJvLWQWZpyHuAMq6uYWRjkBXFad11vE2LHhQ7 readme\n')
170+
})
171+
})
172+
173+
// Temporarily expect to fail as raw-leaves not yet implemented
174+
//
175+
// When cid-version=1 then raw-leaves MUST be present and false.
176+
//
177+
// This is because raw-leaves is not yet implemented in js-ipfs,
178+
// and go-ipfs changes the value of raw-leaves to true when
179+
// cid-version > 0 unless explicitly set to false.
180+
//
181+
// This retains feature parity without having to implement raw-leaves.
182+
it('add with cid-version=1 and raw-leaves=true', () => {
183+
return new Promise((resolve, reject) => {
184+
ipfs('add src/init-files/init-docs/readme --cid-version=1 --raw-leaves=true')
185+
.then(() => reject(new Error('Raw leaves not expected to be implemented')))
186+
.catch((err) => {
187+
expect(err).to.exist()
188+
resolve()
189+
})
190+
})
191+
})
192+
139193
it('cat', () => {
140194
return ipfs('files cat QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB')
141195
.then((out) => {

test/http-api/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('HTTP API', () => {
4141
.forEach((file) => require('./spec/' + file)(http))
4242
})
4343

44-
describe('## interface tests', () => {
44+
describe.only('## interface tests', () => {
4545
fs.readdirSync(path.join(__dirname, '/interface'))
4646
.forEach((file) => require('./interface/' + file))
4747
})

0 commit comments

Comments
 (0)