Giter Club home page Giter Club logo

node-streams's Introduction

node-streams

How to run this project

  1. type npm install on your terminal inside the project
  2. run command node index.js

Issues?

  1. why code is writing a single response from the API in the excel file when I'm iterating till 20 pages of the API
  2. Other warning is
(node:9561) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added to [Stream]. Use emitter.setMaxListeners() to increase limit

node-streams's People

Contributors

amustaque97 avatar

Watchers

 avatar  avatar

node-streams's Issues

My solution

import fetch from 'node-fetch';
import {
  Readable,
  pipeline,
  Writable
} from 'stream';
import exceljs from 'exceljs';
import util from 'util';

const URL = 'https://jsonplaceholder.typicode.com/posts/'

const cpipeline = util.promisify(pipeline);

class ExcelStream extends Writable {
  constructor(options = {}) {
    super({
      ...options,
      objectMode: true
    });
    this.i = 0;
    const o = {
      filename: "./test-file.xlsx",
      useStyles: true,
      useSharedStrings: true,
    };
    this.workbook = new exceljs.stream.xlsx.WorkbookWriter(o);
    this.workbook.creator = "ABCDEF";
    this.workbook.created = new Date();
    this.sheet = this.workbook.addWorksheet("Discussion Event");
    this.sheet.columns = [{
      header: "testColumn",
      width: 10,
      style: {
        font: {
          bold: true,
        },
      },
    }, ];
  }

  _write (chunk, encoding, done) {
    console.log(chunk)
    try {
      this.i += 1;
      this.sheet.addRow([chunk]).commit();
    } catch (error) {
      console.log("Error while transforming data - commit failed");
      done(error)
      return
    }
    done()
  }

  _flush(cb) {
    try {
      this.sheet.commit();
      this.workbook.commit().then(() => cb(), cb);
    } catch (error) {
      console.log("Error cannot commit workbook");
    }
  }
}

async function run() {
  let i = 1;
  await cpipeline(async function * () {
    while (true) {
      let u = URL + i
      const response = await fetch(u)
      const result = await response.json()
      if (result) {
        yield result
      } else {
        break
      }

      i++
      if (i == 20) break
    }
  }, new ExcelStream())
}

run()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.