Skip to content

Commit d7d3582

Browse files
committed
toBuffer opt, Long dep
1 parent ac0cbea commit d7d3582

File tree

6 files changed

+193
-252
lines changed

6 files changed

+193
-252
lines changed

ByteBuffer.js

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
/**
4747
* Constructs a new ByteBuffer.
4848
* @exports ByteBuffer
49-
* @class A full-features ByteBuffer implementation in JavaScript using typed arrays.
49+
* @class A full-featured ByteBuffer implementation in JavaScript using typed arrays.
5050
* @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
5151
* @param {boolean=} littleEndian `true` to use little endian multi byte values, defaults to `false` for big
5252
* endian.
@@ -1165,52 +1165,48 @@
11651165
*/
11661166
var TWO_PWR_28_DBL = TWO_PWR_14_DBL * TWO_PWR_14_DBL;
11671167

1168-
/**
1169-
* Writes a 64bit base 128 variable-length integer as used in protobuf.
1170-
* @param {number|Long} value Value to write
1171-
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
1172-
* @returns {!ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
1173-
* @throws {Error} If long support is not available
1174-
* @expose
1175-
*/
1176-
ByteBuffer.prototype.writeVarint64 = function(value, offset) {
1177-
if (!Long) {
1178-
throw(new Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"))
1179-
}
1180-
var advance = typeof offset === 'undefined';
1181-
offset = typeof offset !== 'undefined' ? offset : this.offset;
1182-
if (!(typeof value === 'object' && value instanceof Long)) value = Long.fromNumber(value, false);
1183-
1184-
var part0 = value.toInt() >>> 0,
1185-
part1 = value.shiftRightUnsigned(28).toInt() >>> 0,
1186-
part2 = value.shiftRightUnsigned(56).toInt() >>> 0,
1187-
size = ByteBuffer.calculateVarint64(value);
1188-
1189-
this.ensureCapacity(offset+size);
1190-
var dst = this.view;
1191-
switch (size) {
1192-
case 10: dst.setUint8(offset+9, (part2 >>> 7) | 0x80);
1193-
case 9 : dst.setUint8(offset+8, (part2 ) | 0x80);
1194-
case 8 : dst.setUint8(offset+7, (part1 >>> 21) | 0x80);
1195-
case 7 : dst.setUint8(offset+6, (part1 >>> 14) | 0x80);
1196-
case 6 : dst.setUint8(offset+5, (part1 >>> 7) | 0x80);
1197-
case 5 : dst.setUint8(offset+4, (part1 ) | 0x80);
1198-
case 4 : dst.setUint8(offset+3, (part0 >>> 21) | 0x80);
1199-
case 3 : dst.setUint8(offset+2, (part0 >>> 14) | 0x80);
1200-
case 2 : dst.setUint8(offset+1, (part0 >>> 7) | 0x80);
1201-
case 1 : dst.setUint8(offset+0, (part0 ) | 0x80);
1202-
}
1203-
dst.setUint8(offset+size-1, dst.getUint8(offset+size-1) & 0x7F);
1204-
if (advance) {
1205-
this.offset += size;
1206-
return this;
1207-
} else {
1208-
return size;
1209-
}
1210-
};
1211-
12121168
// Available with Long.js only
12131169
if (Long) {
1170+
1171+
/**
1172+
* Writes a 64bit base 128 variable-length integer as used in protobuf.
1173+
* @param {number|Long} value Value to write
1174+
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
1175+
* @returns {!ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
1176+
* @expose
1177+
*/
1178+
ByteBuffer.prototype.writeVarint64 = function(value, offset) {
1179+
var advance = typeof offset === 'undefined';
1180+
offset = typeof offset !== 'undefined' ? offset : this.offset;
1181+
if (!(typeof value === 'object' && value instanceof Long)) value = Long.fromNumber(value, false);
1182+
1183+
var part0 = value.toInt() >>> 0,
1184+
part1 = value.shiftRightUnsigned(28).toInt() >>> 0,
1185+
part2 = value.shiftRightUnsigned(56).toInt() >>> 0,
1186+
size = ByteBuffer.calculateVarint64(value);
1187+
1188+
this.ensureCapacity(offset+size);
1189+
var dst = this.view;
1190+
switch (size) {
1191+
case 10: dst.setUint8(offset+9, (part2 >>> 7) | 0x80);
1192+
case 9 : dst.setUint8(offset+8, (part2 ) | 0x80);
1193+
case 8 : dst.setUint8(offset+7, (part1 >>> 21) | 0x80);
1194+
case 7 : dst.setUint8(offset+6, (part1 >>> 14) | 0x80);
1195+
case 6 : dst.setUint8(offset+5, (part1 >>> 7) | 0x80);
1196+
case 5 : dst.setUint8(offset+4, (part1 ) | 0x80);
1197+
case 4 : dst.setUint8(offset+3, (part0 >>> 21) | 0x80);
1198+
case 3 : dst.setUint8(offset+2, (part0 >>> 14) | 0x80);
1199+
case 2 : dst.setUint8(offset+1, (part0 >>> 7) | 0x80);
1200+
case 1 : dst.setUint8(offset+0, (part0 ) | 0x80);
1201+
}
1202+
dst.setUint8(offset+size-1, dst.getUint8(offset+size-1) & 0x7F);
1203+
if (advance) {
1204+
this.offset += size;
1205+
return this;
1206+
} else {
1207+
return size;
1208+
}
1209+
};
12141210

12151211
/**
12161212
* Reads a 32bit base 128 variable-length integer as used in protobuf. Requires Long.js.
@@ -2157,29 +2153,28 @@
21572153
}
21582154
return forceCopy && !copied ? b.copy().array : b.array;
21592155
};
2160-
2161-
/**
2162-
* Returns a node Buffer compacted to contain this ByteBuffer's actual contents. Will transparently
2163-
* {@link ByteBuffer#flip} the ByteBuffer if its offset is larger than its length. Will also copy all data (not
2164-
* a reference).
2165-
* @returns {?Buffer} Compacted node Buffer or null if already destroyed
2166-
* @throws {Error} If not running inside of node
2167-
* @expose
2168-
*/
2169-
ByteBuffer.prototype.toBuffer = function() {
2170-
if (Buffer) {
2156+
2157+
// Available with node.js only
2158+
if (Buffer) {
2159+
2160+
/**
2161+
* Returns a node Buffer compacted to contain this ByteBuffer's actual contents. Will transparently
2162+
* {@link ByteBuffer#flip} the ByteBuffer if its offset is larger than its length. Will also copy all data (not
2163+
* a reference).
2164+
* @returns {?Buffer} Compacted node Buffer or null if already destroyed
2165+
* @expose
2166+
*/
2167+
ByteBuffer.prototype.toBuffer = function() {
21712168
if (this.array === null) return null;
21722169
var offset = this.offset, length = this.length;
21732170
if (offset > length) {
21742171
var temp = offset;
21752172
offset = length;
21762173
length = temp;
21772174
}
2178-
var srcView = new Uint8Array(this.array);
2179-
return new Buffer(srcView.subarray(offset, length));
2180-
}
2181-
throw(new Error("Conversion to Buffer is available under node.js only"));
2182-
};
2175+
return new Buffer(new Uint8Array(this.array).subarray(offset, length));
2176+
};
2177+
}
21832178

21842179
return ByteBuffer;
21852180
}

0 commit comments

Comments
 (0)