Comparing version 0.6.6 to 0.7.0
@@ -33,2 +33,6 @@ /* Config file used during tests | ||
cfg.db_url = function (postfix) { | ||
return cfg.url + '/' + postfix; | ||
}; | ||
module.exports = exports = cfg; |
123
error.js
@@ -1,53 +0,54 @@ | ||
var STATUS_CODES = { '100': 'Continue', | ||
'101': 'Switching Protocols', | ||
'102': 'Processing', | ||
'200': 'OK', | ||
'201': 'Created', | ||
'202': 'Accepted', | ||
'203': 'Non-Authoritative Information', | ||
'204': 'No Content', | ||
'205': 'Reset Content', | ||
'206': 'Partial Content', | ||
'207': 'Multi-Status', | ||
'300': 'Multiple Choices', | ||
'301': 'Moved Permanently', | ||
'302': 'Moved Temporarily', | ||
'303': 'See Other', | ||
'304': 'Not Modified', | ||
'305': 'Use Proxy', | ||
'307': 'Temporary Redirect', | ||
'400': 'Bad Request', | ||
'401': 'Unauthorized', | ||
'402': 'Payment Required', | ||
'403': 'Forbidden', | ||
'404': 'Not Found', | ||
'405': 'Method Not Allowed', | ||
'406': 'Not Acceptable', | ||
'407': 'Proxy Authentication Required', | ||
'408': 'Request Time-out', | ||
'409': 'Conflict', | ||
'410': 'Gone', | ||
'411': 'Length Required', | ||
'412': 'Precondition Failed', | ||
'413': 'Request Entity Too Large', | ||
'414': 'Request-URI Too Large', | ||
'415': 'Unsupported Media Type', | ||
'416': 'Requested Range Not Satisfiable', | ||
'417': 'Expectation Failed', | ||
'418': 'I\'m a teapot', | ||
'422': 'Unprocessable Entity', | ||
'423': 'Locked', | ||
'424': 'Failed Dependency', | ||
'425': 'Unordered Collection', | ||
'426': 'Upgrade Required', | ||
'500': 'Internal Server Error', | ||
'501': 'Not Implemented', | ||
'502': 'Bad Gateway', | ||
'503': 'Service Unavailable', | ||
'504': 'Gateway Time-out', | ||
'505': 'HTTP Version not supported', | ||
'506': 'Variant Also Negotiates', | ||
'507': 'Insufficient Storage', | ||
'509': 'Bandwidth Limit Exceeded', | ||
'510': 'Not Extended' }; | ||
var STATUS_CODES = { '100': 'Continue' | ||
,'101': 'Switching Protocols' | ||
,'102': 'Processing' | ||
,'200': 'OK' | ||
,'201': 'Created' | ||
,'202': 'Accepted' | ||
,'203': 'Non-Authoritative Information' | ||
,'204': 'No Content' | ||
,'205': 'Reset Content' | ||
,'206': 'Partial Content' | ||
,'207': 'Multi-Status' | ||
,'300': 'Multiple Choices' | ||
,'301': 'Moved Permanently' | ||
,'302': 'Moved Temporarily' | ||
,'303': 'See Other' | ||
,'304': 'Not Modified' | ||
,'305': 'Use Proxy' | ||
,'307': 'Temporary Redirect' | ||
,'400': 'Bad Request' | ||
,'401': 'Unauthorized' | ||
,'402': 'Payment Required' | ||
,'403': 'Forbidden' | ||
,'404': 'Not Found' | ||
,'405': 'Method Not Allowed' | ||
,'406': 'Not Acceptable' | ||
,'407': 'Proxy Authentication Required' | ||
,'408': 'Request Time-out' | ||
,'409': 'Conflict' | ||
,'410': 'Gone' | ||
,'411': 'Length Required' | ||
,'412': 'Precondition Failed' | ||
,'413': 'Request Entity Too Large' | ||
,'414': 'Request-URI Too Large' | ||
,'415': 'Unsupported Media Type' | ||
,'416': 'Requested Range Not Satisfiable' | ||
,'417': 'Expectation Failed' | ||
,'418': 'I\'m a teapot' | ||
,'422': 'Unprocessable Entity' | ||
,'423': 'Locked' | ||
,'424': 'Failed Dependency' | ||
,'425': 'Unordered Collection' | ||
,'426': 'Upgrade Required' | ||
,'500': 'Internal Server Error' | ||
,'501': 'Not Implemented' | ||
,'502': 'Bad Gateway' | ||
,'503': 'Service Unavailable' | ||
,'504': 'Gateway Time-out' | ||
,'505': 'HTTP Version not supported' | ||
,'506': 'Variant Also Negotiates' | ||
,'507': 'Insufficient Storage' | ||
,'509': 'Bandwidth Limit Exceeded' | ||
,'510': 'Not Extended' | ||
}; | ||
/* | ||
@@ -87,11 +88,11 @@ * generic error | ||
*/ | ||
function gen_err(error,code,request,http_code,type) { | ||
if(!type) { type = http_code; http_code = null; } | ||
if(!error) { error = new Error(STATUS_CODES[http_code] || "Unknown Error"); } | ||
if(typeof http_code !== "number") { http_code = 500; } | ||
function gen_err(scope,error,code,request,status_code) { | ||
error = error || STATUS_CODES[status_code] || 'Unknown Error'; | ||
code = code || 'unknown'; | ||
status_code = typeof status_code === 'number' && status_code || 500; | ||
request = request || {}; | ||
if(typeof error === 'string') { error = new Error(error); } | ||
error.error = code; | ||
error.status_code = http_code; | ||
error.namespace = type; | ||
error.status_code = status_code; | ||
error.scope = scope; | ||
error.request = request; | ||
@@ -101,3 +102,3 @@ return error; | ||
exports.request_err = function (e,c,r,h) { return gen_err(e,c,r,h,"request");}; | ||
exports.couch_err = function (e,c,r,h) { return gen_err(e,c,r,h,"couch"); }; | ||
exports.request = function (e,c,r,h) { return gen_err('request',e,c,r,h);}; | ||
exports.couch = function (e,c,r,h) { return gen_err('couch',e,c,r,h); }; |
41
nano.js
@@ -21,2 +21,3 @@ /* minimal couch in node | ||
, _ = require('underscore') | ||
, u = require('url') | ||
, error = require('./error') | ||
@@ -36,3 +37,3 @@ , default_url = "http://localhost:5984" | ||
module.exports = exports = nano = function database_module(cfg) { | ||
var public_functions = {}; | ||
var public_functions = {}, path, db; | ||
if(typeof cfg === "string") { | ||
@@ -45,2 +46,6 @@ if(/^https?:/.test(cfg)) { cfg = {url: cfg}; } // url | ||
} | ||
if(!cfg) { | ||
console.error("bad cfg: you passed undefined"); | ||
cfg = {}; | ||
} | ||
if(cfg.proxy) { | ||
@@ -53,5 +58,6 @@ request = request.defaults({proxy: cfg.proxy}); // proxy support | ||
} | ||
path = u.parse(cfg.url); | ||
/**************************************************************************** | ||
* relax * | ||
* relax * | ||
****************************************************************************/ | ||
@@ -78,2 +84,3 @@ /* | ||
* {opts.method:string:optional} http method, defaults to "GET" | ||
* {opts.path:string:optional} a full path, override `doc` and `att` | ||
* {opts.doc:string:optional} document name | ||
@@ -96,6 +103,9 @@ * {opts.att:string:optional} attachment name | ||
, rh; | ||
if(opts.doc) { | ||
url += "/" + opts.doc; // add the document to the url | ||
if(opts.path) { | ||
url += "/" + opts.path; | ||
} | ||
else if(opts.doc) { | ||
url += "/" + encodeURIComponent(opts.doc); // add the document to the url | ||
if(opts.att) { url += "/" + opts.att; } // add the attachment to the url | ||
} | ||
} | ||
if(opts.encoding && callback) { | ||
@@ -121,3 +131,3 @@ req.encoding = opts.encoding; | ||
rh['status-code'] = status_code = (h && h.statusCode || 500); | ||
if(e) { return callback(error.request_err(e,"socket",req,status_code),rh,b); } | ||
if(e) { return callback(error.request(e,"socket",req,status_code),rh,b); } | ||
delete rh.server; // prevent security vunerabilities related to couchdb | ||
@@ -130,3 +140,3 @@ delete rh["content-length"]; // prevent problems with trims and stalled responses | ||
else { // proxy the error directly from couchdb | ||
callback(error.couch_err(parsed.reason,parsed.error,req,status_code),rh,parsed); | ||
callback(error.couch(parsed.reason,parsed.error,req,status_code),rh,parsed); | ||
} | ||
@@ -222,3 +232,3 @@ }); | ||
} | ||
return relax({db: db_name, doc: "_compact", att: design_name, method: "POST"},callback); | ||
return relax({db: db_name, path: ("_compact" + design_name), method: "POST"},callback); | ||
} | ||
@@ -243,3 +253,3 @@ | ||
} | ||
return relax({db: db_name, doc: "_changes", params: params, method: "GET"},callback); | ||
return relax({db: db_name, path: "_changes", params: params, method: "GET"},callback); | ||
} | ||
@@ -344,3 +354,3 @@ | ||
} | ||
return relax({db: db_name, doc: "_all_docs", method: "GET", params: params},callback); | ||
return relax({db: db_name, path: "_all_docs", method: "GET", params: params},callback); | ||
} | ||
@@ -358,3 +368,3 @@ | ||
function bulk_docs(docs,callback) { | ||
return relax({db: db_name, doc: "_bulk_docs", body: docs, method: "POST"},callback); | ||
return relax({db: db_name, path: "_bulk_docs", body: docs, method: "POST"},callback); | ||
} | ||
@@ -473,3 +483,10 @@ | ||
}; | ||
return public_functions; | ||
// does the user want a database, or nano? | ||
if(!_.isEmpty(path.pathname.split('/')[1])) { | ||
db = path.pathname.split('/')[1]; | ||
cfg.url = path.protocol + '//' + path.host; // reset url | ||
return document_module(db); | ||
} | ||
else { return public_functions; } | ||
}; | ||
@@ -476,0 +493,0 @@ |
{ "name": "nano" | ||
, "description": "minimalistic couchdb driver for node.js" | ||
, "homepage": "http://github.com/dscape/nano" | ||
, "version": "0.6.6" | ||
, "version": "0.7.0" | ||
, "author": "Nuno Job <nunojobpinto@gmail.com> (http://nunojob.com)" | ||
, "keywords": ["couchdb", "data", "request", "json", "nosql", "micro", "nano"] | ||
, "dependencies": {"request": "~2.0.5", "underscore": "~1.1.7"} | ||
, "dependencies": {"request": "~2.1.1", "underscore": "~1.1.7"} | ||
, "devDependencies": { "async": "~0.1.9", "vows": "~0.5.10" } | ||
@@ -9,0 +9,0 @@ , "main": "./nano.js" |
@@ -126,3 +126,3 @@ # nano | ||
this is the same as (assuming `alice = nano.use("alice");`): | ||
this is the same as (assuming `alice = require('nano')('http://localhost:5984/alice')`): | ||
@@ -129,0 +129,0 @@ alice.get("rabbit", {rev: "1-967a00dff5e02add41819138abb3284d"}, |
var vows = require('vows') | ||
, assert = require('assert') | ||
, cfg = require('../../cfg/tests.js') | ||
, nano = require('../../nano')(cfg) | ||
, db_name = "doc_in1" | ||
, db = nano.use(db_name); | ||
, nano = require('../../nano')(cfg); | ||
function db_name(i) { return "doc_in" + i; } | ||
function db(i) { return nano.use(db_name(i)); } | ||
/***************************************************************************** | ||
@@ -12,4 +13,4 @@ * insert_doc * | ||
function insert_doc(callback) { | ||
nano.db.create(db_name, function () { | ||
db.insert({foo: "bar"}, callback); | ||
nano.db.create(db_name("a"), function () { | ||
db("a").insert({foo: "bar"}, callback); | ||
}); | ||
@@ -19,3 +20,3 @@ } | ||
function insert_doc_ok(e,h,b) { | ||
nano.db.destroy(db_name); | ||
nano.db.destroy(db_name("a")); | ||
assert.isNull(e); | ||
@@ -27,7 +28,26 @@ assert.ok(b.ok); | ||
/***************************************************************************** | ||
* insert_doc_path * | ||
*****************************************************************************/ | ||
function insert_doc_path(callback) { | ||
nano.db.create(db_name("b"), function () { | ||
db("b").insert({foo: "bar"}, 'some/path', callback); | ||
}); | ||
} | ||
function insert_doc_path_ok(e,h,b) { | ||
nano.db.destroy(db_name("b")); | ||
assert.isNull(e); | ||
assert.ok(b.ok); | ||
assert.ok(b.rev); | ||
assert.equal(b.id, "some/path"); | ||
} | ||
vows.describe('db.insert').addBatch({ | ||
"insert_doc": { | ||
topic: function () { insert_doc(this.callback); } | ||
, "=": insert_doc_ok | ||
} | ||
, "=": insert_doc_ok }, | ||
"insert_doc_path": { | ||
topic: function () { insert_doc_path(this.callback); } | ||
, "=": insert_doc_path_ok } | ||
}).exportTo(module); |
@@ -13,2 +13,56 @@ var vows = require('vows') | ||
/***************************************************************************** | ||
* url2 * | ||
*****************************************************************************/ | ||
function url2(callback) { callback(null,nano('http://someurl.com/')); } | ||
function url2_ok(_,n) { assert.equal(n.config.url, "http://someurl.com/"); } | ||
/***************************************************************************** | ||
* url_db * | ||
*****************************************************************************/ | ||
function url_db(callback) { | ||
nano(cfg.db_url("adb")).info(callback); | ||
} | ||
function url_db_ok(e,h,b) { | ||
assert.equal(e.message,"no_db_file"); | ||
assert.equal(e.error, "not_found"); | ||
assert.equal(e.request.uri, (cfg.url + '/adb')); | ||
} | ||
/***************************************************************************** | ||
* url_db2 * | ||
*****************************************************************************/ | ||
function url_db2(callback) { | ||
nano(cfg.db_url("adb/")).info(callback); | ||
} | ||
function url_db2_ok(e,h,b) { | ||
assert.equal(e.message,"no_db_file"); | ||
assert.equal(e.error, "not_found"); | ||
assert.equal(e.request.uri, (cfg.url + '/adb')); | ||
} | ||
/***************************************************************************** | ||
* url_db3 * | ||
*****************************************************************************/ | ||
function url_db3(callback) { | ||
nano(cfg.db_url("adb/blergh")).info(callback); | ||
} | ||
function url_db3_ok(e,h,b) { | ||
assert.equal(e.message,"no_db_file"); | ||
assert.equal(e.error, "not_found"); | ||
assert.equal(e.request.uri, (cfg.url + '/adb')); | ||
} | ||
/***************************************************************************** | ||
* url_db4 * | ||
*****************************************************************************/ | ||
function url_db4(callback) { | ||
nano(cfg.db_url("a/b/d/c/")).info(callback); | ||
} | ||
function url_db4_ok(e,h,b) { | ||
assert.equal(e.message,"no_db_file"); | ||
assert.equal(e.error, "not_found"); | ||
assert.equal(e.request.uri, (cfg.url + '/a')); | ||
} | ||
/***************************************************************************** | ||
* file * | ||
@@ -42,2 +96,12 @@ *****************************************************************************/ | ||
} | ||
/***************************************************************************** | ||
* nano_undefined * | ||
*****************************************************************************/ | ||
function nano_undefined(callback) { | ||
callback(null,nano()); | ||
} | ||
function nano_undefined_ok(_,e) { | ||
assert.equal(e.config.url,"http://localhost:5984"); | ||
} | ||
@@ -48,2 +112,17 @@ vows.describe('config').addBatch({ | ||
, "=": url_ok }, | ||
"url2": { | ||
topic: function () { url2(this.callback); } | ||
, "=": url2_ok }, | ||
"url_db": { | ||
topic: function () { url_db(this.callback); } | ||
, "=": url_db_ok }, | ||
"url_db2": { | ||
topic: function () { url_db2(this.callback); } | ||
, "=": url_db2_ok }, | ||
"url_db3": { | ||
topic: function () { url_db3(this.callback); } | ||
, "=": url_db3_ok }, | ||
"url_db4": { | ||
topic: function () { url_db4(this.callback); } | ||
, "=": url_db4_ok }, | ||
"file": { | ||
@@ -58,2 +137,5 @@ topic: function () { file(this.callback); } | ||
, "=": object_ok }, | ||
"nano_undefined": { | ||
topic: function () { nano_undefined(this.callback); } | ||
, "=": nano_undefined_ok }, | ||
"not_string_or_object": { | ||
@@ -60,0 +142,0 @@ topic: function () { not_string_or_object(this.callback); } |
@@ -9,3 +9,3 @@ var vows = require('vows') | ||
function empty_error(callback) { | ||
callback(null,err.couch_err(null,null,null,null)); | ||
callback(null,err.couch(null,null,null,null)); | ||
} | ||
@@ -16,4 +16,4 @@ | ||
assert.equal(e.status_code, 500); | ||
assert.isNull(e.error); | ||
assert.isNull(e.request); | ||
assert.equal(e.error, "unknown"); | ||
assert.ok(typeof e.request === 'object'); | ||
} | ||
@@ -25,3 +25,3 @@ | ||
function error_412(callback) { | ||
callback(null,err.couch_err(null,null,null,412)); | ||
callback(null,err.couch(null,null,null,412)); | ||
} | ||
@@ -32,4 +32,4 @@ | ||
assert.equal(e.status_code, 412); | ||
assert.isNull(e.error); | ||
assert.isNull(e.request); | ||
assert.equal(e.error, "unknown"); | ||
assert.ok(typeof e.request === 'object'); | ||
} | ||
@@ -36,0 +36,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61833
34
1480
+ Addedrequest@2.1.1(transitive)
- Removedrequest@2.0.5(transitive)
Updatedrequest@~2.1.1