Skip to main content

🔀 Buffer

  • Buffer: A temporary storage spot for data. Think of it like a digital clipboard where you can keep data until you use it.
  • Stream: A sequence of data elements that can be accessed in a sequential manner. It's like a conveyor belt of data items.

If you're looking to upload raw data, like a buffer or a stream, to Lighthouse, here's a simple explanation:

/**
* This function allows you to upload a buffer or a stream directly to Lighthouse.
*
* @param {Buffer} buffer - Your data in the form of a buffer or stream.
* @param {string} apiKey - Your personal API key for Lighthouse.
* @param {object} options - Optional configuration (cidVersion, headers).
*
* @return {object} - Returns details about the uploaded data.
*/

const uploadResponse = await lighthouse.uploadBuffer(buffer, apiKey)
console.log(uploadResponse)

/* Sample response
{
data: {
Name: 'QmUHDKv3NNL1mrg4NTW4WwJqetzwZbGNitdjr2G6Z5Xe6s',
Hash: 'QmUHDKv3NNL1mrg4NTW4WwJqetzwZbGNitdjr2G6Z5Xe6s',
Size: '31735'
}
}
*/

With Options (Storage Type Header):

import lighthouse from '@lighthouse-web3/sdk'

const buffer = Buffer.from('your data here')

const uploadResponse = await lighthouse.uploadBuffer(buffer, apiKey, {
cidVersion: 1,
headers: {
storageType: 'lifetime' // Storage plan: "annual" or "lifetime"
}
})

console.log(uploadResponse)
Backward Compatibility

The old positional parameter signature still works: uploadBuffer(buffer, apiKey, cidVersion)