diff --git a/index.js b/index.js index fd18c96..0da411d 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ (function() { 'use strict'; - module.exports = require('./lib/xmlhttprequest'); + module.exports = require('./lib'); })(); diff --git a/lib/anonxmlhttprequest.js b/lib/anonxmlhttprequest.js new file mode 100644 index 0000000..7bc3d80 --- /dev/null +++ b/lib/anonxmlhttprequest.js @@ -0,0 +1,14 @@ +(function() { + 'use strict'; + + var XMLHttpRequest = require('./xmlhttprequest'); + + function AnonXMLHttpRequest() { + if (!(this instanceof AnonXMLHttpRequest)) { + throw new TypeError('DOM object constructor cannot be called as a function'); + } + XMLHttpRequest.call(this); + } + AnonXMLHttpRequest.prototype = Object.create(XMLHttpRequest.prototype); + module.exports = AnonXMLHttpRequest; +})(); diff --git a/lib/eventtarget.js b/lib/eventtarget.js new file mode 100644 index 0000000..16a711b --- /dev/null +++ b/lib/eventtarget.js @@ -0,0 +1,30 @@ +(function() { + 'use strict'; + + function EventTarget() { + Object.defineProperty(this, '_eventListeners', { + value: {}, + writable: true, + enumerable: false, + configurable: false + }); + } + module.exports = EventTarget; + + (function(proto) { + proto.addEventListener = function addEventListener(type, listener, useCapture) { + this['on' + type] = listener; + }; + + proto.dispatchEvent = function dispatchEvent(event) { + var _listener = this['on' + event.type]; + if (typeof(_listener) === 'function') { + _listener.call(this, event); + } + }; + + proto.removeEventListener = function removeEventListener(type, listener, useCapture) { + // todo + }; + })(EventTarget.prototype); +})(); diff --git a/lib/formdata.js b/lib/formdata.js new file mode 100644 index 0000000..de77305 --- /dev/null +++ b/lib/formdata.js @@ -0,0 +1,16 @@ +(function() { + 'use strict'; + + function FormData() { + if (!(this instanceof FormData)) { + throw new TypeError('DOM object constructor cannot be called as a function.'); + } + } + module.exports = FormData; + + (function(proto) { + proto.append = function append(name, value, filename) { + // todo + }; + })(FormData.prototype); +})(); diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..dd7f76f --- /dev/null +++ b/lib/index.js @@ -0,0 +1,15 @@ +(function() { + 'use strict'; + + var XMLHttpRequestUpload = require('./xmlhttprequestupload'); + var XMLHttpRequest = require('./xmlhttprequest'); + var AnonXMLHttpRequest = require('./anonxmlhttprequest'); + var FormData = require('./formdata'); + + module.exports = { + XMLHttpRequestUpload: XMLHttpRequestUpload, + XMLHttpRequest: XMLHttpRequest, + AnonXMLHttpRequest: AnonXMLHttpRequest, + FormData: FormData + }; +})(); diff --git a/lib/xmlhttprequest.js b/lib/xmlhttprequest.js index e067005..a1be843 100644 --- a/lib/xmlhttprequest.js +++ b/lib/xmlhttprequest.js @@ -4,88 +4,9 @@ var http = require('http'); var https = require('https'); var urlparse = require('url').parse; - - function EventTarget() { - Object.defineProperty(this, '_eventListeners', { - value: {}, - writable: true, - enumerable: false, - configurable: false - }); - } - - (function(proto) { - proto.addEventListener = function addEventListener(type, listener, useCapture) { - this['on' + type] = listener; - }; - - proto.dispatchEvent = function dispatchEvent(event) { - var _listener = this['on' + event.type]; - if (typeof(_listener) === 'function') { - _listener.call(this, event); - } - }; - - proto.removeEventListener = function removeEventListener(type, listener, useCapture) { - // todo - }; - })(EventTarget.prototype); - - function XMLHttpRequestEventTarget() { - EventTarget.call(this); - } - XMLHttpRequestEventTarget.prototype = Object.create(EventTarget.prototype); - - (function(proto) { - // Event handler - (function(types) { - types.forEach(function(type) { - Object.defineProperty(proto, 'on' + type, { - get: function() { - var _listener = this._eventListeners[type]; - if (typeof(_listener) !== 'function') { - return null; - } - return _listener; - }, - set: function(listener) { - if (typeof(listener) !== 'function') { - delete this._eventListeners[type]; - return listener; - } - return this._eventListeners[type] = listener; - }, - enumerable: true, - configurable: false - }); - }); - })([ - 'loadstart', - 'progress', - 'abort', - 'error', // todo - 'load', - 'timeout', // todo - 'loadend' - ]); - })(XMLHttpRequestEventTarget.prototype); - - function XMLHttpRequestUpload() { - if (!(this instanceof XMLHttpRequestUpload)) { - throw new TypeError('DOM object constructor cannot be called as a function.'); - } - XMLHttpRequestEventTarget.call(this); - } - XMLHttpRequestUpload.prototype = Object.create(XMLHttpRequestEventTarget.prototype); - - var XMLHttpRequestResponseType = [ - '', - 'arraybuffer', - 'blob', - 'document', - 'json', - 'text' - ]; + var XMLHttpRequestEventTarget = require('./xmlhttprequesteventtarget'); + var XMLHttpRequestUpload = require('./xmlhttprequestupload'); + var XMLHttpRequestResponseType = require('./xmlhttprequestresponsetype'); function XMLHttpRequest() { if (!(this instanceof XMLHttpRequest)) { @@ -128,6 +49,7 @@ this.statusText = ''; } XMLHttpRequest.prototype = Object.create(XMLHttpRequestEventTarget.prototype); + module.exports = XMLHttpRequest; (function(proto) { // Event handler @@ -366,30 +288,4 @@ }.bind(this)); }; })(XMLHttpRequest.prototype); - - function AnonXMLHttpRequest() { - if (!(this instanceof AnonXMLHttpRequest)) { - throw new TypeError('DOM object constructor cannot be called as a function'); - } - XMLHttpRequest.call(this); - } - AnonXMLHttpRequest.prototype = Object.create(XMLHttpRequest.prototype); - - function FormData() { - if (!(this instanceof FormData)) { - throw new TypeError('DOM object constructor cannot be called as a function.'); - } - } - - (function(proto) { - proto.append = function append(name, value, filename) { - // todo - }; - })(FormData.prototype); - - XMLHttpRequest.XMLHttpRequestUpload = XMLHttpRequestUpload; - XMLHttpRequest.XMLHttpRequest = XMLHttpRequest; - XMLHttpRequest.AnonXMLHttpRequest = AnonXMLHttpRequest; - XMLHttpRequest.FormData = FormData; - module.exports = XMLHttpRequest; })(); diff --git a/lib/xmlhttprequesteventtarget.js b/lib/xmlhttprequesteventtarget.js new file mode 100644 index 0000000..a9fa39c --- /dev/null +++ b/lib/xmlhttprequesteventtarget.js @@ -0,0 +1,45 @@ +(function() { + 'use strict'; + + var EventTarget = require('./eventtarget'); + + function XMLHttpRequestEventTarget() { + EventTarget.call(this); + } + XMLHttpRequestEventTarget.prototype = Object.create(EventTarget.prototype); + module.exports = XMLHttpRequestEventTarget; + + (function(proto) { + // Event handler + (function(types) { + types.forEach(function(type) { + Object.defineProperty(proto, 'on' + type, { + get: function() { + var _listener = this._eventListeners[type]; + if (typeof(_listener) !== 'function') { + return null; + } + return _listener; + }, + set: function(listener) { + if (typeof(listener) !== 'function') { + delete this._eventListeners[type]; + return listener; + } + return this._eventListeners[type] = listener; + }, + enumerable: true, + configurable: false + }); + }); + })([ + 'loadstart', + 'progress', + 'abort', + 'error', // todo + 'load', + 'timeout', // todo + 'loadend' + ]); + })(XMLHttpRequestEventTarget.prototype); +})(); diff --git a/lib/xmlhttprequestresponsetype.js b/lib/xmlhttprequestresponsetype.js new file mode 100644 index 0000000..2892c9c --- /dev/null +++ b/lib/xmlhttprequestresponsetype.js @@ -0,0 +1,13 @@ +(function() { + 'use strict'; + + var XMLHttpRequestResponseType = [ + '', + 'arraybuffer', + 'blob', + 'document', + 'json', + 'text' + ]; + module.exports = XMLHttpRequestResponseType; +})(); diff --git a/lib/xmlhttprequestupload.js b/lib/xmlhttprequestupload.js new file mode 100644 index 0000000..2aac008 --- /dev/null +++ b/lib/xmlhttprequestupload.js @@ -0,0 +1,14 @@ +(function() { + 'use strict'; + + var XMLHttpRequestEventTarget = require('./xmlhttprequesteventtarget'); + + function XMLHttpRequestUpload() { + if (!(this instanceof XMLHttpRequestUpload)) { + throw new TypeError('DOM object constructor cannot be called as a function.'); + } + XMLHttpRequestEventTarget.call(this); + } + XMLHttpRequestUpload.prototype = Object.create(XMLHttpRequestEventTarget.prototype); + module.exports = XMLHttpRequestUpload; +})();