Skip to content

Commit

Permalink
feat: jsipfs pin improvements (ipfs#1249)
Browse files Browse the repository at this point in the history
* initial sweep through to understand how pin works. Did make some changes but mostly minor.

* refactor pb schema to it's own file

* fix: don't pin files during files.add if opts.pin === false

* feat: add some http qs parsing, http route/resources cleanup, cleanup core/utils.parseIpfsPath

* feat: expand pin tests. \nFirst draft. still needs some further work.

* feat: Add logging for entry/exit of pins: add/rm/flush/load. Clean some documentation.

* feat: add --pin to files.add, fix: improper pin option parsing in core.

* feat: Use ipfs.files.add to add init-docs instead of directly using the unix-fs importer.

* feat(tests): Add tests for cli --pin option. I know this should be more of an integration test. Should be written in /core. Maybe talk with Victor about testing different layers

* feat: use isIPFS to valiate a multihash.
  • Loading branch information
JonKrone committed Mar 27, 2018
1 parent 8e3ea44 commit 28a1b84
Show file tree
Hide file tree
Showing 18 changed files with 1,418 additions and 37 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@
"hapi-set-header": "^1.0.2",
"hoek": "^5.0.3",
"human-to-milliseconds": "^1.0.0",
"ipfs-api": "^18.2.1",
"ipfs-bitswap": "~0.19.0",
"ipfs-block": "~0.6.1",
"ipfs-block-service": "~0.13.0",
Expand Down
6 changes: 6 additions & 0 deletions src/cli/commands/files/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ module.exports = {
type: 'boolean',
default: false,
describe: 'Write no output'
},
pin: {
type: 'boolean',
default: true,
describe: 'Pin this object when adding'
}
},

Expand All @@ -192,6 +197,7 @@ module.exports = {
cidVersion: argv.cidVersion,
rawLeaves: argv.rawLeaves,
onlyHash: argv.onlyHash
pin: argv.pin
}

// Temporary restriction on raw-leaves:
Expand Down
30 changes: 30 additions & 0 deletions src/cli/commands/pin/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

const print = require('../../utils').print

module.exports = {
command: 'add <ipfs-path>',

describe: 'Pins objects to local storage.',

builder: {
recursive: {
type: 'boolean',
alias: 'r',
default: true,
describe: 'Recursively pin the object linked to by the specified object(s).'
}
},

handler (argv) {
const paths = argv['ipfs-path'].split(' ')
const recursive = argv.recursive
const type = recursive ? 'recursive' : 'direct'
argv.ipfs.pin.add(paths[0], { recursive: recursive }, (err, results) => {
if (err) { throw err }
results.forEach((res) => {
print(`pinned ${res.hash} ${type}ly`)
})
})
}
}
42 changes: 42 additions & 0 deletions src/cli/commands/pin/ls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict'

const print = require('../../utils').print

module.exports = {
// bracket syntax with '...' tells yargs to optionally accept a list
command: 'ls [ipfs-path...]',

describe: 'List objects pinned to local storage.',

builder: {
type: {
type: 'string',
alias: 't',
default: 'all',
choices: ['direct', 'indirect', 'recursive', 'all'],
describe: 'The type of pinned keys to list.'
},
quiet: {
type: 'boolean',
alias: 'q',
default: false,
describe: 'Write just hashes of objects.'
}
},

handler: (argv) => {
const paths = argv.ipfsPath || ''
const type = argv.type
const quiet = argv.quiet
argv.ipfs.pin.ls(paths, { type: type }, (err, results) => {
if (err) { throw err }
results.forEach((res) => {
let line = res.hash
if (!quiet) {
line += ` ${res.type}`
}
print(line)
})
})
}
}
29 changes: 29 additions & 0 deletions src/cli/commands/pin/rm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const print = require('../../utils').print

module.exports = {
command: 'rm <ipfs-path>',

describe: 'Removes the pinned object from local storage.',

builder: {
recursive: {
type: 'boolean',
alias: 'r',
default: true,
describe: 'Recursively unpin the objects linked to by the specified object(s).'
}
},

handler: (argv) => {
const paths = argv['ipfs-path'].split(' ')
const recursive = argv.recursive
argv.ipfs.pin.rm(paths, { recursive: recursive }, (err, results) => {
if (err) { throw err }
results.forEach((res) => {
print(`unpinned ${res.hash}`)
})
})
}
}
7 changes: 5 additions & 2 deletions src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const CID = require('cids')
const toB58String = require('multihashes').toB58String

function noop () {}
function identity (x) { return x }

function prepareFile (self, opts, file, callback) {
opts = opts || {}
Expand Down Expand Up @@ -111,7 +112,8 @@ module.exports = function files (self) {
}, options)

let total = 0
let prog = opts.progress || (() => {})
const shouldPin = 'pin' in opts ? opts.pin : true
const prog = opts.progress || noop
const progress = (bytes) => {
total += bytes
prog(total)
Expand All @@ -122,7 +124,8 @@ module.exports = function files (self) {
pull.map(normalizeContent),
pull.flatten(),
importer(self._ipld, opts),
pull.asyncMap(prepareFile.bind(null, self, opts))
pull.asyncMap(prepareFile.bind(null, self, opts)),
shouldPin ? pull.asyncMap(pinFile.bind(null, self)) : identity
)
}

Expand Down
21 changes: 9 additions & 12 deletions src/core/components/init-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,20 @@ module.exports = function addDefaultAssets (self, log, callback) {

pull(
pull.values([initDocsPath]),
pull.asyncMap((val, cb) => glob(path.join(val, '/**/*'), cb)),
pull.asyncMap((val, cb) =>
glob(path.join(val, '/**/*'), { nodir: true }, cb)
),
pull.flatten(),
pull.map((element) => {
pull.map(element => {
const addPath = element.substring(index + 1)

if (fs.statSync(element).isDirectory()) { return }

return { path: addPath, content: file(element) }
}),
// Filter out directories, which are undefined from above
pull.filter(Boolean),
importer(self._ipld),
pull.through((el) => {
if (el.path === 'init-docs') {
const cid = new CID(el.multihash)
self.files.addPullStream(),
pull.through(file => {
if (file.path === 'init-docs') {
const cid = new CID(file.hash)
log('to get started, enter:\n')
log(`\t jsipfs files cat /ipfs/${cid.toBaseEncodedString()}/readme\n`)
log(`\tjsipfs files cat /ipfs/${cid.toBaseEncodedString()}/readme\n`)
}
}),
pull.collect((err) => {
Expand Down
13 changes: 4 additions & 9 deletions src/core/components/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,11 @@ module.exports = function init (self) {
}

self.log('adding assets')
const tasks = [
parallel([
// add empty unixfs dir object (go-ipfs assumes this exists)
(cb) => self.object.new('unixfs-dir', cb)
]

if (typeof addDefaultAssets === 'function') {
tasks.push((cb) => addDefaultAssets(self, opts.log, cb))
}

parallel(tasks, (err) => {
(cb) => self.object.new('unixfs-dir', cb),
(cb) => addDefaultAssets(self, opts.log, cb)
], (err) => {
if (err) {
cb(err)
} else {
Expand Down
Loading

0 comments on commit 28a1b84

Please sign in to comment.