Block API

June 10, 2022 ยท View on GitHub

ipfs.block.get(cid, [options])

Get a raw IPFS block.

Parameters

NameTypeDescription
cidCID, String or Uint8ArrayA CID that corresponds to the desired block

Options

An optional object which may have the following keys:

NameTypeDefaultDescription
timeoutNumberundefinedA timeout in ms
signalAbortSignalundefinedCan be used to cancel any long running requests started as a result of this call
preloadbooleanfalseWhether to preload all blocks created during this operation

Returns

TypeDescription
Promise<Uint8Array>A Uint8Array containing the data of the block

Example

const block = await ipfs.block.get(cid)
console.log(block)

A great source of examples can be found in the tests for this API.

ipfs.block.put(block, [options])

Stores input as an IPFS block.

Parameters

NameTypeDescription
blockUint8ArrayThe block of data to store

Options

An optional object which may have the following keys:

NameTypeDefaultDescription
formatString'dag-pb'The codec to use to create the CID
mhtypeStringsha2-256The hashing algorithm to use to create the CID
mhlenNumberundefinedThe hash length (only relevant for go-ipfs)
versionNumber0The version to use to create the CID
pinbooleanfalseIf true, pin added blocks recursively
timeoutNumberundefinedA timeout in ms
signalAbortSignalundefinedCan be used to cancel any long running requests started as a result of this call
preloadbooleanfalseWhether to preload all blocks created during this operation

Returns

TypeDescription
Promise<CID>A CID type object containing the hash of the block

Example

// Defaults
const buf = new TextEncoder().encode('a serialized object')
const decoder = new TextDecoder()

const block = await ipfs.block.put(buf)

console.log(decoder.decode(block.data))
// Logs:
// a serialized object
console.log(block.cid.toString())
// Logs:
// the CID of the object

A great source of examples can be found in the tests for this API.

ipfs.block.rm(cid, [options])

Remove one or more IPFS block(s).

Parameters

NameTypeDescription
cidA CID or Array of CIDsBlocks corresponding to the passed CID(s) will be removed

Options

An optional object which may have the following keys:

NameTypeDefaultDescription
forcebooleanfalseIgnores nonexistent blocks
quietbooleanfalseWrite minimal output
timeoutNumberundefinedA timeout in ms
signalAbortSignalundefinedCan be used to cancel any long running requests started as a result of this call

Returns

TypeDescription
AsyncIterable<Object>An async iterable that yields objects containing hash and (potentially) error strings

Each object yielded is of the form:

{
  cid: CID,
  error?: Error
}

Note: If an error is present for a given object, the block with that cid was not removed and the error will contain the reason why, for example if the block was pinned.

Example

for await (const result of ipfs.block.rm(cid)) {
  if (result.error) {
    console.error(`Failed to remove block ${result.cid} due to ${result.error.message}`)
  } else {
    console.log(`Removed block ${result.cid}`)
  }
}

A great source of examples can be found in the tests for this API.

ipfs.block.stat(cid, [options])

Print information of a raw IPFS block.

Parameters

NameTypeDescription
cidA CID or Array of CIDsThe stats of the passed CID will be returned

Options

An optional object which may have the following keys:

NameTypeDefaultDescription
timeoutNumberundefinedA timeout in ms
signalAbortSignalundefinedCan be used to cancel any long running requests started as a result of this call
preloadbooleanfalseWhether to preload all blocks created during this operation

Returns

TypeDescription
Promise<Object>An object containing the block's info

the returned object has the following keys:

{
  cid: CID
  size: number
}

Example

const multihashStr = 'QmQULBtTjNcMwMr4VMNknnVv3RpytrLSdgpvMcTnfNhrBJ'
const cid = CID.parse(multihashStr)

const stats = await ipfs.block.stat(cid)
console.log(stats.cid.toString())
// Logs: QmQULBtTjNcMwMr4VMNknnVv3RpytrLSdgpvMcTnfNhrBJ
console.log(stat.size)
// Logs: 3739

A great source of examples can be found in the tests for this API.