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

Commit 5af0dd1

Browse files
committed
feat: expose GC to http api
1 parent 3b26ac7 commit 5af0dd1

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

src/http/api/resources/repo.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
'use strict'
22

3-
exports.gc = async (request, h) => {
4-
const { ipfs } = request.server.app
5-
await ipfs.repo.gc()
6-
return h.response()
3+
const Joi = require('joi')
4+
5+
exports.gc = {
6+
validate: {
7+
query: Joi.object().keys({
8+
quiet: Joi.boolean().default(false),
9+
'stream-errors': Joi.boolean().default(false)
10+
}).unknown()
11+
},
12+
13+
async handler (request, h) {
14+
const quiet = request.query.quiet
15+
const streamErrors = request.query['stream-errors']
16+
const { ipfs } = request.server.app
17+
const res = await ipfs.repo.gc({ quiet, streamErrors })
18+
return h.response(res.map(r => ({ Err: r.err, Key: { '/': r.cid } })))
19+
}
720
}
821

922
exports.version = async (request, h) => {

src/http/api/routes/repo.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ module.exports = [
1212
method: '*',
1313
path: '/api/v0/repo/stat',
1414
handler: resources.repo.stat
15+
},
16+
{
17+
method: '*',
18+
path: '/api/v0/repo/gc',
19+
options: {
20+
validate: resources.repo.gc.validate
21+
},
22+
handler: resources.repo.gc.handler
1523
}
1624
// TODO: implement the missing spec https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/REPO.md
1725
]

test/cli/repo.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
'use strict'
33

44
const expect = require('chai').expect
5+
const fs = require('fs')
6+
const os = require('os')
7+
const path = require('path')
58
const repoVersion = require('ipfs-repo').repoVersion
9+
const hat = require('hat')
10+
const clean = require('../utils/clean')
611

712
const runOnAndOff = require('../utils/on-and-off')
813

@@ -18,4 +23,34 @@ describe('repo', () => runOnAndOff((thing) => {
1823
expect(out).to.eql(`${repoVersion}\n`)
1924
})
2025
})
26+
27+
// Note: There are more comprehensive GC tests in interface-js-ipfs-core
28+
it('should run garbage collection', async () => {
29+
// Create and add a file to IPFS
30+
const filePath = path.join(os.tmpdir(), hat())
31+
const content = String(Math.random())
32+
fs.writeFileSync(filePath, content)
33+
const cid = (await ipfs(`add -Q ${filePath}`)).trim()
34+
35+
// File hash should be in refs local
36+
const localRefs = await ipfs('refs local')
37+
expect(localRefs.split('\n')).includes(cid)
38+
39+
// Run GC, file should not have been removed because it's pinned
40+
const gcOut = await ipfs('repo gc')
41+
expect(gcOut.split('\n')).not.includes('Removed ' + cid)
42+
43+
// Unpin file
44+
await ipfs('pin rm ' + cid)
45+
46+
// Run GC, file should now be removed
47+
const gcOutAfterUnpin = await ipfs('repo gc')
48+
expect(gcOutAfterUnpin.split('\n')).to.includes('Removed ' + cid)
49+
50+
const localRefsAfterGc = await ipfs('refs local')
51+
expect(localRefsAfterGc.split('\n')).not.includes(cid)
52+
53+
// Clean up file
54+
await clean(filePath)
55+
})
2156
}))

0 commit comments

Comments
 (0)