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 🙏

© 2026 – Pkg Stats / Ryan Hefner

level-q

v0.2.1

Published

Priority queuing for leveldb/levelup

Readme

level-q

Priority queuing for leveldb/levelup

build status

Installation

This module is installed via npm:

$ npm install level-q

Example Usage

Basic Usage

By default adding items to a queue will be ordered first in, first out.

var queue = require('level-q'),
    level = require('level'),
    bytewise = require('bytewise');

// instantiate queue (adds a 'queue' property on the db)
var q = queue(level('/db/path', { keyEncoding: bytewise, valueEncoding: 'json' }));

// data to put in queue
var data = {
  id: 1,
  name: 'Eugene',
  value: 42
};

// add to queue
q.queue.push(data);

// read a single item from the queue, then stop listening
q.queue.read(function (err, value) {
  // value should equal data
});

// keep listening to the queue
q.queue.listen(function (err, value, key, next) {
  // do something with the value
  console.log(value);
  // ok, done processing, get the next item from the queue
  next();
});

Custom Priority Queue

By providing an order by function you can return a key that can be used to sort the queue. This example uses a quality of service (QOS) field so that the higher the QOS, the earlier it will get serviced.

var queue = require('level-q'),
    level = require('level'),
    bytewise = require('bytewise');

// order the queue by the QOS field
var db = queue(level('/db/path', { keyEncoding: bytewise, valueEncoding: 'json' }),
  { order: orderByQos });

function orderByQos(data) {
  return data.QOS;
}

// data to put in queue
var data = [
  // should be serviced last
  {
    id: 1,
    QOS: 'C',
    name: 'Eugene',
    value: 42
  },
  // should be serviced first
  {
    id: 2,
    QOS: 'A',
    name: 'Eugene',
    value: 42
  },
  // should be serviced second
  {
    id: 3,
    QOS: 'B',
    name: 'Eugene',
    value: 42
  }
];

// add to queue
var count = data.length;
data.forEach(function (item) {
  db.queue.push(item, function (err) {
    if (err) throw err;
    --count || listen();
  });
});

function listen() {
  db.queue.listen(function (err, value, key, next) {
    console.log(value);
    // should print the second item, then the third item, then the first
    next();
  });
}

Control when an item is able to be released from the queue

You can use the release option when instantiating your queue to control when an item is able to be returned to consumers of the queue.

For example, you may wish to create a job queue, and delay jobs until to a scheduled time in the future (eg. like a cron job);

var bytewise = require('bytewise'),
    level = require('level')
    range = require('range'),
    queue = require('level-q');

// create a priority queue based on time to next service
var db = queue(
  level('/db/path', { keyEncoding: bytewise, valueEncoding: 'json' }),
  { order: orderByDeadline, release: releaseOnDeadline });

// sort queue by deadline
function orderByDeadline(data) {
  return data.deadline;
}

// only allow an item to be read from queue if it's time is up
function releaseOnDeadline(data) {
  return Date.now() >= data.deadline;
}

// add some work to the queue that will be scheduled 5 seconds in the future
db.queue.push({
  deadline: Date.now() + 5000,
  state: 'square'
  n: 2,
  squareResult: 0,
  sinResult: 0
});

// The job will ONLY be available for processing once the 5 seconds has passed
db.queue.listen(function (err, value, key, next) {
  if (err) throw err;
  switch (value.state) {
    case 'square':
      // do the work
      value.squareResult = value.n * value.n;

      // schedule then next part of work 2 seconds in the future
      value.state = 'sin';
      value.deadline = Date.now() + 2000;
      db.queue.push(value, next);
      break;

    case 'sin':
      // do the work
      value.sinResult = Math.sin(value.n);

      console.log(value);
      // Should print out:
      // { deadline: 1390487269236,
      //    state: 'sin',
      //    n: 2,
      //    squareResult: 4,
      //    sinResult: 0.9092974268256817 }
      next();
      break;
  }
});