ipfslibp2pgo-libp2p

The use of Buffer before saving to IPFS


I am following an IPFS github example to save to IPFS:

'use strict'

const IPFS = require('ipfs')
const all = require('it-all')

async function main () {
  const node = await IPFS.create()
  const version = await node.version()

  console.log('Version:', version.version)

  for await (const file of await node.add({
    path: 'hello.txt',
    content: Buffer.from('Hello World 101')   //<<<=== why Buffer before assigned to content?
  })) {
    console.log('Added file:', file.path, file.cid.toString())

    const data = Buffer.concat(await all(node.cat(file.cid)))

    console.log('Added file contents:', data.toString())
  }
}

main()

I notice the string is converted to binary with Buffer before saving. Can someone explain the use of Buffer here? What about saving an image or video file?


Solution

  • By default Node.js works with Buffers where use APIs who can be streams, if you works with strings in JavaScript, these works in unicode (utf-8) and these can broke with binary data (for example, an image, a video file, etc).

    An easy example to difference strings from Buffers could be compare the size of UTF-8 string, as unicode text (counting by character) and a buffer (counting by byte):

    > const str = 'Hello 世界';
    undefined
    > str.length
    8
    > const buf = Buffer.from(str, 'utf8');
    undefined
    > buf.length
    12
    > buf.toString('hex');
    '48656c6c6f7720e4b896e7958c'
    > buf.toString('utf8');
    'Hello 世界'
    

    In summary, it's a standard work with buffers with APIs like FS, Socket, etc.