Skip to content

Commit

Permalink
make it work with browserify
Browse files Browse the repository at this point in the history
The main issue preventing browserify usage was computed paths
in require statements: `require("./" + someVariable)`. Those have
all been eliminated.

This patch only adds browser support for `v3.0.0` (see the throwing
code in `/index.js` where it states exactly that to understand why).
Hint: it's related to computed paths again.

There were also a number of issues in `browserify-http`, and
`browserify-https` that I needed to code around to get things working:

  - https://github.com/substack/https-browserify/pull/1
  - browserify/http-browserify#90
  - browserify/http-browserify#21
  - browserify/http-browserify#10
  • Loading branch information
jamestalmage committed Jun 30, 2015
1 parent 1c9571c commit 055c559
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
20 changes: 16 additions & 4 deletions api/v3.0.0/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var error = require("./../../error");

var GithubHandler = module.exports = function(client) {
this.client = client;
this.routes = JSON.parse(Fs.readFileSync(__dirname + "/routes.json", "utf8"));
this.routes = require("./routes.json");
};

var proto = {
Expand All @@ -33,8 +33,20 @@ var proto = {
}
};

["gists", "gitdata", "issues", "authorization", "orgs", "statuses", "pullRequests", "repos", "user", "events", "releases", "search", "markdown", "gitignore", "misc"].forEach(function(api) {
Util.extend(proto, require("./" + api));
});
Util.extend(proto, require("./gists"));
Util.extend(proto, require("./gitdata"));
Util.extend(proto, require("./issues"));
Util.extend(proto, require("./authorization"));
Util.extend(proto, require("./orgs"));
Util.extend(proto, require("./statuses"));
Util.extend(proto, require("./pullRequests"));
Util.extend(proto, require("./repos"));
Util.extend(proto, require("./user"));
Util.extend(proto, require("./events"));
Util.extend(proto, require("./releases"));
Util.extend(proto, require("./search"));
Util.extend(proto, require("./markdown"));
Util.extend(proto, require("./gitignore"));
Util.extend(proto, require("./misc"));

GithubHandler.prototype = proto;
6 changes: 5 additions & 1 deletion generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,15 @@ var main = module.exports = function(versions, tests, restore) {
var sectionNames = Object.keys(sections);

Util.log("Writing index.js file for version " + version);

var scripts = sectionNames.map(function(sectionName) {
return 'Util.extend(proto, require("./' + sectionName + '"));';
}).join('\n');
Fs.writeFileSync(Path.join(dir, "index.js"),
IndexTpl
.replace("<%name%>", defines.constants.name)
.replace("<%description%>", defines.constants.description)
.replace("<%scripts%>", "\"" + sectionNames.join("\", \"") + "\""),
.replace("<%scripts%>", scripts),
"utf8");

Object.keys(sections).forEach(function(section) {
Expand Down
28 changes: 24 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,15 @@ var Client = module.exports = function(config) {
this.debug = Util.isTrue(config.debug);

this.version = config.version;
var cls = require("./api/v" + this.version);
var cls;
if (this.version === '3.0.0') {
cls = require("./api/v3.0.0");
} else {
if (process.browser) {
throw new Error('only version 3.0.0 is supported in the browser');
}
cls = require("./api/v" + this.version);
}
this[this.version] = new cls(this);

var pathPrefix = "";
Expand Down Expand Up @@ -765,7 +773,13 @@ var Client = module.exports = function(config) {
port: port,
path: path,
method: method,
headers: headers
headers: headers,

// https://github.com/substack/https-browserify/pull/1
scheme: protocol,

// https://github.com/substack/http-browserify/pull/90
withCredentials: false
};

if (this.config.rejectUnauthorized !== undefined)
Expand All @@ -775,12 +789,18 @@ var Client = module.exports = function(config) {
console.log("REQUEST: ", options);

function httpSendRequest() {
var req = require(protocol).request(options, function(res) {
var p = protocol === 'https' ? require('https') : require('http');
var req = p.request(options, function(res) {
if (self.debug) {
console.log("STATUS: " + res.statusCode);
console.log("HEADERS: " + JSON.stringify(res.headers));
}
res.setEncoding("utf8");
if (res.setEncoding) {
// This method does not exist in the browser, so we just skip it for now.
// https://github.com/substack/http-browserify/issues/21
// https://github.com/substack/http-browserify/pull/10
res.setEncoding("utf8");
}
var data = "";
res.on("data", function(chunk) {
data += chunk;
Expand Down
6 changes: 2 additions & 4 deletions templates/index.js.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var error = require("./../../error");

var GithubHandler = module.exports = function(client) {
this.client = client;
this.routes = JSON.parse(Fs.readFileSync(__dirname + "/routes.json", "utf8"));
this.routes = require("./routes.json");
};

var proto = {
Expand All @@ -33,8 +33,6 @@ var proto = {
}
};

[<%scripts%>].forEach(function(api) {
Util.extend(proto, require("./" + api));
});
<%scripts%>

GithubHandler.prototype = proto;

0 comments on commit 055c559

Please sign in to comment.