Skip to content

Commit 055c559

Browse files
committed
make it work with browserify
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
1 parent 1c9571c commit 055c559

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

api/v3.0.0/index.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var error = require("./../../error");
1919

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

2525
var proto = {
@@ -33,8 +33,20 @@ var proto = {
3333
}
3434
};
3535

36-
["gists", "gitdata", "issues", "authorization", "orgs", "statuses", "pullRequests", "repos", "user", "events", "releases", "search", "markdown", "gitignore", "misc"].forEach(function(api) {
37-
Util.extend(proto, require("./" + api));
38-
});
36+
Util.extend(proto, require("./gists"));
37+
Util.extend(proto, require("./gitdata"));
38+
Util.extend(proto, require("./issues"));
39+
Util.extend(proto, require("./authorization"));
40+
Util.extend(proto, require("./orgs"));
41+
Util.extend(proto, require("./statuses"));
42+
Util.extend(proto, require("./pullRequests"));
43+
Util.extend(proto, require("./repos"));
44+
Util.extend(proto, require("./user"));
45+
Util.extend(proto, require("./events"));
46+
Util.extend(proto, require("./releases"));
47+
Util.extend(proto, require("./search"));
48+
Util.extend(proto, require("./markdown"));
49+
Util.extend(proto, require("./gitignore"));
50+
Util.extend(proto, require("./misc"));
3951

4052
GithubHandler.prototype = proto;

generate.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,15 @@ var main = module.exports = function(versions, tests, restore) {
242242
var sectionNames = Object.keys(sections);
243243

244244
Util.log("Writing index.js file for version " + version);
245+
246+
var scripts = sectionNames.map(function(sectionName) {
247+
return 'Util.extend(proto, require("./' + sectionName + '"));';
248+
}).join('\n');
245249
Fs.writeFileSync(Path.join(dir, "index.js"),
246250
IndexTpl
247251
.replace("<%name%>", defines.constants.name)
248252
.replace("<%description%>", defines.constants.description)
249-
.replace("<%scripts%>", "\"" + sectionNames.join("\", \"") + "\""),
253+
.replace("<%scripts%>", scripts),
250254
"utf8");
251255

252256
Object.keys(sections).forEach(function(section) {

index.js

+24-4
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,15 @@ var Client = module.exports = function(config) {
180180
this.debug = Util.isTrue(config.debug);
181181

182182
this.version = config.version;
183-
var cls = require("./api/v" + this.version);
183+
var cls;
184+
if (this.version === '3.0.0') {
185+
cls = require("./api/v3.0.0");
186+
} else {
187+
if (process.browser) {
188+
throw new Error('only version 3.0.0 is supported in the browser');
189+
}
190+
cls = require("./api/v" + this.version);
191+
}
184192
this[this.version] = new cls(this);
185193

186194
var pathPrefix = "";
@@ -765,7 +773,13 @@ var Client = module.exports = function(config) {
765773
port: port,
766774
path: path,
767775
method: method,
768-
headers: headers
776+
headers: headers,
777+
778+
// https://github.com/substack/https-browserify/pull/1
779+
scheme: protocol,
780+
781+
// https://github.com/substack/http-browserify/pull/90
782+
withCredentials: false
769783
};
770784

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

777791
function httpSendRequest() {
778-
var req = require(protocol).request(options, function(res) {
792+
var p = protocol === 'https' ? require('https') : require('http');
793+
var req = p.request(options, function(res) {
779794
if (self.debug) {
780795
console.log("STATUS: " + res.statusCode);
781796
console.log("HEADERS: " + JSON.stringify(res.headers));
782797
}
783-
res.setEncoding("utf8");
798+
if (res.setEncoding) {
799+
// This method does not exist in the browser, so we just skip it for now.
800+
// https://github.com/substack/http-browserify/issues/21
801+
// https://github.com/substack/http-browserify/pull/10
802+
res.setEncoding("utf8");
803+
}
784804
var data = "";
785805
res.on("data", function(chunk) {
786806
data += chunk;

templates/index.js.tpl

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var error = require("./../../error");
1919

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

2525
var proto = {
@@ -33,8 +33,6 @@ var proto = {
3333
}
3434
};
3535

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

4038
GithubHandler.prototype = proto;

0 commit comments

Comments
 (0)