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

Commit

Permalink
feat: add pin.add to ipfs-message-port
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset committed Feb 24, 2021
1 parent e25091c commit e8236c4
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/ipfs-message-port-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const BlockClient = require('./block')
const DAGClient = require('./dag')
const CoreClient = require('./core')
const FilesClient = require('./files')
const PinClient = require('./pin')

/**
* @typedef {Object} ClientOptions
Expand All @@ -19,9 +20,10 @@ class IPFSClient extends CoreClient {
constructor (transport) {
super(transport)
this.transport = transport
this.block = new BlockClient(this.transport)
this.dag = new DAGClient(this.transport)
this.files = new FilesClient(this.transport)
this.block = new BlockClient(this.transport)
this.pin = new PinClient(this.transport)
}

/**
Expand Down
55 changes: 55 additions & 0 deletions packages/ipfs-message-port-client/src/pin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict'

/* eslint-env browser */
const Client = require('./client')
const { decodeCID, CID } = require('ipfs-message-port-protocol/src/cid')

/**
* @typedef {import('cids')} CID
* @typedef {import('ipfs-message-port-server/src/pin').EncodedCID} EncodedCID
* @typedef {import('ipfs-message-port-server/src/pin').PinService} PinService
* @typedef {import('./client').MessageTransport} MessageTransport
*/

/**
* @class
* @extends {Client<PinService>}
*/
class PinClient extends Client {
/**
* @param {MessageTransport} transport
*/
constructor (transport) {
super('pin', ['add'], transport)
}

/**
* @param {string|CID} pathOrCID
* @param {Object} [options]
* @property {boolean} [recursive=true]
* @property {number} [timeout]
* @property {AbortSignal} [signal]
*
* @returns {Promise<CID>}
*/
async add (pathOrCID, options = {}) {
const { recursive, timeout, signal } = options
const { cid } = await this.remote.add({
path: encodeLocation(pathOrCID),
recursive,
timeout,
signal
})
return decodeCID(cid)
}
}
module.exports = PinClient

/**
* Turns content address (path or CID) into path.
*
* @param {string|CID} pathOrCID
* @returns {string}
*/
const encodeLocation = pathOrCID =>
CID.isCID(pathOrCID) ? `/ipfs/${pathOrCID.toString()}` : `${pathOrCID}`
3 changes: 3 additions & 0 deletions packages/ipfs-message-port-server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ exports.BlockService = BlockService
const { IPFSService } = require('./service')
exports.IPFSService = IPFSService

const { PinService } = require('./pin')
exports.PinService = PinService

const { Server } = require('./server')
exports.Server = Server
11 changes: 10 additions & 1 deletion packages/ipfs-message-port-server/src/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface IPFS extends Core {
dag: DAG
files: Files
block: BlockService
pin: PinService
}

export interface IPFSFactory {
Expand Down Expand Up @@ -62,6 +63,10 @@ export interface Core {
ls: (ipfsPath: CID | string, options: CoreLsOptions) => AsyncIterable<LsEntry>
}

export interface PinService {
add(path: string | CID, options?: AddPinOptions): Promise<CID>
}

export interface AddOptions extends AbortOptions {
chunker?: string
cidVersion?: number
Expand All @@ -76,7 +81,11 @@ export interface AddOptions extends AbortOptions {
wrapWithDirectory?: boolean
}

export interface FileInput {
export interface AddPinOptions extends AbortOptions {
recursive?: boolean
}

export interface FileInput = {
path?: string
content?: FileContent
mode?: string | number | undefined
Expand Down
41 changes: 41 additions & 0 deletions packages/ipfs-message-port-server/src/pin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

/* eslint-env browser */

const { encodeCID } = require('ipfs-message-port-protocol/src/cid')

/**
* @typedef {import('ipfs-message-port-protocol/src/dag').EncodedCID} EncodedCID
* @typedef {import('./ipfs').IPFS} IPFS
*/

exports.PinService = class PinService {
/**
*
* @param {IPFS} ipfs
*/
constructor (ipfs) {
this.ipfs = ipfs
}

/**
* @typedef {Object} PinQuery
* @property {string} path
* @property {boolean} [recursive=true]
* @property {number} [timeout]
* @property {AbortSignal} [signal]
*
* @typedef {Object} PinResult
* @property {EncodedCID} cid
* @property {Transferable[]} transfer
*
* @param {PinQuery} input
* @returns {Promise<PinResult>}
*/
async add (input) {
const cid = await this.ipfs.pin.add(input.path, input)
/** @type {Transferable[]} */
const transfer = []
return { cid: encodeCID(cid, transfer), transfer }
}
}
10 changes: 6 additions & 4 deletions packages/ipfs-message-port-server/src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

/* eslint-env browser */

const { DAGService } = require('./dag')
const { BlockService } = require('./block')
const { CoreService } = require('./core')
const { DAGService } = require('./dag')
const { FilesService } = require('./files')
const { BlockService } = require('./block')
const { PinService } = require('./pin')

/**
* @typedef {import('./ipfs').IPFS} IPFS
Expand All @@ -17,9 +18,10 @@ exports.IPFSService = class IPFSService {
* @param {IPFS} ipfs
*/
constructor (ipfs) {
this.dag = new DAGService(ipfs)
this.block = new BlockService(ipfs)
this.core = new CoreService(ipfs)
this.dag = new DAGService(ipfs)
this.files = new FilesService(ipfs)
this.block = new BlockService(ipfs)
this.pin = new PinService(ipfs)
}
}

0 comments on commit e8236c4

Please sign in to comment.