Skip to content

Commit 6e5fdb6

Browse files
committed
prepublish script for bin files, fixes #511; swallow enum value options (no wrapper), fixes #510; expose zero on LongBits, fixes #508
1 parent d3ae961 commit 6e5fdb6

12 files changed

+57
-62
lines changed

dist/protobuf.js

+20-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js.gz

-3 Bytes
Binary file not shown.

dist/protobuf.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@
3434
"zuul-local": "zuul --ui tape --no-coverage --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js",
3535
"bench": "node bench",
3636
"all": "npm run lint && npm run test && npm run build && npm run types && npm run docs && npm run bench",
37-
"postinstall": "npm install @types/node @types/long"
37+
"prepublish": "node scripts/prepublish.js"
3838
},
3939
"optionalDependencies": {
4040
"long": "^3.2.0"
4141
},
4242
"devDependencies": {
43+
"@types/long": "^3.0.31",
44+
"@types/node": "0.0.1",
4345
"benchmark": "^2.1.2",
4446
"browserify": "^13.1.1",
4547
"bundle-collapser": "^1.2.1",

scripts/prepublish.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var path = require("path"),
2+
fs = require("fs");
3+
4+
// ensure LF on bin files
5+
[ path.join(__dirname, "..", "bin", "pbjs") ].forEach(function(file) {
6+
fs.writeFileSync(file, fs.readFileSync(file).toString("utf8").replace(/\r?\n/g, "\n"), "utf8");
7+
});

src/parse.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ function parse(source, root) {
360360
var name = token;
361361
skip("=");
362362
var value = parseId(next(), true);
363-
parseInlineOptions(parent.values[name] = new Number(value)); // eslint-disable-line no-new-wrappers
363+
parent.values[name] = value;
364+
parseInlineOptions({}); // skips enum value options
364365
}
365366

366367
function parseOption(parent, token) {

src/service.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,19 @@ ServicePrototype.remove = function remove(object) {
151151

152152
/**
153153
* Creates a runtime service using the specified rpc implementation.
154-
* @param {RPCImpl} rpc RPC implementation
154+
* @param {function(Method, Uint8Array, function)} rpc RPC implementation ({@link RPCImpl|see})
155155
* @param {boolean} [requestDelimited=false] Whether request data is length delimited
156156
* @param {boolean} [responseDelimited=false] Whether response data is length delimited
157157
* @returns {Object} Runtime service
158158
*/
159159
ServicePrototype.create = function create(rpc, requestDelimited, responseDelimited) {
160160
var rpcService = {};
161+
Object.defineProperty(rpcService, "$rpc", {
162+
value: rpc
163+
});
161164
this.getMethodsArray().forEach(function(method) {
162-
rpcService[method.resolve().name] = function(request, callback) {
165+
rpcService[method.name] = function(request, callback) {
166+
method.resolve();
163167
var requestData;
164168
try {
165169
requestData = (requestDelimited && method.resolvedRequestType.encodeDelimited(request) || method.resolvedRequestType.encode(request)).finish();

src/util/longbits.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var LongBitsPrototype = LongBits.prototype;
3535
* @memberof util.LongBits
3636
* @type {util.LongBits}
3737
*/
38-
var zero = new LongBits(0, 0);
38+
var zero = LongBits.zero = new LongBits(0, 0);
3939

4040
zero.toNumber = function() { return 0; };
4141
zero.zzEncode = zero.zzDecode = function() { return this; };
@@ -71,9 +71,13 @@ LongBits.fromNumber = function fromNumber(value) {
7171
* @returns {util.LongBits} Instance
7272
*/
7373
LongBits.from = function from(value) {
74-
return typeof value === 'number'
75-
? LongBits.fromNumber(value)
76-
: new LongBits(value.low >>> 0, value.high >>> 0);
74+
switch (typeof value) {
75+
case 'number':
76+
return LongBits.fromNumber(value);
77+
case 'string':
78+
value = util.Long.fromString(value); // throws without a long lib
79+
}
80+
return (value.low || value.high) && new LongBits(value.low >>> 0, value.high >>> 0) || zero;
7781
};
7882

7983
/**

src/writer.js

+3-19
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,7 @@ function writeVarint64(buf, pos, val) {
218218
* @returns {Writer} `this`
219219
*/
220220
WriterPrototype.uint64 = function write_uint64(value) {
221-
var bits;
222-
if (typeof value === 'number')
223-
bits = value ? LongBits.fromNumber(value) : LongBits.zero;
224-
else if (value.low || value.high)
225-
bits = new LongBits(value.low >>> 0, value.high >>> 0);
226-
else
227-
bits = LongBits.zero;
221+
var bits = LongBits.from(value);
228222
return this.push(writeVarint64, bits.length(), bits);
229223
};
230224

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

283-
function writeFixed64(buf, pos, val) {
284-
buf[pos++] = val.lo & 255;
285-
buf[pos++] = val.lo >>> 8 & 255;
286-
buf[pos++] = val.lo >>> 16 & 255;
287-
buf[pos++] = val.lo >>> 24 ;
288-
buf[pos++] = val.hi & 255;
289-
buf[pos++] = val.hi >>> 8 & 255;
290-
buf[pos++] = val.hi >>> 16 & 255;
291-
buf[pos ] = val.hi >>> 24 ;
292-
}
293-
294277
/**
295278
* Writes a 64 bit value as fixed 64 bits.
296279
* @param {Long|number} value Value to write
297280
* @returns {Writer} `this`
298281
*/
299282
WriterPrototype.fixed64 = function write_fixed64(value) {
300-
return this.push(writeFixed64, 8, LongBits.from(value));
283+
var bits = LongBits.from(value);
284+
return this.push(writeFixed32, 4, bits.hi).push(writeFixed32, 4, bits.lo);
301285
};
302286

303287
/**

types/protobuf.js.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
/*
55
* protobuf.js v6.0.1 TypeScript definitions
6-
* Generated Wed, 30 Nov 2016 22:05:17 UTC
6+
* Generated Wed, 30 Nov 2016 22:50:15 UTC
77
*/
88
declare module protobuf {
99

@@ -1261,12 +1261,12 @@ declare module protobuf {
12611261

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

12711271
}
12721272

0 commit comments

Comments
 (0)