npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

streambox-collection

v0.0.6

Published

streambox-collection is a lightweight utility as a wrapper for displaying objects, arrays, strings, and number formats to clients using data streams.

Downloads

8

Readme

StreamBox Collection

Build Status Coverage Status npm version node-current npm npm bundle size npm bundle size Snyk Vulnerabilities for GitHub Repo PRs Welcome

streambox-collection is a lightweight utility as a wrapper for displaying objects, arrays, strings, and number formats to clients using data streams, so data flow responses will be returned in buffer form, streambox-collection can also support large or small data streams, see this article for more information on streams.

Installation Package

npm install streambox-collection -S or yarn add streambox-collection -S

API Reference

StreamBox Array( data: Record<string, any>[] | string[] | number[], delay?: number ): Promise

  • streamBox.Array - Method - create a data stream for the array and display it to the client
  • streamBox.Array.data - Params - set stream data for consumption to the client
  • streamBox.Array.delay - Params - set delay before returning data to the client

StreamBox Object( data: Record<string, any>, delay?: number ): Promise

  • streamBox.object - Method - create a data stream for the object and display it to the client
  • streamBox.object.data - Params - set stream data for consumption to the client
  • streamBox.object.delay - Params - set delay before returning data to the client

StreamBox String( data: string, delay?: number ): Promise

  • streamBox.string - Method - create a data stream for the string and display it to the client
  • streamBox.string.data - Params - set stream data for consumption to the client
  • streamBox.string.delay - Params - set delay before returning data to the client

StreamBox Number( data: number, delay?: number ): Promise

  • streamBox.number - Method - create a data stream for the number and display it to the client
  • streamBox.number.data - Params - set stream data for consumption to the client
  • streamBox.number.delay - Params - set delay before returning data to the client

StreamBox Parser( data: Buffer ): any

  • streamBox.toArray - Method - parse the buffer data to an array and pass its value to the client
  • streamBox.toObject - Method - parse the buffer data to an object and pass its value to the client
  • streamBox.toString - Method - parse the buffer data to an string and pass its value to the client
  • streamBox.toNumber - Method - parse the buffer data to an number and pass its value to the client

StreamBox Callback( parameter: Promise, callback: any ): void

  • streamBox.toCallback - Method - convert promise function to callback function

Example Usage

  • Example Usage Using CommonJS With Grpc

    const streamBox = require('streambox-collection')
    const { Empty } = require('../typedefs/users_pb')
    const { client } = require('./client')
    
    client.getUsers(Empty, (error, response) => {
    	if (error) console.error(error)
    	streamBox
    		.array(response.toObject())
    		.then((res) => console.log(streamBox.toArray(res)))
    		.catch(console.log)
    })
  • Example Usage Using ES6 With Grpc

    import * as streamBox from 'streambox-collection'
    import { Empty } from '../typedefs/users_pb'
    import { client } from './client'
    
    client.getUsers(Empty, (error, response) => {
    	if (error) console.error(error)
    	streamBox
    		.array(response.toObject())
    		.then((res) => console.log(streamBox.toArray(res)))
    		.catch(console.log)
    })
  • Example Usage Using ES6 With Grpc And Typescript

    import * as streamBox from 'streambox-collection'
    import { ServiceError } from 'grpc'
    import { UserList, Empty } from '../typedefs/users_pb'
    import { client } from './client'
    
    client.getUsers(Empty, (error: ServiceError, response: UserList) => {
    	if (error) console.error(error)
    	streamBox
    		.array(response.toObject())
    		.then((res: Buffer) => console.log(streamBox.toArray(res)))
    		.catch(console.log)
    })
  • Example Usage Using CommonJS With Express.js

    const express = require('express')
    const axios = require('axios')
    const streamBox = require('streambox-collection')
    
    const app = express()
    
    app.get('/fetch', async (req, res) => {
    	const { data } = await axios.get('https://jsonplaceholder.typicode.com/users')
    	streamBox.array(data).then((response) => res.json(streamBox.toArray(response)))
    })
  • Example Usage Using ES6 With Express.js

    import express from 'express'
    import axios from 'axios'
    import * as streamBox from 'streambox-collection'
    
    const app = express()
    
    app.get('/fetch', async (req, res) => {
    	const { data } = await axios.get('https://jsonplaceholder.typicode.com/users')
    	streamBox.array(data).then((response) => res.json(streamBox.toArray(response)))
    })
  • Example Usage Using ES6 With Express.js And Typescript

    import express, { Express, Request, Response } from 'express'
    import axios from 'axios'
    import * as streamBox from 'streambox-collection'
    
    const app = express() as Express
    
    app.get('/fetch', async (req: Request, res: Response) => {
    	const { data } = await axios.get('https://jsonplaceholder.typicode.com/users')
    	streamBox.array(data).then((response: Buffer) => res.json(streamBox.toArray(response)))
    })
  • Example Usage Custom Stream Using CommonJS With Express.js

    // util.stream.js
    const streamBox = require('streambox-collection')
    
    exports.streamBox = (handler, statusCode, data) => {
    	streamBox.object({ ...data }).then((res) => {
    		return handler.status(statusCode).json(streamBox.toObject(res))
    	})
    }
    
    // app.js
    const express = require('express')
    const axios = require('axios')
    const { streamBox } = require('../utils/util.stream')
    
    const app = express()
    
    app.get('/fetch', async (req, res) => {
    	const { data } = await axios.get('https://jsonplaceholder.typicode.com/users')
    		streamBox(res, 200, {
          method: req.method,
          statusCode: res.statusCode,
          users: data
         })
    })
  • Example Usage Custom Stream Using ES6 With Express.js

    // util.stream.js
    import * as streamBoxCollection from 'streambox-collection'
    
    export const streamBox = (handler, statusCode, data) => {
    	streamBoxCollection.object({ ...data }).then((res) => {
    		return handler.status(statusCode).json(streamBoxCollection.toObject(res))
    	})
    }
    
    // app.js
    import express from 'express'
    import axios from 'axios'
    import { streamBox } from '../utils/util.stream'
    
    const app = express()
    
    app.get('/fetch', async (req, res) => {
    	const { data } = await axios.get('https://jsonplaceholder.typicode.com/users')
    		streamBox(res, 200, {
          method: req.method,
          statusCode: res.statusCode,
          users: data
        })
    })
  • Example Usage Custom Stream Using ES6 With Express.js And Typescript

    // util.stream.ts
    import { Response } from 'express'
    import * as streamBoxCollection from 'streambox-collection'
    
    export const streamBox = (handler: Response, statusCode: number, data: Record<string, any>): void => {
    	streamBoxCollection.object({ ...data }).then((res: Buffer) => {
    		return handler.status(statusCode).json(streamBoxCollection.toObject(res))
    	})
    }
    
    // app.ts
    import express, { Request, Response } from 'express'
    import axios from 'axios'
    import { streamBox } from '../utils/util.stream'
    
    const app = express() as Express
    
    app.get('/fetch', async (req: Request, res: Response) => {
    	const { data } = await axios.get('https://jsonplaceholder.typicode.com/users')
    		streamBox(res, 200, {
          method: req.method,
          statusCode: res.statusCode,
          users: data
        })
    })
  • Example Usage With React.js

    import React, { useState, useEffect } from 'react'
    import * as streamBox from 'streambox-collection'
    import axios from 'axios'
    
    function App() {
      const [values, setValues] = useState({
        loading: true,
        users: []
      })
    
      useEffect(() => {
        if (values.loading) {
          fetchData()
        }
      }, [])
    
      function fetchData() {
        streamBox.toCallback(axios.get('https://jsonplaceholder.typicode.com/users'), (err, res) => {
          if (err) console.log(err)
          streamBox
            .array(res.data)
            .then((data) => setValues({ ...values, loading: false, users: streamBox.toArray(data) }))
            .catch(console.error)
        })
      }
    
      return (
        <>
          {values.loading && <h1>Loading...</h1>}
          <ul>{!values.loading && values.users.map((user) => <li key={user.id}>{user.name}</li>)}</ul>
        </>
      )
    }
    
    export default App
  • Example Usage Convert Promise To Callback Using CommonJS

    const streambox = require('streambox-collection')
    const axios = require('axios')
    const fetch = require('node-fetch')
    
    // using axios
    streambox.toCallback(axios.get('https://jsonplaceholder.typicode.com/users'), (err, res) => console.log(res.data))
      
    // using fetch
    streambox.toCallback(fetch('https://jsonplaceholder.typicode.com/users'), async (err, res) => {
         const data = await res.json()
         console.log(data)
     })
       
    // using promise constructor
     function resultData() {
           return new Promise((resolve, reject) => {
      	    resolve('hello wordl')
         })
     }
    
    streambox.toCallback(resultData(), (err, res) => console.log(res))
  • Example Usage Convert Promise To Callback Using ES6

    import * as streambox from 'streambox-collection'
    import axios from 'axios'
    import fetch from 'node-fetch'
    
    // using axios
    streambox.toCallback(axios.get('https://jsonplaceholder.typicode.com/users'), (err, res) => console.log(res.data))
    
    // using fetch
    streambox.toCallback(fetch('https://jsonplaceholder.typicode.com/users'), async (err, res) => {
       const data = await res.json()
       console.log(data)
     })
    
    // using promise constructor
     function resultData() {
         return new Promise((resolve, reject) => {
          resolve('hello wordl')
       })
     }
    
    streambox.toCallback(resultData(), (err, res) => console.log(res))

Testing

  • Testing Via Local

    npm run test or make test
  • Testing Via Local And Build

    make build
  • Testing Via Docker

    docker build -t streambox-collection or make dkb tag=streambox-collection

Bugs

For information on bugs related to package libraries, please visit here

Contributing

Want to make Streambox-Collection more perfect ? Let's contribute and follow the contribution guide.

License