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

5m

v0.5.0

Published

is a near real time utility, 5mb or 5min

Downloads

25

Readme

5m

is a near real time utility, 5mb or 5min

KLP

Installation | Use case | Annotated source | License

Installation

npm i 5m

Use case

I use this package with debug and aws-sdk to upload logs on S3.

Then there is a trigger that feeds every file created on S3 to an AWS Lambda which sends file content to a server that broadcast it using socket.io. Yes I know there are other cool tools like CloudWatch, Kinesis, etc. (but 5m is free as in speach and free as in beer too ;)

The cool part is that it is possible to trigger an AWS Lambda every time a new file is created, and for example load it on a database.

Here is an example code to achieve logging and upload on S3.

// File: log.js
//
// Prints to STDOUT if DEBUG environment variable is set properly.
// Uploads logs on some S3 bucket.
//
//     const log = require('./log')
//     log('Hello world')
//

const AWS = require('aws-sdk')
const debug = require('debug')
const fiveM = require('5m')

const s3 = new AWS.S3()

// Use package name as namespace for logging, just as an example.
const pkg = require('./package.json')
const namespace = pkg.name
const debug5m = debug(`${namespace}:5m`)
const debugPkg = debug(namespace)

const writeOnS3 = (data) => {
  const Bucket = 'my-bucket'

  const tstamp = new Date().getTime()
  const Key = `my/path/${tstamp}.log`

  s3.upload({ Bucket, Key }, error => {
    if (error) debug5m(error)
    else debug5m()
  })
}

function log (message) {
  const tstamp = new Date().getTime()
  const record = `${tstamp} ${namespace} ${message}`

  fiveM(namespace, writeOnS3)(record)
  debugPkg(message)
}

module.exports = log

Annotated source

Store data in a namespaced bucket, as well as its function to flush data.

var bucket = {}
var flush = {}

Considering 1 char should be 1 byte, and dates are expressed in milliseconds, the following constants express 5 MB and 5 minutes. Aproximation is ok, since we want to achieve a near real time. The timeout used for flushing data can be set, for testing or other purpouse via the FIVEM_TIMEOUT_MILLISECONDS environment variable.

const fiveMb = 1024 * 1024 * 5
const fiveMin = 300 * 1000
const flushTimeout = process.env.FIVEM_TIMEOUT_MILLISECONDS || fiveMin

Make sure no data is lost on exit.

process.on('exit', () => {
  for (var namespace in bucket) flush[namespace]()
})

Create the 5m function.

Since 5m is allowed as npm package name, but not as JavaScript identifier, using a roman number like Vm can be confusing, so maybe naming the function as fiveM is a good idea.

/**
 * **@param** `{String}` namespace
 * **@param** `{Function}` write
 *
 * **@returns** `{Function}` logger
 */
function fiveM (namespace, write) {

Create the namespaced flush function: write data and clean up.

  flush[namespace] = () => {
    if (bucket[namespace]) {
      write(bucket[namespace])

      delete bucket[namespace]
    }
  }

Create the logger function.

  /**
   * @param {*} data
   */
  return function logger (data) {

If necessary, initialize data bucket and set timeout to flush it later.

    if (typeof bucket[namespace] === 'undefined') {
      bucket[namespace] = ''

      setTimeout(flush[namespace], flushTimeout)
    }

Append data to named bucket.

    bucket[namespace] += data

Check if data is bigger than 5 MB.

    const exceededSpace = bucket[namespace] && (bucket[namespace].length > 0) && (bucket[namespace].length > fiveMb)

If yes, flush it!

    if (exceededSpace) flush[namespace]()
  }
}

Export 5m function.

module.exports = fiveM

License

MIT