Skip to content
This repository
Browse code

get rid of require('http') fallback

  • Loading branch information...
commit 2f3d7b98d4c2b24bc215d6084fe4a9059d3d84b0 1 parent df8d107
James Halliday authored

Showing 3 changed files with 58 additions and 59 deletions. Show diff stats Hide diff stats

  1. +0 56 browser.js
  2. +56 1 index.js
  3. +2 2 package.json
56 browser.js
... ... @@ -1,56 +0,0 @@
1   -var http = module.exports;
2   -var EventEmitter = require('events').EventEmitter;
3   -var Request = require('./lib/request');
4   -
5   -http.request = function (params, cb) {
6   - if (!params) params = {};
7   - if (!params.host) params.host = window.location.host.split(':')[0];
8   - if (!params.port) params.port = window.location.port;
9   -
10   - var req = new Request(new xhrHttp, params);
11   - if (cb) req.on('response', cb);
12   - return req;
13   -};
14   -
15   -http.get = function (params, cb) {
16   - params.method = 'GET';
17   - var req = http.request(params, cb);
18   - req.end();
19   - return req;
20   -};
21   -
22   -var xhrHttp = (function () {
23   - if (typeof window === 'undefined') {
24   - throw new Error('no window object present');
25   - }
26   - else if (window.XMLHttpRequest) {
27   - return window.XMLHttpRequest;
28   - }
29   - else if (window.ActiveXObject) {
30   - var axs = [
31   - 'Msxml2.XMLHTTP.6.0',
32   - 'Msxml2.XMLHTTP.3.0',
33   - 'Microsoft.XMLHTTP'
34   - ];
35   - for (var i = 0; i < axs.length; i++) {
36   - try {
37   - var ax = new(window.ActiveXObject)(axs[i]);
38   - return function () {
39   - if (ax) {
40   - var ax_ = ax;
41   - ax = null;
42   - return ax_;
43   - }
44   - else {
45   - return new(window.ActiveXObject)(axs[i]);
46   - }
47   - };
48   - }
49   - catch (e) {}
50   - }
51   - throw new Error('ajax not supported in this browser')
52   - }
53   - else {
54   - throw new Error('ajax not supported in this browser');
55   - }
56   -})();
57 index.js
... ... @@ -1 +1,56 @@
1   -module.exports = require('http');
  1 +var http = module.exports;
  2 +var EventEmitter = require('events').EventEmitter;
  3 +var Request = require('./lib/request');
  4 +
  5 +http.request = function (params, cb) {
  6 + if (!params) params = {};
  7 + if (!params.host) params.host = window.location.host.split(':')[0];
  8 + if (!params.port) params.port = window.location.port;
  9 +
  10 + var req = new Request(new xhrHttp, params);
  11 + if (cb) req.on('response', cb);
  12 + return req;
  13 +};
  14 +
  15 +http.get = function (params, cb) {
  16 + params.method = 'GET';
  17 + var req = http.request(params, cb);
  18 + req.end();
  19 + return req;
  20 +};
  21 +
  22 +var xhrHttp = (function () {
  23 + if (typeof window === 'undefined') {
  24 + throw new Error('no window object present');
  25 + }
  26 + else if (window.XMLHttpRequest) {
  27 + return window.XMLHttpRequest;
  28 + }
  29 + else if (window.ActiveXObject) {
  30 + var axs = [
  31 + 'Msxml2.XMLHTTP.6.0',
  32 + 'Msxml2.XMLHTTP.3.0',
  33 + 'Microsoft.XMLHTTP'
  34 + ];
  35 + for (var i = 0; i < axs.length; i++) {
  36 + try {
  37 + var ax = new(window.ActiveXObject)(axs[i]);
  38 + return function () {
  39 + if (ax) {
  40 + var ax_ = ax;
  41 + ax = null;
  42 + return ax_;
  43 + }
  44 + else {
  45 + return new(window.ActiveXObject)(axs[i]);
  46 + }
  47 + };
  48 + }
  49 + catch (e) {}
  50 + }
  51 + throw new Error('ajax not supported in this browser')
  52 + }
  53 + else {
  54 + throw new Error('ajax not supported in this browser');
  55 + }
  56 +})();
4 package.json
... ... @@ -1,9 +1,9 @@
1 1 {
2 2 "name" : "http-browserify",
3   - "version" : "0.0.3",
  3 + "version" : "0.1.0",
4 4 "description" : "http module compatability for browserify",
5 5 "main" : "index.js",
6   - "browserify" : "browser.js",
  6 + "browserify" : "index.js",
7 7 "directories" : {
8 8 "lib" : ".",
9 9 "example" : "example",

0 comments on commit 2f3d7b9

Please sign in to comment.
Something went wrong with that request. Please try again.