Permalink
Browse files

Shrinked bundle size - a bit

  • Loading branch information...
dcodeIO committed Dec 18, 2016
1 parent fed2000 commit 47b51ec95a540681cbed0bac1b2f02fc4cf0b73d

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +99 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +8 Bytes (100%) dist/runtime/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -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.
@@ -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) {
@@ -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) {
@@ -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
@@ -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]();
@@ -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);
@@ -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);
}
@@ -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);
}
}
@@ -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;");
@@ -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.
@@ -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)
@@ -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];
@@ -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.
@@ -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.
@@ -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");
@@ -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;
}
@@ -1,6 +1,8 @@
"use strict";
module.exports = Message;
var Object_keys = Object.keys;
/**
* Constructs a new message instance.
*
@@ -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]];
}
@@ -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];
@@ -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.
@@ -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);
@@ -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
};
};
@@ -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.
@@ -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;
@@ -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 {
@@ -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);
Oops, something went wrong.

0 comments on commit 47b51ec

Please sign in to comment.