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 13 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
2 changes: 1 addition & 1 deletion packages/interface-ipfs-core/src/pin/add-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module.exports = (common, options) => {
}())
})

it('should respect timeout option when pinning a block', () => {
it('should respect timeout option when pinning a blocks', () => {
return testTimeout(() => ipfs.pin.addAll([new CID('Qmd7qZS4T7xXtsNFdRoK1trfMs5zU94EpokQ9WFtxdPxsZ')], {
timeout: 1
}))
Expand Down
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: 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
}

21 changes: 21 additions & 0 deletions packages/ipfs-core-utils/src/iterable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @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 }
119 changes: 38 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,31 @@ 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 String>} input
* @param {{recursive?:boolean}} [options]
* @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, options) => {
if (typeof input === 'string') {
return { path: input, ...options }
} else if (input instanceof String) {
return { path: input.toString(), ...options }
} else if (CID.isCID(input)) {
return { path: input.toString(), ...options }
} 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
}
3 changes: 2 additions & 1 deletion packages/ipfs-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"err-code": "^3.0.1",
"hamt-sharding": "^2.0.0",
"hashlru": "^2.3.0",
"interface-datastore": "^3.0.3",
"interface-datastore": "^4.0.0",
"ipfs-bitswap": "^5.0.1",
"ipfs-block-service": "^0.19.0",
"ipfs-core-types": "^0.3.1",
Expand Down Expand Up @@ -122,6 +122,7 @@
"uint8arrays": "^2.1.3"
},
"devDependencies": {
"libp2p-interfaces": "^0.8.4",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise

src/ipns/routing/config.js:26:36 - error TS2345: Argument of type 'import("/Users/gozala/Projects/js-ipfs-2/node_modules/libp2p/node_modules/libp2p-interfaces/dist/src/pubsub/index")' is not assignable to parameter of type 'import("/Users/gozala/Projects/js-ipfs-2/node_modules/libp2p-interfaces/dist/src/pubsub/index")'.
  Types of property 'peers' are incompatible.
    Type 'Map<string, import("/Users/gozala/Projects/js-ipfs-2/node_modules/libp2p/node_modules/libp2p-interfaces/dist/src/pubsub/peer-streams")>' is not assignable to type 'Map<string, import("/Users/gozala/Projects/js-ipfs-2/node_modules/libp2p-interfaces/dist/src/pubsub/peer-streams")>'.
      Type 'import("/Users/gozala/Projects/js-ipfs-2/node_modules/libp2p/node_modules/libp2p-interfaces/dist/src/pubsub/peer-streams")' is not assignable to type 'import("/Users/gozala/Projects/js-ipfs-2/node_modules/libp2p-interfaces/dist/src/pubsub/peer-streams")'.
        Types have separate declarations of a private property '_rawOutboundStream'.
26     pubsubDs = new PubsubDatastore(pubsub, localDatastore, peerId)

"@types/dlv": "^1.1.2",
"aegir": "^32.1.0",
"delay": "^5.0.0",
Expand Down
Loading