# All files / src/serialization block.js

100% Statements 27/27
100% Branches 8/8
100% Functions 8/8
100% Lines 20/20

    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
    371x
    1x
    1x

    7x

    1x
    7x
    7x
    7x

    5x
    5x

    1x
    23x
    5x
    1x

    5x

    1x
    23x
    5x
    1x

    5x

     import Block from 'ipfs-block'
    import { cidFromJson, cidToJson } from './cid'
    import { bufferFromJson, bufferToJson } from './buffer'

    export const blockToJson = (block) => ({
      __ipfsPostMsgProxyType: 'Block',
      cid: cidToJson(block.cid),
      data: bufferToJson(block.data)
    })

    export const blockFromJson = (obj) => {
      const cid = cidFromJson(obj.cid)
      const data = bufferFromJson(obj.data)
      return new Block(data, cid)
    }

    export const isBlock = (obj) => obj && Block.isBlock(obj)
    export const isBlockJson = (obj) => obj && obj.__ipfsPostMsgProxyType === 'Block'

    export const preBlockFromJson = (index) => {
      return (...args) => {
        if (isBlockJson(args[index])) {
          args[index] = blockFromJson(args[index])
        }
        return args
      }
    }

    export const preBlockToJson = (index) => {
      return (...args) => {
        if (isBlock(args[index])) {
          args[index] = blockToJson(args[index])
        }
        return args
      }
    }