How to use the BufferList function from bl

Find comprehensive JavaScript bl.BufferList code examples handpicked from public code repositorys.

bl.BufferList is a utility for Node.js that provides a way to buffer data from a stream into a single object.

92
93
94
95
96
97
98
99
100
101
// you usually go content.toString() after this...
const getFromIPFS = async hashToGet => {
  for await (const file of ipfs.get(hashToGet)) {
    console.log(file.path);
    if (!file.content) continue;
    const content = new BufferList();
    for await (const chunk of file.content) {
      content.append(chunk);
    }
    console.log(content);
fork icon1
star icon2
watch icon0

21
22
23
24
25
26
27
28
29
30
const { statusCode, headers, body: rawBody } = await telemetry.trackDuration(
  'http-head-cid-fetchs',
  request(`https://${s3Bucket}.s3.${awsRegion}.amazonaws.com/head`)
)

const buffer = new BufferList()

for await (const chunk of rawBody) {
  buffer.append(chunk)
}
fork icon1
star icon1
watch icon8

+ 2 other calls in file

How does bl.BufferList work?

bl.BufferList is a utility for Node.js that provides a way to buffer data from a stream into a single object. It works by creating an empty BufferList object and then listening to the data event on a stream. Whenever data is received on the stream, it is added to the end of the BufferList. When the stream ends, the BufferList object will contain all of the received data concatenated into a single Buffer. BufferList also provides a number of methods for working with the buffered data, such as slice for extracting a portion of the buffer, toString for converting the buffer to a string, and copy for copying the buffer to a new buffer object. Additionally, BufferList instances can be concatenated using the concat method to combine multiple buffers into a single buffer. Using BufferList can be useful in situations where you need to process the data from a stream as a single object, rather than processing it incrementally as it is received.

50
51
52
53
54
55
56
57
58
59
// you usually go content.toString() after this...
const getFromIPFS = async hashToGet => {
  // for await (const file of ipfs.get(hashToGet)) {
  //   console.log(file.path);
  //   if (!file.content) continue;
  //   const content = new BufferList();
  //   for await (const chunk of file.content) {
  //     content.append(chunk);
  //   }
  //   console.log(content);
fork icon0
star icon1
watch icon3

41
42
43
44
45
46
47
48
49
50
// // you usually go content.toString() after this...
// const getFromIPFS = async (hashToGet: string) => {
//   for await (const file of ipfs.get(hashToGet)) {
//     console.log(file.path);
//     if (!file.content) continue;
//     const content = new BufferList();
//     for await (const chunk of file.content) {
//       content.append(chunk);
//     }
//     console.log(content);
fork icon0
star icon1
watch icon3

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
const fs = require("fs");
const bl = require("bl");

const readableStream = fs.createReadStream("file.txt");
const bufferList = new bl.BufferList();

readableStream.on("data", (chunk) => {
  bufferList.append(chunk);
});

readableStream.on("end", () => {
  console.log(bufferList.toString());
});

In this example, we create a readable stream from a file using fs.createReadStream. We then create a new BufferList object to store the data. We add a data event listener to the stream to listen for incoming data. Each time data is received, we append it to the end of the BufferList using the append method. Finally, we listen for the end event on the stream, and when it is emitted, we log the entire buffer to the console using the toString method of the BufferList instance.

10
11
12
13
14
15
16
17
18
19
20
const goTestData = 'test data from go'


async function readWrite (stream) {
  await Promise.all([
    (async () => {
      let data = new BufferList(await pipe(stream, collect))
      let offset = 0
      for (let i = 0; i < 100; i++) {
        let expected = goTestData + ' ' + i
        assert.equal(data.slice(offset, offset + expected.length).toString(), expected)
fork icon0
star icon0
watch icon0

50
51
52
53
54
55
56
57
58
59
app.use(bodyParser.json());
app.post("/ytd", function (req, res) {
  const videoUrl = req.body.url;
  if (req.body.url.includes("https://www.youtube.com")) {
    const { BufferList } = require("bl");
    const buffer = new BufferList();
    const r = (Math.random() + 1).toString(36).substring(2);
    const options = {
      quality: "highest",
      filter: "videoandaudio",
fork icon0
star icon0
watch icon0

+ 2 other calls in file