Skip to content

Commit d3ebd57

Browse files
committed
Other: Expose array implementation used with (older) browsers on util for tests
1 parent c144e73 commit d3ebd57

16 files changed

+70
-47
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# [6.3.0](https://github.com/dcodeIO/protobuf.js/releases/tag/6.3.0)
22

33
## Breaking
4+
[: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 />
45
[: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 />
56
[: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 />
67
[: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 @@
3334
[: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 />
3435

3536
## CLI
37+
[: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 />
3638
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/3e7e4fc59e6d2d6c862410b4b427fbedccdb237b) Removed type/ns alias comment in static target to not confuse jsdoc unnecessarily<br />
3739
[: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 />
3840

@@ -45,6 +47,7 @@
4547
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/7939a4bd8baca5f7e07530fc93f27911a6d91c6f) Updated README and bundler according to dynamic require calls<br />
4648

4749
## Other
50+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/b1b6a813c93da4c7459755186aa02ef2f3765c94) Updated test cases<br />
4851
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/99dc5faa7b39fdad8ebc102de4463f8deb7f48ff) Added assumptions to float test case<br />
4952
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/948ca2e3c5c62fedcd918d75539c261abf1a7347) Updated travis config to use C++11<br />
5053
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/c59647a7542cbc4292248787e5f32bb99a9b8d46) Updated / added additional LICENSE files where appropriate<br />

dist/protobuf.js

+20-14
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

21 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.

dist/runtime/protobuf.js

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

dist/runtime/protobuf.js.map

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

dist/runtime/protobuf.min.js

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

dist/runtime/protobuf.min.js.gz

4 Bytes
Binary file not shown.

dist/runtime/protobuf.min.js.map

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

src/convert.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,14 @@ convert.toMessage = function toMessage(field, value, options) {
132132
if (value) {
133133
if (field.resolvedType instanceof Type)
134134
return convert(field.resolvedType, value, new (field.resolvedType.getCtor())(), options, toMessage);
135-
if (field.type === "bytes") {
136-
if (util.Buffer && !util.Buffer.isBuffer(value))
137-
return util.Buffer.from(value); // polyfilled
138-
}
135+
if (field.type === "bytes")
136+
return util.Buffer
137+
? util.Buffer.isBuffer(value)
138+
? value
139+
: util.Buffer.from(value) // polyfilled
140+
: value instanceof util.Array
141+
? value
142+
: new util.Array(value);
139143
}
140144
break;
141145

src/reader.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ var BufferReader; // cyclic
88
var LongBits = util.LongBits,
99
utf8 = util.utf8;
1010

11-
var ArrayImpl = typeof Uint8Array !== "undefined" ? Uint8Array : Array;
12-
1311
/* istanbul ignore next */
1412
function indexOutOfRange(reader, writeLength) {
1513
return RangeError("index out of range: " + reader.pos + " + " + (writeLength || 1) + " > " + reader.len);
@@ -63,7 +61,7 @@ Reader.create = util.Buffer
6361
/** @alias Reader.prototype */
6462
var ReaderPrototype = Reader.prototype;
6563

66-
ReaderPrototype._slice = ArrayImpl.prototype.subarray || ArrayImpl.prototype.slice;
64+
ReaderPrototype._slice = util.Array.prototype.subarray || util.Array.prototype.slice;
6765

6866
/**
6967
* Reads a varint as an unsigned 32 bit value.

src/util/runtime.js

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ if (util.Buffer) {
3737
util.Buffer.from = function from(value, encoding) { return new util.Buffer(value, encoding); };
3838
}
3939

40+
/**
41+
* Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`.
42+
* @type {?function(new: Uint8Array, *)}
43+
*/
44+
util.Array = typeof Uint8Array === "undefined" ? Array : Uint8Array;
45+
4046
/**
4147
* Long.js's Long class if available.
4248
* @type {?function(new: Long)}

src/writer.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ var LongBits = util.LongBits,
99
base64 = util.base64,
1010
utf8 = util.utf8;
1111

12-
var ArrayImpl = typeof Uint8Array !== "undefined" ? Uint8Array : Array;
13-
1412
/**
1513
* Constructs a new writer operation instance.
1614
* @classdesc Scheduled writer operation.
@@ -149,12 +147,12 @@ Writer.create = util.Buffer
149147
* @returns {Uint8Array} Buffer
150148
*/
151149
Writer.alloc = function alloc(size) {
152-
return new ArrayImpl(size);
150+
return new util.Array(size);
153151
};
154152

155153
// Use Uint8Array buffer pool in the browser, just like node does with buffers
156-
if (ArrayImpl !== Array)
157-
Writer.alloc = util.pool(Writer.alloc, ArrayImpl.prototype.subarray);
154+
if (util.Array !== Array)
155+
Writer.alloc = util.pool(Writer.alloc, util.Array.prototype.subarray);
158156

159157
/** @alias Writer.prototype */
160158
var WriterPrototype = Writer.prototype;
@@ -440,7 +438,7 @@ WriterPrototype.double = function write_double(value) {
440438
return this.push(writeDouble, 8, value);
441439
};
442440

443-
var writeBytes = ArrayImpl.prototype.set
441+
var writeBytes = util.Array.prototype.set
444442
? function writeBytes_set(val, buf, pos) {
445443
buf.set(val, pos);
446444
}

types/protobuf.js.d.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// $> pbts --name protobufjs --out types/protobuf.js.d.ts src
2-
// Generated Thu, 22 Dec 2016 22:25:58 UTC
2+
// Generated Thu, 22 Dec 2016 22:48:48 UTC
33
declare module "protobufjs" {
44

55
/**
@@ -2187,6 +2187,12 @@ declare module "protobufjs" {
21872187
*/
21882188
var Buffer: () => any;
21892189

2190+
/**
2191+
* Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`.
2192+
* @type {?function(new: Uint8Array, *)}
2193+
*/
2194+
var Array: () => any;
2195+
21902196
/**
21912197
* Long.js's Long class if available.
21922198
* @type {?function(new: Long)}

0 commit comments

Comments
 (0)