Skip to content

Commit

Permalink
prepublish script for bin files, fixes #511; swallow enum value optio…
Browse files Browse the repository at this point in the history
…ns (no wrapper), fixes #510; expose zero on LongBits, fixes #508
  • Loading branch information
dcodeIO committed Dec 1, 2016
1 parent d3ae961 commit 6e5fdb6
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 62 deletions.
47 changes: 20 additions & 27 deletions dist/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/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.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@
"zuul-local": "zuul --ui tape --no-coverage --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js",
"bench": "node bench",
"all": "npm run lint && npm run test && npm run build && npm run types && npm run docs && npm run bench",
"postinstall": "npm install @types/node @types/long"
"prepublish": "node scripts/prepublish.js"
},
"optionalDependencies": {
"long": "^3.2.0"
},
"devDependencies": {
"@types/long": "^3.0.31",
"@types/node": "0.0.1",
"benchmark": "^2.1.2",
"browserify": "^13.1.1",
"bundle-collapser": "^1.2.1",
Expand Down
7 changes: 7 additions & 0 deletions scripts/prepublish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var path = require("path"),
fs = require("fs");

// ensure LF on bin files
[ path.join(__dirname, "..", "bin", "pbjs") ].forEach(function(file) {
fs.writeFileSync(file, fs.readFileSync(file).toString("utf8").replace(/\r?\n/g, "\n"), "utf8");
});
3 changes: 2 additions & 1 deletion src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@ function parse(source, root) {
var name = token;
skip("=");
var value = parseId(next(), true);
parseInlineOptions(parent.values[name] = new Number(value)); // eslint-disable-line no-new-wrappers
parent.values[name] = value;
parseInlineOptions({}); // skips enum value options
}

function parseOption(parent, token) {
Expand Down
8 changes: 6 additions & 2 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,19 @@ ServicePrototype.remove = function remove(object) {

/**
* Creates a runtime service using the specified rpc implementation.
* @param {RPCImpl} rpc RPC implementation
* @param {function(Method, Uint8Array, function)} rpc RPC implementation ({@link RPCImpl|see})
* @param {boolean} [requestDelimited=false] Whether request data is length delimited
* @param {boolean} [responseDelimited=false] Whether response data is length delimited
* @returns {Object} Runtime service
*/
ServicePrototype.create = function create(rpc, requestDelimited, responseDelimited) {
var rpcService = {};
Object.defineProperty(rpcService, "$rpc", {
value: rpc
});
this.getMethodsArray().forEach(function(method) {
rpcService[method.resolve().name] = function(request, callback) {
rpcService[method.name] = function(request, callback) {
method.resolve();
var requestData;
try {
requestData = (requestDelimited && method.resolvedRequestType.encodeDelimited(request) || method.resolvedRequestType.encode(request)).finish();
Expand Down
12 changes: 8 additions & 4 deletions src/util/longbits.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var LongBitsPrototype = LongBits.prototype;
* @memberof util.LongBits
* @type {util.LongBits}
*/
var zero = new LongBits(0, 0);
var zero = LongBits.zero = new LongBits(0, 0);

zero.toNumber = function() { return 0; };
zero.zzEncode = zero.zzDecode = function() { return this; };
Expand Down Expand Up @@ -71,9 +71,13 @@ LongBits.fromNumber = function fromNumber(value) {
* @returns {util.LongBits} Instance
*/
LongBits.from = function from(value) {
return typeof value === 'number'
? LongBits.fromNumber(value)
: new LongBits(value.low >>> 0, value.high >>> 0);
switch (typeof value) {
case 'number':
return LongBits.fromNumber(value);
case 'string':
value = util.Long.fromString(value); // throws without a long lib
}
return (value.low || value.high) && new LongBits(value.low >>> 0, value.high >>> 0) || zero;
};

/**
Expand Down
22 changes: 3 additions & 19 deletions src/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,7 @@ function writeVarint64(buf, pos, val) {
* @returns {Writer} `this`
*/
WriterPrototype.uint64 = function write_uint64(value) {
var bits;
if (typeof value === 'number')
bits = value ? LongBits.fromNumber(value) : LongBits.zero;
else if (value.low || value.high)
bits = new LongBits(value.low >>> 0, value.high >>> 0);
else
bits = LongBits.zero;
var bits = LongBits.from(value);
return this.push(writeVarint64, bits.length(), bits);
};

Expand Down Expand Up @@ -280,24 +274,14 @@ WriterPrototype.sfixed32 = function write_sfixed32(value) {
return this.push(writeFixed32, 4, value << 1 ^ value >> 31);
};

function writeFixed64(buf, pos, val) {
buf[pos++] = val.lo & 255;
buf[pos++] = val.lo >>> 8 & 255;
buf[pos++] = val.lo >>> 16 & 255;
buf[pos++] = val.lo >>> 24 ;
buf[pos++] = val.hi & 255;
buf[pos++] = val.hi >>> 8 & 255;
buf[pos++] = val.hi >>> 16 & 255;
buf[pos ] = val.hi >>> 24 ;
}

/**
* Writes a 64 bit value as fixed 64 bits.
* @param {Long|number} value Value to write
* @returns {Writer} `this`
*/
WriterPrototype.fixed64 = function write_fixed64(value) {
return this.push(writeFixed64, 8, LongBits.from(value));
var bits = LongBits.from(value);
return this.push(writeFixed32, 4, bits.hi).push(writeFixed32, 4, bits.lo);
};

/**
Expand Down
6 changes: 3 additions & 3 deletions types/protobuf.js.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/*
* protobuf.js v6.0.1 TypeScript definitions
* Generated Wed, 30 Nov 2016 22:05:17 UTC
* Generated Wed, 30 Nov 2016 22:50:15 UTC
*/
declare module protobuf {

Expand Down Expand Up @@ -1261,12 +1261,12 @@ declare module protobuf {

/**
* Creates a runtime service using the specified rpc implementation.
* @param {RPCImpl} rpc RPC implementation
* @param {function(Method, Uint8Array, function)} rpc RPC implementation ({@link RPCImpl|see})
* @param {boolean} [requestDelimited=false] Whether request data is length delimited
* @param {boolean} [responseDelimited=false] Whether response data is length delimited
* @returns {Object} Runtime service
*/
create(rpc: RPCImpl, requestDelimited?: boolean, responseDelimited?: boolean): Object;
create(rpc: (() => any), requestDelimited?: boolean, responseDelimited?: boolean): Object;

}

Expand Down

0 comments on commit 6e5fdb6

Please sign in to comment.