Skip to content

Commit

Permalink
Other: Expose array implementation used with (older) browsers on util…
Browse files Browse the repository at this point in the history
… for tests
  • Loading branch information
dcodeIO committed Dec 22, 2016
1 parent c144e73 commit d3ebd57
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 47 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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 />
Expand Down Expand Up @@ -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 />

Expand All @@ -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 />
Expand Down
34 changes: 20 additions & 14 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.

22 changes: 12 additions & 10 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: 8 additions & 4 deletions src/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 1 addition & 3 deletions src/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions src/util/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down
10 changes: 4 additions & 6 deletions src/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 7 additions & 1 deletion types/protobuf.js.d.ts
Original file line number Diff line number Diff line change
@@ -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" {

/**
Expand Down Expand Up @@ -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)}
Expand Down

0 comments on commit d3ebd57

Please sign in to comment.