-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdojo.couchdb.js
150 lines (147 loc) · 4.92 KB
/
dojo.couchdb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
function CouchDB( options ) {
options = options || { };
var self = {};
var toJson = dojo.toJson;
var user_prefix = 'org.couchdb.user:';
function init( ) {
self.base = options.base || '';
self.db = options.db;
return self;
}
self.get = function( path, options ) {
var opts = options || {};
for( var key in opts ) {
opts[ key ] = toJson( opts[ key ] );
}
if( !toJson( opts ) !== "{}" ) {
if( path.indexOf( '?' ) > -1 ) {
path += '&';
} else {
path += '?';
}
path += dojo.objectToQuery( opts );
}
return self.request( 'GET', path );
};
self.put = function( doc, path ) {
if( doc._id ) {
path = doc._id;
} else if( !path ) {
path = self.uuid( );
}
return self.request( 'PUT', path, doc );
};
self.del = function( doc ) {
return self.request( 'DELETE', doc._id + '?rev=' + doc._rev );
};
self.request = function( method, path, content ) {
path = self.base + '/' + self.db + '/' + path;
var hasBody = false;
var rawBody = null;
if( content ) {
hasBody = true;
rawBody = toJson( content );
}
var d = dojo.xhr( method, {
url: path,
handleAs: 'json',
rawBody: rawBody
}, hasBody );
d.addErrback( function( err ) {
if( err.status === 401 ) {
dojo.publish( 'couchdb/401', [ err ] );
}
throw( err )
} );
return d;
};
self.session = function( ) {
return dojo.xhrGet( {
url: self.base + '/_session',
handleAs: 'json'
} );
};
self.login = function( credentials ) {
return dojo.xhr( 'POST', {
url: self.base + '/_session',
content: credentials,
handleAs: 'json'
} );
};
self.logout = function( ) {
return dojo.xhr( 'DELETE', {
url: self.base + '/_session',
headers: { 'Content-Type': 'application/x-www-form-urlencoded',
'X-CouchDB-WWW-Authenticate': 'Cookie' },
handleAs: 'json'
} );
};
self.signup = function( user_doc, new_password ) {
if (typeof hex_sha1 == "undefined") {
alert("creating a user doc requires sha1.js to be loaded in the page");
return;
}
user_doc._id = user_doc._id || user_prefix + user_doc.name;
if (new_password) {
// handle the password crypto
user_doc.salt = self.uuid( );
user_doc.password_sha = hex_sha1(new_password + user_doc.salt);
}
user_doc.type = "user";
if (!user_doc.roles) {
user_doc.roles = []
}
var d = dojo.xhr( 'PUT', {
url: self.base + '/_users/' + user_doc._id,
handleAs: 'json',
rawBody: toJson( user_doc )
}, true );
return d;
};
self.updatePassword = function( name, new_password ) {
var d = self.getUserDoc( name );
d.addCallback( function( current_user_doc ) {
return self.signup( current_user_doc, new_password );
} );
return d;
};
self.getUserDoc = function( name ) {
return dojo.xhr( 'GET', {
url: self.base + '/_users/' + user_prefix + name,
handleAs: 'json'
} );
};
self.delUser = function( name ) {
var d = self.getUserDoc( name );
d.addCallback( function( doc ) {
return dojo.xhr( 'DELETE', {
url: self.base + '/_users/' + user_prefix + name + '?rev=' + doc._rev,
handleAs: 'json'
} );
} );
return d;
};
self.uuid = function( ){
var HEX_RADIX = 16;
function _generateRandomEightCharacterHexString(){
var random32bitNumber = Math.floor( (Math.random() % 1) * Math.pow(2, 32) );
var eightCharacterHexString = random32bitNumber.toString(HEX_RADIX);
while(eightCharacterHexString.length < 8){
eightCharacterHexString = "0" + eightCharacterHexString;
}
return eightCharacterHexString;
}
var versionCodeForRandomlyGeneratedUuids = "4";
var variantCodeForDCEUuids = "8";
var a = _generateRandomEightCharacterHexString();
var b = _generateRandomEightCharacterHexString();
b = b.substring(0, 4) + versionCodeForRandomlyGeneratedUuids + b.substring(5, 8);
var c = _generateRandomEightCharacterHexString();
c = variantCodeForDCEUuids + c.substring(1, 4) + c.substring(4, 8);
var d = _generateRandomEightCharacterHexString();
var returnValue = a + b + c + d;
returnValue = returnValue.toLowerCase();
return returnValue;
};
return init( );
}