Skip to content

Commit 4ff4d2b

Browse files
committed
Fixed #65
1 parent 1b3fb7d commit 4ff4d2b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+173
-277
lines changed

client-side/download.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function download({name, url, options}: DownloadOptions): Promise<any> {
6060
}
6161
return fetch(url, options)
6262
.then(response => {
63-
if (!response.ok) throw new Error('Received status of ' + String(response.status))
63+
if (!response.ok) throw new Error(`Received status of ${response.status}`)
6464
const sig = response.headers.get('sig')!
6565
if (typeInCache && typeInCache.sig === sig) {
6666
return response.arrayBuffer()

dist/lib/appendable-stream.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class AppendableStream extends appendable_1.default {
3232
*/
3333
add(value) {
3434
assert_1.default.integer(value);
35-
assert_1.default.between(0, value, 0x100, 'Not a byte: ' + String(value));
35+
assert_1.default.between(0, value, 0x100, `Not a byte: ${value}`);
3636
return this.addAll(new Uint8Array([value]).buffer);
3737
}
3838
/**

dist/lib/assert.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,8 @@ function integer(instance) {
4545
*/
4646
function between(lower, value, upper, message) {
4747
if (value < lower || value >= upper) {
48-
const outOfBoundsMessage = String(value) +
49-
' is not in [' +
50-
String(lower) +
51-
',' +
52-
String(upper) +
53-
')';
54-
if (message)
55-
throw new RangeError(message + ' (' + outOfBoundsMessage + ')');
56-
else
57-
throw new RangeError(outOfBoundsMessage);
48+
const outOfBoundsMessage = `${value} is not in [${lower},${upper})`;
49+
throw new RangeError(message ? `${message} (${outOfBoundsMessage})` : outOfBoundsMessage);
5850
}
5951
}
6052
/**
@@ -121,7 +113,7 @@ function throws(block, message) {
121113
*/
122114
function equal(actual, expected) {
123115
const error = () => //lazily computed
124-
new RangeError('Expected ' + util_inspect_1.inspect(expected) + ' but got ' + util_inspect_1.inspect(actual));
116+
new RangeError(`Expected ${util_inspect_1.inspect(expected)} but got ${util_inspect_1.inspect(actual)}`);
125117
if (expected) {
126118
let matchedSpecialCase = true;
127119
switch (expected.constructor) {
@@ -266,7 +258,7 @@ function equal(actual, expected) {
266258
*/
267259
function errorMessage(err, message) {
268260
instanceOf(message, String);
269-
assert(err !== null && err.message.startsWith(message), 'Message "' + (err ? err.message : 'No error thrown') + '" does not start with "' + message + '"');
261+
assert(err !== null && err.message.startsWith(message), `Message "${err ? err.message : 'No error thrown'}" does not start with "${message}"`);
270262
}
271263
//tslint:disable-next-line:prefer-object-spread
272264
exports.default = Object.assign(assert, {

dist/lib/flex-int.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ const NUMBER_OF_BYTES = new Map();
3636
*/
3737
function makeValueBuffer(value) {
3838
assert_1.default.integer(value);
39-
assert_1.default(value >= 0, String(value) + ' is negative');
39+
assert_1.default(value >= 0, `${value} is negative`);
4040
const bytes = (() => {
4141
for (const [byteCount, maxValue] of UPPER_BOUNDS) {
4242
if (maxValue > value)
4343
return byteCount;
4444
}
4545
/*istanbul ignore next*/
46-
throw new Error('Cannot represent ' + String(value)); //should never occur
46+
throw new Error(`Cannot represent ${value}`); //should never occur
4747
})();
4848
let writeValue = value - UPPER_BOUNDS.get(bytes - 1);
4949
const buffer = new Uint8Array(bytes);

dist/lib/growable-buffer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class GrowableBuffer extends appendable_1.default {
2323
assert_1.default(initialLength >= 0);
2424
}
2525
catch (e) {
26-
throw new RangeError(String(initialLength) + ' is not a valid buffer length');
26+
throw new RangeError(`${initialLength} is not a valid buffer length`);
2727
}
2828
this.buffer = new ArrayBuffer(initialLength);
2929
this.size = 0;
@@ -64,7 +64,7 @@ class GrowableBuffer extends appendable_1.default {
6464
*/
6565
add(value) {
6666
assert_1.default.integer(value);
67-
assert_1.default.between(0, value, 0x100, 'Not a byte: ' + String(value));
67+
assert_1.default.between(0, value, 0x100, `Not a byte: ${value}`);
6868
return this.addAll(new Uint8Array([value]).buffer);
6969
}
7070
/**

dist/lib/read-util.d.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,6 @@ export interface ReadResult<E> {
2525
* @return `[]`, `new Map`, `new Set`, or `{}`
2626
*/
2727
export declare function makeBaseValue(readType: RegisterableType, count?: number): any;
28-
/**
29-
* Pads a string with preceding `0` characters
30-
* so it has the desired length
31-
* (for readability)
32-
* @param str The numeric string
33-
* @param digits The target number of digits
34-
* @return `str` if str has at least enough digits,
35-
* otherwise `str` with enough zeros in front to have
36-
* the desired number of digits
37-
*/
38-
export declare function pad(str: string, digits: number): string;
3928
/**
4029
* Reads a byte from the buffer,
4130
* requires it to be `0x00` or `0xFF`,

dist/lib/read-util.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,6 @@ function makeBaseValue(readType, count) {
3333
}
3434
}
3535
exports.makeBaseValue = makeBaseValue;
36-
/**
37-
* Pads a string with preceding `0` characters
38-
* so it has the desired length
39-
* (for readability)
40-
* @param str The numeric string
41-
* @param digits The target number of digits
42-
* @return `str` if str has at least enough digits,
43-
* otherwise `str` with enough zeros in front to have
44-
* the desired number of digits
45-
*/
46-
function pad(str, digits) {
47-
if (str.length < digits)
48-
return '0'.repeat(digits - str.length) + str;
49-
else
50-
return str;
51-
}
52-
exports.pad = pad;
5336
/**
5437
* Reads a byte from the buffer,
5538
* requires it to be `0x00` or `0xFF`,
@@ -62,17 +45,17 @@ exports.pad = pad;
6245
*/
6346
function readBooleanByte(buffer, offset) {
6447
assert_1.default(buffer.byteLength > offset, exports.NOT_LONG_ENOUGH);
65-
let readValue;
48+
let value;
6649
const readByte = new Uint8Array(buffer)[offset];
6750
switch (readByte) {
6851
case 0x00:
6952
case 0xFF:
70-
readValue = Boolean(readByte);
53+
value = !!readByte;
7154
break;
7255
default:
73-
throw new Error('0x' + pad(readByte.toString(16), 2) + ' is an invalid Boolean value');
56+
throw new Error(`0x${util_inspect_1.hexByte(readByte)} is an invalid Boolean value`);
7457
}
75-
return { value: readValue, length: 1 };
58+
return { value, length: 1 };
7659
}
7760
exports.readBooleanByte = readBooleanByte;
7861
/**
@@ -135,7 +118,7 @@ function readLong(buffer, offset) {
135118
const upper = dataView.getInt32(0);
136119
const lower = dataView.getUint32(4);
137120
return {
138-
value: strint.add(strint.mul(String(upper), strint.LONG_UPPER_SHIFT), String(lower)),
121+
value: strint.add(strint.mul(`${upper}`, strint.LONG_UPPER_SHIFT), `${lower}`),
139122
length
140123
};
141124
}

dist/lib/util-inspect.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
/**
2+
* Converts a byte to a 2-digit hexadecimal string
3+
* @param n The byte value
4+
* @return `n` with a possible leading 0
5+
*/
6+
export declare const hexByte: (n: number) => string;
17
/**
28
* A simple replacement for `util.inspect()`.
3-
* Makes little effort at readability,
4-
* and cannot handle circular values.
9+
* Makes little effort at readability.
510
* Useful for generating more detailed
611
* error messages, and so that the client-side
712
* code doesn't need to pack `util` as a dependency.

dist/lib/util-inspect.js

Lines changed: 42 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
33
const jsonTypes = new Set([String, Number, Boolean, Date]);
4-
function toObject(obj) {
5-
const result = {};
6-
for (const key in obj) {
7-
/*istanbul ignore else*/
8-
if ({}.hasOwnProperty.call(obj, key))
9-
result[key] = obj[key];
10-
}
11-
return result;
12-
}
4+
/**
5+
* Converts a byte to a 2-digit hexadecimal string
6+
* @param n The byte value
7+
* @return `n` with a possible leading 0
8+
*/
9+
exports.hexByte = (n) => (n < 16 ? '0' : '') + n.toString(16);
1310
/**
1411
* A simple replacement for `util.inspect()`.
15-
* Makes little effort at readability,
16-
* and cannot handle circular values.
12+
* Makes little effort at readability.
1713
* Useful for generating more detailed
1814
* error messages, and so that the client-side
1915
* code doesn't need to pack `util` as a dependency.
2016
* @param obj The value to inspect
2117
* @return A string expressing the given value
2218
*/
2319
function inspect(obj) {
24-
return inspectWithSeen(obj, new Map);
20+
return inspectWithSeen(obj, new Set);
2521
}
2622
exports.inspect = inspect;
2723
function inspectWithSeen(obj, seen) {
@@ -32,84 +28,69 @@ function inspectWithSeen(obj, seen) {
3228
if (obj instanceof ArrayBuffer) {
3329
const castBuffer = new Uint8Array(obj);
3430
let result = '[';
35-
for (const b of castBuffer) {
36-
if (result !== '[')
31+
for (let i = 0; i < castBuffer.length; i++) {
32+
if (i)
3733
result += ', ';
38-
result += '0x' + (b < 16 ? '0' : '') + b.toString(16);
34+
result += '0x' + exports.hexByte(castBuffer[i]);
3935
}
4036
return result + ']';
4137
}
4238
//tslint:disable-next-line:strict-type-predicates
4339
if (typeof Buffer !== 'undefined' && obj instanceof Buffer) {
4440
let result = '<Buffer';
4541
for (const b of obj)
46-
result += ' ' + (b < 16 ? '0' : '') + b.toString(16);
42+
result += ' ' + exports.hexByte(b);
4743
return result + '>';
4844
}
4945
if (obj instanceof Function) {
5046
return 'Function ' + obj.name;
5147
}
5248
//obj might have circular references
53-
if (seen.get(obj))
49+
if (seen.has(obj))
5450
return '[Circular]';
55-
else
56-
seen.set(obj, 1);
51+
seen.add(obj);
52+
let firstElement = true;
5753
if (obj instanceof Set) {
5854
let result = 'Set {';
59-
const iterator = obj.values();
60-
let value = iterator.next();
61-
while (!value.done) {
62-
result += inspectWithSeen(value.value, seen);
63-
value = iterator.next();
64-
if (!value.done)
55+
for (const value of obj) {
56+
if (firstElement)
57+
firstElement = false;
58+
else
6559
result += ', ';
60+
result += inspectWithSeen(value, seen);
6661
}
67-
seen.set(obj, seen.get(obj) - 1);
62+
seen.delete(obj);
6863
return result + '}';
6964
}
7065
if (obj instanceof Map) {
7166
let result = 'Map {';
72-
const iterator = obj.entries();
73-
let value = iterator.next();
74-
while (!value.done) {
75-
result += inspectWithSeen(value.value[0], seen);
76-
result += ' => ';
77-
result += inspectWithSeen(value.value[1], seen);
78-
value = iterator.next();
79-
if (!value.done)
67+
for (const [key, value] of obj) {
68+
if (firstElement)
69+
firstElement = false;
70+
else
8071
result += ', ';
72+
result += inspectWithSeen(key, seen) + ' => ' + inspectWithSeen(value, seen);
8173
}
82-
seen.set(obj, seen.get(obj) - 1);
74+
seen.delete(obj);
8375
return result + '}';
8476
}
8577
if (obj instanceof Array) {
86-
let result = '[';
87-
const iterator = obj[Symbol.iterator]();
88-
let value = iterator.next();
89-
while (!value.done) {
90-
result += inspectWithSeen(value.value, seen);
91-
value = iterator.next();
92-
if (!value.done)
93-
result += ', ';
94-
}
95-
seen.set(obj, seen.get(obj) - 1);
96-
return result + ']';
78+
const result = `[${obj.map(item => inspectWithSeen(item, seen)).join(', ')}]`;
79+
seen.delete(obj);
80+
return result;
9781
}
98-
if (obj.constructor === Object) { //as opposed to a subclass of Object
99-
let result = '{';
100-
for (const key in obj) {
101-
/*istanbul ignore else*/
102-
if ({}.hasOwnProperty.call(obj, key)) {
103-
if (result !== '{')
104-
result += ', ';
105-
result += key + ': ' + inspectWithSeen(obj[key], seen);
106-
}
82+
const { name } = obj.constructor;
83+
let objectResult = `${name && name !== 'Object' ? name + ' ' : ''}{`;
84+
for (const key in obj) {
85+
/*istanbul ignore else*/
86+
if ({}.hasOwnProperty.call(obj, key)) {
87+
if (firstElement)
88+
firstElement = false;
89+
else
90+
objectResult += ', ';
91+
objectResult += key + ': ' + inspectWithSeen(obj[key], seen);
10792
}
108-
seen.set(obj, seen.get(obj) - 1);
109-
return result + '}';
11093
}
111-
const { name } = obj.constructor;
112-
const genericResult = (name ? name + ' ' : '') + inspectWithSeen(toObject(obj), seen);
113-
seen.set(obj, seen.get(obj) - 1);
114-
return genericResult;
94+
seen.delete(obj);
95+
return objectResult + '}';
11596
}

dist/read.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const recursiveNames = new WeakMap();
3434
//Reads a type from the specified bytes at the specified offset
3535
//Returns the type that was read and the number of bytes consumed
3636
function consumeType(typeBuffer, offset) {
37-
assert_1.default(offset >= 0, 'Offset is negative: ' + String(offset));
37+
assert_1.default(offset >= 0, `Offset is negative: ${offset}`);
3838
const castBuffer = new Uint8Array(typeBuffer);
3939
assert_1.default(typeBuffer.byteLength > offset, read_util_1.NOT_LONG_ENOUGH); //make sure there is a type byte
4040
const typeByte = castBuffer[offset];
@@ -208,7 +208,7 @@ function consumeType(typeBuffer, offset) {
208208
break;
209209
}
210210
default:
211-
throw new Error('No such type: 0x' + read_util_1.pad(castBuffer[offset].toString(16), 2));
211+
throw new Error(`No such type: 0x${util_inspect_1.hexByte(castBuffer[offset])}`);
212212
}
213213
return { value: readType, length };
214214
}

0 commit comments

Comments
 (0)