{"version":3,"sources":["../../src/storage/ipfs/index.ts"],"names":[],"mappings":"AAAA;;GAEG;;AAEH,OAAO,EAAE,GAAG,EAAE,MAAM,KAAK,CAAC;AAC1B,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAE3B,OAAO,EAAE,KAAK,EAAE,oBAAoB;AACpC,OAAO,KAAK,MAAM,qBAAqB;AACvC,OAAO,KAAK,OAAO,aAAsB;AAMzC,UAAU,WAAW;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,qBAAa,WAAY,YAAW,OAAO,CAAC,OAAO;IACjD,OAAO,CAAC,IAAI,CAAS;IAErB,OAAO,CAAC,IAAI,CAAS;IAErB,OAAO,CAAC,QAAQ,CAAS;IAEzB,OAAO,CAAC,IAAI,CAAM;gBAEN,OAAO,EAAE,WAAW;IAY1B,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;IAOpC,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAQtC,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAItC,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;CAKhD;AAED,MAAM,CAAC,OAAO,OAAO,aAAc,SAAQ,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC;IAC1E,WAAW;;;;;;OAMR;IAEH;;OAEG;IACG,IAAI,CAAC,OAAO,EAAE;QAAE,IAAI,EAAE,WAAW,CAAA;KAAE,GAAG,OAAO,CAAC,WAAW,CAAC;CAIjE","file":"index.d.ts","sourcesContent":["/**\n * @module \"ethpm/storage/ipfs\"\n */\n\nimport { URL } from 'url';\nimport * as t from 'io-ts';\n\nimport { Maybe } from 'ethpm/types';\nimport * as config from 'ethpm/config';\nimport * as storage from 'ethpm/storage';\n\nimport hash from './hash';\n\nconst IPFS = require('ipfs-http-client');\n\ninterface IpfsOptions {\n  host: string;\n  port: number | string;\n  protocol: string;\n}\n\nexport class IpfsService implements storage.Service {\n  private host: string;\n\n  private port: string;\n\n  private protocol: string;\n\n  private ipfs: any;\n\n  constructor(options: IpfsOptions) {\n    this.host = options.host;\n    this.port = options.port.toString();\n    this.protocol = options.protocol;\n\n    this.ipfs = new IPFS({\n      host: this.host,\n      port: this.port,\n      protocol: this.protocol,\n    });\n  }\n\n  async write(content: string): Promise<URL> {\n    const buffer = this.ipfs.types.Buffer.from(content);\n    const [{ hash }] = await this.ipfs.add(buffer);\n\n    return new URL(`ipfs://${hash}`);\n  }\n\n  async read(uri: URL): Promise<Maybe<string>> {\n    const hash = uri.host;\n\n    const buffer = await this.ipfs.cat(hash);\n\n    return buffer.toString();\n  }\n\n  async hash(content: string): Promise<string> {\n    return await hash(content);\n  }\n\n  async predictUri(content: string): Promise<URL> {\n    const hash = await this.hash(content);\n\n    return new URL(`ipfs://${hash}`);\n  }\n}\n\nexport default class IpfsConnector extends config.Connector<storage.Service> {\n  optionsType = t.interface({\n    ipfs: t.interface({\n      host: t.string,\n      port: t.union([t.number, t.string]),\n      protocol: t.string,\n    }),\n  });\n\n  /**\n   * Construct IpfsService and load with specified contents\n   */\n  async init(options: { ipfs: IpfsOptions }): Promise<IpfsService> {\n    const service = new IpfsService(options.ipfs);\n    return service;\n  }\n}\n"]}