forked from bitpay/bitpay-public-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitpay-public-client.js
1014 lines (863 loc) · 27 KB
/
bitpay-public-client.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Bitpay = f()}})(function(){var define,module,exports;return (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);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.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){
var Client = require('./lib');
module.exports = Client;
// Errors thrown by the library
Client.errors = require('./lib/errors');
},{"./lib":5,"./lib/errors":3}],2:[function(require,module,exports){
'use strict';
var util = require('./utils/util');
var url = require('./utils/url');
var request = require('browser-request');
var log = require('./log');
var Errors = require('./errors');
var BASE_URL = 'https://bitpay.com';
/**
* @desc ClientAPI constructor.
*
* @param {Object} opts
* @constructor
*/
function API(opts) {
opts = opts || {};
this.verbose = !!opts.verbose;
this.request = opts.request || request;
this.baseUrl = opts.baseUrl || BASE_URL;
var parsedUrl = url.parse(this.baseUrl);
this.basePath = parsedUrl.path;
this.timeout = opts.timeout || 50000;
if (this.verbose) {
log.setLevel('debug');
} else {
log.setLevel('info');
}
}
API.prototype.initialize = function(opts, cb) {
var self = this;
return cb();
};
/**
* Returns a new invoice response using the public facade only (no cryptographic signing).
* Required parameters listed below. See https://bitpay.com/api#resource-Invoices.
* @param {Object} opts
* @param {Object} opts.token - The API token which associates the merchant account with this request.
* @param {Object} opts.guid - A unique identifier.
* @param {String} opts.currency - The price currency for which the invoice is to be created.
* @param {String} opts.price - The amount for which the invoice is to be created.
* @returns {Object} response - the invoice.
*/
API.prototype.createInvoice = function(opts, cb) {
var url = '/invoices/';
this._doPostRequest(url, opts, function(err, response) {
if (err) return cb(err);
return cb(null, response.data);
});
};
/**
* Returns all rates or the specified exchange rate.
* @param {Object} opts
* @param {String} opts.currency - (Optional) The currency to get the rate.
* @returns {Object} response - the rate or array of rates.
*/
API.prototype.getRates = function(opts, cb) {
var url = '/rates/';
if (opts && opts.currency) {
url += opts.currency;
}
this._doGetRequest(url, function(err, response) {
if (err) return cb(err);
return cb(null, response.data);
}, false);
};
/**
* Returns subscription status.
* @param {Object} opts
* @param {String} opts.user - User to subscribe.
* @param {String} opts.type - Device type (ios or android).
* @param {String} opts.token - Device token.
* @returns {Object} response - Status of subscription.
*/
API.prototype.subscribeToNotifications = function(opts, cb) {
var url = '/notifications/subscribe/';
this._doPostRequest(url, opts, function(err, response) {
if (err) return cb(err);
return cb(null, response);
});
};
/**
* Returns unsubscription status.
* @param {Object} opts
* @param {String} opts.token - Device token.
* @return {Callback} cb - Status of unsubscription.
*/
API.prototype.unsubscribeFromNotifications = function(opts, cb) {
var url = '/notifications/unsubscribe/';
this._doPostRequest(url, opts, function(err, response) {
if (err) return cb(err);
return cb(null, response);
});
};
/**
* Returns a list of recently created invoices.
* @param {Object} opts
* @param {String} opts.token - Device token.
* @return {Callback} cb - An array of payment URLs or an error.
*/
API.prototype.getNotifiedInvoices = function(opts, cb) {
var url = '/notifications/invoices/';
this._doPostRequest(url, opts, function(err, response) {
if (err) return cb(err);
return cb(null, response);
});
};
/**
* Parse errors
* @private
* @static
* @memberof Client.API
* @param {Object} body
*/
API._parseError = function(body) {
if (typeof body === "string") {
try {
body = JSON.parse(body);
} catch (e) {
body = {
error: body
};
}
}
var ret;
if (body && body.code) {
if (Errors[body.code]) {
ret = new Errors[body.code];
} else {
ret = new Error(body.code);
}
} else if (body && body.errors) {
var msg = '';
for (var i = 0; i < body.errors.length; i++) {
switch (body.errors[i].error) {
case 'Missing required parameter.':
msg += body.errors[i].error + ': ' + body.errors[i].param + '\n';
break;
default:
msg += body.errors[i].error + '\n';
break;
}
}
ret = new Error(msg);
} else {
ret = new Error(body.error || body);
}
log.error(ret);
return ret;
};
API.prototype._getHeaders = function(method, url, args) {
var headers = {
};
return headers;
}
/**
* Do an HTTP request
* @private
*
* @param {Object} method
* @param {String} url
* @param {Object} args
* @param {Callback} cb
*/
API.prototype._doRequest = function(method, url, args, cb) {
var absUrl = this.baseUrl + url;
var newArgs = {
// relUrl: only for testing with `supertest`
relUrl: this.basePath + url,
headers: this._getHeaders(method, url, args),
method: method,
url: absUrl,
body: args,
json: true,
timeout: this.timeout,
};
log.debug('Request Args', util.inspect(args, {
depth: 10
}));
this.request(newArgs, function(err, res, body) {
log.debug(util.inspect(body, {
depth: 10
}));
if (!res) {
return cb(new Errors.CONNECTION_ERROR);
}
if (res.statusCode !== 200) {
if (res.statusCode === 404)
return cb(new Errors.NOT_FOUND);
if (!res.statusCode)
return cb(new Errors.CONNECTION_ERROR);
if (body)
return cb(API._parseError(body));
return cb(new Errors.BAD_REQUEST(res.statusCode));
}
if (body === '{"error":"read ECONNRESET"}')
return cb(new Errors.ECONNRESET_ERROR(JSON.parse(body)));
return cb(null, body, res.header);
});
};
/**
* Do a POST request
* @private
*
* @param {String} url
* @param {Object} args
* @param {Callback} cb
*/
API.prototype._doPostRequest = function(url, args, cb) {
return this._doRequest('post', url, args, cb);
};
API.prototype._doPutRequest = function(url, args, cb) {
return this._doRequest('put', url, args, cb);
};
/**
* Do a GET request
* @private
*
* @param {String} url
* @param {Callback} cb
* @param {Boolean} cacheBust
*/
API.prototype._doGetRequest = function(url, cb, cacheBust) {
if (cacheBust) {
url += url.indexOf('?') > 0 ? '&' : '?';
url += 'r=' + Math.floor(Math.random()*90000) + 10000;
}
return this._doRequest('get', url, {}, cb);
};
/**
* Do a DELETE request
* @private
*
* @param {String} url
* @param {Callback} cb
*/
API.prototype._doDeleteRequest = function(url, cb) {
return this._doRequest('delete', url, {}, cb);
};
module.exports = API;
},{"./errors":3,"./log":6,"./utils/url":7,"./utils/util":8,"browser-request":9}],3:[function(require,module,exports){
'use strict';
function format(message, args) {
return message
.replace('{0}', args[0])
.replace('{1}', args[1])
.replace('{2}', args[2]);
}
var traverseNode = function(parent, errorDefinition) {
var NodeError = function() {
if (typeof errorDefinition.message === "string") {
this.message = format(errorDefinition.message, arguments);
} else if (typeof errorDefinition.message === "function") {
this.message = errorDefinition.message.apply(null, arguments);
} else {
throw new Error('Invalid error definition for ' + errorDefinition.name);
}
this.stack = this.message + '\n' + (new Error()).stack;
};
NodeError.prototype = Object.create(parent.prototype);
NodeError.prototype.name = parent.prototype.name + errorDefinition.name;
parent[errorDefinition.name] = NodeError;
if (errorDefinition.errors) {
childDefinitions(NodeError, errorDefinition.errors);
}
return NodeError;
};
/* jshint latedef: false */
var childDefinitions = function(parent, childDefinitions) {
childDefinitions.forEach(function(childDefinition) {
traverseNode(parent, childDefinition);
});
};
/* jshint latedef: true */
var traverseRoot = function(parent, errorsDefinition) {
childDefinitions(parent, errorsDefinition);
return parent;
};
var bpc = {};
bpc.Error = function() {
this.message = 'Internal error';
this.stack = this.message + '\n' + (new Error()).stack;
};
bpc.Error.prototype = Object.create(Error.prototype);
bpc.Error.prototype.name = 'bpc.Error';
var data = require('./spec');
traverseRoot(bpc.Error, data);
module.exports = bpc.Error;
module.exports.extend = function(spec) {
return traverseNode(bpc.Error, spec);
};
},{"./spec":4}],4:[function(require,module,exports){
'use strict';
var errorSpec = [{
name: 'CONNECTION_ERROR',
message: 'connection error'
}, {
name: 'NOT_FOUND',
message: 'not found'
}, {
name: 'ECONNRESET_ERROR',
message: 'ECONNRESET, body: {0}'
}, {
name: 'BAD_REQUEST',
message: 'bad request, code: {0}'
} ];
module.exports = errorSpec;
},{}],5:[function(require,module,exports){
/**
* A client library for the public facade of the BitPay API.
* @module Client
*/
/**
* Client API.
* @alias module:Client.API
*/
var client = module.exports = require('./api');
},{"./api":2}],6:[function(require,module,exports){
/**
* @desc
* A simple logger that wraps the <tt>console.log</tt> methods when available.
*
* Usage:
* <pre>
* log = new Logger('copay');
* log.setLevel('info');
* log.debug('Message!'); // won't show
* log.setLevel('debug');
* log.debug('Message!', 1); // will show '[debug] copay: Message!, 1'
* </pre>
*
* @param {string} name - a name for the logger. This will show up on every log call
* @constructor
*/
var Logger = function(name) {
this.name = name || 'log';
this.level = 2;
};
Logger.prototype.getLevels = function() {
return levels;
};
var levels = {
'debug': 0,
'info': 1,
'log': 2,
'warn': 3,
'error': 4,
'fatal': 5
};
Object.keys(levels).forEach(function(levelName) {
var level = levels[levelName];
Logger.prototype[levelName] = function() {
if (level >= levels[this.level]) {
if (Error.stackTraceLimit && this.level == 'debug') {
var old = Error.stackTraceLimit;
Error.stackTraceLimit = 2;
var stack;
// this hack is to be compatible with IE11
try {
anerror();
} catch (e) {
stack = e.stack;
}
var lines = stack.split('\n');
var caller = lines[2];
caller = ':' + caller.substr(6);
Error.stackTraceLimit = old;
}
var str = '[' + levelName + (caller || '') + '] ' + arguments[0],
extraArgs,
extraArgs = [].slice.call(arguments, 1);
if (console[levelName]) {
extraArgs.unshift(str);
console[levelName].apply(console, extraArgs);
} else {
if (extraArgs.length) {
str += JSON.stringify(extraArgs);
}
console.log(str);
}
}
};
});
/**
* @desc
* Sets the level of a logger. A level can be any bewteen: 'debug', 'info', 'log',
* 'warn', 'error', and 'fatal'. That order matters: if a logger's level is set to
* 'warn', calling <tt>level.debug</tt> won't have any effect.
*
* @param {number} level - the name of the logging level
*/
Logger.prototype.setLevel = function(level) {
this.level = level;
};
/**
* @class Logger
* @method debug
* @desc Log messages at the debug level.
* @param {*} args - the arguments to be logged.
*/
/**
* @class Logger
* @method info
* @desc Log messages at the info level.
* @param {*} args - the arguments to be logged.
*/
/**
* @class Logger
* @method log
* @desc Log messages at an intermediary level called 'log'.
* @param {*} args - the arguments to be logged.
*/
/**
* @class Logger
* @method warn
* @desc Log messages at the warn level.
* @param {*} args - the arguments to be logged.
*/
/**
* @class Logger
* @method error
* @desc Log messages at the error level.
* @param {*} args - the arguments to be logged.
*/
/**
* @class Logger
* @method fatal
* @desc Log messages at the fatal level.
* @param {*} args - the arguments to be logged.
*/
var logger = new Logger('copay');
var error = new Error();
logger.setLevel('info');
module.exports = logger;
},{}],7:[function(require,module,exports){
'use strict';
module.exports.parse = function(href) {
var match = href.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
return match && {
protocol: match[1],
host: match[2],
hostname: match[3],
port: match[4],
path: '/' + match[5],
search: match[6],
hash: match[7]
}
};
},{}],8:[function(require,module,exports){
'use strict';
module.exports.inspect = function(obj, opts) {
// Do nothing in the browser
};
},{}],9:[function(require,module,exports){
// Browser Request
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// UMD HEADER START
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.returnExports = factory();
}
}(this, function () {
// UMD HEADER END
var XHR = XMLHttpRequest
if (!XHR) throw new Error('missing XMLHttpRequest')
request.log = {
'trace': noop, 'debug': noop, 'info': noop, 'warn': noop, 'error': noop
}
var DEFAULT_TIMEOUT = 3 * 60 * 1000 // 3 minutes
//
// request
//
function request(options, callback) {
// The entry-point to the API: prep the options object and pass the real work to run_xhr.
if(typeof callback !== 'function')
throw new Error('Bad callback given: ' + callback)
if(!options)
throw new Error('No options given')
var options_onResponse = options.onResponse; // Save this for later.
if(typeof options === 'string')
options = {'uri':options};
else
options = JSON.parse(JSON.stringify(options)); // Use a duplicate for mutating.
options.onResponse = options_onResponse // And put it back.
if (options.verbose) request.log = getLogger();
if(options.url) {
options.uri = options.url;
delete options.url;
}
if(!options.uri && options.uri !== "")
throw new Error("options.uri is a required argument");
if(typeof options.uri != "string")
throw new Error("options.uri must be a string");
var unsupported_options = ['proxy', '_redirectsFollowed', 'maxRedirects', 'followRedirect']
for (var i = 0; i < unsupported_options.length; i++)
if(options[ unsupported_options[i] ])
throw new Error("options." + unsupported_options[i] + " is not supported")
options.callback = callback
options.method = options.method || 'GET';
options.headers = options.headers || {};
options.body = options.body || null
options.timeout = options.timeout || request.DEFAULT_TIMEOUT
if(options.headers.host)
throw new Error("Options.headers.host is not supported");
if(options.json) {
options.headers.accept = options.headers.accept || 'application/json'
if(options.method !== 'GET')
options.headers['content-type'] = 'application/json'
if(typeof options.json !== 'boolean')
options.body = JSON.stringify(options.json)
else if(typeof options.body !== 'string')
options.body = JSON.stringify(options.body)
}
//BEGIN QS Hack
var serialize = function(obj) {
var str = [];
for(var p in obj)
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
}
if(options.qs){
var qs = (typeof options.qs == 'string')? options.qs : serialize(options.qs);
if(options.uri.indexOf('?') !== -1){ //no get params
options.uri = options.uri+'&'+qs;
}else{ //existing get params
options.uri = options.uri+'?'+qs;
}
}
//END QS Hack
//BEGIN FORM Hack
var multipart = function(obj) {
//todo: support file type (useful?)
var result = {};
result.boundry = '-------------------------------'+Math.floor(Math.random()*1000000000);
var lines = [];
for(var p in obj){
if (obj.hasOwnProperty(p)) {
lines.push(
'--'+result.boundry+"\n"+
'Content-Disposition: form-data; name="'+p+'"'+"\n"+
"\n"+
obj[p]+"\n"
);
}
}
lines.push( '--'+result.boundry+'--' );
result.body = lines.join('');
result.length = result.body.length;
result.type = 'multipart/form-data; boundary='+result.boundry;
return result;
}
if(options.form){
if(typeof options.form == 'string') throw('form name unsupported');
if(options.method === 'POST'){
var encoding = (options.encoding || 'application/x-www-form-urlencoded').toLowerCase();
options.headers['content-type'] = encoding;
switch(encoding){
case 'application/x-www-form-urlencoded':
options.body = serialize(options.form).replace(/%20/g, "+");
break;
case 'multipart/form-data':
var multi = multipart(options.form);
//options.headers['content-length'] = multi.length;
options.body = multi.body;
options.headers['content-type'] = multi.type;
break;
default : throw new Error('unsupported encoding:'+encoding);
}
}
}
//END FORM Hack
// If onResponse is boolean true, call back immediately when the response is known,
// not when the full request is complete.
options.onResponse = options.onResponse || noop
if(options.onResponse === true) {
options.onResponse = callback
options.callback = noop
}
// XXX Browsers do not like this.
//if(options.body)
// options.headers['content-length'] = options.body.length;
// HTTP basic authentication
if(!options.headers.authorization && options.auth)
options.headers.authorization = 'Basic ' + b64_enc(options.auth.username + ':' + options.auth.password);
return run_xhr(options)
}
var req_seq = 0
function run_xhr(options) {
var xhr = new XHR
, timed_out = false
, is_cors = is_crossDomain(options.uri)
, supports_cors = ('withCredentials' in xhr)
req_seq += 1
xhr.seq_id = req_seq
xhr.id = req_seq + ': ' + options.method + ' ' + options.uri
xhr._id = xhr.id // I know I will type "_id" from habit all the time.
if(is_cors && !supports_cors) {
var cors_err = new Error('Browser does not support cross-origin request: ' + options.uri)
cors_err.cors = 'unsupported'
return options.callback(cors_err, xhr)
}
xhr.timeoutTimer = setTimeout(too_late, options.timeout)
function too_late() {
timed_out = true
var er = new Error('ETIMEDOUT')
er.code = 'ETIMEDOUT'
er.duration = options.timeout
request.log.error('Timeout', { 'id':xhr._id, 'milliseconds':options.timeout })
return options.callback(er, xhr)
}
// Some states can be skipped over, so remember what is still incomplete.
var did = {'response':false, 'loading':false, 'end':false}
xhr.onreadystatechange = on_state_change
xhr.open(options.method, options.uri, true) // asynchronous
if(is_cors)
xhr.withCredentials = !! options.withCredentials
xhr.send(options.body)
return xhr
function on_state_change(event) {
if(timed_out)
return request.log.debug('Ignoring timed out state change', {'state':xhr.readyState, 'id':xhr.id})
request.log.debug('State change', {'state':xhr.readyState, 'id':xhr.id, 'timed_out':timed_out})
if(xhr.readyState === XHR.OPENED) {
request.log.debug('Request started', {'id':xhr.id})
for (var key in options.headers)
xhr.setRequestHeader(key, options.headers[key])
}
else if(xhr.readyState === XHR.HEADERS_RECEIVED)
on_response()
else if(xhr.readyState === XHR.LOADING) {
on_response()
on_loading()
}
else if(xhr.readyState === XHR.DONE) {
on_response()
on_loading()
on_end()
}
}
function on_response() {
if(did.response)
return
did.response = true
request.log.debug('Got response', {'id':xhr.id, 'status':xhr.status})
clearTimeout(xhr.timeoutTimer)
xhr.statusCode = xhr.status // Node request compatibility
// Detect failed CORS requests.
if(is_cors && xhr.statusCode == 0) {
var cors_err = new Error('CORS request rejected: ' + options.uri)
cors_err.cors = 'rejected'
// Do not process this request further.
did.loading = true
did.end = true
return options.callback(cors_err, xhr)
}
options.onResponse(null, xhr)
}
function on_loading() {
if(did.loading)
return
did.loading = true
request.log.debug('Response body loading', {'id':xhr.id})
// TODO: Maybe simulate "data" events by watching xhr.responseText
}
function on_end() {
if(did.end)
return
did.end = true
request.log.debug('Request done', {'id':xhr.id})
xhr.body = xhr.responseText
if(options.json) {
try { xhr.body = JSON.parse(xhr.responseText) }
catch (er) { return options.callback(er, xhr) }
}
options.callback(null, xhr, xhr.body)
}
} // request
request.withCredentials = false;
request.DEFAULT_TIMEOUT = DEFAULT_TIMEOUT;
//
// defaults
//
request.defaults = function(options, requester) {
var def = function (method) {
var d = function (params, callback) {
if(typeof params === 'string')
params = {'uri': params};
else {
params = JSON.parse(JSON.stringify(params));
}
for (var i in options) {
if (params[i] === undefined) params[i] = options[i]
}
return method(params, callback)
}
return d
}
var de = def(request)
de.get = def(request.get)
de.post = def(request.post)
de.put = def(request.put)
de.head = def(request.head)
return de
}
//
// HTTP method shortcuts
//
var shortcuts = [ 'get', 'put', 'post', 'head' ];
shortcuts.forEach(function(shortcut) {
var method = shortcut.toUpperCase();
var func = shortcut.toLowerCase();
request[func] = function(opts) {
if(typeof opts === 'string')
opts = {'method':method, 'uri':opts};
else {
opts = JSON.parse(JSON.stringify(opts));
opts.method = method;
}
var args = [opts].concat(Array.prototype.slice.apply(arguments, [1]));
return request.apply(this, args);
}
})
//
// CouchDB shortcut
//
request.couch = function(options, callback) {
if(typeof options === 'string')
options = {'uri':options}
// Just use the request API to do JSON.
options.json = true
if(options.body)
options.json = options.body
delete options.body
callback = callback || noop
var xhr = request(options, couch_handler)
return xhr
function couch_handler(er, resp, body) {
if(er)
return callback(er, resp, body)
if((resp.statusCode < 200 || resp.statusCode > 299) && body.error) {
// The body is a Couch JSON object indicating the error.
er = new Error('CouchDB error: ' + (body.error.reason || body.error.error))
for (var key in body)
er[key] = body[key]
return callback(er, resp, body);
}
return callback(er, resp, body);
}
}
//
// Utility
//
function noop() {}
function getLogger() {
var logger = {}
, levels = ['trace', 'debug', 'info', 'warn', 'error']
, level, i
for(i = 0; i < levels.length; i++) {
level = levels[i]
logger[level] = noop
if(typeof console !== 'undefined' && console && console[level])
logger[level] = formatted(console, level)
}
return logger
}
function formatted(obj, method) {
return formatted_logger
function formatted_logger(str, context) {
if(typeof context === 'object')
str += ' ' + JSON.stringify(context)
return obj[method].call(obj, str)
}
}
// Return whether a URL is a cross-domain request.
function is_crossDomain(url) {
var rurl = /^([\w\+\.\-]+:)(?:\/\/([^\/?#:]*)(?::(\d+))?)?/
// jQuery #8138, IE may throw an exception when accessing
// a field from window.location if document.domain has been set
var ajaxLocation
try { ajaxLocation = location.href }
catch (e) {
// Use the href attribute of an A element since IE will modify it given document.location
ajaxLocation = document.createElement( "a" );
ajaxLocation.href = "";
ajaxLocation = ajaxLocation.href;
}
var ajaxLocParts = rurl.exec(ajaxLocation.toLowerCase()) || []
, parts = rurl.exec(url.toLowerCase() )
var result = !!(
parts &&
( parts[1] != ajaxLocParts[1]
|| parts[2] != ajaxLocParts[2]
|| (parts[3] || (parts[1] === "http:" ? 80 : 443)) != (ajaxLocParts[3] || (ajaxLocParts[1] === "http:" ? 80 : 443))
)
)
//console.debug('is_crossDomain('+url+') -> ' + result)
return result
}
// MIT License from http://phpjs.org/functions/base64_encode:358
function b64_enc (data) {
// Encodes string using MIME base64 algorithm
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, enc="", tmp_arr = [];
if (!data) {
return data;
}
// assume utf8 data
// data = this.utf8_encode(data+'');
do { // pack three octets into four hexets
o1 = data.charCodeAt(i++);
o2 = data.charCodeAt(i++);
o3 = data.charCodeAt(i++);
bits = o1<<16 | o2<<8 | o3;
h1 = bits>>18 & 0x3f;
h2 = bits>>12 & 0x3f;
h3 = bits>>6 & 0x3f;
h4 = bits & 0x3f;
// use hexets to index into b64, and append result to encoded string
tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
} while (i < data.length);
enc = tmp_arr.join('');
switch (data.length % 3) {
case 1:
enc = enc.slice(0, -2) + '==';
break;