Other: Expose array implementation used with (older) browsers on util for tests

This commit is contained in:
dcodeIO 2016-12-22 23:50:04 +01:00
parent c144e73865
commit d3ebd5745b
16 changed files with 70 additions and 47 deletions

View File

@ -1,6 +1,7 @@
# [6.3.0](https://github.com/dcodeIO/protobuf.js/releases/tag/6.3.0)
## Breaking
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/c144e7386529b53235a4a5bdd8383bdb322f2825) Renamed asJSON option keys (enum to enums, long to longs) because enum is a reserved keyword<br />
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/5b9ade428dca2df6a13277522f2916e22092a98b) Moved JSON/Message conversion to its own source file and added Message/Type.from + test case, see [#575](https://github.com/dcodeIO/protobuf.js/issues/575)<br />
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/0b0de2458a1ade1ccd4ceb789697be13290f856b) Relicensed the library and its components to BSD-3-Clause to match the official implementation (again)<br />
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/22a64c641d4897965035cc80e92667bd243f182f) Dropped support for browser buffer entirely (is an Uint8Array anyway), ensures performance and makes things simpler<br />
@ -33,6 +34,7 @@
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/9c9a66bf393d9d6927f35a9c18abf5d1c31db912) Added asJSON bytes as Buffer, see [#566](https://github.com/dcodeIO/protobuf.js/issues/566)<br />
## CLI
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/c60cd397e902ae6851c017f2c298520b8336cbee) Annotated callback types in pbjs-generated services, see [#582](https://github.com/dcodeIO/protobuf.js/issues/582)<br />
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/3e7e4fc59e6d2d6c862410b4b427fbedccdb237b) Removed type/ns alias comment in static target to not confuse jsdoc unnecessarily<br />
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/99ad9cc08721b834a197d4bbb67fa152d7ad79aa) Made pbjs use loadSync for deterministic outputs, see [#573](https://github.com/dcodeIO/protobuf.js/issues/573)<br />
@ -45,6 +47,7 @@
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/7939a4bd8baca5f7e07530fc93f27911a6d91c6f) Updated README and bundler according to dynamic require calls<br />
## Other
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/b1b6a813c93da4c7459755186aa02ef2f3765c94) Updated test cases<br />
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/99dc5faa7b39fdad8ebc102de4463f8deb7f48ff) Added assumptions to float test case<br />
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/948ca2e3c5c62fedcd918d75539c261abf1a7347) Updated travis config to use C++11<br />
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/c59647a7542cbc4292248787e5f32bb99a9b8d46) Updated / added additional LICENSE files where appropriate<br />

34
dist/protobuf.js vendored
View File

@ -1,6 +1,6 @@
/*!
* protobuf.js v6.3.0 (c) 2016, Daniel Wirtz
* Compiled Thu, 22 Dec 2016 22:26:03 UTC
* Compiled Thu, 22 Dec 2016 22:48:52 UTC
* Licensed under the BSD-3-Clause License
* see: https://github.com/dcodeIO/protobuf.js for details
*/
@ -1188,10 +1188,14 @@ convert.toMessage = function toMessage(field, value, options) {
if (value) {
if (field.resolvedType instanceof Type)
return convert(field.resolvedType, value, new (field.resolvedType.getCtor())(), options, toMessage);
if (field.type === "bytes") {
if (util.Buffer && !util.Buffer.isBuffer(value))
return util.Buffer.from(value); // polyfilled
}
if (field.type === "bytes")
return util.Buffer
? util.Buffer.isBuffer(value)
? value
: util.Buffer.from(value) // polyfilled
: value instanceof util.Array
? value
: new util.Array(value);
}
break;
@ -3686,8 +3690,6 @@ var BufferReader; // cyclic
var LongBits = util.LongBits,
utf8 = util.utf8;
var ArrayImpl = typeof Uint8Array !== "undefined" ? Uint8Array : Array;
/* istanbul ignore next */
function indexOutOfRange(reader, writeLength) {
return RangeError("index out of range: " + reader.pos + " + " + (writeLength || 1) + " > " + reader.len);
@ -3741,7 +3743,7 @@ Reader.create = util.Buffer
/** @alias Reader.prototype */
var ReaderPrototype = Reader.prototype;
ReaderPrototype._slice = ArrayImpl.prototype.subarray || ArrayImpl.prototype.slice;
ReaderPrototype._slice = util.Array.prototype.subarray || util.Array.prototype.slice;
/**
* Reads a varint as an unsigned 32 bit value.
@ -5974,6 +5976,12 @@ if (util.Buffer) {
util.Buffer.from = function from(value, encoding) { return new util.Buffer(value, encoding); };
}
/**
* Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`.
* @type {?function(new: Uint8Array, *)}
*/
util.Array = typeof Uint8Array === "undefined" ? Array : Uint8Array;
/**
* Long.js's Long class if available.
* @type {?function(new: Long)}
@ -6286,8 +6294,6 @@ var LongBits = util.LongBits,
base64 = util.base64,
utf8 = util.utf8;
var ArrayImpl = typeof Uint8Array !== "undefined" ? Uint8Array : Array;
/**
* Constructs a new writer operation instance.
* @classdesc Scheduled writer operation.
@ -6426,12 +6432,12 @@ Writer.create = util.Buffer
* @returns {Uint8Array} Buffer
*/
Writer.alloc = function alloc(size) {
return new ArrayImpl(size);
return new util.Array(size);
};
// Use Uint8Array buffer pool in the browser, just like node does with buffers
if (ArrayImpl !== Array)
Writer.alloc = util.pool(Writer.alloc, ArrayImpl.prototype.subarray);
if (util.Array !== Array)
Writer.alloc = util.pool(Writer.alloc, util.Array.prototype.subarray);
/** @alias Writer.prototype */
var WriterPrototype = Writer.prototype;
@ -6717,7 +6723,7 @@ WriterPrototype.double = function write_double(value) {
return this.push(writeDouble, 8, value);
};
var writeBytes = ArrayImpl.prototype.set
var writeBytes = util.Array.prototype.set
? function writeBytes_set(val, buf, pos) {
buf.set(val, pos);
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
/*!
* protobuf.js v6.3.0 (c) 2016, Daniel Wirtz
* Compiled Thu, 22 Dec 2016 22:26:03 UTC
* Compiled Thu, 22 Dec 2016 22:48:52 UTC
* Licensed under the BSD-3-Clause License
* see: https://github.com/dcodeIO/protobuf.js for details
*/
@ -338,8 +338,6 @@ var BufferReader; // cyclic
var LongBits = util.LongBits,
utf8 = util.utf8;
var ArrayImpl = typeof Uint8Array !== "undefined" ? Uint8Array : Array;
/* istanbul ignore next */
function indexOutOfRange(reader, writeLength) {
return RangeError("index out of range: " + reader.pos + " + " + (writeLength || 1) + " > " + reader.len);
@ -393,7 +391,7 @@ Reader.create = util.Buffer
/** @alias Reader.prototype */
var ReaderPrototype = Reader.prototype;
ReaderPrototype._slice = ArrayImpl.prototype.subarray || ArrayImpl.prototype.slice;
ReaderPrototype._slice = util.Array.prototype.subarray || util.Array.prototype.slice;
/**
* Reads a varint as an unsigned 32 bit value.
@ -1108,6 +1106,12 @@ if (util.Buffer) {
util.Buffer.from = function from(value, encoding) { return new util.Buffer(value, encoding); };
}
/**
* Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`.
* @type {?function(new: Uint8Array, *)}
*/
util.Array = typeof Uint8Array === "undefined" ? Array : Uint8Array;
/**
* Long.js's Long class if available.
* @type {?function(new: Long)}
@ -1270,8 +1274,6 @@ var LongBits = util.LongBits,
base64 = util.base64,
utf8 = util.utf8;
var ArrayImpl = typeof Uint8Array !== "undefined" ? Uint8Array : Array;
/**
* Constructs a new writer operation instance.
* @classdesc Scheduled writer operation.
@ -1410,12 +1412,12 @@ Writer.create = util.Buffer
* @returns {Uint8Array} Buffer
*/
Writer.alloc = function alloc(size) {
return new ArrayImpl(size);
return new util.Array(size);
};
// Use Uint8Array buffer pool in the browser, just like node does with buffers
if (ArrayImpl !== Array)
Writer.alloc = util.pool(Writer.alloc, ArrayImpl.prototype.subarray);
if (util.Array !== Array)
Writer.alloc = util.pool(Writer.alloc, util.Array.prototype.subarray);
/** @alias Writer.prototype */
var WriterPrototype = Writer.prototype;
@ -1701,7 +1703,7 @@ WriterPrototype.double = function write_double(value) {
return this.push(writeDouble, 8, value);
};
var writeBytes = ArrayImpl.prototype.set
var writeBytes = util.Array.prototype.set
? function writeBytes_set(val, buf, pos) {
buf.set(val, pos);
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@ -132,10 +132,14 @@ convert.toMessage = function toMessage(field, value, options) {
if (value) {
if (field.resolvedType instanceof Type)
return convert(field.resolvedType, value, new (field.resolvedType.getCtor())(), options, toMessage);
if (field.type === "bytes") {
if (util.Buffer && !util.Buffer.isBuffer(value))
return util.Buffer.from(value); // polyfilled
}
if (field.type === "bytes")
return util.Buffer
? util.Buffer.isBuffer(value)
? value
: util.Buffer.from(value) // polyfilled
: value instanceof util.Array
? value
: new util.Array(value);
}
break;

View File

@ -8,8 +8,6 @@ var BufferReader; // cyclic
var LongBits = util.LongBits,
utf8 = util.utf8;
var ArrayImpl = typeof Uint8Array !== "undefined" ? Uint8Array : Array;
/* istanbul ignore next */
function indexOutOfRange(reader, writeLength) {
return RangeError("index out of range: " + reader.pos + " + " + (writeLength || 1) + " > " + reader.len);
@ -63,7 +61,7 @@ Reader.create = util.Buffer
/** @alias Reader.prototype */
var ReaderPrototype = Reader.prototype;
ReaderPrototype._slice = ArrayImpl.prototype.subarray || ArrayImpl.prototype.slice;
ReaderPrototype._slice = util.Array.prototype.subarray || util.Array.prototype.slice;
/**
* Reads a varint as an unsigned 32 bit value.

View File

@ -37,6 +37,12 @@ if (util.Buffer) {
util.Buffer.from = function from(value, encoding) { return new util.Buffer(value, encoding); };
}
/**
* Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`.
* @type {?function(new: Uint8Array, *)}
*/
util.Array = typeof Uint8Array === "undefined" ? Array : Uint8Array;
/**
* Long.js's Long class if available.
* @type {?function(new: Long)}

View File

@ -9,8 +9,6 @@ var LongBits = util.LongBits,
base64 = util.base64,
utf8 = util.utf8;
var ArrayImpl = typeof Uint8Array !== "undefined" ? Uint8Array : Array;
/**
* Constructs a new writer operation instance.
* @classdesc Scheduled writer operation.
@ -149,12 +147,12 @@ Writer.create = util.Buffer
* @returns {Uint8Array} Buffer
*/
Writer.alloc = function alloc(size) {
return new ArrayImpl(size);
return new util.Array(size);
};
// Use Uint8Array buffer pool in the browser, just like node does with buffers
if (ArrayImpl !== Array)
Writer.alloc = util.pool(Writer.alloc, ArrayImpl.prototype.subarray);
if (util.Array !== Array)
Writer.alloc = util.pool(Writer.alloc, util.Array.prototype.subarray);
/** @alias Writer.prototype */
var WriterPrototype = Writer.prototype;
@ -440,7 +438,7 @@ WriterPrototype.double = function write_double(value) {
return this.push(writeDouble, 8, value);
};
var writeBytes = ArrayImpl.prototype.set
var writeBytes = util.Array.prototype.set
? function writeBytes_set(val, buf, pos) {
buf.set(val, pos);
}

View File

@ -1,5 +1,5 @@
// $> pbts --name protobufjs --out types/protobuf.js.d.ts src
// Generated Thu, 22 Dec 2016 22:25:58 UTC
// Generated Thu, 22 Dec 2016 22:48:48 UTC
declare module "protobufjs" {
/**
@ -2187,6 +2187,12 @@ declare module "protobufjs" {
*/
var Buffer: () => any;
/**
* Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`.
* @type {?function(new: Uint8Array, *)}
*/
var Array: () => any;
/**
* Long.js's Long class if available.
* @type {?function(new: Long)}