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

Commit df09eac

Browse files
committed
Pinning core, cli and http
1 parent 18755d4 commit df09eac

File tree

24 files changed

+1359
-2
lines changed

24 files changed

+1359
-2
lines changed

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,17 @@
9393
"async": "^2.5.0",
9494
"bl": "^1.2.1",
9595
"boom": "^5.2.0",
96-
"debug": "^3.0.1",
9796
"cids": "^0.5.1",
97+
"debug": "^3.0.1",
9898
"file-type": "^6.1.0",
9999
"filesize": "^3.5.10",
100+
"fnv1a": "^1.0.1",
100101
"fsm-event": "^2.1.0",
101102
"glob": "^7.1.2",
102103
"hapi": "^16.5.2",
103104
"hapi-set-header": "^1.0.2",
104105
"hoek": "^4.2.0",
106+
"interface-datastore": "^0.3.0",
105107
"ipfs-api": "^14.3.5",
106108
"ipfs-bitswap": "~0.17.2",
107109
"ipfs-block": "~0.6.0",
@@ -126,6 +128,7 @@
126128
"libp2p-websockets": "~0.10.1",
127129
"lodash.flatmap": "^4.5.0",
128130
"lodash.get": "^4.4.2",
131+
"lodash.set": "^4.3.0",
129132
"lodash.sortby": "^4.7.0",
130133
"lodash.values": "^4.3.0",
131134
"mafmt": "^3.0.1",
@@ -139,6 +142,7 @@
139142
"peer-id": "~0.10.1",
140143
"peer-info": "~0.11.0",
141144
"promisify-es6": "^1.0.3",
145+
"protocol-buffers": "^3.2.1",
142146
"pull-file": "^1.0.0",
143147
"pull-paramap": "^1.2.2",
144148
"pull-pushable": "^2.1.1",
@@ -216,4 +220,4 @@
216220
"Łukasz Magiera <[email protected]>",
217221
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <[email protected]>"
218222
]
219-
}
223+
}

src/cli/commands/pin.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
module.exports = {
4+
command: 'pin',
5+
6+
description: 'Pin and unpin objects to local storage.',
7+
8+
builder (yargs) {
9+
return yargs
10+
.commandDir('pin')
11+
},
12+
13+
handler (argv) {
14+
}
15+
}

src/cli/commands/pin/add.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict'
2+
3+
module.exports = {
4+
command: 'add <ipfs-path>',
5+
6+
describe: 'Pins objects to local storage.',
7+
8+
builder: {
9+
recursive: {
10+
type: 'boolean',
11+
alias: 'r',
12+
default: true,
13+
describe: 'Recursively pin the object linked to by the specified object(s).'
14+
}
15+
},
16+
17+
handler (argv) {
18+
const paths = argv['ipfs-path'].split(' ')
19+
const recursive = argv.recursive
20+
const type = recursive ? 'recursive' : 'direct'
21+
argv.ipfs.pin.add(paths[0], { recursive }, (err, results) => {
22+
if (err) { throw err }
23+
results.forEach((res) => {
24+
console.log(`pinned ${res.hash} ${type}ly`)
25+
})
26+
})
27+
}
28+
}

src/cli/commands/pin/ls.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict'
2+
3+
module.exports = {
4+
command: 'ls',
5+
6+
describe: 'List objects pinned to local storage.',
7+
8+
builder: {
9+
path: {
10+
type: 'string',
11+
describe: 'List pinned state of specific <ipfs-path>.'
12+
},
13+
type: {
14+
type: 'string',
15+
alias: 't',
16+
default: 'all',
17+
describe: ('The type of pinned keys to list. ' +
18+
'Can be "direct", "indirect", "recursive", or "all".')
19+
},
20+
quiet: {
21+
type: 'boolean',
22+
alias: 'q',
23+
default: false,
24+
describe: 'Write just hashes of objects.'
25+
}
26+
},
27+
28+
handler: (argv) => {
29+
const paths = argv.path && argv.path.split(' ')
30+
const type = argv.type
31+
const quiet = argv.quiet
32+
argv.ipfs.pin.ls(paths, { type }, (err, results) => {
33+
if (err) { throw err }
34+
results.forEach((res) => {
35+
let line = res.hash
36+
if (!quiet) {
37+
line += ` ${res.type}`
38+
}
39+
console.log(line)
40+
})
41+
})
42+
}
43+
}

src/cli/commands/pin/rm.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict'
2+
3+
const utils = require('../../utils')
4+
const debug = require('debug')
5+
const log = debug('cli:pin')
6+
log.error = debug('cli:pin:error')
7+
8+
module.exports = {
9+
command: 'rm <ipfs-path>',
10+
11+
describe: 'Removes the pinned object from local storage.',
12+
13+
builder: {
14+
recursive: {
15+
type: 'boolean',
16+
alias: 'r',
17+
default: true,
18+
describe: 'Recursively unpin the objects linked to by the specified object(s).'
19+
}
20+
},
21+
22+
handler: (argv) => {
23+
const paths = argv['ipfs-path'].split(' ')
24+
const recursive = argv.recursive
25+
utils.getIPFS((err, ipfs) => {
26+
if (err) { throw err }
27+
ipfs.pin.rm(paths, { recursive }, (err, results) => {
28+
if (err) { throw err }
29+
results.forEach((res) => {
30+
console.log(`unpinned ${res.hash}`)
31+
})
32+
})
33+
})
34+
}
35+
}

src/core/boot.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module.exports = (self) => {
2828

2929
series([
3030
(cb) => self._repo.open(cb),
31+
(cb) => self.pin.load(cb),
3132
(cb) => self.preStart(cb),
3233
(cb) => {
3334
self.state.initialized()

src/core/components/files.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ module.exports = function files (self) {
7474
if (a.path > b.path) return -1
7575
return 0
7676
}),
77+
pull.asyncMap((file, cb) => {
78+
self.pin.add(file.hash, (err) => {
79+
cb(err, file)
80+
})
81+
}),
7782
pull.collect(callback)
7883
)
7984
}),

src/core/components/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ exports.dag = require('./dag')
1616
exports.libp2p = require('./libp2p')
1717
exports.swarm = require('./swarm')
1818
exports.ping = require('./ping')
19+
exports.pin = require('./pin')
1920
exports.files = require('./files')
2021
exports.bitswap = require('./bitswap')
2122
exports.pubsub = require('./pubsub')

src/core/components/key-set.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
const multihashes = require('multihashes')
4+
5+
module.exports = function KeySet (keys) {
6+
// Buffers with identical data are still different objects, so
7+
// they need to be cast to strings to prevent duplicates in Sets
8+
this.keys = {}
9+
this.add = (key) => {
10+
this.keys[multihashes.toB58String(key)] = key
11+
}
12+
this.delete = (key) => {
13+
delete this.keys[multihashes.toB58String(key)]
14+
}
15+
this.clear = () => {
16+
this.keys = {}
17+
}
18+
this.has = (key) => {
19+
return (multihashes.toB58String(key) in this.keys)
20+
}
21+
this.toArray = () => {
22+
return Object.keys(this.keys).map((hash) => {
23+
return this.keys[hash]
24+
})
25+
}
26+
this.toStringArray = () => {
27+
return Object.keys(this.keys)
28+
}
29+
keys = keys || []
30+
keys.forEach(this.add)
31+
}

0 commit comments

Comments
 (0)