Skip to content

Commit

Permalink
Shrinked bundle size - a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 18, 2016
1 parent fed2000 commit 47b51ec
Show file tree
Hide file tree
Showing 26 changed files with 310 additions and 330 deletions.
309 changes: 149 additions & 160 deletions dist/protobuf.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions dist/runtime/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/runtime/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/runtime/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/runtime/protobuf.min.js.map

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var Message = require("./message"),

var Type; // cyclic

var _TypeError = util._TypeError;
var TypeError = util._TypeError;

/**
* Constructs a class instance, which is also a message prototype.
Expand All @@ -15,23 +15,25 @@ var _TypeError = util._TypeError;
* @param {Type} type Reflected type
*/
function Class(type) {
return Class.create(type);
return create(type);
}

Class.create =
/**
* Constructs a new message prototype for the specified reflected type and sets up its constructor.
* @memberof Class
* @param {Type} type Reflected message type
* @param {*} [ctor] Custom constructor to set up, defaults to create a generic one if omitted
* @returns {Message} Message prototype
*/
Class.create = function create(type, ctor) {
function create(type, ctor) {
if (!Type)
Type = require("./type");
if (!(type instanceof Type))
throw _TypeError("type", "a Type");
throw TypeError("type", "a Type");
if (ctor) {
if (typeof ctor !== "function")
throw _TypeError("ctor", "a function");
throw TypeError("ctor", "a function");
} else
ctor = (function(MessageCtor) { // eslint-disable-line wrap-iife
return function Message(properties) {
Expand Down
15 changes: 8 additions & 7 deletions src/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ function decode(readerOrBuffer, length) {
limit = length === undefined ? reader.len : reader.pos + length,
message = new (this.getCtor())();
while (reader.pos < limit) {
var tag = reader.uint32(),
var tag = reader.uint32(),
wireType = tag & 7;

// End group
if (wireType === 4)
break;

var field = fields[tag >>> 3].resolve(),
type = field.resolvedType instanceof Enum ? "uint32" : field.type;
var field = fields[tag >>> 3].resolve(),
type = field.resolvedType instanceof Enum ? "uint32" : field.type,
resolvedType = field.resolvedType;

// Known fields
if (field) {
Expand All @@ -46,7 +47,7 @@ function decode(readerOrBuffer, length) {
key = util.longToHash(key);
reader.pos++; // assumes id 2
message[field.name][key] = types.basic[type] === undefined
? field.resolvedType.decode(reader, reader.uint32())
? resolvedType.decode(reader, reader.uint32())
: reader[type]();

// Repeated fields
Expand All @@ -61,13 +62,13 @@ function decode(readerOrBuffer, length) {

// Non-packed
} else if (types.basic[type] === undefined)
values.push(field.resolvedType.decode(reader, field.resolvedType.group ? undefined : reader.uint32()));
values.push(resolvedType.decode(reader, resolvedType.group ? undefined : reader.uint32()));
else
values.push(reader[type]());

// Non-repeated
} else if (types.basic[type] === undefined)
message[field.name] = field.resolvedType.decode(reader, field.resolvedType.group ? undefined : reader.uint32());
message[field.name] = resolvedType.decode(reader, resolvedType.group ? undefined : reader.uint32());
else
message[field.name] = reader[type]();

Expand Down Expand Up @@ -122,7 +123,7 @@ decode.generate = function generate(mtype) {
("k=util.longToHash(k)")
("r.pos++");
if (types.basic[type] === undefined) gen
("m%s[k]=types[%d].decode(r,r.uint32())", prop, i);
("m%s[k]=types[%d].decode(r,r.uint32())", prop, i); // can't be groups
else gen
("m%s[k]=r.%s()", prop, type);

Expand Down
18 changes: 9 additions & 9 deletions src/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ encode.generate = function generate(mtype) {

("if(m%s)", prop)
("for(var i=0;i<m%s.length;++i)", prop);
if (wireType !== undefined) gen
("w.uint32(%d).%s(m%s[i])", (field.id << 3 | wireType) >>> 0, type, prop);
else
if (wireType === undefined)
genEncodeType(gen, field, i, "m" + prop + "[i]", true);
else gen
("w.uint32(%d).%s(m%s[i])", (field.id << 3 | wireType) >>> 0, type, prop);

}

Expand All @@ -179,10 +179,10 @@ encode.generate = function generate(mtype) {

}

if (wireType !== undefined) gen
if (wireType === undefined)
genEncodeType(gen, field, i, "m" + prop);
else gen
("w.uint32(%d).%s(m%s)", (field.id << 3 | wireType) >>> 0, type, prop);
else
genEncodeType(gen, field, i, "m" + prop);

}
}
Expand All @@ -200,10 +200,10 @@ encode.generate = function generate(mtype) {
gen
("case%j:", field.name);

if (wireType !== undefined) gen
("w.uint32(%d).%s(m%s)", (field.id << 3 | wireType) >>> 0, type, prop);
else
if (wireType === undefined)
genEncodeType(gen, field, fields.indexOf(field), "m" + prop);
else gen
("w.uint32(%d).%s(m%s)", (field.id << 3 | wireType) >>> 0, type, prop);

gen
("break;");
Expand Down
13 changes: 7 additions & 6 deletions src/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Enum.className = "Enum";

var util = require("./util");

var _TypeError = util._TypeError;
var isString = util.isString,
TypeError = util._TypeError;

/**
* Constructs a new enum instance.
Expand Down Expand Up @@ -112,10 +113,10 @@ EnumPrototype.toJSON = function toJSON() {
* @throws {Error} If there is already a value with this name or id
*/
EnumPrototype.add = function(name, id) {
if (!util.isString(name))
throw _TypeError("name");
if (!isString(name))
throw TypeError("name");
if (!util.isInteger(id) || id < 0)
throw _TypeError("id", "a non-negative integer");
throw TypeError("id", "a non-negative integer");
if (this.values[name] !== undefined)
throw Error("duplicate name '" + name + "' in " + this);
if (this.getValuesById()[id] !== undefined)
Expand All @@ -132,8 +133,8 @@ EnumPrototype.add = function(name, id) {
* @throws {Error} If `name` is not a name of this enum
*/
EnumPrototype.remove = function(name) {
if (!util.isString(name))
throw _TypeError("name");
if (!isString(name))
throw TypeError("name");
if (this.values[name] === undefined)
throw Error("'" + name + "' is not a name of " + this);
delete this.values[name];
Expand Down
20 changes: 11 additions & 9 deletions src/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ var Message = require("./message"),
var Type, // cyclic
MapField; // cyclic

var _TypeError = util._TypeError;
var isObject = util.isObject,
isString = util.isString,
TypeError = util._TypeError;

/**
* Constructs a new message field instance. Note that {@link MapField|map fields} have their own class.
Expand All @@ -30,22 +32,22 @@ var _TypeError = util._TypeError;
* @param {Object} [options] Declared options
*/
function Field(name, id, type, rule, extend, options) {
if (util.isObject(rule)) {
if (isObject(rule)) {
options = rule;
rule = extend = undefined;
} else if (util.isObject(extend)) {
} else if (isObject(extend)) {
options = extend;
extend = undefined;
}
ReflectionObject.call(this, name, options);
if (!util.isInteger(id) || id < 0)
throw _TypeError("id", "a non-negative integer");
if (!util.isString(type))
throw _TypeError("type");
if (extend !== undefined && !util.isString(extend))
throw _TypeError("extend");
throw TypeError("id", "a non-negative integer");
if (!isString(type))
throw TypeError("type");
if (extend !== undefined && !isString(extend))
throw TypeError("extend");
if (rule !== undefined && !/^required|optional|repeated$/.test(rule = rule.toString().toLowerCase()))
throw _TypeError("rule", "a valid rule string");
throw TypeError("rule", "a valid rule string");

/**
* Field rule, if any.
Expand Down
10 changes: 4 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,10 @@ protobuf.tokenize = require("./tokenize");
protobuf.parse = require("./parse");

// Serialization
var Writer =
protobuf.Writer = require("./writer");
protobuf.BufferWriter = Writer.BufferWriter;
var Reader =
protobuf.Reader = require("./reader");
protobuf.BufferReader = Reader.BufferReader;
protobuf.BufferWriter = (
protobuf.Writer = require("./writer")).BufferWriter;
protobuf.BufferReader = (
protobuf.Reader = require("./reader")).BufferReader;
protobuf.encode = require("./encode");
protobuf.decode = require("./decode");
protobuf.verify = require("./verify");
Expand Down
2 changes: 1 addition & 1 deletion src/mapfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ MapFieldPrototype.resolve = function resolve() {
if (keyWireType === undefined) {
var resolved = this.parent.lookup(this.keyType);
if (!(resolved instanceof Enum))
throw Error("unresolvable map key type: " + this.keyType);
throw Error("unresolvable key type: " + this.keyType);
this.resolvedKeyType = resolved;
}

Expand Down
8 changes: 5 additions & 3 deletions src/message.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict";
module.exports = Message;

var Object_keys = Object.keys;

/**
* Constructs a new message instance.
*
Expand All @@ -14,7 +16,7 @@ module.exports = Message;
*/
function Message(properties) {
if (properties) {
var keys = Object.keys(properties);
var keys = Object_keys(properties);
for (var i = 0; i < keys.length; ++i)
this[keys[i]] = properties[keys[i]];
}
Expand Down Expand Up @@ -46,9 +48,9 @@ MessagePrototype.asJSON = function asJSON(options) {
json = {};
var keys;
if (options.defaults) {
keys = Object.keys(fields);
keys = Object_keys(fields);
} else
keys = Object.keys(this);
keys = Object_keys(this);
for (var i = 0, key; i < keys.length; ++i) {
var field = fields[key = keys[i]],
value = this[key];
Expand Down
24 changes: 13 additions & 11 deletions src/method.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Method.className = "Method";
var Type = require("./type"),
util = require("./util");

var _TypeError = util._TypeError;
var isObject = util.isObject,
isString = util.isString,
TypeError = util._TypeError;

/**
* Constructs a new service method instance.
Expand All @@ -26,19 +28,19 @@ var _TypeError = util._TypeError;
* @param {Object} [options] Declared options
*/
function Method(name, type, requestType, responseType, requestStream, responseStream, options) {
if (util.isObject(requestStream)) {
if (isObject(requestStream)) {
options = requestStream;
requestStream = responseStream = undefined;
} else if (util.isObject(responseStream)) {
} else if (isObject(responseStream)) {
options = responseStream;
responseStream = undefined;
}
if (type && !util.isString(type))
throw _TypeError("type");
if (!util.isString(requestType))
throw _TypeError("requestType");
if (!util.isString(responseType))
throw _TypeError("responseType");
if (type && !isString(type))
throw TypeError("type");
if (!isString(requestType))
throw TypeError("requestType");
if (!isString(responseType))
throw TypeError("responseType");

ReflectionObject.call(this, name, options);

Expand Down Expand Up @@ -112,9 +114,9 @@ MethodPrototype.toJSON = function toJSON() {
return {
type : this.type !== "rpc" && this.type || undefined,
requestType : this.requestType,
requestStream : this.requestStream,
requestStream : this.requestStream || undefined,
responseType : this.responseType,
responseStream : this.responseStream,
responseStream : this.responseStream || undefined,
options : this.options
};
};
Expand Down
15 changes: 8 additions & 7 deletions src/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function initNested() {
nestedError = "one of " + nestedTypes.map(function(ctor) { return ctor.name; }).join(", ");
}

var _TypeError = util._TypeError;
var TypeError = util._TypeError,
Object_keys = Object.keys;

/**
* Constructs a new namespace instance.
Expand Down Expand Up @@ -137,12 +138,12 @@ NamespacePrototype.addJSON = function addJSON(nestedJson) {
if (nestedJson) {
if (!nestedTypes)
initNested();
Object.keys(nestedJson).forEach(function(nestedName) {
Object_keys(nestedJson).forEach(function(nestedName) {
var nested = nestedJson[nestedName];
for (var j = 0; j < nestedTypes.length; ++j)
if (nestedTypes[j].testJSON(nested))
return ns.add(nestedTypes[j].fromJSON(nestedName, nested));
throw _TypeError("nested." + nestedName, "JSON for " + nestedError);
throw TypeError("nested." + nestedName, "JSON for " + nestedError);
});
}
return this;
Expand Down Expand Up @@ -170,9 +171,9 @@ NamespacePrototype.add = function add(object) {
if (!nestedTypes)
initNested();
if (!object || nestedTypes.indexOf(object.constructor) < 0)
throw _TypeError("object", nestedError);
throw TypeError("object", nestedError);
if (object instanceof Field && object.extend === undefined)
throw _TypeError("object", "an extension field when not part of a type");
throw TypeError("object", "an extension field when not part of a type");
if (!this.nested)
this.nested = {};
else {
Expand Down Expand Up @@ -209,11 +210,11 @@ NamespacePrototype.add = function add(object) {
*/
NamespacePrototype.remove = function remove(object) {
if (!(object instanceof ReflectionObject))
throw _TypeError("object", "a ReflectionObject");
throw TypeError("object", "a ReflectionObject");
if (object.parent !== this || !this.nested)
throw Error(object + " is not a member of " + this);
delete this.nested[object.name];
if (!Object.keys(this.nested).length)
if (!Object_keys(this.nested).length)
this.nested = undefined;
object.onRemove(this);
return clearCache(this);
Expand Down
Loading

0 comments on commit 47b51ec

Please sign in to comment.