|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const CID = require('cids') |
| 4 | +const Client = require('../../lib/core') |
| 5 | +const Service = require('./service') |
| 6 | +const toUrlSearchParams = require('../../lib/to-url-search-params') |
| 7 | + |
| 8 | +/** |
| 9 | + * @typedef {import('../..').HttpOptions} HttpOptions |
| 10 | + * @typedef {import('../../lib/core').ClientOptions} ClientOptions |
| 11 | + * @typedef {import('ipfs-core-types/src/basic').AbortOptions} AbortOptions |
| 12 | + * @typedef {import('ipfs-core-types/src/pin/remote').API} API |
| 13 | + * @typedef {import('ipfs-core-types/src/pin/remote').Pin} Pin |
| 14 | + * @typedef {import('ipfs-core-types/src/pin/remote').AddOptions} AddOptions |
| 15 | + * @typedef {import('ipfs-core-types/src/pin/remote').Query} Query |
| 16 | + * @typedef {import('ipfs-core-types/src/pin/remote').Status} Status |
| 17 | + * |
| 18 | + * @implements {API} |
| 19 | + */ |
| 20 | +class Remote { |
| 21 | + /** |
| 22 | + * @param {ClientOptions} options |
| 23 | + */ |
| 24 | + constructor (options) { |
| 25 | + /** @private */ |
| 26 | + this.client = new Client(options) |
| 27 | + /** @readonly */ |
| 28 | + this.service = new Service(options) |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Stores an IPFS object(s) from a given path to a remote pinning service. |
| 33 | + * |
| 34 | + * @param {CID} cid |
| 35 | + * @param {AddOptions & AbortOptions & HttpOptions} options |
| 36 | + * @returns {Promise<Pin>} |
| 37 | + */ |
| 38 | + add (cid, options) { |
| 39 | + return Remote.add(this.client, cid, options) |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param {Client} client |
| 44 | + * @param {CID} cid |
| 45 | + * @param {AddOptions & AbortOptions & HttpOptions} options |
| 46 | + */ |
| 47 | + static async add (client, cid, { timeout, signal, headers, ...options }) { |
| 48 | + const response = await client.post('pin/remote/add', { |
| 49 | + timeout, |
| 50 | + signal, |
| 51 | + headers, |
| 52 | + searchParams: encodeAddParams({ cid, ...options }) |
| 53 | + }) |
| 54 | + |
| 55 | + return Remote.decodePin(await response.json()) |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @param {Object} json |
| 60 | + * @param {string} json.Name |
| 61 | + * @param {string} json.Cid |
| 62 | + * @param {Status} json.Status |
| 63 | + * @returns {Pin} |
| 64 | + */ |
| 65 | + static decodePin ({ Name: name, Status: status, Cid: cid }) { |
| 66 | + return { |
| 67 | + cid: new CID(cid), |
| 68 | + name, |
| 69 | + status |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns a list of matching pins on the remote pinning service. |
| 75 | + * |
| 76 | + * @param {Query & AbortOptions & HttpOptions} query |
| 77 | + */ |
| 78 | + ls (query) { |
| 79 | + return Remote.ls(this.client, query) |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * |
| 84 | + * @param {Client} client |
| 85 | + * @param {Query & AbortOptions & HttpOptions} options |
| 86 | + * @returns {AsyncIterable<Pin>} |
| 87 | + */ |
| 88 | + static async * ls (client, { timeout, signal, headers, ...query }) { |
| 89 | + const response = await client.post('pin/remote/ls', { |
| 90 | + signal, |
| 91 | + timeout, |
| 92 | + headers, |
| 93 | + searchParams: encodeQuery(query) |
| 94 | + }) |
| 95 | + |
| 96 | + for await (const pin of response.ndjson()) { |
| 97 | + yield Remote.decodePin(pin) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Removes a single pin object matching query allowing it to be garbage |
| 103 | + * collected (if needed). Will error if multiple pins mtach provided |
| 104 | + * query. To remove all matches use `rmAll` instead. |
| 105 | + * |
| 106 | + * @param {Query & AbortOptions & HttpOptions} query |
| 107 | + */ |
| 108 | + rm (query) { |
| 109 | + return Remote.rm(this.client, { ...query, all: false }) |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Removes all pin object that match given query allowing them to be garbage |
| 114 | + * collected if needed. |
| 115 | + * |
| 116 | + * @param {Query & AbortOptions & HttpOptions} query |
| 117 | + */ |
| 118 | + rmAll (query) { |
| 119 | + return Remote.rm(this.client, { ...query, all: true }) |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * |
| 124 | + * @param {Client} client |
| 125 | + * @param {{all: boolean} & Query & AbortOptions & HttpOptions} options |
| 126 | + */ |
| 127 | + static async rm (client, { timeout, signal, headers, ...query }) { |
| 128 | + await client.post('pin/remote/rm', { |
| 129 | + timeout, |
| 130 | + signal, |
| 131 | + headers, |
| 132 | + searchParams: encodeQuery(query) |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * @param {any} service |
| 139 | + * @returns {string} |
| 140 | + */ |
| 141 | +const encodeService = (service) => { |
| 142 | + if (typeof service === 'string' && service !== '') { |
| 143 | + return service |
| 144 | + } else { |
| 145 | + throw new TypeError('service name must be passed') |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * @param {any} cid |
| 151 | + * @returns {string} |
| 152 | + */ |
| 153 | +const encodeCID = (cid) => { |
| 154 | + if (CID.isCID(cid)) { |
| 155 | + return cid.toString() |
| 156 | + } else { |
| 157 | + throw new TypeError(`CID instance expected instead of ${cid}`) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * @param {Query & { all?: boolean }} query |
| 163 | + * @returns {URLSearchParams} |
| 164 | + */ |
| 165 | +const encodeQuery = ({ service, cid, name, status, all }) => { |
| 166 | + const query = toUrlSearchParams({ |
| 167 | + service: encodeService(service), |
| 168 | + name, |
| 169 | + force: all ? true : undefined |
| 170 | + }) |
| 171 | + |
| 172 | + if (cid) { |
| 173 | + for (const value of cid) { |
| 174 | + query.append('cid', encodeCID(value)) |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + if (status) { |
| 179 | + for (const value of status) { |
| 180 | + query.append('status', value) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + return query |
| 185 | +} |
| 186 | + |
| 187 | +/** |
| 188 | + * @param {AddOptions & {cid:CID}} options |
| 189 | + * @returns {URLSearchParams} |
| 190 | + */ |
| 191 | +const encodeAddParams = ({ cid, service, background, name, origins }) => { |
| 192 | + const params = toUrlSearchParams({ |
| 193 | + arg: encodeCID(cid), |
| 194 | + service: encodeService(service), |
| 195 | + name, |
| 196 | + background: background ? true : undefined |
| 197 | + }) |
| 198 | + |
| 199 | + if (origins) { |
| 200 | + for (const origin of origins) { |
| 201 | + params.append('origin', origin.toString()) |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + return params |
| 206 | +} |
| 207 | + |
| 208 | +module.exports = Remote |
0 commit comments