All files / src/fs link.ts

0% Statements 0/17
0% Branches 0/1
0% Functions 0/6
0% Lines 0/17

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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48                                                                                               
import dagPB from 'ipld-dag-pb'
 
import { DAGLink, UnixFSFile } from '../ipfs'
import { Link, SimpleLink } from './types'
 
 
export const toDAGLink = (link: SimpleLink): DAGLink => {
  const { name, cid, size } = link
  return new dagPB.DAGLink(name, size, cid)
}
 
export const fromFSFile = (fsObj: UnixFSFile): Link => {
  const { name = '', cid, size, mtime, type } = fsObj
  return {
    name,
    cid: cid.toString(),
    size,
    mtime,
    isFile: type !== "dir"
  }
}
 
export const fromDAGLink = (link: DAGLink): SimpleLink => {
  const name = link.Name
  const cid = link.Hash.toString()
  const size = link.Tsize
  return { name, cid, size }
}
 
export const make = (name: string, cid: string, isFile: boolean, size: number): Link => {
  return {
    name,
    cid,
    size,
    isFile,
    mtime: Date.now()
  }
}
 
type HasName = { name: string }
 
export const arrToMap = <T extends HasName>(arr: T[]): { [name: string]: T } => {
  return arr.reduce((acc, cur) => {
    acc[cur.name] = cur
    return acc
  }, {} as { [name: string]: T})
}