|
@@ -0,0 +1,2023 @@
|
|
|
|
+(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){
|
|
|
|
+'use strict';
|
|
|
|
+
|
|
|
|
+module.exports = require('./lib/core');
|
|
|
|
+
|
|
|
|
+},{"./lib/core":4}],2:[function(require,module,exports){
|
|
|
|
+'use strict';
|
|
|
|
+
|
|
|
|
+function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
|
|
|
+
|
|
|
|
+var _core = require('../../../../core');
|
|
|
|
+
|
|
|
|
+var _core2 = _interopRequireDefault(_core);
|
|
|
|
+
|
|
|
|
+var _plugins = require('../../../../plugins');
|
|
|
|
+
|
|
|
|
+var transloadit = new _core2['default']({ wait: false });
|
|
|
|
+var files = transloadit.use(_plugins.DragDrop, { modal: true, selector: '#upload-target' }).use(_plugins.Tus10, { endpoint: 'http://master.tus.io:8080' }).run();
|
|
|
|
+
|
|
|
|
+console.log('--> Finished transloadit. Final result: ');
|
|
|
|
+console.dir(files);
|
|
|
|
+
|
|
|
|
+// var Transloadit = require('./src/core/Transloadit.js');
|
|
|
|
+// var DragDrop = require('./src/plugins/DragDrop.js');
|
|
|
|
+// var Tus10 = require('./src/plugins/Tus10.js');
|
|
|
|
+//
|
|
|
|
+// var transloadit = new Transloadit({wait: false});
|
|
|
|
+// var files = transloadit
|
|
|
|
+// .use(DragDrop, {modal: true})
|
|
|
|
+// .use(Tus10, {endpoint: 'http://master.tus.io:8080'})
|
|
|
|
+// .run();
|
|
|
|
+//
|
|
|
|
+// console.log('--> Finished transloadit. Final result: ');
|
|
|
|
+// console.dir(files);
|
|
|
|
+
|
|
|
|
+},{"../../../../core":1,"../../../../plugins":14}],3:[function(require,module,exports){
|
|
|
|
+'use strict';
|
|
|
|
+
|
|
|
|
+Object.defineProperty(exports, '__esModule', {
|
|
|
|
+ value: true
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+var _createClass = (function () {
|
|
|
|
+ function defineProperties(target, props) {
|
|
|
|
+ for (var i = 0; i < props.length; i++) {
|
|
|
|
+ var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ('value' in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);
|
|
|
|
+ }
|
|
|
|
+ }return function (Constructor, protoProps, staticProps) {
|
|
|
|
+ if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;
|
|
|
|
+ };
|
|
|
|
+})();
|
|
|
|
+
|
|
|
|
+function _classCallCheck(instance, Constructor) {
|
|
|
|
+ if (!(instance instanceof Constructor)) {
|
|
|
|
+ throw new TypeError('Cannot call a class as a function');
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+var _default = (function () {
|
|
|
|
+ function _default(opts) {
|
|
|
|
+ _classCallCheck(this, _default);
|
|
|
|
+
|
|
|
|
+ // Dictates in what order different plugin types are ran:
|
|
|
|
+ this.types = ['presetter', 'selecter', 'uploader'];
|
|
|
|
+
|
|
|
|
+ // Container for different types of plugins
|
|
|
|
+ this.plugins = {};
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ _createClass(_default, [{
|
|
|
|
+ key: 'use',
|
|
|
|
+ value: function use(Plugin, opts) {
|
|
|
|
+ // Instantiate
|
|
|
|
+ var plugin = new Plugin(this, opts);
|
|
|
|
+
|
|
|
|
+ // Save in plugin container
|
|
|
|
+ if (!this.plugins[plugin.type]) {
|
|
|
|
+ this.plugins[plugin.type] = [];
|
|
|
|
+ }
|
|
|
|
+ this.plugins[plugin.type].push(plugin);
|
|
|
|
+
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+ }, {
|
|
|
|
+ key: 'setProgress',
|
|
|
|
+ value: function setProgress(plugin, percentage) {
|
|
|
|
+ // Any plugin can call this via `this.core.setProgress(this, precentage)`
|
|
|
|
+ console.log(plugin.type + ' plugin ' + plugin.name + ' set the progress to ' + percentage);
|
|
|
|
+
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+ }, {
|
|
|
|
+ key: 'run',
|
|
|
|
+ value: function run() {
|
|
|
|
+ // Walk over plugins in the order as defined by this.types.
|
|
|
|
+ var files = [];
|
|
|
|
+ for (var j in this.types) {
|
|
|
|
+ var type = this.types[j];
|
|
|
|
+ // Walk over all plugins of this type, passing & modifying the files array as we go
|
|
|
|
+ for (var i in this.plugins[type]) {
|
|
|
|
+ var plugin = this.plugins[type][i];
|
|
|
|
+ console.log('--> Now running ' + plugin.type + ' plugin ' + plugin.name + ': ');
|
|
|
|
+ files = plugin.run(files);
|
|
|
|
+ console.dir(files);
|
|
|
|
+ console.log('');
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // core.run is the final step and retuns the results (vs every other method, returning `this`)
|
|
|
|
+ // for chainability
|
|
|
|
+ return files;
|
|
|
|
+ }
|
|
|
|
+ }]);
|
|
|
|
+
|
|
|
|
+ return _default;
|
|
|
|
+})();
|
|
|
|
+
|
|
|
|
+exports['default'] = _default;
|
|
|
|
+module.exports = exports['default'];
|
|
|
|
+
|
|
|
|
+},{}],4:[function(require,module,exports){
|
|
|
|
+'use strict';
|
|
|
|
+
|
|
|
|
+Object.defineProperty(exports, '__esModule', {
|
|
|
|
+ value: true
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+function _interopRequireDefault(obj) {
|
|
|
|
+ return obj && obj.__esModule ? obj : { 'default': obj };
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+var _Transloadit = require('./Transloadit');
|
|
|
|
+
|
|
|
|
+var _Transloadit2 = _interopRequireDefault(_Transloadit);
|
|
|
|
+
|
|
|
|
+exports['default'] = _Transloadit2['default'];
|
|
|
|
+module.exports = exports['default'];
|
|
|
|
+
|
|
|
|
+},{"./Transloadit":3}],5:[function(require,module,exports){
|
|
|
|
+'use strict';
|
|
|
|
+
|
|
|
|
+Object.defineProperty(exports, '__esModule', {
|
|
|
|
+ value: true
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+var _createClass = (function () {
|
|
|
|
+ function defineProperties(target, props) {
|
|
|
|
+ for (var i = 0; i < props.length; i++) {
|
|
|
|
+ var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ('value' in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);
|
|
|
|
+ }
|
|
|
|
+ }return function (Constructor, protoProps, staticProps) {
|
|
|
|
+ if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;
|
|
|
|
+ };
|
|
|
|
+})();
|
|
|
|
+
|
|
|
|
+var _get = function get(_x, _x2, _x3) {
|
|
|
|
+ var _again = true;_function: while (_again) {
|
|
|
|
+ var object = _x,
|
|
|
|
+ property = _x2,
|
|
|
|
+ receiver = _x3;_again = false;if (object === null) object = Function.prototype;var desc = Object.getOwnPropertyDescriptor(object, property);if (desc === undefined) {
|
|
|
|
+ var parent = Object.getPrototypeOf(object);if (parent === null) {
|
|
|
|
+ return undefined;
|
|
|
|
+ } else {
|
|
|
|
+ _x = parent;_x2 = property;_x3 = receiver;_again = true;desc = parent = undefined;continue _function;
|
|
|
|
+ }
|
|
|
|
+ } else if ('value' in desc) {
|
|
|
|
+ return desc.value;
|
|
|
|
+ } else {
|
|
|
|
+ var getter = desc.get;if (getter === undefined) {
|
|
|
|
+ return undefined;
|
|
|
|
+ }return getter.call(receiver);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+function _interopRequireDefault(obj) {
|
|
|
|
+ return obj && obj.__esModule ? obj : { 'default': obj };
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function _classCallCheck(instance, Constructor) {
|
|
|
|
+ if (!(instance instanceof Constructor)) {
|
|
|
|
+ throw new TypeError('Cannot call a class as a function');
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function _inherits(subClass, superClass) {
|
|
|
|
+ if (typeof superClass !== 'function' && superClass !== null) {
|
|
|
|
+ throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass);
|
|
|
|
+ }subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } });if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+var _TransloaditPlugin2 = require('./TransloaditPlugin');
|
|
|
|
+
|
|
|
|
+var _TransloaditPlugin3 = _interopRequireDefault(_TransloaditPlugin2);
|
|
|
|
+
|
|
|
|
+// This is how we roll $('.element').toggleClass in non-jQuery world
|
|
|
|
+function toggleClass(el, className) {
|
|
|
|
+ // console.log(el);
|
|
|
|
+
|
|
|
|
+ if (el.classList) {
|
|
|
|
+ el.classList.toggle(className);
|
|
|
|
+ } else {
|
|
|
|
+ var classes = el.className.split(' ');
|
|
|
|
+ var existingIndex = classes.indexOf(className);
|
|
|
|
+
|
|
|
|
+ if (existingIndex >= 0) {
|
|
|
|
+ classes.splice(existingIndex, 1);
|
|
|
|
+ } else {
|
|
|
|
+ classes.push(className);
|
|
|
|
+ el.className = classes.join(' ');
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+var DragDrop = (function (_TransloaditPlugin) {
|
|
|
|
+ _inherits(DragDrop, _TransloaditPlugin);
|
|
|
|
+
|
|
|
|
+ function DragDrop(core, opts) {
|
|
|
|
+ _classCallCheck(this, DragDrop);
|
|
|
|
+
|
|
|
|
+ _get(Object.getPrototypeOf(DragDrop.prototype), 'constructor', this).call(this, core, opts);
|
|
|
|
+ this.type = 'selecter';
|
|
|
|
+ this.opts = opts;
|
|
|
|
+ console.log(this.opts);
|
|
|
|
+
|
|
|
|
+ // get the element where Drag & Drop event will occur
|
|
|
|
+ this.dropzone = document.querySelectorAll(this.opts.selector)[0];
|
|
|
|
+
|
|
|
|
+ // crazy stuff so that ‘this’ will behave in class
|
|
|
|
+ this.handleDragEnter = this.handleDragEnter.bind(this);
|
|
|
|
+ this.handleDragOver = this.handleDragOver.bind(this);
|
|
|
|
+ this.handleDrop = this.handleDrop.bind(this);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ _createClass(DragDrop, [{
|
|
|
|
+ key: 'listenForEvents',
|
|
|
|
+ value: function listenForEvents() {
|
|
|
|
+ this.dropzone.addEventListener('dragenter', this.handleDragEnter);
|
|
|
|
+ this.dropzone.addEventListener('dragover', this.handleDragOver);
|
|
|
|
+ this.dropzone.addEventListener('drop', this.handleDrop);
|
|
|
|
+ console.log('waiting for some files to be dropped on ' + this.opts.selector);
|
|
|
|
+ }
|
|
|
|
+ }, {
|
|
|
|
+ key: 'handleDragEnter',
|
|
|
|
+ value: function handleDragEnter(e) {
|
|
|
|
+ event.stopPropagation();
|
|
|
|
+ event.preventDefault();
|
|
|
|
+ toggleClass(this.dropzone, 'is-dragover');
|
|
|
|
+ }
|
|
|
|
+ }, {
|
|
|
|
+ key: 'handleDragOver',
|
|
|
|
+ value: function handleDragOver(e) {
|
|
|
|
+ e.stopPropagation();
|
|
|
|
+ e.preventDefault();
|
|
|
|
+ }
|
|
|
|
+ }, {
|
|
|
|
+ key: 'handleDrop',
|
|
|
|
+ value: function handleDrop(e) {
|
|
|
|
+ console.log('all right, someone dropped something here...');
|
|
|
|
+ e.preventDefault();
|
|
|
|
+ toggleClass(this.dropzone, 'is-dragover');
|
|
|
|
+ var files = e.dataTransfer.files;
|
|
|
|
+ console.log(files);
|
|
|
|
+ this.handleFiles(files);
|
|
|
|
+ }
|
|
|
|
+ }, {
|
|
|
|
+ key: 'handleFiles',
|
|
|
|
+ value: function handleFiles(files) {
|
|
|
|
+ return files;
|
|
|
|
+ }
|
|
|
|
+ }, {
|
|
|
|
+ key: 'run',
|
|
|
|
+ value: function run(files) {
|
|
|
|
+ this.listenForEvents();
|
|
|
|
+ // this.core.setProgress(this, 0);
|
|
|
|
+ var selected = [{ name: 'lolcat.jpeg' }];
|
|
|
|
+ // this.core.setProgress(this, 100);
|
|
|
|
+
|
|
|
|
+ // return selected;
|
|
|
|
+ }
|
|
|
|
+ }]);
|
|
|
|
+
|
|
|
|
+ return DragDrop;
|
|
|
|
+})(_TransloaditPlugin3['default']);
|
|
|
|
+
|
|
|
|
+exports['default'] = DragDrop;
|
|
|
|
+module.exports = exports['default'];
|
|
|
|
+
|
|
|
|
+},{"./TransloaditPlugin":8}],6:[function(require,module,exports){
|
|
|
|
+'use strict';
|
|
|
|
+
|
|
|
|
+Object.defineProperty(exports, '__esModule', {
|
|
|
|
+ value: true
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+var _createClass = (function () {
|
|
|
|
+ function defineProperties(target, props) {
|
|
|
|
+ for (var i = 0; i < props.length; i++) {
|
|
|
|
+ var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ('value' in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);
|
|
|
|
+ }
|
|
|
|
+ }return function (Constructor, protoProps, staticProps) {
|
|
|
|
+ if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;
|
|
|
|
+ };
|
|
|
|
+})();
|
|
|
|
+
|
|
|
|
+function _interopRequireDefault(obj) {
|
|
|
|
+ return obj && obj.__esModule ? obj : { 'default': obj };
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function _classCallCheck(instance, Constructor) {
|
|
|
|
+ if (!(instance instanceof Constructor)) {
|
|
|
|
+ throw new TypeError('Cannot call a class as a function');
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+var _superagent = require('superagent');
|
|
|
|
+
|
|
|
|
+var _superagent2 = _interopRequireDefault(_superagent);
|
|
|
|
+
|
|
|
|
+var DropboxPlugin = (function () {
|
|
|
|
+ function DropboxPlugin() {
|
|
|
|
+ _classCallCheck(this, DropboxPlugin);
|
|
|
|
+
|
|
|
|
+ this.authenticate = this.authenticate.bind(this);
|
|
|
|
+ this.connect = this.connect.bind(this);
|
|
|
|
+ this.render = this.render.bind(this);
|
|
|
|
+ this.files = [];
|
|
|
|
+ this.currentDir = '/';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ _createClass(DropboxPlugin, [{
|
|
|
|
+ key: 'connect',
|
|
|
|
+ value: function connect(target) {
|
|
|
|
+ this._target = document.getElementById(target);
|
|
|
|
+
|
|
|
|
+ this.client = new Dropbox.Client({ key: 'b7dzc9ei5dv5hcv', token: '' });
|
|
|
|
+ this.client.authDriver(new Dropbox.AuthDriver.Redirect());
|
|
|
|
+ this.authenticate();
|
|
|
|
+
|
|
|
|
+ if (this.client.credentials().token) {
|
|
|
|
+ this.getDirectory();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }, {
|
|
|
|
+ key: 'authenticate',
|
|
|
|
+ value: function authenticate() {
|
|
|
|
+ this.client.authenticate();
|
|
|
|
+ }
|
|
|
|
+ }, {
|
|
|
|
+ key: 'addFile',
|
|
|
|
+ value: function addFile() {}
|
|
|
|
+ }, {
|
|
|
|
+ key: 'getDirectory',
|
|
|
|
+ value: function getDirectory() {
|
|
|
|
+ var _this = this;
|
|
|
|
+
|
|
|
|
+ // request.get(`https://api18.dropbox.com/1/metadata/auto/`)
|
|
|
|
+ // .query({
|
|
|
|
+ // client_id: 'b7dzc9ei5dv5hcv',
|
|
|
|
+ // token: this.client.credentials().token
|
|
|
|
+ // })
|
|
|
|
+ // .set('Content-Type', 'application/json')
|
|
|
|
+ // .end((err, res) => {
|
|
|
|
+ // console.log(res);
|
|
|
|
+ // })
|
|
|
|
+
|
|
|
|
+ return this.client.readdir(this.currentDir, function (error, entries, stat, statFiles) {
|
|
|
|
+ if (error) {
|
|
|
|
+ return showError(error); // Something went wrong.
|
|
|
|
+ }
|
|
|
|
+ return _this.render(statFiles);
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ }, {
|
|
|
|
+ key: 'run',
|
|
|
|
+ value: function run() {}
|
|
|
|
+ }, {
|
|
|
|
+ key: 'render',
|
|
|
|
+ value: function render(files) {
|
|
|
|
+ var _this2 = this;
|
|
|
|
+
|
|
|
|
+ // for each file in the directory, create a list item element
|
|
|
|
+ var elems = files.map(function (file, i) {
|
|
|
|
+ var icon = file.isFolder ? 'folder' : 'file';
|
|
|
|
+ return '<li data-type="' + icon + '" data-name="' + file.name + '"><span>' + icon + ' : </span><span> ' + file.name + '</span></li>';
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ // appends the list items to the target
|
|
|
|
+ this._target.innerHTML = elems.sort().join('');
|
|
|
|
+
|
|
|
|
+ if (this.currentDir.length > 1) {
|
|
|
|
+ var _parent = document.createElement('LI');
|
|
|
|
+ _parent.setAttribute('data-type', 'parent');
|
|
|
|
+ _parent.innerHTML = '<span>...</span>';
|
|
|
|
+ this._target.appendChild(_parent);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // add an onClick to each list item
|
|
|
|
+ var fileElems = this._target.querySelectorAll('li');
|
|
|
|
+
|
|
|
|
+ Array.prototype.forEach.call(fileElems, function (element) {
|
|
|
|
+ var type = element.getAttribute('data-type');
|
|
|
|
+
|
|
|
|
+ if (type === 'file') {
|
|
|
|
+ element.addEventListener('click', function () {
|
|
|
|
+ _this2.files.push(element.getAttribute('data-name'));
|
|
|
|
+ console.dir('files: ' + _this2.files);
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ element.addEventListener('dblclick', function () {
|
|
|
|
+ var length = _this2.currentDir.split('/').length;
|
|
|
|
+
|
|
|
|
+ if (type === 'folder') {
|
|
|
|
+ _this2.currentDir = '' + _this2.currentDir + element.getAttribute('data-name') + '/';
|
|
|
|
+ } else if (type === 'parent') {
|
|
|
|
+ _this2.currentDir = _this2.currentDir.split('/').slice(0, length - 2).join('/') + '/';
|
|
|
|
+ }
|
|
|
|
+ console.log(_this2.currentDir);
|
|
|
|
+ _this2.getDirectory();
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ }]);
|
|
|
|
+
|
|
|
|
+ return DropboxPlugin;
|
|
|
|
+})();
|
|
|
|
+
|
|
|
|
+exports['default'] = new DropboxPlugin();
|
|
|
|
+module.exports = exports['default'];
|
|
|
|
+
|
|
|
|
+},{"superagent":11}],7:[function(require,module,exports){
|
|
|
|
+'use strict';
|
|
|
|
+
|
|
|
|
+var _get = function get(_x, _x2, _x3) {
|
|
|
|
+ var _again = true;_function: while (_again) {
|
|
|
|
+ var object = _x,
|
|
|
|
+ property = _x2,
|
|
|
|
+ receiver = _x3;_again = false;if (object === null) object = Function.prototype;var desc = Object.getOwnPropertyDescriptor(object, property);if (desc === undefined) {
|
|
|
|
+ var parent = Object.getPrototypeOf(object);if (parent === null) {
|
|
|
|
+ return undefined;
|
|
|
|
+ } else {
|
|
|
|
+ _x = parent;_x2 = property;_x3 = receiver;_again = true;desc = parent = undefined;continue _function;
|
|
|
|
+ }
|
|
|
|
+ } else if ('value' in desc) {
|
|
|
|
+ return desc.value;
|
|
|
|
+ } else {
|
|
|
|
+ var getter = desc.get;if (getter === undefined) {
|
|
|
|
+ return undefined;
|
|
|
|
+ }return getter.call(receiver);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+function _interopRequireDefault(obj) {
|
|
|
|
+ return obj && obj.__esModule ? obj : { 'default': obj };
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function _classCallCheck(instance, Constructor) {
|
|
|
|
+ if (!(instance instanceof Constructor)) {
|
|
|
|
+ throw new TypeError('Cannot call a class as a function');
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function _inherits(subClass, superClass) {
|
|
|
|
+ if (typeof superClass !== 'function' && superClass !== null) {
|
|
|
|
+ throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass);
|
|
|
|
+ }subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } });if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+var _TransloaditPlugin2 = require('./TransloaditPlugin');
|
|
|
|
+
|
|
|
|
+var _TransloaditPlugin3 = _interopRequireDefault(_TransloaditPlugin2);
|
|
|
|
+
|
|
|
|
+var TransloaditBasic = (function (_TransloaditPlugin) {
|
|
|
|
+ _inherits(TransloaditBasic, _TransloaditPlugin);
|
|
|
|
+
|
|
|
|
+ function TransloaditBasic(core, opts) {
|
|
|
|
+ _classCallCheck(this, TransloaditBasic);
|
|
|
|
+
|
|
|
|
+ _get(Object.getPrototypeOf(TransloaditBasic.prototype), 'constructor', this).call(this, core, opts);
|
|
|
|
+ this.type = 'presetter';
|
|
|
|
+ this.core.use(DragDrop, { modal: true, wait: true }).use(Tus10, { endpoint: 'http://master.tus.io:8080' });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return TransloaditBasic;
|
|
|
|
+})(_TransloaditPlugin3['default']);
|
|
|
|
+
|
|
|
|
+},{"./TransloaditPlugin":8}],8:[function(require,module,exports){
|
|
|
|
+"use strict";
|
|
|
|
+
|
|
|
|
+Object.defineProperty(exports, "__esModule", {
|
|
|
|
+ value: true
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+var _createClass = (function () {
|
|
|
|
+ function defineProperties(target, props) {
|
|
|
|
+ for (var i = 0; i < props.length; i++) {
|
|
|
|
+ var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);
|
|
|
|
+ }
|
|
|
|
+ }return function (Constructor, protoProps, staticProps) {
|
|
|
|
+ if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;
|
|
|
|
+ };
|
|
|
|
+})();
|
|
|
|
+
|
|
|
|
+function _classCallCheck(instance, Constructor) {
|
|
|
|
+ if (!(instance instanceof Constructor)) {
|
|
|
|
+ throw new TypeError("Cannot call a class as a function");
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+var TransloaditPlugin = (function () {
|
|
|
|
+ // This contains boilerplate that all TransloaditPlugins share - and should not be used
|
|
|
|
+ // directly. It also shows which methods final plugins should implement/override,
|
|
|
|
+ // this deciding on structure.
|
|
|
|
+
|
|
|
|
+ function TransloaditPlugin(core, opts) {
|
|
|
|
+ _classCallCheck(this, TransloaditPlugin);
|
|
|
|
+
|
|
|
|
+ this.core = core;
|
|
|
|
+ this.opts = opts;
|
|
|
|
+ this.name = this.constructor.name;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ _createClass(TransloaditPlugin, [{
|
|
|
|
+ key: "run",
|
|
|
|
+ value: function run(files) {
|
|
|
|
+ return files;
|
|
|
|
+ }
|
|
|
|
+ }]);
|
|
|
|
+
|
|
|
|
+ return TransloaditPlugin;
|
|
|
|
+})();
|
|
|
|
+
|
|
|
|
+exports["default"] = TransloaditPlugin;
|
|
|
|
+module.exports = exports["default"];
|
|
|
|
+
|
|
|
|
+},{}],9:[function(require,module,exports){
|
|
|
|
+'use strict';
|
|
|
|
+
|
|
|
|
+Object.defineProperty(exports, '__esModule', {
|
|
|
|
+ value: true
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+var _createClass = (function () {
|
|
|
|
+ function defineProperties(target, props) {
|
|
|
|
+ for (var i = 0; i < props.length; i++) {
|
|
|
|
+ var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ('value' in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);
|
|
|
|
+ }
|
|
|
|
+ }return function (Constructor, protoProps, staticProps) {
|
|
|
|
+ if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;
|
|
|
|
+ };
|
|
|
|
+})();
|
|
|
|
+
|
|
|
|
+var _get = function get(_x, _x2, _x3) {
|
|
|
|
+ var _again = true;_function: while (_again) {
|
|
|
|
+ var object = _x,
|
|
|
|
+ property = _x2,
|
|
|
|
+ receiver = _x3;_again = false;if (object === null) object = Function.prototype;var desc = Object.getOwnPropertyDescriptor(object, property);if (desc === undefined) {
|
|
|
|
+ var parent = Object.getPrototypeOf(object);if (parent === null) {
|
|
|
|
+ return undefined;
|
|
|
|
+ } else {
|
|
|
|
+ _x = parent;_x2 = property;_x3 = receiver;_again = true;desc = parent = undefined;continue _function;
|
|
|
|
+ }
|
|
|
|
+ } else if ('value' in desc) {
|
|
|
|
+ return desc.value;
|
|
|
|
+ } else {
|
|
|
|
+ var getter = desc.get;if (getter === undefined) {
|
|
|
|
+ return undefined;
|
|
|
|
+ }return getter.call(receiver);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+function _interopRequireDefault(obj) {
|
|
|
|
+ return obj && obj.__esModule ? obj : { 'default': obj };
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function _classCallCheck(instance, Constructor) {
|
|
|
|
+ if (!(instance instanceof Constructor)) {
|
|
|
|
+ throw new TypeError('Cannot call a class as a function');
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function _inherits(subClass, superClass) {
|
|
|
|
+ if (typeof superClass !== 'function' && superClass !== null) {
|
|
|
|
+ throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass);
|
|
|
|
+ }subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } });if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+var _TransloaditPlugin2 = require('./TransloaditPlugin');
|
|
|
|
+
|
|
|
|
+var _TransloaditPlugin3 = _interopRequireDefault(_TransloaditPlugin2);
|
|
|
|
+
|
|
|
|
+var Tus10 = (function (_TransloaditPlugin) {
|
|
|
|
+ _inherits(Tus10, _TransloaditPlugin);
|
|
|
|
+
|
|
|
|
+ function Tus10(core, opts) {
|
|
|
|
+ _classCallCheck(this, Tus10);
|
|
|
|
+
|
|
|
|
+ _get(Object.getPrototypeOf(Tus10.prototype), 'constructor', this).call(this, core, opts);
|
|
|
|
+ this.type = 'uploader';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ _createClass(Tus10, [{
|
|
|
|
+ key: 'run',
|
|
|
|
+ value: function run(files) {
|
|
|
|
+ this.core.setProgress(this, 0);
|
|
|
|
+ var uploaded = [];
|
|
|
|
+ for (var i in files) {
|
|
|
|
+ var file = files[i];
|
|
|
|
+ this.core.setProgress(this, i * 1 + 1);
|
|
|
|
+ uploaded[i] = file;
|
|
|
|
+ uploaded[i].url = this.opts.endpoint + '/uploaded/' + file.name;
|
|
|
|
+ }
|
|
|
|
+ this.core.setProgress(this, 100);
|
|
|
|
+
|
|
|
|
+ return uploaded;
|
|
|
|
+ }
|
|
|
|
+ }]);
|
|
|
|
+
|
|
|
|
+ return Tus10;
|
|
|
|
+})(_TransloaditPlugin3['default']);
|
|
|
|
+
|
|
|
|
+exports['default'] = Tus10;
|
|
|
|
+module.exports = exports['default'];
|
|
|
|
+
|
|
|
|
+},{"./TransloaditPlugin":8}],10:[function(require,module,exports){
|
|
|
|
+'use strict';
|
|
|
|
+
|
|
|
|
+Object.defineProperty(exports, '__esModule', {
|
|
|
|
+ value: true
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+function _interopRequireDefault(obj) {
|
|
|
|
+ return obj && obj.__esModule ? obj : { 'default': obj };
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+var _TransloaditPlugin = require('./TransloaditPlugin');
|
|
|
|
+
|
|
|
|
+var _TransloaditPlugin2 = _interopRequireDefault(_TransloaditPlugin);
|
|
|
|
+
|
|
|
|
+var _DragDrop = require('./DragDrop');
|
|
|
|
+
|
|
|
|
+var _DragDrop2 = _interopRequireDefault(_DragDrop);
|
|
|
|
+
|
|
|
|
+var _Dropbox = require('./Dropbox');
|
|
|
|
+
|
|
|
|
+var _Dropbox2 = _interopRequireDefault(_Dropbox);
|
|
|
|
+
|
|
|
|
+var _TransloaditBasic = require('./TransloaditBasic');
|
|
|
|
+
|
|
|
|
+var _TransloaditBasic2 = _interopRequireDefault(_TransloaditBasic);
|
|
|
|
+
|
|
|
|
+var _Tus10 = require('./Tus10');
|
|
|
|
+
|
|
|
|
+var _Tus102 = _interopRequireDefault(_Tus10);
|
|
|
|
+
|
|
|
|
+exports['default'] = {
|
|
|
|
+ TransloaditPlugin: _TransloaditPlugin2['default'],
|
|
|
|
+ DropboxPlugin: _Dropbox2['default'],
|
|
|
|
+ DragDrop: _DragDrop2['default'],
|
|
|
|
+ TransloaditBasic: _TransloaditBasic2['default'],
|
|
|
|
+ Tus10: _Tus102['default']
|
|
|
|
+};
|
|
|
|
+module.exports = exports['default'];
|
|
|
|
+
|
|
|
|
+},{"./DragDrop":5,"./Dropbox":6,"./TransloaditBasic":7,"./TransloaditPlugin":8,"./Tus10":9}],11:[function(require,module,exports){
|
|
|
|
+/**
|
|
|
|
+ * Module dependencies.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+var Emitter = require('emitter');
|
|
|
|
+var reduce = require('reduce');
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Root reference for iframes.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+var root;
|
|
|
|
+if (typeof window !== 'undefined') { // Browser window
|
|
|
|
+ root = window;
|
|
|
|
+} else if (typeof self !== 'undefined') { // Web Worker
|
|
|
|
+ root = self;
|
|
|
|
+} else { // Other environments
|
|
|
|
+ root = this;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Noop.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+function noop(){};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Check if `obj` is a host object,
|
|
|
|
+ * we don't want to serialize these :)
|
|
|
|
+ *
|
|
|
|
+ * TODO: future proof, move to compoent land
|
|
|
|
+ *
|
|
|
|
+ * @param {Object} obj
|
|
|
|
+ * @return {Boolean}
|
|
|
|
+ * @api private
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+function isHost(obj) {
|
|
|
|
+ var str = {}.toString.call(obj);
|
|
|
|
+
|
|
|
|
+ switch (str) {
|
|
|
|
+ case '[object File]':
|
|
|
|
+ case '[object Blob]':
|
|
|
|
+ case '[object FormData]':
|
|
|
|
+ return true;
|
|
|
|
+ default:
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Determine XHR.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+request.getXHR = function () {
|
|
|
|
+ if (root.XMLHttpRequest
|
|
|
|
+ && (!root.location || 'file:' != root.location.protocol
|
|
|
|
+ || !root.ActiveXObject)) {
|
|
|
|
+ return new XMLHttpRequest;
|
|
|
|
+ } else {
|
|
|
|
+ try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch(e) {}
|
|
|
|
+ try { return new ActiveXObject('Msxml2.XMLHTTP.6.0'); } catch(e) {}
|
|
|
|
+ try { return new ActiveXObject('Msxml2.XMLHTTP.3.0'); } catch(e) {}
|
|
|
|
+ try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch(e) {}
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Removes leading and trailing whitespace, added to support IE.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} s
|
|
|
|
+ * @return {String}
|
|
|
|
+ * @api private
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+var trim = ''.trim
|
|
|
|
+ ? function(s) { return s.trim(); }
|
|
|
|
+ : function(s) { return s.replace(/(^\s*|\s*$)/g, ''); };
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Check if `obj` is an object.
|
|
|
|
+ *
|
|
|
|
+ * @param {Object} obj
|
|
|
|
+ * @return {Boolean}
|
|
|
|
+ * @api private
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+function isObject(obj) {
|
|
|
|
+ return obj === Object(obj);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Serialize the given `obj`.
|
|
|
|
+ *
|
|
|
|
+ * @param {Object} obj
|
|
|
|
+ * @return {String}
|
|
|
|
+ * @api private
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+function serialize(obj) {
|
|
|
|
+ if (!isObject(obj)) return obj;
|
|
|
|
+ var pairs = [];
|
|
|
|
+ for (var key in obj) {
|
|
|
|
+ if (null != obj[key]) {
|
|
|
|
+ pairs.push(encodeURIComponent(key)
|
|
|
|
+ + '=' + encodeURIComponent(obj[key]));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return pairs.join('&');
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Expose serialization method.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ request.serializeObject = serialize;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Parse the given x-www-form-urlencoded `str`.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} str
|
|
|
|
+ * @return {Object}
|
|
|
|
+ * @api private
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+function parseString(str) {
|
|
|
|
+ var obj = {};
|
|
|
|
+ var pairs = str.split('&');
|
|
|
|
+ var parts;
|
|
|
|
+ var pair;
|
|
|
|
+
|
|
|
|
+ for (var i = 0, len = pairs.length; i < len; ++i) {
|
|
|
|
+ pair = pairs[i];
|
|
|
|
+ parts = pair.split('=');
|
|
|
|
+ obj[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return obj;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Expose parser.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+request.parseString = parseString;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Default MIME type map.
|
|
|
|
+ *
|
|
|
|
+ * superagent.types.xml = 'application/xml';
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+request.types = {
|
|
|
|
+ html: 'text/html',
|
|
|
|
+ json: 'application/json',
|
|
|
|
+ xml: 'application/xml',
|
|
|
|
+ urlencoded: 'application/x-www-form-urlencoded',
|
|
|
|
+ 'form': 'application/x-www-form-urlencoded',
|
|
|
|
+ 'form-data': 'application/x-www-form-urlencoded'
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Default serialization map.
|
|
|
|
+ *
|
|
|
|
+ * superagent.serialize['application/xml'] = function(obj){
|
|
|
|
+ * return 'generated xml here';
|
|
|
|
+ * };
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ request.serialize = {
|
|
|
|
+ 'application/x-www-form-urlencoded': serialize,
|
|
|
|
+ 'application/json': JSON.stringify
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Default parsers.
|
|
|
|
+ *
|
|
|
|
+ * superagent.parse['application/xml'] = function(str){
|
|
|
|
+ * return { object parsed from str };
|
|
|
|
+ * };
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+request.parse = {
|
|
|
|
+ 'application/x-www-form-urlencoded': parseString,
|
|
|
|
+ 'application/json': JSON.parse
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Parse the given header `str` into
|
|
|
|
+ * an object containing the mapped fields.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} str
|
|
|
|
+ * @return {Object}
|
|
|
|
+ * @api private
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+function parseHeader(str) {
|
|
|
|
+ var lines = str.split(/\r?\n/);
|
|
|
|
+ var fields = {};
|
|
|
|
+ var index;
|
|
|
|
+ var line;
|
|
|
|
+ var field;
|
|
|
|
+ var val;
|
|
|
|
+
|
|
|
|
+ lines.pop(); // trailing CRLF
|
|
|
|
+
|
|
|
|
+ for (var i = 0, len = lines.length; i < len; ++i) {
|
|
|
|
+ line = lines[i];
|
|
|
|
+ index = line.indexOf(':');
|
|
|
|
+ field = line.slice(0, index).toLowerCase();
|
|
|
|
+ val = trim(line.slice(index + 1));
|
|
|
|
+ fields[field] = val;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return fields;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Return the mime type for the given `str`.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} str
|
|
|
|
+ * @return {String}
|
|
|
|
+ * @api private
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+function type(str){
|
|
|
|
+ return str.split(/ *; */).shift();
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Return header field parameters.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} str
|
|
|
|
+ * @return {Object}
|
|
|
|
+ * @api private
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+function params(str){
|
|
|
|
+ return reduce(str.split(/ *; */), function(obj, str){
|
|
|
|
+ var parts = str.split(/ *= */)
|
|
|
|
+ , key = parts.shift()
|
|
|
|
+ , val = parts.shift();
|
|
|
|
+
|
|
|
|
+ if (key && val) obj[key] = val;
|
|
|
|
+ return obj;
|
|
|
|
+ }, {});
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Initialize a new `Response` with the given `xhr`.
|
|
|
|
+ *
|
|
|
|
+ * - set flags (.ok, .error, etc)
|
|
|
|
+ * - parse header
|
|
|
|
+ *
|
|
|
|
+ * Examples:
|
|
|
|
+ *
|
|
|
|
+ * Aliasing `superagent` as `request` is nice:
|
|
|
|
+ *
|
|
|
|
+ * request = superagent;
|
|
|
|
+ *
|
|
|
|
+ * We can use the promise-like API, or pass callbacks:
|
|
|
|
+ *
|
|
|
|
+ * request.get('/').end(function(res){});
|
|
|
|
+ * request.get('/', function(res){});
|
|
|
|
+ *
|
|
|
|
+ * Sending data can be chained:
|
|
|
|
+ *
|
|
|
|
+ * request
|
|
|
|
+ * .post('/user')
|
|
|
|
+ * .send({ name: 'tj' })
|
|
|
|
+ * .end(function(res){});
|
|
|
|
+ *
|
|
|
|
+ * Or passed to `.send()`:
|
|
|
|
+ *
|
|
|
|
+ * request
|
|
|
|
+ * .post('/user')
|
|
|
|
+ * .send({ name: 'tj' }, function(res){});
|
|
|
|
+ *
|
|
|
|
+ * Or passed to `.post()`:
|
|
|
|
+ *
|
|
|
|
+ * request
|
|
|
|
+ * .post('/user', { name: 'tj' })
|
|
|
|
+ * .end(function(res){});
|
|
|
|
+ *
|
|
|
|
+ * Or further reduced to a single call for simple cases:
|
|
|
|
+ *
|
|
|
|
+ * request
|
|
|
|
+ * .post('/user', { name: 'tj' }, function(res){});
|
|
|
|
+ *
|
|
|
|
+ * @param {XMLHTTPRequest} xhr
|
|
|
|
+ * @param {Object} options
|
|
|
|
+ * @api private
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+function Response(req, options) {
|
|
|
|
+ options = options || {};
|
|
|
|
+ this.req = req;
|
|
|
|
+ this.xhr = this.req.xhr;
|
|
|
|
+ // responseText is accessible only if responseType is '' or 'text' and on older browsers
|
|
|
|
+ this.text = ((this.req.method !='HEAD' && (this.xhr.responseType === '' || this.xhr.responseType === 'text')) || typeof this.xhr.responseType === 'undefined')
|
|
|
|
+ ? this.xhr.responseText
|
|
|
|
+ : null;
|
|
|
|
+ this.statusText = this.req.xhr.statusText;
|
|
|
|
+ this.setStatusProperties(this.xhr.status);
|
|
|
|
+ this.header = this.headers = parseHeader(this.xhr.getAllResponseHeaders());
|
|
|
|
+ // getAllResponseHeaders sometimes falsely returns "" for CORS requests, but
|
|
|
|
+ // getResponseHeader still works. so we get content-type even if getting
|
|
|
|
+ // other headers fails.
|
|
|
|
+ this.header['content-type'] = this.xhr.getResponseHeader('content-type');
|
|
|
|
+ this.setHeaderProperties(this.header);
|
|
|
|
+ this.body = this.req.method != 'HEAD'
|
|
|
|
+ ? this.parseBody(this.text ? this.text : this.xhr.response)
|
|
|
|
+ : null;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Get case-insensitive `field` value.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} field
|
|
|
|
+ * @return {String}
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Response.prototype.get = function(field){
|
|
|
|
+ return this.header[field.toLowerCase()];
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Set header related properties:
|
|
|
|
+ *
|
|
|
|
+ * - `.type` the content type without params
|
|
|
|
+ *
|
|
|
|
+ * A response of "Content-Type: text/plain; charset=utf-8"
|
|
|
|
+ * will provide you with a `.type` of "text/plain".
|
|
|
|
+ *
|
|
|
|
+ * @param {Object} header
|
|
|
|
+ * @api private
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Response.prototype.setHeaderProperties = function(header){
|
|
|
|
+ // content-type
|
|
|
|
+ var ct = this.header['content-type'] || '';
|
|
|
|
+ this.type = type(ct);
|
|
|
|
+
|
|
|
|
+ // params
|
|
|
|
+ var obj = params(ct);
|
|
|
|
+ for (var key in obj) this[key] = obj[key];
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Force given parser
|
|
|
|
+ *
|
|
|
|
+ * Sets the body parser no matter type.
|
|
|
|
+ *
|
|
|
|
+ * @param {Function}
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Response.prototype.parse = function(fn){
|
|
|
|
+ this.parser = fn;
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Parse the given body `str`.
|
|
|
|
+ *
|
|
|
|
+ * Used for auto-parsing of bodies. Parsers
|
|
|
|
+ * are defined on the `superagent.parse` object.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} str
|
|
|
|
+ * @return {Mixed}
|
|
|
|
+ * @api private
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Response.prototype.parseBody = function(str){
|
|
|
|
+ var parse = this.parser || request.parse[this.type];
|
|
|
|
+ return parse && str && (str.length || str instanceof Object)
|
|
|
|
+ ? parse(str)
|
|
|
|
+ : null;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Set flags such as `.ok` based on `status`.
|
|
|
|
+ *
|
|
|
|
+ * For example a 2xx response will give you a `.ok` of __true__
|
|
|
|
+ * whereas 5xx will be __false__ and `.error` will be __true__. The
|
|
|
|
+ * `.clientError` and `.serverError` are also available to be more
|
|
|
|
+ * specific, and `.statusType` is the class of error ranging from 1..5
|
|
|
|
+ * sometimes useful for mapping respond colors etc.
|
|
|
|
+ *
|
|
|
|
+ * "sugar" properties are also defined for common cases. Currently providing:
|
|
|
|
+ *
|
|
|
|
+ * - .noContent
|
|
|
|
+ * - .badRequest
|
|
|
|
+ * - .unauthorized
|
|
|
|
+ * - .notAcceptable
|
|
|
|
+ * - .notFound
|
|
|
|
+ *
|
|
|
|
+ * @param {Number} status
|
|
|
|
+ * @api private
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Response.prototype.setStatusProperties = function(status){
|
|
|
|
+ // handle IE9 bug: http://stackoverflow.com/questions/10046972/msie-returns-status-code-of-1223-for-ajax-request
|
|
|
|
+ if (status === 1223) {
|
|
|
|
+ status = 204;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var type = status / 100 | 0;
|
|
|
|
+
|
|
|
|
+ // status / class
|
|
|
|
+ this.status = this.statusCode = status;
|
|
|
|
+ this.statusType = type;
|
|
|
|
+
|
|
|
|
+ // basics
|
|
|
|
+ this.info = 1 == type;
|
|
|
|
+ this.ok = 2 == type;
|
|
|
|
+ this.clientError = 4 == type;
|
|
|
|
+ this.serverError = 5 == type;
|
|
|
|
+ this.error = (4 == type || 5 == type)
|
|
|
|
+ ? this.toError()
|
|
|
|
+ : false;
|
|
|
|
+
|
|
|
|
+ // sugar
|
|
|
|
+ this.accepted = 202 == status;
|
|
|
|
+ this.noContent = 204 == status;
|
|
|
|
+ this.badRequest = 400 == status;
|
|
|
|
+ this.unauthorized = 401 == status;
|
|
|
|
+ this.notAcceptable = 406 == status;
|
|
|
|
+ this.notFound = 404 == status;
|
|
|
|
+ this.forbidden = 403 == status;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Return an `Error` representative of this response.
|
|
|
|
+ *
|
|
|
|
+ * @return {Error}
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Response.prototype.toError = function(){
|
|
|
|
+ var req = this.req;
|
|
|
|
+ var method = req.method;
|
|
|
|
+ var url = req.url;
|
|
|
|
+
|
|
|
|
+ var msg = 'cannot ' + method + ' ' + url + ' (' + this.status + ')';
|
|
|
|
+ var err = new Error(msg);
|
|
|
|
+ err.status = this.status;
|
|
|
|
+ err.method = method;
|
|
|
|
+ err.url = url;
|
|
|
|
+
|
|
|
|
+ return err;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Expose `Response`.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+request.Response = Response;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Initialize a new `Request` with the given `method` and `url`.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} method
|
|
|
|
+ * @param {String} url
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+function Request(method, url) {
|
|
|
|
+ var self = this;
|
|
|
|
+ Emitter.call(this);
|
|
|
|
+ this._query = this._query || [];
|
|
|
|
+ this.method = method;
|
|
|
|
+ this.url = url;
|
|
|
|
+ this.header = {};
|
|
|
|
+ this._header = {};
|
|
|
|
+ this.on('end', function(){
|
|
|
|
+ var err = null;
|
|
|
|
+ var res = null;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ res = new Response(self);
|
|
|
|
+ } catch(e) {
|
|
|
|
+ err = new Error('Parser is unable to parse the response');
|
|
|
|
+ err.parse = true;
|
|
|
|
+ err.original = e;
|
|
|
|
+ return self.callback(err);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ self.emit('response', res);
|
|
|
|
+
|
|
|
|
+ if (err) {
|
|
|
|
+ return self.callback(err, res);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (res.status >= 200 && res.status < 300) {
|
|
|
|
+ return self.callback(err, res);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var new_err = new Error(res.statusText || 'Unsuccessful HTTP response');
|
|
|
|
+ new_err.original = err;
|
|
|
|
+ new_err.response = res;
|
|
|
|
+ new_err.status = res.status;
|
|
|
|
+
|
|
|
|
+ self.callback(new_err, res);
|
|
|
|
+ });
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Mixin `Emitter`.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Emitter(Request.prototype);
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Allow for extension
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.use = function(fn) {
|
|
|
|
+ fn(this);
|
|
|
|
+ return this;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Set timeout to `ms`.
|
|
|
|
+ *
|
|
|
|
+ * @param {Number} ms
|
|
|
|
+ * @return {Request} for chaining
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.timeout = function(ms){
|
|
|
|
+ this._timeout = ms;
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Clear previous timeout.
|
|
|
|
+ *
|
|
|
|
+ * @return {Request} for chaining
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.clearTimeout = function(){
|
|
|
|
+ this._timeout = 0;
|
|
|
|
+ clearTimeout(this._timer);
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Abort the request, and clear potential timeout.
|
|
|
|
+ *
|
|
|
|
+ * @return {Request}
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.abort = function(){
|
|
|
|
+ if (this.aborted) return;
|
|
|
|
+ this.aborted = true;
|
|
|
|
+ this.xhr.abort();
|
|
|
|
+ this.clearTimeout();
|
|
|
|
+ this.emit('abort');
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Set header `field` to `val`, or multiple fields with one object.
|
|
|
|
+ *
|
|
|
|
+ * Examples:
|
|
|
|
+ *
|
|
|
|
+ * req.get('/')
|
|
|
|
+ * .set('Accept', 'application/json')
|
|
|
|
+ * .set('X-API-Key', 'foobar')
|
|
|
|
+ * .end(callback);
|
|
|
|
+ *
|
|
|
|
+ * req.get('/')
|
|
|
|
+ * .set({ Accept: 'application/json', 'X-API-Key': 'foobar' })
|
|
|
|
+ * .end(callback);
|
|
|
|
+ *
|
|
|
|
+ * @param {String|Object} field
|
|
|
|
+ * @param {String} val
|
|
|
|
+ * @return {Request} for chaining
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.set = function(field, val){
|
|
|
|
+ if (isObject(field)) {
|
|
|
|
+ for (var key in field) {
|
|
|
|
+ this.set(key, field[key]);
|
|
|
|
+ }
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+ this._header[field.toLowerCase()] = val;
|
|
|
|
+ this.header[field] = val;
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Remove header `field`.
|
|
|
|
+ *
|
|
|
|
+ * Example:
|
|
|
|
+ *
|
|
|
|
+ * req.get('/')
|
|
|
|
+ * .unset('User-Agent')
|
|
|
|
+ * .end(callback);
|
|
|
|
+ *
|
|
|
|
+ * @param {String} field
|
|
|
|
+ * @return {Request} for chaining
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.unset = function(field){
|
|
|
|
+ delete this._header[field.toLowerCase()];
|
|
|
|
+ delete this.header[field];
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Get case-insensitive header `field` value.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} field
|
|
|
|
+ * @return {String}
|
|
|
|
+ * @api private
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.getHeader = function(field){
|
|
|
|
+ return this._header[field.toLowerCase()];
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Set Content-Type to `type`, mapping values from `request.types`.
|
|
|
|
+ *
|
|
|
|
+ * Examples:
|
|
|
|
+ *
|
|
|
|
+ * superagent.types.xml = 'application/xml';
|
|
|
|
+ *
|
|
|
|
+ * request.post('/')
|
|
|
|
+ * .type('xml')
|
|
|
|
+ * .send(xmlstring)
|
|
|
|
+ * .end(callback);
|
|
|
|
+ *
|
|
|
|
+ * request.post('/')
|
|
|
|
+ * .type('application/xml')
|
|
|
|
+ * .send(xmlstring)
|
|
|
|
+ * .end(callback);
|
|
|
|
+ *
|
|
|
|
+ * @param {String} type
|
|
|
|
+ * @return {Request} for chaining
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.type = function(type){
|
|
|
|
+ this.set('Content-Type', request.types[type] || type);
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Set Accept to `type`, mapping values from `request.types`.
|
|
|
|
+ *
|
|
|
|
+ * Examples:
|
|
|
|
+ *
|
|
|
|
+ * superagent.types.json = 'application/json';
|
|
|
|
+ *
|
|
|
|
+ * request.get('/agent')
|
|
|
|
+ * .accept('json')
|
|
|
|
+ * .end(callback);
|
|
|
|
+ *
|
|
|
|
+ * request.get('/agent')
|
|
|
|
+ * .accept('application/json')
|
|
|
|
+ * .end(callback);
|
|
|
|
+ *
|
|
|
|
+ * @param {String} accept
|
|
|
|
+ * @return {Request} for chaining
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.accept = function(type){
|
|
|
|
+ this.set('Accept', request.types[type] || type);
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Set Authorization field value with `user` and `pass`.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} user
|
|
|
|
+ * @param {String} pass
|
|
|
|
+ * @return {Request} for chaining
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.auth = function(user, pass){
|
|
|
|
+ var str = btoa(user + ':' + pass);
|
|
|
|
+ this.set('Authorization', 'Basic ' + str);
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+* Add query-string `val`.
|
|
|
|
+*
|
|
|
|
+* Examples:
|
|
|
|
+*
|
|
|
|
+* request.get('/shoes')
|
|
|
|
+* .query('size=10')
|
|
|
|
+* .query({ color: 'blue' })
|
|
|
|
+*
|
|
|
|
+* @param {Object|String} val
|
|
|
|
+* @return {Request} for chaining
|
|
|
|
+* @api public
|
|
|
|
+*/
|
|
|
|
+
|
|
|
|
+Request.prototype.query = function(val){
|
|
|
|
+ if ('string' != typeof val) val = serialize(val);
|
|
|
|
+ if (val) this._query.push(val);
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Write the field `name` and `val` for "multipart/form-data"
|
|
|
|
+ * request bodies.
|
|
|
|
+ *
|
|
|
|
+ * ``` js
|
|
|
|
+ * request.post('/upload')
|
|
|
|
+ * .field('foo', 'bar')
|
|
|
|
+ * .end(callback);
|
|
|
|
+ * ```
|
|
|
|
+ *
|
|
|
|
+ * @param {String} name
|
|
|
|
+ * @param {String|Blob|File} val
|
|
|
|
+ * @return {Request} for chaining
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.field = function(name, val){
|
|
|
|
+ if (!this._formData) this._formData = new root.FormData();
|
|
|
|
+ this._formData.append(name, val);
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Queue the given `file` as an attachment to the specified `field`,
|
|
|
|
+ * with optional `filename`.
|
|
|
|
+ *
|
|
|
|
+ * ``` js
|
|
|
|
+ * request.post('/upload')
|
|
|
|
+ * .attach(new Blob(['<a id="a"><b id="b">hey!</b></a>'], { type: "text/html"}))
|
|
|
|
+ * .end(callback);
|
|
|
|
+ * ```
|
|
|
|
+ *
|
|
|
|
+ * @param {String} field
|
|
|
|
+ * @param {Blob|File} file
|
|
|
|
+ * @param {String} filename
|
|
|
|
+ * @return {Request} for chaining
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.attach = function(field, file, filename){
|
|
|
|
+ if (!this._formData) this._formData = new root.FormData();
|
|
|
|
+ this._formData.append(field, file, filename);
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Send `data`, defaulting the `.type()` to "json" when
|
|
|
|
+ * an object is given.
|
|
|
|
+ *
|
|
|
|
+ * Examples:
|
|
|
|
+ *
|
|
|
|
+ * // querystring
|
|
|
|
+ * request.get('/search')
|
|
|
|
+ * .end(callback)
|
|
|
|
+ *
|
|
|
|
+ * // multiple data "writes"
|
|
|
|
+ * request.get('/search')
|
|
|
|
+ * .send({ search: 'query' })
|
|
|
|
+ * .send({ range: '1..5' })
|
|
|
|
+ * .send({ order: 'desc' })
|
|
|
|
+ * .end(callback)
|
|
|
|
+ *
|
|
|
|
+ * // manual json
|
|
|
|
+ * request.post('/user')
|
|
|
|
+ * .type('json')
|
|
|
|
+ * .send('{"name":"tj"})
|
|
|
|
+ * .end(callback)
|
|
|
|
+ *
|
|
|
|
+ * // auto json
|
|
|
|
+ * request.post('/user')
|
|
|
|
+ * .send({ name: 'tj' })
|
|
|
|
+ * .end(callback)
|
|
|
|
+ *
|
|
|
|
+ * // manual x-www-form-urlencoded
|
|
|
|
+ * request.post('/user')
|
|
|
|
+ * .type('form')
|
|
|
|
+ * .send('name=tj')
|
|
|
|
+ * .end(callback)
|
|
|
|
+ *
|
|
|
|
+ * // auto x-www-form-urlencoded
|
|
|
|
+ * request.post('/user')
|
|
|
|
+ * .type('form')
|
|
|
|
+ * .send({ name: 'tj' })
|
|
|
|
+ * .end(callback)
|
|
|
|
+ *
|
|
|
|
+ * // defaults to x-www-form-urlencoded
|
|
|
|
+ * request.post('/user')
|
|
|
|
+ * .send('name=tobi')
|
|
|
|
+ * .send('species=ferret')
|
|
|
|
+ * .end(callback)
|
|
|
|
+ *
|
|
|
|
+ * @param {String|Object} data
|
|
|
|
+ * @return {Request} for chaining
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.send = function(data){
|
|
|
|
+ var obj = isObject(data);
|
|
|
|
+ var type = this.getHeader('Content-Type');
|
|
|
|
+
|
|
|
|
+ // merge
|
|
|
|
+ if (obj && isObject(this._data)) {
|
|
|
|
+ for (var key in data) {
|
|
|
|
+ this._data[key] = data[key];
|
|
|
|
+ }
|
|
|
|
+ } else if ('string' == typeof data) {
|
|
|
|
+ if (!type) this.type('form');
|
|
|
|
+ type = this.getHeader('Content-Type');
|
|
|
|
+ if ('application/x-www-form-urlencoded' == type) {
|
|
|
|
+ this._data = this._data
|
|
|
|
+ ? this._data + '&' + data
|
|
|
|
+ : data;
|
|
|
|
+ } else {
|
|
|
|
+ this._data = (this._data || '') + data;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ this._data = data;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!obj || isHost(data)) return this;
|
|
|
|
+ if (!type) this.type('json');
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Invoke the callback with `err` and `res`
|
|
|
|
+ * and handle arity check.
|
|
|
|
+ *
|
|
|
|
+ * @param {Error} err
|
|
|
|
+ * @param {Response} res
|
|
|
|
+ * @api private
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.callback = function(err, res){
|
|
|
|
+ var fn = this._callback;
|
|
|
|
+ this.clearTimeout();
|
|
|
|
+ fn(err, res);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Invoke callback with x-domain error.
|
|
|
|
+ *
|
|
|
|
+ * @api private
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.crossDomainError = function(){
|
|
|
|
+ var err = new Error('Origin is not allowed by Access-Control-Allow-Origin');
|
|
|
|
+ err.crossDomain = true;
|
|
|
|
+ this.callback(err);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Invoke callback with timeout error.
|
|
|
|
+ *
|
|
|
|
+ * @api private
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.timeoutError = function(){
|
|
|
|
+ var timeout = this._timeout;
|
|
|
|
+ var err = new Error('timeout of ' + timeout + 'ms exceeded');
|
|
|
|
+ err.timeout = timeout;
|
|
|
|
+ this.callback(err);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Enable transmission of cookies with x-domain requests.
|
|
|
|
+ *
|
|
|
|
+ * Note that for this to work the origin must not be
|
|
|
|
+ * using "Access-Control-Allow-Origin" with a wildcard,
|
|
|
|
+ * and also must set "Access-Control-Allow-Credentials"
|
|
|
|
+ * to "true".
|
|
|
|
+ *
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.withCredentials = function(){
|
|
|
|
+ this._withCredentials = true;
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Initiate request, invoking callback `fn(res)`
|
|
|
|
+ * with an instanceof `Response`.
|
|
|
|
+ *
|
|
|
|
+ * @param {Function} fn
|
|
|
|
+ * @return {Request} for chaining
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.end = function(fn){
|
|
|
|
+ var self = this;
|
|
|
|
+ var xhr = this.xhr = request.getXHR();
|
|
|
|
+ var query = this._query.join('&');
|
|
|
|
+ var timeout = this._timeout;
|
|
|
|
+ var data = this._formData || this._data;
|
|
|
|
+
|
|
|
|
+ // store callback
|
|
|
|
+ this._callback = fn || noop;
|
|
|
|
+
|
|
|
|
+ // state change
|
|
|
|
+ xhr.onreadystatechange = function(){
|
|
|
|
+ if (4 != xhr.readyState) return;
|
|
|
|
+
|
|
|
|
+ // In IE9, reads to any property (e.g. status) off of an aborted XHR will
|
|
|
|
+ // result in the error "Could not complete the operation due to error c00c023f"
|
|
|
|
+ var status;
|
|
|
|
+ try { status = xhr.status } catch(e) { status = 0; }
|
|
|
|
+
|
|
|
|
+ if (0 == status) {
|
|
|
|
+ if (self.timedout) return self.timeoutError();
|
|
|
|
+ if (self.aborted) return;
|
|
|
|
+ return self.crossDomainError();
|
|
|
|
+ }
|
|
|
|
+ self.emit('end');
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ // progress
|
|
|
|
+ var handleProgress = function(e){
|
|
|
|
+ if (e.total > 0) {
|
|
|
|
+ e.percent = e.loaded / e.total * 100;
|
|
|
|
+ }
|
|
|
|
+ self.emit('progress', e);
|
|
|
|
+ };
|
|
|
|
+ if (this.hasListeners('progress')) {
|
|
|
|
+ xhr.onprogress = handleProgress;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ if (xhr.upload && this.hasListeners('progress')) {
|
|
|
|
+ xhr.upload.onprogress = handleProgress;
|
|
|
|
+ }
|
|
|
|
+ } catch(e) {
|
|
|
|
+ // Accessing xhr.upload fails in IE from a web worker, so just pretend it doesn't exist.
|
|
|
|
+ // Reported here:
|
|
|
|
+ // https://connect.microsoft.com/IE/feedback/details/837245/xmlhttprequest-upload-throws-invalid-argument-when-used-from-web-worker-context
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // timeout
|
|
|
|
+ if (timeout && !this._timer) {
|
|
|
|
+ this._timer = setTimeout(function(){
|
|
|
|
+ self.timedout = true;
|
|
|
|
+ self.abort();
|
|
|
|
+ }, timeout);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // querystring
|
|
|
|
+ if (query) {
|
|
|
|
+ query = request.serializeObject(query);
|
|
|
|
+ this.url += ~this.url.indexOf('?')
|
|
|
|
+ ? '&' + query
|
|
|
|
+ : '?' + query;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // initiate request
|
|
|
|
+ xhr.open(this.method, this.url, true);
|
|
|
|
+
|
|
|
|
+ // CORS
|
|
|
|
+ if (this._withCredentials) xhr.withCredentials = true;
|
|
|
|
+
|
|
|
|
+ // body
|
|
|
|
+ if ('GET' != this.method && 'HEAD' != this.method && 'string' != typeof data && !isHost(data)) {
|
|
|
|
+ // serialize stuff
|
|
|
|
+ var contentType = this.getHeader('Content-Type');
|
|
|
|
+ var serialize = request.serialize[contentType ? contentType.split(';')[0] : ''];
|
|
|
|
+ if (serialize) data = serialize(data);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // set header fields
|
|
|
|
+ for (var field in this.header) {
|
|
|
|
+ if (null == this.header[field]) continue;
|
|
|
|
+ xhr.setRequestHeader(field, this.header[field]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // send stuff
|
|
|
|
+ this.emit('request', this);
|
|
|
|
+ xhr.send(data);
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Faux promise support
|
|
|
|
+ *
|
|
|
|
+ * @param {Function} fulfill
|
|
|
|
+ * @param {Function} reject
|
|
|
|
+ * @return {Request}
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Request.prototype.then = function (fulfill, reject) {
|
|
|
|
+ return this.end(function(err, res) {
|
|
|
|
+ err ? reject(err) : fulfill(res);
|
|
|
|
+ });
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Expose `Request`.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+request.Request = Request;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Issue a request:
|
|
|
|
+ *
|
|
|
|
+ * Examples:
|
|
|
|
+ *
|
|
|
|
+ * request('GET', '/users').end(callback)
|
|
|
|
+ * request('/users').end(callback)
|
|
|
|
+ * request('/users', callback)
|
|
|
|
+ *
|
|
|
|
+ * @param {String} method
|
|
|
|
+ * @param {String|Function} url or callback
|
|
|
|
+ * @return {Request}
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+function request(method, url) {
|
|
|
|
+ // callback
|
|
|
|
+ if ('function' == typeof url) {
|
|
|
|
+ return new Request('GET', method).end(url);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // url first
|
|
|
|
+ if (1 == arguments.length) {
|
|
|
|
+ return new Request('GET', method);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return new Request(method, url);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * GET `url` with optional callback `fn(res)`.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} url
|
|
|
|
+ * @param {Mixed|Function} data or fn
|
|
|
|
+ * @param {Function} fn
|
|
|
|
+ * @return {Request}
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+request.get = function(url, data, fn){
|
|
|
|
+ var req = request('GET', url);
|
|
|
|
+ if ('function' == typeof data) fn = data, data = null;
|
|
|
|
+ if (data) req.query(data);
|
|
|
|
+ if (fn) req.end(fn);
|
|
|
|
+ return req;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * HEAD `url` with optional callback `fn(res)`.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} url
|
|
|
|
+ * @param {Mixed|Function} data or fn
|
|
|
|
+ * @param {Function} fn
|
|
|
|
+ * @return {Request}
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+request.head = function(url, data, fn){
|
|
|
|
+ var req = request('HEAD', url);
|
|
|
|
+ if ('function' == typeof data) fn = data, data = null;
|
|
|
|
+ if (data) req.send(data);
|
|
|
|
+ if (fn) req.end(fn);
|
|
|
|
+ return req;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * DELETE `url` with optional callback `fn(res)`.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} url
|
|
|
|
+ * @param {Function} fn
|
|
|
|
+ * @return {Request}
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+request.del = function(url, fn){
|
|
|
|
+ var req = request('DELETE', url);
|
|
|
|
+ if (fn) req.end(fn);
|
|
|
|
+ return req;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * PATCH `url` with optional `data` and callback `fn(res)`.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} url
|
|
|
|
+ * @param {Mixed} data
|
|
|
|
+ * @param {Function} fn
|
|
|
|
+ * @return {Request}
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+request.patch = function(url, data, fn){
|
|
|
|
+ var req = request('PATCH', url);
|
|
|
|
+ if ('function' == typeof data) fn = data, data = null;
|
|
|
|
+ if (data) req.send(data);
|
|
|
|
+ if (fn) req.end(fn);
|
|
|
|
+ return req;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * POST `url` with optional `data` and callback `fn(res)`.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} url
|
|
|
|
+ * @param {Mixed} data
|
|
|
|
+ * @param {Function} fn
|
|
|
|
+ * @return {Request}
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+request.post = function(url, data, fn){
|
|
|
|
+ var req = request('POST', url);
|
|
|
|
+ if ('function' == typeof data) fn = data, data = null;
|
|
|
|
+ if (data) req.send(data);
|
|
|
|
+ if (fn) req.end(fn);
|
|
|
|
+ return req;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * PUT `url` with optional `data` and callback `fn(res)`.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} url
|
|
|
|
+ * @param {Mixed|Function} data or fn
|
|
|
|
+ * @param {Function} fn
|
|
|
|
+ * @return {Request}
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+request.put = function(url, data, fn){
|
|
|
|
+ var req = request('PUT', url);
|
|
|
|
+ if ('function' == typeof data) fn = data, data = null;
|
|
|
|
+ if (data) req.send(data);
|
|
|
|
+ if (fn) req.end(fn);
|
|
|
|
+ return req;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Expose `request`.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+module.exports = request;
|
|
|
|
+
|
|
|
|
+},{"emitter":12,"reduce":13}],12:[function(require,module,exports){
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Expose `Emitter`.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+module.exports = Emitter;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Initialize a new `Emitter`.
|
|
|
|
+ *
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+function Emitter(obj) {
|
|
|
|
+ if (obj) return mixin(obj);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Mixin the emitter properties.
|
|
|
|
+ *
|
|
|
|
+ * @param {Object} obj
|
|
|
|
+ * @return {Object}
|
|
|
|
+ * @api private
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+function mixin(obj) {
|
|
|
|
+ for (var key in Emitter.prototype) {
|
|
|
|
+ obj[key] = Emitter.prototype[key];
|
|
|
|
+ }
|
|
|
|
+ return obj;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Listen on the given `event` with `fn`.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} event
|
|
|
|
+ * @param {Function} fn
|
|
|
|
+ * @return {Emitter}
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Emitter.prototype.on =
|
|
|
|
+Emitter.prototype.addEventListener = function(event, fn){
|
|
|
|
+ this._callbacks = this._callbacks || {};
|
|
|
|
+ (this._callbacks[event] = this._callbacks[event] || [])
|
|
|
|
+ .push(fn);
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Adds an `event` listener that will be invoked a single
|
|
|
|
+ * time then automatically removed.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} event
|
|
|
|
+ * @param {Function} fn
|
|
|
|
+ * @return {Emitter}
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Emitter.prototype.once = function(event, fn){
|
|
|
|
+ var self = this;
|
|
|
|
+ this._callbacks = this._callbacks || {};
|
|
|
|
+
|
|
|
|
+ function on() {
|
|
|
|
+ self.off(event, on);
|
|
|
|
+ fn.apply(this, arguments);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ on.fn = fn;
|
|
|
|
+ this.on(event, on);
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Remove the given callback for `event` or all
|
|
|
|
+ * registered callbacks.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} event
|
|
|
|
+ * @param {Function} fn
|
|
|
|
+ * @return {Emitter}
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Emitter.prototype.off =
|
|
|
|
+Emitter.prototype.removeListener =
|
|
|
|
+Emitter.prototype.removeAllListeners =
|
|
|
|
+Emitter.prototype.removeEventListener = function(event, fn){
|
|
|
|
+ this._callbacks = this._callbacks || {};
|
|
|
|
+
|
|
|
|
+ // all
|
|
|
|
+ if (0 == arguments.length) {
|
|
|
|
+ this._callbacks = {};
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // specific event
|
|
|
|
+ var callbacks = this._callbacks[event];
|
|
|
|
+ if (!callbacks) return this;
|
|
|
|
+
|
|
|
|
+ // remove all handlers
|
|
|
|
+ if (1 == arguments.length) {
|
|
|
|
+ delete this._callbacks[event];
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // remove specific handler
|
|
|
|
+ var cb;
|
|
|
|
+ for (var i = 0; i < callbacks.length; i++) {
|
|
|
|
+ cb = callbacks[i];
|
|
|
|
+ if (cb === fn || cb.fn === fn) {
|
|
|
|
+ callbacks.splice(i, 1);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Emit `event` with the given args.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} event
|
|
|
|
+ * @param {Mixed} ...
|
|
|
|
+ * @return {Emitter}
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Emitter.prototype.emit = function(event){
|
|
|
|
+ this._callbacks = this._callbacks || {};
|
|
|
|
+ var args = [].slice.call(arguments, 1)
|
|
|
|
+ , callbacks = this._callbacks[event];
|
|
|
|
+
|
|
|
|
+ if (callbacks) {
|
|
|
|
+ callbacks = callbacks.slice(0);
|
|
|
|
+ for (var i = 0, len = callbacks.length; i < len; ++i) {
|
|
|
|
+ callbacks[i].apply(this, args);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return this;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Return array of callbacks for `event`.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} event
|
|
|
|
+ * @return {Array}
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Emitter.prototype.listeners = function(event){
|
|
|
|
+ this._callbacks = this._callbacks || {};
|
|
|
|
+ return this._callbacks[event] || [];
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Check if this emitter has `event` handlers.
|
|
|
|
+ *
|
|
|
|
+ * @param {String} event
|
|
|
|
+ * @return {Boolean}
|
|
|
|
+ * @api public
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+Emitter.prototype.hasListeners = function(event){
|
|
|
|
+ return !! this.listeners(event).length;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+},{}],13:[function(require,module,exports){
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Reduce `arr` with `fn`.
|
|
|
|
+ *
|
|
|
|
+ * @param {Array} arr
|
|
|
|
+ * @param {Function} fn
|
|
|
|
+ * @param {Mixed} initial
|
|
|
|
+ *
|
|
|
|
+ * TODO: combatible error handling?
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+module.exports = function(arr, fn, initial){
|
|
|
|
+ var idx = 0;
|
|
|
|
+ var len = arr.length;
|
|
|
|
+ var curr = arguments.length == 3
|
|
|
|
+ ? initial
|
|
|
|
+ : arr[idx++];
|
|
|
|
+
|
|
|
|
+ while (idx < len) {
|
|
|
|
+ curr = fn.call(null, curr, arr[idx], ++idx, arr);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return curr;
|
|
|
|
+};
|
|
|
|
+},{}],14:[function(require,module,exports){
|
|
|
|
+'use strict';
|
|
|
|
+
|
|
|
|
+module.exports = require('./lib/plugins');
|
|
|
|
+
|
|
|
|
+},{"./lib/plugins":10}]},{},[2]);
|