Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 483733472f91176e46e3747c9ac1656dc1ce6f25 @DamonOehlman committed Nov 30, 2011
Showing with 212 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +15 −0 README.md
  3. +9 −0 examples/save-attachments.js
  4. +2 −0 index.js
  5. +78 −0 lib/reader.js
  6. +79 −0 lib/requestentry.js
  7. 0 lib/writer.js
  8. +27 −0 package.json
@@ -0,0 +1,2 @@
+node_modules
+examples/output
@@ -0,0 +1,15 @@
+# attachmate - Streamable CouchDB attachments :)
+
+Well, that's the idea anyway, and well it's working with reading attachments :)
+
+```js
+var attachmate = require('attachmate'),
+ fstream = require('fstream'),
+ path = require("path"),
+
+ r = new attachmate.Reader({ path: 'http://localhost:5984/testdb/doc_with_attachments' }),
+ w = fstream.Writer({ path: path.resolve(__dirname, 'output'), type: 'Directory'});
+
+// pipe the attachments to the directory
+r.pipe(w);
+```
@@ -0,0 +1,9 @@
+var attachmate = require('../'),
+ fstream = require('fstream'),
+ path = require("path"),
+
+ r = new attachmate.Reader({ path: 'http://10.211.55.4:5984/steelmesh/app::tripplanner' }),
+ w = fstream.Writer({ path: path.resolve(__dirname, 'output'), type: 'Directory'});
+
+// pipe the attachments to the directory
+r.pipe(w);
@@ -0,0 +1,2 @@
+exports.Reader = require('./lib/reader');
+exports.Writer = require('./lib/writer');
@@ -0,0 +1,78 @@
+var request = require('request'),
+ util = require('util'),
+ Reader = require('fstream').Reader,
+ HttpRequestEntry = require('./requestentry'),
+ reTrailingSlash = /\/$/;
+
+function AttachmentReader(props) {
+ // TODO: check the path property is a url
+ this.path = props.path;
+
+
+ this._entries = null;
+ this._index = -1;
+ this._ended = false;
+
+ this._read();
+}
+
+util.inherits(AttachmentReader, Reader);
+
+AttachmentReader.prototype._getEntries = function() {
+ var reader = this;
+
+ request(this.path, function(err, resp, body) {
+ try {
+ var parsed = JSON.parse(body),
+ attachments = parsed._attachments,
+ entries = [];
+
+ // iterate through the attachments
+ if (attachments) {
+ for (var key in attachments) {
+ entries[entries.length] = key;
+ }
+ }
+
+ reader._entries = entries;
+ }
+ catch (e) {
+ reader._entries = [];
+ }
+
+ reader._read();
+ });
+};
+
+AttachmentReader.prototype._read = function() {
+ var reader = this, entry, entryPath;
+
+ if (! this._entries) {
+ return this._getEntries();
+ }
+
+ this._index++;
+ if (this._index >= this._entries.length) {
+ if (! this._ended) {
+ this._ended = true;
+ this.emit('end');
+ this.emit('close');
+ }
+
+ return;
+ }
+
+ console.log(this.path.replace(reTrailingSlash, ''), this._entries[this._index]);
+ entry = new HttpRequestEntry(this.path.replace(reTrailingSlash, ''), this._entries[this._index]);
+ this.emit('entry', entry);
+
+ entry.on('end', function() {
+ reader._read();
+ });
+
+ entry.on('error', function(err) {
+ console.log(err);
+ });
+};
+
+module.exports = AttachmentReader;
@@ -0,0 +1,79 @@
+var Stream = require("stream").Stream,
+ util = require('util'),
+ http = require('http'),
+ url = require('url'),
+ request = require('request');
+
+function HttpRequestEntry(targetDoc, path) {
+ var entry = this;
+
+ this.path = path;
+ this.props = {};
+ this._paused = false;
+ this._pipeTarget = null;
+ this._buffer = [];
+
+ this._request = http.get(url.parse(targetDoc + '/' + path), function(couchRes) {
+ // handle data
+ couchRes.on('data', function(chunk) {
+ if (entry._paused) {
+ entry._buffer.push(chunk);
+ }
+ else {
+ entry.emit('data', chunk);
+ }
+ });
+
+ // handle the response end
+ couchRes.on('end', function() {
+ entry._flush();
+ entry.emit('end');
+ });
+ });
+
+ this._request.on('error', function(e) {
+ entry.emit('close');
+ });
+
+ this._request.setSocketKeepAlive(false);
+
+ /*
+ this._request.setTimeout(10000, function() {
+ if (! responseStarted) {
+ // flag as timed out
+ timedOut = true;
+
+ docRequest.abort();
+ mesh.log.warn('request for \'' + targetDoc + '\' timed out');
+ res.send('Timed out: ' + new Date().getTime(), 500);
+ }
+ });
+ */
+} // HttpRequestEntry
+
+util.inherits(HttpRequestEntry, Stream);
+
+HttpRequestEntry.prototype._flush = function() {
+ for (var ii = 0, count = this._buffer.length; ii < count; ii++) {
+ this.emit('data', this._buffer[ii]);
+ }
+
+ // reset the buffer
+ this._buffer = [];
+};
+
+HttpRequestEntry.prototype.pause = function(who) {
+ this._paused = true;
+ this.emit('pause', who || this);
+};
+
+HttpRequestEntry.prototype.resume = function(who) {
+ if (this._paused) {
+ this._flush();
+ }
+
+ this._paused = false;
+ this.emit('resume', who || this);
+};
+
+module.exports = HttpRequestEntry;
No changes.
@@ -0,0 +1,27 @@
+{
+ "name": "attachmate",
+ "description": "CouchDB Attachment Helpers (part of the Steelmesh stack)",
+ "tags": [
+ "couchdb"
+ ],
+ "author": "Damon Oehlman <damon.oehlman@sidelab.com>",
+ "version": "0.0.1",
+ "engines": {
+ "node": ">= 0.4.x < 0.7.0"
+ },
+ "dependencies": {
+ "request": "2.2.x",
+ "fstream": ">= 0.1.3"
+ },
+ "devDependencies": {
+ "vows": "0.5.x"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/steelmesh/attachmate.git"
+ },
+ "bugs": {
+ "url": "http://github.com/steelmesh/attachmate/issues"
+ },
+ "contributors": []
+}

0 comments on commit 4837334

Please sign in to comment.