Skip to content

Commit b798214

Browse files
committed
Fixed #51
1 parent df53202 commit b798214

File tree

182 files changed

+556
-2122
lines changed

Some content is hidden

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

182 files changed

+556
-2122
lines changed

compiled/download.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiled/upload-download.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiled/upload.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/lib/buffer-string.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ exports.toString = toString;
8484
* The inverse of [[toString]].
8585
* @param str The string to convert
8686
*/
87-
//Convert a string to UTF-8 bytes
8887
function fromString(str) {
8988
assert_1.default.instanceOf(str, String);
9089
//Taken from http://stackoverflow.com/a/18729931

dist/lib/pointers.d.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

dist/lib/pointers.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

dist/lib/sha-256.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ const K = new Uint32Array([
1212
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
1313
]);
1414
const rightRotate = (value, bits) => (value >>> bits) | (value << (32 - bits));
15+
/**
16+
* Computes a SHA-256 hash of the binary data,
17+
* output as an `ArrayBuffer`.
18+
* Implementation details mostly copied from
19+
* [Wikipedia](https://en.wikipedia.org/wiki/SHA-2#Pseudocode).
20+
* @param input The input data
21+
*/
1522
exports.default = (input) => {
1623
const lBytes = input.byteLength;
1724
const l = lBytes * 8; //not using bitwise math in case this overflows a 32-bit integer

dist/lib/write-iterable.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export interface IterableWriteParams<E> {
55
buffer: GrowableBuffer;
66
value: Iterable<E>;
77
length: number;
8-
root: boolean;
98
}
10-
declare const _default: <E>({type, buffer, value, length, root}: IterableWriteParams<E>) => void;
9+
declare const _default: <E>({type, buffer, value, length}: IterableWriteParams<E>) => void;
1110
export default _default;

dist/lib/write-iterable.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
33
const assert_1 = require("./assert");
44
const flexInt = require("./flex-int");
55
const growable_buffer_1 = require("./growable-buffer");
6-
const pointers_1 = require("./pointers");
76
/**
87
* Writes any iterable value to the buffer.
98
* Used by [[ArrayType]] and [[SetType]].
@@ -14,10 +13,9 @@ const pointers_1 = require("./pointers");
1413
* @param length The number of elements in `value`
1514
* @throws If the value doesn't match the type, e.g. `new sb.ArrayType().writeValue(buffer, 23)`
1615
*/
17-
exports.default = ({ type, buffer, value, length, root }) => {
16+
exports.default = ({ type, buffer, value, length }) => {
1817
assert_1.default.instanceOf(buffer, growable_buffer_1.default);
1918
buffer.addAll(flexInt.makeValueBuffer(length));
2019
for (const instance of value)
21-
type.writeValue(buffer, instance, false);
22-
pointers_1.setPointers({ buffer, root });
20+
type.writeValue(buffer, instance);
2321
};

dist/read.js

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,13 @@ const pointerReads = new WeakMap();
127127
/*
128128
Reads a value from the specified bytes at the specified offset, given a type
129129
Returns the value that was read and the number of bytes consumed (excepting any values being pointed to)
130-
Pointer start refers to the position in the buffer where the root value starts
131-
(pointers will be relative to this location)
132130
*/
133-
function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue }) {
131+
function consumeValue({ buffer, offset, type: readType, baseValue }) {
134132
let readValue, length;
135133
switch (readType.constructor) {
136134
case t.ByteType: {
137135
length = 1;
138-
assert_1.default(buffer.byteLength >= offset + length, NOT_LONG_ENOUGH);
136+
assert_1.default(buffer.byteLength > offset, NOT_LONG_ENOUGH);
139137
readValue = new Int8Array(buffer)[offset]; //endianness doesn't matter because there is only 1 byte
140138
break;
141139
}
@@ -304,7 +302,7 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
304302
const castType = readType;
305303
readValue = baseValue || makeBaseValue(castType);
306304
for (let i = 0; i < castType.length; i++) {
307-
const element = consumeValue({ buffer, pointerStart, offset: offset + length, type: castType.type });
305+
const element = consumeValue({ buffer, offset: offset + length, type: castType.type });
308306
length += element.length;
309307
readValue[i] = element.value;
310308
}
@@ -315,7 +313,7 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
315313
const castType = readType;
316314
readValue = baseValue || makeBaseValue(castType);
317315
for (const field of castType.fields) {
318-
const readField = consumeValue({ buffer, pointerStart, offset: offset + length, type: field.type });
316+
const readField = consumeValue({ buffer, offset: offset + length, type: field.type });
319317
readValue[field.name] = readField.value;
320318
length += readField.length;
321319
}
@@ -328,7 +326,7 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
328326
const castType = readType;
329327
readValue = baseValue || makeBaseValue(castType, arrayLength);
330328
for (let i = 0; i < arrayLength; i++) {
331-
const element = consumeValue({ buffer, pointerStart, offset: offset + length, type: castType.type });
329+
const element = consumeValue({ buffer, offset: offset + length, type: castType.type });
332330
length += element.length;
333331
readValue[i] = element.value;
334332
}
@@ -341,7 +339,7 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
341339
const castType = readType;
342340
readValue = baseValue || makeBaseValue(castType);
343341
for (let i = 0; i < setSize; i++) {
344-
const element = consumeValue({ buffer, pointerStart, offset: offset + length, type: castType.type });
342+
const element = consumeValue({ buffer, offset: offset + length, type: castType.type });
345343
length += element.length;
346344
readValue.add(element.value);
347345
}
@@ -353,9 +351,9 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
353351
const castType = readType;
354352
readValue = baseValue || makeBaseValue(castType);
355353
for (let i = 0; i < size.value; i++) {
356-
const keyElement = consumeValue({ buffer, pointerStart, offset: offset + length, type: castType.keyType });
354+
const keyElement = consumeValue({ buffer, offset: offset + length, type: castType.keyType });
357355
length += keyElement.length;
358-
const valueElement = consumeValue({ buffer, pointerStart, offset: offset + length, type: castType.valueType });
356+
const valueElement = consumeValue({ buffer, offset: offset + length, type: castType.valueType });
359357
length += valueElement.length;
360358
readValue.set(keyElement.value, valueElement.value);
361359
}
@@ -376,7 +374,6 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
376374
const typeIndex = new Uint8Array(buffer)[offset];
377375
const subValue = consumeValue({
378376
buffer,
379-
pointerStart,
380377
offset: offset + length,
381378
type: readType.types[typeIndex]
382379
});
@@ -395,7 +392,6 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
395392
const constructor = constructorRegistry.get(typeConstructor.name);
396393
const subValue = consumeValue({
397394
buffer,
398-
pointerStart,
399395
offset: offset + length,
400396
type: castType.constructorTypes[typeIndex].type,
401397
baseValue: new constructor
@@ -416,7 +412,7 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
416412
readRecursives.set(buffer, bufferReadRecursives);
417413
}
418414
bufferReadRecursives.set(offset + length, readValue);
419-
length += consumeValue({ buffer, pointerStart, offset: offset + length, type: subType, baseValue: readValue }).length;
415+
length += consumeValue({ buffer, offset: offset + length, type: subType, baseValue: readValue }).length;
420416
}
421417
else {
422418
const indexOffset = readFlexInt(buffer, offset + length);
@@ -433,7 +429,6 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
433429
if (nonNull) {
434430
const subValue = consumeValue({
435431
buffer,
436-
pointerStart,
437432
offset: offset + length,
438433
type: readType.type
439434
});
@@ -445,8 +440,18 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
445440
break;
446441
}
447442
case t.PointerType: {
448-
length = 4;
449-
assert_1.default(buffer.byteLength >= offset + length, NOT_LONG_ENOUGH);
443+
length = 1;
444+
let explicitValue = true;
445+
while (true) {
446+
const offsetDiff = readFlexInt(buffer, offset);
447+
if (!offsetDiff.value)
448+
break;
449+
if (explicitValue) {
450+
({ length } = offsetDiff);
451+
explicitValue = false;
452+
}
453+
offset -= offsetDiff.value;
454+
}
450455
let bufferPointerReads = pointerReads.get(buffer);
451456
if (!bufferPointerReads) {
452457
bufferPointerReads = new Map;
@@ -458,17 +463,16 @@ function consumeValue({ buffer, pointerStart, offset, type: readType, baseValue
458463
bufferTypePointerReads = new Map;
459464
bufferPointerReads.set(castType, bufferTypePointerReads);
460465
}
461-
const location = new DataView(buffer).getUint32(offset);
462-
if (bufferTypePointerReads.has(location))
463-
readValue = bufferTypePointerReads.get(location);
464-
else {
465-
readValue = consumeValue({
466+
readValue = bufferTypePointerReads.get(offset);
467+
if (readValue === undefined) {
468+
const explicitRead = consumeValue({
466469
buffer,
467-
pointerStart,
468-
offset: pointerStart + location,
470+
offset: offset + length,
469471
type: castType.type
470-
}).value;
471-
bufferTypePointerReads.set(location, readValue);
472+
});
473+
readValue = explicitRead.value;
474+
length += explicitRead.length;
475+
bufferTypePointerReads.set(offset, readValue);
472476
}
473477
break;
474478
}
@@ -564,7 +568,6 @@ function consumeType(typeBuffer, offset) {
564568
const valueLocation = offset + length;
565569
const enumValue = consumeValue({
566570
buffer: typeBuffer,
567-
pointerStart: valueLocation,
568571
offset: valueLocation,
569572
type: valueType.value
570573
});
@@ -726,8 +729,7 @@ function value(params) {
726729
const readValue = consumeValue({
727730
buffer,
728731
offset,
729-
type: readType,
730-
pointerStart: offset
732+
type: readType
731733
}).value;
732734
//no length validation because bytes being pointed to don't get counted in the length
733735
return readValue;

0 commit comments

Comments
 (0)