All files / src/ipfs encoded.ts

0% Statements 0/9
0% Branches 0/6
0% Functions 0/2
0% Lines 0/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27                                                     
import cbor from 'borc'
 
import { isBlob, isJust } from '../common/type-checks'
import { Maybe } from '../common/types'
import * as blob from '../common/blob'
import * as keystore from '../keystore'
 
// IPFS
 
import { CID, FileContent, AddResult } from './types'
import * as basic from './basic'
 
 
export const add = async (content: FileContent, key: Maybe<string>): Promise<AddResult> => {
  // can't cbor encode blobs ie file streams
  const normalized = isBlob(content) ? await blob.toBuffer(content) : content
  const encoded = cbor.encode(normalized)
  const toAdd = isJust(key) ? await keystore.encrypt(encoded, key) : encoded
  return basic.add(toAdd)
}
 
export const catAndDecode = async (cid: CID, key: Maybe<string>): Promise<unknown> => {
  const buf = await basic.catBuf(cid)
  const toDecode = isJust(key) ? await keystore.decrypt(buf, key) : buf
  return cbor.decode(toDecode)
}