Comparing version 0.2.21 to 0.2.22
@@ -6,2 +6,10 @@ # Change Log | ||
### [0.2.22](https://github.com/endojs/endo/compare/@endo/zip@0.2.21...@endo/zip@0.2.22) (2022-04-12) | ||
**Note:** Version bump only for package @endo/zip | ||
### [0.2.21](https://github.com/endojs/endo/compare/@endo/zip@0.2.20...@endo/zip@0.2.21) (2022-03-07) | ||
@@ -8,0 +16,0 @@ |
{ | ||
"name": "@endo/zip", | ||
"version": "0.2.21", | ||
"version": "0.2.22", | ||
"description": "A minimal, synchronous Zip reader and writer", | ||
@@ -38,3 +38,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"@endo/eslint-config": "^0.4.6", | ||
"@endo/eslint-config": "^0.4.7", | ||
"ava": "^3.12.1", | ||
@@ -50,3 +50,3 @@ "babel-eslint": "^10.0.3", | ||
"prettier": "^1.19.1", | ||
"typescript": "~4.5.5" | ||
"typescript": "~4.6.2" | ||
}, | ||
@@ -79,3 +79,3 @@ "files": [ | ||
}, | ||
"gitHead": "9ddd58b4a26755cdba9403b0cb042b2067c69832" | ||
"gitHead": "59e511891ab67f4fa52d67141510974e22362134" | ||
} |
// @ts-check | ||
/* eslint no-bitwise: ["off"] */ | ||
/** | ||
* @type {WeakMap<BufferWriter, { | ||
* length: number, | ||
* index: number, | ||
* bytes: Uint8Array, | ||
* data: DataView, | ||
* capacity: number, | ||
* }>} | ||
*/ | ||
const privateFields = new WeakMap(); | ||
/** | ||
* @param {BufferWriter} self | ||
*/ | ||
const getPrivateFields = self => { | ||
const fields = privateFields.get(self); | ||
if (!fields) { | ||
throw new Error('BufferWriter fields are not initialized'); | ||
} | ||
return fields; | ||
}; | ||
const assertNatNumber = n => { | ||
if (Number.isSafeInteger(n) && n >= 0) { | ||
return; | ||
} | ||
throw TypeError(`must be a non-negative integer, got ${n}`); | ||
}; | ||
export class BufferWriter { | ||
@@ -11,3 +38,3 @@ /** | ||
get length() { | ||
return privateFields.get(this).length; | ||
return getPrivateFields(this).length; | ||
} | ||
@@ -19,3 +46,3 @@ | ||
get index() { | ||
return privateFields.get(this).index; | ||
return getPrivateFields(this).index; | ||
} | ||
@@ -49,3 +76,4 @@ | ||
ensureCanSeek(required) { | ||
const fields = privateFields.get(this); | ||
assertNatNumber(required); | ||
const fields = getPrivateFields(this); | ||
let capacity = fields.capacity; | ||
@@ -67,3 +95,3 @@ while (capacity < required) { | ||
seek(index) { | ||
const fields = privateFields.get(this); | ||
const fields = getPrivateFields(this); | ||
this.ensureCanSeek(index); | ||
@@ -78,3 +106,4 @@ fields.index = index; | ||
ensureCanWrite(size) { | ||
const fields = privateFields.get(this); | ||
assertNatNumber(size); | ||
const fields = getPrivateFields(this); | ||
this.ensureCanSeek(fields.index + size); | ||
@@ -87,6 +116,6 @@ } | ||
write(bytes) { | ||
const fields = privateFields.get(this); | ||
this.ensureCanWrite(bytes.length); | ||
const fields = getPrivateFields(this); | ||
this.ensureCanWrite(bytes.byteLength); | ||
fields.bytes.set(bytes, fields.index); | ||
fields.index += bytes.length; | ||
fields.index += bytes.byteLength; | ||
fields.length = Math.max(fields.index, fields.length); | ||
@@ -100,3 +129,5 @@ } | ||
writeCopy(start, end) { | ||
const fields = privateFields.get(this); | ||
assertNatNumber(start); | ||
assertNatNumber(end); | ||
const fields = getPrivateFields(this); | ||
const size = end - start; | ||
@@ -113,3 +144,3 @@ this.ensureCanWrite(size); | ||
writeUint8(value) { | ||
const fields = privateFields.get(this); | ||
const fields = getPrivateFields(this); | ||
this.ensureCanWrite(1); | ||
@@ -126,3 +157,3 @@ fields.data.setUint8(fields.index, value); | ||
writeUint16(value, littleEndian) { | ||
const fields = privateFields.get(this); | ||
const fields = getPrivateFields(this); | ||
this.ensureCanWrite(2); | ||
@@ -140,3 +171,3 @@ const index = fields.index; | ||
writeUint32(value, littleEndian) { | ||
const fields = privateFields.get(this); | ||
const fields = getPrivateFields(this); | ||
this.ensureCanWrite(4); | ||
@@ -155,3 +186,3 @@ const index = fields.index; | ||
subarray(begin, end) { | ||
const fields = privateFields.get(this); | ||
const fields = getPrivateFields(this); | ||
return fields.bytes.subarray(0, fields.length).subarray(begin, end); | ||
@@ -158,0 +189,0 @@ } |
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
63014
1338