This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pin.add to ipfs-message-port
- Loading branch information
Showing
6 changed files
with
118 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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,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}` |
This file contains 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 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 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,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 } | ||
} | ||
} |
This file contains 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