@agoric/compartment-mapper
Advanced tools
Comparing version 0.2.0 to 0.2.1
{ | ||
"name": "@agoric/compartment-mapper", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "The compartment mapper assembles Node applications in a sandbox", | ||
@@ -5,0 +5,0 @@ "author": "Agoric", |
@@ -6,20 +6,16 @@ // @ts-check | ||
const privateFields = new WeakMap(); | ||
export class BufferReader { | ||
/** @type {Uint8Array} */ | ||
#data = null; | ||
#length = 0; | ||
#offset = 0; | ||
#index = 0; | ||
/** | ||
* @param {ArrayBuffer} data | ||
* @param {ArrayBuffer} buffer | ||
*/ | ||
constructor(data) { | ||
this.#data = new Uint8Array(data); | ||
this.#length = this.#data.length; | ||
this.#index = 0; | ||
this.#offset = 0; | ||
constructor(buffer) { | ||
const data = new Uint8Array(buffer); | ||
privateFields.set(this, { | ||
data, | ||
length: data.length, | ||
index: 0, | ||
offset: 0 | ||
}); | ||
} | ||
@@ -31,3 +27,3 @@ | ||
get length() { | ||
return this.#length; | ||
return privateFields.get(this).length; | ||
} | ||
@@ -39,3 +35,3 @@ | ||
get index() { | ||
return this.#index; | ||
return privateFields.get(this).index; | ||
} | ||
@@ -54,3 +50,4 @@ | ||
set offset(offset) { | ||
if (offset > this.#data.length) { | ||
const fields = privateFields.get(this); | ||
if (offset > fields.data.length) { | ||
throw new Error(`Cannot set offset beyond length of underlying data`); | ||
@@ -61,4 +58,4 @@ } | ||
} | ||
this.#offset = offset; | ||
this.#length = this.#data.length - this.#offset; | ||
fields.offset = offset; | ||
fields.length = fields.data.length - fields.offset; | ||
} | ||
@@ -72,3 +69,4 @@ | ||
canSeek(index) { | ||
return index >= 0 && this.#offset + index <= this.#length; | ||
const fields = privateFields.get(this); | ||
return index >= 0 && fields.offset + index <= fields.length; | ||
} | ||
@@ -81,7 +79,6 @@ | ||
assertCanSeek(index) { | ||
const fields = privateFields.get(this); | ||
if (!this.canSeek(index)) { | ||
throw new Error( | ||
`End of data reached (data length = ${ | ||
this.#length | ||
}, asked index ${index}` | ||
`End of data reached (data length = ${fields.length}, asked index ${index}` | ||
); | ||
@@ -96,5 +93,6 @@ } | ||
seek(index) { | ||
const restore = this.#index; | ||
const fields = privateFields.get(this); | ||
const restore = fields.index; | ||
this.assertCanSeek(index); | ||
this.#index = index; | ||
fields.index = index; | ||
return restore; | ||
@@ -108,4 +106,5 @@ } | ||
peek(size) { | ||
const fields = privateFields.get(this); | ||
// Clamp size. | ||
size = Math.max(0, Math.min(this.#length - this.#index, size)); | ||
size = Math.max(0, Math.min(fields.length - fields.index, size)); | ||
if (size === 0) { | ||
@@ -115,5 +114,5 @@ // in IE10, when using subarray(idx, idx), we get the array [0x00] instead of []. | ||
} | ||
const result = this.#data.subarray( | ||
this.#offset + this.#index, | ||
this.#offset + this.#index + size | ||
const result = fields.data.subarray( | ||
fields.offset + fields.index, | ||
fields.offset + fields.index + size | ||
); | ||
@@ -127,3 +126,4 @@ return result; | ||
canRead(offset) { | ||
return this.canSeek(this.#index + offset); | ||
const fields = privateFields.get(this); | ||
return this.canSeek(fields.index + offset); | ||
} | ||
@@ -137,3 +137,4 @@ | ||
assertCanRead(offset) { | ||
this.assertCanSeek(this.#index + offset); | ||
const fields = privateFields.get(this); | ||
this.assertCanSeek(fields.index + offset); | ||
} | ||
@@ -147,5 +148,6 @@ | ||
read(size) { | ||
const fields = privateFields.get(this); | ||
this.assertCanRead(size); | ||
const result = this.peek(size); | ||
this.#index += size; | ||
fields.index += size; | ||
return result; | ||
@@ -158,5 +160,6 @@ } | ||
readUint8() { | ||
const fields = privateFields.get(this); | ||
this.assertCanRead(1); | ||
const value = this.#data[this.#offset + this.#index]; | ||
this.#index += 1; | ||
const value = fields.data[fields.offset + fields.index]; | ||
fields.index += 1; | ||
return value; | ||
@@ -169,8 +172,9 @@ } | ||
readUint16LE() { | ||
const fields = privateFields.get(this); | ||
this.assertCanRead(2); | ||
const index = this.#offset + this.#index; | ||
const a = this.#data[index + 0]; | ||
const b = this.#data[index + 1]; | ||
const index = fields.offset + fields.index; | ||
const a = fields.data[index + 0]; | ||
const b = fields.data[index + 1]; | ||
const value = (b << 8) | a; | ||
this.#index += 2; | ||
fields.index += 2; | ||
return value; | ||
@@ -183,10 +187,11 @@ } | ||
readUint32LE() { | ||
const fields = privateFields.get(this); | ||
this.assertCanRead(4); | ||
const index = this.#offset + this.#index; | ||
const a = this.#data[index + 0]; | ||
const b = this.#data[index + 1]; | ||
const c = this.#data[index + 2]; | ||
const d = this.#data[index + 3]; | ||
const index = fields.offset + fields.index; | ||
const a = fields.data[index + 0]; | ||
const b = fields.data[index + 1]; | ||
const c = fields.data[index + 2]; | ||
const d = fields.data[index + 3]; | ||
const value = ((d << 24) >>> 0) + ((c << 16) | (b << 8) | a); | ||
this.#index += 4; | ||
fields.index += 4; | ||
return value; | ||
@@ -200,3 +205,4 @@ } | ||
byteAt(index) { | ||
return this.#data[this.#offset + index]; | ||
const fields = privateFields.get(this); | ||
return fields.data[fields.offset + index]; | ||
} | ||
@@ -208,3 +214,4 @@ | ||
skip(offset) { | ||
this.seek(this.#index + offset); | ||
const fields = privateFields.get(this); | ||
this.seek(fields.index + offset); | ||
} | ||
@@ -217,6 +224,7 @@ | ||
expect(expected) { | ||
if (!this.matchAt(this.#index, expected)) { | ||
const fields = privateFields.get(this); | ||
if (!this.matchAt(fields.index, expected)) { | ||
return false; | ||
} | ||
this.#index += expected.length; | ||
fields.index += expected.length; | ||
return true; | ||
@@ -231,3 +239,4 @@ } | ||
matchAt(index, expected) { | ||
if (index + expected.length > this.#length || index < 0) { | ||
const fields = privateFields.get(this); | ||
if (index + expected.length > fields.length || index < 0) { | ||
return false; | ||
@@ -247,5 +256,6 @@ } | ||
assert(expected) { | ||
const fields = privateFields.get(this); | ||
if (!this.expect(expected)) { | ||
throw new Error( | ||
`Expected ${q(expected)} at ${this.#index}, got ${this.peek( | ||
`Expected ${q(expected)} at ${fields.index}, got ${this.peek( | ||
expected.length | ||
@@ -262,3 +272,4 @@ )}` | ||
findLast(expected) { | ||
let index = this.#length - expected.length; | ||
const fields = privateFields.get(this); | ||
let index = fields.length - expected.length; | ||
while (index >= 0 && !this.matchAt(index, expected)) { | ||
@@ -265,0 +276,0 @@ index -= 1; |
// @ts-check | ||
/* eslint no-bitwise: ["off"] */ | ||
const privateFields = new WeakMap(); | ||
export class BufferWriter { | ||
/** @type {Uint8Array} */ | ||
#data = null; | ||
#index = 0; | ||
#length = 0; | ||
#capacity = 0; | ||
/** | ||
@@ -18,3 +11,3 @@ * @return {number} | ||
get length() { | ||
return this.#length; | ||
return privateFields.get(this).length; | ||
} | ||
@@ -26,3 +19,3 @@ | ||
get index() { | ||
return this.#index; | ||
return privateFields.get(this).index; | ||
} | ||
@@ -41,6 +34,9 @@ | ||
constructor(capacity = 16) { | ||
this.#data = new Uint8Array(capacity); | ||
this.#index = 0; | ||
this.#length = 0; | ||
this.#capacity = capacity; | ||
const data = new Uint8Array(capacity); | ||
privateFields.set(this, { | ||
data, | ||
index: 0, | ||
length: 0, | ||
capacity | ||
}); | ||
} | ||
@@ -52,3 +48,4 @@ | ||
ensureCanSeek(required) { | ||
let capacity = this.#capacity; | ||
const fields = privateFields.get(this); | ||
let capacity = fields.capacity; | ||
while (capacity < required) { | ||
@@ -58,5 +55,5 @@ capacity *= 2; | ||
const data = new Uint8Array(capacity); | ||
data.set(this.#data.subarray(0, this.#length)); | ||
this.#data = data; | ||
this.#capacity = capacity; | ||
data.set(fields.data.subarray(0, fields.length)); | ||
fields.data = data; | ||
fields.capacity = capacity; | ||
} | ||
@@ -68,5 +65,6 @@ | ||
seek(index) { | ||
const fields = privateFields.get(this); | ||
this.ensureCanSeek(index); | ||
this.#index = index; | ||
this.#length = Math.max(this.#index, this.#length); | ||
fields.index = index; | ||
fields.length = Math.max(fields.index, fields.length); | ||
} | ||
@@ -78,3 +76,4 @@ | ||
ensureCanWrite(size) { | ||
this.ensureCanSeek(this.#index + size); | ||
const fields = privateFields.get(this); | ||
this.ensureCanSeek(fields.index + size); | ||
} | ||
@@ -86,6 +85,7 @@ | ||
write(bytes) { | ||
const fields = privateFields.get(this); | ||
this.ensureCanWrite(bytes.length); | ||
this.#data.set(bytes, this.#index); | ||
this.#index += bytes.length; | ||
this.#length = Math.max(this.#index, this.#length); | ||
fields.data.set(bytes, fields.index); | ||
fields.index += bytes.length; | ||
fields.length = Math.max(fields.index, fields.length); | ||
} | ||
@@ -98,7 +98,8 @@ | ||
writeCopy(start, end) { | ||
const fields = privateFields.get(this); | ||
const size = end - start; | ||
this.ensureCanWrite(size); | ||
this.#data.copyWithin(this.#index, start, end); | ||
this.#index += size; | ||
this.#length = Math.max(this.#index, this.#length); | ||
fields.data.copyWithin(fields.index, start, end); | ||
fields.index += size; | ||
fields.length = Math.max(fields.index, fields.length); | ||
} | ||
@@ -110,6 +111,7 @@ | ||
writeUint8(value) { | ||
const fields = privateFields.get(this); | ||
this.ensureCanWrite(1); | ||
this.#data[this.#index] = value; | ||
this.#index += 1; | ||
this.#length = Math.max(this.#index, this.#length); | ||
fields.data[fields.index] = value; | ||
fields.index += 1; | ||
fields.length = Math.max(fields.index, fields.length); | ||
} | ||
@@ -121,8 +123,9 @@ | ||
writeUint16LE(value) { | ||
const fields = privateFields.get(this); | ||
this.ensureCanWrite(2); | ||
const index = this.#index; | ||
this.#data[index + 0] = value >>> 0; | ||
this.#data[index + 1] = value >>> 8; | ||
this.#index += 2; | ||
this.#length = Math.max(this.#index, this.#length); | ||
const index = fields.index; | ||
fields.data[index + 0] = value >>> 0; | ||
fields.data[index + 1] = value >>> 8; | ||
fields.index += 2; | ||
fields.length = Math.max(fields.index, fields.length); | ||
} | ||
@@ -134,10 +137,11 @@ | ||
writeUint32LE(value) { | ||
const fields = privateFields.get(this); | ||
this.ensureCanWrite(4); | ||
const index = this.#index; | ||
this.#data[index + 0] = value >>> 0; | ||
this.#data[index + 1] = value >>> 8; | ||
this.#data[index + 2] = value >>> 16; | ||
this.#data[index + 3] = value >>> 24; | ||
this.#index += 4; | ||
this.#length = Math.max(this.#index, this.#length); | ||
const index = fields.index; | ||
fields.data[index + 0] = value >>> 0; | ||
fields.data[index + 1] = value >>> 8; | ||
fields.data[index + 2] = value >>> 16; | ||
fields.data[index + 3] = value >>> 24; | ||
fields.index += 4; | ||
fields.length = Math.max(fields.index, fields.length); | ||
} | ||
@@ -151,3 +155,4 @@ | ||
subarray(begin, end) { | ||
return this.#data.subarray(0, this.#length).subarray(begin, end); | ||
const fields = privateFields.get(this); | ||
return fields.data.subarray(0, fields.length).subarray(begin, end); | ||
} | ||
@@ -154,0 +159,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
188404
4787
0