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

feat: Add pin.add to ipfs-message-port #3575

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
e8236c4
feat: add pin.add to ipfs-message-port
icidasset Jan 21, 2021
dd8fa53
chore: add interface-ipfs-core tests for pin to ipfs-message-port-client
icidasset Jan 27, 2021
7ab685b
feat: add pin.ls and pin.rmAll to ipfs-message-port
icidasset Feb 25, 2021
c6e5fc5
chore: undo dep changes
Gozala Feb 26, 2021
d184a72
fix: in progress msgport pin API
Gozala Feb 27, 2021
848da0e
chore: revert version change
Gozala Feb 27, 2021
43b7a44
chore: update buffer deps
achingbrain Apr 1, 2021
103f4f8
chore: dep updates
achingbrain Apr 9, 2021
8282424
chore: switch to multiaddr named export
achingbrain Apr 9, 2021
f780d96
chore: update more deps
achingbrain Apr 12, 2021
c6381a4
chore: update types
achingbrain Apr 16, 2021
46a76be
chore: linting
achingbrain Apr 16, 2021
58def59
chore: update deps
achingbrain Apr 17, 2021
7bef78d
chore: update deps
achingbrain Apr 21, 2021
302f0c3
chore: fix up tests
achingbrain Apr 21, 2021
ae92293
chore: merge master
Gozala Apr 21, 2021
3cd20f2
fix: regressions introduced by a merge
Gozala Apr 22, 2021
81dbb83
fix: uintended api change
Gozala Apr 22, 2021
1bec996
chore: undo uintended changes
Gozala Apr 22, 2021
c68dd94
fix: missed import update
Gozala Apr 22, 2021
223853e
chore: align interface-datastore versions
Gozala Apr 22, 2021
d49191b
fix: align libp2p-interfaces versions
Gozala Apr 22, 2021
3c26253
fix: import regressions
Gozala Apr 22, 2021
9026ee4
fix: lint errors
Gozala Apr 22, 2021
d3ba7c3
fix: lint issue
Gozala Apr 22, 2021
8a9d433
fix: remove libp2p-interfaces dependency
Gozala Apr 22, 2021
3ac0bf9
chore: update deps, remove ts-ignores, fix linting
achingbrain Apr 22, 2021
e178955
chore: downgrade mock-ipfs-pinning-service
achingbrain Apr 22, 2021
de303dd
chore: fix up interface tests
achingbrain Apr 22, 2021
e842b5e
chore: demand non-broken datastore-pubsub
achingbrain Apr 22, 2021
76067b7
chore: types
achingbrain Apr 22, 2021
ebb2f50
chore: remove redundunt types
Gozala Apr 23, 2021
ed8e1f0
chore: merge chore/update-buffer-deps
Gozala Apr 23, 2021
29a7aad
fix: type missmatch
Gozala Apr 23, 2021
0a5a214
fix: address regressions in normaliseInput
Gozala Apr 23, 2021
0a21541
Merge remote-tracking branch 'upstream/master' into feat/pin-msgport
Gozala Jun 22, 2021
66cb5db
chore: undo unintended changes
Gozala Jun 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 37 additions & 25 deletions packages/ipfs-core-types/src/pin/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AbortOptions, AwaitIterable } from '../utils'
import type CID from 'cids'
import type { API as Remote } from './remote'
import type { API as RemoteAPI } from './remote'

export interface API<OptionExtension = {}> {
/**
Expand Down Expand Up @@ -32,7 +32,7 @@ export interface API<OptionExtension = {}> {
* // CID('QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u')
* ```
*/
addAll: (source: AwaitIterable<AddInput>, options?: AddAllOptions & OptionExtension) => AsyncIterable<CID>
addAll: (source: PinSource, options?: AddAllOptions & OptionExtension) => AsyncIterable<CID>

/**
* List all the objects pinned to local storage
Expand Down Expand Up @@ -90,9 +90,9 @@ export interface API<OptionExtension = {}> {
* // CID('QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u')
* ```
*/
rmAll: (source: AwaitIterable<RmAllInput>, options?: AbortOptions & OptionExtension) => AsyncIterable<CID>
rmAll: (source: PinSource, options?: AbortOptions & OptionExtension) => AsyncIterable<CID>

remote: Remote<OptionExtension>
remote: RemoteAPI<OptionExtension>
}

export interface AddOptions extends AbortOptions {
Expand Down Expand Up @@ -124,50 +124,62 @@ export interface AddAllOptions extends AbortOptions {
lock?: boolean
}

export interface AddInput {
/**
* A CID to pin - nb. you must pass either `cid` or `path`, not both
*/
cid?: CID
export type Metadata = Record<string, any>
export type ToPin =
| CID
| string
| ToPinWithCID
| ToPinWithPath

export interface ToPinWithPath extends PinDetails {
/**
* An IPFS path to pin - nb. you must pass either `cid` or `path`, not both
*/
path?: string
* An IPFS path to pin.
*/
path: string

cid?: undefined
}

export interface ToPinWithCID extends PinDetails {
path?: undefined
cid: CID
}

export interface PinDetails {
/**
* If true, pin all blocked linked to from the pinned CID
*/
recursive?: boolean

metadata?: Metadata

/**
* A human readable string to store with this pin
*/
comments?: string
}

export type PinType = 'recursive' | 'direct' | 'indirect' | 'all'

export type PinQueryType = 'recursive' | 'direct' | 'indirect' | 'all'

export interface LsOptions extends AbortOptions {
paths?: CID | CID[] | string | string[]
export type PinSource = AwaitIterable<ToPin>

export type PinType =
| 'recursive'
| 'direct'
| 'indirect'
export type PinQueryType =
| PinType
| 'all'
export interface LsOptions extends AbortOptions {
paths?: CID | string | Array<CID | string>
type?: PinQueryType
}

export interface LsResult {
cid: CID
type: PinType | string
metadata?: Record<string, any>
metadata?: Metadata
}

export interface RmOptions extends AbortOptions {
recursive?: boolean
}

export interface RmAllInput {
cid?: CID
path?: string
recursive?: boolean
}

54 changes: 54 additions & 0 deletions packages/ipfs-core-types/src/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,57 @@ export interface BufferStore {
get: (key: Uint8Array) => Promise<Uint8Array>
stores: any[]
}

export interface Blockstore {
open: () => Promise<Void>

/**
* Query the store
*/
query: (Query, options?: DatastoreOptions) => AsyncIterable<Block>

/**
* Query the store, returning only keys
*/
queryKeys: (query: KeyQuery, options?: DatastoreOptions) => AsyncIterable<CID>

/**
* Get a single block by CID
*/
get: (cid: CID, options?: DatastoreOptions) => Promise<Block>

/**
* Like get, but for more
*/
getMany: (cids: AwaitIterable<CID>, options?: DatastoreOptions) => AsyncIterable<Block>

/**
* Write a single block to the store
*/
put: (block: Block, options?: DatastoreOptions) => Promise<Block>

/**
* Like put, but for more
*/
putMany: (blocks: AwaitIterable<Block>, options?: DatastoreOptions) => AsyncIterable<Block>

/**
* Does the store contain block with this CID?
*/
has: (cid: CID, options?: DatastoreOptions) => Promise<boolean>

/**
* Delete a block from the store
*/
delete: (cid: CID, options?: DatastoreOptions) => Promise<Void>

/**
* Delete a block from the store
*/
deleteMany: (cids: AwaitIterable<any>, options?: DatastoreOptions) => AsyncIterable<Key>

/**
* Close the store
*/
close: () => Promise<Void>
}
23 changes: 23 additions & 0 deletions packages/ipfs-core-utils/src/iterable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

/**
* @template {Iterable<any>} T
* @template U
* @param {T|U} value
* @returns {value is T}
*/
const isIterable = (value) =>
// @ts-ignore
value && value[Symbol.iterator]

/**
* @template {AsyncIterable<any>} T
* @template U
* @param {T|U} value
* @returns {value is T}
*/
const isAsyncIterable = value =>
// @ts-ignore
value && value[Symbol.asyncIterator]

module.exports = { isIterable, isAsyncIterable }
118 changes: 37 additions & 81 deletions packages/ipfs-core-utils/src/pins/normalise-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const errCode = require('err-code')
const CID = require('cids')
const { isIterable, isAsyncIterable } = require('../iterable')

/**
* @typedef {Object} Pinnable
Expand Down Expand Up @@ -42,24 +43,33 @@ const CID = require('cids')
* AsyncIterable<{ path: CID|String, recursive:boolean, metadata }>
* ```
*
* @param {Source} input
* @returns {AsyncIterable<Pin>}
* @param {import('ipfs-core-types/src/pin').ToPin|import('ipfs-core-types/src/pin').PinSource} input
* @returns {AsyncIterable<import('ipfs-core-types/src/pin').ToPinWithPath>}
*/
// eslint-disable-next-line complexity
module.exports = async function * normaliseInput (input) {
async function * normaliseInput (input) {
// must give us something
if (input === null || input === undefined) {
throw errCode(new Error(`Unexpected input: ${input}`), 'ERR_UNEXPECTED_INPUT')
}

// CID|String
if (CID.isCID(input)) {
yield toPin({ cid: input })
if (CID.isCID(input) || typeof input === 'string' || input instanceof String) {
return yield toPin(input)
}

// Iterable<?>
if (isIterable(input)) {
for (const each of input) {
yield toPin(each)
}
return
}

if (input instanceof String || typeof input === 'string') {
yield toPin({ path: input })
// AsyncIterable<?>
if (isAsyncIterable(input)) {
for await (const each of input) {
yield toPin(each)
}
return
}

Expand All @@ -70,84 +80,30 @@ module.exports = async function * normaliseInput (input) {
return yield toPin(input)
}

// Iterable<?>
if (Symbol.iterator in input) {
// @ts-ignore
const iterator = input[Symbol.iterator]()
const first = iterator.next()
if (first.done) return iterator

// Iterable<CID|String>
if (CID.isCID(first.value) || first.value instanceof String || typeof first.value === 'string') {
yield toPin({ cid: first.value })
for (const cid of iterator) {
yield toPin({ cid })
}
return
}

// Iterable<{ cid: CID recursive, metadata }>
if (first.value.cid != null || first.value.path != null) {
yield toPin(first.value)
for (const obj of iterator) {
yield toPin(obj)
}
return
}

throw errCode(new Error('Unexpected input: ' + typeof input), 'ERR_UNEXPECTED_INPUT')
}

// AsyncIterable<?>
if (Symbol.asyncIterator in input) {
// @ts-ignore
const iterator = input[Symbol.asyncIterator]()
const first = await iterator.next()
if (first.done) return iterator

// AsyncIterable<CID|String>
if (CID.isCID(first.value) || first.value instanceof String || typeof first.value === 'string') {
yield toPin({ cid: first.value })
for await (const cid of iterator) {
yield toPin({ cid })
}
return
}

// AsyncIterable<{ cid: CID|String recursive, metadata }>
if (first.value.cid != null || first.value.path != null) {
yield toPin(first.value)
for await (const obj of iterator) {
yield toPin(obj)
}
return
}

throw errCode(new Error('Unexpected input: ' + typeof input), 'ERR_UNEXPECTED_INPUT')
}

throw errCode(new Error('Unexpected input: ' + typeof input), 'ERR_UNEXPECTED_INPUT')
}

/**
* @param {Pinnable} input
* @param {import('ipfs-core-types/src/pin').ToPin | InstanceType<typeof window.String>} input
* @returns {import('ipfs-core-types/src/pin').ToPinWithPath}
*/
function toPin (input) {
const path = input.cid || `${input.path}`

if (!path) {
throw errCode(new Error('Unexpected input: Please path either a CID or an IPFS path'), 'ERR_UNEXPECTED_INPUT')
}

/** @type {Pin} */
const pin = {
path,
recursive: input.recursive !== false
}

if (input.metadata != null) {
pin.metadata = input.metadata
const toPin = (input) => {
if (typeof input === 'string') {
return { path: input, recursive: true }
} else if (input instanceof String) {
return { path: input.toString(), recursive: true }
} else if (CID.isCID(input)) {
return { path: input.toString(), recursive: true }
} else {
return {
path: `${input.path == null ? input.cid : input.path}`,
recursive: input.recursive !== false,
...(input.metadata && { metadata: input.metadata })
}
}
}

return pin
module.exports = {
normaliseInput,
toPin
}
2 changes: 1 addition & 1 deletion packages/ipfs-core-utils/test/pins/normalise-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint-env mocha */

const { expect } = require('aegir/utils/chai')
const normalise = require('../../src/pins/normalise-input')
const { normaliseInput: normalise } = require('../../src/pins/normalise-input')
const all = require('it-all')
const CID = require('cids')

Expand Down
Loading