-
Notifications
You must be signed in to change notification settings - Fork 14
feat: support pin.update #337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,20 @@ | ||
import { CID } from 'multiformats/cid' | ||
import { toUrlSearchParams } from '../lib/to-url-search-params.js' | ||
import type { PinAPI } from './index.js' | ||
import type { HTTPRPCClient } from '../lib/core.js' | ||
|
||
export function createUpdate (client: HTTPRPCClient): PinAPI['update'] { | ||
return async function update (from, to, options = {}) { | ||
const res = await client.post('pin/update', { | ||
signal: options.signal, | ||
searchParams: toUrlSearchParams({ | ||
...options, | ||
arg: [from.toString(), to.toString()] | ||
}), | ||
headers: options.headers | ||
}) | ||
|
||
const { Pins } = await res.json() | ||
return Pins.map((cid: string) => CID.parse(cid)) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,197 @@ | ||
/* eslint-env mocha */ | ||
|
||
import { expect } from 'aegir/chai' | ||
import all from 'it-all' | ||
import drain from 'it-drain' | ||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' | ||
import { getDescribe, getIt, type MochaConfig } from '../utils/mocha.js' | ||
import { fixtures, clearPins, expectPinned, expectNotPinned, pinTypes } from './utils.js' | ||
import type { KuboRPCClient } from '../../../../src/index.js' | ||
import type { Factory, KuboNode } from 'ipfsd-ctl' | ||
|
||
export function testUpdate (factory: Factory<KuboNode>, options: MochaConfig): void { | ||
const describe = getDescribe(options) | ||
const it = getIt(options) | ||
|
||
describe('.pin.update', function () { | ||
this.timeout(50 * 1000) | ||
|
||
let ipfs: KuboRPCClient | ||
|
||
before(async function () { | ||
ipfs = (await factory.spawn()).api | ||
|
||
await drain( | ||
ipfs.addAll( | ||
fixtures.files.map(file => ({ content: file.data })), { | ||
pin: false | ||
} | ||
) | ||
) | ||
|
||
await drain( | ||
ipfs.addAll(fixtures.directory.files.map( | ||
file => ({ | ||
path: file.path, | ||
content: file.data | ||
}) | ||
), { | ||
pin: false | ||
}) | ||
) | ||
}) | ||
|
||
after(async function () { | ||
await factory.clean() | ||
}) | ||
|
||
beforeEach(async function () { | ||
return clearPins(ipfs) | ||
}) | ||
|
||
it('should update a recursive pin', async () => { | ||
// First pin the old object | ||
await ipfs.pin.add(fixtures.directory.cid) | ||
|
||
// Create a new object to update to that links to one of the existing files | ||
const newObject = await ipfs.dag.put({ | ||
Data: uint8ArrayFromString('new object'), | ||
Links: [{ | ||
Name: 'ipfs-add.js', | ||
Hash: fixtures.directory.files[0].cid, | ||
Tsize: 0 | ||
}] | ||
}, { | ||
storeCodec: 'dag-pb', | ||
hashAlg: 'sha2-256' | ||
}) | ||
|
||
// Update the pin | ||
const result = await ipfs.pin.update(fixtures.directory.cid, newObject) | ||
|
||
// Check the result | ||
expect(result).to.deep.include(newObject) | ||
|
||
// Verify old pin is removed | ||
await expectNotPinned(ipfs, fixtures.directory.cid) | ||
|
||
// Verify new pin is added | ||
await expectPinned(ipfs, newObject, pinTypes.recursive) | ||
|
||
// Verify the linked file is still pinned indirectly | ||
await expectPinned(ipfs, fixtures.directory.files[0].cid, pinTypes.indirect) | ||
|
||
// Verify the old recursive object is not pinned | ||
await expectNotPinned(ipfs, fixtures.directory.files[1].cid) | ||
}) | ||
|
||
it('should update a recursive pin without removing the old pin', async () => { | ||
// First pin the old object | ||
await ipfs.pin.add(fixtures.directory.cid) | ||
|
||
// Create a new object to update to that links to one of the existing files | ||
const newObject = await ipfs.dag.put({ | ||
Data: uint8ArrayFromString('new object'), | ||
Links: [{ | ||
Name: 'ipfs-add.js', | ||
Hash: fixtures.directory.files[0].cid, | ||
Tsize: 0 | ||
}] | ||
}, { | ||
storeCodec: 'dag-pb', | ||
hashAlg: 'sha2-256' | ||
}) | ||
|
||
// Update the pin without removing the old one | ||
const result = await ipfs.pin.update(fixtures.directory.cid, newObject, { | ||
unpin: false | ||
}) | ||
|
||
// Check the result | ||
expect(result).to.deep.include(newObject) | ||
|
||
// Verify old pin is still there | ||
await expectPinned(ipfs, fixtures.directory.cid, pinTypes.recursive) | ||
|
||
// Verify new pin is added | ||
await expectPinned(ipfs, newObject, pinTypes.recursive) | ||
|
||
// Verify the linked file is pinned indirectly through both objects | ||
await expectPinned(ipfs, fixtures.directory.files[0].cid, pinTypes.indirect) | ||
|
||
// Verify the unlinked file is still pinned indirectly through the old object | ||
await expectPinned(ipfs, fixtures.directory.files[1].cid, pinTypes.indirect) | ||
}) | ||
|
||
it('should update a recursive pin with a name label', async () => { | ||
// First pin the old object with a name | ||
const pinName = 'my-label' | ||
await ipfs.pin.add(fixtures.directory.cid, { name: pinName }) | ||
|
||
// Create a new object to update to that links to one of the existing files | ||
const newObject = await ipfs.dag.put({ | ||
Data: uint8ArrayFromString('new object'), | ||
Links: [{ | ||
Name: 'ipfs-add.js', | ||
Hash: fixtures.directory.files[0].cid, | ||
Tsize: 0 | ||
}] | ||
}, { | ||
storeCodec: 'dag-pb', | ||
hashAlg: 'sha2-256' | ||
}) | ||
|
||
// Update the pin | ||
const result = await ipfs.pin.update(fixtures.directory.cid, newObject) | ||
|
||
// Check the result | ||
expect(result).to.deep.include(newObject) | ||
|
||
// Verify old pin is removed | ||
await expectNotPinned(ipfs, fixtures.directory.cid) | ||
// Verify the old recursive object is not pinned | ||
await expectNotPinned(ipfs, fixtures.directory.files[1].cid) | ||
|
||
// Verify new pin is added and has the name label | ||
const pinset = await all(ipfs.pin.ls({ name: pinName })) | ||
expect(pinset).to.have.lengthOf(1) | ||
expect(pinset[0].cid.toString()).to.equal(newObject.toString()) | ||
expect(pinset[0].name).to.equal(pinName) | ||
}) | ||
|
||
it('should fail to update a non-recursive pin', async () => { | ||
// First pin the old object directly | ||
await ipfs.pin.add(fixtures.directory.cid, { | ||
recursive: false | ||
}) | ||
|
||
// Create a new object to update to | ||
const newObject = await ipfs.dag.put({ | ||
Data: uint8ArrayFromString('new object'), | ||
Links: [] | ||
}, { | ||
storeCodec: 'dag-pb', | ||
hashAlg: 'sha2-256' | ||
}) | ||
|
||
// Try to update the pin | ||
await expect(ipfs.pin.update(fixtures.directory.cid, newObject)) | ||
.to.eventually.be.rejectedWith(/not recursively pinned/) | ||
}) | ||
|
||
it('should fail to update a non-existent pin', async () => { | ||
// Create a new object to update to | ||
const newObject = await ipfs.dag.put({ | ||
Data: uint8ArrayFromString('new object'), | ||
Links: [] | ||
}, { | ||
storeCodec: 'dag-pb', | ||
hashAlg: 'sha2-256' | ||
}) | ||
|
||
// Try to update a non-existent pin | ||
await expect(ipfs.pin.update(fixtures.directory.cid, newObject)) | ||
.to.eventually.be.rejectedWith(/not recursively pinned/) | ||
}) | ||
}) | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.