From e8236c436a05dbad8ee11ac4fc39f36d82074436 Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Thu, 21 Jan 2021 18:44:30 +0100 Subject: [PATCH] feat: add pin.add to ipfs-message-port --- .../ipfs-message-port-client/src/index.js | 4 +- packages/ipfs-message-port-client/src/pin.js | 55 +++++++++++++++++++ .../ipfs-message-port-server/src/index.js | 3 + packages/ipfs-message-port-server/src/ipfs.ts | 11 +++- packages/ipfs-message-port-server/src/pin.js | 41 ++++++++++++++ .../ipfs-message-port-server/src/service.js | 10 ++-- 6 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 packages/ipfs-message-port-client/src/pin.js create mode 100644 packages/ipfs-message-port-server/src/pin.js diff --git a/packages/ipfs-message-port-client/src/index.js b/packages/ipfs-message-port-client/src/index.js index 205298b822..4c9c2b6042 100644 --- a/packages/ipfs-message-port-client/src/index.js +++ b/packages/ipfs-message-port-client/src/index.js @@ -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 @@ -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) } /** diff --git a/packages/ipfs-message-port-client/src/pin.js b/packages/ipfs-message-port-client/src/pin.js new file mode 100644 index 0000000000..0f8d4b5ef8 --- /dev/null +++ b/packages/ipfs-message-port-client/src/pin.js @@ -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} + */ +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} + */ + 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}` diff --git a/packages/ipfs-message-port-server/src/index.js b/packages/ipfs-message-port-server/src/index.js index 119aa05894..647f179fd3 100644 --- a/packages/ipfs-message-port-server/src/index.js +++ b/packages/ipfs-message-port-server/src/index.js @@ -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 diff --git a/packages/ipfs-message-port-server/src/ipfs.ts b/packages/ipfs-message-port-server/src/ipfs.ts index 765a4087e5..ed400e3d10 100644 --- a/packages/ipfs-message-port-server/src/ipfs.ts +++ b/packages/ipfs-message-port-server/src/ipfs.ts @@ -14,6 +14,7 @@ export interface IPFS extends Core { dag: DAG files: Files block: BlockService + pin: PinService } export interface IPFSFactory { @@ -62,6 +63,10 @@ export interface Core { ls: (ipfsPath: CID | string, options: CoreLsOptions) => AsyncIterable } +export interface PinService { + add(path: string | CID, options?: AddPinOptions): Promise +} + export interface AddOptions extends AbortOptions { chunker?: string cidVersion?: number @@ -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 diff --git a/packages/ipfs-message-port-server/src/pin.js b/packages/ipfs-message-port-server/src/pin.js new file mode 100644 index 0000000000..82860391d4 --- /dev/null +++ b/packages/ipfs-message-port-server/src/pin.js @@ -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} + */ + async add (input) { + const cid = await this.ipfs.pin.add(input.path, input) + /** @type {Transferable[]} */ + const transfer = [] + return { cid: encodeCID(cid, transfer), transfer } + } +} diff --git a/packages/ipfs-message-port-server/src/service.js b/packages/ipfs-message-port-server/src/service.js index 5c48f3836b..77068ad2eb 100644 --- a/packages/ipfs-message-port-server/src/service.js +++ b/packages/ipfs-message-port-server/src/service.js @@ -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 @@ -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) } }