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

Commit cbdf9e6

Browse files
author
Alan Shaw
committed
fix: convert config API as it used by ipfsd-ctl
1 parent d8e82b7 commit cbdf9e6

File tree

11 files changed

+30
-248
lines changed

11 files changed

+30
-248
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
"interface-ipfs-core": "github:ipfs/interface-js-ipfs-core#refactor/async-iterables",
210210
"ipfs-interop": "^0.1.1",
211211
"ipfsd-ctl": "github:ipfs/js-ipfsd-ctl#fix/do-not-call-shutdown-twice",
212+
"it-all": "^1.0.1",
212213
"libp2p-websocket-star": "~0.10.2",
213214
"lodash": "^4.17.15",
214215
"ncp": "^2.0.0",

src/core/components/add/index.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,23 @@ module.exports = ({ ipld, dag, gcLock, preload, pin, constructorOptions }) => {
5959
function transformFile (dag, opts) {
6060
return async function * (source) {
6161
for await (const { cid, path, unixfs } of source) {
62+
const hash = cid.toString()
63+
6264
if (opts.onlyHash) {
6365
yield {
64-
cid,
65-
path: path || cid.toString(),
66+
hash,
67+
path: path || hash,
6668
size: unixfs.fileSize()
6769
}
6870

6971
continue
7072
}
7173

72-
const node = await dag.get(cid, { ...opts, preload: false })
74+
const { value: node } = await dag.get(cid, { ...opts, preload: false })
7375

7476
yield {
75-
cid,
76-
path: path || cid.toString(),
77+
hash,
78+
path: path || hash,
7779
size: Buffer.isBuffer(node) ? node.length : node.size
7880
}
7981
}
@@ -103,9 +105,8 @@ function pinFile (pin, opts) {
103105
for await (const file of source) {
104106
// Pin a file if it is the root dir of a recursive add or the single file
105107
// of a direct add.
106-
const pin = 'pin' in opts ? opts.pin : true
107108
const isRootDir = !file.path.includes('/')
108-
const shouldPin = pin && isRootDir && !opts.onlyHash
109+
const shouldPin = (opts.pin == null ? true : opts.pin) && isRootDir && !opts.onlyHash
109110

110111
if (shouldPin) {
111112
// Note: addAsyncIterator() has already taken a GC lock, so tell

src/core/components/config.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
'use strict'
22

3-
const callbackify = require('callbackify')
43
const getDefaultConfig = require('../runtime/config-nodejs.js')
54
const log = require('debug')('ipfs:core:config')
65

7-
module.exports = function config (self) {
6+
module.exports = ({ repo }) => {
87
return {
9-
get: callbackify.variadic(self._repo.config.get),
10-
set: callbackify(self._repo.config.set),
11-
replace: callbackify.variadic(self._repo.config.set),
8+
get: repo.config.get,
9+
set: repo.config.set,
10+
replace: repo.config.set,
1211
profiles: {
13-
apply: callbackify.variadic(applyProfile),
14-
list: callbackify.variadic(listProfiles)
12+
apply: applyProfile,
13+
list: listProfiles
1514
}
1615
}
1716

@@ -26,12 +25,12 @@ module.exports = function config (self) {
2625
}
2726

2827
try {
29-
const oldCfg = await self.config.get()
28+
const oldCfg = await repo.config.get()
3029
let newCfg = JSON.parse(JSON.stringify(oldCfg)) // clone
3130
newCfg = profile.transform(newCfg)
3231

3332
if (!dryRun) {
34-
await self.config.replace(newCfg)
33+
await repo.config.set(newCfg)
3534
}
3635

3736
// Scrub private key from output

src/core/components/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict'
22

33
exports.add = require('./add')
4+
exports.config = require('./config')
45
exports.init = require('./init')
56
exports.start = require('./start')
67
exports.stop = require('./stop')
78

89
exports.legacy = { // TODO: these will be removed as the new API is completed
9-
config: require('./config'),
1010
dag: require('./dag'),
1111
libp2p: require('./libp2p'),
1212
object: require('./object'),

src/core/components/init.js

+1
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ function createApi ({
299299

300300
const api = {
301301
add,
302+
config: Commands.config({ repo }),
302303
init: () => { throw new AlreadyInitializedError() },
303304
start
304305
}

src/core/components/start.js

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ function createApi ({
131131

132132
const api = {
133133
add,
134+
config: Commands.config({ repo }),
134135
init: () => { throw new AlreadyInitializedError() },
135136
start: () => apiManager.api,
136137
stop

src/core/components/stop.js

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ function createApi ({
9898

9999
const api = {
100100
add,
101+
config: Commands.config({ repo }),
101102
init: () => { throw new AlreadyInitializedError() },
102103
start,
103104
stop: () => apiManager.api

test/cli/files.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const HASH_ALGS = [
2626
'keccak-512'
2727
]
2828

29-
describe('files', () => runOnAndOff((thing) => {
29+
describe.only('files', () => runOnAndOff((thing) => {
3030
let ipfs
3131
const readme = fs.readFileSync(path.join(process.cwd(), '/src/init-files/init-docs/readme'))
3232
.toString('utf-8')

test/core/config.spec.js

-223
This file was deleted.

test/core/files.spec.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ const { expect } = require('interface-ipfs-core/src/utils/mocha')
66
const hat = require('hat')
77
const pull = require('pull-stream')
88
const IPFSFactory = require('ipfsd-ctl')
9+
const all = require('it-all')
910
const IPFS = require('../../src/core')
1011

11-
describe('files', function () {
12+
describe.only('files', function () {
1213
this.timeout(10 * 1000)
1314
let ipfsd, ipfs
1415

@@ -74,24 +75,24 @@ describe('files', function () {
7475

7576
describe('add', () => {
7677
it('should not error when passed null options', async () => {
77-
await ipfs.add(Buffer.from(hat()), null)
78+
await all(ipfs.add(Buffer.from(hat()), null))
7879
})
7980

8081
it('should add a file with a v1 CID', async () => {
81-
const files = await ipfs.add(Buffer.from([0, 1, 2]), {
82+
const files = await all(ipfs.add(Buffer.from([0, 1, 2]), {
8283
cidVersion: 1
83-
})
84+
}))
8485

8586
expect(files.length).to.equal(1)
8687
expect(files[0].hash).to.equal('bafkreifojmzibzlof6xyh5auu3r5vpu5l67brf3fitaf73isdlglqw2t7q')
8788
expect(files[0].size).to.equal(3)
8889
})
8990

9091
it('should add a file with a v1 CID and not raw leaves', async () => {
91-
const files = await ipfs.add(Buffer.from([0, 1, 2]), {
92+
const files = await all(ipfs.add(Buffer.from([0, 1, 2]), {
9293
cidVersion: 1,
9394
rawLeaves: false
94-
})
95+
}))
9596

9697
expect(files.length).to.equal(1)
9798
expect(files[0].hash).to.equal('bafybeide2caf5we5a7izifzwzz5ds2gla67vsfgrzvbzpnyyirnfzgwf5e')

0 commit comments

Comments
 (0)