UNPKG

@ceramicnetwork/pinning-ipfs-backend

Version:

IPFS Pinning backend

92 lines 3.88 kB
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _IpfsPinning_ipfs; import * as sha256 from '@stablelib/sha256'; import { toString } from 'uint8arrays/to-string'; import { create as createIpfsClient } from 'ipfs-http-client'; const FROM_CONTEXT_HOST = 'ipfs+context'; export class NoIpfsInstanceError extends Error { constructor() { super('No IPFS instance available'); } } const textEncoder = new TextEncoder(); export class IpfsPinning { constructor(connectionString, ipfs) { this.connectionString = connectionString; _IpfsPinning_ipfs.set(this, void 0); if (connectionString == 'ipfs+context') { this.ipfsAddress = FROM_CONTEXT_HOST; } else { const url = new URL(connectionString); const ipfsHost = url.hostname; const ipfsPort = parseInt(url.port, 10) || 5001; const protocol = url.protocol .replace('ipfs+http:', 'http') .replace('ipfs+https:', 'https') .replace('ipfs+context:', FROM_CONTEXT_HOST); if (protocol === FROM_CONTEXT_HOST) { this.ipfsAddress = FROM_CONTEXT_HOST; } else { this.ipfsAddress = `${protocol}://${ipfsHost}:${ipfsPort}`; } } __classPrivateFieldSet(this, _IpfsPinning_ipfs, ipfs, "f"); const bytes = textEncoder.encode(this.connectionString); const digest = toString(sha256.hash(bytes), 'base64urlpad'); this.id = `${IpfsPinning.designator}@${digest}`; } get ipfs() { return __classPrivateFieldGet(this, _IpfsPinning_ipfs, "f"); } open() { if (this.ipfsAddress === FROM_CONTEXT_HOST) { if (!__classPrivateFieldGet(this, _IpfsPinning_ipfs, "f")) { throw new NoIpfsInstanceError(); } } else { __classPrivateFieldSet(this, _IpfsPinning_ipfs, createIpfsClient({ url: this.ipfsAddress, }), "f"); } } async close() { } async pin(cid) { await __classPrivateFieldGet(this, _IpfsPinning_ipfs, "f")?.pin.add(cid, { recursive: false }); } async unpin(cid) { await __classPrivateFieldGet(this, _IpfsPinning_ipfs, "f")?.pin.rm(cid); } async ls() { const iterable = __classPrivateFieldGet(this, _IpfsPinning_ipfs, "f")?.pin.ls(); if (iterable) { const result = {}; for await (const r of iterable) { result[r.cid.toString()] = [this.id]; } return result; } else { return {}; } } async info() { return { [this.id]: {} }; } } _IpfsPinning_ipfs = new WeakMap(); IpfsPinning.designator = 'ipfs'; //# sourceMappingURL=index.js.map