Skip to content

Commit

Permalink
Attempted fix for #298 by abstracting require calls through a couple …
Browse files Browse the repository at this point in the history
…of layers
  • Loading branch information
dcodeIO committed Jul 10, 2015
1 parent 4887203 commit 0e61d62
Show file tree
Hide file tree
Showing 38 changed files with 721 additions and 525 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "protobuf",
"description": "Protocol Buffers for JavaScript. Finally.",
"version": "4.0.0",
"version": "4.0.1",
"main": "dist/ProtoBuf.js",
"license": "Apache-2.0",
"homepage": "http://dcode.io/",
Expand Down
29 changes: 20 additions & 9 deletions dist/ProtoBuf-light.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
/* AMD */ if (typeof define === 'function' && define["amd"])
define(["ByteBuffer"], factory);
/* CommonJS */ else if (typeof require === "function" && typeof module === "object" && module && module["exports"])
module["exports"] = factory(require("bytebuffer"));
module["exports"] = factory(require("bytebuffer"), require);
/* Global */ else
(global["dcodeIO"] = global["dcodeIO"] || {})["ProtoBuf"] = factory(global["dcodeIO"]["ByteBuffer"]);

})(this, function(ByteBuffer) {
})(this, function(ByteBuffer, nodeRequire) {
"use strict";

/**
Expand Down Expand Up @@ -57,7 +57,7 @@
* @const
* @expose
*/
ProtoBuf.VERSION = "4.0.0";
ProtoBuf.VERSION = "4.0.1";

/**
* Wire types.
Expand Down Expand Up @@ -314,8 +314,7 @@
// redundant modules.
// * Works for browserify because node-process does not implement toString
// https://github.com/defunctzombie/node-process
typeof process === 'object' &&
process+'' === '[object process]'
typeof process === 'object' && process+'' === '[object process]'
);

/**
Expand Down Expand Up @@ -357,15 +356,15 @@
callback = null;
if (Util.IS_NODE) {
if (callback) {
require("fs").readFile(path, function(err, data) {
Util.require("fs").readFile(path, function(err, data) {
if (err)
callback(null);
else
callback(""+data);
});
} else
try {
return require("fs").readFileSync(path);
return Util.require("fs").readFileSync(path);
} catch (e) {
return null;
}
Expand Down Expand Up @@ -395,6 +394,18 @@
}
};

/**
* Requires a node module.
* @function
* @param {string} path
* @returns {*}
* @throws Error If node require is not supported
* @expose
*/
Util.require = Util.IS_NODE
? function(path) { return nodeRequire(path); }
: function(path) { throw Error("node require is not supported by this platform")};

/**
* Converts a string to camel case.
* @param {string} str
Expand Down Expand Up @@ -3700,7 +3711,7 @@
BuilderPrototype["import"] = function(json, filename) {
if (typeof filename === 'string') {
if (ProtoBuf.Util.IS_NODE)
filename = require("path")['resolve'](filename);
filename = ProtoBuf.Util.require("path")['resolve'](filename);
if (this.files[filename] === true) {
this.reset();
return this; // Skip duplicate imports
Expand All @@ -3709,7 +3720,7 @@
} else if (typeof filename === 'object') { // Assume object with root, filename.
var root = filename.root
if (ProtoBuf.Util.IS_NODE)
root = require("path")['resolve'](root);
root = ProtoBuf.Util.require("path")['resolve'](root);
var delim = '/';
if (root.indexOf("\\") >= 0 || filename.file.indexOf("\\") >= 0) delim = '\\';
var fname = [root, filename.file].join(delim);
Expand Down
173 changes: 87 additions & 86 deletions dist/ProtoBuf-light.min.js

Large diffs are not rendered by default.

Binary file modified dist/ProtoBuf-light.min.js.gz
Binary file not shown.
6 changes: 3 additions & 3 deletions dist/ProtoBuf-light.min.map

Large diffs are not rendered by default.

29 changes: 20 additions & 9 deletions dist/ProtoBuf.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
/* AMD */ if (typeof define === 'function' && define["amd"])
define(["ByteBuffer"], factory);
/* CommonJS */ else if (typeof require === "function" && typeof module === "object" && module && module["exports"])
module["exports"] = factory(require("bytebuffer"));
module["exports"] = factory(require("bytebuffer"), require);
/* Global */ else
(global["dcodeIO"] = global["dcodeIO"] || {})["ProtoBuf"] = factory(global["dcodeIO"]["ByteBuffer"]);

})(this, function(ByteBuffer) {
})(this, function(ByteBuffer, nodeRequire) {
"use strict";

/**
Expand Down Expand Up @@ -57,7 +57,7 @@
* @const
* @expose
*/
ProtoBuf.VERSION = "4.0.0";
ProtoBuf.VERSION = "4.0.1";

/**
* Wire types.
Expand Down Expand Up @@ -314,8 +314,7 @@
// redundant modules.
// * Works for browserify because node-process does not implement toString
// https://github.com/defunctzombie/node-process
typeof process === 'object' &&
process+'' === '[object process]'
typeof process === 'object' && process+'' === '[object process]'
);

/**
Expand Down Expand Up @@ -357,15 +356,15 @@
callback = null;
if (Util.IS_NODE) {
if (callback) {
require("fs").readFile(path, function(err, data) {
Util.require("fs").readFile(path, function(err, data) {
if (err)
callback(null);
else
callback(""+data);
});
} else
try {
return require("fs").readFileSync(path);
return Util.require("fs").readFileSync(path);
} catch (e) {
return null;
}
Expand Down Expand Up @@ -395,6 +394,18 @@
}
};

/**
* Requires a node module.
* @function
* @param {string} path
* @returns {*}
* @throws Error If node require is not supported
* @expose
*/
Util.require = Util.IS_NODE
? function(path) { return nodeRequire(path); }
: function(path) { throw Error("node require is not supported by this platform")};

/**
* Converts a string to camel case.
* @param {string} str
Expand Down Expand Up @@ -4695,7 +4706,7 @@
BuilderPrototype["import"] = function(json, filename) {
if (typeof filename === 'string') {
if (ProtoBuf.Util.IS_NODE)
filename = require("path")['resolve'](filename);
filename = ProtoBuf.Util.require("path")['resolve'](filename);
if (this.files[filename] === true) {
this.reset();
return this; // Skip duplicate imports
Expand All @@ -4704,7 +4715,7 @@
} else if (typeof filename === 'object') { // Assume object with root, filename.
var root = filename.root
if (ProtoBuf.Util.IS_NODE)
root = require("path")['resolve'](root);
root = ProtoBuf.Util.require("path")['resolve'](root);
var delim = '/';
if (root.indexOf("\\") >= 0 || filename.file.indexOf("\\") >= 0) delim = '\\';
var fname = [root, filename.file].join(delim);
Expand Down
Loading

0 comments on commit 0e61d62

Please sign in to comment.