This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support block.rm over http api (#2514)
* feat: support block.rm over http api Also, validate that a block is not pinned before removing it. * chore: skip config endpoint tests * chore: fix up interface tests * chore: rev http client * test: add more cli tests * chore: update deps * chore: give dep correct name * chore: fix failing test * chore: address PR comments * chore: update interop dep
- Loading branch information
1 parent
eefb10f
commit c9be79e
Showing
9 changed files
with
199 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,46 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
command: 'rm <key>', | ||
command: 'rm <hash...>', | ||
|
||
describe: 'Remove a raw IPFS block', | ||
describe: 'Remove IPFS block(s)', | ||
|
||
builder: {}, | ||
builder: { | ||
force: { | ||
alias: 'f', | ||
describe: 'Ignore nonexistent blocks', | ||
type: 'boolean', | ||
default: false | ||
}, | ||
quiet: { | ||
alias: 'q', | ||
describe: 'Write minimal output', | ||
type: 'boolean', | ||
default: false | ||
} | ||
}, | ||
|
||
handler ({ getIpfs, print, isDaemonOn, key, resolve }) { | ||
handler ({ getIpfs, print, hash, force, quiet, resolve }) { | ||
resolve((async () => { | ||
if (isDaemonOn()) { | ||
// TODO implement this once `js-ipfs-http-client` supports it | ||
throw new Error('rm block with daemon running is not yet implemented') | ||
const ipfs = await getIpfs() | ||
let errored = false | ||
|
||
for await (const result of ipfs.block._rmAsyncIterator(hash, { | ||
force, | ||
quiet | ||
})) { | ||
if (result.error) { | ||
errored = true | ||
} | ||
|
||
if (!quiet) { | ||
print(result.error || 'removed ' + result.hash) | ||
} | ||
} | ||
|
||
const ipfs = await getIpfs() | ||
await ipfs.block.rm(key) | ||
print('removed ' + key) | ||
if (errored && !quiet) { | ||
throw new Error('some blocks not removed') | ||
} | ||
})()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict' | ||
|
||
const { PassThrough } = require('readable-stream') | ||
|
||
function streamResponse (request, h, fn) { | ||
const output = new PassThrough() | ||
const errorTrailer = 'X-Stream-Error' | ||
|
||
Promise.resolve() | ||
.then(() => fn(output)) | ||
.catch(err => { | ||
request.raw.res.addTrailers({ | ||
[errorTrailer]: JSON.stringify({ | ||
Message: err.message, | ||
Code: 0 | ||
}) | ||
}) | ||
}) | ||
.finally(() => { | ||
output.end() | ||
}) | ||
|
||
return h.response(output) | ||
.header('x-chunked-output', '1') | ||
.header('content-type', 'application/json') | ||
.header('Trailer', errorTrailer) | ||
} | ||
|
||
module.exports = streamResponse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters