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

Commit

Permalink
fix: convert config API as it used by ipfsd-ctl
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Dec 10, 2019
1 parent d8e82b7 commit cbdf9e6
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 248 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
"interface-ipfs-core": "github:ipfs/interface-js-ipfs-core#refactor/async-iterables",
"ipfs-interop": "^0.1.1",
"ipfsd-ctl": "github:ipfs/js-ipfsd-ctl#fix/do-not-call-shutdown-twice",
"it-all": "^1.0.1",
"libp2p-websocket-star": "~0.10.2",
"lodash": "^4.17.15",
"ncp": "^2.0.0",
Expand Down
15 changes: 8 additions & 7 deletions src/core/components/add/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,23 @@ module.exports = ({ ipld, dag, gcLock, preload, pin, constructorOptions }) => {
function transformFile (dag, opts) {
return async function * (source) {
for await (const { cid, path, unixfs } of source) {
const hash = cid.toString()

if (opts.onlyHash) {
yield {
cid,
path: path || cid.toString(),
hash,
path: path || hash,
size: unixfs.fileSize()
}

continue
}

const node = await dag.get(cid, { ...opts, preload: false })
const { value: node } = await dag.get(cid, { ...opts, preload: false })

yield {
cid,
path: path || cid.toString(),
hash,
path: path || hash,
size: Buffer.isBuffer(node) ? node.length : node.size
}
}
Expand Down Expand Up @@ -103,9 +105,8 @@ function pinFile (pin, opts) {
for await (const file of source) {
// Pin a file if it is the root dir of a recursive add or the single file
// of a direct add.
const pin = 'pin' in opts ? opts.pin : true
const isRootDir = !file.path.includes('/')
const shouldPin = pin && isRootDir && !opts.onlyHash
const shouldPin = (opts.pin == null ? true : opts.pin) && isRootDir && !opts.onlyHash

if (shouldPin) {
// Note: addAsyncIterator() has already taken a GC lock, so tell
Expand Down
17 changes: 8 additions & 9 deletions src/core/components/config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
'use strict'

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

module.exports = function config (self) {
module.exports = ({ repo }) => {
return {
get: callbackify.variadic(self._repo.config.get),
set: callbackify(self._repo.config.set),
replace: callbackify.variadic(self._repo.config.set),
get: repo.config.get,
set: repo.config.set,
replace: repo.config.set,
profiles: {
apply: callbackify.variadic(applyProfile),
list: callbackify.variadic(listProfiles)
apply: applyProfile,
list: listProfiles
}
}

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

try {
const oldCfg = await self.config.get()
const oldCfg = await repo.config.get()
let newCfg = JSON.parse(JSON.stringify(oldCfg)) // clone
newCfg = profile.transform(newCfg)

if (!dryRun) {
await self.config.replace(newCfg)
await repo.config.set(newCfg)
}

// Scrub private key from output
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'

exports.add = require('./add')
exports.config = require('./config')
exports.init = require('./init')
exports.start = require('./start')
exports.stop = require('./stop')

exports.legacy = { // TODO: these will be removed as the new API is completed
config: require('./config'),
dag: require('./dag'),
libp2p: require('./libp2p'),
object: require('./object'),
Expand Down
1 change: 1 addition & 0 deletions src/core/components/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ function createApi ({

const api = {
add,
config: Commands.config({ repo }),
init: () => { throw new AlreadyInitializedError() },
start
}
Expand Down
1 change: 1 addition & 0 deletions src/core/components/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ function createApi ({

const api = {
add,
config: Commands.config({ repo }),
init: () => { throw new AlreadyInitializedError() },
start: () => apiManager.api,
stop
Expand Down
1 change: 1 addition & 0 deletions src/core/components/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ function createApi ({

const api = {
add,
config: Commands.config({ repo }),
init: () => { throw new AlreadyInitializedError() },
start,
stop: () => apiManager.api
Expand Down
2 changes: 1 addition & 1 deletion test/cli/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const HASH_ALGS = [
'keccak-512'
]

describe('files', () => runOnAndOff((thing) => {
describe.only('files', () => runOnAndOff((thing) => {
let ipfs
const readme = fs.readFileSync(path.join(process.cwd(), '/src/init-files/init-docs/readme'))
.toString('utf-8')
Expand Down
223 changes: 0 additions & 223 deletions test/core/config.spec.js

This file was deleted.

13 changes: 7 additions & 6 deletions test/core/files.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ const { expect } = require('interface-ipfs-core/src/utils/mocha')
const hat = require('hat')
const pull = require('pull-stream')
const IPFSFactory = require('ipfsd-ctl')
const all = require('it-all')
const IPFS = require('../../src/core')

describe('files', function () {
describe.only('files', function () {
this.timeout(10 * 1000)
let ipfsd, ipfs

Expand Down Expand Up @@ -74,24 +75,24 @@ describe('files', function () {

describe('add', () => {
it('should not error when passed null options', async () => {
await ipfs.add(Buffer.from(hat()), null)
await all(ipfs.add(Buffer.from(hat()), null))
})

it('should add a file with a v1 CID', async () => {
const files = await ipfs.add(Buffer.from([0, 1, 2]), {
const files = await all(ipfs.add(Buffer.from([0, 1, 2]), {
cidVersion: 1
})
}))

expect(files.length).to.equal(1)
expect(files[0].hash).to.equal('bafkreifojmzibzlof6xyh5auu3r5vpu5l67brf3fitaf73isdlglqw2t7q')
expect(files[0].size).to.equal(3)
})

it('should add a file with a v1 CID and not raw leaves', async () => {
const files = await ipfs.add(Buffer.from([0, 1, 2]), {
const files = await all(ipfs.add(Buffer.from([0, 1, 2]), {
cidVersion: 1,
rawLeaves: false
})
}))

expect(files.length).to.equal(1)
expect(files[0].hash).to.equal('bafybeide2caf5we5a7izifzwzz5ds2gla67vsfgrzvbzpnyyirnfzgwf5e')
Expand Down
Loading

0 comments on commit cbdf9e6

Please sign in to comment.