Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make it work with browserify #261

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still generated code - see the updates to generate.js


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");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading a file via the fs module is not well supported in browserify without a special transform.
Fortunately for json files we can just require() them and it works across platforms.

};

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

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

GithubHandler.prototype = proto;