# All files / serialization block.js

33.33% Statements 9/27
0% Branches 0/8
0% Functions 0/8
45% Lines 9/20

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

    1x

    1x

    1x
    1x

    1x

    1x

     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
      }
    }