Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

couchdb-view-cache-stream

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

couchdb-view-cache-stream - npm Package Compare versions

Comparing version 2.0.1 to 3.0.0

122

lib/index.js

@@ -1,100 +0,42 @@

var url = require('url');
var url = require('url')
var request = require('request')
var jsonfilter = require('jsonfilter')
var ndjson = require('ndjson')
var through = require('through2')
var from = require('from2')
var request = require('request');
var jsonfilter = require('jsonfilter');
var ndjson = require('ndjson');
var through = require('through2');
var levelup = require('levelup');
var devnull = require('dev-null');
module.exports = function (view_url, opts) {
if (!opts) opts = {}
var last_etag
var current_view
module.exports = function(view_url, opts){
return function (ready) {
var _all_docs = toInfoUrl(view_url)
request.head(_all_docs, function (err, resp, body) {
if (err) return ready(err)
if (last_etag === resp.headers.etag) return ready(null, from.obj(current_view))
if (!opts) opts = {};
if (!opts.preload) opts.preload = false;
var tempdb = []
var dapipe = request(view_url).pipe(jsonfilter('rows.*'))
.pipe(ndjson.parse())
.pipe(through.obj(function (data, enc, cb) {
tempdb.push(data.value)
cb(null, data.value)
}, function (cb) {
last_etag = resp.headers.etag
current_view = tempdb
cb()
}))
var info_url = toInfoUrl(view_url);
var update_seq;
var current_view;
var loading = false;
var load = function(done){
if (!done) done = function(){}; // a dumb callback
if (loading) return done();
loading = true;
fetch_update_seq(info_url, function(err, seq){
var clean_up = false;
if (err) { loading = false; return done(err) }
if (update_seq && update_seq === seq) { loading = false; return done() }
if (update_seq) clean_up = true;
console.log('updating view', view_url,' from', update_seq, 'to', seq)
// TODO we should move this to a sublevel, but it was not working for memdown
var db_path = opts.db_path || '/' + 'vcs/' + Date.now() + '/' + update_seq;
var tempdb = levelup(db_path, {
db: opts.db_type || require('memdown'),
keyEncoding: 'json',
valueEncoding: 'json'
})
request(view_url).pipe(jsonfilter('rows.*'))
.pipe(ndjson.parse())
.pipe(through.obj(function(data, enc, cb){
tempdb.put(data.key, data.value, cb)
}, function(cb){
process.nextTick(function(){
update_seq = seq;
current_view = tempdb;
done(null, seq);
});
cb()
}))
.pipe(devnull())
ready(null, dapipe)
})
}
load(function(err, seq){
if(seq) console.log('view loaded. currrently at update_seq', seq)
})
return function(){
process.nextTick(load);
if (!current_view) {
var stream = request(view_url)
.pipe(jsonfilter('rows.*.value'))
.pipe(ndjson.parse())
return stream;
}
// TODO - danger - on first load current_view will be undefined until the load
// is complete. Need to think how we want to handle this
return current_view.createValueStream()
}
};
function fetch_update_seq(info_url, cb) {
request(info_url, function(err, resp, body){
if (err) return cb(err);
try { return cb(null, JSON.parse(body).view_index.update_seq) }
catch(e) { console.log('got an error'); cb(e) }
})
}
function toInfoUrl(view_url) {
var view_url_parsed = url.parse(view_url);
var temp = view_url_parsed.pathname.split('/');
temp.splice(-2, 2)
temp.push('_info');
temp = temp.join('/');
view_url_parsed.pathname = temp;
function toInfoUrl (view_url) {
var view_url_parsed = url.parse(view_url)
var temp = view_url_parsed.pathname.split('/')
temp = temp[1] + '/_all_docs'
view_url_parsed.pathname = temp
return url.format(view_url_parsed)
}
{
"name": "couchdb-view-cache-stream",
"version": "2.0.1",
"version": "3.0.0",
"description": "An experimental way to cache the results of a couchdb view",
"main": "lib/index.js",
"dependencies": {
"dev-null": "^0.1.1",
"from2": "^2.1.0",
"jsonfilter": "^1.1.2",
"levelup": "^1.2.1",
"memdown": "^1.0.0",
"ndjson": "^1.4.1",

@@ -16,3 +14,7 @@ "request": "^2.58.0",

"devDependencies": {
"async": "^1.5.0",
"concat-stream": "^1.5.1",
"couchr": "0.0.16",
"jshint": "^2.5.5",
"standard": "^5.4.1",
"tap": "^0.4.12",

@@ -22,3 +24,3 @@ "tape": "^2.14.0"

"scripts": {
"test": "./node_modules/.bin/jshint lib/*.js && ./node_modules/.bin/tap test/*.js"
"test": "standard"
},

@@ -25,0 +27,0 @@ "repository": {

@@ -1,22 +0,48 @@

var mod = require('../lib/index');
var ndjson = require('ndjson');
var view_cache_stream = require('../lib/index')
var test = require('tape')
var async = require('async')
var couchr = require('couchr')
var concat = require('concat-stream')
var db = 'http://localhost:5984/idx-edm-v5'
var view = '/_design/idx/_view/by_id'
var docid = 'E3331885'
var test = require('tape');
test('fill in this', function (t) {
var s = mod('http://localhost:5984/idx-edm-v5/_design/idx/_view/by_id');
var stream = s();
stream.on('end', function(){
console.log('round 2')
var stream2 = s();
stream.pipe(ndjson.serialize()).pipe(process.stdout)
var cache = view_cache_stream(db + view)
async.timesSeries(10, function (n, next) {
run(cache, n + '', next)
}, function (err) {
t.error(err)
alter(db, docid, function (err) {
t.error(err)
setTimeout(function () {
async.timesSeries(10, function (n, next) {
run(cache, n + '', next)
}, t.end)
}, 5000)
})
})
})
function run (cache, name, cb) {
console.time(name)
cache(function (err, stream) {
if (err) return cb(err)
stream.on('end', function () {
console.timeEnd(name)
cb()
})
.pipe(concat(function (data) {
console.log('data')
}))
})
}
stream.pipe(ndjson.serialize()).pipe(process.stdout)
t.ok(true)
t.end();
});
function alter (db, docid, cb) {
couchr.get(db + '/' + docid, function (err, doc) {
if (err) return cb(err)
doc['Legal Plan'] += 'A'
couchr.put(db + '/' + docid, doc, cb)
})
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc