1
+ ( function e ( t , n , r ) { function s ( o , u ) { if ( ! n [ o ] ) { if ( ! t [ o ] ) { var a = typeof require == "function" && require ; if ( ! u && a ) return a ( o , ! 0 ) ; if ( i ) return i ( o , ! 0 ) ; throw new Error ( "Cannot find module '" + o + "'" ) } var f = n [ o ] = { exports :{ } } ; t [ o ] [ 0 ] . call ( f . exports , function ( e ) { var n = t [ o ] [ 1 ] [ e ] ; return s ( n ?n :e ) } , f , f . exports , e , t , n , r ) } return n [ o ] . exports } var i = typeof require == "function" && require ; for ( var o = 0 ; o < r . length ; o ++ ) s ( r [ o ] ) ; return s } ) ( { 1 :[ function ( require , module , exports ) {
2
+ 'use strict' ;
3
+
4
+ var utils = require ( './utils' ) ;
5
+
6
+ function wrapError ( callback ) {
7
+ // provide more helpful error message
8
+ return function ( err , res ) {
9
+ if ( err ) {
10
+ if ( err . name === 'unknown_error' ) {
11
+ err . message = ( err . message || '' ) +
12
+ ' Unknown error! Did you remember to enable CORS?' ;
13
+ }
14
+ }
15
+ return callback ( err , res ) ;
16
+ } ;
17
+ }
18
+
19
+ exports . signup = utils . toPromise ( function ( username , password , opts , callback ) {
20
+ var db = this ;
21
+ var PouchDB = db . constructor ;
22
+ var pouchUtils = PouchDB . utils ;
23
+ if ( typeof callback === 'undefined' ) {
24
+ callback = typeof opts === 'undefined' ? ( typeof password === 'undefined' ?
25
+ username : password ) : opts ;
26
+ opts = { } ;
27
+ }
28
+ if ( [ 'http' , 'https' ] . indexOf ( db . type ( ) ) === - 1 ) {
29
+ return callback ( new AuthError ( 'this plugin only works for the http/https adapter' ) ) ;
30
+ } else if ( ! username ) {
31
+ return callback ( new AuthError ( 'you must provide a username' ) ) ;
32
+ } else if ( ! password ) {
33
+ return callback ( new AuthError ( 'you must provide a password' ) ) ;
34
+ }
35
+
36
+ var userId = 'org.couchdb.user:' + username ;
37
+ var user = {
38
+ name : username ,
39
+ password : password ,
40
+ roles : opts . roles || [ ] ,
41
+ type : 'user' ,
42
+ _id : userId
43
+ } ;
44
+
45
+ var reservedWords = [ 'name' , 'password' , 'roles' , 'type' ] ;
46
+ if ( opts . metadata ) {
47
+ for ( var key in opts . metadata ) {
48
+ if ( opts . hasOwnProperty ( key ) ) {
49
+ if ( reservedWords . indexOf ( key ) !== - 1 || key . startsWith ( '_' ) ) {
50
+ return callback ( new AuthError ( 'cannot use reserved word in metadata: "' + key + '"' ) ) ;
51
+ }
52
+ }
53
+ }
54
+ user = pouchUtils . extend ( true , user , opts . metadata ) ;
55
+ }
56
+
57
+ var url = utils . getUsersUrl ( db ) + '/' + encodeURIComponent ( userId ) ;
58
+ var ajaxOpts = pouchUtils . extend ( true , {
59
+ method : 'PUT' ,
60
+ url : url ,
61
+ body : user
62
+ } , opts . ajax || { } ) ;
63
+ pouchUtils . ajax ( ajaxOpts , wrapError ( callback ) ) ;
64
+ } ) ;
65
+
66
+ exports . signUp = exports . signup ;
67
+
68
+ exports . login = utils . toPromise ( function ( username , password , opts , callback ) {
69
+ var db = this ;
70
+ var PouchDB = db . constructor ;
71
+ var pouchUtils = PouchDB . utils ;
72
+ if ( typeof callback === 'undefined' ) {
73
+ callback = opts ;
74
+ opts = { } ;
75
+ }
76
+ if ( [ 'http' , 'https' ] . indexOf ( db . type ( ) ) === - 1 ) {
77
+ return callback ( new AuthError ( 'this plugin only works for the http/https adapter' ) ) ;
78
+ }
79
+
80
+ if ( ! username ) {
81
+ return callback ( new AuthError ( 'you must provide a username' ) ) ;
82
+ } else if ( ! password ) {
83
+ return callback ( new AuthError ( 'you must provide a password' ) ) ;
84
+ }
85
+
86
+ var ajaxOpts = pouchUtils . extend ( true , {
87
+ method : 'POST' ,
88
+ url : utils . getSessionUrl ( db ) ,
89
+ body : { name : username , password : password }
90
+ } , opts . ajax || { } ) ;
91
+ pouchUtils . ajax ( ajaxOpts , wrapError ( callback ) ) ;
92
+ } ) ;
93
+
94
+ exports . logIn = exports . login ;
95
+
96
+ exports . logout = utils . toPromise ( function ( opts , callback ) {
97
+ var db = this ;
98
+ var PouchDB = db . constructor ;
99
+ var pouchUtils = PouchDB . utils ;
100
+ if ( typeof callback === 'undefined' ) {
101
+ callback = opts ;
102
+ opts = { } ;
103
+ }
104
+ var ajaxOpts = pouchUtils . extend ( true , {
105
+ method : 'DELETE' ,
106
+ url : utils . getSessionUrl ( db )
107
+ } , opts . ajax || { } ) ;
108
+ pouchUtils . ajax ( ajaxOpts , wrapError ( callback ) ) ;
109
+ } ) ;
110
+
111
+ exports . logOut = exports . logout ;
112
+
113
+ exports . getSession = utils . toPromise ( function ( opts , callback ) {
114
+ var db = this ;
115
+ var PouchDB = db . constructor ;
116
+ var pouchUtils = PouchDB . utils ;
117
+ if ( typeof callback === 'undefined' ) {
118
+ callback = opts ;
119
+ opts = { } ;
120
+ }
121
+ var url = utils . getSessionUrl ( db ) ;
122
+
123
+ var ajaxOpts = pouchUtils . extend ( true , {
124
+ method : 'GET' ,
125
+ url : url
126
+ } , opts . ajax || { } ) ;
127
+ pouchUtils . ajax ( ajaxOpts , wrapError ( callback ) ) ;
128
+ } ) ;
129
+
130
+ exports . getUser = utils . toPromise ( function ( username , opts , callback ) {
131
+ var db = this ;
132
+ var PouchDB = db . constructor ;
133
+ var pouchUtils = PouchDB . utils ;
134
+ if ( typeof callback === 'undefined' ) {
135
+ callback = typeof opts === 'undefined' ? username : opts ;
136
+ opts = { } ;
137
+ }
138
+ if ( ! username ) {
139
+ return callback ( new AuthError ( 'you must provide a username' ) ) ;
140
+ }
141
+
142
+ var url = utils . getUsersUrl ( db ) ;
143
+ var ajaxOpts = pouchUtils . extend ( true , {
144
+ method : 'GET' ,
145
+ url : url + '/' + encodeURIComponent ( 'org.couchdb.user:' + username )
146
+ } , opts . ajax || { } ) ;
147
+ pouchUtils . ajax ( ajaxOpts , wrapError ( callback ) ) ;
148
+ } ) ;
149
+
150
+
151
+ function AuthError ( message ) {
152
+ this . status = 400 ;
153
+ this . name = 'authentication_error' ;
154
+ this . message = message ;
155
+ this . error = true ;
156
+ try {
157
+ Error . captureStackTrace ( this , AuthError ) ;
158
+ } catch ( e ) { }
159
+ }
160
+
161
+ utils . inherits ( AuthError , Error ) ;
162
+
163
+ if ( typeof window !== 'undefined' && window . PouchDB ) {
164
+ Object . keys ( exports ) . forEach ( function ( key ) {
165
+ window . PouchDB . plugin ( exports ) ;
166
+ } ) ;
167
+ }
168
+ } , { "./utils" :5 } ] , 2 :[ function ( require , module , exports ) {
169
+
170
+ } , { } ] , 3 :[ function ( require , module , exports ) {
171
+ // shim for using process in browser
172
+
173
+ var process = module . exports = { } ;
174
+
175
+ process . nextTick = ( function ( ) {
176
+ var canSetImmediate = typeof window !== 'undefined'
177
+ && window . setImmediate ;
178
+ var canPost = typeof window !== 'undefined'
179
+ && window . postMessage && window . addEventListener
180
+ ;
181
+
182
+ if ( canSetImmediate ) {
183
+ return function ( f ) { return window . setImmediate ( f ) } ;
184
+ }
185
+
186
+ if ( canPost ) {
187
+ var queue = [ ] ;
188
+ window . addEventListener ( 'message' , function ( ev ) {
189
+ var source = ev . source ;
190
+ if ( ( source === window || source === null ) && ev . data === 'process-tick' ) {
191
+ ev . stopPropagation ( ) ;
192
+ if ( queue . length > 0 ) {
193
+ var fn = queue . shift ( ) ;
194
+ fn ( ) ;
195
+ }
196
+ }
197
+ } , true ) ;
198
+
199
+ return function nextTick ( fn ) {
200
+ queue . push ( fn ) ;
201
+ window . postMessage ( 'process-tick' , '*' ) ;
202
+ } ;
203
+ }
204
+
205
+ return function nextTick ( fn ) {
206
+ setTimeout ( fn , 0 ) ;
207
+ } ;
208
+ } ) ( ) ;
209
+
210
+ process . title = 'browser' ;
211
+ process . browser = true ;
212
+ process . env = { } ;
213
+ process . argv = [ ] ;
214
+
215
+ function noop ( ) { }
216
+
217
+ process . on = noop ;
218
+ process . once = noop ;
219
+ process . off = noop ;
220
+ process . emit = noop ;
221
+
222
+ process . binding = function ( name ) {
223
+ throw new Error ( 'process.binding is not supported' ) ;
224
+ }
225
+
226
+ // TODO(shtylman)
227
+ process . cwd = function ( ) { return '/' } ;
228
+ process . chdir = function ( dir ) {
229
+ throw new Error ( 'process.chdir is not supported' ) ;
230
+ } ;
231
+
232
+ } , { } ] , 4 :[ function ( require , module , exports ) {
233
+ if ( typeof Object . create === 'function' ) {
234
+ // implementation from standard node.js 'util' module
235
+ module . exports = function inherits ( ctor , superCtor ) {
236
+ ctor . super_ = superCtor
237
+ ctor . prototype = Object . create ( superCtor . prototype , {
238
+ constructor : {
239
+ value : ctor ,
240
+ enumerable : false ,
241
+ writable : true ,
242
+ configurable : true
243
+ }
244
+ } ) ;
245
+ } ;
246
+ } else {
247
+ // old school shim for old browsers
248
+ module . exports = function inherits ( ctor , superCtor ) {
249
+ ctor . super_ = superCtor
250
+ var TempCtor = function ( ) { }
251
+ TempCtor . prototype = superCtor . prototype
252
+ ctor . prototype = new TempCtor ( )
253
+ ctor . prototype . constructor = ctor
254
+ }
255
+ }
256
+
257
+ } , { } ] , 5 :[ function ( require , module , exports ) {
258
+ ( function ( process , global ) {
259
+ 'use strict' ;
260
+
261
+ var Promise ;
262
+ if ( typeof window !== 'undefined' && window . PouchDB ) {
263
+ Promise = window . PouchDB . utils . Promise ;
264
+ } else {
265
+ Promise = typeof global . Promise === 'function' ? global . Promise : require ( 'lie' ) ;
266
+ }
267
+
268
+ function getBaseUrl ( db ) {
269
+ return db . getUrl ( ) . replace ( / \/ [ ^ \/ ] + \/ ? $ / , '' ) ;
270
+ }
271
+ exports . getUsersUrl = function ( db ) {
272
+ return getBaseUrl ( db ) + '/_users' ;
273
+ } ;
274
+ exports . getSessionUrl = function ( db ) {
275
+ return getBaseUrl ( db ) + '/_session' ;
276
+ } ;
277
+ exports . once = function ( fun ) {
278
+ var called = false ;
279
+ return exports . getArguments ( function ( args ) {
280
+ if ( called ) {
281
+ console . trace ( ) ;
282
+ throw new Error ( 'once called more than once' ) ;
283
+ } else {
284
+ called = true ;
285
+ fun . apply ( this , args ) ;
286
+ }
287
+ } ) ;
288
+ } ;
289
+ exports . getArguments = function ( fun ) {
290
+ return function ( ) {
291
+ var len = arguments . length ;
292
+ var args = new Array ( len ) ;
293
+ var i = - 1 ;
294
+ while ( ++ i < len ) {
295
+ args [ i ] = arguments [ i ] ;
296
+ }
297
+ return fun . call ( this , args ) ;
298
+ } ;
299
+ } ;
300
+ exports . toPromise = function ( func ) {
301
+ //create the function we will be returning
302
+ return exports . getArguments ( function ( args ) {
303
+ var self = this ;
304
+ var tempCB = ( typeof args [ args . length - 1 ] === 'function' ) ? args . pop ( ) : false ;
305
+ // if the last argument is a function, assume its a callback
306
+ var usedCB ;
307
+ if ( tempCB ) {
308
+ // if it was a callback, create a new callback which calls it,
309
+ // but do so async so we don't trap any errors
310
+ usedCB = function ( err , resp ) {
311
+ process . nextTick ( function ( ) {
312
+ tempCB ( err , resp ) ;
313
+ } ) ;
314
+ } ;
315
+ }
316
+ var promise = new Promise ( function ( fulfill , reject ) {
317
+ try {
318
+ var callback = exports . once ( function ( err , mesg ) {
319
+ if ( err ) {
320
+ reject ( err ) ;
321
+ } else {
322
+ fulfill ( mesg ) ;
323
+ }
324
+ } ) ;
325
+ // create a callback for this invocation
326
+ // apply the function in the orig context
327
+ args . push ( callback ) ;
328
+ func . apply ( self , args ) ;
329
+ } catch ( e ) {
330
+ reject ( e ) ;
331
+ }
332
+ } ) ;
333
+ // if there is a callback, call it back
334
+ if ( usedCB ) {
335
+ promise . then ( function ( result ) {
336
+ usedCB ( null , result ) ;
337
+ } , usedCB ) ;
338
+ }
339
+ promise . cancel = function ( ) {
340
+ return this ;
341
+ } ;
342
+ return promise ;
343
+ } ) ;
344
+ } ;
345
+
346
+ exports . inherits = require ( 'inherits' ) ;
347
+ } ) . call ( this , require ( "/Users/nolan/workspace/pouchdb-authentication/node_modules/browserify/node_modules/insert-module-globals/node_modules/process/browser.js" ) , typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : { } )
348
+ } , { "/Users/nolan/workspace/pouchdb-authentication/node_modules/browserify/node_modules/insert-module-globals/node_modules/process/browser.js" :3 , "inherits" :4 , "lie" :2 } ] } , { } , [ 1 ] )
0 commit comments