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

couch-streams

v0.0.0

Published

CouchDB stream utils.

Downloads

3

Readme

couch-streams

A stream interface to CouchDB which provides application level error handling and json parsing.

Error Handling

Instead of doing this:

var request = require('request')

try {
  var req = request('http://localhost:5984/wtf') // where the wtf database doesnt exist
} catch (er) {
  return handleRequestError(er)
}

req.on('error', function (er) {
  handleSocketError(er)
})

req.on('response', function(res) {
  if (res.statusCode >= 400)
    return handleCouchdbError(er)

  andleResponse(res)
})

You could do this:

var request = require('couch-stream')

var req = request('http://localhost:5984/wtf')
.on('error', function (er) {
  handleError(er) // with er.scope being either "request", "socket", or "couchdb"
})
.pipe(handleData)

JSON parsing

In addition to error handling, you get real-time JSON parsing for free (note that you can opt out of JSON parsing). For example, you have a database named "db" which contains a good amount of docs (good amount meaning thousands). You query _changes on the database. Now, instead of waiting to recieve the entire JSON response from Couch in order to begin parsing, couch-streams will use dominictarrs JSONStream module to parse the data as it comes in, in real time. couch-streams will examine the url to figure out how best to parse it too.

curling the "db" database will give you:

∴ couch-streams master!∶ curl http://localhost:5984/db/_changes
{"results":[
{"seq":1,"id":"1","changes":[{"rev":"1-967a00dff5e02add41819138abb3284d"}]},
{"seq":2,"id":"2","changes":[{"rev":"1-967a00dff5e02add41819138abb3284d"}]},
{"seq":3,"id":"3","changes":[{"rev":"1-967a00dff5e02add41819138abb3284d"}]}
],
"last_seq":3}

Using couch-streams with JSON parsing will give you:

var request = require('couch-stream')
var through = require('through')

var req = request('http://localhost:5984/db')
.pipe(through(function (doc) {
  /*
  doc will be JSON:
  {"seq":1,"id":"1","changes":[{"rev":"1-967a00dff5e02add41819138abb3284d"}]}
  {"seq":2,"id":"2","changes":[{"rev":"1-967a00dff5e02add41819138abb3284d"}]}
  {"seq":3,"id":"3","changes":[{"rev":"1-967a00dff5e02add41819138abb3284d"}]}
  */
}))

methods

var request = require('couch-streams')

var stream = request(url, opts)

create a stream from a fully qualified url and opts

  • opts.json default: true stream data as json objects
  • opts.parser default: undefined a json path which gets passed into JSONStream.parse(), JSONStream docs

events

stream.on('error', function (er) {})

  • er.scope the scope of where the error ocuured.
    1. request : request through an exception when attempting to create connection
    2. socket: an error was emitted when attempting to create a connection. Something like the wrong port number.
    3. couch: CouchDB level error such as "database not found" or "unauthorized"

todo

This module current implements only GET's. I eventually want to do PUT's, POST's, and DELETE's.

posts

bulk

using a stream.post api:


var request = require('couch-streams')
var dataSource = [
  {"seq":1,"id":"1","changes":[{"rev":"1-967a00dff5e02add41819138abb3284d"}]},
  {"seq":2,"id":"2","changes":[{"rev":"1-967a00dff5e02add41819138abb3284d"}]},
  {"seq":3,"id":"3","changes":[{"rev":"1-967a00dff5e02add41819138abb3284d"}]}
]

var req = request('http://localhost:5984/db/_bulk_docs')
req.post(dataSouce)
.on('error', handleError)
.pipe(handleResponss)

or piping into the stream:

var request = require('couch-streams')
var through = require('through')
var dataSource = [
  {"seq":1,"id":"1","changes":[{"rev":"1-967a00dff5e02add41819138abb3284d"}]},
  {"seq":2,"id":"2","changes":[{"rev":"1-967a00dff5e02add41819138abb3284d"}]},
  {"seq":3,"id":"3","changes":[{"rev":"1-967a00dff5e02add41819138abb3284d"}]}
]

through(function () {
  emit('data', dataSource)
})
.pipe(request('http://localhost:5984/db/_bulk_docs'))
.on('error', handleError)
.pipe(handleResponse)